tunnelfetch 1.0.0

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.
Files changed (96) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +617 -0
  3. package/README.zh-CN.md +470 -0
  4. package/package.json +74 -0
  5. package/src/client/cookies.js +429 -0
  6. package/src/client/decode.js +346 -0
  7. package/src/client/redirect.js +249 -0
  8. package/src/client.js +704 -0
  9. package/src/errors.js +181 -0
  10. package/src/http1/chunked.js +289 -0
  11. package/src/http1/index.js +10 -0
  12. package/src/http1/request.js +143 -0
  13. package/src/http1/response.js +493 -0
  14. package/src/http2/connection.js +1170 -0
  15. package/src/http2/constants.js +129 -0
  16. package/src/http2/frames.js +291 -0
  17. package/src/http2/hpack.js +420 -0
  18. package/src/http2/huffman.js +203 -0
  19. package/src/http2/index.js +21 -0
  20. package/src/index.js +46 -0
  21. package/src/pool.js +256 -0
  22. package/src/proxy/direct.js +62 -0
  23. package/src/proxy/http-connect.js +206 -0
  24. package/src/proxy/index.js +197 -0
  25. package/src/proxy/socks5.js +344 -0
  26. package/src/tls/aead.js +263 -0
  27. package/src/tls/connect.js +407 -0
  28. package/src/tls/constants.js +334 -0
  29. package/src/tls/extensions.js +376 -0
  30. package/src/tls/handshake-messages.js +901 -0
  31. package/src/tls/handshake.js +568 -0
  32. package/src/tls/handshake12.js +507 -0
  33. package/src/tls/index.js +44 -0
  34. package/src/tls/keyschedule.js +473 -0
  35. package/src/tls/record.js +872 -0
  36. package/src/tls/tickets.js +145 -0
  37. package/src/tls/transcript.js +101 -0
  38. package/src/tls/wire.js +224 -0
  39. package/src/transport.js +296 -0
  40. package/src/trust/der.js +551 -0
  41. package/src/trust/index.js +375 -0
  42. package/src/trust/name.js +235 -0
  43. package/src/trust/ocsp.js +759 -0
  44. package/src/trust/path.js +595 -0
  45. package/src/trust/roots.js +454 -0
  46. package/src/trust/x509.js +902 -0
  47. package/src/util/bytes.js +470 -0
  48. package/src/util/deadline.js +266 -0
  49. package/src/warmup-fixture.js +85 -0
  50. package/src/warmup.js +243 -0
  51. package/types/client/cookies.d.ts +159 -0
  52. package/types/client/decode.d.ts +54 -0
  53. package/types/client/redirect.d.ts +96 -0
  54. package/types/client.d.ts +323 -0
  55. package/types/errors.d.ts +141 -0
  56. package/types/http1/chunked.d.ts +48 -0
  57. package/types/http1/index.d.ts +3 -0
  58. package/types/http1/request.d.ts +44 -0
  59. package/types/http1/response.d.ts +183 -0
  60. package/types/http2/connection.d.ts +282 -0
  61. package/types/http2/constants.d.ts +95 -0
  62. package/types/http2/frames.d.ts +116 -0
  63. package/types/http2/hpack.d.ts +99 -0
  64. package/types/http2/huffman.d.ts +21 -0
  65. package/types/http2/index.d.ts +5 -0
  66. package/types/index.d.ts +17 -0
  67. package/types/pool.d.ts +135 -0
  68. package/types/proxy/direct.d.ts +26 -0
  69. package/types/proxy/http-connect.d.ts +37 -0
  70. package/types/proxy/index.d.ts +62 -0
  71. package/types/proxy/socks5.d.ts +47 -0
  72. package/types/tls/aead.d.ts +67 -0
  73. package/types/tls/connect.d.ts +280 -0
  74. package/types/tls/constants.d.ts +275 -0
  75. package/types/tls/extensions.d.ts +195 -0
  76. package/types/tls/handshake-messages.d.ts +430 -0
  77. package/types/tls/handshake.d.ts +90 -0
  78. package/types/tls/handshake12.d.ts +35 -0
  79. package/types/tls/index.d.ts +9 -0
  80. package/types/tls/keyschedule.d.ts +272 -0
  81. package/types/tls/record.d.ts +361 -0
  82. package/types/tls/tickets.d.ts +66 -0
  83. package/types/tls/transcript.d.ts +52 -0
  84. package/types/tls/wire.d.ts +106 -0
  85. package/types/transport.d.ts +222 -0
  86. package/types/trust/der.d.ts +239 -0
  87. package/types/trust/index.d.ts +194 -0
  88. package/types/trust/name.d.ts +33 -0
  89. package/types/trust/ocsp.d.ts +138 -0
  90. package/types/trust/path.d.ts +139 -0
  91. package/types/trust/roots.d.ts +36 -0
  92. package/types/trust/x509.d.ts +401 -0
  93. package/types/util/bytes.d.ts +183 -0
  94. package/types/util/deadline.d.ts +133 -0
  95. package/types/warmup-fixture.d.ts +11 -0
  96. package/types/warmup.d.ts +45 -0
@@ -0,0 +1,401 @@
1
+ /**
2
+ * A parsed X.500 Name. `bytes` is the exact DER of the whole Name — the canonical identity for
3
+ * every comparison; `rdns` and `text` exist for constraint checks and log lines respectively.
4
+ * @typedef {object} DistinguishedName
5
+ * @property {Uint8Array} bytes
6
+ * @property {Array<Array<{ oid: string, value: string }>>} rdns one array per RDN, in order;
7
+ * non-string attribute values are rendered as '#hex'
8
+ * @property {string} text human-readable 'CN=..., O=...' form
9
+ */
10
+ /**
11
+ * Name ::= RDNSequence ::= SEQUENCE OF RelativeDistinguishedName (SET OF AttributeTypeAndValue).
12
+ *
13
+ * `bytes` is the exact DER of the whole Name and is the canonical form used for all issuer ==
14
+ * subject comparisons. RFC 5280 s7.1 also allows caseIgnore/whitespace-folded matching, but a CA
15
+ * that spells its own name two different ways between certificates breaks every deployed
16
+ * validator that matters (they compare bytes too), and a lax comparator is one more place to
17
+ * confuse two names. Exact bytes, fail closed.
18
+ *
19
+ * @param {Uint8Array} bytes
20
+ * @param {import('./der.js').Tlv} tlv
21
+ * @returns {DistinguishedName}
22
+ */
23
+ export function parseName(bytes: Uint8Array, tlv: import("./der.js").Tlv): DistinguishedName;
24
+ /**
25
+ * One GeneralSubtree, reduced to what constraint enforcement can act on. 'other' entries are
26
+ * forms this validator cannot enforce; path.js rejects the path when a critical extension
27
+ * carries one, which is why they are preserved rather than dropped.
28
+ * @typedef {{ type: 'dns', value: string } | { type: 'email', value: string }
29
+ * | { type: 'uri', value: string } | { type: 'ip', addr: Uint8Array, mask: Uint8Array }
30
+ * | { type: 'other', tag: number }} NameConstraintSubtree
31
+ */
32
+ /**
33
+ * @typedef {object} NameConstraints
34
+ * @property {ReadonlyArray<NameConstraintSubtree> | null} permitted
35
+ * @property {ReadonlyArray<NameConstraintSubtree> | null} excluded
36
+ */
37
+ /**
38
+ * NameConstraints (RFC 5280 s4.2.1.10). Subtrees we cannot enforce are preserved as
39
+ * `{type:'other'}` entries so path.js can refuse to ignore them when the extension is critical.
40
+ * A GeneralSubtree with minimum != 0 or maximum present is demoted to unsupported for the same
41
+ * reason: RFC 5280 forbids them, and enforcing a constraint we cannot interpret is worse than
42
+ * rejecting.
43
+ *
44
+ * @param {Uint8Array} valueBytes the extnValue content
45
+ * @returns {NameConstraints}
46
+ */
47
+ export function parseNameConstraints(valueBytes: Uint8Array): NameConstraints;
48
+ /**
49
+ * AlgorithmIdentifier ::= SEQUENCE { algorithm OID, parameters ANY OPTIONAL }.
50
+ *
51
+ * Exported for the OCSP checker, which meets the same structure in BasicOCSPResponse and CertID
52
+ * and must read it with the same strictness rather than a second, slightly different walk.
53
+ *
54
+ * @param {Uint8Array} bytes
55
+ * @param {import('./der.js').Tlv} tlv
56
+ * @param {string} what
57
+ * @returns {AlgorithmId}
58
+ */
59
+ export function parseAlgorithmIdentifier(bytes: Uint8Array, tlv: import("./der.js").Tlv, what: string): AlgorithmId;
60
+ /**
61
+ * How to verify one certificate signature. `scheme` indexes SIG_SCHEME_PARAMS where the OID
62
+ * fully determines it; for ECDSA only the hash is known here and path.js completes the plan
63
+ * from the issuer's curve.
64
+ * @typedef {object} SignaturePlan
65
+ * @property {'rsa-pkcs1' | 'rsa-pss' | 'ecdsa' | 'ed25519'} kind
66
+ * @property {number} [scheme]
67
+ * @property {'SHA-256' | 'SHA-384' | 'SHA-512'} [hash] weaker hashes died in the OID check
68
+ * @property {string} name for error messages
69
+ */
70
+ /**
71
+ * Map a certificate's signature algorithm to a verification plan, or throw.
72
+ *
73
+ * Called by path.js exactly when a certificate's signature is about to anchor trust, and by the
74
+ * OCSP checker for a response's own signature — the parameter is therefore the structural subset
75
+ * both can supply, and a full Certificate qualifies as-is. Weak algorithms (MD2/MD4/MD5, SHA-1)
76
+ * are rejected here by OID, before any cryptography runs — some runtimes' verifiers still accept
77
+ * SHA-1 and this one must provably not be among them. ECDSA returns only the hash: in X.509
78
+ * (unlike TLS) the curve belongs to the issuer's key, so path.js completes the plan from the
79
+ * issuer's SPKI.
80
+ *
81
+ * @param {{ signatureAlgorithm: AlgorithmId, subject: { text: string } }} cert
82
+ * @returns {SignaturePlan}
83
+ */
84
+ export function resolveSignatureScheme(cert: {
85
+ signatureAlgorithm: AlgorithmId;
86
+ subject: {
87
+ text: string;
88
+ };
89
+ }): SignaturePlan;
90
+ /**
91
+ * Parse a bare SubjectPublicKeyInfo element (as stored for trust anchors, which persist only the
92
+ * SPKI rather than a whole certificate). Same walk as inside a certificate.
93
+ * @param {Uint8Array} spkiDer
94
+ * @returns {Spki}
95
+ */
96
+ export function parseSubjectPublicKeyInfo(spkiDer: Uint8Array): Spki;
97
+ /**
98
+ * The certificate's signatureAlgorithm, with parameters kept both raw and as a Tlv because
99
+ * RSA-PSS resolution has to re-walk them.
100
+ * @typedef {object} AlgorithmId
101
+ * @property {string} oid
102
+ * @property {Uint8Array | null} paramsBytes
103
+ * @property {import('./der.js').Tlv | null} paramsTlv
104
+ * @property {Uint8Array} bytes the whole AlgorithmIdentifier element
105
+ */
106
+ /**
107
+ * A fully parsed certificate. Frozen; every byte field is a subarray of the original `der`.
108
+ * This is the complete shape behind the trimmed `ParsedCertificate` documented on the public
109
+ * verifyChain surface.
110
+ * @typedef {object} Certificate
111
+ * @property {Uint8Array} der the original bytes, never re-encoded
112
+ * @property {Uint8Array} tbsBytes exact TBSCertificate slice the signature covers
113
+ * @property {number} version 1, 2 or 3
114
+ * @property {string} serialNumber hex of the INTEGER content bytes
115
+ * @property {boolean} serialNegative negative serials are misissuance but must still parse
116
+ * @property {AlgorithmId} signatureAlgorithm
117
+ * @property {Uint8Array} signature
118
+ * @property {DistinguishedName} issuer
119
+ * @property {DistinguishedName} subject
120
+ * @property {number} notBefore epoch ms
121
+ * @property {number} notAfter epoch ms
122
+ * @property {Spki} spki
123
+ * @property {Map<string, { critical: boolean, valueBytes: Uint8Array }>} extensions by OID
124
+ * @property {{ present: boolean, ca: boolean, pathLenConstraint: number | null }} basicConstraints
125
+ * @property {KeyUsage | null} keyUsage null when the extension is absent
126
+ * @property {ReadonlyArray<string> | null} extendedKeyUsage KeyPurposeId OIDs
127
+ * @property {SubjectAltNames} subjectAltNames
128
+ * @property {Uint8Array | null} subjectKeyIdentifier
129
+ * @property {Uint8Array | null} authorityKeyIdentifier keyIdentifier field only
130
+ * @property {NameConstraints | null} nameConstraints
131
+ * @property {ReadonlyArray<string>} unknownCriticalExtensions OIDs path.js must reject on
132
+ * @property {boolean} isSelfIssued subject DER equals issuer DER
133
+ */
134
+ /**
135
+ * Parse one DER certificate into a frozen, fully-walked structure. Throws CertificateError
136
+ * (CERT_PARSE) on malformed or self-contradictory encodings; judgement about what the
137
+ * certificate MAY do lives in path.js.
138
+ *
139
+ * `tbsBytes` is the exact original slice of the TBSCertificate element — signature verification
140
+ * happens over these bytes and never over anything re-encoded.
141
+ *
142
+ * @param {Uint8Array} der
143
+ * @returns {Certificate}
144
+ */
145
+ export function parseCertificate(der: Uint8Array): Certificate;
146
+ /**
147
+ * Extract every CERTIFICATE block from PEM text as DER. Used for user-supplied trust anchors;
148
+ * TLS itself always delivers DER. Throws CERT_PARSE on bad base64 or when no block is found.
149
+ * @param {string} text
150
+ * @returns {Uint8Array[]}
151
+ */
152
+ export function decodePem(text: string): Uint8Array[];
153
+ export namespace OID {
154
+ let rsaEncryption: "1.2.840.113549.1.1.1";
155
+ let rsassaPss: "1.2.840.113549.1.1.10";
156
+ let ecPublicKey: "1.2.840.10045.2.1";
157
+ let ed25519: "1.3.101.112";
158
+ let ed448: "1.3.101.113";
159
+ let secp256r1: "1.2.840.10045.3.1.7";
160
+ let secp384r1: "1.3.132.0.34";
161
+ let secp521r1: "1.3.132.0.35";
162
+ let sha256WithRsa: "1.2.840.113549.1.1.11";
163
+ let sha384WithRsa: "1.2.840.113549.1.1.12";
164
+ let sha512WithRsa: "1.2.840.113549.1.1.13";
165
+ let ecdsaWithSha256: "1.2.840.10045.4.3.2";
166
+ let ecdsaWithSha384: "1.2.840.10045.4.3.3";
167
+ let ecdsaWithSha512: "1.2.840.10045.4.3.4";
168
+ let sha1: "1.3.14.3.2.26";
169
+ let sha256: "2.16.840.1.101.3.4.2.1";
170
+ let sha384: "2.16.840.1.101.3.4.2.2";
171
+ let sha512: "2.16.840.1.101.3.4.2.3";
172
+ let mgf1: "1.2.840.113549.1.1.8";
173
+ let subjectKeyIdentifier: "2.5.29.14";
174
+ let keyUsage: "2.5.29.15";
175
+ let subjectAltName: "2.5.29.17";
176
+ let issuerAltName: "2.5.29.18";
177
+ let basicConstraints: "2.5.29.19";
178
+ let nameConstraints: "2.5.29.30";
179
+ let crlDistributionPoints: "2.5.29.31";
180
+ let certificatePolicies: "2.5.29.32";
181
+ let authorityKeyIdentifier: "2.5.29.35";
182
+ let extendedKeyUsage: "2.5.29.37";
183
+ let freshestCrl: "2.5.29.46";
184
+ let authorityInfoAccess: "1.3.6.1.5.5.7.1.1";
185
+ let subjectInfoAccess: "1.3.6.1.5.5.7.1.11";
186
+ let sctList: "1.3.6.1.4.1.11129.2.4.2";
187
+ let serverAuth: "1.3.6.1.5.5.7.3.1";
188
+ let clientAuth: "1.3.6.1.5.5.7.3.2";
189
+ let ocspSigning: "1.3.6.1.5.5.7.3.9";
190
+ let anyExtendedKeyUsage: "2.5.29.37.0";
191
+ let ocspBasic: "1.3.6.1.5.5.7.48.1.1";
192
+ let ocspNonce: "1.3.6.1.5.5.7.48.1.2";
193
+ let ocspNocheck: "1.3.6.1.5.5.7.48.1.5";
194
+ }
195
+ /**
196
+ * Extensions this validator understands, or has deliberately judged safe to leave unprocessed
197
+ * even when marked critical. Everything else that is critical causes rejection in path.js
198
+ * (RFC 5280 s6.1: a relying party MUST reject on unrecognised critical extensions — ignoring
199
+ * them is the classic fail-open).
200
+ *
201
+ * certificatePolicies is listed because with no required policy set, RFC 5280 policy processing
202
+ * cannot fail; policyConstraints / inhibitAnyPolicy are deliberately NOT listed, because they
203
+ * make policy processing mandatory and we do not implement it — they are always critical, so
204
+ * their presence in a path rejects it.
205
+ */
206
+ export const KNOWN_EXTENSIONS: Set<"2.5.29.14" | "2.5.29.15" | "2.5.29.17" | "2.5.29.18" | "2.5.29.19" | "2.5.29.30" | "2.5.29.31" | "2.5.29.32" | "2.5.29.35" | "2.5.29.37" | "2.5.29.46" | "1.3.6.1.5.5.7.1.1" | "1.3.6.1.5.5.7.1.11" | "1.3.6.1.4.1.11129.2.4.2">;
207
+ /**
208
+ * A parsed X.500 Name. `bytes` is the exact DER of the whole Name — the canonical identity for
209
+ * every comparison; `rdns` and `text` exist for constraint checks and log lines respectively.
210
+ */
211
+ export type DistinguishedName = {
212
+ bytes: Uint8Array;
213
+ /**
214
+ * one array per RDN, in order;
215
+ * non-string attribute values are rendered as '#hex'
216
+ */
217
+ rdns: Array<Array<{
218
+ oid: string;
219
+ value: string;
220
+ }>>;
221
+ /**
222
+ * human-readable 'CN=..., O=...' form
223
+ */
224
+ text: string;
225
+ };
226
+ /**
227
+ * The nine RFC 5280 s4.2.1.3 bits, each explicit so a validator reads `false`, never
228
+ * `undefined` — an absent bit and an unset bit must be indistinguishable.
229
+ */
230
+ export type KeyUsage = {
231
+ digitalSignature: boolean;
232
+ nonRepudiation: boolean;
233
+ keyEncipherment: boolean;
234
+ dataEncipherment: boolean;
235
+ keyAgreement: boolean;
236
+ keyCertSign: boolean;
237
+ cRLSign: boolean;
238
+ encipherOnly: boolean;
239
+ decipherOnly: boolean;
240
+ };
241
+ /**
242
+ * The SAN entries identity matching consults. `present` distinguishes "no SAN extension"
243
+ * (matches nothing, by policy) from "SAN with no entries of this type".
244
+ */
245
+ export type SubjectAltNames = {
246
+ present: boolean;
247
+ dns: ReadonlyArray<string>;
248
+ /**
249
+ * raw 4- or 16-byte addresses
250
+ */
251
+ ip: ReadonlyArray<Uint8Array>;
252
+ uri: ReadonlyArray<string>;
253
+ email: ReadonlyArray<string>;
254
+ };
255
+ /**
256
+ * One GeneralSubtree, reduced to what constraint enforcement can act on. 'other' entries are
257
+ * forms this validator cannot enforce; path.js rejects the path when a critical extension
258
+ * carries one, which is why they are preserved rather than dropped.
259
+ */
260
+ export type NameConstraintSubtree = {
261
+ type: "dns";
262
+ value: string;
263
+ } | {
264
+ type: "email";
265
+ value: string;
266
+ } | {
267
+ type: "uri";
268
+ value: string;
269
+ } | {
270
+ type: "ip";
271
+ addr: Uint8Array;
272
+ mask: Uint8Array;
273
+ } | {
274
+ type: "other";
275
+ tag: number;
276
+ };
277
+ export type NameConstraints = {
278
+ permitted: ReadonlyArray<NameConstraintSubtree> | null;
279
+ excluded: ReadonlyArray<NameConstraintSubtree> | null;
280
+ };
281
+ /**
282
+ * How to verify one certificate signature. `scheme` indexes SIG_SCHEME_PARAMS where the OID
283
+ * fully determines it; for ECDSA only the hash is known here and path.js completes the plan
284
+ * from the issuer's curve.
285
+ */
286
+ export type SignaturePlan = {
287
+ kind: "rsa-pkcs1" | "rsa-pss" | "ecdsa" | "ed25519";
288
+ scheme?: number | undefined;
289
+ /**
290
+ * weaker hashes died in the OID check
291
+ */
292
+ hash?: "SHA-256" | "SHA-384" | "SHA-512" | undefined;
293
+ /**
294
+ * for error messages
295
+ */
296
+ name: string;
297
+ };
298
+ /**
299
+ * A parsed SubjectPublicKeyInfo. `spkiDer` is the exact original element — the bytes WebCrypto
300
+ * imports and the bytes SPKI pinning hashes, so it must never be a re-encoding.
301
+ */
302
+ export type Spki = {
303
+ algorithmOid: string;
304
+ /**
305
+ * named curve, EC keys only
306
+ */
307
+ curveOid: string | null;
308
+ /**
309
+ * the subjectPublicKey payload
310
+ */
311
+ keyBytes: Uint8Array;
312
+ spkiDer: Uint8Array;
313
+ };
314
+ /**
315
+ * The certificate's signatureAlgorithm, with parameters kept both raw and as a Tlv because
316
+ * RSA-PSS resolution has to re-walk them.
317
+ */
318
+ export type AlgorithmId = {
319
+ oid: string;
320
+ paramsBytes: Uint8Array | null;
321
+ paramsTlv: import("./der.js").Tlv | null;
322
+ /**
323
+ * the whole AlgorithmIdentifier element
324
+ */
325
+ bytes: Uint8Array;
326
+ };
327
+ /**
328
+ * A fully parsed certificate. Frozen; every byte field is a subarray of the original `der`.
329
+ * This is the complete shape behind the trimmed `ParsedCertificate` documented on the public
330
+ * verifyChain surface.
331
+ */
332
+ export type Certificate = {
333
+ /**
334
+ * the original bytes, never re-encoded
335
+ */
336
+ der: Uint8Array;
337
+ /**
338
+ * exact TBSCertificate slice the signature covers
339
+ */
340
+ tbsBytes: Uint8Array;
341
+ /**
342
+ * 1, 2 or 3
343
+ */
344
+ version: number;
345
+ /**
346
+ * hex of the INTEGER content bytes
347
+ */
348
+ serialNumber: string;
349
+ /**
350
+ * negative serials are misissuance but must still parse
351
+ */
352
+ serialNegative: boolean;
353
+ signatureAlgorithm: AlgorithmId;
354
+ signature: Uint8Array;
355
+ issuer: DistinguishedName;
356
+ subject: DistinguishedName;
357
+ /**
358
+ * epoch ms
359
+ */
360
+ notBefore: number;
361
+ /**
362
+ * epoch ms
363
+ */
364
+ notAfter: number;
365
+ spki: Spki;
366
+ /**
367
+ * by OID
368
+ */
369
+ extensions: Map<string, {
370
+ critical: boolean;
371
+ valueBytes: Uint8Array;
372
+ }>;
373
+ basicConstraints: {
374
+ present: boolean;
375
+ ca: boolean;
376
+ pathLenConstraint: number | null;
377
+ };
378
+ /**
379
+ * null when the extension is absent
380
+ */
381
+ keyUsage: KeyUsage | null;
382
+ /**
383
+ * KeyPurposeId OIDs
384
+ */
385
+ extendedKeyUsage: ReadonlyArray<string> | null;
386
+ subjectAltNames: SubjectAltNames;
387
+ subjectKeyIdentifier: Uint8Array | null;
388
+ /**
389
+ * keyIdentifier field only
390
+ */
391
+ authorityKeyIdentifier: Uint8Array | null;
392
+ nameConstraints: NameConstraints | null;
393
+ /**
394
+ * OIDs path.js must reject on
395
+ */
396
+ unknownCriticalExtensions: ReadonlyArray<string>;
397
+ /**
398
+ * subject DER equals issuer DER
399
+ */
400
+ isSelfIssued: boolean;
401
+ };
@@ -0,0 +1,183 @@
1
+ /**
2
+ * @param {Uint8Array[]} parts
3
+ * @param {number} [total] byte total, when the caller already counted; computed otherwise
4
+ * @returns {Uint8Array}
5
+ */
6
+ export function concat(parts: Uint8Array[], total?: number): Uint8Array;
7
+ /**
8
+ * Boyer-Moore is not worth it for 1-4 byte needles over small buffers.
9
+ * @param {Uint8Array} haystack
10
+ * @param {Uint8Array} needle
11
+ * @param {number} [from]
12
+ * @returns {number} index of the first occurrence at or after `from`, or -1
13
+ */
14
+ export function indexOf(haystack: Uint8Array, needle: Uint8Array, from?: number): number;
15
+ /**
16
+ * @param {Uint8Array} a
17
+ * @param {Uint8Array} b
18
+ * @returns {boolean}
19
+ */
20
+ export function equal(a: Uint8Array, b: Uint8Array): boolean;
21
+ /**
22
+ * Comparison whose running time does not depend on where the first difference is.
23
+ * JS cannot truly guarantee constant time (JIT tiering, GC), which is precisely why this package
24
+ * refuses MAC-then-encrypt cipher suites. It is used only where a timing leak would be a
25
+ * nice-to-have for an attacker rather than a decryption oracle: certificate pins and Finished
26
+ * verification, where the compared value is already authenticated or public.
27
+ * @param {Uint8Array} a
28
+ * @param {Uint8Array} b
29
+ * @returns {boolean}
30
+ */
31
+ export function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
32
+ /**
33
+ * @param {Uint8Array} bytes
34
+ * @returns {string}
35
+ */
36
+ export function toHex(bytes: Uint8Array): string;
37
+ /**
38
+ * @param {string} hex whitespace and ':' separators tolerated
39
+ * @returns {Uint8Array}
40
+ */
41
+ export function fromHex(hex: string): Uint8Array;
42
+ /**
43
+ * Latin-1 decode: HTTP header field values are opaque octets, not UTF-8.
44
+ * @param {Uint8Array} bytes
45
+ * @returns {string}
46
+ */
47
+ export function latin1(bytes: Uint8Array): string;
48
+ /**
49
+ * Big-endian integer writers, the byte order of every protocol in this package.
50
+ * @param {number} n
51
+ * @returns {Uint8Array}
52
+ */
53
+ export function u8(n: number): Uint8Array;
54
+ /** @param {number} n @returns {Uint8Array} */
55
+ export function u16(n: number): Uint8Array;
56
+ /** @param {number} n @returns {Uint8Array} */
57
+ export function u24(n: number): Uint8Array;
58
+ /** @param {number} n @returns {Uint8Array} */
59
+ export function u32(n: number): Uint8Array;
60
+ /** Raised when the peer stops sending in the middle of a structure we must read whole. */
61
+ export class UnexpectedEofError extends TunnelFetchError {
62
+ /**
63
+ * @param {number} wanted bytes the structure needed (-1 when scanning for a delimiter)
64
+ * @param {number} got bytes actually buffered when the stream ended
65
+ * @param {string} what the structure being read, named in the message
66
+ */
67
+ constructor(wanted: number, got: number, what: string);
68
+ }
69
+ /**
70
+ * Buffered reader over a ReadableStream<Uint8Array>.
71
+ *
72
+ * Returned slices may alias the stream's own chunks (or, on the BYOB path, buffers this class
73
+ * allocated and will never touch again); they are never written to by this class and must not
74
+ * be retained beyond the caller's immediate use if memory matters.
75
+ *
76
+ * When the source is a byte stream — on the target runtime, a socket's readable is one — the
77
+ * reader pulls with BYOB reads into large fresh views instead of taking the source's own
78
+ * chunking. This is measured, not stylistic: the runtime delivers socket data in chunks of at
79
+ * most 4096 bytes, ~1200 of them for a 4 MB body, and every chunk is a runtime/JS boundary
80
+ * crossing; a BYOB read hands over everything the transport has buffered (up to the view size)
81
+ * in one crossing, and resolves with a partial fill the instant anything at all is available,
82
+ * so delivery latency is unchanged. Sources that are not byte streams (every in-process
83
+ * ReadableStream in this package and its tests) take the default-reader path unchanged.
84
+ */
85
+ export class ByteReader {
86
+ /** @param {ReadableStream<Uint8Array>} readable */
87
+ constructor(readable: ReadableStream<Uint8Array>);
88
+ /** @type {ReadableStreamBYOBReader | null} */
89
+ _byob: ReadableStreamBYOBReader | null;
90
+ _reader: ReadableStreamBYOBReader | ReadableStreamDefaultReader<Uint8Array<ArrayBufferLike>> | null;
91
+ /** @type {Uint8Array[]} */
92
+ _chunks: Uint8Array[];
93
+ _head: number;
94
+ _len: number;
95
+ _eof: boolean;
96
+ _done: boolean;
97
+ get buffered(): number;
98
+ get atEof(): boolean;
99
+ /**
100
+ * Push bytes back to the front. Used when a layer over-reads (e.g. proxy replies with data).
101
+ * @param {Uint8Array} bytes
102
+ */
103
+ unshift(bytes: Uint8Array): void;
104
+ /**
105
+ * Pull one more chunk from the source. Returns false at EOF.
106
+ * The BYOB view is freshly allocated per read and never reused: _take hands out subarrays of
107
+ * buffered chunks, so recycling a view would rewrite bytes a parser already holds.
108
+ * @returns {Promise<boolean>} annotated because the tail-recursive skip of empty chunks
109
+ * defeats return-type inference
110
+ */
111
+ _pull(): Promise<boolean>;
112
+ /**
113
+ * Take exactly n bytes from the buffer. Caller guarantees _len >= n.
114
+ * @param {number} n
115
+ * @returns {Uint8Array}
116
+ */
117
+ _take(n: number): Uint8Array;
118
+ /**
119
+ * Read exactly n bytes, or throw. This is the workhorse for length-prefixed formats
120
+ * (TLS records, chunked bodies, SOCKS5 replies).
121
+ * @param {number} n
122
+ * @param {string} [what] described in the error if the stream ends early
123
+ * @returns {Promise<Uint8Array>}
124
+ */
125
+ readExactly(n: number, what?: string): Promise<Uint8Array>;
126
+ /**
127
+ * Read at least 1 and at most n bytes. Returns null at clean EOF.
128
+ * @param {number} [n]
129
+ * @returns {Promise<Uint8Array | null>}
130
+ */
131
+ readSome(n?: number): Promise<Uint8Array | null>;
132
+ /**
133
+ * Read until `needle` is found, returning everything up to and including it.
134
+ * Used for CRLF-delimited HTTP structures. Fails closed past `maxBytes` so a peer cannot
135
+ * make us buffer without bound.
136
+ * @param {Uint8Array} needle
137
+ * @param {number} maxBytes
138
+ * @param {string} [what]
139
+ * @returns {Promise<Uint8Array>}
140
+ */
141
+ readUntil(needle: Uint8Array, maxBytes: number, what?: string): Promise<Uint8Array>;
142
+ /**
143
+ * Drain everything remaining, up to maxBytes.
144
+ * @param {number} [maxBytes]
145
+ * @returns {Promise<Uint8Array>}
146
+ */
147
+ readToEnd(maxBytes?: number): Promise<Uint8Array>;
148
+ releaseLock(): void;
149
+ /** @param {unknown} [reason] */
150
+ cancel(reason?: unknown): Promise<void>;
151
+ }
152
+ /** Buffered writer over a WritableStream<Uint8Array>. */
153
+ export class ByteWriter {
154
+ /** @param {WritableStream<Uint8Array>} writable */
155
+ constructor(writable: WritableStream<Uint8Array>);
156
+ _writer: WritableStreamDefaultWriter<Uint8Array<ArrayBufferLike>>;
157
+ _done: boolean;
158
+ /**
159
+ * @param {Uint8Array} bytes
160
+ * @returns {Promise<void>}
161
+ */
162
+ write(bytes: Uint8Array): Promise<void>;
163
+ /**
164
+ * Write several buffers as one, avoiding per-piece stream overhead.
165
+ * @param {Uint8Array[]} parts
166
+ * @returns {Promise<void>}
167
+ */
168
+ writeAll(parts: Uint8Array[]): Promise<void>;
169
+ releaseLock(): void;
170
+ close(): Promise<void>;
171
+ /** @param {unknown} [reason] */
172
+ abort(reason?: unknown): Promise<void>;
173
+ }
174
+ /** @type {(s: string) => Uint8Array} */
175
+ export const utf8: (s: string) => Uint8Array;
176
+ /** Big-endian integer readers over a byte view at offset `o`. */
177
+ /** @type {(b: Uint8Array, o?: number) => number} */
178
+ export const readU16: (b: Uint8Array, o?: number) => number;
179
+ /** @type {(b: Uint8Array, o?: number) => number} */
180
+ export const readU24: (b: Uint8Array, o?: number) => number;
181
+ /** @type {(b: Uint8Array, o?: number) => number} */
182
+ export const readU32: (b: Uint8Array, o?: number) => number;
183
+ import { TunnelFetchError } from '../errors.js';