Encoder

Encoder

Transform JavaScript values into CBOR bytes. The `Writable` side of the stream is in object mode.

Constructor

new Encoder(optionsopt)

Creates an instance of Encoder.
Source:
Parameters:
Name Type Attributes Default Description
options Object <optional>
{} options for the encoder
Name Type Attributes Default Description
genTypes Array.<any> <optional>
[] array of pairs of `type`, `function(Encoder)` for semantic types to be encoded. Not needed for Array, Date, Buffer, Map, RegExp, Set, Url, or bignumber.
canonical boolean <optional>
false should the output be canonicalized
detectLoops boolean | Symbol <optional>
false should object loops be detected? This will currently modify the encoded object graph by adding a Symbol property to each object. If this bothers you, call `removeLoopDetectors` on the encoded object when done. Do not encode the same object twice on the same encoder, without calling `removeLoopDetectors` in between.

Extends

  • stream.Transform

Methods

(static) encode(…objs) → {Buffer}

Encode one or more JavaScript objects, and return a Buffer containing the CBOR bytes.
Source:
Parameters:
Name Type Attributes Description
objs any <repeatable>
the objects to encode
Returns:
Type:
Buffer
- the encoded objects

(static) encodeCanonical(…objs) → {Buffer}

Encode one or more JavaScript objects canonically (slower!), and return a Buffer containing the CBOR bytes.
Source:
Parameters:
Name Type Attributes Description
objs any <repeatable>
the objects to encode
Returns:
Type:
Buffer
- the encoded objects

addSemanticType(type, fun) → {encodeFunction}

Add an encoding function to the list of supported semantic types. This is useful for objects for which you can't add an encodeCBOR method
Source:
Parameters:
Name Type Description
type any
fun any
Returns:
Type:
encodeFunction

pushAny(obj) → {boolean}

Push any supported type onto the encoded stream
Source:
Parameters:
Name Type Description
obj any
Returns:
Type:
boolean
true on success