Comparing version 1.8.1 to 1.9.0
@@ -7,2 +7,3 @@ 'use strict'; | ||
var byteUtils = require('../lib/byte-utils.js'); | ||
var common = require('./common.js'); | ||
var encode = require('../lib/encode.js'); | ||
@@ -16,17 +17,2 @@ var decode = require('../lib/decode.js'); | ||
const {assert} = chai__default["default"]; | ||
function dateDecoder(obj) { | ||
if (typeof obj !== 'string') { | ||
throw new Error('expected string for tag 1'); | ||
} | ||
return new Date(obj); | ||
} | ||
function dateEncoder(obj) { | ||
if (!(obj instanceof Date)) { | ||
throw new Error('expected Date for "Date" encoder'); | ||
} | ||
return [ | ||
new token.Token(token.Type.tag, 0), | ||
new token.Token(token.Type.string, obj.toISOString().replace(/\.000Z$/, 'Z')) | ||
]; | ||
} | ||
function Uint16ArrayDecoder(obj) { | ||
@@ -51,4 +37,4 @@ if (typeof obj !== 'string') { | ||
assert.throws(() => encode.encode({ d: new Date() }), /unsupported type: Date/); | ||
assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a'); | ||
const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: dateDecoder } }); | ||
assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: common.dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a'); | ||
const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: common.dateDecoder } }); | ||
assert.instanceOf(decodedDate, Date); | ||
@@ -75,3 +61,3 @@ assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString()); | ||
assert.throws(() => decode.decode(byteUtils.fromHex(hex), { | ||
tags: { 8: dateDecoder }, | ||
tags: { 8: common.dateDecoder }, | ||
strict: true | ||
@@ -81,3 +67,3 @@ }), /integer encoded in more bytes than necessary/); | ||
const decodedDate = decode.decode(byteUtils.fromHex(hex), { | ||
tags: { 8: dateDecoder }, | ||
tags: { 8: common.dateDecoder }, | ||
strict | ||
@@ -84,0 +70,0 @@ }); |
@@ -41,2 +41,5 @@ 'use strict'; | ||
encodeArray.compareTokens = _0uint.encodeUint.compareTokens; | ||
encodeArray.encodedSize = function encodedSize(token) { | ||
return _0uint.encodeUintValue.encodedSize(token.value); | ||
}; | ||
@@ -43,0 +46,0 @@ exports.decodeArray16 = decodeArray16; |
@@ -41,2 +41,5 @@ 'use strict'; | ||
encodeMap.compareTokens = _0uint.encodeUint.compareTokens; | ||
encodeMap.encodedSize = function encodedSize(token) { | ||
return _0uint.encodeUintValue.encodedSize(token.value); | ||
}; | ||
@@ -43,0 +46,0 @@ exports.decodeMap16 = decodeMap16; |
@@ -27,2 +27,5 @@ 'use strict'; | ||
encodeTag.compareTokens = _0uint.encodeUint.compareTokens; | ||
encodeTag.encodedSize = function encodedSize(token) { | ||
return _0uint.encodeUintValue.encodedSize(token.value); | ||
}; | ||
@@ -29,0 +32,0 @@ exports.decodeTag16 = decodeTag16; |
@@ -90,6 +90,5 @@ 'use strict'; | ||
} | ||
let decoded; | ||
if (!options || options.float64 !== true) { | ||
encodeFloat16(float); | ||
decoded = readFloat16(ui8a, 1); | ||
let decoded = readFloat16(ui8a, 1); | ||
if (float === decoded || Number.isNaN(float)) { | ||
@@ -96,0 +95,0 @@ return 3; |
@@ -17,5 +17,7 @@ 'use strict'; | ||
reset() { | ||
this.chunks = []; | ||
this.cursor = 0; | ||
this.maxCursor = -1; | ||
if (this.chunks.length) { | ||
this.chunks = []; | ||
} | ||
if (this._initReuseChunk !== null) { | ||
@@ -22,0 +24,0 @@ this.chunks.push(this._initReuseChunk); |
@@ -25,11 +25,15 @@ 'use strict'; | ||
}; | ||
const cborEncoders = []; | ||
cborEncoders[token.Type.uint.major] = _0uint.encodeUint; | ||
cborEncoders[token.Type.negint.major] = _1negint.encodeNegint; | ||
cborEncoders[token.Type.bytes.major] = _2bytes.encodeBytes; | ||
cborEncoders[token.Type.string.major] = _3string.encodeString; | ||
cborEncoders[token.Type.array.major] = _4array.encodeArray; | ||
cborEncoders[token.Type.map.major] = _5map.encodeMap; | ||
cborEncoders[token.Type.tag.major] = _6tag.encodeTag; | ||
cborEncoders[token.Type.float.major] = _7float.encodeFloat; | ||
function makeCborEncoders() { | ||
const encoders = []; | ||
encoders[token.Type.uint.major] = _0uint.encodeUint; | ||
encoders[token.Type.negint.major] = _1negint.encodeNegint; | ||
encoders[token.Type.bytes.major] = _2bytes.encodeBytes; | ||
encoders[token.Type.string.major] = _3string.encodeString; | ||
encoders[token.Type.array.major] = _4array.encodeArray; | ||
encoders[token.Type.map.major] = _5map.encodeMap; | ||
encoders[token.Type.tag.major] = _6tag.encodeTag; | ||
encoders[token.Type.float.major] = _7float.encodeFloat; | ||
return encoders; | ||
} | ||
const cborEncoders = makeCborEncoders(); | ||
const buf = new bl.Bl(); | ||
@@ -232,2 +236,3 @@ class Ref { | ||
} | ||
buf.reset(); | ||
tokensToEncoded(buf, tokens, encoders, options); | ||
@@ -244,2 +249,3 @@ return buf.toBytes(true); | ||
exports.encodeCustom = encodeCustom; | ||
exports.makeCborEncoders = makeCborEncoders; | ||
exports.objectToTokens = objectToTokens; |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var byteUtils = require('../lib/byte-utils.js'); | ||
var common = require('./common.js'); | ||
var encode = require('../lib/encode.js'); | ||
@@ -16,17 +17,2 @@ var decode = require('../lib/decode.js'); | ||
const {assert} = chai__default["default"]; | ||
function dateDecoder(obj) { | ||
if (typeof obj !== 'string') { | ||
throw new Error('expected string for tag 1'); | ||
} | ||
return new Date(obj); | ||
} | ||
function dateEncoder(obj) { | ||
if (!(obj instanceof Date)) { | ||
throw new Error('expected Date for "Date" encoder'); | ||
} | ||
return [ | ||
new token.Token(token.Type.tag, 0), | ||
new token.Token(token.Type.string, obj.toISOString().replace(/\.000Z$/, 'Z')) | ||
]; | ||
} | ||
function Uint16ArrayDecoder(obj) { | ||
@@ -51,4 +37,4 @@ if (typeof obj !== 'string') { | ||
assert.throws(() => encode.encode({ d: new Date() }), /unsupported type: Date/); | ||
assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a'); | ||
const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: dateDecoder } }); | ||
assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: common.dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a'); | ||
const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: common.dateDecoder } }); | ||
assert.instanceOf(decodedDate, Date); | ||
@@ -75,3 +61,3 @@ assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString()); | ||
assert.throws(() => decode.decode(byteUtils.fromHex(hex), { | ||
tags: { 8: dateDecoder }, | ||
tags: { 8: common.dateDecoder }, | ||
strict: true | ||
@@ -81,3 +67,3 @@ }), /integer encoded in more bytes than necessary/); | ||
const decodedDate = decode.decode(byteUtils.fromHex(hex), { | ||
tags: { 8: dateDecoder }, | ||
tags: { 8: common.dateDecoder }, | ||
strict | ||
@@ -84,0 +70,0 @@ }); |
@@ -14,18 +14,7 @@ import chai from 'chai'; | ||
} from '../lib/byte-utils.js'; | ||
import { | ||
dateDecoder, | ||
dateEncoder | ||
} from './common.js'; | ||
const {assert} = chai; | ||
function dateDecoder(obj) { | ||
if (typeof obj !== 'string') { | ||
throw new Error('expected string for tag 1'); | ||
} | ||
return new Date(obj); | ||
} | ||
function dateEncoder(obj) { | ||
if (!(obj instanceof Date)) { | ||
throw new Error('expected Date for "Date" encoder'); | ||
} | ||
return [ | ||
new Token(Type.tag, 0), | ||
new Token(Type.string, obj.toISOString().replace(/\.000Z$/, 'Z')) | ||
]; | ||
} | ||
function Uint16ArrayDecoder(obj) { | ||
@@ -32,0 +21,0 @@ if (typeof obj !== 'string') { |
@@ -38,2 +38,5 @@ import { | ||
} | ||
encodeArray.compareTokens = uint.encodeUint.compareTokens; | ||
encodeArray.compareTokens = uint.encodeUint.compareTokens; | ||
encodeArray.encodedSize = function encodedSize(token) { | ||
return uint.encodeUintValue.encodedSize(token.value); | ||
}; |
@@ -38,2 +38,5 @@ import { | ||
} | ||
encodeMap.compareTokens = uint.encodeUint.compareTokens; | ||
encodeMap.compareTokens = uint.encodeUint.compareTokens; | ||
encodeMap.encodedSize = function encodedSize(token) { | ||
return uint.encodeUintValue.encodedSize(token.value); | ||
}; |
@@ -24,2 +24,5 @@ import { | ||
} | ||
encodeTag.compareTokens = uint.encodeUint.compareTokens; | ||
encodeTag.compareTokens = uint.encodeUint.compareTokens; | ||
encodeTag.encodedSize = function encodedSize(token) { | ||
return uint.encodeUintValue.encodedSize(token.value); | ||
}; |
@@ -88,6 +88,5 @@ import { | ||
} | ||
let decoded; | ||
if (!options || options.float64 !== true) { | ||
encodeFloat16(float); | ||
decoded = readFloat16(ui8a, 1); | ||
let decoded = readFloat16(ui8a, 1); | ||
if (float === decoded || Number.isNaN(float)) { | ||
@@ -94,0 +93,0 @@ return 3; |
@@ -16,5 +16,7 @@ import { | ||
reset() { | ||
this.chunks = []; | ||
this.cursor = 0; | ||
this.maxCursor = -1; | ||
if (this.chunks.length) { | ||
this.chunks = []; | ||
} | ||
if (this._initReuseChunk !== null) { | ||
@@ -21,0 +23,0 @@ this.chunks.push(this._initReuseChunk); |
@@ -23,11 +23,15 @@ import { is } from './is.js'; | ||
}; | ||
const cborEncoders = []; | ||
cborEncoders[Type.uint.major] = encodeUint; | ||
cborEncoders[Type.negint.major] = encodeNegint; | ||
cborEncoders[Type.bytes.major] = encodeBytes; | ||
cborEncoders[Type.string.major] = encodeString; | ||
cborEncoders[Type.array.major] = encodeArray; | ||
cborEncoders[Type.map.major] = encodeMap; | ||
cborEncoders[Type.tag.major] = encodeTag; | ||
cborEncoders[Type.float.major] = encodeFloat; | ||
export function makeCborEncoders() { | ||
const encoders = []; | ||
encoders[Type.uint.major] = encodeUint; | ||
encoders[Type.negint.major] = encodeNegint; | ||
encoders[Type.bytes.major] = encodeBytes; | ||
encoders[Type.string.major] = encodeString; | ||
encoders[Type.array.major] = encodeArray; | ||
encoders[Type.map.major] = encodeMap; | ||
encoders[Type.tag.major] = encodeTag; | ||
encoders[Type.float.major] = encodeFloat; | ||
return encoders; | ||
} | ||
const cborEncoders = makeCborEncoders(); | ||
const buf = new Bl(); | ||
@@ -230,2 +234,3 @@ class Ref { | ||
} | ||
buf.reset(); | ||
tokensToEncoded(buf, tokens, encoders, options); | ||
@@ -232,0 +237,0 @@ return buf.toBytes(true); |
@@ -14,18 +14,7 @@ import chai from 'chai'; | ||
} from '../lib/byte-utils.js'; | ||
import { | ||
dateDecoder, | ||
dateEncoder | ||
} from './common.js'; | ||
const {assert} = chai; | ||
function dateDecoder(obj) { | ||
if (typeof obj !== 'string') { | ||
throw new Error('expected string for tag 1'); | ||
} | ||
return new Date(obj); | ||
} | ||
function dateEncoder(obj) { | ||
if (!(obj instanceof Date)) { | ||
throw new Error('expected Date for "Date" encoder'); | ||
} | ||
return [ | ||
new Token(Type.tag, 0), | ||
new Token(Type.string, obj.toISOString().replace(/\.000Z$/, 'Z')) | ||
]; | ||
} | ||
function Uint16ArrayDecoder(obj) { | ||
@@ -32,0 +21,0 @@ if (typeof obj !== 'string') { |
@@ -19,2 +19,3 @@ import { Token } from './lib/token' | ||
compareTokens(t1: Token, t2: Token): number; | ||
// TODO: make this non-optional as a breaking change and remove the throw in length.js | ||
encodedSize?(token: Token, options?: EncodeOptions): number; | ||
@@ -21,0 +22,0 @@ } |
@@ -106,1 +106,9 @@ import { Token, Type } from './token.js' | ||
encodeArray.compareTokens = uint.encodeUint.compareTokens | ||
/** | ||
* @param {Token} token | ||
* @returns {number} | ||
*/ | ||
encodeArray.encodedSize = function encodedSize (token) { | ||
return uint.encodeUintValue.encodedSize(token.value) | ||
} |
@@ -106,1 +106,9 @@ import { Token, Type } from './token.js' | ||
encodeMap.compareTokens = uint.encodeUint.compareTokens | ||
/** | ||
* @param {Token} token | ||
* @returns {number} | ||
*/ | ||
encodeMap.encodedSize = function encodedSize (token) { | ||
return uint.encodeUintValue.encodedSize(token.value) | ||
} |
@@ -73,1 +73,9 @@ import { Token, Type } from './token.js' | ||
encodeTag.compareTokens = uint.encodeUint.compareTokens | ||
/** | ||
* @param {Token} token | ||
* @returns {number} | ||
*/ | ||
encodeTag.encodedSize = function encodedSize (token) { | ||
return uint.encodeUintValue.encodedSize(token.value) | ||
} |
@@ -157,6 +157,5 @@ // TODO: shift some of the bytes logic to bytes-utils so we can use Buffer | ||
let decoded | ||
if (!options || options.float64 !== true) { | ||
encodeFloat16(float) | ||
decoded = readFloat16(ui8a, 1) | ||
let decoded = readFloat16(ui8a, 1) | ||
if (float === decoded || Number.isNaN(float)) { | ||
@@ -163,0 +162,0 @@ return 3 |
@@ -45,5 +45,7 @@ /** | ||
reset () { | ||
this.chunks = [] | ||
this.cursor = 0 | ||
this.maxCursor = -1 | ||
if (this.chunks.length) { | ||
this.chunks = [] | ||
} | ||
if (this._initReuseChunk !== null) { | ||
@@ -50,0 +52,0 @@ this.chunks.push(this._initReuseChunk) |
@@ -33,13 +33,18 @@ import { is } from './is.js' | ||
/** @type {TokenTypeEncoder[]} */ | ||
const cborEncoders = [] | ||
cborEncoders[Type.uint.major] = encodeUint | ||
cborEncoders[Type.negint.major] = encodeNegint | ||
cborEncoders[Type.bytes.major] = encodeBytes | ||
cborEncoders[Type.string.major] = encodeString | ||
cborEncoders[Type.array.major] = encodeArray | ||
cborEncoders[Type.map.major] = encodeMap | ||
cborEncoders[Type.tag.major] = encodeTag | ||
cborEncoders[Type.float.major] = encodeFloat | ||
/** @returns {TokenTypeEncoder[]} */ | ||
export function makeCborEncoders () { | ||
const encoders = [] | ||
encoders[Type.uint.major] = encodeUint | ||
encoders[Type.negint.major] = encodeNegint | ||
encoders[Type.bytes.major] = encodeBytes | ||
encoders[Type.string.major] = encodeString | ||
encoders[Type.array.major] = encodeArray | ||
encoders[Type.map.major] = encodeMap | ||
encoders[Type.tag.major] = encodeTag | ||
encoders[Type.float.major] = encodeFloat | ||
return encoders | ||
} | ||
const cborEncoders = makeCborEncoders() | ||
const buf = new Bl() | ||
@@ -445,2 +450,3 @@ | ||
} | ||
buf.reset() | ||
tokensToEncoded(buf, tokens, encoders, options) | ||
@@ -447,0 +453,0 @@ return buf.toBytes(true) |
{ | ||
"name": "cborg", | ||
"version": "1.8.1", | ||
"version": "1.9.0", | ||
"description": "Fast CBOR with a focus on strictness", | ||
@@ -50,2 +50,7 @@ "main": "./cjs/cborg.js", | ||
}, | ||
"./length": { | ||
"browser": "./esm/lib/length.js", | ||
"require": "./cjs/lib/length.js", | ||
"import": "./esm/lib/length.js" | ||
}, | ||
"./taglib": { | ||
@@ -68,2 +73,5 @@ "browser": "./esm/taglib.js", | ||
], | ||
"length": [ | ||
"types/lib/length.d.ts" | ||
], | ||
"*": [ | ||
@@ -165,2 +173,3 @@ "types/*" | ||
".": "./cjs/cborg.js", | ||
"./length": "./cjs/lib/length.js", | ||
"./taglib": "./cjs/taglib.js", | ||
@@ -167,0 +176,0 @@ "./json": "./cjs/lib/json/json.js" |
@@ -30,2 +30,3 @@ # cborg - fast CBOR with a focus on strictness | ||
* [Options](#options-1) | ||
* [`encodedLength(data[, options])`](#encodedlengthdata-options) | ||
* [Type encoders](#type-encoders) | ||
@@ -254,2 +255,16 @@ * [Tag decoders](#tag-decoders) | ||
### `encodedLength(data[, options])` | ||
```js | ||
import { encodedLength } from 'cborg/length' | ||
``` | ||
```js | ||
const { encodedLength } = require('cborg/length') | ||
``` | ||
Calculate the byte length of the given data when encoded as CBOR with the options provided. The options are the same as for an `encode()` call. This calculation will be accurate if the same options are used as when performing a normal `encode()`. Some encode options can change the encoding output length. | ||
A `tokensToLength()` function is available which deals directly with a tokenized form of the object, but this only recommended for advanced users. | ||
### Type encoders | ||
@@ -256,0 +271,0 @@ |
@@ -8,22 +8,6 @@ /* eslint-env mocha */ | ||
import { fromHex, toHex } from '../lib/byte-utils.js' | ||
import { dateDecoder, dateEncoder } from './common.js' | ||
const { assert } = chai | ||
function dateDecoder (obj) { | ||
if (typeof obj !== 'string') { | ||
throw new Error('expected string for tag 1') | ||
} | ||
return new Date(obj) | ||
} | ||
function dateEncoder (obj) { | ||
if (!(obj instanceof Date)) { | ||
throw new Error('expected Date for "Date" encoder') | ||
} | ||
return [ | ||
new Token(Type.tag, 0), | ||
new Token(Type.string, obj.toISOString().replace(/\.000Z$/, 'Z')) | ||
] | ||
} | ||
function Uint16ArrayDecoder (obj) { | ||
@@ -30,0 +14,0 @@ if (typeof obj !== 'string') { |
@@ -56,2 +56,7 @@ /** | ||
const compareTokens: (tok1: Token, tok2: Token) => number; | ||
/** | ||
* @param {Token} token | ||
* @returns {number} | ||
*/ | ||
function encodedSize(token: Token): number; | ||
} | ||
@@ -58,0 +63,0 @@ export type Bl = import('./bl.js').Bl; |
@@ -56,2 +56,7 @@ /** | ||
const compareTokens: (tok1: Token, tok2: Token) => number; | ||
/** | ||
* @param {Token} token | ||
* @returns {number} | ||
*/ | ||
function encodedSize(token: Token): number; | ||
} | ||
@@ -58,0 +63,0 @@ export type Bl = import('./bl.js').Bl; |
@@ -52,2 +52,7 @@ /** | ||
const compareTokens: (tok1: Token, tok2: Token) => number; | ||
/** | ||
* @param {Token} token | ||
* @returns {number} | ||
*/ | ||
function encodedSize(token: Token): number; | ||
} | ||
@@ -54,0 +59,0 @@ export type Bl = import('./bl.js').Bl; |
@@ -0,1 +1,3 @@ | ||
/** @returns {TokenTypeEncoder[]} */ | ||
export function makeCborEncoders(): TokenTypeEncoder[]; | ||
export type EncodeOptions = import('../interface').EncodeOptions; | ||
@@ -2,0 +4,0 @@ export type OptionalTypeEncoder = import('../interface').OptionalTypeEncoder; |
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
863785
225
26530
444