diff --git a/package.json b/package.json index 4ded060..85f2ec5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "goshawkdb", - "version": "0.2.3", + "version": "0.2.4", "description": "A javascript client for goshawkdb.", "main": "./index.js", "scripts": { diff --git a/src/connection.js b/src/connection.js index 26c72bb..62cd161 100644 --- a/src/connection.js +++ b/src/connection.js @@ -19,7 +19,7 @@ class Connection { /** @private used in logging to distinguish different connections */ this.connectionId = (nextConnectionNumber++) /** @private */ - this.link = new MsgpackConnection(url, ("000" + this.connectionId).substr(-3)) + this.link = new MsgpackConnection(url, ("000" + this.connectionId).slice(-3)) /** * The product and version information we sent to the server during the initial connection handshake. @@ -93,7 +93,7 @@ class Connection { // now we're properly connected this.messageHandler = null this.cache = new ObjectCache(this.namespace) - console.info(`Connection ${this.connectionId}: Connected to goshawkdb.`, this.serverInfo, this.clientInfo, this.namespace, this.roots) + console.info("Connection %s: Connected to goshawkdb.", this.connectionId, this.serverInfo, this.clientInfo, this.namespace, this.roots) this._onConnectionNegotiated(this) } @@ -113,8 +113,11 @@ class Connection { console.warn(`Connection ${this.connectionId}: No handler found for message`, data) } }, - // on end - if the connection stops for any reason (error, or deliberate) before it resolves then it rejects. - (e) => reject(e), + // on end - if the connection stops for any reason (error, or deliberate). + (e) => { + reject(e) + this.onclose(e) + }, // on open we start the handshake by sending the client info. () => this.link.send(this.clientInfo), connectionOptions @@ -122,6 +125,12 @@ class Connection { }) } + /** + * A callback that can be set to be told when the connection closes, either in error, or deliberately. + * @param closeEvent the websocket event explaining why the connection has closed. + */ + onclose(closeEvent) {} + /** * Queues a transaction for running, then ensures that transaction processing is happening. * The returned promise resolves with the value returned by the fn once the transaction has committed. The fn may be diff --git a/src/errors.js b/src/errors.js index 269da0a..89b40d7 100644 --- a/src/errors.js +++ b/src/errors.js @@ -3,11 +3,15 @@ exports.Throwable = class Throwable extends Error { /** @param {string} message a human readable description of this error. */ constructor(message) { super(message) - /** the type of this error. - * @type {string} */ + /** + * The type of this error. + * @type {string} + */ this.name = this.constructor.name - /** human readable description of this error. - * @type {string} */ + /** + * Human readable description of this error. + * @type {string} + */ this.message = message } } diff --git a/src/msgpack-connection.js b/src/msgpack-connection.js index e4b1c6a..be7503e 100644 --- a/src/msgpack-connection.js +++ b/src/msgpack-connection.js @@ -15,11 +15,11 @@ class MsgpackConnection { codec: msgpack.createCodec({binarraybuffer: true}) } // all the callbacks! - this.onOpen = null - this.onEnd = null - this.onMessage = null - this.onClose = null - this.onError = null + this.onOpen = noop + this.onEnd = noop + this.onMessage = noop + this.onClose = noop + this.onError = noop } connect(onMessage, onEnd, onOpen, connectionOptions) { @@ -30,43 +30,32 @@ class MsgpackConnection { this.onEnd = onEnd this.onOpen = onOpen const websocket = this.websocket = new WebSocket(this.url, undefined, connectionOptions) - websocket.binaryType = 'arraybuffer' - websocket.onopen = (evt) => { - console.debug(`Connection ${this.connectionLabel}: Connection Open`) - if (this.onOpen) { + Object.assign(websocket, { + binaryType: 'arraybuffer', + onopen: (evt) => { + console.debug("Connection %s: Connection Open", this.connectionLabel) this.onOpen(evt) - } - } - websocket.onclose = (evt) => { - console.debug(`Connection ${this.connectionLabel}: Connection Closed`, evt.code, evt.reason) - if (this.onEnd) { + }, + onclose: (evt) => { + console.debug("Connection %s: Connection Closed", this.connectionLabel, evt.code, evt.reason) this.onEnd(evt) - } - if (this.onClose) { this.onClose(evt) - } - } - websocket.onerror = (evt) => { - console.error(`Connection ${this.connectionLabel}: Connection Error`, evt.code, evt.reason) - if (this.onEnd) { + }, + onerror: (evt) => { + console.error("Connection %s: Connection Error", this.connectionLabel, evt.code, evt.reason) this.onEnd(evt) - } - if (this.onError) { this.onError(evt) - } - } - websocket.onmessage = (messageEvent) => { - const data = msgpack.decode(new Uint8Array(messageEvent.data)); - console.debug(`${this.connectionLabel} <`, data) - - if (this.onMessage) { + }, + onmessage: (messageEvent) => { + const data = msgpack.decode(new Uint8Array(messageEvent.data)); + console.debug("%s <", this.connectionLabel, data) this.onMessage(data) } - } + }) } send(message) { - console.debug(`${this.connectionLabel} >`, message) + console.debug("%s >", this.connectionLabel, message) this.websocket.send(msgpack.encode(message, this.options)) } @@ -99,3 +88,5 @@ class MsgpackConnection { } module.exports = MsgpackConnection + +function noop() {} \ No newline at end of file diff --git a/src/objectcache.js b/src/objectcache.js index d1c686f..e2b682a 100644 --- a/src/objectcache.js +++ b/src/objectcache.js @@ -135,8 +135,8 @@ class ObjectCacheEntry { } create(value, refs) { - if (value instanceof ArrayBuffer != true) { - throw new TypeError("values should be array buffers : " + value) + if (value instanceof ArrayBuffer !== true) { + throw new TypeError("Values should be array buffers : " + value) } checkRefs(refs) this.hasBeenCreated = true diff --git a/src/uint64.js b/src/uint64.js index ff8db7a..c47afd4 100644 --- a/src/uint64.js +++ b/src/uint64.js @@ -10,24 +10,26 @@ class Uint64 { */ constructor(uintArray = new Uint8Array(8)) { if (uintArray instanceof Uint8Array === false) { - throw new TypeError("Uint64 source must be a Uint8Array, Uint64.from(bytes), Uint64.fromTypedArray, Uint64.fromArrayBuffer might suit your needs better.") + throw new TypeError("Uint64 source must be a Uint8Array. Uint64.from(bytes), Uint64.fromTypedArray, Uint64.fromArrayBuffer might suit your needs better.") } /** * @private * @type {Uint8Array} */ this.data = uintArray + } - /** - * An arraybuffer representation of this value. - * @type {ArrayBuffer} - */ - this.buffer = this.data.buffer + /** + * An arraybuffer representation of this value. + * @type {ArrayBuffer} + */ + get buffer() { + return this.data.buffer } /** * Creates a Uint64 from bytes - * @param {number[]} bytes + * @param {...number} bytes the byte values to initialise a new Uint64 with. Only the last 8 bytes are taken. * @returns {Uint64} */ static from(...bytes) { @@ -82,8 +84,8 @@ class Uint64 { } /** - * Set the last last bytes.length bytes. - * @param {number[]} bytes the bytes to set. + * Set the last bytes.length bytes. + * @param {...number} bytes the bytes to set. */ set(...bytes) { if (bytes[0] instanceof Uint64) { @@ -96,7 +98,7 @@ class Uint64 { } /** - * Returns an array buffer where this Uint64 is the first 8 bytes and + * Returns a new array buffer where this Uint64 is the first 8 bytes and * the passed buffer is the subsequent bytes. * @param {ArrayBuffer|Buffer|TypedArray} buffer the subsequent bytes. * @returns {ArrayBuffer} @@ -110,7 +112,7 @@ class Uint64 { } /** - * A string representation of this Uint64. This string representation is for debugging purposes + * A string representation of this Uint64. The exact string representation is for debugging purposes * and does not form part of the public API. */ toString() {