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