Comparing version 1.0.1 to 1.0.2
@@ -48,2 +48,17 @@ 'use strict'; | ||
}); | ||
it('bad cmd', async () => { | ||
try { | ||
await execBin('blip'); | ||
assert.fail('should have errored'); | ||
} catch (e) { | ||
assert.strictEqual(e.stdout, ''); | ||
assert.strictEqual(e.stderr, `Unknown command: 'blip' | ||
Usage: cborg <command> <args> | ||
Valid commands: | ||
\thex2diag <hex input> | ||
\thex2json [--pretty] <hex input> | ||
\tjson2hex '<json input>' | ||
`); | ||
} | ||
}); | ||
it('help', async () => { | ||
@@ -50,0 +65,0 @@ const {stdout, stderr} = await execBin('help'); |
@@ -88,2 +88,8 @@ 'use strict'; | ||
{ | ||
data: 'fb7ff0000000000000', | ||
expected: Infinity, | ||
type: 'Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f9fc00', | ||
@@ -94,2 +100,8 @@ expected: -Infinity, | ||
{ | ||
data: 'fbfff0000000000000', | ||
expected: -Infinity, | ||
type: '-Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f97e00', | ||
@@ -100,2 +112,26 @@ expected: NaN, | ||
{ | ||
data: 'f97ff8', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fa7ff80000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8000000000000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8cafedeadbeef', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb40f4241a31a5a515', | ||
@@ -124,5 +160,7 @@ expected: 82497.63712086187, | ||
for (const fixture of fixtures) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
if (fixture.strict !== false) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
} | ||
} | ||
@@ -187,2 +225,5 @@ }); | ||
assert.throws(() => decode.decode(byteUtils.fromHex('830102f9fc00'), { allowInfinity: false }), /Infinity/); | ||
for (const fixture of fixtures.filter(f => f.type.endsWith('Infinity'))) { | ||
assert.throws(() => decode.decode(byteUtils.fromHex(fixture.data), { allowInfinity: false }), /Infinity/); | ||
} | ||
}); | ||
@@ -196,4 +237,7 @@ it('can switch off NaN support', () => { | ||
assert.throws(() => decode.decode(byteUtils.fromHex('830102f97e00'), { allowNaN: false }), /NaN/); | ||
for (const fixture of fixtures.filter(f => f.type === 'NaN')) { | ||
assert.throws(() => decode.decode(byteUtils.fromHex(fixture.data), { allowNaN: false }), /NaN/); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -24,10 +24,21 @@ 'use strict'; | ||
} | ||
function createToken(value, bytes, options) { | ||
if (options) { | ||
if (options.allowNaN === false && Number.isNaN(value)) { | ||
throw new Error(`${ common.decodeErrPrefix } NaN values are not supported`); | ||
} | ||
if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) { | ||
throw new Error(`${ common.decodeErrPrefix } Infinity values are not supported`); | ||
} | ||
} | ||
return new token.Token(token.Type.float, value, bytes); | ||
} | ||
function decodeFloat16(data, pos, minor, options) { | ||
return new token.Token(token.Type.float, readFloat16(data, pos + 1, options), 3); | ||
return createToken(readFloat16(data, pos + 1), 3, options); | ||
} | ||
function decodeFloat32(data, pos, minor, options) { | ||
return new token.Token(token.Type.float, readFloat32(data, pos + 1), 5); | ||
return createToken(readFloat32(data, pos + 1), 5, options); | ||
} | ||
function decodeFloat64(data, pos, minor, options) { | ||
return new token.Token(token.Type.float, readFloat64(data, pos + 1), 9); | ||
return createToken(readFloat64(data, pos + 1), 9, options); | ||
} | ||
@@ -126,3 +137,3 @@ function encodeFloat(buf, token$1, options) { | ||
} | ||
function readFloat16(ui8a, pos, options) { | ||
function readFloat16(ui8a, pos) { | ||
if (ui8a.length - pos < 2) { | ||
@@ -133,17 +144,8 @@ throw new Error(`${ common.decodeErrPrefix } not enough data for float16`); | ||
if (half === 31744) { | ||
if (options && options.allowInfinity === false) { | ||
throw new Error(`${ common.decodeErrPrefix } Infinity values are not supported`); | ||
} | ||
return Infinity; | ||
} | ||
if (half === 64512) { | ||
if (options && options.allowInfinity === false) { | ||
throw new Error(`${ common.decodeErrPrefix } Infinity values are not supported`); | ||
} | ||
return -Infinity; | ||
} | ||
if (half === 32256) { | ||
if (options && options.allowNaN === false) { | ||
throw new Error(`${ common.decodeErrPrefix } NaN values are not supported`); | ||
} | ||
return NaN; | ||
@@ -150,0 +152,0 @@ } |
@@ -23,5 +23,2 @@ 'use strict'; | ||
} | ||
if (!cmd) { | ||
usage(1); | ||
} | ||
if (cmd === 'help') { | ||
@@ -54,2 +51,9 @@ usage(0); | ||
console.log(byteUtils.toHex(encode.encode(obj))); | ||
} else { | ||
if (process__default['default'].argv.findIndex(a => a.endsWith('mocha')) === -1) { | ||
if (cmd) { | ||
console.error(`Unknown command: '${ cmd }'`); | ||
} | ||
usage(1); | ||
} | ||
} | ||
@@ -56,0 +60,0 @@ var bin = true; |
@@ -48,2 +48,17 @@ 'use strict'; | ||
}); | ||
it('bad cmd', async () => { | ||
try { | ||
await execBin('blip'); | ||
assert.fail('should have errored'); | ||
} catch (e) { | ||
assert.strictEqual(e.stdout, ''); | ||
assert.strictEqual(e.stderr, `Unknown command: 'blip' | ||
Usage: cborg <command> <args> | ||
Valid commands: | ||
\thex2diag <hex input> | ||
\thex2json [--pretty] <hex input> | ||
\tjson2hex '<json input>' | ||
`); | ||
} | ||
}); | ||
it('help', async () => { | ||
@@ -50,0 +65,0 @@ const {stdout, stderr} = await execBin('help'); |
@@ -88,2 +88,8 @@ 'use strict'; | ||
{ | ||
data: 'fb7ff0000000000000', | ||
expected: Infinity, | ||
type: 'Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f9fc00', | ||
@@ -94,2 +100,8 @@ expected: -Infinity, | ||
{ | ||
data: 'fbfff0000000000000', | ||
expected: -Infinity, | ||
type: '-Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f97e00', | ||
@@ -100,2 +112,26 @@ expected: NaN, | ||
{ | ||
data: 'f97ff8', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fa7ff80000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8000000000000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8cafedeadbeef', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb40f4241a31a5a515', | ||
@@ -124,5 +160,7 @@ expected: 82497.63712086187, | ||
for (const fixture of fixtures) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
if (fixture.strict !== false) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
} | ||
} | ||
@@ -187,2 +225,5 @@ }); | ||
assert.throws(() => decode.decode(byteUtils.fromHex('830102f9fc00'), { allowInfinity: false }), /Infinity/); | ||
for (const fixture of fixtures.filter(f => f.type.endsWith('Infinity'))) { | ||
assert.throws(() => decode.decode(byteUtils.fromHex(fixture.data), { allowInfinity: false }), /Infinity/); | ||
} | ||
}); | ||
@@ -196,4 +237,7 @@ it('can switch off NaN support', () => { | ||
assert.throws(() => decode.decode(byteUtils.fromHex('830102f97e00'), { allowNaN: false }), /NaN/); | ||
for (const fixture of fixtures.filter(f => f.type === 'NaN')) { | ||
assert.throws(() => decode.decode(byteUtils.fromHex(fixture.data), { allowNaN: false }), /NaN/); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -39,2 +39,17 @@ import chai from 'chai'; | ||
}); | ||
it('bad cmd', async () => { | ||
try { | ||
await execBin('blip'); | ||
assert.fail('should have errored'); | ||
} catch (e) { | ||
assert.strictEqual(e.stdout, ''); | ||
assert.strictEqual(e.stderr, `Unknown command: 'blip' | ||
Usage: cborg <command> <args> | ||
Valid commands: | ||
\thex2diag <hex input> | ||
\thex2json [--pretty] <hex input> | ||
\tjson2hex '<json input>' | ||
`); | ||
} | ||
}); | ||
it('help', async () => { | ||
@@ -41,0 +56,0 @@ const {stdout, stderr} = await execBin('help'); |
@@ -85,2 +85,8 @@ import chai from 'chai'; | ||
{ | ||
data: 'fb7ff0000000000000', | ||
expected: Infinity, | ||
type: 'Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f9fc00', | ||
@@ -91,2 +97,8 @@ expected: -Infinity, | ||
{ | ||
data: 'fbfff0000000000000', | ||
expected: -Infinity, | ||
type: '-Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f97e00', | ||
@@ -97,2 +109,26 @@ expected: NaN, | ||
{ | ||
data: 'f97ff8', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fa7ff80000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8000000000000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8cafedeadbeef', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb40f4241a31a5a515', | ||
@@ -121,5 +157,7 @@ expected: 82497.63712086187, | ||
for (const fixture of fixtures) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
if (fixture.strict !== false) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
} | ||
} | ||
@@ -184,2 +222,5 @@ }); | ||
assert.throws(() => decode(fromHex('830102f9fc00'), { allowInfinity: false }), /Infinity/); | ||
for (const fixture of fixtures.filter(f => f.type.endsWith('Infinity'))) { | ||
assert.throws(() => decode(fromHex(fixture.data), { allowInfinity: false }), /Infinity/); | ||
} | ||
}); | ||
@@ -193,4 +234,7 @@ it('can switch off NaN support', () => { | ||
assert.throws(() => decode(fromHex('830102f97e00'), { allowNaN: false }), /NaN/); | ||
for (const fixture of fixtures.filter(f => f.type === 'NaN')) { | ||
assert.throws(() => decode(fromHex(fixture.data), { allowNaN: false }), /NaN/); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -22,10 +22,21 @@ import { | ||
} | ||
function createToken(value, bytes, options) { | ||
if (options) { | ||
if (options.allowNaN === false && Number.isNaN(value)) { | ||
throw new Error(`${ decodeErrPrefix } NaN values are not supported`); | ||
} | ||
if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) { | ||
throw new Error(`${ decodeErrPrefix } Infinity values are not supported`); | ||
} | ||
} | ||
return new Token(Type.float, value, bytes); | ||
} | ||
export function decodeFloat16(data, pos, minor, options) { | ||
return new Token(Type.float, readFloat16(data, pos + 1, options), 3); | ||
return createToken(readFloat16(data, pos + 1), 3, options); | ||
} | ||
export function decodeFloat32(data, pos, minor, options) { | ||
return new Token(Type.float, readFloat32(data, pos + 1), 5); | ||
return createToken(readFloat32(data, pos + 1), 5, options); | ||
} | ||
export function decodeFloat64(data, pos, minor, options) { | ||
return new Token(Type.float, readFloat64(data, pos + 1), 9); | ||
return createToken(readFloat64(data, pos + 1), 9, options); | ||
} | ||
@@ -124,3 +135,3 @@ export function encodeFloat(buf, token, options) { | ||
} | ||
function readFloat16(ui8a, pos, options) { | ||
function readFloat16(ui8a, pos) { | ||
if (ui8a.length - pos < 2) { | ||
@@ -131,17 +142,8 @@ throw new Error(`${ decodeErrPrefix } not enough data for float16`); | ||
if (half === 31744) { | ||
if (options && options.allowInfinity === false) { | ||
throw new Error(`${ decodeErrPrefix } Infinity values are not supported`); | ||
} | ||
return Infinity; | ||
} | ||
if (half === 64512) { | ||
if (options && options.allowInfinity === false) { | ||
throw new Error(`${ decodeErrPrefix } Infinity values are not supported`); | ||
} | ||
return -Infinity; | ||
} | ||
if (half === 32256) { | ||
if (options && options.allowNaN === false) { | ||
throw new Error(`${ decodeErrPrefix } NaN values are not supported`); | ||
} | ||
return NaN; | ||
@@ -148,0 +150,0 @@ } |
@@ -20,5 +20,2 @@ import process from 'process'; | ||
} | ||
if (!cmd) { | ||
usage(1); | ||
} | ||
if (cmd === 'help') { | ||
@@ -51,3 +48,10 @@ usage(0); | ||
console.log(toHex(encode(obj))); | ||
} else { | ||
if (process.argv.findIndex(a => a.endsWith('mocha')) === -1) { | ||
if (cmd) { | ||
console.error(`Unknown command: '${ cmd }'`); | ||
} | ||
usage(1); | ||
} | ||
} | ||
export default true; |
@@ -39,2 +39,17 @@ import chai from 'chai'; | ||
}); | ||
it('bad cmd', async () => { | ||
try { | ||
await execBin('blip'); | ||
assert.fail('should have errored'); | ||
} catch (e) { | ||
assert.strictEqual(e.stdout, ''); | ||
assert.strictEqual(e.stderr, `Unknown command: 'blip' | ||
Usage: cborg <command> <args> | ||
Valid commands: | ||
\thex2diag <hex input> | ||
\thex2json [--pretty] <hex input> | ||
\tjson2hex '<json input>' | ||
`); | ||
} | ||
}); | ||
it('help', async () => { | ||
@@ -41,0 +56,0 @@ const {stdout, stderr} = await execBin('help'); |
@@ -85,2 +85,8 @@ import chai from 'chai'; | ||
{ | ||
data: 'fb7ff0000000000000', | ||
expected: Infinity, | ||
type: 'Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f9fc00', | ||
@@ -91,2 +97,8 @@ expected: -Infinity, | ||
{ | ||
data: 'fbfff0000000000000', | ||
expected: -Infinity, | ||
type: '-Infinity', | ||
strict: false | ||
}, | ||
{ | ||
data: 'f97e00', | ||
@@ -97,2 +109,26 @@ expected: NaN, | ||
{ | ||
data: 'f97ff8', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fa7ff80000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8000000000000', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb7ff8cafedeadbeef', | ||
expected: NaN, | ||
type: 'NaN', | ||
strict: false | ||
}, | ||
{ | ||
data: 'fb40f4241a31a5a515', | ||
@@ -121,5 +157,7 @@ expected: 82497.63712086187, | ||
for (const fixture of fixtures) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
if (fixture.strict !== false) { | ||
it(`should encode ${ fixture.type }=${ fixture.expected }`, () => { | ||
assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`); | ||
}); | ||
} | ||
} | ||
@@ -184,2 +222,5 @@ }); | ||
assert.throws(() => decode(fromHex('830102f9fc00'), { allowInfinity: false }), /Infinity/); | ||
for (const fixture of fixtures.filter(f => f.type.endsWith('Infinity'))) { | ||
assert.throws(() => decode(fromHex(fixture.data), { allowInfinity: false }), /Infinity/); | ||
} | ||
}); | ||
@@ -193,4 +234,7 @@ it('can switch off NaN support', () => { | ||
assert.throws(() => decode(fromHex('830102f97e00'), { allowNaN: false }), /NaN/); | ||
for (const fixture of fixtures.filter(f => f.type === 'NaN')) { | ||
assert.throws(() => decode(fromHex(fixture.data), { allowNaN: false }), /NaN/); | ||
} | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "cborg", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Fast CBOR with a focus on strictness", | ||
@@ -19,3 +19,3 @@ "bin": { | ||
"type": "git", | ||
"url": "https+git://github.com/rvagg/cborg" | ||
"url": "https://github.com/rvagg/cborg.git" | ||
}, | ||
@@ -22,0 +22,0 @@ "keywords": [ |
@@ -246,2 +246,3 @@ # cborg - fast CBOR with a focus on strictness | ||
* Varying int sizes used as lengths for lengthed objects (maps, arrays, strings, bytes) - e.g. a single entry array could specify its length using any of the above forms for `1`. Tags can also vary in size and still represent the same number. | ||
* IEEE 754 allows for `NaN`, `Infinity` and `-Infinity` to be represented in many different ways, meaning it is possible to represent the same data using many different byte forms. | ||
* Indefinite length items where the length is omitted from the additional item of the entity token and a "break" is inserted to indicate the end of of the object. This provides two ways to encode the same object. | ||
@@ -262,2 +263,3 @@ * Tags that can allow alternative representations of objects - e.g. using the bigint or negative bigint tags to represent standard size integers. | ||
* `strict: true` to impose strict sizing rules for int, negative ints and lengths of lengthed objects | ||
* `allowNaN: false` and `allowInfinity` to prevent decoding of any value that would resolve to `NaN`, `Infinity` or `-Infinity`, using CBOR tokens or IEEE 754 representationโas long as your application can do without these symbols. | ||
* `allowIndefinite: false` to disallow indefinite lengthed objects and the "break" tag | ||
@@ -264,0 +266,0 @@ * Not providing any tag decoders, or ensuring that tag decoders are strict about their forms (e.g. a bigint decoder could reject bigints that could have fit into a standard major 0 64-bit integer). |
416287
14536
286