@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.
- package/dist/base/decoder.d.ts +1 -0
- package/dist/base/decoder.d.ts.map +1 -0
- package/dist/base/encoder.d.ts +5 -1
- package/dist/base/encoder.d.ts.map +1 -0
- package/dist/base/encoder.js +50 -188
- package/dist/base/encoder.js.map +1 -1
- package/dist/decoder.d.ts +1 -0
- package/dist/decoder.d.ts.map +1 -0
- package/dist/encoder.d.ts +7 -1
- package/dist/encoder.d.ts.map +1 -0
- package/dist/encoder.js +24 -15
- package/dist/encoder.js.map +1 -1
- package/dist/helper/constants.d.ts +1 -0
- package/dist/helper/constants.d.ts.map +1 -0
- package/dist/helper/decode-ae.d.ts +50 -0
- package/dist/helper/decode-ae.d.ts.map +1 -0
- package/dist/helper/decode-ae.js +584 -0
- package/dist/helper/decode-ae.js.map +1 -0
- package/dist/helper/decode.d.ts +6 -3
- package/dist/helper/decode.d.ts.map +1 -0
- package/dist/helper/decode.js +33 -20
- package/dist/helper/decode.js.map +1 -1
- package/dist/helper/encode.d.ts +19 -1
- package/dist/helper/encode.d.ts.map +1 -0
- package/dist/helper/encode.js +138 -16
- package/dist/helper/encode.js.map +1 -1
- package/dist/helper/errors.d.ts +1 -0
- package/dist/helper/errors.d.ts.map +1 -0
- package/dist/helper/string-decoder.d.ts +5 -2
- package/dist/helper/string-decoder.d.ts.map +1 -0
- package/dist/helper/string-decoder.js +10 -38
- package/dist/helper/string-decoder.js.map +1 -1
- package/dist/helper/string-encoder.d.ts +1 -0
- package/dist/helper/string-encoder.d.ts.map +1 -0
- package/dist/helper/utils.d.ts +1 -0
- package/dist/helper/utils.d.ts.map +1 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/options.d.ts +6 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +2 -0
- package/dist/options.js.map +1 -0
- package/dist/rxjs/decoder.d.ts +1 -0
- package/dist/rxjs/decoder.d.ts.map +1 -0
- package/dist/rxjs/decoder.js +66 -40
- package/dist/rxjs/decoder.js.map +1 -1
- package/dist/rxjs/encoder.d.ts +3 -1
- package/dist/rxjs/encoder.d.ts.map +1 -0
- package/dist/rxjs/encoder.js +2 -2
- package/dist/rxjs/encoder.js.map +1 -1
- package/dist/rxjs/index.d.ts +2 -0
- package/dist/rxjs/index.d.ts.map +1 -0
- package/dist/stream/decoder.d.ts +1 -0
- package/dist/stream/decoder.d.ts.map +1 -0
- package/dist/stream/encoder.d.ts +4 -2
- package/dist/stream/encoder.d.ts.map +1 -0
- package/dist/stream/encoder.js +2 -2
- package/dist/stream/encoder.js.map +1 -1
- package/dist/stream/index.d.ts +6 -3
- package/dist/stream/index.d.ts.map +1 -0
- package/dist/stream/index.js +6 -6
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +1 -0
- package/dist/stream-helper/decoder.d.ts.map +1 -0
- package/dist/stream-helper/encoder.d.ts +5 -2
- package/dist/stream-helper/encoder.d.ts.map +1 -0
- package/dist/stream-helper/encoder.js +33 -31
- package/dist/stream-helper/encoder.js.map +1 -1
- package/jest.config.js +1 -1
- package/package.json +3 -4
- package/src/base/encoder.ts +58 -174
- package/src/encoder.ts +27 -15
- package/src/helper/decode-ae.ts +621 -0
- package/src/helper/decode.ts +36 -23
- package/src/helper/encode.ts +141 -15
- package/src/helper/string-decoder.ts +10 -39
- package/src/index.ts +7 -4
- package/src/options.ts +5 -0
- package/src/rxjs/decoder.ts +66 -40
- package/src/rxjs/encoder.ts +3 -2
- package/src/rxjs/index.ts +1 -0
- package/src/stream/encoder.ts +4 -3
- package/src/stream/index.ts +8 -6
- package/src/stream-helper/encoder.ts +39 -34
- package/tests/.utils.js +1 -0
- package/tests/e2e/stream.js +13 -1
- package/tests/encode.js +8 -0
- package/tests/string-encoding.js +1 -10
- package/tests/tsconfig.json +1 -7
- 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(
|
|
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
|
-
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
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(
|
|
124
|
-
this.view.
|
|
125
|
-
this.length +=
|
|
125
|
+
this.ensureCapacity(8);
|
|
126
|
+
this.view.setFloat64(this.length, (value as Float64Array)[i]!);
|
|
127
|
+
this.length += 8;
|
|
126
128
|
}
|
|
127
|
-
} else if (
|
|
129
|
+
} else if (type === constants.INT32) {
|
|
130
|
+
const arrayLength = byteLength / 4;
|
|
128
131
|
for (let i = 0; i < arrayLength; i++) {
|
|
129
|
-
this.ensureCapacity(
|
|
130
|
-
this.view.setInt32(this.length, value[i]!);
|
|
131
|
-
this.length +=
|
|
132
|
+
this.ensureCapacity(4);
|
|
133
|
+
this.view.setInt32(this.length, (value as Int32Array)[i]!);
|
|
134
|
+
this.length += 4;
|
|
132
135
|
}
|
|
133
|
-
} else if (
|
|
136
|
+
} else if (type === constants.INT64) {
|
|
137
|
+
const arrayLength = byteLength / 8;
|
|
134
138
|
for (let i = 0; i < arrayLength; i++) {
|
|
135
|
-
this.ensureCapacity(
|
|
136
|
-
this.view.
|
|
137
|
-
this.length +=
|
|
139
|
+
this.ensureCapacity(8);
|
|
140
|
+
this.view.setBigInt64(this.length, (value as BigInt64Array)[i]!);
|
|
141
|
+
this.length += 8;
|
|
138
142
|
}
|
|
139
|
-
} else if (
|
|
143
|
+
} else if (type === constants.FLOAT32) {
|
|
144
|
+
const arrayLength = byteLength / 4;
|
|
140
145
|
for (let i = 0; i < arrayLength; i++) {
|
|
141
|
-
this.ensureCapacity(
|
|
142
|
-
this.view.
|
|
143
|
-
this.length +=
|
|
146
|
+
this.ensureCapacity(4);
|
|
147
|
+
this.view.setFloat32(this.length, (value as Float32Array)[i]!);
|
|
148
|
+
this.length += 4;
|
|
144
149
|
}
|
|
145
|
-
} else
|
|
150
|
+
} else {
|
|
151
|
+
(type) satisfies constants.INT16;
|
|
152
|
+
const arrayLength = byteLength / 2;
|
|
146
153
|
for (let i = 0; i < arrayLength; i++) {
|
|
147
|
-
this.ensureCapacity(
|
|
148
|
-
this.view.
|
|
149
|
-
this.length +=
|
|
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
|
* 重设所有环境
|
package/tests/e2e/stream.js
CHANGED
|
@@ -13,7 +13,19 @@ describe('stream', () => {
|
|
|
13
13
|
expect(decoded).toEqual(expected);
|
|
14
14
|
}).rejects.toThrow(expected);
|
|
15
15
|
} else {
|
|
16
|
-
const
|
|
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
|
package/tests/string-encoding.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Encoder } from '../dist/encoder.js';
|
|
2
|
-
import { decode,
|
|
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) {
|
package/tests/tsconfig.json
CHANGED
package/tsconfig.json
CHANGED