Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cbor

Package Overview
Dependencies
Maintainers
3
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cbor - npm Package Compare versions

Comparing version 3.0.3 to 4.0.0

docs/CborMap.html

1

lib/cbor.js

@@ -9,2 +9,3 @@ 'use strict'

exports.Tagged = require('./tagged')
exports.Map = require('./map')

@@ -11,0 +12,0 @@ exports.comment = exports.Commented.comment

2

lib/commented.js

@@ -85,3 +85,3 @@ 'use strict'

* @param {(string|Buffer|NoFilter)} input
* @param {(string|object|function)} options
* @param {(string|Object|function)} options
* @param {number} [options.max_depth=10] how many times to indent the dashes

@@ -88,0 +88,0 @@ * @param {commentCallback=} cb

@@ -17,3 +17,3 @@ 'use strict'

const NEG_ONE = new bignumber(-1)
const NEG_MAX = NEG_ONE.sub(
const NEG_MAX = NEG_ONE.minus(
new bignumber(Number.MAX_SAFE_INTEGER.toString(16), 16))

@@ -394,3 +394,3 @@ const COUNT = Symbol('count')

} else if (val instanceof bignumber) {
val = NEG_ONE.sub(val)
val = NEG_ONE.minus(val)
} else {

@@ -397,0 +397,0 @@ val = -1 - val

@@ -12,3 +12,4 @@ 'use strict'

const MT = constants.MT, SYMS = constants.SYMS
const MT = constants.MT
const SYMS = constants.SYMS

@@ -30,11 +31,12 @@ /**

* @param {number} [options.max_depth=-1] - -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.
* 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.
*/
constructor (options) {
constructor(options) {
options = options || {}
const separator = (options.separator != null) ? options.separator : '\n'
delete options.separator
const stream_errors = (options.stream_errors != null) ? options.stream_errors : false
const stream_errors = (options.stream_errors != null) ?
options.stream_errors : false
delete options.stream_errors

@@ -57,7 +59,7 @@ options.readableObjectMode = false

_transform (fresh, encoding, cb) {
_transform(fresh, encoding, cb) {
return this.parser.write(fresh, encoding, cb)
}
_flush (cb) {
_flush(cb) {
return this.parser._flush((er) => {

@@ -77,7 +79,8 @@ if (this.stream_errors) {

* @param {(Buffer|string)} input - the CBOR bytes to format
* @param {string} [encoding='hex'] - the encoding of input, ignored if input is Buffer
* @param {string} [encoding='hex'] - the encoding of input, ignored if
* input is Buffer
* @param {commentCallback} cb - callback
* @returns {Promise} if callback not specified
*/
static diagnose (input, encoding, cb) {
static diagnose(input, encoding, cb) {
if (input == null) {

@@ -95,3 +98,4 @@ throw new Error('input required')

opts = utils.extend({}, encoding)
encod = (opts.encoding != null) ? opts.encoding : utils.guessEncoding(input)
encod = (opts.encoding != null) ?
opts.encoding : utils.guessEncoding(input)
delete opts.encoding

@@ -106,11 +110,7 @@ break

if (typeof cb === 'function') {
d.on('end', function () {
return cb(null, bs.toString('utf8'))
})
d.on('end', () => cb(null, bs.toString('utf8')))
d.on('error', cb)
} else {
p = new Promise(function (resolve, reject) {
d.on('end', function () {
return resolve(bs.toString('utf8'))
})
p = new Promise((resolve, reject) => {
d.on('end', () => resolve(bs.toString('utf8')))
return d.on('error', reject)

@@ -124,3 +124,3 @@ })

_on_error (er) {
_on_error(er) {
if (this.stream_errors) {

@@ -133,5 +133,5 @@ return this.push(er.toString())

_on_more (mt, len, parent_mt, pos) {
_on_more(mt, len, parent_mt, pos) {
if (mt === MT.SIMPLE_FLOAT) {
return this.float_bytes = (function () {
return this.float_bytes = (function() {
switch (len) {

@@ -149,3 +149,3 @@ case 2:

_fore (parent_mt, pos) {
_fore(parent_mt, pos) {
switch (parent_mt) {

@@ -170,3 +170,3 @@ case MT.BYTE_STRING:

_on_value (val, parent_mt, pos) {
_on_value(val, parent_mt, pos) {
if (val === SYMS.BREAK) {

@@ -176,3 +176,3 @@ return

this._fore(parent_mt, pos)
return this.push((function () {
return this.push((function() {
switch (false) {

@@ -190,3 +190,3 @@ case val !== SYMS.NULL:

case !Buffer.isBuffer(val):
return "h'" + (val.toString('hex')) + "'"
return 'h\'' + (val.toString('hex')) + '\''
case !(val instanceof bignumber):

@@ -200,5 +200,5 @@ return val.toString()

_on_start (mt, tag, parent_mt, pos) {
_on_start(mt, tag, parent_mt, pos) {
this._fore(parent_mt, pos)
this.push((function () {
this.push((function() {
switch (mt) {

@@ -224,4 +224,4 @@ case MT.TAG:

_on_stop (mt) {
return this.push((function () {
_on_stop(mt) {
return this.push((function() {
switch (mt) {

@@ -244,3 +244,3 @@ case MT.TAG:

_on_data () {
_on_data() {
return this.push(this.separator)

@@ -247,0 +247,0 @@ }

@@ -12,3 +12,7 @@ 'use strict'

const constants = require('./constants')
const MT = constants.MT, NUMBYTES = constants.NUMBYTES, SHIFT32 = constants.SHIFT32, SYMS = constants.SYMS, TAG = constants.TAG
const MT = constants.MT
const NUMBYTES = constants.NUMBYTES
const SHIFT32 = constants.SHIFT32
const SYMS = constants.SYMS
const TAG = constants.TAG
const HALF = (constants.MT.SIMPLE_FLOAT << 5) | constants.NUMBYTES.TWO

@@ -26,2 +30,3 @@ const FLOAT = (constants.MT.SIMPLE_FLOAT << 5) | constants.NUMBYTES.FOUR

const BUF_INF_POS = new Buffer('f97c00', 'hex')
const LOOP_DETECT = Symbol('CBOR_LOOP_DETECT')

@@ -45,4 +50,10 @@ /**

* canonicalized
* @param {boolean|Symbol} [options.detectLoops=false] - should object loops
* be detected? This will currently modify the encoded object graph
* by adding a Symbol property to each object. If this bothers you,
* call `removeLoopDetectors` on the encoded object when done. Do not
* encode the same object twice on the same encoder, without calling
* `removeLoopDetectors` in between.
*/
constructor (options) {
constructor(options) {
options = options || {}

@@ -54,2 +65,10 @@ options.readableObjectMode = false

this.canonical = options.canonical
// new Symbol for each instance. Note: means we can't re-use the same
// encoder and encoded object
if (typeof(options.detectLoops) === 'symbol') {
this.detectLoops = options.detectLoops
} else {
this.detectLoops = !!options.detectLoops ? Symbol('CBOR_DETECT') : null
}
this.semanticTypes = [

@@ -73,3 +92,3 @@ Array, this._pushArray,

_transform (fresh, encoding, cb) {
_transform(fresh, encoding, cb) {
const ret = this.pushAny(fresh)

@@ -80,3 +99,3 @@ // Old transformers might not return bool. undefined !== false

_flush (cb) {
_flush(cb) {
return cb()

@@ -100,3 +119,3 @@ }

*/
addSemanticType (type, fun) {
addSemanticType(type, fun) {
for (let i = 0, len = this.semanticTypes.length; i < len; i += 2) {

@@ -114,3 +133,3 @@ const typ = this.semanticTypes[i]

_pushUInt8 (val) {
_pushUInt8(val) {
const b = new Buffer(1)

@@ -121,3 +140,3 @@ b.writeUInt8(val)

_pushUInt16BE (val) {
_pushUInt16BE(val) {
const b = new Buffer(2)

@@ -128,3 +147,3 @@ b.writeUInt16BE(val)

_pushUInt32BE (val) {
_pushUInt32BE(val) {
const b = new Buffer(4)

@@ -135,3 +154,3 @@ b.writeUInt32BE(val)

_pushDoubleBE (val) {
_pushDoubleBE(val) {
const b = new Buffer(8)

@@ -142,7 +161,7 @@ b.writeDoubleBE(val)

_pushNaN () {
_pushNaN() {
return this.push(BUF_NAN)
}
_pushInfinity (obj) {
_pushInfinity(obj) {
const half = (obj < 0) ? BUF_INF_NEG : BUF_INF_POS

@@ -152,3 +171,3 @@ return this.push(half)

_pushFloat (obj) {
_pushFloat(obj) {
if (this.canonical) {

@@ -183,3 +202,3 @@ // TODO: is this enough slower to hide behind canonical?

_pushInt (obj, mt, orig) {
_pushInt(obj, mt, orig) {
const m = mt << 5

@@ -208,3 +227,3 @@ switch (false) {

_pushIntNum (obj) {
_pushIntNum(obj) {
if (obj < 0) {

@@ -217,3 +236,3 @@ return this._pushInt(-obj - 1, MT.NEG_INT, obj)

_pushNumber (obj) {
_pushNumber(obj) {
switch (false) {

@@ -231,3 +250,3 @@ case !isNaN(obj):

_pushString (obj) {
_pushString(obj) {
const len = Buffer.byteLength(obj, 'utf8')

@@ -237,15 +256,15 @@ return this._pushInt(len, MT.UTF8_STRING) && this.push(obj, 'utf8')

_pushBoolean (obj) {
_pushBoolean(obj) {
return this._pushUInt8(obj ? TRUE : FALSE)
}
_pushUndefined (obj) {
_pushUndefined(obj) {
return this._pushUInt8(UNDEFINED)
}
_pushNull (obj) {
_pushNull(obj) {
return this._pushUInt8(NULL)
}
_pushArray (gen, obj) {
_pushArray(gen, obj) {
const len = obj.length

@@ -263,27 +282,27 @@ if (!gen._pushInt(len, MT.ARRAY)) {

_pushTag (tag) {
_pushTag(tag) {
return this._pushInt(tag, MT.TAG)
}
_pushDate (gen, obj) {
_pushDate(gen, obj) {
return gen._pushTag(TAG.DATE_EPOCH) && gen.pushAny(obj / 1000)
}
_pushBuffer (gen, obj) {
_pushBuffer(gen, obj) {
return gen._pushInt(obj.length, MT.BYTE_STRING) && gen.push(obj)
}
_pushNoFilter (gen, obj) {
_pushNoFilter(gen, obj) {
return gen._pushBuffer(gen, obj.slice())
}
_pushRegexp (gen, obj) {
_pushRegexp(gen, obj) {
return gen._pushTag(TAG.REGEXP) && gen.pushAny(obj.source)
}
_pushSet (gen, obj) {
_pushSet(gen, obj) {
if (!gen._pushInt(obj.size, MT.ARRAY)) {
return false
}
for (let x of obj) {
for (const x of obj) {
if (!gen.pushAny(x)) {

@@ -296,7 +315,7 @@ return false

_pushUrl (gen, obj) {
_pushUrl(gen, obj) {
return gen._pushTag(TAG.URI) && gen.pushAny(obj.format())
}
_pushBigint (obj) {
_pushBigint(obj) {
let tag = TAG.POS_BIGINT

@@ -315,3 +334,3 @@ if (obj.isNegative()) {

_pushBigNumber (gen, obj) {
_pushBigNumber(gen, obj) {
if (obj.isNaN()) {

@@ -332,7 +351,7 @@ return gen._pushNaN()

const dec = obj.decimalPlaces()
const slide = obj.mul(new bignumber(10).pow(dec))
const slide = obj.times(new bignumber(10).pow(dec))
if (!gen._pushIntNum(-dec)) {
return false
}
if (slide.abs().lessThan(MAXINT_BN)) {
if (slide.abs().isLessThan(MAXINT_BN)) {
return gen._pushIntNum(slide.toNumber())

@@ -344,3 +363,3 @@ } else {

_pushMap (gen, obj) {
_pushMap(gen, obj) {
if (!gen._pushInt(obj.size, MT.MAP)) {

@@ -359,3 +378,3 @@ return false

}
entries.sort((a,b) => {
entries.sort((a, b) => {
// a, b are both entries of [key, value]

@@ -381,6 +400,39 @@ const a_cbor = Encoder.encode(a[0])

_pushObject (obj) {
removeLoopDetectors(obj) {
if (!this.detectLoops || (typeof(obj) !== 'object') || !obj) {
return false
}
const dl = obj[LOOP_DETECT]
if (!dl || (dl !== this.detectLoops)) {
// ironically, use loop marking to detect loops on removal as well
return false
}
delete obj[LOOP_DETECT]
if (Array.isArray(obj)) {
for (const i of obj) {
this.removeLoopDetectors(i)
}
} else {
for (const k in obj) {
this.removeLoopDetectors(obj[k])
}
}
return true
}
_pushObject(obj) {
if (!obj) {
return this._pushNull(obj)
}
if (this.detectLoops) {
if (obj[LOOP_DETECT] === this.detectLoops) {
throw new Error('Loop detected while CBOR encoding')
} else {
obj[LOOP_DETECT] = this.detectLoops
}
}
const f = obj.encodeCBOR
if (typeof f === 'function') {
return f.call(obj, this)
}
for (let i = 0, len1 = this.semanticTypes.length; i < len1; i += 2) {

@@ -392,6 +444,2 @@ const typ = this.semanticTypes[i]

}
const f = obj.encodeCBOR
if (typeof f === 'function') {
return f.call(obj, this)
}
const keys = Object.keys(obj)

@@ -441,3 +489,3 @@ const cbor_keys = {}

*/
pushAny (obj) {
pushAny(obj) {
switch (typeof obj) {

@@ -465,3 +513,5 @@ case 'number':

default:
throw new Error('Unknown type: ' + typeof obj + ', ' + (!!obj ? obj.toString() : ''))
throw new Error(
'Unknown type: ' + typeof obj + ', ' +
(!!obj ? obj.toString() : ''))
}

@@ -471,3 +521,3 @@ }

/* backwards-compat wrapper */
_pushAny (obj) {
_pushAny(obj) {
// TODO: write deprecation warning

@@ -477,6 +527,6 @@ return this.pushAny(obj)

_encodeAll (objs) {
_encodeAll(objs) {
const bs = new NoFilter()
this.pipe(bs)
for (let o of objs) {
for (const o of objs) {
if (typeof o === 'undefined') {

@@ -501,3 +551,3 @@ this._pushUndefined()

*/
static encode () {
static encode() {
const objs = Array.prototype.slice.apply(arguments)

@@ -514,3 +564,3 @@ return new Encoder()._encodeAll(objs)

*/
static encodeCanonical () {
static encodeCanonical() {
const objs = Array.prototype.slice.apply(arguments)

@@ -517,0 +567,0 @@ return new Encoder({canonical: true})._encodeAll(objs)

'use strict'
const constants = require('./constants')
const MT = constants.MT, SIMPLE = constants.SIMPLE, SYMS = constants.SYMS
const MT = constants.MT
const SIMPLE = constants.SIMPLE
const SYMS = constants.SYMS

@@ -15,3 +17,3 @@ /**

*/
constructor (value) {
constructor(value) {
if (typeof value !== 'number') {

@@ -31,3 +33,3 @@ throw new Error('Invalid Simple type: ' + (typeof value))

*/
toString () {
toString() {
return 'simple(' + this.value + ')'

@@ -41,3 +43,3 @@ }

*/
inspect (depth, opts) {
inspect(depth, opts) {
return 'simple(' + this.value + ')'

@@ -51,3 +53,3 @@ }

*/
encodeCBOR (gen) {
encodeCBOR(gen) {
return gen._pushInt(this.value, MT.SIMPLE_FLOAT)

@@ -62,3 +64,3 @@ }

*/
static isSimple (obj) {
static isSimple(obj) {
return obj instanceof Simple

@@ -73,7 +75,7 @@ }

*
* @param {Number} val - the CBOR additional info to convert
* @param {number} val - the CBOR additional info to convert
* @param {bool} has_parent - Does the CBOR item have a parent?
* @returns {(null|undefined|Boolean|Symbol)} - the decoded value
* @returns {(null|undefined|bool|Symbol)} - the decoded value
*/
static decode (val, has_parent) {
static decode(val, has_parent) {
if (has_parent == null) {

@@ -80,0 +82,0 @@ has_parent = true

@@ -11,3 +11,2 @@ 'use strict'

/**

@@ -23,7 +22,7 @@ * A CBOR tagged item, where the tag does not have semantics specified at the

*
* @param {Number} tag - the number of the tag
* @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
*/
constructor (tag, value, err) {
constructor(tag, value, err) {
this.tag = tag

@@ -43,5 +42,5 @@ this.value = value

*
* @returns {String} string of the form '1(2)'
* @returns {string} string of the form '1(2)'
*/
toString () {
toString() {
return `${this.tag}(${JSON.stringify(this.value)})`

@@ -55,3 +54,3 @@ }

*/
encodeCBOR (gen) {
encodeCBOR(gen) {
gen._pushTag(this.tag)

@@ -72,5 +71,4 @@ return gen.pushAny(this.value)

*/
convert (converters) {
var er, f
f = converters != null ? converters[this.tag] : void 0
convert(converters) {
let f = converters != null ? converters[this.tag] : void 0
if (typeof f !== 'function') {

@@ -85,4 +83,3 @@ f = Tagged['_tag_' + this.tag]

} catch (error) {
er = error
this.err = er
this.err = error
return this

@@ -92,31 +89,31 @@ }

static _tag_0 (v) {
static _tag_0(v) {
return new Date(v)
}
static _tag_1 (v) {
static _tag_1(v) {
return new Date(v * 1000)
}
static _tag_2 (v) {
static _tag_2(v) {
return utils.bufferToBignumber(v)
}
static _tag_3 (v) {
static _tag_3(v) {
return MINUS_ONE.minus(utils.bufferToBignumber(v))
}
static _tag_4 (v) {
static _tag_4(v) {
return TEN.pow(v[0]).times(v[1])
}
static _tag_5 (v) {
static _tag_5(v) {
return TWO.pow(v[0]).times(v[1])
}
static _tag_32 (v) {
static _tag_32(v) {
return url.parse(v)
}
static _tag_35 (v) {
static _tag_35(v) {
return new RegExp(v)

@@ -123,0 +120,0 @@ }

{
"name": "cbor",
"version": "3.0.3",
"version": "4.0.0",
"description": "Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).",

@@ -26,3 +26,3 @@ "main": "./lib/cbor.js",

"clean": "rm -rf coverage .nyc_output/ docs/api man/*",
"lint": "eslint src/*.js bin/* test/*.js",
"lint": "eslint lib/*.js bin/* test/*.js",
"doc": "jsdoc -c .jsdoc.conf",

@@ -52,3 +52,4 @@ "coverage": "nyc npm test",

"Patrick Gansterer <paroga@paroga.com> (http://paroga.com/)",
"Artyom Yagilev <github@scorpi.org> (http://scorpi.org/)"
"Artyom Yagilev <github@scorpi.org> (http://scorpi.org/)",
"Denis Lapaev <den@lapaev.me> (http://lapaev.me/)"
],

@@ -61,12 +62,12 @@ "ava": {

"devDependencies": {
"ava": "^0.21",
"ava": "^0.25",
"concordance": "^3.0",
"eslint": "^4.3.0",
"garbage": "0.0.0",
"jsdoc": "*",
"eslint": "^4.17",
"garbage": "0.0",
"jsdoc": "^3.5",
"live-server": "*",
"marked-man": "*",
"minami": "*",
"npm-run-all": "*",
"nyc": "*",
"npm-run-all": "^4.1",
"nyc": "^11.5",
"watch": "*"

@@ -77,4 +78,4 @@ },

"dependencies": {
"bignumber.js": "^4.0",
"commander": "^2.11",
"bignumber.js": "^6.0",
"commander": "^2.14.1",
"json-text-sequence": "^0.1",

@@ -81,0 +82,0 @@ "nofilter": "^0.0.3"

@@ -123,2 +123,39 @@ 'use strict'

t.deepEqual(cbor.encodeCanonical(-1.25), new Buffer('f9bd00', 'hex'))
})
test('detect loops', t => {
const enc = new cbor.Encoder({detectLoops: true})
const bs = new NoFilter()
enc.pipe(bs)
const a = {c: false}
const b = [a]
enc.write(b)
t.is(bs.read().toString('hex'), '81a16163f4')
t.is(Object.getOwnPropertySymbols(a).length, 1)
t.is(Object.getOwnPropertySymbols(b).length, 1)
enc.removeLoopDetectors(b)
t.is(Object.getOwnPropertySymbols(a).length, 0)
t.is(Object.getOwnPropertySymbols(b).length, 0)
enc.removeLoopDetectors(b)
t.is(Object.getOwnPropertySymbols(a).length, 0)
t.is(Object.getOwnPropertySymbols(b).length, 0)
a.a = a
t.throws(() => enc.write(b))
})
test('detect loops, own symbol', t => {
const s = Symbol('MINE')
const enc = new cbor.Encoder({detectLoops: s})
const bs = new NoFilter()
enc.pipe(bs)
const a = {c: new Date()}
enc.write(a)
const a_syms = Object.getOwnPropertySymbols(a)
const date_syms = Object.getOwnPropertySymbols(a.c)
t.is(a_syms.length, 1)
t.is(date_syms.length, 1)
t.is(a[a_syms[0]], s)
t.is(a.c[date_syms[0]], s)
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc