-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
3615 lines (3583 loc) · 126 KB
/
index.js
File metadata and controls
3615 lines (3583 loc) · 126 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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true,
writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// node_modules/hextreme/index.mjs
var __defProp2 = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, {
enumerable: true,
configurable: true,
writable: true,
value
}) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp2(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp2(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var chunkBytes = 1008e3;
var littleEndian = new Uint8Array(new Uint16Array([258]).buffer)[0] === 2;
var td = new TextDecoder();
var te = new TextEncoder();
var hexCharsLower = te.encode("0123456789abcdef");
var hexCharsUpper = te.encode("0123456789ABCDEF");
var b64ChStd = te.encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
var b64ChPad = 61;
var b64ChUrl = b64ChStd.slice();
b64ChUrl[62] = 45;
b64ChUrl[63] = 95;
var ccl;
var ccu;
function _toHex(in8, { alphabet, scratchArr } = {}) {
if (!ccl) {
ccl = new Uint16Array(256);
ccu = new Uint16Array(256);
if (littleEndian) for (let i2 = 0; i2 < 256; i2++) {
ccl[i2] = hexCharsLower[i2 & 15] << 8 | hexCharsLower[i2 >>> 4];
ccu[i2] = hexCharsUpper[i2 & 15] << 8 | hexCharsUpper[i2 >>> 4];
}
else for (let i2 = 0; i2 < 256; i2++) {
ccl[i2] = hexCharsLower[i2 & 15] | hexCharsLower[i2 >>> 4] << 8;
ccu[i2] = hexCharsUpper[i2 & 15] | hexCharsUpper[i2 >>> 4] << 8;
}
}
if (in8.byteOffset % 4 !== 0) in8 = new Uint8Array(in8);
const len = in8.length, halfLen = len >>> 1, quarterLen = len >>> 2, out16 = scratchArr || new Uint16Array(len),
in32 = new Uint32Array(
in8.buffer,
in8.byteOffset,
quarterLen
), out32 = new Uint32Array(out16.buffer, out16.byteOffset, halfLen), cc = alphabet === "upper" ? ccu : ccl;
let i = 0, j = 0, v;
if (littleEndian) while (i < quarterLen) {
v = in32[i++];
out32[j++] = cc[v >>> 8 & 255] << 16 | cc[v & 255];
out32[j++] = cc[v >>> 24] << 16 | cc[v >>> 16 & 255];
}
else while (i < quarterLen) {
v = in32[i++];
out32[j++] = cc[v >>> 24] << 16 | cc[v >>> 16 & 255];
out32[j++] = cc[v >>> 8 & 255] << 16 | cc[v & 255];
}
i <<= 2;
while (i < len) out16[i] = cc[in8[i++]];
const hex = td.decode(out16.subarray(0, len));
return hex;
}
function _toHexChunked(d, options = {}) {
let hex = "", len = d.length, chunkWords = chunkBytes >>> 1, chunks = Math.ceil(len / chunkWords), scratchArr = new Uint16Array(
chunks > 1 ? chunkWords : len
);
for (let i = 0; i < chunks; i++) {
const start = i * chunkWords, end = start + chunkWords;
hex += _toHex(d.subarray(start, end), __spreadProps(__spreadValues({}, options), { scratchArr }));
}
return hex;
}
function toHex(d, options = {}) {
return options.alphabet !== "upper" && typeof d.toHex === "function" ? d.toHex() : _toHexChunked(d, options);
}
var vff = 26214;
var hl;
function _fromHex(s, { onInvalidInput, scratchArray: scratchArr, outArray: outArr, indexOffset } = {}) {
if (!hl) {
hl = new Uint8Array(vff + 1);
for (let l = 0; l < 22; l++) for (let r = 0; r < 22; r++) {
const cl = l + (l < 10 ? 48 : l < 16 ? 55 : 81), cr = r + (r < 10 ? 48 : r < 16 ? 55 : 81), vin = littleEndian ?
cr << 8 | cl : cr | cl << 8, vout = (l < 16 ? l : l - 6) << 4 | (r < 16 ? r : r - 6);
hl[vin] = vout;
}
}
const lax = onInvalidInput === "truncate", slen = s.length;
if (!lax && slen & 1) throw new Error("Hex input is an odd number of characters");
const bytelen = slen >>> 1, last7 = bytelen - 7, h16len = bytelen + 2, h16 = scratchArr || new Uint16Array(h16len),
h8 = new Uint8Array(
h16.buffer,
h16.byteOffset
), out = outArr || new Uint8Array(bytelen);
if (h16.length < h16len) throw new Error(`Wrong-sized scratch array supplied (was ${h16.length}, expected at\
least ${h16len})`);
if (out.length != bytelen) throw new Error(`Wrong-sized output array supplied (was ${out.length}, expected ${bytelen}\
)`);
te.encodeInto(s, h8);
let i = 0, ok = false;
e: {
let vin, vout;
while (i < last7) {
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
}
while (i < bytelen) {
vin = h16[i];
vout = hl[vin];
if (!vout && vin !== 12336) break e;
out[i++] = vout;
}
ok = true;
}
if (!ok && !lax) throw new Error(`Invalid pair in hex input at index ${(indexOffset || 0) + i << 1}`);
return i < bytelen ? out.subarray(0, i) : out;
}
function _fromHexChunked(s, { onInvalidInput, outArray } = {}) {
const lax = onInvalidInput === "truncate", slen = s.length;
if (!lax && slen & 1) throw new Error("Hex input is an odd number of characters");
const byteLength = slen >>> 1, chunkInts = chunkBytes >>> 1, chunksCount = Math.ceil(byteLength / chunkInts),
scratchArr = new Uint16Array(
(chunksCount > 1 ? chunkInts : byteLength) + 2
), outArr = outArray || new Uint8Array(byteLength);
if (outArr.length !== byteLength) throw new Error(`Provided output array is of wrong length: expected ${byteLength}\
, got ${outArr.length}`);
for (let i = 0; i < chunksCount; i++) {
const chunkStartByte = i * chunkInts, chunkEndByte = chunkStartByte + chunkInts, result = _fromHex(s.slice(
chunkStartByte << 1,
chunkEndByte << 1
), {
onInvalidInput,
scratchArray: scratchArr,
outArray: outArr.subarray(chunkStartByte, chunkEndByte),
indexOffset: chunkStartByte
});
if (lax && result.length < chunkEndByte - chunkStartByte) {
return outArr.subarray(0, chunkStartByte + result.length);
}
}
return outArr;
}
function fromHex(s, options = {}) {
if (typeof Uint8Array.fromHex === "function" && options.onInvalidInput !== "truncate" && !options.outArray) return Uint8Array.
fromHex(
s
);
return _fromHexChunked(s, options);
}
var chpairsStd;
var chpairsUrl;
function _toBase64(d, { omitPadding, alphabet, scratchArr } = {}) {
if (!chpairsStd) {
chpairsStd = new Uint16Array(4096);
if (littleEndian) for (let i2 = 0; i2 < 64; i2++) for (let j2 = 0; j2 < 64; j2++) chpairsStd[i2 << 6 | j2] =
b64ChStd[i2] | b64ChStd[j2] << 8;
else for (let i2 = 0; i2 < 64; i2++) for (let j2 = 0; j2 < 64; j2++) chpairsStd[i2 << 6 | j2] = b64ChStd[i2] <<
8 | b64ChStd[j2];
chpairsUrl = chpairsStd.slice();
if (littleEndian) {
for (let i2 = 0; i2 < 64; i2++) for (let j2 = 62; j2 < 64; j2++) chpairsUrl[i2 << 6 | j2] = b64ChUrl[i2] |
b64ChUrl[j2] << 8;
for (let i2 = 62; i2 < 64; i2++) for (let j2 = 0; j2 < 62; j2++) chpairsUrl[i2 << 6 | j2] = b64ChUrl[i2] |
b64ChUrl[j2] << 8;
} else {
for (let i2 = 0; i2 < 64; i2++) for (let j2 = 62; j2 < 64; j2++) chpairsUrl[i2 << 6 | j2] = b64ChUrl[i2] <<
8 | b64ChUrl[j2];
for (let i2 = 62; i2 < 64; i2++) for (let j2 = 0; j2 < 62; j2++) chpairsUrl[i2 << 6 | j2] = b64ChUrl[i2] <<
8 | b64ChUrl[j2];
}
}
if (d.byteOffset % 4 !== 0) d = new Uint8Array(d);
const urlsafe = alphabet === "base64url", ch = urlsafe ? b64ChUrl : b64ChStd, chpairs = urlsafe ? chpairsUrl :
chpairsStd, inlen = d.length, last2 = inlen - 2, inints = inlen >>> 2, intlast3 = inints - 3, d32 = new Uint32Array(
d.buffer, d.byteOffset, inints), outints = Math.ceil(inlen / 3), out = scratchArr || new Uint32Array(outints);
let i = 0, j = 0, u1, u2, u3, b1, b2, b3;
if (littleEndian) while (i < intlast3) {
u1 = d32[i++];
u2 = d32[i++];
u3 = d32[i++];
b1 = u1 & 255;
b2 = u1 >>> 8 & 255;
b3 = u1 >>> 16 & 255;
out[j++] = chpairs[b1 << 4 | b2 >>> 4] | chpairs[(b2 & 15) << 8 | b3] << 16;
b1 = u1 >>> 24;
b2 = u2 & 255;
b3 = u2 >>> 8 & 255;
out[j++] = chpairs[b1 << 4 | b2 >>> 4] | chpairs[(b2 & 15) << 8 | b3] << 16;
b1 = u2 >>> 16 & 255;
b2 = u2 >>> 24;
b3 = u3 & 255;
out[j++] = chpairs[b1 << 4 | b2 >>> 4] | chpairs[(b2 & 15) << 8 | b3] << 16;
b1 = u3 >>> 8 & 255;
b2 = u3 >>> 16 & 255;
b3 = u3 >>> 24;
out[j++] = chpairs[b1 << 4 | b2 >>> 4] | chpairs[(b2 & 15) << 8 | b3] << 16;
}
else while (i < intlast3) {
u1 = d32[i++];
u2 = d32[i++];
u3 = d32[i++];
out[j++] = chpairs[u1 >>> 20] << 16 | chpairs[u1 >>> 8 & 4095];
out[j++] = chpairs[(u1 & 255) << 4 | u2 >>> 28] << 16 | chpairs[u2 >>> 16 & 4095];
out[j++] = chpairs[u2 >>> 4 & 4095] << 16 | chpairs[(u2 & 15) << 8 | u3 >>> 24];
out[j++] = chpairs[u3 >>> 12 & 4095] << 16 | chpairs[u3 & 4095];
}
i = i << 2;
while (i < last2) {
b1 = d[i++];
b2 = d[i++];
b3 = d[i++];
out[j++] = chpairs[b1 << 4 | b2 >>> 4] << (littleEndian ? 0 : 16) | chpairs[(b2 & 15) << 8 | b3] << (littleEndian ?
16 : 0);
}
if (i === inlen) return td.decode(out);
b1 = d[i++];
b2 = d[i++];
out[j++] = chpairs[b1 << 4 | (b2 || 0) >>> 4] << (littleEndian ? 0 : 16) | // first 16 bits (no padding)
(b2 === void 0 ? b64ChPad : ch[((b2 || 0) & 15) << 2]) << (littleEndian ? 16 : 8) | // next 8 bits
b64ChPad << (littleEndian ? 24 : 0);
if (!omitPadding) return td.decode(out);
let out8 = new Uint8Array(out.buffer, 0, (outints << 2) - (b2 === void 0 ? 2 : 1));
return td.decode(out8);
}
function _toBase64Chunked(d, options = {}) {
const inBytes = d.length, outInts = Math.ceil(inBytes / 3), outChunkInts = chunkBytes >>> 2, chunksCount = Math.
ceil(outInts / outChunkInts), inChunkBytes = outChunkInts * 3, scratchArr = new Uint32Array(chunksCount > 1 ?
outChunkInts : outInts);
let b64 = "";
for (let i = 0; i < chunksCount; i++) {
const startInBytes = i * inChunkBytes, endInBytes = startInBytes + inChunkBytes, startOutInts = i * outChunkInts,
endOutInts = Math.min(startOutInts + outChunkInts, outInts);
b64 += _toBase64(d.subarray(startInBytes, endInBytes), __spreadProps(__spreadValues({}, options), {
scratchArr: scratchArr.subarray(0, endOutInts - startOutInts)
}));
}
return b64;
}
function toBase64(d, options = {}) {
return typeof d.toBase64 === "function" ? d.toBase64(options) : _toBase64Chunked(d, options);
}
var vzz = 31354;
var stdWordLookup;
var urlWordLookup;
var anyWordLookup;
var stdByteLookup;
var urlByteLookup;
var anyByteLookup;
function _fromBase64(s, { alphabet, onInvalidInput } = {}) {
const lax = onInvalidInput === "skip";
if (!stdWordLookup && alphabet !== "base64url" && alphabet !== "base64any") {
stdWordLookup = new Uint16Array(vzz + 1);
for (let l = 0; l < 64; l++) for (let r = 0; r < 64; r++) {
const cl = b64ChStd[l], cr = b64ChStd[r], vin = littleEndian ? cr << 8 | cl : cr | cl << 8, vout = l << 6 |
r;
stdWordLookup[vin] = vout;
}
}
if (!urlWordLookup && alphabet === "base64url") {
urlWordLookup = new Uint16Array(vzz + 1);
for (let l = 0; l < 64; l++) for (let r = 0; r < 64; r++) {
const cl = b64ChUrl[l], cr = b64ChUrl[r], vin = littleEndian ? cr << 8 | cl : cr | cl << 8, vout = l << 6 |
r;
urlWordLookup[vin] = vout;
}
}
if (!anyWordLookup && alphabet === "base64any") {
anyWordLookup = new Uint16Array(vzz + 1);
for (let l = 0; l < 64; l++) for (let r = 0; r < 64; r++) {
const cl = b64ChStd[l], cr = b64ChStd[r], vin = littleEndian ? cr << 8 | cl : cr | cl << 8, vout = l << 6 |
r;
anyWordLookup[vin] = vout;
if (l > 61 || r > 61) {
const cl2 = b64ChUrl[l], cr2 = b64ChUrl[r], vin2 = littleEndian ? cr2 << 8 | cl2 : cr2 | cl2 << 8;
anyWordLookup[vin2] = vout;
}
}
}
if (!stdByteLookup) {
stdByteLookup = new Uint8Array(256).fill(66);
urlByteLookup = new Uint8Array(256).fill(66);
anyByteLookup = new Uint8Array(256).fill(66);
stdByteLookup[b64ChPad] = urlByteLookup[b64ChPad] = anyByteLookup[b64ChPad] = 65;
stdByteLookup[9] = stdByteLookup[10] = stdByteLookup[13] = stdByteLookup[32] = // tab, \r, \n, space
urlByteLookup[9] = urlByteLookup[10] = urlByteLookup[13] = urlByteLookup[32] = anyByteLookup[9] = anyByteLookup[10] =
anyByteLookup[13] = anyByteLookup[32] = 64;
for (let i2 = 0; i2 < 64; i2++) {
const chStdI = b64ChStd[i2], chUrlI = b64ChUrl[i2];
stdByteLookup[chStdI] = urlByteLookup[chUrlI] = anyByteLookup[chStdI] = anyByteLookup[chUrlI] = i2;
}
}
const inBytes = te.encode(s), inBytesLen = inBytes.length, inIntsLen = inBytesLen >>> 2, inInts = new Uint32Array(
inBytes.buffer,
inBytes.byteOffset,
inIntsLen
), last3 = inIntsLen - 3, maxOutBytesLen = inIntsLen * 3 + inBytesLen % 4, outBytes = new Uint8Array(
maxOutBytesLen
), outInts = new Uint32Array(outBytes.buffer, 0, maxOutBytesLen >>> 2), wl = alphabet === "base64url" ? urlWordLookup :
alphabet === "base64any" ? anyWordLookup : stdWordLookup, bl = alphabet === "base64url" ? urlByteLookup : alphabet ===
"base64any" ? anyByteLookup : stdByteLookup;
let i = 0, j = 0, inInt, inL, inR, vL1, vR1, vL2, vR2, vL3, vR3, vL4, vR4;
if (littleEndian) while (i < last3) {
inInt = inInts[i++];
inL = inInt & 65535;
inR = inInt >>> 16;
vL1 = wl[inL];
vR1 = wl[inR];
if (!((vL1 || inL === 16705) && (vR1 || inR === 16705))) {
i -= 1;
break;
}
inInt = inInts[i++];
inL = inInt & 65535;
inR = inInt >>> 16;
vL2 = wl[inL];
vR2 = wl[inR];
if (!((vL2 || inL === 16705) && (vR2 || inR === 16705))) {
i -= 2;
break;
}
inInt = inInts[i++];
inL = inInt & 65535;
inR = inInt >>> 16;
vL3 = wl[inL];
vR3 = wl[inR];
if (!((vL3 || inL === 16705) && (vR3 || inR === 16705))) {
i -= 3;
break;
}
inInt = inInts[i++];
inL = inInt & 65535;
inR = inInt >>> 16;
vL4 = wl[inL];
vR4 = wl[inR];
if (!((vL4 || inL === 16705) && (vR4 || inR === 16705))) {
i -= 4;
break;
}
outInts[j++] = vL1 >>> 4 | (vL1 & 15) << 12 | vR1 & 65280 | (vR1 & 255) << 16 | (vL2 & 4080) << 20;
outInts[j++] = (vL2 & 15) << 4 | (vR2 & 65280) >>> 8 | (vR2 & 255) << 8 | (vL3 & 4080) << 12 | (vL3 & 15) <<
28 | (vR3 & 65280) << 16;
outInts[j++] = vR3 & 255 | (vL4 & 4080) << 4 | (vL4 & 15) << 20 | (vR4 & 3840) << 8 | vR4 << 24;
}
else while (i < last3) {
inInt = inInts[i++];
inL = inInt >>> 16;
inR = inInt & 65535;
vL1 = wl[inL];
vR1 = wl[inR];
if (!((vL1 || inL === 16705) && (vR1 || inR === 16705))) {
i -= 1;
break;
}
inInt = inInts[i++];
inL = inInt >>> 16;
inR = inInt & 65535;
vL2 = wl[inL];
vR2 = wl[inR];
if (!((vL2 || inL === 16705) && (vR2 || inR === 16705))) {
i -= 2;
break;
}
inInt = inInts[i++];
inL = inInt >>> 16;
inR = inInt & 65535;
vL3 = wl[inL];
vR3 = wl[inR];
if (!((vL3 || inL === 16705) && (vR3 || inR === 16705))) {
i -= 3;
break;
}
inInt = inInts[i++];
inL = inInt >>> 16;
inR = inInt & 65535;
vL4 = wl[inL];
vR4 = wl[inR];
if (!((vL4 || inL === 16705) && (vR4 || inR === 16705))) {
i -= 4;
break;
}
outInts[j++] = vL1 << 20 | vR1 << 8 | vL2 >>> 4;
outInts[j++] = (vL2 & 15) << 28 | vR2 << 16 | vL3 << 4 | vR3 >>> 8;
outInts[j++] = (vR3 & 255) << 24 | vL4 << 12 | vR4;
}
i <<= 2;
j <<= 2;
if (i === inBytesLen) return outBytes;
let i0 = i, ok = false;
e: {
if (lax) while (i < inBytesLen) {
i0 = i;
while ((vL1 = bl[inBytes[i++]]) > 63) if (vL1 === 65) ok = true;
while ((vL2 = bl[inBytes[i++]]) > 63) if (vL2 === 65) ok = true;
while ((vL3 = bl[inBytes[i++]]) > 63) if (vL3 === 65) ok = true;
while ((vL4 = bl[inBytes[i++]]) > 63) if (vL4 === 65) ok = true;
outBytes[j++] = vL1 << 2 | vL2 >>> 4;
outBytes[j++] = (vL2 << 4 | vL3 >>> 2) & 255;
outBytes[j++] = (vL3 << 6 | vL4) & 255;
if (ok) break;
}
else while (i < inBytesLen) {
i0 = i;
while ((vL1 = bl[inBytes[i++]]) > 63) if (vL1 === 66) break e;
else if (vL1 === 65) ok = true;
while ((vL2 = bl[inBytes[i++]]) > 63) if (vL2 === 66) break e;
else if (vL2 === 65) ok = true;
while ((vL3 = bl[inBytes[i++]]) > 63) if (vL3 === 66) break e;
else if (vL3 === 65) ok = true;
while ((vL4 = bl[inBytes[i++]]) > 63) if (vL4 === 66) break e;
else if (vL4 === 65) ok = true;
outBytes[j++] = vL1 << 2 | vL2 >>> 4;
outBytes[j++] = (vL2 << 4 | vL3 >>> 2) & 255;
outBytes[j++] = (vL3 << 6 | vL4) & 255;
if (ok) break;
}
ok = true;
}
if (!ok) throw new Error(`Invalid character in base64 at index ${i - 1}`);
let validChars = 0;
for (i = i0; i < inBytesLen; i++) {
const v = bl[inBytes[i]];
if (v < 64) validChars++;
if (v === 65) break;
}
if (!lax) for (i = i0; i < inBytesLen; i++) {
const v = bl[inBytes[i]];
if (v > 65) throw new Error(`Invalid character in base64 after padding`);
}
const truncateBytes = { 4: 0, 3: 1, 2: 2, 1: 3, 0: 3 }[validChars];
return outBytes.subarray(0, j - truncateBytes);
}
function fromBase64(s, options = {}) {
if (typeof Uint8Array.fromBase64 === "function" && options.onInvalidInput !== "skip" && options.alphabet !==
"base64any") return Uint8Array.fromBase64(s, options);
return _fromBase64(s, options);
}
// src/util/array.ts
function concat(...arrs) {
if (arrs.length === 1 && arrs[0] instanceof Uint8Array) return arrs[0];
const length = arrs.reduce((memo, arr) => memo + arr.length, 0);
const result = new Uint8Array(length);
let offset = 0;
for (const arr of arrs) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
function equal(a, b) {
const aLength = a.length;
if (aLength !== b.length) return false;
for (let i = 0; i < aLength; i++) if (a[i] !== b[i]) return false;
return true;
}
var GrowableData = class {
constructor() {
__publicField(this, "length");
__publicField(this, "data");
this.length = 0;
this.data = new Uint8Array();
}
append(newData) {
const newDataLength = newData.length;
if (this.length + newDataLength > this.data.length) {
const prevData = this.data;
this.data = new Uint8Array(this.length * 2 + newDataLength);
this.data.set(prevData);
}
this.data.set(newData, this.length);
this.length += newData.length;
}
getData() {
return this.data.subarray(0, this.length);
}
};
// src/presentation/appearance.ts
var indentChars = "\xB7\xB7 ";
// src/util/bytes.ts
var initialSize = 256;
var growthFactor = 2;
var txtEnc = new TextEncoder();
var txtDec = new TextDecoder();
var emptyArray = new Uint8Array(0);
var hexLookup = [];
for (let i = 0; i < 256; i++) hexLookup[i] = i.toString(16).padStart(2, "0") + " ";
var Bytes = class {
/**
* @param data -
* * If data is a `Uint8Array`, this is the initial data
* * If data is a `number`, this is the initial size in bytes (all zeroes)
* * If data is a `function`, this function is called to retrieve data when required
*/
constructor(data, indent = 0) {
this.indent = indent;
__publicField(this, "fetchFn");
__publicField(this, "endOfReadableData");
// how much data exists to read (not used for writing)
__publicField(this, "offset");
// current read/write cursor
__publicField(this, "dataView");
__publicField(this, "data");
__publicField(this, "comments");
__publicField(this, "indents");
__publicField(this, "fetchPoints");
this.endOfReadableData = this.offset = 0;
this.comments = {};
this.indents = { 0: indent };
this.fetchPoints = /* @__PURE__ */ new Set();
if (typeof data === "number") {
this.data = new Uint8Array(data);
} else if (data === void 0 || typeof data === "function") {
this.data = emptyArray;
this.fetchFn = data;
} else {
this.data = data;
this.endOfReadableData = data.length;
}
this.dataView = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
}
changeIndent(indentDelta) {
this.indent += indentDelta;
this.indents[this.offset] = this.indent;
}
readRemaining() {
return this.endOfReadableData - this.offset;
}
resizeTo(newSize) {
const newData = new Uint8Array(newSize);
newData.set(this.data);
this.data = newData;
this.dataView = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
}
async ensureReadAvailable(bytes) {
if (bytes <= this.readRemaining()) return;
if (this.fetchFn === void 0) throw new Error("Not enough data and no read function supplied");
const freeSpace = this.data.length - this.endOfReadableData;
if (bytes > freeSpace) {
const newSize = Math.max(
initialSize,
this.data.length * growthFactor,
this.endOfReadableData + bytes
);
this.resizeTo(newSize);
}
const newData = await this.fetchFn(bytes);
if (newData === void 0 || newData.length < bytes) {
const e = new Error(`Not enough data: requested ${bytes} byte(s), received ${newData === void 0 ? "EOF" :
`${newData.length} byte(s)`}`);
e._bytes_error_reason = "EOF";
throw e;
}
for (const fetchPoint of newData.fetchPoints ?? []) this.fetchPoints.add(fetchPoint + this.endOfReadableData);
this.data.set(newData, this.endOfReadableData);
this.endOfReadableData += newData.length;
}
ensureWriteAvailable(bytes) {
if (this.offset + bytes < this.data.length) return;
const newSize = Math.max(
initialSize,
this.data.length * growthFactor,
this.offset + bytes
);
this.resizeTo(newSize);
}
expectLength(length, indentDelta = 1) {
const startOffset = this.offset;
const endOffset = startOffset + length;
this.changeIndent(indentDelta);
return [
() => {
this.changeIndent(-indentDelta);
if (this.offset !== endOffset) throw new Error(`${length} bytes expected but ${this.offset - startOffset}\
advanced`);
},
() => endOffset - this.offset
];
}
comment(s, offset = this.offset) {
if (true) throw new Error("No comments should be emitted outside of chatty mode");
const existing = this.comments[offset];
const result = (existing === void 0 ? "" : existing + " ") + s;
this.comments[offset] = result;
return this;
}
lengthComment(length, comment, inclusive = false) {
return length === 1 ? `${length} byte${comment ? ` of ${comment}` : ""} ${inclusive ? "starts here" : "fol\
lows"}` : `${length === 0 ? "no" : length} bytes${comment ? ` of ${comment}` : ""} ${inclusive ? "start here" :
"follow"}`;
}
// reading
async subarrayForRead(length) {
await this.ensureReadAvailable(length);
return this.data.subarray(this.offset, this.offset += length);
}
async skipRead(length, comment) {
await this.ensureReadAvailable(length);
this.offset += length;
if (comment) this.comment(comment);
return this;
}
async readBytes(length) {
await this.ensureReadAvailable(length);
return this.data.slice(this.offset, this.offset += length);
}
async readUTF8String(length) {
await this.ensureReadAvailable(length);
const bytes = await this.subarrayForRead(length);
const s = txtDec.decode(bytes);
return s;
}
async readUTF8StringNullTerminated() {
let i = 0;
while (true) {
await this.ensureReadAvailable(i + 1);
const charCode = this.data[this.offset + i];
if (charCode === 0) break;
i++;
}
;
const str = await this.readUTF8String(i);
await this.expectUint8(0, "end of string");
return str;
}
async readUint8(comment) {
await this.ensureReadAvailable(1);
const result = this.dataView.getUint8(this.offset);
this.offset += 1;
if (0) this.comment(comment.replace(/%/g, String(result)));
return result;
}
async readUint16(comment) {
await this.ensureReadAvailable(2);
const result = this.dataView.getUint16(this.offset);
this.offset += 2;
if (0) this.comment(comment.replace(/%/g, String(result)));
return result;
}
async readUint24(comment) {
const msb = await this.readUint8();
const lsbs = await this.readUint16();
const result = (msb << 16) + lsbs;
if (0) this.comment(comment.replace(/%/g, String(result)));
return result;
}
async readUint32(comment) {
await this.ensureReadAvailable(4);
const result = this.dataView.getUint32(this.offset);
this.offset += 4;
if (0) this.comment(comment.replace(/%/g, String(result)));
return result;
}
async expectBytes(expected, comment) {
await this.ensureReadAvailable(expected.length);
const actual = await this.readBytes(expected.length);
if (0) this.comment(comment);
if (!equal(actual, expected)) throw new Error("Unexpected bytes");
}
async expectUint8(expectedValue, comment) {
const actualValue = await this.readUint8();
if (0) this.comment(comment);
if (actualValue !== expectedValue) throw new Error(`Expected u8 ${expectedValue}, got ${actualValue}`);
}
async expectUint16(expectedValue, comment) {
const actualValue = await this.readUint16();
if (0) this.comment(comment);
if (actualValue !== expectedValue) throw new Error(`Expected u16 ${expectedValue}, got ${actualValue}`);
}
async expectUint24(expectedValue, comment) {
const actualValue = await this.readUint24();
if (0) this.comment(comment);
if (actualValue !== expectedValue) throw new Error(`Expected u24 ${expectedValue}, got ${actualValue}`);
}
async expectUint32(expectedValue, comment) {
const actualValue = await this.readUint32();
if (0) this.comment(comment);
if (actualValue !== expectedValue) throw new Error(`Expected u32 ${expectedValue}, got ${actualValue}`);
}
async expectReadLength(length, indentDelta = 1) {
await this.ensureReadAvailable(length);
return this.expectLength(length, indentDelta);
}
async expectLengthUint8(comment) {
const length = await this.readUint8();
return this.expectReadLength(length);
}
async expectLengthUint16(comment) {
const length = await this.readUint16();
return this.expectReadLength(length);
}
async expectLengthUint24(comment) {
const length = await this.readUint24();
return this.expectReadLength(length);
}
async expectLengthUint32(comment) {
const length = await this.readUint32();
return this.expectReadLength(length);
}
async expectLengthUint8Incl(comment) {
const length = await this.readUint8();
return this.expectReadLength(length - 1);
}
async expectLengthUint16Incl(comment) {
const length = await this.readUint16();
return this.expectReadLength(length - 2);
}
async expectLengthUint24Incl(comment) {
const length = await this.readUint24();
return this.expectReadLength(length - 3);
}
async expectLengthUint32Incl(comment) {
const length = await this.readUint32();
return this.expectReadLength(length - 4);
}
// writing
subarrayForWrite(length) {
this.ensureWriteAvailable(length);
return this.data.subarray(this.offset, this.offset += length);
}
skipWrite(length, comment) {
this.ensureWriteAvailable(length);
this.offset += length;
if (comment) this.comment(comment);
return this;
}
writeBytes(bytes) {
this.ensureWriteAvailable(bytes.length);
this.data.set(bytes, this.offset);
this.offset += bytes.length;
return this;
}
writeUTF8String(s) {
const bytes = txtEnc.encode(s);
this.writeBytes(bytes);
return this;
}
writeUTF8StringNullTerminated(s) {
const bytes = txtEnc.encode(s);
this.writeBytes(bytes);
this.writeUint8(0);
return this;
}
writeUint8(value, comment) {
this.ensureWriteAvailable(1);
this.dataView.setUint8(this.offset, value);
this.offset += 1;
if (0) this.comment(comment);
return this;
}
writeUint16(value, comment) {
this.ensureWriteAvailable(2);
this.dataView.setUint16(this.offset, value);
this.offset += 2;
if (0) this.comment(comment);
return this;
}
writeUint24(value, comment) {
this.writeUint8((value & 16711680) >> 16);
this.writeUint16(value & 65535, comment);
return this;
}
writeUint32(value, comment) {
this.ensureWriteAvailable(4);
this.dataView.setUint32(this.offset, value);
this.offset += 4;
if (0) this.comment(comment);
return this;
}
// forward-looking lengths
_writeLengthGeneric(lengthBytes, inclusive, comment) {
this.ensureWriteAvailable(lengthBytes);
const startOffset = this.offset;
this.offset += lengthBytes;
const endOffset = this.offset;
this.changeIndent(1);
return () => {
const length = this.offset - (inclusive ? startOffset : endOffset);
switch (lengthBytes) {
case 1:
this.dataView.setUint8(startOffset, length);
break;
case 2:
this.dataView.setUint16(startOffset, length);
break;
case 3:
this.dataView.setUint8(startOffset, (length & 16711680) >> 16);
this.dataView.setUint16(startOffset + 1, length & 65535);
break;
case 4:
this.dataView.setUint32(startOffset, length);
break;
default:
throw new Error(`Invalid length for length field: ${lengthBytes}`);
}
this.changeIndent(-1);
};
}
writeLengthUint8(comment) {
return this._writeLengthGeneric(1, false, comment);
}
writeLengthUint16(comment) {
return this._writeLengthGeneric(2, false, comment);
}
writeLengthUint24(comment) {
return this._writeLengthGeneric(3, false, comment);
}
writeLengthUint32(comment) {
return this._writeLengthGeneric(4, false, comment);
}
writeLengthUint8Incl(comment) {
return this._writeLengthGeneric(1, true, comment);
}
writeLengthUint16Incl(comment) {
return this._writeLengthGeneric(2, true, comment);
}
writeLengthUint24Incl(comment) {
return this._writeLengthGeneric(3, true, comment);
}
writeLengthUint32Incl(comment) {
return this._writeLengthGeneric(4, true, comment);
}
expectWriteLength(length, indentDelta = 1) {
this.ensureWriteAvailable(length);
return this.expectLength(length, indentDelta);
}
// output
array() {
return this.data.subarray(0, this.offset);
}
commentedString(all = false) {
let indent = this.indents[0] ?? 0;
let s = indentChars.repeat(indent);
const len = all ? this.data.length : this.offset;
for (let i = 0; i < len; i++) {
s += hexLookup[this.data[i]];
const comment = this.comments[i + 1];
indent = this.indents[i + 1] ?? indent;
if (comment) {
s += ` ${comment}`;
if (i < len - 1) s += `
${indentChars.repeat(indent)}`;
}
if (this.fetchPoints.has(i + 1)) s += "\n--- next TLS record ---\n";
}
return s;
}
};
// src/util/cryptoRandom.ts
var cryptoPromise = typeof crypto !== "undefined" ? Promise.resolve(crypto) : (
// browsers and Node 19+
import("crypto").then((c) => c.webcrypto)
);
async function getRandomValues(...args) {
const c = await cryptoPromise;
return c.getRandomValues(...args);
}
// src/tls/makeClientHello.ts
async function makeClientHello(host, publicKey, sessionId, useSNI = true, protocolsForALPN) {
const h = new Bytes();
h.writeUint8(22, 0);
h.writeUint16(769, 0);
const endRecordHeader = h.writeLengthUint16("TLS record");
h.writeUint8(1, 0);
const endHandshakeHeader = h.writeLengthUint24();
h.writeUint16(771, 0);
await getRandomValues(h.subarrayForWrite(32));
const endSessionId = h.writeLengthUint8(0);
h.writeBytes(sessionId);
endSessionId();
const endCiphers = h.writeLengthUint16(0);
h.writeUint16(4865, 0);
endCiphers();
const endCompressionMethods = h.writeLengthUint8(0);
h.writeUint8(0, 0);
endCompressionMethods();
const endExtensions = h.writeLengthUint16(0);
if (useSNI) {
h.writeUint16(0, 0);
const endSNIExt = h.writeLengthUint16(0);
const endSNI = h.writeLengthUint16(0);
h.writeUint8(0, 0);
const endHostname = h.writeLengthUint16(0);
h.writeUTF8String(host);
endHostname();
endSNI();
endSNIExt();
}
if (protocolsForALPN) {
h.writeUint16(16, 0);
const endALPNExt = h.writeLengthUint16(0);
const endALPN = h.writeLengthUint16(0);
for (const protocol of protocolsForALPN) {
const endProtocol = h.writeLengthUint8(0);
h.writeUTF8String(protocol);
endProtocol();
}
endALPN();
endALPNExt();
}
h.writeUint16(11, 0);
const endFormatTypesExt = h.writeLengthUint16(0);
const endFormatTypes = h.writeLengthUint8(0);
h.writeUint8(0, 0);
endFormatTypes();
endFormatTypesExt();
h.writeUint16(10, 0);
const endGroupsExt = h.writeLengthUint16(0);
const endGroups = h.writeLengthUint16(0);
h.writeUint16(23, 0);
endGroups();
endGroupsExt();
h.writeUint16(13, 0);
const endSigsExt = h.writeLengthUint16(0);
const endSigs = h.writeLengthUint16(0);
h.writeUint16(1027, 0);
h.writeUint16(2052, 0);
endSigs();
endSigsExt();
h.writeUint16(43, 0);
const endVersionsExt = h.writeLengthUint16(0);
const endVersions = h.writeLengthUint8(0);
h.writeUint16(772, 0);
endVersions();
endVersionsExt();
h.writeUint16(51, 0);
const endKeyShareExt = h.writeLengthUint16(0);
const endKeyShares = h.writeLengthUint16(0);
h.writeUint16(23, 0);
const endKeyShare = h.writeLengthUint16(0);
if (0) {
h.writeUint8(publicKey[0], "legacy point format: always 4, which means uncompressed ([RFC 8446 \xA74.2.8.2](h\
ttps://datatracker.ietf.org/doc/html/rfc8446#section-4.2.8.2) and [RFC 8422 \xA75.4.1](https://datatracker.ietf.o\
rg/doc/html/rfc8422#section-5.4.1))");
h.writeBytes(publicKey.subarray(1, 33));
h.comment("x coordinate");
h.writeBytes(publicKey.subarray(33, 65));
h.comment("y coordinate");
} else {
h.writeBytes(publicKey);
}
endKeyShare();
endKeyShares();
endKeyShareExt();
endExtensions();
endHandshakeHeader();
endRecordHeader();
return h;
}
// src/util/hex.ts