Encoder

Encoder

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

Constructor

new Encoder(optionsopt)

Description:
  • Creates an instance of Encoder.
Source:
Parameters:
Name Type Attributes Default Description
options EncodingOptions <optional>
{} Options for the encoder.

Extends

  • stream.Transform

Members

(nullable) detectLoops :WeakSet

Source:
Type:
  • WeakSet

(static) SEMANTIC_TYPES :SemanticMap

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

Methods

addSemanticType(type, fun) → (nullable) {EncodeFunction}

Description:
  • 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:
The previous encoder or undefined if there wasn't one.
Type
EncodeFunction

pushAny(obj) → {boolean}

Description:
  • 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:
True on success.
Type
boolean

removeLoopDetectors() → {boolean}

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

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

Description:
  • 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:
The encoded objects.
Type
Buffer

(static) encodeAsync(obj, optionsopt) → {Promise.<Buffer>}

Description:
  • 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:
A promise for the encoded buffer.
Type
Promise.<Buffer>

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

Description:
  • 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:
The encoded objects.
Type
Buffer

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

Description:
  • 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:
Example

Force indefinite encoding:

const o = {
  a: true,
  encodeCBOR: cbor.Encoder.encodeIndefinite,
}
const m = []
m.encodeCBOR = cbor.Encoder.encodeIndefinite
cbor.encodeOne([o, m])
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:
True on success.
Type
boolean

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

Description:
  • 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:
The encoded objects.
Type
Buffer

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

Description:
  • 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.
Properties
Name Type Attributes Default Description
indefinite boolean <optional>
false Use indefinite encoding?
Returns:
True on success.
Type
boolean

(static) reset()

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