@cloudpss/ubjson 0.3.6 → 0.3.9
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 +18 -20
- package/benchmark.js +1 -0
- package/dist/{constants.d.ts → common/constants.d.ts} +0 -0
- package/dist/{constants.js → common/constants.js} +0 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/decoder.d.ts +48 -0
- package/dist/{decoder.js → common/decoder.js} +13 -18
- package/dist/common/decoder.js.map +1 -0
- package/dist/common/encoder.d.ts +74 -0
- package/dist/common/encoder.js +282 -0
- package/dist/common/encoder.js.map +1 -0
- package/dist/common/string-decoder.d.ts +13 -0
- package/dist/common/string-decoder.js +106 -0
- package/dist/common/string-decoder.js.map +1 -0
- package/dist/{string-encoder.d.ts → common/string-encoder.d.ts} +0 -0
- package/dist/{string-encoder.js → common/string-encoder.js} +0 -0
- package/dist/common/string-encoder.js.map +1 -0
- package/dist/encoder.d.ts +10 -2
- package/dist/encoder.js +2 -283
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/stream/encoder.d.ts +14 -0
- package/dist/stream/encoder.js +86 -0
- package/dist/stream/encoder.js.map +1 -0
- package/dist/stream/index.d.ts +4 -0
- package/dist/stream/index.js +7 -0
- package/dist/stream/index.js.map +1 -0
- package/jest.config.js +3 -0
- package/package.json +18 -8
- package/src/{constants.ts → common/constants.ts} +0 -0
- package/src/{decoder.ts → common/decoder.ts} +13 -18
- package/src/common/encoder.ts +305 -0
- package/src/common/string-decoder.ts +107 -0
- package/src/{string-encoder.ts → common/string-encoder.ts} +0 -0
- package/src/encoder.ts +4 -305
- package/src/index.ts +14 -2
- package/src/stream/encoder.ts +83 -0
- package/src/stream/index.ts +8 -0
- package/tests/decode.js +433 -0
- package/{test → tests}/e2e.js +19 -27
- package/tests/encode.js +354 -0
- package/tests/stream/encode.js +380 -0
- package/tests/string-encoding.js +71 -0
- package/tests/tsconfig.json +7 -0
- package/tsconfig.json +1 -2
- package/dist/constants.js.map +0 -1
- package/dist/decoder.d.ts +0 -2
- package/dist/decoder.js.map +0 -1
- package/dist/string-decoder.d.ts +0 -9
- package/dist/string-decoder.js +0 -67
- package/dist/string-decoder.js.map +0 -1
- package/dist/string-encoder.js.map +0 -1
- package/src/string-decoder.ts +0 -69
- package/test/decode.js +0 -491
- package/test/encode.js +0 -432
- package/test/string-encoding.js +0 -62
package/benchmark-string.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import { textDecoder,
|
|
2
|
-
|
|
3
|
-
function decode(data) {
|
|
4
|
-
if (data.every((b) => b < 128)) {
|
|
5
|
-
// 为 ASCII 字符串优化
|
|
6
|
-
return decodeAscii(data);
|
|
7
|
-
}
|
|
8
|
-
// 使用系统解码
|
|
9
|
-
return textDecoder.decode(data);
|
|
10
|
-
}
|
|
1
|
+
import { textDecoder, decode, decodeJs } from './dist/string-decoder.js';
|
|
11
2
|
|
|
12
3
|
const LOOP = 500000;
|
|
13
4
|
|
|
@@ -19,25 +10,32 @@ function createTestData(size) {
|
|
|
19
10
|
|
|
20
11
|
/* eslint-disable no-console */
|
|
21
12
|
(async function () {
|
|
22
|
-
|
|
13
|
+
console.log(` Size\tTextDecoder MyStringDecoder my/sys%\tJsDecoder js/sys%`);
|
|
14
|
+
for (let index = 50; index < 1000; index = Math.trunc(index * 1.2)) {
|
|
23
15
|
const data = createTestData(index);
|
|
24
16
|
|
|
25
|
-
let
|
|
26
|
-
let
|
|
17
|
+
let decodeTimeSystem = 0;
|
|
18
|
+
let decodeTimeOptimal = 0;
|
|
19
|
+
let decodeTimeJs = 0;
|
|
27
20
|
for (let i = 0; i < LOOP; i++) {
|
|
28
21
|
let start = performance.now();
|
|
29
22
|
textDecoder.decode(data);
|
|
30
|
-
|
|
23
|
+
decodeTimeSystem += performance.now() - start;
|
|
24
|
+
|
|
25
|
+
start = performance.now();
|
|
26
|
+
decode(data, 0, data.byteLength);
|
|
27
|
+
decodeTimeOptimal += performance.now() - start;
|
|
31
28
|
|
|
32
29
|
start = performance.now();
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
decodeJs(data, 0, data.byteLength);
|
|
31
|
+
decodeTimeJs += performance.now() - start;
|
|
35
32
|
}
|
|
36
|
-
const
|
|
33
|
+
const ratioOptimal = decodeTimeOptimal / decodeTimeSystem;
|
|
34
|
+
const ratioJs = decodeTimeJs / decodeTimeSystem;
|
|
37
35
|
console.log(
|
|
38
|
-
`Data: ${data.byteLength} \t${t(
|
|
39
|
-
|
|
40
|
-
)}%`,
|
|
36
|
+
`Data: ${data.byteLength} \t${t(decodeTimeSystem / LOOP)} ${t(decodeTimeOptimal / LOOP)} ${(
|
|
37
|
+
ratioOptimal * 100
|
|
38
|
+
).toFixed(3)}%\t${t(decodeTimeJs / LOOP)} ${(ratioJs * 100).toFixed(3)}%`,
|
|
41
39
|
);
|
|
42
40
|
}
|
|
43
41
|
})();
|
package/benchmark.js
CHANGED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/common/constants.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAE3B,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC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view;
|
|
5
|
+
/** 当前读指针位置 */
|
|
6
|
+
private offset;
|
|
7
|
+
constructor(data: Uint8Array);
|
|
8
|
+
/** 解码 */
|
|
9
|
+
decode(): unknown;
|
|
10
|
+
/** 检查是否有给定字节数的未读数据 */
|
|
11
|
+
private ensureData;
|
|
12
|
+
/** 读取数据 */
|
|
13
|
+
private read;
|
|
14
|
+
/** 读取 marker,跳过 noop */
|
|
15
|
+
private readMarker;
|
|
16
|
+
/** 读取数据 */
|
|
17
|
+
private readData;
|
|
18
|
+
/** 读取一个大于 0 的整数 */
|
|
19
|
+
private readIntLength;
|
|
20
|
+
/** 字符串解码 */
|
|
21
|
+
private readonly stringDecoder;
|
|
22
|
+
/** readStringData */
|
|
23
|
+
private readStringData;
|
|
24
|
+
/** readHighPrecisionNumberData */
|
|
25
|
+
private readHighPrecisionNumberData;
|
|
26
|
+
/** readStringData */
|
|
27
|
+
private readCharData;
|
|
28
|
+
/** readInt8Data */
|
|
29
|
+
private readInt8Data;
|
|
30
|
+
/** readUint8Data */
|
|
31
|
+
private readUint8Data;
|
|
32
|
+
/** readInt16Data */
|
|
33
|
+
private readInt16Data;
|
|
34
|
+
/** readInt32Data */
|
|
35
|
+
private readInt32Data;
|
|
36
|
+
/** readInt64Data */
|
|
37
|
+
private readInt64Data;
|
|
38
|
+
/** readFloat32Data */
|
|
39
|
+
private readFloat32Data;
|
|
40
|
+
/** readFloat64Data */
|
|
41
|
+
private readFloat64Data;
|
|
42
|
+
/** 读取 Optimized Format 数据 */
|
|
43
|
+
private readOptimizedFormatMarkers;
|
|
44
|
+
/** 读取数组 */
|
|
45
|
+
private readArray;
|
|
46
|
+
/** 读对象 */
|
|
47
|
+
private readObject;
|
|
48
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as constants from './constants.js';
|
|
2
2
|
import { StringDecoder } from './string-decoder.js';
|
|
3
3
|
/** decoder */
|
|
4
|
-
class Decoder {
|
|
4
|
+
export class Decoder {
|
|
5
5
|
constructor(data) {
|
|
6
6
|
this.data = data;
|
|
7
7
|
/** 当前读指针位置 */
|
|
@@ -90,21 +90,21 @@ class Decoder {
|
|
|
90
90
|
case constants.INT64:
|
|
91
91
|
length = this.readInt64Data();
|
|
92
92
|
break;
|
|
93
|
+
default:
|
|
94
|
+
throw new Error(`Unexpected marker '${String.fromCharCode(marker)}'(${marker}) for int length`);
|
|
93
95
|
}
|
|
94
|
-
if (length
|
|
95
|
-
throw new Error(`Unexpected marker '${String.fromCharCode(marker)}'(${marker}) for int length`);
|
|
96
|
-
}
|
|
97
|
-
if (length < 0)
|
|
96
|
+
if (length < 0) {
|
|
98
97
|
throw new Error('Invalid length');
|
|
98
|
+
}
|
|
99
99
|
return length;
|
|
100
100
|
}
|
|
101
101
|
/** readStringData */
|
|
102
102
|
readStringData() {
|
|
103
103
|
const length = this.readIntLength();
|
|
104
104
|
this.ensureData(length);
|
|
105
|
-
const
|
|
105
|
+
const begin = this.offset;
|
|
106
106
|
this.offset += length;
|
|
107
|
-
return this.stringDecoder.decode(
|
|
107
|
+
return this.stringDecoder.decode(this.data, begin, this.offset);
|
|
108
108
|
}
|
|
109
109
|
/** readHighPrecisionNumberData */
|
|
110
110
|
readHighPrecisionNumberData() {
|
|
@@ -239,7 +239,8 @@ class Decoder {
|
|
|
239
239
|
this.ensureData(count * 8);
|
|
240
240
|
return BigInt64Array.from({ length: count }, () => this.readInt64Data());
|
|
241
241
|
}
|
|
242
|
-
const array =
|
|
242
|
+
const array = [];
|
|
243
|
+
array.length = count;
|
|
243
244
|
for (let i = 0; i < count; i++) {
|
|
244
245
|
array[i] = type == null ? this.read() : this.readData(type);
|
|
245
246
|
}
|
|
@@ -253,23 +254,17 @@ class Decoder {
|
|
|
253
254
|
const object = [];
|
|
254
255
|
while (this.readMarker() !== constants.OBJECT_END) {
|
|
255
256
|
this.offset--;
|
|
256
|
-
|
|
257
|
-
object.push([key, this.read()]);
|
|
257
|
+
object.push([this.readStringData(), this.read()]);
|
|
258
258
|
}
|
|
259
259
|
return Object.fromEntries(object);
|
|
260
260
|
}
|
|
261
261
|
const { count, type } = markers;
|
|
262
|
-
const object =
|
|
262
|
+
const object = [];
|
|
263
|
+
object.length = count;
|
|
263
264
|
for (let i = 0; i < count; i++) {
|
|
264
|
-
|
|
265
|
-
object[i] = [key, type == null ? this.read() : this.readData(type)];
|
|
265
|
+
object[i] = [this.readStringData(), type == null ? this.read() : this.readData(type)];
|
|
266
266
|
}
|
|
267
267
|
return Object.fromEntries(object);
|
|
268
268
|
}
|
|
269
269
|
}
|
|
270
|
-
/** 解码 */
|
|
271
|
-
export function decode(value) {
|
|
272
|
-
const decoder = new Decoder(value);
|
|
273
|
-
return decoder.decode();
|
|
274
|
-
}
|
|
275
270
|
//# sourceMappingURL=decoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decoder.js","sourceRoot":"","sources":["../../src/common/decoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,cAAc;AACd,MAAM,OAAO,OAAO;IAIhB,YAAqB,IAAgB;QAAhB,SAAI,GAAJ,IAAI,CAAY;QAFrC,cAAc;QACN,WAAM,GAAG,CAAC,CAAC;QAiGnB,YAAY;QACK,kBAAa,GAAG,IAAI,aAAa,EAAE,CAAC;QAhGjD,IAAI,CAAC,IAAI,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAC5E,CAAC;IACD,SAAS;IACT,MAAM;QACF,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;IACvB,CAAC;IAED,sBAAsB;IACd,UAAU,CAAC,UAAkB;QACjC,IAAI,IAAI,CAAC,MAAM,GAAG,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACjD,MAAM,IAAI,KAAK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EAAE,KAAK,SAAS,CAAC,UAAU,EAAE;gBAC/C,IAAI,CAAC,MAAM,EAAE,CAAC;gBACd,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;aACrD;YACD,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;SACrC;QAED,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;QAChC,MAAM,MAAM,GAA6B,EAAE,CAAC;QAC5C,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC;QACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE;YAC5B,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;SACzF;QACD,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;CACJ"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { StringEncoder } from './string-encoder.js';
|
|
2
|
+
/** 编码至 ubjson */
|
|
3
|
+
export declare abstract class EncoderBase {
|
|
4
|
+
readonly value: unknown;
|
|
5
|
+
/** 字符串编码 */
|
|
6
|
+
protected stringEncoder: StringEncoder;
|
|
7
|
+
/** 当前写指针位置 */
|
|
8
|
+
protected length: number;
|
|
9
|
+
/** 数据 */
|
|
10
|
+
protected buffer: Uint8Array;
|
|
11
|
+
/** buffer 的 DataView */
|
|
12
|
+
protected view: DataView;
|
|
13
|
+
constructor(value: unknown);
|
|
14
|
+
/**
|
|
15
|
+
* 确保 buffer 还有 capacity 的空闲空间
|
|
16
|
+
*/
|
|
17
|
+
protected abstract ensureCapacity(capacity: number): void;
|
|
18
|
+
/** 写入一个对象 */
|
|
19
|
+
protected write(value: unknown): void;
|
|
20
|
+
/** 写入 marker */
|
|
21
|
+
protected writeMarker(marker: number): void;
|
|
22
|
+
/** 写入 marker */
|
|
23
|
+
protected writeNull(): void;
|
|
24
|
+
/** writeNoOp */
|
|
25
|
+
protected writeNoOp(): void;
|
|
26
|
+
/** writeBoolean */
|
|
27
|
+
protected writeBoolean(value: boolean): void;
|
|
28
|
+
/** writeInt8 */
|
|
29
|
+
protected writeInt8(value: number): void;
|
|
30
|
+
/** writeInt8Data */
|
|
31
|
+
protected writeInt8Data(value: number): void;
|
|
32
|
+
/** writeUint8 */
|
|
33
|
+
protected writeUint8(value: number): void;
|
|
34
|
+
/** writeUint8Data */
|
|
35
|
+
protected writeUint8Data(value: number): void;
|
|
36
|
+
/** writeInt16 */
|
|
37
|
+
protected writeInt16(value: number): void;
|
|
38
|
+
/** writeInt16Data */
|
|
39
|
+
protected writeInt16Data(value: number): void;
|
|
40
|
+
/** writeInt32 */
|
|
41
|
+
protected writeInt32(value: number): void;
|
|
42
|
+
/** writeInt32Data */
|
|
43
|
+
protected writeInt32Data(value: number): void;
|
|
44
|
+
/** writeFloat32 */
|
|
45
|
+
protected writeFloat32(value: number): void;
|
|
46
|
+
/** writeFloat32Data */
|
|
47
|
+
protected writeFloat32Data(value: number): void;
|
|
48
|
+
/** writeFloat64 */
|
|
49
|
+
protected writeFloat64(value: number): void;
|
|
50
|
+
/** writeFloat64Data */
|
|
51
|
+
protected writeFloat64Data(value: number): void;
|
|
52
|
+
/** writeChar */
|
|
53
|
+
protected writeChar(value: string): void;
|
|
54
|
+
/** writeCharData */
|
|
55
|
+
protected writeCharData(value: string): void;
|
|
56
|
+
/** writeString */
|
|
57
|
+
protected writeString(value: string): void;
|
|
58
|
+
/** writeStringData */
|
|
59
|
+
protected writeStringData(value: string): void;
|
|
60
|
+
/**
|
|
61
|
+
* 写入整形数字,选取合适的大小
|
|
62
|
+
*
|
|
63
|
+
* @throws 无法在 int32 范围内表示
|
|
64
|
+
*/
|
|
65
|
+
protected writeInt(value: number): (this: this, value: number) => void;
|
|
66
|
+
/** 写入数字,选取合适的大小 */
|
|
67
|
+
protected writeNumber(value: number): void;
|
|
68
|
+
/** writeObject */
|
|
69
|
+
protected writeObject(value: Record<string, unknown>): void;
|
|
70
|
+
/** writeArray */
|
|
71
|
+
protected writeArray(value: unknown[]): void;
|
|
72
|
+
/** writeArray */
|
|
73
|
+
protected writeTypedArray(value: ArrayBufferView): void;
|
|
74
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/unbound-method */
|
|
2
|
+
import * as constants from './constants.js';
|
|
3
|
+
import { StringEncoder } from './string-encoder.js';
|
|
4
|
+
/** 编码至 ubjson */
|
|
5
|
+
export class EncoderBase {
|
|
6
|
+
constructor(value) {
|
|
7
|
+
this.value = value;
|
|
8
|
+
/** 字符串编码 */
|
|
9
|
+
this.stringEncoder = new StringEncoder();
|
|
10
|
+
/** 当前写指针位置 */
|
|
11
|
+
this.length = 0;
|
|
12
|
+
}
|
|
13
|
+
/** 写入一个对象 */
|
|
14
|
+
write(value) {
|
|
15
|
+
switch (typeof value) {
|
|
16
|
+
case 'undefined':
|
|
17
|
+
return this.writeNoOp();
|
|
18
|
+
case 'boolean':
|
|
19
|
+
return this.writeBoolean(value);
|
|
20
|
+
case 'number':
|
|
21
|
+
return this.writeNumber(value);
|
|
22
|
+
case 'string':
|
|
23
|
+
if (value.length === 1 && value.charCodeAt(0) < 255) {
|
|
24
|
+
// 1 byte string
|
|
25
|
+
return this.writeChar(value);
|
|
26
|
+
}
|
|
27
|
+
return this.writeString(value);
|
|
28
|
+
case 'object': {
|
|
29
|
+
if (value === null) {
|
|
30
|
+
return this.writeNull();
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
return this.writeArray(value);
|
|
34
|
+
}
|
|
35
|
+
if (ArrayBuffer.isView(value)) {
|
|
36
|
+
return this.writeTypedArray(value);
|
|
37
|
+
}
|
|
38
|
+
return this.writeObject(value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Unsupported type ${Object.prototype.toString.call(value)}`);
|
|
42
|
+
}
|
|
43
|
+
/** 写入 marker */
|
|
44
|
+
writeMarker(marker) {
|
|
45
|
+
this.ensureCapacity(1);
|
|
46
|
+
this.view.setUint8(this.length, marker);
|
|
47
|
+
this.length += 1;
|
|
48
|
+
}
|
|
49
|
+
/** 写入 marker */
|
|
50
|
+
writeNull() {
|
|
51
|
+
this.writeMarker(constants.NULL);
|
|
52
|
+
}
|
|
53
|
+
/** writeNoOp */
|
|
54
|
+
writeNoOp() {
|
|
55
|
+
this.writeMarker(constants.NO_OP);
|
|
56
|
+
}
|
|
57
|
+
/** writeBoolean */
|
|
58
|
+
writeBoolean(value) {
|
|
59
|
+
this.writeMarker(value ? constants.TRUE : constants.FALSE);
|
|
60
|
+
}
|
|
61
|
+
/** writeInt8 */
|
|
62
|
+
writeInt8(value) {
|
|
63
|
+
this.writeMarker(constants.INT8);
|
|
64
|
+
this.writeInt8Data(value);
|
|
65
|
+
}
|
|
66
|
+
/** writeInt8Data */
|
|
67
|
+
writeInt8Data(value) {
|
|
68
|
+
this.ensureCapacity(1);
|
|
69
|
+
this.view.setInt8(this.length, value);
|
|
70
|
+
this.length += 1;
|
|
71
|
+
}
|
|
72
|
+
/** writeUint8 */
|
|
73
|
+
writeUint8(value) {
|
|
74
|
+
this.writeMarker(constants.UINT8);
|
|
75
|
+
this.writeUint8Data(value);
|
|
76
|
+
}
|
|
77
|
+
/** writeUint8Data */
|
|
78
|
+
writeUint8Data(value) {
|
|
79
|
+
this.ensureCapacity(1);
|
|
80
|
+
this.view.setUint8(this.length, value);
|
|
81
|
+
this.length += 1;
|
|
82
|
+
}
|
|
83
|
+
/** writeInt16 */
|
|
84
|
+
writeInt16(value) {
|
|
85
|
+
this.writeMarker(constants.INT16);
|
|
86
|
+
this.writeInt16Data(value);
|
|
87
|
+
}
|
|
88
|
+
/** writeInt16Data */
|
|
89
|
+
writeInt16Data(value) {
|
|
90
|
+
this.ensureCapacity(2);
|
|
91
|
+
this.view.setInt16(this.length, value);
|
|
92
|
+
this.length += 2;
|
|
93
|
+
}
|
|
94
|
+
/** writeInt32 */
|
|
95
|
+
writeInt32(value) {
|
|
96
|
+
this.writeMarker(constants.INT32);
|
|
97
|
+
this.writeInt32Data(value);
|
|
98
|
+
}
|
|
99
|
+
/** writeInt32Data */
|
|
100
|
+
writeInt32Data(value) {
|
|
101
|
+
this.ensureCapacity(4);
|
|
102
|
+
this.view.setInt32(this.length, value);
|
|
103
|
+
this.length += 4;
|
|
104
|
+
}
|
|
105
|
+
/** writeFloat32 */
|
|
106
|
+
writeFloat32(value) {
|
|
107
|
+
this.writeMarker(constants.FLOAT32);
|
|
108
|
+
this.writeFloat32Data(value);
|
|
109
|
+
}
|
|
110
|
+
/** writeFloat32Data */
|
|
111
|
+
writeFloat32Data(value) {
|
|
112
|
+
this.ensureCapacity(4);
|
|
113
|
+
this.view.setFloat32(this.length, value);
|
|
114
|
+
this.length += 4;
|
|
115
|
+
}
|
|
116
|
+
/** writeFloat64 */
|
|
117
|
+
writeFloat64(value) {
|
|
118
|
+
this.writeMarker(constants.FLOAT64);
|
|
119
|
+
this.writeFloat64Data(value);
|
|
120
|
+
}
|
|
121
|
+
/** writeFloat64Data */
|
|
122
|
+
writeFloat64Data(value) {
|
|
123
|
+
this.ensureCapacity(8);
|
|
124
|
+
this.view.setFloat64(this.length, value);
|
|
125
|
+
this.length += 8;
|
|
126
|
+
}
|
|
127
|
+
/** writeChar */
|
|
128
|
+
writeChar(value) {
|
|
129
|
+
this.writeMarker(constants.CHAR);
|
|
130
|
+
this.writeCharData(value);
|
|
131
|
+
}
|
|
132
|
+
/** writeCharData */
|
|
133
|
+
writeCharData(value) {
|
|
134
|
+
this.ensureCapacity(1);
|
|
135
|
+
this.view.setUint8(this.length, value.charCodeAt(0));
|
|
136
|
+
this.length += 1;
|
|
137
|
+
}
|
|
138
|
+
/** writeString */
|
|
139
|
+
writeString(value) {
|
|
140
|
+
this.writeMarker(constants.STRING);
|
|
141
|
+
this.writeStringData(value);
|
|
142
|
+
}
|
|
143
|
+
/** writeStringData */
|
|
144
|
+
writeStringData(value) {
|
|
145
|
+
// 优化小字符串
|
|
146
|
+
if (value.length < 32) {
|
|
147
|
+
const buf = this.stringEncoder.encode(value);
|
|
148
|
+
this.writeInt(buf.length);
|
|
149
|
+
this.ensureCapacity(buf.length);
|
|
150
|
+
this.buffer.set(buf, this.length);
|
|
151
|
+
this.length += buf.length;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (this.stringEncoder.encodeInto != null) {
|
|
155
|
+
const maxUsage = value.length * 3;
|
|
156
|
+
const currentPos = this.length;
|
|
157
|
+
// 一次性分配 writeInt 和 encodeInto 的空间,避免无法回溯
|
|
158
|
+
this.ensureCapacity(maxUsage + 4);
|
|
159
|
+
// 先写入最大大小
|
|
160
|
+
const lengthWriter = this.writeInt(maxUsage);
|
|
161
|
+
// 写入文本数据
|
|
162
|
+
const { written } = this.stringEncoder.encodeInto(value, this.buffer.subarray(this.length));
|
|
163
|
+
// 回溯,写入实际大小
|
|
164
|
+
this.length = currentPos;
|
|
165
|
+
lengthWriter.call(this, written);
|
|
166
|
+
// 移动指针到写入末尾
|
|
167
|
+
this.length += written;
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const data = this.stringEncoder.encode(value);
|
|
171
|
+
this.writeInt(data.length);
|
|
172
|
+
this.ensureCapacity(data.length);
|
|
173
|
+
this.buffer.set(data, this.length);
|
|
174
|
+
this.length += data.length;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 写入整形数字,选取合适的大小
|
|
178
|
+
*
|
|
179
|
+
* @throws 无法在 int32 范围内表示
|
|
180
|
+
*/
|
|
181
|
+
writeInt(value) {
|
|
182
|
+
value = value | 0;
|
|
183
|
+
if (value >= -128 && value <= 127) {
|
|
184
|
+
this.writeInt8(value);
|
|
185
|
+
return this.writeInt8;
|
|
186
|
+
}
|
|
187
|
+
if (value >= -32768 && value <= 32767) {
|
|
188
|
+
this.writeInt16(value);
|
|
189
|
+
return this.writeInt16;
|
|
190
|
+
}
|
|
191
|
+
if (value >= -2147483648 && value <= 2147483647) {
|
|
192
|
+
this.writeInt32(value);
|
|
193
|
+
return this.writeInt32;
|
|
194
|
+
}
|
|
195
|
+
/* c8 ignore next 2 */
|
|
196
|
+
throw new Error(`Unsupported int value ${value}: out of range`);
|
|
197
|
+
}
|
|
198
|
+
/** 写入数字,选取合适的大小 */
|
|
199
|
+
writeNumber(value) {
|
|
200
|
+
if (Number.isInteger(value) && value >= -2147483648 && value <= 2147483647) {
|
|
201
|
+
if (value >= 0 && value <= 255)
|
|
202
|
+
return this.writeUint8(value);
|
|
203
|
+
this.writeInt(value);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!(Number.isNaN(value) || Math.fround(value) === value)) {
|
|
207
|
+
return this.writeFloat64(value);
|
|
208
|
+
}
|
|
209
|
+
// 如果不会损失精度,则使用 32 位浮点
|
|
210
|
+
return this.writeFloat32(value);
|
|
211
|
+
}
|
|
212
|
+
/** writeObject */
|
|
213
|
+
writeObject(value) {
|
|
214
|
+
this.writeMarker(constants.OBJECT);
|
|
215
|
+
// 生成稳定的结果以便 hash 计算
|
|
216
|
+
for (const key of Object.keys(value).sort()) {
|
|
217
|
+
const element = value[key];
|
|
218
|
+
if (element === undefined)
|
|
219
|
+
continue;
|
|
220
|
+
this.writeStringData(key);
|
|
221
|
+
this.write(element);
|
|
222
|
+
}
|
|
223
|
+
this.writeMarker(constants.OBJECT_END);
|
|
224
|
+
}
|
|
225
|
+
/** writeArray */
|
|
226
|
+
writeArray(value) {
|
|
227
|
+
this.writeMarker(constants.ARRAY);
|
|
228
|
+
for (const v of value) {
|
|
229
|
+
// 在数组中 undefined 也被视作 null 进行序列化
|
|
230
|
+
if (v == null)
|
|
231
|
+
this.writeNull();
|
|
232
|
+
else
|
|
233
|
+
this.write(v);
|
|
234
|
+
}
|
|
235
|
+
this.writeMarker(constants.ARRAY_END);
|
|
236
|
+
}
|
|
237
|
+
/** writeArray */
|
|
238
|
+
writeTypedArray(value) {
|
|
239
|
+
this.writeMarker(constants.ARRAY);
|
|
240
|
+
this.writeMarker(constants.TYPE_MARKER);
|
|
241
|
+
if (value instanceof Uint8Array || value instanceof Int8Array) {
|
|
242
|
+
// fast path for typed arrays with `BYTES_PER_ELEMENT` of 1
|
|
243
|
+
this.writeMarker(value instanceof Uint8Array ? constants.UINT8 : constants.INT8);
|
|
244
|
+
this.writeMarker(constants.COUNT_MARKER);
|
|
245
|
+
this.writeInt(value.length);
|
|
246
|
+
this.ensureCapacity(value.byteLength);
|
|
247
|
+
this.buffer.set(value, this.length);
|
|
248
|
+
this.length += value.byteLength;
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
/** 用于写入的 setter */
|
|
252
|
+
let setValue;
|
|
253
|
+
if (value instanceof Int16Array) {
|
|
254
|
+
this.writeMarker(constants.INT16);
|
|
255
|
+
setValue = this.view.setInt16;
|
|
256
|
+
}
|
|
257
|
+
else if (value instanceof Int32Array) {
|
|
258
|
+
this.writeMarker(constants.INT32);
|
|
259
|
+
setValue = this.view.setInt32;
|
|
260
|
+
}
|
|
261
|
+
else if (value instanceof Float32Array) {
|
|
262
|
+
this.writeMarker(constants.FLOAT32);
|
|
263
|
+
setValue = this.view.setFloat32;
|
|
264
|
+
}
|
|
265
|
+
else if (value instanceof Float64Array) {
|
|
266
|
+
this.writeMarker(constants.FLOAT64);
|
|
267
|
+
setValue = this.view.setFloat64;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
throw new Error(`Unsupported typed array type ${Object.prototype.toString.call(value)}`);
|
|
271
|
+
}
|
|
272
|
+
this.writeMarker(constants.COUNT_MARKER);
|
|
273
|
+
this.writeInt(value.length);
|
|
274
|
+
this.ensureCapacity(value.byteLength);
|
|
275
|
+
// 不要在前面 bind,this.ensureCapacity 有可能导致 this.view 指向新的对象
|
|
276
|
+
for (const v of value) {
|
|
277
|
+
setValue.call(this.view, this.length, v);
|
|
278
|
+
this.length += value.BYTES_PER_ELEMENT;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
//# sourceMappingURL=encoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoder.js","sourceRoot":"","sources":["../../src/common/encoder.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,iBAAiB;AACjB,MAAM,OAAgB,WAAW;IAU7B,YAAqB,KAAc;QAAd,UAAK,GAAL,KAAK,CAAS;QATnC,YAAY;QACF,kBAAa,GAAG,IAAI,aAAa,EAAE,CAAC;QAC9C,cAAc;QACJ,WAAM,GAAG,CAAC,CAAC;IAMiB,CAAC;IAKvC,aAAa;IACH,KAAK,CAAC,KAAc;QAC1B,QAAQ,OAAO,KAAK,EAAE;YAClB,KAAK,WAAW;gBACZ,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC;YAC5B,KAAK,SAAS;gBACV,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YACpC,KAAK,QAAQ;gBACT,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;YACnC,KAAK,QAAQ;gBACT,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,GAAG,EAAE;oBACjD,gBAAgB;oBAChB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;iBAChC;gBACD,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;YACnC,KAAK,QAAQ,CAAC,CAAC;g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IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACpC,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;SACnC;aAAM,IAAI,KAAK,YAAY,YAAY,EAAE;YACtC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACpC,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;SACnC;aAAM;YACH,MAAM,IAAI,KAAK,CAAC,gCAAgC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;SAC5F;QACD,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;QACzC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC5B,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACtC,wDAAwD;QACxD,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;YACnB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YACzC,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC;SAC1C;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const textDecoder: TextDecoder | null;
|
|
2
|
+
export declare const TEXT_ENCODER_THRESHOLD: number;
|
|
3
|
+
/** 解码 */
|
|
4
|
+
export declare function decodeJs(bytes: Uint8Array, begin: number, end: number): string;
|
|
5
|
+
/** 解码 */
|
|
6
|
+
export declare function decode(data: Uint8Array, begin: number, end: number): string;
|
|
7
|
+
/** 特别优化字符串解码速度 */
|
|
8
|
+
export declare class StringDecoder {
|
|
9
|
+
/** 小字符串缓存 */
|
|
10
|
+
private readonly cache;
|
|
11
|
+
/** 字符串解码 */
|
|
12
|
+
decode(data: Uint8Array, begin: number, end: number): string;
|
|
13
|
+
}
|