Home Reference Source
import Parser from 'remote-instance'
public class | source

Parser

Example:

import Parser from 'remote-instance';

const parser = new Parser();

Static Method Summary

Static Public Methods
public static

construct(constructor: function, argumentsList: Array): *

Construct a new instance of the given constructor with the given arguments list.

public static

isConstructor(constructor: function): boolean

Check if the given value is a remote-instance constructor.

public static

trimArgumentsList(argumentsList: Array, defaults: Array): Array

Trim a given argument list to a shorter list by removing undefined or default values.

Constructor Summary

Public Constructor
public

Create an instance of remote-instance Parser.

Method Summary

Public Methods
public

decode(buffer: Buffer | BufferList): *

Decode an value or an instance from an encoded buffer.

public

decodeArgumentsList(buffer: Buffer | BufferList): Array

Decode a given arguments list buffer.

public

decoder(): Stream

Get an transform stream that convert buffer to objects.

public

encode(value: *): BufferList

Encode an value or an instance to a buffer.

public

encodeArgumentsList(argumentsList: Array): BufferList

Encode a given arguments list to a buffer.

public

encoder(): Stream

Get an transform stream that convert objects to buffer.

public

register(typeCode: number | object, constructor: function): Parser

Register an instance on the parser with given type-code and constructor.

public

transform(stream: Stream, opts: object): Stream

Create a duplex stream that transform duplex buffer stream to object stream.

Static Public Methods

public static construct(constructor: function, argumentsList: Array): * source

Construct a new instance of the given constructor with the given arguments list. If the constructor has a static method fromArgumentsList(argumentsList) this function will use that function to construct the instance.

Params:

NameTypeAttributeDescription
constructor function

The given constructor

argumentsList Array
  • optional

The given arguments list that will apply an the new operator.

Return:

*

A new instance of the given constructor

public static isConstructor(constructor: function): boolean source

Check if the given value is a remote-instance constructor. An remote-instance constructor is a constructor with a method toArgumentsList() that enable to get the construct arguments of this class to create it remotely.

Params:

NameTypeAttributeDescription
constructor function

the given value to check

Return:

boolean

True if it's a remote-instance constructor

public static trimArgumentsList(argumentsList: Array, defaults: Array): Array source

Trim a given argument list to a shorter list by removing undefined or default values. Default arguments comparing via strict deep-equal comparison. For example, for the given argument list: [1, 'foo', undefined, {}] and the given default: [undefined, 'foo', 'bar', {}]. The trimmed arguments list will be: [1].

Params:

NameTypeAttributeDescription
argumentsList Array

The given argument list

defaults Array
  • optional

The default arguments for this argument list.

Return:

Array

Public Constructors

public constructor(opts: object) source

Create an instance of remote-instance Parser.

Params:

NameTypeAttributeDescription
opts object
  • optional

The given options for the parser

opts.parser object
  • optional

An alternative msgpack5 parser instance

Public Methods

public decode(buffer: Buffer | BufferList): * source

Decode an value or an instance from an encoded buffer.

Params:

NameTypeAttributeDescription
buffer Buffer | BufferList

The encoded buffer

Return:

*

Any value or registered instance

public decodeArgumentsList(buffer: Buffer | BufferList): Array source

Decode a given arguments list buffer.

Params:

NameTypeAttributeDescription
buffer Buffer | BufferList

an encoded arguments list buffer

Return:

Array

The decoded arguments list

public decoder(): Stream source

Get an transform stream that convert buffer to objects.

Return:

Stream

public encode(value: *): BufferList source

Encode an value or an instance to a buffer.

Params:

NameTypeAttributeDescription
value *

Any value or registered instance

Return:

BufferList

The encoded buffer

public encodeArgumentsList(argumentsList: Array): BufferList source

Encode a given arguments list to a buffer.

Params:

NameTypeAttributeDescription
argumentsList Array

The given arguments list

Return:

BufferList

The encoded arguments list

public encoder(): Stream source

Get an transform stream that convert objects to buffer.

Return:

Stream

public register(typeCode: number | object, constructor: function): Parser source

Register an instance on the parser with given type-code and constructor. The type-code value must be consistent in all the remote devices for the given constructor. The first argument could be also an object-map of type-codes and constructors instead.

Params:

NameTypeAttributeDescription
typeCode number | object

The numeric type-code for this instance between 0x01 to 0xff.

constructor function
  • optional

A remote-instance constructor

Return:

Parser

public transform(stream: Stream, opts: object): Stream source

Create a duplex stream that transform duplex buffer stream to object stream.

Params:

NameTypeAttributeDescription
stream Stream

A duplex buffer stream

opts object
  • optional

An options object to construct the duplex stream with. The option objectMode is forced to be true.

Return:

Stream

A duplex object stream