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 EncodingOptions <optional>
{} Options for the encoder.

Extends

  • stream.Transform

Members

(static) SEMANTIC_TYPES :SemanticMap

The currently supported set of semantic types. May be modified by plugins.
Source:
Type:

(nullable) detectLoops :WeakSet

Source:
Type:
  • WeakSet

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) encodeAsync(obj, optionsopt) → {Promise.<Buffer>}

Encode one JavaScript object using the given options in a way that is more resilient to objects being larger than the highWaterMark number of bytes. As with the other static encode functions, this will still use a large amount of memory. Use a stream-based approach directly if you need to process large and complicated inputs.
Source:
Parameters:
Name Type Attributes Default Description
obj any The object to encode.
options EncodingOptions <optional>
{} Passed to the Encoder constructor.
Returns:
Type:
Promise.<Buffer>
A promise for the encoded buffer.

(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.

(static) encodeIndefinite(gen, objopt, optionsopt) → {boolean}

Encode the given object with indefinite length. There are apparently some (IMO) broken implementations of poorly-specified protocols that REQUIRE indefinite-encoding. See the example for how to add this as an `encodeCBOR` function to an object or class to get indefinite encoding.
Source:
Parameters:
Name Type Attributes Default Description
gen Encoder The encoder to use.
obj string | Buffer | Array | Map | object <optional>
The object to encode. If null, use "this" instead.
options EncodingOptions <optional>
{} Options for encoding.
Throws:
No object to encode or invalid indefinite encoding.
Type
Error
Returns:
Type:
boolean
True on success.
Example

Force indefinite encoding:

const o = {
  a: true,
  encodeCBOR: cbor.Encoder.encodeIndefinite,
}
const m = []
m.encodeCBOR = cbor.Encoder.encodeIndefinite
cbor.encodeOne([o, m])

(static) encodeOne(obj, optionsopt) → {Buffer}

Encode one JavaScript object using the given options.
Source:
Parameters:
Name Type Attributes Default Description
obj any The object to encode.
options EncodingOptions <optional>
{} Passed to the Encoder constructor.
Returns:
Type:
Buffer
The encoded objects.

(static) pushArray(gen, obj, optsopt) → {boolean}

Encode an array and all of its elements.
Source:
Parameters:
Name Type Attributes Description
gen Encoder Encoder to use.
obj Array.<any> Array to encode.
opts object <optional>
Options.
Name Type Attributes Default Description
indefinite boolean <optional>
false Use indefinite encoding?
Returns:
Type:
boolean
True on success.

(static) reset()

Reset the supported semantic types to the original set, before any plugins modified the list.
Source:

_transform(fresh, encoding, cb)

Transforming.
Source:
Parameters:
Name Type Description
fresh any
encoding BufferEncoding
cb stream.TransformCallback

addSemanticType(type, fun) → (nullable) {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 string | function The type to encode.
fun EncodeFunction The encoder to use.
Throws:
Invalid function.
Type
TypeError
Returns:
Type:
EncodeFunction
The previous encoder or undefined if there wasn't one.

pushAny(obj) → {boolean}

Push any supported type onto the encoded stream.
Source:
Parameters:
Name Type Description
obj any The thing to encode.
Throws:
Unknown type for obj.
Type
TypeError
Returns:
Type:
boolean
True on success.

removeLoopDetectors() → {boolean}

Remove the loop detector WeakSet for this Encoder.
Source:
Returns:
Type:
boolean
True when the Encoder was reset, else false.