Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "goshawkdb",
"version": "0.2.3",
"version": "0.2.4",
"description": "A javascript client for goshawkdb.",
"main": "./index.js",
"scripts": {
Expand Down
17 changes: 13 additions & 4 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -113,15 +113,24 @@ 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
)
})
}

/**
* 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
Expand Down
12 changes: 8 additions & 4 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
55 changes: 23 additions & 32 deletions src/msgpack-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if only WebSocket had a big o

this.onEnd = noop
this.onMessage = noop
this.onClose = noop
this.onError = noop
}

connect(onMessage, onEnd, onOpen, connectionOptions) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a part of me that really wants onOpen, onEnd... but I can think of no justification, at all.

Expand All @@ -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))
}

Expand Down Expand Up @@ -99,3 +88,5 @@ class MsgpackConnection {
}

module.exports = MsgpackConnection

function noop() {}
4 changes: 2 additions & 2 deletions src/objectcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 13 additions & 11 deletions src/uint64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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}
Expand All @@ -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() {
Expand Down