@cloudpss/ubjson 0.5.11 → 0.5.13
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 +9 -2
- package/dist/common/decoder.js +30 -17
- package/dist/common/decoder.js.map +1 -1
- package/dist/common/encoder.js +30 -4
- package/dist/common/encoder.js.map +1 -1
- package/dist/decoder.d.ts +7 -1
- package/dist/decoder.js +8 -1
- package/dist/decoder.js.map +1 -1
- package/dist/encoder.d.ts +6 -0
- package/dist/encoder.js +33 -4
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/dist/stream/index.d.ts +4 -0
- package/dist/stream/index.js +24 -0
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +2 -2
- package/dist/stream-helper/decoder.js +2 -2
- package/dist/stream-helper/decoder.js.map +1 -1
- package/package.json +2 -2
- package/src/common/decoder.ts +37 -21
- package/src/common/encoder.ts +29 -5
- package/src/decoder.ts +10 -1
- package/src/encoder.ts +34 -4
- package/src/index.ts +18 -3
- package/src/stream/index.ts +22 -0
- package/src/stream-helper/decoder.ts +2 -2
- package/tests/decode.js +39 -9
- package/tests/encode.js +31 -2
- package/tests/many.js +44 -0
- package/tests/rxjs/decode.js +10 -8
- package/tests/rxjs/encode.js +6 -2
- package/tests/stream/decode.js +10 -8
- package/tests/stream/encode.js +6 -2
- package/tests/stream/many.js +100 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { encodeMany, decodeMany } from '../../dist/stream/index.js';
|
|
3
|
+
import { buffer } from 'node:stream/consumers';
|
|
4
|
+
import { finished } from 'node:stream/promises';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* iterableToAsyncIterable
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {Iterable<T>} iterable - iterable
|
|
10
|
+
* @returns {AsyncIterable<T>} async iterable
|
|
11
|
+
*/
|
|
12
|
+
function iterableToAsyncIterable(iterable) {
|
|
13
|
+
return {
|
|
14
|
+
[Symbol.asyncIterator]: () => {
|
|
15
|
+
const iterator = iterable[Symbol.iterator]();
|
|
16
|
+
return {
|
|
17
|
+
next: () => Promise.resolve(iterator.next()),
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* asyncIterableToArray
|
|
25
|
+
* @template T
|
|
26
|
+
* @param {AsyncIterable<T>} asyncIterable - async iterable
|
|
27
|
+
* @returns {Promise<Array<T>>} array
|
|
28
|
+
*/
|
|
29
|
+
async function asyncIterableToArray(asyncIterable) {
|
|
30
|
+
const array = [];
|
|
31
|
+
for await (const item of asyncIterable) {
|
|
32
|
+
array.push(item);
|
|
33
|
+
}
|
|
34
|
+
return array;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('encode/decode many', async () => {
|
|
38
|
+
const data = [
|
|
39
|
+
{
|
|
40
|
+
a: 1,
|
|
41
|
+
b: 2,
|
|
42
|
+
c: 3,
|
|
43
|
+
d: 4,
|
|
44
|
+
e: 5,
|
|
45
|
+
},
|
|
46
|
+
1,
|
|
47
|
+
null,
|
|
48
|
+
'x',
|
|
49
|
+
true,
|
|
50
|
+
[1, 2, 3],
|
|
51
|
+
Uint8Array.of(3, 3, 1),
|
|
52
|
+
];
|
|
53
|
+
const encoded = await buffer(encodeMany(iterableToAsyncIterable(data)));
|
|
54
|
+
const decoded = await asyncIterableToArray(decodeMany(Readable.from([encoded])));
|
|
55
|
+
expect(decoded).toEqual([
|
|
56
|
+
{
|
|
57
|
+
a: 1,
|
|
58
|
+
b: 2,
|
|
59
|
+
c: 3,
|
|
60
|
+
d: 4,
|
|
61
|
+
e: 5,
|
|
62
|
+
},
|
|
63
|
+
1,
|
|
64
|
+
// null is not allowed in stream
|
|
65
|
+
'x',
|
|
66
|
+
true,
|
|
67
|
+
[1, 2, 3],
|
|
68
|
+
Uint8Array.of(3, 3, 1),
|
|
69
|
+
]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('encode/decode many with invalid value', async () => {
|
|
73
|
+
await expect(async () => await finished(encodeMany(iterableToAsyncIterable([1, () => 1])))).rejects.toThrow();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('encode/decode many with error', async () => {
|
|
77
|
+
await expect(
|
|
78
|
+
async () =>
|
|
79
|
+
await finished(
|
|
80
|
+
encodeMany(
|
|
81
|
+
(async function* () {
|
|
82
|
+
yield 1;
|
|
83
|
+
await Promise.resolve();
|
|
84
|
+
throw new Error('xx');
|
|
85
|
+
})(),
|
|
86
|
+
),
|
|
87
|
+
),
|
|
88
|
+
).rejects.toThrow('xx');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('encode/decode many with undefined', async () => {
|
|
92
|
+
const data = [1, null, undefined, [undefined]];
|
|
93
|
+
const encoded = await buffer(encodeMany(iterableToAsyncIterable(data)));
|
|
94
|
+
const decoded = await asyncIterableToArray(decodeMany(Readable.from([encoded])));
|
|
95
|
+
expect(decoded).toEqual([
|
|
96
|
+
1,
|
|
97
|
+
// null is not allowed in stream
|
|
98
|
+
[null],
|
|
99
|
+
]);
|
|
100
|
+
});
|