@cloudpss/ubjson 0.4.11 → 0.4.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/benchmark-string.js +32 -32
- package/benchmark.js +33 -35
- package/dist/common/constants.d.ts +21 -21
- package/dist/common/constants.js +21 -21
- package/dist/common/decoder.d.ts +52 -52
- package/dist/common/decoder.js +277 -277
- package/dist/common/encoder.d.ts +74 -74
- package/dist/common/encoder.js +282 -282
- package/dist/common/string-decoder.d.ts +13 -13
- package/dist/common/string-decoder.js +106 -106
- package/dist/common/string-encoder.d.ts +6 -6
- package/dist/common/string-encoder.js +37 -37
- package/dist/decoder.d.ts +2 -2
- package/dist/decoder.js +2 -2
- package/dist/encoder.d.ts +11 -11
- package/dist/encoder.js +77 -77
- package/dist/index.d.ts +6 -6
- package/dist/index.js +15 -15
- package/dist/rxjs/decoder.d.ts +3 -3
- package/dist/rxjs/decoder.js +69 -69
- package/dist/rxjs/decoder.js.map +1 -1
- package/dist/rxjs/encoder.d.ts +3 -3
- package/dist/rxjs/encoder.js +27 -27
- package/dist/rxjs/encoder.js.map +1 -1
- package/dist/rxjs/index.d.ts +2 -2
- package/dist/rxjs/index.js +2 -2
- package/dist/stream/decoder.d.ts +13 -13
- package/dist/stream/decoder.js +51 -51
- package/dist/stream/decoder.js.map +1 -1
- package/dist/stream/encoder.d.ts +9 -9
- package/dist/stream/encoder.js +22 -22
- package/dist/stream/encoder.js.map +1 -1
- package/dist/stream/index.d.ts +11 -11
- package/dist/stream/index.js +31 -31
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +8 -8
- package/dist/stream-helper/decoder.js +12 -12
- package/dist/stream-helper/encoder.d.ts +12 -12
- package/dist/stream-helper/encoder.js +56 -56
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +7 -7
- package/package.json +3 -2
- package/src/rxjs/decoder.ts +1 -1
- package/src/rxjs/encoder.ts +1 -1
- package/src/stream/decoder.ts +1 -1
- package/src/stream/encoder.ts +1 -1
- package/tests/.utils.js +25 -0
- package/tests/decode.js +2 -9
- package/tests/e2e.js +2 -0
- package/tests/encode.js +13 -17
- package/tests/rxjs/decode.js +8 -11
- package/tests/rxjs/encode.js +17 -19
- package/tests/stream/decode.js +7 -10
- package/tests/stream/encode.js +16 -18
- package/tests/string-encoding.js +7 -14
- package/yarn-error.log +4520 -0
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import { EncoderBase } from '../common/encoder.js';
|
|
2
|
-
const BLOCK_SIZE = 1024 * 8; // 8 KiB
|
|
3
|
-
const MAX_SIZE = 1024 * 1024 * 256; // 256 MiB
|
|
4
|
-
/** 流式编码 UBJSON */
|
|
5
|
-
export class StreamEncoderHelper extends EncoderBase {
|
|
6
|
-
constructor(value, onChunk) {
|
|
7
|
-
super(value);
|
|
8
|
-
this.onChunk = onChunk;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 确保 buffer 还有 capacity 的空闲空间
|
|
12
|
-
*/
|
|
13
|
-
ensureCapacity(capacity) {
|
|
14
|
-
if (capacity > MAX_SIZE) {
|
|
15
|
-
// 超过最大尺寸限制
|
|
16
|
-
throw new Error('Buffer has exceed max size');
|
|
17
|
-
}
|
|
18
|
-
if (this.buffer == null) {
|
|
19
|
-
this.buffer = new Uint8Array(capacity);
|
|
20
|
-
this.view = new DataView(this.buffer.buffer);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (capacity < 0) {
|
|
24
|
-
// 结束流
|
|
25
|
-
this.onChunk(this.buffer.subarray(0, this.length));
|
|
26
|
-
this.buffer = new Uint8Array(0);
|
|
27
|
-
this.view = new DataView(this.buffer.buffer);
|
|
28
|
-
this.length = 0;
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
// 无需扩容
|
|
32
|
-
if (this.buffer.byteLength >= this.length + capacity)
|
|
33
|
-
return;
|
|
34
|
-
// 提交目前的数据
|
|
35
|
-
this.onChunk(this.buffer.subarray(0, this.length));
|
|
36
|
-
// 重新分配缓冲区
|
|
37
|
-
if (capacity < BLOCK_SIZE)
|
|
38
|
-
capacity = BLOCK_SIZE;
|
|
39
|
-
this.buffer = new Uint8Array(capacity);
|
|
40
|
-
this.view = new DataView(this.buffer.buffer);
|
|
41
|
-
this.length = 0;
|
|
42
|
-
}
|
|
43
|
-
/** 获取写入结果 */
|
|
44
|
-
encode() {
|
|
45
|
-
if (typeof this.value != 'object' || this.value == null || ArrayBuffer.isView(this.value)) {
|
|
46
|
-
this.ensureCapacity(20);
|
|
47
|
-
this.write(this.value);
|
|
48
|
-
this.ensureCapacity(-1);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
this.ensureCapacity(BLOCK_SIZE);
|
|
52
|
-
this.write(this.value);
|
|
53
|
-
this.ensureCapacity(-1);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
1
|
+
import { EncoderBase } from '../common/encoder.js';
|
|
2
|
+
const BLOCK_SIZE = 1024 * 8; // 8 KiB
|
|
3
|
+
const MAX_SIZE = 1024 * 1024 * 256; // 256 MiB
|
|
4
|
+
/** 流式编码 UBJSON */
|
|
5
|
+
export class StreamEncoderHelper extends EncoderBase {
|
|
6
|
+
constructor(value, onChunk) {
|
|
7
|
+
super(value);
|
|
8
|
+
this.onChunk = onChunk;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 确保 buffer 还有 capacity 的空闲空间
|
|
12
|
+
*/
|
|
13
|
+
ensureCapacity(capacity) {
|
|
14
|
+
if (capacity > MAX_SIZE) {
|
|
15
|
+
// 超过最大尺寸限制
|
|
16
|
+
throw new Error('Buffer has exceed max size');
|
|
17
|
+
}
|
|
18
|
+
if (this.buffer == null) {
|
|
19
|
+
this.buffer = new Uint8Array(capacity);
|
|
20
|
+
this.view = new DataView(this.buffer.buffer);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (capacity < 0) {
|
|
24
|
+
// 结束流
|
|
25
|
+
this.onChunk(this.buffer.subarray(0, this.length));
|
|
26
|
+
this.buffer = new Uint8Array(0);
|
|
27
|
+
this.view = new DataView(this.buffer.buffer);
|
|
28
|
+
this.length = 0;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// 无需扩容
|
|
32
|
+
if (this.buffer.byteLength >= this.length + capacity)
|
|
33
|
+
return;
|
|
34
|
+
// 提交目前的数据
|
|
35
|
+
this.onChunk(this.buffer.subarray(0, this.length));
|
|
36
|
+
// 重新分配缓冲区
|
|
37
|
+
if (capacity < BLOCK_SIZE)
|
|
38
|
+
capacity = BLOCK_SIZE;
|
|
39
|
+
this.buffer = new Uint8Array(capacity);
|
|
40
|
+
this.view = new DataView(this.buffer.buffer);
|
|
41
|
+
this.length = 0;
|
|
42
|
+
}
|
|
43
|
+
/** 获取写入结果 */
|
|
44
|
+
encode() {
|
|
45
|
+
if (typeof this.value != 'object' || this.value == null || ArrayBuffer.isView(this.value)) {
|
|
46
|
+
this.ensureCapacity(20);
|
|
47
|
+
this.write(this.value);
|
|
48
|
+
this.ensureCapacity(-1);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
this.ensureCapacity(BLOCK_SIZE);
|
|
52
|
+
this.write(this.value);
|
|
53
|
+
this.ensureCapacity(-1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
57
|
//# sourceMappingURL=encoder.js.map
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/** 支持的数据转为 Uint8Array */
|
|
2
|
-
export declare function toUint8Array(data: BinaryData): Uint8Array;
|
|
1
|
+
/** 支持的数据转为 Uint8Array */
|
|
2
|
+
export declare function toUint8Array(data: BinaryData): Uint8Array;
|
package/dist/utils.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/** 支持的数据转为 Uint8Array */
|
|
2
|
-
export function toUint8Array(data) {
|
|
3
|
-
if (ArrayBuffer.isView(data)) {
|
|
4
|
-
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
5
|
-
}
|
|
6
|
-
return new Uint8Array(data, 0, data.byteLength);
|
|
7
|
-
}
|
|
1
|
+
/** 支持的数据转为 Uint8Array */
|
|
2
|
+
export function toUint8Array(data) {
|
|
3
|
+
if (ArrayBuffer.isView(data)) {
|
|
4
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
5
|
+
}
|
|
6
|
+
return new Uint8Array(data, 0, data.byteLength);
|
|
7
|
+
}
|
|
8
8
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/ubjson",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.13",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@msgpack/msgpack": "^3.0.0-beta2"
|
|
52
|
+
"@msgpack/msgpack": "^3.0.0-beta2",
|
|
53
|
+
"@types/lodash": "^4.14.192"
|
|
53
54
|
},
|
|
54
55
|
"dependencies": {
|
|
55
56
|
"rxjs": "^7.8.0"
|
package/src/rxjs/decoder.ts
CHANGED
package/src/rxjs/encoder.ts
CHANGED
package/src/stream/decoder.ts
CHANGED
package/src/stream/encoder.ts
CHANGED
package/tests/.utils.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 输入转为数字数组以便比较
|
|
3
|
+
*
|
|
4
|
+
* @param {[ArrayBuffer] | [Uint8Array] | (number | string)[]} args 输入
|
|
5
|
+
* @returns {number[]} 数字数组
|
|
6
|
+
*/
|
|
7
|
+
export function toArray(...args) {
|
|
8
|
+
if (args[0] instanceof ArrayBuffer) {
|
|
9
|
+
return Array.from(new Uint8Array(args[0]));
|
|
10
|
+
}
|
|
11
|
+
if (args[0] instanceof Uint8Array) {
|
|
12
|
+
return Array.from(args[0]);
|
|
13
|
+
}
|
|
14
|
+
return args.map((x) => (typeof x == 'number' ? x : /** @type {string} */ (x).charCodeAt(0)));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 将数字或字符串转换为 Uint8Array
|
|
19
|
+
*
|
|
20
|
+
* @param {...string | number} args 数字或 char 的数组
|
|
21
|
+
* @returns {Uint8Array} Uint8Array
|
|
22
|
+
*/
|
|
23
|
+
export function toBuffer(...args) {
|
|
24
|
+
return Uint8Array.from(args, (x) => (typeof x == 'number' ? x : x.charCodeAt(0)));
|
|
25
|
+
}
|
package/tests/decode.js
CHANGED
|
@@ -3,21 +3,14 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
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
|
-
}
|
|
6
|
+
import { toBuffer } from './.utils.js';
|
|
14
7
|
|
|
15
8
|
test('decode unsupported type', () => {
|
|
16
9
|
expect(() => decode(toBuffer('!'))).toThrow();
|
|
17
10
|
});
|
|
18
11
|
|
|
19
12
|
test('decode undefined data', () => {
|
|
20
|
-
// @ts-expect-error
|
|
13
|
+
// @ts-expect-error 不传参数
|
|
21
14
|
expect(() => decode(undefined)).toThrow();
|
|
22
15
|
});
|
|
23
16
|
|
package/tests/e2e.js
CHANGED
|
@@ -279,7 +279,9 @@ test('encode/decode model', () => {
|
|
|
279
279
|
});
|
|
280
280
|
|
|
281
281
|
test('encode/decode complex object (no encodeInto)', () => {
|
|
282
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
282
283
|
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
284
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
283
285
|
// @ts-ignore
|
|
284
286
|
TextEncoder.prototype.encodeInto = undefined;
|
|
285
287
|
const expected = {
|
package/tests/encode.js
CHANGED
|
@@ -3,24 +3,14 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { encode, decode } from '../dist/index.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @param {[ArrayBuffer] | [Uint8Array] | (number | string)[]} args
|
|
10
|
-
* @returns {number[]}
|
|
11
|
-
*/
|
|
12
|
-
function toArray(...args) {
|
|
13
|
-
if (args[0] instanceof ArrayBuffer) {
|
|
14
|
-
return Array.from(new Uint8Array(args[0]));
|
|
15
|
-
}
|
|
16
|
-
if (args[0] instanceof Uint8Array) {
|
|
17
|
-
return Array.from(args[0]);
|
|
18
|
-
}
|
|
19
|
-
return args.map((x) => (typeof x == 'number' ? x : /** @type {string} */ (x).charCodeAt(0)));
|
|
20
|
-
}
|
|
6
|
+
import { toArray } from './.utils.js';
|
|
21
7
|
|
|
22
8
|
test('encode function', () => {
|
|
23
|
-
expect(() =>
|
|
9
|
+
expect(() =>
|
|
10
|
+
encode(function () {
|
|
11
|
+
//noop
|
|
12
|
+
}),
|
|
13
|
+
).toThrow();
|
|
24
14
|
});
|
|
25
15
|
|
|
26
16
|
test('encode bigint', () => {
|
|
@@ -96,7 +86,9 @@ test('encode string', () => {
|
|
|
96
86
|
});
|
|
97
87
|
|
|
98
88
|
test('encode string (no encodeInto)', () => {
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
99
90
|
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
100
92
|
// @ts-ignore
|
|
101
93
|
TextEncoder.prototype.encodeInto = undefined;
|
|
102
94
|
expect(toArray(encode('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
@@ -314,7 +306,11 @@ test('encode object (skip undefined)', () => {
|
|
|
314
306
|
});
|
|
315
307
|
|
|
316
308
|
test('encode object (function) [error]', () => {
|
|
317
|
-
const obj = {
|
|
309
|
+
const obj = {
|
|
310
|
+
a: () => {
|
|
311
|
+
// noop
|
|
312
|
+
},
|
|
313
|
+
};
|
|
318
314
|
expect(() => encode(obj)).toThrow(/Unsupported type \[object Function\]/);
|
|
319
315
|
});
|
|
320
316
|
|
package/tests/rxjs/decode.js
CHANGED
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
import { jest } from '@jest/globals';
|
|
5
5
|
import { firstValueFrom, of, Subject } from 'rxjs';
|
|
6
6
|
import { decode as decodePipe } from '../../dist/rxjs/index.js';
|
|
7
|
+
import { toBuffer } from '../.utils.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
+
* 包装为 promise
|
|
11
|
+
*
|
|
12
|
+
* @param {Uint8Array} data ubjson 数据
|
|
10
13
|
*/
|
|
11
14
|
async function decode(data) {
|
|
12
15
|
const readable = of(data);
|
|
@@ -14,8 +17,10 @@ async function decode(data) {
|
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
20
|
+
* 包装为 promise
|
|
21
|
+
*
|
|
22
|
+
* @param {import("rxjs").Observable<Uint8Array>} observable ubjson 数据流
|
|
23
|
+
* @param {(data: unknown) => void} onData 数据回调
|
|
19
24
|
*/
|
|
20
25
|
function decodeStream(observable, onData) {
|
|
21
26
|
return /** @type {Promise<void>} */ (
|
|
@@ -29,14 +34,6 @@ function decodeStream(observable, onData) {
|
|
|
29
34
|
);
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
/**
|
|
33
|
-
* @param {...string | number} args
|
|
34
|
-
* @returns {Uint8Array}
|
|
35
|
-
*/
|
|
36
|
-
function toBuffer(...args) {
|
|
37
|
-
return Uint8Array.from(args, (x) => (typeof x == 'number' ? x : x.charCodeAt(0)));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
37
|
test('decode unsupported type', async () => {
|
|
41
38
|
await expect(() => decode(toBuffer('!'))).rejects.toThrow();
|
|
42
39
|
});
|
package/tests/rxjs/encode.js
CHANGED
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
import { firstValueFrom, of, reduce, Subject } from 'rxjs';
|
|
5
5
|
import { encode } from '../../dist/rxjs/index.js';
|
|
6
6
|
import { decode, encode as encodeRef } from '../../dist/index.js';
|
|
7
|
+
import { toArray } from '../.utils.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
+
* 包装为 promise
|
|
11
|
+
*
|
|
12
|
+
* @param {unknown} value 要编码的值
|
|
10
13
|
*/
|
|
11
14
|
function encodeAsync(value) {
|
|
12
15
|
return firstValueFrom(
|
|
@@ -17,23 +20,12 @@ function encodeAsync(value) {
|
|
|
17
20
|
);
|
|
18
21
|
}
|
|
19
22
|
|
|
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
23
|
test('encode function', async () => {
|
|
36
|
-
await expect(() =>
|
|
24
|
+
await expect(() =>
|
|
25
|
+
encodeAsync(function () {
|
|
26
|
+
// noop
|
|
27
|
+
}),
|
|
28
|
+
).rejects.toThrow();
|
|
37
29
|
});
|
|
38
30
|
|
|
39
31
|
test('encode bigint', async () => {
|
|
@@ -116,7 +108,9 @@ test('encode huge string', async () => {
|
|
|
116
108
|
});
|
|
117
109
|
|
|
118
110
|
test('encode string (no encodeInto)', async () => {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
119
112
|
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
120
114
|
// @ts-ignore
|
|
121
115
|
TextEncoder.prototype.encodeInto = undefined;
|
|
122
116
|
expect(toArray(await encodeAsync('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
@@ -338,7 +332,11 @@ test('encode object (skip undefined)', async () => {
|
|
|
338
332
|
});
|
|
339
333
|
|
|
340
334
|
test('encode object (function) [error]', async () => {
|
|
341
|
-
const obj = {
|
|
335
|
+
const obj = {
|
|
336
|
+
a: () => {
|
|
337
|
+
// noop
|
|
338
|
+
},
|
|
339
|
+
};
|
|
342
340
|
await expect(() => encodeAsync(obj)).rejects.toThrow(/Unsupported type \[object Function\]/);
|
|
343
341
|
});
|
|
344
342
|
|
|
@@ -408,5 +406,5 @@ test('encode stream input error [error]', async () => {
|
|
|
408
406
|
stream.next(true);
|
|
409
407
|
stream.next(false);
|
|
410
408
|
stream.error(new Error('123456'));
|
|
411
|
-
expect(result).rejects.toThrow('123456');
|
|
409
|
+
await expect(result).rejects.toThrow('123456');
|
|
412
410
|
});
|
package/tests/stream/decode.js
CHANGED
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
import { jest } from '@jest/globals';
|
|
5
5
|
import { Readable } from 'node:stream';
|
|
6
6
|
import { decoder as decodeStream, decode as decodeAsync } from '../../dist/stream/index.js';
|
|
7
|
+
import { toBuffer } from '../.utils.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
+
* 包装为 promise
|
|
11
|
+
*
|
|
12
|
+
* @param {Uint8Array} data ubjson 数据
|
|
10
13
|
*/
|
|
11
14
|
async function decode(data) {
|
|
12
15
|
const readable = Readable.from([data], { objectMode: false });
|
|
@@ -14,7 +17,9 @@ async function decode(data) {
|
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
|
-
*
|
|
20
|
+
* 包装为 promise
|
|
21
|
+
*
|
|
22
|
+
* @param {import("stream").Transform} stream ubjson 流
|
|
18
23
|
*/
|
|
19
24
|
function eos(stream) {
|
|
20
25
|
return new Promise((resolve, reject) => {
|
|
@@ -23,14 +28,6 @@ function eos(stream) {
|
|
|
23
28
|
});
|
|
24
29
|
}
|
|
25
30
|
|
|
26
|
-
/**
|
|
27
|
-
* @param {...string | number} args
|
|
28
|
-
* @returns {Uint8Array}
|
|
29
|
-
*/
|
|
30
|
-
function toBuffer(...args) {
|
|
31
|
-
return Uint8Array.from(args, (x) => (typeof x == 'number' ? x : x.charCodeAt(0)));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
31
|
test('decode unsupported type', async () => {
|
|
35
32
|
await expect(() => decode(toBuffer('!'))).rejects.toThrow();
|
|
36
33
|
});
|
package/tests/stream/encode.js
CHANGED
|
@@ -4,31 +4,23 @@
|
|
|
4
4
|
import { buffer } from 'node:stream/consumers';
|
|
5
5
|
import { encode, encoder } from '../../dist/stream/index.js';
|
|
6
6
|
import { decode, encode as encodeRef } from '../../dist/index.js';
|
|
7
|
+
import { toArray } from '../.utils.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
+
* 包装为 promise
|
|
11
|
+
*
|
|
12
|
+
* @param {unknown} value 值
|
|
10
13
|
*/
|
|
11
14
|
function encodeAsync(value) {
|
|
12
15
|
return buffer(encode(value));
|
|
13
16
|
}
|
|
14
17
|
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
17
|
-
* @param {[ArrayBuffer] | [Uint8Array] | (number | string)[]} args
|
|
18
|
-
* @returns {number[]}
|
|
19
|
-
*/
|
|
20
|
-
function toArray(...args) {
|
|
21
|
-
if (args[0] instanceof ArrayBuffer) {
|
|
22
|
-
return Array.from(new Uint8Array(args[0]));
|
|
23
|
-
}
|
|
24
|
-
if (args[0] instanceof Uint8Array) {
|
|
25
|
-
return Array.from(args[0]);
|
|
26
|
-
}
|
|
27
|
-
return args.map((x) => (typeof x == 'number' ? x : /** @type {string} */ (x).charCodeAt(0)));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
18
|
test('encode function', async () => {
|
|
31
|
-
await expect(() =>
|
|
19
|
+
await expect(() =>
|
|
20
|
+
encodeAsync(function () {
|
|
21
|
+
// noop
|
|
22
|
+
}),
|
|
23
|
+
).rejects.toThrow();
|
|
32
24
|
});
|
|
33
25
|
|
|
34
26
|
test('encode bigint', async () => {
|
|
@@ -111,7 +103,9 @@ test('encode huge string', async () => {
|
|
|
111
103
|
});
|
|
112
104
|
|
|
113
105
|
test('encode string (no encodeInto)', async () => {
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
114
107
|
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
115
109
|
// @ts-ignore
|
|
116
110
|
TextEncoder.prototype.encodeInto = undefined;
|
|
117
111
|
expect(toArray(await encodeAsync('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
@@ -333,7 +327,11 @@ test('encode object (skip undefined)', async () => {
|
|
|
333
327
|
});
|
|
334
328
|
|
|
335
329
|
test('encode object (function) [error]', async () => {
|
|
336
|
-
const obj = {
|
|
330
|
+
const obj = {
|
|
331
|
+
a: () => {
|
|
332
|
+
// noop
|
|
333
|
+
},
|
|
334
|
+
};
|
|
337
335
|
await expect(() => encodeAsync(obj)).rejects.toThrow(/Unsupported type \[object Function\]/);
|
|
338
336
|
});
|
|
339
337
|
|
package/tests/string-encoding.js
CHANGED
|
@@ -2,11 +2,12 @@ import { StringEncoder } from '../dist/common/string-encoder.js';
|
|
|
2
2
|
import { StringDecoder, decodeJs } from '../dist/common/string-decoder.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param {Pick<TextEncoder, 'encode'>} encoder
|
|
7
|
-
* @param {Pick<TextDecoder, 'decode'>} decoder
|
|
5
|
+
* 测试编码
|
|
8
6
|
*/
|
|
9
|
-
function testEncoding(
|
|
7
|
+
function testEncoding(
|
|
8
|
+
/** @type {Pick<TextEncoder, 'encode'>} */ encoder,
|
|
9
|
+
/** @type {Pick<TextDecoder, 'decode'>} */ decoder,
|
|
10
|
+
) {
|
|
10
11
|
expect(decoder.decode(encoder.encode(''))).toEqual('');
|
|
11
12
|
expect(decoder.decode(encoder.encode('p4'))).toEqual('p4');
|
|
12
13
|
expect(decoder.decode(encoder.encode('t0'))).toEqual('t0');
|
|
@@ -59,11 +60,7 @@ test('decode string', () => {
|
|
|
59
60
|
testEncoding(
|
|
60
61
|
new TextEncoder(),
|
|
61
62
|
new (class extends StringDecoder {
|
|
62
|
-
/**
|
|
63
|
-
* @override
|
|
64
|
-
* @param {Uint8Array} buffer
|
|
65
|
-
*/
|
|
66
|
-
decode(buffer) {
|
|
63
|
+
/** @override */ decode(/** @type {Uint8Array} */ buffer) {
|
|
67
64
|
return super.decode(buffer, 0, buffer.byteLength);
|
|
68
65
|
}
|
|
69
66
|
})(),
|
|
@@ -74,11 +71,7 @@ test('decode string js', () => {
|
|
|
74
71
|
testEncoding(
|
|
75
72
|
new TextEncoder(),
|
|
76
73
|
new (class extends StringDecoder {
|
|
77
|
-
/**
|
|
78
|
-
* @override
|
|
79
|
-
* @param {Uint8Array} buffer
|
|
80
|
-
*/
|
|
81
|
-
decode(buffer) {
|
|
74
|
+
/** @override */ decode(/** @type {Uint8Array} */ buffer) {
|
|
82
75
|
return decodeJs(buffer, 0, buffer.byteLength);
|
|
83
76
|
}
|
|
84
77
|
})(),
|