@fireproof/vendor 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -8
- package/src/@ipld/car/README.md +229 -175
- package/src/@ipld/car/dist/src/reader.d.ts.map +1 -1
- package/src/@ipld/car/src/reader.js +19 -2
- package/src/cborg/.github/dependabot.yml +16 -0
- package/src/cborg/.github/workflows/test-and-release.yml +52 -0
- package/src/cborg/{lib → cborg}/bin.js +2 -2
- package/src/cborg/cborg/common.js +28 -0
- package/src/cborg/{lib → cborg}/decode.js +24 -22
- package/src/cborg/{lib → cborg}/diagnostic.js +6 -4
- package/src/cborg/{lib → cborg}/diagnostic_test.js +1 -1
- package/src/cborg/{lib → cborg}/encode.js +23 -21
- package/src/cborg/{cborg.js → cborg/index.js} +12 -10
- package/src/cborg/{lib → cborg}/length.js +3 -2
- package/src/cborg/interface.ts +7 -4
- package/src/cborg/{lib/json → json}/decode.js +25 -25
- package/src/cborg/{lib/json → json}/encode.js +19 -16
- package/src/cborg/taglib.js +3 -1
- package/src/cborg/test/common.js +1 -1
- package/src/cborg/test/noop-bin-test.js +1 -1
- package/src/cborg/test/test-0uint.js +2 -2
- package/src/cborg/test/test-1negint.js +2 -2
- package/src/cborg/test/test-2bytes.js +2 -2
- package/src/cborg/test/test-3string.js +2 -2
- package/src/cborg/test/test-4array.js +2 -2
- package/src/cborg/test/test-5map.js +2 -2
- package/src/cborg/test/test-6tag.js +2 -3
- package/src/cborg/test/test-7float.js +2 -2
- package/src/cborg/test/test-bl.js +1 -1
- package/src/cborg/test/test-cbor-vectors.js +2 -2
- package/src/cborg/test/test-decode-errors.js +2 -2
- package/src/cborg/test/test-fuzz.js +1 -1
- package/src/cborg/test/test-json.js +25 -25
- package/src/cborg/test/test-length.js +3 -3
- package/src/cborg/test/test-partial.js +2 -2
- package/src/cborg/tsconfig.json +5 -2
- package/src/cborg/types/{lib → cborg}/decode.d.ts +5 -3
- package/src/cborg/types/cborg/decode.d.ts.map +1 -0
- package/src/cborg/types/{lib → cborg}/encode.d.ts +4 -3
- package/src/cborg/types/cborg/encode.d.ts.map +1 -0
- package/src/cborg/types/cborg/index.d.ts +26 -0
- package/src/cborg/types/cborg/index.d.ts.map +1 -0
- package/src/cborg/types/cborg/is.d.ts.map +1 -0
- package/src/cborg/types/interface.d.ts +6 -4
- package/src/cborg/types/interface.d.ts.map +1 -1
- package/src/cborg/types/{lib/json → json}/decode.d.ts +7 -7
- package/src/cborg/types/json/decode.d.ts.map +1 -0
- package/src/cborg/types/json/encode.d.ts +11 -0
- package/src/cborg/types/json/encode.d.ts.map +1 -0
- package/src/cborg/types/json/json.d.ts.map +1 -0
- package/src/cborg/types/taglib.d.ts +1 -1
- package/src/cborg/types/taglib.d.ts.map +1 -1
- package/src/cborg/types/tsconfig.tsbuildinfo +1 -1
- package/src/cborg/types/{lib → utils}/0uint.d.ts +6 -4
- package/src/cborg/types/utils/0uint.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/1negint.d.ts +4 -3
- package/src/cborg/types/utils/1negint.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/2bytes.d.ts +5 -4
- package/src/cborg/types/utils/2bytes.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/3string.d.ts +1 -1
- package/src/cborg/types/utils/3string.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/4array.d.ts +3 -2
- package/src/cborg/types/utils/4array.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/5map.d.ts +3 -2
- package/src/cborg/types/utils/5map.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/6tag.d.ts +4 -3
- package/src/cborg/types/utils/6tag.d.ts.map +1 -0
- package/src/cborg/types/{lib → utils}/7float.d.ts +2 -2
- package/src/cborg/types/utils/7float.d.ts.map +1 -0
- package/src/cborg/types/utils/bl.d.ts.map +1 -0
- package/src/cborg/types/utils/byte-utils.d.ts.map +1 -0
- package/src/cborg/types/utils/common.d.ts +8 -0
- package/src/cborg/types/utils/common.d.ts.map +1 -0
- package/src/cborg/types/utils/index.d.ts +13 -0
- package/src/cborg/types/utils/index.d.ts.map +1 -0
- package/src/cborg/types/utils/jump.d.ts +16 -0
- package/src/cborg/types/utils/jump.d.ts.map +1 -0
- package/src/cborg/types/utils/token.d.ts.map +1 -0
- package/src/cborg/{lib → utils}/0uint.js +16 -14
- package/src/cborg/{lib → utils}/1negint.js +5 -5
- package/src/cborg/{lib → utils}/2bytes.js +16 -14
- package/src/cborg/{lib → utils}/3string.js +4 -4
- package/src/cborg/{lib → utils}/4array.js +7 -6
- package/src/cborg/{lib → utils}/5map.js +6 -6
- package/src/cborg/{lib → utils}/6tag.js +4 -3
- package/src/cborg/{lib → utils}/7float.js +23 -21
- package/src/cborg/{lib → utils}/bl.js +1 -1
- package/src/cborg/utils/common.js +11 -0
- package/src/cborg/utils/index.js +12 -0
- package/src/cborg/utils/jump.js +222 -0
- package/src/@ipld/car/package.json +0 -235
- package/src/@ipld/dag-cbor/package.json +0 -171
- package/src/@ipld/dag-json/package.json +0 -173
- package/src/@web3-storage/pail/package.json +0 -173
- package/src/cborg/lib/common.js +0 -27
- package/src/cborg/lib/jump.js +0 -209
- package/src/cborg/package.json +0 -168
- package/src/cborg/types/cborg.d.ts +0 -28
- package/src/cborg/types/cborg.d.ts.map +0 -1
- package/src/cborg/types/lib/0uint.d.ts.map +0 -1
- package/src/cborg/types/lib/1negint.d.ts.map +0 -1
- package/src/cborg/types/lib/2bytes.d.ts.map +0 -1
- package/src/cborg/types/lib/3string.d.ts.map +0 -1
- package/src/cborg/types/lib/4array.d.ts.map +0 -1
- package/src/cborg/types/lib/5map.d.ts.map +0 -1
- package/src/cborg/types/lib/6tag.d.ts.map +0 -1
- package/src/cborg/types/lib/7float.d.ts.map +0 -1
- package/src/cborg/types/lib/bin.d.ts +0 -4
- package/src/cborg/types/lib/bin.d.ts.map +0 -1
- package/src/cborg/types/lib/bl.d.ts.map +0 -1
- package/src/cborg/types/lib/byte-utils.d.ts.map +0 -1
- package/src/cborg/types/lib/common.d.ts +0 -10
- package/src/cborg/types/lib/common.d.ts.map +0 -1
- package/src/cborg/types/lib/decode.d.ts.map +0 -1
- package/src/cborg/types/lib/diagnostic.d.ts +0 -12
- package/src/cborg/types/lib/diagnostic.d.ts.map +0 -1
- package/src/cborg/types/lib/diagnostic_test.d.ts +0 -2
- package/src/cborg/types/lib/diagnostic_test.d.ts.map +0 -1
- package/src/cborg/types/lib/encode.d.ts.map +0 -1
- package/src/cborg/types/lib/is.d.ts.map +0 -1
- package/src/cborg/types/lib/json/decode.d.ts.map +0 -1
- package/src/cborg/types/lib/json/encode.d.ts +0 -11
- package/src/cborg/types/lib/json/encode.d.ts.map +0 -1
- package/src/cborg/types/lib/json/forward-cborg.d.ts +0 -6
- package/src/cborg/types/lib/json/forward-cborg.d.ts.map +0 -1
- package/src/cborg/types/lib/json/json.d.ts.map +0 -1
- package/src/cborg/types/lib/jump.d.ts +0 -12
- package/src/cborg/types/lib/jump.d.ts.map +0 -1
- package/src/cborg/types/lib/length.d.ts +0 -27
- package/src/cborg/types/lib/length.d.ts.map +0 -1
- package/src/cborg/types/lib/token.d.ts.map +0 -1
- package/src/ipfs-unixfs-exporter/package.json +0 -180
- /package/src/cborg/{lib → cborg}/is.js +0 -0
- /package/src/cborg/{lib/json → json}/json.js +0 -0
- /package/src/cborg/types/{lib → cborg}/is.d.ts +0 -0
- /package/src/cborg/types/{lib/json → json}/json.d.ts +0 -0
- /package/src/cborg/types/{lib → utils}/bl.d.ts +0 -0
- /package/src/cborg/types/{lib → utils}/byte-utils.d.ts +0 -0
- /package/src/cborg/types/{lib → utils}/token.d.ts +0 -0
- /package/src/cborg/{lib → utils}/byte-utils.js +0 -0
- /package/src/cborg/{lib → utils}/token.js +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../src/reader.js"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../src/reader.js"],"names":[],"mappings":";AA6BA;;;GAGG;AACH;IACE;;;;;;;;;;;;;;;;;;;OAmBG;IACH,mBARW,GAAG,QAAQ,CAAC,UAAU,GAAG,MAAM,cAG/B,UAAU,GAGR,QAAQ,KAAK,CAAC,CAiB1B;CACF;AAED,8BAA4B;oBAnEf,OAAO,OAAO,EAAE,KAAK;yBACrB,OAAO,OAAO,EAAE,UAAU;6BAC1B,OAAO,OAAO,EAAE,SAAS;8CALQ,qBAAqB;eAFpD,IAAI"}
|
|
@@ -8,7 +8,24 @@ import { CarReader as BrowserCarReader } from './reader-browser.js'
|
|
|
8
8
|
* @typedef {import('./api').CarReader} CarReaderIface
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @description not happy with typing here, but it's needed for the `promisify(fs.read)` function.
|
|
13
|
+
* @type {any}
|
|
14
|
+
*/
|
|
15
|
+
let _fsReadFn
|
|
16
|
+
/**
|
|
17
|
+
* @description This function is needed not to initialize the `fs.read` on load time. To run in cf workers without polyfill.
|
|
18
|
+
* @param {number} fd
|
|
19
|
+
* @param {Uint8Array} buffer
|
|
20
|
+
* @param {number} offset
|
|
21
|
+
* @param {number} length
|
|
22
|
+
* @param {number} position
|
|
23
|
+
* @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
|
|
24
|
+
*/
|
|
25
|
+
function fsread (fd, buffer, offset, length, position) {
|
|
26
|
+
_fsReadFn = _fsReadFn || promisify(fs.read)
|
|
27
|
+
return _fsReadFn(fd, buffer, offset, length, position)
|
|
28
|
+
}
|
|
12
29
|
|
|
13
30
|
/**
|
|
14
31
|
* @class
|
|
@@ -53,4 +70,4 @@ export class CarReader extends BrowserCarReader {
|
|
|
53
70
|
}
|
|
54
71
|
}
|
|
55
72
|
|
|
56
|
-
export const __browser =
|
|
73
|
+
export const __browser = !fs
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: 'github-actions'
|
|
4
|
+
directory: '/'
|
|
5
|
+
schedule:
|
|
6
|
+
interval: 'daily'
|
|
7
|
+
commit-message:
|
|
8
|
+
prefix: 'chore'
|
|
9
|
+
include: 'scope'
|
|
10
|
+
- package-ecosystem: 'npm'
|
|
11
|
+
directory: '/'
|
|
12
|
+
schedule:
|
|
13
|
+
interval: 'daily'
|
|
14
|
+
commit-message:
|
|
15
|
+
prefix: 'chore'
|
|
16
|
+
include: 'scope'
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
name: Test & Maybe Release
|
|
2
|
+
on: [push, pull_request]
|
|
3
|
+
jobs:
|
|
4
|
+
test:
|
|
5
|
+
strategy:
|
|
6
|
+
fail-fast: false
|
|
7
|
+
matrix:
|
|
8
|
+
node: [18.x, 20.x, lts/*, current]
|
|
9
|
+
os: [macos-latest, ubuntu-latest, windows-latest]
|
|
10
|
+
runs-on: ${{ matrix.os }}
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout Repository
|
|
13
|
+
uses: actions/checkout@v4
|
|
14
|
+
- name: Use Node.js ${{ matrix.node }}
|
|
15
|
+
uses: actions/setup-node@v4.1.0
|
|
16
|
+
with:
|
|
17
|
+
node-version: ${{ matrix.node }}
|
|
18
|
+
- name: Install Dependencies
|
|
19
|
+
run: |
|
|
20
|
+
npm install --no-progress
|
|
21
|
+
- name: Run tests
|
|
22
|
+
run: |
|
|
23
|
+
npm config set script-shell bash
|
|
24
|
+
npm run test:ci
|
|
25
|
+
- name: Typecheck
|
|
26
|
+
uses: gozala/typescript-error-reporter-action@v1.0.9
|
|
27
|
+
release:
|
|
28
|
+
name: Release
|
|
29
|
+
needs: test
|
|
30
|
+
runs-on: ubuntu-latest
|
|
31
|
+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
|
32
|
+
steps:
|
|
33
|
+
- name: Checkout
|
|
34
|
+
uses: actions/checkout@v4
|
|
35
|
+
with:
|
|
36
|
+
fetch-depth: 0
|
|
37
|
+
- name: Setup Node.js
|
|
38
|
+
uses: actions/setup-node@v4.1.0
|
|
39
|
+
with:
|
|
40
|
+
node-version: lts/*
|
|
41
|
+
- name: Install dependencies
|
|
42
|
+
run: |
|
|
43
|
+
npm install --no-progress --no-package-lock --no-save
|
|
44
|
+
- name: Build
|
|
45
|
+
run: |
|
|
46
|
+
npm run build
|
|
47
|
+
- name: Release
|
|
48
|
+
env:
|
|
49
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
50
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
51
|
+
run: npx semantic-release
|
|
52
|
+
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import process from 'process'
|
|
4
|
-
import { decode, encode } from
|
|
4
|
+
import { decode, encode } from "@fireproof/vendor/cborg"
|
|
5
5
|
import { tokensToDiagnostic, fromDiag } from './diagnostic.js'
|
|
6
|
-
import { fromHex as _fromHex, toHex } from '
|
|
6
|
+
import { fromHex as _fromHex, toHex } from 'cborg/utils'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @param {number} code
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// const decodeErrPrefix = 'CBOR decode error:'
|
|
2
|
+
// const encodeErrPrefix = 'CBOR encode error:'
|
|
3
|
+
|
|
4
|
+
const uintMinorPrefixBytes = []
|
|
5
|
+
uintMinorPrefixBytes[23] = 1
|
|
6
|
+
uintMinorPrefixBytes[24] = 2
|
|
7
|
+
uintMinorPrefixBytes[25] = 3
|
|
8
|
+
uintMinorPrefixBytes[26] = 5
|
|
9
|
+
uintMinorPrefixBytes[27] = 9
|
|
10
|
+
|
|
11
|
+
// /**
|
|
12
|
+
// * @param {Uint8Array} data
|
|
13
|
+
// * @param {number} pos
|
|
14
|
+
// * @param {number} need
|
|
15
|
+
// * @param {string} decodeErrPrefix
|
|
16
|
+
// */
|
|
17
|
+
// function assertEnoughData (data, pos, need, decodeErrPrefix) {
|
|
18
|
+
// if (data.length - pos < need) {
|
|
19
|
+
// throw new Error(`${decodeErrPrefix} not enough data for type`)
|
|
20
|
+
// }
|
|
21
|
+
// }
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
// decodeErrPrefix,
|
|
25
|
+
// encodeErrPrefix,
|
|
26
|
+
uintMinorPrefixBytes
|
|
27
|
+
// assertEnoughData
|
|
28
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Type } from './token.js'
|
|
3
|
-
import { jump, quick } from './jump.js'
|
|
1
|
+
import { jump, quick, Type } from 'cborg/utils'
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
|
-
* @typedef {import('
|
|
4
|
+
* @typedef {import('cborg/utils').Token} Token
|
|
7
5
|
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
8
6
|
* @typedef {import('../interface').DecodeTokenizer} DecodeTokenizer
|
|
7
|
+
* @typedef {import('../interface').DecodeFunction} DecodeFunction
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
|
-
const defaultDecodeOptions = {
|
|
10
|
+
const /** @type {DecodeOptions} */defaultDecodeOptions = {
|
|
12
11
|
strict: false,
|
|
13
12
|
allowIndefinite: true,
|
|
14
13
|
allowUndefined: true,
|
|
15
|
-
allowBigInt: true
|
|
14
|
+
allowBigInt: true,
|
|
15
|
+
decodeErrPrefix: 'CBOR decode error:'
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -23,10 +23,11 @@ class Tokeniser {
|
|
|
23
23
|
* @param {Uint8Array} data
|
|
24
24
|
* @param {DecodeOptions} options
|
|
25
25
|
*/
|
|
26
|
-
constructor (data, options
|
|
26
|
+
constructor (data, options) {
|
|
27
27
|
this._pos = 0
|
|
28
28
|
this.data = data
|
|
29
29
|
this.options = options
|
|
30
|
+
this.jump = jump(options.decodeErrPrefix)
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
pos () {
|
|
@@ -41,11 +42,11 @@ class Tokeniser {
|
|
|
41
42
|
const byt = this.data[this._pos]
|
|
42
43
|
let token = quick[byt]
|
|
43
44
|
if (token === undefined) {
|
|
44
|
-
const decoder = jump[byt]
|
|
45
|
+
const decoder = this.jump[byt]
|
|
45
46
|
/* c8 ignore next 4 */
|
|
46
47
|
// if we're here then there's something wrong with our jump or quick lists!
|
|
47
48
|
if (!decoder) {
|
|
48
|
-
throw new Error(`${decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, '0')})`)
|
|
49
|
+
throw new Error(`${this.options.decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, '0')})`)
|
|
49
50
|
}
|
|
50
51
|
const minor = byt & 31
|
|
51
52
|
token = decoder(this.data, this._pos, minor, this.options)
|
|
@@ -74,10 +75,10 @@ function tokenToArray (token, tokeniser, options) {
|
|
|
74
75
|
// normal end to indefinite length array
|
|
75
76
|
break
|
|
76
77
|
}
|
|
77
|
-
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed array`)
|
|
78
|
+
throw new Error(`${options.decodeErrPrefix} got unexpected break to lengthed array`)
|
|
78
79
|
}
|
|
79
80
|
if (value === DONE) {
|
|
80
|
-
throw new Error(`${decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`)
|
|
81
|
+
throw new Error(`${options.decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`)
|
|
81
82
|
}
|
|
82
83
|
arr[i] = value
|
|
83
84
|
}
|
|
@@ -101,23 +102,23 @@ function tokenToMap (token, tokeniser, options) {
|
|
|
101
102
|
// normal end to indefinite length map
|
|
102
103
|
break
|
|
103
104
|
}
|
|
104
|
-
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`)
|
|
105
|
+
throw new Error(`${options.decodeErrPrefix} got unexpected break to lengthed map`)
|
|
105
106
|
}
|
|
106
107
|
if (key === DONE) {
|
|
107
|
-
throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`)
|
|
108
|
+
throw new Error(`${options.decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`)
|
|
108
109
|
}
|
|
109
110
|
if (useMaps !== true && typeof key !== 'string') {
|
|
110
|
-
throw new Error(`${decodeErrPrefix} non-string keys not supported (got ${typeof key})`)
|
|
111
|
+
throw new Error(`${options.decodeErrPrefix} non-string keys not supported (got ${typeof key})`)
|
|
111
112
|
}
|
|
112
113
|
if (options.rejectDuplicateMapKeys === true) {
|
|
113
114
|
// @ts-ignore
|
|
114
115
|
if ((useMaps && m.has(key)) || (!useMaps && (key in obj))) {
|
|
115
|
-
throw new Error(`${decodeErrPrefix} found repeat map key "${key}"`)
|
|
116
|
+
throw new Error(`${options.decodeErrPrefix} found repeat map key "${key}"`)
|
|
116
117
|
}
|
|
117
118
|
}
|
|
118
119
|
const value = tokensToObject(tokeniser, options)
|
|
119
120
|
if (value === DONE) {
|
|
120
|
-
throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`)
|
|
121
|
+
throw new Error(`${options.decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`)
|
|
121
122
|
}
|
|
122
123
|
if (useMaps) {
|
|
123
124
|
// @ts-ignore TODO reconsider this .. maybe needs to be strict about key types
|
|
@@ -166,7 +167,7 @@ function tokensToObject (tokeniser, options) {
|
|
|
166
167
|
const tagged = tokensToObject(tokeniser, options)
|
|
167
168
|
return options.tags[token.value](tagged)
|
|
168
169
|
}
|
|
169
|
-
throw new Error(`${decodeErrPrefix} tag not supported (${token.value})`)
|
|
170
|
+
throw new Error(`${options.decodeErrPrefix} tag not supported (${token.value})`)
|
|
170
171
|
}
|
|
171
172
|
/* c8 ignore next */
|
|
172
173
|
throw new Error('unsupported')
|
|
@@ -178,17 +179,17 @@ function tokensToObject (tokeniser, options) {
|
|
|
178
179
|
* @returns {[any, Uint8Array]}
|
|
179
180
|
*/
|
|
180
181
|
function decodeFirst (data, options) {
|
|
182
|
+
options = Object.assign({}, defaultDecodeOptions, options)
|
|
181
183
|
if (!(data instanceof Uint8Array)) {
|
|
182
|
-
throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`)
|
|
184
|
+
throw new Error(`${options.decodeErrPrefix} data to decode must be a Uint8Array`)
|
|
183
185
|
}
|
|
184
|
-
options = Object.assign({}, defaultDecodeOptions, options)
|
|
185
186
|
const tokeniser = options.tokenizer || new Tokeniser(data, options)
|
|
186
187
|
const decoded = tokensToObject(tokeniser, options)
|
|
187
188
|
if (decoded === DONE) {
|
|
188
|
-
throw new Error(`${decodeErrPrefix} did not find any content to decode`)
|
|
189
|
+
throw new Error(`${options.decodeErrPrefix} did not find any content to decode`)
|
|
189
190
|
}
|
|
190
191
|
if (decoded === BREAK) {
|
|
191
|
-
throw new Error(`${decodeErrPrefix} got unexpected break`)
|
|
192
|
+
throw new Error(`${options.decodeErrPrefix} got unexpected break`)
|
|
192
193
|
}
|
|
193
194
|
return [decoded, data.subarray(tokeniser.pos())]
|
|
194
195
|
}
|
|
@@ -199,9 +200,10 @@ function decodeFirst (data, options) {
|
|
|
199
200
|
* @returns {any}
|
|
200
201
|
*/
|
|
201
202
|
function decode (data, options) {
|
|
203
|
+
options = Object.assign({}, defaultDecodeOptions, options)
|
|
202
204
|
const [decoded, remainder] = decodeFirst(data, options)
|
|
203
205
|
if (remainder.length > 0) {
|
|
204
|
-
throw new Error(`${decodeErrPrefix} too many terminals, data makes no sense`)
|
|
206
|
+
throw new Error(`${options.decodeErrPrefix} too many terminals, data makes no sense:${remainder.length}`)
|
|
205
207
|
}
|
|
206
208
|
return decoded
|
|
207
209
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Tokeniser } from './decode.js'
|
|
2
|
-
import { Type } from '
|
|
3
|
-
import { toHex, fromHex } from './byte-utils.js'
|
|
4
|
-
import { uintBoundaries } from './0uint.js'
|
|
2
|
+
import { uintBoundaries, toHex, fromHex, Type } from 'cborg/utils'
|
|
5
3
|
|
|
6
4
|
const utf8Encoder = new TextEncoder()
|
|
7
5
|
const utf8Decoder = new TextDecoder()
|
|
@@ -11,7 +9,11 @@ const utf8Decoder = new TextDecoder()
|
|
|
11
9
|
* @param {number} [width]
|
|
12
10
|
*/
|
|
13
11
|
function * tokensToDiagnostic (inp, width = 100) {
|
|
14
|
-
const tokeniser = new Tokeniser(inp, {
|
|
12
|
+
const tokeniser = new Tokeniser(inp, {
|
|
13
|
+
decodeErrPrefix: 'Diagnostic decode error: ',
|
|
14
|
+
retainStringBytes: true,
|
|
15
|
+
allowBigInt: true
|
|
16
|
+
})
|
|
15
17
|
let pos = 0
|
|
16
18
|
const indent = []
|
|
17
19
|
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import { is } from './is.js'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
import { encodeFloat } from './7float.js'
|
|
2
|
+
import {
|
|
3
|
+
Token, Type, Bl,
|
|
4
|
+
quickEncodeToken,
|
|
5
|
+
asU8A,
|
|
6
|
+
encodeUint,
|
|
7
|
+
encodeNegint,
|
|
8
|
+
encodeBytes,
|
|
9
|
+
encodeString,
|
|
10
|
+
encodeArray,
|
|
11
|
+
encodeMap,
|
|
12
|
+
encodeTag,
|
|
13
|
+
encodeFloat
|
|
14
|
+
} from 'cborg/utils'
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
17
|
* @typedef {import('../interface').EncodeOptions} EncodeOptions
|
|
@@ -27,7 +26,9 @@ import { encodeFloat } from './7float.js'
|
|
|
27
26
|
const defaultEncodeOptions = {
|
|
28
27
|
float64: false,
|
|
29
28
|
mapSorter,
|
|
30
|
-
quickEncodeToken
|
|
29
|
+
quickEncodeToken,
|
|
30
|
+
encodeErrPrefix: 'CBOR encode error:'
|
|
31
|
+
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
/** @returns {TokenTypeEncoder[]} */
|
|
@@ -77,9 +78,10 @@ class Ref {
|
|
|
77
78
|
/**
|
|
78
79
|
* @param {Reference|undefined} stack
|
|
79
80
|
* @param {object|any[]} obj
|
|
81
|
+
* @param {EncodeOptions} options
|
|
80
82
|
* @returns {Reference}
|
|
81
83
|
*/
|
|
82
|
-
static createCheck (stack, obj) {
|
|
84
|
+
static createCheck (stack, obj, { encodeErrPrefix }) {
|
|
83
85
|
if (stack && stack.includes(obj)) {
|
|
84
86
|
throw new Error(`${encodeErrPrefix} object contains circular references`)
|
|
85
87
|
}
|
|
@@ -221,7 +223,7 @@ const typeEncoders = {
|
|
|
221
223
|
}
|
|
222
224
|
return simpleTokens.emptyArray
|
|
223
225
|
}
|
|
224
|
-
refStack = Ref.createCheck(refStack, obj)
|
|
226
|
+
refStack = Ref.createCheck(refStack, obj, options)
|
|
225
227
|
const entries = []
|
|
226
228
|
let i = 0
|
|
227
229
|
for (const e of obj) {
|
|
@@ -252,7 +254,7 @@ const typeEncoders = {
|
|
|
252
254
|
}
|
|
253
255
|
return simpleTokens.emptyMap
|
|
254
256
|
}
|
|
255
|
-
refStack = Ref.createCheck(refStack, obj)
|
|
257
|
+
refStack = Ref.createCheck(refStack, obj, options)
|
|
256
258
|
/** @type {TokenOrNestedTokens[]} */
|
|
257
259
|
const entries = []
|
|
258
260
|
let i = 0
|
|
@@ -278,11 +280,11 @@ for (const typ of 'Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt6
|
|
|
278
280
|
|
|
279
281
|
/**
|
|
280
282
|
* @param {any} obj
|
|
281
|
-
* @param {EncodeOptions}
|
|
283
|
+
* @param {EncodeOptions} options
|
|
282
284
|
* @param {Reference} [refStack]
|
|
283
285
|
* @returns {TokenOrNestedTokens}
|
|
284
286
|
*/
|
|
285
|
-
function objectToTokens (obj, options
|
|
287
|
+
function objectToTokens (obj, options, refStack) {
|
|
286
288
|
const typ = is(obj)
|
|
287
289
|
const customTypeEncoder = (options && options.typeEncoders && /** @type {OptionalTypeEncoder} */ options.typeEncoders[typ]) || typeEncoders[typ]
|
|
288
290
|
if (typeof customTypeEncoder === 'function') {
|
|
@@ -293,7 +295,7 @@ function objectToTokens (obj, options = {}, refStack) {
|
|
|
293
295
|
}
|
|
294
296
|
const typeEncoder = typeEncoders[typ]
|
|
295
297
|
if (!typeEncoder) {
|
|
296
|
-
throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`)
|
|
298
|
+
throw new Error(`${options.encodeErrPrefix} unsupported type: ${typ}`)
|
|
297
299
|
}
|
|
298
300
|
return typeEncoder(obj, typ, options, refStack)
|
|
299
301
|
}
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import { encode } from './
|
|
2
|
-
import { decode, decodeFirst, Tokeniser, tokensToObject } from './
|
|
3
|
-
import { Token, Type } from '
|
|
1
|
+
import { encode } from './encode.js'
|
|
2
|
+
import { decode, decodeFirst, Tokeniser, tokensToObject } from './decode.js'
|
|
3
|
+
import { Token, Type } from 'cborg/utils'
|
|
4
4
|
|
|
5
5
|
// is this needed for the json module and other independ encoders
|
|
6
|
-
export { encodeCustom } from './
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
6
|
+
export { encodeCustom } from './encode.js'
|
|
7
|
+
// export { asU8A, fromString, decodeCodePointsArray } from './utils/byte-utils.js'
|
|
8
|
+
// export { quickEncodeToken } from './utils/jump.js'
|
|
9
|
+
// export { encodeErrPrefix, decodeErrPrefix } from './lib/common.js'
|
|
10
|
+
// export { makeCborEncoders, objectToTokens } from './lib/encode.js'
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Export the types that were present in the original manual cborg.d.ts
|
|
12
|
-
* @typedef {import('
|
|
14
|
+
* @typedef {import('../interface').TagDecoder} TagDecoder
|
|
13
15
|
* There was originally just `TypeEncoder` so don't break types by renaming or not exporting
|
|
14
|
-
* @typedef {import('
|
|
15
|
-
* @typedef {import('
|
|
16
|
-
* @typedef {import('
|
|
16
|
+
* @typedef {import('../interface').OptionalTypeEncoder} TypeEncoder
|
|
17
|
+
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
18
|
+
* @typedef {import('../interface').EncodeOptions} EncodeOptions
|
|
17
19
|
*/
|
|
18
20
|
|
|
19
21
|
export {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { makeCborEncoders, objectToTokens } from './encode.js'
|
|
2
|
-
import { quickEncodeToken } from '
|
|
2
|
+
import { quickEncodeToken } from 'cborg/utils'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {import('../interface').EncodeOptions} EncodeOptions
|
|
@@ -12,7 +12,8 @@ const cborEncoders = makeCborEncoders()
|
|
|
12
12
|
/** @type {EncodeOptions} */
|
|
13
13
|
const defaultEncodeOptions = {
|
|
14
14
|
float64: false,
|
|
15
|
-
quickEncodeToken
|
|
15
|
+
quickEncodeToken,
|
|
16
|
+
encodeErrPrefix: 'CBOR encode error: '
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
/**
|
package/src/cborg/interface.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Token } from '
|
|
2
|
-
import { Bl } from './lib/bl'
|
|
1
|
+
import type { Token, Bl } from 'cborg/utils'
|
|
3
2
|
|
|
4
3
|
export type TokenOrNestedTokens = Token | Token[] | TokenOrNestedTokens[]
|
|
5
4
|
|
|
@@ -14,12 +13,14 @@ export type OptionalTypeEncoder = (data: any, typ: string, options: EncodeOption
|
|
|
14
13
|
export type StrictTypeEncoder = (data: any, typ: string, options: EncodeOptions, refStack?: Reference) => TokenOrNestedTokens
|
|
15
14
|
|
|
16
15
|
export type TokenTypeEncoder = {
|
|
17
|
-
(buf: Bl, token: Token, options
|
|
16
|
+
(buf: Bl, token: Token, options: EncodeOptions): void;
|
|
18
17
|
compareTokens(t1: Token, t2: Token): number;
|
|
19
18
|
// TODO: make this non-optional as a breaking change and remove the throw in length.js
|
|
20
|
-
encodedSize?(token: Token, options
|
|
19
|
+
encodedSize?(token: Token, options: EncodeOptions): number;
|
|
21
20
|
}
|
|
22
21
|
|
|
22
|
+
export type DecodeFunction = (data:Uint8Array, pos:number, minor:number, options:DecodeOptions) => any
|
|
23
|
+
|
|
23
24
|
export type MapSorter = (e1: (Token | Token[])[], e2: (Token | Token[])[]) => number
|
|
24
25
|
|
|
25
26
|
export type QuickEncodeToken = (token: Token) => Uint8Array | undefined
|
|
@@ -45,6 +46,7 @@ export interface DecodeOptions {
|
|
|
45
46
|
retainStringBytes?: boolean
|
|
46
47
|
tags?: TagDecoder[],
|
|
47
48
|
tokenizer?: DecodeTokenizer
|
|
49
|
+
decodeErrPrefix: string
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
export interface EncodeOptions {
|
|
@@ -53,4 +55,5 @@ export interface EncodeOptions {
|
|
|
53
55
|
mapSorter?: MapSorter,
|
|
54
56
|
quickEncodeToken?: QuickEncodeToken,
|
|
55
57
|
typeEncoders?: { [typeName: string]: OptionalTypeEncoder }
|
|
58
|
+
encodeErrPrefix: string
|
|
56
59
|
}
|