@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.
Files changed (62) hide show
  1. package/dist/common/decoder.d.ts +7 -3
  2. package/dist/common/decoder.js +9 -1
  3. package/dist/common/decoder.js.map +1 -1
  4. package/dist/common/encoder.js +2 -1
  5. package/dist/common/encoder.js.map +1 -1
  6. package/dist/common/string-decoder.js +2 -1
  7. package/dist/common/string-decoder.js.map +1 -1
  8. package/dist/decoder.d.ts +2 -0
  9. package/dist/decoder.js +3 -0
  10. package/dist/decoder.js.map +1 -0
  11. package/dist/encoder.d.ts +1 -0
  12. package/dist/encoder.js +35 -39
  13. package/dist/encoder.js.map +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/rxjs/decoder.d.ts +3 -0
  17. package/dist/rxjs/decoder.js +68 -0
  18. package/dist/rxjs/decoder.js.map +1 -0
  19. package/dist/rxjs/encoder.d.ts +3 -0
  20. package/dist/rxjs/encoder.js +28 -0
  21. package/dist/rxjs/encoder.js.map +1 -0
  22. package/dist/rxjs/index.d.ts +2 -0
  23. package/dist/rxjs/index.js +3 -0
  24. package/dist/rxjs/index.js.map +1 -0
  25. package/dist/stream/decoder.d.ts +13 -0
  26. package/dist/stream/decoder.js +52 -0
  27. package/dist/stream/decoder.js.map +1 -0
  28. package/dist/stream/encoder.d.ts +6 -11
  29. package/dist/stream/encoder.js +15 -78
  30. package/dist/stream/encoder.js.map +1 -1
  31. package/dist/stream/index.d.ts +8 -1
  32. package/dist/stream/index.js +27 -2
  33. package/dist/stream/index.js.map +1 -1
  34. package/dist/stream-helper/decoder.d.ts +8 -0
  35. package/dist/stream-helper/decoder.js +13 -0
  36. package/dist/stream-helper/decoder.js.map +1 -0
  37. package/dist/stream-helper/encoder.d.ts +12 -0
  38. package/dist/stream-helper/encoder.js +57 -0
  39. package/dist/stream-helper/encoder.js.map +1 -0
  40. package/package.json +5 -2
  41. package/src/common/decoder.ts +13 -4
  42. package/src/common/encoder.ts +2 -1
  43. package/src/common/string-decoder.ts +3 -1
  44. package/src/decoder.ts +3 -0
  45. package/src/encoder.ts +34 -38
  46. package/src/index.ts +1 -1
  47. package/src/rxjs/decoder.ts +65 -0
  48. package/src/rxjs/encoder.ts +27 -0
  49. package/src/rxjs/index.ts +2 -0
  50. package/src/stream/decoder.ts +54 -0
  51. package/src/stream/encoder.ts +16 -76
  52. package/src/stream/index.ts +30 -3
  53. package/src/stream-helper/decoder.ts +15 -0
  54. package/src/stream-helper/encoder.ts +56 -0
  55. package/tests/encode.js +13 -2
  56. package/tests/rxjs/decode.js +535 -0
  57. package/tests/rxjs/encode.js +412 -0
  58. package/tests/stream/decode.js +502 -0
  59. package/tests/stream/encode.js +13 -11
  60. package/tests/string-encoding.js +21 -1
  61. package/tests/tsconfig.json +0 -8
  62. package/tsconfig.json +0 -7
@@ -1,83 +1,23 @@
1
- import { Readable } from 'node:stream';
2
- import { EncoderBase } from '../common/encoder.js';
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 EncoderBase {
9
- protected stream!: Readable;
10
- /**
11
- * 确保 buffer 还有 capacity 的空闲空间
12
- */
13
- protected ensureCapacity(capacity: number): void {
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
- protected currentPos: Array<string | number> = [];
46
- /** 获取写入结果 */
47
- encode(): Readable {
48
- if (typeof this.value != 'object' || this.value == null || ArrayBuffer.isView(this.value)) {
49
- this.stream = new Readable({
50
- objectMode: false,
51
- construct: (callback) => {
52
- this.ensureCapacity(20);
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
  }
@@ -1,8 +1,35 @@
1
- import type { Readable } from 'node:stream';
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
- const encoder = new StreamEncoder(value);
7
- return encoder.encode();
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
- expect(toArray(encode(obj).slice(0, 11))).toEqual(toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0));
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) [error]', () => {
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
  });