Socket
Socket
Sign inDemoInstall

jsonbird

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonbird

JSON-RPC 2.0 client/server/peer for any reliable transport. Inter-process communication. REST. WebSocket. WebWorker. Out of order messages or in-order byte stream


Version published
Weekly downloads
1.7K
increased by49.95%
Maintainers
1
Weekly downloads
 
Created
Source

JSONBird

JSONBird is a Duplex stream which makes it easy to create a flexible JSON-RPC 2.0 client or server (or a bidirectional combination) over any reliable transport. You can use out of order messages or in-order byte stream.",

It can parse/emit JSON strings or plain old javascript objects.

JSONBird does not care what transport is used, for example you could use:

  • Synchronous HTTP request/responses
  • HTTP polling
  • WebSocket
  • TCP
  • SCTP
  • postMessage() between a Web Worker or iframe's in a browser (without having to serialize to JSON)
  • direct in-memory communication between two instances (for example, for test stubs)
  • message port's for multiprocess browser extensions

Kind: global class

new JSONBird([optionsArg])

ParamTypeDefaultDescription
[optionsArg]ObjectThe effect of these options are documented at the getter/setter with the same name
[optionsArg.sendErrorStack]booleanfalse
[optionsArg.writableMode]string"json-stream"
[optionsArg.readableMode]string"json-stream"
[optionsArg.firstRequestId]number0The first request id to use
[optionsArg.sessionId]string"randomString()"
[optionsArg.endOfJSONWhitespace=]string
[optionsArg.endOnFinish]booleantrue
[optionsArg.finishOnEnd]booleantrue

jsonBird.sessionId ⇒ string

This is a string that will be appended to the id of all request objects that we send out.

This is useful in case the same transport is reused, to make sure that we do not parse any stale response objects. By default, this is set to a short unique id (using the "shortid" module)

Kind: instance property of JSONBird

jsonBird.writableMode ⇒ string

Determines how to JSONBird interprets messages that are written to the writable side of this Duplex stream.

If the value is "object", the writable stream is put in object mode and a plain old javascript object is expected.

For example:

rpc.write({jsonrpc: '2.0', method: 'subtract', params: [42, 23], id: 0})

If the value is "json-message", the writable stream is put in object mode and a json string or a Buffer (utf8) is expected,

For example:

rpc.write('{"jsonrpc":"2.0","method":"subtract","params":[42,23],"id":0}')
rpc.write('{"jsonrpc"') // invalid json string, a `protocolError` event will emitted

If the value is "json-stream", a streaming sequence of json strings or Buffers (utf8) are expected.

For example:

// will wait until more data arrives to complete the json string:
rpc.write('{"jsonrpc":"2.0","method":"subt')
rpc.write('ract","params":[42,23],"id":0}{"jsonrpc"')
rpc.write(':"2.0","method":"subtract","params":[100,1],"id":1}')

Kind: instance property of JSONBird
Returns: string - "object", "json-stream" or "json-message"

jsonBird.readableMode ⇒ string

Determines how JSONBird sends messages to the readable side of this Duplex stream.

If the value is "object", the readable stream is put in object mode and a plain old javascript object is sent.

For example:

rpc.on('data', object => assert.deepEqual(object, {jsonrpc: '2.0', result: 19, id: 0}));

If the value is "json-message", the readable stream is put in object mode and a json string is sent.

For example:

rpc.on('data', string => console.log('json string:', string));
// json string: {"jsonrpc":"2.0","result":19,"id":0}
// json string: {"jsonrpc":"2.0","result":99,"id":1}

If the value is "json-stream", a streaming sequence of json strings are sent.

For example:

rpc.on('data', string => console.log('chunk:', string));
// chunk: {"jsonrpc":"2.0","res
// chunk: ult":19,"id":0}{"jsonrpc":"2.0",
// chunk: "result":99,"id":1}

Kind: instance property of JSONBird
Returns: string - "object" or "json-stream"

jsonBird.endOfJSONWhitespace ⇒ string

This value is appended to the end of every json string sent to the readable stream.

Only whitespace characters are allowed. This option only has an affect if readableMode == 'json-stream'

Kind: instance property of JSONBird

jsonBird.endOnFinish ⇒ boolean

If true and the writable side of this Duplex stream has finished, automatically end the readable side (after all pending responses have been sent).

Kind: instance property of JSONBird

jsonBird.endOnFinish

If true and the writable side of this Duplex stream has finished, automatically end the readable side (after all pending responses have been sent).

Kind: instance property of JSONBird

ParamType
valueboolean

jsonBird.finishOnEnd ⇒ boolean

If true and the readable side of this Duplex stream has ended, automatically finish the writable side (after all pending requests have received a response).

Kind: instance property of JSONBird

jsonBird.finishOnEnd

If true and the readable side of this Duplex stream has ended, automatically finish the writable side (after all pending requests have received a response).

Kind: instance property of JSONBird

ParamType
valueboolean

jsonBird.serverPending ⇒ number

The number of incoming RPC requests for which we have not sent a reply yet

Kind: instance property of JSONBird

jsonBird.clientPending ⇒ number

The number of outstanding RPC requests for which we have not yet received a response.

Kind: instance property of JSONBird

jsonBird.sendErrorStack ⇒ boolean

If true, the fileName, lineNumber, columnNumber and stack of an Error thrown during a method is sent to the client using the JSON-RPC error.data property.

Kind: instance property of JSONBird

jsonBird.sendErrorStack

If true, the fileName, lineNumber, columnNumber and stack of an Error thrown during a method is sent to the client using the JSON-RPC error.data property.

Kind: instance property of JSONBird

ParamType
valueboolean

jsonBird.defaultTimeout ⇒ number

The timeout to use for an outgoing method call unless a different timeout was explicitly specified to call().

Kind: instance property of JSONBird

jsonBird.defaultTimeout

The timeout to use for an outgoing method call unless a different timeout was explicitly specified to call().

Kind: instance property of JSONBird

ParamType
valuenumber

jsonBird.generateId() ⇒ string | number

Generate a new id to be used for an outgoing request object

Kind: instance method of JSONBird

jsonBird.waitForPendingResponses() ⇒ Promise

Returns a promise which resolves as soon as all pending requests (as a server) have had their appropriate responses sent to the underlying readable stream.

Note that if a new requests comes in after using waitForPendingResponses(), they will not further delay this Promise.

Kind: instance method of JSONBird

jsonBird.waitForPendingRequests() ⇒ Promise

Returns a promise which resolves as soon as all pending requests (as a client) have had their appropriate responses received from the underlying writable stream.

Note that if a new call() is made after using waitForPendingResponses(), it will not further delay this Promise.

Kind: instance method of JSONBird

jsonBird.method(name, func)

Registers a new method with the given name.

If the same method name is registered multiple times, earlier definitions will be overridden

Kind: instance method of JSONBird

ParamTypeDescription
namestringThe method name
funcfunction

jsonBird.methods(objectOrMap)

Registers multiple methods using an object or Map.

Each key->value pair is registered as a method. Values that are not a function are ignored. The this object during a method call is set to the objectOrMap (unless a Map was used)

If the same method name is registered multiple times, earlier definitions will be overridden

Kind: instance method of JSONBird

ParamType
objectOrMapObject | Map

jsonBird.notification(name, func)

Registers a notification with the given name.

A notification is a method for which the return value or thrown Error is ignored. A response object is never sent.

If the same method name is registered multiple times, all functions handlers will be called (in the same order as they were registered)

Kind: instance method of JSONBird

ParamTypeDescription
namestringThe method name
funcfunction

jsonBird.notifications(objectOrMap)

Registers multiple notifications using an object or Map.

A notification is a method for which the return value or thrown Error is ignored. A response object is never sent.

If the same method name is registered multiple times, all functions handlers will be called (in the same order as they were registered)

Each key->value pair is registered as a notification. Values that are not a "function" are ignored. The this object during a method call is set to the objectOrMap (unless a Map was used)

If the same method name is registered multiple times, earlier definitions will be overridden

Kind: instance method of JSONBird

ParamType
objectOrMapObject | Map

jsonBird.call(nameOrOptions, ...args) ⇒ Promise

Call a method on the remote instance, by sending a JSON-RPC request object to our write stream.

If no write stream has been set, the method call will be buffered until a write stream is set (setWriteStream). Note: if a read stream is never set, any call() will also never resolve.

Kind: instance method of JSONBird
Returns: Promise - A Promise which will resole with the return value of the remote method

ParamTypeDescription
nameOrOptionsstring | ObjectThe method name or an options object
nameOrOptions.namestringThe method name
nameOrOptions.timeoutnumberA maximum time (in milliseconds) to wait for a response. The returned promise will reject after this time.
...args*

jsonBird.bindCall(nameOrOptions) ⇒ function

Returns a new function which calls the given method name by binding the function to this RPC instance and the given method name (or options object).

For example:

const subtract = rpc.bindCall('subtract');
subtract(10, 3).then(result => console.log(result)) // 7

Kind: instance method of JSONBird

ParamTypeDescription
nameOrOptionsstring | ObjectThe method name or an options object
nameOrOptions.namestringThe method name
nameOrOptions.timeoutnumberA maximum time (in milliseconds) to wait for a response. The returned promise will reject after this time.

jsonBird.notify(nameOrOptions, ...args) ⇒ Promise

Execute a notification on the remote instance, by sending a JSON-RPC request object to our write stream.

If no write stream has been set, the method call will be buffered until a write stream is set (setWriteStream).

This function resolves as soon as the request object has been buffered, but does not wait for the remote instance to have actually received the request object.

Kind: instance method of JSONBird

ParamTypeDescription
nameOrOptionsstring | ObjectThe method name or an options object
nameOrOptions.namestringThe method name
...args*

jsonBird.bindNotify(nameOrOptions) ⇒ function

Returns a new function which sends a notification with the given method name by binding the function to this RPC instance and the given method name (or options object).

For example:

const userDeleted = rpc.bindNotify('userDeleted');
userDeleted(123)

Kind: instance method of JSONBird

ParamTypeDescription
nameOrOptionsstring | ObjectThe method name or an options object
nameOrOptions.namestringThe method name
nameOrOptions.timeoutnumberA maximum time (in milliseconds) to wait for a response. The returned promise will reject after this time.

JSONBird.errorToResponseObject(error, [includeErrorStack]) ⇒ Object

Converts any javascript Error object to a JSON-RPC error object

Kind: static method of JSONBird

ParamTypeDefaultDescription
errorErrorThe message, code and data properties of this error will be copied over to the resulting object.
[includeErrorStack]booleanfalseIf true and error.data is undefined, the resulting data property will be an object containing the fileName, lineNumber, columnNumber and stack of the error

JSONBird.isValidVersion(jsonrpc) ⇒ boolean

Is the given value for the JSON-RPC jsonrpc property a value that we recognise?

Currently, only "2.0" is supported

Kind: static method of JSONBird

ParamType
jsonrpc*

JSONBird.isValidID(id) ⇒ boolean

Is the given value for the JSON-RPC id property valid?

Kind: static method of JSONBird

ParamType
id*

JSONBird.isValidMethodName(method) ⇒ boolean

Is the given value for the JSON-RPC method property valid?

Kind: static method of JSONBird

ParamType
method*

JSONBird.isValidParams(params) ⇒ boolean

Is the given value for the JSON-RPC params property valid?

Kind: static method of JSONBird

ParamType
params*

JSONBird.isObjectBuiltinFunction(object, name) ⇒ boolean

Test if the given property name of object is one of the builtin Object.prototype functions.

Such as: hasOwnProperty, defineGetter, etc

Kind: static method of JSONBird

ParamType
objectObject
namestring

Keywords

FAQs

Package last updated on 03 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc