undici 7.14.0 → 7.16.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 (63) hide show
  1. package/README.md +1 -1
  2. package/docs/docs/api/Agent.md +1 -0
  3. package/docs/docs/api/Dispatcher.md +59 -0
  4. package/docs/docs/api/Errors.md +0 -1
  5. package/index-fetch.js +2 -2
  6. package/index.js +6 -9
  7. package/lib/api/api-request.js +22 -8
  8. package/lib/api/readable.js +7 -5
  9. package/lib/core/errors.js +217 -13
  10. package/lib/core/request.js +5 -1
  11. package/lib/core/util.js +45 -11
  12. package/lib/dispatcher/agent.js +44 -23
  13. package/lib/dispatcher/client-h1.js +20 -9
  14. package/lib/dispatcher/client-h2.js +13 -3
  15. package/lib/dispatcher/client.js +57 -57
  16. package/lib/dispatcher/dispatcher-base.js +12 -7
  17. package/lib/dispatcher/env-http-proxy-agent.js +12 -16
  18. package/lib/dispatcher/fixed-queue.js +15 -39
  19. package/lib/dispatcher/h2c-client.js +6 -6
  20. package/lib/dispatcher/pool-base.js +60 -43
  21. package/lib/dispatcher/pool.js +2 -2
  22. package/lib/dispatcher/proxy-agent.js +14 -9
  23. package/lib/global.js +19 -1
  24. package/lib/interceptor/cache.js +61 -0
  25. package/lib/interceptor/decompress.js +253 -0
  26. package/lib/llhttp/constants.d.ts +99 -1
  27. package/lib/llhttp/constants.js +34 -1
  28. package/lib/llhttp/llhttp-wasm.js +1 -1
  29. package/lib/llhttp/llhttp_simd-wasm.js +1 -1
  30. package/lib/llhttp/utils.d.ts +2 -2
  31. package/lib/llhttp/utils.js +3 -6
  32. package/lib/mock/mock-agent.js +4 -4
  33. package/lib/mock/mock-errors.js +10 -0
  34. package/lib/mock/mock-utils.js +12 -10
  35. package/lib/util/cache.js +6 -7
  36. package/lib/util/date.js +534 -140
  37. package/lib/web/cookies/index.js +1 -1
  38. package/lib/web/cookies/parse.js +2 -2
  39. package/lib/web/eventsource/eventsource-stream.js +2 -2
  40. package/lib/web/eventsource/eventsource.js +34 -29
  41. package/lib/web/eventsource/util.js +1 -9
  42. package/lib/web/fetch/body.js +20 -26
  43. package/lib/web/fetch/index.js +15 -16
  44. package/lib/web/fetch/response.js +2 -4
  45. package/lib/web/fetch/util.js +8 -230
  46. package/lib/web/subresource-integrity/Readme.md +9 -0
  47. package/lib/web/subresource-integrity/subresource-integrity.js +306 -0
  48. package/lib/web/webidl/index.js +203 -42
  49. package/lib/web/websocket/connection.js +4 -3
  50. package/lib/web/websocket/events.js +1 -1
  51. package/lib/web/websocket/stream/websocketerror.js +22 -1
  52. package/lib/web/websocket/stream/websocketstream.js +16 -7
  53. package/lib/web/websocket/websocket.js +32 -42
  54. package/package.json +9 -7
  55. package/types/agent.d.ts +1 -0
  56. package/types/diagnostics-channel.d.ts +0 -1
  57. package/types/errors.d.ts +5 -15
  58. package/types/interceptors.d.ts +5 -0
  59. package/types/snapshot-agent.d.ts +5 -3
  60. package/types/webidl.d.ts +82 -21
  61. package/lib/api/util.js +0 -95
  62. package/lib/llhttp/constants.js.map +0 -1
  63. package/lib/llhttp/utils.js.map +0 -1
@@ -0,0 +1,306 @@
1
+ 'use strict'
2
+
3
+ const assert = require('node:assert')
4
+
5
+ /**
6
+ * @typedef {object} Metadata
7
+ * @property {SRIHashAlgorithm} alg - The algorithm used for the hash.
8
+ * @property {string} val - The base64-encoded hash value.
9
+ */
10
+
11
+ /**
12
+ * @typedef {Metadata[]} MetadataList
13
+ */
14
+
15
+ /**
16
+ * @typedef {('sha256' | 'sha384' | 'sha512')} SRIHashAlgorithm
17
+ */
18
+
19
+ /**
20
+ * @type {Map<SRIHashAlgorithm, number>}
21
+ *
22
+ * The valid SRI hash algorithm token set is the ordered set « "sha256",
23
+ * "sha384", "sha512" » (corresponding to SHA-256, SHA-384, and SHA-512
24
+ * respectively). The ordering of this set is meaningful, with stronger
25
+ * algorithms appearing later in the set.
26
+ *
27
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#valid-sri-hash-algorithm-token-set
28
+ */
29
+ const validSRIHashAlgorithmTokenSet = new Map([['sha256', 0], ['sha384', 1], ['sha512', 2]])
30
+
31
+ // https://nodejs.org/api/crypto.html#determining-if-crypto-support-is-unavailable
32
+ /** @type {import('crypto')} */
33
+ let crypto
34
+ try {
35
+ crypto = require('node:crypto')
36
+ const cryptoHashes = crypto.getHashes()
37
+
38
+ // If no hashes are available, we cannot support SRI.
39
+ if (cryptoHashes.length === 0) {
40
+ validSRIHashAlgorithmTokenSet.clear()
41
+ }
42
+
43
+ for (const algorithm of validSRIHashAlgorithmTokenSet.keys()) {
44
+ // If the algorithm is not supported, remove it from the list.
45
+ if (cryptoHashes.includes(algorithm) === false) {
46
+ validSRIHashAlgorithmTokenSet.delete(algorithm)
47
+ }
48
+ }
49
+ /* c8 ignore next 4 */
50
+ } catch {
51
+ // If crypto is not available, we cannot support SRI.
52
+ validSRIHashAlgorithmTokenSet.clear()
53
+ }
54
+
55
+ /**
56
+ * @typedef GetSRIHashAlgorithmIndex
57
+ * @type {(algorithm: SRIHashAlgorithm) => number}
58
+ * @param {SRIHashAlgorithm} algorithm
59
+ * @returns {number} The index of the algorithm in the valid SRI hash algorithm
60
+ * token set.
61
+ */
62
+
63
+ const getSRIHashAlgorithmIndex = /** @type {GetSRIHashAlgorithmIndex} */ (Map.prototype.get.bind(
64
+ validSRIHashAlgorithmTokenSet))
65
+
66
+ /**
67
+ * @typedef IsValidSRIHashAlgorithm
68
+ * @type {(algorithm: string) => algorithm is SRIHashAlgorithm}
69
+ * @param {*} algorithm
70
+ * @returns {algorithm is SRIHashAlgorithm}
71
+ */
72
+
73
+ const isValidSRIHashAlgorithm = /** @type {IsValidSRIHashAlgorithm} */ (
74
+ Map.prototype.has.bind(validSRIHashAlgorithmTokenSet)
75
+ )
76
+
77
+ /**
78
+ * @param {Uint8Array} bytes
79
+ * @param {string} metadataList
80
+ * @returns {boolean}
81
+ *
82
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#does-response-match-metadatalist
83
+ */
84
+ const bytesMatch = crypto === undefined || validSRIHashAlgorithmTokenSet.size === 0
85
+ // If node is not built with OpenSSL support, we cannot check
86
+ // a request's integrity, so allow it by default (the spec will
87
+ // allow requests if an invalid hash is given, as precedence).
88
+ ? () => true
89
+ : (bytes, metadataList) => {
90
+ // 1. Let parsedMetadata be the result of parsing metadataList.
91
+ const parsedMetadata = parseMetadata(metadataList)
92
+
93
+ // 2. If parsedMetadata is empty set, return true.
94
+ if (parsedMetadata.length === 0) {
95
+ return true
96
+ }
97
+
98
+ // 3. Let metadata be the result of getting the strongest
99
+ // metadata from parsedMetadata.
100
+ const metadata = getStrongestMetadata(parsedMetadata)
101
+
102
+ // 4. For each item in metadata:
103
+ for (const item of metadata) {
104
+ // 1. Let algorithm be the item["alg"].
105
+ const algorithm = item.alg
106
+
107
+ // 2. Let expectedValue be the item["val"].
108
+ const expectedValue = item.val
109
+
110
+ // See https://github.com/web-platform-tests/wpt/commit/e4c5cc7a5e48093220528dfdd1c4012dc3837a0e
111
+ // "be liberal with padding". This is annoying, and it's not even in the spec.
112
+
113
+ // 3. Let actualValue be the result of applying algorithm to bytes .
114
+ const actualValue = applyAlgorithmToBytes(algorithm, bytes)
115
+
116
+ // 4. If actualValue is a case-sensitive match for expectedValue,
117
+ // return true.
118
+ if (caseSensitiveMatch(actualValue, expectedValue)) {
119
+ return true
120
+ }
121
+ }
122
+
123
+ // 5. Return false.
124
+ return false
125
+ }
126
+
127
+ /**
128
+ * @param {MetadataList} metadataList
129
+ * @returns {MetadataList} The strongest hash algorithm from the metadata list.
130
+ */
131
+ function getStrongestMetadata (metadataList) {
132
+ // 1. Let result be the empty set and strongest be the empty string.
133
+ const result = []
134
+ /** @type {Metadata|null} */
135
+ let strongest = null
136
+
137
+ // 2. For each item in set:
138
+ for (const item of metadataList) {
139
+ // 1. Assert: item["alg"] is a valid SRI hash algorithm token.
140
+ assert(isValidSRIHashAlgorithm(item.alg), 'Invalid SRI hash algorithm token')
141
+
142
+ // 2. If result is the empty set, then:
143
+ if (result.length === 0) {
144
+ // 1. Append item to result.
145
+ result.push(item)
146
+
147
+ // 2. Set strongest to item.
148
+ strongest = item
149
+
150
+ // 3. Continue.
151
+ continue
152
+ }
153
+
154
+ // 3. Let currentAlgorithm be strongest["alg"], and currentAlgorithmIndex be
155
+ // the index of currentAlgorithm in the valid SRI hash algorithm token set.
156
+ const currentAlgorithm = /** @type {Metadata} */ (strongest).alg
157
+ const currentAlgorithmIndex = getSRIHashAlgorithmIndex(currentAlgorithm)
158
+
159
+ // 4. Let newAlgorithm be the item["alg"], and newAlgorithmIndex be the
160
+ // index of newAlgorithm in the valid SRI hash algorithm token set.
161
+ const newAlgorithm = item.alg
162
+ const newAlgorithmIndex = getSRIHashAlgorithmIndex(newAlgorithm)
163
+
164
+ // 5. If newAlgorithmIndex is less than currentAlgorithmIndex, then continue.
165
+ if (newAlgorithmIndex < currentAlgorithmIndex) {
166
+ continue
167
+
168
+ // 6. Otherwise, if newAlgorithmIndex is greater than
169
+ // currentAlgorithmIndex:
170
+ } else if (newAlgorithmIndex > currentAlgorithmIndex) {
171
+ // 1. Set strongest to item.
172
+ strongest = item
173
+
174
+ // 2. Set result to « item ».
175
+ result[0] = item
176
+ result.length = 1
177
+
178
+ // 7. Otherwise, newAlgorithmIndex and currentAlgorithmIndex are the same
179
+ // value. Append item to result.
180
+ } else {
181
+ result.push(item)
182
+ }
183
+ }
184
+
185
+ // 3. Return result.
186
+ return result
187
+ }
188
+
189
+ /**
190
+ * @param {string} metadata
191
+ * @returns {MetadataList}
192
+ *
193
+ * @see https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
194
+ */
195
+ function parseMetadata (metadata) {
196
+ // 1. Let result be the empty set.
197
+ /** @type {MetadataList} */
198
+ const result = []
199
+
200
+ // 2. For each item returned by splitting metadata on spaces:
201
+ for (const item of metadata.split(' ')) {
202
+ // 1. Let expression-and-options be the result of splitting item on U+003F (?).
203
+ const expressionAndOptions = item.split('?', 1)
204
+
205
+ // 2. Let algorithm-expression be expression-and-options[0].
206
+ const algorithmExpression = expressionAndOptions[0]
207
+
208
+ // 3. Let base64-value be the empty string.
209
+ let base64Value = ''
210
+
211
+ // 4. Let algorithm-and-value be the result of splitting algorithm-expression on U+002D (-).
212
+ const algorithmAndValue = [algorithmExpression.slice(0, 6), algorithmExpression.slice(7)]
213
+
214
+ // 5. Let algorithm be algorithm-and-value[0].
215
+ const algorithm = algorithmAndValue[0]
216
+
217
+ // 6. If algorithm is not a valid SRI hash algorithm token, then continue.
218
+ if (!isValidSRIHashAlgorithm(algorithm)) {
219
+ continue
220
+ }
221
+
222
+ // 7. If algorithm-and-value[1] exists, set base64-value to
223
+ // algorithm-and-value[1].
224
+ if (algorithmAndValue[1]) {
225
+ base64Value = algorithmAndValue[1]
226
+ }
227
+
228
+ // 8. Let metadata be the ordered map
229
+ // «["alg" → algorithm, "val" → base64-value]».
230
+ const metadata = {
231
+ alg: algorithm,
232
+ val: base64Value
233
+ }
234
+
235
+ // 9. Append metadata to result.
236
+ result.push(metadata)
237
+ }
238
+
239
+ // 3. Return result.
240
+ return result
241
+ }
242
+
243
+ /**
244
+ * Applies the specified hash algorithm to the given bytes
245
+ *
246
+ * @typedef {(algorithm: SRIHashAlgorithm, bytes: Uint8Array) => string} ApplyAlgorithmToBytes
247
+ * @param {SRIHashAlgorithm} algorithm
248
+ * @param {Uint8Array} bytes
249
+ * @returns {string}
250
+ */
251
+ const applyAlgorithmToBytes = (algorithm, bytes) => {
252
+ return crypto.hash(algorithm, bytes, 'base64')
253
+ }
254
+
255
+ /**
256
+ * Compares two base64 strings, allowing for base64url
257
+ * in the second string.
258
+ *
259
+ * @param {string} actualValue base64 encoded string
260
+ * @param {string} expectedValue base64 or base64url encoded string
261
+ * @returns {boolean}
262
+ */
263
+ function caseSensitiveMatch (actualValue, expectedValue) {
264
+ // Ignore padding characters from the end of the strings by
265
+ // decreasing the length by 1 or 2 if the last characters are `=`.
266
+ let actualValueLength = actualValue.length
267
+ if (actualValueLength !== 0 && actualValue[actualValueLength - 1] === '=') {
268
+ actualValueLength -= 1
269
+ }
270
+ if (actualValueLength !== 0 && actualValue[actualValueLength - 1] === '=') {
271
+ actualValueLength -= 1
272
+ }
273
+ let expectedValueLength = expectedValue.length
274
+ if (expectedValueLength !== 0 && expectedValue[expectedValueLength - 1] === '=') {
275
+ expectedValueLength -= 1
276
+ }
277
+ if (expectedValueLength !== 0 && expectedValue[expectedValueLength - 1] === '=') {
278
+ expectedValueLength -= 1
279
+ }
280
+
281
+ if (actualValueLength !== expectedValueLength) {
282
+ return false
283
+ }
284
+
285
+ for (let i = 0; i < actualValueLength; ++i) {
286
+ if (
287
+ actualValue[i] === expectedValue[i] ||
288
+ (actualValue[i] === '+' && expectedValue[i] === '-') ||
289
+ (actualValue[i] === '/' && expectedValue[i] === '_')
290
+ ) {
291
+ continue
292
+ }
293
+ return false
294
+ }
295
+
296
+ return true
297
+ }
298
+
299
+ module.exports = {
300
+ applyAlgorithmToBytes,
301
+ bytesMatch,
302
+ caseSensitiveMatch,
303
+ isValidSRIHashAlgorithm,
304
+ getStrongestMetadata,
305
+ parseMetadata
306
+ }