Decoder

Decoder

Decode a stream of CBOR bytes by transforming them into equivalent JavaScript data. Because of the limitations of Node object streams, special symbols are emitted instead of NULL or UNDEFINED. Fix those up by calling Decoder.nullcheck.

Constructor

new Decoder(optionsopt)

Description:
  • Create a parsing stream.
Source:
Parameters:
Name Type Attributes Default Description
options DecoderOptions <optional>
{} Options.

Extends

Methods

close()

Description:
  • Stop processing.
Source:

(static) decodeAll(input, optionsopt, cbopt) → {Promise.<(Array.<ExtendedResults>|Array.<any>)>}

Description:
  • Decode all of the CBOR items in the input. This will error if there are more bytes left over at the end.
Source:
Parameters:
Name Type Attributes Default Description
input BufferLike What to parse?
options DecoderOptions | decodeAllCallback | string <optional>
{} Decoding options, the callback, or the input encoding.
cb decodeAllCallback <optional>
null Callback.
Throws:
No input specified.
Type
TypeError
Returns:
Even if callback is specified.
Type
Promise.<(Array.<ExtendedResults>|Array.<any>)>

(static) decodeAllSync(input, optionsopt) → {Array.<ExtendedResults>|Array.<any>}

Description:
  • Decode all of the CBOR items in the input into an array. This will throw an exception if the input is not valid CBOR; a zero-length input will return an empty array.
Source:
Parameters:
Name Type Attributes Default Description
input BufferLike What to parse?
options DecoderOptions | string <optional>
{} Options or encoding for input.
Throws:
  • No input provided.
    Type
    TypeError
  • Insufficient data provided.
    Type
    Error
Returns:
Array of all found items.
Type
Array.<ExtendedResults> | Array.<any>

(static) decodeFirst(input, optionsopt, cbopt) → {Promise.<(ExtendedResults|any)>}

Description:
  • Decode the first CBOR item in the input. This will error if there are more bytes left over at the end (if options.extendedResults is not true), and optionally if there were no valid CBOR bytes in the input. Emits the {Decoder.NOT_FOUND} Symbol in the callback if no data was found and the `required` option is false.
Source:
Parameters:
Name Type Attributes Default Description
input BufferLike What to parse?
options DecoderOptions | decodeCallback | string <optional>
{} Options, the callback, or input encoding.
cb decodeCallback <optional>
null Callback.
Throws:
No input provided.
Type
TypeError
Returns:
Returned even if callback is specified.
Type
Promise.<(ExtendedResults|any)>

(static) decodeFirstSync(input, optionsopt) → {ExtendedResults|any}

Description:
  • Decode the first CBOR item in the input, synchronously. This will throw an exception if the input is not valid CBOR, or if there are more bytes left over at the end (if options.extendedResults is not true).
Source:
Parameters:
Name Type Attributes Default Description
input BufferLike If a Readable stream, must have received the `readable` event already, or you will get an error claiming "Insufficient data".
options DecoderOptions | string <optional>
{} Options or encoding for input.
Throws:
  • Data is left over after decoding.
    Type
    UnexpectedDataError
  • Insufficient data.
    Type
    Error
Returns:
The decoded value.
Type
ExtendedResults | any

(static) nullcheck(val) → {any}

Description:
  • Check the given value for a symbol encoding a NULL or UNDEFINED value in the CBOR stream.
Source:
Example
myDecoder.on('data', val => {
  val = Decoder.nullcheck(val)
  // ...
})
Parameters:
Name Type Description
val any The value to check.
Throws:
Nothing was found.
Type
Error
Returns:
The corrected value.
Type
any