Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathDecoder.ts
More file actions
Latest commit
880 lines (766 loc) · 24.6 KB
/
Copy pathDecoder.ts
File metadata and controls
880 lines (766 loc) · 24.6 KB
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
import{prettyByte}from"./utils/prettyByte.ts";
import{ExtensionCodec}from"./ExtensionCodec.ts";
import{getInt64,getUint64,UINT32_MAX}from"./utils/int.ts";
import{utf8Decode}from"./utils/utf8.ts";
import{ensureUint8Array}from"./utils/typedArrays.ts";
import{CachedKeyDecoder}from"./CachedKeyDecoder.ts";
import{DecodeError}from"./DecodeError.ts";
importtype{ContextOf}from"./context.ts";
importtype{ExtensionCodecType}from"./ExtensionCodec.ts";
importtype{KeyDecoder}from"./CachedKeyDecoder.ts";
exporttypeDecoderOptions<ContextType=undefined>=Readonly<
Partial<{
extensionCodec: ExtensionCodecType<ContextType>;
/**
* Decodes Int64 and Uint64 as bigint if it's set to true.
* Depends on ES2020's {@link DataView#getBigInt64} and
* {@link DataView#getBigUint64}.
*
* Defaults to false.
*/
useBigInt64: boolean;
/**
* By default, string values will be decoded as UTF-8 strings. However, if this option is true,
* string values will be returned as Uint8Arrays without additional decoding.
*
* This is useful if the strings may contain invalid UTF-8 sequences.
*
* Note that this option only applies to string values, not map keys. Additionally, when
* enabled, raw string length is limited by the maxBinLength option.
*/
rawStrings: boolean;
/**
* Maximum string length.
*
* Defaults to 4_294_967_295 (UINT32_MAX).
*/
maxStrLength: number;
/**
* Maximum binary length.
*
* Defaults to 4_294_967_295 (UINT32_MAX).
*/
maxBinLength: number;
/**
* Maximum array length.
*
* Defaults to 4_294_967_295 (UINT32_MAX).
*/
maxArrayLength: number;
/**
* Maximum map length.
*
* Defaults to 4_294_967_295 (UINT32_MAX).
*/
maxMapLength: number;
/**
* Maximum extension length.
*
* Defaults to 4_294_967_295 (UINT32_MAX).
*/
maxExtLength: number;
/**
* An object key decoder. Defaults to the shared instance of {@link CachedKeyDecoder}.
* `null` is a special value to disable the use of the key decoder at all.
*/
keyDecoder: KeyDecoder|null;
/**
* A function to convert decoded map key to a valid JS key type.
*
* Defaults to a function that throws an error if the key is not a string or a number.
*/
mapKeyConverter: (key: unknown)=>MapKeyType;
}>
>&
ContextOf<ContextType>;
constSTATE_ARRAY="array";
constSTATE_MAP_KEY="map_key";
constSTATE_MAP_VALUE="map_value";
typeMapKeyType=string|number;
constmapKeyConverter=(key: unknown): MapKeyType=>{
if(typeofkey==="string"||typeofkey==="number"){
returnkey;
}
thrownewDecodeError("The type of key must be string or number but "+typeofkey);
};
typeStackMapState={
type: typeofSTATE_MAP_KEY|typeofSTATE_MAP_VALUE;
size: number;
key: MapKeyType|null;
readCount: number;
map: Record<string,unknown>;
};
typeStackArrayState={
type: typeofSTATE_ARRAY;
size: number;
array: Array<unknown>;
position: number;
};
classStackPool{
privatereadonlystack: Array<StackState>=[];
privatestackHeadPosition=-1;
publicgetlength(): number{
returnthis.stackHeadPosition+1;
}
publictop(): StackState|undefined{
returnthis.stack[this.stackHeadPosition];
}
publicpushArrayState(size: number){
conststate=this.getUninitializedStateFromPool()asStackArrayState;
state.type=STATE_ARRAY;
state.position=0;
state.size=size;
state.array=newArray(size);
}
publicpushMapState(size: number){
conststate=this.getUninitializedStateFromPool()asStackMapState;
state.type=STATE_MAP_KEY;
state.readCount=0;
state.size=size;
state.map={};
}
privategetUninitializedStateFromPool(){
this.stackHeadPosition++;
if(this.stackHeadPosition===this.stack.length){
constpartialState: Partial<StackState>={
type: undefined,
size: 0,
array: undefined,
position: 0,
readCount: 0,
map: undefined,
key: null,
};
this.stack.push(partialStateasStackState);
}
returnthis.stack[this.stackHeadPosition];
}
publicrelease(state: StackState): void{
consttopStackState=this.stack[this.stackHeadPosition];
if(topStackState!==state){
thrownewError("Invalid stack state. Released state is not on top of the stack.");
}
if(state.type===STATE_ARRAY){
constpartialState=stateasPartial<StackArrayState>;
partialState.size=0;
partialState.array=undefined;
partialState.position=0;
partialState.type=undefined;
}
if(state.type===STATE_MAP_KEY||state.type===STATE_MAP_VALUE){
constpartialState=stateasPartial<StackMapState>;
partialState.size=0;
partialState.map=undefined;
partialState.readCount=0;
partialState.type=undefined;
}
this.stackHeadPosition--;
}
publicreset(): void{
this.stack.length=0;
this.stackHeadPosition=-1;
}
}
typeStackState=StackArrayState|StackMapState;
constHEAD_BYTE_REQUIRED=-1;
constEMPTY_VIEW=newDataView<ArrayBufferLike>(newArrayBuffer(0));
constEMPTY_BYTES=newUint8Array<ArrayBufferLike>(EMPTY_VIEW.buffer);
try{
// IE11: The spec says it should throw RangeError,
// IE11: but in IE11 it throws TypeError.
EMPTY_VIEW.getInt8(0);
}catch(e){
if(!(einstanceofRangeError)){
thrownewError(
"This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access",
);
}
}
constMORE_DATA=newRangeError("Insufficient data");
constsharedCachedKeyDecoder=newCachedKeyDecoder();
exportclassDecoder<ContextType=undefined>{
privatereadonlyextensionCodec: ExtensionCodecType<ContextType>;
privatereadonlycontext: ContextType;
privatereadonlyuseBigInt64: boolean;
privatereadonlyrawStrings: boolean;
privatereadonlymaxStrLength: number;
privatereadonlymaxBinLength: number;
privatereadonlymaxArrayLength: number;
privatereadonlymaxMapLength: number;
privatereadonlymaxExtLength: number;
privatereadonlykeyDecoder: KeyDecoder|null;
privatereadonlymapKeyConverter: (key: unknown)=>MapKeyType;
privatetotalPos=0;
privatepos=0;
privateview=EMPTY_VIEW;
privatebytes=EMPTY_BYTES;
privateheadByte=HEAD_BYTE_REQUIRED;
privatereadonlystack=newStackPool();
privateentered=false;
publicconstructor(options?: DecoderOptions<ContextType>){
this.extensionCodec=options?.extensionCodec??(ExtensionCodec.defaultCodecasExtensionCodecType<ContextType>);
this.context=(optionsas{context: ContextType}|undefined)?.contextasContextType;// needs a type assertion because EncoderOptions has no context property when ContextType is undefined
this.useBigInt64=options?.useBigInt64??false;
this.rawStrings=options?.rawStrings??false;
this.maxStrLength=options?.maxStrLength??UINT32_MAX;
this.maxBinLength=options?.maxBinLength??UINT32_MAX;
this.maxArrayLength=options?.maxArrayLength??UINT32_MAX;
this.maxMapLength=options?.maxMapLength??UINT32_MAX;
this.maxExtLength=options?.maxExtLength??UINT32_MAX;
this.keyDecoder=options?.keyDecoder!==undefined ? options.keyDecoder : sharedCachedKeyDecoder;
this.mapKeyConverter=options?.mapKeyConverter??mapKeyConverter;
}
privateclone(): Decoder<ContextType>{
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
returnnewDecoder({
extensionCodec: this.extensionCodec,
context: this.context,
useBigInt64: this.useBigInt64,
rawStrings: this.rawStrings,
maxStrLength: this.maxStrLength,
maxBinLength: this.maxBinLength,
maxArrayLength: this.maxArrayLength,
maxMapLength: this.maxMapLength,
maxExtLength: this.maxExtLength,
keyDecoder: this.keyDecoder,
}asany);
}
privatereinitializeState(){
this.totalPos=0;
this.headByte=HEAD_BYTE_REQUIRED;
this.stack.reset();
// view, bytes, and pos will be re-initialized in setBuffer()
}
privatesetBuffer(buffer: ArrayLike<number>|ArrayBufferView|ArrayBufferLike): void{
constbytes=ensureUint8Array(buffer);
this.bytes=bytes;
this.view=newDataView(bytes.buffer,bytes.byteOffset,bytes.byteLength);
this.pos=0;
}
privateappendBuffer(buffer: ArrayLike<number>|ArrayBufferView|ArrayBufferLike): void{
if(this.headByte===HEAD_BYTE_REQUIRED&&!this.hasRemaining(1)){
this.setBuffer(buffer);
}else{
constremainingData=this.bytes.subarray(this.pos);
constnewData=ensureUint8Array(buffer);
// concat remainingData + newData
constnewBuffer=newUint8Array(remainingData.length+newData.length);
newBuffer.set(remainingData);
newBuffer.set(newData,remainingData.length);
this.setBuffer(newBuffer);
}
}
privatehasRemaining(size: number){
returnthis.view.byteLength-this.pos>=size;
}
privatecreateExtraByteError(posToShow: number): Error{
const{ view, pos }=this;
returnnewRangeError(`Extra ${view.byteLength-pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
}
/**
* @throws {@link DecodeError}
* @throws {@link RangeError}
*/
publicdecode(buffer: ArrayLike<number>|ArrayBufferView|ArrayBufferLike): unknown{
if(this.entered){
constinstance=this.clone();
returninstance.decode(buffer);
}
try{
this.entered=true;
this.reinitializeState();
this.setBuffer(buffer);
constobject=this.doDecodeSync();
if(this.hasRemaining(1)){
throwthis.createExtraByteError(this.pos);
}
returnobject;
}finally{
this.entered=false;
}
}
public*decodeMulti(buffer: ArrayLike<number>|ArrayBufferView|ArrayBufferLike): Generator<unknown,void,unknown>{
if(this.entered){
constinstance=this.clone();
yield*instance.decodeMulti(buffer);
return;
}
try{
this.entered=true;
this.reinitializeState();
this.setBuffer(buffer);
while(this.hasRemaining(1)){
yieldthis.doDecodeSync();
}
}finally{
this.entered=false;
}
}
publicasyncdecodeAsync(stream: AsyncIterable<ArrayLike<number>|ArrayBufferView|ArrayBufferLike>): Promise<unknown>{
if(this.entered){
constinstance=this.clone();
returninstance.decodeAsync(stream);
}
try{
this.entered=true;
letdecoded=false;
letobject: unknown;
forawait(constbufferofstream){
if(decoded){
this.entered=false;
throwthis.createExtraByteError(this.totalPos);
}
this.appendBuffer(buffer);
try{
object=this.doDecodeSync();
decoded=true;
}catch(e){
if(!(einstanceofRangeError)){
throwe;// rethrow
}
// fallthrough
}
this.totalPos+=this.pos;
}
if(decoded){
if(this.hasRemaining(1)){
throwthis.createExtraByteError(this.totalPos);
}
returnobject;
}
const{ headByte, pos, totalPos }=this;
thrownewRangeError(
`Insufficient data in parsing ${prettyByte(headByte)} at ${totalPos} (${pos} in the current buffer)`,
);
}finally{
this.entered=false;
}
}
publicdecodeArrayStream(
stream: AsyncIterable<ArrayLike<number>|ArrayBufferView|ArrayBufferLike>,
): AsyncGenerator<unknown,void,unknown>{
returnthis.decodeMultiAsync(stream,true);
}
publicdecodeStream(stream: AsyncIterable<ArrayLike<number>|ArrayBufferView|ArrayBufferLike>): AsyncGenerator<unknown,void,unknown>{
returnthis.decodeMultiAsync(stream,false);
}
privateasync*decodeMultiAsync(stream: AsyncIterable<ArrayLike<number>|ArrayBufferView|ArrayBufferLike>,isArray: boolean): AsyncGenerator<unknown,void,unknown>{
if(this.entered){
constinstance=this.clone();
yield*instance.decodeMultiAsync(stream,isArray);
return;
}
try{
this.entered=true;
letisArrayHeaderRequired=isArray;
letarrayItemsLeft=-1;
forawait(constbufferofstream){
if(isArray&&arrayItemsLeft===0){
throwthis.createExtraByteError(this.totalPos);
}
this.appendBuffer(buffer);
if(isArrayHeaderRequired){
arrayItemsLeft=this.readArraySize();
isArrayHeaderRequired=false;
this.complete();
}
try{
while(true){
yieldthis.doDecodeSync();
if(--arrayItemsLeft===0){
break;
}
}
}catch(e){
if(!(einstanceofRangeError)){
throwe;// rethrow
}
// fallthrough
}
this.totalPos+=this.pos;
}
}finally{
this.entered=false;
}
}
privatedoDecodeSync(): unknown{
DECODE: while(true){
constheadByte=this.readHeadByte();
letobject: unknown;
if(headByte>=0xe0){
// negative fixint (111x xxxx) 0xe0 - 0xff
object=headByte-0x100;
}elseif(headByte<0xc0){
if(headByte<0x80){
// positive fixint (0xxx xxxx) 0x00 - 0x7f
object=headByte;
}elseif(headByte<0x90){
// fixmap (1000 xxxx) 0x80 - 0x8f
constsize=headByte-0x80;
if(size!==0){
this.pushMapState(size);
this.complete();
continue DECODE;
}else{
object={};
}
}elseif(headByte<0xa0){
// fixarray (1001 xxxx) 0x90 - 0x9f
constsize=headByte-0x90;
if(size!==0){
this.pushArrayState(size);
this.complete();
continue DECODE;
}else{
object=[];
}
}else{
// fixstr (101x xxxx) 0xa0 - 0xbf
constbyteLength=headByte-0xa0;
object=this.decodeString(byteLength,0);
}
}elseif(headByte===0xc0){
// nil
object=null;
}elseif(headByte===0xc2){
// false
object=false;
}elseif(headByte===0xc3){
// true
object=true;
}elseif(headByte===0xca){
// float 32
object=this.readF32();
}elseif(headByte===0xcb){
// float 64
object=this.readF64();
}elseif(headByte===0xcc){
// uint 8
object=this.readU8();
}elseif(headByte===0xcd){
// uint 16
object=this.readU16();
}elseif(headByte===0xce){
// uint 32
object=this.readU32();
}elseif(headByte===0xcf){
// uint 64
if(this.useBigInt64){
object=this.readU64AsBigInt();
}else{
object=this.readU64();
}
}elseif(headByte===0xd0){
// int 8
object=this.readI8();
}elseif(headByte===0xd1){
// int 16
object=this.readI16();
}elseif(headByte===0xd2){
// int 32
object=this.readI32();
}elseif(headByte===0xd3){
// int 64
if(this.useBigInt64){
object=this.readI64AsBigInt();
}else{
object=this.readI64();
}
}elseif(headByte===0xd9){
// str 8
constbyteLength=this.lookU8();
object=this.decodeString(byteLength,1);
}elseif(headByte===0xda){
// str 16
constbyteLength=this.lookU16();
object=this.decodeString(byteLength,2);
}elseif(headByte===0xdb){
// str 32
constbyteLength=this.lookU32();
object=this.decodeString(byteLength,4);
}elseif(headByte===0xdc){
// array 16
constsize=this.readU16();
if(size!==0){
this.pushArrayState(size);
this.complete();
continue DECODE;
}else{
object=[];
}
}elseif(headByte===0xdd){
// array 32
constsize=this.readU32();
if(size!==0){
this.pushArrayState(size);
this.complete();
continue DECODE;
}else{
object=[];
}
}elseif(headByte===0xde){
// map 16
constsize=this.readU16();
if(size!==0){
this.pushMapState(size);
this.complete();
continue DECODE;
}else{
object={};
}
}elseif(headByte===0xdf){
// map 32
constsize=this.readU32();
if(size!==0){
this.pushMapState(size);
this.complete();
continue DECODE;
}else{
object={};
}
}elseif(headByte===0xc4){
// bin 8
constsize=this.lookU8();
object=this.decodeBinary(size,1);
}elseif(headByte===0xc5){
// bin 16
constsize=this.lookU16();
object=this.decodeBinary(size,2);
}elseif(headByte===0xc6){
// bin 32
constsize=this.lookU32();
object=this.decodeBinary(size,4);
}elseif(headByte===0xd4){
// fixext 1
object=this.decodeExtension(1,0);
}elseif(headByte===0xd5){
// fixext 2
object=this.decodeExtension(2,0);
}elseif(headByte===0xd6){
// fixext 4
object=this.decodeExtension(4,0);
}elseif(headByte===0xd7){
// fixext 8
object=this.decodeExtension(8,0);
}elseif(headByte===0xd8){
// fixext 16
object=this.decodeExtension(16,0);
}elseif(headByte===0xc7){
// ext 8
constsize=this.lookU8();
object=this.decodeExtension(size,1);
}elseif(headByte===0xc8){
// ext 16
constsize=this.lookU16();
object=this.decodeExtension(size,2);
}elseif(headByte===0xc9){
// ext 32
constsize=this.lookU32();
object=this.decodeExtension(size,4);
}else{
thrownewDecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`);
}
this.complete();
conststack=this.stack;
while(stack.length>0){
// arrays and maps
conststate=stack.top()!;
if(state.type===STATE_ARRAY){
state.array[state.position]=object;
state.position++;
if(state.position===state.size){
object=state.array;
stack.release(state);
}else{
continue DECODE;
}
}elseif(state.type===STATE_MAP_KEY){
if(object==="__proto__"||object==="constructor"||object==="prototype"){
thrownewDecodeError(`The key ${object} is not allowed`);
}
state.key=this.mapKeyConverter(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){
object=state.map;
stack.release(state);
}else{
state.key=null;
state.type=STATE_MAP_KEY;
continue DECODE;
}
}
}
returnobject;
}
}
privatereadHeadByte(): number{
if(this.headByte===HEAD_BYTE_REQUIRED){
this.headByte=this.readU8();
// console.log("headByte", prettyByte(this.headByte));
}
returnthis.headByte;
}
privatecomplete(): void{
this.headByte=HEAD_BYTE_REQUIRED;
}
privatereadArraySize(): number{
constheadByte=this.readHeadByte();
switch(headByte){
case0xdc:
returnthis.readU16();
case0xdd:
returnthis.readU32();
default: {
if(headByte<0xa0){
returnheadByte-0x90;
}else{
thrownewDecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`);
}
}
}
}
privatepushMapState(size: number){
if(size>this.maxMapLength){
thrownewDecodeError(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
}
this.stack.pushMapState(size);
}
privatepushArrayState(size: number){
if(size>this.maxArrayLength){
thrownewDecodeError(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
}
this.stack.pushArrayState(size);
}
privatedecodeString(byteLength: number,headerOffset: number): string|Uint8Array{
if(!this.rawStrings||this.stateIsMapKey()){
returnthis.decodeUtf8String(byteLength,headerOffset);
}
returnthis.decodeBinary(byteLength,headerOffset);
}
/**
* @throws {@link RangeError}
*/
privatedecodeUtf8String(byteLength: number,headerOffset: number): string{
if(byteLength>this.maxStrLength){
thrownewDecodeError(
`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`,
);
}
if(this.bytes.byteLength<this.pos+headerOffset+byteLength){
throwMORE_DATA;
}
constoffset=this.pos+headerOffset;
letobject: string;
if(this.stateIsMapKey()&&this.keyDecoder?.canBeCached(byteLength)){
object=this.keyDecoder.decode(this.bytes,offset,byteLength);
}else{
object=utf8Decode(this.bytes,offset,byteLength);
}
this.pos+=headerOffset+byteLength;
returnobject;
}
privatestateIsMapKey(): boolean{
if(this.stack.length>0){
conststate=this.stack.top()!;
returnstate.type===STATE_MAP_KEY;
}
returnfalse;
}
/**
* @throws {@link RangeError}
*/
privatedecodeBinary(byteLength: number,headOffset: number): Uint8Array{
if(byteLength>this.maxBinLength){
thrownewDecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
}
if(!this.hasRemaining(byteLength+headOffset)){
throwMORE_DATA;
}
constoffset=this.pos+headOffset;
constobject=this.bytes.subarray(offset,offset+byteLength);
this.pos+=headOffset+byteLength;
returnobject;
}
privatedecodeExtension(size: number,headOffset: number): unknown{
if(size>this.maxExtLength){
thrownewDecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
}
constextType=this.view.getInt8(this.pos+headOffset);
constdata=this.decodeBinary(size,headOffset+1/* extType */);
returnthis.extensionCodec.decode(data,extType,this.context);
}
privatelookU8(){
returnthis.view.getUint8(this.pos);
}
privatelookU16(){
returnthis.view.getUint16(this.pos);
}
privatelookU32(){
returnthis.view.getUint32(this.pos);
}
privatereadU8(): number{
constvalue=this.view.getUint8(this.pos);
this.pos++;
returnvalue;
}
privatereadI8(): number{
constvalue=this.view.getInt8(this.pos);
this.pos++;
returnvalue;
}
privatereadU16(): number{
constvalue=this.view.getUint16(this.pos);
this.pos+=2;
returnvalue;
}
privatereadI16(): number{
constvalue=this.view.getInt16(this.pos);
this.pos+=2;
returnvalue;
}
privatereadU32(): number{
constvalue=this.view.getUint32(this.pos);
this.pos+=4;
returnvalue;
}
privatereadI32(): number{
constvalue=this.view.getInt32(this.pos);
this.pos+=4;
returnvalue;
}
privatereadU64(): number{
constvalue=getUint64(this.view,this.pos);
this.pos+=8;
returnvalue;
}
privatereadI64(): number{
constvalue=getInt64(this.view,this.pos);
this.pos+=8;
returnvalue;
}
privatereadU64AsBigInt(): bigint{
constvalue=this.view.getBigUint64(this.pos);
this.pos+=8;
returnvalue;
}
privatereadI64AsBigInt(): bigint{
constvalue=this.view.getBigInt64(this.pos);
this.pos+=8;
returnvalue;
}
privatereadF32(){
constvalue=this.view.getFloat32(this.pos);
this.pos+=4;
returnvalue;
}
privatereadF64(){
constvalue=this.view.getFloat64(this.pos);
this.pos+=8;
returnvalue;
}
}