Comparing version 8.0.0 to 8.0.2
@@ -12,54 +12,54 @@ 'use strict' | ||
/** | ||
* Convenience name for {@linkcode Commented.comment} | ||
*/ | ||
* Convenience name for {@linkcode Commented.comment}. | ||
*/ | ||
exports.comment = exports.Commented.comment | ||
/** | ||
* Convenience name for {@linkcode Decoder.decodeAll} | ||
*/ | ||
* Convenience name for {@linkcode Decoder.decodeAll}. | ||
*/ | ||
exports.decodeAll = exports.Decoder.decodeAll | ||
/** | ||
* Convenience name for {@linkcode Decoder.decodeFirst} | ||
*/ | ||
* Convenience name for {@linkcode Decoder.decodeFirst}. | ||
*/ | ||
exports.decodeFirst = exports.Decoder.decodeFirst | ||
/** | ||
* Convenience name for {@linkcode Decoder.decodeAllSync} | ||
*/ | ||
* Convenience name for {@linkcode Decoder.decodeAllSync}. | ||
*/ | ||
exports.decodeAllSync = exports.Decoder.decodeAllSync | ||
/** | ||
* Convenience name for {@linkcode Decoder.decodeFirstSync} | ||
*/ | ||
* Convenience name for {@linkcode Decoder.decodeFirstSync}. | ||
*/ | ||
exports.decodeFirstSync = exports.Decoder.decodeFirstSync | ||
/** | ||
* Convenience name for {@linkcode Diagnose.diagnose} | ||
*/ | ||
* Convenience name for {@linkcode Diagnose.diagnose}. | ||
*/ | ||
exports.diagnose = exports.Diagnose.diagnose | ||
/** | ||
* Convenience name for {@linkcode Encoder.encode} | ||
*/ | ||
* Convenience name for {@linkcode Encoder.encode}. | ||
*/ | ||
exports.encode = exports.Encoder.encode | ||
/** | ||
* Convenience name for {@linkcode Encoder.encodeCanonical} | ||
*/ | ||
* Convenience name for {@linkcode Encoder.encodeCanonical}. | ||
*/ | ||
exports.encodeCanonical = exports.Encoder.encodeCanonical | ||
/** | ||
* Convenience name for {@linkcode Encoder.encodeOne} | ||
*/ | ||
* Convenience name for {@linkcode Encoder.encodeOne}. | ||
*/ | ||
exports.encodeOne = exports.Encoder.encodeOne | ||
/** | ||
* Convenience name for {@linkcode Encoder.encodeAsync} | ||
*/ | ||
* Convenience name for {@linkcode Encoder.encodeAsync}. | ||
*/ | ||
exports.encodeAsync = exports.Encoder.encodeAsync | ||
/** | ||
* Convenience name for {@linkcode Decoder.decodeFirstSync} | ||
*/ | ||
* Convenience name for {@linkcode Decoder.decodeFirstSync}. | ||
*/ | ||
exports.decode = exports.Decoder.decodeFirstSync | ||
@@ -78,9 +78,11 @@ | ||
* | ||
* const db = level('./db', { | ||
* keyEncoding: cbor.leveldb, | ||
* valueEncoding: cbor.leveldb | ||
* }) | ||
* async function putget() { | ||
* const db = level('./db', { | ||
* keyEncoding: cbor.leveldb, | ||
* valueEncoding: cbor.leveldb, | ||
* }) | ||
* | ||
* await db.put({a:1}, 9857298342094820394820394820398234092834n) | ||
* const val = await db.get({a:1})) // 9857298342094820394820394820398234092834n | ||
* await db.put({a: 1}, 9857298342094820394820394820398234092834n) | ||
* const val = await db.get({a: 1}) | ||
* } | ||
*/ | ||
@@ -87,0 +89,0 @@ exports.leveldb = { |
@@ -7,4 +7,4 @@ 'use strict' | ||
const NoFilter = require('nofilter') | ||
const { MT, NUMBYTES, SYMS } = require('./constants') | ||
const { Buffer } = require('buffer') | ||
const {MT, NUMBYTES, SYMS} = require('./constants') | ||
const {Buffer} = require('buffer') | ||
@@ -19,45 +19,43 @@ function plural(c) { | ||
/** | ||
* @typedef CommentOptions | ||
* @property {number} [max_depth=10] - how many times to indent | ||
* the dashes | ||
* @property {number} [depth=1] - initial indentation depth | ||
* @property {boolean} [no_summary=false] - if true, omit the summary | ||
* of the full bytes read at the end | ||
* @property {object} [tags] - 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. | ||
* @property {object} [tags] - 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. | ||
* @property {boolean} [preferWeb=false] if true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
* changes in the future, so forward-compatibility is not guaranteed yet. | ||
* @property {string} [encoding='hex'] - Encoding to use for input, if it | ||
* is a string | ||
*/ | ||
* @typedef CommentOptions | ||
* @property {number} [max_depth=10] How many times to indent | ||
* the dashes. | ||
* @property {number} [depth=1] Initial indentation depth. | ||
* @property {boolean} [no_summary=false] If true, omit the summary | ||
* of the full bytes read at the end. | ||
* @property {object} [tags] 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. | ||
* @property {boolean} [preferWeb=false] If true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
* changes in the future, so forward-compatibility is not guaranteed yet. | ||
* @property {BufferEncoding} [encoding='hex'] Encoding to use for input, if it | ||
* is a string. | ||
*/ | ||
/** | ||
* @callback commentCallback | ||
* @param {Error} [error] - if one was generated | ||
* @param {string} [commented] - the comment string | ||
* @returns {void} | ||
*/ | ||
* @callback commentCallback | ||
* @param {Error} [error] If one was generated. | ||
* @param {string} [commented] The comment string. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* Normalize inputs to the static functions. | ||
* | ||
* @param {CommentOptions|commentCallback|string|number} opts encoding, | ||
* max_depth, or callback | ||
* @param {commentCallback} [cb] - called on completion | ||
* @returns {{options: CommentOptions, cb: commentCallback}} | ||
* @private | ||
*/ | ||
* Normalize inputs to the static functions. | ||
* | ||
* @param {CommentOptions|commentCallback|string|number} opts Encoding, | ||
* max_depth, or callback. | ||
* @param {commentCallback} [cb] Called on completion. | ||
* @returns {{options: CommentOptions, cb: commentCallback}} Normalized value. | ||
* @throws {TypeError} Unknown option type. | ||
* @private | ||
*/ | ||
function normalizeOptions(opts, cb) { | ||
switch (typeof opts) { | ||
case 'function': | ||
return { options: {}, cb: /** @type {commentCallback} */ (opts) } | ||
return {options: {}, cb: /** @type {commentCallback} */ (opts)} | ||
case 'string': | ||
return { options: {encoding: opts}, cb } | ||
return {options: {encoding: /** @type {BufferEncoding} */ (opts)}, cb} | ||
case 'number': | ||
return { options: { max_depth: opts }, cb } | ||
return {options: {max_depth: opts}, cb} | ||
case 'object': | ||
return { options: opts || {}, cb } | ||
return {options: opts || {}, cb} | ||
default: | ||
@@ -69,5 +67,5 @@ throw new TypeError('Unknown option type') | ||
/** | ||
* Generate the expanded format of RFC 7049, section 2.2.1 | ||
* Generate the expanded format of RFC 8949, section 3.2.2. | ||
* | ||
* @extends {stream.Transform} | ||
* @extends stream.Transform | ||
*/ | ||
@@ -78,3 +76,3 @@ class Commented extends stream.Transform { | ||
* | ||
* @param {CommentOptions} [options={}] - Stream options | ||
* @param {CommentOptions} [options={}] Stream options. | ||
*/ | ||
@@ -126,2 +124,3 @@ constructor(options = {}) { | ||
/** | ||
* @param {Buffer} v Descend into embedded CBOR. | ||
* @private | ||
@@ -152,7 +151,8 @@ */ | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|stream.Readable} input | ||
* |DataView|stream.Readable} input Something to parse. | ||
* @param {CommentOptions|commentCallback|string|number} [options={}] | ||
* encoding, max_depth, or callback | ||
* @param {commentCallback} [cb] - called on completion | ||
* @returns {Promise} if cb not specified | ||
* Encoding, max_depth, or callback. | ||
* @param {commentCallback} [cb] If specified, called on completion. | ||
* @returns {Promise} If cb not specified. | ||
* @throws {Error} Input required. | ||
*/ | ||
@@ -188,3 +188,3 @@ static comment(input, options = {}, cb = null) { | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -198,3 +198,3 @@ _on_error(er) { | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -213,7 +213,7 @@ _on_read(buf) { | ||
this.push(new Array(ind + 1).join(' ')) | ||
return this.push('-- ') | ||
this.push('-- ') | ||
} | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -251,9 +251,9 @@ _on_more(mt, len, parent_mt, pos) { | ||
} | ||
return this.push(`${desc} next ${len} byte${plural(len)}\n`) | ||
this.push(`${desc} next ${len} byte${plural(len)}\n`) | ||
} | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
_on_start_string(mt, tag, parent_mt, pos) { | ||
_on_start_string(mt, len, parent_mt, pos) { | ||
let desc = '' | ||
@@ -264,13 +264,13 @@ | ||
case MT.BYTE_STRING: | ||
desc = `Bytes, length: ${tag}` | ||
desc = `Bytes, length: ${len}` | ||
break | ||
case MT.UTF8_STRING: | ||
desc = `String, length: ${tag.toString()}` | ||
desc = `String, length: ${len.toString()}` | ||
break | ||
} | ||
return this.push(`${desc}\n`) | ||
this.push(`${desc}\n`) | ||
} | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -319,10 +319,10 @@ _on_start(mt, tag, parent_mt, pos) { | ||
} | ||
return this.push('\n') | ||
this.push('\n') | ||
} | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
_on_stop(mt) { | ||
return this.depth-- | ||
this.depth-- | ||
} | ||
@@ -372,3 +372,3 @@ | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -378,3 +378,3 @@ _on_data() { | ||
this.push(this.all.read().toString('hex')) | ||
return this.push('\n') | ||
this.push('\n') | ||
} | ||
@@ -381,0 +381,0 @@ } |
@@ -8,5 +8,6 @@ 'use strict' | ||
const NoFilter = require('nofilter') | ||
const stream = require('stream') | ||
const constants = require('./constants') | ||
const { MT, NUMBYTES, SYMS, BI } = constants | ||
const { Buffer } = require('buffer') | ||
const {MT, NUMBYTES, SYMS, BI} = constants | ||
const {Buffer} = require('buffer') | ||
@@ -46,9 +47,15 @@ const COUNT = Symbol('count') | ||
/** | ||
* Things that can act as inputs, from which a NoFilter can be created. | ||
* | ||
* @typedef {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|stream.Readable} BufferLike | ||
*/ | ||
/** | ||
* @typedef ExtendedResults | ||
* @property {any} value - the value that was found | ||
* @property {number} length - the number of bytes of the original input that | ||
* were read | ||
* @property {Buffer} bytes - the bytes of the original input that were used | ||
* to produce the value | ||
* @property {Buffer} [unused] - the bytes that were left over from the original | ||
* @property {any} value The value that was found. | ||
* @property {number} length The number of bytes of the original input that | ||
* were read. | ||
* @property {Buffer} bytes The bytes of the original input that were used | ||
* to produce the value. | ||
* @property {Buffer} [unused] The bytes that were left over from the original | ||
* input. This property only exists if {@linkcode Decoder.decodeFirst} or | ||
@@ -59,3 +66,3 @@ * {@linkcode Decoder.decodeFirstSync} was called. | ||
* @typedef DecoderOptions | ||
* @property {number} [max_depth=-1] - the maximum depth to parse. | ||
* @property {number} [max_depth=-1] The maximum depth to parse. | ||
* Use -1 for "until you run out of memory". Set this to a finite | ||
@@ -65,13 +72,13 @@ * positive number for un-trusted inputs. Most standard inputs won't nest | ||
* running out of memory. | ||
* @property {Tagged.TagMap} [tags] - mapping from tag number to function(v), | ||
* @property {Tagged.TagMap} [tags] 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. | ||
* @property {boolean} [preferWeb=false] if true, prefer Uint8Arrays to | ||
* @property {boolean} [preferWeb=false] If true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
* changes in the future, so forward-compatibility is not guaranteed yet. | ||
* @property {string} [encoding='hex'] - The encoding of the input. | ||
* @property {BufferEncoding} [encoding='hex'] The encoding of the input. | ||
* Ignored if input is a Buffer. | ||
* @property {boolean} [required=false] - Should an error be thrown when no | ||
* @property {boolean} [required=false] Should an error be thrown when no | ||
* data is in the input? | ||
* @property {boolean} [extendedResults=false] - if true, emit extended | ||
* @property {boolean} [extendedResults=false] If true, emit extended | ||
* results, which will be an object with shape {@link ExtendedResults}. | ||
@@ -81,22 +88,23 @@ * The value will already have been null-checked. | ||
/** | ||
* @callback decodeCallback | ||
* @param {Error} [error] - if one was generated | ||
* @param {any} [value] - the decoded value | ||
* @returns {void} | ||
*/ | ||
* @callback decodeCallback | ||
* @param {Error} [error] If one was generated. | ||
* @param {any} [value] The decoded value. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* @param {DecoderOptions|decodeCallback|string} opts options, | ||
* the callback, or input incoding | ||
* @param {decodeCallback} [cb] - called on completion | ||
* @returns {{options: DecoderOptions, cb: decodeCallback}} | ||
* @private | ||
*/ | ||
* @param {DecoderOptions|decodeCallback|string} opts Options, | ||
* the callback, or input incoding. | ||
* @param {decodeCallback} [cb] Called on completion. | ||
* @returns {{options: DecoderOptions, cb: decodeCallback}} Normalized. | ||
* @throws {TypeError} On unknown option type. | ||
* @private | ||
*/ | ||
function normalizeOptions(opts, cb) { | ||
switch (typeof opts) { | ||
case 'function': | ||
return { options: {}, cb: /** @type {decodeCallback} */ (opts) } | ||
return {options: {}, cb: /** @type {decodeCallback} */ (opts)} | ||
case 'string': | ||
return { options: { encoding: opts }, cb } | ||
return {options: {encoding: /** @type {BufferEncoding} */ (opts)}, cb} | ||
case 'object': | ||
return { options: opts || {}, cb } | ||
return {options: opts || {}, cb} | ||
default: | ||
@@ -113,3 +121,3 @@ throw new TypeError('Unknown option type') | ||
* | ||
* @extends {BinaryParseStream} | ||
* @extends BinaryParseStream | ||
*/ | ||
@@ -120,3 +128,3 @@ class Decoder extends BinaryParseStream { | ||
* | ||
* @param {DecoderOptions} [options={}] | ||
* @param {DecoderOptions} [options={}] Options. | ||
*/ | ||
@@ -154,10 +162,10 @@ constructor(options = {}) { | ||
* @static | ||
* @param {any} val - the value to check | ||
* @returns {any} the corrected value | ||
* | ||
* @param {any} val The value to check. | ||
* @returns {any} The corrected value. | ||
* @throws {Error} Nothing was found. | ||
* @example | ||
* myDecoder.on('data', function(val) { | ||
* val = Decoder.nullcheck(val); | ||
* ... | ||
* }); | ||
* myDecoder.on('data', val => { | ||
* val = Decoder.nullcheck(val) | ||
* // ... | ||
* }) | ||
*/ | ||
@@ -187,8 +195,9 @@ static nullcheck(val) { | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input - If a Readable stream, must have | ||
* @param {BufferLike} input If a Readable stream, must have | ||
* received the `readable` event already, or you will get an error | ||
* claiming "Insufficient data" | ||
* @param {DecoderOptions|string} [options={}] Options or encoding for input | ||
* @returns {any} - the decoded value | ||
* claiming "Insufficient data". | ||
* @param {DecoderOptions|string} [options={}] Options or encoding for input. | ||
* @returns {ExtendedResults|any} The decoded value. | ||
* @throws {UnexpectedDataError} Data is left over after decoding. | ||
* @throws {Error} Insufficient data. | ||
*/ | ||
@@ -244,7 +253,8 @@ static decodeFirstSync(input, options = {}) { | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input | ||
* @param {BufferLike} input What to parse? | ||
* @param {DecoderOptions|string} [options={}] Options or encoding | ||
* for input | ||
* @returns {Array} - Array of all found items | ||
* for input. | ||
* @returns {Array<ExtendedResults>|Array<any>} Array of all found items. | ||
* @throws {TypeError} No input provided. | ||
* @throws {Error} Insufficient data provided. | ||
*/ | ||
@@ -289,8 +299,9 @@ static decodeAllSync(input, options = {}) { | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input | ||
* @param {DecoderOptions|decodeCallback|string} [options={}] - options, the | ||
* callback, or input encoding | ||
* @param {decodeCallback} [cb] callback | ||
* @returns {Promise<any>} returned even if callback is specified | ||
* @param {BufferLike} input What to parse? | ||
* @param {DecoderOptions|decodeCallback|string} [options={}] Options, the | ||
* callback, or input encoding. | ||
* @param {decodeCallback} [cb] Callback. | ||
* @returns {Promise<ExtendedResults|any>} Returned even if callback is | ||
* specified. | ||
* @throws {TypeError} No input provided. | ||
*/ | ||
@@ -353,4 +364,5 @@ static decodeFirst(input, options = {}, cb = null) { | ||
* @callback decodeAllCallback | ||
* @param {Error} error - if one was generated | ||
* @param {Array} value - all of the decoded values, wrapped in an Array | ||
* @param {Error} error If one was generated. | ||
* @param {Array<ExtendedResults>|Array<any>} value All of the decoded | ||
* values, wrapped in an Array. | ||
*/ | ||
@@ -363,8 +375,9 @@ | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input | ||
* @param {DecoderOptions|decodeAllCallback|string} [options={}] - | ||
* @param {BufferLike} input What to parse? | ||
* @param {DecoderOptions|decodeAllCallback|string} [options={}] | ||
* Decoding options, the callback, or the input encoding. | ||
* @param {decodeAllCallback} [cb] callback | ||
* @returns {Promise<Array>} even if callback is specified | ||
* @param {decodeAllCallback} [cb] Callback. | ||
* @returns {Promise<Array<ExtendedResults>|Array<any>>} Even if callback | ||
* is specified. | ||
* @throws {TypeError} No input specified. | ||
*/ | ||
@@ -396,3 +409,3 @@ static decodeAll(input, options = {}, cb = null) { | ||
/** | ||
* Stop processing | ||
* Stop processing. | ||
*/ | ||
@@ -405,3 +418,4 @@ close() { | ||
/** | ||
* Only called if extendedResults is true | ||
* Only called if extendedResults is true. | ||
* | ||
* @ignore | ||
@@ -414,4 +428,7 @@ */ | ||
/** | ||
* @yields {number} Number of bytes to read. | ||
* @returns {Generator<number, any, Buffer>} Yields a number of bytes, | ||
* returns anything, next returns a Buffer. | ||
* @throws {Error} Maximum depth exceeded. | ||
* @ignore | ||
* @returns {Generator<number, any, Buffer>} | ||
*/ | ||
@@ -553,19 +570,17 @@ *_parse() { | ||
while (parent != null) { | ||
switch (false) { | ||
case val !== SYMS.BREAK: | ||
parent[COUNT] = 1 | ||
break | ||
case !Array.isArray(parent): | ||
parent.push(val) | ||
break | ||
case !(parent instanceof NoFilter): { | ||
const pm = parent[MAJOR] | ||
if (val === SYMS.BREAK) { | ||
parent[COUNT] = 1 | ||
} else if (Array.isArray(parent)) { | ||
parent.push(val) | ||
} else { | ||
// Assert: parent instanceof NoFilter | ||
const pm = parent[MAJOR] | ||
if ((pm != null) && (pm !== mt)) { | ||
this.running = false | ||
throw new Error('Invalid major type in indefinite encoding') | ||
} | ||
parent.write(val) | ||
if ((pm != null) && (pm !== mt)) { | ||
this.running = false | ||
throw new Error('Invalid major type in indefinite encoding') | ||
} | ||
parent.write(val) | ||
} | ||
if ((--parent[COUNT]) !== 0) { | ||
@@ -572,0 +587,0 @@ again = true |
@@ -10,44 +10,49 @@ 'use strict' | ||
/** | ||
* @typedef DiagnoseOptions | ||
* @property {string} [separator='\n'] - output between detected objects | ||
* @property {boolean} [stream_errors=false] - put error info into the | ||
* output stream | ||
* @property {number} [max_depth=-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. | ||
* @property {object} [tags] - 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. | ||
* @property {object} [tags] - 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. | ||
* @property {boolean} [preferWeb=false] - if true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
* changes in the future, so forward-compatibility is not guaranteed yet. | ||
* @property {string} [encoding='hex'] - the encoding of input, ignored if | ||
* input is not string | ||
*/ | ||
* Things that can act as inputs, from which a NoFilter can be created. | ||
* | ||
* @typedef {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|stream.Readable} BufferLike | ||
*/ | ||
/** | ||
* @callback diagnoseCallback | ||
* @param {Error} [error] - if one was generated | ||
* @param {string} [value] - the diagnostic value | ||
* @returns {void} | ||
*/ | ||
* @typedef DiagnoseOptions | ||
* @property {string} [separator='\n'] Output between detected objects. | ||
* @property {boolean} [stream_errors=false] Put error info into the | ||
* output stream. | ||
* @property {number} [max_depth=-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. | ||
* @property {object} [tags] 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. | ||
* @property {boolean} [preferWeb=false] If true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
* changes in the future, so forward-compatibility is not guaranteed yet. | ||
* @property {BufferEncoding} [encoding='hex'] The encoding of input, ignored if | ||
* input is not string. | ||
*/ | ||
/** | ||
* @param {DiagnoseOptions|diagnoseCallback|string} opts options, | ||
* the callback, or input incoding | ||
* @param {diagnoseCallback} [cb] - called on completion | ||
* @returns {{options: DiagnoseOptions, cb: diagnoseCallback}} | ||
* @private | ||
*/ | ||
* @callback diagnoseCallback | ||
* @param {Error} [error] If one was generated. | ||
* @param {string} [value] The diagnostic value. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* @param {DiagnoseOptions|diagnoseCallback|string} opts Options, | ||
* the callback, or input incoding. | ||
* @param {diagnoseCallback} [cb] Called on completion. | ||
* @returns {{options: DiagnoseOptions, cb: diagnoseCallback}} Normalized. | ||
* @throws {TypeError} Unknown option type. | ||
* @private | ||
*/ | ||
function normalizeOptions(opts, cb) { | ||
switch (typeof opts) { | ||
case 'function': | ||
return { options: {}, cb: /** @type {diagnoseCallback} */ (opts) } | ||
return {options: {}, cb: /** @type {diagnoseCallback} */ (opts)} | ||
case 'string': | ||
return { options: { encoding: opts }, cb } | ||
return {options: {encoding: /** @type {BufferEncoding} */ (opts)}, cb} | ||
case 'object': | ||
return { options: opts || {}, cb } | ||
return {options: opts || {}, cb} | ||
default: | ||
@@ -61,3 +66,3 @@ throw new TypeError('Unknown option type') | ||
* | ||
* @extends {stream.Transform} | ||
* @extends stream.Transform | ||
*/ | ||
@@ -68,3 +73,3 @@ class Diagnose extends stream.Transform { | ||
* | ||
* @param {DiagnoseOptions} [options={}] - options for creation | ||
* @param {DiagnoseOptions} [options={}] Options for creation. | ||
*/ | ||
@@ -125,12 +130,12 @@ constructor(options = {}) { | ||
* | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|stream.Readable} input - the CBOR bytes to format | ||
* @param {DiagnoseOptions |diagnoseCallback|string} [options={}] - | ||
* options, the callback, or the input encoding | ||
* @param {diagnoseCallback} [cb] - callback | ||
* @returns {Promise} if callback not specified | ||
* @param {BufferLike} input The CBOR bytes to format. | ||
* @param {DiagnoseOptions |diagnoseCallback|string} [options={}] | ||
* Options, the callback, or the input encoding. | ||
* @param {diagnoseCallback} [cb] Callback. | ||
* @throws {TypeError} Input not provided. | ||
* @returns {Promise} If callback not specified. | ||
*/ | ||
static diagnose(input, options = {}, cb = null) { | ||
if (input == null) { | ||
throw new Error('input required') | ||
throw new TypeError('input required') | ||
} | ||
@@ -157,8 +162,11 @@ ({options, cb} = normalizeOptions(options, cb)) | ||
/** @private */ | ||
/** | ||
* @ignore | ||
*/ | ||
_on_error(er) { | ||
if (this.stream_errors) { | ||
return this.push(er.toString()) | ||
this.push(er.toString()) | ||
} else { | ||
this.emit('error', er) | ||
} | ||
return this.emit('error', er) | ||
} | ||
@@ -165,0 +173,0 @@ |
@@ -10,3 +10,3 @@ 'use strict' | ||
} = constants | ||
const { Buffer } = require('buffer') | ||
const {Buffer} = require('buffer') | ||
@@ -33,27 +33,31 @@ const HALF = (MT.SIMPLE_FLOAT << 5) | NUMBYTES.TWO | ||
* @callback EncodeFunction | ||
* @param {Encoder} enc - the encoder to use | ||
* @param {any} val - the value to encode | ||
* @return {boolean} - true on success | ||
* @param {Encoder} enc The encoder to use. | ||
* @param {any} val The value to encode. | ||
* @returns {boolean} True on success. | ||
*/ | ||
/* eslint-disable jsdoc/check-types */ | ||
/** | ||
* A mapping from tag number to a tag decoding function | ||
* A mapping from tag number to a tag decoding function. | ||
* | ||
* @typedef {Object.<string, EncodeFunction>} SemanticMap | ||
*/ | ||
/* eslint-enable jsdoc/check-types */ | ||
/** | ||
* @type {SemanticMap} | ||
* @private | ||
*/ | ||
* @type {SemanticMap} | ||
* @private | ||
*/ | ||
const SEMANTIC_TYPES = {} | ||
/** | ||
* @type {SemanticMap} | ||
* @private | ||
*/ | ||
* @type {SemanticMap} | ||
* @private | ||
*/ | ||
let current_SEMANTIC_TYPES = {} | ||
/** | ||
* @param {string} str | ||
* @returns {"number"|"float"|"int"|"string"} | ||
* @param {string} str String to normalize. | ||
* @returns {"number"|"float"|"int"|"string"} Normalized. | ||
* @throws {TypeError} Invalid input. | ||
* @private | ||
@@ -81,9 +85,9 @@ */ | ||
* @typedef EncodingOptions | ||
* @property {any[]|Object} [genTypes=[]] - array of pairs of | ||
* @property {any[]|object} [genTypes=[]] Array of pairs of | ||
* `type`, `function(Encoder)` for semantic types to be encoded. Not | ||
* needed for Array, Date, Buffer, Map, RegExp, Set, or URL. | ||
* If an object, the keys are the constructor names for the types. | ||
* @property {boolean} [canonical=false] - should the output be | ||
* canonicalized | ||
* @property {boolean|WeakSet} [detectLoops=false] - should object loops | ||
* @property {boolean} [canonical=false] Should the output be | ||
* canonicalized. | ||
* @property {boolean|WeakSet} [detectLoops=false] Should object loops | ||
* be detected? This will currently add memory to track every part of the | ||
@@ -98,3 +102,3 @@ * object being encoded in a WeakSet. Do not encode | ||
* fractional seconds. | ||
* @property {any} [encodeUndefined=undefined] - How should an | ||
* @property {any} [encodeUndefined=undefined] How should an | ||
* "undefined" in the input be encoded. By default, just encode a CBOR | ||
@@ -106,3 +110,3 @@ * undefined. If this is a buffer, use those bytes without re-encoding | ||
* be encoded as CBOR. | ||
* @property {boolean} [disallowUndefinedKeys=false] - Should | ||
* @property {boolean} [disallowUndefinedKeys=false] Should | ||
* "undefined" be disallowed as a key in a Map that is serialized? If | ||
@@ -112,8 +116,8 @@ * this is true, encode(new Map([[undefined, 1]])) will throw an | ||
* normal JS object. | ||
* @property {boolean} [collapseBigIntegers=false] - Should integers | ||
* @property {boolean} [collapseBigIntegers=false] Should integers | ||
* that come in as ECMAscript bigint's be encoded | ||
* as normal CBOR integers if they fit, discarding type information? | ||
* @property {number} [chunkSize=4096] - Number of characters or bytes | ||
* for each chunk, if obj is a string or Buffer, when indefinite encoding | ||
* @property {boolean} [omitUndefinedProperties=false] - When encoding | ||
* @property {number} [chunkSize=4096] Number of characters or bytes | ||
* for each chunk, if obj is a string or Buffer, when indefinite encoding. | ||
* @property {boolean} [omitUndefinedProperties=false] When encoding | ||
* objects or Maps, do not include a key if its corresponding value is | ||
@@ -127,3 +131,3 @@ * `undefined`. | ||
* | ||
* @extends {stream.Transform} | ||
* @extends stream.Transform | ||
*/ | ||
@@ -134,3 +138,3 @@ class Encoder extends stream.Transform { | ||
* | ||
* @param {EncodingOptions} [options={}] - options for the encoder | ||
* @param {EncodingOptions} [options={}] Options for the encoder. | ||
*/ | ||
@@ -162,3 +166,3 @@ constructor(options = {}) { | ||
/** @type WeakSet<any>? */ | ||
/** @type {WeakSet?} */ | ||
this.detectLoops = undefined | ||
@@ -201,4 +205,4 @@ if (typeof detectLoops === 'boolean') { | ||
/** | ||
* @param {number} val - Number(0-255) to encode | ||
* @returns {boolean} true on success | ||
* @param {number} val Number(0-255) to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -213,4 +217,4 @@ */ | ||
/** | ||
* @param {number} val - Number(0-65535) to encode | ||
* @returns {boolean} true on success | ||
* @param {number} val Number(0-65535) to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -225,4 +229,4 @@ */ | ||
/** | ||
* @param {number} val - Number(0..2**32-1) to encode | ||
* @returns {boolean} true on success | ||
* @param {number} val Number(0..2**32-1) to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -237,4 +241,4 @@ */ | ||
/** | ||
* @param {number} val - Number to encode as 4-byte float | ||
* @returns {boolean} true on success | ||
* @param {number} val Number to encode as 4-byte float. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -249,4 +253,4 @@ */ | ||
/** | ||
* @param {number} val - Number to encode as 8-byte double | ||
* @returns {boolean} true on success | ||
* @param {number} val Number to encode as 8-byte double. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -261,3 +265,3 @@ */ | ||
/** | ||
* @returns {boolean} true on success | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -270,4 +274,4 @@ */ | ||
/** | ||
* @param {number} obj - Positive or negative infinity | ||
* @returns {boolean} true on success | ||
* @param {number} obj Positive or negative infinity. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -283,5 +287,5 @@ */ | ||
* | ||
* @param {number} obj - A number that is known to be not-integer, but not | ||
* how many bytes of precision it needs | ||
* @returns {boolean} true on success | ||
* @param {number} obj A number that is known to be not-integer, but not | ||
* how many bytes of precision it needs. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -322,11 +326,11 @@ */ | ||
* | ||
* @param {number} obj - A positive number that is known to be an integer, | ||
* but not how many bytes of precision it needs | ||
* @param {number} mt - The Major Type number to combine with the integer. | ||
* @param {number} obj A positive number that is known to be an integer, | ||
* but not how many bytes of precision it needs. | ||
* @param {number} mt The Major Type number to combine with the integer. | ||
* Not yet shifted. | ||
* @param {number} [orig] - The number before it was transformed to positive. | ||
* @param {number} [orig] The number before it was transformed to positive. | ||
* If the mt is NEG_INT, and the positive number is over MAX_SAFE_INT, | ||
* then we'll encode this as a float rather than making the number | ||
* negative again and losing precision. | ||
* @returns {boolean} true on success | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -336,21 +340,29 @@ */ | ||
const m = mt << 5 | ||
switch (false) { | ||
case !(obj < 24): | ||
return this._pushUInt8(m | obj) | ||
case !(obj <= 0xff): | ||
return this._pushUInt8(m | NUMBYTES.ONE) && this._pushUInt8(obj) | ||
case !(obj <= 0xffff): | ||
return this._pushUInt8(m | NUMBYTES.TWO) && this._pushUInt16BE(obj) | ||
case !(obj <= 0xffffffff): | ||
return this._pushUInt8(m | NUMBYTES.FOUR) && this._pushUInt32BE(obj) | ||
case !(obj <= Number.MAX_SAFE_INTEGER): | ||
return this._pushUInt8(m | NUMBYTES.EIGHT) && | ||
this._pushUInt32BE(Math.floor(obj / SHIFT32)) && | ||
this._pushUInt32BE(obj % SHIFT32) | ||
default: | ||
if (mt === MT.NEG_INT) { | ||
return this._pushFloat(orig) | ||
} | ||
return this._pushFloat(obj) | ||
if (obj < 24) { | ||
return this._pushUInt8(m | obj) | ||
} | ||
if (obj <= 0xff) { | ||
return this._pushUInt8(m | NUMBYTES.ONE) && this._pushUInt8(obj) | ||
} | ||
if (obj <= 0xffff) { | ||
return this._pushUInt8(m | NUMBYTES.TWO) && this._pushUInt16BE(obj) | ||
} | ||
if (obj <= 0xffffffff) { | ||
return this._pushUInt8(m | NUMBYTES.FOUR) && this._pushUInt32BE(obj) | ||
} | ||
let max = Number.MAX_SAFE_INTEGER | ||
if (mt === MT.NEG_INT) { | ||
// Special case for Number.MIN_SAFE_INTEGER - 1 | ||
max-- | ||
} | ||
if (obj <= max) { | ||
return this._pushUInt8(m | NUMBYTES.EIGHT) && | ||
this._pushUInt32BE(Math.floor(obj / SHIFT32)) && | ||
this._pushUInt32BE(obj % SHIFT32) | ||
} | ||
if (mt === MT.NEG_INT) { | ||
return this._pushFloat(orig) | ||
} | ||
return this._pushFloat(obj) | ||
} | ||
@@ -361,5 +373,5 @@ | ||
* | ||
* @param {number} obj - A number that is known to be an integer, | ||
* but not how many bytes of precision it needs | ||
* @returns {boolean} true on success | ||
* @param {number} obj A number that is known to be an integer, | ||
* but not how many bytes of precision it needs. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -379,22 +391,22 @@ */ | ||
/** | ||
* @param {number} obj - plain JS number to encode | ||
* @returns {boolean} true on success | ||
* @param {number} obj Plain JS number to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
*/ | ||
_pushNumber(obj) { | ||
switch (false) { | ||
case !isNaN(obj): | ||
return this._pushNaN() | ||
case isFinite(obj): | ||
return this._pushInfinity(obj) | ||
case Math.round(obj) !== obj: | ||
return this._pushIntNum(obj) | ||
default: | ||
return this._pushFloat(obj) | ||
if (isNaN(obj)) { | ||
return this._pushNaN() | ||
} | ||
if (!isFinite(obj)) { | ||
return this._pushInfinity(obj) | ||
} | ||
if (Math.round(obj) === obj) { | ||
return this._pushIntNum(obj) | ||
} | ||
return this._pushFloat(obj) | ||
} | ||
/** | ||
* @param {string} obj - string to encode | ||
* @returns {boolean} true on success | ||
* @param {string} obj String to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -408,4 +420,4 @@ */ | ||
/** | ||
* @param {boolean} obj - bool to encode | ||
* @returns {boolean} true on success | ||
* @param {boolean} obj Bool to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -418,4 +430,4 @@ */ | ||
/** | ||
* @param {undefined} obj - ignored | ||
* @returns {boolean} true on success | ||
* @param {undefined} obj Ignored. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -440,4 +452,4 @@ */ | ||
/** | ||
* @param {null} obj - ignored | ||
* @returns {boolean} true on success | ||
* @param {null} obj Ignored. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -450,4 +462,4 @@ */ | ||
/** | ||
* @param {number} tag - Tag number to encode | ||
* @returns {boolean} true on success | ||
* @param {number} tag Tag number to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -460,4 +472,4 @@ */ | ||
/** | ||
* @param {bigint} obj - BigInt to encode | ||
* @returns {boolean} true on success | ||
* @param {bigint} obj BigInt to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -495,4 +507,5 @@ */ | ||
/** | ||
* @param {object} obj - object to encode | ||
* @returns {boolean} true on success | ||
* @param {object} obj Object to encode. | ||
* @returns {boolean} True on success. | ||
* @throws {Error} Loop detected. | ||
* @ignore | ||
@@ -582,8 +595,8 @@ */ | ||
/** | ||
* @param {any[]} objs - Array of supported things | ||
* @returns {Buffer} Concatenation of encodings for the supported things | ||
* @param {any[]} objs Array of supported things. | ||
* @returns {Buffer} Concatenation of encodings for the supported things. | ||
* @ignore | ||
*/ | ||
_encodeAll(objs) { | ||
const bs = new NoFilter({ highWaterMark: this.readableHighWaterMark }) | ||
const bs = new NoFilter({highWaterMark: this.readableHighWaterMark}) | ||
this.pipe(bs) | ||
@@ -599,8 +612,9 @@ for (const o of objs) { | ||
* 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 | ||
* is useful for objects for which you can't add an encodeCBOR method. | ||
* | ||
* @param {string|function} type - The type to encode | ||
* @param {EncodeFunction} fun - The encoder to use | ||
* @param {string|Function} type The type to encode. | ||
* @param {EncodeFunction} fun The encoder to use. | ||
* @returns {EncodeFunction?} The previous encoder or undefined if there | ||
* wasn't one. | ||
* @throws {TypeError} Invalid function. | ||
*/ | ||
@@ -623,6 +637,7 @@ addSemanticType(type, fun) { | ||
/** | ||
* Push any supported type onto the encoded stream | ||
* Push any supported type onto the encoded stream. | ||
* | ||
* @param {any} obj | ||
* @returns {boolean} true on success | ||
* @param {any} obj The thing to encode. | ||
* @returns {boolean} True on success. | ||
* @throws {TypeError} Unknown type for obj. | ||
*/ | ||
@@ -651,6 +666,6 @@ pushAny(obj) { | ||
default: | ||
throw new Error(`Unknown symbol: ${obj.toString()}`) | ||
throw new TypeError(`Unknown symbol: ${obj.toString()}`) | ||
} | ||
default: | ||
throw new Error( | ||
throw new TypeError( | ||
`Unknown type: ${typeof obj}, ${(typeof obj.toString === 'function') ? obj.toString() : ''}` | ||
@@ -664,7 +679,7 @@ ) | ||
* | ||
* @param {Encoder} gen - Encoder to use | ||
* @param {any[]} obj - Array to encode | ||
* @param {Object} [opts] - options | ||
* @param {boolean} [opts.indefinite=false] - Use indefinite encoding? | ||
* @returns {boolean} true on success | ||
* @param {Encoder} gen Encoder to use. | ||
* @param {any[]} obj Array to encode. | ||
* @param {object} [opts] Options. | ||
* @param {boolean} [opts.indefinite=false] Use indefinite encoding? | ||
* @returns {boolean} True on success. | ||
*/ | ||
@@ -700,3 +715,3 @@ static pushArray(gen, obj, opts) { | ||
* | ||
* @returns {boolean} - true when the Encoder was reset, else false | ||
* @returns {boolean} True when the Encoder was reset, else false. | ||
*/ | ||
@@ -712,5 +727,5 @@ removeLoopDetectors() { | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Date} obj - Date to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Date} obj Date to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -740,5 +755,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Buffer} obj - Buffer to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Buffer} obj Buffer to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -751,5 +766,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {NoFilter} obj - Buffer to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {NoFilter} obj Buffer to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -762,5 +777,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {RegExp} obj - RegExp to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {RegExp} obj RegExp to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -773,5 +788,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Set} obj - Set to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Set} obj Set to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -795,5 +810,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {URL} obj - URL to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {URL} obj URL to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -806,5 +821,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {object} obj - Boxed String, Number, or Boolean object to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {object} obj Boxed String, Number, or Boolean object to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -817,5 +832,6 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Map} obj - Map to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Map} obj Map to encode. | ||
* @returns {boolean} True on success. | ||
* @throws {Error} Map key that is undefined. | ||
* @ignore | ||
@@ -889,8 +905,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param { Uint8Array|Uint16Array|Uint32Array| | ||
* Int8Array|Int16Array|Int32Array| | ||
* Float32Array|Float64Array| | ||
* BigUint64Array|BigInt64Array } obj - Array to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {NodeJS.TypedArray} obj Array to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -930,5 +943,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param { ArrayBuffer } obj - Array to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param { ArrayBuffer } obj Array to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -943,8 +956,15 @@ */ | ||
* some (IMO) broken implementations of poorly-specified protocols that | ||
* REQUIRE indefinite-encoding. Add this to an object or class as the | ||
* `encodeCBOR` function to get indefinite encoding: | ||
* @example | ||
* REQUIRE indefinite-encoding. See the example for how to add this as an | ||
* `encodeCBOR` function to an object or class to get indefinite encoding. | ||
* | ||
* @param {Encoder} gen The encoder to use. | ||
* @param {string|Buffer|Array|Map|object} [obj] The object to encode. If | ||
* null, use "this" instead. | ||
* @param {EncodingOptions} [options={}] Options for encoding. | ||
* @returns {boolean} True on success. | ||
* @throws {Error} No object to encode or invalid indefinite encoding. | ||
* @example <caption>Force indefinite encoding:</caption> | ||
* const o = { | ||
* a: true, | ||
* encodeCBOR: cbor.Encoder.encodeIndefinite | ||
* encodeCBOR: cbor.Encoder.encodeIndefinite, | ||
* } | ||
@@ -954,8 +974,2 @@ * const m = [] | ||
* cbor.encodeOne([o, m]) | ||
* | ||
* @param {Encoder} gen - the encoder to use | ||
* @param {String|Buffer|Array|Map|Object} [obj] - the object to encode. If | ||
* null, use "this" instead. | ||
* @param {EncodingOptions} [options={}] - Options for encoding | ||
* @returns {boolean} - true on success | ||
*/ | ||
@@ -971,3 +985,3 @@ static encodeIndefinite(gen, obj, options = {}) { | ||
// TODO: consider other options | ||
const { chunkSize = 4096 } = options | ||
const {chunkSize = 4096} = options | ||
@@ -1021,4 +1035,4 @@ let ret = true | ||
* | ||
* @param {...any} objs - the objects to encode | ||
* @returns {Buffer} - the encoded objects | ||
* @param {...any} objs The objects to encode. | ||
* @returns {Buffer} The encoded objects. | ||
*/ | ||
@@ -1033,4 +1047,4 @@ static encode(...objs) { | ||
* | ||
* @param {...any} objs - the objects to encode | ||
* @returns {Buffer} - the encoded objects | ||
* @param {...any} objs The objects to encode. | ||
* @returns {Buffer} The encoded objects. | ||
*/ | ||
@@ -1047,5 +1061,5 @@ static encodeCanonical(...objs) { | ||
* @static | ||
* @param {any} obj - the object to encode | ||
* @param {EncodingOptions} [options={}] - passed to the Encoder constructor | ||
* @returns {Buffer} - the encoded objects | ||
* @param {any} obj The object to encode. | ||
* @param {EncodingOptions} [options={}] Passed to the Encoder constructor. | ||
* @returns {Buffer} The encoded objects. | ||
*/ | ||
@@ -1063,4 +1077,5 @@ static encodeOne(obj, options) { | ||
* | ||
* @param {any} obj - the object to encode | ||
* @param {EncodingOptions} [options={}] - passed to the Encoder constructor | ||
* @param {any} obj The object to encode. | ||
* @param {EncodingOptions} [options={}] Passed to the Encoder constructor. | ||
* @returns {Promise<Buffer>} A promise for the encoded buffer. | ||
*/ | ||
@@ -1081,2 +1096,3 @@ static encodeAsync(obj, options) { | ||
* The currently supported set of semantic types. May be modified by plugins. | ||
* | ||
* @type {SemanticMap} | ||
@@ -1083,0 +1099,0 @@ */ |
'use strict' | ||
const { Buffer } = require('buffer') | ||
const {Buffer} = require('buffer') | ||
const encoder = require('./encoder') | ||
const decoder = require('./decoder') | ||
const { MT } = require('./constants') | ||
const {MT} = require('./constants') | ||
@@ -17,4 +17,3 @@ /** | ||
* | ||
* @class CborMap | ||
* @extends {Map} | ||
* @extends Map | ||
*/ | ||
@@ -24,2 +23,3 @@ class CborMap extends Map { | ||
* Creates an instance of CborMap. | ||
* | ||
* @param {Iterable<any>} [iterable] An Array or other iterable | ||
@@ -35,3 +35,3 @@ * object whose elements are key-value pairs (arrays with two elements, e.g. | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -43,3 +43,3 @@ static _encode(key) { | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
@@ -68,3 +68,4 @@ static _decode(key) { | ||
* value. | ||
* @param {any} val The element to store | ||
* @param {any} val The element to store. | ||
* @returns {this} This object. | ||
*/ | ||
@@ -78,6 +79,6 @@ set(key, val) { | ||
* | ||
* @param {any} key The key identifying the element to delete. | ||
* Can be any type, which will be serialized into CBOR and compared by | ||
* value. | ||
* @returns {boolean} | ||
* @param {any} key The key identifying the element to delete. Can be any | ||
* type, which will be serialized into CBOR and compared by value. | ||
* @returns {boolean} True if an element in the Map object existed and has | ||
* been removed, or false if the element does not exist. | ||
*/ | ||
@@ -94,3 +95,4 @@ delete(key) { | ||
* value. | ||
* @returns {boolean} | ||
* @returns {boolean} True if an element with the specified key exists in | ||
* the Map object; otherwise false. | ||
*/ | ||
@@ -106,3 +108,3 @@ has(key) { | ||
* | ||
* @returns {IterableIterator<any>} | ||
* @yields {any} The keys of the map. | ||
*/ | ||
@@ -115,2 +117,3 @@ *keys() { | ||
/* eslint-disable jsdoc/require-returns-check */ | ||
/** | ||
@@ -120,3 +123,4 @@ * Returns a new Iterator object that contains the [key, value] pairs for | ||
* | ||
* @returns {IterableIterator} | ||
* @yields {any[]} Key value pairs. | ||
* @returns {IterableIterator<any, any>} Key value pairs. | ||
*/ | ||
@@ -128,2 +132,3 @@ *entries() { | ||
} | ||
/* eslint-enable jsdoc/require-returns-check */ | ||
@@ -134,3 +139,3 @@ /** | ||
* | ||
* @returns {IterableIterator} | ||
* @returns {IterableIterator} Key value pairs. | ||
*/ | ||
@@ -147,3 +152,4 @@ [Symbol.iterator]() { | ||
* each element, which takes a value, a key, and the Map being traversed. | ||
* @param {any} thisArg Value to use as this when executing callback | ||
* @param {any} thisArg Value to use as this when executing callback. | ||
* @throws {TypeError} Invalid function. | ||
*/ | ||
@@ -160,6 +166,6 @@ forEach(fun, thisArg) { | ||
/** | ||
* Push the simple value onto the CBOR stream | ||
* Push the simple value onto the CBOR stream. | ||
* | ||
* @param {Object} gen The generator to push onto | ||
* @returns {boolean} true on success | ||
* @param {object} gen The generator to push onto. | ||
* @returns {boolean} True on success. | ||
*/ | ||
@@ -166,0 +172,0 @@ encodeCBOR(gen) { |
@@ -12,3 +12,3 @@ 'use strict' | ||
* | ||
* @param {number} value - the simple value's integer value | ||
* @param {number} value The simple value's integer value. | ||
*/ | ||
@@ -26,5 +26,5 @@ constructor(value) { | ||
/** | ||
* Debug string for simple value | ||
* Debug string for simple value. | ||
* | ||
* @returns {string} simple(value) | ||
* @returns {string} Formated string of `simple(value)`. | ||
*/ | ||
@@ -36,7 +36,9 @@ toString() { | ||
/** | ||
* Debug string for simple value (backward-compatibility version) | ||
* Debug string for simple value. | ||
* | ||
* @returns {string} simple(value) | ||
* @param {number} depth How deep are we? | ||
* @param {object} opts Options. | ||
* @returns {string} Formatted string of `simple(value)`. | ||
*/ | ||
inspect(depth, opts) { | ||
[Symbol.for('nodejs.util.inspect.custom')](depth, opts) { | ||
return `simple(${this.value})` | ||
@@ -46,5 +48,6 @@ } | ||
/** | ||
* Push the simple value onto the CBOR stream | ||
* Push the simple value onto the CBOR stream. | ||
* | ||
* @param {Object} gen The generator to push onto | ||
* @param {object} gen The generator to push onto. | ||
* @returns {boolean} True on success. | ||
*/ | ||
@@ -58,4 +61,4 @@ encodeCBOR(gen) { | ||
* | ||
* @param {any} obj - object to test | ||
* @returns {boolean} - is it Simple? | ||
* @param {any} obj Object to test. | ||
* @returns {boolean} Is it Simple? | ||
*/ | ||
@@ -72,7 +75,8 @@ static isSimple(obj) { | ||
* | ||
* @param {number} val - the CBOR additional info to convert | ||
* @param {boolean} [has_parent=true] - Does the CBOR item have a parent? | ||
* @param {boolean} [parent_indefinite=false] - Is the parent element | ||
* @param {number} val The CBOR additional info to convert. | ||
* @param {boolean} [has_parent=true] Does the CBOR item have a parent? | ||
* @param {boolean} [parent_indefinite=false] Is the parent element | ||
* indefinitely encoded? | ||
* @returns {(null|undefined|boolean|Symbol|Simple)} - the decoded value | ||
* @returns {(null|undefined|boolean|symbol|Simple)} The decoded value. | ||
* @throws {Error} Invalid BREAK. | ||
*/ | ||
@@ -79,0 +83,0 @@ static decode(val, has_parent = true, parent_indefinite = false) { |
@@ -66,17 +66,20 @@ 'use strict' | ||
* @callback TagFunction | ||
* @param {any} value - the value inside the tag | ||
* @param {Tagged} tag - the enclosing Tagged instance; useful if you want to | ||
* @param {any} value The value inside the tag. | ||
* @param {Tagged} tag The enclosing Tagged instance; useful if you want to | ||
* modify it and return it. Also available as "this". | ||
* @return {any} the transformed value | ||
* @returns {any} The transformed value. | ||
*/ | ||
/* eslint-disable jsdoc/check-types */ | ||
/** | ||
* A mapping from tag number to a tag decoding function | ||
* A mapping from tag number to a tag decoding function. | ||
* | ||
* @typedef {Object.<string, TagFunction>} TagMap | ||
*/ | ||
/* eslint-enable jsdoc/check-types */ | ||
/** | ||
* @type {TagMap} | ||
* @private | ||
*/ | ||
* @type {TagMap} | ||
* @private | ||
*/ | ||
const TAGS = { | ||
@@ -256,5 +259,5 @@ // Standard date/time string; see Section 3.4.1 | ||
/** | ||
* @type {TagMap} | ||
* @private | ||
*/ | ||
* @type {TagMap} | ||
* @private | ||
*/ | ||
let current_TAGS = {} | ||
@@ -271,5 +274,5 @@ | ||
* | ||
* @param {number} tag - the number of the tag | ||
* @param {any} value - the value inside the tag | ||
* @param {Error} [err] - the error that was thrown parsing the tag, or null | ||
* @param {number} tag The number of the tag. | ||
* @param {any} value The value inside the tag. | ||
* @param {Error} [err] The error that was thrown parsing the tag, or null. | ||
*/ | ||
@@ -303,5 +306,5 @@ constructor(tag, value, err) { | ||
/** | ||
* Convert to a String | ||
* Convert to a String. | ||
* | ||
* @returns {string} string of the form '1(2)' | ||
* @returns {string} String of the form '1(2)'. | ||
*/ | ||
@@ -313,5 +316,6 @@ toString() { | ||
/** | ||
* Push the simple value onto the CBOR stream | ||
* Push the simple value onto the CBOR stream. | ||
* | ||
* @param {Object} gen The generator to push onto | ||
* @param {object} gen The generator to push onto. | ||
* @returns {boolean} True on success. | ||
*/ | ||
@@ -329,6 +333,6 @@ encodeCBOR(gen) { | ||
* | ||
* @param {Object} converters - keys in the object are a tag number, the value | ||
* @param {object} converters Keys in the object are a tag number, the value | ||
* is a function that takes the decoded CBOR and returns a JavaScript value | ||
* of the appropriate type. Throw an exception in the function on errors. | ||
* @returns {any} - the converted item | ||
* @returns {any} The converted item. | ||
*/ | ||
@@ -374,3 +378,3 @@ convert(converters) { | ||
static reset() { | ||
Tagged.TAGS = { ...TAGS } | ||
Tagged.TAGS = {...TAGS} | ||
} | ||
@@ -377,0 +381,0 @@ } |
'use strict' | ||
const { Buffer } = require('buffer') | ||
const {Buffer} = require('buffer') | ||
const NoFilter = require('nofilter') | ||
const stream = require('stream') | ||
const constants = require('./constants') | ||
const { NUMBYTES, SHIFT32, BI, SYMS } = constants | ||
const {NUMBYTES, SHIFT32, BI, SYMS} = constants | ||
const MAX_SAFE_HIGH = 0x1fffff | ||
@@ -13,2 +13,3 @@ | ||
* on invalid UTF8. Byte Order Marks are not looked at or stripped. | ||
* | ||
* @private | ||
@@ -246,4 +247,4 @@ */ | ||
} | ||
if (val && (typeof val.inspect === 'function')) { | ||
return val.inspect() | ||
if (typeof val[Symbol.for('nodejs.util.inspect.custom')] === 'function') { | ||
return val[Symbol.for('nodejs.util.inspect.custom')]() | ||
} | ||
@@ -282,4 +283,5 @@ // Shouldn't get non-empty arrays here | ||
/** | ||
* @param {Buffer|Uint8Array|Uint8ClampedArray|ArrayBuffer|DataView} buf - | ||
* Buffer to convert | ||
* @param {Buffer|Uint8Array|Uint8ClampedArray|ArrayBuffer|DataView} buf | ||
* Buffer to convert. | ||
* @returns {string} Base64url string. | ||
* @private | ||
@@ -294,4 +296,5 @@ */ | ||
/** | ||
* @param {Buffer|Uint8Array|Uint8ClampedArray|ArrayBuffer|DataView} buf - | ||
* Buffer to convert | ||
* @param {Buffer|Uint8Array|Uint8ClampedArray|ArrayBuffer|DataView} buf | ||
* Buffer to convert. | ||
* @returns {string} Base64 string. | ||
* @private | ||
@@ -298,0 +301,0 @@ */ |
{ | ||
"name": "cbor", | ||
"version": "8.0.0", | ||
"description": "Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).", | ||
"version": "8.0.2", | ||
"description": "Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).", | ||
"main": "./lib/cbor.js", | ||
@@ -31,3 +31,5 @@ "repository": { | ||
"cbor", | ||
"json" | ||
"json", | ||
"rfc7049", | ||
"rfc8949" | ||
], | ||
@@ -47,3 +49,3 @@ "author": { | ||
"dependencies": { | ||
"nofilter": "^3.0.2" | ||
"nofilter": "^3.0.3" | ||
}, | ||
@@ -62,3 +64,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "93d11477589995ec91c02df99b1dba9f01d583a7" | ||
"gitHead": "815ba89b6e4f5431cf4db4dffb73431bf1b6cd8e" | ||
} |
# cbor | ||
Encode and parse data in the Concise Binary Object Representation (CBOR) data format ([RFC7049](http://tools.ietf.org/html/rfc7049)). | ||
Encode and parse data in the Concise Binary Object Representation (CBOR) data format ([RFC8949](https://www.rfc-editor.org/rfc/rfc8949.html)). | ||
@@ -95,4 +95,4 @@ ## Supported Node.js versions | ||
``js | ||
cbor.encodeOne(Buffer.alloc(40000), {highWaterMark: 65535}) | ||
```js | ||
cbor.encodeOne(new ArrayBuffer(40000), {highWaterMark: 65535}) | ||
``` | ||
@@ -104,5 +104,5 @@ | ||
const enc = new cbor.Encoder() | ||
enc.on('data', buf => /* send the data somewhere */) | ||
enc.on('data', buf => /* Send the data somewhere */ null) | ||
enc.on('error', console.error) | ||
enc.on('finish', () => /* tell the consumer we are finished */) | ||
enc.on('finish', () => /* Tell the consumer we are finished */ null) | ||
@@ -257,3 +257,3 @@ enc.end(['foo', 1, false]) | ||
The tests for this package use a set of test vectors from RFC 7049 appendix A | ||
The tests for this package use a set of test vectors from RFC 8949 appendix A | ||
by importing a machine readable version of them from | ||
@@ -260,0 +260,0 @@ https://github.com/cbor/test-vectors. For these tests to work, you will need |
/// <reference types="node" /> | ||
export = Commented; | ||
/** | ||
* Generate the expanded format of RFC 7049, section 2.2.1 | ||
* Generate the expanded format of RFC 8949, section 3.2.2. | ||
* | ||
* @extends {stream.Transform} | ||
* @extends stream.Transform | ||
*/ | ||
@@ -15,7 +15,8 @@ declare class Commented extends stream.Transform { | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|stream.Readable} input | ||
* |DataView|stream.Readable} input Something to parse. | ||
* @param {CommentOptions|commentCallback|string|number} [options={}] | ||
* encoding, max_depth, or callback | ||
* @param {commentCallback} [cb] - called on completion | ||
* @returns {Promise} if cb not specified | ||
* Encoding, max_depth, or callback. | ||
* @param {commentCallback} [cb] If specified, called on completion. | ||
* @returns {Promise} If cb not specified. | ||
* @throws {Error} Input required. | ||
*/ | ||
@@ -26,3 +27,3 @@ static comment(input: string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | stream.Readable, options?: CommentOptions | commentCallback | string | number, cb?: commentCallback): Promise<any>; | ||
* | ||
* @param {CommentOptions} [options={}] - Stream options | ||
* @param {CommentOptions} [options={}] Stream options. | ||
*/ | ||
@@ -35,2 +36,3 @@ constructor(options?: CommentOptions); | ||
/** | ||
* @param {Buffer} v Descend into embedded CBOR. | ||
* @private | ||
@@ -40,25 +42,25 @@ */ | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_error; | ||
_on_error(er: any): void; | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_read; | ||
_on_read(buf: any): void; | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_more; | ||
_on_more(mt: any, len: any, parent_mt: any, pos: any): void; | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_start_string; | ||
_on_start_string(mt: any, len: any, parent_mt: any, pos: any): void; | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_start; | ||
_on_start(mt: any, tag: any, parent_mt: any, pos: any): void; | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_stop; | ||
_on_stop(mt: any): void; | ||
/** | ||
@@ -69,5 +71,5 @@ * @private | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private _on_data; | ||
_on_data(): void; | ||
} | ||
@@ -83,17 +85,17 @@ declare namespace Commented { | ||
/** | ||
* - how many times to indent | ||
* the dashes | ||
* How many times to indent | ||
* the dashes. | ||
*/ | ||
max_depth?: number; | ||
/** | ||
* - initial indentation depth | ||
* Initial indentation depth. | ||
*/ | ||
depth?: number; | ||
/** | ||
* - if true, omit the summary | ||
* of the full bytes read at the end | ||
* If true, omit the summary | ||
* of the full bytes read at the end. | ||
*/ | ||
no_summary?: boolean; | ||
/** | ||
* - mapping from tag number to function(v), | ||
* Mapping from tag number to function(v), | ||
* where v is the decoded value that comes after the tag, and where the | ||
@@ -104,3 +106,3 @@ * function returns the correctly-created value for that tag. | ||
/** | ||
* if true, prefer Uint8Arrays to | ||
* If true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
@@ -111,7 +113,7 @@ * changes in the future, so forward-compatibility is not guaranteed yet. | ||
/** | ||
* - Encoding to use for input, if it | ||
* is a string | ||
* Encoding to use for input, if it | ||
* is a string. | ||
*/ | ||
encoding?: string; | ||
encoding?: BufferEncoding; | ||
}; | ||
type commentCallback = (error?: Error, commented?: string) => void; |
@@ -9,3 +9,3 @@ /// <reference types="node" /> | ||
* | ||
* @extends {BinaryParseStream} | ||
* @extends BinaryParseStream | ||
*/ | ||
@@ -18,10 +18,10 @@ declare class Decoder extends BinaryParseStream { | ||
* @static | ||
* @param {any} val - the value to check | ||
* @returns {any} the corrected value | ||
* | ||
* @param {any} val The value to check. | ||
* @returns {any} The corrected value. | ||
* @throws {Error} Nothing was found. | ||
* @example | ||
* myDecoder.on('data', function(val) { | ||
* val = Decoder.nullcheck(val); | ||
* ... | ||
* }); | ||
* myDecoder.on('data', val => { | ||
* val = Decoder.nullcheck(val) | ||
* // ... | ||
* }) | ||
*/ | ||
@@ -35,10 +35,11 @@ static nullcheck(val: any): any; | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input - If a Readable stream, must have | ||
* @param {BufferLike} input If a Readable stream, must have | ||
* received the `readable` event already, or you will get an error | ||
* claiming "Insufficient data" | ||
* @param {DecoderOptions|string} [options={}] Options or encoding for input | ||
* @returns {any} - the decoded value | ||
* claiming "Insufficient data". | ||
* @param {DecoderOptions|string} [options={}] Options or encoding for input. | ||
* @returns {ExtendedResults|any} The decoded value. | ||
* @throws {UnexpectedDataError} Data is left over after decoding. | ||
* @throws {Error} Insufficient data. | ||
*/ | ||
static decodeFirstSync(input: string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | ReadableStream, options?: DecoderOptions | string): any; | ||
static decodeFirstSync(input: BufferLike, options?: DecoderOptions | string): ExtendedResults | any; | ||
/** | ||
@@ -50,9 +51,10 @@ * Decode all of the CBOR items in the input into an array. This will throw | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input | ||
* @param {BufferLike} input What to parse? | ||
* @param {DecoderOptions|string} [options={}] Options or encoding | ||
* for input | ||
* @returns {Array} - Array of all found items | ||
* for input. | ||
* @returns {Array<ExtendedResults>|Array<any>} Array of all found items. | ||
* @throws {TypeError} No input provided. | ||
* @throws {Error} Insufficient data provided. | ||
*/ | ||
static decodeAllSync(input: string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | ReadableStream, options?: DecoderOptions | string): any[]; | ||
static decodeAllSync(input: BufferLike, options?: DecoderOptions | string): Array<ExtendedResults> | Array<any>; | ||
/** | ||
@@ -66,14 +68,16 @@ * Decode the first CBOR item in the input. This will error if there are | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input | ||
* @param {DecoderOptions|decodeCallback|string} [options={}] - options, the | ||
* callback, or input encoding | ||
* @param {decodeCallback} [cb] callback | ||
* @returns {Promise<any>} returned even if callback is specified | ||
* @param {BufferLike} input What to parse? | ||
* @param {DecoderOptions|decodeCallback|string} [options={}] Options, the | ||
* callback, or input encoding. | ||
* @param {decodeCallback} [cb] Callback. | ||
* @returns {Promise<ExtendedResults|any>} Returned even if callback is | ||
* specified. | ||
* @throws {TypeError} No input provided. | ||
*/ | ||
static decodeFirst(input: string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | ReadableStream, options?: DecoderOptions | decodeCallback | string, cb?: decodeCallback): Promise<any>; | ||
static decodeFirst(input: BufferLike, options?: DecoderOptions | decodeCallback | string, cb?: decodeCallback): Promise<ExtendedResults | any>; | ||
/** | ||
* @callback decodeAllCallback | ||
* @param {Error} error - if one was generated | ||
* @param {Array} value - all of the decoded values, wrapped in an Array | ||
* @param {Error} error If one was generated. | ||
* @param {Array<ExtendedResults>|Array<any>} value All of the decoded | ||
* values, wrapped in an Array. | ||
*/ | ||
@@ -85,14 +89,15 @@ /** | ||
* @static | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|ReadableStream} input | ||
* @param {DecoderOptions|decodeAllCallback|string} [options={}] - | ||
* @param {BufferLike} input What to parse? | ||
* @param {DecoderOptions|decodeAllCallback|string} [options={}] | ||
* Decoding options, the callback, or the input encoding. | ||
* @param {decodeAllCallback} [cb] callback | ||
* @returns {Promise<Array>} even if callback is specified | ||
* @param {decodeAllCallback} [cb] Callback. | ||
* @returns {Promise<Array<ExtendedResults>|Array<any>>} Even if callback | ||
* is specified. | ||
* @throws {TypeError} No input specified. | ||
*/ | ||
static decodeAll(input: string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | ReadableStream, options?: string | DecoderOptions | ((error: Error, value: any[]) => any), cb?: (error: Error, value: any[]) => any): Promise<any[]>; | ||
static decodeAll(input: BufferLike, options?: string | DecoderOptions | ((error: Error, value: Array<ExtendedResults> | Array<any>) => any), cb?: (error: Error, value: Array<ExtendedResults> | Array<any>) => any): Promise<Array<ExtendedResults> | Array<any>>; | ||
/** | ||
* Create a parsing stream. | ||
* | ||
* @param {DecoderOptions} [options={}] | ||
* @param {DecoderOptions} [options={}] Options. | ||
*/ | ||
@@ -110,7 +115,8 @@ constructor(options?: DecoderOptions); | ||
/** | ||
* Stop processing | ||
* Stop processing. | ||
*/ | ||
close(): void; | ||
/** | ||
* Only called if extendedResults is true | ||
* Only called if extendedResults is true. | ||
* | ||
* @ignore | ||
@@ -121,3 +127,3 @@ */ | ||
declare namespace Decoder { | ||
export { NOT_FOUND, ExtendedResults, DecoderOptions, decodeCallback }; | ||
export { NOT_FOUND, BufferLike, ExtendedResults, DecoderOptions, decodeCallback }; | ||
} | ||
@@ -127,6 +133,9 @@ import BinaryParseStream = require("../vendor/binary-parse-stream"); | ||
import NoFilter = require("nofilter"); | ||
import { Buffer } from "buffer"; | ||
/** | ||
* Things that can act as inputs, from which a NoFilter can be created. | ||
*/ | ||
type BufferLike = string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | stream.Readable; | ||
type DecoderOptions = { | ||
/** | ||
* - the maximum depth to parse. | ||
* The maximum depth to parse. | ||
* Use -1 for "until you run out of memory". Set this to a finite | ||
@@ -139,3 +148,3 @@ * positive number for un-trusted inputs. Most standard inputs won't nest | ||
/** | ||
* - mapping from tag number to function(v), | ||
* Mapping from tag number to function(v), | ||
* where v is the decoded value that comes after the tag, and where the | ||
@@ -146,3 +155,3 @@ * function returns the correctly-created value for that tag. | ||
/** | ||
* if true, prefer Uint8Arrays to | ||
* If true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
@@ -153,8 +162,8 @@ * changes in the future, so forward-compatibility is not guaranteed yet. | ||
/** | ||
* - The encoding of the input. | ||
* The encoding of the input. | ||
* Ignored if input is a Buffer. | ||
*/ | ||
encoding?: string; | ||
encoding?: BufferEncoding; | ||
/** | ||
* - Should an error be thrown when no | ||
* Should an error be thrown when no | ||
* data is in the input? | ||
@@ -164,3 +173,3 @@ */ | ||
/** | ||
* - if true, emit extended | ||
* If true, emit extended | ||
* results, which will be an object with shape {@link ExtendedResults }. | ||
@@ -171,25 +180,27 @@ * The value will already have been null-checked. | ||
}; | ||
type decodeCallback = (error?: Error, value?: any) => void; | ||
declare const NOT_FOUND: unique symbol; | ||
type ExtendedResults = { | ||
/** | ||
* - the value that was found | ||
* The value that was found. | ||
*/ | ||
value: any; | ||
/** | ||
* - the number of bytes of the original input that | ||
* were read | ||
* The number of bytes of the original input that | ||
* were read. | ||
*/ | ||
length: number; | ||
/** | ||
* - the bytes of the original input that were used | ||
* to produce the value | ||
* The bytes of the original input that were used | ||
* to produce the value. | ||
*/ | ||
bytes: Buffer; | ||
/** | ||
* - the bytes that were left over from the original | ||
* input. This property only exists if {@linkcode Decoder.decodeFirst} or | ||
* {@linkcode Decoder.decodeFirstSync} was called. | ||
* The bytes that were left over from the original | ||
* input. This property only exists if {@link Decoder.decodeFirst } or | ||
* {@link Decoder.decodeFirstSync } was called. | ||
*/ | ||
unused?: Buffer; | ||
}; | ||
type decodeCallback = (error?: Error, value?: any) => void; | ||
declare const NOT_FOUND: unique symbol; | ||
import { Buffer } from "buffer"; | ||
import stream = require("stream"); |
@@ -5,3 +5,3 @@ export = Diagnose; | ||
* | ||
* @extends {stream.Transform} | ||
* @extends stream.Transform | ||
*/ | ||
@@ -12,14 +12,14 @@ declare class Diagnose extends stream.Transform { | ||
* | ||
* @param {string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray | ||
* |DataView|stream.Readable} input - the CBOR bytes to format | ||
* @param {DiagnoseOptions |diagnoseCallback|string} [options={}] - | ||
* options, the callback, or the input encoding | ||
* @param {diagnoseCallback} [cb] - callback | ||
* @returns {Promise} if callback not specified | ||
* @param {BufferLike} input The CBOR bytes to format. | ||
* @param {DiagnoseOptions |diagnoseCallback|string} [options={}] | ||
* Options, the callback, or the input encoding. | ||
* @param {diagnoseCallback} [cb] Callback. | ||
* @throws {TypeError} Input not provided. | ||
* @returns {Promise} If callback not specified. | ||
*/ | ||
static diagnose(input: string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | stream.Readable, options?: DiagnoseOptions | diagnoseCallback | string, cb?: diagnoseCallback): Promise<any>; | ||
static diagnose(input: BufferLike, options?: DiagnoseOptions | diagnoseCallback | string, cb?: diagnoseCallback): Promise<any>; | ||
/** | ||
* Creates an instance of Diagnose. | ||
* | ||
* @param {DiagnoseOptions} [options={}] - options for creation | ||
* @param {DiagnoseOptions} [options={}] Options for creation. | ||
*/ | ||
@@ -31,5 +31,7 @@ constructor(options?: DiagnoseOptions); | ||
parser: Decoder; | ||
/** | ||
* @ignore | ||
*/ | ||
_on_error(er: any): void; | ||
/** @private */ | ||
private _on_error; | ||
/** @private */ | ||
private _on_more; | ||
@@ -48,18 +50,22 @@ /** @private */ | ||
declare namespace Diagnose { | ||
export { DiagnoseOptions, diagnoseCallback }; | ||
export { BufferLike, DiagnoseOptions, diagnoseCallback }; | ||
} | ||
import stream = require("stream"); | ||
import Decoder = require("./decoder"); | ||
/** | ||
* Things that can act as inputs, from which a NoFilter can be created. | ||
*/ | ||
type BufferLike = string | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | DataView | stream.Readable; | ||
type DiagnoseOptions = { | ||
/** | ||
* - output between detected objects | ||
* Output between detected objects. | ||
*/ | ||
separator?: string; | ||
/** | ||
* - put error info into the | ||
* output stream | ||
* Put error info into the | ||
* output stream. | ||
*/ | ||
stream_errors?: boolean; | ||
/** | ||
* - the maximum depth to parse. | ||
* The maximum depth to parse. | ||
* Use -1 for "until you run out of memory". Set this to a finite | ||
@@ -72,3 +78,3 @@ * positive number for un-trusted inputs. Most standard inputs won't nest | ||
/** | ||
* - mapping from tag number to function(v), | ||
* Mapping from tag number to function(v), | ||
* where v is the decoded value that comes after the tag, and where the | ||
@@ -79,3 +85,3 @@ * function returns the correctly-created value for that tag. | ||
/** | ||
* - if true, prefer Uint8Arrays to | ||
* If true, prefer Uint8Arrays to | ||
* be generated instead of node Buffers. This might turn on some more | ||
@@ -86,7 +92,7 @@ * changes in the future, so forward-compatibility is not guaranteed yet. | ||
/** | ||
* - the encoding of input, ignored if | ||
* input is not string | ||
* The encoding of input, ignored if | ||
* input is not string. | ||
*/ | ||
encoding?: string; | ||
encoding?: BufferEncoding; | ||
}; | ||
type diagnoseCallback = (error?: Error, value?: string) => void; |
@@ -5,9 +5,9 @@ /// <reference types="node" /> | ||
* @typedef EncodingOptions | ||
* @property {any[]|Object} [genTypes=[]] - array of pairs of | ||
* @property {any[]|object} [genTypes=[]] Array of pairs of | ||
* `type`, `function(Encoder)` for semantic types to be encoded. Not | ||
* needed for Array, Date, Buffer, Map, RegExp, Set, or URL. | ||
* If an object, the keys are the constructor names for the types. | ||
* @property {boolean} [canonical=false] - should the output be | ||
* canonicalized | ||
* @property {boolean|WeakSet} [detectLoops=false] - should object loops | ||
* @property {boolean} [canonical=false] Should the output be | ||
* canonicalized. | ||
* @property {boolean|WeakSet} [detectLoops=false] Should object loops | ||
* be detected? This will currently add memory to track every part of the | ||
@@ -22,3 +22,3 @@ * object being encoded in a WeakSet. Do not encode | ||
* fractional seconds. | ||
* @property {any} [encodeUndefined=undefined] - How should an | ||
* @property {any} [encodeUndefined=undefined] How should an | ||
* "undefined" in the input be encoded. By default, just encode a CBOR | ||
@@ -30,3 +30,3 @@ * undefined. If this is a buffer, use those bytes without re-encoding | ||
* be encoded as CBOR. | ||
* @property {boolean} [disallowUndefinedKeys=false] - Should | ||
* @property {boolean} [disallowUndefinedKeys=false] Should | ||
* "undefined" be disallowed as a key in a Map that is serialized? If | ||
@@ -36,8 +36,8 @@ * this is true, encode(new Map([[undefined, 1]])) will throw an | ||
* normal JS object. | ||
* @property {boolean} [collapseBigIntegers=false] - Should integers | ||
* @property {boolean} [collapseBigIntegers=false] Should integers | ||
* that come in as ECMAscript bigint's be encoded | ||
* as normal CBOR integers if they fit, discarding type information? | ||
* @property {number} [chunkSize=4096] - Number of characters or bytes | ||
* for each chunk, if obj is a string or Buffer, when indefinite encoding | ||
* @property {boolean} [omitUndefinedProperties=false] - When encoding | ||
* @property {number} [chunkSize=4096] Number of characters or bytes | ||
* for each chunk, if obj is a string or Buffer, when indefinite encoding. | ||
* @property {boolean} [omitUndefinedProperties=false] When encoding | ||
* objects or Maps, do not include a key if its corresponding value is | ||
@@ -50,3 +50,3 @@ * `undefined`. | ||
* | ||
* @extends {stream.Transform} | ||
* @extends stream.Transform | ||
*/ | ||
@@ -57,7 +57,7 @@ declare class Encoder extends stream.Transform { | ||
* | ||
* @param {Encoder} gen - Encoder to use | ||
* @param {any[]} obj - Array to encode | ||
* @param {Object} [opts] - options | ||
* @param {boolean} [opts.indefinite=false] - Use indefinite encoding? | ||
* @returns {boolean} true on success | ||
* @param {Encoder} gen Encoder to use. | ||
* @param {any[]} obj Array to encode. | ||
* @param {object} [opts] Options. | ||
* @param {boolean} [opts.indefinite=false] Use indefinite encoding? | ||
* @returns {boolean} True on success. | ||
*/ | ||
@@ -68,5 +68,5 @@ static pushArray(gen: Encoder, obj: any[], opts?: { | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Date} obj - Date to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Date} obj Date to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -76,5 +76,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Buffer} obj - Buffer to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Buffer} obj Buffer to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -84,5 +84,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {NoFilter} obj - Buffer to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {NoFilter} obj Buffer to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -92,5 +92,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {RegExp} obj - RegExp to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {RegExp} obj RegExp to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -100,5 +100,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Set} obj - Set to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Set} obj Set to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -108,5 +108,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {URL} obj - URL to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {URL} obj URL to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -116,5 +116,5 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {object} obj - Boxed String, Number, or Boolean object to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {object} obj Boxed String, Number, or Boolean object to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -124,5 +124,6 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param {Map} obj - Map to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {Map} obj Map to encode. | ||
* @returns {boolean} True on success. | ||
* @throws {Error} Map key that is undefined. | ||
* @ignore | ||
@@ -132,15 +133,12 @@ */ | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param { Uint8Array|Uint16Array|Uint32Array| | ||
* Int8Array|Int16Array|Int32Array| | ||
* Float32Array|Float64Array| | ||
* BigUint64Array|BigInt64Array } obj - Array to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param {NodeJS.TypedArray} obj Array to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
*/ | ||
static _pushTypedArray(gen: Encoder, obj: Uint8Array | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigUint64Array | BigInt64Array): boolean; | ||
static _pushTypedArray(gen: Encoder, obj: NodeJS.TypedArray): boolean; | ||
/** | ||
* @param {Encoder} gen - Encoder | ||
* @param { ArrayBuffer } obj - Array to encode | ||
* @returns {boolean} True on success | ||
* @param {Encoder} gen Encoder. | ||
* @param { ArrayBuffer } obj Array to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -152,8 +150,15 @@ */ | ||
* some (IMO) broken implementations of poorly-specified protocols that | ||
* REQUIRE indefinite-encoding. Add this to an object or class as the | ||
* `encodeCBOR` function to get indefinite encoding: | ||
* @example | ||
* REQUIRE indefinite-encoding. See the example for how to add this as an | ||
* `encodeCBOR` function to an object or class to get indefinite encoding. | ||
* | ||
* @param {Encoder} gen The encoder to use. | ||
* @param {string|Buffer|Array|Map|object} [obj] The object to encode. If | ||
* null, use "this" instead. | ||
* @param {EncodingOptions} [options={}] Options for encoding. | ||
* @returns {boolean} True on success. | ||
* @throws {Error} No object to encode or invalid indefinite encoding. | ||
* @example <caption>Force indefinite encoding:</caption> | ||
* const o = { | ||
* a: true, | ||
* encodeCBOR: cbor.Encoder.encodeIndefinite | ||
* encodeCBOR: cbor.Encoder.encodeIndefinite, | ||
* } | ||
@@ -163,10 +168,4 @@ * const m = [] | ||
* cbor.encodeOne([o, m]) | ||
* | ||
* @param {Encoder} gen - the encoder to use | ||
* @param {String|Buffer|Array|Map|Object} [obj] - the object to encode. If | ||
* null, use "this" instead. | ||
* @param {EncodingOptions} [options={}] - Options for encoding | ||
* @returns {boolean} - true on success | ||
*/ | ||
static encodeIndefinite(gen: Encoder, obj?: string | Buffer | any[] | Map<any, any> | any, options?: EncodingOptions): boolean; | ||
static encodeIndefinite(gen: Encoder, obj?: string | Buffer | any[] | Map<any, any> | object, options?: EncodingOptions): boolean; | ||
/** | ||
@@ -176,4 +175,4 @@ * Encode one or more JavaScript objects, and return a Buffer containing the | ||
* | ||
* @param {...any} objs - the objects to encode | ||
* @returns {Buffer} - the encoded objects | ||
* @param {...any} objs The objects to encode. | ||
* @returns {Buffer} The encoded objects. | ||
*/ | ||
@@ -185,4 +184,4 @@ static encode(...objs: any[]): Buffer; | ||
* | ||
* @param {...any} objs - the objects to encode | ||
* @returns {Buffer} - the encoded objects | ||
* @param {...any} objs The objects to encode. | ||
* @returns {Buffer} The encoded objects. | ||
*/ | ||
@@ -194,5 +193,5 @@ static encodeCanonical(...objs: any[]): Buffer; | ||
* @static | ||
* @param {any} obj - the object to encode | ||
* @param {EncodingOptions} [options={}] - passed to the Encoder constructor | ||
* @returns {Buffer} - the encoded objects | ||
* @param {any} obj The object to encode. | ||
* @param {EncodingOptions} [options={}] Passed to the Encoder constructor. | ||
* @returns {Buffer} The encoded objects. | ||
*/ | ||
@@ -207,6 +206,7 @@ static encodeOne(obj: any, options?: EncodingOptions): Buffer; | ||
* | ||
* @param {any} obj - the object to encode | ||
* @param {EncodingOptions} [options={}] - passed to the Encoder constructor | ||
* @param {any} obj The object to encode. | ||
* @param {EncodingOptions} [options={}] Passed to the Encoder constructor. | ||
* @returns {Promise<Buffer>} A promise for the encoded buffer. | ||
*/ | ||
static encodeAsync(obj: any, options?: EncodingOptions): Promise<any>; | ||
static encodeAsync(obj: any, options?: EncodingOptions): Promise<Buffer>; | ||
static set SEMANTIC_TYPES(arg: { | ||
@@ -217,2 +217,3 @@ [x: string]: EncodeFunction; | ||
* The currently supported set of semantic types. May be modified by plugins. | ||
* | ||
* @type {SemanticMap} | ||
@@ -231,3 +232,3 @@ */ | ||
* | ||
* @param {EncodingOptions} [options={}] - options for the encoder | ||
* @param {EncodingOptions} [options={}] Options for the encoder. | ||
*/ | ||
@@ -240,3 +241,3 @@ constructor(options?: EncodingOptions); | ||
collapseBigIntegers: boolean; | ||
/** @type WeakSet<any>? */ | ||
/** @type {WeakSet?} */ | ||
detectLoops: WeakSet<any> | null; | ||
@@ -248,4 +249,4 @@ omitUndefinedProperties: boolean; | ||
/** | ||
* @param {number} val - Number(0-255) to encode | ||
* @returns {boolean} true on success | ||
* @param {number} val Number(0-255) to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -255,4 +256,4 @@ */ | ||
/** | ||
* @param {number} val - Number(0-65535) to encode | ||
* @returns {boolean} true on success | ||
* @param {number} val Number(0-65535) to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -262,4 +263,4 @@ */ | ||
/** | ||
* @param {number} val - Number(0..2**32-1) to encode | ||
* @returns {boolean} true on success | ||
* @param {number} val Number(0..2**32-1) to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -269,4 +270,4 @@ */ | ||
/** | ||
* @param {number} val - Number to encode as 4-byte float | ||
* @returns {boolean} true on success | ||
* @param {number} val Number to encode as 4-byte float. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -276,4 +277,4 @@ */ | ||
/** | ||
* @param {number} val - Number to encode as 8-byte double | ||
* @returns {boolean} true on success | ||
* @param {number} val Number to encode as 8-byte double. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -283,3 +284,3 @@ */ | ||
/** | ||
* @returns {boolean} true on success | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -289,4 +290,4 @@ */ | ||
/** | ||
* @param {number} obj - Positive or negative infinity | ||
* @returns {boolean} true on success | ||
* @param {number} obj Positive or negative infinity. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -298,5 +299,5 @@ */ | ||
* | ||
* @param {number} obj - A number that is known to be not-integer, but not | ||
* how many bytes of precision it needs | ||
* @returns {boolean} true on success | ||
* @param {number} obj A number that is known to be not-integer, but not | ||
* how many bytes of precision it needs. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -310,11 +311,11 @@ */ | ||
* | ||
* @param {number} obj - A positive number that is known to be an integer, | ||
* but not how many bytes of precision it needs | ||
* @param {number} mt - The Major Type number to combine with the integer. | ||
* @param {number} obj A positive number that is known to be an integer, | ||
* but not how many bytes of precision it needs. | ||
* @param {number} mt The Major Type number to combine with the integer. | ||
* Not yet shifted. | ||
* @param {number} [orig] - The number before it was transformed to positive. | ||
* @param {number} [orig] The number before it was transformed to positive. | ||
* If the mt is NEG_INT, and the positive number is over MAX_SAFE_INT, | ||
* then we'll encode this as a float rather than making the number | ||
* negative again and losing precision. | ||
* @returns {boolean} true on success | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -326,5 +327,5 @@ */ | ||
* | ||
* @param {number} obj - A number that is known to be an integer, | ||
* but not how many bytes of precision it needs | ||
* @returns {boolean} true on success | ||
* @param {number} obj A number that is known to be an integer, | ||
* but not how many bytes of precision it needs. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -334,4 +335,4 @@ */ | ||
/** | ||
* @param {number} obj - plain JS number to encode | ||
* @returns {boolean} true on success | ||
* @param {number} obj Plain JS number to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -341,4 +342,4 @@ */ | ||
/** | ||
* @param {string} obj - string to encode | ||
* @returns {boolean} true on success | ||
* @param {string} obj String to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -348,4 +349,4 @@ */ | ||
/** | ||
* @param {boolean} obj - bool to encode | ||
* @returns {boolean} true on success | ||
* @param {boolean} obj Bool to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -355,4 +356,4 @@ */ | ||
/** | ||
* @param {undefined} obj - ignored | ||
* @returns {boolean} true on success | ||
* @param {undefined} obj Ignored. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -362,4 +363,4 @@ */ | ||
/** | ||
* @param {null} obj - ignored | ||
* @returns {boolean} true on success | ||
* @param {null} obj Ignored. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -369,4 +370,4 @@ */ | ||
/** | ||
* @param {number} tag - Tag number to encode | ||
* @returns {boolean} true on success | ||
* @param {number} tag Tag number to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -376,4 +377,4 @@ */ | ||
/** | ||
* @param {bigint} obj - BigInt to encode | ||
* @returns {boolean} true on success | ||
* @param {bigint} obj BigInt to encode. | ||
* @returns {boolean} True on success. | ||
* @ignore | ||
@@ -383,4 +384,5 @@ */ | ||
/** | ||
* @param {object} obj - object to encode | ||
* @returns {boolean} true on success | ||
* @param {object} obj Object to encode. | ||
* @returns {boolean} True on success. | ||
* @throws {Error} Loop detected. | ||
* @ignore | ||
@@ -390,4 +392,4 @@ */ | ||
/** | ||
* @param {any[]} objs - Array of supported things | ||
* @returns {Buffer} Concatenation of encodings for the supported things | ||
* @param {any[]} objs Array of supported things. | ||
* @returns {Buffer} Concatenation of encodings for the supported things. | ||
* @ignore | ||
@@ -398,15 +400,17 @@ */ | ||
* 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 | ||
* is useful for objects for which you can't add an encodeCBOR method. | ||
* | ||
* @param {string|function} type - The type to encode | ||
* @param {EncodeFunction} fun - The encoder to use | ||
* @param {string|Function} type The type to encode. | ||
* @param {EncodeFunction} fun The encoder to use. | ||
* @returns {EncodeFunction?} The previous encoder or undefined if there | ||
* wasn't one. | ||
* @throws {TypeError} Invalid function. | ||
*/ | ||
addSemanticType(type: string | Function, fun: EncodeFunction): EncodeFunction | null; | ||
/** | ||
* Push any supported type onto the encoded stream | ||
* Push any supported type onto the encoded stream. | ||
* | ||
* @param {any} obj | ||
* @returns {boolean} true on success | ||
* @param {any} obj The thing to encode. | ||
* @returns {boolean} True on success. | ||
* @throws {TypeError} Unknown type for obj. | ||
*/ | ||
@@ -417,3 +421,3 @@ pushAny(obj: any): boolean; | ||
* | ||
* @returns {boolean} - true when the Encoder was reset, else false | ||
* @returns {boolean} True when the Encoder was reset, else false. | ||
*/ | ||
@@ -436,3 +440,3 @@ removeLoopDetectors(): boolean; | ||
/** | ||
* - array of pairs of | ||
* Array of pairs of | ||
* `type`, `function(Encoder)` for semantic types to be encoded. Not | ||
@@ -442,10 +446,10 @@ * needed for Array, Date, Buffer, Map, RegExp, Set, or URL. | ||
*/ | ||
genTypes?: any[] | any; | ||
genTypes?: any[] | object; | ||
/** | ||
* - should the output be | ||
* canonicalized | ||
* Should the output be | ||
* canonicalized. | ||
*/ | ||
canonical?: boolean; | ||
/** | ||
* - should object loops | ||
* Should object loops | ||
* be detected? This will currently add memory to track every part of the | ||
@@ -466,3 +470,3 @@ * object being encoded in a WeakSet. Do not encode | ||
/** | ||
* - How should an | ||
* How should an | ||
* "undefined" in the input be encoded. By default, just encode a CBOR | ||
@@ -477,3 +481,3 @@ * undefined. If this is a buffer, use those bytes without re-encoding | ||
/** | ||
* - Should | ||
* Should | ||
* "undefined" be disallowed as a key in a Map that is serialized? If | ||
@@ -486,3 +490,3 @@ * this is true, encode(new Map([[undefined, 1]])) will throw an | ||
/** | ||
* - Should integers | ||
* Should integers | ||
* that come in as ECMAscript bigint's be encoded | ||
@@ -493,8 +497,8 @@ * as normal CBOR integers if they fit, discarding type information? | ||
/** | ||
* - Number of characters or bytes | ||
* for each chunk, if obj is a string or Buffer, when indefinite encoding | ||
* Number of characters or bytes | ||
* for each chunk, if obj is a string or Buffer, when indefinite encoding. | ||
*/ | ||
chunkSize?: number; | ||
/** | ||
* - When encoding | ||
* When encoding | ||
* objects or Maps, do not include a key if its corresponding value is | ||
@@ -506,3 +510,3 @@ * `undefined`. | ||
/** | ||
* A mapping from tag number to a tag decoding function | ||
* A mapping from tag number to a tag decoding function. | ||
*/ | ||
@@ -509,0 +513,0 @@ type SemanticMap = { |
@@ -11,16 +11,16 @@ export = CborMap; | ||
* | ||
* @class CborMap | ||
* @extends {Map} | ||
* @extends Map | ||
*/ | ||
declare class CborMap extends Map<any, any> { | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private static _encode; | ||
static _encode(key: any): string; | ||
/** | ||
* @private | ||
* @ignore | ||
*/ | ||
private static _decode; | ||
static _decode(key: any): any; | ||
/** | ||
* Creates an instance of CborMap. | ||
* | ||
* @param {Iterable<any>} [iterable] An Array or other iterable | ||
@@ -33,8 +33,8 @@ * object whose elements are key-value pairs (arrays with two elements, e.g. | ||
/** | ||
* Push the simple value onto the CBOR stream | ||
* Push the simple value onto the CBOR stream. | ||
* | ||
* @param {Object} gen The generator to push onto | ||
* @returns {boolean} true on success | ||
* @param {object} gen The generator to push onto. | ||
* @returns {boolean} True on success. | ||
*/ | ||
encodeCBOR(gen: any): boolean; | ||
encodeCBOR(gen: object): boolean; | ||
} |
@@ -9,4 +9,4 @@ export = Simple; | ||
* | ||
* @param {any} obj - object to test | ||
* @returns {boolean} - is it Simple? | ||
* @param {any} obj Object to test. | ||
* @returns {boolean} Is it Simple? | ||
*/ | ||
@@ -20,13 +20,14 @@ static isSimple(obj: any): boolean; | ||
* | ||
* @param {number} val - the CBOR additional info to convert | ||
* @param {boolean} [has_parent=true] - Does the CBOR item have a parent? | ||
* @param {boolean} [parent_indefinite=false] - Is the parent element | ||
* @param {number} val The CBOR additional info to convert. | ||
* @param {boolean} [has_parent=true] Does the CBOR item have a parent? | ||
* @param {boolean} [parent_indefinite=false] Is the parent element | ||
* indefinitely encoded? | ||
* @returns {(null|undefined|boolean|Symbol|Simple)} - the decoded value | ||
* @returns {(null|undefined|boolean|symbol|Simple)} The decoded value. | ||
* @throws {Error} Invalid BREAK. | ||
*/ | ||
static decode(val: number, has_parent?: boolean, parent_indefinite?: boolean): (null | undefined | boolean | Symbol | Simple); | ||
static decode(val: number, has_parent?: boolean, parent_indefinite?: boolean): (null | undefined | boolean | symbol | Simple); | ||
/** | ||
* Creates an instance of Simple. | ||
* | ||
* @param {number} value - the simple value's integer value | ||
* @param {number} value The simple value's integer value. | ||
*/ | ||
@@ -36,19 +37,14 @@ constructor(value: number); | ||
/** | ||
* Debug string for simple value | ||
* Debug string for simple value. | ||
* | ||
* @returns {string} simple(value) | ||
* @returns {string} Formated string of `simple(value)`. | ||
*/ | ||
toString(): string; | ||
/** | ||
* Debug string for simple value (backward-compatibility version) | ||
* Push the simple value onto the CBOR stream. | ||
* | ||
* @returns {string} simple(value) | ||
* @param {object} gen The generator to push onto. | ||
* @returns {boolean} True on success. | ||
*/ | ||
inspect(depth: any, opts: any): string; | ||
/** | ||
* Push the simple value onto the CBOR stream | ||
* | ||
* @param {Object} gen The generator to push onto | ||
*/ | ||
encodeCBOR(gen: any): any; | ||
encodeCBOR(gen: object): boolean; | ||
} |
@@ -28,5 +28,5 @@ export = Tagged; | ||
* | ||
* @param {number} tag - the number of the tag | ||
* @param {any} value - the value inside the tag | ||
* @param {Error} [err] - the error that was thrown parsing the tag, or null | ||
* @param {number} tag The number of the tag. | ||
* @param {any} value The value inside the tag. | ||
* @param {Error} [err] The error that was thrown parsing the tag, or null. | ||
*/ | ||
@@ -39,13 +39,14 @@ constructor(tag: number, value: any, err?: Error); | ||
/** | ||
* Convert to a String | ||
* Convert to a String. | ||
* | ||
* @returns {string} string of the form '1(2)' | ||
* @returns {string} String of the form '1(2)'. | ||
*/ | ||
toString(): string; | ||
/** | ||
* Push the simple value onto the CBOR stream | ||
* Push the simple value onto the CBOR stream. | ||
* | ||
* @param {Object} gen The generator to push onto | ||
* @param {object} gen The generator to push onto. | ||
* @returns {boolean} True on success. | ||
*/ | ||
encodeCBOR(gen: any): any; | ||
encodeCBOR(gen: object): boolean; | ||
/** | ||
@@ -57,8 +58,8 @@ * If we have a converter for this type, do the conversion. Some converters | ||
* | ||
* @param {Object} converters - keys in the object are a tag number, the value | ||
* @param {object} converters Keys in the object are a tag number, the value | ||
* is a function that takes the decoded CBOR and returns a JavaScript value | ||
* of the appropriate type. Throw an exception in the function on errors. | ||
* @returns {any} - the converted item | ||
* @returns {any} The converted item. | ||
*/ | ||
convert(converters: any): any; | ||
convert(converters: object): any; | ||
} | ||
@@ -76,3 +77,3 @@ declare namespace Tagged { | ||
/** | ||
* A mapping from tag number to a tag decoding function | ||
* A mapping from tag number to a tag decoding function. | ||
*/ | ||
@@ -79,0 +80,0 @@ type TagMap = { |
@@ -9,7 +9,12 @@ export = BinaryParseStream; | ||
* | ||
* @class BinaryParseStream | ||
* @extends {TransformStream} | ||
* @extends stream.Transform | ||
*/ | ||
declare class BinaryParseStream extends Stream.Transform { | ||
constructor(options: any); | ||
declare class BinaryParseStream extends stream.Transform { | ||
/** | ||
* Creates an instance of BinaryParseStream. | ||
* | ||
* @param {stream.TransformOptions} options Stream options. | ||
* @memberof BinaryParseStream | ||
*/ | ||
constructor(options: stream.TransformOptions); | ||
bs: NoFilter; | ||
@@ -19,11 +24,13 @@ __fresh: boolean; | ||
/** | ||
* Subclasses must override this to set their parsing behavior. Yield a | ||
* number to receive a Buffer of that many bytes. | ||
* | ||
* @abstract | ||
* @protected | ||
* @returns {Generator<number, undefined, Buffer>} | ||
*/ | ||
protected _parse(): Generator<number, undefined, Buffer>; | ||
_parse(): Generator<number, undefined, Buffer>; | ||
__restart(): void; | ||
__parser: Generator<number, undefined, Buffer>; | ||
} | ||
import Stream = require("stream"); | ||
import stream = require("stream"); | ||
import NoFilter = require("nofilter"); |
@@ -11,5 +11,4 @@ // Tweaked version of nathan7's binary-parse-stream | ||
'use strict' | ||
const Stream = require('stream') | ||
const stream = require('stream') | ||
const NoFilter = require('nofilter') | ||
const TransformStream = Stream.Transform | ||
@@ -23,6 +22,11 @@ /** | ||
* | ||
* @class BinaryParseStream | ||
* @extends {TransformStream} | ||
* @extends stream.Transform | ||
*/ | ||
class BinaryParseStream extends TransformStream { | ||
class BinaryParseStream extends stream.Transform { | ||
/** | ||
* Creates an instance of BinaryParseStream. | ||
* | ||
* @param {stream.TransformOptions} options Stream options. | ||
* @memberof BinaryParseStream | ||
*/ | ||
constructor(options) { | ||
@@ -72,4 +76,6 @@ super(options) | ||
/** | ||
* Subclasses must override this to set their parsing behavior. Yield a | ||
* number to receive a Buffer of that many bytes. | ||
* | ||
* @abstract | ||
* @protected | ||
* @returns {Generator<number, undefined, Buffer>} | ||
@@ -76,0 +82,0 @@ */ |
@@ -43,3 +43,3 @@ # binary-parse-stream | ||
this.count++ | ||
return { type, buf } | ||
return {type, buf} | ||
} | ||
@@ -46,0 +46,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
153074
4511
Updatednofilter@^3.0.3