@cloudpss/ubjson 0.5.10 → 0.5.12
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-small.js +81 -0
- package/dist/common/decoder.d.ts +8 -1
- package/dist/common/decoder.js +29 -15
- package/dist/common/decoder.js.map +1 -1
- package/dist/common/encoder.d.ts +1 -3
- package/dist/common/encoder.js +33 -11
- package/dist/common/encoder.js.map +1 -1
- package/dist/decoder.d.ts +1 -1
- package/dist/decoder.js.map +1 -1
- package/dist/encoder.d.ts +9 -1
- package/dist/encoder.js +41 -20
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/dist/rxjs/decoder.js +5 -2
- package/dist/rxjs/decoder.js.map +1 -1
- package/dist/rxjs/encoder.js +5 -4
- package/dist/rxjs/encoder.js.map +1 -1
- package/dist/stream/encoder.d.ts +3 -0
- package/dist/stream/encoder.js +8 -2
- package/dist/stream/encoder.js.map +1 -1
- package/dist/stream-helper/encoder.d.ts +10 -2
- package/dist/stream-helper/encoder.js +50 -24
- package/dist/stream-helper/encoder.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -4
- package/dist/utils.js.map +1 -1
- package/package.json +6 -15
- package/src/common/decoder.ts +36 -19
- package/src/common/encoder.ts +32 -10
- package/src/decoder.ts +1 -1
- package/src/encoder.ts +44 -21
- package/src/index.ts +7 -6
- package/src/rxjs/decoder.ts +6 -2
- package/src/rxjs/encoder.ts +5 -4
- package/src/stream/encoder.ts +10 -2
- package/src/stream-helper/encoder.ts +48 -26
- package/src/utils.ts +1 -4
- package/tests/decode.js +29 -9
- package/tests/e2e.js +3 -0
- package/tests/encode.js +173 -2
- package/tests/huge-string.js +3 -0
- package/tests/rxjs/decode.js +10 -8
- package/tests/rxjs/encode.js +6 -2
- package/tests/stream/decode.js +25 -8
- package/tests/stream/encode.js +13 -2
- package/tests/string-encoding.js +2 -2
package/tests/encode.js
CHANGED
|
@@ -3,104 +3,156 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { encode, decode } from '../dist/index.js';
|
|
6
|
+
import { getEncoder } from '../dist/encoder.js';
|
|
6
7
|
import { toArray } from './.utils.js';
|
|
7
8
|
|
|
9
|
+
// @ts-expect-error Access private property
|
|
10
|
+
const poolInit = getEncoder().pool;
|
|
11
|
+
|
|
8
12
|
test('encode function', () => {
|
|
9
13
|
expect(() =>
|
|
10
14
|
encode(function () {
|
|
11
15
|
//noop
|
|
12
16
|
}),
|
|
13
17
|
).toThrow();
|
|
18
|
+
// @ts-expect-error Access private property
|
|
19
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
14
20
|
});
|
|
15
21
|
|
|
16
22
|
test('encode bigint', () => {
|
|
17
|
-
expect((
|
|
23
|
+
expect(toArray(encode(1n))).toEqual(toArray(encode(1)));
|
|
24
|
+
expect(toArray(encode(0x1234_5678_90ab_cdefn))).toEqual(
|
|
25
|
+
toArray('L', 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef),
|
|
26
|
+
);
|
|
27
|
+
expect(() => encode(0x8234_5678_90ab_cdefn)).toThrow(/BigInt value out of range:/);
|
|
28
|
+
// @ts-expect-error Access private property
|
|
29
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
18
30
|
});
|
|
19
31
|
|
|
20
32
|
test('encode symbol', () => {
|
|
21
33
|
expect(() => encode(Symbol('sym'))).toThrow();
|
|
34
|
+
// @ts-expect-error Access private property
|
|
35
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
22
36
|
});
|
|
23
37
|
|
|
24
38
|
test('encode undefined', () => {
|
|
25
39
|
expect(toArray(encode(undefined))).toEqual(toArray('N'));
|
|
40
|
+
// @ts-expect-error Access private property
|
|
41
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
26
42
|
});
|
|
27
43
|
|
|
28
44
|
test('encode null', () => {
|
|
29
45
|
expect(toArray(encode(null))).toEqual(toArray('Z'));
|
|
46
|
+
// @ts-expect-error Access private property
|
|
47
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
30
48
|
});
|
|
31
49
|
|
|
32
50
|
test('encode true', () => {
|
|
33
51
|
expect(toArray(encode(true))).toEqual(toArray('T'));
|
|
52
|
+
// @ts-expect-error Access private property
|
|
53
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
34
54
|
});
|
|
35
55
|
|
|
36
56
|
test('encode false', () => {
|
|
37
57
|
expect(toArray(encode(false))).toEqual(toArray('F'));
|
|
58
|
+
// @ts-expect-error Access private property
|
|
59
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
38
60
|
});
|
|
39
61
|
|
|
40
62
|
test('encode int8', () => {
|
|
41
63
|
expect(toArray(encode(-1))).toEqual(toArray('i', 255));
|
|
64
|
+
// @ts-expect-error Access private property
|
|
65
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
42
66
|
});
|
|
43
67
|
|
|
44
68
|
test('encode uint8', () => {
|
|
45
69
|
expect(toArray(encode(200))).toEqual(toArray('U', 200));
|
|
70
|
+
// @ts-expect-error Access private property
|
|
71
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
46
72
|
});
|
|
47
73
|
|
|
48
74
|
test('encode int16', () => {
|
|
49
75
|
expect(toArray(encode(0x1234))).toEqual(toArray('I', 0x12, 0x34));
|
|
76
|
+
// @ts-expect-error Access private property
|
|
77
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
50
78
|
});
|
|
51
79
|
|
|
52
80
|
test('encode int32', () => {
|
|
53
81
|
expect(toArray(encode(0x1234_5678))).toEqual(toArray('l', 0x12, 0x34, 0x56, 0x78));
|
|
82
|
+
// @ts-expect-error Access private property
|
|
83
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
54
84
|
});
|
|
55
85
|
|
|
56
86
|
test('encode float32', () => {
|
|
57
87
|
expect(toArray(encode(1.003_906_25))).toEqual(toArray('d', 0x3f, 0x80, 0x80, 0x00));
|
|
88
|
+
// @ts-expect-error Access private property
|
|
89
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
58
90
|
});
|
|
59
91
|
|
|
60
92
|
test('encode float32 (too large integer)', () => {
|
|
61
93
|
expect(toArray(encode(2_147_483_648))).toEqual(toArray('d', 0x4f, 0x00, 0x00, 0x00));
|
|
94
|
+
// @ts-expect-error Access private property
|
|
95
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
62
96
|
});
|
|
63
97
|
|
|
64
98
|
test('encode float64', () => {
|
|
65
99
|
expect(toArray(encode(100_000.003_906_25))).toEqual(toArray('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00));
|
|
100
|
+
// @ts-expect-error Access private property
|
|
101
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
66
102
|
});
|
|
67
103
|
|
|
68
104
|
test('encode char', () => {
|
|
69
105
|
expect(toArray(encode('a'))).toEqual(toArray('C', 'a'));
|
|
106
|
+
// @ts-expect-error Access private property
|
|
107
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
70
108
|
});
|
|
71
109
|
|
|
72
110
|
test('encode char 127', () => {
|
|
73
111
|
expect(toArray(encode('\u007F'))).toEqual(toArray('C', '\u007F'));
|
|
112
|
+
// @ts-expect-error Access private property
|
|
113
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
74
114
|
});
|
|
75
115
|
|
|
76
116
|
test('encode char 257', () => {
|
|
77
117
|
expect(toArray(encode('\u0123'))).toEqual(toArray('S', 'i', 2, 196, 163));
|
|
118
|
+
// @ts-expect-error Access private property
|
|
119
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
78
120
|
});
|
|
79
121
|
|
|
80
122
|
test('encode char emoji', () => {
|
|
81
123
|
expect(toArray(encode('💖'))).toEqual(toArray('S', 'i', 4, 240, 159, 146, 150));
|
|
124
|
+
// @ts-expect-error Access private property
|
|
125
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
82
126
|
});
|
|
83
127
|
|
|
84
128
|
test('encode string', () => {
|
|
85
129
|
expect(toArray(encode('ubjson'))).toEqual(toArray('S', 'i', 6, ...'ubjson'));
|
|
130
|
+
// @ts-expect-error Access private property
|
|
131
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
86
132
|
});
|
|
87
133
|
|
|
88
134
|
test('encode string (len = 60)', () => {
|
|
89
135
|
// 不生成 u8
|
|
90
136
|
const str = 'ubjson'.repeat(10);
|
|
91
137
|
expect(toArray(encode(str))).toEqual(toArray('S', 'i', str.length, ...str));
|
|
138
|
+
// @ts-expect-error Access private property
|
|
139
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
92
140
|
});
|
|
93
141
|
|
|
94
142
|
test('encode string (len = 120)', () => {
|
|
95
143
|
// 不生成 u8
|
|
96
144
|
const str = 'ubjson'.repeat(20);
|
|
97
145
|
expect(toArray(encode(str))).toEqual(toArray('S', 'i', str.length, ...str));
|
|
146
|
+
// @ts-expect-error Access private property
|
|
147
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
98
148
|
});
|
|
99
149
|
|
|
100
150
|
test('encode string (len = 180)', () => {
|
|
101
151
|
// 不生成 u8
|
|
102
152
|
const str = 'ubjson'.repeat(30);
|
|
103
153
|
expect(toArray(encode(str))).toEqual(toArray('S', 'I', 0, str.length, ...str));
|
|
154
|
+
// @ts-expect-error Access private property
|
|
155
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
104
156
|
});
|
|
105
157
|
|
|
106
158
|
test('encode string (no encodeInto)', () => {
|
|
@@ -111,44 +163,64 @@ test('encode string (no encodeInto)', () => {
|
|
|
111
163
|
expect(toArray(encode('ubjson'))).toEqual(toArray('S', 'i', 6, ...'ubjson'));
|
|
112
164
|
|
|
113
165
|
TextEncoder.prototype.encodeInto = encodeInto;
|
|
166
|
+
// @ts-expect-error Access private property
|
|
167
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
114
168
|
});
|
|
115
169
|
|
|
116
170
|
test('encode array', () => {
|
|
117
171
|
expect(toArray(encode([1, 2, 3, -1]))).toEqual(toArray('[', 'U', 1, 'U', 2, 'U', 3, 'i', 255, ']'));
|
|
172
|
+
// @ts-expect-error Access private property
|
|
173
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
118
174
|
});
|
|
119
175
|
|
|
120
176
|
test('encode array (empty)', () => {
|
|
121
177
|
expect(toArray(encode([]))).toEqual(toArray('[', ']'));
|
|
178
|
+
// @ts-expect-error Access private property
|
|
179
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
122
180
|
});
|
|
123
181
|
|
|
124
182
|
test('encode array (undefined)', () => {
|
|
125
183
|
expect(toArray(encode([undefined]))).toEqual(toArray('[', 'Z', ']'));
|
|
184
|
+
// @ts-expect-error Access private property
|
|
185
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
126
186
|
});
|
|
127
187
|
|
|
128
188
|
test('encode array (function)', () => {
|
|
129
189
|
expect(toArray(encode([() => 1]))).toEqual(toArray('[', 'Z', ']'));
|
|
190
|
+
// @ts-expect-error Access private property
|
|
191
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
130
192
|
});
|
|
131
193
|
|
|
132
194
|
test('encode array (spares)', () => {
|
|
133
195
|
const array = Array.from({ length: 3 });
|
|
134
196
|
array[1] = true;
|
|
135
197
|
expect(toArray(encode(array))).toEqual(toArray('[', 'Z', 'T', 'Z', ']'));
|
|
198
|
+
// @ts-expect-error Access private property
|
|
199
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
136
200
|
});
|
|
137
201
|
|
|
138
202
|
test('encode array (mixed)', () => {
|
|
139
203
|
expect(toArray(encode([1, 'a', true]))).toEqual(toArray('[', 'U', 1, 'C', 'a', 'T', ']'));
|
|
204
|
+
// @ts-expect-error Access private property
|
|
205
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
140
206
|
});
|
|
141
207
|
|
|
142
208
|
test('encode array (int8)', () => {
|
|
143
209
|
expect(toArray(encode([-1, 2, 3]))).toEqual(toArray('[', 'i', 255, 'U', 2, 'U', 3, ']'));
|
|
210
|
+
// @ts-expect-error Access private property
|
|
211
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
144
212
|
});
|
|
145
213
|
|
|
146
214
|
test('encode array (int16)', () => {
|
|
147
215
|
expect(toArray(encode([255, -1]))).toEqual(toArray('[', 'U', 0xff, 'i', 0xff, ']'));
|
|
216
|
+
// @ts-expect-error Access private property
|
|
217
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
148
218
|
});
|
|
149
219
|
|
|
150
220
|
test('encode array (only null values)', () => {
|
|
151
221
|
expect(toArray(encode([null, null, null]))).toEqual(toArray('[', 'Z', 'Z', 'Z', ']'));
|
|
222
|
+
// @ts-expect-error Access private property
|
|
223
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
152
224
|
});
|
|
153
225
|
|
|
154
226
|
test('encode N-D array', () => {
|
|
@@ -163,6 +235,8 @@ test('encode N-D array', () => {
|
|
|
163
235
|
[1, 2, 3],
|
|
164
236
|
[4, 5, 6],
|
|
165
237
|
]);
|
|
238
|
+
// @ts-expect-error Access private property
|
|
239
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
166
240
|
});
|
|
167
241
|
|
|
168
242
|
test('encode array of objects', () => {
|
|
@@ -177,6 +251,8 @@ test('encode array of objects', () => {
|
|
|
177
251
|
{ a: 1, b: 2, c: 3 },
|
|
178
252
|
{ d: 4, e: 5, f: 6 },
|
|
179
253
|
]);
|
|
254
|
+
// @ts-expect-error Access private property
|
|
255
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
180
256
|
});
|
|
181
257
|
|
|
182
258
|
test('encode array of objects of arrays', () => {
|
|
@@ -191,36 +267,50 @@ test('encode array of objects of arrays', () => {
|
|
|
191
267
|
{ a: [1, 2], b: [3, 4] },
|
|
192
268
|
{ c: [5, 6], d: [7, 8] },
|
|
193
269
|
]);
|
|
270
|
+
// @ts-expect-error Access private property
|
|
271
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
194
272
|
});
|
|
195
273
|
|
|
196
274
|
test('encode array (int8 typed array)', () => {
|
|
197
275
|
expect(toArray(encode(Int8Array.from([18, -2])))).toEqual(toArray('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe));
|
|
276
|
+
// @ts-expect-error Access private property
|
|
277
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
198
278
|
});
|
|
199
279
|
|
|
200
280
|
test('encode array (uint8 typed array)', () => {
|
|
201
281
|
expect(toArray(encode(Uint8Array.from([18, 254])))).toEqual(toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe));
|
|
282
|
+
// @ts-expect-error Access private property
|
|
283
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
202
284
|
});
|
|
203
285
|
|
|
204
286
|
test('encode array (buffer array)', () => {
|
|
205
287
|
expect(toArray(encode(Buffer.from([18, -2])))).toEqual(toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe));
|
|
288
|
+
// @ts-expect-error Access private property
|
|
289
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
206
290
|
});
|
|
207
291
|
|
|
208
292
|
test('encode array (int16 typed array)', () => {
|
|
209
293
|
expect(toArray(encode(Int16Array.from([4660, -292])))).toEqual(
|
|
210
294
|
toArray('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc),
|
|
211
295
|
);
|
|
296
|
+
// @ts-expect-error Access private property
|
|
297
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
212
298
|
});
|
|
213
299
|
|
|
214
300
|
test('encode array (int32 typed array)', () => {
|
|
215
301
|
expect(toArray(encode(Int32Array.from([305_419_896, -19_088_744])))).toEqual(
|
|
216
302
|
toArray('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98),
|
|
217
303
|
);
|
|
304
|
+
// @ts-expect-error Access private property
|
|
305
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
218
306
|
});
|
|
219
307
|
|
|
220
308
|
test('encode array (float32 typed array)', () => {
|
|
221
309
|
expect(toArray(encode(Float32Array.from([0.25, 0.125])))).toEqual(
|
|
222
310
|
toArray('[', '$', 'd', '#', 'i', 2, 0x3e, 0x80, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00),
|
|
223
311
|
);
|
|
312
|
+
// @ts-expect-error Access private property
|
|
313
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
224
314
|
});
|
|
225
315
|
|
|
226
316
|
test('encode array (float64 typed array)', () => {
|
|
@@ -250,52 +340,99 @@ test('encode array (float64 typed array)', () => {
|
|
|
250
340
|
0x00,
|
|
251
341
|
),
|
|
252
342
|
);
|
|
343
|
+
// @ts-expect-error Access private property
|
|
344
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
253
345
|
});
|
|
254
346
|
|
|
255
347
|
test('encode array (uint8clamped typed array)', () => {
|
|
256
348
|
expect(() => encode(new Uint8ClampedArray())).toThrow();
|
|
349
|
+
// @ts-expect-error Access private property
|
|
350
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
257
351
|
});
|
|
258
352
|
|
|
259
353
|
test('encode array (uint16 typed array)', () => {
|
|
260
354
|
expect(() => encode(new Uint16Array())).toThrow();
|
|
355
|
+
// @ts-expect-error Access private property
|
|
356
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
261
357
|
});
|
|
262
358
|
|
|
263
359
|
test('encode array (uint32 typed array)', () => {
|
|
264
360
|
expect(() => encode(new Uint32Array())).toThrow();
|
|
361
|
+
// @ts-expect-error Access private property
|
|
362
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
265
363
|
});
|
|
266
364
|
|
|
267
365
|
test('encode array (uint64 typed array)', () => {
|
|
268
366
|
expect(() => encode(new BigUint64Array())).toThrow();
|
|
367
|
+
// @ts-expect-error Access private property
|
|
368
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
269
369
|
});
|
|
270
370
|
|
|
271
371
|
test('encode array (int64 typed array)', () => {
|
|
272
|
-
expect((
|
|
372
|
+
expect(toArray(encode(new BigInt64Array([1n, 0x1234_5678_1234_5678n])))).toEqual(
|
|
373
|
+
toArray(
|
|
374
|
+
'[',
|
|
375
|
+
'$',
|
|
376
|
+
'L',
|
|
377
|
+
'#',
|
|
378
|
+
'i',
|
|
379
|
+
2,
|
|
380
|
+
0x00,
|
|
381
|
+
0x00,
|
|
382
|
+
0x00,
|
|
383
|
+
0x00,
|
|
384
|
+
0x00,
|
|
385
|
+
0x00,
|
|
386
|
+
0x00,
|
|
387
|
+
0x01,
|
|
388
|
+
0x12,
|
|
389
|
+
0x34,
|
|
390
|
+
0x56,
|
|
391
|
+
0x78,
|
|
392
|
+
0x12,
|
|
393
|
+
0x34,
|
|
394
|
+
0x56,
|
|
395
|
+
0x78,
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
// @ts-expect-error Access private property
|
|
399
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
273
400
|
});
|
|
274
401
|
|
|
275
402
|
test('encode object', () => {
|
|
276
403
|
expect(toArray(encode({ a: 1, b: 2, c: 3 }))).toEqual(
|
|
277
404
|
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'),
|
|
278
405
|
);
|
|
406
|
+
// @ts-expect-error Access private property
|
|
407
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
279
408
|
});
|
|
280
409
|
|
|
281
410
|
test('encode object (empty)', () => {
|
|
282
411
|
expect(toArray(encode({}))).toEqual(toArray('{', '}'));
|
|
412
|
+
// @ts-expect-error Access private property
|
|
413
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
283
414
|
});
|
|
284
415
|
|
|
285
416
|
test('encode object (empty key)', () => {
|
|
286
417
|
expect(toArray(encode({ '': '' }))).toEqual(toArray('{', 'i', 0, 'S', 'i', 0, '}'));
|
|
418
|
+
// @ts-expect-error Access private property
|
|
419
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
287
420
|
});
|
|
288
421
|
|
|
289
422
|
test('encode object (mixed)', () => {
|
|
290
423
|
expect(toArray(encode({ a: 1, b: 'a', c: true }))).toEqual(
|
|
291
424
|
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
|
|
292
425
|
);
|
|
426
|
+
// @ts-expect-error Access private property
|
|
427
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
293
428
|
});
|
|
294
429
|
|
|
295
430
|
test('encode object (only null values)', () => {
|
|
296
431
|
expect(toArray(encode({ a: null, b: null, c: null }))).toEqual(
|
|
297
432
|
toArray('{', 'i', 1, 'a', 'Z', 'i', 1, 'b', 'Z', 'i', 1, 'c', 'Z', '}'),
|
|
298
433
|
);
|
|
434
|
+
// @ts-expect-error Access private property
|
|
435
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
299
436
|
});
|
|
300
437
|
|
|
301
438
|
test('encode object (skip prototype)', () => {
|
|
@@ -306,58 +443,80 @@ test('encode object (skip prototype)', () => {
|
|
|
306
443
|
expect(toArray(encode(obj))).toEqual(
|
|
307
444
|
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
|
|
308
445
|
);
|
|
446
|
+
// @ts-expect-error Access private property
|
|
447
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
309
448
|
});
|
|
310
449
|
|
|
311
450
|
test('encode object (skip symbol)', () => {
|
|
312
451
|
const obj = { [Symbol()]: true, a: 1 };
|
|
313
452
|
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
453
|
+
// @ts-expect-error Access private property
|
|
454
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
314
455
|
});
|
|
315
456
|
|
|
316
457
|
test('encode object (skip non-enumerable)', () => {
|
|
317
458
|
const obj = {};
|
|
318
459
|
Object.defineProperty(obj, 'a', { value: 1, configurable: true, writable: true });
|
|
319
460
|
expect(toArray(encode(obj))).toEqual(toArray('{', '}'));
|
|
461
|
+
// @ts-expect-error Access private property
|
|
462
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
320
463
|
});
|
|
321
464
|
|
|
322
465
|
test('encode object (include getter)', () => {
|
|
323
466
|
const obj = {};
|
|
324
467
|
Object.defineProperty(obj, 'a', { get: () => 1, enumerable: true });
|
|
325
468
|
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
469
|
+
// @ts-expect-error Access private property
|
|
470
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
326
471
|
});
|
|
327
472
|
|
|
328
473
|
test('encode object (skip undefined)', () => {
|
|
329
474
|
const obj = { a: undefined };
|
|
330
475
|
expect(toArray(encode(obj))).toEqual(toArray('{', '}'));
|
|
476
|
+
// @ts-expect-error Access private property
|
|
477
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
331
478
|
});
|
|
332
479
|
|
|
333
480
|
test('encode object (skip function)', () => {
|
|
334
481
|
const obj = { a: 1, b: () => 1 };
|
|
335
482
|
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
483
|
+
// @ts-expect-error Access private property
|
|
484
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
336
485
|
});
|
|
337
486
|
|
|
338
487
|
test('encode object (include null)', () => {
|
|
339
488
|
const obj = { a: null };
|
|
340
489
|
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'Z', '}'));
|
|
490
|
+
// @ts-expect-error Access private property
|
|
491
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
341
492
|
});
|
|
342
493
|
|
|
343
494
|
test('encode huge typed array (16K)', () => {
|
|
344
495
|
const obj = new Uint8Array(16 * 1024);
|
|
345
496
|
expect(toArray(encode(obj).slice(0, 8))).toEqual(toArray('[', '$', 'U', '#', 'I', 0x40, 0x00, 0));
|
|
497
|
+
// @ts-expect-error Access private property
|
|
498
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
346
499
|
});
|
|
347
500
|
|
|
348
501
|
test('encode huge typed array (~128M)', () => {
|
|
349
502
|
const obj = new Uint8Array(128 * 1024 * 1024 - 10);
|
|
350
503
|
expect(toArray(encode(obj).slice(0, 10))).toEqual(toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0));
|
|
504
|
+
// @ts-expect-error Access private property
|
|
505
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
351
506
|
});
|
|
352
507
|
|
|
353
508
|
test('encode huge typed array (128M) [error]', () => {
|
|
354
509
|
const obj = new Uint8Array(128 * 1024 * 1024);
|
|
355
510
|
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
511
|
+
// @ts-expect-error Access private property
|
|
512
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
356
513
|
});
|
|
357
514
|
|
|
358
515
|
test('encode huge typed array (3G) [error]', () => {
|
|
359
516
|
const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
|
|
360
517
|
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
518
|
+
// @ts-expect-error Access private property
|
|
519
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
361
520
|
});
|
|
362
521
|
|
|
363
522
|
test('encode huge data (~128M)', () => {
|
|
@@ -367,6 +526,8 @@ test('encode huge data (~128M)', () => {
|
|
|
367
526
|
expect(toArray(encode(obj).slice(0, 12))).toEqual(
|
|
368
527
|
toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0x12, 0x34),
|
|
369
528
|
);
|
|
529
|
+
// @ts-expect-error Access private property
|
|
530
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
370
531
|
});
|
|
371
532
|
|
|
372
533
|
test('encode huge data (128M - 9)', () => {
|
|
@@ -379,11 +540,15 @@ test('encode huge data (128M - 9)', () => {
|
|
|
379
540
|
expect(toArray(encoded.slice(0, 12))).toEqual(
|
|
380
541
|
toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf7, 0x12, 0x34),
|
|
381
542
|
);
|
|
543
|
+
// @ts-expect-error Access private property
|
|
544
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
382
545
|
});
|
|
383
546
|
|
|
384
547
|
test('encode huge data (128M - 8) [error]', () => {
|
|
385
548
|
const obj = [new Uint8Array(128 * 1024 * 1024 - 8)];
|
|
386
549
|
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
550
|
+
// @ts-expect-error Access private property
|
|
551
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
387
552
|
});
|
|
388
553
|
|
|
389
554
|
test('encode huge data (256M)', () => {
|
|
@@ -401,6 +566,8 @@ test('encode huge data (256M)', () => {
|
|
|
401
566
|
toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf7, 0x56, 0x78),
|
|
402
567
|
);
|
|
403
568
|
expect(toArray(encoded.slice(-1))).toEqual(toArray(']'));
|
|
569
|
+
// @ts-expect-error Access private property
|
|
570
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
404
571
|
});
|
|
405
572
|
|
|
406
573
|
test('encode custom toJSON', () => {
|
|
@@ -408,9 +575,13 @@ test('encode custom toJSON', () => {
|
|
|
408
575
|
toJSON: () => ({ a: 1 }),
|
|
409
576
|
};
|
|
410
577
|
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
578
|
+
// @ts-expect-error Access private property
|
|
579
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
411
580
|
});
|
|
412
581
|
|
|
413
582
|
test('encode Date', () => {
|
|
414
583
|
const obj = new Date(0);
|
|
415
584
|
expect(toArray(encode(obj))).toEqual(toArray('S', 'i', 24, ...'1970-01-01T00:00:00.000Z'));
|
|
585
|
+
// @ts-expect-error Access private property
|
|
586
|
+
expect(getEncoder().pool).toBe(poolInit);
|
|
416
587
|
});
|
package/tests/huge-string.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { encode } from '../dist/index.js';
|
|
2
|
+
import { resetEncoder } from '../dist/encoder.js';
|
|
2
3
|
import { toArray } from './.utils.js';
|
|
3
4
|
|
|
4
5
|
const STR_BYTE_LENGTH = 128 * 1024 * 1024 - 20;
|
|
@@ -74,10 +75,12 @@ describe(`encode huge string`, () => {
|
|
|
74
75
|
buffer = global.Buffer;
|
|
75
76
|
// @ts-expect-error remove buffer
|
|
76
77
|
delete global.Buffer;
|
|
78
|
+
resetEncoder();
|
|
77
79
|
});
|
|
78
80
|
|
|
79
81
|
afterAll(() => {
|
|
80
82
|
global.Buffer = buffer;
|
|
83
|
+
resetEncoder();
|
|
81
84
|
});
|
|
82
85
|
|
|
83
86
|
it(`~128M [1]`, () => {
|
package/tests/rxjs/decode.js
CHANGED
|
@@ -77,8 +77,9 @@ test('decode int32', async () => {
|
|
|
77
77
|
expect(await decode(toBuffer('l', 0x12, 0x34, 0x56, 0x78))).toBe(0x1234_5678);
|
|
78
78
|
});
|
|
79
79
|
|
|
80
|
-
test('decode int64
|
|
81
|
-
|
|
80
|
+
test('decode int64', async () => {
|
|
81
|
+
expect(await decode(toBuffer('L', 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0))).toBe(0x1234_5678_9abc_def0n);
|
|
82
|
+
expect(await decode(toBuffer('L', 0x00, 0x00, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0))).toBe(0x5678_9abc_def0);
|
|
82
83
|
});
|
|
83
84
|
|
|
84
85
|
test('decode float32', async () => {
|
|
@@ -140,9 +141,10 @@ test('decode huge string', async () => {
|
|
|
140
141
|
expect(await decode(toBuffer('S', 'l', 0x00, 0x00, 0x00, 6, 'u', 'b', 'j', 's', 'o', 'n'))).toBe('ubjson');
|
|
141
142
|
});
|
|
142
143
|
|
|
143
|
-
test('decode huge string [
|
|
144
|
-
|
|
145
|
-
|
|
144
|
+
test('decode huge string [int64 length]', async () => {
|
|
145
|
+
expect(await decode(toBuffer('S', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'x'))).toBe('x');
|
|
146
|
+
await expect(() => decode(toBuffer('S', 'L', 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'x'))).rejects.toThrow(
|
|
147
|
+
/Invalid length/,
|
|
146
148
|
);
|
|
147
149
|
});
|
|
148
150
|
|
|
@@ -359,9 +361,9 @@ test('decode array (int32, strongly typed, optimized) [use typed array]', async
|
|
|
359
361
|
});
|
|
360
362
|
|
|
361
363
|
test('decode array (int64, strongly typed, optimized) [use typed array]', async () => {
|
|
362
|
-
await
|
|
363
|
-
|
|
364
|
-
)
|
|
364
|
+
expect(await decode(toBuffer('[', '$', 'L', '#', 'i', 1, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98))).toEqual(
|
|
365
|
+
new BigInt64Array([0x1234_5678_fedc_ba98n]),
|
|
366
|
+
);
|
|
365
367
|
});
|
|
366
368
|
|
|
367
369
|
test('decode array (float32, strongly typed, optimized) [use typed array]', async () => {
|
package/tests/rxjs/encode.js
CHANGED
|
@@ -28,7 +28,11 @@ test('encode function', async () => {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
test('encode bigint', async () => {
|
|
31
|
-
|
|
31
|
+
expect(toArray(await encodeAsync(1n))).toEqual(toArray('U', 1));
|
|
32
|
+
expect(toArray(await encodeAsync(0x1876_9876_1234_1234n))).toEqual(
|
|
33
|
+
toArray('L', 0x18, 0x76, 0x98, 0x76, 0x12, 0x34, 0x12, 0x34),
|
|
34
|
+
);
|
|
35
|
+
await expect(encodeAsync(0x8234_5678_90ab_cdefn)).rejects.toThrow(/BigInt value out of range:/);
|
|
32
36
|
});
|
|
33
37
|
|
|
34
38
|
test('encode symbol', async () => {
|
|
@@ -268,7 +272,7 @@ test('encode array (uint64 typed array)', async () => {
|
|
|
268
272
|
});
|
|
269
273
|
|
|
270
274
|
test('encode array (int64 typed array)', async () => {
|
|
271
|
-
|
|
275
|
+
expect(toArray(await encodeAsync(new BigInt64Array()))).toEqual(toArray('[', '$', 'L', '#', 'i', 0));
|
|
272
276
|
});
|
|
273
277
|
|
|
274
278
|
test('encode object', async () => {
|
package/tests/stream/decode.js
CHANGED
|
@@ -72,8 +72,9 @@ test('decode int32', async () => {
|
|
|
72
72
|
expect(await decode(toBuffer('l', 0x12, 0x34, 0x56, 0x78))).toBe(0x1234_5678);
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
-
test('decode int64
|
|
76
|
-
|
|
75
|
+
test('decode int64', async () => {
|
|
76
|
+
expect(await decode(toBuffer('L', 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0))).toBe(0x1234_5678_9abc_def0n);
|
|
77
|
+
expect(await decode(toBuffer('L', 0x00, 0x04, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0))).toBe(0x04_5678_9abc_def0);
|
|
77
78
|
});
|
|
78
79
|
|
|
79
80
|
test('decode float32', async () => {
|
|
@@ -126,9 +127,10 @@ test('decode huge string', async () => {
|
|
|
126
127
|
expect(await decode(toBuffer('S', 'l', 0x00, 0x00, 0x00, 6, 'u', 'b', 'j', 's', 'o', 'n'))).toBe('ubjson');
|
|
127
128
|
});
|
|
128
129
|
|
|
129
|
-
test('decode huge string [
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
test('decode huge string [int64 length]', async () => {
|
|
131
|
+
expect(await decode(toBuffer('S', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'x'))).toBe('x');
|
|
132
|
+
await expect(() => decode(toBuffer('S', 'L', 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'x'))).rejects.toThrow(
|
|
133
|
+
/Invalid length/,
|
|
132
134
|
);
|
|
133
135
|
});
|
|
134
136
|
|
|
@@ -345,9 +347,9 @@ test('decode array (int32, strongly typed, optimized) [use typed array]', async
|
|
|
345
347
|
});
|
|
346
348
|
|
|
347
349
|
test('decode array (int64, strongly typed, optimized) [use typed array]', async () => {
|
|
348
|
-
await
|
|
349
|
-
|
|
350
|
-
)
|
|
350
|
+
expect(await decode(toBuffer('[', '$', 'L', '#', 'i', 1, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98))).toEqual(
|
|
351
|
+
new BigInt64Array([0x1234_5678_fedc_ba98n]),
|
|
352
|
+
);
|
|
351
353
|
});
|
|
352
354
|
|
|
353
355
|
test('decode array (float32, strongly typed, optimized) [use typed array]', async () => {
|
|
@@ -500,3 +502,18 @@ test('decode partial stream [error]', async () => {
|
|
|
500
502
|
expect(onData).nthCalledWith(1, true);
|
|
501
503
|
expect(onData).nthCalledWith(2, false);
|
|
502
504
|
});
|
|
505
|
+
|
|
506
|
+
test('decode in parallel', async () => {
|
|
507
|
+
const buf = toBuffer('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'S', 'i', 3, ...'str', '}');
|
|
508
|
+
const result = await Promise.all([decode(buf), decode(buf)]);
|
|
509
|
+
expect(result).toEqual([
|
|
510
|
+
{
|
|
511
|
+
a: 1,
|
|
512
|
+
b: 'str',
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
a: 1,
|
|
516
|
+
b: 'str',
|
|
517
|
+
},
|
|
518
|
+
]);
|
|
519
|
+
});
|