@cloudpss/ubjson 0.5.36 → 0.5.38
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 +12 -0
- package/dist/base/decoder.js +26 -0
- package/dist/base/decoder.js.map +1 -0
- package/dist/{common → base}/encoder.d.ts +2 -6
- package/dist/{common → base}/encoder.js +71 -121
- package/dist/base/encoder.js.map +1 -0
- package/dist/decoder.d.ts +1 -2
- package/dist/decoder.js +1 -1
- package/dist/decoder.js.map +1 -1
- package/dist/encoder.d.ts +1 -1
- package/dist/encoder.js +11 -11
- package/dist/encoder.js.map +1 -1
- package/dist/{common → helper}/constants.js.map +1 -1
- package/dist/helper/decode.d.ts +49 -0
- package/dist/helper/decode.js +344 -0
- package/dist/helper/decode.js.map +1 -0
- package/dist/helper/encode.d.ts +20 -0
- package/dist/helper/encode.js +84 -0
- package/dist/helper/encode.js.map +1 -0
- package/dist/{common → helper}/errors.d.ts +4 -0
- package/dist/{common → helper}/errors.js +10 -2
- package/dist/helper/errors.js.map +1 -0
- package/dist/{common → helper}/string-decoder.js +1 -1
- package/dist/{common → helper}/string-decoder.js.map +1 -1
- package/dist/{common → helper}/string-encoder.js.map +1 -1
- package/dist/{utils.d.ts → helper/utils.d.ts} +0 -4
- package/dist/{utils.js → helper/utils.js} +0 -6
- package/dist/helper/utils.js.map +1 -0
- package/dist/index.d.ts +3 -5
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/rxjs/decoder.js +1 -1
- package/dist/rxjs/decoder.js.map +1 -1
- package/dist/rxjs/index.d.ts +1 -1
- package/dist/rxjs/index.js +1 -1
- package/dist/rxjs/index.js.map +1 -1
- package/dist/stream/index.d.ts +2 -2
- package/dist/stream/index.js +2 -2
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +2 -3
- package/dist/stream-helper/decoder.js +3 -6
- package/dist/stream-helper/decoder.js.map +1 -1
- package/dist/stream-helper/encoder.d.ts +1 -1
- package/dist/stream-helper/encoder.js +15 -15
- package/dist/stream-helper/encoder.js.map +1 -1
- package/package.json +4 -4
- package/src/base/decoder.ts +27 -0
- package/src/{common → base}/encoder.ts +72 -116
- package/src/decoder.ts +1 -2
- package/src/encoder.ts +11 -11
- package/src/helper/decode.ts +362 -0
- package/src/helper/encode.ts +89 -0
- package/src/{common → helper}/errors.ts +12 -2
- package/src/{common → helper}/string-decoder.ts +1 -1
- package/src/{utils.ts → helper/utils.ts} +0 -7
- package/src/index.ts +6 -8
- package/src/rxjs/decoder.ts +1 -1
- package/src/rxjs/index.ts +1 -1
- package/src/stream/index.ts +3 -3
- package/src/stream-helper/decoder.ts +3 -6
- package/src/stream-helper/encoder.ts +16 -16
- package/tests/.utils.js +6 -4
- package/tests/decode.js +0 -18
- package/tests/e2e/no-encode-into.js +1 -1
- package/tests/encode.js +1 -1
- package/tests/huge-string.js +1 -0
- package/tests/rxjs/decode.js +14 -13
- package/tests/rxjs/encode.js +13 -12
- package/tests/stream/decode.js +15 -13
- package/tests/stream/encode.js +9 -8
- package/tests/stream/many.js +2 -1
- package/tests/string-encoding.js +4 -6
- package/benchmark-small.js +0 -81
- package/benchmark-string-decode.js +0 -41
- package/benchmark-string-encode.js +0 -32
- package/benchmark-string-size-caculation.js +0 -50
- package/benchmark.js +0 -50
- package/dist/common/decoder.d.ts +0 -45
- package/dist/common/decoder.js +0 -348
- package/dist/common/decoder.js.map +0 -1
- package/dist/common/encoder.js.map +0 -1
- package/dist/common/errors.js.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/common/decoder.ts +0 -356
- /package/dist/{common → helper}/constants.d.ts +0 -0
- /package/dist/{common → helper}/constants.js +0 -0
- /package/dist/{common → helper}/string-decoder.d.ts +0 -0
- /package/dist/{common → helper}/string-encoder.d.ts +0 -0
- /package/dist/{common → helper}/string-encoder.js +0 -0
- /package/src/{common → helper}/constants.ts +0 -0
- /package/src/{common → helper}/string-encoder.ts +0 -0
package/src/common/decoder.ts
DELETED
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
import { UnexpectedEof, toUint8Array } from '../utils.js';
|
|
2
|
-
import { constants } from './constants.js';
|
|
3
|
-
import { unsupportedType } from './errors.js';
|
|
4
|
-
import { decode, decodeKey } from './string-decoder.js';
|
|
5
|
-
|
|
6
|
-
const fromEntries = Object.fromEntries;
|
|
7
|
-
|
|
8
|
-
/** decoder options */
|
|
9
|
-
export interface DecoderOptions {
|
|
10
|
-
/** 对 {@link Uint8Array} 使用 subarray */
|
|
11
|
-
noAllocBuffer?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** decoder */
|
|
15
|
-
export class DecoderBase {
|
|
16
|
-
protected readonly view: DataView;
|
|
17
|
-
protected readonly data: Uint8Array;
|
|
18
|
-
/** 当前读指针位置 */
|
|
19
|
-
protected offset = 0;
|
|
20
|
-
|
|
21
|
-
/** 选项 */
|
|
22
|
-
protected readonly noAllocBuffer;
|
|
23
|
-
constructor(data: BinaryData, options?: DecoderOptions) {
|
|
24
|
-
this.data = toUint8Array(data);
|
|
25
|
-
this.view = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
|
|
26
|
-
if (options) {
|
|
27
|
-
this.noAllocBuffer = !!options.noAllocBuffer;
|
|
28
|
-
} else {
|
|
29
|
-
this.noAllocBuffer = false;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** EOF */
|
|
34
|
-
protected eof(): never {
|
|
35
|
-
throw new UnexpectedEof();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** 解码 */
|
|
39
|
-
decode(): unknown {
|
|
40
|
-
let marker = constants.NO_OP;
|
|
41
|
-
do {
|
|
42
|
-
marker = this.data[this.offset++]!;
|
|
43
|
-
} while (marker === constants.NO_OP); // 跳过 noop
|
|
44
|
-
if (marker === undefined) return undefined;
|
|
45
|
-
return this.readData(marker);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** 读取数据 */
|
|
49
|
-
private read(): unknown {
|
|
50
|
-
const marker = this.readMarker();
|
|
51
|
-
return this.readData(marker);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** 读取 marker,跳过 noop */
|
|
55
|
-
private readMarker(): number {
|
|
56
|
-
let marker = constants.NO_OP;
|
|
57
|
-
do {
|
|
58
|
-
marker = this.data[this.offset++]!;
|
|
59
|
-
} while (marker === constants.NO_OP);
|
|
60
|
-
if (marker === undefined) this.eof();
|
|
61
|
-
return marker;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** 读取数据 */
|
|
65
|
-
private readData(marker: number): unknown {
|
|
66
|
-
// 按照出现频率排序
|
|
67
|
-
switch (marker) {
|
|
68
|
-
case constants.STRING: {
|
|
69
|
-
const length = this.readIntLength();
|
|
70
|
-
const begin = this.offset;
|
|
71
|
-
const end = begin + length;
|
|
72
|
-
this.offset = end;
|
|
73
|
-
const data = this.data;
|
|
74
|
-
if (end > data.length) this.eof();
|
|
75
|
-
return decode(data, begin, end);
|
|
76
|
-
}
|
|
77
|
-
case constants.OBJECT: {
|
|
78
|
-
const markers = this.readOptimizedFormatMarkers();
|
|
79
|
-
if (markers == null) {
|
|
80
|
-
// 直到 '}'
|
|
81
|
-
const object: Array<[string, unknown]> = [];
|
|
82
|
-
while (this.readMarker() !== constants.OBJECT_END) {
|
|
83
|
-
this.offset--;
|
|
84
|
-
object.push([this.readKey(), this.read()]);
|
|
85
|
-
}
|
|
86
|
-
return fromEntries(object);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const { count, type } = markers;
|
|
90
|
-
const object: Array<[string, unknown]> = [];
|
|
91
|
-
object.length = count;
|
|
92
|
-
for (let i = 0; i < count; i++) {
|
|
93
|
-
const key = this.readKey();
|
|
94
|
-
const value = this.readData(type ?? this.readMarker());
|
|
95
|
-
object[i] = [key, value];
|
|
96
|
-
}
|
|
97
|
-
return fromEntries(object);
|
|
98
|
-
}
|
|
99
|
-
case constants.ARRAY: {
|
|
100
|
-
const markers = this.readOptimizedFormatMarkers();
|
|
101
|
-
if (markers == null) {
|
|
102
|
-
const array = [];
|
|
103
|
-
// 直到 ']'
|
|
104
|
-
while (this.readMarker() !== constants.ARRAY_END) {
|
|
105
|
-
this.offset--;
|
|
106
|
-
array.push(this.read());
|
|
107
|
-
}
|
|
108
|
-
return array;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const { count, type } = markers;
|
|
112
|
-
switch (type) {
|
|
113
|
-
case constants.UINT8:
|
|
114
|
-
try {
|
|
115
|
-
const buf = new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset, count);
|
|
116
|
-
this.offset += count;
|
|
117
|
-
return this.noAllocBuffer ? buf : buf.slice();
|
|
118
|
-
} catch {
|
|
119
|
-
return this.eof();
|
|
120
|
-
}
|
|
121
|
-
case constants.INT8:
|
|
122
|
-
try {
|
|
123
|
-
const buf = new Int8Array(
|
|
124
|
-
this.data.buffer,
|
|
125
|
-
this.data.byteOffset + this.offset,
|
|
126
|
-
count,
|
|
127
|
-
).slice();
|
|
128
|
-
this.offset += count;
|
|
129
|
-
return buf;
|
|
130
|
-
} catch {
|
|
131
|
-
return this.eof();
|
|
132
|
-
}
|
|
133
|
-
case constants.INT16: {
|
|
134
|
-
const result = new Int16Array(count);
|
|
135
|
-
for (let i = 0; i < count; i++) {
|
|
136
|
-
result[i] = this.readInt16Data();
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
}
|
|
140
|
-
case constants.INT32: {
|
|
141
|
-
const result = new Int32Array(count);
|
|
142
|
-
for (let i = 0; i < count; i++) {
|
|
143
|
-
result[i] = this.readInt32Data();
|
|
144
|
-
}
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
|
-
case constants.FLOAT32: {
|
|
148
|
-
const result = new Float32Array(count);
|
|
149
|
-
for (let i = 0; i < count; i++) {
|
|
150
|
-
result[i] = this.readFloat32Data();
|
|
151
|
-
}
|
|
152
|
-
return result;
|
|
153
|
-
}
|
|
154
|
-
case constants.FLOAT64: {
|
|
155
|
-
const result = new Float64Array(count);
|
|
156
|
-
for (let i = 0; i < count; i++) {
|
|
157
|
-
result[i] = this.readFloat64Data();
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
case constants.INT64: {
|
|
162
|
-
const result = new BigInt64Array(count);
|
|
163
|
-
for (let i = 0; i < count; i++) {
|
|
164
|
-
result[i] = this.readInt64Data();
|
|
165
|
-
}
|
|
166
|
-
return result;
|
|
167
|
-
}
|
|
168
|
-
case constants.NULL:
|
|
169
|
-
return Array.from({ length: count }).fill(null);
|
|
170
|
-
case constants.TRUE:
|
|
171
|
-
return Array.from({ length: count }).fill(true);
|
|
172
|
-
case constants.FALSE:
|
|
173
|
-
return Array.from({ length: count }).fill(false);
|
|
174
|
-
default:
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
const array: unknown[] = [];
|
|
178
|
-
array.length = count;
|
|
179
|
-
for (let i = 0; i < count; i++) {
|
|
180
|
-
array[i] = type == null ? this.read() : this.readData(type);
|
|
181
|
-
}
|
|
182
|
-
return array;
|
|
183
|
-
}
|
|
184
|
-
case constants.FLOAT64:
|
|
185
|
-
return this.readFloat64Data();
|
|
186
|
-
case constants.UINT8:
|
|
187
|
-
return this.readUint8Data();
|
|
188
|
-
case constants.INT16:
|
|
189
|
-
return this.readInt16Data();
|
|
190
|
-
case constants.FLOAT32:
|
|
191
|
-
return this.readFloat32Data();
|
|
192
|
-
case constants.CHAR:
|
|
193
|
-
return String.fromCharCode(this.readUint8Data());
|
|
194
|
-
case constants.INT32:
|
|
195
|
-
return this.readInt32Data();
|
|
196
|
-
case constants.INT8:
|
|
197
|
-
return this.readInt8Data();
|
|
198
|
-
case constants.NULL:
|
|
199
|
-
return null;
|
|
200
|
-
case constants.TRUE:
|
|
201
|
-
return true;
|
|
202
|
-
case constants.FALSE:
|
|
203
|
-
return false;
|
|
204
|
-
case constants.INT64: {
|
|
205
|
-
const n = this.readInt64Data();
|
|
206
|
-
if (n < Number.MIN_SAFE_INTEGER || n > Number.MAX_SAFE_INTEGER) {
|
|
207
|
-
return n;
|
|
208
|
-
}
|
|
209
|
-
return Number(n);
|
|
210
|
-
}
|
|
211
|
-
case constants.HIGH_PRECISION_NUMBER: {
|
|
212
|
-
const length = this.readIntLength();
|
|
213
|
-
try {
|
|
214
|
-
const _buffer = new Uint8Array(this.data.buffer, this.offset, length).slice();
|
|
215
|
-
this.offset += length;
|
|
216
|
-
// return buffer;
|
|
217
|
-
} catch {
|
|
218
|
-
return this.eof();
|
|
219
|
-
}
|
|
220
|
-
unsupportedType('high precision number');
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
throw new Error(`Unexpected marker '${String.fromCharCode(marker)}'(${marker})`);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/** 读取一个大于 0 的整数 */
|
|
227
|
-
private readIntLength(): number {
|
|
228
|
-
const marker = this.readMarker();
|
|
229
|
-
let length;
|
|
230
|
-
switch (marker) {
|
|
231
|
-
case constants.INT8:
|
|
232
|
-
length = this.readInt8Data();
|
|
233
|
-
break;
|
|
234
|
-
case constants.UINT8:
|
|
235
|
-
length = this.readUint8Data();
|
|
236
|
-
break;
|
|
237
|
-
case constants.INT16:
|
|
238
|
-
length = this.readInt16Data();
|
|
239
|
-
break;
|
|
240
|
-
case constants.INT32:
|
|
241
|
-
length = this.readInt32Data();
|
|
242
|
-
break;
|
|
243
|
-
case constants.INT64: {
|
|
244
|
-
const l = this.readInt64Data();
|
|
245
|
-
if (l < 0 || l > Number.MAX_SAFE_INTEGER) {
|
|
246
|
-
throw new Error('Invalid length');
|
|
247
|
-
}
|
|
248
|
-
length = Number(l);
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
default:
|
|
252
|
-
throw new Error(`Unexpected marker '${String.fromCharCode(marker)}'(${marker}) for int length`);
|
|
253
|
-
}
|
|
254
|
-
if (length < 0) {
|
|
255
|
-
throw new Error('Invalid length');
|
|
256
|
-
}
|
|
257
|
-
return length;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/** readKey */
|
|
261
|
-
private readKey(): string {
|
|
262
|
-
const length = this.readIntLength();
|
|
263
|
-
const begin = this.offset;
|
|
264
|
-
const end = begin + length;
|
|
265
|
-
this.offset = end;
|
|
266
|
-
const data = this.data;
|
|
267
|
-
if (end > data.length) this.eof();
|
|
268
|
-
return decodeKey(data, begin, end);
|
|
269
|
-
}
|
|
270
|
-
/** readInt8Data */
|
|
271
|
-
private readInt8Data(): number {
|
|
272
|
-
try {
|
|
273
|
-
return this.view.getInt8(this.offset++);
|
|
274
|
-
} catch {
|
|
275
|
-
this.eof();
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/** readUint8Data */
|
|
279
|
-
private readUint8Data(): number {
|
|
280
|
-
const num = this.data[this.offset++];
|
|
281
|
-
if (num === undefined) this.eof();
|
|
282
|
-
return num;
|
|
283
|
-
}
|
|
284
|
-
/** readInt16Data */
|
|
285
|
-
private readInt16Data(): number {
|
|
286
|
-
try {
|
|
287
|
-
const result = this.view.getInt16(this.offset);
|
|
288
|
-
this.offset += 2;
|
|
289
|
-
return result;
|
|
290
|
-
} catch {
|
|
291
|
-
this.eof();
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
/** readInt32Data */
|
|
295
|
-
private readInt32Data(): number {
|
|
296
|
-
try {
|
|
297
|
-
const result = this.view.getInt32(this.offset);
|
|
298
|
-
this.offset += 4;
|
|
299
|
-
return result;
|
|
300
|
-
} catch {
|
|
301
|
-
this.eof();
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
/** readInt64Data */
|
|
305
|
-
private readInt64Data(): bigint {
|
|
306
|
-
try {
|
|
307
|
-
const result = this.view.getBigInt64(this.offset);
|
|
308
|
-
this.offset += 8;
|
|
309
|
-
return result;
|
|
310
|
-
} catch {
|
|
311
|
-
this.eof();
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
/** readFloat32Data */
|
|
315
|
-
private readFloat32Data(): number {
|
|
316
|
-
try {
|
|
317
|
-
const result = this.view.getFloat32(this.offset);
|
|
318
|
-
this.offset += 4;
|
|
319
|
-
return result;
|
|
320
|
-
} catch {
|
|
321
|
-
this.eof();
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
/** readFloat64Data */
|
|
325
|
-
private readFloat64Data(): number {
|
|
326
|
-
try {
|
|
327
|
-
const result = this.view.getFloat64(this.offset);
|
|
328
|
-
this.offset += 8;
|
|
329
|
-
return result;
|
|
330
|
-
} catch {
|
|
331
|
-
this.eof();
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/** 读取 Optimized Format 数据 */
|
|
336
|
-
private readOptimizedFormatMarkers(): { type?: number; count: number } | undefined {
|
|
337
|
-
let type;
|
|
338
|
-
let count;
|
|
339
|
-
switch (this.readMarker()) {
|
|
340
|
-
case constants.TYPE_MARKER:
|
|
341
|
-
type = this.readMarker();
|
|
342
|
-
if (this.readMarker() !== constants.COUNT_MARKER) {
|
|
343
|
-
throw new Error('Expected count marker');
|
|
344
|
-
}
|
|
345
|
-
/* fall through */
|
|
346
|
-
case constants.COUNT_MARKER:
|
|
347
|
-
count = this.readIntLength();
|
|
348
|
-
break;
|
|
349
|
-
default:
|
|
350
|
-
// 不是 '$' 或 '#',回溯
|
|
351
|
-
this.offset--;
|
|
352
|
-
return undefined;
|
|
353
|
-
}
|
|
354
|
-
return { type, count };
|
|
355
|
-
}
|
|
356
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|