grpc-libp2p-client 0.0.1

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.
@@ -0,0 +1,1199 @@
1
+ function coerce(o) {
2
+ if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array')
3
+ return o;
4
+ if (o instanceof ArrayBuffer)
5
+ return new Uint8Array(o);
6
+ if (ArrayBuffer.isView(o)) {
7
+ return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
8
+ }
9
+ throw new Error('Unknown type, must be binary type');
10
+ }
11
+ function fromString(str) {
12
+ return new TextEncoder().encode(str);
13
+ }
14
+ function toString$1(b) {
15
+ return new TextDecoder().decode(b);
16
+ }
17
+
18
+ /* eslint-disable */
19
+ // base-x encoding / decoding
20
+ // Copyright (c) 2018 base-x contributors
21
+ // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
22
+ // Distributed under the MIT software license, see the accompanying
23
+ // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
24
+ /**
25
+ * @param {string} ALPHABET
26
+ * @param {any} name
27
+ */
28
+ function base(ALPHABET, name) {
29
+ if (ALPHABET.length >= 255) {
30
+ throw new TypeError('Alphabet too long');
31
+ }
32
+ var BASE_MAP = new Uint8Array(256);
33
+ for (var j = 0; j < BASE_MAP.length; j++) {
34
+ BASE_MAP[j] = 255;
35
+ }
36
+ for (var i = 0; i < ALPHABET.length; i++) {
37
+ var x = ALPHABET.charAt(i);
38
+ var xc = x.charCodeAt(0);
39
+ if (BASE_MAP[xc] !== 255) {
40
+ throw new TypeError(x + ' is ambiguous');
41
+ }
42
+ BASE_MAP[xc] = i;
43
+ }
44
+ var BASE = ALPHABET.length;
45
+ var LEADER = ALPHABET.charAt(0);
46
+ var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
47
+ var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
48
+ /**
49
+ * @param {any[] | Iterable<number>} source
50
+ */
51
+ function encode(source) {
52
+ // @ts-ignore
53
+ if (source instanceof Uint8Array)
54
+ ;
55
+ else if (ArrayBuffer.isView(source)) {
56
+ source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
57
+ }
58
+ else if (Array.isArray(source)) {
59
+ source = Uint8Array.from(source);
60
+ }
61
+ if (!(source instanceof Uint8Array)) {
62
+ throw new TypeError('Expected Uint8Array');
63
+ }
64
+ if (source.length === 0) {
65
+ return '';
66
+ }
67
+ // Skip & count leading zeroes.
68
+ var zeroes = 0;
69
+ var length = 0;
70
+ var pbegin = 0;
71
+ var pend = source.length;
72
+ while (pbegin !== pend && source[pbegin] === 0) {
73
+ pbegin++;
74
+ zeroes++;
75
+ }
76
+ // Allocate enough space in big-endian base58 representation.
77
+ var size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
78
+ var b58 = new Uint8Array(size);
79
+ // Process the bytes.
80
+ while (pbegin !== pend) {
81
+ var carry = source[pbegin];
82
+ // Apply "b58 = b58 * 256 + ch".
83
+ var i = 0;
84
+ for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
85
+ carry += (256 * b58[it1]) >>> 0;
86
+ b58[it1] = (carry % BASE) >>> 0;
87
+ carry = (carry / BASE) >>> 0;
88
+ }
89
+ if (carry !== 0) {
90
+ throw new Error('Non-zero carry');
91
+ }
92
+ length = i;
93
+ pbegin++;
94
+ }
95
+ // Skip leading zeroes in base58 result.
96
+ var it2 = size - length;
97
+ while (it2 !== size && b58[it2] === 0) {
98
+ it2++;
99
+ }
100
+ // Translate the result into a string.
101
+ var str = LEADER.repeat(zeroes);
102
+ for (; it2 < size; ++it2) {
103
+ str += ALPHABET.charAt(b58[it2]);
104
+ }
105
+ return str;
106
+ }
107
+ /**
108
+ * @param {string | string[]} source
109
+ */
110
+ function decodeUnsafe(source) {
111
+ if (typeof source !== 'string') {
112
+ throw new TypeError('Expected String');
113
+ }
114
+ if (source.length === 0) {
115
+ return new Uint8Array();
116
+ }
117
+ var psz = 0;
118
+ // Skip leading spaces.
119
+ if (source[psz] === ' ') {
120
+ return;
121
+ }
122
+ // Skip and count leading '1's.
123
+ var zeroes = 0;
124
+ var length = 0;
125
+ while (source[psz] === LEADER) {
126
+ zeroes++;
127
+ psz++;
128
+ }
129
+ // Allocate enough space in big-endian base256 representation.
130
+ var size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
131
+ var b256 = new Uint8Array(size);
132
+ // Process the characters.
133
+ while (source[psz]) {
134
+ // Decode character
135
+ var carry = BASE_MAP[source.charCodeAt(psz)];
136
+ // Invalid character
137
+ if (carry === 255) {
138
+ return;
139
+ }
140
+ var i = 0;
141
+ for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
142
+ carry += (BASE * b256[it3]) >>> 0;
143
+ b256[it3] = (carry % 256) >>> 0;
144
+ carry = (carry / 256) >>> 0;
145
+ }
146
+ if (carry !== 0) {
147
+ throw new Error('Non-zero carry');
148
+ }
149
+ length = i;
150
+ psz++;
151
+ }
152
+ // Skip trailing spaces.
153
+ if (source[psz] === ' ') {
154
+ return;
155
+ }
156
+ // Skip leading zeroes in b256.
157
+ var it4 = size - length;
158
+ while (it4 !== size && b256[it4] === 0) {
159
+ it4++;
160
+ }
161
+ var vch = new Uint8Array(zeroes + (size - it4));
162
+ var j = zeroes;
163
+ while (it4 !== size) {
164
+ vch[j++] = b256[it4++];
165
+ }
166
+ return vch;
167
+ }
168
+ /**
169
+ * @param {string | string[]} string
170
+ */
171
+ function decode(string) {
172
+ var buffer = decodeUnsafe(string);
173
+ if (buffer) {
174
+ return buffer;
175
+ }
176
+ throw new Error(`Non-${name} character`);
177
+ }
178
+ return {
179
+ encode: encode,
180
+ decodeUnsafe: decodeUnsafe,
181
+ decode: decode
182
+ };
183
+ }
184
+ var src = base;
185
+ var _brrp__multiformats_scope_baseX = src;
186
+
187
+ /**
188
+ * Class represents both BaseEncoder and MultibaseEncoder meaning it
189
+ * can be used to encode to multibase or base encode without multibase
190
+ * prefix.
191
+ */
192
+ class Encoder {
193
+ name;
194
+ prefix;
195
+ baseEncode;
196
+ constructor(name, prefix, baseEncode) {
197
+ this.name = name;
198
+ this.prefix = prefix;
199
+ this.baseEncode = baseEncode;
200
+ }
201
+ encode(bytes) {
202
+ if (bytes instanceof Uint8Array) {
203
+ return `${this.prefix}${this.baseEncode(bytes)}`;
204
+ }
205
+ else {
206
+ throw Error('Unknown type, must be binary type');
207
+ }
208
+ }
209
+ }
210
+ /**
211
+ * Class represents both BaseDecoder and MultibaseDecoder so it could be used
212
+ * to decode multibases (with matching prefix) or just base decode strings
213
+ * with corresponding base encoding.
214
+ */
215
+ class Decoder {
216
+ name;
217
+ prefix;
218
+ baseDecode;
219
+ prefixCodePoint;
220
+ constructor(name, prefix, baseDecode) {
221
+ this.name = name;
222
+ this.prefix = prefix;
223
+ const prefixCodePoint = prefix.codePointAt(0);
224
+ /* c8 ignore next 3 */
225
+ if (prefixCodePoint === undefined) {
226
+ throw new Error('Invalid prefix character');
227
+ }
228
+ this.prefixCodePoint = prefixCodePoint;
229
+ this.baseDecode = baseDecode;
230
+ }
231
+ decode(text) {
232
+ if (typeof text === 'string') {
233
+ if (text.codePointAt(0) !== this.prefixCodePoint) {
234
+ throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);
235
+ }
236
+ return this.baseDecode(text.slice(this.prefix.length));
237
+ }
238
+ else {
239
+ throw Error('Can only multibase decode strings');
240
+ }
241
+ }
242
+ or(decoder) {
243
+ return or(this, decoder);
244
+ }
245
+ }
246
+ class ComposedDecoder {
247
+ decoders;
248
+ constructor(decoders) {
249
+ this.decoders = decoders;
250
+ }
251
+ or(decoder) {
252
+ return or(this, decoder);
253
+ }
254
+ decode(input) {
255
+ const prefix = input[0];
256
+ const decoder = this.decoders[prefix];
257
+ if (decoder != null) {
258
+ return decoder.decode(input);
259
+ }
260
+ else {
261
+ throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`);
262
+ }
263
+ }
264
+ }
265
+ function or(left, right) {
266
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
267
+ return new ComposedDecoder({
268
+ ...(left.decoders ?? { [left.prefix]: left }),
269
+ ...(right.decoders ?? { [right.prefix]: right })
270
+ });
271
+ }
272
+ class Codec {
273
+ name;
274
+ prefix;
275
+ baseEncode;
276
+ baseDecode;
277
+ encoder;
278
+ decoder;
279
+ constructor(name, prefix, baseEncode, baseDecode) {
280
+ this.name = name;
281
+ this.prefix = prefix;
282
+ this.baseEncode = baseEncode;
283
+ this.baseDecode = baseDecode;
284
+ this.encoder = new Encoder(name, prefix, baseEncode);
285
+ this.decoder = new Decoder(name, prefix, baseDecode);
286
+ }
287
+ encode(input) {
288
+ return this.encoder.encode(input);
289
+ }
290
+ decode(input) {
291
+ return this.decoder.decode(input);
292
+ }
293
+ }
294
+ function from({ name, prefix, encode, decode }) {
295
+ return new Codec(name, prefix, encode, decode);
296
+ }
297
+ function baseX({ name, prefix, alphabet }) {
298
+ const { encode, decode } = _brrp__multiformats_scope_baseX(alphabet, name);
299
+ return from({
300
+ prefix,
301
+ name,
302
+ encode,
303
+ decode: (text) => coerce(decode(text))
304
+ });
305
+ }
306
+ function decode$1(string, alphabet, bitsPerChar, name) {
307
+ // Build the character lookup table:
308
+ const codes = {};
309
+ for (let i = 0; i < alphabet.length; ++i) {
310
+ codes[alphabet[i]] = i;
311
+ }
312
+ // Count the padding bytes:
313
+ let end = string.length;
314
+ while (string[end - 1] === '=') {
315
+ --end;
316
+ }
317
+ // Allocate the output:
318
+ const out = new Uint8Array((end * bitsPerChar / 8) | 0);
319
+ // Parse the data:
320
+ let bits = 0; // Number of bits currently in the buffer
321
+ let buffer = 0; // Bits waiting to be written out, MSB first
322
+ let written = 0; // Next byte to write
323
+ for (let i = 0; i < end; ++i) {
324
+ // Read one character from the string:
325
+ const value = codes[string[i]];
326
+ if (value === undefined) {
327
+ throw new SyntaxError(`Non-${name} character`);
328
+ }
329
+ // Append the bits to the buffer:
330
+ buffer = (buffer << bitsPerChar) | value;
331
+ bits += bitsPerChar;
332
+ // Write out some bits if the buffer has a byte's worth:
333
+ if (bits >= 8) {
334
+ bits -= 8;
335
+ out[written++] = 0xff & (buffer >> bits);
336
+ }
337
+ }
338
+ // Verify that we have received just enough bits:
339
+ if (bits >= bitsPerChar || (0xff & (buffer << (8 - bits))) !== 0) {
340
+ throw new SyntaxError('Unexpected end of data');
341
+ }
342
+ return out;
343
+ }
344
+ function encode$1(data, alphabet, bitsPerChar) {
345
+ const pad = alphabet[alphabet.length - 1] === '=';
346
+ const mask = (1 << bitsPerChar) - 1;
347
+ let out = '';
348
+ let bits = 0; // Number of bits currently in the buffer
349
+ let buffer = 0; // Bits waiting to be written out, MSB first
350
+ for (let i = 0; i < data.length; ++i) {
351
+ // Slurp data into the buffer:
352
+ buffer = (buffer << 8) | data[i];
353
+ bits += 8;
354
+ // Write out as much as we can:
355
+ while (bits > bitsPerChar) {
356
+ bits -= bitsPerChar;
357
+ out += alphabet[mask & (buffer >> bits)];
358
+ }
359
+ }
360
+ // Partial character:
361
+ if (bits !== 0) {
362
+ out += alphabet[mask & (buffer << (bitsPerChar - bits))];
363
+ }
364
+ // Add padding characters until we hit a byte boundary:
365
+ if (pad) {
366
+ while (((out.length * bitsPerChar) & 7) !== 0) {
367
+ out += '=';
368
+ }
369
+ }
370
+ return out;
371
+ }
372
+ /**
373
+ * RFC4648 Factory
374
+ */
375
+ function rfc4648({ name, prefix, bitsPerChar, alphabet }) {
376
+ return from({
377
+ prefix,
378
+ name,
379
+ encode(input) {
380
+ return encode$1(input, alphabet, bitsPerChar);
381
+ },
382
+ decode(input) {
383
+ return decode$1(input, alphabet, bitsPerChar, name);
384
+ }
385
+ });
386
+ }
387
+
388
+ const base10 = baseX({
389
+ prefix: '9',
390
+ name: 'base10',
391
+ alphabet: '0123456789'
392
+ });
393
+
394
+ var base10$1 = /*#__PURE__*/Object.freeze({
395
+ __proto__: null,
396
+ base10: base10
397
+ });
398
+
399
+ const base16 = rfc4648({
400
+ prefix: 'f',
401
+ name: 'base16',
402
+ alphabet: '0123456789abcdef',
403
+ bitsPerChar: 4
404
+ });
405
+ const base16upper = rfc4648({
406
+ prefix: 'F',
407
+ name: 'base16upper',
408
+ alphabet: '0123456789ABCDEF',
409
+ bitsPerChar: 4
410
+ });
411
+
412
+ var base16$1 = /*#__PURE__*/Object.freeze({
413
+ __proto__: null,
414
+ base16: base16,
415
+ base16upper: base16upper
416
+ });
417
+
418
+ const base2 = rfc4648({
419
+ prefix: '0',
420
+ name: 'base2',
421
+ alphabet: '01',
422
+ bitsPerChar: 1
423
+ });
424
+
425
+ var base2$1 = /*#__PURE__*/Object.freeze({
426
+ __proto__: null,
427
+ base2: base2
428
+ });
429
+
430
+ const alphabet = Array.from('🚀🪐☄🛰🌌🌑🌒🌓🌔🌕🌖🌗🌘🌍🌏🌎🐉☀💻🖥💾💿😂❤😍🤣😊🙏💕😭😘👍😅👏😁🔥🥰💔💖💙😢🤔😆🙄💪😉☺👌🤗💜😔😎😇🌹🤦🎉💞✌✨🤷😱😌🌸🙌😋💗💚😏💛🙂💓🤩😄😀🖤😃💯🙈👇🎶😒🤭❣😜💋👀😪😑💥🙋😞😩😡🤪👊🥳😥🤤👉💃😳✋😚😝😴🌟😬🙃🍀🌷😻😓⭐✅🥺🌈😈🤘💦✔😣🏃💐☹🎊💘😠☝😕🌺🎂🌻😐🖕💝🙊😹🗣💫💀👑🎵🤞😛🔴😤🌼😫⚽🤙☕🏆🤫👈😮🙆🍻🍃🐶💁😲🌿🧡🎁⚡🌞🎈❌✊👋😰🤨😶🤝🚶💰🍓💢🤟🙁🚨💨🤬✈🎀🍺🤓😙💟🌱😖👶🥴▶➡❓💎💸⬇😨🌚🦋😷🕺⚠🙅😟😵👎🤲🤠🤧📌🔵💅🧐🐾🍒😗🤑🌊🤯🐷☎💧😯💆👆🎤🙇🍑❄🌴💣🐸💌📍🥀🤢👅💡💩👐📸👻🤐🤮🎼🥵🚩🍎🍊👼💍📣🥂');
431
+ const alphabetBytesToChars = (alphabet.reduce((p, c, i) => { p[i] = c; return p; }, ([])));
432
+ const alphabetCharsToBytes = (alphabet.reduce((p, c, i) => {
433
+ const codePoint = c.codePointAt(0);
434
+ if (codePoint == null) {
435
+ throw new Error(`Invalid character: ${c}`);
436
+ }
437
+ p[codePoint] = i;
438
+ return p;
439
+ }, ([])));
440
+ function encode(data) {
441
+ return data.reduce((p, c) => {
442
+ p += alphabetBytesToChars[c];
443
+ return p;
444
+ }, '');
445
+ }
446
+ function decode(str) {
447
+ const byts = [];
448
+ for (const char of str) {
449
+ const codePoint = char.codePointAt(0);
450
+ if (codePoint == null) {
451
+ throw new Error(`Invalid character: ${char}`);
452
+ }
453
+ const byt = alphabetCharsToBytes[codePoint];
454
+ if (byt == null) {
455
+ throw new Error(`Non-base256emoji character: ${char}`);
456
+ }
457
+ byts.push(byt);
458
+ }
459
+ return new Uint8Array(byts);
460
+ }
461
+ const base256emoji = from({
462
+ prefix: '🚀',
463
+ name: 'base256emoji',
464
+ encode,
465
+ decode
466
+ });
467
+
468
+ var base256emoji$1 = /*#__PURE__*/Object.freeze({
469
+ __proto__: null,
470
+ base256emoji: base256emoji
471
+ });
472
+
473
+ const base32 = rfc4648({
474
+ prefix: 'b',
475
+ name: 'base32',
476
+ alphabet: 'abcdefghijklmnopqrstuvwxyz234567',
477
+ bitsPerChar: 5
478
+ });
479
+ const base32upper = rfc4648({
480
+ prefix: 'B',
481
+ name: 'base32upper',
482
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
483
+ bitsPerChar: 5
484
+ });
485
+ const base32pad = rfc4648({
486
+ prefix: 'c',
487
+ name: 'base32pad',
488
+ alphabet: 'abcdefghijklmnopqrstuvwxyz234567=',
489
+ bitsPerChar: 5
490
+ });
491
+ const base32padupper = rfc4648({
492
+ prefix: 'C',
493
+ name: 'base32padupper',
494
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=',
495
+ bitsPerChar: 5
496
+ });
497
+ const base32hex = rfc4648({
498
+ prefix: 'v',
499
+ name: 'base32hex',
500
+ alphabet: '0123456789abcdefghijklmnopqrstuv',
501
+ bitsPerChar: 5
502
+ });
503
+ const base32hexupper = rfc4648({
504
+ prefix: 'V',
505
+ name: 'base32hexupper',
506
+ alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
507
+ bitsPerChar: 5
508
+ });
509
+ const base32hexpad = rfc4648({
510
+ prefix: 't',
511
+ name: 'base32hexpad',
512
+ alphabet: '0123456789abcdefghijklmnopqrstuv=',
513
+ bitsPerChar: 5
514
+ });
515
+ const base32hexpadupper = rfc4648({
516
+ prefix: 'T',
517
+ name: 'base32hexpadupper',
518
+ alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=',
519
+ bitsPerChar: 5
520
+ });
521
+ const base32z = rfc4648({
522
+ prefix: 'h',
523
+ name: 'base32z',
524
+ alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
525
+ bitsPerChar: 5
526
+ });
527
+
528
+ var base32$1 = /*#__PURE__*/Object.freeze({
529
+ __proto__: null,
530
+ base32: base32,
531
+ base32hex: base32hex,
532
+ base32hexpad: base32hexpad,
533
+ base32hexpadupper: base32hexpadupper,
534
+ base32hexupper: base32hexupper,
535
+ base32pad: base32pad,
536
+ base32padupper: base32padupper,
537
+ base32upper: base32upper,
538
+ base32z: base32z
539
+ });
540
+
541
+ const base36 = baseX({
542
+ prefix: 'k',
543
+ name: 'base36',
544
+ alphabet: '0123456789abcdefghijklmnopqrstuvwxyz'
545
+ });
546
+ const base36upper = baseX({
547
+ prefix: 'K',
548
+ name: 'base36upper',
549
+ alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
550
+ });
551
+
552
+ var base36$1 = /*#__PURE__*/Object.freeze({
553
+ __proto__: null,
554
+ base36: base36,
555
+ base36upper: base36upper
556
+ });
557
+
558
+ const base58btc = baseX({
559
+ name: 'base58btc',
560
+ prefix: 'z',
561
+ alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
562
+ });
563
+ const base58flickr = baseX({
564
+ name: 'base58flickr',
565
+ prefix: 'Z',
566
+ alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
567
+ });
568
+
569
+ var base58 = /*#__PURE__*/Object.freeze({
570
+ __proto__: null,
571
+ base58btc: base58btc,
572
+ base58flickr: base58flickr
573
+ });
574
+
575
+ const base64 = rfc4648({
576
+ prefix: 'm',
577
+ name: 'base64',
578
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
579
+ bitsPerChar: 6
580
+ });
581
+ const base64pad = rfc4648({
582
+ prefix: 'M',
583
+ name: 'base64pad',
584
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
585
+ bitsPerChar: 6
586
+ });
587
+ const base64url = rfc4648({
588
+ prefix: 'u',
589
+ name: 'base64url',
590
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
591
+ bitsPerChar: 6
592
+ });
593
+ const base64urlpad = rfc4648({
594
+ prefix: 'U',
595
+ name: 'base64urlpad',
596
+ alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=',
597
+ bitsPerChar: 6
598
+ });
599
+
600
+ var base64$1 = /*#__PURE__*/Object.freeze({
601
+ __proto__: null,
602
+ base64: base64,
603
+ base64pad: base64pad,
604
+ base64url: base64url,
605
+ base64urlpad: base64urlpad
606
+ });
607
+
608
+ const base8 = rfc4648({
609
+ prefix: '7',
610
+ name: 'base8',
611
+ alphabet: '01234567',
612
+ bitsPerChar: 3
613
+ });
614
+
615
+ var base8$1 = /*#__PURE__*/Object.freeze({
616
+ __proto__: null,
617
+ base8: base8
618
+ });
619
+
620
+ const identity = from({
621
+ prefix: '\x00',
622
+ name: 'identity',
623
+ encode: (buf) => toString$1(buf),
624
+ decode: (str) => fromString(str)
625
+ });
626
+
627
+ var identityBase = /*#__PURE__*/Object.freeze({
628
+ __proto__: null,
629
+ identity: identity
630
+ });
631
+
632
+ new TextEncoder();
633
+ new TextDecoder();
634
+
635
+ const bases = { ...identityBase, ...base2$1, ...base8$1, ...base10$1, ...base16$1, ...base32$1, ...base36$1, ...base58, ...base64$1, ...base256emoji$1 };
636
+
637
+ /**
638
+ * Returns a `Uint8Array` of the requested size. Referenced memory will
639
+ * be initialized to 0.
640
+ */
641
+ /**
642
+ * Where possible returns a Uint8Array of the requested size that references
643
+ * uninitialized memory. Only use if you are certain you will immediately
644
+ * overwrite every value in the returned `Uint8Array`.
645
+ */
646
+ function allocUnsafe(size = 0) {
647
+ return new Uint8Array(size);
648
+ }
649
+
650
+ function createCodec(name, prefix, encode, decode) {
651
+ return {
652
+ name,
653
+ prefix,
654
+ encoder: {
655
+ name,
656
+ prefix,
657
+ encode
658
+ },
659
+ decoder: {
660
+ decode
661
+ }
662
+ };
663
+ }
664
+ const string = createCodec('utf8', 'u', (buf) => {
665
+ const decoder = new TextDecoder('utf8');
666
+ return 'u' + decoder.decode(buf);
667
+ }, (str) => {
668
+ const encoder = new TextEncoder();
669
+ return encoder.encode(str.substring(1));
670
+ });
671
+ const ascii = createCodec('ascii', 'a', (buf) => {
672
+ let string = 'a';
673
+ for (let i = 0; i < buf.length; i++) {
674
+ string += String.fromCharCode(buf[i]);
675
+ }
676
+ return string;
677
+ }, (str) => {
678
+ str = str.substring(1);
679
+ const buf = allocUnsafe(str.length);
680
+ for (let i = 0; i < str.length; i++) {
681
+ buf[i] = str.charCodeAt(i);
682
+ }
683
+ return buf;
684
+ });
685
+ const BASES = {
686
+ utf8: string,
687
+ 'utf-8': string,
688
+ hex: bases.base16,
689
+ latin1: ascii,
690
+ ascii,
691
+ binary: ascii,
692
+ ...bases
693
+ };
694
+
695
+ /**
696
+ * Turns a `Uint8Array` into a string.
697
+ *
698
+ * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
699
+ *
700
+ * Also `ascii` which is similar to node's 'binary' encoding.
701
+ */
702
+ function toString(array, encoding = 'utf8') {
703
+ const base = BASES[encoding];
704
+ if (base == null) {
705
+ throw new Error(`Unsupported encoding "${encoding}"`);
706
+ }
707
+ // strip multibase prefix
708
+ return base.encoder.encode(array).substring(1);
709
+ }
710
+
711
+ // HPACK Implementation
712
+ class HPACK {
713
+ constructor(maxDynamicTableSize = 4096) {
714
+ // 初始化动态表
715
+ this.dynamicTable = [];
716
+ this.dynamicTableSize = 0;
717
+ this.maxDynamicTableSize = maxDynamicTableSize;
718
+ // 初始化静态表
719
+ this.staticTable = [
720
+ ['', ''], // Index 0 is not used
721
+ [':authority', ''],
722
+ [':method', 'GET'],
723
+ [':method', 'POST'],
724
+ [':path', '/'],
725
+ [':path', '/index.html'],
726
+ [':scheme', 'http'],
727
+ [':scheme', 'https'],
728
+ [':status', '200'],
729
+ [':status', '204'],
730
+ [':status', '206'],
731
+ [':status', '304'],
732
+ [':status', '400'],
733
+ [':status', '404'],
734
+ [':status', '500'],
735
+ ['accept-charset', ''],
736
+ ['accept-encoding', 'gzip, deflate'],
737
+ ['accept-language', ''],
738
+ ['accept-ranges', ''],
739
+ ['accept', ''],
740
+ ['access-control-allow-origin', ''],
741
+ ['age', ''],
742
+ ['allow', ''],
743
+ ['authorization', ''],
744
+ ['cache-control', ''],
745
+ ['content-disposition', ''],
746
+ ['content-encoding', ''],
747
+ ['content-language', ''],
748
+ ['content-length', ''],
749
+ ['content-location', ''],
750
+ ['content-range', ''],
751
+ ['content-type', ''],
752
+ ['cookie', ''],
753
+ ['date', ''],
754
+ ['etag', ''],
755
+ ['expect', ''],
756
+ ['expires', ''],
757
+ ['from', ''],
758
+ ['host', ''],
759
+ ['if-match', ''],
760
+ ['if-modified-since', ''],
761
+ ['if-none-match', ''],
762
+ ['if-range', ''],
763
+ ['if-unmodified-since', ''],
764
+ ['last-modified', ''],
765
+ ['link', ''],
766
+ ['location', ''],
767
+ ['max-forwards', ''],
768
+ ['proxy-authenticate', ''],
769
+ ['proxy-authorization', ''],
770
+ ['range', ''],
771
+ ['referer', ''],
772
+ ['refresh', ''],
773
+ ['retry-after', ''],
774
+ ['server', ''],
775
+ ['set-cookie', ''],
776
+ ['strict-transport-security', ''],
777
+ ['transfer-encoding', ''],
778
+ ['user-agent', ''],
779
+ ['vary', ''],
780
+ ['via', ''],
781
+ ['www-authenticate', '']
782
+ ];
783
+ // Huffman编码表
784
+ this.huffmanTable = this.buildHuffmanTable();
785
+ }
786
+ // 编码headers
787
+ encode(headers) {
788
+ const buffer = [];
789
+ for (const [name, value] of Object.entries(headers)) {
790
+ // 查找静态表索引
791
+ const staticIndex = this.findInStaticTable(name, value);
792
+ if (staticIndex !== -1) {
793
+ // 使用索引编码
794
+ buffer.push(...this.encodeInteger(staticIndex, 7, 0x80));
795
+ continue;
796
+ }
797
+ // 查找动态表索引
798
+ const dynamicIndex = this.findInDynamicTable(name, value);
799
+ if (dynamicIndex !== -1) {
800
+ buffer.push(...this.encodeInteger(dynamicIndex + this.staticTable.length, 7, 0x80));
801
+ continue;
802
+ }
803
+ // 字面量编码
804
+ this.encodeLiteral(buffer, name, value);
805
+ }
806
+ return new Uint8Array(buffer);
807
+ }
808
+ // // 解码二进制数据
809
+ // decode(buffer: Uint8Array): { [key: string]: string } {
810
+ // const headers: { [key: string]: string } = {};
811
+ // let pos = 0;
812
+ // while (pos < buffer.length) {
813
+ // const firstByte = buffer[pos];
814
+ // // 检查是否是索引头部字段
815
+ // if ((firstByte & 0x80) === 0x80) {
816
+ // const { value: index, bytesRead } = this.decodeInteger(
817
+ // buffer.slice(pos),
818
+ // 7
819
+ // );
820
+ // pos += bytesRead;
821
+ // const [name, value] = this.getIndexedHeader(index);
822
+ // if (name !== undefined && value !== undefined) {
823
+ // headers[name] = value;
824
+ // }
825
+ // }
826
+ // // 字面量头部字段
827
+ // else {
828
+ // const { name, value, bytesRead } = this.decodeLiteral(
829
+ // buffer.slice(pos)
830
+ // );
831
+ // pos += bytesRead;
832
+ // headers[name] = value;
833
+ // }
834
+ // }
835
+ // return headers;
836
+ // }
837
+ // 编码整数
838
+ encodeInteger(value, prefixBits, prefix = 0) {
839
+ const buffer = [];
840
+ const mask = (1 << prefixBits) - 1;
841
+ if (value < mask) {
842
+ buffer.push(prefix | value);
843
+ return buffer;
844
+ }
845
+ buffer.push(prefix | mask);
846
+ value -= mask;
847
+ while (value >= 128) {
848
+ buffer.push((value & 127) | 128);
849
+ value = value >> 7;
850
+ }
851
+ buffer.push(value);
852
+ return buffer;
853
+ }
854
+ // // 解码整数
855
+ // decodeInteger(buffer: Uint8Array, prefixBits: number) {
856
+ // let value = buffer[0] & ((1 << prefixBits) - 1);
857
+ // let bytesRead = 1;
858
+ // if (value === (1 << prefixBits) - 1) {
859
+ // let shift = 0;
860
+ // do {
861
+ // value += (buffer[bytesRead] & 127) << shift;
862
+ // shift += 7;
863
+ // bytesRead++;
864
+ // } while (buffer[bytesRead - 1] & 128);
865
+ // }
866
+ // return { value, bytesRead };
867
+ // }
868
+ // 编码字符串
869
+ encodeString(str) {
870
+ const buffer = [];
871
+ const bytes = new TextEncoder().encode(str);
872
+ // 尝试Huffman编码
873
+ const huffmanEncoded = this.huffmanEncode(bytes);
874
+ if (huffmanEncoded.length < bytes.length) {
875
+ // 使用Huffman编码
876
+ buffer.push(...this.encodeInteger(huffmanEncoded.length, 7, 0x80));
877
+ buffer.push(...huffmanEncoded);
878
+ }
879
+ else {
880
+ // 不使用Huffman编码
881
+ buffer.push(...this.encodeInteger(bytes.length, 7, 0x00));
882
+ buffer.push(...bytes);
883
+ }
884
+ return buffer;
885
+ }
886
+ // 在静态表中查找
887
+ findInStaticTable(name, value) {
888
+ for (let i = 1; i < this.staticTable.length; i++) {
889
+ if (this.staticTable[i][0] === name &&
890
+ this.staticTable[i][1] === value) {
891
+ return i;
892
+ }
893
+ }
894
+ return -1;
895
+ }
896
+ // 在动态表中查找
897
+ findInDynamicTable(name, value) {
898
+ for (let i = 0; i < this.dynamicTable.length; i++) {
899
+ if (this.dynamicTable[i][0] === name &&
900
+ this.dynamicTable[i][1] === value) {
901
+ return i;
902
+ }
903
+ }
904
+ return -1;
905
+ }
906
+ // 编码字面量头部字段
907
+ encodeLiteral(buffer, name, value) {
908
+ const nameIndex = this.findInStaticTable(name, '');
909
+ if (nameIndex !== -1) {
910
+ // 名称索引存在
911
+ buffer.push(...this.encodeInteger(nameIndex, 6, 0x40));
912
+ }
913
+ else {
914
+ // 名称需要字面量编码
915
+ buffer.push(0x40);
916
+ buffer.push(...this.encodeString(name));
917
+ }
918
+ // 值总是需要字面量编码
919
+ buffer.push(...this.encodeString(value));
920
+ // 添加到动态表
921
+ this.addToDynamicTable(name, value);
922
+ }
923
+ // 添加到动态表
924
+ addToDynamicTable(name, value) {
925
+ const size = name.length + value.length + 32; // 32 bytes overhead
926
+ // 确保不超过最大大小
927
+ while (this.dynamicTableSize + size > this.maxDynamicTableSize &&
928
+ this.dynamicTable.length > 0) {
929
+ const entry = this.dynamicTable.pop();
930
+ if (entry) {
931
+ this.dynamicTableSize -= entry[0].length + entry[1].length + 32;
932
+ }
933
+ }
934
+ if (size <= this.maxDynamicTableSize) {
935
+ this.dynamicTable.unshift([name, value]);
936
+ this.dynamicTableSize += size;
937
+ }
938
+ this.dynamicTable.push([name, value]);
939
+ }
940
+ // 获取索引的头部
941
+ getIndexedHeader(index) {
942
+ if (index <= this.staticTable.length - 1) {
943
+ return this.staticTable[index];
944
+ }
945
+ return this.dynamicTable[index - this.staticTable.length];
946
+ }
947
+ buildHuffmanTable() {
948
+ // HTTP/2规范中定义的完整Huffman编码表
949
+ return {
950
+ codes: new Uint32Array([
951
+ 0x1ff8, 0x7fffd8, 0xfffffe2, 0xfffffe3, 0xfffffe4, 0xfffffe5, 0xfffffe6, 0xfffffe7,
952
+ 0xfffffe8, 0xffffea, 0x3ffffffc, 0xfffffe9, 0xfffffea, 0x3ffffffd, 0xfffffeb, 0xfffffec,
953
+ 0xfffffed, 0xfffffee, 0xfffffef, 0xffffff0, 0xffffff1, 0xffffff2, 0x3ffffffe, 0xffffff3,
954
+ 0xffffff4, 0xffffff5, 0xffffff6, 0xffffff7, 0xffffff8, 0xffffff9, 0xffffffa, 0xffffffb,
955
+ 0x14, 0x3f8, 0x3f9, 0xffa, 0x1ff9, 0x15, 0xf8, 0x7fa,
956
+ 0x3fa, 0x3fb, 0xf9, 0x7fb, 0xfa, 0x16, 0x17, 0x18,
957
+ 0x0, 0x1, 0x2, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
958
+ 0x1e, 0x1f, 0x5c, 0xfb, 0x7ffc, 0x20, 0xffb, 0x3fc,
959
+ 0x1ffa, 0x21, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
960
+ 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
961
+ 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72,
962
+ 0xfc, 0x73, 0xfd, 0x1ffb, 0x7fff0, 0x1ffc, 0x3ffc, 0x22,
963
+ 0x7ffd, 0x3, 0x23, 0x4, 0x24, 0x5, 0x25, 0x26,
964
+ 0x27, 0x6, 0x74, 0x75, 0x28, 0x29, 0x2a, 0x7,
965
+ 0x2b, 0x76, 0x2c, 0x8, 0x9, 0x2d, 0x77, 0x78,
966
+ 0x79, 0x7a, 0x7b, 0x7ffe, 0x7fc, 0x3ffd, 0x1ffd, 0xffffffc,
967
+ 0xfffe6, 0x3fffd2, 0xfffe7, 0xfffe8, 0x3fffd3, 0x3fffd4, 0x3fffd5, 0x7fffd9,
968
+ 0x3fffd6, 0x7fffda, 0x7fffdb, 0x7fffdc, 0x7fffdd, 0x7fffde, 0xffffeb, 0x7fffdf,
969
+ 0xffffec, 0xffffed, 0x3fffd7, 0x7fffe0, 0xffffee, 0x7fffe1, 0x7fffe2, 0x7fffe3,
970
+ 0x7fffe4, 0x1fffdc, 0x3fffd8, 0x7fffe5, 0x3fffd9, 0x7fffe6, 0x7fffe7, 0xffffef,
971
+ 0x3fffda, 0x1fffdd, 0xfffe9, 0x3fffdb, 0x3fffdc, 0x7fffe8, 0x7fffe9, 0x1fffde,
972
+ 0x7fffea, 0x3fffdd, 0x3fffde, 0xfffff0, 0x1fffdf, 0x3fffdf, 0x7fffeb, 0x7fffec,
973
+ 0x1fffe0, 0x1fffe1, 0x3fffe0, 0x1fffe2, 0x7fffed, 0x3fffe1, 0x7fffee, 0x7fffef,
974
+ 0xfffea, 0x3fffe2, 0x3fffe3, 0x3fffe4, 0x7ffff0, 0x3fffe5, 0x3fffe6, 0x7ffff1,
975
+ 0x3ffffe0, 0x3ffffe1, 0xfffeb, 0x7fff1, 0x3fffe7, 0x7ffff2, 0x3fffe8, 0x1ffffec,
976
+ 0x3ffffe2, 0x3ffffe3, 0x3ffffe4, 0x7ffffde, 0x7ffffdf, 0x3ffffe5, 0xfffff1, 0x1ffffed,
977
+ 0x7fff2, 0x1fffe3, 0x3ffffe6, 0x7ffffe0, 0x7ffffe1, 0x3ffffe7, 0x7ffffe2, 0xfffff2,
978
+ 0x1fffe4, 0x1fffe5, 0x3ffffe8, 0x3ffffe9, 0xffffffd, 0x7ffffe3, 0x7ffffe4, 0x7ffffe5,
979
+ 0xfffec, 0xfffff3, 0xfffed, 0x1fffe6, 0x3fffe9, 0x1fffe7, 0x1fffe8, 0x7ffff3,
980
+ 0x3fffea, 0x3fffeb, 0x1ffffee, 0x1ffffef, 0xfffff4, 0xfffff5, 0x3ffffea, 0x7ffff4,
981
+ 0x3ffffeb, 0x7ffffe6, 0x3ffffec, 0x3ffffed, 0x7ffffe7, 0x7ffffe8, 0x7ffffe9, 0x7ffffea,
982
+ 0x7ffffeb, 0xffffffe, 0x7ffffec, 0x7ffffed, 0x7ffffee, 0x7ffffef, 0x7fffff0, 0x3ffffee
983
+ ]),
984
+ lengths: new Uint8Array([
985
+ 13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28,
986
+ 28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28,
987
+ 6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6,
988
+ 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10,
989
+ 13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
990
+ 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6,
991
+ 15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5,
992
+ 6, 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28,
993
+ 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23,
994
+ 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24,
995
+ 22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23,
996
+ 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23,
997
+ 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25,
998
+ 19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27,
999
+ 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23,
1000
+ 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26
1001
+ ])
1002
+ };
1003
+ }
1004
+ /**
1005
+ * 解码Uint8Array为字符串
1006
+ * @param input 编码后的Uint8Array
1007
+ * @returns 解码后的字符串
1008
+ */
1009
+ decode(input) {
1010
+ let result = '';
1011
+ let accumulator = 0;
1012
+ let bits = 0;
1013
+ for (let i = 0; i < input.length; i++) {
1014
+ const byte = input[i];
1015
+ accumulator = (accumulator << 8) | byte;
1016
+ bits += 8;
1017
+ while (bits >= 5) {
1018
+ const decoded = this.findSymbol(accumulator, bits);
1019
+ if (!decoded) {
1020
+ break;
1021
+ }
1022
+ const [symbol, length] = decoded;
1023
+ result += String.fromCharCode(symbol);
1024
+ accumulator &= (1 << (bits - length)) - 1;
1025
+ bits -= length;
1026
+ }
1027
+ }
1028
+ // 验证填充
1029
+ if (bits > 0) {
1030
+ const mask = (1 << bits) - 1;
1031
+ if ((accumulator & mask) !== mask) {
1032
+ throw new Error('Invalid Huffman padding');
1033
+ }
1034
+ }
1035
+ return result;
1036
+ }
1037
+ /**
1038
+ * 在Huffman树中查找符号
1039
+ */
1040
+ findSymbol(value, bits) {
1041
+ for (let i = 0; i < this.huffmanTable.codes.length; i++) {
1042
+ const length = this.huffmanTable.lengths[i];
1043
+ if (bits < length)
1044
+ continue;
1045
+ const code = (value >> (bits - length)) & ((1 << length) - 1);
1046
+ if (code === this.huffmanTable.codes[i]) {
1047
+ return [i, length];
1048
+ }
1049
+ }
1050
+ return null;
1051
+ }
1052
+ // Huffman编码实现
1053
+ huffmanEncode(bytes) {
1054
+ let result = [];
1055
+ let current = 0;
1056
+ let bits = 0;
1057
+ for (let i = 0; i < bytes.length; i++) {
1058
+ const b = bytes[i];
1059
+ const code = this.huffmanTable.codes[b];
1060
+ const length = this.huffmanTable.lengths[b];
1061
+ bits += length;
1062
+ current = (current << length) | code;
1063
+ while (bits >= 8) {
1064
+ bits -= 8;
1065
+ result.push((current >> bits) & 0xFF);
1066
+ }
1067
+ }
1068
+ // 处理剩余的位
1069
+ if (bits > 0) {
1070
+ current = (current << (8 - bits)) | ((1 << (8 - bits)) - 1);
1071
+ result.push(current & 0xFF);
1072
+ }
1073
+ return new Uint8Array(result);
1074
+ }
1075
+ decodeHeaderFields(buffer) {
1076
+ const headers = new Map();
1077
+ let index = 0;
1078
+ while (index < buffer.length) {
1079
+ const firstByte = buffer[index];
1080
+ // 检查首字节的类型
1081
+ if ((firstByte & 0x80) !== 0) { // 1xxxxxxx - Indexed Header Field
1082
+ const [name, value, newIndex] = this.decodeIndexedHeader(buffer, index);
1083
+ if (name && value)
1084
+ headers.set(name, value);
1085
+ index = newIndex;
1086
+ }
1087
+ else if ((firstByte & 0x40) !== 0) { // 01xxxxxx - Literal Header Field with Incremental Indexing
1088
+ const [name, value, newIndex] = this.decodeLiteralHeaderWithIndexing(buffer, index);
1089
+ if (name && value)
1090
+ headers.set(name, value);
1091
+ index = newIndex;
1092
+ }
1093
+ else if ((firstByte & 0x20) !== 0) { // 001xxxxx - Dynamic Table Size Update
1094
+ index++; // 简单跳过,实际应该更新动态表大小
1095
+ }
1096
+ else if ((firstByte & 0x10) !== 0) { // 0001xxxx - Literal Header Field Never Indexed
1097
+ const [name, value, newIndex] = this.decodeLiteralHeaderWithoutIndexing(buffer, index);
1098
+ if (name && value)
1099
+ headers.set(name, value);
1100
+ index = newIndex;
1101
+ }
1102
+ else { // 0000xxxx - Literal Header Field without Indexing
1103
+ const [name, value, newIndex] = this.decodeLiteralHeaderWithoutIndexing(buffer, index);
1104
+ if (name && value)
1105
+ headers.set(name, value);
1106
+ index = newIndex;
1107
+ }
1108
+ }
1109
+ return headers;
1110
+ }
1111
+ decodeInteger(buffer, startIndex, prefixBits) {
1112
+ const prefix = (1 << prefixBits) - 1;
1113
+ const firstByte = buffer[startIndex];
1114
+ let index = startIndex;
1115
+ let value = firstByte & prefix;
1116
+ if (value < prefix) {
1117
+ return [value, index + 1];
1118
+ }
1119
+ index++;
1120
+ let shift = 0;
1121
+ while (index < buffer.length) {
1122
+ const byte = buffer[index++];
1123
+ value += (byte & 0x7F) << shift;
1124
+ shift += 7;
1125
+ if ((byte & 0x80) === 0) {
1126
+ break;
1127
+ }
1128
+ }
1129
+ return [value, index];
1130
+ }
1131
+ decodeLiteralString(buffer, startIndex) {
1132
+ if (startIndex >= buffer.length) {
1133
+ return ['', startIndex];
1134
+ }
1135
+ const firstByte = buffer[startIndex];
1136
+ const isHuffman = (firstByte & 0x80) !== 0;
1137
+ const [length, index] = this.decodeInteger(buffer, startIndex, 7);
1138
+ if (index + length > buffer.length) {
1139
+ return ['', index];
1140
+ }
1141
+ const bytes = buffer.slice(index, index + length);
1142
+ let result;
1143
+ if (isHuffman) {
1144
+ try {
1145
+ result = this.decode(bytes);
1146
+ }
1147
+ catch (e) {
1148
+ result = '';
1149
+ }
1150
+ }
1151
+ else {
1152
+ try {
1153
+ // result = new TextDecoder().decode(bytes);
1154
+ result = toString(bytes);
1155
+ }
1156
+ catch (e) {
1157
+ result = '';
1158
+ }
1159
+ }
1160
+ return [result, index + length];
1161
+ }
1162
+ decodeIndexedHeader(buffer, index) {
1163
+ const [staticIndex, newIndex] = this.decodeInteger(buffer, index, 7);
1164
+ if (staticIndex <= 0) {
1165
+ return ['', '', newIndex];
1166
+ }
1167
+ const headerField = this.staticTable[staticIndex];
1168
+ if (!headerField) {
1169
+ return ['', '', newIndex];
1170
+ }
1171
+ return [headerField[0], headerField[1], newIndex];
1172
+ }
1173
+ decodeLiteralHeaderWithIndexing(buffer, index) {
1174
+ const [staticIndex, nameIndex] = this.decodeInteger(buffer, index, 6);
1175
+ index = nameIndex;
1176
+ let name;
1177
+ if (staticIndex > 0) {
1178
+ const headerField = this.staticTable[staticIndex];
1179
+ name = headerField ? headerField[0] : '';
1180
+ }
1181
+ else {
1182
+ const [decodedName, newIndex] = this.decodeLiteralString(buffer, index);
1183
+ name = decodedName;
1184
+ index = newIndex;
1185
+ }
1186
+ const [value, finalIndex] = this.decodeLiteralString(buffer, index);
1187
+ return [name, value, finalIndex];
1188
+ }
1189
+ decodeLiteralHeaderWithoutIndexing(buffer, index) {
1190
+ return this.decodeLiteralHeaderWithIndexing(buffer, index);
1191
+ }
1192
+ // 直接转换为字符串的方法
1193
+ huffmanDecodeToString(bytes) {
1194
+ return this.decode(bytes);
1195
+ }
1196
+ }
1197
+
1198
+ export { HPACK };
1199
+ //# sourceMappingURL=hpack.esm.js.map