@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,11 +1,11 @@
|
|
|
1
1
|
import { Token, Type } from './token.js'
|
|
2
|
-
import { assertEnoughData
|
|
2
|
+
import { assertEnoughData } from './common.js'
|
|
3
3
|
import * as uint from './0uint.js'
|
|
4
4
|
import { compare, fromString, slice } from './byte-utils.js'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @typedef {import('./bl.js').Bl} Bl
|
|
8
|
-
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
8
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -13,10 +13,11 @@ import { compare, fromString, slice } from './byte-utils.js'
|
|
|
13
13
|
* @param {number} pos
|
|
14
14
|
* @param {number} prefix
|
|
15
15
|
* @param {number} length
|
|
16
|
+
* @param {string} decodeErrPrefix
|
|
16
17
|
* @returns {Token}
|
|
17
18
|
*/
|
|
18
|
-
function toToken (data, pos, prefix, length) {
|
|
19
|
-
assertEnoughData(data, pos, prefix + length)
|
|
19
|
+
function toToken (data, pos, prefix, length, decodeErrPrefix) {
|
|
20
|
+
assertEnoughData(data, pos, prefix + length, decodeErrPrefix)
|
|
20
21
|
const buf = slice(data, pos + prefix, pos + prefix + length)
|
|
21
22
|
return new Token(Type.bytes, buf, prefix + length)
|
|
22
23
|
}
|
|
@@ -25,11 +26,11 @@ function toToken (data, pos, prefix, length) {
|
|
|
25
26
|
* @param {Uint8Array} data
|
|
26
27
|
* @param {number} pos
|
|
27
28
|
* @param {number} minor
|
|
28
|
-
* @param {DecodeOptions}
|
|
29
|
+
* @param {DecodeOptions} options
|
|
29
30
|
* @returns {Token}
|
|
30
31
|
*/
|
|
31
|
-
export function decodeBytesCompact (data, pos, minor,
|
|
32
|
-
return toToken(data, pos, 1, minor)
|
|
32
|
+
export function decodeBytesCompact (data, pos, minor, options) {
|
|
33
|
+
return toToken(data, pos, 1, minor, options.decodeErrPrefix)
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
/**
|
|
@@ -40,7 +41,7 @@ export function decodeBytesCompact (data, pos, minor, _options) {
|
|
|
40
41
|
* @returns {Token}
|
|
41
42
|
*/
|
|
42
43
|
export function decodeBytes8 (data, pos, _minor, options) {
|
|
43
|
-
return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options))
|
|
44
|
+
return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options), options.decodeErrPrefix)
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
/**
|
|
@@ -51,7 +52,7 @@ export function decodeBytes8 (data, pos, _minor, options) {
|
|
|
51
52
|
* @returns {Token}
|
|
52
53
|
*/
|
|
53
54
|
export function decodeBytes16 (data, pos, _minor, options) {
|
|
54
|
-
return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options))
|
|
55
|
+
return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options), options.decodeErrPrefix)
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
/**
|
|
@@ -62,7 +63,7 @@ export function decodeBytes16 (data, pos, _minor, options) {
|
|
|
62
63
|
* @returns {Token}
|
|
63
64
|
*/
|
|
64
65
|
export function decodeBytes32 (data, pos, _minor, options) {
|
|
65
|
-
return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options))
|
|
66
|
+
return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options), options.decodeErrPrefix)
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
// TODO: maybe we shouldn't support this ..
|
|
@@ -76,9 +77,9 @@ export function decodeBytes32 (data, pos, _minor, options) {
|
|
|
76
77
|
export function decodeBytes64 (data, pos, _minor, options) {
|
|
77
78
|
const l = uint.readUint64(data, pos + 1, options)
|
|
78
79
|
if (typeof l === 'bigint') {
|
|
79
|
-
throw new Error(`${decodeErrPrefix} 64-bit integer bytes lengths not supported`)
|
|
80
|
+
throw new Error(`${options.decodeErrPrefix} 64-bit integer bytes lengths not supported`)
|
|
80
81
|
}
|
|
81
|
-
return toToken(data, pos, 9, l)
|
|
82
|
+
return toToken(data, pos, 9, l, options.decodeErrPrefix)
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
/**
|
|
@@ -98,10 +99,11 @@ function tokenBytes (token) {
|
|
|
98
99
|
/**
|
|
99
100
|
* @param {Bl} buf
|
|
100
101
|
* @param {Token} token
|
|
102
|
+
* @param {import('../interface.js').EncodeOptions} options
|
|
101
103
|
*/
|
|
102
|
-
export function encodeBytes (buf, token) {
|
|
104
|
+
export function encodeBytes (buf, token, { encodeErrPrefix }) {
|
|
103
105
|
const bytes = tokenBytes(token)
|
|
104
|
-
uint.encodeUintValue(buf, token.type.majorEncoded, bytes.length)
|
|
106
|
+
uint.encodeUintValue(buf, token.type.majorEncoded, bytes.length, encodeErrPrefix)
|
|
105
107
|
buf.push(bytes)
|
|
106
108
|
}
|
|
107
109
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Token, Type } from './token.js'
|
|
2
|
-
import { assertEnoughData
|
|
2
|
+
import { assertEnoughData } from './common.js'
|
|
3
3
|
import * as uint from './0uint.js'
|
|
4
4
|
import { encodeBytes } from './2bytes.js'
|
|
5
5
|
import { toString, slice } from './byte-utils.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @typedef {import('./bl.js').Bl} Bl
|
|
9
|
-
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
9
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -19,7 +19,7 @@ import { toString, slice } from './byte-utils.js'
|
|
|
19
19
|
*/
|
|
20
20
|
function toToken (data, pos, prefix, length, options) {
|
|
21
21
|
const totLength = prefix + length
|
|
22
|
-
assertEnoughData(data, pos, totLength)
|
|
22
|
+
assertEnoughData(data, pos, totLength, options.decodeErrPrefix)
|
|
23
23
|
const tok = new Token(Type.string, toString(data, pos + prefix, pos + totLength), totLength)
|
|
24
24
|
if (options.retainStringBytes === true) {
|
|
25
25
|
tok.byteValue = slice(data, pos + prefix, pos + totLength)
|
|
@@ -82,7 +82,7 @@ export function decodeString32 (data, pos, _minor, options) {
|
|
|
82
82
|
export function decodeString64 (data, pos, _minor, options) {
|
|
83
83
|
const l = uint.readUint64(data, pos + 1, options)
|
|
84
84
|
if (typeof l === 'bigint') {
|
|
85
|
-
throw new Error(`${decodeErrPrefix} 64-bit integer string lengths not supported`)
|
|
85
|
+
throw new Error(`${options.decodeErrPrefix} 64-bit integer string lengths not supported`)
|
|
86
86
|
}
|
|
87
87
|
return toToken(data, pos, 9, l, options)
|
|
88
88
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Token, Type } from './token.js'
|
|
2
2
|
import * as uint from './0uint.js'
|
|
3
|
-
import { decodeErrPrefix } from '
|
|
3
|
+
// import { decodeErrPrefix } from '../lib/common.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @typedef {import('./bl.js').Bl} Bl
|
|
7
|
-
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
7
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -73,7 +73,7 @@ export function decodeArray32 (data, pos, _minor, options) {
|
|
|
73
73
|
export function decodeArray64 (data, pos, _minor, options) {
|
|
74
74
|
const l = uint.readUint64(data, pos + 1, options)
|
|
75
75
|
if (typeof l === 'bigint') {
|
|
76
|
-
throw new Error(`${decodeErrPrefix} 64-bit integer array lengths not supported`)
|
|
76
|
+
throw new Error(`${options.decodeErrPrefix} 64-bit integer array lengths not supported`)
|
|
77
77
|
}
|
|
78
78
|
return toToken(data, pos, 9, l)
|
|
79
79
|
}
|
|
@@ -87,7 +87,7 @@ export function decodeArray64 (data, pos, _minor, options) {
|
|
|
87
87
|
*/
|
|
88
88
|
export function decodeArrayIndefinite (data, pos, _minor, options) {
|
|
89
89
|
if (options.allowIndefinite === false) {
|
|
90
|
-
throw new Error(`${decodeErrPrefix} indefinite length items not allowed`)
|
|
90
|
+
throw new Error(`${options.decodeErrPrefix} indefinite length items not allowed`)
|
|
91
91
|
}
|
|
92
92
|
return toToken(data, pos, 1, Infinity)
|
|
93
93
|
}
|
|
@@ -95,9 +95,10 @@ export function decodeArrayIndefinite (data, pos, _minor, options) {
|
|
|
95
95
|
/**
|
|
96
96
|
* @param {Bl} buf
|
|
97
97
|
* @param {Token} token
|
|
98
|
+
* @param {import('../interface.js').EncodeOptions} options
|
|
98
99
|
*/
|
|
99
|
-
export function encodeArray (buf, token) {
|
|
100
|
-
uint.encodeUintValue(buf, Type.array.majorEncoded, token.value)
|
|
100
|
+
export function encodeArray (buf, token, { encodeErrPrefix }) {
|
|
101
|
+
uint.encodeUintValue(buf, Type.array.majorEncoded, token.value, encodeErrPrefix)
|
|
101
102
|
}
|
|
102
103
|
|
|
103
104
|
// using an array as a map key, are you sure about this? we can only sort
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Token, Type } from './token.js'
|
|
2
2
|
import * as uint from './0uint.js'
|
|
3
|
-
import { decodeErrPrefix } from './common.js'
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* @typedef {import('./bl.js').Bl} Bl
|
|
7
|
-
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
6
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
8
7
|
*/
|
|
9
8
|
|
|
10
9
|
/**
|
|
@@ -73,7 +72,7 @@ export function decodeMap32 (data, pos, _minor, options) {
|
|
|
73
72
|
export function decodeMap64 (data, pos, _minor, options) {
|
|
74
73
|
const l = uint.readUint64(data, pos + 1, options)
|
|
75
74
|
if (typeof l === 'bigint') {
|
|
76
|
-
throw new Error(`${decodeErrPrefix} 64-bit integer map lengths not supported`)
|
|
75
|
+
throw new Error(`${options.decodeErrPrefix} 64-bit integer map lengths not supported`)
|
|
77
76
|
}
|
|
78
77
|
return toToken(data, pos, 9, l)
|
|
79
78
|
}
|
|
@@ -87,7 +86,7 @@ export function decodeMap64 (data, pos, _minor, options) {
|
|
|
87
86
|
*/
|
|
88
87
|
export function decodeMapIndefinite (data, pos, _minor, options) {
|
|
89
88
|
if (options.allowIndefinite === false) {
|
|
90
|
-
throw new Error(`${decodeErrPrefix} indefinite length items not allowed`)
|
|
89
|
+
throw new Error(`${options.decodeErrPrefix} indefinite length items not allowed`)
|
|
91
90
|
}
|
|
92
91
|
return toToken(data, pos, 1, Infinity)
|
|
93
92
|
}
|
|
@@ -95,9 +94,10 @@ export function decodeMapIndefinite (data, pos, _minor, options) {
|
|
|
95
94
|
/**
|
|
96
95
|
* @param {Bl} buf
|
|
97
96
|
* @param {Token} token
|
|
97
|
+
* @param {import('../interface.js').EncodeOptions} options
|
|
98
98
|
*/
|
|
99
|
-
export function encodeMap (buf, token) {
|
|
100
|
-
uint.encodeUintValue(buf, Type.map.majorEncoded, token.value)
|
|
99
|
+
export function encodeMap (buf, token, { encodeErrPrefix }) {
|
|
100
|
+
uint.encodeUintValue(buf, Type.map.majorEncoded, token.value, encodeErrPrefix)
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
// using a map as a map key, are you sure about this? we can only sort
|
|
@@ -3,7 +3,7 @@ import * as uint from './0uint.js'
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {import('./bl.js').Bl} Bl
|
|
6
|
-
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
6
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -64,9 +64,10 @@ export function decodeTag64 (data, pos, _minor, options) {
|
|
|
64
64
|
/**
|
|
65
65
|
* @param {Bl} buf
|
|
66
66
|
* @param {Token} token
|
|
67
|
+
* @param {import('../interface.js').EncodeOptions} options
|
|
67
68
|
*/
|
|
68
|
-
export function encodeTag (buf, token) {
|
|
69
|
-
uint.encodeUintValue(buf, Type.tag.majorEncoded, token.value)
|
|
69
|
+
export function encodeTag (buf, token, { encodeErrPrefix }) {
|
|
70
|
+
uint.encodeUintValue(buf, Type.tag.majorEncoded, token.value, encodeErrPrefix)
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
encodeTag.compareTokens = uint.encodeUint.compareTokens
|
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
// where possible
|
|
3
3
|
|
|
4
4
|
import { Token, Type } from './token.js'
|
|
5
|
-
import { decodeErrPrefix } from './common.js'
|
|
6
5
|
import { encodeUint } from './0uint.js'
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* @typedef {import('./bl.js').Bl} Bl
|
|
10
|
-
* @typedef {import('../interface').DecodeOptions} DecodeOptions
|
|
11
|
-
* @typedef {import('../interface').EncodeOptions} EncodeOptions
|
|
9
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
10
|
+
* @typedef {import('../interface.js').EncodeOptions} EncodeOptions
|
|
12
11
|
*/
|
|
13
12
|
|
|
14
13
|
const MINOR_FALSE = 20
|
|
@@ -25,7 +24,7 @@ const MINOR_UNDEFINED = 23
|
|
|
25
24
|
*/
|
|
26
25
|
export function decodeUndefined (_data, _pos, _minor, options) {
|
|
27
26
|
if (options.allowUndefined === false) {
|
|
28
|
-
throw new Error(`${decodeErrPrefix} undefined values are not supported`)
|
|
27
|
+
throw new Error(`${options.decodeErrPrefix} undefined values are not supported`)
|
|
29
28
|
} else if (options.coerceUndefinedToNull === true) {
|
|
30
29
|
return new Token(Type.null, null, 1)
|
|
31
30
|
}
|
|
@@ -41,7 +40,7 @@ export function decodeUndefined (_data, _pos, _minor, options) {
|
|
|
41
40
|
*/
|
|
42
41
|
export function decodeBreak (_data, _pos, _minor, options) {
|
|
43
42
|
if (options.allowIndefinite === false) {
|
|
44
|
-
throw new Error(`${decodeErrPrefix} indefinite length items not allowed`)
|
|
43
|
+
throw new Error(`${options.decodeErrPrefix} indefinite length items not allowed`)
|
|
45
44
|
}
|
|
46
45
|
return new Token(Type.break, undefined, 1)
|
|
47
46
|
}
|
|
@@ -55,10 +54,10 @@ export function decodeBreak (_data, _pos, _minor, options) {
|
|
|
55
54
|
function createToken (value, bytes, options) {
|
|
56
55
|
if (options) {
|
|
57
56
|
if (options.allowNaN === false && Number.isNaN(value)) {
|
|
58
|
-
throw new Error(`${decodeErrPrefix} NaN values are not supported`)
|
|
57
|
+
throw new Error(`${options.decodeErrPrefix} NaN values are not supported`)
|
|
59
58
|
}
|
|
60
59
|
if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) {
|
|
61
|
-
throw new Error(`${decodeErrPrefix} Infinity values are not supported`)
|
|
60
|
+
throw new Error(`${options.decodeErrPrefix} Infinity values are not supported`)
|
|
62
61
|
}
|
|
63
62
|
}
|
|
64
63
|
return new Token(Type.float, value, bytes)
|
|
@@ -72,7 +71,7 @@ function createToken (value, bytes, options) {
|
|
|
72
71
|
* @returns {Token}
|
|
73
72
|
*/
|
|
74
73
|
export function decodeFloat16 (data, pos, _minor, options) {
|
|
75
|
-
return createToken(readFloat16(data, pos + 1), 3, options)
|
|
74
|
+
return createToken(readFloat16(data, pos + 1, options.decodeErrPrefix), 3, options)
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
/**
|
|
@@ -83,7 +82,7 @@ export function decodeFloat16 (data, pos, _minor, options) {
|
|
|
83
82
|
* @returns {Token}
|
|
84
83
|
*/
|
|
85
84
|
export function decodeFloat32 (data, pos, _minor, options) {
|
|
86
|
-
return createToken(readFloat32(data, pos + 1), 5, options)
|
|
85
|
+
return createToken(readFloat32(data, pos + 1, options.decodeErrPrefix), 5, options)
|
|
87
86
|
}
|
|
88
87
|
|
|
89
88
|
/**
|
|
@@ -94,7 +93,7 @@ export function decodeFloat32 (data, pos, _minor, options) {
|
|
|
94
93
|
* @returns {Token}
|
|
95
94
|
*/
|
|
96
95
|
export function decodeFloat64 (data, pos, _minor, options) {
|
|
97
|
-
return createToken(readFloat64(data, pos + 1), 9, options)
|
|
96
|
+
return createToken(readFloat64(data, pos + 1, options.decodeErrPrefix), 9, options)
|
|
98
97
|
}
|
|
99
98
|
|
|
100
99
|
/**
|
|
@@ -118,14 +117,14 @@ export function encodeFloat (buf, token, options) {
|
|
|
118
117
|
let success = false
|
|
119
118
|
if (!options || options.float64 !== true) {
|
|
120
119
|
encodeFloat16(float)
|
|
121
|
-
decoded = readFloat16(ui8a, 1)
|
|
120
|
+
decoded = readFloat16(ui8a, 1, options.encodeErrPrefix)
|
|
122
121
|
if (float === decoded || Number.isNaN(float)) {
|
|
123
122
|
ui8a[0] = 0xf9
|
|
124
123
|
buf.push(ui8a.slice(0, 3))
|
|
125
124
|
success = true
|
|
126
125
|
} else {
|
|
127
126
|
encodeFloat32(float)
|
|
128
|
-
decoded = readFloat32(ui8a, 1)
|
|
127
|
+
decoded = readFloat32(ui8a, 1, options.encodeErrPrefix)
|
|
129
128
|
if (float === decoded) {
|
|
130
129
|
ui8a[0] = 0xfa
|
|
131
130
|
buf.push(ui8a.slice(0, 5))
|
|
@@ -135,7 +134,7 @@ export function encodeFloat (buf, token, options) {
|
|
|
135
134
|
}
|
|
136
135
|
if (!success) {
|
|
137
136
|
encodeFloat64(float)
|
|
138
|
-
decoded = readFloat64(ui8a, 1)
|
|
137
|
+
decoded = readFloat64(ui8a, 1, options.encodeErrPrefix)
|
|
139
138
|
ui8a[0] = 0xfb
|
|
140
139
|
buf.push(ui8a.slice(0, 9))
|
|
141
140
|
}
|
|
@@ -156,12 +155,12 @@ encodeFloat.encodedSize = function encodedSize (token, options) {
|
|
|
156
155
|
|
|
157
156
|
if (!options || options.float64 !== true) {
|
|
158
157
|
encodeFloat16(float)
|
|
159
|
-
let decoded = readFloat16(ui8a, 1)
|
|
158
|
+
let decoded = readFloat16(ui8a, 1, options.encodeErrPrefix)
|
|
160
159
|
if (float === decoded || Number.isNaN(float)) {
|
|
161
160
|
return 3
|
|
162
161
|
}
|
|
163
162
|
encodeFloat32(float)
|
|
164
|
-
decoded = readFloat32(ui8a, 1)
|
|
163
|
+
decoded = readFloat32(ui8a, 1, options.encodeErrPrefix)
|
|
165
164
|
if (float === decoded) {
|
|
166
165
|
return 5
|
|
167
166
|
}
|
|
@@ -222,11 +221,12 @@ function encodeFloat16 (inp) {
|
|
|
222
221
|
/**
|
|
223
222
|
* @param {Uint8Array} ui8a
|
|
224
223
|
* @param {number} pos
|
|
224
|
+
* @param {string} encodeErrPrefix
|
|
225
225
|
* @returns {number}
|
|
226
226
|
*/
|
|
227
|
-
function readFloat16 (ui8a, pos) {
|
|
227
|
+
function readFloat16 (ui8a, pos, encodeErrPrefix) {
|
|
228
228
|
if (ui8a.length - pos < 2) {
|
|
229
|
-
throw new Error(`${
|
|
229
|
+
throw new Error(`${encodeErrPrefix} not enough data for float16`)
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
const half = (ui8a[pos] << 8) + ui8a[pos + 1]
|
|
@@ -264,11 +264,12 @@ function encodeFloat32 (inp) {
|
|
|
264
264
|
/**
|
|
265
265
|
* @param {Uint8Array} ui8a
|
|
266
266
|
* @param {number} pos
|
|
267
|
+
* @param {string} encodeErrPrefix
|
|
267
268
|
* @returns {number}
|
|
268
269
|
*/
|
|
269
|
-
function readFloat32 (ui8a, pos) {
|
|
270
|
+
function readFloat32 (ui8a, pos, encodeErrPrefix) {
|
|
270
271
|
if (ui8a.length - pos < 4) {
|
|
271
|
-
throw new Error(`${
|
|
272
|
+
throw new Error(`${encodeErrPrefix} not enough data for float32`)
|
|
272
273
|
}
|
|
273
274
|
const offset = (ui8a.byteOffset || 0) + pos
|
|
274
275
|
return new DataView(ui8a.buffer, offset, 4).getFloat32(0, false)
|
|
@@ -284,11 +285,12 @@ function encodeFloat64 (inp) {
|
|
|
284
285
|
/**
|
|
285
286
|
* @param {Uint8Array} ui8a
|
|
286
287
|
* @param {number} pos
|
|
288
|
+
* @param {string} encodeErrPrefix
|
|
287
289
|
* @returns {number}
|
|
288
290
|
*/
|
|
289
|
-
function readFloat64 (ui8a, pos) {
|
|
291
|
+
function readFloat64 (ui8a, pos, encodeErrPrefix) {
|
|
290
292
|
if (ui8a.length - pos < 8) {
|
|
291
|
-
throw new Error(`${
|
|
293
|
+
throw new Error(`${encodeErrPrefix} not enough data for float64`)
|
|
292
294
|
}
|
|
293
295
|
const offset = (ui8a.byteOffset || 0) + pos
|
|
294
296
|
return new DataView(ui8a.buffer, offset, 8).getFloat64(0, false)
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
// TODO: ipjs doesn't support this, only for test files: https://github.com/mikeal/ipjs/blob/master/src/package/testFile.js#L39
|
|
19
|
-
import { alloc, concat, slice } from '
|
|
19
|
+
import { alloc, concat, slice } from 'cborg/utils'
|
|
20
20
|
|
|
21
21
|
// the ts-ignores in this file are almost all for the `Uint8Array|number[]` duality that exists
|
|
22
22
|
// for perf reasons. Consider better approaches to this or removing it entirely, it is quite
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {Uint8Array} data
|
|
3
|
+
* @param {number} pos
|
|
4
|
+
* @param {number} need
|
|
5
|
+
* @param {string} decodeErrPrefix
|
|
6
|
+
*/
|
|
7
|
+
export function assertEnoughData (data, pos, need, decodeErrPrefix) {
|
|
8
|
+
if (data.length - pos < need) {
|
|
9
|
+
throw new Error(`${decodeErrPrefix} not enough data for type`)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './byte-utils.js'
|
|
2
|
+
export { encodeUint, uintBoundaries } from './0uint.js'
|
|
3
|
+
export { encodeNegint } from './1negint.js'
|
|
4
|
+
export { encodeBytes } from './2bytes.js'
|
|
5
|
+
export { encodeString } from './3string.js'
|
|
6
|
+
export { encodeArray } from './4array.js'
|
|
7
|
+
export { encodeMap } from './5map.js'
|
|
8
|
+
export { encodeTag } from './6tag.js'
|
|
9
|
+
export { encodeFloat } from './7float.js'
|
|
10
|
+
export { Bl } from './bl.js'
|
|
11
|
+
export { Token, Type } from './token.js'
|
|
12
|
+
export * from './jump.js'
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { Token, Type } from './token.js'
|
|
2
|
+
import * as uint from './0uint.js'
|
|
3
|
+
import * as negint from './1negint.js'
|
|
4
|
+
import * as bytes from './2bytes.js'
|
|
5
|
+
import * as string from './3string.js'
|
|
6
|
+
import * as array from './4array.js'
|
|
7
|
+
import * as map from './5map.js'
|
|
8
|
+
import * as tag from './6tag.js'
|
|
9
|
+
import * as float from './7float.js'
|
|
10
|
+
import { fromArray } from './byte-utils.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
14
|
+
* @typedef {import('../interface.js').DecodeFunction} DecodeFunction
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} decodeErrPrefix
|
|
19
|
+
* @returns {DecodeFunction}
|
|
20
|
+
*/
|
|
21
|
+
function invalidMinorWithPrefix (decodeErrPrefix) {
|
|
22
|
+
/**
|
|
23
|
+
* @param {Uint8Array} data
|
|
24
|
+
* @param {number} pos
|
|
25
|
+
* @param {number} minor
|
|
26
|
+
*/
|
|
27
|
+
return (data, pos, minor) => {
|
|
28
|
+
throw new Error(`${decodeErrPrefix} encountered invalid minor (${minor}) for major ${data[pos] >>> 5}`)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} msg
|
|
34
|
+
* @param {string} decodeErrPrefix
|
|
35
|
+
* @returns {()=>any}
|
|
36
|
+
*/
|
|
37
|
+
function errorer (msg, decodeErrPrefix) {
|
|
38
|
+
return () => { throw new Error(`${decodeErrPrefix} ${msg}`) }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {string} decodeErrPrefix
|
|
43
|
+
* @return {DecodeFunction[]}
|
|
44
|
+
*/
|
|
45
|
+
export function jump (decodeErrPrefix) {
|
|
46
|
+
const invalidMinor = invalidMinorWithPrefix(decodeErrPrefix)
|
|
47
|
+
const jump = Array(256).fill(invalidMinor)
|
|
48
|
+
// unsigned integer, 0x00..0x17 (0..23)
|
|
49
|
+
// for (let i = 0; i <= 0x17; i++) {
|
|
50
|
+
// jump[i] = invalidMinor // uint.decodeUintCompact, handled by quick[]
|
|
51
|
+
// }
|
|
52
|
+
jump[0x18] = uint.decodeUint8 // unsigned integer, one-byte uint8_t follows
|
|
53
|
+
jump[0x19] = uint.decodeUint16 // unsigned integer, two-byte uint16_t follows
|
|
54
|
+
jump[0x1a] = uint.decodeUint32 // unsigned integer, four-byte uint32_t follows
|
|
55
|
+
jump[0x1b] = uint.decodeUint64 // unsigned integer, eight-byte uint64_t follows
|
|
56
|
+
// jump[0x1c] = invalidMinor
|
|
57
|
+
// jump[0x1d] = invalidMinor
|
|
58
|
+
// jump[0x1e] = invalidMinor
|
|
59
|
+
// jump[0x1f] = invalidMinor
|
|
60
|
+
// negative integer, -1-0x00..-1-0x17 (-1..-24)
|
|
61
|
+
// for (let i = 0x20; i <= 0x37; i++) {
|
|
62
|
+
// jump[i] = invalidMinor // negintDecode, handled by quick[]
|
|
63
|
+
// }
|
|
64
|
+
jump[0x38] = negint.decodeNegint8 // negative integer, -1-n one-byte uint8_t for n follows
|
|
65
|
+
jump[0x39] = negint.decodeNegint16 // negative integer, -1-n two-byte uint16_t for n follows
|
|
66
|
+
jump[0x3a] = negint.decodeNegint32 // negative integer, -1-n four-byte uint32_t for follows
|
|
67
|
+
jump[0x3b] = negint.decodeNegint64 // negative integer, -1-n eight-byte uint64_t for follows
|
|
68
|
+
// jump[0x3c] = invalidMinor
|
|
69
|
+
// jump[0x3d] = invalidMinor
|
|
70
|
+
// jump[0x3e] = invalidMinor
|
|
71
|
+
// jump[0x3f] = invalidMinor
|
|
72
|
+
// byte string, 0x00..0x17 bytes follow
|
|
73
|
+
for (let i = 0x40; i <= 0x57; i++) {
|
|
74
|
+
jump[i] = bytes.decodeBytesCompact
|
|
75
|
+
}
|
|
76
|
+
jump[0x58] = bytes.decodeBytes8 // byte string, one-byte uint8_t for n, and then n bytes follow
|
|
77
|
+
jump[0x59] = bytes.decodeBytes16 // byte string, two-byte uint16_t for n, and then n bytes follow
|
|
78
|
+
jump[0x5a] = bytes.decodeBytes32 // byte string, four-byte uint32_t for n, and then n bytes follow
|
|
79
|
+
jump[0x5b] = bytes.decodeBytes64 // byte string, eight-byte uint64_t for n, and then n bytes follow
|
|
80
|
+
// jump[0x5c] = invalidMinor
|
|
81
|
+
// jump[0x5d] = invalidMinor
|
|
82
|
+
// jump[0x5e] = invalidMinor
|
|
83
|
+
jump[0x5f] = errorer('indefinite length bytes/strings are not supported', decodeErrPrefix) // byte string, byte strings follow, terminated by "break"
|
|
84
|
+
// UTF-8 string 0x00..0x17 bytes follow
|
|
85
|
+
for (let i = 0x60; i <= 0x77; i++) {
|
|
86
|
+
jump[i] = string.decodeStringCompact
|
|
87
|
+
}
|
|
88
|
+
jump[0x78] = string.decodeString8 // UTF-8 string, one-byte uint8_t for n, and then n bytes follow
|
|
89
|
+
jump[0x79] = string.decodeString16 // UTF-8 string, two-byte uint16_t for n, and then n bytes follow
|
|
90
|
+
jump[0x7a] = string.decodeString32 // UTF-8 string, four-byte uint32_t for n, and then n bytes follow
|
|
91
|
+
jump[0x7b] = string.decodeString64 // UTF-8 string, eight-byte uint64_t for n, and then n bytes follow
|
|
92
|
+
// jump[0x7c] = invalidMinor
|
|
93
|
+
// jump[0x7d] = invalidMinor
|
|
94
|
+
// jump[0x7e] = invalidMinor
|
|
95
|
+
jump[0x7f] = errorer('indefinite length bytes/strings are not supported', decodeErrPrefix) // UTF-8 strings follow, terminated by "break"
|
|
96
|
+
// array, 0x00..0x17 data items follow
|
|
97
|
+
for (let i = 0x80; i <= 0x97; i++) {
|
|
98
|
+
jump[i] = array.decodeArrayCompact
|
|
99
|
+
}
|
|
100
|
+
jump[0x98] = array.decodeArray8 // array, one-byte uint8_t for n, and then n data items follow
|
|
101
|
+
jump[0x99] = array.decodeArray16 // array, two-byte uint16_t for n, and then n data items follow
|
|
102
|
+
jump[0x9a] = array.decodeArray32 // array, four-byte uint32_t for n, and then n data items follow
|
|
103
|
+
jump[0x9b] = array.decodeArray64 // array, eight-byte uint64_t for n, and then n data items follow
|
|
104
|
+
// jump[0x9c] = invalidMinor
|
|
105
|
+
// jump[0x9d] = invalidMinor
|
|
106
|
+
// jump[0x9e] = invalidMinor
|
|
107
|
+
jump[0x9f] = array.decodeArrayIndefinite // array, data items follow, terminated by "break"
|
|
108
|
+
// map, 0x00..0x17 pairs of data items follow
|
|
109
|
+
for (let i = 0xa0; i <= 0xb7; i++) {
|
|
110
|
+
jump[i] = map.decodeMapCompact
|
|
111
|
+
}
|
|
112
|
+
jump[0xb8] = map.decodeMap8 // map, one-byte uint8_t for n, and then n pairs of data items follow
|
|
113
|
+
jump[0xb9] = map.decodeMap16 // map, two-byte uint16_t for n, and then n pairs of data items follow
|
|
114
|
+
jump[0xba] = map.decodeMap32 // map, four-byte uint32_t for n, and then n pairs of data items follow
|
|
115
|
+
jump[0xbb] = map.decodeMap64 // map, eight-byte uint64_t for n, and then n pairs of data items follow
|
|
116
|
+
// jump[0xbc] = invalidMinor
|
|
117
|
+
// jump[0xbd] = invalidMinor
|
|
118
|
+
// jump[0xbe] = invalidMinor
|
|
119
|
+
jump[0xbf] = map.decodeMapIndefinite // map, pairs of data items follow, terminated by "break"
|
|
120
|
+
// tags
|
|
121
|
+
for (let i = 0xc0; i <= 0xd7; i++) {
|
|
122
|
+
jump[i] = tag.decodeTagCompact
|
|
123
|
+
}
|
|
124
|
+
jump[0xd8] = tag.decodeTag8
|
|
125
|
+
jump[0xd9] = tag.decodeTag16
|
|
126
|
+
jump[0xda] = tag.decodeTag32
|
|
127
|
+
jump[0xdb] = tag.decodeTag64
|
|
128
|
+
// jump[0xdc] = invalidMinor
|
|
129
|
+
// jump[0xdd] = invalidMinor
|
|
130
|
+
// jump[0xde] = invalidMinor
|
|
131
|
+
// jump[0xdf] = invalidMinor
|
|
132
|
+
// 0xe0..0xf3 simple values, unsupported
|
|
133
|
+
for (let i = 0xe0; i <= 0xf3; i++) {
|
|
134
|
+
jump[i] = errorer('simple values are not supported', decodeErrPrefix)
|
|
135
|
+
}
|
|
136
|
+
// jump[0xf4] = invalidMinor // false, handled by quick[]
|
|
137
|
+
// jump[0xf5] = invalidMinor // true, handled by quick[]
|
|
138
|
+
// jump[0xf6] = invalidMinor // null, handled by quick[]
|
|
139
|
+
jump[0xf7] = float.decodeUndefined // undefined
|
|
140
|
+
jump[0xf8] = errorer('simple values are not supported', decodeErrPrefix) // simple value, one byte follows, unsupported
|
|
141
|
+
jump[0xf9] = float.decodeFloat16 // half-precision float (two-byte IEEE 754)
|
|
142
|
+
jump[0xfa] = float.decodeFloat32 // single-precision float (four-byte IEEE 754)
|
|
143
|
+
jump[0xfb] = float.decodeFloat64 // double-precision float (eight-byte IEEE 754)
|
|
144
|
+
// jump[0xfc] = invalidMinor
|
|
145
|
+
// jump[0xfd] = invalidMinor
|
|
146
|
+
// jump[0xfe] = invalidMinor
|
|
147
|
+
jump[0xff] = float.decodeBreak // "break" stop code
|
|
148
|
+
return jump
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** @type {Token[]} */
|
|
152
|
+
export const quick = []
|
|
153
|
+
// ints <24
|
|
154
|
+
for (let i = 0; i < 24; i++) {
|
|
155
|
+
quick[i] = new Token(Type.uint, i, 1)
|
|
156
|
+
}
|
|
157
|
+
// negints >= -24
|
|
158
|
+
for (let i = -1; i >= -24; i--) {
|
|
159
|
+
quick[31 - i] = new Token(Type.negint, i, 1)
|
|
160
|
+
}
|
|
161
|
+
// empty bytes
|
|
162
|
+
quick[0x40] = new Token(Type.bytes, new Uint8Array(0), 1)
|
|
163
|
+
// empty string
|
|
164
|
+
quick[0x60] = new Token(Type.string, '', 1)
|
|
165
|
+
// empty list
|
|
166
|
+
quick[0x80] = new Token(Type.array, 0, 1)
|
|
167
|
+
// empty map
|
|
168
|
+
quick[0xa0] = new Token(Type.map, 0, 1)
|
|
169
|
+
// false
|
|
170
|
+
quick[0xf4] = new Token(Type.false, false, 1)
|
|
171
|
+
// true
|
|
172
|
+
quick[0xf5] = new Token(Type.true, true, 1)
|
|
173
|
+
// null
|
|
174
|
+
quick[0xf6] = new Token(Type.null, null, 1)
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {Token} token
|
|
178
|
+
* @returns {Uint8Array|undefined}
|
|
179
|
+
*/
|
|
180
|
+
export function quickEncodeToken (token) {
|
|
181
|
+
switch (token.type) {
|
|
182
|
+
case Type.false:
|
|
183
|
+
return fromArray([0xf4])
|
|
184
|
+
case Type.true:
|
|
185
|
+
return fromArray([0xf5])
|
|
186
|
+
case Type.null:
|
|
187
|
+
return fromArray([0xf6])
|
|
188
|
+
case Type.bytes:
|
|
189
|
+
if (!token.value.length) {
|
|
190
|
+
return fromArray([0x40])
|
|
191
|
+
}
|
|
192
|
+
return
|
|
193
|
+
case Type.string:
|
|
194
|
+
if (token.value === '') {
|
|
195
|
+
return fromArray([0x60])
|
|
196
|
+
}
|
|
197
|
+
return
|
|
198
|
+
case Type.array:
|
|
199
|
+
if (token.value === 0) {
|
|
200
|
+
return fromArray([0x80])
|
|
201
|
+
}
|
|
202
|
+
/* c8 ignore next 2 */
|
|
203
|
+
// shouldn't be possible if this were called when there was only one token
|
|
204
|
+
return
|
|
205
|
+
case Type.map:
|
|
206
|
+
if (token.value === 0) {
|
|
207
|
+
return fromArray([0xa0])
|
|
208
|
+
}
|
|
209
|
+
/* c8 ignore next 2 */
|
|
210
|
+
// shouldn't be possible if this were called when there was only one token
|
|
211
|
+
return
|
|
212
|
+
case Type.uint:
|
|
213
|
+
if (token.value < 24) {
|
|
214
|
+
return fromArray([Number(token.value)])
|
|
215
|
+
}
|
|
216
|
+
return
|
|
217
|
+
case Type.negint:
|
|
218
|
+
if (token.value >= -24) {
|
|
219
|
+
return fromArray([31 - Number(token.value)])
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|