@cloudpss/ubjson 0.3.11 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common/decoder.d.ts +7 -3
- package/dist/common/decoder.js +9 -1
- package/dist/common/decoder.js.map +1 -1
- package/dist/common/encoder.js +2 -1
- package/dist/common/encoder.js.map +1 -1
- package/dist/common/string-decoder.js +2 -1
- package/dist/common/string-decoder.js.map +1 -1
- package/dist/decoder.d.ts +2 -0
- package/dist/decoder.js +3 -0
- package/dist/decoder.js.map +1 -0
- package/dist/encoder.d.ts +1 -0
- package/dist/encoder.js +35 -39
- package/dist/encoder.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/rxjs/decoder.d.ts +3 -0
- package/dist/rxjs/decoder.js +68 -0
- package/dist/rxjs/decoder.js.map +1 -0
- package/dist/rxjs/encoder.d.ts +3 -0
- package/dist/rxjs/encoder.js +28 -0
- package/dist/rxjs/encoder.js.map +1 -0
- package/dist/rxjs/index.d.ts +2 -0
- package/dist/rxjs/index.js +3 -0
- package/dist/rxjs/index.js.map +1 -0
- package/dist/stream/decoder.d.ts +13 -0
- package/dist/stream/decoder.js +52 -0
- package/dist/stream/decoder.js.map +1 -0
- package/dist/stream/encoder.d.ts +6 -11
- package/dist/stream/encoder.js +15 -78
- package/dist/stream/encoder.js.map +1 -1
- package/dist/stream/index.d.ts +8 -1
- package/dist/stream/index.js +27 -2
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +8 -0
- package/dist/stream-helper/decoder.js +13 -0
- package/dist/stream-helper/decoder.js.map +1 -0
- package/dist/stream-helper/encoder.d.ts +12 -0
- package/dist/stream-helper/encoder.js +57 -0
- package/dist/stream-helper/encoder.js.map +1 -0
- package/package.json +5 -2
- package/src/common/decoder.ts +13 -4
- package/src/common/encoder.ts +2 -1
- package/src/common/string-decoder.ts +3 -1
- package/src/decoder.ts +3 -0
- package/src/encoder.ts +34 -38
- package/src/index.ts +1 -1
- package/src/rxjs/decoder.ts +65 -0
- package/src/rxjs/encoder.ts +27 -0
- package/src/rxjs/index.ts +2 -0
- package/src/stream/decoder.ts +54 -0
- package/src/stream/encoder.ts +16 -76
- package/src/stream/index.ts +30 -3
- package/src/stream-helper/decoder.ts +15 -0
- package/src/stream-helper/encoder.ts +56 -0
- package/tests/encode.js +13 -2
- package/tests/rxjs/decode.js +535 -0
- package/tests/rxjs/encode.js +412 -0
- package/tests/stream/decode.js +502 -0
- package/tests/stream/encode.js +13 -11
- package/tests/string-encoding.js +21 -1
- package/tests/tsconfig.json +0 -8
- package/tsconfig.json +0 -7
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests from https://bitbucket.org/shelacek/ubjson
|
|
3
|
+
*/
|
|
4
|
+
import { firstValueFrom, of, reduce, Subject } from 'rxjs';
|
|
5
|
+
import { encode } from '../../dist/rxjs/index.js';
|
|
6
|
+
import { decode, encode as encodeRef } from '../../dist/index.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {unknown} value
|
|
10
|
+
*/
|
|
11
|
+
function encodeAsync(value) {
|
|
12
|
+
return firstValueFrom(
|
|
13
|
+
of(value).pipe(
|
|
14
|
+
encode(),
|
|
15
|
+
reduce((a, b) => Buffer.concat([a, b])),
|
|
16
|
+
),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param {[ArrayBuffer] | [Uint8Array] | (number | string)[]} args
|
|
23
|
+
* @returns {number[]}
|
|
24
|
+
*/
|
|
25
|
+
function toArray(...args) {
|
|
26
|
+
if (args[0] instanceof ArrayBuffer) {
|
|
27
|
+
return Array.from(new Uint8Array(args[0]));
|
|
28
|
+
}
|
|
29
|
+
if (args[0] instanceof Uint8Array) {
|
|
30
|
+
return Array.from(args[0]);
|
|
31
|
+
}
|
|
32
|
+
return args.map((x) => (typeof x == 'number' ? x : /** @type {string} */ (x).charCodeAt(0)));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test('encode function', async () => {
|
|
36
|
+
await expect(() => encodeAsync(function () {})).rejects.toThrow();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('encode bigint', async () => {
|
|
40
|
+
await expect(() => encodeAsync(1n)).rejects.toThrow();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('encode symbol', async () => {
|
|
44
|
+
await expect(() => encodeAsync(Symbol('sym'))).rejects.toThrow();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('encode undefined', async () => {
|
|
48
|
+
expect(toArray(await encodeAsync(undefined))).toEqual(toArray('N'));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('encode null', async () => {
|
|
52
|
+
expect(toArray(await encodeAsync(null))).toEqual(toArray('Z'));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('encode true', async () => {
|
|
56
|
+
expect(toArray(await encodeAsync(true))).toEqual(toArray('T'));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('encode false', async () => {
|
|
60
|
+
expect(toArray(await encodeAsync(false))).toEqual(toArray('F'));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('encode int8', async () => {
|
|
64
|
+
expect(toArray(await encodeAsync(-1))).toEqual(toArray('i', 255));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('encode uint8', async () => {
|
|
68
|
+
expect(toArray(await encodeAsync(200))).toEqual(toArray('U', 200));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('encode int16', async () => {
|
|
72
|
+
expect(toArray(await encodeAsync(0x1234))).toEqual(toArray('I', 0x12, 0x34));
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('encode int32', async () => {
|
|
76
|
+
expect(toArray(await encodeAsync(0x12345678))).toEqual(toArray('l', 0x12, 0x34, 0x56, 0x78));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('encode float32', async () => {
|
|
80
|
+
expect(toArray(await encodeAsync(1.00390625))).toEqual(toArray('d', 0x3f, 0x80, 0x80, 0x00));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('encode float32 (too large integer)', async () => {
|
|
84
|
+
expect(toArray(await encodeAsync(2147483648))).toEqual(toArray('d', 0x4f, 0x00, 0x00, 0x00));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('encode float64', async () => {
|
|
88
|
+
expect(toArray(await encodeAsync(100000.00390625))).toEqual(
|
|
89
|
+
toArray('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00),
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('encode char', async () => {
|
|
94
|
+
expect(toArray(await encodeAsync('a'))).toEqual(toArray('C', 'a'));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('encode char 128', async () => {
|
|
98
|
+
expect(toArray(await encodeAsync('\xcc'))).toEqual(toArray('C', '\xcc'));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('encode char 257', async () => {
|
|
102
|
+
expect(toArray(await encodeAsync('\u0123'))).toEqual(toArray('S', 'i', 2, 196, 163));
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('encode char emoji', async () => {
|
|
106
|
+
expect(toArray(await encodeAsync('💖'))).toEqual(toArray('S', 'i', 4, 240, 159, 146, 150));
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('encode string', async () => {
|
|
110
|
+
expect(toArray(await encodeAsync('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('encode huge string', async () => {
|
|
114
|
+
const str = 'ubjson💖💖💖'.repeat(10000);
|
|
115
|
+
expect(toArray(await encodeAsync(str))).toEqual(toArray(encodeRef(str)));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('encode string (no encodeInto)', async () => {
|
|
119
|
+
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
TextEncoder.prototype.encodeInto = undefined;
|
|
122
|
+
expect(toArray(await encodeAsync('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
123
|
+
|
|
124
|
+
TextEncoder.prototype.encodeInto = encodeInto;
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('encode array', async () => {
|
|
128
|
+
expect(toArray(await encodeAsync([1, 2, 3, -1]))).toEqual(toArray('[', 'U', 1, 'U', 2, 'U', 3, 'i', 255, ']'));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('encode array (empty)', async () => {
|
|
132
|
+
expect(toArray(await encodeAsync([]))).toEqual(toArray('[', ']'));
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('encode array (undefined)', async () => {
|
|
136
|
+
expect(toArray(await encodeAsync([undefined]))).toEqual(toArray('[', 'Z', ']'));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('encode array (spares)', async () => {
|
|
140
|
+
const array = new Array(3);
|
|
141
|
+
array[1] = true;
|
|
142
|
+
expect(toArray(await encodeAsync(array))).toEqual(toArray('[', 'Z', 'T', 'Z', ']'));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('encode array (mixed)', async () => {
|
|
146
|
+
expect(toArray(await encodeAsync([1, 'a', true]))).toEqual(toArray('[', 'U', 1, 'C', 'a', 'T', ']'));
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('encode array (int8)', async () => {
|
|
150
|
+
expect(toArray(await encodeAsync([-1, 2, 3]))).toEqual(toArray('[', 'i', 255, 'U', 2, 'U', 3, ']'));
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('encode array (int16)', async () => {
|
|
154
|
+
expect(toArray(await encodeAsync([255, -1]))).toEqual(toArray('[', 'U', 0xff, 'i', 0xff, ']'));
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('encode array (only null values)', async () => {
|
|
158
|
+
expect(toArray(await encodeAsync([null, null, null]))).toEqual(toArray('[', 'Z', 'Z', 'Z', ']'));
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('encode N-D array', async () => {
|
|
162
|
+
expect(
|
|
163
|
+
decode(
|
|
164
|
+
await encodeAsync([
|
|
165
|
+
[1, 2, 3],
|
|
166
|
+
[4, 5, 6],
|
|
167
|
+
]),
|
|
168
|
+
),
|
|
169
|
+
).toEqual([
|
|
170
|
+
[1, 2, 3],
|
|
171
|
+
[4, 5, 6],
|
|
172
|
+
]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test('encode array of objects', async () => {
|
|
176
|
+
expect(
|
|
177
|
+
decode(
|
|
178
|
+
await encodeAsync([
|
|
179
|
+
{ a: 1, b: 2, c: 3 },
|
|
180
|
+
{ d: 4, e: 5, f: 6 },
|
|
181
|
+
]),
|
|
182
|
+
),
|
|
183
|
+
).toEqual([
|
|
184
|
+
{ a: 1, b: 2, c: 3 },
|
|
185
|
+
{ d: 4, e: 5, f: 6 },
|
|
186
|
+
]);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('encode array of objects of arrays', async () => {
|
|
190
|
+
expect(
|
|
191
|
+
decode(
|
|
192
|
+
await encodeAsync([
|
|
193
|
+
{ a: [1, 2], b: [3, 4] },
|
|
194
|
+
{ c: [5, 6], d: [7, 8] },
|
|
195
|
+
]),
|
|
196
|
+
),
|
|
197
|
+
).toEqual([
|
|
198
|
+
{ a: [1, 2], b: [3, 4] },
|
|
199
|
+
{ c: [5, 6], d: [7, 8] },
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('encode array (int8 typed array)', async () => {
|
|
204
|
+
expect(toArray(await encodeAsync(Int8Array.from([18, -2])))).toEqual(
|
|
205
|
+
toArray('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe),
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('encode array (uint8 typed array)', async () => {
|
|
210
|
+
expect(toArray(await encodeAsync(Uint8Array.from([18, 254])))).toEqual(
|
|
211
|
+
toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe),
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('encode array (int16 typed array)', async () => {
|
|
216
|
+
expect(toArray(await encodeAsync(Int16Array.from([4660, -292])))).toEqual(
|
|
217
|
+
toArray('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc),
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('encode array (int32 typed array)', async () => {
|
|
222
|
+
expect(toArray(await encodeAsync(Int32Array.from([305419896, -19088744])))).toEqual(
|
|
223
|
+
toArray('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98),
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test('encode array (float32 typed array)', async () => {
|
|
228
|
+
expect(toArray(await encodeAsync(Float32Array.from([0.25, 0.125])))).toEqual(
|
|
229
|
+
toArray('[', '$', 'd', '#', 'i', 2, 0x3e, 0x80, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00),
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('encode array (float64 typed array)', async () => {
|
|
234
|
+
expect(toArray(await encodeAsync(Float64Array.from([0.25, 0.125])))).toEqual(
|
|
235
|
+
toArray(
|
|
236
|
+
'[',
|
|
237
|
+
'$',
|
|
238
|
+
'D',
|
|
239
|
+
'#',
|
|
240
|
+
'i',
|
|
241
|
+
2,
|
|
242
|
+
0x3f,
|
|
243
|
+
0xd0,
|
|
244
|
+
0x00,
|
|
245
|
+
0x00,
|
|
246
|
+
0x00,
|
|
247
|
+
0x00,
|
|
248
|
+
0x00,
|
|
249
|
+
0x00,
|
|
250
|
+
0x3f,
|
|
251
|
+
0xc0,
|
|
252
|
+
0x00,
|
|
253
|
+
0x00,
|
|
254
|
+
0x00,
|
|
255
|
+
0x00,
|
|
256
|
+
0x00,
|
|
257
|
+
0x00,
|
|
258
|
+
),
|
|
259
|
+
);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('encode array (uint8clamped typed array)', async () => {
|
|
263
|
+
await expect(() => encodeAsync(new Uint8ClampedArray())).rejects.toThrow();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test('encode array (uint16 typed array)', async () => {
|
|
267
|
+
await expect(() => encodeAsync(new Uint16Array())).rejects.toThrow();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('encode array (uint32 typed array)', async () => {
|
|
271
|
+
await expect(() => encodeAsync(new Uint32Array())).rejects.toThrow();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test('encode array (uint64 typed array)', async () => {
|
|
275
|
+
await expect(() => encodeAsync(new BigUint64Array())).rejects.toThrow();
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('encode array (int64 typed array)', async () => {
|
|
279
|
+
await expect(() => encodeAsync(new BigInt64Array())).rejects.toThrow();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('encode object', async () => {
|
|
283
|
+
expect(toArray(await encodeAsync({ a: 1, b: 2, c: 3 }))).toEqual(
|
|
284
|
+
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'),
|
|
285
|
+
);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test('encode object (empty)', async () => {
|
|
289
|
+
expect(toArray(await encodeAsync({}))).toEqual(toArray('{', '}'));
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('encode object (empty key)', async () => {
|
|
293
|
+
expect(toArray(await encodeAsync({ '': '' }))).toEqual(toArray('{', 'i', 0, 'S', 'i', 0, '}'));
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test('encode object (mixed)', async () => {
|
|
297
|
+
expect(toArray(await encodeAsync({ a: 1, b: 'a', c: true }))).toEqual(
|
|
298
|
+
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
|
|
299
|
+
);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('encode object (only null values)', async () => {
|
|
303
|
+
expect(toArray(await encodeAsync({ a: null, b: null, c: null }))).toEqual(
|
|
304
|
+
toArray('{', 'i', 1, 'a', 'Z', 'i', 1, 'b', 'Z', 'i', 1, 'c', 'Z', '}'),
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('encode object (skip prototype)', async () => {
|
|
309
|
+
const obj = Object.create({ a: 2, x: 'xx' });
|
|
310
|
+
obj.a = 1;
|
|
311
|
+
obj.b = 'a';
|
|
312
|
+
obj.c = true;
|
|
313
|
+
expect(toArray(await encodeAsync(obj))).toEqual(
|
|
314
|
+
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
|
|
315
|
+
);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test('encode object (skip symbol)', async () => {
|
|
319
|
+
const obj = { [Symbol()]: true, a: 1 };
|
|
320
|
+
expect(toArray(await encodeAsync(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('encode object (skip non-enumerable)', async () => {
|
|
324
|
+
const obj = {};
|
|
325
|
+
Object.defineProperty(obj, 'a', { value: 1, configurable: true, writable: true });
|
|
326
|
+
expect(toArray(await encodeAsync(obj))).toEqual(toArray('{', '}'));
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test('encode object (include getter)', async () => {
|
|
330
|
+
const obj = {};
|
|
331
|
+
Object.defineProperty(obj, 'a', { get: () => 1, enumerable: true });
|
|
332
|
+
expect(toArray(await encodeAsync(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('encode object (skip undefined)', async () => {
|
|
336
|
+
const obj = { a: undefined };
|
|
337
|
+
expect(toArray(await encodeAsync(obj))).toEqual(toArray('{', '}'));
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('encode object (function) [error]', async () => {
|
|
341
|
+
const obj = { a: () => {} };
|
|
342
|
+
await expect(() => encodeAsync(obj)).rejects.toThrow(/Unsupported type \[object Function\]/);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test('encode object (include null)', async () => {
|
|
346
|
+
const obj = { a: null };
|
|
347
|
+
expect(toArray(await encodeAsync(obj))).toEqual(toArray('{', 'i', 1, 'a', 'Z', '}'));
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test('encode huge typed array (16K)', async () => {
|
|
351
|
+
const obj = new Uint8Array(16 * 1024);
|
|
352
|
+
expect(toArray((await encodeAsync(obj)).slice(0, 8))).toEqual(toArray('[', '$', 'U', '#', 'I', 0x40, 0x00, 0));
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test('encode huge typed array (~128M)', async () => {
|
|
356
|
+
const obj = new Uint8Array(128 * 1024 * 1024 - 10);
|
|
357
|
+
expect(toArray((await encodeAsync(obj)).slice(0, 10))).toEqual(
|
|
358
|
+
toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0),
|
|
359
|
+
);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test('encode huge typed array (256M + 1) [error]', async () => {
|
|
363
|
+
const obj = new Uint8Array(256 * 1024 * 1024 + 1);
|
|
364
|
+
await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test('encode huge typed array (3G) [error]', async () => {
|
|
368
|
+
const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
|
|
369
|
+
await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
test('encode huge data (~128M)', async () => {
|
|
373
|
+
const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
|
|
374
|
+
expect(toArray((await encodeAsync(obj)).slice(0, 11))).toEqual(
|
|
375
|
+
toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0),
|
|
376
|
+
);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test('encode huge data (256M + 1) [error]', async () => {
|
|
380
|
+
const obj = [new Uint8Array(256 * 1024 * 1024 + 1)];
|
|
381
|
+
await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
test('encode stream', async () => {
|
|
385
|
+
const stream = new Subject();
|
|
386
|
+
const result = firstValueFrom(
|
|
387
|
+
stream.pipe(
|
|
388
|
+
encode(),
|
|
389
|
+
reduce((a, b) => Buffer.concat([a, b])),
|
|
390
|
+
),
|
|
391
|
+
);
|
|
392
|
+
stream.next(undefined);
|
|
393
|
+
stream.next(true);
|
|
394
|
+
stream.next(false);
|
|
395
|
+
stream.complete();
|
|
396
|
+
expect(toArray(await result)).toEqual(toArray('N', 'T', 'F'));
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test('encode stream input error [error]', async () => {
|
|
400
|
+
const stream = new Subject();
|
|
401
|
+
const result = firstValueFrom(
|
|
402
|
+
stream.pipe(
|
|
403
|
+
encode(),
|
|
404
|
+
reduce((a, b) => Buffer.concat([a, b])),
|
|
405
|
+
),
|
|
406
|
+
);
|
|
407
|
+
stream.next(undefined);
|
|
408
|
+
stream.next(true);
|
|
409
|
+
stream.next(false);
|
|
410
|
+
stream.error(new Error('123456'));
|
|
411
|
+
expect(result).rejects.toThrow('123456');
|
|
412
|
+
});
|