@cloudpss/ubjson 0.4.32 → 0.4.34
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/constants.d.ts +22 -20
- package/dist/common/constants.js +1 -21
- package/dist/common/constants.js.map +1 -1
- package/dist/common/decoder.d.ts +4 -20
- package/dist/common/decoder.js +217 -178
- package/dist/common/decoder.js.map +1 -1
- package/dist/common/encoder.d.ts +8 -54
- package/dist/common/encoder.js +241 -259
- package/dist/common/encoder.js.map +1 -1
- package/dist/common/string-decoder.d.ts +3 -8
- package/dist/common/string-decoder.js +190 -46
- package/dist/common/string-decoder.js.map +1 -1
- package/dist/common/string-encoder.d.ts +2 -8
- package/dist/common/string-encoder.js +8 -44
- package/dist/common/string-encoder.js.map +1 -1
- package/dist/encoder.js +2 -21
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/rxjs/decoder.js +2 -2
- package/dist/rxjs/decoder.js.map +1 -1
- package/dist/rxjs/index.d.ts +1 -0
- package/dist/rxjs/index.js +1 -0
- package/dist/rxjs/index.js.map +1 -1
- package/dist/stream/index.d.ts +1 -0
- package/dist/stream/index.js +2 -1
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +1 -1
- package/dist/stream-helper/decoder.js +1 -1
- package/dist/stream-helper/decoder.js.map +1 -1
- package/dist/stream-helper/encoder.js +2 -2
- package/dist/stream-helper/encoder.js.map +1 -1
- package/dist/utils.d.ts +5 -1
- package/dist/utils.js +13 -4
- package/dist/utils.js.map +1 -1
- package/package.json +3 -2
- package/src/common/constants.ts +22 -21
- package/src/common/decoder.ts +197 -162
- package/src/common/encoder.ts +230 -277
- package/src/common/string-decoder.ts +173 -41
- package/src/common/string-encoder.ts +10 -41
- package/src/encoder.ts +2 -16
- package/src/index.ts +2 -5
- package/src/rxjs/decoder.ts +2 -2
- package/src/rxjs/index.ts +1 -0
- package/src/stream/index.ts +2 -0
- package/src/stream-helper/decoder.ts +1 -1
- package/src/stream-helper/encoder.ts +2 -2
- package/src/utils.ts +14 -4
- package/tests/decode.js +69 -5
- package/tests/e2e.js +12 -1
- package/tests/encode.js +52 -23
- package/tests/rxjs/decode.js +3 -2
- package/tests/rxjs/encode.js +5 -8
- package/tests/stream/decode.js +2 -1
- package/tests/stream/encode.js +4 -5
- package/tests/string-encoding.js +77 -25
- package/tsconfig.json +3 -1
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
/** Constants of markers */
|
|
2
|
-
export declare const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
2
|
+
export declare const enum constants {
|
|
3
|
+
NULL = 90,
|
|
4
|
+
NO_OP = 78,
|
|
5
|
+
TRUE = 84,
|
|
6
|
+
FALSE = 70,
|
|
7
|
+
INT8 = 105,
|
|
8
|
+
UINT8 = 85,
|
|
9
|
+
INT16 = 73,
|
|
10
|
+
INT32 = 108,
|
|
11
|
+
INT64 = 76,
|
|
12
|
+
FLOAT32 = 100,
|
|
13
|
+
FLOAT64 = 68,
|
|
14
|
+
HIGH_PRECISION_NUMBER = 72,
|
|
15
|
+
CHAR = 67,
|
|
16
|
+
STRING = 83,
|
|
17
|
+
ARRAY = 91,
|
|
18
|
+
ARRAY_END = 93,
|
|
19
|
+
OBJECT = 123,
|
|
20
|
+
OBJECT_END = 125,
|
|
21
|
+
TYPE_MARKER = 36,
|
|
22
|
+
COUNT_MARKER = 35
|
|
23
|
+
}
|
package/dist/common/constants.js
CHANGED
|
@@ -1,22 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export const NULL = 'Z'.charCodeAt(0);
|
|
3
|
-
export const NO_OP = 'N'.charCodeAt(0);
|
|
4
|
-
export const TRUE = 'T'.charCodeAt(0);
|
|
5
|
-
export const FALSE = 'F'.charCodeAt(0);
|
|
6
|
-
export const INT8 = 'i'.charCodeAt(0);
|
|
7
|
-
export const UINT8 = 'U'.charCodeAt(0);
|
|
8
|
-
export const INT16 = 'I'.charCodeAt(0);
|
|
9
|
-
export const INT32 = 'l'.charCodeAt(0);
|
|
10
|
-
export const INT64 = 'L'.charCodeAt(0);
|
|
11
|
-
export const FLOAT32 = 'd'.charCodeAt(0);
|
|
12
|
-
export const FLOAT64 = 'D'.charCodeAt(0);
|
|
13
|
-
export const HIGH_PRECISION_NUMBER = 'H'.charCodeAt(0);
|
|
14
|
-
export const CHAR = 'C'.charCodeAt(0);
|
|
15
|
-
export const STRING = 'S'.charCodeAt(0);
|
|
16
|
-
export const ARRAY = '['.charCodeAt(0);
|
|
17
|
-
export const ARRAY_END = ']'.charCodeAt(0);
|
|
18
|
-
export const OBJECT = '{'.charCodeAt(0);
|
|
19
|
-
export const OBJECT_END = '}'.charCodeAt(0);
|
|
20
|
-
export const TYPE_MARKER = '$'.charCodeAt(0);
|
|
21
|
-
export const COUNT_MARKER = '#'.charCodeAt(0);
|
|
1
|
+
export {};
|
|
22
2
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/common/constants.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/common/constants.ts"],"names":[],"mappings":""}
|
package/dist/common/decoder.d.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
/** 未结束的流 */
|
|
2
|
-
export declare class UnexpectedEof extends Error {
|
|
3
|
-
constructor();
|
|
4
|
-
}
|
|
5
1
|
/** decoder */
|
|
6
2
|
export declare class Decoder {
|
|
7
|
-
readonly data: Uint8Array;
|
|
8
3
|
protected readonly view: DataView;
|
|
4
|
+
protected readonly data: Uint8Array;
|
|
9
5
|
/** 当前读指针位置 */
|
|
10
6
|
protected offset: number;
|
|
11
|
-
constructor(data:
|
|
7
|
+
constructor(data: BinaryData);
|
|
12
8
|
/** 解码 */
|
|
13
9
|
decode(): unknown;
|
|
14
|
-
/** 检查是否有给定字节数的未读数据 */
|
|
15
|
-
protected ensureData(byteLength: number): void;
|
|
16
10
|
/** 读取数据 */
|
|
17
11
|
private read;
|
|
18
12
|
/** 读取 marker,跳过 noop */
|
|
@@ -21,14 +15,8 @@ export declare class Decoder {
|
|
|
21
15
|
private readData;
|
|
22
16
|
/** 读取一个大于 0 的整数 */
|
|
23
17
|
private readIntLength;
|
|
24
|
-
/**
|
|
25
|
-
private
|
|
26
|
-
/** readStringData */
|
|
27
|
-
private readStringData;
|
|
28
|
-
/** readHighPrecisionNumberData */
|
|
29
|
-
private readHighPrecisionNumberData;
|
|
30
|
-
/** readStringData */
|
|
31
|
-
private readCharData;
|
|
18
|
+
/** readKey */
|
|
19
|
+
private readKey;
|
|
32
20
|
/** readInt8Data */
|
|
33
21
|
private readInt8Data;
|
|
34
22
|
/** readUint8Data */
|
|
@@ -45,8 +33,4 @@ export declare class Decoder {
|
|
|
45
33
|
private readFloat64Data;
|
|
46
34
|
/** 读取 Optimized Format 数据 */
|
|
47
35
|
private readOptimizedFormatMarkers;
|
|
48
|
-
/** 读取数组 */
|
|
49
|
-
private readArray;
|
|
50
|
-
/** 读对象 */
|
|
51
|
-
private readObject;
|
|
52
36
|
}
|
package/dist/common/decoder.js
CHANGED
|
@@ -1,102 +1,211 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export class UnexpectedEof extends Error {
|
|
5
|
-
constructor() {
|
|
6
|
-
super('Unexpected EOF');
|
|
7
|
-
}
|
|
8
|
-
}
|
|
1
|
+
import { UnexpectedEof, toUint8Array } from '../utils.js';
|
|
2
|
+
import { decode, decodeKey } from './string-decoder.js';
|
|
3
|
+
const fromEntries = Object.fromEntries;
|
|
9
4
|
/** decoder */
|
|
10
5
|
export class Decoder {
|
|
11
6
|
constructor(data) {
|
|
12
|
-
this.data = data;
|
|
13
7
|
/** 当前读指针位置 */
|
|
14
8
|
this.offset = 0;
|
|
15
|
-
|
|
16
|
-
this.
|
|
17
|
-
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
9
|
+
this.data = toUint8Array(data, true);
|
|
10
|
+
this.view = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
|
|
18
11
|
}
|
|
19
12
|
/** 解码 */
|
|
20
13
|
decode() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (this.offset + byteLength > this.data.byteLength) {
|
|
26
|
-
throw new UnexpectedEof();
|
|
27
|
-
}
|
|
14
|
+
const marker = this.readMarker();
|
|
15
|
+
if (marker === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
return this.readData(marker);
|
|
28
18
|
}
|
|
29
19
|
/** 读取数据 */
|
|
30
20
|
read() {
|
|
31
|
-
|
|
21
|
+
const marker = this.readMarker();
|
|
22
|
+
if (marker === undefined) {
|
|
23
|
+
throw new UnexpectedEof();
|
|
24
|
+
}
|
|
25
|
+
return this.readData(marker);
|
|
32
26
|
}
|
|
33
27
|
/** 读取 marker,跳过 noop */
|
|
34
28
|
readMarker() {
|
|
35
|
-
let marker = constants.NO_OP
|
|
29
|
+
let marker = 78 /* constants.NO_OP */;
|
|
36
30
|
do {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
marker = this.view.getUint8(this.offset++);
|
|
40
|
-
} while (marker === constants.NO_OP);
|
|
31
|
+
marker = this.data[this.offset++];
|
|
32
|
+
} while (marker === 78 /* constants.NO_OP */);
|
|
41
33
|
return marker;
|
|
42
34
|
}
|
|
43
35
|
/** 读取数据 */
|
|
44
36
|
readData(marker) {
|
|
45
37
|
// 按照出现频率排序
|
|
46
38
|
switch (marker) {
|
|
47
|
-
case constants.STRING
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
case constants.
|
|
39
|
+
case 83 /* constants.STRING */: {
|
|
40
|
+
const length = this.readIntLength();
|
|
41
|
+
const begin = this.offset;
|
|
42
|
+
this.offset += length;
|
|
43
|
+
return decode(this.data, begin, this.offset);
|
|
44
|
+
}
|
|
45
|
+
case 123 /* constants.OBJECT */: {
|
|
46
|
+
const markers = this.readOptimizedFormatMarkers();
|
|
47
|
+
if (markers == null) {
|
|
48
|
+
// 直到 '}'
|
|
49
|
+
const object = [];
|
|
50
|
+
while (this.readMarker() !== 125 /* constants.OBJECT_END */) {
|
|
51
|
+
this.offset--;
|
|
52
|
+
object.push([this.readKey(), this.read()]);
|
|
53
|
+
}
|
|
54
|
+
return fromEntries(object);
|
|
55
|
+
}
|
|
56
|
+
const { count, type } = markers;
|
|
57
|
+
const object = [];
|
|
58
|
+
object.length = count;
|
|
59
|
+
for (let i = 0; i < count; i++) {
|
|
60
|
+
const key = this.readKey();
|
|
61
|
+
const t = type ?? this.readMarker();
|
|
62
|
+
if (t === undefined)
|
|
63
|
+
throw new UnexpectedEof();
|
|
64
|
+
const value = this.readData(t);
|
|
65
|
+
object[i] = [key, value];
|
|
66
|
+
}
|
|
67
|
+
return fromEntries(object);
|
|
68
|
+
}
|
|
69
|
+
case 91 /* constants.ARRAY */: {
|
|
70
|
+
const markers = this.readOptimizedFormatMarkers();
|
|
71
|
+
if (markers == null) {
|
|
72
|
+
const array = [];
|
|
73
|
+
// 直到 ']'
|
|
74
|
+
while (this.readMarker() !== 93 /* constants.ARRAY_END */) {
|
|
75
|
+
this.offset--;
|
|
76
|
+
array.push(this.read());
|
|
77
|
+
}
|
|
78
|
+
return array;
|
|
79
|
+
}
|
|
80
|
+
const { count, type } = markers;
|
|
81
|
+
switch (type) {
|
|
82
|
+
case 85 /* constants.UINT8 */:
|
|
83
|
+
try {
|
|
84
|
+
const buf = new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset, count).slice();
|
|
85
|
+
this.offset += count;
|
|
86
|
+
return buf;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
throw new UnexpectedEof();
|
|
90
|
+
}
|
|
91
|
+
case 105 /* constants.INT8 */:
|
|
92
|
+
try {
|
|
93
|
+
const buf = new Int8Array(this.data.buffer, this.data.byteOffset + this.offset, count).slice();
|
|
94
|
+
this.offset += count;
|
|
95
|
+
return buf;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
throw new UnexpectedEof();
|
|
99
|
+
}
|
|
100
|
+
case 73 /* constants.INT16 */: {
|
|
101
|
+
const result = new Int16Array(count);
|
|
102
|
+
for (let i = 0; i < count; i++) {
|
|
103
|
+
result[i] = this.readInt16Data();
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
case 108 /* constants.INT32 */: {
|
|
108
|
+
const result = new Int32Array(count);
|
|
109
|
+
for (let i = 0; i < count; i++) {
|
|
110
|
+
result[i] = this.readInt32Data();
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
case 100 /* constants.FLOAT32 */: {
|
|
115
|
+
const result = new Float32Array(count);
|
|
116
|
+
for (let i = 0; i < count; i++) {
|
|
117
|
+
result[i] = this.readFloat32Data();
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
case 68 /* constants.FLOAT64 */: {
|
|
122
|
+
const result = new Float64Array(count);
|
|
123
|
+
for (let i = 0; i < count; i++) {
|
|
124
|
+
result[i] = this.readFloat64Data();
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
case 76 /* constants.INT64 */: {
|
|
129
|
+
const result = new BigInt64Array(count);
|
|
130
|
+
for (let i = 0; i < count; i++) {
|
|
131
|
+
result[i] = this.readInt64Data();
|
|
132
|
+
}
|
|
133
|
+
throw new Error('Unsupported type int64');
|
|
134
|
+
//return result;
|
|
135
|
+
}
|
|
136
|
+
case 90 /* constants.NULL */:
|
|
137
|
+
return Array.from({ length: count }).fill(null);
|
|
138
|
+
case 84 /* constants.TRUE */:
|
|
139
|
+
return Array.from({ length: count }).fill(true);
|
|
140
|
+
case 70 /* constants.FALSE */:
|
|
141
|
+
return Array.from({ length: count }).fill(false);
|
|
142
|
+
default:
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
const array = [];
|
|
146
|
+
array.length = count;
|
|
147
|
+
for (let i = 0; i < count; i++) {
|
|
148
|
+
array[i] = type == null ? this.read() : this.readData(type);
|
|
149
|
+
}
|
|
150
|
+
return array;
|
|
151
|
+
}
|
|
152
|
+
case 68 /* constants.FLOAT64 */:
|
|
54
153
|
return this.readFloat64Data();
|
|
55
|
-
case constants.UINT8
|
|
154
|
+
case 85 /* constants.UINT8 */:
|
|
56
155
|
return this.readUint8Data();
|
|
57
|
-
case constants.INT16
|
|
156
|
+
case 73 /* constants.INT16 */:
|
|
58
157
|
return this.readInt16Data();
|
|
59
|
-
case constants.FLOAT32
|
|
158
|
+
case 100 /* constants.FLOAT32 */:
|
|
60
159
|
return this.readFloat32Data();
|
|
61
|
-
case constants.CHAR
|
|
62
|
-
return this.
|
|
63
|
-
case constants.INT32
|
|
160
|
+
case 67 /* constants.CHAR */:
|
|
161
|
+
return String.fromCharCode(this.readUint8Data());
|
|
162
|
+
case 108 /* constants.INT32 */:
|
|
64
163
|
return this.readInt32Data();
|
|
65
|
-
case constants.INT8
|
|
164
|
+
case 105 /* constants.INT8 */:
|
|
66
165
|
return this.readInt8Data();
|
|
67
|
-
case constants.NULL
|
|
166
|
+
case 90 /* constants.NULL */:
|
|
68
167
|
return null;
|
|
69
|
-
case constants.TRUE
|
|
168
|
+
case 84 /* constants.TRUE */:
|
|
70
169
|
return true;
|
|
71
|
-
case constants.FALSE
|
|
170
|
+
case 70 /* constants.FALSE */:
|
|
72
171
|
return false;
|
|
73
|
-
case constants.
|
|
74
|
-
return undefined;
|
|
75
|
-
case constants.INT64:
|
|
172
|
+
case 76 /* constants.INT64 */:
|
|
76
173
|
return this.readInt64Data();
|
|
77
|
-
case constants.HIGH_PRECISION_NUMBER
|
|
78
|
-
|
|
174
|
+
case 72 /* constants.HIGH_PRECISION_NUMBER */: {
|
|
175
|
+
const length = this.readIntLength();
|
|
176
|
+
try {
|
|
177
|
+
const _buffer = new Uint8Array(this.data.buffer, this.offset, length).slice();
|
|
178
|
+
this.offset += length;
|
|
179
|
+
// return buffer;
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
throw new UnexpectedEof();
|
|
183
|
+
}
|
|
184
|
+
throw new Error('Unsupported type high precision number');
|
|
185
|
+
}
|
|
79
186
|
}
|
|
80
187
|
throw new Error(`Unexpected marker '${String.fromCharCode(marker)}'(${marker})`);
|
|
81
188
|
}
|
|
82
189
|
/** 读取一个大于 0 的整数 */
|
|
83
190
|
readIntLength() {
|
|
84
191
|
const marker = this.readMarker();
|
|
192
|
+
if (marker === undefined)
|
|
193
|
+
throw new UnexpectedEof();
|
|
85
194
|
let length;
|
|
86
195
|
switch (marker) {
|
|
87
|
-
case constants.INT8
|
|
196
|
+
case 105 /* constants.INT8 */:
|
|
88
197
|
length = this.readInt8Data();
|
|
89
198
|
break;
|
|
90
|
-
case constants.UINT8
|
|
199
|
+
case 85 /* constants.UINT8 */:
|
|
91
200
|
length = this.readUint8Data();
|
|
92
201
|
break;
|
|
93
|
-
case constants.INT16
|
|
202
|
+
case 73 /* constants.INT16 */:
|
|
94
203
|
length = this.readInt16Data();
|
|
95
204
|
break;
|
|
96
|
-
case constants.INT32
|
|
205
|
+
case 108 /* constants.INT32 */:
|
|
97
206
|
length = this.readInt32Data();
|
|
98
207
|
break;
|
|
99
|
-
case constants.INT64
|
|
208
|
+
case 76 /* constants.INT64 */:
|
|
100
209
|
length = this.readInt64Data();
|
|
101
210
|
break;
|
|
102
211
|
default:
|
|
@@ -107,89 +216,97 @@ export class Decoder {
|
|
|
107
216
|
}
|
|
108
217
|
return length;
|
|
109
218
|
}
|
|
110
|
-
/**
|
|
111
|
-
|
|
219
|
+
/** readKey */
|
|
220
|
+
readKey() {
|
|
112
221
|
const length = this.readIntLength();
|
|
113
|
-
this.ensureData(length);
|
|
114
222
|
const begin = this.offset;
|
|
115
223
|
this.offset += length;
|
|
116
|
-
return
|
|
117
|
-
}
|
|
118
|
-
/** readHighPrecisionNumberData */
|
|
119
|
-
readHighPrecisionNumberData() {
|
|
120
|
-
const length = this.readIntLength();
|
|
121
|
-
this.ensureData(length);
|
|
122
|
-
throw new Error('Unsupported type high precision number');
|
|
123
|
-
// const buffer = this.data.subarray(this.offset, this.offset + length);
|
|
124
|
-
// this.offset += length;
|
|
125
|
-
// return buffer.slice();
|
|
126
|
-
}
|
|
127
|
-
/** readStringData */
|
|
128
|
-
readCharData() {
|
|
129
|
-
return String.fromCharCode(this.readUint8Data());
|
|
224
|
+
return decodeKey(this.data, begin, this.offset);
|
|
130
225
|
}
|
|
131
226
|
/** readInt8Data */
|
|
132
227
|
readInt8Data() {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
228
|
+
try {
|
|
229
|
+
return this.view.getInt8(this.offset++);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
throw new UnexpectedEof();
|
|
233
|
+
}
|
|
137
234
|
}
|
|
138
235
|
/** readUint8Data */
|
|
139
236
|
readUint8Data() {
|
|
140
|
-
this.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return
|
|
237
|
+
const num = this.data[this.offset++];
|
|
238
|
+
if (num === undefined)
|
|
239
|
+
throw new UnexpectedEof();
|
|
240
|
+
return num;
|
|
144
241
|
}
|
|
145
242
|
/** readInt16Data */
|
|
146
243
|
readInt16Data() {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
244
|
+
try {
|
|
245
|
+
const result = this.view.getInt16(this.offset);
|
|
246
|
+
this.offset += 2;
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
throw new UnexpectedEof();
|
|
251
|
+
}
|
|
151
252
|
}
|
|
152
253
|
/** readInt32Data */
|
|
153
254
|
readInt32Data() {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
255
|
+
try {
|
|
256
|
+
const result = this.view.getInt32(this.offset);
|
|
257
|
+
this.offset += 4;
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
throw new UnexpectedEof();
|
|
262
|
+
}
|
|
158
263
|
}
|
|
159
264
|
/** readInt64Data */
|
|
160
265
|
readInt64Data() {
|
|
161
|
-
|
|
266
|
+
try {
|
|
267
|
+
const _result = this.view.getBigInt64(this.offset);
|
|
268
|
+
this.offset += 8;
|
|
269
|
+
// return result;
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
throw new UnexpectedEof();
|
|
273
|
+
}
|
|
162
274
|
throw new Error('Unsupported type int64');
|
|
163
|
-
// const result = this.view.getBigInt64(this.offset);
|
|
164
|
-
// this.offset += 8;
|
|
165
|
-
// return result;
|
|
166
275
|
}
|
|
167
276
|
/** readFloat32Data */
|
|
168
277
|
readFloat32Data() {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
278
|
+
try {
|
|
279
|
+
const result = this.view.getFloat32(this.offset);
|
|
280
|
+
this.offset += 4;
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
throw new UnexpectedEof();
|
|
285
|
+
}
|
|
173
286
|
}
|
|
174
287
|
/** readFloat64Data */
|
|
175
288
|
readFloat64Data() {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
289
|
+
try {
|
|
290
|
+
const result = this.view.getFloat64(this.offset);
|
|
291
|
+
this.offset += 8;
|
|
292
|
+
return result;
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
throw new UnexpectedEof();
|
|
296
|
+
}
|
|
180
297
|
}
|
|
181
298
|
/** 读取 Optimized Format 数据 */
|
|
182
299
|
readOptimizedFormatMarkers() {
|
|
183
300
|
let type;
|
|
184
301
|
let count;
|
|
185
302
|
switch (this.readMarker()) {
|
|
186
|
-
case constants.TYPE_MARKER
|
|
303
|
+
case 36 /* constants.TYPE_MARKER */:
|
|
187
304
|
type = this.readMarker();
|
|
188
|
-
if (this.readMarker() !== constants.COUNT_MARKER) {
|
|
305
|
+
if (this.readMarker() !== 35 /* constants.COUNT_MARKER */) {
|
|
189
306
|
throw new Error('Expected count marker');
|
|
190
307
|
}
|
|
191
308
|
/* fall through */
|
|
192
|
-
case constants.COUNT_MARKER
|
|
309
|
+
case 35 /* constants.COUNT_MARKER */:
|
|
193
310
|
count = this.readIntLength();
|
|
194
311
|
break;
|
|
195
312
|
default:
|
|
@@ -199,83 +316,5 @@ export class Decoder {
|
|
|
199
316
|
}
|
|
200
317
|
return { type, count };
|
|
201
318
|
}
|
|
202
|
-
/** 读取数组 */
|
|
203
|
-
readArray() {
|
|
204
|
-
const markers = this.readOptimizedFormatMarkers();
|
|
205
|
-
if (markers == null) {
|
|
206
|
-
const array = [];
|
|
207
|
-
// 直到 ']'
|
|
208
|
-
while (this.readMarker() !== constants.ARRAY_END) {
|
|
209
|
-
this.offset--;
|
|
210
|
-
array.push(this.read());
|
|
211
|
-
}
|
|
212
|
-
return array;
|
|
213
|
-
}
|
|
214
|
-
const { count, type } = markers;
|
|
215
|
-
switch (type) {
|
|
216
|
-
case constants.UINT8:
|
|
217
|
-
this.ensureData(count);
|
|
218
|
-
this.offset += count;
|
|
219
|
-
return new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset - count, count).slice();
|
|
220
|
-
case constants.INT8:
|
|
221
|
-
this.ensureData(count);
|
|
222
|
-
this.offset += count;
|
|
223
|
-
return new Int8Array(this.data.buffer, this.data.byteOffset + this.offset - count, count).slice();
|
|
224
|
-
case constants.INT16:
|
|
225
|
-
this.ensureData(count * 2);
|
|
226
|
-
return Int16Array.from({ length: count }, () => this.readInt16Data());
|
|
227
|
-
case constants.INT32:
|
|
228
|
-
this.ensureData(count * 4);
|
|
229
|
-
return Int32Array.from({ length: count }, () => this.readInt32Data());
|
|
230
|
-
case constants.FLOAT32:
|
|
231
|
-
this.ensureData(count * 4);
|
|
232
|
-
return Float32Array.from({ length: count }, () => this.readFloat32Data());
|
|
233
|
-
case constants.FLOAT64: {
|
|
234
|
-
this.ensureData(count * 8);
|
|
235
|
-
const result = new Float64Array(count);
|
|
236
|
-
for (let i = 0; i < count; i++) {
|
|
237
|
-
result[i] = this.readFloat64Data();
|
|
238
|
-
}
|
|
239
|
-
return result;
|
|
240
|
-
}
|
|
241
|
-
case constants.NULL:
|
|
242
|
-
return Array.from({ length: count }).fill(null);
|
|
243
|
-
case constants.TRUE:
|
|
244
|
-
return Array.from({ length: count }).fill(true);
|
|
245
|
-
case constants.FALSE:
|
|
246
|
-
return Array.from({ length: count }).fill(false);
|
|
247
|
-
case constants.INT64:
|
|
248
|
-
this.ensureData(count * 8);
|
|
249
|
-
return BigInt64Array.from({ length: count }, () => this.readInt64Data());
|
|
250
|
-
default:
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
const array = [];
|
|
254
|
-
array.length = count;
|
|
255
|
-
for (let i = 0; i < count; i++) {
|
|
256
|
-
array[i] = type == null ? this.read() : this.readData(type);
|
|
257
|
-
}
|
|
258
|
-
return array;
|
|
259
|
-
}
|
|
260
|
-
/** 读对象 */
|
|
261
|
-
readObject() {
|
|
262
|
-
const markers = this.readOptimizedFormatMarkers();
|
|
263
|
-
if (markers == null) {
|
|
264
|
-
// 直到 '}'
|
|
265
|
-
const object = [];
|
|
266
|
-
while (this.readMarker() !== constants.OBJECT_END) {
|
|
267
|
-
this.offset--;
|
|
268
|
-
object.push([this.readStringData(), this.read()]);
|
|
269
|
-
}
|
|
270
|
-
return Object.fromEntries(object);
|
|
271
|
-
}
|
|
272
|
-
const { count, type } = markers;
|
|
273
|
-
const object = [];
|
|
274
|
-
object.length = count;
|
|
275
|
-
for (let i = 0; i < count; i++) {
|
|
276
|
-
object[i] = [this.readStringData(), type == null ? this.read() : this.readData(type)];
|
|
277
|
-
}
|
|
278
|
-
return Object.fromEntries(object);
|
|
279
|
-
}
|
|
280
319
|
}
|
|
281
320
|
//# sourceMappingURL=decoder.js.map
|