cborg 2.0.4 → 3.0.0-alpha.1
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/.github/dependabot.yml +16 -0
- package/.github/workflows/test-and-release.yml +63 -0
- package/CHANGELOG.md +332 -0
- package/bench/bench.js +117 -0
- package/bench/json.js +124 -0
- package/bench/package.json +8 -0
- package/lib/bin.js +1 -1
- package/package.json +29 -47
- package/types/lib/bin.d.ts +1 -0
- package/cjs/browser-test/appendix_a.js +0 -643
- package/cjs/browser-test/common.js +0 -24
- package/cjs/browser-test/node-test-bin.js +0 -348
- package/cjs/browser-test/noop-bin-test.js +0 -4
- package/cjs/browser-test/test-0uint.js +0 -158
- package/cjs/browser-test/test-1negint.js +0 -152
- package/cjs/browser-test/test-2bytes.js +0 -254
- package/cjs/browser-test/test-3string.js +0 -144
- package/cjs/browser-test/test-4array.js +0 -200
- package/cjs/browser-test/test-5map.js +0 -667
- package/cjs/browser-test/test-6tag.js +0 -75
- package/cjs/browser-test/test-7float.js +0 -253
- package/cjs/browser-test/test-bl.js +0 -91
- package/cjs/browser-test/test-cbor-vectors.js +0 -98
- package/cjs/browser-test/test-decode-errors.js +0 -77
- package/cjs/browser-test/test-fuzz.js +0 -56
- package/cjs/browser-test/test-json.js +0 -281
- package/cjs/browser-test/test-length.js +0 -63
- package/cjs/cborg.js +0 -14
- package/cjs/lib/0uint.js +0 -163
- package/cjs/lib/1negint.js +0 -63
- package/cjs/lib/2bytes.js +0 -62
- package/cjs/lib/3string.js +0 -46
- package/cjs/lib/4array.js +0 -51
- package/cjs/lib/5map.js +0 -51
- package/cjs/lib/6tag.js +0 -36
- package/cjs/lib/7float.js +0 -188
- package/cjs/lib/bin.js +0 -139
- package/cjs/lib/bl.js +0 -77
- package/cjs/lib/byte-utils.js +0 -245
- package/cjs/lib/common.js +0 -22
- package/cjs/lib/decode.js +0 -140
- package/cjs/lib/diagnostic.js +0 -124
- package/cjs/lib/encode.js +0 -248
- package/cjs/lib/is.js +0 -87
- package/cjs/lib/json/decode.js +0 -414
- package/cjs/lib/json/encode.js +0 -161
- package/cjs/lib/json/json.js +0 -12
- package/cjs/lib/jump.js +0 -174
- package/cjs/lib/length.js +0 -36
- package/cjs/lib/token.js +0 -46
- package/cjs/node-test/appendix_a.js +0 -643
- package/cjs/node-test/common.js +0 -24
- package/cjs/node-test/node-test-bin.js +0 -348
- package/cjs/node-test/noop-bin-test.js +0 -4
- package/cjs/node-test/test-0uint.js +0 -158
- package/cjs/node-test/test-1negint.js +0 -152
- package/cjs/node-test/test-2bytes.js +0 -254
- package/cjs/node-test/test-3string.js +0 -144
- package/cjs/node-test/test-4array.js +0 -200
- package/cjs/node-test/test-5map.js +0 -667
- package/cjs/node-test/test-6tag.js +0 -75
- package/cjs/node-test/test-7float.js +0 -253
- package/cjs/node-test/test-bl.js +0 -91
- package/cjs/node-test/test-cbor-vectors.js +0 -98
- package/cjs/node-test/test-decode-errors.js +0 -77
- package/cjs/node-test/test-fuzz.js +0 -56
- package/cjs/node-test/test-json.js +0 -281
- package/cjs/node-test/test-length.js +0 -63
- package/cjs/taglib.js +0 -44
- package/cli.js +0 -7
- package/esm/browser-test/appendix_a.js +0 -640
- package/esm/browser-test/common.js +0 -19
- package/esm/browser-test/node-test-bin.js +0 -339
- package/esm/browser-test/noop-bin-test.js +0 -1
- package/esm/browser-test/test-0uint.js +0 -155
- package/esm/browser-test/test-1negint.js +0 -149
- package/esm/browser-test/test-2bytes.js +0 -252
- package/esm/browser-test/test-3string.js +0 -141
- package/esm/browser-test/test-4array.js +0 -197
- package/esm/browser-test/test-5map.js +0 -664
- package/esm/browser-test/test-6tag.js +0 -78
- package/esm/browser-test/test-7float.js +0 -250
- package/esm/browser-test/test-bl.js +0 -84
- package/esm/browser-test/test-cbor-vectors.js +0 -95
- package/esm/browser-test/test-decode-errors.js +0 -69
- package/esm/browser-test/test-fuzz.js +0 -50
- package/esm/browser-test/test-json.js +0 -279
- package/esm/browser-test/test-length.js +0 -55
- package/esm/cborg.js +0 -12
- package/esm/lib/0uint.js +0 -152
- package/esm/lib/1negint.js +0 -55
- package/esm/lib/2bytes.js +0 -59
- package/esm/lib/3string.js +0 -43
- package/esm/lib/4array.js +0 -41
- package/esm/lib/5map.js +0 -41
- package/esm/lib/6tag.js +0 -27
- package/esm/lib/7float.js +0 -179
- package/esm/lib/bin.js +0 -137
- package/esm/lib/bl.js +0 -74
- package/esm/lib/byte-utils.js +0 -228
- package/esm/lib/common.js +0 -19
- package/esm/lib/decode.js +0 -139
- package/esm/lib/diagnostic.js +0 -123
- package/esm/lib/encode.js +0 -246
- package/esm/lib/is.js +0 -81
- package/esm/lib/json/decode.js +0 -413
- package/esm/lib/json/encode.js +0 -160
- package/esm/lib/json/json.js +0 -10
- package/esm/lib/jump.js +0 -168
- package/esm/lib/length.js +0 -31
- package/esm/lib/token.js +0 -43
- package/esm/node-test/appendix_a.js +0 -640
- package/esm/node-test/common.js +0 -19
- package/esm/node-test/node-test-bin.js +0 -339
- package/esm/node-test/noop-bin-test.js +0 -1
- package/esm/node-test/test-0uint.js +0 -155
- package/esm/node-test/test-1negint.js +0 -149
- package/esm/node-test/test-2bytes.js +0 -252
- package/esm/node-test/test-3string.js +0 -141
- package/esm/node-test/test-4array.js +0 -197
- package/esm/node-test/test-5map.js +0 -664
- package/esm/node-test/test-6tag.js +0 -78
- package/esm/node-test/test-7float.js +0 -250
- package/esm/node-test/test-bl.js +0 -84
- package/esm/node-test/test-cbor-vectors.js +0 -95
- package/esm/node-test/test-decode-errors.js +0 -69
- package/esm/node-test/test-fuzz.js +0 -50
- package/esm/node-test/test-json.js +0 -279
- package/esm/node-test/test-length.js +0 -55
- package/esm/package.json +0 -4
- package/esm/taglib.js +0 -37
- package/index.js +0 -1
- package/json +0 -1
- package/length +0 -1
- package/taglib +0 -1
package/cjs/lib/byte-utils.js
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const useBuffer = globalThis.process && !globalThis.process.browser && globalThis.Buffer && typeof globalThis.Buffer.isBuffer === 'function';
|
|
6
|
-
const textDecoder = new TextDecoder();
|
|
7
|
-
const textEncoder = new TextEncoder();
|
|
8
|
-
function isBuffer(buf) {
|
|
9
|
-
return useBuffer && globalThis.Buffer.isBuffer(buf);
|
|
10
|
-
}
|
|
11
|
-
function asU8A(buf) {
|
|
12
|
-
if (!(buf instanceof Uint8Array)) {
|
|
13
|
-
return Uint8Array.from(buf);
|
|
14
|
-
}
|
|
15
|
-
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf;
|
|
16
|
-
}
|
|
17
|
-
const toString = useBuffer ? (bytes, start, end) => {
|
|
18
|
-
return end - start > 64 ? globalThis.Buffer.from(bytes.subarray(start, end)).toString('utf8') : utf8Slice(bytes, start, end);
|
|
19
|
-
} : (bytes, start, end) => {
|
|
20
|
-
return end - start > 64 ? textDecoder.decode(bytes.subarray(start, end)) : utf8Slice(bytes, start, end);
|
|
21
|
-
};
|
|
22
|
-
const fromString = useBuffer ? string => {
|
|
23
|
-
return string.length > 64 ? globalThis.Buffer.from(string) : utf8ToBytes(string);
|
|
24
|
-
} : string => {
|
|
25
|
-
return string.length > 64 ? textEncoder.encode(string) : utf8ToBytes(string);
|
|
26
|
-
};
|
|
27
|
-
const fromArray = arr => {
|
|
28
|
-
return Uint8Array.from(arr);
|
|
29
|
-
};
|
|
30
|
-
const slice = useBuffer ? (bytes, start, end) => {
|
|
31
|
-
if (isBuffer(bytes)) {
|
|
32
|
-
return new Uint8Array(bytes.subarray(start, end));
|
|
33
|
-
}
|
|
34
|
-
return bytes.slice(start, end);
|
|
35
|
-
} : (bytes, start, end) => {
|
|
36
|
-
return bytes.slice(start, end);
|
|
37
|
-
};
|
|
38
|
-
const concat = useBuffer ? (chunks, length) => {
|
|
39
|
-
chunks = chunks.map(c => c instanceof Uint8Array ? c : globalThis.Buffer.from(c));
|
|
40
|
-
return asU8A(globalThis.Buffer.concat(chunks, length));
|
|
41
|
-
} : (chunks, length) => {
|
|
42
|
-
const out = new Uint8Array(length);
|
|
43
|
-
let off = 0;
|
|
44
|
-
for (let b of chunks) {
|
|
45
|
-
if (off + b.length > out.length) {
|
|
46
|
-
b = b.subarray(0, out.length - off);
|
|
47
|
-
}
|
|
48
|
-
out.set(b, off);
|
|
49
|
-
off += b.length;
|
|
50
|
-
}
|
|
51
|
-
return out;
|
|
52
|
-
};
|
|
53
|
-
const alloc = useBuffer ? size => {
|
|
54
|
-
return globalThis.Buffer.allocUnsafe(size);
|
|
55
|
-
} : size => {
|
|
56
|
-
return new Uint8Array(size);
|
|
57
|
-
};
|
|
58
|
-
const toHex = useBuffer ? d => {
|
|
59
|
-
if (typeof d === 'string') {
|
|
60
|
-
return d;
|
|
61
|
-
}
|
|
62
|
-
return globalThis.Buffer.from(toBytes(d)).toString('hex');
|
|
63
|
-
} : d => {
|
|
64
|
-
if (typeof d === 'string') {
|
|
65
|
-
return d;
|
|
66
|
-
}
|
|
67
|
-
return Array.prototype.reduce.call(toBytes(d), (p, c) => `${ p }${ c.toString(16).padStart(2, '0') }`, '');
|
|
68
|
-
};
|
|
69
|
-
const fromHex = useBuffer ? hex => {
|
|
70
|
-
if (hex instanceof Uint8Array) {
|
|
71
|
-
return hex;
|
|
72
|
-
}
|
|
73
|
-
return globalThis.Buffer.from(hex, 'hex');
|
|
74
|
-
} : hex => {
|
|
75
|
-
if (hex instanceof Uint8Array) {
|
|
76
|
-
return hex;
|
|
77
|
-
}
|
|
78
|
-
if (!hex.length) {
|
|
79
|
-
return new Uint8Array(0);
|
|
80
|
-
}
|
|
81
|
-
return new Uint8Array(hex.split('').map((c, i, d) => i % 2 === 0 ? `0x${ c }${ d[i + 1] }` : '').filter(Boolean).map(e => parseInt(e, 16)));
|
|
82
|
-
};
|
|
83
|
-
function toBytes(obj) {
|
|
84
|
-
if (obj instanceof Uint8Array && obj.constructor.name === 'Uint8Array') {
|
|
85
|
-
return obj;
|
|
86
|
-
}
|
|
87
|
-
if (obj instanceof ArrayBuffer) {
|
|
88
|
-
return new Uint8Array(obj);
|
|
89
|
-
}
|
|
90
|
-
if (ArrayBuffer.isView(obj)) {
|
|
91
|
-
return new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength);
|
|
92
|
-
}
|
|
93
|
-
throw new Error('Unknown type, must be binary type');
|
|
94
|
-
}
|
|
95
|
-
function compare(b1, b2) {
|
|
96
|
-
if (isBuffer(b1) && isBuffer(b2)) {
|
|
97
|
-
return b1.compare(b2);
|
|
98
|
-
}
|
|
99
|
-
for (let i = 0; i < b1.length; i++) {
|
|
100
|
-
if (b1[i] === b2[i]) {
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
return b1[i] < b2[i] ? -1 : 1;
|
|
104
|
-
}
|
|
105
|
-
return 0;
|
|
106
|
-
}
|
|
107
|
-
function utf8ToBytes(string, units = Infinity) {
|
|
108
|
-
let codePoint;
|
|
109
|
-
const length = string.length;
|
|
110
|
-
let leadSurrogate = null;
|
|
111
|
-
const bytes = [];
|
|
112
|
-
for (let i = 0; i < length; ++i) {
|
|
113
|
-
codePoint = string.charCodeAt(i);
|
|
114
|
-
if (codePoint > 55295 && codePoint < 57344) {
|
|
115
|
-
if (!leadSurrogate) {
|
|
116
|
-
if (codePoint > 56319) {
|
|
117
|
-
if ((units -= 3) > -1)
|
|
118
|
-
bytes.push(239, 191, 189);
|
|
119
|
-
continue;
|
|
120
|
-
} else if (i + 1 === length) {
|
|
121
|
-
if ((units -= 3) > -1)
|
|
122
|
-
bytes.push(239, 191, 189);
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
leadSurrogate = codePoint;
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
if (codePoint < 56320) {
|
|
129
|
-
if ((units -= 3) > -1)
|
|
130
|
-
bytes.push(239, 191, 189);
|
|
131
|
-
leadSurrogate = codePoint;
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
|
|
135
|
-
} else if (leadSurrogate) {
|
|
136
|
-
if ((units -= 3) > -1)
|
|
137
|
-
bytes.push(239, 191, 189);
|
|
138
|
-
}
|
|
139
|
-
leadSurrogate = null;
|
|
140
|
-
if (codePoint < 128) {
|
|
141
|
-
if ((units -= 1) < 0)
|
|
142
|
-
break;
|
|
143
|
-
bytes.push(codePoint);
|
|
144
|
-
} else if (codePoint < 2048) {
|
|
145
|
-
if ((units -= 2) < 0)
|
|
146
|
-
break;
|
|
147
|
-
bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128);
|
|
148
|
-
} else if (codePoint < 65536) {
|
|
149
|
-
if ((units -= 3) < 0)
|
|
150
|
-
break;
|
|
151
|
-
bytes.push(codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
|
|
152
|
-
} else if (codePoint < 1114112) {
|
|
153
|
-
if ((units -= 4) < 0)
|
|
154
|
-
break;
|
|
155
|
-
bytes.push(codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
|
|
156
|
-
} else {
|
|
157
|
-
throw new Error('Invalid code point');
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return bytes;
|
|
161
|
-
}
|
|
162
|
-
function utf8Slice(buf, offset, end) {
|
|
163
|
-
const res = [];
|
|
164
|
-
while (offset < end) {
|
|
165
|
-
const firstByte = buf[offset];
|
|
166
|
-
let codePoint = null;
|
|
167
|
-
let bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1;
|
|
168
|
-
if (offset + bytesPerSequence <= end) {
|
|
169
|
-
let secondByte, thirdByte, fourthByte, tempCodePoint;
|
|
170
|
-
switch (bytesPerSequence) {
|
|
171
|
-
case 1:
|
|
172
|
-
if (firstByte < 128) {
|
|
173
|
-
codePoint = firstByte;
|
|
174
|
-
}
|
|
175
|
-
break;
|
|
176
|
-
case 2:
|
|
177
|
-
secondByte = buf[offset + 1];
|
|
178
|
-
if ((secondByte & 192) === 128) {
|
|
179
|
-
tempCodePoint = (firstByte & 31) << 6 | secondByte & 63;
|
|
180
|
-
if (tempCodePoint > 127) {
|
|
181
|
-
codePoint = tempCodePoint;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
break;
|
|
185
|
-
case 3:
|
|
186
|
-
secondByte = buf[offset + 1];
|
|
187
|
-
thirdByte = buf[offset + 2];
|
|
188
|
-
if ((secondByte & 192) === 128 && (thirdByte & 192) === 128) {
|
|
189
|
-
tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63;
|
|
190
|
-
if (tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) {
|
|
191
|
-
codePoint = tempCodePoint;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
break;
|
|
195
|
-
case 4:
|
|
196
|
-
secondByte = buf[offset + 1];
|
|
197
|
-
thirdByte = buf[offset + 2];
|
|
198
|
-
fourthByte = buf[offset + 3];
|
|
199
|
-
if ((secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) {
|
|
200
|
-
tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63;
|
|
201
|
-
if (tempCodePoint > 65535 && tempCodePoint < 1114112) {
|
|
202
|
-
codePoint = tempCodePoint;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (codePoint === null) {
|
|
208
|
-
codePoint = 65533;
|
|
209
|
-
bytesPerSequence = 1;
|
|
210
|
-
} else if (codePoint > 65535) {
|
|
211
|
-
codePoint -= 65536;
|
|
212
|
-
res.push(codePoint >>> 10 & 1023 | 55296);
|
|
213
|
-
codePoint = 56320 | codePoint & 1023;
|
|
214
|
-
}
|
|
215
|
-
res.push(codePoint);
|
|
216
|
-
offset += bytesPerSequence;
|
|
217
|
-
}
|
|
218
|
-
return decodeCodePointsArray(res);
|
|
219
|
-
}
|
|
220
|
-
const MAX_ARGUMENTS_LENGTH = 4096;
|
|
221
|
-
function decodeCodePointsArray(codePoints) {
|
|
222
|
-
const len = codePoints.length;
|
|
223
|
-
if (len <= MAX_ARGUMENTS_LENGTH) {
|
|
224
|
-
return String.fromCharCode.apply(String, codePoints);
|
|
225
|
-
}
|
|
226
|
-
let res = '';
|
|
227
|
-
let i = 0;
|
|
228
|
-
while (i < len) {
|
|
229
|
-
res += String.fromCharCode.apply(String, codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH));
|
|
230
|
-
}
|
|
231
|
-
return res;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
exports.alloc = alloc;
|
|
235
|
-
exports.asU8A = asU8A;
|
|
236
|
-
exports.compare = compare;
|
|
237
|
-
exports.concat = concat;
|
|
238
|
-
exports.decodeCodePointsArray = decodeCodePointsArray;
|
|
239
|
-
exports.fromArray = fromArray;
|
|
240
|
-
exports.fromHex = fromHex;
|
|
241
|
-
exports.fromString = fromString;
|
|
242
|
-
exports.slice = slice;
|
|
243
|
-
exports.toHex = toHex;
|
|
244
|
-
exports.toString = toString;
|
|
245
|
-
exports.useBuffer = useBuffer;
|
package/cjs/lib/common.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const decodeErrPrefix = 'CBOR decode error:';
|
|
6
|
-
const encodeErrPrefix = 'CBOR encode error:';
|
|
7
|
-
const uintMinorPrefixBytes = [];
|
|
8
|
-
uintMinorPrefixBytes[23] = 1;
|
|
9
|
-
uintMinorPrefixBytes[24] = 2;
|
|
10
|
-
uintMinorPrefixBytes[25] = 3;
|
|
11
|
-
uintMinorPrefixBytes[26] = 5;
|
|
12
|
-
uintMinorPrefixBytes[27] = 9;
|
|
13
|
-
function assertEnoughData(data, pos, need) {
|
|
14
|
-
if (data.length - pos < need) {
|
|
15
|
-
throw new Error(`${ decodeErrPrefix } not enough data for type`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
exports.assertEnoughData = assertEnoughData;
|
|
20
|
-
exports.decodeErrPrefix = decodeErrPrefix;
|
|
21
|
-
exports.encodeErrPrefix = encodeErrPrefix;
|
|
22
|
-
exports.uintMinorPrefixBytes = uintMinorPrefixBytes;
|
package/cjs/lib/decode.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var common = require('./common.js');
|
|
6
|
-
var token = require('./token.js');
|
|
7
|
-
var jump = require('./jump.js');
|
|
8
|
-
|
|
9
|
-
const defaultDecodeOptions = {
|
|
10
|
-
strict: false,
|
|
11
|
-
allowIndefinite: true,
|
|
12
|
-
allowUndefined: true,
|
|
13
|
-
allowBigInt: true
|
|
14
|
-
};
|
|
15
|
-
class Tokeniser {
|
|
16
|
-
constructor(data, options = {}) {
|
|
17
|
-
this.pos = 0;
|
|
18
|
-
this.data = data;
|
|
19
|
-
this.options = options;
|
|
20
|
-
}
|
|
21
|
-
done() {
|
|
22
|
-
return this.pos >= this.data.length;
|
|
23
|
-
}
|
|
24
|
-
next() {
|
|
25
|
-
const byt = this.data[this.pos];
|
|
26
|
-
let token = jump.quick[byt];
|
|
27
|
-
if (token === undefined) {
|
|
28
|
-
const decoder = jump.jump[byt];
|
|
29
|
-
if (!decoder) {
|
|
30
|
-
throw new Error(`${ common.decodeErrPrefix } no decoder for major type ${ byt >>> 5 } (byte 0x${ byt.toString(16).padStart(2, '0') })`);
|
|
31
|
-
}
|
|
32
|
-
const minor = byt & 31;
|
|
33
|
-
token = decoder(this.data, this.pos, minor, this.options);
|
|
34
|
-
}
|
|
35
|
-
this.pos += token.encodedLength;
|
|
36
|
-
return token;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const DONE = Symbol.for('DONE');
|
|
40
|
-
const BREAK = Symbol.for('BREAK');
|
|
41
|
-
function tokenToArray(token, tokeniser, options) {
|
|
42
|
-
const arr = [];
|
|
43
|
-
for (let i = 0; i < token.value; i++) {
|
|
44
|
-
const value = tokensToObject(tokeniser, options);
|
|
45
|
-
if (value === BREAK) {
|
|
46
|
-
if (token.value === Infinity) {
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
throw new Error(`${ common.decodeErrPrefix } got unexpected break to lengthed array`);
|
|
50
|
-
}
|
|
51
|
-
if (value === DONE) {
|
|
52
|
-
throw new Error(`${ common.decodeErrPrefix } found array but not enough entries (got ${ i }, expected ${ token.value })`);
|
|
53
|
-
}
|
|
54
|
-
arr[i] = value;
|
|
55
|
-
}
|
|
56
|
-
return arr;
|
|
57
|
-
}
|
|
58
|
-
function tokenToMap(token, tokeniser, options) {
|
|
59
|
-
const useMaps = options.useMaps === true;
|
|
60
|
-
const obj = useMaps ? undefined : {};
|
|
61
|
-
const m = useMaps ? new Map() : undefined;
|
|
62
|
-
for (let i = 0; i < token.value; i++) {
|
|
63
|
-
const key = tokensToObject(tokeniser, options);
|
|
64
|
-
if (key === BREAK) {
|
|
65
|
-
if (token.value === Infinity) {
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
throw new Error(`${ common.decodeErrPrefix } got unexpected break to lengthed map`);
|
|
69
|
-
}
|
|
70
|
-
if (key === DONE) {
|
|
71
|
-
throw new Error(`${ common.decodeErrPrefix } found map but not enough entries (got ${ i } [no key], expected ${ token.value })`);
|
|
72
|
-
}
|
|
73
|
-
if (useMaps !== true && typeof key !== 'string') {
|
|
74
|
-
throw new Error(`${ common.decodeErrPrefix } non-string keys not supported (got ${ typeof key })`);
|
|
75
|
-
}
|
|
76
|
-
if (options.rejectDuplicateMapKeys === true) {
|
|
77
|
-
if (useMaps && m.has(key) || !useMaps && key in obj) {
|
|
78
|
-
throw new Error(`${ common.decodeErrPrefix } found repeat map key "${ key }"`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const value = tokensToObject(tokeniser, options);
|
|
82
|
-
if (value === DONE) {
|
|
83
|
-
throw new Error(`${ common.decodeErrPrefix } found map but not enough entries (got ${ i } [no value], expected ${ token.value })`);
|
|
84
|
-
}
|
|
85
|
-
if (useMaps) {
|
|
86
|
-
m.set(key, value);
|
|
87
|
-
} else {
|
|
88
|
-
obj[key] = value;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return useMaps ? m : obj;
|
|
92
|
-
}
|
|
93
|
-
function tokensToObject(tokeniser, options) {
|
|
94
|
-
if (tokeniser.done()) {
|
|
95
|
-
return DONE;
|
|
96
|
-
}
|
|
97
|
-
const token$1 = tokeniser.next();
|
|
98
|
-
if (token$1.type === token.Type.break) {
|
|
99
|
-
return BREAK;
|
|
100
|
-
}
|
|
101
|
-
if (token$1.type.terminal) {
|
|
102
|
-
return token$1.value;
|
|
103
|
-
}
|
|
104
|
-
if (token$1.type === token.Type.array) {
|
|
105
|
-
return tokenToArray(token$1, tokeniser, options);
|
|
106
|
-
}
|
|
107
|
-
if (token$1.type === token.Type.map) {
|
|
108
|
-
return tokenToMap(token$1, tokeniser, options);
|
|
109
|
-
}
|
|
110
|
-
if (token$1.type === token.Type.tag) {
|
|
111
|
-
if (options.tags && typeof options.tags[token$1.value] === 'function') {
|
|
112
|
-
const tagged = tokensToObject(tokeniser, options);
|
|
113
|
-
return options.tags[token$1.value](tagged);
|
|
114
|
-
}
|
|
115
|
-
throw new Error(`${ common.decodeErrPrefix } tag not supported (${ token$1.value })`);
|
|
116
|
-
}
|
|
117
|
-
throw new Error('unsupported');
|
|
118
|
-
}
|
|
119
|
-
function decode(data, options) {
|
|
120
|
-
if (!(data instanceof Uint8Array)) {
|
|
121
|
-
throw new Error(`${ common.decodeErrPrefix } data to decode must be a Uint8Array`);
|
|
122
|
-
}
|
|
123
|
-
options = Object.assign({}, defaultDecodeOptions, options);
|
|
124
|
-
const tokeniser = options.tokenizer || new Tokeniser(data, options);
|
|
125
|
-
const decoded = tokensToObject(tokeniser, options);
|
|
126
|
-
if (decoded === DONE) {
|
|
127
|
-
throw new Error(`${ common.decodeErrPrefix } did not find any content to decode`);
|
|
128
|
-
}
|
|
129
|
-
if (decoded === BREAK) {
|
|
130
|
-
throw new Error(`${ common.decodeErrPrefix } got unexpected break`);
|
|
131
|
-
}
|
|
132
|
-
if (!tokeniser.done()) {
|
|
133
|
-
throw new Error(`${ common.decodeErrPrefix } too many terminals, data makes no sense`);
|
|
134
|
-
}
|
|
135
|
-
return decoded;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
exports.Tokeniser = Tokeniser;
|
|
139
|
-
exports.decode = decode;
|
|
140
|
-
exports.tokensToObject = tokensToObject;
|
package/cjs/lib/diagnostic.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var decode = require('./decode.js');
|
|
6
|
-
var byteUtils = require('./byte-utils.js');
|
|
7
|
-
var _0uint = require('./0uint.js');
|
|
8
|
-
|
|
9
|
-
const utf8Encoder = new TextEncoder();
|
|
10
|
-
const utf8Decoder = new TextDecoder();
|
|
11
|
-
function* tokensToDiagnostic(inp, width = 100) {
|
|
12
|
-
const tokeniser = new decode.Tokeniser(inp, {
|
|
13
|
-
retainStringBytes: true,
|
|
14
|
-
allowBigInt: true
|
|
15
|
-
});
|
|
16
|
-
let pos = 0;
|
|
17
|
-
const indent = [];
|
|
18
|
-
const slc = (start, length) => {
|
|
19
|
-
return byteUtils.toHex(inp.slice(pos + start, pos + start + length));
|
|
20
|
-
};
|
|
21
|
-
while (!tokeniser.done()) {
|
|
22
|
-
const token = tokeniser.next();
|
|
23
|
-
let margin = ''.padStart(indent.length * 2, ' ');
|
|
24
|
-
let vLength = token.encodedLength - 1;
|
|
25
|
-
let v = String(token.value);
|
|
26
|
-
let outp = `${ margin }${ slc(0, 1) }`;
|
|
27
|
-
const str = token.type.name === 'bytes' || token.type.name === 'string';
|
|
28
|
-
if (token.type.name === 'string') {
|
|
29
|
-
v = v.length;
|
|
30
|
-
vLength -= v;
|
|
31
|
-
} else if (token.type.name === 'bytes') {
|
|
32
|
-
v = token.value.length;
|
|
33
|
-
vLength -= v;
|
|
34
|
-
}
|
|
35
|
-
let multilen;
|
|
36
|
-
switch (token.type.name) {
|
|
37
|
-
case 'string':
|
|
38
|
-
case 'bytes':
|
|
39
|
-
case 'map':
|
|
40
|
-
case 'array':
|
|
41
|
-
multilen = token.type.name === 'string' ? utf8Encoder.encode(token.value).length : token.value.length;
|
|
42
|
-
if (multilen >= _0uint.uintBoundaries[0]) {
|
|
43
|
-
if (multilen < _0uint.uintBoundaries[1]) {
|
|
44
|
-
outp += ` ${ slc(1, 1) }`;
|
|
45
|
-
} else if (multilen < _0uint.uintBoundaries[2]) {
|
|
46
|
-
outp += ` ${ slc(1, 2) }`;
|
|
47
|
-
} else if (multilen < _0uint.uintBoundaries[3]) {
|
|
48
|
-
outp += ` ${ slc(1, 4) }`;
|
|
49
|
-
} else if (multilen < _0uint.uintBoundaries[4]) {
|
|
50
|
-
outp += ` ${ slc(1, 8) }`;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
break;
|
|
54
|
-
default:
|
|
55
|
-
outp += ` ${ slc(1, vLength) }`;
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
outp = outp.padEnd(width / 2, ' ');
|
|
59
|
-
outp += `# ${ margin }${ token.type.name }`;
|
|
60
|
-
if (token.type.name !== v) {
|
|
61
|
-
outp += `(${ v })`;
|
|
62
|
-
}
|
|
63
|
-
yield outp;
|
|
64
|
-
if (str) {
|
|
65
|
-
let asString = token.type.name === 'string';
|
|
66
|
-
margin += ' ';
|
|
67
|
-
let repr = asString ? utf8Encoder.encode(token.value) : token.value;
|
|
68
|
-
if (asString && token.byteValue !== undefined) {
|
|
69
|
-
if (repr.length !== token.byteValue.length) {
|
|
70
|
-
repr = token.byteValue;
|
|
71
|
-
asString = false;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const wh = (width / 2 - margin.length - 1) / 2;
|
|
75
|
-
let snip = 0;
|
|
76
|
-
while (repr.length - snip > 0) {
|
|
77
|
-
const piece = repr.slice(snip, snip + wh);
|
|
78
|
-
snip += piece.length;
|
|
79
|
-
const st = asString ? utf8Decoder.decode(piece) : piece.reduce((p, c) => {
|
|
80
|
-
if (c < 32 || c >= 127 && c < 161 || c === 173) {
|
|
81
|
-
return `${ p }\\x${ c.toString(16).padStart(2, '0') }`;
|
|
82
|
-
}
|
|
83
|
-
return `${ p }${ String.fromCharCode(c) }`;
|
|
84
|
-
}, '');
|
|
85
|
-
yield `${ margin }${ byteUtils.toHex(piece) }`.padEnd(width / 2, ' ') + `# ${ margin }"${ st }"`;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (indent.length) {
|
|
89
|
-
indent[indent.length - 1]--;
|
|
90
|
-
}
|
|
91
|
-
if (!token.type.terminal) {
|
|
92
|
-
switch (token.type.name) {
|
|
93
|
-
case 'map':
|
|
94
|
-
indent.push(token.value * 2);
|
|
95
|
-
break;
|
|
96
|
-
case 'array':
|
|
97
|
-
indent.push(token.value);
|
|
98
|
-
break;
|
|
99
|
-
case 'tag':
|
|
100
|
-
indent.push(1);
|
|
101
|
-
break;
|
|
102
|
-
default:
|
|
103
|
-
throw new Error(`Unknown token type '${ token.type.name }'`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
while (indent.length && indent[indent.length - 1] <= 0) {
|
|
107
|
-
indent.pop();
|
|
108
|
-
}
|
|
109
|
-
pos += token.encodedLength;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
function fromDiag(input) {
|
|
113
|
-
if (typeof input !== 'string') {
|
|
114
|
-
throw new TypeError('Expected string input');
|
|
115
|
-
}
|
|
116
|
-
input = input.replace(/#.*?$/mg, '').replace(/[\s\r\n]+/mg, '');
|
|
117
|
-
if (/[^a-f0-9]/i.test(input)) {
|
|
118
|
-
throw new TypeError('Input string was not CBOR diagnostic format');
|
|
119
|
-
}
|
|
120
|
-
return byteUtils.fromHex(input);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
exports.fromDiag = fromDiag;
|
|
124
|
-
exports.tokensToDiagnostic = tokensToDiagnostic;
|