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