@cloudpss/ubjson 0.3.6 → 0.3.9
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/benchmark-string.js +18 -20
- package/benchmark.js +1 -0
- package/dist/{constants.d.ts → common/constants.d.ts} +0 -0
- package/dist/{constants.js → common/constants.js} +0 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/decoder.d.ts +48 -0
- package/dist/{decoder.js → common/decoder.js} +13 -18
- package/dist/common/decoder.js.map +1 -0
- package/dist/common/encoder.d.ts +74 -0
- package/dist/common/encoder.js +282 -0
- package/dist/common/encoder.js.map +1 -0
- package/dist/common/string-decoder.d.ts +13 -0
- package/dist/common/string-decoder.js +106 -0
- package/dist/common/string-decoder.js.map +1 -0
- package/dist/{string-encoder.d.ts → common/string-encoder.d.ts} +0 -0
- package/dist/{string-encoder.js → common/string-encoder.js} +0 -0
- package/dist/common/string-encoder.js.map +1 -0
- package/dist/encoder.d.ts +10 -2
- package/dist/encoder.js +2 -283
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/stream/encoder.d.ts +14 -0
- package/dist/stream/encoder.js +86 -0
- package/dist/stream/encoder.js.map +1 -0
- package/dist/stream/index.d.ts +4 -0
- package/dist/stream/index.js +7 -0
- package/dist/stream/index.js.map +1 -0
- package/jest.config.js +3 -0
- package/package.json +18 -8
- package/src/{constants.ts → common/constants.ts} +0 -0
- package/src/{decoder.ts → common/decoder.ts} +13 -18
- package/src/common/encoder.ts +305 -0
- package/src/common/string-decoder.ts +107 -0
- package/src/{string-encoder.ts → common/string-encoder.ts} +0 -0
- package/src/encoder.ts +4 -305
- package/src/index.ts +14 -2
- package/src/stream/encoder.ts +83 -0
- package/src/stream/index.ts +8 -0
- package/tests/decode.js +433 -0
- package/{test → tests}/e2e.js +19 -27
- package/tests/encode.js +354 -0
- package/tests/stream/encode.js +380 -0
- package/tests/string-encoding.js +71 -0
- package/tests/tsconfig.json +7 -0
- package/tsconfig.json +1 -2
- package/dist/constants.js.map +0 -1
- package/dist/decoder.d.ts +0 -2
- package/dist/decoder.js.map +0 -1
- package/dist/string-decoder.d.ts +0 -9
- package/dist/string-decoder.js +0 -67
- package/dist/string-decoder.js.map +0 -1
- package/dist/string-encoder.js.map +0 -1
- package/src/string-decoder.ts +0 -69
- package/test/decode.js +0 -491
- package/test/encode.js +0 -432
- package/test/string-encoding.js +0 -62
package/tests/decode.js
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests from https://bitbucket.org/shelacek/ubjson
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { decode } from '../dist/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {...string | number} args
|
|
9
|
+
* @returns {Uint8Array}
|
|
10
|
+
*/
|
|
11
|
+
function toBuffer(...args) {
|
|
12
|
+
return Uint8Array.from(args, (x) => (typeof x == 'number' ? x : x.charCodeAt(0)));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
test('decode unsupported type', () => {
|
|
16
|
+
expect(() => decode(toBuffer('!'))).toThrow();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('decode undefined data', () => {
|
|
20
|
+
// @ts-expect-error
|
|
21
|
+
expect(() => decode(undefined)).toThrow();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('decode undefined', () => {
|
|
25
|
+
expect(decode(toBuffer('N'))).toBeUndefined();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('decode undefined (multiple noop)', () => {
|
|
29
|
+
expect(decode(toBuffer('N', 'N', 'N'))).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('decode undefined (empty buffer)', () => {
|
|
33
|
+
expect(decode(toBuffer())).toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('decode null', () => {
|
|
37
|
+
expect(decode(toBuffer('Z'))).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('decode true', () => {
|
|
41
|
+
expect(decode(toBuffer('T'))).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('decode false', () => {
|
|
45
|
+
expect(decode(toBuffer('F'))).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('decode int8', () => {
|
|
49
|
+
expect(decode(toBuffer('i', 100))).toBe(100);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('decode uint8', () => {
|
|
53
|
+
expect(decode(toBuffer('U', 200))).toBe(200);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('decode int16', () => {
|
|
57
|
+
expect(decode(toBuffer('I', 0x12, 0x34))).toBe(0x1234);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('decode int32', () => {
|
|
61
|
+
expect(decode(toBuffer('l', 0x12, 0x34, 0x56, 0x78))).toBe(0x12345678);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('decode int64 [error]', () => {
|
|
65
|
+
expect(() => decode(toBuffer('L', 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0))).toThrow();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('decode float32', () => {
|
|
69
|
+
expect(decode(toBuffer('d', 0x3f, 0x80, 0x80, 0x00))).toBe(1.00390625);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('decode float64', () => {
|
|
73
|
+
expect(decode(toBuffer('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00))).toBe(100000.00390625);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('decode high-precision number [error]', () => {
|
|
77
|
+
expect(() => decode(toBuffer('H', 'i', 3, '1', '.', '1'))).toThrow();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('decode char', () => {
|
|
81
|
+
expect(decode(toBuffer('C', 'a'))).toBe('a');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('decode string', () => {
|
|
85
|
+
expect(decode(toBuffer('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'))).toBe('ubjson');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('decode empty string', () => {
|
|
89
|
+
expect(decode(toBuffer('S', 'i', 0))).toBe('');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('decode string (bad size) [error]', () => {
|
|
93
|
+
expect(() => decode(toBuffer('S', 'i', 0xff, 'x'))).toThrow(/Invalid length/);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('decode string (unexpected eof) [error]', () => {
|
|
97
|
+
expect(() => decode(toBuffer('S', 'i', 2, 'x'))).toThrow(/Unexpected EOF/);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('decode ascii string', () => {
|
|
101
|
+
const header = toBuffer('S', 'I', 0x3f, 0xff);
|
|
102
|
+
const payload = new Uint8Array(0x3fff + header.byteLength).fill('a'.charCodeAt(0));
|
|
103
|
+
payload.set(header);
|
|
104
|
+
expect(decode(payload)).toBe('a'.repeat(0x3fff));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('decode ascii string [huge]', () => {
|
|
108
|
+
const header = toBuffer('S', 'I', 0x7f, 0xff);
|
|
109
|
+
const payload = new Uint8Array(0x7fff + header.byteLength).fill('a'.charCodeAt(0));
|
|
110
|
+
payload.set(header);
|
|
111
|
+
expect(decode(payload)).toBe('a'.repeat(0x7fff));
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('decode huge string', () => {
|
|
115
|
+
expect(decode(toBuffer('S', 'l', 0x00, 0x00, 0x00, 6, 'u', 'b', 'j', 's', 'o', 'n'))).toBe('ubjson');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('decode huge string [error]', () => {
|
|
119
|
+
expect(() => decode(toBuffer('S', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'x'))).toThrow(
|
|
120
|
+
/Unsupported type int64/,
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('decode array', () => {
|
|
125
|
+
expect(decode(toBuffer('[', 'i', 1, 'i', 2, 'i', 3, ']'))).toEqual([1, 2, 3]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('decode array (with no-op)', () => {
|
|
129
|
+
expect(decode(toBuffer('[', 'i', 1, 'N', 'i', 2, 'i', 3, 'N', ']'))).toEqual([1, 2, 3]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('decode array (empty)', () => {
|
|
133
|
+
expect(decode(toBuffer('[', ']'))).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('decode array (empty, optimized)', () => {
|
|
137
|
+
expect(decode(toBuffer('[', '#', 'i', 0))).toEqual([]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('decode array (empty, strongly typed, optimized)', () => {
|
|
141
|
+
expect(decode(toBuffer('[', '$', 'i', '#', 'i', 0))).toEqual(new Int8Array(0));
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('decode array (mixed, optimized)', () => {
|
|
145
|
+
expect(decode(toBuffer('[', '#', 'i', 3, 'i', 1, 'C', 'a', 'T'))).toEqual([1, 'a', true]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('decode array (strongly typed, optimized)', () => {
|
|
149
|
+
expect(decode(toBuffer('[', '$', 'i', '#', 'i', 3, 1, 2, 3))).toEqual(new Int8Array([1, 2, 3]));
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('decode array (strongly typed, empty, optimized)', () => {
|
|
153
|
+
expect(decode(toBuffer('[', '$', 'i', '#', 'i', 0))).toEqual(new Int8Array([]));
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('decode N-D array (strongly typed, optimized)', () => {
|
|
157
|
+
expect(
|
|
158
|
+
decode(toBuffer('[', '$', '[', '#', 'i', 2, '$', 'i', '#', 'i', 3, 1, 2, 3, '$', 'i', '#', 'i', 3, 4, 5, 6)),
|
|
159
|
+
).toEqual([new Int8Array([1, 2, 3]), new Int8Array([4, 5, 6])]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('decode array of objects (optimized)', () => {
|
|
163
|
+
expect(
|
|
164
|
+
decode(
|
|
165
|
+
toBuffer(
|
|
166
|
+
'[',
|
|
167
|
+
'$',
|
|
168
|
+
'{',
|
|
169
|
+
'#',
|
|
170
|
+
'i',
|
|
171
|
+
2,
|
|
172
|
+
'$',
|
|
173
|
+
'i',
|
|
174
|
+
'#',
|
|
175
|
+
'i',
|
|
176
|
+
3,
|
|
177
|
+
'i',
|
|
178
|
+
1,
|
|
179
|
+
'a',
|
|
180
|
+
1,
|
|
181
|
+
'i',
|
|
182
|
+
1,
|
|
183
|
+
'b',
|
|
184
|
+
2,
|
|
185
|
+
'i',
|
|
186
|
+
1,
|
|
187
|
+
'c',
|
|
188
|
+
3,
|
|
189
|
+
'$',
|
|
190
|
+
'i',
|
|
191
|
+
'#',
|
|
192
|
+
'i',
|
|
193
|
+
3,
|
|
194
|
+
'i',
|
|
195
|
+
1,
|
|
196
|
+
'd',
|
|
197
|
+
4,
|
|
198
|
+
'i',
|
|
199
|
+
1,
|
|
200
|
+
'e',
|
|
201
|
+
5,
|
|
202
|
+
'i',
|
|
203
|
+
1,
|
|
204
|
+
'f',
|
|
205
|
+
6,
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
).toEqual([
|
|
209
|
+
{ a: 1, b: 2, c: 3 },
|
|
210
|
+
{ d: 4, e: 5, f: 6 },
|
|
211
|
+
]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('decode array of objects of arrays (optimized)', () => {
|
|
215
|
+
expect(
|
|
216
|
+
decode(
|
|
217
|
+
toBuffer(
|
|
218
|
+
'[',
|
|
219
|
+
'$',
|
|
220
|
+
'{',
|
|
221
|
+
'#',
|
|
222
|
+
'i',
|
|
223
|
+
2,
|
|
224
|
+
'$',
|
|
225
|
+
'[',
|
|
226
|
+
'#',
|
|
227
|
+
'i',
|
|
228
|
+
2,
|
|
229
|
+
'i',
|
|
230
|
+
1,
|
|
231
|
+
'a',
|
|
232
|
+
'$',
|
|
233
|
+
'i',
|
|
234
|
+
'#',
|
|
235
|
+
'i',
|
|
236
|
+
2,
|
|
237
|
+
1,
|
|
238
|
+
2,
|
|
239
|
+
'i',
|
|
240
|
+
1,
|
|
241
|
+
'b',
|
|
242
|
+
'$',
|
|
243
|
+
'i',
|
|
244
|
+
'#',
|
|
245
|
+
'i',
|
|
246
|
+
2,
|
|
247
|
+
3,
|
|
248
|
+
4,
|
|
249
|
+
'$',
|
|
250
|
+
'[',
|
|
251
|
+
'#',
|
|
252
|
+
'i',
|
|
253
|
+
2,
|
|
254
|
+
'i',
|
|
255
|
+
1,
|
|
256
|
+
'c',
|
|
257
|
+
'$',
|
|
258
|
+
'i',
|
|
259
|
+
'#',
|
|
260
|
+
'i',
|
|
261
|
+
2,
|
|
262
|
+
5,
|
|
263
|
+
6,
|
|
264
|
+
'i',
|
|
265
|
+
1,
|
|
266
|
+
'd',
|
|
267
|
+
'$',
|
|
268
|
+
'i',
|
|
269
|
+
'#',
|
|
270
|
+
'i',
|
|
271
|
+
2,
|
|
272
|
+
7,
|
|
273
|
+
8,
|
|
274
|
+
),
|
|
275
|
+
),
|
|
276
|
+
).toEqual([
|
|
277
|
+
{ a: new Int8Array([1, 2]), b: new Int8Array([3, 4]) },
|
|
278
|
+
{ c: new Int8Array([5, 6]), d: new Int8Array([7, 8]) },
|
|
279
|
+
]);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('decode array (strongly typed, unexpected eof, optimized)', () => {
|
|
283
|
+
expect(() => decode(toBuffer('[', '$', 'i', '#', 'i', 3, 1, 2))).toThrow();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('decode array (strongly typed, invalid length value, optimized)', () => {
|
|
287
|
+
expect(() => decode(toBuffer('[', '$', 'i', '#', 'i', -1))).toThrow();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test('decode array (strongly typed, invalid length type, optimized)', () => {
|
|
291
|
+
expect(() => decode(toBuffer('[', '$', 'i', '#', 'C', '0'))).toThrow();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('decode array (strongly typed, malformed, optimized)', () => {
|
|
295
|
+
expect(() => decode(toBuffer('[', '$', 'i', 1, 2, 3, ']'))).toThrow();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test('decode array (only null values, optimized)', () => {
|
|
299
|
+
expect(decode(toBuffer('[', '$', 'Z', '#', 'i', 3))).toEqual([null, null, null]);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('decode array (only true values, optimized)', () => {
|
|
303
|
+
expect(decode(toBuffer('[', '$', 'T', '#', 'i', 3))).toEqual([true, true, true]);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test('decode array (only false values, optimized)', () => {
|
|
307
|
+
expect(decode(toBuffer('[', '$', 'F', '#', 'i', 3))).toEqual([false, false, false]);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test('decode array (int8, strongly typed, optimized) [use typed array]', () => {
|
|
311
|
+
const actual = decode(toBuffer('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe));
|
|
312
|
+
expect(actual).toBeInstanceOf(Int8Array);
|
|
313
|
+
expect(actual).toEqual(Int8Array.from([18, -2]));
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('decode array (uint8, strongly typed, optimized) [use typed array]', () => {
|
|
317
|
+
const actual = decode(toBuffer('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe));
|
|
318
|
+
expect(actual).toBeInstanceOf(Uint8Array);
|
|
319
|
+
expect(actual).toEqual(Uint8Array.from([18, 254]));
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test('decode array (int16, strongly typed, optimized) [use typed array]', () => {
|
|
323
|
+
const actual = decode(toBuffer('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc));
|
|
324
|
+
expect(actual).toBeInstanceOf(Int16Array);
|
|
325
|
+
expect(actual).toEqual(Int16Array.from([4660, -292]));
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
test('decode array (int32, strongly typed, optimized) [use typed array]', () => {
|
|
329
|
+
const actual = decode(toBuffer('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98));
|
|
330
|
+
expect(actual).toBeInstanceOf(Int32Array);
|
|
331
|
+
expect(actual).toEqual(Int32Array.from([305419896, -19088744]));
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
test('decode array (int64, strongly typed, optimized) [use typed array]', () => {
|
|
335
|
+
expect(() =>
|
|
336
|
+
decode(toBuffer('[', '$', 'L', '#', 'i', 1, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98)),
|
|
337
|
+
).toThrow();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('decode array (float32, strongly typed, optimized) [use typed array]', () => {
|
|
341
|
+
const actual = decode(toBuffer('[', '$', 'd', '#', 'i', 2, 0x3e, 0x80, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00));
|
|
342
|
+
expect(actual).toBeInstanceOf(Float32Array);
|
|
343
|
+
expect(actual).toEqual(Float32Array.from([0.25, 0.125]));
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('decode array (float64, strongly typed, optimized) [use typed array]', () => {
|
|
347
|
+
const actual = decode(
|
|
348
|
+
toBuffer(
|
|
349
|
+
'[',
|
|
350
|
+
'$',
|
|
351
|
+
'D',
|
|
352
|
+
'#',
|
|
353
|
+
'i',
|
|
354
|
+
2,
|
|
355
|
+
0x3f,
|
|
356
|
+
0xd0,
|
|
357
|
+
0x00,
|
|
358
|
+
0x00,
|
|
359
|
+
0x00,
|
|
360
|
+
0x00,
|
|
361
|
+
0x00,
|
|
362
|
+
0x00,
|
|
363
|
+
0x3f,
|
|
364
|
+
0xc0,
|
|
365
|
+
0x00,
|
|
366
|
+
0x00,
|
|
367
|
+
0x00,
|
|
368
|
+
0x00,
|
|
369
|
+
0x00,
|
|
370
|
+
0x00,
|
|
371
|
+
),
|
|
372
|
+
);
|
|
373
|
+
expect(actual).toBeInstanceOf(Float64Array);
|
|
374
|
+
expect(actual).toEqual(Float64Array.from([0.25, 0.125]));
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test('decode object', () => {
|
|
378
|
+
expect(decode(toBuffer('{', 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'i', 2, 'i', 1, 'c', 'i', 3, '}'))).toEqual({
|
|
379
|
+
a: 1,
|
|
380
|
+
b: 2,
|
|
381
|
+
c: 3,
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test('decode object (with no-op)', () => {
|
|
386
|
+
expect(
|
|
387
|
+
decode(
|
|
388
|
+
toBuffer('N', '{', 'N', 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'N', 'i', 2, 'i', 1, 'c', 'i', 3, 'N', '}', 'N'),
|
|
389
|
+
),
|
|
390
|
+
).toEqual({ a: 1, b: 2, c: 3 });
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test('decode array (empty, optimized)', () => {
|
|
394
|
+
expect(decode(toBuffer('{', '#', 'i', 0))).toEqual({});
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test('decode object (mixed, optimized)', () => {
|
|
398
|
+
expect(decode(toBuffer('{', '#', 'i', 3, 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T'))).toEqual({
|
|
399
|
+
a: 1,
|
|
400
|
+
b: 'a',
|
|
401
|
+
c: true,
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
test('decode object (strongly typed, optimized)', () => {
|
|
406
|
+
expect(decode(toBuffer('{', '$', 'i', '#', 'i', 3, 'i', 1, 'a', 1, 'i', 1, 'b', 2, 'i', 1, 'c', 3))).toEqual({
|
|
407
|
+
a: 1,
|
|
408
|
+
b: 2,
|
|
409
|
+
c: 3,
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
test('decode object (only null values, optimized)', () => {
|
|
414
|
+
expect(decode(toBuffer('{', '$', 'Z', '#', 'i', 3, 'i', 1, 'a', 'i', 1, 'b', 'i', 1, 'c'))).toEqual({
|
|
415
|
+
a: null,
|
|
416
|
+
b: null,
|
|
417
|
+
c: null,
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('decode object (empty key)', () => {
|
|
422
|
+
expect(decode(toBuffer('{', 'i', 0, 'T', '}'))).toEqual({
|
|
423
|
+
'': true,
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
test('decode object (empty key, optimized)', () => {
|
|
428
|
+
expect(decode(toBuffer('{', '$', 'Z', '#', 'i', 3, 'i', 0, 'i', 1, 'a', 'i', 1, 'b'))).toEqual({
|
|
429
|
+
'': null,
|
|
430
|
+
a: null,
|
|
431
|
+
b: null,
|
|
432
|
+
});
|
|
433
|
+
});
|
package/{test → tests}/e2e.js
RENAMED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tests from https://bitbucket.org/shelacek/ubjson
|
|
3
3
|
*/
|
|
4
|
+
import { encode, decode } from '../dist/index.js';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
import * as ubjson from '../dist/index.js';
|
|
7
|
-
|
|
8
|
-
test('encode/decode complex object', (t) => {
|
|
6
|
+
test('encode/decode complex object', () => {
|
|
9
7
|
const expected = {
|
|
10
8
|
hello: 'world',
|
|
11
9
|
from: ['UBJSON'],
|
|
@@ -56,13 +54,12 @@ test('encode/decode complex object', (t) => {
|
|
|
56
54
|
document1: 'long text'.repeat(10000),
|
|
57
55
|
document2: 'long text'.repeat(100000),
|
|
58
56
|
};
|
|
59
|
-
const encoded =
|
|
60
|
-
const actual =
|
|
61
|
-
|
|
62
|
-
t.end();
|
|
57
|
+
const encoded = encode(expected);
|
|
58
|
+
const actual = decode(Buffer.from(encoded));
|
|
59
|
+
expect(actual).toEqual(expected);
|
|
63
60
|
});
|
|
64
61
|
|
|
65
|
-
test('encode/decode model', (
|
|
62
|
+
test('encode/decode model', () => {
|
|
66
63
|
const expected = {
|
|
67
64
|
rid: 'project/CloudPSS/_SubSysPort',
|
|
68
65
|
name: '模块端口',
|
|
@@ -276,14 +273,14 @@ test('encode/decode model', (t) => {
|
|
|
276
273
|
documentation: '@[docs](http://docs.cloudpss.net/components/comp_PSS/comp_PSSSystem/BasicComp/SystemPort)',
|
|
277
274
|
},
|
|
278
275
|
};
|
|
279
|
-
const encoded =
|
|
280
|
-
const actual =
|
|
281
|
-
|
|
282
|
-
t.end();
|
|
276
|
+
const encoded = encode(expected);
|
|
277
|
+
const actual = decode(encoded);
|
|
278
|
+
expect(actual).toEqual(expected);
|
|
283
279
|
});
|
|
284
280
|
|
|
285
|
-
test('encode/decode complex object (no encodeInto)', (
|
|
281
|
+
test('encode/decode complex object (no encodeInto)', () => {
|
|
286
282
|
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
283
|
+
// @ts-ignore
|
|
287
284
|
TextEncoder.prototype.encodeInto = undefined;
|
|
288
285
|
const expected = {
|
|
289
286
|
hello: 'world',
|
|
@@ -334,28 +331,23 @@ test('encode/decode complex object (no encodeInto)', (t) => {
|
|
|
334
331
|
document1: 'long text'.repeat(10000),
|
|
335
332
|
document2: 'long text'.repeat(100000),
|
|
336
333
|
};
|
|
337
|
-
const encoded =
|
|
338
|
-
const actual =
|
|
339
|
-
|
|
340
|
-
t.end();
|
|
334
|
+
const encoded = encode(expected);
|
|
335
|
+
const actual = decode(encoded);
|
|
336
|
+
expect(actual).toEqual(expected);
|
|
341
337
|
TextEncoder.prototype.encodeInto = encodeInto;
|
|
342
338
|
});
|
|
343
339
|
|
|
344
|
-
test('encode/decode large array', (
|
|
340
|
+
test('encode/decode large array', () => {
|
|
345
341
|
const array = new Array(100000);
|
|
346
342
|
array[12345] = 'item';
|
|
347
343
|
|
|
348
344
|
const expected = Array.from({ length: 100000 }).fill(null);
|
|
349
345
|
expected[12345] = 'item';
|
|
350
346
|
|
|
351
|
-
|
|
352
|
-
t.end();
|
|
347
|
+
expect(decode(encode(array))).toEqual(expected);
|
|
353
348
|
});
|
|
354
349
|
|
|
355
|
-
test('encode/decode object with undefined values', (
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
c: { d: 2, f: null },
|
|
359
|
-
});
|
|
360
|
-
t.end();
|
|
350
|
+
test('encode/decode object with undefined values', () => {
|
|
351
|
+
const obj = { a: 1, b: undefined, c: { d: 2, e: undefined, f: null } };
|
|
352
|
+
expect(decode(encode(obj))).toEqual(obj);
|
|
361
353
|
});
|