-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathDecoder.ts
626 lines (549 loc) · 17.4 KB
/
Decoder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import { prettyByte } from "./utils/prettyByte";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
import { getInt64, getUint64 } from "./utils/int";
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
import { DecodeError } from "./DecodeError";
const enum State {
ARRAY,
MAP_KEY,
MAP_VALUE,
}
type MapKeyType = string | number;
const isValidMapKeyType = (key: unknown): key is MapKeyType => {
const keyType = typeof key;
return keyType === "string" || keyType === "number";
};
type StackMapState = {
type: State.MAP_KEY | State.MAP_VALUE;
size: number;
key: MapKeyType | null;
readCount: number;
map: Record<string, unknown>;
};
type StackArrayState = {
type: State.ARRAY;
size: number;
array: Array<unknown>;
position: number;
};
type StackState = StackArrayState | StackMapState;
const HEAD_BYTE_REQUIRED = -1;
const EMPTY_VIEW = new DataView(new ArrayBuffer(0));
const EMPTY_BYTES = new Uint8Array(EMPTY_VIEW.buffer);
// IE11: Hack to support IE11.
// IE11: Drop this hack and just use RangeError when IE11 is obsolete.
export const DataViewIndexOutOfBoundsError: typeof Error = (() => {
try {
// IE11: The spec says it should throw RangeError,
// IE11: but in IE11 it throws TypeError.
EMPTY_VIEW.getInt8(0);
} catch (e: any) {
return e.constructor;
}
throw new Error("never reached");
})();
const MORE_DATA = new DataViewIndexOutOfBoundsError("Insufficient data");
const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max
const sharedCachedKeyDecoder = new CachedKeyDecoder();
export class Decoder<ContextType = undefined> {
private totalPos = 0;
private pos = 0;
private view = EMPTY_VIEW;
private bytes = EMPTY_BYTES;
private headByte = HEAD_BYTE_REQUIRED;
private readonly stack: Array<StackState> = [];
public constructor(
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
private readonly context: ContextType = undefined as any,
private readonly maxStrLength = DEFAULT_MAX_LENGTH,
private readonly maxBinLength = DEFAULT_MAX_LENGTH,
private readonly maxArrayLength = DEFAULT_MAX_LENGTH,
private readonly maxMapLength = DEFAULT_MAX_LENGTH,
private readonly maxExtLength = DEFAULT_MAX_LENGTH,
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
) {}
private reinitializeState() {
this.totalPos = 0;
this.headByte = HEAD_BYTE_REQUIRED;
this.stack.length = 0;
// view, bytes, and pos will be re-initialized in setBuffer()
}
private setBuffer(buffer: ArrayLike<number> | BufferSource): void {
this.bytes = ensureUint8Array(buffer);
this.view = createDataView(this.bytes);
this.pos = 0;
}
private appendBuffer(buffer: ArrayLike<number> | BufferSource) {
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining()) {
this.setBuffer(buffer);
} else {
// retried because data is insufficient
const remainingData = this.bytes.subarray(this.pos);
const newData = ensureUint8Array(buffer);
const concated = new Uint8Array(remainingData.length + newData.length);
concated.set(remainingData);
concated.set(newData, remainingData.length);
this.setBuffer(concated);
}
}
private hasRemaining(size = 1) {
return this.view.byteLength - this.pos >= size;
}
private createExtraByteError(posToShow: number): Error {
const { view, pos } = this;
return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
}
/**
* @throws {DecodeError}
* @throws {RangeError}
*/
public decode(buffer: ArrayLike<number> | BufferSource): unknown {
this.reinitializeState();
this.setBuffer(buffer);
const object = this.doDecodeSync();
if (this.hasRemaining()) {
throw this.createExtraByteError(this.pos);
}
return object;
}
public *decodeMulti(buffer: ArrayLike<number> | BufferSource): Generator<unknown, void, unknown> {
this.reinitializeState();
this.setBuffer(buffer);
while (this.hasRemaining()) {
yield this.doDecodeSync();
}
}
public async decodeAsync(stream: AsyncIterable<ArrayLike<number> | BufferSource>): Promise<unknown> {
let decoded = false;
let object: unknown;
for await (const buffer of stream) {
if (decoded) {
throw this.createExtraByteError(this.totalPos);
}
this.appendBuffer(buffer);
try {
object = this.doDecodeSync();
decoded = true;
} catch (e) {
if (!(e instanceof DataViewIndexOutOfBoundsError)) {
throw e; // rethrow
}
// fallthrough
}
this.totalPos += this.pos;
}
if (decoded) {
if (this.hasRemaining()) {
throw this.createExtraByteError(this.totalPos);
}
return object;
}
const { headByte, pos, totalPos } = this;
throw new RangeError(
`Insufficient data in parsing ${prettyByte(headByte)} at ${totalPos} (${pos} in the current buffer)`,
);
}
public decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | BufferSource>): AsyncGenerator<unknown, void, unknown> {
return this.decodeMultiAsync(stream, true);
}
public decodeStream(stream: AsyncIterable<ArrayLike<number> | BufferSource>): AsyncGenerator<unknown, void, unknown> {
return this.decodeMultiAsync(stream, false);
}
private async *decodeMultiAsync(stream: AsyncIterable<ArrayLike<number> | BufferSource>, isArray: boolean) {
let isArrayHeaderRequired = isArray;
let arrayItemsLeft = -1;
for await (const buffer of stream) {
if (isArray && arrayItemsLeft === 0) {
throw this.createExtraByteError(this.totalPos);
}
this.appendBuffer(buffer);
if (isArrayHeaderRequired) {
arrayItemsLeft = this.readArraySize();
isArrayHeaderRequired = false;
this.complete();
}
try {
while (true) {
yield this.doDecodeSync();
if (--arrayItemsLeft === 0) {
break;
}
}
} catch (e) {
if (!(e instanceof DataViewIndexOutOfBoundsError)) {
throw e; // rethrow
}
// fallthrough
}
this.totalPos += this.pos;
}
}
private doDecodeSync(): unknown {
DECODE: while (true) {
const headByte = this.readHeadByte();
let object: unknown;
if (headByte >= 0xe0) {
// negative fixint (111x xxxx) 0xe0 - 0xff
object = headByte - 0x100;
} else if (headByte < 0xc0) {
if (headByte < 0x80) {
// positive fixint (0xxx xxxx) 0x00 - 0x7f
object = headByte;
} else if (headByte < 0x90) {
// fixmap (1000 xxxx) 0x80 - 0x8f
const size = headByte - 0x80;
if (size !== 0) {
this.pushMapState(size);
this.complete();
continue DECODE;
} else {
object = {};
}
} else if (headByte < 0xa0) {
// fixarray (1001 xxxx) 0x90 - 0x9f
const size = headByte - 0x90;
if (size !== 0) {
this.pushArrayState(size);
this.complete();
continue DECODE;
} else {
object = [];
}
} else {
// fixstr (101x xxxx) 0xa0 - 0xbf
const byteLength = headByte - 0xa0;
object = this.decodeUtf8String(byteLength, 0);
}
} else if (headByte === 0xc0) {
// nil
object = null;
} else if (headByte === 0xc2) {
// false
object = false;
} else if (headByte === 0xc3) {
// true
object = true;
} else if (headByte === 0xca) {
// float 32
object = this.readF32();
} else if (headByte === 0xcb) {
// float 64
object = this.readF64();
} else if (headByte === 0xcc) {
// uint 8
object = this.readU8();
} else if (headByte === 0xcd) {
// uint 16
object = this.readU16();
} else if (headByte === 0xce) {
// uint 32
object = this.readU32();
} else if (headByte === 0xcf) {
// uint 64
object = this.readU64();
} else if (headByte === 0xd0) {
// int 8
object = this.readI8();
} else if (headByte === 0xd1) {
// int 16
object = this.readI16();
} else if (headByte === 0xd2) {
// int 32
object = this.readI32();
} else if (headByte === 0xd3) {
// int 64
object = this.readI64();
} else if (headByte === 0xd9) {
// str 8
const byteLength = this.lookU8();
object = this.decodeUtf8String(byteLength, 1);
} else if (headByte === 0xda) {
// str 16
const byteLength = this.lookU16();
object = this.decodeUtf8String(byteLength, 2);
} else if (headByte === 0xdb) {
// str 32
const byteLength = this.lookU32();
object = this.decodeUtf8String(byteLength, 4);
} else if (headByte === 0xdc) {
// array 16
const size = this.readU16();
if (size !== 0) {
this.pushArrayState(size);
this.complete();
continue DECODE;
} else {
object = [];
}
} else if (headByte === 0xdd) {
// array 32
const size = this.readU32();
if (size !== 0) {
this.pushArrayState(size);
this.complete();
continue DECODE;
} else {
object = [];
}
} else if (headByte === 0xde) {
// map 16
const size = this.readU16();
if (size !== 0) {
this.pushMapState(size);
this.complete();
continue DECODE;
} else {
object = {};
}
} else if (headByte === 0xdf) {
// map 32
const size = this.readU32();
if (size !== 0) {
this.pushMapState(size);
this.complete();
continue DECODE;
} else {
object = {};
}
} else if (headByte === 0xc4) {
// bin 8
const size = this.lookU8();
object = this.decodeBinary(size, 1);
} else if (headByte === 0xc5) {
// bin 16
const size = this.lookU16();
object = this.decodeBinary(size, 2);
} else if (headByte === 0xc6) {
// bin 32
const size = this.lookU32();
object = this.decodeBinary(size, 4);
} else if (headByte === 0xd4) {
// fixext 1
object = this.decodeExtension(1, 0);
} else if (headByte === 0xd5) {
// fixext 2
object = this.decodeExtension(2, 0);
} else if (headByte === 0xd6) {
// fixext 4
object = this.decodeExtension(4, 0);
} else if (headByte === 0xd7) {
// fixext 8
object = this.decodeExtension(8, 0);
} else if (headByte === 0xd8) {
// fixext 16
object = this.decodeExtension(16, 0);
} else if (headByte === 0xc7) {
// ext 8
const size = this.lookU8();
object = this.decodeExtension(size, 1);
} else if (headByte === 0xc8) {
// ext 16
const size = this.lookU16();
object = this.decodeExtension(size, 2);
} else if (headByte === 0xc9) {
// ext 32
const size = this.lookU32();
object = this.decodeExtension(size, 4);
} else {
throw new DecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`);
}
this.complete();
const stack = this.stack;
while (stack.length > 0) {
// arrays and maps
const state = stack[stack.length - 1]!;
if (state.type === State.ARRAY) {
state.array[state.position] = object;
state.position++;
if (state.position === state.size) {
stack.pop();
object = state.array;
} else {
continue DECODE;
}
} else if (state.type === State.MAP_KEY) {
if (!isValidMapKeyType(object)) {
throw new DecodeError("The type of key must be string or number but " + typeof object);
}
if (object === "__proto__") {
throw new DecodeError("The key __proto__ is not allowed");
}
state.key = object;
state.type = State.MAP_VALUE;
continue DECODE;
} else {
// it must be `state.type === State.MAP_VALUE` here
state.map[state.key!] = object;
state.readCount++;
if (state.readCount === state.size) {
stack.pop();
object = state.map;
} else {
state.key = null;
state.type = State.MAP_KEY;
continue DECODE;
}
}
}
return object;
}
}
private readHeadByte(): number {
if (this.headByte === HEAD_BYTE_REQUIRED) {
this.headByte = this.readU8();
// console.log("headByte", prettyByte(this.headByte));
}
return this.headByte;
}
private complete(): void {
this.headByte = HEAD_BYTE_REQUIRED;
}
private readArraySize(): number {
const headByte = this.readHeadByte();
switch (headByte) {
case 0xdc:
return this.readU16();
case 0xdd:
return this.readU32();
default: {
if (headByte < 0xa0) {
return headByte - 0x90;
} else {
throw new DecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`);
}
}
}
}
private pushMapState(size: number) {
if (size > this.maxMapLength) {
throw new DecodeError(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
}
this.stack.push({
type: State.MAP_KEY,
size,
key: null,
readCount: 0,
map: {},
});
}
private pushArrayState(size: number) {
if (size > this.maxArrayLength) {
throw new DecodeError(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
}
this.stack.push({
type: State.ARRAY,
size,
array: new Array<unknown>(size),
position: 0,
});
}
private decodeUtf8String(byteLength: number, headerOffset: number): string {
if (byteLength > this.maxStrLength) {
throw new DecodeError(
`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`,
);
}
if (this.bytes.byteLength < this.pos + headerOffset + byteLength) {
throw MORE_DATA;
}
const offset = this.pos + headerOffset;
let object: string;
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) {
object = this.keyDecoder.decode(this.bytes, offset, byteLength);
} else if (byteLength > TEXT_DECODER_THRESHOLD) {
object = utf8DecodeTD(this.bytes, offset, byteLength);
} else {
object = utf8DecodeJs(this.bytes, offset, byteLength);
}
this.pos += headerOffset + byteLength;
return object;
}
private stateIsMapKey(): boolean {
if (this.stack.length > 0) {
const state = this.stack[this.stack.length - 1]!;
return state.type === State.MAP_KEY;
}
return false;
}
private decodeBinary(byteLength: number, headOffset: number): Uint8Array {
if (byteLength > this.maxBinLength) {
throw new DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
}
if (!this.hasRemaining(byteLength + headOffset)) {
throw MORE_DATA;
}
const offset = this.pos + headOffset;
const object = this.bytes.subarray(offset, offset + byteLength);
this.pos += headOffset + byteLength;
return object;
}
private decodeExtension(size: number, headOffset: number): unknown {
if (size > this.maxExtLength) {
throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
}
const extType = this.view.getInt8(this.pos + headOffset);
const data = this.decodeBinary(size, headOffset + 1 /* extType */);
return this.extensionCodec.decode(data, extType, this.context);
}
private lookU8() {
return this.view.getUint8(this.pos);
}
private lookU16() {
return this.view.getUint16(this.pos);
}
private lookU32() {
return this.view.getUint32(this.pos);
}
private readU8(): number {
const value = this.view.getUint8(this.pos);
this.pos++;
return value;
}
private readI8(): number {
const value = this.view.getInt8(this.pos);
this.pos++;
return value;
}
private readU16(): number {
const value = this.view.getUint16(this.pos);
this.pos += 2;
return value;
}
private readI16(): number {
const value = this.view.getInt16(this.pos);
this.pos += 2;
return value;
}
private readU32(): number {
const value = this.view.getUint32(this.pos);
this.pos += 4;
return value;
}
private readI32(): number {
const value = this.view.getInt32(this.pos);
this.pos += 4;
return value;
}
private readU64(): number {
const value = getUint64(this.view, this.pos);
this.pos += 8;
return value;
}
private readI64(): number {
const value = getInt64(this.view, this.pos);
this.pos += 8;
return value;
}
private readF32() {
const value = this.view.getFloat32(this.pos);
this.pos += 4;
return value;
}
private readF64() {
const value = this.view.getFloat64(this.pos);
this.pos += 8;
return value;
}
}