diff --git a/lib/bin.js b/cborg/bin.js similarity index 98% rename from lib/bin.js rename to cborg/bin.js index d6ae72b..d74924e 100755 --- a/lib/bin.js +++ b/cborg/bin.js @@ -3,7 +3,7 @@ import process from 'process' import { decode, encode } from '../cborg.js' import { tokensToDiagnostic, fromDiag } from './diagnostic.js' -import { fromHex as _fromHex, toHex } from './byte-utils.js' +import { fromHex as _fromHex, toHex } from 'cborg/utils' /** * @param {number} code diff --git a/cborg/common.js b/cborg/common.js new file mode 100644 index 0000000..760f6d8 --- /dev/null +++ b/cborg/common.js @@ -0,0 +1,28 @@ +// const decodeErrPrefix = 'CBOR decode error:' +// const encodeErrPrefix = 'CBOR encode error:' + +const uintMinorPrefixBytes = [] +uintMinorPrefixBytes[23] = 1 +uintMinorPrefixBytes[24] = 2 +uintMinorPrefixBytes[25] = 3 +uintMinorPrefixBytes[26] = 5 +uintMinorPrefixBytes[27] = 9 + +// /** +// * @param {Uint8Array} data +// * @param {number} pos +// * @param {number} need +// * @param {string} decodeErrPrefix +// */ +// function assertEnoughData (data, pos, need, decodeErrPrefix) { +// if (data.length - pos < need) { +// throw new Error(`${decodeErrPrefix} not enough data for type`) +// } +// } + +export { + // decodeErrPrefix, + // encodeErrPrefix, + uintMinorPrefixBytes + // assertEnoughData +} diff --git a/lib/decode.js b/cborg/decode.js similarity index 71% rename from lib/decode.js rename to cborg/decode.js index c3d8105..b20de47 100644 --- a/lib/decode.js +++ b/cborg/decode.js @@ -1,18 +1,18 @@ -import { decodeErrPrefix } from './common.js' -import { Type } from './token.js' -import { jump, quick } from './jump.js' +import { jump, quick, Type } from 'cborg/utils' /** - * @typedef {import('./token.js').Token} Token + * @typedef {import('cborg/utils').Token} Token * @typedef {import('../interface').DecodeOptions} DecodeOptions * @typedef {import('../interface').DecodeTokenizer} DecodeTokenizer + * @typedef {import('../interface').DecodeFunction} DecodeFunction */ -const defaultDecodeOptions = { +const /** @type {DecodeOptions} */defaultDecodeOptions = { strict: false, allowIndefinite: true, allowUndefined: true, - allowBigInt: true + allowBigInt: true, + decodeErrPrefix: 'CBOR decode error:' } /** @@ -23,10 +23,11 @@ class Tokeniser { * @param {Uint8Array} data * @param {DecodeOptions} options */ - constructor (data, options = {}) { + constructor (data, options) { this._pos = 0 this.data = data this.options = options + this.jump = jump(options.decodeErrPrefix) } pos () { @@ -41,11 +42,11 @@ class Tokeniser { const byt = this.data[this._pos] let token = quick[byt] if (token === undefined) { - const decoder = jump[byt] + const decoder = this.jump[byt] /* c8 ignore next 4 */ // if we're here then there's something wrong with our jump or quick lists! if (!decoder) { - throw new Error(`${decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, '0')})`) + throw new Error(`${this.options.decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, '0')})`) } const minor = byt & 31 token = decoder(this.data, this._pos, minor, this.options) @@ -74,10 +75,10 @@ function tokenToArray (token, tokeniser, options) { // normal end to indefinite length array break } - throw new Error(`${decodeErrPrefix} got unexpected break to lengthed array`) + throw new Error(`${options.decodeErrPrefix} got unexpected break to lengthed array`) } if (value === DONE) { - throw new Error(`${decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`) + throw new Error(`${options.decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`) } arr[i] = value } @@ -101,23 +102,23 @@ function tokenToMap (token, tokeniser, options) { // normal end to indefinite length map break } - throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`) + throw new Error(`${options.decodeErrPrefix} got unexpected break to lengthed map`) } if (key === DONE) { - throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`) + throw new Error(`${options.decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`) } if (useMaps !== true && typeof key !== 'string') { - throw new Error(`${decodeErrPrefix} non-string keys not supported (got ${typeof key})`) + throw new Error(`${options.decodeErrPrefix} non-string keys not supported (got ${typeof key})`) } if (options.rejectDuplicateMapKeys === true) { // @ts-ignore if ((useMaps && m.has(key)) || (!useMaps && (key in obj))) { - throw new Error(`${decodeErrPrefix} found repeat map key "${key}"`) + throw new Error(`${options.decodeErrPrefix} found repeat map key "${key}"`) } } const value = tokensToObject(tokeniser, options) if (value === DONE) { - throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`) + throw new Error(`${options.decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`) } if (useMaps) { // @ts-ignore TODO reconsider this .. maybe needs to be strict about key types @@ -166,7 +167,7 @@ function tokensToObject (tokeniser, options) { const tagged = tokensToObject(tokeniser, options) return options.tags[token.value](tagged) } - throw new Error(`${decodeErrPrefix} tag not supported (${token.value})`) + throw new Error(`${options.decodeErrPrefix} tag not supported (${token.value})`) } /* c8 ignore next */ throw new Error('unsupported') @@ -178,17 +179,17 @@ function tokensToObject (tokeniser, options) { * @returns {[any, Uint8Array]} */ function decodeFirst (data, options) { + options = Object.assign({}, defaultDecodeOptions, options) if (!(data instanceof Uint8Array)) { - throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`) + throw new Error(`${options.decodeErrPrefix} data to decode must be a Uint8Array`) } - options = Object.assign({}, defaultDecodeOptions, options) const tokeniser = options.tokenizer || new Tokeniser(data, options) const decoded = tokensToObject(tokeniser, options) if (decoded === DONE) { - throw new Error(`${decodeErrPrefix} did not find any content to decode`) + throw new Error(`${options.decodeErrPrefix} did not find any content to decode`) } if (decoded === BREAK) { - throw new Error(`${decodeErrPrefix} got unexpected break`) + throw new Error(`${options.decodeErrPrefix} got unexpected break`) } return [decoded, data.subarray(tokeniser.pos())] } @@ -199,9 +200,10 @@ function decodeFirst (data, options) { * @returns {any} */ function decode (data, options) { + options = Object.assign({}, defaultDecodeOptions, options) const [decoded, remainder] = decodeFirst(data, options) if (remainder.length > 0) { - throw new Error(`${decodeErrPrefix} too many terminals, data makes no sense`) + throw new Error(`${options.decodeErrPrefix} too many terminals, data makes no sense:${remainder.length}`) } return decoded } diff --git a/lib/diagnostic.js b/cborg/diagnostic.js similarity index 95% rename from lib/diagnostic.js rename to cborg/diagnostic.js index f32b0a9..c596f58 100644 --- a/lib/diagnostic.js +++ b/cborg/diagnostic.js @@ -1,7 +1,6 @@ import { Tokeniser } from './decode.js' import { Type } from './token.js' -import { toHex, fromHex } from './byte-utils.js' -import { uintBoundaries } from './0uint.js' +import { uintBoundaries, toHex, fromHex } from 'cborg/utils' const utf8Encoder = new TextEncoder() const utf8Decoder = new TextDecoder() @@ -11,7 +10,11 @@ const utf8Decoder = new TextDecoder() * @param {number} [width] */ function * tokensToDiagnostic (inp, width = 100) { - const tokeniser = new Tokeniser(inp, { retainStringBytes: true, allowBigInt: true }) + const tokeniser = new Tokeniser(inp, { + decodeErrPrefix: 'Diagnostic decode error: ', + retainStringBytes: true, + allowBigInt: true + }) let pos = 0 const indent = [] diff --git a/lib/diagnostic_test.js b/cborg/diagnostic_test.js similarity index 98% rename from lib/diagnostic_test.js rename to cborg/diagnostic_test.js index b372a43..5cb855e 100644 --- a/lib/diagnostic_test.js +++ b/cborg/diagnostic_test.js @@ -1,5 +1,5 @@ import { tokensToDiagnostic } from './diagnostic.js' -import { fromHex } from './byte-utils.js' +import { fromHex } from 'cborg/utils' const inp = ` a7 diff --git a/lib/encode.js b/cborg/encode.js similarity index 94% rename from lib/encode.js rename to cborg/encode.js index acd7bac..ba40370 100644 --- a/lib/encode.js +++ b/cborg/encode.js @@ -1,18 +1,17 @@ import { is } from './is.js' -import { Token, Type } from './token.js' -import { Bl } from './bl.js' -import { encodeErrPrefix } from './common.js' -import { quickEncodeToken } from './jump.js' -import { asU8A } from './byte-utils.js' - -import { encodeUint } from './0uint.js' -import { encodeNegint } from './1negint.js' -import { encodeBytes } from './2bytes.js' -import { encodeString } from './3string.js' -import { encodeArray } from './4array.js' -import { encodeMap } from './5map.js' -import { encodeTag } from './6tag.js' -import { encodeFloat } from './7float.js' +import { + Token, Type, Bl, + quickEncodeToken, + asU8A, + encodeUint, + encodeNegint, + encodeBytes, + encodeString, + encodeArray, + encodeMap, + encodeTag, + encodeFloat +} from 'cborg/utils' /** * @typedef {import('../interface').EncodeOptions} EncodeOptions @@ -27,7 +26,9 @@ import { encodeFloat } from './7float.js' const defaultEncodeOptions = { float64: false, mapSorter, - quickEncodeToken + quickEncodeToken, + encodeErrPrefix: 'CBOR encode error:' + } /** @returns {TokenTypeEncoder[]} */ @@ -77,9 +78,10 @@ class Ref { /** * @param {Reference|undefined} stack * @param {object|any[]} obj + * @param {EncodeOptions} options * @returns {Reference} */ - static createCheck (stack, obj) { + static createCheck (stack, obj, { encodeErrPrefix }) { if (stack && stack.includes(obj)) { throw new Error(`${encodeErrPrefix} object contains circular references`) } @@ -221,7 +223,7 @@ const typeEncoders = { } return simpleTokens.emptyArray } - refStack = Ref.createCheck(refStack, obj) + refStack = Ref.createCheck(refStack, obj, options) const entries = [] let i = 0 for (const e of obj) { @@ -252,7 +254,7 @@ const typeEncoders = { } return simpleTokens.emptyMap } - refStack = Ref.createCheck(refStack, obj) + refStack = Ref.createCheck(refStack, obj, options) /** @type {TokenOrNestedTokens[]} */ const entries = [] let i = 0 @@ -278,11 +280,11 @@ for (const typ of 'Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt6 /** * @param {any} obj - * @param {EncodeOptions} [options] + * @param {EncodeOptions} options * @param {Reference} [refStack] * @returns {TokenOrNestedTokens} */ -function objectToTokens (obj, options = {}, refStack) { +function objectToTokens (obj, options, refStack) { const typ = is(obj) const customTypeEncoder = (options && options.typeEncoders && /** @type {OptionalTypeEncoder} */ options.typeEncoders[typ]) || typeEncoders[typ] if (typeof customTypeEncoder === 'function') { @@ -293,7 +295,7 @@ function objectToTokens (obj, options = {}, refStack) { } const typeEncoder = typeEncoders[typ] if (!typeEncoder) { - throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`) + throw new Error(`${options.encodeErrPrefix} unsupported type: ${typ}`) } return typeEncoder(obj, typ, options, refStack) } diff --git a/cborg.js b/cborg/index.js similarity index 50% rename from cborg.js rename to cborg/index.js index ba43c5f..ed029a8 100644 --- a/cborg.js +++ b/cborg/index.js @@ -1,21 +1,21 @@ -import { encode } from './lib/encode.js' -import { decode, decodeFirst, Tokeniser, tokensToObject } from './lib/decode.js' -import { Token, Type } from './lib/token.js' +import { encode } from './encode.js' +import { decode, decodeFirst, Tokeniser, tokensToObject } from './decode.js' +import { Token, Type } from 'cborg/utils' // is this needed for the json module and other independ encoders -export { encodeCustom } from './lib/encode.js' -export { encodeErrPrefix, decodeErrPrefix } from './lib/common.js' -export { asU8A, fromString, decodeCodePointsArray } from './lib/byte-utils.js' -export { quickEncodeToken } from './lib/jump.js' -export { makeCborEncoders, objectToTokens } from './lib/encode.js' +export { encodeCustom } from './encode.js' +// export { asU8A, fromString, decodeCodePointsArray } from './utils/byte-utils.js' +// export { quickEncodeToken } from './utils/jump.js' +// export { encodeErrPrefix, decodeErrPrefix } from './lib/common.js' +// export { makeCborEncoders, objectToTokens } from './lib/encode.js' /** * Export the types that were present in the original manual cborg.d.ts - * @typedef {import('./interface').TagDecoder} TagDecoder + * @typedef {import('../interface').TagDecoder} TagDecoder * There was originally just `TypeEncoder` so don't break types by renaming or not exporting - * @typedef {import('./interface').OptionalTypeEncoder} TypeEncoder - * @typedef {import('./interface').DecodeOptions} DecodeOptions - * @typedef {import('./interface').EncodeOptions} EncodeOptions + * @typedef {import('../interface').OptionalTypeEncoder} TypeEncoder + * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface').EncodeOptions} EncodeOptions */ export { diff --git a/lib/is.js b/cborg/is.js similarity index 100% rename from lib/is.js rename to cborg/is.js diff --git a/lib/length.js b/cborg/length.js similarity index 91% rename from lib/length.js rename to cborg/length.js index 3c490e0..fabf72a 100644 --- a/lib/length.js +++ b/cborg/length.js @@ -1,6 +1,5 @@ -// never reference the file directly to ensure the -// indepency of the json module -import { makeCborEncoders, objectToTokens, quickEncodeToken } from 'cborg' +import { makeCborEncoders, objectToTokens } from './encode.js' +import { quickEncodeToken } from 'cborg/utils' /** * @typedef {import('../interface').EncodeOptions} EncodeOptions @@ -13,7 +12,8 @@ const cborEncoders = makeCborEncoders() /** @type {EncodeOptions} */ const defaultEncodeOptions = { float64: false, - quickEncodeToken + quickEncodeToken, + encodeErrPrefix: 'CBOR encode error: ' } /** diff --git a/interface.ts b/interface.ts index 020264d..6c6a322 100644 --- a/interface.ts +++ b/interface.ts @@ -1,5 +1,4 @@ -import { Token } from './lib/token' -import { Bl } from './lib/bl' +import type { Token, Bl } from 'cborg/utils' export type TokenOrNestedTokens = Token | Token[] | TokenOrNestedTokens[] @@ -14,12 +13,14 @@ export type OptionalTypeEncoder = (data: any, typ: string, options: EncodeOption export type StrictTypeEncoder = (data: any, typ: string, options: EncodeOptions, refStack?: Reference) => TokenOrNestedTokens export type TokenTypeEncoder = { - (buf: Bl, token: Token, options?: EncodeOptions): void; + (buf: Bl, token: Token, options: EncodeOptions): void; 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; + encodedSize?(token: Token, options: EncodeOptions): number; } +export type DecodeFunction = (data:Uint8Array, pos:number, minor:number, options:DecodeOptions) => any + export type MapSorter = (e1: (Token | Token[])[], e2: (Token | Token[])[]) => number export type QuickEncodeToken = (token: Token) => Uint8Array | undefined @@ -45,6 +46,7 @@ export interface DecodeOptions { retainStringBytes?: boolean tags?: TagDecoder[], tokenizer?: DecodeTokenizer + decodeErrPrefix: string } export interface EncodeOptions { @@ -53,4 +55,5 @@ export interface EncodeOptions { mapSorter?: MapSorter, quickEncodeToken?: QuickEncodeToken, typeEncoders?: { [typeName: string]: OptionalTypeEncoder } + encodeErrPrefix: string } diff --git a/lib/json/decode.js b/json/decode.js similarity index 81% rename from lib/json/decode.js rename to json/decode.js index ea16ccd..38aa6c3 100644 --- a/lib/json/decode.js +++ b/json/decode.js @@ -1,13 +1,11 @@ // never reference the file directly to ensure the // indepency of the json module import { decode as _decode, decodeFirst as _decodeFirst } from 'cborg' -import { Token, Type } from 'cborg' -import { decodeCodePointsArray } from 'cborg' -import { decodeErrPrefix } from 'cborg' +import { Token, Type, decodeCodePointsArray } from 'cborg/utils' /** - * @typedef {import('../../interface').DecodeOptions} DecodeOptions - * @typedef {import('../../interface').DecodeTokenizer} DecodeTokenizer + * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface').DecodeTokenizer} DecodeTokenizer */ /** @@ -18,7 +16,7 @@ class Tokenizer { * @param {Uint8Array} data * @param {DecodeOptions} options */ - constructor (data, options = {}) { + constructor (data, options) { this._pos = 0 this.data = data this.options = options @@ -65,11 +63,11 @@ class Tokenizer { */ expect (str) { if (this.data.length - this._pos < str.length) { - throw new Error(`${decodeErrPrefix} unexpected end of input at position ${this._pos}`) + throw new Error(`${this.options.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)}'`) + throw new Error(`${this.options.decodeErrPrefix} unexpected token at position ${this._pos}, expected to find '${String.fromCharCode(...str)}'`) } } } @@ -109,11 +107,11 @@ class Tokenizer { } 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}`) + throw new Error(`${this.options.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(`${this.options.decodeErrPrefix} unexpected token at position ${this._pos}`) } float = true this._pos++ @@ -146,7 +144,7 @@ class Tokenizer { /* c8 ignore next 4 */ if (this.ch() !== 34) { // '"' // this would be a programming error - throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}; this shouldn't happen`) + throw new Error(`${this.options.decodeErrPrefix} unexpected character at position ${this._pos}; this shouldn't happen`) } this._pos++ @@ -170,7 +168,7 @@ class Tokenizer { const readu4 = () => { if (this._pos + 4 >= this.data.length) { - throw new Error(`${decodeErrPrefix} unexpected end of unicode escape sequence at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected end of unicode escape sequence at position ${this._pos}`) } let u4 = 0 for (let i = 0; i < 4; i++) { @@ -182,7 +180,7 @@ class Tokenizer { } else if (ch >= 65 && ch <= 70) { // 'A' && 'F' ch = ch - 65 + 10 } else { - throw new Error(`${decodeErrPrefix} unexpected unicode escape character at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected unicode escape character at position ${this._pos}`) } u4 = u4 * 16 + ch this._pos++ @@ -198,7 +196,7 @@ class Tokenizer { let bytesPerSequence = (firstByte > 0xef) ? 4 : (firstByte > 0xdf) ? 3 : (firstByte > 0xbf) ? 2 : 1 if (this._pos + bytesPerSequence > this.data.length) { - throw new Error(`${decodeErrPrefix} unexpected unicode sequence at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected unicode sequence at position ${this._pos}`) } let secondByte, thirdByte, fourthByte, tempCodePoint @@ -269,7 +267,7 @@ class Tokenizer { case 92: // '\' this._pos++ if (this.done()) { - throw new Error(`${decodeErrPrefix} unexpected string termination at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected string termination at position ${this._pos}`) } ch1 = this.ch() this._pos++ @@ -299,7 +297,7 @@ class Tokenizer { chars.push(readu4()) break default: - throw new Error(`${decodeErrPrefix} unexpected string escape character at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected string escape character at position ${this._pos}`) } break case 34: // '"' @@ -307,7 +305,7 @@ class Tokenizer { 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(`${this.options.decodeErrPrefix} invalid control character at position ${this._pos}`) } else if (ch < 0x80) { chars.push(ch) this._pos++ @@ -317,7 +315,7 @@ class Tokenizer { } } - throw new Error(`${decodeErrPrefix} unexpected end of string at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected end of string at position ${this._pos}`) } /** @@ -358,7 +356,7 @@ class Tokenizer { case 57: // '9' return this.parseNumber() default: - throw new Error(`${decodeErrPrefix} unexpected character at position ${this._pos}`) + throw new Error(`${this.options.decodeErrPrefix} unexpected character at position ${this._pos}`) } } @@ -379,7 +377,7 @@ class Tokenizer { 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(`${this.options.decodeErrPrefix} unexpected character at position ${this._pos}, was expecting array delimiter but found '${String.fromCharCode(this.ch())}'`) } this._pos++ this.modeStack.push('array-value') @@ -406,7 +404,7 @@ class Tokenizer { 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(`${this.options.decodeErrPrefix} unexpected character at position ${this._pos}, was expecting object delimiter but found '${String.fromCharCode(this.ch())}'`) } this._pos++ this.skipWhitespace() @@ -420,7 +418,7 @@ class Tokenizer { const token = this.parseString() this.skipWhitespace() 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(`${this.options.decodeErrPrefix} unexpected character at position ${this._pos}, was expecting key/value delimiter ':' but found '${String.fromCharCode(this.ch())}'`) } this._pos++ this.modeStack.push('obj-value') @@ -434,7 +432,7 @@ class Tokenizer { } /* c8 ignore next 2 */ default: - throw new Error(`${decodeErrPrefix} unexpected parse state at position ${this._pos}; this shouldn't happen`) + throw new Error(`${this.options.decodeErrPrefix} unexpected parse state at position ${this._pos}; this shouldn't happen`) } } } @@ -445,7 +443,8 @@ class Tokenizer { * @returns {any} */ function decode (data, options) { - options = Object.assign({ tokenizer: new Tokenizer(data, options) }, options) + options = { decodeErrPrefix: 'JSON decode error:', ...options } + options.tokenizer = options.tokenizer || new Tokenizer(data, options) return _decode(data, options) } @@ -455,7 +454,8 @@ function decode (data, options) { * @returns {[any, Uint8Array]} */ function decodeFirst (data, options) { - options = Object.assign({ tokenizer: new Tokenizer(data, options) }, options) + options = { decodeErrPrefix: 'JSON decode error:', ...options } + options.tokenizer = options.tokenizer || new Tokenizer(data, options) return _decodeFirst(data, options) } diff --git a/lib/json/encode.js b/json/encode.js similarity index 89% rename from lib/json/encode.js rename to json/encode.js index 650b44d..91d96e9 100644 --- a/lib/json/encode.js +++ b/json/encode.js @@ -1,23 +1,21 @@ // never reference the file directly to ensure the // indepency of the json module -import { Type } from 'cborg' import { encodeCustom } from 'cborg' -import { encodeErrPrefix } from 'cborg' -import { asU8A, fromString } from 'cborg' +import { Type, asU8A, fromString } from 'cborg/utils' /** - * @typedef {import('../../interface').EncodeOptions} EncodeOptions - * @typedef {import('../../interface').TokenTypeEncoder} TokenTypeEncoder - * @typedef {import('../token').Token} Token - * @typedef {import('../bl').Bl} Bl + * @typedef {import('../interface').EncodeOptions} EncodeOptions + * @typedef {import('../interface').TokenTypeEncoder} TokenTypeEncoder + * @typedef {import('cborg/utils').Token} Token + * @typedef {import('cborg/utils').Bl} Bl */ /** - * @param {(buf: Bl, token: Token) => void} action + * @param {(buf: Bl, token: Token, opt: EncodeOptions) => void} action * @returns {TokenTypeEncoder} */ function wrapCompareTokens (action) { - const wrapped = (/** @type {Bl} */ buf, /** @type {Token} */token) => action(buf, token) + const wrapped = (/** @type {Bl} */ buf, /** @type {Token} */token, /** @type {EncodeOptions} */opt) => action(buf, token, opt) /** * @param {Token} tok1 * @param {Token} tok2 @@ -32,7 +30,7 @@ function wrapCompareTokens (action) { /** * @returns {TokenTypeEncoder[]} */ -function makeJSONEncoders() { +function makeJSONEncoders () { /** @type {TokenTypeEncoder[]} */ const encoders = [] /** @type {{type:Type,elements:number}[]} */ @@ -73,11 +71,11 @@ function makeJSONEncoders() { buf.push(isa) }) - encoders[Type.negint.major] = wrapCompareTokens((buf, token) => { - encoders[Type.uint.major](buf, token) - }) + encoders[Type.negint.major] = wrapCompareTokens((buf, token, opt) => { + encoders[Type.uint.major](buf, token, opt) + }) - encoders[Type.bytes.major] = wrapCompareTokens((_buf, _token) => { + encoders[Type.bytes.major] = wrapCompareTokens((_buf, _token, { encodeErrPrefix }) => { throw new Error(`${encodeErrPrefix} unsupported type: Uint8Array`) }) @@ -102,7 +100,7 @@ function makeJSONEncoders() { encoders[Type.tag.major] = wrapCompareTokens((_buf, _token) => { }) - encoders[Type.float.major] = wrapCompareTokens((buf, token) => { + encoders[Type.float.major] = wrapCompareTokens((buf, token, { encodeErrPrefix }) => { if (token.type.equals(Type.break)) { const recurs = inRecursive.pop() if (recurs) { @@ -260,6 +258,7 @@ function makeJSONEncoders() { // } // } +const encodeErrPrefix = 'JSON encode error:' /** * @param {(Token|Token[])[]} e1 * @param {(Token|Token[])[]} e2 @@ -284,7 +283,11 @@ function mapSorter (e1, e2) { throw new Error(`${encodeErrPrefix} unexpected duplicate map keys, this is not supported`) } -const defaultEncodeOptions = { addBreakTokens: true, mapSorter } +const defaultEncodeOptions = { + addBreakTokens: true, + encodeErrPrefix, + mapSorter +} /** * @param {any} data diff --git a/lib/json/json.js b/json/json.js similarity index 100% rename from lib/json/json.js rename to json/json.js diff --git a/lib/common.js b/lib/common.js deleted file mode 100644 index 701566c..0000000 --- a/lib/common.js +++ /dev/null @@ -1,27 +0,0 @@ -const decodeErrPrefix = 'CBOR decode error:' -const encodeErrPrefix = 'CBOR encode error:' - -const uintMinorPrefixBytes = [] -uintMinorPrefixBytes[23] = 1 -uintMinorPrefixBytes[24] = 2 -uintMinorPrefixBytes[25] = 3 -uintMinorPrefixBytes[26] = 5 -uintMinorPrefixBytes[27] = 9 - -/** - * @param {Uint8Array} data - * @param {number} pos - * @param {number} need - */ -function assertEnoughData (data, pos, need) { - if (data.length - pos < need) { - throw new Error(`${decodeErrPrefix} not enough data for type`) - } -} - -export { - decodeErrPrefix, - encodeErrPrefix, - uintMinorPrefixBytes, - assertEnoughData -} diff --git a/lib/jump.js b/lib/jump.js deleted file mode 100644 index e1b6a5b..0000000 --- a/lib/jump.js +++ /dev/null @@ -1,209 +0,0 @@ -import { Token, Type } from './token.js' -import * as uint from './0uint.js' -import * as negint from './1negint.js' -import * as bytes from './2bytes.js' -import * as string from './3string.js' -import * as array from './4array.js' -import * as map from './5map.js' -import * as tag from './6tag.js' -import * as float from './7float.js' -import { decodeErrPrefix } from './common.js' -import { fromArray } from './byte-utils.js' - -/** - * @typedef {import('../interface').DecodeOptions} DecodeOptions - */ - -/** - * @param {Uint8Array} data - * @param {number} pos - * @param {number} minor - */ -function invalidMinor (data, pos, minor) { - throw new Error(`${decodeErrPrefix} encountered invalid minor (${minor}) for major ${data[pos] >>> 5}`) -} - -/** - * @param {string} msg - * @returns {()=>any} - */ -function errorer (msg) { - return () => { throw new Error(`${decodeErrPrefix} ${msg}`) } -} - -/** @type {((data:Uint8Array, pos:number, minor:number, options?:DecodeOptions) => any)[]} */ -export const jump = [] - -// unsigned integer, 0x00..0x17 (0..23) -for (let i = 0; i <= 0x17; i++) { - jump[i] = invalidMinor // uint.decodeUintCompact, handled by quick[] -} -jump[0x18] = uint.decodeUint8 // unsigned integer, one-byte uint8_t follows -jump[0x19] = uint.decodeUint16 // unsigned integer, two-byte uint16_t follows -jump[0x1a] = uint.decodeUint32 // unsigned integer, four-byte uint32_t follows -jump[0x1b] = uint.decodeUint64 // unsigned integer, eight-byte uint64_t follows -jump[0x1c] = invalidMinor -jump[0x1d] = invalidMinor -jump[0x1e] = invalidMinor -jump[0x1f] = invalidMinor -// negative integer, -1-0x00..-1-0x17 (-1..-24) -for (let i = 0x20; i <= 0x37; i++) { - jump[i] = invalidMinor // negintDecode, handled by quick[] -} -jump[0x38] = negint.decodeNegint8 // negative integer, -1-n one-byte uint8_t for n follows -jump[0x39] = negint.decodeNegint16 // negative integer, -1-n two-byte uint16_t for n follows -jump[0x3a] = negint.decodeNegint32 // negative integer, -1-n four-byte uint32_t for follows -jump[0x3b] = negint.decodeNegint64 // negative integer, -1-n eight-byte uint64_t for follows -jump[0x3c] = invalidMinor -jump[0x3d] = invalidMinor -jump[0x3e] = invalidMinor -jump[0x3f] = invalidMinor -// byte string, 0x00..0x17 bytes follow -for (let i = 0x40; i <= 0x57; i++) { - jump[i] = bytes.decodeBytesCompact -} -jump[0x58] = bytes.decodeBytes8 // byte string, one-byte uint8_t for n, and then n bytes follow -jump[0x59] = bytes.decodeBytes16 // byte string, two-byte uint16_t for n, and then n bytes follow -jump[0x5a] = bytes.decodeBytes32 // byte string, four-byte uint32_t for n, and then n bytes follow -jump[0x5b] = bytes.decodeBytes64 // byte string, eight-byte uint64_t for n, and then n bytes follow -jump[0x5c] = invalidMinor -jump[0x5d] = invalidMinor -jump[0x5e] = invalidMinor -jump[0x5f] = errorer('indefinite length bytes/strings are not supported') // byte string, byte strings follow, terminated by "break" -// UTF-8 string 0x00..0x17 bytes follow -for (let i = 0x60; i <= 0x77; i++) { - jump[i] = string.decodeStringCompact -} -jump[0x78] = string.decodeString8 // UTF-8 string, one-byte uint8_t for n, and then n bytes follow -jump[0x79] = string.decodeString16 // UTF-8 string, two-byte uint16_t for n, and then n bytes follow -jump[0x7a] = string.decodeString32 // UTF-8 string, four-byte uint32_t for n, and then n bytes follow -jump[0x7b] = string.decodeString64 // UTF-8 string, eight-byte uint64_t for n, and then n bytes follow -jump[0x7c] = invalidMinor -jump[0x7d] = invalidMinor -jump[0x7e] = invalidMinor -jump[0x7f] = errorer('indefinite length bytes/strings are not supported') // UTF-8 strings follow, terminated by "break" -// array, 0x00..0x17 data items follow -for (let i = 0x80; i <= 0x97; i++) { - jump[i] = array.decodeArrayCompact -} -jump[0x98] = array.decodeArray8 // array, one-byte uint8_t for n, and then n data items follow -jump[0x99] = array.decodeArray16 // array, two-byte uint16_t for n, and then n data items follow -jump[0x9a] = array.decodeArray32 // array, four-byte uint32_t for n, and then n data items follow -jump[0x9b] = array.decodeArray64 // array, eight-byte uint64_t for n, and then n data items follow -jump[0x9c] = invalidMinor -jump[0x9d] = invalidMinor -jump[0x9e] = invalidMinor -jump[0x9f] = array.decodeArrayIndefinite // array, data items follow, terminated by "break" -// map, 0x00..0x17 pairs of data items follow -for (let i = 0xa0; i <= 0xb7; i++) { - jump[i] = map.decodeMapCompact -} -jump[0xb8] = map.decodeMap8 // map, one-byte uint8_t for n, and then n pairs of data items follow -jump[0xb9] = map.decodeMap16 // map, two-byte uint16_t for n, and then n pairs of data items follow -jump[0xba] = map.decodeMap32 // map, four-byte uint32_t for n, and then n pairs of data items follow -jump[0xbb] = map.decodeMap64 // map, eight-byte uint64_t for n, and then n pairs of data items follow -jump[0xbc] = invalidMinor -jump[0xbd] = invalidMinor -jump[0xbe] = invalidMinor -jump[0xbf] = map.decodeMapIndefinite // map, pairs of data items follow, terminated by "break" -// tags -for (let i = 0xc0; i <= 0xd7; i++) { - jump[i] = tag.decodeTagCompact -} -jump[0xd8] = tag.decodeTag8 -jump[0xd9] = tag.decodeTag16 -jump[0xda] = tag.decodeTag32 -jump[0xdb] = tag.decodeTag64 -jump[0xdc] = invalidMinor -jump[0xdd] = invalidMinor -jump[0xde] = invalidMinor -jump[0xdf] = invalidMinor -// 0xe0..0xf3 simple values, unsupported -for (let i = 0xe0; i <= 0xf3; i++) { - jump[i] = errorer('simple values are not supported') -} -jump[0xf4] = invalidMinor // false, handled by quick[] -jump[0xf5] = invalidMinor // true, handled by quick[] -jump[0xf6] = invalidMinor // null, handled by quick[] -jump[0xf7] = float.decodeUndefined // undefined -jump[0xf8] = errorer('simple values are not supported') // simple value, one byte follows, unsupported -jump[0xf9] = float.decodeFloat16 // half-precision float (two-byte IEEE 754) -jump[0xfa] = float.decodeFloat32 // single-precision float (four-byte IEEE 754) -jump[0xfb] = float.decodeFloat64 // double-precision float (eight-byte IEEE 754) -jump[0xfc] = invalidMinor -jump[0xfd] = invalidMinor -jump[0xfe] = invalidMinor -jump[0xff] = float.decodeBreak // "break" stop code - -/** @type {Token[]} */ -export const quick = [] -// ints <24 -for (let i = 0; i < 24; i++) { - quick[i] = new Token(Type.uint, i, 1) -} -// negints >= -24 -for (let i = -1; i >= -24; i--) { - quick[31 - i] = new Token(Type.negint, i, 1) -} -// empty bytes -quick[0x40] = new Token(Type.bytes, new Uint8Array(0), 1) -// empty string -quick[0x60] = new Token(Type.string, '', 1) -// empty list -quick[0x80] = new Token(Type.array, 0, 1) -// empty map -quick[0xa0] = new Token(Type.map, 0, 1) -// false -quick[0xf4] = new Token(Type.false, false, 1) -// true -quick[0xf5] = new Token(Type.true, true, 1) -// null -quick[0xf6] = new Token(Type.null, null, 1) - -/** - * @param {Token} token - * @returns {Uint8Array|undefined} - */ -export function quickEncodeToken (token) { - switch (token.type) { - case Type.false: - return fromArray([0xf4]) - case Type.true: - return fromArray([0xf5]) - case Type.null: - return fromArray([0xf6]) - case Type.bytes: - if (!token.value.length) { - return fromArray([0x40]) - } - return - case Type.string: - if (token.value === '') { - return fromArray([0x60]) - } - return - case Type.array: - if (token.value === 0) { - return fromArray([0x80]) - } - /* c8 ignore next 2 */ - // shouldn't be possible if this were called when there was only one token - return - case Type.map: - if (token.value === 0) { - return fromArray([0xa0]) - } - /* c8 ignore next 2 */ - // shouldn't be possible if this were called when there was only one token - return - case Type.uint: - if (token.value < 24) { - return fromArray([Number(token.value)]) - } - return - case Type.negint: - if (token.value >= -24) { - return fromArray([31 - Number(token.value)]) - } - } -} diff --git a/package.json b/package.json index 1943e2c..a4a43d3 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,13 @@ "name": "cborg", "version": "4.2.7", "description": "Fast CBOR with a focus on strictness", - "main": "cborg.js", + "main": "cborg/index.js", "type": "module", "bin": { - "cborg": "lib/bin.js" + "cborg": "cborg/bin.js" }, "scripts": { - "lint": "standard *.js lib/*.js test/*.js", + "lint": "standard *.js cborg/*.js json/*.js utils/*.js test/*.js", "build": "npm run build:types", "build:types": "tsc --build", "prepublishOnly": "npm run build", @@ -49,33 +49,37 @@ }, "exports": { ".": { - "import": "./cborg.js", - "types": "./types/cborg.d.ts" + "import": "./cborg/index.js", + "types": "./types/cborg/index.d.ts" }, "./length": { - "import": "./lib/length.js", - "types": "./types/lib/length.d.ts" + "import": "./cborg/length.js", + "types": "./types/cborg/length.d.ts" }, "./taglib": { "import": "./taglib.js", "types": "./types/taglib.d.ts" }, "./json": { - "import": "./lib/json/json.js", - "types": "./types/lib/json/json.d.ts" + "import": "./json/json.js", + "types": "./types/json/json.d.ts" }, "./interface": { "types": "./types/interface.d.ts" + }, + "./utils": { + "import": "./utils/index.js", + "types": "./types/utils/index.d.ts" } }, "types": "cborg.d.ts", "typesVersions": { "*": { "json": [ - "types/lib/json/json.d.ts" + "types/json/json.d.ts" ], "length": [ - "types/lib/length.d.ts" + "types/cborg/length.d.ts" ], "*": [ "types/*" diff --git a/taglib.js b/taglib.js index 790c83d..4833859 100644 --- a/taglib.js +++ b/taglib.js @@ -1,6 +1,6 @@ // never reference the file directly to ensure the // indepency of the json module -import { Token, Type } from 'cborg' +import { Token, Type } from 'cborg/utils' /* A collection of some standard CBOR tags. diff --git a/test/common.js b/test/common.js index d305b97..dd7872f 100644 --- a/test/common.js +++ b/test/common.js @@ -1,4 +1,4 @@ -import { Token, Type } from '../lib/token.js' +import { Token, Type } from 'cborg' export function dateDecoder (obj) { if (typeof obj !== 'string') { diff --git a/test/noop-bin-test.js b/test/noop-bin-test.js index c3d6375..d7b16a4 100644 --- a/test/noop-bin-test.js +++ b/test/noop-bin-test.js @@ -1,3 +1,3 @@ // file included so ipjs will compile ../lib/bin.js // this test file is not intended to be run or loaded -import bin from '../lib/bin.js' // eslint-disable-line +import bin from 'cborg' // eslint-disable-line diff --git a/test/test-0uint.js b/test/test-0uint.js index 9116df5..54989cb 100644 --- a/test/test-0uint.js +++ b/test/test-0uint.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { fromHex, toHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-1negint.js b/test/test-1negint.js index 45969fa..85862e7 100644 --- a/test/test-1negint.js +++ b/test/test-1negint.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { fromHex, toHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-2bytes.js b/test/test-2bytes.js index 521fd78..fd01fbd 100644 --- a/test/test-2bytes.js +++ b/test/test-2bytes.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { useBuffer, fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { useBuffer, fromHex, toHex } from 'cborg/utils' const { assert } = chai @@ -70,7 +70,7 @@ describe('bytes', () => { describe('decode', () => { for (const fixture of fixtures) { const data = fromHex(fixture.data) - it(`should decode ${fixture.type}=${fixture.label || fixture.expected}`, () => { + it(`should decode ${fixture.type}=${fixture.label || fixture.expected}=${fixture.data.length}`, () => { let actual = decode(data) assert.strictEqual( toHex(actual), diff --git a/test/test-3string.js b/test/test-3string.js index 8d347b2..b324f2b 100644 --- a/test/test-3string.js +++ b/test/test-3string.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { fromHex, toHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-4array.js b/test/test-4array.js index 8888a1e..962bf7f 100644 --- a/test/test-4array.js +++ b/test/test-4array.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { fromHex, toHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-5map.js b/test/test-5map.js index 147f028..c683eba 100644 --- a/test/test-5map.js +++ b/test/test-5map.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { fromHex, toHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-6tag.js b/test/test-6tag.js index 3905ec8..dcef744 100644 --- a/test/test-6tag.js +++ b/test/test-6tag.js @@ -2,9 +2,13 @@ import * as chai from 'chai' -import { Token, Type } from '../lib/token.js' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { + decode, + encode, + Token, + Type +} from 'cborg' +import { fromHex, toHex } from 'cborg/utils' import { dateDecoder, dateEncoder } from './common.js' const { assert } = chai diff --git a/test/test-7float.js b/test/test-7float.js index 070837e..7ef5603 100644 --- a/test/test-7float.js +++ b/test/test-7float.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { decode, encode } from 'cborg' +import { fromHex, toHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-bl.js b/test/test-bl.js index 27584ca..85be245 100644 --- a/test/test-bl.js +++ b/test/test-bl.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ import * as chai from 'chai' -import { Bl } from '../lib/bl.js' +import { Bl } from 'cborg/utils' const { assert } = chai diff --git a/test/test-cbor-vectors.js b/test/test-cbor-vectors.js index 0459d02..0a65e6a 100644 --- a/test/test-cbor-vectors.js +++ b/test/test-cbor-vectors.js @@ -2,9 +2,9 @@ import * as chai from 'chai' -import { decode, encode } from '../cborg.js' +import { decode, encode } from 'cborg' import * as taglib from 'cborg/taglib' -import { fromHex, toHex } from '../lib/byte-utils.js' +import { fromHex, toHex } from 'cborg/utils' // fixtures from https://github.com/cbor/test-vectors import { fixtures } from './appendix_a.js' diff --git a/test/test-decode-errors.js b/test/test-decode-errors.js index 4d38d9e..3cf488c 100644 --- a/test/test-decode-errors.js +++ b/test/test-decode-errors.js @@ -2,8 +2,8 @@ import * as chai from 'chai' -import { decode } from '../cborg.js' -import { fromHex } from '../lib/byte-utils.js' +import { decode } from 'cborg' +import { fromHex } from 'cborg/utils' const { assert } = chai diff --git a/test/test-fuzz.js b/test/test-fuzz.js index 8a51181..c19ff14 100644 --- a/test/test-fuzz.js +++ b/test/test-fuzz.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { garbage } from 'ipld-garbage' -import { decode, encode } from '../cborg.js' +import { decode, encode } from 'cborg' import * as chai from 'chai' const { assert } = chai diff --git a/test/test-json.js b/test/test-json.js index 7962457..068c9dc 100644 --- a/test/test-json.js +++ b/test/test-json.js @@ -154,39 +154,39 @@ describe('json basics', () => { }) it('should throw on bad types', () => { - assert.throws(() => encode(new Uint8Array([1, 2])), /CBOR encode error: unsupported type: Uint8Array/) - assert.throws(() => encode({ boop: new Uint8Array([1, 2]) }), /CBOR encode error: unsupported type: Uint8Array/) - assert.throws(() => encode(undefined), /CBOR encode error: unsupported type: undefined/) - assert.throws(() => encode(new Map([[1, 2], [2, 3]])), /CBOR encode error: non-string map keys are not supported/) - assert.throws(() => encode(new Map([[['foo', 'bar'], 2], [['bar', 'foo'], 3]])), /CBOR encode error: complex map keys are not supported/) + assert.throws(() => encode(new Uint8Array([1, 2])), /JSON encode error: unsupported type: Uint8Array/) + assert.throws(() => encode({ boop: new Uint8Array([1, 2]) }), /JSON encode error: unsupported type: Uint8Array/) + assert.throws(() => encode(undefined), /JSON encode error: unsupported type: undefined/) + assert.throws(() => encode(new Map([[1, 2], [2, 3]])), /JSON encode error: non-string map keys are not supported/) + assert.throws(() => encode(new Map([[['foo', 'bar'], 2], [['bar', 'foo'], 3]])), /JSON encode error: complex map keys are not supported/) }) it('should throw on bad decode failure modes', () => { - assert.throws(() => decode(toBytes('{"a":1 & "b":2}')), 'CBOR decode error: unexpected character at position 7, was expecting object delimiter but found \'&\'') - assert.throws(() => decode(toBytes('{"a":1,"b"!2}')), 'CBOR decode error: unexpected character at position 10, was expecting key/value delimiter \':\' but found \'!\'') - assert.throws(() => decode(toBytes('[1,2&3]')), 'CBOR decode error: unexpected character at position 4, was expecting array delimiter but found \'&\'') - assert.throws(() => decode(toBytes('{"a":!}')), 'CBOR decode error: unexpected character at position 5') - assert.throws(() => decode(toBytes('"abc')), 'CBOR decode error: unexpected end of string at position 4') - assert.throws(() => decode(toBytes('"ab\\xc"')), 'CBOR decode error: unexpected string escape character at position 5') - assert.throws(() => decode(toBytes('"ab\x1Ec"')), 'CBOR decode error: invalid control character at position 3') - assert.throws(() => decode(toBytes('"ab\\')), 'CBOR decode error: unexpected string termination at position 4') + assert.throws(() => decode(toBytes('{"a":1 & "b":2}')), 'JSON decode error: unexpected character at position 7, was expecting object delimiter but found \'&\'') + assert.throws(() => decode(toBytes('{"a":1,"b"!2}')), 'JSON decode error: unexpected character at position 10, was expecting key/value delimiter \':\' but found \'!\'') + assert.throws(() => decode(toBytes('[1,2&3]')), 'JSON decode error: unexpected character at position 4, was expecting array delimiter but found \'&\'') + assert.throws(() => decode(toBytes('{"a":!}')), 'JSON decode error: unexpected character at position 5') + assert.throws(() => decode(toBytes('"abc')), 'JSON decode error: unexpected end of string at position 4') + assert.throws(() => decode(toBytes('"ab\\xc"')), 'JSON decode error: unexpected string escape character at position 5') + assert.throws(() => decode(toBytes('"ab\x1Ec"')), 'JSON decode error: invalid control character at position 3') + assert.throws(() => decode(toBytes('"ab\\')), 'JSON decode error: unexpected string termination at position 4') // begining of a quoted string with 3-byte unicode sequence with the last one truncated - assert.throws(() => decode(toBytes('"☺').subarray(0, 3)), 'CBOR decode error: unexpected unicode sequence at position 1') - assert.throws(() => decode(toBytes('"\\uxyza"')), 'CBOR decode error: unexpected unicode escape character at position 3') - assert.throws(() => decode(toBytes('"\\u11"')), 'CBOR decode error: unexpected end of unicode escape sequence at position 3') - assert.throws(() => decode(toBytes('-boop')), 'CBOR decode error: unexpected token at position 1') - assert.throws(() => decode(toBytes('{"v":nope}')), 'CBOR decode error: unexpected token at position 7, expected to find \'null\'') - assert.throws(() => decode(toBytes('[n]')), 'CBOR decode error: unexpected end of input at position 1') - assert.throws(() => decode(toBytes('{"v":truu}')), 'CBOR decode error: unexpected token at position 9, expected to find \'true\'') - assert.throws(() => decode(toBytes('[tr]')), 'CBOR decode error: unexpected end of input at position 1') - assert.throws(() => decode(toBytes('{"v":flase}')), 'CBOR decode error: unexpected token at position 7, expected to find \'false\'') - assert.throws(() => decode(toBytes('[fa]')), 'CBOR decode error: unexpected end of input at position 1') - assert.throws(() => decode(toBytes('-0..1')), 'CBOR decode error: unexpected token at position 3') + assert.throws(() => decode(toBytes('"☺').subarray(0, 3)), 'JSON decode error: unexpected unicode sequence at position 1') + assert.throws(() => decode(toBytes('"\\uxyza"')), 'JSON decode error: unexpected unicode escape character at position 3') + assert.throws(() => decode(toBytes('"\\u11"')), 'JSON decode error: unexpected end of unicode escape sequence at position 3') + assert.throws(() => decode(toBytes('-boop')), 'JSON decode error: unexpected token at position 1') + assert.throws(() => decode(toBytes('{"v":nope}')), 'JSON decode error: unexpected token at position 7, expected to find \'null\'') + assert.throws(() => decode(toBytes('[n]')), 'JSON decode error: unexpected end of input at position 1') + assert.throws(() => decode(toBytes('{"v":truu}')), 'JSON decode error: unexpected token at position 9, expected to find \'true\'') + assert.throws(() => decode(toBytes('[tr]')), 'JSON decode error: unexpected end of input at position 1') + assert.throws(() => decode(toBytes('{"v":flase}')), 'JSON decode error: unexpected token at position 7, expected to find \'false\'') + assert.throws(() => decode(toBytes('[fa]')), 'JSON decode error: unexpected end of input at position 1') + assert.throws(() => decode(toBytes('-0..1')), 'JSON decode error: unexpected token at position 3') }) it('should throw when rejectDuplicateMapKeys enabled on duplicate keys', () => { assert.deepStrictEqual(decode(toBytes('{"foo":1,"foo":2}')), { foo: 2 }) - assert.throws(() => decode(toBytes('{"foo":1,"foo":2}'), { rejectDuplicateMapKeys: true }), /CBOR decode error: found repeat map key "foo"/) + assert.throws(() => decode(toBytes('{"foo":1,"foo":2}'), { rejectDuplicateMapKeys: true }), /JSON decode error: found repeat map key "foo"/) }) it('decodeFirst', () => { diff --git a/test/test-length.js b/test/test-length.js index 713883b..b94e75c 100644 --- a/test/test-length.js +++ b/test/test-length.js @@ -2,9 +2,9 @@ import * as chai from 'chai' import { garbage } from 'ipld-garbage' -import { uintBoundaries } from '../lib/0uint.js' -import { encode } from '../cborg.js' -import { encodedLength } from '../lib/length.js' +import { uintBoundaries } from 'cborg/utils' +import { encode } from 'cborg' +import { encodedLength } from 'cborg/length' import { dateEncoder } from './common.js' const { assert } = chai diff --git a/test/test-partial.js b/test/test-partial.js index a59344e..41897bf 100644 --- a/test/test-partial.js +++ b/test/test-partial.js @@ -2,8 +2,8 @@ import * as chai from 'chai' import { garbage } from 'ipld-garbage' -import { uintBoundaries } from '../lib/0uint.js' -import { encode, decodeFirst } from '../cborg.js' +import { uintBoundaries } from 'cborg/utils' +import { encode, decodeFirst } from 'cborg' import { dateDecoder, dateEncoder } from './common.js' const { assert } = chai diff --git a/tsconfig.json b/tsconfig.json index 74f4307..75e33b6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -28,7 +28,10 @@ "emitDeclarationOnly": true, "paths": { "cborg": [ - "cborg.js" + "cborg/index.js" + ], + "cborg/utils": [ + "./utils/index.js" ] } }, @@ -37,7 +40,7 @@ "example.js", "taglib.js", "lib/" - ], +, "utils/byte-utils.js", "utils/jump.js", "utils/0uint.js", "utils/1negint.js", "utils/2bytes.js", "utils/3string.js", "utils/4array.js", "utils/5map.js", "utils/6tag.js", "utils/7float.js", "json" ], "exclude": [ "node_modules" ], diff --git a/types/cborg.d.ts b/types/cborg.d.ts index 20ed762..0a49baa 100644 --- a/types/cborg.d.ts +++ b/types/cborg.d.ts @@ -23,6 +23,4 @@ import { encode } from './lib/encode.js'; import { Token } from './lib/token.js'; import { Type } from './lib/token.js'; export { decode, decodeFirst, Tokeniser as Tokenizer, tokensToObject, encode, Token, Type }; -export { encodeErrPrefix, decodeErrPrefix } from "./lib/common.js"; -export { asU8A, fromString, decodeCodePointsArray } from "./lib/byte-utils.js"; //# sourceMappingURL=cborg.d.ts.map \ No newline at end of file diff --git a/types/cborg.d.ts.map b/types/cborg.d.ts.map index 763b6dd..8815be0 100644 --- a/types/cborg.d.ts.map +++ b/types/cborg.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"cborg.d.ts","sourceRoot":"","sources":["../cborg.js"],"names":[],"mappings":";;;;yBAWa,OAAO,aAAa,EAAE,UAAU;;;;0BAEhC,OAAO,aAAa,EAAE,mBAAmB;;;;4BACzC,OAAO,aAAa,EAAE,aAAa;;;;4BACnC,OAAO,aAAa,EAAE,aAAa;uBAde,iBAAiB;4BAAjB,iBAAiB;0BAAjB,iBAAiB;+BAAjB,iBAAiB;uBADzD,iBAAiB;sBAEZ,gBAAgB;qBAAhB,gBAAgB"} \ No newline at end of file +{"version":3,"file":"cborg.d.ts","sourceRoot":"","sources":["../cborg.js"],"names":[],"mappings":";;;;yBAaa,OAAO,aAAa,EAAE,UAAU;;;;0BAEhC,OAAO,aAAa,EAAE,mBAAmB;;;;4BACzC,OAAO,aAAa,EAAE,aAAa;;;;4BACnC,OAAO,aAAa,EAAE,aAAa;uBAhBe,iBAAiB;4BAAjB,iBAAiB;0BAAjB,iBAAiB;+BAAjB,iBAAiB;uBADzD,iBAAiB;sBAEZ,gBAAgB;qBAAhB,gBAAgB"} \ No newline at end of file diff --git a/types/cborg/decode.d.ts b/types/cborg/decode.d.ts new file mode 100644 index 0000000..db59b08 --- /dev/null +++ b/types/cborg/decode.d.ts @@ -0,0 +1,43 @@ +export type Token = import("cborg/utils").Token; +export type DecodeOptions = import("../interface").DecodeOptions; +export type DecodeTokenizer = import("../interface").DecodeTokenizer; +export type DecodeFunction = import("../interface").DecodeFunction; +/** + * @implements {DecodeTokenizer} + */ +export class Tokeniser implements DecodeTokenizer { + /** + * @param {Uint8Array} data + * @param {DecodeOptions} options + */ + constructor(data: Uint8Array, options: DecodeOptions); + _pos: number; + data: Uint8Array; + options: import("../interface").DecodeOptions; + jump: import("../interface").DecodeFunction[]; + pos(): number; + done(): boolean; + next(): import("cborg/utils").Token; +} +/** + * @param {DecodeTokenizer} tokeniser + * @param {DecodeOptions} options + * @returns {any|BREAK|DONE} + */ +export function tokensToObject(tokeniser: DecodeTokenizer, options: DecodeOptions): any | typeof BREAK | typeof DONE; +/** + * @param {Uint8Array} data + * @param {DecodeOptions} [options] + * @returns {any} + */ +export function decode(data: Uint8Array, options?: DecodeOptions): any; +/** + * @param {Uint8Array} data + * @param {DecodeOptions} [options] + * @returns {[any, Uint8Array]} + */ +export function decodeFirst(data: Uint8Array, options?: DecodeOptions): [any, Uint8Array]; +declare const BREAK: unique symbol; +declare const DONE: unique symbol; +export {}; +//# sourceMappingURL=decode.d.ts.map \ No newline at end of file diff --git a/types/cborg/decode.d.ts.map b/types/cborg/decode.d.ts.map new file mode 100644 index 0000000..962bdc0 --- /dev/null +++ b/types/cborg/decode.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../cborg/decode.js"],"names":[],"mappings":"oBAGa,OAAO,aAAa,EAAE,KAAK;4BAC3B,OAAO,cAAc,EAAE,aAAa;8BACpC,OAAO,cAAc,EAAE,eAAe;6BACtC,OAAO,cAAc,EAAE,cAAc;AAWlD;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,WACV,aAAa,EAOvB;IAJC,aAAa;IACb,kCAAgB;IAChB,8CAAsB;IACtB,8CAAyC;IAG3C,cAEC;IAED,gBAEC;IAED,oCAkBC;CACF;AA6ED;;;;GAIG;AACH,0CAJW,eAAe,WACf,aAAa,GACX,GAAG,6BAAW,CAqC1B;AAwBD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CASf;AAlCD;;;;GAIG;AACH,kCAJW,UAAU,YACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAiB7B;AAxID,mCAAiC;AADjC,kCAA+B"} \ No newline at end of file diff --git a/types/cborg/encode.d.ts b/types/cborg/encode.d.ts new file mode 100644 index 0000000..3dfe4d1 --- /dev/null +++ b/types/cborg/encode.d.ts @@ -0,0 +1,51 @@ +/** @returns {TokenTypeEncoder[]} */ +export function makeCborEncoders(): TokenTypeEncoder[]; +export type EncodeOptions = import("../interface").EncodeOptions; +export type OptionalTypeEncoder = import("../interface").OptionalTypeEncoder; +export type Reference = import("../interface").Reference; +export type StrictTypeEncoder = import("../interface").StrictTypeEncoder; +export type TokenTypeEncoder = import("../interface").TokenTypeEncoder; +export type TokenOrNestedTokens = import("../interface").TokenOrNestedTokens; +/** + * @param {any} obj + * @param {EncodeOptions} options + * @param {Reference} [refStack] + * @returns {TokenOrNestedTokens} + */ +export function objectToTokens(obj: any, options: EncodeOptions, refStack?: Reference): TokenOrNestedTokens; +/** + * @param {any} data + * @param {EncodeOptions} [options] + * @returns {Uint8Array} + */ +export function encode(data: any, options?: EncodeOptions): Uint8Array; +/** + * @param {any} data + * @param {TokenTypeEncoder[]} encoders + * @param {EncodeOptions} options + * @returns {Uint8Array} + */ +export function encodeCustom(data: any, encoders: TokenTypeEncoder[], options: EncodeOptions): Uint8Array; +/** @implements {Reference} */ +export class Ref implements Reference { + /** + * @param {Reference|undefined} stack + * @param {object|any[]} obj + * @param {EncodeOptions} options + * @returns {Reference} + */ + static createCheck(stack: Reference | undefined, obj: object | any[], { encodeErrPrefix }: EncodeOptions): Reference; + /** + * @param {object|any[]} obj + * @param {Reference|undefined} parent + */ + constructor(obj: object | any[], parent: Reference | undefined); + obj: object | any[]; + parent: import("../interface").Reference | undefined; + /** + * @param {object|any[]} obj + * @returns {boolean} + */ + includes(obj: object | any[]): boolean; +} +//# sourceMappingURL=encode.d.ts.map \ No newline at end of file diff --git a/types/cborg/encode.d.ts.map b/types/cborg/encode.d.ts.map new file mode 100644 index 0000000..b8780ca --- /dev/null +++ b/types/cborg/encode.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../cborg/encode.js"],"names":[],"mappings":"AAiCA,oCAAoC;AACpC,oCADc,gBAAgB,EAAE,CAY/B;4BA7BY,OAAO,cAAc,EAAE,aAAa;kCACpC,OAAO,cAAc,EAAE,mBAAmB;wBAC1C,OAAO,cAAc,EAAE,SAAS;gCAChC,OAAO,cAAc,EAAE,iBAAiB;+BACxC,OAAO,cAAc,EAAE,gBAAgB;kCACvC,OAAO,cAAc,EAAE,mBAAmB;AAmQvD;;;;;GAKG;AACH,oCALW,GAAG,WACH,aAAa,aACb,SAAS,GACP,mBAAmB,CAgB/B;AA2JD;;;;GAIG;AACH,6BAJW,GAAG,YACH,aAAa,GACX,UAAU,CAKtB;AAvCD;;;;;GAKG;AACH,mCALW,GAAG,YACH,gBAAgB,EAAE,WAClB,aAAa,GACX,UAAU,CAyBtB;AAlZD,8BAA8B;AAC9B,4BADiB,SAAS;IA0BxB;;;;;OAKG;IACH,0BALW,SAAS,GAAC,SAAS,OACnB,MAAM,GAAC,GAAG,EAAE,uBACZ,aAAa,GACX,SAAS,CAOrB;IAnCD;;;OAGG;IACH,iBAHW,MAAM,GAAC,GAAG,EAAE,UACZ,SAAS,GAAC,SAAS,EAK7B;IAFC,oBAAc;IACd,qDAAoB;IAGtB;;;OAGG;IACH,cAHW,MAAM,GAAC,GAAG,EAAE,GACV,OAAO,CAWnB;CAcF"} \ No newline at end of file diff --git a/types/cborg/index.d.ts b/types/cborg/index.d.ts new file mode 100644 index 0000000..dbded4b --- /dev/null +++ b/types/cborg/index.d.ts @@ -0,0 +1,26 @@ +export { encodeCustom } from "./encode.js"; +/** + * There was originally just `TypeEncoder` so don't break types by renaming or not exporting + */ +export type TagDecoder = import("../interface").TagDecoder; +/** + * Export the types that were present in the original manual cborg.d.ts + */ +export type TypeEncoder = import("../interface").OptionalTypeEncoder; +/** + * Export the types that were present in the original manual cborg.d.ts + */ +export type DecodeOptions = import("../interface").DecodeOptions; +/** + * Export the types that were present in the original manual cborg.d.ts + */ +export type EncodeOptions = import("../interface").EncodeOptions; +import { decode } from './decode.js'; +import { decodeFirst } from './decode.js'; +import { Tokeniser } from './decode.js'; +import { tokensToObject } from './decode.js'; +import { encode } from './encode.js'; +import { Token } from 'cborg/utils'; +import { Type } from 'cborg/utils'; +export { decode, decodeFirst, Tokeniser as Tokenizer, tokensToObject, encode, Token, Type }; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/types/cborg/index.d.ts.map b/types/cborg/index.d.ts.map new file mode 100644 index 0000000..2695fff --- /dev/null +++ b/types/cborg/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../cborg/index.js"],"names":[],"mappings":";;;;yBAaa,OAAO,cAAc,EAAE,UAAU;;;;0BAEjC,OAAO,cAAc,EAAE,mBAAmB;;;;4BAC1C,OAAO,cAAc,EAAE,aAAa;;;;4BACpC,OAAO,cAAc,EAAE,aAAa;uBAhBc,aAAa;4BAAb,aAAa;0BAAb,aAAa;+BAAb,aAAa;uBADrD,aAAa;sBAER,aAAa;qBAAb,aAAa"} \ No newline at end of file diff --git a/types/cborg/is.d.ts b/types/cborg/is.d.ts new file mode 100644 index 0000000..30d30f9 --- /dev/null +++ b/types/cborg/is.d.ts @@ -0,0 +1,6 @@ +/** + * @param {any} value + * @returns {string} + */ +export function is(value: any): string; +//# sourceMappingURL=is.d.ts.map \ No newline at end of file diff --git a/types/cborg/is.d.ts.map b/types/cborg/is.d.ts.map new file mode 100644 index 0000000..005b74b --- /dev/null +++ b/types/cborg/is.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.d.ts","sourceRoot":"","sources":["../../cborg/is.js"],"names":[],"mappings":"AAiDA;;;GAGG;AACH,0BAHW,GAAG,GACD,MAAM,CAiClB"} \ No newline at end of file diff --git a/types/interface.d.ts b/types/interface.d.ts index 40b734f..0676841 100644 --- a/types/interface.d.ts +++ b/types/interface.d.ts @@ -1,5 +1,4 @@ -import { Token } from './lib/token'; -import { Bl } from './lib/bl'; +import type { Token, Bl } from 'cborg/utils'; export type TokenOrNestedTokens = Token | Token[] | TokenOrNestedTokens[]; export interface Reference { parent: Reference | undefined; @@ -9,10 +8,11 @@ export interface Reference { export type OptionalTypeEncoder = (data: any, typ: string, options: EncodeOptions, refStack?: Reference) => TokenOrNestedTokens | null; export type StrictTypeEncoder = (data: any, typ: string, options: EncodeOptions, refStack?: Reference) => TokenOrNestedTokens; export type TokenTypeEncoder = { - (buf: Bl, token: Token, options?: EncodeOptions): void; + (buf: Bl, token: Token, options: EncodeOptions): void; compareTokens(t1: Token, t2: Token): number; - encodedSize?(token: Token, options?: EncodeOptions): number; + encodedSize?(token: Token, options: EncodeOptions): number; }; +export type DecodeFunction = (data: Uint8Array, pos: number, minor: number, options: DecodeOptions) => any; export type MapSorter = (e1: (Token | Token[])[], e2: (Token | Token[])[]) => number; export type QuickEncodeToken = (token: Token) => Uint8Array | undefined; export interface DecodeTokenizer { @@ -34,6 +34,7 @@ export interface DecodeOptions { retainStringBytes?: boolean; tags?: TagDecoder[]; tokenizer?: DecodeTokenizer; + decodeErrPrefix: string; } export interface EncodeOptions { float64?: boolean; @@ -43,5 +44,6 @@ export interface EncodeOptions { typeEncoders?: { [typeName: string]: OptionalTypeEncoder; }; + encodeErrPrefix: string; } //# sourceMappingURL=interface.d.ts.map \ No newline at end of file diff --git a/types/interface.d.ts.map b/types/interface.d.ts.map index b975497..6c43462 100644 --- a/types/interface.d.ts.map +++ b/types/interface.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAE7B,MAAM,MAAM,mBAAmB,GAAG,KAAK,GAAG,KAAK,EAAE,GAAG,mBAAmB,EAAE,CAAA;AAEzE,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,SAAS,GAAG,SAAS,CAAA;IAC7B,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,CAAA;IACnB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,GAAG,OAAO,CAAA;CACvC;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,QAAQ,CAAC,EAAE,SAAS,KAAK,mBAAmB,GAAG,IAAI,CAAA;AAEtI,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,QAAQ,CAAC,EAAE,SAAS,KAAK,mBAAmB,CAAA;AAE7H,MAAM,MAAM,gBAAgB,GAAG;IAC7B,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,IAAI,CAAC;IACvD,aAAa,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,GAAG,MAAM,CAAC;IAE5C,WAAW,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;CAC7D,CAAA;AAED,MAAM,MAAM,SAAS,GAAG,CAAC,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,EAAE,CAAC,EAAE,KAAK,MAAM,CAAA;AAEpF,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,UAAU,GAAG,SAAS,CAAA;AAEvE,MAAM,WAAW,eAAe;IAC9B,IAAI,IAAI,OAAO,CAAC;IAChB,IAAI,IAAI,KAAK,CAAC;IACd,GAAG,IAAI,MAAM,CAAC;CACf;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAA;AAE5C,MAAM,WAAW,aAAa;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC;IACpB,SAAS,CAAC,EAAE,eAAe,CAAA;CAC5B;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,YAAY,CAAC,EAAE;QAAE,CAAC,QAAQ,EAAE,MAAM,GAAG,mBAAmB,CAAA;KAAE,CAAA;CAC3D"} \ No newline at end of file +{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,aAAa,CAAA;AAE5C,MAAM,MAAM,mBAAmB,GAAG,KAAK,GAAG,KAAK,EAAE,GAAG,mBAAmB,EAAE,CAAA;AAEzE,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,SAAS,GAAG,SAAS,CAAA;IAC7B,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,CAAA;IACnB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,GAAG,OAAO,CAAA;CACvC;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,QAAQ,CAAC,EAAE,SAAS,KAAK,mBAAmB,GAAG,IAAI,CAAA;AAEtI,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,QAAQ,CAAC,EAAE,SAAS,KAAK,mBAAmB,CAAA;AAE7H,MAAM,MAAM,gBAAgB,GAAG;IAC7B,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAAC;IACtD,aAAa,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,GAAG,MAAM,CAAC;IAE5C,WAAW,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,CAAC;CAC5D,CAAA;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,IAAI,EAAC,UAAU,EAAE,GAAG,EAAC,MAAM,EAAE,KAAK,EAAC,MAAM,EAAE,OAAO,EAAC,aAAa,KAAK,GAAG,CAAA;AAEtG,MAAM,MAAM,SAAS,GAAG,CAAC,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,EAAE,CAAC,EAAE,KAAK,MAAM,CAAA;AAEpF,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,UAAU,GAAG,SAAS,CAAA;AAEvE,MAAM,WAAW,eAAe;IAC9B,IAAI,IAAI,OAAO,CAAC;IAChB,IAAI,IAAI,KAAK,CAAC;IACd,GAAG,IAAI,MAAM,CAAC;CACf;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAA;AAE5C,MAAM,WAAW,aAAa;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC;IACpB,SAAS,CAAC,EAAE,eAAe,CAAA;IAC3B,eAAe,EAAE,MAAM,CAAA;CACxB;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,YAAY,CAAC,EAAE;QAAE,CAAC,QAAQ,EAAE,MAAM,GAAG,mBAAmB,CAAA;KAAE,CAAA;IAC1D,eAAe,EAAE,MAAM,CAAA;CACxB"} \ No newline at end of file diff --git a/types/json/decode.d.ts b/types/json/decode.d.ts new file mode 100644 index 0000000..dc36ef5 --- /dev/null +++ b/types/json/decode.d.ts @@ -0,0 +1,67 @@ +export type DecodeOptions = import("../interface").DecodeOptions; +export type DecodeTokenizer = import("../interface").DecodeTokenizer; +/** + * @param {Uint8Array} data + * @param {DecodeOptions} [options] + * @returns {any} + */ +export function decode(data: Uint8Array, options?: DecodeOptions): any; +/** + * @param {Uint8Array} data + * @param {DecodeOptions} [options] + * @returns {[any, Uint8Array]} + */ +export function decodeFirst(data: Uint8Array, options?: DecodeOptions): [any, Uint8Array]; +/** + * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface').DecodeTokenizer} DecodeTokenizer + */ +/** + * @implements {DecodeTokenizer} + */ +export class Tokenizer implements DecodeTokenizer { + /** + * @param {Uint8Array} data + * @param {DecodeOptions} options + */ + constructor(data: Uint8Array, options: DecodeOptions); + _pos: number; + data: Uint8Array; + options: import("../interface").DecodeOptions; + /** @type {string[]} */ + modeStack: string[]; + lastToken: string; + pos(): number; + /** + * @returns {boolean} + */ + done(): boolean; + /** + * @returns {number} + */ + ch(): number; + /** + * @returns {string} + */ + currentMode(): string; + skipWhitespace(): void; + /** + * @param {number[]} str + */ + expect(str: number[]): void; + parseNumber(): Token; + /** + * @returns {Token} + */ + parseString(): Token; + /** + * @returns {Token} + */ + parseValue(): Token; + /** + * @returns {Token} + */ + next(): Token; +} +import { Token } from 'cborg/utils'; +//# sourceMappingURL=decode.d.ts.map \ No newline at end of file diff --git a/types/json/decode.d.ts.map b/types/json/decode.d.ts.map new file mode 100644 index 0000000..937d02c --- /dev/null +++ b/types/json/decode.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../json/decode.js"],"names":[],"mappings":"4BAMa,OAAO,cAAc,EAAE,aAAa;8BACpC,OAAO,cAAc,EAAE,eAAe;AAgbnD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CAMf;AAED;;;;GAIG;AACH,kCAJW,UAAU,YACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAM7B;AAtcD;;;GAGG;AAEH;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,WACV,aAAa,EASvB;IANC,aAAa;IACb,kCAAgB;IAChB,8CAAsB;IACtB,uBAAuB;IACvB,WADW,MAAM,EAAE,CACO;IAC1B,kBAAmB;IAGrB,cAEC;IAED;;OAEG;IACH,QAFa,OAAO,CAInB;IAED;;OAEG;IACH,MAFa,MAAM,CAIlB;IAED;;OAEG;IACH,eAFa,MAAM,CAIlB;IAED,uBAMC;IAED;;OAEG;IACH,YAFW,MAAM,EAAE,QAWlB;IAED,qBA+DC;IAED;;OAEG;IACH,eAFa,KAAK,CAkLjB;IAED;;OAEG;IACH,cAFa,KAAK,CAuCjB;IAED;;OAEG;IACH,QAFa,KAAK,CAyEjB;CACF;sBAlbkD,aAAa"} \ No newline at end of file diff --git a/types/json/encode.d.ts b/types/json/encode.d.ts new file mode 100644 index 0000000..e2e2c3d --- /dev/null +++ b/types/json/encode.d.ts @@ -0,0 +1,11 @@ +export type EncodeOptions = import("../interface").EncodeOptions; +export type TokenTypeEncoder = import("../interface").TokenTypeEncoder; +export type Token = import("cborg/utils").Token; +export type Bl = import("cborg/utils").Bl; +/** + * @param {any} data + * @param {EncodeOptions} [options] + * @returns {Uint8Array} + */ +export function encode(data: any, options?: EncodeOptions): Uint8Array; +//# sourceMappingURL=encode.d.ts.map \ No newline at end of file diff --git a/types/json/encode.d.ts.map b/types/json/encode.d.ts.map new file mode 100644 index 0000000..985330d --- /dev/null +++ b/types/json/encode.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../json/encode.js"],"names":[],"mappings":"4BAMa,OAAO,cAAc,EAAE,aAAa;+BACpC,OAAO,cAAc,EAAE,gBAAgB;oBACvC,OAAO,aAAa,EAAE,KAAK;iBAC3B,OAAO,aAAa,EAAE,EAAE;AA0RrC;;;;GAIG;AACH,6BAJW,GAAG,YACH,aAAa,GACX,UAAU,CAKtB"} \ No newline at end of file diff --git a/types/json/json.d.ts b/types/json/json.d.ts new file mode 100644 index 0000000..ea57cfd --- /dev/null +++ b/types/json/json.d.ts @@ -0,0 +1,6 @@ +import { encode } from './encode.js'; +import { decode } from './decode.js'; +import { decodeFirst } from './decode.js'; +import { Tokenizer } from './decode.js'; +export { encode, decode, decodeFirst, Tokenizer }; +//# sourceMappingURL=json.d.ts.map \ No newline at end of file diff --git a/types/json/json.d.ts.map b/types/json/json.d.ts.map new file mode 100644 index 0000000..5414316 --- /dev/null +++ b/types/json/json.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../json/json.js"],"names":[],"mappings":"uBAAuB,aAAa;uBACW,aAAa;4BAAb,aAAa;0BAAb,aAAa"} \ No newline at end of file diff --git a/types/lib/common.d.ts b/types/lib/common.d.ts index 69e8342..7ada197 100644 --- a/types/lib/common.d.ts +++ b/types/lib/common.d.ts @@ -1,10 +1,9 @@ -export const decodeErrPrefix: "CBOR decode error:"; -export const encodeErrPrefix: "CBOR encode error:"; export const uintMinorPrefixBytes: any[]; /** * @param {Uint8Array} data * @param {number} pos * @param {number} need + * @param {string} decodeErrPrefix */ -export function assertEnoughData(data: Uint8Array, pos: number, need: number): void; +export function assertEnoughData(data: Uint8Array, pos: number, need: number, decodeErrPrefix: string): void; //# sourceMappingURL=common.d.ts.map \ No newline at end of file diff --git a/types/lib/common.d.ts.map b/types/lib/common.d.ts.map index 482cb5a..212450b 100644 --- a/types/lib/common.d.ts.map +++ b/types/lib/common.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../lib/common.js"],"names":[],"mappings":"AAAA,8BAAwB,oBAAoB,CAAA;AAC5C,8BAAwB,oBAAoB,CAAA;AAE5C,yCAA+B;AAO/B;;;;GAIG;AACH,uCAJW,UAAU,OACV,MAAM,QACN,MAAM,QAMhB"} \ No newline at end of file +{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../lib/common.js"],"names":[],"mappings":"AAGA,yCAA+B;AAO/B;;;;;GAKG;AACH,uCALW,UAAU,OACV,MAAM,QACN,MAAM,mBACN,MAAM,QAMhB"} \ No newline at end of file diff --git a/types/lib/decode.d.ts b/types/lib/decode.d.ts index 539912a..33e5871 100644 --- a/types/lib/decode.d.ts +++ b/types/lib/decode.d.ts @@ -1,6 +1,7 @@ export type Token = import("./token.js").Token; export type DecodeOptions = import("../interface").DecodeOptions; export type DecodeTokenizer = import("../interface").DecodeTokenizer; +export type DecodeFunction = import("../interface").DecodeFunction; /** * @implements {DecodeTokenizer} */ @@ -9,10 +10,11 @@ export class Tokeniser implements DecodeTokenizer { * @param {Uint8Array} data * @param {DecodeOptions} options */ - constructor(data: Uint8Array, options?: DecodeOptions); + constructor(data: Uint8Array, options: DecodeOptions); _pos: number; data: Uint8Array; options: import("../interface").DecodeOptions; + jump: import("../interface").DecodeFunction[]; pos(): number; done(): boolean; next(): import("./token.js").Token; @@ -31,10 +33,10 @@ export function tokensToObject(tokeniser: DecodeTokenizer, options: DecodeOption export function decode(data: Uint8Array, options?: DecodeOptions): any; /** * @param {Uint8Array} data - * @param {DecodeOptions} [options] + * @param {DecodeOptions} options * @returns {[any, Uint8Array]} */ -export function decodeFirst(data: Uint8Array, options?: DecodeOptions): [any, Uint8Array]; +export function decodeFirst(data: Uint8Array, options: DecodeOptions): [any, Uint8Array]; declare const BREAK: unique symbol; declare const DONE: unique symbol; export {}; diff --git a/types/lib/decode.d.ts.map b/types/lib/decode.d.ts.map index 785d40e..5164855 100644 --- a/types/lib/decode.d.ts.map +++ b/types/lib/decode.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../lib/decode.js"],"names":[],"mappings":"oBAKa,OAAO,YAAY,EAAE,KAAK;4BAC1B,OAAO,cAAc,EAAE,aAAa;8BACpC,OAAO,cAAc,EAAE,eAAe;AAUnD;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,YACV,aAAa,EAMvB;IAHC,aAAa;IACb,kCAAgB;IAChB,8CAAsB;IAGxB,cAEC;IAED,gBAEC;IAED,mCAgBC;CACF;AA6ED;;;;GAIG;AACH,0CAJW,eAAe,WACf,aAAa,GACX,GAAG,6BAAW,CAoC1B;AAuBD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CAQf;AAhCD;;;;GAIG;AACH,kCAJW,UAAU,YACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAgB7B;AAtID,mCAAiC;AADjC,kCAA+B"} \ No newline at end of file +{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../lib/decode.js"],"names":[],"mappings":"oBAIa,OAAO,YAAY,EAAE,KAAK;4BAC1B,OAAO,cAAc,EAAE,aAAa;8BACpC,OAAO,cAAc,EAAE,eAAe;6BACtC,OAAO,cAAc,EAAE,cAAc;AAUlD;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,WACV,aAAa,EAOvB;IAJC,aAAa;IACb,kCAAgB;IAChB,8CAAsB;IACtB,8CAAyC;IAG3C,cAEC;IAED,gBAEC;IAED,mCAgBC;CACF;AA6ED;;;;GAIG;AACH,0CAJW,eAAe,WACf,aAAa,GACX,GAAG,6BAAW,CAoC1B;AAuBD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CASf;AAjCD;;;;GAIG;AACH,kCAJW,UAAU,WACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAgB7B;AAtID,mCAAiC;AADjC,kCAA+B"} \ No newline at end of file diff --git a/types/lib/diagnostic.d.ts.map b/types/lib/diagnostic.d.ts.map index c7e2ecc..a379472 100644 --- a/types/lib/diagnostic.d.ts.map +++ b/types/lib/diagnostic.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"diagnostic.d.ts","sourceRoot":"","sources":["../../lib/diagnostic.js"],"names":[],"mappings":"AAQA;;;GAGG;AACH,wCAHW,UAAU,UACV,MAAM,oCA6HhB;AAED;;;;GAIG;AACH,gCAHW,MAAM,GACJ,UAAU,CAatB"} \ No newline at end of file +{"version":3,"file":"diagnostic.d.ts","sourceRoot":"","sources":["../../lib/diagnostic.js"],"names":[],"mappings":"AAOA;;;GAGG;AACH,wCAHW,UAAU,UACV,MAAM,oCAiIhB;AAED;;;;GAIG;AACH,gCAHW,MAAM,GACJ,UAAU,CAatB"} \ No newline at end of file diff --git a/types/lib/encode.d.ts b/types/lib/encode.d.ts index 42bd8ed..3dfe4d1 100644 --- a/types/lib/encode.d.ts +++ b/types/lib/encode.d.ts @@ -8,11 +8,11 @@ export type TokenTypeEncoder = import("../interface").TokenTypeEncoder; export type TokenOrNestedTokens = import("../interface").TokenOrNestedTokens; /** * @param {any} obj - * @param {EncodeOptions} [options] + * @param {EncodeOptions} options * @param {Reference} [refStack] * @returns {TokenOrNestedTokens} */ -export function objectToTokens(obj: any, options?: EncodeOptions, refStack?: Reference): TokenOrNestedTokens; +export function objectToTokens(obj: any, options: EncodeOptions, refStack?: Reference): TokenOrNestedTokens; /** * @param {any} data * @param {EncodeOptions} [options] @@ -31,9 +31,10 @@ export class Ref implements Reference { /** * @param {Reference|undefined} stack * @param {object|any[]} obj + * @param {EncodeOptions} options * @returns {Reference} */ - static createCheck(stack: Reference | undefined, obj: object | any[]): Reference; + static createCheck(stack: Reference | undefined, obj: object | any[], { encodeErrPrefix }: EncodeOptions): Reference; /** * @param {object|any[]} obj * @param {Reference|undefined} parent diff --git a/types/lib/encode.d.ts.map b/types/lib/encode.d.ts.map index 70e4a80..c8519ac 100644 --- a/types/lib/encode.d.ts.map +++ b/types/lib/encode.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../lib/encode.js"],"names":[],"mappings":"AAgCA,oCAAoC;AACpC,oCADc,gBAAgB,EAAE,CAY/B;4BA3BY,OAAO,cAAc,EAAE,aAAa;kCACpC,OAAO,cAAc,EAAE,mBAAmB;wBAC1C,OAAO,cAAc,EAAE,SAAS;gCAChC,OAAO,cAAc,EAAE,iBAAiB;+BACxC,OAAO,cAAc,EAAE,gBAAgB;kCACvC,OAAO,cAAc,EAAE,mBAAmB;AAgQvD;;;;;GAKG;AACH,oCALW,GAAG,YACH,aAAa,aACb,SAAS,GACP,mBAAmB,CAgB/B;AA2JD;;;;GAIG;AACH,6BAJW,GAAG,YACH,aAAa,GACX,UAAU,CAKtB;AAvCD;;;;;GAKG;AACH,mCALW,GAAG,YACH,gBAAgB,EAAE,WAClB,aAAa,GACX,UAAU,CAyBtB;AAjZD,8BAA8B;AAC9B,4BADiB,SAAS;IA0BxB;;;;OAIG;IACH,0BAJW,SAAS,GAAC,SAAS,OACnB,MAAM,GAAC,GAAG,EAAE,GACV,SAAS,CAOrB;IAlCD;;;OAGG;IACH,iBAHW,MAAM,GAAC,GAAG,EAAE,UACZ,SAAS,GAAC,SAAS,EAK7B;IAFC,oBAAc;IACd,qDAAoB;IAGtB;;;OAGG;IACH,cAHW,MAAM,GAAC,GAAG,EAAE,GACV,OAAO,CAWnB;CAaF"} \ No newline at end of file +{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../lib/encode.js"],"names":[],"mappings":"AAkCA,oCAAoC;AACpC,oCADc,gBAAgB,EAAE,CAY/B;4BA7BY,OAAO,cAAc,EAAE,aAAa;kCACpC,OAAO,cAAc,EAAE,mBAAmB;wBAC1C,OAAO,cAAc,EAAE,SAAS;gCAChC,OAAO,cAAc,EAAE,iBAAiB;+BACxC,OAAO,cAAc,EAAE,gBAAgB;kCACvC,OAAO,cAAc,EAAE,mBAAmB;AAmQvD;;;;;GAKG;AACH,oCALW,GAAG,WACH,aAAa,aACb,SAAS,GACP,mBAAmB,CAgB/B;AA2JD;;;;GAIG;AACH,6BAJW,GAAG,YACH,aAAa,GACX,UAAU,CAKtB;AAvCD;;;;;GAKG;AACH,mCALW,GAAG,YACH,gBAAgB,EAAE,WAClB,aAAa,GACX,UAAU,CAyBtB;AAlZD,8BAA8B;AAC9B,4BADiB,SAAS;IA0BxB;;;;;OAKG;IACH,0BALW,SAAS,GAAC,SAAS,OACnB,MAAM,GAAC,GAAG,EAAE,uBACZ,aAAa,GACX,SAAS,CAOrB;IAnCD;;;OAGG;IACH,iBAHW,MAAM,GAAC,GAAG,EAAE,UACZ,SAAS,GAAC,SAAS,EAK7B;IAFC,oBAAc;IACd,qDAAoB;IAGtB;;;OAGG;IACH,cAHW,MAAM,GAAC,GAAG,EAAE,GACV,OAAO,CAWnB;CAcF"} \ No newline at end of file diff --git a/types/lib/json/decode.d.ts b/types/lib/json/decode.d.ts index 98f188f..6bc8db0 100644 --- a/types/lib/json/decode.d.ts +++ b/types/lib/json/decode.d.ts @@ -1,5 +1,5 @@ -export type DecodeOptions = import("../../interface").DecodeOptions; -export type DecodeTokenizer = import("../../interface").DecodeTokenizer; +export type DecodeOptions = import("../interface").DecodeOptions; +export type DecodeTokenizer = import("../interface").DecodeTokenizer; /** * @param {Uint8Array} data * @param {DecodeOptions} [options] @@ -24,10 +24,10 @@ export class Tokenizer implements DecodeTokenizer { * @param {Uint8Array} data * @param {DecodeOptions} options */ - constructor(data: Uint8Array, options?: DecodeOptions); + constructor(data: Uint8Array, options: DecodeOptions); _pos: number; data: Uint8Array; - options: import("../../interface").DecodeOptions; + options: import("../interface").DecodeOptions; /** @type {string[]} */ modeStack: string[]; lastToken: string; diff --git a/types/lib/json/decode.d.ts.map b/types/lib/json/decode.d.ts.map index 742e28f..5e5429e 100644 --- a/types/lib/json/decode.d.ts.map +++ b/types/lib/json/decode.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../lib/json/decode.js"],"names":[],"mappings":"4BAQa,OAAO,iBAAiB,EAAE,aAAa;8BACvC,OAAO,iBAAiB,EAAE,eAAe;AAgbtD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CAKf;AAED;;;;GAIG;AACH,kCAJW,UAAU,YACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAK7B;AApcD;;;GAGG;AAEH;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,YACV,aAAa,EASvB;IANC,aAAa;IACb,kCAAgB;IAChB,iDAAsB;IACtB,uBAAuB;IACvB,WADW,MAAM,EAAE,CACO;IAC1B,kBAAmB;IAGrB,cAEC;IAED;;OAEG;IACH,QAFa,OAAO,CAInB;IAED;;OAEG;IACH,MAFa,MAAM,CAIlB;IAED;;OAEG;IACH,eAFa,MAAM,CAIlB;IAED,uBAMC;IAED;;OAEG;IACH,YAFW,MAAM,EAAE,QAWlB;IAED,qBA+DC;IAED;;OAEG;IACH,eAFa,KAAK,CAkLjB;IAED;;OAEG;IACH,cAFa,KAAK,CAuCjB;IAED;;OAEG;IACH,QAFa,KAAK,CAyEjB;CACF;sBApb2B,OAAO"} \ No newline at end of file +{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../lib/json/decode.js"],"names":[],"mappings":"4BAOa,OAAO,iBAAiB,EAAE,aAAa;8BACvC,OAAO,iBAAiB,EAAE,eAAe;AAgbtD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CAMf;AAED;;;;GAIG;AACH,kCAJW,UAAU,YACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAM7B;AAtcD;;;GAGG;AAEH;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,WACV,aAAa,EASvB;IANC,aAAa;IACb,kCAAgB;IAChB,iDAAsB;IACtB,uBAAuB;IACvB,WADW,MAAM,EAAE,CACO;IAC1B,kBAAmB;IAGrB,cAEC;IAED;;OAEG;IACH,QAFa,OAAO,CAInB;IAED;;OAEG;IACH,MAFa,MAAM,CAIlB;IAED;;OAEG;IACH,eAFa,MAAM,CAIlB;IAED,uBAMC;IAED;;OAEG;IACH,YAFW,MAAM,EAAE,QAWlB;IAED,qBA+DC;IAED;;OAEG;IACH,eAFa,KAAK,CAkLjB;IAED;;OAEG;IACH,cAFa,KAAK,CAuCjB;IAED;;OAEG;IACH,QAFa,KAAK,CAyEjB;CACF;sBAnb2B,OAAO"} \ No newline at end of file diff --git a/types/lib/json/encode.d.ts b/types/lib/json/encode.d.ts index c60df9e..07b9299 100644 --- a/types/lib/json/encode.d.ts +++ b/types/lib/json/encode.d.ts @@ -1,7 +1,7 @@ -export type EncodeOptions = import("../../interface").EncodeOptions; -export type TokenTypeEncoder = import("../../interface").TokenTypeEncoder; -export type Token = import("../token").Token; -export type Bl = import("../bl").Bl; +export type EncodeOptions = import("../interface").EncodeOptions; +export type TokenTypeEncoder = import("../interface").TokenTypeEncoder; +export type Token = import("../lib/token").Token; +export type Bl = import("../lib/bl").Bl; /** * @param {any} data * @param {EncodeOptions} [options] diff --git a/types/lib/json/encode.d.ts.map b/types/lib/json/encode.d.ts.map index d212184..5293cb1 100644 --- a/types/lib/json/encode.d.ts.map +++ b/types/lib/json/encode.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../../lib/json/encode.js"],"names":[],"mappings":"4BAQa,OAAO,iBAAiB,EAAE,aAAa;+BACvC,OAAO,iBAAiB,EAAE,gBAAgB;oBAC1C,OAAO,UAAU,EAAE,KAAK;iBACxB,OAAO,OAAO,EAAE,EAAE;AAqR/B;;;;GAIG;AACH,6BAJW,GAAG,YACH,aAAa,GACX,UAAU,CAKtB"} \ No newline at end of file +{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../../lib/json/encode.js"],"names":[],"mappings":"4BAOa,OAAO,iBAAiB,EAAE,aAAa;+BACvC,OAAO,iBAAiB,EAAE,gBAAgB;oBAC1C,OAAO,UAAU,EAAE,KAAK;iBACxB,OAAO,OAAO,EAAE,EAAE;AA0R/B;;;;GAIG;AACH,6BAJW,GAAG,YACH,aAAa,GACX,UAAU,CAKtB"} \ No newline at end of file diff --git a/types/lib/length.d.ts.map b/types/lib/length.d.ts.map index 117466e..b1828f3 100644 --- a/types/lib/length.d.ts.map +++ b/types/lib/length.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"length.d.ts","sourceRoot":"","sources":["../../lib/length.js"],"names":[],"mappings":"AAiBA;;;;;;;;;;GAUG;AACH,oCAJW,GAAG,YACH,aAAa,GACX,MAAM,CAOlB;AAED;;;;;;;;;GASG;AACH,uCAJW,mBAAmB,aACnB,gBAAgB,EAAE,YAClB,aAAa,UAiBvB;4BAxDY,OAAO,cAAc,EAAE,aAAa;+BACpC,OAAO,cAAc,EAAE,gBAAgB;kCACvC,OAAO,cAAc,EAAE,mBAAmB"} \ No newline at end of file +{"version":3,"file":"length.d.ts","sourceRoot":"","sources":["../../lib/length.js"],"names":[],"mappings":"AAkBA;;;;;;;;;;GAUG;AACH,oCAJW,GAAG,YACH,aAAa,GACX,MAAM,CAOlB;AAED;;;;;;;;;GASG;AACH,uCAJW,mBAAmB,aACnB,gBAAgB,EAAE,YAClB,aAAa,UAiBvB;4BAzDY,OAAO,cAAc,EAAE,aAAa;+BACpC,OAAO,cAAc,EAAE,gBAAgB;kCACvC,OAAO,cAAc,EAAE,mBAAmB"} \ No newline at end of file diff --git a/types/taglib.d.ts b/types/taglib.d.ts index cd6a86c..3883a62 100644 --- a/types/taglib.d.ts +++ b/types/taglib.d.ts @@ -14,5 +14,5 @@ export function bigIntEncoder(obj: bigint): Token[] | null; * @returns {bigint} */ export function bigNegIntDecoder(bytes: Uint8Array): bigint; -import { Token } from './cborg.js'; +import { Token } from 'cborg/utils'; //# sourceMappingURL=taglib.d.ts.map \ No newline at end of file diff --git a/types/taglib.d.ts.map b/types/taglib.d.ts.map index fa3a92a..2e4b9c5 100644 --- a/types/taglib.d.ts.map +++ b/types/taglib.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"taglib.d.ts","sourceRoot":"","sources":["../taglib.js"],"names":[],"mappings":"AAqBA;;;GAGG;AACH,qCAHW,UAAU,GACR,MAAM,CASlB;AAmBD;;;GAGG;AACH,mCAHW,MAAM,GACJ,KAAK,EAAE,GAAC,IAAI,CAUxB;AAED;;;;GAIG;AACH,wCAHW,UAAU,GACR,MAAM,CAIlB;sBAxE2B,YAAY"} \ No newline at end of file +{"version":3,"file":"taglib.d.ts","sourceRoot":"","sources":["../taglib.js"],"names":[],"mappings":"AAuBA;;;GAGG;AACH,qCAHW,UAAU,GACR,MAAM,CASlB;AAmBD;;;GAGG;AACH,mCAHW,MAAM,GACJ,KAAK,EAAE,GAAC,IAAI,CAUxB;AAED;;;;GAIG;AACH,wCAHW,UAAU,GACR,MAAM,CAIlB;sBAxE2B,aAAa"} \ No newline at end of file diff --git a/types/utils/0uint.d.ts b/types/utils/0uint.d.ts new file mode 100644 index 0000000..e71b6d9 --- /dev/null +++ b/types/utils/0uint.d.ts @@ -0,0 +1,102 @@ +/** + * @typedef {import('./bl.js').Bl} Bl + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions + */ +/** + * @param {Uint8Array} data + * @param {number} offset + * @param {DecodeOptions} options + * @returns {number} + */ +export function readUint8(data: Uint8Array, offset: number, options: DecodeOptions): number; +/** + * @param {Uint8Array} data + * @param {number} offset + * @param {DecodeOptions} options + * @returns {number} + */ +export function readUint16(data: Uint8Array, offset: number, options: DecodeOptions): number; +/** + * @param {Uint8Array} data + * @param {number} offset + * @param {DecodeOptions} options + * @returns {number} + */ +export function readUint32(data: Uint8Array, offset: number, options: DecodeOptions): number; +/** + * @param {Uint8Array} data + * @param {number} offset + * @param {DecodeOptions} options + * @returns {number|bigint} + */ +export function readUint64(data: Uint8Array, offset: number, options: DecodeOptions): number | bigint; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeUint8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeUint16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeUint32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeUint64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options + */ +export function encodeUint(buf: Bl, token: Token, { encodeErrPrefix }: import("../interface.js").EncodeOptions): void; +export namespace encodeUint { + /** + * @param {Token} token + * @returns {number} + */ + function encodedSize(token: Token): number; + /** + * @param {Token} tok1 + * @param {Token} tok2 + * @returns {number} + */ + function compareTokens(tok1: Token, tok2: Token): number; +} +/** + * @param {Bl} buf + * @param {number} major + * @param {number|bigint} uint + * @param {string} decodeErrPrefix + */ +export function encodeUintValue(buf: Bl, major: number, uint: number | bigint, decodeErrPrefix: string): void; +export namespace encodeUintValue { + /** + * @param {number} uint + * @returns {number} + */ + function encodedSize(uint: number): number; +} +export const uintBoundaries: (number | bigint)[]; +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=0uint.d.ts.map \ No newline at end of file diff --git a/types/utils/0uint.d.ts.map b/types/utils/0uint.d.ts.map new file mode 100644 index 0000000..4c83806 --- /dev/null +++ b/types/utils/0uint.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"0uint.d.ts","sourceRoot":"","sources":["../../utils/0uint.js"],"names":[],"mappings":"AAOA;;;GAGG;AAEH;;;;;GAKG;AACH,gCALW,UAAU,UACV,MAAM,WACN,aAAa,GACX,MAAM,CASlB;AAED;;;;;GAKG;AACH,iCALW,UAAU,UACV,MAAM,WACN,aAAa,GACX,MAAM,CASlB;AAED;;;;;GAKG;AACH,iCALW,UAAU,UACV,MAAM,WACN,aAAa,GACX,MAAM,CASlB;AAED;;;;;GAKG;AACH,iCALW,UAAU,UACV,MAAM,WACN,aAAa,GACX,MAAM,GAAC,MAAM,CAkBzB;AASD;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,mCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,mCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,mCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;GAIG;AACH,gCAJW,EAAE,SACF,KAAK,uBACL,OAAO,iBAAiB,EAAE,aAAa,QAIjD;;IAsDD;;;OAGG;IACH,4BAHW,KAAK,GACH,MAAM,CAIlB;IAsBD;;;;OAIG;IACH,6BAJW,KAAK,QACL,KAAK,GACH,MAAM,CAIlB;;AAvFD;;;;;GAKG;AACH,qCALW,EAAE,SACF,MAAM,QACN,MAAM,GAAC,MAAM,mBACb,MAAM,QA8ChB;;IAUD;;;OAGG;IACH,2BAHW,MAAM,GACJ,MAAM,CAgBlB;;AAtND,iDAA0F;iBAG7E,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBAPxB,YAAY"} \ No newline at end of file diff --git a/types/utils/1negint.d.ts b/types/utils/1negint.d.ts new file mode 100644 index 0000000..7bf6a12 --- /dev/null +++ b/types/utils/1negint.d.ts @@ -0,0 +1,59 @@ +/** + * @typedef {import('./bl.js').Bl} Bl + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions + */ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeNegint8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeNegint16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeNegint32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeNegint64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options + */ +export function encodeNegint(buf: Bl, token: Token, { encodeErrPrefix }: import("../interface.js").EncodeOptions): void; +export namespace encodeNegint { + /** + * @param {Token} token + * @returns {number} + */ + function encodedSize(token: Token): number; + /** + * @param {Token} tok1 + * @param {Token} tok2 + * @returns {number} + */ + function compareTokens(tok1: Token, tok2: Token): number; +} +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=1negint.d.ts.map \ No newline at end of file diff --git a/types/utils/1negint.d.ts.map b/types/utils/1negint.d.ts.map new file mode 100644 index 0000000..797640d --- /dev/null +++ b/types/utils/1negint.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"1negint.d.ts","sourceRoot":"","sources":["../../utils/1negint.js"],"names":[],"mappings":"AAKA;;;GAGG;AAEH;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAKD;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAcjB;AAED;;;;GAIG;AACH,kCAJW,EAAE,SACF,KAAK,uBACL,OAAO,iBAAiB,EAAE,aAAa,QAMjD;;IAED;;;OAGG;IACH,4BAHW,KAAK,GACH,MAAM,CAoBlB;IAED;;;;OAIG;IACH,6BAJW,KAAK,QACL,KAAK,GACH,MAAM,CAKlB;;iBAxGY,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBALxB,YAAY"} \ No newline at end of file diff --git a/types/utils/2bytes.d.ts b/types/utils/2bytes.d.ts new file mode 100644 index 0000000..15bf3a3 --- /dev/null +++ b/types/utils/2bytes.d.ts @@ -0,0 +1,69 @@ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeBytesCompact(data: Uint8Array, pos: number, minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeBytes8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeBytes16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeBytes32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeBytes64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options + */ +export function encodeBytes(buf: Bl, token: Token, { encodeErrPrefix }: import("../interface.js").EncodeOptions): void; +export namespace encodeBytes { + /** + * @param {Token} token + * @returns {number} + */ + function encodedSize(token: Token): number; + /** + * @param {Token} tok1 + * @param {Token} tok2 + * @returns {number} + */ + function compareTokens(tok1: Token, tok2: Token): number; +} +/** + * @param {Uint8Array} b1 + * @param {Uint8Array} b2 + * @returns {number} + */ +export function compareBytes(b1: Uint8Array, b2: Uint8Array): number; +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=2bytes.d.ts.map \ No newline at end of file diff --git a/types/utils/2bytes.d.ts.map b/types/utils/2bytes.d.ts.map new file mode 100644 index 0000000..0c05216 --- /dev/null +++ b/types/utils/2bytes.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"2bytes.d.ts","sourceRoot":"","sources":["../../utils/2bytes.js"],"names":[],"mappings":"AAwBA;;;;;;GAMG;AACH,yCANW,UAAU,OACV,MAAM,SACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,mCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAGD;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAQjB;AAgBD;;;;GAIG;AACH,iCAJW,EAAE,SACF,KAAK,uBACL,OAAO,iBAAiB,EAAE,aAAa,QAMjD;;IAED;;;OAGG;IACH,4BAHW,KAAK,GACH,MAAM,CAKlB;IAED;;;;OAIG;IACH,6BAJW,KAAK,QACL,KAAK,GACH,MAAM,CAIlB;;AAED;;;;GAIG;AACH,iCAJW,UAAU,MACV,UAAU,GACR,MAAM,CAIlB;iBAhIY,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBAPxB,YAAY"} \ No newline at end of file diff --git a/types/utils/3string.d.ts b/types/utils/3string.d.ts new file mode 100644 index 0000000..4eda431 --- /dev/null +++ b/types/utils/3string.d.ts @@ -0,0 +1,46 @@ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeStringCompact(data: Uint8Array, pos: number, minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeString8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeString16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeString32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeString64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +export const encodeString: typeof encodeBytes; +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +import { encodeBytes } from './2bytes.js'; +//# sourceMappingURL=3string.d.ts.map \ No newline at end of file diff --git a/types/utils/3string.d.ts.map b/types/utils/3string.d.ts.map new file mode 100644 index 0000000..a18328b --- /dev/null +++ b/types/utils/3string.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"3string.d.ts","sourceRoot":"","sources":["../../utils/3string.js"],"names":[],"mappings":"AA6BA;;;;;;GAMG;AACH,0CANW,UAAU,OACV,MAAM,SACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAGD;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAQjB;AAED,8CAAuC;iBAlF1B,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBARxB,YAAY;4BAGZ,aAAa"} \ No newline at end of file diff --git a/types/utils/4array.d.ts b/types/utils/4array.d.ts new file mode 100644 index 0000000..c9e5d5b --- /dev/null +++ b/types/utils/4array.d.ts @@ -0,0 +1,66 @@ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} minor + * @param {DecodeOptions} _options + * @returns {Token} + */ +export function decodeArrayCompact(data: Uint8Array, pos: number, minor: number, _options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeArray8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeArray16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeArray32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeArray64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeArrayIndefinite(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options + */ +export function encodeArray(buf: Bl, token: Token, { encodeErrPrefix }: import("../interface.js").EncodeOptions): void; +export namespace encodeArray { + let compareTokens: (tok1: Token, tok2: Token) => number; + /** + * @param {Token} token + * @returns {number} + */ + function encodedSize(token: Token): number; +} +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=4array.d.ts.map \ No newline at end of file diff --git a/types/utils/4array.d.ts.map b/types/utils/4array.d.ts.map new file mode 100644 index 0000000..45a51e0 --- /dev/null +++ b/types/utils/4array.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"4array.d.ts","sourceRoot":"","sources":["../../utils/4array.js"],"names":[],"mappings":"AAoBA;;;;;;GAMG;AACH,yCANW,UAAU,OACV,MAAM,SACN,MAAM,YACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,mCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAGD;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAQjB;AAED;;;;;;GAMG;AACH,4CANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAOjB;AAED;;;;GAIG;AACH,iCAJW,EAAE,SACF,KAAK,uBACL,OAAO,iBAAiB,EAAE,aAAa,QAIjD;;;IAMD;;;OAGG;IACH,4BAHW,KAAK,GACH,MAAM,CAIlB;;iBA5GY,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBANxB,YAAY"} \ No newline at end of file diff --git a/types/utils/5map.d.ts b/types/utils/5map.d.ts new file mode 100644 index 0000000..ec31612 --- /dev/null +++ b/types/utils/5map.d.ts @@ -0,0 +1,66 @@ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} minor + * @param {DecodeOptions} _options + * @returns {Token} + */ +export function decodeMapCompact(data: Uint8Array, pos: number, minor: number, _options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeMap8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeMap16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeMap32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeMap64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeMapIndefinite(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options + */ +export function encodeMap(buf: Bl, token: Token, { encodeErrPrefix }: import("../interface.js").EncodeOptions): void; +export namespace encodeMap { + let compareTokens: (tok1: Token, tok2: Token) => number; + /** + * @param {Token} token + * @returns {number} + */ + function encodedSize(token: Token): number; +} +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=5map.d.ts.map \ No newline at end of file diff --git a/types/utils/5map.d.ts.map b/types/utils/5map.d.ts.map new file mode 100644 index 0000000..2092f66 --- /dev/null +++ b/types/utils/5map.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"5map.d.ts","sourceRoot":"","sources":["../../utils/5map.js"],"names":[],"mappings":"AAmBA;;;;;;GAMG;AACH,uCANW,UAAU,OACV,MAAM,SACN,MAAM,YACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,iCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAGD;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAQjB;AAED;;;;;;GAMG;AACH,0CANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAOjB;AAED;;;;GAIG;AACH,+BAJW,EAAE,SACF,KAAK,uBACL,OAAO,iBAAiB,EAAE,aAAa,QAIjD;;;IAMD;;;OAGG;IACH,4BAHW,KAAK,GACH,MAAM,CAIlB;;iBA5GY,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBALxB,YAAY"} \ No newline at end of file diff --git a/types/utils/6tag.d.ts b/types/utils/6tag.d.ts new file mode 100644 index 0000000..3638563 --- /dev/null +++ b/types/utils/6tag.d.ts @@ -0,0 +1,62 @@ +/** + * @typedef {import('./bl.js').Bl} Bl + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions + */ +/** + * @param {Uint8Array} _data + * @param {number} _pos + * @param {number} minor + * @param {DecodeOptions} _options + * @returns {Token} + */ +export function decodeTagCompact(_data: Uint8Array, _pos: number, minor: number, _options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeTag8(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeTag16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeTag32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeTag64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options + */ +export function encodeTag(buf: Bl, token: Token, { encodeErrPrefix }: import("../interface.js").EncodeOptions): void; +export namespace encodeTag { + let compareTokens: (tok1: Token, tok2: Token) => number; + /** + * @param {Token} token + * @returns {number} + */ + function encodedSize(token: Token): number; +} +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=6tag.d.ts.map \ No newline at end of file diff --git a/types/utils/6tag.d.ts.map b/types/utils/6tag.d.ts.map new file mode 100644 index 0000000..174a3c5 --- /dev/null +++ b/types/utils/6tag.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"6tag.d.ts","sourceRoot":"","sources":["../../utils/6tag.js"],"names":[],"mappings":"AAGA;;;GAGG;AAEH;;;;;;GAMG;AACH,wCANW,UAAU,QACV,MAAM,SACN,MAAM,YACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,iCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,kCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;GAIG;AACH,+BAJW,EAAE,SACF,KAAK,uBACL,OAAO,iBAAiB,EAAE,aAAa,QAIjD;;;IAID;;;OAGG;IACH,4BAHW,KAAK,GACH,MAAM,CAIlB;;iBA5EY,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;sBALxB,YAAY"} \ No newline at end of file diff --git a/types/utils/7float.d.ts b/types/utils/7float.d.ts new file mode 100644 index 0000000..6e6a270 --- /dev/null +++ b/types/utils/7float.d.ts @@ -0,0 +1,60 @@ +/** + * @param {Uint8Array} _data + * @param {number} _pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeUndefined(_data: Uint8Array, _pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} _data + * @param {number} _pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeBreak(_data: Uint8Array, _pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeFloat16(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeFloat32(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} _minor + * @param {DecodeOptions} options + * @returns {Token} + */ +export function decodeFloat64(data: Uint8Array, pos: number, _minor: number, options: DecodeOptions): Token; +/** + * @param {Bl} buf + * @param {Token} token + * @param {EncodeOptions} options + */ +export function encodeFloat(buf: Bl, token: Token, options: EncodeOptions): void; +export namespace encodeFloat { + /** + * @param {Token} token + * @param {EncodeOptions} options + * @returns {number} + */ + function encodedSize(token: Token, options: EncodeOptions): number; + let compareTokens: (tok1: Token, tok2: Token) => number; +} +export type Bl = import("./bl.js").Bl; +export type DecodeOptions = import("../interface.js").DecodeOptions; +export type EncodeOptions = import("../interface.js").EncodeOptions; +import { Token } from './token.js'; +//# sourceMappingURL=7float.d.ts.map \ No newline at end of file diff --git a/types/utils/7float.d.ts.map b/types/utils/7float.d.ts.map new file mode 100644 index 0000000..115616f --- /dev/null +++ b/types/utils/7float.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"7float.d.ts","sourceRoot":"","sources":["../../utils/7float.js"],"names":[],"mappings":"AAiBA;;;;;;GAMG;AACH,uCANW,UAAU,QACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CASjB;AAED;;;;;;GAMG;AACH,mCANW,UAAU,QACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAOjB;AAoBD;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;GAIG;AACH,iCAJW,EAAE,SACF,KAAK,WACL,aAAa,QAwCvB;;IAED;;;;OAIG;IACH,4BAJW,KAAK,WACL,aAAa,GACX,MAAM,CAsBlB;;;iBAjKY,OAAO,SAAS,EAAE,EAAE;4BACpB,OAAO,iBAAiB,EAAE,aAAa;4BACvC,OAAO,iBAAiB,EAAE,aAAa;sBANxB,YAAY"} \ No newline at end of file diff --git a/types/utils/bl.d.ts b/types/utils/bl.d.ts new file mode 100644 index 0000000..010272b --- /dev/null +++ b/types/utils/bl.d.ts @@ -0,0 +1,26 @@ +export class Bl { + /** + * @param {number} [chunkSize] + */ + constructor(chunkSize?: number); + chunkSize: number; + /** @type {number} */ + cursor: number; + /** @type {number} */ + maxCursor: number; + /** @type {(Uint8Array|number[])[]} */ + chunks: (Uint8Array | number[])[]; + /** @type {Uint8Array|number[]|null} */ + _initReuseChunk: Uint8Array | number[] | null; + reset(): void; + /** + * @param {Uint8Array|number[]} bytes + */ + push(bytes: Uint8Array | number[]): void; + /** + * @param {boolean} [reset] + * @returns {Uint8Array} + */ + toBytes(reset?: boolean): Uint8Array; +} +//# sourceMappingURL=bl.d.ts.map \ No newline at end of file diff --git a/types/utils/bl.d.ts.map b/types/utils/bl.d.ts.map new file mode 100644 index 0000000..295fda0 --- /dev/null +++ b/types/utils/bl.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"bl.d.ts","sourceRoot":"","sources":["../../utils/bl.js"],"names":[],"mappings":"AA0BA;IACE;;OAEG;IACH,wBAFW,MAAM,EAahB;IAVC,kBAA0B;IAC1B,qBAAqB;IACrB,QADW,MAAM,CACF;IACf,qBAAqB;IACrB,WADW,MAAM,CACE;IACnB,sCAAsC;IACtC,QADW,CAAC,UAAU,GAAC,MAAM,EAAE,CAAC,EAAE,CAClB;IAEhB,uCAAuC;IACvC,iBADW,UAAU,GAAC,MAAM,EAAE,GAAC,IAAI,CACR;IAG7B,cAUC;IAED;;OAEG;IACH,YAFW,UAAU,GAAC,MAAM,EAAE,QAsC7B;IAED;;;OAGG;IACH,gBAHW,OAAO,GACL,UAAU,CAwBtB;CACF"} \ No newline at end of file diff --git a/types/utils/byte-utils.d.ts b/types/utils/byte-utils.d.ts new file mode 100644 index 0000000..409c35e --- /dev/null +++ b/types/utils/byte-utils.d.ts @@ -0,0 +1,53 @@ +/** + * @param {Uint8Array|number[]} buf + * @returns {Uint8Array} + */ +export function asU8A(buf: Uint8Array | number[]): Uint8Array; +/** + * @param {Uint8Array} b1 + * @param {Uint8Array} b2 + * @returns {number} + */ +export function compare(b1: Uint8Array, b2: Uint8Array): number; +/** + * @param {number[]} codePoints + * @returns {string} + */ +export function decodeCodePointsArray(codePoints: number[]): string; +export const useBuffer: boolean; +/** + * @param {Uint8Array} bytes + * @param {number} start + * @param {number} end + */ +export function toString(bytes: Uint8Array, start: number, end: number): string; +export const fromString: ((string: string) => number[] | Buffer) | ((string: string) => Uint8Array | number[]); +export function fromArray(arr: number[]): Uint8Array; +/** + * @param {Uint8Array} bytes + * @param {number} start + * @param {number} end + */ +export function slice(bytes: Uint8Array, start: number, end: number): Uint8Array; +/** + * @param {Uint8Array[]} chunks + * @param {number} length + * @returns {Uint8Array} + */ +export function concat(chunks: Uint8Array[], length: number): Uint8Array; +/** + * @param {number} size + * @returns {Uint8Array} + */ +export function alloc(size: number): Uint8Array; +/** + * @param {Uint8Array} d + * @returns {string} + */ +export function toHex(d: Uint8Array): string; +/** + * @param {string|Uint8Array} hex + * @returns {Uint8Array} + */ +export function fromHex(hex: string | Uint8Array): Uint8Array; +//# sourceMappingURL=byte-utils.d.ts.map \ No newline at end of file diff --git a/types/utils/byte-utils.d.ts.map b/types/utils/byte-utils.d.ts.map new file mode 100644 index 0000000..5d5429e --- /dev/null +++ b/types/utils/byte-utils.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"byte-utils.d.ts","sourceRoot":"","sources":["../../utils/byte-utils.js"],"names":[],"mappings":"AAwBA;;;GAGG;AACH,2BAHW,UAAU,GAAC,MAAM,EAAE,GACjB,UAAU,CAQtB;AA8ND;;;;GAIG;AACH,4BAJW,UAAU,MACV,UAAU,GACR,MAAM,CAgBlB;AAyHD;;;GAGG;AACH,kDAHW,MAAM,EAAE,GACN,MAAM,CAkBlB;AA5ZD,gCAMkD;AA4B9C;;;;GAIG;AACH,gCAJW,UAAU,SACV,MAAM,OACN,MAAM,UAQhB;AAcL,mCAGe,MAAM,iDAYN,MAAM,6CAIhB;AAOE,+BAHI,MAAM,EAAE,GACN,UAAU,CAItB;AAIG;;;;GAIG;AACH,6BAJW,UAAU,SACV,MAAM,OACN,MAAM,2BAOhB;AAcD;;;;GAIG;AACH,+BAJW,UAAU,EAAE,UACZ,MAAM,GACJ,UAAU,CActB;AAwBD;;;GAGG;AACH,4BAHW,MAAM,GACJ,UAAU,CAMtB;AAaD;;;GAGG;AACH,yBAHW,UAAU,GACR,MAAM,CAQlB;AAiBH;;;GAGG;AACD,6BAHS,MAAM,GAAC,UAAU,GACf,UAAU,CAQpB"} \ No newline at end of file diff --git a/types/utils/common.d.ts b/types/utils/common.d.ts new file mode 100644 index 0000000..da40693 --- /dev/null +++ b/types/utils/common.d.ts @@ -0,0 +1,8 @@ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} need + * @param {string} decodeErrPrefix + */ +export function assertEnoughData(data: Uint8Array, pos: number, need: number, decodeErrPrefix: string): void; +//# sourceMappingURL=common.d.ts.map \ No newline at end of file diff --git a/types/utils/common.d.ts.map b/types/utils/common.d.ts.map new file mode 100644 index 0000000..aa49805 --- /dev/null +++ b/types/utils/common.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../utils/common.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,uCALW,UAAU,OACV,MAAM,QACN,MAAM,mBACN,MAAM,QAMhB"} \ No newline at end of file diff --git a/types/utils/encode.d.ts b/types/utils/encode.d.ts new file mode 100644 index 0000000..e17b0b2 --- /dev/null +++ b/types/utils/encode.d.ts @@ -0,0 +1,22 @@ +/** + * @typedef {import('../interface').EncodeOptions} EncodeOptions + * @typedef {import('../interface').OptionalTypeEncoder} OptionalTypeEncoder + * @typedef {import('../interface').Reference} Reference + * @typedef {import('../interface').StrictTypeEncoder} StrictTypeEncoder + * @typedef {import('../interface').TokenTypeEncoder} TokenTypeEncoder + * @typedef {import('../interface').TokenOrNestedTokens} TokenOrNestedTokens + */ +/** + * @param {any} data + * @param {TokenTypeEncoder[]} encoders + * @param {EncodeOptions} options + * @returns {Uint8Array} + */ +declare function encodeCustom(data: any, encoders: TokenTypeEncoder[], options: EncodeOptions): Uint8Array; +type EncodeOptions = import("../interface").EncodeOptions; +type OptionalTypeEncoder = import("../interface").OptionalTypeEncoder; +type Reference = import("../interface").Reference; +type StrictTypeEncoder = import("../interface").StrictTypeEncoder; +type TokenTypeEncoder = import("../interface").TokenTypeEncoder; +type TokenOrNestedTokens = import("../interface").TokenOrNestedTokens; +//# sourceMappingURL=encode.d.ts.map \ No newline at end of file diff --git a/types/utils/encode.d.ts.map b/types/utils/encode.d.ts.map new file mode 100644 index 0000000..2ce87b5 --- /dev/null +++ b/types/utils/encode.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../utils/encode.js"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH;;;;;GAKG;AACH,oCALW,GAAG,YACH,gBAAgB,EAAE,WAClB,aAAa,GACX,UAAU,CAyBpB;qBArCU,OAAO,cAAc,EAAE,aAAa;2BACpC,OAAO,cAAc,EAAE,mBAAmB;iBAC1C,OAAO,cAAc,EAAE,SAAS;yBAChC,OAAO,cAAc,EAAE,iBAAiB;wBACxC,OAAO,cAAc,EAAE,gBAAgB;2BACvC,OAAO,cAAc,EAAE,mBAAmB"} \ No newline at end of file diff --git a/types/utils/index.d.ts b/types/utils/index.d.ts new file mode 100644 index 0000000..0cdf3db --- /dev/null +++ b/types/utils/index.d.ts @@ -0,0 +1,13 @@ +export * from "./byte-utils.js"; +export * from "./jump.js"; +export { encodeNegint } from "./1negint.js"; +export { encodeBytes } from "./2bytes.js"; +export { encodeString } from "./3string.js"; +export { encodeArray } from "./4array.js"; +export { encodeMap } from "./5map.js"; +export { encodeTag } from "./6tag.js"; +export { encodeFloat } from "./7float.js"; +export { Bl } from "./bl.js"; +export { encodeUint, uintBoundaries } from "./0uint.js"; +export { Token, Type } from "./token.js"; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/types/utils/index.d.ts.map b/types/utils/index.d.ts.map new file mode 100644 index 0000000..636b586 --- /dev/null +++ b/types/utils/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../utils/index.js"],"names":[],"mappings":""} \ No newline at end of file diff --git a/types/utils/jump.d.ts b/types/utils/jump.d.ts new file mode 100644 index 0000000..e3c830c --- /dev/null +++ b/types/utils/jump.d.ts @@ -0,0 +1,16 @@ +/** + * @param {string} decodeErrPrefix + * @return {DecodeFunction[]} + */ +export function jump(decodeErrPrefix: string): DecodeFunction[]; +/** + * @param {Token} token + * @returns {Uint8Array|undefined} + */ +export function quickEncodeToken(token: Token): Uint8Array | undefined; +/** @type {Token[]} */ +export const quick: Token[]; +export type DecodeOptions = import("../interface.js").DecodeOptions; +export type DecodeFunction = import("../interface.js").DecodeFunction; +import { Token } from './token.js'; +//# sourceMappingURL=jump.d.ts.map \ No newline at end of file diff --git a/types/utils/jump.d.ts.map b/types/utils/jump.d.ts.map new file mode 100644 index 0000000..bbe5c9a --- /dev/null +++ b/types/utils/jump.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"jump.d.ts","sourceRoot":"","sources":["../../utils/jump.js"],"names":[],"mappings":"AAwCA;;;GAGG;AACH,sCAHW,MAAM,GACL,cAAc,EAAE,CA0G3B;AA2BD;;;GAGG;AACH,wCAHW,KAAK,GACH,UAAU,GAAC,SAAS,CA4ChC;AAvED,sBAAsB;AACtB,oBADW,KAAK,EAAE,CACK;4BA3IV,OAAO,iBAAiB,EAAE,aAAa;6BACvC,OAAO,iBAAiB,EAAE,cAAc;sBAbzB,YAAY"} \ No newline at end of file diff --git a/types/utils/token.d.ts b/types/utils/token.d.ts new file mode 100644 index 0000000..8fba8f5 --- /dev/null +++ b/types/utils/token.d.ts @@ -0,0 +1,59 @@ +export class Type { + /** + * @param {number} major + * @param {string} name + * @param {boolean} terminal + */ + constructor(major: number, name: string, terminal: boolean); + major: number; + majorEncoded: number; + name: string; + terminal: boolean; + toString(): string; + /** + * @param {Type} typ + * @returns {boolean} + */ + equals(typ: Type): boolean; + /** + * @param {Type} typ + * @returns {number} + */ + compare(typ: Type): number; +} +export namespace Type { + export let uint: Type; + export let negint: Type; + export let bytes: Type; + export let string: Type; + export let array: Type; + export let map: Type; + export let tag: Type; + export let float: Type; + let _false: Type; + export { _false as false }; + let _true: Type; + export { _true as true }; + let _null: Type; + export { _null as null }; + export let undefined: Type; + let _break: Type; + export { _break as break }; +} +export class Token { + /** + * @param {Type} type + * @param {any} [value] + * @param {number} [encodedLength] + */ + constructor(type: Type, value?: any, encodedLength?: number); + type: Type; + value: any; + encodedLength: number | undefined; + /** @type {Uint8Array|undefined} */ + encodedBytes: Uint8Array | undefined; + /** @type {Uint8Array|undefined} */ + byteValue: Uint8Array | undefined; + toString(): string; +} +//# sourceMappingURL=token.d.ts.map \ No newline at end of file diff --git a/types/utils/token.d.ts.map b/types/utils/token.d.ts.map new file mode 100644 index 0000000..94b100e --- /dev/null +++ b/types/utils/token.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../utils/token.js"],"names":[],"mappings":"AAAA;IACE;;;;OAIG;IACH,mBAJW,MAAM,QACN,MAAM,YACN,OAAO,EAOjB;IAJC,cAAkB;IAClB,qBAA8B;IAC9B,aAAgB;IAChB,kBAAwB;IAI1B,mBAEC;IAED;;;OAGG;IACH,YAHW,IAAI,GACF,OAAO,CAInB;IAED;;;OAGG;IACH,aAHW,IAAI,GACF,MAAM,CAKlB;CACF;;;;;;;;;;;;;;;;;;;;AAmBD;IACE;;;;OAIG;IACH,kBAJW,IAAI,UACJ,GAAG,kBACH,MAAM,EAUhB;IAPC,WAAgB;IAChB,WAAkB;IAClB,kCAAkC;IAClC,mCAAmC;IACnC,cADW,UAAU,GAAC,SAAS,CACF;IAC7B,mCAAmC;IACnC,WADW,UAAU,GAAC,SAAS,CACL;IAI5B,mBAEC;CACF"} \ No newline at end of file diff --git a/lib/0uint.js b/utils/0uint.js similarity index 82% rename from lib/0uint.js rename to utils/0uint.js index 21d6555..9f52317 100644 --- a/lib/0uint.js +++ b/utils/0uint.js @@ -1,13 +1,13 @@ /* globals BigInt */ import { Token, Type } from './token.js' -import { decodeErrPrefix, assertEnoughData } from './common.js' +import { assertEnoughData } from './common.js' export const uintBoundaries = [24, 256, 65536, 4294967296, BigInt('18446744073709551616')] /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -17,10 +17,10 @@ export const uintBoundaries = [24, 256, 65536, 4294967296, BigInt('1844674407370 * @returns {number} */ export function readUint8 (data, offset, options) { - assertEnoughData(data, offset, 1) + assertEnoughData(data, offset, 1, options.decodeErrPrefix) const value = data[offset] if (options.strict === true && value < uintBoundaries[0]) { - throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) + throw new Error(`${options.decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) } return value } @@ -32,10 +32,10 @@ export function readUint8 (data, offset, options) { * @returns {number} */ export function readUint16 (data, offset, options) { - assertEnoughData(data, offset, 2) + assertEnoughData(data, offset, 2, options.decodeErrPrefix) const value = (data[offset] << 8) | data[offset + 1] if (options.strict === true && value < uintBoundaries[1]) { - throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) + throw new Error(`${options.decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) } return value } @@ -47,10 +47,10 @@ export function readUint16 (data, offset, options) { * @returns {number} */ export function readUint32 (data, offset, options) { - assertEnoughData(data, offset, 4) + assertEnoughData(data, offset, 4, options.decodeErrPrefix) const value = (data[offset] * 16777216 /* 2 ** 24 */) + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3] if (options.strict === true && value < uintBoundaries[2]) { - throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) + throw new Error(`${options.decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) } return value } @@ -63,12 +63,12 @@ export function readUint32 (data, offset, options) { */ export function readUint64 (data, offset, options) { // assume BigInt, convert back to Number if within safe range - assertEnoughData(data, offset, 8) + assertEnoughData(data, offset, 8, options.decodeErrPrefix) const hi = (data[offset] * 16777216 /* 2 ** 24 */) + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3] const lo = (data[offset + 4] * 16777216 /* 2 ** 24 */) + (data[offset + 5] << 16) + (data[offset + 6] << 8) + data[offset + 7] const value = (BigInt(hi) << BigInt(32)) + BigInt(lo) if (options.strict === true && value < uintBoundaries[3]) { - throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) + throw new Error(`${options.decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`) } if (value <= Number.MAX_SAFE_INTEGER) { return Number(value) @@ -76,7 +76,7 @@ export function readUint64 (data, offset, options) { if (options.allowBigInt === true) { return value } - throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`) + throw new Error(`${options.decodeErrPrefix} integers outside of the safe integer range are not supported`) } /* not required thanks to quick[] list @@ -133,17 +133,19 @@ export function decodeUint64 (data, pos, _minor, options) { /** * @param {Bl} buf * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options */ -export function encodeUint (buf, token) { - return encodeUintValue(buf, 0, token.value) +export function encodeUint (buf, token, { encodeErrPrefix }) { + return encodeUintValue(buf, 0, token.value, encodeErrPrefix) } /** * @param {Bl} buf * @param {number} major * @param {number|bigint} uint + * @param {string} decodeErrPrefix */ -export function encodeUintValue (buf, major, uint) { +export function encodeUintValue (buf, major, uint, decodeErrPrefix) { if (uint < uintBoundaries[0]) { const nuint = Number(uint) // pack into one byte, minor=0, additional=value diff --git a/lib/1negint.js b/utils/1negint.js similarity index 87% rename from lib/1negint.js rename to utils/1negint.js index 199c614..6b4c764 100644 --- a/lib/1negint.js +++ b/utils/1negint.js @@ -2,11 +2,10 @@ import { Token, Type } from './token.js' import * as uint from './0uint.js' -import { decodeErrPrefix } from './common.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -61,7 +60,7 @@ export function decodeNegint64 (data, pos, _minor, options) { } } if (options.allowBigInt !== true) { - throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`) + throw new Error(`${options.decodeErrPrefix} integers outside of the safe integer range are not supported`) } return new Token(Type.negint, neg1b - BigInt(int), 9) } @@ -69,11 +68,12 @@ export function decodeNegint64 (data, pos, _minor, options) { /** * @param {Bl} buf * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options */ -export function encodeNegint (buf, token) { +export function encodeNegint (buf, token, { encodeErrPrefix }) { const negint = token.value const unsigned = (typeof negint === 'bigint' ? (negint * neg1b - pos1b) : (negint * -1 - 1)) - uint.encodeUintValue(buf, token.type.majorEncoded, unsigned) + uint.encodeUintValue(buf, token.type.majorEncoded, unsigned, encodeErrPrefix) } /** diff --git a/lib/2bytes.js b/utils/2bytes.js similarity index 77% rename from lib/2bytes.js rename to utils/2bytes.js index 6fd4feb..32bf6dc 100644 --- a/lib/2bytes.js +++ b/utils/2bytes.js @@ -1,11 +1,11 @@ import { Token, Type } from './token.js' -import { assertEnoughData, decodeErrPrefix } from './common.js' +import { assertEnoughData } from './common.js' import * as uint from './0uint.js' import { compare, fromString, slice } from './byte-utils.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -13,10 +13,11 @@ import { compare, fromString, slice } from './byte-utils.js' * @param {number} pos * @param {number} prefix * @param {number} length + * @param {string} decodeErrPrefix * @returns {Token} */ -function toToken (data, pos, prefix, length) { - assertEnoughData(data, pos, prefix + length) +function toToken (data, pos, prefix, length, decodeErrPrefix) { + assertEnoughData(data, pos, prefix + length, decodeErrPrefix) const buf = slice(data, pos + prefix, pos + prefix + length) return new Token(Type.bytes, buf, prefix + length) } @@ -25,11 +26,11 @@ function toToken (data, pos, prefix, length) { * @param {Uint8Array} data * @param {number} pos * @param {number} minor - * @param {DecodeOptions} _options + * @param {DecodeOptions} options * @returns {Token} */ -export function decodeBytesCompact (data, pos, minor, _options) { - return toToken(data, pos, 1, minor) +export function decodeBytesCompact (data, pos, minor, options) { + return toToken(data, pos, 1, minor, options.decodeErrPrefix) } /** @@ -40,7 +41,7 @@ export function decodeBytesCompact (data, pos, minor, _options) { * @returns {Token} */ export function decodeBytes8 (data, pos, _minor, options) { - return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options)) + return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options), options.decodeErrPrefix) } /** @@ -51,7 +52,7 @@ export function decodeBytes8 (data, pos, _minor, options) { * @returns {Token} */ export function decodeBytes16 (data, pos, _minor, options) { - return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options)) + return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options), options.decodeErrPrefix) } /** @@ -62,7 +63,7 @@ export function decodeBytes16 (data, pos, _minor, options) { * @returns {Token} */ export function decodeBytes32 (data, pos, _minor, options) { - return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options)) + return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options), options.decodeErrPrefix) } // TODO: maybe we shouldn't support this .. @@ -76,9 +77,9 @@ export function decodeBytes32 (data, pos, _minor, options) { export function decodeBytes64 (data, pos, _minor, options) { const l = uint.readUint64(data, pos + 1, options) if (typeof l === 'bigint') { - throw new Error(`${decodeErrPrefix} 64-bit integer bytes lengths not supported`) + throw new Error(`${options.decodeErrPrefix} 64-bit integer bytes lengths not supported`) } - return toToken(data, pos, 9, l) + return toToken(data, pos, 9, l, options.decodeErrPrefix) } /** @@ -98,10 +99,11 @@ function tokenBytes (token) { /** * @param {Bl} buf * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options */ -export function encodeBytes (buf, token) { +export function encodeBytes (buf, token, { encodeErrPrefix }) { const bytes = tokenBytes(token) - uint.encodeUintValue(buf, token.type.majorEncoded, bytes.length) + uint.encodeUintValue(buf, token.type.majorEncoded, bytes.length, encodeErrPrefix) buf.push(bytes) } diff --git a/lib/3string.js b/utils/3string.js similarity index 88% rename from lib/3string.js rename to utils/3string.js index 1098702..487966c 100644 --- a/lib/3string.js +++ b/utils/3string.js @@ -1,12 +1,12 @@ import { Token, Type } from './token.js' -import { assertEnoughData, decodeErrPrefix } from './common.js' +import { assertEnoughData } from './common.js' import * as uint from './0uint.js' import { encodeBytes } from './2bytes.js' import { toString, slice } from './byte-utils.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -19,7 +19,7 @@ import { toString, slice } from './byte-utils.js' */ function toToken (data, pos, prefix, length, options) { const totLength = prefix + length - assertEnoughData(data, pos, totLength) + assertEnoughData(data, pos, totLength, options.decodeErrPrefix) const tok = new Token(Type.string, toString(data, pos + prefix, pos + totLength), totLength) if (options.retainStringBytes === true) { tok.byteValue = slice(data, pos + prefix, pos + totLength) @@ -82,7 +82,7 @@ export function decodeString32 (data, pos, _minor, options) { export function decodeString64 (data, pos, _minor, options) { const l = uint.readUint64(data, pos + 1, options) if (typeof l === 'bigint') { - throw new Error(`${decodeErrPrefix} 64-bit integer string lengths not supported`) + throw new Error(`${options.decodeErrPrefix} 64-bit integer string lengths not supported`) } return toToken(data, pos, 9, l, options) } diff --git a/lib/4array.js b/utils/4array.js similarity index 85% rename from lib/4array.js rename to utils/4array.js index ec8fb37..ad828af 100644 --- a/lib/4array.js +++ b/utils/4array.js @@ -1,10 +1,10 @@ import { Token, Type } from './token.js' import * as uint from './0uint.js' -import { decodeErrPrefix } from './common.js' +// import { decodeErrPrefix } from '../lib/common.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -73,7 +73,7 @@ export function decodeArray32 (data, pos, _minor, options) { export function decodeArray64 (data, pos, _minor, options) { const l = uint.readUint64(data, pos + 1, options) if (typeof l === 'bigint') { - throw new Error(`${decodeErrPrefix} 64-bit integer array lengths not supported`) + throw new Error(`${options.decodeErrPrefix} 64-bit integer array lengths not supported`) } return toToken(data, pos, 9, l) } @@ -87,7 +87,7 @@ export function decodeArray64 (data, pos, _minor, options) { */ export function decodeArrayIndefinite (data, pos, _minor, options) { if (options.allowIndefinite === false) { - throw new Error(`${decodeErrPrefix} indefinite length items not allowed`) + throw new Error(`${options.decodeErrPrefix} indefinite length items not allowed`) } return toToken(data, pos, 1, Infinity) } @@ -95,9 +95,10 @@ export function decodeArrayIndefinite (data, pos, _minor, options) { /** * @param {Bl} buf * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options */ -export function encodeArray (buf, token) { - uint.encodeUintValue(buf, Type.array.majorEncoded, token.value) +export function encodeArray (buf, token, { encodeErrPrefix }) { + uint.encodeUintValue(buf, Type.array.majorEncoded, token.value, encodeErrPrefix) } // using an array as a map key, are you sure about this? we can only sort diff --git a/lib/5map.js b/utils/5map.js similarity index 84% rename from lib/5map.js rename to utils/5map.js index 77bb7c4..4765917 100644 --- a/lib/5map.js +++ b/utils/5map.js @@ -1,10 +1,9 @@ import { Token, Type } from './token.js' import * as uint from './0uint.js' -import { decodeErrPrefix } from './common.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -73,7 +72,7 @@ export function decodeMap32 (data, pos, _minor, options) { export function decodeMap64 (data, pos, _minor, options) { const l = uint.readUint64(data, pos + 1, options) if (typeof l === 'bigint') { - throw new Error(`${decodeErrPrefix} 64-bit integer map lengths not supported`) + throw new Error(`${options.decodeErrPrefix} 64-bit integer map lengths not supported`) } return toToken(data, pos, 9, l) } @@ -87,7 +86,7 @@ export function decodeMap64 (data, pos, _minor, options) { */ export function decodeMapIndefinite (data, pos, _minor, options) { if (options.allowIndefinite === false) { - throw new Error(`${decodeErrPrefix} indefinite length items not allowed`) + throw new Error(`${options.decodeErrPrefix} indefinite length items not allowed`) } return toToken(data, pos, 1, Infinity) } @@ -95,9 +94,10 @@ export function decodeMapIndefinite (data, pos, _minor, options) { /** * @param {Bl} buf * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options */ -export function encodeMap (buf, token) { - uint.encodeUintValue(buf, Type.map.majorEncoded, token.value) +export function encodeMap (buf, token, { encodeErrPrefix }) { + uint.encodeUintValue(buf, Type.map.majorEncoded, token.value, encodeErrPrefix) } // using a map as a map key, are you sure about this? we can only sort diff --git a/lib/6tag.js b/utils/6tag.js similarity index 86% rename from lib/6tag.js rename to utils/6tag.js index 43e094c..2a1015d 100644 --- a/lib/6tag.js +++ b/utils/6tag.js @@ -3,7 +3,7 @@ import * as uint from './0uint.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions */ /** @@ -64,9 +64,10 @@ export function decodeTag64 (data, pos, _minor, options) { /** * @param {Bl} buf * @param {Token} token + * @param {import('../interface.js').EncodeOptions} options */ -export function encodeTag (buf, token) { - uint.encodeUintValue(buf, Type.tag.majorEncoded, token.value) +export function encodeTag (buf, token, { encodeErrPrefix }) { + uint.encodeUintValue(buf, Type.tag.majorEncoded, token.value, encodeErrPrefix) } encodeTag.compareTokens = uint.encodeUint.compareTokens diff --git a/lib/7float.js b/utils/7float.js similarity index 83% rename from lib/7float.js rename to utils/7float.js index 40ae913..1f117d7 100644 --- a/lib/7float.js +++ b/utils/7float.js @@ -2,13 +2,12 @@ // where possible import { Token, Type } from './token.js' -import { decodeErrPrefix } from './common.js' import { encodeUint } from './0uint.js' /** * @typedef {import('./bl.js').Bl} Bl - * @typedef {import('../interface').DecodeOptions} DecodeOptions - * @typedef {import('../interface').EncodeOptions} EncodeOptions + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').EncodeOptions} EncodeOptions */ const MINOR_FALSE = 20 @@ -25,7 +24,7 @@ const MINOR_UNDEFINED = 23 */ export function decodeUndefined (_data, _pos, _minor, options) { if (options.allowUndefined === false) { - throw new Error(`${decodeErrPrefix} undefined values are not supported`) + throw new Error(`${options.decodeErrPrefix} undefined values are not supported`) } else if (options.coerceUndefinedToNull === true) { return new Token(Type.null, null, 1) } @@ -41,7 +40,7 @@ export function decodeUndefined (_data, _pos, _minor, options) { */ export function decodeBreak (_data, _pos, _minor, options) { if (options.allowIndefinite === false) { - throw new Error(`${decodeErrPrefix} indefinite length items not allowed`) + throw new Error(`${options.decodeErrPrefix} indefinite length items not allowed`) } return new Token(Type.break, undefined, 1) } @@ -55,10 +54,10 @@ export function decodeBreak (_data, _pos, _minor, options) { function createToken (value, bytes, options) { if (options) { if (options.allowNaN === false && Number.isNaN(value)) { - throw new Error(`${decodeErrPrefix} NaN values are not supported`) + throw new Error(`${options.decodeErrPrefix} NaN values are not supported`) } if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) { - throw new Error(`${decodeErrPrefix} Infinity values are not supported`) + throw new Error(`${options.decodeErrPrefix} Infinity values are not supported`) } } return new Token(Type.float, value, bytes) @@ -72,7 +71,7 @@ function createToken (value, bytes, options) { * @returns {Token} */ export function decodeFloat16 (data, pos, _minor, options) { - return createToken(readFloat16(data, pos + 1), 3, options) + return createToken(readFloat16(data, pos + 1, options.decodeErrPrefix), 3, options) } /** @@ -83,7 +82,7 @@ export function decodeFloat16 (data, pos, _minor, options) { * @returns {Token} */ export function decodeFloat32 (data, pos, _minor, options) { - return createToken(readFloat32(data, pos + 1), 5, options) + return createToken(readFloat32(data, pos + 1, options.decodeErrPrefix), 5, options) } /** @@ -94,7 +93,7 @@ export function decodeFloat32 (data, pos, _minor, options) { * @returns {Token} */ export function decodeFloat64 (data, pos, _minor, options) { - return createToken(readFloat64(data, pos + 1), 9, options) + return createToken(readFloat64(data, pos + 1, options.decodeErrPrefix), 9, options) } /** @@ -118,14 +117,14 @@ export function encodeFloat (buf, token, options) { let success = false if (!options || options.float64 !== true) { encodeFloat16(float) - decoded = readFloat16(ui8a, 1) + decoded = readFloat16(ui8a, 1, options.encodeErrPrefix) if (float === decoded || Number.isNaN(float)) { ui8a[0] = 0xf9 buf.push(ui8a.slice(0, 3)) success = true } else { encodeFloat32(float) - decoded = readFloat32(ui8a, 1) + decoded = readFloat32(ui8a, 1, options.encodeErrPrefix) if (float === decoded) { ui8a[0] = 0xfa buf.push(ui8a.slice(0, 5)) @@ -135,7 +134,7 @@ export function encodeFloat (buf, token, options) { } if (!success) { encodeFloat64(float) - decoded = readFloat64(ui8a, 1) + decoded = readFloat64(ui8a, 1, options.encodeErrPrefix) ui8a[0] = 0xfb buf.push(ui8a.slice(0, 9)) } @@ -156,12 +155,12 @@ encodeFloat.encodedSize = function encodedSize (token, options) { if (!options || options.float64 !== true) { encodeFloat16(float) - let decoded = readFloat16(ui8a, 1) + let decoded = readFloat16(ui8a, 1, options.encodeErrPrefix) if (float === decoded || Number.isNaN(float)) { return 3 } encodeFloat32(float) - decoded = readFloat32(ui8a, 1) + decoded = readFloat32(ui8a, 1, options.encodeErrPrefix) if (float === decoded) { return 5 } @@ -222,11 +221,12 @@ function encodeFloat16 (inp) { /** * @param {Uint8Array} ui8a * @param {number} pos + * @param {string} encodeErrPrefix * @returns {number} */ -function readFloat16 (ui8a, pos) { +function readFloat16 (ui8a, pos, encodeErrPrefix) { if (ui8a.length - pos < 2) { - throw new Error(`${decodeErrPrefix} not enough data for float16`) + throw new Error(`${encodeErrPrefix} not enough data for float16`) } const half = (ui8a[pos] << 8) + ui8a[pos + 1] @@ -264,11 +264,12 @@ function encodeFloat32 (inp) { /** * @param {Uint8Array} ui8a * @param {number} pos + * @param {string} encodeErrPrefix * @returns {number} */ -function readFloat32 (ui8a, pos) { +function readFloat32 (ui8a, pos, encodeErrPrefix) { if (ui8a.length - pos < 4) { - throw new Error(`${decodeErrPrefix} not enough data for float32`) + throw new Error(`${encodeErrPrefix} not enough data for float32`) } const offset = (ui8a.byteOffset || 0) + pos return new DataView(ui8a.buffer, offset, 4).getFloat32(0, false) @@ -284,11 +285,12 @@ function encodeFloat64 (inp) { /** * @param {Uint8Array} ui8a * @param {number} pos + * @param {string} encodeErrPrefix * @returns {number} */ -function readFloat64 (ui8a, pos) { +function readFloat64 (ui8a, pos, encodeErrPrefix) { if (ui8a.length - pos < 8) { - throw new Error(`${decodeErrPrefix} not enough data for float64`) + throw new Error(`${encodeErrPrefix} not enough data for float64`) } const offset = (ui8a.byteOffset || 0) + pos return new DataView(ui8a.buffer, offset, 8).getFloat64(0, false) diff --git a/lib/bl.js b/utils/bl.js similarity index 98% rename from lib/bl.js rename to utils/bl.js index af974de..8b5fa52 100644 --- a/lib/bl.js +++ b/utils/bl.js @@ -16,7 +16,7 @@ */ // TODO: ipjs doesn't support this, only for test files: https://github.com/mikeal/ipjs/blob/master/src/package/testFile.js#L39 -import { alloc, concat, slice } from './byte-utils.js' +import { alloc, concat, slice } from 'cborg/utils' // the ts-ignores in this file are almost all for the `Uint8Array|number[]` duality that exists // for perf reasons. Consider better approaches to this or removing it entirely, it is quite diff --git a/lib/byte-utils.js b/utils/byte-utils.js similarity index 100% rename from lib/byte-utils.js rename to utils/byte-utils.js diff --git a/utils/common.js b/utils/common.js new file mode 100644 index 0000000..2a7e502 --- /dev/null +++ b/utils/common.js @@ -0,0 +1,11 @@ +/** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} need + * @param {string} decodeErrPrefix + */ +export function assertEnoughData (data, pos, need, decodeErrPrefix) { + if (data.length - pos < need) { + throw new Error(`${decodeErrPrefix} not enough data for type`) + } +} diff --git a/utils/index.js b/utils/index.js new file mode 100644 index 0000000..8ada986 --- /dev/null +++ b/utils/index.js @@ -0,0 +1,12 @@ +export * from './byte-utils.js' +export { encodeUint, uintBoundaries } from './0uint.js' +export { encodeNegint } from './1negint.js' +export { encodeBytes } from './2bytes.js' +export { encodeString } from './3string.js' +export { encodeArray } from './4array.js' +export { encodeMap } from './5map.js' +export { encodeTag } from './6tag.js' +export { encodeFloat } from './7float.js' +export { Bl } from './bl.js' +export { Token, Type } from './token.js' +export * from './jump.js' diff --git a/utils/jump.js b/utils/jump.js new file mode 100644 index 0000000..6ef190a --- /dev/null +++ b/utils/jump.js @@ -0,0 +1,222 @@ +import { Token, Type } from './token.js' +import * as uint from './0uint.js' +import * as negint from './1negint.js' +import * as bytes from './2bytes.js' +import * as string from './3string.js' +import * as array from './4array.js' +import * as map from './5map.js' +import * as tag from './6tag.js' +import * as float from './7float.js' +import { fromArray } from './byte-utils.js' + +/** + * @typedef {import('../interface.js').DecodeOptions} DecodeOptions + * @typedef {import('../interface.js').DecodeFunction} DecodeFunction + */ + +/** + * @param {string} decodeErrPrefix + * @returns {DecodeFunction} + */ +function invalidMinorWithPrefix (decodeErrPrefix) { + /** + * @param {Uint8Array} data + * @param {number} pos + * @param {number} minor + */ + return (data, pos, minor) => { + throw new Error(`${decodeErrPrefix} encountered invalid minor (${minor}) for major ${data[pos] >>> 5}`) + } +} + +/** + * @param {string} msg + * @param {string} decodeErrPrefix + * @returns {()=>any} + */ +function errorer (msg, decodeErrPrefix) { + return () => { throw new Error(`${decodeErrPrefix} ${msg}`) } +} + +/** + * @param {string} decodeErrPrefix + * @return {DecodeFunction[]} + */ +export function jump (decodeErrPrefix) { + const invalidMinor = invalidMinorWithPrefix(decodeErrPrefix) + const jump = Array(256).fill(invalidMinor) + // unsigned integer, 0x00..0x17 (0..23) + // for (let i = 0; i <= 0x17; i++) { + // jump[i] = invalidMinor // uint.decodeUintCompact, handled by quick[] + // } + jump[0x18] = uint.decodeUint8 // unsigned integer, one-byte uint8_t follows + jump[0x19] = uint.decodeUint16 // unsigned integer, two-byte uint16_t follows + jump[0x1a] = uint.decodeUint32 // unsigned integer, four-byte uint32_t follows + jump[0x1b] = uint.decodeUint64 // unsigned integer, eight-byte uint64_t follows + // jump[0x1c] = invalidMinor + // jump[0x1d] = invalidMinor + // jump[0x1e] = invalidMinor + // jump[0x1f] = invalidMinor + // negative integer, -1-0x00..-1-0x17 (-1..-24) + // for (let i = 0x20; i <= 0x37; i++) { + // jump[i] = invalidMinor // negintDecode, handled by quick[] + // } + jump[0x38] = negint.decodeNegint8 // negative integer, -1-n one-byte uint8_t for n follows + jump[0x39] = negint.decodeNegint16 // negative integer, -1-n two-byte uint16_t for n follows + jump[0x3a] = negint.decodeNegint32 // negative integer, -1-n four-byte uint32_t for follows + jump[0x3b] = negint.decodeNegint64 // negative integer, -1-n eight-byte uint64_t for follows + // jump[0x3c] = invalidMinor + // jump[0x3d] = invalidMinor + // jump[0x3e] = invalidMinor + // jump[0x3f] = invalidMinor + // byte string, 0x00..0x17 bytes follow + for (let i = 0x40; i <= 0x57; i++) { + jump[i] = bytes.decodeBytesCompact + } + jump[0x58] = bytes.decodeBytes8 // byte string, one-byte uint8_t for n, and then n bytes follow + jump[0x59] = bytes.decodeBytes16 // byte string, two-byte uint16_t for n, and then n bytes follow + jump[0x5a] = bytes.decodeBytes32 // byte string, four-byte uint32_t for n, and then n bytes follow + jump[0x5b] = bytes.decodeBytes64 // byte string, eight-byte uint64_t for n, and then n bytes follow + // jump[0x5c] = invalidMinor + // jump[0x5d] = invalidMinor + // jump[0x5e] = invalidMinor + jump[0x5f] = errorer('indefinite length bytes/strings are not supported', decodeErrPrefix) // byte string, byte strings follow, terminated by "break" + // UTF-8 string 0x00..0x17 bytes follow + for (let i = 0x60; i <= 0x77; i++) { + jump[i] = string.decodeStringCompact + } + jump[0x78] = string.decodeString8 // UTF-8 string, one-byte uint8_t for n, and then n bytes follow + jump[0x79] = string.decodeString16 // UTF-8 string, two-byte uint16_t for n, and then n bytes follow + jump[0x7a] = string.decodeString32 // UTF-8 string, four-byte uint32_t for n, and then n bytes follow + jump[0x7b] = string.decodeString64 // UTF-8 string, eight-byte uint64_t for n, and then n bytes follow + // jump[0x7c] = invalidMinor + // jump[0x7d] = invalidMinor + // jump[0x7e] = invalidMinor + jump[0x7f] = errorer('indefinite length bytes/strings are not supported', decodeErrPrefix) // UTF-8 strings follow, terminated by "break" + // array, 0x00..0x17 data items follow + for (let i = 0x80; i <= 0x97; i++) { + jump[i] = array.decodeArrayCompact + } + jump[0x98] = array.decodeArray8 // array, one-byte uint8_t for n, and then n data items follow + jump[0x99] = array.decodeArray16 // array, two-byte uint16_t for n, and then n data items follow + jump[0x9a] = array.decodeArray32 // array, four-byte uint32_t for n, and then n data items follow + jump[0x9b] = array.decodeArray64 // array, eight-byte uint64_t for n, and then n data items follow + // jump[0x9c] = invalidMinor + // jump[0x9d] = invalidMinor + // jump[0x9e] = invalidMinor + jump[0x9f] = array.decodeArrayIndefinite // array, data items follow, terminated by "break" + // map, 0x00..0x17 pairs of data items follow + for (let i = 0xa0; i <= 0xb7; i++) { + jump[i] = map.decodeMapCompact + } + jump[0xb8] = map.decodeMap8 // map, one-byte uint8_t for n, and then n pairs of data items follow + jump[0xb9] = map.decodeMap16 // map, two-byte uint16_t for n, and then n pairs of data items follow + jump[0xba] = map.decodeMap32 // map, four-byte uint32_t for n, and then n pairs of data items follow + jump[0xbb] = map.decodeMap64 // map, eight-byte uint64_t for n, and then n pairs of data items follow + // jump[0xbc] = invalidMinor + // jump[0xbd] = invalidMinor + // jump[0xbe] = invalidMinor + jump[0xbf] = map.decodeMapIndefinite // map, pairs of data items follow, terminated by "break" + // tags + for (let i = 0xc0; i <= 0xd7; i++) { + jump[i] = tag.decodeTagCompact + } + jump[0xd8] = tag.decodeTag8 + jump[0xd9] = tag.decodeTag16 + jump[0xda] = tag.decodeTag32 + jump[0xdb] = tag.decodeTag64 + // jump[0xdc] = invalidMinor + // jump[0xdd] = invalidMinor + // jump[0xde] = invalidMinor + // jump[0xdf] = invalidMinor + // 0xe0..0xf3 simple values, unsupported + for (let i = 0xe0; i <= 0xf3; i++) { + jump[i] = errorer('simple values are not supported', decodeErrPrefix) + } + // jump[0xf4] = invalidMinor // false, handled by quick[] + // jump[0xf5] = invalidMinor // true, handled by quick[] + // jump[0xf6] = invalidMinor // null, handled by quick[] + jump[0xf7] = float.decodeUndefined // undefined + jump[0xf8] = errorer('simple values are not supported', decodeErrPrefix) // simple value, one byte follows, unsupported + jump[0xf9] = float.decodeFloat16 // half-precision float (two-byte IEEE 754) + jump[0xfa] = float.decodeFloat32 // single-precision float (four-byte IEEE 754) + jump[0xfb] = float.decodeFloat64 // double-precision float (eight-byte IEEE 754) + // jump[0xfc] = invalidMinor + // jump[0xfd] = invalidMinor + // jump[0xfe] = invalidMinor + jump[0xff] = float.decodeBreak // "break" stop code + return jump +} + +/** @type {Token[]} */ +export const quick = [] +// ints <24 +for (let i = 0; i < 24; i++) { + quick[i] = new Token(Type.uint, i, 1) +} +// negints >= -24 +for (let i = -1; i >= -24; i--) { + quick[31 - i] = new Token(Type.negint, i, 1) +} +// empty bytes +quick[0x40] = new Token(Type.bytes, new Uint8Array(0), 1) +// empty string +quick[0x60] = new Token(Type.string, '', 1) +// empty list +quick[0x80] = new Token(Type.array, 0, 1) +// empty map +quick[0xa0] = new Token(Type.map, 0, 1) +// false +quick[0xf4] = new Token(Type.false, false, 1) +// true +quick[0xf5] = new Token(Type.true, true, 1) +// null +quick[0xf6] = new Token(Type.null, null, 1) + +/** + * @param {Token} token + * @returns {Uint8Array|undefined} + */ +export function quickEncodeToken (token) { + switch (token.type) { + case Type.false: + return fromArray([0xf4]) + case Type.true: + return fromArray([0xf5]) + case Type.null: + return fromArray([0xf6]) + case Type.bytes: + if (!token.value.length) { + return fromArray([0x40]) + } + return + case Type.string: + if (token.value === '') { + return fromArray([0x60]) + } + return + case Type.array: + if (token.value === 0) { + return fromArray([0x80]) + } + /* c8 ignore next 2 */ + // shouldn't be possible if this were called when there was only one token + return + case Type.map: + if (token.value === 0) { + return fromArray([0xa0]) + } + /* c8 ignore next 2 */ + // shouldn't be possible if this were called when there was only one token + return + case Type.uint: + if (token.value < 24) { + return fromArray([Number(token.value)]) + } + return + case Type.negint: + if (token.value >= -24) { + return fromArray([31 - Number(token.value)]) + } + } +} diff --git a/lib/token.js b/utils/token.js similarity index 100% rename from lib/token.js rename to utils/token.js