Comparing version 3.0.0 to 4.0.0
import { encode } from './lib/encode.js' | ||
import { decode } from './lib/decode.js' | ||
import { decode, decodeFirst } from './lib/decode.js' | ||
import { Token, Type } from './lib/token.js' | ||
@@ -16,2 +16,3 @@ | ||
decode, | ||
decodeFirst, | ||
encode, | ||
@@ -18,0 +19,0 @@ Token, |
@@ -0,1 +1,23 @@ | ||
## [4.0.0](https://github.com/rvagg/cborg/compare/v3.0.0...v4.0.0) (2023-09-12) | ||
### ⚠ BREAKING CHANGES | ||
* decodeFirst(), & require DecodeTokenizer to implement pos() | ||
### Features | ||
* add decodeFirst to json decoder ([a1bd349](https://github.com/rvagg/cborg/commit/a1bd349a6eb382737716ec1fc6c1061e710358d1)) | ||
* decodeFirst(), & require DecodeTokenizer to implement pos() ([1b35871](https://github.com/rvagg/cborg/commit/1b358712d51bfd941382b6baab333722dafdada5)) | ||
### Bug Fixes | ||
* make semantic-release work again ([7ab7a4e](https://github.com/rvagg/cborg/commit/7ab7a4eac21bfd3c98bf8836c3dccca9c56a41e4)) | ||
### Trivial Changes | ||
* recompile types ([06133db](https://github.com/rvagg/cborg/commit/06133dbf73025b3e1af8065702d7d65ef03be407)) | ||
## [2.0.5](https://github.com/rvagg/cborg/compare/v2.0.4...v2.0.5) (2023-08-25) | ||
@@ -2,0 +24,0 @@ |
@@ -29,3 +29,4 @@ import { Token } from './lib/token' | ||
done(): boolean, | ||
next(): Token | ||
next(): Token, | ||
pos(): number, | ||
} | ||
@@ -32,0 +33,0 @@ |
@@ -27,3 +27,3 @@ import { decodeErrPrefix } from './common.js' | ||
constructor (data, options = {}) { | ||
this.pos = 0 | ||
this._pos = 0 | ||
this.data = data | ||
@@ -33,8 +33,12 @@ this.options = options | ||
pos () { | ||
return this._pos | ||
} | ||
done () { | ||
return this.pos >= this.data.length | ||
return this._pos >= this.data.length | ||
} | ||
next () { | ||
const byt = this.data[this.pos] | ||
const byt = this.data[this._pos] | ||
let token = quick[byt] | ||
@@ -49,6 +53,6 @@ if (token === undefined) { | ||
const minor = byt & 31 | ||
token = decoder(this.data, this.pos, minor, this.options) | ||
token = decoder(this.data, this._pos, minor, this.options) | ||
} | ||
// @ts-ignore we get to assume encodedLength is set (crossing fingers slightly) | ||
this.pos += token.encodedLength | ||
this._pos += token.encodedLength | ||
return token | ||
@@ -177,5 +181,5 @@ } | ||
* @param {DecodeOptions} [options] | ||
* @returns {any} | ||
* @returns {[any, Uint8Array]} | ||
*/ | ||
function decode (data, options) { | ||
function decodeFirst (data, options) { | ||
if (!(data instanceof Uint8Array)) { | ||
@@ -193,3 +197,13 @@ throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`) | ||
} | ||
if (!tokeniser.done()) { | ||
return [decoded, data.subarray(tokeniser.pos())] | ||
} | ||
/** | ||
* @param {Uint8Array} data | ||
* @param {DecodeOptions} [options] | ||
* @returns {any} | ||
*/ | ||
function decode (data, options) { | ||
const [decoded, remainder] = decodeFirst(data, options) | ||
if (remainder.length > 0) { | ||
throw new Error(`${decodeErrPrefix} too many terminals, data makes no sense`) | ||
@@ -200,2 +214,2 @@ } | ||
export { Tokeniser, tokensToObject, decode } | ||
export { Tokeniser, tokensToObject, decode, decodeFirst } |
@@ -1,2 +0,2 @@ | ||
import { decode as _decode } from '../decode.js' | ||
import { decode as _decode, decodeFirst as _decodeFirst } from '../decode.js' | ||
import { Token, Type } from '../token.js' | ||
@@ -20,3 +20,3 @@ import { decodeCodePointsArray } from '../byte-utils.js' | ||
constructor (data, options = {}) { | ||
this.pos = 0 | ||
this._pos = 0 | ||
this.data = data | ||
@@ -29,2 +29,6 @@ this.options = options | ||
pos () { | ||
return this._pos | ||
} | ||
/** | ||
@@ -34,3 +38,3 @@ * @returns {boolean} | ||
done () { | ||
return this.pos >= this.data.length | ||
return this._pos >= this.data.length | ||
} | ||
@@ -42,3 +46,3 @@ | ||
ch () { | ||
return this.data[this.pos] | ||
return this.data[this._pos] | ||
} | ||
@@ -57,3 +61,3 @@ | ||
while (c === 32 /* ' ' */ || c === 9 /* '\t' */ || c === 13 /* '\r' */ || c === 10 /* '\n' */) { | ||
c = this.data[++this.pos] | ||
c = this.data[++this._pos] | ||
} | ||
@@ -66,8 +70,8 @@ } | ||
expect (str) { | ||
if (this.data.length - this.pos < str.length) { | ||
throw new Error(`${decodeErrPrefix} unexpected end of input at position ${this.pos}`) | ||
if (this.data.length - this._pos < str.length) { | ||
throw new Error(`${decodeErrPrefix} unexpected end of input at position ${this._pos}`) | ||
} | ||
for (let i = 0; i < str.length; i++) { | ||
if (this.data[this.pos++] !== str[i]) { | ||
throw new Error(`${decodeErrPrefix} unexpected token at position ${this.pos}, expected to find '${String.fromCharCode(...str)}'`) | ||
if (this.data[this._pos++] !== str[i]) { | ||
throw new Error(`${decodeErrPrefix} unexpected token at position ${this._pos}, expected to find '${String.fromCharCode(...str)}'`) | ||
} | ||
@@ -78,3 +82,3 @@ } | ||
parseNumber () { | ||
const startPos = this.pos | ||
const startPos = this._pos | ||
let negative = false | ||
@@ -90,3 +94,3 @@ let float = false | ||
if (chars.includes(ch)) { | ||
this.pos++ | ||
this._pos++ | ||
} else { | ||
@@ -101,23 +105,23 @@ break | ||
negative = true | ||
this.pos++ | ||
this._pos++ | ||
} | ||
if (this.ch() === 48) { // '0' | ||
this.pos++ | ||
this._pos++ | ||
if (this.ch() === 46) { // '.' | ||
this.pos++ | ||
this._pos++ | ||
float = true | ||
} else { | ||
return new Token(Type.uint, 0, this.pos - startPos) | ||
return new Token(Type.uint, 0, this._pos - startPos) | ||
} | ||
} | ||
swallow([48, 49, 50, 51, 52, 53, 54, 55, 56, 57]) // DIGIT | ||
if (negative && this.pos === startPos + 1) { | ||
throw new Error(`${decodeErrPrefix} unexpected token at position ${this.pos}`) | ||
if (negative && this._pos === startPos + 1) { | ||
throw new Error(`${decodeErrPrefix} unexpected token at position ${this._pos}`) | ||
} | ||
if (!this.done() && this.ch() === 46) { // '.' | ||
if (float) { | ||
throw new Error(`${decodeErrPrefix} unexpected token at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} unexpected token at position ${this._pos}`) | ||
} | ||
float = true | ||
this.pos++ | ||
this._pos++ | ||
swallow([48, 49, 50, 51, 52, 53, 54, 55, 56, 57]) // DIGIT | ||
@@ -127,5 +131,5 @@ } | ||
float = true | ||
this.pos++ | ||
this._pos++ | ||
if (!this.done() && (this.ch() === 43 || this.ch() === 45)) { // '+', '-' | ||
this.pos++ | ||
this._pos++ | ||
} | ||
@@ -135,11 +139,11 @@ swallow([48, 49, 50, 51, 52, 53, 54, 55, 56, 57]) // DIGIT | ||
// @ts-ignore | ||
const numStr = String.fromCharCode.apply(null, this.data.subarray(startPos, this.pos)) | ||
const numStr = String.fromCharCode.apply(null, this.data.subarray(startPos, this._pos)) | ||
const num = parseFloat(numStr) | ||
if (float) { | ||
return new Token(Type.float, num, this.pos - startPos) | ||
return new Token(Type.float, num, this._pos - startPos) | ||
} | ||
if (this.options.allowBigInt !== true || Number.isSafeInteger(num)) { | ||
return new Token(num >= 0 ? Type.uint : Type.negint, num, this.pos - startPos) | ||
return new Token(num >= 0 ? Type.uint : Type.negint, num, this._pos - startPos) | ||
} | ||
return new Token(num >= 0 ? Type.uint : Type.negint, BigInt(numStr), this.pos - startPos) | ||
return new Token(num >= 0 ? Type.uint : Type.negint, BigInt(numStr), this._pos - startPos) | ||
} | ||
@@ -154,9 +158,9 @@ | ||
// this would be a programming error | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this.pos}; this shouldn't happen`) | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}; this shouldn't happen`) | ||
} | ||
this.pos++ | ||
this._pos++ | ||
// check for simple fast-path, all printable ascii, no escapes | ||
// >0x10000 elements may fail fn.apply() (http://stackoverflow.com/a/22747272/680742) | ||
for (let i = this.pos, l = 0; i < this.data.length && l < 0x10000; i++, l++) { | ||
for (let i = this._pos, l = 0; i < this.data.length && l < 0x10000; i++, l++) { | ||
const ch = this.data[i] | ||
@@ -168,4 +172,4 @@ if (ch === 92 || ch < 32 || ch >= 128) { // '\', ' ', control-chars or non-trivial | ||
// @ts-ignore | ||
const str = String.fromCharCode.apply(null, this.data.subarray(this.pos, i)) | ||
this.pos = i + 1 | ||
const str = String.fromCharCode.apply(null, this.data.subarray(this._pos, i)) | ||
this._pos = i + 1 | ||
return new Token(Type.string, str, l) | ||
@@ -175,8 +179,8 @@ } | ||
const startPos = this.pos | ||
const startPos = this._pos | ||
const chars = [] | ||
const readu4 = () => { | ||
if (this.pos + 4 >= this.data.length) { | ||
throw new Error(`${decodeErrPrefix} unexpected end of unicode escape sequence at position ${this.pos}`) | ||
if (this._pos + 4 >= this.data.length) { | ||
throw new Error(`${decodeErrPrefix} unexpected end of unicode escape sequence at position ${this._pos}`) | ||
} | ||
@@ -193,6 +197,6 @@ let u4 = 0 | ||
} else { | ||
throw new Error(`${decodeErrPrefix} unexpected unicode escape character at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} unexpected unicode escape character at position ${this._pos}`) | ||
} | ||
u4 = u4 * 16 + ch | ||
this.pos++ | ||
this._pos++ | ||
} | ||
@@ -209,4 +213,4 @@ return u4 | ||
if (this.pos + bytesPerSequence > this.data.length) { | ||
throw new Error(`${decodeErrPrefix} unexpected unicode sequence at position ${this.pos}`) | ||
if (this._pos + bytesPerSequence > this.data.length) { | ||
throw new Error(`${decodeErrPrefix} unexpected unicode sequence at position ${this._pos}`) | ||
} | ||
@@ -225,3 +229,3 @@ | ||
case 2: | ||
secondByte = this.data[this.pos + 1] | ||
secondByte = this.data[this._pos + 1] | ||
if ((secondByte & 0xc0) === 0x80) { | ||
@@ -235,4 +239,4 @@ tempCodePoint = (firstByte & 0x1f) << 0x6 | (secondByte & 0x3f) | ||
case 3: | ||
secondByte = this.data[this.pos + 1] | ||
thirdByte = this.data[this.pos + 2] | ||
secondByte = this.data[this._pos + 1] | ||
thirdByte = this.data[this._pos + 2] | ||
if ((secondByte & 0xc0) === 0x80 && (thirdByte & 0xc0) === 0x80) { | ||
@@ -247,5 +251,5 @@ tempCodePoint = (firstByte & 0xf) << 0xc | (secondByte & 0x3f) << 0x6 | (thirdByte & 0x3f) | ||
case 4: | ||
secondByte = this.data[this.pos + 1] | ||
thirdByte = this.data[this.pos + 2] | ||
fourthByte = this.data[this.pos + 3] | ||
secondByte = this.data[this._pos + 1] | ||
thirdByte = this.data[this._pos + 2] | ||
fourthByte = this.data[this._pos + 3] | ||
if ((secondByte & 0xc0) === 0x80 && (thirdByte & 0xc0) === 0x80 && (fourthByte & 0xc0) === 0x80) { | ||
@@ -273,3 +277,3 @@ tempCodePoint = (firstByte & 0xf) << 0x12 | (secondByte & 0x3f) << 0xc | (thirdByte & 0x3f) << 0x6 | (fourthByte & 0x3f) | ||
chars.push(codePoint) | ||
this.pos += bytesPerSequence | ||
this._pos += bytesPerSequence | ||
} | ||
@@ -284,8 +288,8 @@ | ||
case 92: // '\' | ||
this.pos++ | ||
this._pos++ | ||
if (this.done()) { | ||
throw new Error(`${decodeErrPrefix} unexpected string termination at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} unexpected string termination at position ${this._pos}`) | ||
} | ||
ch1 = this.ch() | ||
this.pos++ | ||
this._pos++ | ||
switch (ch1) { | ||
@@ -317,14 +321,14 @@ case 34: // '"' | ||
default: | ||
throw new Error(`${decodeErrPrefix} unexpected string escape character at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} unexpected string escape character at position ${this._pos}`) | ||
} | ||
break | ||
case 34: // '"' | ||
this.pos++ | ||
return new Token(Type.string, decodeCodePointsArray(chars), this.pos - startPos) | ||
this._pos++ | ||
return new Token(Type.string, decodeCodePointsArray(chars), this._pos - startPos) | ||
default: | ||
if (ch < 32) { // ' ' | ||
throw new Error(`${decodeErrPrefix} invalid control character at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} invalid control character at position ${this._pos}`) | ||
} else if (ch < 0x80) { | ||
chars.push(ch) | ||
this.pos++ | ||
this._pos++ | ||
} else { | ||
@@ -336,3 +340,3 @@ readUtf8Char() | ||
throw new Error(`${decodeErrPrefix} unexpected end of string at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} unexpected end of string at position ${this._pos}`) | ||
} | ||
@@ -347,7 +351,7 @@ | ||
this.modeStack.push('obj-start') | ||
this.pos++ | ||
this._pos++ | ||
return new Token(Type.map, Infinity, 1) | ||
case 91: // '[' | ||
this.modeStack.push('array-start') | ||
this.pos++ | ||
this._pos++ | ||
return new Token(Type.array, Infinity, 1) | ||
@@ -379,3 +383,3 @@ case 34: { // '"' | ||
default: | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this.pos}`) | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}`) | ||
} | ||
@@ -396,3 +400,3 @@ } | ||
if (this.ch() === 93) { // ']' | ||
this.pos++ | ||
this._pos++ | ||
this.skipWhitespace() | ||
@@ -402,5 +406,5 @@ return new Token(Type.break, undefined, 1) | ||
if (this.ch() !== 44) { // ',' | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this.pos}, was expecting array delimiter but found '${String.fromCharCode(this.ch())}'`) | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}, was expecting array delimiter but found '${String.fromCharCode(this.ch())}'`) | ||
} | ||
this.pos++ | ||
this._pos++ | ||
this.modeStack.push('array-value') | ||
@@ -413,3 +417,3 @@ this.skipWhitespace() | ||
if (this.ch() === 93) { // ']' | ||
this.pos++ | ||
this._pos++ | ||
this.skipWhitespace() | ||
@@ -426,3 +430,3 @@ return new Token(Type.break, undefined, 1) | ||
this.modeStack.pop() | ||
this.pos++ | ||
this._pos++ | ||
this.skipWhitespace() | ||
@@ -432,5 +436,5 @@ return new Token(Type.break, undefined, 1) | ||
if (this.ch() !== 44) { // ',' | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this.pos}, was expecting object delimiter but found '${String.fromCharCode(this.ch())}'`) | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}, was expecting object delimiter but found '${String.fromCharCode(this.ch())}'`) | ||
} | ||
this.pos++ | ||
this._pos++ | ||
this.skipWhitespace() | ||
@@ -440,3 +444,3 @@ case 'obj-start': { // eslint-disable-line no-fallthrough | ||
if (this.ch() === 125) { // '}' | ||
this.pos++ | ||
this._pos++ | ||
this.skipWhitespace() | ||
@@ -448,5 +452,5 @@ return new Token(Type.break, undefined, 1) | ||
if (this.ch() !== 58) { // ':' | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this.pos}, was expecting key/value delimiter ':' but found '${String.fromCharCode(this.ch())}'`) | ||
throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}, was expecting key/value delimiter ':' but found '${String.fromCharCode(this.ch())}'`) | ||
} | ||
this.pos++ | ||
this._pos++ | ||
this.modeStack.push('obj-value') | ||
@@ -463,3 +467,3 @@ return token | ||
default: | ||
throw new Error(`${decodeErrPrefix} unexpected parse state at position ${this.pos}; this shouldn't happen`) | ||
throw new Error(`${decodeErrPrefix} unexpected parse state at position ${this._pos}; this shouldn't happen`) | ||
} | ||
@@ -479,2 +483,12 @@ } | ||
export { decode, Tokenizer } | ||
/** | ||
* @param {Uint8Array} data | ||
* @param {DecodeOptions} [options] | ||
* @returns {[any, Uint8Array]} | ||
*/ | ||
function decodeFirst (data, options) { | ||
options = Object.assign({ tokenizer: new Tokenizer(data, options) }, options) | ||
return _decodeFirst(data, options) | ||
} | ||
export { decode, decodeFirst, Tokenizer } |
import { encode } from './encode.js' | ||
import { decode, Tokenizer } from './decode.js' | ||
import { decode, decodeFirst, Tokenizer } from './decode.js' | ||
export { encode, decode, Tokenizer } | ||
export { encode, decode, decodeFirst, Tokenizer } |
{ | ||
"name": "cborg", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Fast CBOR with a focus on strictness", | ||
@@ -23,3 +23,3 @@ "main": "cborg.js", | ||
"type": "git", | ||
"url": "https://github.com/rvagg/cborg.git" | ||
"url": "git@github.com:rvagg/cborg.git" | ||
}, | ||
@@ -32,2 +32,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"@semantic-release/changelog": "^6.0.3", | ||
"@semantic-release/commit-analyzer": "^10.0.4", | ||
"@semantic-release/git": "^10.0.1", | ||
"@semantic-release/github": "^9.0.5", | ||
"@semantic-release/npm": "^10.0.5", | ||
"@semantic-release/release-notes-generator": "^11.0.7", | ||
"@types/chai": "^4.3.6", | ||
@@ -38,5 +44,7 @@ "@types/mocha": "^10.0.1", | ||
"chai": "^4.3.8", | ||
"conventional-changelog-conventionalcommits": "^6.1.0", | ||
"ipld-garbage": "^5.0.0", | ||
"mocha": "^10.2.0", | ||
"polendina": "^3.2.1", | ||
"semantic-release": "^21.1.1", | ||
"standard": "^17.1.0", | ||
@@ -43,0 +51,0 @@ "typescript": "^5.2.2" |
@@ -30,2 +30,3 @@ # cborg - fast CBOR with a focus on strictness | ||
* [Options](#options-1) | ||
* [`decodeFirst(data[, options])`](#decodefirstdata-options) | ||
* [`encodedLength(data[, options])`](#encodedlengthdata-options) | ||
@@ -200,6 +201,2 @@ * [Type encoders](#type-encoders) | ||
```js | ||
const { encode } = require('cborg') | ||
``` | ||
Encode a JavaScript object and return a `Uint8Array` with the CBOR byte representation. | ||
@@ -231,6 +228,2 @@ | ||
```js | ||
const { decode } = require('cborg') | ||
``` | ||
Decode valid CBOR bytes from a `Uint8Array` (or `Buffer`) and return a JavaScript object. | ||
@@ -258,12 +251,45 @@ | ||
### `encodedLength(data[, options])` | ||
### `decodeFirst(data[, options])` | ||
```js | ||
import { encodedLength } from 'cborg/length' | ||
import { decodeFirst } from 'cborg' | ||
``` | ||
Decode valid CBOR bytes from a `Uint8Array` (or `Buffer`) and return a JavaScript object ***and*** the remainder of the original byte array that was not consumed by the decode. This can be useful for decoding concatenated CBOR objects, which is often used in streaming modes of CBOR. | ||
The returned remainder `Uint8Array` is a subarray of the original input `Uint8Array` and will share the same underlying buffer. This means that there are no new allocations performed by this function and it is as efficient to use as `decode` but without the additional byte-consumption check. | ||
The options for `decodeFirst` are the same as for [`decode()`](#decodedata-options), but the return type is different and `decodeFirst()` will not error if a decode operation doesn't consume all of the input bytes. | ||
The return value is an array with two elements: | ||
* `value`: the decoded JavaScript object | ||
* `remainder`: a `Uint8Array` containing the bytes that were not consumed by the decode operation | ||
```js | ||
const { encodedLength } = require('cborg/length') | ||
import { decodeFirst } from 'cborg' | ||
let buf = Buffer.from('a16474686973a26269736543424f522163796179f564746869736269736543424f522163796179f5', 'hex') | ||
while (buf.length) { | ||
const [value, remainder] = decodeFirst(buf) | ||
console.log('decoded:', value) | ||
buf = remainder | ||
} | ||
``` | ||
``` | ||
decoded: { this: { is: 'CBOR!', yay: true } } | ||
decoded: this | ||
decoded: is | ||
decoded: CBOR! | ||
decoded: yay | ||
decoded: true | ||
``` | ||
### `encodedLength(data[, options])` | ||
```js | ||
import { encodedLength } from '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. | ||
@@ -407,3 +433,3 @@ | ||
Use `import { encode, decode } from 'cborg/json'` or `const { encode, decode } = require('cborg/json')` to access the JSON handling encoder and decoder. | ||
Use `import { encode, decode, decodeFirst } from 'cborg/json'` to access the JSON handling encoder and decoder. | ||
@@ -410,0 +436,0 @@ Many of the same encode and decode options available for CBOR can be used to manage JSON handling. These include strictness requirements for decode and custom tag encoders for encode. Tag encoders can't create new tags as there are no tags in JSON, but they can replace JavaScript object forms with custom JSON forms (e.g. convert a `Uint8Array` to a valid JSON form rather than having the encoder throw an error). The inverse is also possible, turning specific JSON forms into JavaScript forms, by using a custom tokenizer on decode. |
/* eslint-env mocha,es2020 */ | ||
import { assert } from 'chai' | ||
import { decode, encode } from 'cborg/json' | ||
import { decode, decodeFirst, encode } from 'cborg/json' | ||
@@ -191,2 +191,30 @@ const toBytes = (str) => new TextEncoder().encode(str) | ||
}) | ||
it('decodeFirst', () => { | ||
/* | ||
const encoded = new TextDecoder().decode(encode(obj, sorting === false ? { mapSorter: null } : undefined)) | ||
const json = JSON.stringify(obj) | ||
assert.strictEqual(encoded, json) | ||
const decoded = decode(toBytes(JSON.stringify(obj))) | ||
assert.deepStrictEqual(decoded, obj) | ||
*/ | ||
let buf = new TextEncoder().encode('{"foo":1,"bar":2}1"ping"2null3[1,2,3]""[[],[],{"boop":true}]') | ||
const expected = [ | ||
{ foo: 1, bar: 2 }, | ||
1, | ||
'ping', | ||
2, | ||
null, | ||
3, | ||
[1, 2, 3], | ||
'', | ||
[[], [], { boop: true }] | ||
] | ||
for (const exp of expected) { | ||
const [obj, rest] = decodeFirst(buf) | ||
assert.deepStrictEqual(exp, obj) | ||
buf = rest | ||
} | ||
assert.strictEqual(buf.length, 0) | ||
}) | ||
}) |
@@ -18,6 +18,7 @@ /** | ||
import { decode } from './lib/decode.js'; | ||
import { decodeFirst } from './lib/decode.js'; | ||
import { encode } from './lib/encode.js'; | ||
import { Token } from './lib/token.js'; | ||
import { Type } from './lib/token.js'; | ||
export { decode, encode, Token, Type }; | ||
export { decode, decodeFirst, encode, Token, Type }; | ||
//# sourceMappingURL=cborg.d.ts.map |
@@ -21,2 +21,3 @@ import { Token } from './lib/token'; | ||
next(): Token; | ||
pos(): number; | ||
} | ||
@@ -23,0 +24,0 @@ export type TagDecoder = (inner: any) => any; |
@@ -13,5 +13,6 @@ export type Token = import('./token.js').Token; | ||
constructor(data: Uint8Array, options?: DecodeOptions); | ||
pos: number; | ||
_pos: number; | ||
data: Uint8Array; | ||
options: import("../interface").DecodeOptions; | ||
pos(): number; | ||
done(): boolean; | ||
@@ -32,2 +33,8 @@ next(): import("./token.js").Token; | ||
export function decode(data: Uint8Array, options?: import("../interface").DecodeOptions | undefined): any; | ||
/** | ||
* @param {Uint8Array} data | ||
* @param {DecodeOptions} [options] | ||
* @returns {[any, Uint8Array]} | ||
*/ | ||
export function decodeFirst(data: Uint8Array, options?: import("../interface").DecodeOptions | undefined): [any, Uint8Array]; | ||
declare const BREAK: unique symbol; | ||
@@ -34,0 +41,0 @@ declare const DONE: unique symbol; |
@@ -10,2 +10,8 @@ export type DecodeOptions = import('../../interface').DecodeOptions; | ||
/** | ||
* @param {Uint8Array} data | ||
* @param {DecodeOptions} [options] | ||
* @returns {[any, Uint8Array]} | ||
*/ | ||
export function decodeFirst(data: Uint8Array, options?: import("../../interface").DecodeOptions | undefined): [any, Uint8Array]; | ||
/** | ||
* @typedef {import('../../interface').DecodeOptions} DecodeOptions | ||
@@ -23,3 +29,3 @@ * @typedef {import('../../interface').DecodeTokenizer} DecodeTokenizer | ||
constructor(data: Uint8Array, options?: DecodeOptions); | ||
pos: number; | ||
_pos: number; | ||
data: Uint8Array; | ||
@@ -30,2 +36,3 @@ options: import("../../interface").DecodeOptions; | ||
lastToken: string; | ||
pos(): number; | ||
/** | ||
@@ -32,0 +39,0 @@ * @returns {boolean} |
import { encode } from './encode.js'; | ||
import { decode } from './decode.js'; | ||
import { decodeFirst } from './decode.js'; | ||
import { Tokenizer } from './decode.js'; | ||
export { encode, decode, Tokenizer }; | ||
export { encode, decode, decodeFirst, Tokenizer }; | ||
//# sourceMappingURL=json.d.ts.map |
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
330555
111
7819
471
18