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)

Create a parsing stream.
Source:
Parameters:
Name Type Attributes Default Description
options object <optional>
{}
Name Type Attributes Default Description
max_depth number <optional>
-1 the maximum depth to parse. Use -1 for "until you run out of memory". Set this to a finite positive number for un-trusted inputs. Most standard inputs won't nest more than 100 or so levels; I've tested into the millions before running out of memory.
tags object <optional>
mapping from tag number to function(v), where v is the decoded value that comes after the tag, and where the function returns the correctly-created value for that tag.

Extends

  • BinaryParseStream

Methods

(static) decodeAll(input, options, cb) → {Promise}

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 Description
input string | Buffer the input to parse
options string | Object Decoding options. If string, the input encoding.
cb decodeAllCallback
Returns:
Type:
Promise
if no callback

(static) decodeAllSync(input, optionsopt) → {Array}

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 string | Buffer
options string | Object <optional>
{encoding: 'hex'}
Name Type Attributes Description
encoding: 'hex' string <optional>
The encoding of the input. Ignored if input is a Buffer.
Returns:
Type:
Array
- Array of all found items

(static) decodeFirst(input, options, cb) → {Promise}

Decode the first CBOR item in the input. This will error if there are more bytes left over at the end, 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 Description
input string | Buffer the input to parse
options function | string | Object
Name Type Attributes Description
encoding: 'hex' string <optional>
The encoding of the input. Ignored if input is a Buffer.
cb decodeCallback
Returns:
Type:
Promise
if no cb specified

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

Decode the first CBOR item in the input, synchronously. This will throw an exception if the input is not valid CBOR.
Source:
Parameters:
Name Type Attributes Default Description
input string | Buffer
options object <optional>
{encoding: 'hex'}
Name Type Attributes Description
encoding: 'hex' string <optional>
The encoding of the input. Ignored if input is a Buffer.
Returns:
Type:
any
- the decoded value

(static) nullcheck(val) → {any}

Check the given value for a symbol encoding a NULL or UNDEFINED value in the CBOR stream.
Source:
Parameters:
Name Type Description
val any the value to check
Returns:
Type:
any
the corrected value
Example
myDecoder.on('data', function(val) {
  val = Decoder.nullcheck(val);
  ...
});

close()

Stop processing
Source: