@cloudpss/ubjson 0.5.39 → 0.5.41

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 (92) hide show
  1. package/dist/base/decoder.d.ts +1 -0
  2. package/dist/base/decoder.d.ts.map +1 -0
  3. package/dist/base/encoder.d.ts +5 -1
  4. package/dist/base/encoder.d.ts.map +1 -0
  5. package/dist/base/encoder.js +50 -188
  6. package/dist/base/encoder.js.map +1 -1
  7. package/dist/decoder.d.ts +1 -0
  8. package/dist/decoder.d.ts.map +1 -0
  9. package/dist/encoder.d.ts +7 -1
  10. package/dist/encoder.d.ts.map +1 -0
  11. package/dist/encoder.js +24 -15
  12. package/dist/encoder.js.map +1 -1
  13. package/dist/helper/constants.d.ts +1 -0
  14. package/dist/helper/constants.d.ts.map +1 -0
  15. package/dist/helper/decode-ae.d.ts +50 -0
  16. package/dist/helper/decode-ae.d.ts.map +1 -0
  17. package/dist/helper/decode-ae.js +584 -0
  18. package/dist/helper/decode-ae.js.map +1 -0
  19. package/dist/helper/decode.d.ts +6 -3
  20. package/dist/helper/decode.d.ts.map +1 -0
  21. package/dist/helper/decode.js +33 -20
  22. package/dist/helper/decode.js.map +1 -1
  23. package/dist/helper/encode.d.ts +19 -1
  24. package/dist/helper/encode.d.ts.map +1 -0
  25. package/dist/helper/encode.js +138 -16
  26. package/dist/helper/encode.js.map +1 -1
  27. package/dist/helper/errors.d.ts +1 -0
  28. package/dist/helper/errors.d.ts.map +1 -0
  29. package/dist/helper/string-decoder.d.ts +5 -2
  30. package/dist/helper/string-decoder.d.ts.map +1 -0
  31. package/dist/helper/string-decoder.js +10 -38
  32. package/dist/helper/string-decoder.js.map +1 -1
  33. package/dist/helper/string-encoder.d.ts +1 -0
  34. package/dist/helper/string-encoder.d.ts.map +1 -0
  35. package/dist/helper/utils.d.ts +1 -0
  36. package/dist/helper/utils.d.ts.map +1 -0
  37. package/dist/index.d.ts +5 -2
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +4 -4
  40. package/dist/index.js.map +1 -1
  41. package/dist/options.d.ts +6 -0
  42. package/dist/options.d.ts.map +1 -0
  43. package/dist/options.js +2 -0
  44. package/dist/options.js.map +1 -0
  45. package/dist/rxjs/decoder.d.ts +1 -0
  46. package/dist/rxjs/decoder.d.ts.map +1 -0
  47. package/dist/rxjs/decoder.js +66 -40
  48. package/dist/rxjs/decoder.js.map +1 -1
  49. package/dist/rxjs/encoder.d.ts +3 -1
  50. package/dist/rxjs/encoder.d.ts.map +1 -0
  51. package/dist/rxjs/encoder.js +2 -2
  52. package/dist/rxjs/encoder.js.map +1 -1
  53. package/dist/rxjs/index.d.ts +2 -0
  54. package/dist/rxjs/index.d.ts.map +1 -0
  55. package/dist/stream/decoder.d.ts +1 -0
  56. package/dist/stream/decoder.d.ts.map +1 -0
  57. package/dist/stream/encoder.d.ts +4 -2
  58. package/dist/stream/encoder.d.ts.map +1 -0
  59. package/dist/stream/encoder.js +2 -2
  60. package/dist/stream/encoder.js.map +1 -1
  61. package/dist/stream/index.d.ts +6 -3
  62. package/dist/stream/index.d.ts.map +1 -0
  63. package/dist/stream/index.js +6 -6
  64. package/dist/stream/index.js.map +1 -1
  65. package/dist/stream-helper/decoder.d.ts +1 -0
  66. package/dist/stream-helper/decoder.d.ts.map +1 -0
  67. package/dist/stream-helper/encoder.d.ts +5 -2
  68. package/dist/stream-helper/encoder.d.ts.map +1 -0
  69. package/dist/stream-helper/encoder.js +33 -31
  70. package/dist/stream-helper/encoder.js.map +1 -1
  71. package/jest.config.js +1 -1
  72. package/package.json +3 -4
  73. package/src/base/encoder.ts +58 -174
  74. package/src/encoder.ts +27 -15
  75. package/src/helper/decode-ae.ts +621 -0
  76. package/src/helper/decode.ts +36 -23
  77. package/src/helper/encode.ts +141 -15
  78. package/src/helper/string-decoder.ts +10 -39
  79. package/src/index.ts +7 -4
  80. package/src/options.ts +5 -0
  81. package/src/rxjs/decoder.ts +66 -40
  82. package/src/rxjs/encoder.ts +3 -2
  83. package/src/rxjs/index.ts +1 -0
  84. package/src/stream/encoder.ts +4 -3
  85. package/src/stream/index.ts +8 -6
  86. package/src/stream-helper/encoder.ts +39 -34
  87. package/tests/.utils.js +1 -0
  88. package/tests/e2e/stream.js +13 -1
  89. package/tests/encode.js +8 -0
  90. package/tests/string-encoding.js +1 -10
  91. package/tests/tsconfig.json +1 -7
  92. package/tsconfig.json +1 -1
@@ -1,7 +1,8 @@
1
1
  import { constants } from '../helper/constants.js';
2
- import { unsupportedView } from '../helper/errors.js';
3
2
  import { encode, stringByteLength } from '../helper/string-encoder.js';
4
3
  import { EncoderBase } from '../base/encoder.js';
4
+ import type { TypedArrayType } from '../helper/encode.js';
5
+ import type { EncodeOptions } from '../options.js';
5
6
 
6
7
  const BLOCK_SIZE = 1024 * 64; // 64 KiB
7
8
  const MAX_SIZE = 1024 * 1024 * 32; // 32 MiB
@@ -30,8 +31,12 @@ function free(buf: Uint8Array): boolean {
30
31
 
31
32
  /** 流式编码 UBJSON */
32
33
  export class StreamEncoderHelper extends EncoderBase {
33
- constructor(protected readonly onChunk: (chunk: Uint8Array) => void) {
34
+ constructor(
35
+ options: EncodeOptions | null | undefined,
36
+ protected readonly onChunk: (chunk: Uint8Array) => void,
37
+ ) {
34
38
  super();
39
+ this.sortObjectKeys = options?.sortObjectKeys ?? false;
35
40
  this.data = alloc(BLOCK_SIZE);
36
41
  this.view = new DataView(this.data.buffer);
37
42
  }
@@ -102,54 +107,54 @@ export class StreamEncoderHelper extends EncoderBase {
102
107
  }
103
108
  }
104
109
  /** @inheritdoc */
105
- protected override writeLargeTypedArrayData(value: ArrayBufferView): void {
110
+ protected override writeLargeTypedArrayData(type: TypedArrayType, value: ArrayBufferView): void {
106
111
  this.ensureCapacity(-1);
107
- if (value instanceof Uint8Array || value instanceof Int8Array) {
112
+ const { byteLength } = value;
113
+ if (type === constants.UINT8 || type === constants.INT8) {
108
114
  // fast path for typed arrays with `BYTES_PER_ELEMENT` of 1
109
115
  // divide buffer to 64k chunks
110
- for (let i = 0; i < value.byteLength; i += BLOCK_SIZE) {
111
- this.onChunk(
112
- new Uint8Array(value.buffer.slice(value.byteOffset + i, value.byteOffset + i + BLOCK_SIZE)),
113
- );
116
+ const { buffer, byteOffset } = value;
117
+ for (let i = 0; i < byteLength; i += BLOCK_SIZE) {
118
+ this.onChunk(new Uint8Array(buffer.slice(byteOffset + i, byteOffset + i + BLOCK_SIZE)));
114
119
  }
115
120
  return;
116
121
  }
117
-
118
- const arrayLength = (value as Int16Array | Int32Array | BigInt64Array | Float32Array | Float64Array).length;
119
- const elementSize = (value as Int16Array | Int32Array | BigInt64Array | Float32Array | Float64Array)
120
- .BYTES_PER_ELEMENT;
121
- if (value instanceof Int16Array) {
122
+ if (type === constants.FLOAT64) {
123
+ const arrayLength = byteLength / 8;
122
124
  for (let i = 0; i < arrayLength; i++) {
123
- this.ensureCapacity(elementSize);
124
- this.view.setInt16(this.length, value[i]!);
125
- this.length += elementSize;
125
+ this.ensureCapacity(8);
126
+ this.view.setFloat64(this.length, (value as Float64Array)[i]!);
127
+ this.length += 8;
126
128
  }
127
- } else if (value instanceof Int32Array) {
129
+ } else if (type === constants.INT32) {
130
+ const arrayLength = byteLength / 4;
128
131
  for (let i = 0; i < arrayLength; i++) {
129
- this.ensureCapacity(elementSize);
130
- this.view.setInt32(this.length, value[i]!);
131
- this.length += elementSize;
132
+ this.ensureCapacity(4);
133
+ this.view.setInt32(this.length, (value as Int32Array)[i]!);
134
+ this.length += 4;
132
135
  }
133
- } else if (value instanceof Float32Array) {
136
+ } else if (type === constants.INT64) {
137
+ const arrayLength = byteLength / 8;
134
138
  for (let i = 0; i < arrayLength; i++) {
135
- this.ensureCapacity(elementSize);
136
- this.view.setFloat32(this.length, value[i]!);
137
- this.length += elementSize;
139
+ this.ensureCapacity(8);
140
+ this.view.setBigInt64(this.length, (value as BigInt64Array)[i]!);
141
+ this.length += 8;
138
142
  }
139
- } else if (value instanceof Float64Array) {
143
+ } else if (type === constants.FLOAT32) {
144
+ const arrayLength = byteLength / 4;
140
145
  for (let i = 0; i < arrayLength; i++) {
141
- this.ensureCapacity(elementSize);
142
- this.view.setFloat64(this.length, value[i]!);
143
- this.length += elementSize;
146
+ this.ensureCapacity(4);
147
+ this.view.setFloat32(this.length, (value as Float32Array)[i]!);
148
+ this.length += 4;
144
149
  }
145
- } else if (value instanceof BigInt64Array) {
150
+ } else {
151
+ (type) satisfies constants.INT16;
152
+ const arrayLength = byteLength / 2;
146
153
  for (let i = 0; i < arrayLength; i++) {
147
- this.ensureCapacity(elementSize);
148
- this.view.setBigInt64(this.length, value[i]!);
149
- this.length += elementSize;
154
+ this.ensureCapacity(2);
155
+ this.view.setInt16(this.length, (value as Int16Array)[i]!);
156
+ this.length += 2;
150
157
  }
151
- } else {
152
- unsupportedView(value);
153
158
  }
154
159
  }
155
160
  /** 获取写入结果 */
package/tests/.utils.js CHANGED
@@ -2,6 +2,7 @@ import { resetEnv as resetDecoderEnv } from '../dist/helper/string-decoder.js';
2
2
  import { resetEnv as resetEncoderEnv } from '../dist/helper/string-encoder.js';
3
3
  import { resetEncoder } from '../dist/encoder.js';
4
4
  import '../dist/helper/constants.js';
5
+ import '../dist/options.js';
5
6
 
6
7
  /**
7
8
  * 重设所有环境
@@ -13,7 +13,19 @@ describe('stream', () => {
13
13
  expect(decoded).toEqual(expected);
14
14
  }).rejects.toThrow(expected);
15
15
  } else {
16
- const decoded = await decodeStream(encodeStream(input));
16
+ const encoded = await encodeStream(input).toArray();
17
+ const data = encoded.flatMap((/** @type {Buffer} */ chunk) => {
18
+ // split to random chunks
19
+ const chunks = [];
20
+ let offset = 0;
21
+ while (offset < chunk.length) {
22
+ const size = Math.floor(Math.random() * chunk.length);
23
+ chunks.push(chunk.subarray(offset, offset + size));
24
+ offset += size;
25
+ }
26
+ return chunks;
27
+ });
28
+ const decoded = await decodeStream(Readable.from(data));
17
29
  expect(decoded).toEqual(expected);
18
30
  }
19
31
  });
package/tests/encode.js CHANGED
@@ -407,6 +407,14 @@ test('encode object', () => {
407
407
  expect(getEncoder().pool).toBe(poolInit);
408
408
  });
409
409
 
410
+ test('encode object (keep order)', () => {
411
+ expect(toArray(encode({ b: 2, a: 1, c: 3 }, { sortObjectKeys: true }))).toEqual(
412
+ toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'),
413
+ );
414
+ // @ts-expect-error Access private property
415
+ expect(getEncoder().pool).toBe(poolInit);
416
+ });
417
+
410
418
  test('encode object (empty)', () => {
411
419
  expect(toArray(encode({}))).toEqual(toArray('{', '}'));
412
420
  // @ts-expect-error Access private property
@@ -1,5 +1,5 @@
1
1
  import { Encoder } from '../dist/encoder.js';
2
- import { decode, decodeKey, jsDecode } from '../dist/helper/string-decoder.js';
2
+ import { decode, jsDecode } from '../dist/helper/string-decoder.js';
3
3
 
4
4
  /**
5
5
  * 测试编码
@@ -111,15 +111,6 @@ test('decode string', () => {
111
111
  });
112
112
  });
113
113
 
114
- test('decode string key', () => {
115
- testEncoding(new TextEncoder(), {
116
- decode(buffer) {
117
- if (!(buffer instanceof Uint8Array)) return '';
118
- return decodeKey(buffer, 0, buffer.byteLength);
119
- },
120
- });
121
- });
122
-
123
114
  test('decode string js', () => {
124
115
  testEncoding(new TextEncoder(), {
125
116
  decode(buffer) {
@@ -1,9 +1,3 @@
1
1
  {
2
- "extends": "../tsconfig",
3
- "include": ["./"],
4
- "compilerOptions": {
5
- "checkJs": true,
6
- "noEmit": true,
7
- "noUncheckedIndexedAccess": false
8
- }
2
+ "extends": "../../tsconfig.test"
9
3
  }
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "extends": "../../tsconfig",
2
+ "extends": "../tsconfig.build",
3
3
  "compilerOptions": {
4
4
  "isolatedModules": false,
5
5
  "verbatimModuleSyntax": false