cborg 2.0.5 → 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/esm/lib/json/decode.js
DELETED
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
import { decode as _decode } from '../decode.js';
|
|
2
|
-
import {
|
|
3
|
-
Token,
|
|
4
|
-
Type
|
|
5
|
-
} from '../token.js';
|
|
6
|
-
import { decodeCodePointsArray } from '../byte-utils.js';
|
|
7
|
-
import { decodeErrPrefix } from '../common.js';
|
|
8
|
-
class Tokenizer {
|
|
9
|
-
constructor(data, options = {}) {
|
|
10
|
-
this.pos = 0;
|
|
11
|
-
this.data = data;
|
|
12
|
-
this.options = options;
|
|
13
|
-
this.modeStack = ['value'];
|
|
14
|
-
this.lastToken = '';
|
|
15
|
-
}
|
|
16
|
-
done() {
|
|
17
|
-
return this.pos >= this.data.length;
|
|
18
|
-
}
|
|
19
|
-
ch() {
|
|
20
|
-
return this.data[this.pos];
|
|
21
|
-
}
|
|
22
|
-
currentMode() {
|
|
23
|
-
return this.modeStack[this.modeStack.length - 1];
|
|
24
|
-
}
|
|
25
|
-
skipWhitespace() {
|
|
26
|
-
let c = this.ch();
|
|
27
|
-
while (c === 32 || c === 9 || c === 13 || c === 10) {
|
|
28
|
-
c = this.data[++this.pos];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
expect(str) {
|
|
32
|
-
if (this.data.length - this.pos < str.length) {
|
|
33
|
-
throw new Error(`${ decodeErrPrefix } unexpected end of input at position ${ this.pos }`);
|
|
34
|
-
}
|
|
35
|
-
for (let i = 0; i < str.length; i++) {
|
|
36
|
-
if (this.data[this.pos++] !== str[i]) {
|
|
37
|
-
throw new Error(`${ decodeErrPrefix } unexpected token at position ${ this.pos }, expected to find '${ String.fromCharCode(...str) }'`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
parseNumber() {
|
|
42
|
-
const startPos = this.pos;
|
|
43
|
-
let negative = false;
|
|
44
|
-
let float = false;
|
|
45
|
-
const swallow = chars => {
|
|
46
|
-
while (!this.done()) {
|
|
47
|
-
const ch = this.ch();
|
|
48
|
-
if (chars.includes(ch)) {
|
|
49
|
-
this.pos++;
|
|
50
|
-
} else {
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
if (this.ch() === 45) {
|
|
56
|
-
negative = true;
|
|
57
|
-
this.pos++;
|
|
58
|
-
}
|
|
59
|
-
if (this.ch() === 48) {
|
|
60
|
-
this.pos++;
|
|
61
|
-
if (this.ch() === 46) {
|
|
62
|
-
this.pos++;
|
|
63
|
-
float = true;
|
|
64
|
-
} else {
|
|
65
|
-
return new Token(Type.uint, 0, this.pos - startPos);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
swallow([
|
|
69
|
-
48,
|
|
70
|
-
49,
|
|
71
|
-
50,
|
|
72
|
-
51,
|
|
73
|
-
52,
|
|
74
|
-
53,
|
|
75
|
-
54,
|
|
76
|
-
55,
|
|
77
|
-
56,
|
|
78
|
-
57
|
|
79
|
-
]);
|
|
80
|
-
if (negative && this.pos === startPos + 1) {
|
|
81
|
-
throw new Error(`${ decodeErrPrefix } unexpected token at position ${ this.pos }`);
|
|
82
|
-
}
|
|
83
|
-
if (!this.done() && this.ch() === 46) {
|
|
84
|
-
if (float) {
|
|
85
|
-
throw new Error(`${ decodeErrPrefix } unexpected token at position ${ this.pos }`);
|
|
86
|
-
}
|
|
87
|
-
float = true;
|
|
88
|
-
this.pos++;
|
|
89
|
-
swallow([
|
|
90
|
-
48,
|
|
91
|
-
49,
|
|
92
|
-
50,
|
|
93
|
-
51,
|
|
94
|
-
52,
|
|
95
|
-
53,
|
|
96
|
-
54,
|
|
97
|
-
55,
|
|
98
|
-
56,
|
|
99
|
-
57
|
|
100
|
-
]);
|
|
101
|
-
}
|
|
102
|
-
if (!this.done() && (this.ch() === 101 || this.ch() === 69)) {
|
|
103
|
-
float = true;
|
|
104
|
-
this.pos++;
|
|
105
|
-
if (!this.done() && (this.ch() === 43 || this.ch() === 45)) {
|
|
106
|
-
this.pos++;
|
|
107
|
-
}
|
|
108
|
-
swallow([
|
|
109
|
-
48,
|
|
110
|
-
49,
|
|
111
|
-
50,
|
|
112
|
-
51,
|
|
113
|
-
52,
|
|
114
|
-
53,
|
|
115
|
-
54,
|
|
116
|
-
55,
|
|
117
|
-
56,
|
|
118
|
-
57
|
|
119
|
-
]);
|
|
120
|
-
}
|
|
121
|
-
const numStr = String.fromCharCode.apply(null, this.data.subarray(startPos, this.pos));
|
|
122
|
-
const num = parseFloat(numStr);
|
|
123
|
-
if (float) {
|
|
124
|
-
return new Token(Type.float, num, this.pos - startPos);
|
|
125
|
-
}
|
|
126
|
-
if (this.options.allowBigInt !== true || Number.isSafeInteger(num)) {
|
|
127
|
-
return new Token(num >= 0 ? Type.uint : Type.negint, num, this.pos - startPos);
|
|
128
|
-
}
|
|
129
|
-
return new Token(num >= 0 ? Type.uint : Type.negint, BigInt(numStr), this.pos - startPos);
|
|
130
|
-
}
|
|
131
|
-
parseString() {
|
|
132
|
-
if (this.ch() !== 34) {
|
|
133
|
-
throw new Error(`${ decodeErrPrefix } unexpected character at position ${ this.pos }; this shouldn't happen`);
|
|
134
|
-
}
|
|
135
|
-
this.pos++;
|
|
136
|
-
for (let i = this.pos, l = 0; i < this.data.length && l < 65536; i++, l++) {
|
|
137
|
-
const ch = this.data[i];
|
|
138
|
-
if (ch === 92 || ch < 32 || ch >= 128) {
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
if (ch === 34) {
|
|
142
|
-
const str = String.fromCharCode.apply(null, this.data.subarray(this.pos, i));
|
|
143
|
-
this.pos = i + 1;
|
|
144
|
-
return new Token(Type.string, str, l);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
const startPos = this.pos;
|
|
148
|
-
const chars = [];
|
|
149
|
-
const readu4 = () => {
|
|
150
|
-
if (this.pos + 4 >= this.data.length) {
|
|
151
|
-
throw new Error(`${ decodeErrPrefix } unexpected end of unicode escape sequence at position ${ this.pos }`);
|
|
152
|
-
}
|
|
153
|
-
let u4 = 0;
|
|
154
|
-
for (let i = 0; i < 4; i++) {
|
|
155
|
-
let ch = this.ch();
|
|
156
|
-
if (ch >= 48 && ch <= 57) {
|
|
157
|
-
ch -= 48;
|
|
158
|
-
} else if (ch >= 97 && ch <= 102) {
|
|
159
|
-
ch = ch - 97 + 10;
|
|
160
|
-
} else if (ch >= 65 && ch <= 70) {
|
|
161
|
-
ch = ch - 65 + 10;
|
|
162
|
-
} else {
|
|
163
|
-
throw new Error(`${ decodeErrPrefix } unexpected unicode escape character at position ${ this.pos }`);
|
|
164
|
-
}
|
|
165
|
-
u4 = u4 * 16 + ch;
|
|
166
|
-
this.pos++;
|
|
167
|
-
}
|
|
168
|
-
return u4;
|
|
169
|
-
};
|
|
170
|
-
const readUtf8Char = () => {
|
|
171
|
-
const firstByte = this.ch();
|
|
172
|
-
let codePoint = null;
|
|
173
|
-
let bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1;
|
|
174
|
-
if (this.pos + bytesPerSequence > this.data.length) {
|
|
175
|
-
throw new Error(`${ decodeErrPrefix } unexpected unicode sequence at position ${ this.pos }`);
|
|
176
|
-
}
|
|
177
|
-
let secondByte, thirdByte, fourthByte, tempCodePoint;
|
|
178
|
-
switch (bytesPerSequence) {
|
|
179
|
-
case 1:
|
|
180
|
-
if (firstByte < 128) {
|
|
181
|
-
codePoint = firstByte;
|
|
182
|
-
}
|
|
183
|
-
break;
|
|
184
|
-
case 2:
|
|
185
|
-
secondByte = this.data[this.pos + 1];
|
|
186
|
-
if ((secondByte & 192) === 128) {
|
|
187
|
-
tempCodePoint = (firstByte & 31) << 6 | secondByte & 63;
|
|
188
|
-
if (tempCodePoint > 127) {
|
|
189
|
-
codePoint = tempCodePoint;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
break;
|
|
193
|
-
case 3:
|
|
194
|
-
secondByte = this.data[this.pos + 1];
|
|
195
|
-
thirdByte = this.data[this.pos + 2];
|
|
196
|
-
if ((secondByte & 192) === 128 && (thirdByte & 192) === 128) {
|
|
197
|
-
tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63;
|
|
198
|
-
if (tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) {
|
|
199
|
-
codePoint = tempCodePoint;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
break;
|
|
203
|
-
case 4:
|
|
204
|
-
secondByte = this.data[this.pos + 1];
|
|
205
|
-
thirdByte = this.data[this.pos + 2];
|
|
206
|
-
fourthByte = this.data[this.pos + 3];
|
|
207
|
-
if ((secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) {
|
|
208
|
-
tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63;
|
|
209
|
-
if (tempCodePoint > 65535 && tempCodePoint < 1114112) {
|
|
210
|
-
codePoint = tempCodePoint;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (codePoint === null) {
|
|
215
|
-
codePoint = 65533;
|
|
216
|
-
bytesPerSequence = 1;
|
|
217
|
-
} else if (codePoint > 65535) {
|
|
218
|
-
codePoint -= 65536;
|
|
219
|
-
chars.push(codePoint >>> 10 & 1023 | 55296);
|
|
220
|
-
codePoint = 56320 | codePoint & 1023;
|
|
221
|
-
}
|
|
222
|
-
chars.push(codePoint);
|
|
223
|
-
this.pos += bytesPerSequence;
|
|
224
|
-
};
|
|
225
|
-
while (!this.done()) {
|
|
226
|
-
const ch = this.ch();
|
|
227
|
-
let ch1;
|
|
228
|
-
switch (ch) {
|
|
229
|
-
case 92:
|
|
230
|
-
this.pos++;
|
|
231
|
-
if (this.done()) {
|
|
232
|
-
throw new Error(`${ decodeErrPrefix } unexpected string termination at position ${ this.pos }`);
|
|
233
|
-
}
|
|
234
|
-
ch1 = this.ch();
|
|
235
|
-
this.pos++;
|
|
236
|
-
switch (ch1) {
|
|
237
|
-
case 34:
|
|
238
|
-
case 39:
|
|
239
|
-
case 92:
|
|
240
|
-
case 47:
|
|
241
|
-
chars.push(ch1);
|
|
242
|
-
break;
|
|
243
|
-
case 98:
|
|
244
|
-
chars.push(8);
|
|
245
|
-
break;
|
|
246
|
-
case 116:
|
|
247
|
-
chars.push(9);
|
|
248
|
-
break;
|
|
249
|
-
case 110:
|
|
250
|
-
chars.push(10);
|
|
251
|
-
break;
|
|
252
|
-
case 102:
|
|
253
|
-
chars.push(12);
|
|
254
|
-
break;
|
|
255
|
-
case 114:
|
|
256
|
-
chars.push(13);
|
|
257
|
-
break;
|
|
258
|
-
case 117:
|
|
259
|
-
chars.push(readu4());
|
|
260
|
-
break;
|
|
261
|
-
default:
|
|
262
|
-
throw new Error(`${ decodeErrPrefix } unexpected string escape character at position ${ this.pos }`);
|
|
263
|
-
}
|
|
264
|
-
break;
|
|
265
|
-
case 34:
|
|
266
|
-
this.pos++;
|
|
267
|
-
return new Token(Type.string, decodeCodePointsArray(chars), this.pos - startPos);
|
|
268
|
-
default:
|
|
269
|
-
if (ch < 32) {
|
|
270
|
-
throw new Error(`${ decodeErrPrefix } invalid control character at position ${ this.pos }`);
|
|
271
|
-
} else if (ch < 128) {
|
|
272
|
-
chars.push(ch);
|
|
273
|
-
this.pos++;
|
|
274
|
-
} else {
|
|
275
|
-
readUtf8Char();
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
throw new Error(`${ decodeErrPrefix } unexpected end of string at position ${ this.pos }`);
|
|
280
|
-
}
|
|
281
|
-
parseValue() {
|
|
282
|
-
switch (this.ch()) {
|
|
283
|
-
case 123:
|
|
284
|
-
this.modeStack.push('obj-start');
|
|
285
|
-
this.pos++;
|
|
286
|
-
return new Token(Type.map, Infinity, 1);
|
|
287
|
-
case 91:
|
|
288
|
-
this.modeStack.push('array-start');
|
|
289
|
-
this.pos++;
|
|
290
|
-
return new Token(Type.array, Infinity, 1);
|
|
291
|
-
case 34: {
|
|
292
|
-
return this.parseString();
|
|
293
|
-
}
|
|
294
|
-
case 110:
|
|
295
|
-
this.expect([
|
|
296
|
-
110,
|
|
297
|
-
117,
|
|
298
|
-
108,
|
|
299
|
-
108
|
|
300
|
-
]);
|
|
301
|
-
return new Token(Type.null, null, 4);
|
|
302
|
-
case 102:
|
|
303
|
-
this.expect([
|
|
304
|
-
102,
|
|
305
|
-
97,
|
|
306
|
-
108,
|
|
307
|
-
115,
|
|
308
|
-
101
|
|
309
|
-
]);
|
|
310
|
-
return new Token(Type.false, false, 5);
|
|
311
|
-
case 116:
|
|
312
|
-
this.expect([
|
|
313
|
-
116,
|
|
314
|
-
114,
|
|
315
|
-
117,
|
|
316
|
-
101
|
|
317
|
-
]);
|
|
318
|
-
return new Token(Type.true, true, 4);
|
|
319
|
-
case 45:
|
|
320
|
-
case 48:
|
|
321
|
-
case 49:
|
|
322
|
-
case 50:
|
|
323
|
-
case 51:
|
|
324
|
-
case 52:
|
|
325
|
-
case 53:
|
|
326
|
-
case 54:
|
|
327
|
-
case 55:
|
|
328
|
-
case 56:
|
|
329
|
-
case 57:
|
|
330
|
-
return this.parseNumber();
|
|
331
|
-
default:
|
|
332
|
-
throw new Error(`${ decodeErrPrefix } unexpected character at position ${ this.pos }`);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
next() {
|
|
336
|
-
this.skipWhitespace();
|
|
337
|
-
switch (this.currentMode()) {
|
|
338
|
-
case 'value':
|
|
339
|
-
this.modeStack.pop();
|
|
340
|
-
return this.parseValue();
|
|
341
|
-
case 'array-value': {
|
|
342
|
-
this.modeStack.pop();
|
|
343
|
-
if (this.ch() === 93) {
|
|
344
|
-
this.pos++;
|
|
345
|
-
this.skipWhitespace();
|
|
346
|
-
return new Token(Type.break, undefined, 1);
|
|
347
|
-
}
|
|
348
|
-
if (this.ch() !== 44) {
|
|
349
|
-
throw new Error(`${ decodeErrPrefix } unexpected character at position ${ this.pos }, was expecting array delimiter but found '${ String.fromCharCode(this.ch()) }'`);
|
|
350
|
-
}
|
|
351
|
-
this.pos++;
|
|
352
|
-
this.modeStack.push('array-value');
|
|
353
|
-
this.skipWhitespace();
|
|
354
|
-
return this.parseValue();
|
|
355
|
-
}
|
|
356
|
-
case 'array-start': {
|
|
357
|
-
this.modeStack.pop();
|
|
358
|
-
if (this.ch() === 93) {
|
|
359
|
-
this.pos++;
|
|
360
|
-
this.skipWhitespace();
|
|
361
|
-
return new Token(Type.break, undefined, 1);
|
|
362
|
-
}
|
|
363
|
-
this.modeStack.push('array-value');
|
|
364
|
-
this.skipWhitespace();
|
|
365
|
-
return this.parseValue();
|
|
366
|
-
}
|
|
367
|
-
case 'obj-key':
|
|
368
|
-
if (this.ch() === 125) {
|
|
369
|
-
this.modeStack.pop();
|
|
370
|
-
this.pos++;
|
|
371
|
-
this.skipWhitespace();
|
|
372
|
-
return new Token(Type.break, undefined, 1);
|
|
373
|
-
}
|
|
374
|
-
if (this.ch() !== 44) {
|
|
375
|
-
throw new Error(`${ decodeErrPrefix } unexpected character at position ${ this.pos }, was expecting object delimiter but found '${ String.fromCharCode(this.ch()) }'`);
|
|
376
|
-
}
|
|
377
|
-
this.pos++;
|
|
378
|
-
this.skipWhitespace();
|
|
379
|
-
case 'obj-start': {
|
|
380
|
-
this.modeStack.pop();
|
|
381
|
-
if (this.ch() === 125) {
|
|
382
|
-
this.pos++;
|
|
383
|
-
this.skipWhitespace();
|
|
384
|
-
return new Token(Type.break, undefined, 1);
|
|
385
|
-
}
|
|
386
|
-
const token = this.parseString();
|
|
387
|
-
this.skipWhitespace();
|
|
388
|
-
if (this.ch() !== 58) {
|
|
389
|
-
throw new Error(`${ decodeErrPrefix } unexpected character at position ${ this.pos }, was expecting key/value delimiter ':' but found '${ String.fromCharCode(this.ch()) }'`);
|
|
390
|
-
}
|
|
391
|
-
this.pos++;
|
|
392
|
-
this.modeStack.push('obj-value');
|
|
393
|
-
return token;
|
|
394
|
-
}
|
|
395
|
-
case 'obj-value': {
|
|
396
|
-
this.modeStack.pop();
|
|
397
|
-
this.modeStack.push('obj-key');
|
|
398
|
-
this.skipWhitespace();
|
|
399
|
-
return this.parseValue();
|
|
400
|
-
}
|
|
401
|
-
default:
|
|
402
|
-
throw new Error(`${ decodeErrPrefix } unexpected parse state at position ${ this.pos }; this shouldn't happen`);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
function decode(data, options) {
|
|
407
|
-
options = Object.assign({ tokenizer: new Tokenizer(data, options) }, options);
|
|
408
|
-
return _decode(data, options);
|
|
409
|
-
}
|
|
410
|
-
export {
|
|
411
|
-
decode,
|
|
412
|
-
Tokenizer
|
|
413
|
-
};
|
package/esm/lib/json/encode.js
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { Type } from '../token.js';
|
|
2
|
-
import { encodeCustom } from '../encode.js';
|
|
3
|
-
import { encodeErrPrefix } from '../common.js';
|
|
4
|
-
import {
|
|
5
|
-
asU8A,
|
|
6
|
-
fromString
|
|
7
|
-
} from '../byte-utils.js';
|
|
8
|
-
class JSONEncoder extends Array {
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
11
|
-
this.inRecursive = [];
|
|
12
|
-
}
|
|
13
|
-
prefix(buf) {
|
|
14
|
-
const recurs = this.inRecursive[this.inRecursive.length - 1];
|
|
15
|
-
if (recurs) {
|
|
16
|
-
if (recurs.type === Type.array) {
|
|
17
|
-
recurs.elements++;
|
|
18
|
-
if (recurs.elements !== 1) {
|
|
19
|
-
buf.push([44]);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
if (recurs.type === Type.map) {
|
|
23
|
-
recurs.elements++;
|
|
24
|
-
if (recurs.elements !== 1) {
|
|
25
|
-
if (recurs.elements % 2 === 1) {
|
|
26
|
-
buf.push([44]);
|
|
27
|
-
} else {
|
|
28
|
-
buf.push([58]);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
[Type.uint.major](buf, token) {
|
|
35
|
-
this.prefix(buf);
|
|
36
|
-
const is = String(token.value);
|
|
37
|
-
const isa = [];
|
|
38
|
-
for (let i = 0; i < is.length; i++) {
|
|
39
|
-
isa[i] = is.charCodeAt(i);
|
|
40
|
-
}
|
|
41
|
-
buf.push(isa);
|
|
42
|
-
}
|
|
43
|
-
[Type.negint.major](buf, token) {
|
|
44
|
-
this[Type.uint.major](buf, token);
|
|
45
|
-
}
|
|
46
|
-
[Type.bytes.major](_buf, _token) {
|
|
47
|
-
throw new Error(`${ encodeErrPrefix } unsupported type: Uint8Array`);
|
|
48
|
-
}
|
|
49
|
-
[Type.string.major](buf, token) {
|
|
50
|
-
this.prefix(buf);
|
|
51
|
-
const byts = fromString(JSON.stringify(token.value));
|
|
52
|
-
buf.push(byts.length > 32 ? asU8A(byts) : byts);
|
|
53
|
-
}
|
|
54
|
-
[Type.array.major](buf, _token) {
|
|
55
|
-
this.prefix(buf);
|
|
56
|
-
this.inRecursive.push({
|
|
57
|
-
type: Type.array,
|
|
58
|
-
elements: 0
|
|
59
|
-
});
|
|
60
|
-
buf.push([91]);
|
|
61
|
-
}
|
|
62
|
-
[Type.map.major](buf, _token) {
|
|
63
|
-
this.prefix(buf);
|
|
64
|
-
this.inRecursive.push({
|
|
65
|
-
type: Type.map,
|
|
66
|
-
elements: 0
|
|
67
|
-
});
|
|
68
|
-
buf.push([123]);
|
|
69
|
-
}
|
|
70
|
-
[Type.tag.major](_buf, _token) {
|
|
71
|
-
}
|
|
72
|
-
[Type.float.major](buf, token) {
|
|
73
|
-
if (token.type.name === 'break') {
|
|
74
|
-
const recurs = this.inRecursive.pop();
|
|
75
|
-
if (recurs) {
|
|
76
|
-
if (recurs.type === Type.array) {
|
|
77
|
-
buf.push([93]);
|
|
78
|
-
} else if (recurs.type === Type.map) {
|
|
79
|
-
buf.push([125]);
|
|
80
|
-
} else {
|
|
81
|
-
throw new Error('Unexpected recursive type; this should not happen!');
|
|
82
|
-
}
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
throw new Error('Unexpected break; this should not happen!');
|
|
86
|
-
}
|
|
87
|
-
if (token.value === undefined) {
|
|
88
|
-
throw new Error(`${ encodeErrPrefix } unsupported type: undefined`);
|
|
89
|
-
}
|
|
90
|
-
this.prefix(buf);
|
|
91
|
-
if (token.type.name === 'true') {
|
|
92
|
-
buf.push([
|
|
93
|
-
116,
|
|
94
|
-
114,
|
|
95
|
-
117,
|
|
96
|
-
101
|
|
97
|
-
]);
|
|
98
|
-
return;
|
|
99
|
-
} else if (token.type.name === 'false') {
|
|
100
|
-
buf.push([
|
|
101
|
-
102,
|
|
102
|
-
97,
|
|
103
|
-
108,
|
|
104
|
-
115,
|
|
105
|
-
101
|
|
106
|
-
]);
|
|
107
|
-
return;
|
|
108
|
-
} else if (token.type.name === 'null') {
|
|
109
|
-
buf.push([
|
|
110
|
-
110,
|
|
111
|
-
117,
|
|
112
|
-
108,
|
|
113
|
-
108
|
|
114
|
-
]);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
const is = String(token.value);
|
|
118
|
-
const isa = [];
|
|
119
|
-
let dp = false;
|
|
120
|
-
for (let i = 0; i < is.length; i++) {
|
|
121
|
-
isa[i] = is.charCodeAt(i);
|
|
122
|
-
if (!dp && (isa[i] === 46 || isa[i] === 101 || isa[i] === 69)) {
|
|
123
|
-
dp = true;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (!dp) {
|
|
127
|
-
isa.push(46);
|
|
128
|
-
isa.push(48);
|
|
129
|
-
}
|
|
130
|
-
buf.push(isa);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
function mapSorter(e1, e2) {
|
|
134
|
-
if (Array.isArray(e1[0]) || Array.isArray(e2[0])) {
|
|
135
|
-
throw new Error(`${ encodeErrPrefix } complex map keys are not supported`);
|
|
136
|
-
}
|
|
137
|
-
const keyToken1 = e1[0];
|
|
138
|
-
const keyToken2 = e2[0];
|
|
139
|
-
if (keyToken1.type !== Type.string || keyToken2.type !== Type.string) {
|
|
140
|
-
throw new Error(`${ encodeErrPrefix } non-string map keys are not supported`);
|
|
141
|
-
}
|
|
142
|
-
if (keyToken1 < keyToken2) {
|
|
143
|
-
return -1;
|
|
144
|
-
}
|
|
145
|
-
if (keyToken1 > keyToken2) {
|
|
146
|
-
return 1;
|
|
147
|
-
}
|
|
148
|
-
throw new Error(`${ encodeErrPrefix } unexpected duplicate map keys, this is not supported`);
|
|
149
|
-
}
|
|
150
|
-
const defaultEncodeOptions = {
|
|
151
|
-
addBreakTokens: true,
|
|
152
|
-
mapSorter
|
|
153
|
-
};
|
|
154
|
-
function encode(data, options) {
|
|
155
|
-
options = Object.assign({}, defaultEncodeOptions, options);
|
|
156
|
-
return encodeCustom(data, new JSONEncoder(), options);
|
|
157
|
-
}
|
|
158
|
-
export {
|
|
159
|
-
encode
|
|
160
|
-
};
|