@atproto/lex-json 0.1.1 → 0.1.3
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/CHANGELOG.md +22 -0
- package/dist/blob.d.ts +1 -1
- package/dist/blob.d.ts.map +1 -1
- package/dist/blob.js +1 -1
- package/dist/blob.js.map +1 -1
- package/dist/bytes.d.ts +1 -1
- package/dist/bytes.d.ts.map +1 -1
- package/dist/bytes.js.map +1 -1
- package/dist/json-bytes-decoder.d.ts +1 -1
- package/dist/json-bytes-decoder.d.ts.map +1 -1
- package/dist/json-bytes-decoder.js.map +1 -1
- package/dist/lex-json.d.ts +2 -2
- package/dist/lex-json.d.ts.map +1 -1
- package/dist/lex-json.js +1 -1
- package/dist/lex-json.js.map +1 -1
- package/dist/link.d.ts +2 -2
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +1 -1
- package/dist/link.js.map +1 -1
- package/package.json +5 -9
- package/src/blob.ts +0 -70
- package/src/bytes.test.ts +0 -55
- package/src/bytes.ts +0 -73
- package/src/index.ts +0 -4
- package/src/json-bytes-decoder.bench.ts +0 -252
- package/src/json-bytes-decoder.test.ts +0 -889
- package/src/json-bytes-decoder.ts +0 -672
- package/src/json.ts +0 -48
- package/src/lex-json.bench.ts +0 -125
- package/src/lex-json.test.ts +0 -991
- package/src/lex-json.ts +0 -352
- package/src/link.ts +0 -101
- package/tsconfig.build.json +0 -11
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -8
package/src/lex-json.test.ts
DELETED
|
@@ -1,991 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest'
|
|
2
|
-
import { LexValue, lexEquals, parseCid } from '@atproto/lex-data'
|
|
3
|
-
import { JsonValue } from './json.js'
|
|
4
|
-
import {
|
|
5
|
-
jsonToLex,
|
|
6
|
-
lexParse,
|
|
7
|
-
lexParseJsonBytes,
|
|
8
|
-
lexStringify,
|
|
9
|
-
lexToJson,
|
|
10
|
-
} from './lex-json.js'
|
|
11
|
-
|
|
12
|
-
export const validVectors: Array<{
|
|
13
|
-
name: string
|
|
14
|
-
json: JsonValue
|
|
15
|
-
lex: LexValue
|
|
16
|
-
}> = [
|
|
17
|
-
{
|
|
18
|
-
name: 'pure json',
|
|
19
|
-
json: {
|
|
20
|
-
string: 'abc',
|
|
21
|
-
unicode: 'a~öñ©⽘☎𓋓😀👨👩👧👧',
|
|
22
|
-
integer: 123,
|
|
23
|
-
bool: true,
|
|
24
|
-
null: null,
|
|
25
|
-
array: ['abc', 'def', 'ghi'],
|
|
26
|
-
object: {
|
|
27
|
-
string: 'abc',
|
|
28
|
-
number: 123,
|
|
29
|
-
bool: true,
|
|
30
|
-
arr: ['abc', 'def', 'ghi'],
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
lex: {
|
|
34
|
-
string: 'abc',
|
|
35
|
-
unicode: 'a~öñ©⽘☎𓋓😀👨👩👧👧',
|
|
36
|
-
integer: 123,
|
|
37
|
-
bool: true,
|
|
38
|
-
null: null,
|
|
39
|
-
array: ['abc', 'def', 'ghi'],
|
|
40
|
-
object: {
|
|
41
|
-
string: 'abc',
|
|
42
|
-
number: 123,
|
|
43
|
-
bool: true,
|
|
44
|
-
arr: ['abc', 'def', 'ghi'],
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
name: 'lex data',
|
|
50
|
-
json: {
|
|
51
|
-
a: {
|
|
52
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
53
|
-
},
|
|
54
|
-
b: {
|
|
55
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
56
|
-
},
|
|
57
|
-
c: {
|
|
58
|
-
$type: 'blob',
|
|
59
|
-
ref: {
|
|
60
|
-
$link: 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
61
|
-
},
|
|
62
|
-
mimeType: 'image/jpeg',
|
|
63
|
-
size: 10000,
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
lex: {
|
|
67
|
-
a: parseCid(
|
|
68
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
69
|
-
),
|
|
70
|
-
b: new Uint8Array([
|
|
71
|
-
156, 81, 17, 142, 242, 203, 139, 15, 106, 155, 142, 73, 174, 161, 253,
|
|
72
|
-
65, 60, 242, 11, 98, 238, 213, 118, 248, 157, 238, 190, 176, 26, 194,
|
|
73
|
-
204, 141,
|
|
74
|
-
]),
|
|
75
|
-
c: {
|
|
76
|
-
$type: 'blob',
|
|
77
|
-
ref: parseCid(
|
|
78
|
-
'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
79
|
-
),
|
|
80
|
-
mimeType: 'image/jpeg',
|
|
81
|
-
size: 10000,
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
name: 'lexArray',
|
|
87
|
-
json: [
|
|
88
|
-
{
|
|
89
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
$link: 'bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q',
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
$link: 'bafyreiaizynclnqiolq7byfpjjtgqzn4sfrsgn7z2hhf6bo4utdwkin7ke',
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
$link: 'bafyreifd4w4tcr5tluxz7osjtnofffvtsmgdqcfrfi6evjde4pl27lrjpy',
|
|
99
|
-
},
|
|
100
|
-
],
|
|
101
|
-
lex: [
|
|
102
|
-
parseCid('bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'),
|
|
103
|
-
parseCid('bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q'),
|
|
104
|
-
parseCid('bafyreiaizynclnqiolq7byfpjjtgqzn4sfrsgn7z2hhf6bo4utdwkin7ke'),
|
|
105
|
-
parseCid('bafyreifd4w4tcr5tluxz7osjtnofffvtsmgdqcfrfi6evjde4pl27lrjpy'),
|
|
106
|
-
],
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
name: 'root cid',
|
|
110
|
-
json: {
|
|
111
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
112
|
-
},
|
|
113
|
-
lex: parseCid(
|
|
114
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
115
|
-
),
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
name: 'root bytes',
|
|
119
|
-
json: {
|
|
120
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
121
|
-
},
|
|
122
|
-
lex: new Uint8Array([
|
|
123
|
-
156, 81, 17, 142, 242, 203, 139, 15, 106, 155, 142, 73, 174, 161, 253, 65,
|
|
124
|
-
60, 242, 11, 98, 238, 213, 118, 248, 157, 238, 190, 176, 26, 194, 204,
|
|
125
|
-
141,
|
|
126
|
-
]),
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
name: 'lexNested',
|
|
130
|
-
json: {
|
|
131
|
-
a: {
|
|
132
|
-
b: [
|
|
133
|
-
{
|
|
134
|
-
d: [
|
|
135
|
-
{
|
|
136
|
-
$link:
|
|
137
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
$link:
|
|
141
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
e: [
|
|
145
|
-
{
|
|
146
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
147
|
-
},
|
|
148
|
-
{
|
|
149
|
-
$bytes: 'iE+sPoHobU9tSIqGI+309LLCcWQIRmEXwxcoDt19tas',
|
|
150
|
-
},
|
|
151
|
-
],
|
|
152
|
-
},
|
|
153
|
-
],
|
|
154
|
-
},
|
|
155
|
-
},
|
|
156
|
-
lex: {
|
|
157
|
-
a: {
|
|
158
|
-
b: [
|
|
159
|
-
{
|
|
160
|
-
d: [
|
|
161
|
-
parseCid(
|
|
162
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
163
|
-
),
|
|
164
|
-
parseCid(
|
|
165
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
166
|
-
),
|
|
167
|
-
],
|
|
168
|
-
e: [
|
|
169
|
-
new Uint8Array([
|
|
170
|
-
156, 81, 17, 142, 242, 203, 139, 15, 106, 155, 142, 73, 174,
|
|
171
|
-
161, 253, 65, 60, 242, 11, 98, 238, 213, 118, 248, 157, 238,
|
|
172
|
-
190, 176, 26, 194, 204, 141,
|
|
173
|
-
]),
|
|
174
|
-
new Uint8Array([
|
|
175
|
-
136, 79, 172, 62, 129, 232, 109, 79, 109, 72, 138, 134, 35, 237,
|
|
176
|
-
244, 244, 178, 194, 113, 100, 8, 70, 97, 23, 195, 23, 40, 14,
|
|
177
|
-
221, 125, 181, 171,
|
|
178
|
-
]),
|
|
179
|
-
],
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
},
|
|
185
|
-
{
|
|
186
|
-
name: 'empty structures',
|
|
187
|
-
json: {
|
|
188
|
-
emptyObject: {},
|
|
189
|
-
emptyArray: [],
|
|
190
|
-
emtyBytes: { $bytes: '' },
|
|
191
|
-
},
|
|
192
|
-
lex: {
|
|
193
|
-
emptyObject: {},
|
|
194
|
-
emptyArray: [],
|
|
195
|
-
emtyBytes: new Uint8Array([]),
|
|
196
|
-
},
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
name: 'mixed types in array',
|
|
200
|
-
json: {
|
|
201
|
-
arr: [
|
|
202
|
-
'string',
|
|
203
|
-
123,
|
|
204
|
-
true,
|
|
205
|
-
null,
|
|
206
|
-
{
|
|
207
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
208
|
-
},
|
|
209
|
-
{ $bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0' },
|
|
210
|
-
{ nested: 'object' },
|
|
211
|
-
['nested', 'array'],
|
|
212
|
-
],
|
|
213
|
-
},
|
|
214
|
-
lex: {
|
|
215
|
-
arr: [
|
|
216
|
-
'string',
|
|
217
|
-
123,
|
|
218
|
-
true,
|
|
219
|
-
null,
|
|
220
|
-
parseCid('bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'),
|
|
221
|
-
new Uint8Array([
|
|
222
|
-
156, 81, 17, 142, 242, 203, 139, 15, 106, 155, 142, 73, 174, 161, 253,
|
|
223
|
-
65, 60, 242, 11, 98, 238, 213, 118, 248, 157, 238, 190, 176, 26, 194,
|
|
224
|
-
204, 141,
|
|
225
|
-
]),
|
|
226
|
-
{ nested: 'object' },
|
|
227
|
-
['nested', 'array'],
|
|
228
|
-
],
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
{
|
|
232
|
-
name: "mismatched order in object doesn't affect equality",
|
|
233
|
-
json: {
|
|
234
|
-
a: 'valueA',
|
|
235
|
-
b: 'valueB',
|
|
236
|
-
c: {
|
|
237
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
238
|
-
},
|
|
239
|
-
d: {
|
|
240
|
-
a: 'valueA',
|
|
241
|
-
b: 'valueB',
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
lex: {
|
|
245
|
-
c: parseCid(
|
|
246
|
-
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
247
|
-
),
|
|
248
|
-
d: {
|
|
249
|
-
b: 'valueB',
|
|
250
|
-
a: 'valueA',
|
|
251
|
-
},
|
|
252
|
-
a: 'valueA',
|
|
253
|
-
b: 'valueB',
|
|
254
|
-
},
|
|
255
|
-
},
|
|
256
|
-
]
|
|
257
|
-
|
|
258
|
-
export const acceptableVectors: Array<{
|
|
259
|
-
note: string
|
|
260
|
-
json: JsonValue
|
|
261
|
-
}> = [
|
|
262
|
-
{
|
|
263
|
-
note: 'non string $type',
|
|
264
|
-
json: {
|
|
265
|
-
$type: 3124,
|
|
266
|
-
foo: 'bar',
|
|
267
|
-
},
|
|
268
|
-
},
|
|
269
|
-
{
|
|
270
|
-
note: 'object with float values',
|
|
271
|
-
json: {
|
|
272
|
-
a: 1.5,
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
{
|
|
276
|
-
note: 'blob with wrong field type',
|
|
277
|
-
json: {
|
|
278
|
-
$type: 'blob',
|
|
279
|
-
ref: 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
280
|
-
mimeType: 'image/jpeg',
|
|
281
|
-
size: 10000,
|
|
282
|
-
},
|
|
283
|
-
},
|
|
284
|
-
{
|
|
285
|
-
note: 'blob with missing key',
|
|
286
|
-
json: {
|
|
287
|
-
$type: 'blob',
|
|
288
|
-
mimeType: 'image/jpeg',
|
|
289
|
-
size: 10000,
|
|
290
|
-
},
|
|
291
|
-
},
|
|
292
|
-
{
|
|
293
|
-
note: 'blob with extra fields',
|
|
294
|
-
json: {
|
|
295
|
-
$type: 'blob',
|
|
296
|
-
ref: {
|
|
297
|
-
$link: 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
298
|
-
},
|
|
299
|
-
mimeType: 'image/jpeg',
|
|
300
|
-
size: 10000,
|
|
301
|
-
other: 'blah',
|
|
302
|
-
},
|
|
303
|
-
},
|
|
304
|
-
{
|
|
305
|
-
note: 'bytes with extra fields',
|
|
306
|
-
json: {
|
|
307
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
308
|
-
other: 'blah',
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
{
|
|
312
|
-
note: 'link with extra fields',
|
|
313
|
-
json: {
|
|
314
|
-
$link: 'bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity',
|
|
315
|
-
other: 'blah',
|
|
316
|
-
},
|
|
317
|
-
},
|
|
318
|
-
{
|
|
319
|
-
note: '$bytes and $link',
|
|
320
|
-
json: {
|
|
321
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
322
|
-
$link: 'bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity',
|
|
323
|
-
},
|
|
324
|
-
},
|
|
325
|
-
{
|
|
326
|
-
note: '$bytes and $type',
|
|
327
|
-
json: {
|
|
328
|
-
$type: 'bytes',
|
|
329
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
330
|
-
},
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
note: '$link and $type',
|
|
334
|
-
json: {
|
|
335
|
-
$type: 'blob',
|
|
336
|
-
$link: 'bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity',
|
|
337
|
-
},
|
|
338
|
-
},
|
|
339
|
-
{
|
|
340
|
-
note: 'blob with CBOR CID ref',
|
|
341
|
-
json: {
|
|
342
|
-
$type: 'blob',
|
|
343
|
-
ref: {
|
|
344
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
345
|
-
},
|
|
346
|
-
mimeType: 'image/png',
|
|
347
|
-
size: 1,
|
|
348
|
-
},
|
|
349
|
-
},
|
|
350
|
-
{
|
|
351
|
-
note: 'object with empty $type',
|
|
352
|
-
json: {
|
|
353
|
-
$type: '',
|
|
354
|
-
foo: 'bar',
|
|
355
|
-
},
|
|
356
|
-
},
|
|
357
|
-
]
|
|
358
|
-
|
|
359
|
-
export const invalidVectors: Array<{
|
|
360
|
-
note: string
|
|
361
|
-
json: JsonValue
|
|
362
|
-
}> = [
|
|
363
|
-
{
|
|
364
|
-
note: 'bytes with wrong field type',
|
|
365
|
-
json: {
|
|
366
|
-
$bytes: [1, 2, 3],
|
|
367
|
-
},
|
|
368
|
-
},
|
|
369
|
-
{
|
|
370
|
-
note: 'invalid base64 in $bytes',
|
|
371
|
-
json: {
|
|
372
|
-
$bytes: '🐻',
|
|
373
|
-
},
|
|
374
|
-
},
|
|
375
|
-
{
|
|
376
|
-
note: 'link with wrong field type',
|
|
377
|
-
json: {
|
|
378
|
-
$link: 1234,
|
|
379
|
-
},
|
|
380
|
-
},
|
|
381
|
-
{
|
|
382
|
-
note: 'link with bogus CID',
|
|
383
|
-
json: {
|
|
384
|
-
$link: '.',
|
|
385
|
-
},
|
|
386
|
-
},
|
|
387
|
-
]
|
|
388
|
-
|
|
389
|
-
describe(lexParse, () => {
|
|
390
|
-
describe('valid vectors', () => {
|
|
391
|
-
describe('strict mode', () => {
|
|
392
|
-
for (const { name, json, lex } of validVectors) {
|
|
393
|
-
test(name, () => {
|
|
394
|
-
expect(
|
|
395
|
-
lexEquals(lex, lexParse(JSON.stringify(json), { strict: true })),
|
|
396
|
-
).toBe(true)
|
|
397
|
-
})
|
|
398
|
-
}
|
|
399
|
-
})
|
|
400
|
-
describe('non-strict mode', () => {
|
|
401
|
-
for (const { name, json, lex } of validVectors) {
|
|
402
|
-
test(name, () => {
|
|
403
|
-
expect(
|
|
404
|
-
lexEquals(lex, lexParse(JSON.stringify(json), { strict: false })),
|
|
405
|
-
).toBe(true)
|
|
406
|
-
})
|
|
407
|
-
}
|
|
408
|
-
})
|
|
409
|
-
})
|
|
410
|
-
|
|
411
|
-
describe('acceptable vectors', () => {
|
|
412
|
-
describe('strict mode', () => {
|
|
413
|
-
for (const { note, json } of acceptableVectors) {
|
|
414
|
-
test(note, () => {
|
|
415
|
-
expect(() =>
|
|
416
|
-
lexParse(JSON.stringify(json), { strict: true }),
|
|
417
|
-
).toThrow()
|
|
418
|
-
})
|
|
419
|
-
}
|
|
420
|
-
})
|
|
421
|
-
describe('non-strict mode', () => {
|
|
422
|
-
for (const { note, json } of acceptableVectors) {
|
|
423
|
-
test(note, () => {
|
|
424
|
-
expect(() =>
|
|
425
|
-
lexParse(JSON.stringify(json), { strict: false }),
|
|
426
|
-
).not.toThrow()
|
|
427
|
-
})
|
|
428
|
-
}
|
|
429
|
-
})
|
|
430
|
-
})
|
|
431
|
-
|
|
432
|
-
describe('invalid vectors', () => {
|
|
433
|
-
describe('strict mode', () => {
|
|
434
|
-
for (const { note, json } of invalidVectors) {
|
|
435
|
-
test(note, () => {
|
|
436
|
-
expect(() =>
|
|
437
|
-
lexParse(JSON.stringify(json), { strict: true }),
|
|
438
|
-
).toThrow()
|
|
439
|
-
})
|
|
440
|
-
}
|
|
441
|
-
})
|
|
442
|
-
describe('non-strict mode', () => {
|
|
443
|
-
for (const { note, json } of invalidVectors) {
|
|
444
|
-
test(note, () => {
|
|
445
|
-
expect(() =>
|
|
446
|
-
lexParse(JSON.stringify(json), { strict: false }),
|
|
447
|
-
).not.toThrow()
|
|
448
|
-
})
|
|
449
|
-
}
|
|
450
|
-
})
|
|
451
|
-
})
|
|
452
|
-
})
|
|
453
|
-
|
|
454
|
-
describe(lexParseJsonBytes, () => {
|
|
455
|
-
describe('valid vectors', () => {
|
|
456
|
-
describe('strict mode', () => {
|
|
457
|
-
describe('with pretty-printed JSON', () => {
|
|
458
|
-
for (const { name, json, lex } of validVectors) {
|
|
459
|
-
test(name, () => {
|
|
460
|
-
const jsonBytes = Buffer.from(JSON.stringify(json, undefined, 4))
|
|
461
|
-
expect(
|
|
462
|
-
lexEquals(lex, lexParseJsonBytes(jsonBytes, { strict: true })),
|
|
463
|
-
).toBe(true)
|
|
464
|
-
})
|
|
465
|
-
}
|
|
466
|
-
})
|
|
467
|
-
describe('with compact JSON', () => {
|
|
468
|
-
for (const { name, json, lex } of validVectors) {
|
|
469
|
-
test(name, () => {
|
|
470
|
-
const jsonBytes = Buffer.from(JSON.stringify(json))
|
|
471
|
-
expect(
|
|
472
|
-
lexEquals(lex, lexParseJsonBytes(jsonBytes, { strict: true })),
|
|
473
|
-
).toBe(true)
|
|
474
|
-
})
|
|
475
|
-
}
|
|
476
|
-
})
|
|
477
|
-
})
|
|
478
|
-
|
|
479
|
-
describe('non-strict mode', () => {
|
|
480
|
-
for (const { name, json, lex } of validVectors) {
|
|
481
|
-
test(name, () => {
|
|
482
|
-
const jsonBytes = Buffer.from(JSON.stringify(json))
|
|
483
|
-
expect(
|
|
484
|
-
lexEquals(lex, lexParseJsonBytes(jsonBytes, { strict: false })),
|
|
485
|
-
).toBe(true)
|
|
486
|
-
})
|
|
487
|
-
}
|
|
488
|
-
})
|
|
489
|
-
})
|
|
490
|
-
|
|
491
|
-
describe('acceptable vectors', () => {
|
|
492
|
-
describe('strict mode', () => {
|
|
493
|
-
for (const { note, json } of acceptableVectors) {
|
|
494
|
-
test(note, () => {
|
|
495
|
-
const jsonBytes = Buffer.from(JSON.stringify(json))
|
|
496
|
-
expect(() => lexParseJsonBytes(jsonBytes, { strict: true })).toThrow()
|
|
497
|
-
})
|
|
498
|
-
}
|
|
499
|
-
})
|
|
500
|
-
describe('non-strict mode', () => {
|
|
501
|
-
for (const { note, json } of acceptableVectors) {
|
|
502
|
-
test(note, () => {
|
|
503
|
-
const jsonBytes = Buffer.from(JSON.stringify(json))
|
|
504
|
-
expect(() =>
|
|
505
|
-
lexParseJsonBytes(jsonBytes, { strict: false }),
|
|
506
|
-
).not.toThrow()
|
|
507
|
-
})
|
|
508
|
-
}
|
|
509
|
-
})
|
|
510
|
-
})
|
|
511
|
-
|
|
512
|
-
describe('invalid vectors', () => {
|
|
513
|
-
describe('strict mode', () => {
|
|
514
|
-
for (const { note, json } of invalidVectors) {
|
|
515
|
-
test(note, () => {
|
|
516
|
-
const jsonBytes = Buffer.from(JSON.stringify(json))
|
|
517
|
-
expect(() => lexParseJsonBytes(jsonBytes, { strict: true })).toThrow()
|
|
518
|
-
})
|
|
519
|
-
}
|
|
520
|
-
})
|
|
521
|
-
describe('non-strict mode', () => {
|
|
522
|
-
for (const { note, json } of invalidVectors) {
|
|
523
|
-
test(note, () => {
|
|
524
|
-
const jsonBytes = Buffer.from(JSON.stringify(json))
|
|
525
|
-
expect(() =>
|
|
526
|
-
lexParseJsonBytes(jsonBytes, { strict: false }),
|
|
527
|
-
).not.toThrow()
|
|
528
|
-
})
|
|
529
|
-
}
|
|
530
|
-
})
|
|
531
|
-
})
|
|
532
|
-
})
|
|
533
|
-
|
|
534
|
-
describe(lexStringify, () => {
|
|
535
|
-
describe('valid vectors', () => {
|
|
536
|
-
for (const { name, json, lex } of validVectors) {
|
|
537
|
-
test(name, () => {
|
|
538
|
-
expect(JSON.parse(lexStringify(lex))).toStrictEqual(json)
|
|
539
|
-
})
|
|
540
|
-
}
|
|
541
|
-
})
|
|
542
|
-
})
|
|
543
|
-
|
|
544
|
-
describe(jsonToLex, () => {
|
|
545
|
-
describe('valid vectors', () => {
|
|
546
|
-
describe('strict mode', () => {
|
|
547
|
-
for (const { name, json, lex } of validVectors) {
|
|
548
|
-
test(name, () => {
|
|
549
|
-
expect(lexEquals(jsonToLex(json, { strict: true }), lex)).toBe(true)
|
|
550
|
-
expect(lexEquals(lex, jsonToLex(json, { strict: true }))).toBe(true)
|
|
551
|
-
})
|
|
552
|
-
}
|
|
553
|
-
})
|
|
554
|
-
|
|
555
|
-
describe('non-strict mode', () => {
|
|
556
|
-
for (const { name, json, lex } of validVectors) {
|
|
557
|
-
test(name, () => {
|
|
558
|
-
expect(lexEquals(jsonToLex(json, { strict: false }), lex)).toBe(true)
|
|
559
|
-
expect(lexEquals(lex, jsonToLex(json, { strict: false }))).toBe(true)
|
|
560
|
-
})
|
|
561
|
-
}
|
|
562
|
-
})
|
|
563
|
-
})
|
|
564
|
-
|
|
565
|
-
describe('acceptable vectors', () => {
|
|
566
|
-
describe('strict mode', () => {
|
|
567
|
-
for (const { note, json } of acceptableVectors) {
|
|
568
|
-
test(note, () => {
|
|
569
|
-
expect(() => jsonToLex(json, { strict: true })).toThrow()
|
|
570
|
-
})
|
|
571
|
-
}
|
|
572
|
-
})
|
|
573
|
-
|
|
574
|
-
describe('non-strict mode', () => {
|
|
575
|
-
for (const { note, json } of acceptableVectors) {
|
|
576
|
-
test(note, () => {
|
|
577
|
-
expect(() => jsonToLex(json, { strict: false })).not.toThrow()
|
|
578
|
-
})
|
|
579
|
-
}
|
|
580
|
-
})
|
|
581
|
-
})
|
|
582
|
-
|
|
583
|
-
describe('invalid vectors', () => {
|
|
584
|
-
describe('strict mode', () => {
|
|
585
|
-
for (const { note, json } of invalidVectors) {
|
|
586
|
-
test(note, () => {
|
|
587
|
-
expect(() => jsonToLex(json, { strict: true })).toThrow()
|
|
588
|
-
})
|
|
589
|
-
}
|
|
590
|
-
})
|
|
591
|
-
describe('non-strict mode', () => {
|
|
592
|
-
for (const { note, json } of invalidVectors) {
|
|
593
|
-
test(note, () => {
|
|
594
|
-
expect(() => jsonToLex(json, { strict: false })).not.toThrow()
|
|
595
|
-
})
|
|
596
|
-
}
|
|
597
|
-
})
|
|
598
|
-
})
|
|
599
|
-
})
|
|
600
|
-
|
|
601
|
-
describe(lexToJson, () => {
|
|
602
|
-
describe('valid vectors', () => {
|
|
603
|
-
for (const { name, json, lex } of validVectors) {
|
|
604
|
-
test(name, () => {
|
|
605
|
-
expect(lexToJson(lex)).toStrictEqual(json)
|
|
606
|
-
})
|
|
607
|
-
}
|
|
608
|
-
})
|
|
609
|
-
})
|
|
610
|
-
|
|
611
|
-
describe('json > lex > json', () => {
|
|
612
|
-
describe('valid vectors', () => {
|
|
613
|
-
for (const { name, json } of validVectors) {
|
|
614
|
-
test(name, () => {
|
|
615
|
-
expect(lexToJson(jsonToLex(json))).toStrictEqual(json)
|
|
616
|
-
})
|
|
617
|
-
}
|
|
618
|
-
})
|
|
619
|
-
})
|
|
620
|
-
|
|
621
|
-
describe('json (binary) > lex > json', () => {
|
|
622
|
-
describe('valid vectors', () => {
|
|
623
|
-
for (const { name, json } of validVectors) {
|
|
624
|
-
test(name, () => {
|
|
625
|
-
const jsonBytes = Buffer.from(JSON.stringify(json, undefined, 4))
|
|
626
|
-
expect(lexToJson(lexParseJsonBytes(jsonBytes))).toStrictEqual(json)
|
|
627
|
-
})
|
|
628
|
-
}
|
|
629
|
-
})
|
|
630
|
-
})
|
|
631
|
-
|
|
632
|
-
describe('lex > json > lex', () => {
|
|
633
|
-
describe('valid vectors', () => {
|
|
634
|
-
for (const { name, lex } of validVectors) {
|
|
635
|
-
test(name, () => {
|
|
636
|
-
expect(lexEquals(jsonToLex(lexToJson(lex)), lex)).toBe(true)
|
|
637
|
-
expect(lexEquals(lex, jsonToLex(lexToJson(lex)))).toBe(true)
|
|
638
|
-
})
|
|
639
|
-
}
|
|
640
|
-
})
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
describe('lexParseJsonBytes strict mode error parity with lexParse', () => {
|
|
644
|
-
describe('invalid JSON input throws SyntaxError containing "Unexpected token"', () => {
|
|
645
|
-
test('lexParse throws with Unexpected token', () => {
|
|
646
|
-
expect(() => lexParse('not valid json', { strict: true })).toThrow(
|
|
647
|
-
/Unexpected token/,
|
|
648
|
-
)
|
|
649
|
-
})
|
|
650
|
-
|
|
651
|
-
test('lexParseJsonBytes throws with Unexpected token', () => {
|
|
652
|
-
expect(() =>
|
|
653
|
-
lexParseJsonBytes(Buffer.from('not valid json'), { strict: true }),
|
|
654
|
-
).toThrow(/Unexpected token/)
|
|
655
|
-
})
|
|
656
|
-
|
|
657
|
-
test('lexParseJsonBytes non-strict also throws with Unexpected token for invalid JSON', () => {
|
|
658
|
-
expect(() =>
|
|
659
|
-
lexParseJsonBytes(Buffer.from('not valid json'), { strict: false }),
|
|
660
|
-
).toThrow(/Unexpected token/)
|
|
661
|
-
})
|
|
662
|
-
})
|
|
663
|
-
|
|
664
|
-
describe('float numbers: strict throws TypeError, non-strict accepts', () => {
|
|
665
|
-
const jsonStr = '{"value":1.5}'
|
|
666
|
-
|
|
667
|
-
test('lexParse strict throws TypeError with value in message', () => {
|
|
668
|
-
expect(() => lexParse(jsonStr, { strict: true })).toThrow(TypeError)
|
|
669
|
-
expect(() => lexParse(jsonStr, { strict: true })).toThrow(
|
|
670
|
-
'Invalid non-integer number: 1.5',
|
|
671
|
-
)
|
|
672
|
-
})
|
|
673
|
-
|
|
674
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
675
|
-
const bytes = Buffer.from(jsonStr)
|
|
676
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
677
|
-
TypeError,
|
|
678
|
-
)
|
|
679
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
680
|
-
'Invalid non-integer number: 1.5',
|
|
681
|
-
)
|
|
682
|
-
})
|
|
683
|
-
|
|
684
|
-
test('lexParse non-strict accepts float', () => {
|
|
685
|
-
expect(() => lexParse(jsonStr, { strict: false })).not.toThrow()
|
|
686
|
-
})
|
|
687
|
-
|
|
688
|
-
test('lexParseJsonBytes non-strict accepts float', () => {
|
|
689
|
-
expect(() =>
|
|
690
|
-
lexParseJsonBytes(Buffer.from(jsonStr), { strict: false }),
|
|
691
|
-
).not.toThrow()
|
|
692
|
-
})
|
|
693
|
-
})
|
|
694
|
-
|
|
695
|
-
describe('exponent notation: safe integers accepted, unsafe integers rejected', () => {
|
|
696
|
-
test('lexParse strict accepts 1e10 (safe integer)', () => {
|
|
697
|
-
expect(lexParse('1e10', { strict: true })).toBe(1e10)
|
|
698
|
-
})
|
|
699
|
-
|
|
700
|
-
test('lexParseJsonBytes strict accepts 1e10 (safe integer)', () => {
|
|
701
|
-
expect(lexParseJsonBytes(Buffer.from('1e10'), { strict: true })).toBe(
|
|
702
|
-
1e10,
|
|
703
|
-
)
|
|
704
|
-
})
|
|
705
|
-
|
|
706
|
-
test('lexParse strict rejects 1e20 (unsafe integer)', () => {
|
|
707
|
-
expect(() => lexParse('1e20', { strict: true })).toThrow(TypeError)
|
|
708
|
-
})
|
|
709
|
-
|
|
710
|
-
test('lexParseJsonBytes strict rejects 1e20 (unsafe integer)', () => {
|
|
711
|
-
expect(() =>
|
|
712
|
-
lexParseJsonBytes(Buffer.from('1e20'), { strict: true }),
|
|
713
|
-
).toThrow(TypeError)
|
|
714
|
-
})
|
|
715
|
-
})
|
|
716
|
-
|
|
717
|
-
describe('invalid blob: strict throws TypeError, non-strict returns plain object', () => {
|
|
718
|
-
const invalidBlobJson = '{"$type":"blob"}'
|
|
719
|
-
|
|
720
|
-
test('lexParse strict throws TypeError with "Invalid blob object"', () => {
|
|
721
|
-
expect(() => lexParse(invalidBlobJson, { strict: true })).toThrow(
|
|
722
|
-
TypeError,
|
|
723
|
-
)
|
|
724
|
-
expect(() => lexParse(invalidBlobJson, { strict: true })).toThrow(
|
|
725
|
-
'Invalid blob object',
|
|
726
|
-
)
|
|
727
|
-
})
|
|
728
|
-
|
|
729
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
730
|
-
const bytes = Buffer.from(invalidBlobJson)
|
|
731
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
732
|
-
TypeError,
|
|
733
|
-
)
|
|
734
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
735
|
-
'Invalid blob object',
|
|
736
|
-
)
|
|
737
|
-
})
|
|
738
|
-
|
|
739
|
-
test('lexParse non-strict returns plain object', () => {
|
|
740
|
-
expect(() => lexParse(invalidBlobJson, { strict: false })).not.toThrow()
|
|
741
|
-
})
|
|
742
|
-
|
|
743
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
744
|
-
expect(() =>
|
|
745
|
-
lexParseJsonBytes(Buffer.from(invalidBlobJson), { strict: false }),
|
|
746
|
-
).not.toThrow()
|
|
747
|
-
})
|
|
748
|
-
})
|
|
749
|
-
|
|
750
|
-
describe('blob with CBOR CID: strict throws TypeError, non-strict returns plain object', () => {
|
|
751
|
-
const blobWithCborCidJson = JSON.stringify({
|
|
752
|
-
$type: 'blob',
|
|
753
|
-
ref: {
|
|
754
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
755
|
-
},
|
|
756
|
-
mimeType: 'image/png',
|
|
757
|
-
size: 1,
|
|
758
|
-
})
|
|
759
|
-
|
|
760
|
-
test('lexParse strict throws TypeError with "Invalid blob object"', () => {
|
|
761
|
-
expect(() => lexParse(blobWithCborCidJson, { strict: true })).toThrow(
|
|
762
|
-
TypeError,
|
|
763
|
-
)
|
|
764
|
-
expect(() => lexParse(blobWithCborCidJson, { strict: true })).toThrow(
|
|
765
|
-
'Invalid blob object',
|
|
766
|
-
)
|
|
767
|
-
})
|
|
768
|
-
|
|
769
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
770
|
-
const bytes = Buffer.from(blobWithCborCidJson)
|
|
771
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
772
|
-
TypeError,
|
|
773
|
-
)
|
|
774
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
775
|
-
'Invalid blob object',
|
|
776
|
-
)
|
|
777
|
-
})
|
|
778
|
-
|
|
779
|
-
test('lexParse non-strict returns plain object', () => {
|
|
780
|
-
expect(() =>
|
|
781
|
-
lexParse(blobWithCborCidJson, { strict: false }),
|
|
782
|
-
).not.toThrow()
|
|
783
|
-
})
|
|
784
|
-
|
|
785
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
786
|
-
expect(() =>
|
|
787
|
-
lexParseJsonBytes(Buffer.from(blobWithCborCidJson), { strict: false }),
|
|
788
|
-
).not.toThrow()
|
|
789
|
-
})
|
|
790
|
-
})
|
|
791
|
-
|
|
792
|
-
describe('invalid $link: strict throws TypeError, non-strict returns plain object', () => {
|
|
793
|
-
const invalidLinkJson = '{"$link":"."}'
|
|
794
|
-
|
|
795
|
-
test('lexParse strict throws TypeError with "Invalid $link object"', () => {
|
|
796
|
-
expect(() => lexParse(invalidLinkJson, { strict: true })).toThrow(
|
|
797
|
-
TypeError,
|
|
798
|
-
)
|
|
799
|
-
expect(() => lexParse(invalidLinkJson, { strict: true })).toThrow(
|
|
800
|
-
'Invalid $link object',
|
|
801
|
-
)
|
|
802
|
-
})
|
|
803
|
-
|
|
804
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
805
|
-
const bytes = Buffer.from(invalidLinkJson)
|
|
806
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
807
|
-
TypeError,
|
|
808
|
-
)
|
|
809
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
810
|
-
'Invalid $link object',
|
|
811
|
-
)
|
|
812
|
-
})
|
|
813
|
-
|
|
814
|
-
test('lexParse non-strict returns plain object', () => {
|
|
815
|
-
expect(() => lexParse(invalidLinkJson, { strict: false })).not.toThrow()
|
|
816
|
-
})
|
|
817
|
-
|
|
818
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
819
|
-
expect(() =>
|
|
820
|
-
lexParseJsonBytes(Buffer.from(invalidLinkJson), { strict: false }),
|
|
821
|
-
).not.toThrow()
|
|
822
|
-
})
|
|
823
|
-
})
|
|
824
|
-
|
|
825
|
-
describe('$link with extra fields: strict throws TypeError, non-strict returns plain object', () => {
|
|
826
|
-
const linkWithExtraJson =
|
|
827
|
-
'{"$link":"bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity","extra":"field"}'
|
|
828
|
-
|
|
829
|
-
test('lexParse strict throws TypeError with "Invalid $link object"', () => {
|
|
830
|
-
expect(() => lexParse(linkWithExtraJson, { strict: true })).toThrow(
|
|
831
|
-
TypeError,
|
|
832
|
-
)
|
|
833
|
-
expect(() => lexParse(linkWithExtraJson, { strict: true })).toThrow(
|
|
834
|
-
'Invalid $link object',
|
|
835
|
-
)
|
|
836
|
-
})
|
|
837
|
-
|
|
838
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
839
|
-
const bytes = Buffer.from(linkWithExtraJson)
|
|
840
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
841
|
-
TypeError,
|
|
842
|
-
)
|
|
843
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
844
|
-
'Invalid $link object',
|
|
845
|
-
)
|
|
846
|
-
})
|
|
847
|
-
|
|
848
|
-
test('lexParse non-strict returns plain object', () => {
|
|
849
|
-
expect(() => lexParse(linkWithExtraJson, { strict: false })).not.toThrow()
|
|
850
|
-
})
|
|
851
|
-
|
|
852
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
853
|
-
expect(() =>
|
|
854
|
-
lexParseJsonBytes(Buffer.from(linkWithExtraJson), { strict: false }),
|
|
855
|
-
).not.toThrow()
|
|
856
|
-
})
|
|
857
|
-
})
|
|
858
|
-
|
|
859
|
-
describe('invalid $bytes: strict throws TypeError, non-strict returns plain object', () => {
|
|
860
|
-
const invalidBytesJson = '{"$bytes":"🐻"}'
|
|
861
|
-
|
|
862
|
-
test('lexParse strict throws TypeError with "Invalid $bytes object"', () => {
|
|
863
|
-
expect(() => lexParse(invalidBytesJson, { strict: true })).toThrow(
|
|
864
|
-
TypeError,
|
|
865
|
-
)
|
|
866
|
-
expect(() => lexParse(invalidBytesJson, { strict: true })).toThrow(
|
|
867
|
-
'Invalid $bytes object',
|
|
868
|
-
)
|
|
869
|
-
})
|
|
870
|
-
|
|
871
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
872
|
-
const bytes = Buffer.from(invalidBytesJson)
|
|
873
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
874
|
-
TypeError,
|
|
875
|
-
)
|
|
876
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
877
|
-
'Invalid $bytes object',
|
|
878
|
-
)
|
|
879
|
-
})
|
|
880
|
-
|
|
881
|
-
test('lexParse non-strict returns plain object', () => {
|
|
882
|
-
expect(() => lexParse(invalidBytesJson, { strict: false })).not.toThrow()
|
|
883
|
-
})
|
|
884
|
-
|
|
885
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
886
|
-
expect(() =>
|
|
887
|
-
lexParseJsonBytes(Buffer.from(invalidBytesJson), { strict: false }),
|
|
888
|
-
).not.toThrow()
|
|
889
|
-
})
|
|
890
|
-
})
|
|
891
|
-
|
|
892
|
-
describe('$bytes with extra fields: strict throws TypeError, non-strict returns plain object', () => {
|
|
893
|
-
const bytesWithExtraJson =
|
|
894
|
-
'{"$bytes":"nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0","extra":"field"}'
|
|
895
|
-
|
|
896
|
-
test('lexParse strict throws TypeError with "Invalid $bytes object"', () => {
|
|
897
|
-
expect(() => lexParse(bytesWithExtraJson, { strict: true })).toThrow(
|
|
898
|
-
TypeError,
|
|
899
|
-
)
|
|
900
|
-
expect(() => lexParse(bytesWithExtraJson, { strict: true })).toThrow(
|
|
901
|
-
'Invalid $bytes object',
|
|
902
|
-
)
|
|
903
|
-
})
|
|
904
|
-
|
|
905
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
906
|
-
const bytes = Buffer.from(bytesWithExtraJson)
|
|
907
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
908
|
-
TypeError,
|
|
909
|
-
)
|
|
910
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
911
|
-
'Invalid $bytes object',
|
|
912
|
-
)
|
|
913
|
-
})
|
|
914
|
-
|
|
915
|
-
test('lexParse non-strict returns plain object', () => {
|
|
916
|
-
expect(() =>
|
|
917
|
-
lexParse(bytesWithExtraJson, { strict: false }),
|
|
918
|
-
).not.toThrow()
|
|
919
|
-
})
|
|
920
|
-
|
|
921
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
922
|
-
expect(() =>
|
|
923
|
-
lexParseJsonBytes(Buffer.from(bytesWithExtraJson), { strict: false }),
|
|
924
|
-
).not.toThrow()
|
|
925
|
-
})
|
|
926
|
-
})
|
|
927
|
-
|
|
928
|
-
describe('empty $type: strict throws TypeError, non-strict returns plain object', () => {
|
|
929
|
-
const emptyTypeJson = '{"$type":"","foo":"bar"}'
|
|
930
|
-
|
|
931
|
-
test('lexParse strict throws TypeError with "Empty $type property"', () => {
|
|
932
|
-
expect(() => lexParse(emptyTypeJson, { strict: true })).toThrow(TypeError)
|
|
933
|
-
expect(() => lexParse(emptyTypeJson, { strict: true })).toThrow(
|
|
934
|
-
'Empty $type property',
|
|
935
|
-
)
|
|
936
|
-
})
|
|
937
|
-
|
|
938
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
939
|
-
const bytes = Buffer.from(emptyTypeJson)
|
|
940
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
941
|
-
TypeError,
|
|
942
|
-
)
|
|
943
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
944
|
-
'Empty $type property',
|
|
945
|
-
)
|
|
946
|
-
})
|
|
947
|
-
|
|
948
|
-
test('lexParse non-strict returns plain object', () => {
|
|
949
|
-
expect(() => lexParse(emptyTypeJson, { strict: false })).not.toThrow()
|
|
950
|
-
})
|
|
951
|
-
|
|
952
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
953
|
-
expect(() =>
|
|
954
|
-
lexParseJsonBytes(Buffer.from(emptyTypeJson), { strict: false }),
|
|
955
|
-
).not.toThrow()
|
|
956
|
-
})
|
|
957
|
-
})
|
|
958
|
-
|
|
959
|
-
describe('non-string $type: strict throws TypeError, non-strict returns plain object', () => {
|
|
960
|
-
const nonStringTypeJson = '{"$type":123,"foo":"bar"}'
|
|
961
|
-
|
|
962
|
-
test('lexParse strict throws TypeError with type name in message', () => {
|
|
963
|
-
expect(() => lexParse(nonStringTypeJson, { strict: true })).toThrow(
|
|
964
|
-
TypeError,
|
|
965
|
-
)
|
|
966
|
-
expect(() => lexParse(nonStringTypeJson, { strict: true })).toThrow(
|
|
967
|
-
'Invalid $type property (number)',
|
|
968
|
-
)
|
|
969
|
-
})
|
|
970
|
-
|
|
971
|
-
test('lexParseJsonBytes strict throws same TypeError', () => {
|
|
972
|
-
const bytes = Buffer.from(nonStringTypeJson)
|
|
973
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
974
|
-
TypeError,
|
|
975
|
-
)
|
|
976
|
-
expect(() => lexParseJsonBytes(bytes, { strict: true })).toThrow(
|
|
977
|
-
'Invalid $type property (number)',
|
|
978
|
-
)
|
|
979
|
-
})
|
|
980
|
-
|
|
981
|
-
test('lexParse non-strict returns plain object', () => {
|
|
982
|
-
expect(() => lexParse(nonStringTypeJson, { strict: false })).not.toThrow()
|
|
983
|
-
})
|
|
984
|
-
|
|
985
|
-
test('lexParseJsonBytes non-strict returns plain object', () => {
|
|
986
|
-
expect(() =>
|
|
987
|
-
lexParseJsonBytes(Buffer.from(nonStringTypeJson), { strict: false }),
|
|
988
|
-
).not.toThrow()
|
|
989
|
-
})
|
|
990
|
-
})
|
|
991
|
-
})
|