@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
package/src/stream/encoder.ts
CHANGED
|
@@ -1,83 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const BLOCK_SIZE = 1024 * 8; // 8 KiB
|
|
5
|
-
const MAX_SIZE = 1024 * 1024 * 256; //256 MiB
|
|
1
|
+
import { Transform, TransformCallback } from 'node:stream';
|
|
2
|
+
import { StreamEncoderHelper } from '../stream-helper/encoder.js';
|
|
6
3
|
|
|
7
4
|
/** 流式编码 UBJSON */
|
|
8
|
-
export class StreamEncoder extends
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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.stream.push(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
|
-
this.stream.push(null);
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
// 无需扩容
|
|
33
|
-
if (this.buffer.byteLength >= this.length + capacity) return;
|
|
34
|
-
|
|
35
|
-
// 提交目前的数据
|
|
36
|
-
this.stream.push(this.buffer.subarray(0, this.length));
|
|
37
|
-
|
|
38
|
-
// 重新分配缓冲区
|
|
39
|
-
if (capacity < BLOCK_SIZE) capacity = BLOCK_SIZE;
|
|
40
|
-
this.buffer = new Uint8Array(capacity);
|
|
41
|
-
this.view = new DataView(this.buffer.buffer);
|
|
42
|
-
this.length = 0;
|
|
5
|
+
export class StreamEncoder extends Transform {
|
|
6
|
+
constructor() {
|
|
7
|
+
super({
|
|
8
|
+
readableObjectMode: false,
|
|
9
|
+
writableObjectMode: true,
|
|
10
|
+
});
|
|
43
11
|
}
|
|
44
12
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
callback();
|
|
54
|
-
},
|
|
55
|
-
read: () => {
|
|
56
|
-
try {
|
|
57
|
-
this.write(this.value);
|
|
58
|
-
this.ensureCapacity(-1);
|
|
59
|
-
} catch (ex) {
|
|
60
|
-
this.stream.destroy(ex as Error);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
} else {
|
|
65
|
-
this.stream = new Readable({
|
|
66
|
-
objectMode: false,
|
|
67
|
-
construct: (callback) => {
|
|
68
|
-
this.ensureCapacity(BLOCK_SIZE);
|
|
69
|
-
callback();
|
|
70
|
-
},
|
|
71
|
-
read: () => {
|
|
72
|
-
try {
|
|
73
|
-
this.write(this.value);
|
|
74
|
-
this.ensureCapacity(-1);
|
|
75
|
-
} catch (ex) {
|
|
76
|
-
this.stream.destroy(ex as Error);
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
});
|
|
13
|
+
/** @inheritdoc */
|
|
14
|
+
override _transform(obj: unknown, _encoding: BufferEncoding, callback: TransformCallback): void {
|
|
15
|
+
const helper = new StreamEncoderHelper(obj, (binary) => this.push(binary));
|
|
16
|
+
try {
|
|
17
|
+
helper.encode();
|
|
18
|
+
callback();
|
|
19
|
+
} catch (ex) {
|
|
20
|
+
callback(ex as Error);
|
|
80
21
|
}
|
|
81
|
-
return this.stream;
|
|
82
22
|
}
|
|
83
23
|
}
|
package/src/stream/index.ts
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Readable, Transform } from 'node:stream';
|
|
2
2
|
import { StreamEncoder } from './encoder.js';
|
|
3
|
+
import { StreamDecoder } from './decoder.js';
|
|
3
4
|
|
|
4
5
|
/** 编码为 UBJSON */
|
|
5
6
|
export function encode(value: unknown): Readable {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
if (value == null) {
|
|
8
|
+
return Readable.from([value === null ? 'Z' : 'N'], { objectMode: false });
|
|
9
|
+
}
|
|
10
|
+
const encoder = new StreamEncoder();
|
|
11
|
+
encoder.write(value);
|
|
12
|
+
encoder.end();
|
|
13
|
+
return encoder;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 编码为 UBJSON */
|
|
17
|
+
export function encoder(): Transform {
|
|
18
|
+
return new StreamEncoder();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 解码 UBJSON */
|
|
22
|
+
export function decode(stream: NodeJS.ReadableStream): Promise<unknown> {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const decoder = new StreamDecoder();
|
|
25
|
+
decoder.on('error', reject);
|
|
26
|
+
decoder.on('end', resolve);
|
|
27
|
+
decoder.on('data', resolve);
|
|
28
|
+
stream.pipe(decoder);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 解码 UBJSON */
|
|
33
|
+
export function decoder(): Transform {
|
|
34
|
+
return new StreamDecoder();
|
|
8
35
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Decoder, UnexpectedEof } from '../common/decoder.js';
|
|
2
|
+
|
|
3
|
+
export { UnexpectedEof };
|
|
4
|
+
|
|
5
|
+
/** 流式解码 UBJSON */
|
|
6
|
+
export class StreamDecoderHelper extends Decoder {
|
|
7
|
+
constructor(data: Uint8Array) {
|
|
8
|
+
super(data);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** 读取的字节数 */
|
|
12
|
+
get readLength(): number {
|
|
13
|
+
return this.offset;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { EncoderBase } from '../common/encoder.js';
|
|
2
|
+
|
|
3
|
+
const BLOCK_SIZE = 1024 * 8; // 8 KiB
|
|
4
|
+
const MAX_SIZE = 1024 * 1024 * 256; // 256 MiB
|
|
5
|
+
|
|
6
|
+
/** 流式编码 UBJSON */
|
|
7
|
+
export class StreamEncoderHelper extends EncoderBase {
|
|
8
|
+
constructor(value: unknown, protected readonly onChunk: (chunk: Uint8Array) => void) {
|
|
9
|
+
super(value);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 确保 buffer 还有 capacity 的空闲空间
|
|
13
|
+
*/
|
|
14
|
+
protected ensureCapacity(capacity: number): void {
|
|
15
|
+
if (capacity > MAX_SIZE) {
|
|
16
|
+
// 超过最大尺寸限制
|
|
17
|
+
throw new Error('Buffer has exceed max size');
|
|
18
|
+
}
|
|
19
|
+
if (this.buffer == null) {
|
|
20
|
+
this.buffer = new Uint8Array(capacity);
|
|
21
|
+
this.view = new DataView(this.buffer.buffer);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (capacity < 0) {
|
|
25
|
+
// 结束流
|
|
26
|
+
this.onChunk(this.buffer.subarray(0, this.length));
|
|
27
|
+
this.buffer = new Uint8Array(0);
|
|
28
|
+
this.view = new DataView(this.buffer.buffer);
|
|
29
|
+
this.length = 0;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// 无需扩容
|
|
33
|
+
if (this.buffer.byteLength >= this.length + capacity) return;
|
|
34
|
+
|
|
35
|
+
// 提交目前的数据
|
|
36
|
+
this.onChunk(this.buffer.subarray(0, this.length));
|
|
37
|
+
|
|
38
|
+
// 重新分配缓冲区
|
|
39
|
+
if (capacity < BLOCK_SIZE) capacity = BLOCK_SIZE;
|
|
40
|
+
this.buffer = new Uint8Array(capacity);
|
|
41
|
+
this.view = new DataView(this.buffer.buffer);
|
|
42
|
+
this.length = 0;
|
|
43
|
+
}
|
|
44
|
+
/** 获取写入结果 */
|
|
45
|
+
encode(): void {
|
|
46
|
+
if (typeof this.value != 'object' || this.value == null || ArrayBuffer.isView(this.value)) {
|
|
47
|
+
this.ensureCapacity(20);
|
|
48
|
+
this.write(this.value);
|
|
49
|
+
this.ensureCapacity(-1);
|
|
50
|
+
} else {
|
|
51
|
+
this.ensureCapacity(BLOCK_SIZE);
|
|
52
|
+
this.write(this.value);
|
|
53
|
+
this.ensureCapacity(-1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
package/tests/encode.js
CHANGED
|
@@ -345,10 +345,21 @@ test('encode huge typed array (3G) [error]', () => {
|
|
|
345
345
|
|
|
346
346
|
test('encode huge data (~128M)', () => {
|
|
347
347
|
const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
|
|
348
|
-
|
|
348
|
+
obj[0][0] = 0x12;
|
|
349
|
+
obj[0][1] = 0x34;
|
|
350
|
+
expect(toArray(encode(obj).slice(0, 12))).toEqual(
|
|
351
|
+
toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0x12, 0x34),
|
|
352
|
+
);
|
|
349
353
|
});
|
|
350
354
|
|
|
351
|
-
test('encode huge data (128M)
|
|
355
|
+
test('encode huge data (128M)', () => {
|
|
352
356
|
const obj = [new Uint8Array(128 * 1024 * 1024)];
|
|
357
|
+
obj[0][0] = 0x12;
|
|
358
|
+
obj[0][1] = 0x34;
|
|
359
|
+
expect(toArray(encode(obj).slice(0, 12))).toEqual(toArray('[', '[', '$', 'U', '#', 'l', 0x8, 0, 0, 0, 0x12, 0x34));
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test('encode huge data (128M + 1) [error]', () => {
|
|
363
|
+
const obj = [new Uint8Array(128 * 1024 * 1024 + 1)];
|
|
353
364
|
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
354
365
|
});
|