undici 6.19.7 → 7.0.0-alpha.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.
Files changed (115) hide show
  1. package/README.md +5 -9
  2. package/docs/docs/api/Agent.md +0 -3
  3. package/docs/docs/api/Client.md +0 -2
  4. package/docs/docs/api/Dispatcher.md +204 -6
  5. package/docs/docs/api/EnvHttpProxyAgent.md +0 -1
  6. package/docs/docs/api/Fetch.md +1 -0
  7. package/docs/docs/api/Pool.md +0 -1
  8. package/docs/docs/api/RetryHandler.md +1 -1
  9. package/index.js +0 -4
  10. package/lib/api/api-connect.js +3 -1
  11. package/lib/api/api-pipeline.js +3 -4
  12. package/lib/api/api-request.js +29 -46
  13. package/lib/api/api-stream.js +36 -49
  14. package/lib/api/api-upgrade.js +5 -3
  15. package/lib/api/readable.js +71 -27
  16. package/lib/core/connect.js +39 -24
  17. package/lib/core/errors.js +17 -4
  18. package/lib/core/request.js +7 -5
  19. package/lib/core/symbols.js +0 -1
  20. package/lib/core/tree.js +6 -0
  21. package/lib/core/util.js +1 -11
  22. package/lib/dispatcher/agent.js +3 -17
  23. package/lib/dispatcher/balanced-pool.js +27 -11
  24. package/lib/dispatcher/client-h1.js +44 -39
  25. package/lib/dispatcher/client.js +3 -27
  26. package/lib/dispatcher/dispatcher-base.js +2 -34
  27. package/lib/dispatcher/dispatcher.js +3 -24
  28. package/lib/dispatcher/pool.js +3 -6
  29. package/lib/dispatcher/proxy-agent.js +3 -6
  30. package/lib/handler/decorator-handler.js +24 -0
  31. package/lib/handler/redirect-handler.js +9 -0
  32. package/lib/handler/retry-handler.js +22 -3
  33. package/lib/interceptor/dump.js +2 -2
  34. package/lib/interceptor/redirect.js +11 -14
  35. package/lib/interceptor/response-error.js +89 -0
  36. package/lib/llhttp/constants.d.ts +97 -0
  37. package/lib/llhttp/constants.js +412 -192
  38. package/lib/llhttp/constants.js.map +1 -0
  39. package/lib/llhttp/llhttp-wasm.js +11 -1
  40. package/lib/llhttp/llhttp_simd-wasm.js +11 -1
  41. package/lib/llhttp/utils.d.ts +2 -0
  42. package/lib/llhttp/utils.js +9 -9
  43. package/lib/llhttp/utils.js.map +1 -0
  44. package/lib/mock/mock-client.js +2 -2
  45. package/lib/mock/mock-pool.js +2 -2
  46. package/lib/mock/mock-symbols.js +1 -0
  47. package/lib/util/timers.js +324 -44
  48. package/lib/web/cookies/index.js +15 -13
  49. package/lib/web/cookies/parse.js +2 -2
  50. package/lib/web/eventsource/eventsource-stream.js +9 -8
  51. package/lib/web/eventsource/eventsource.js +10 -6
  52. package/lib/web/fetch/body.js +31 -11
  53. package/lib/web/fetch/data-url.js +1 -1
  54. package/lib/web/fetch/formdata-parser.js +1 -2
  55. package/lib/web/fetch/formdata.js +28 -37
  56. package/lib/web/fetch/headers.js +1 -1
  57. package/lib/web/fetch/index.js +7 -8
  58. package/lib/web/fetch/request.js +11 -28
  59. package/lib/web/fetch/response.js +13 -41
  60. package/lib/web/fetch/symbols.js +0 -1
  61. package/lib/web/fetch/util.js +3 -12
  62. package/lib/web/fetch/webidl.js +73 -62
  63. package/lib/web/websocket/connection.js +26 -174
  64. package/lib/web/websocket/constants.js +1 -1
  65. package/lib/web/websocket/frame.js +45 -3
  66. package/lib/web/websocket/receiver.js +28 -26
  67. package/lib/web/websocket/sender.js +18 -13
  68. package/lib/web/websocket/util.js +20 -74
  69. package/lib/web/websocket/websocket.js +294 -70
  70. package/package.json +16 -29
  71. package/scripts/strip-comments.js +3 -1
  72. package/types/agent.d.ts +7 -7
  73. package/types/api.d.ts +24 -24
  74. package/types/balanced-pool.d.ts +11 -11
  75. package/types/client.d.ts +11 -12
  76. package/types/diagnostics-channel.d.ts +10 -10
  77. package/types/dispatcher.d.ts +96 -97
  78. package/types/env-http-proxy-agent.d.ts +2 -2
  79. package/types/errors.d.ts +53 -47
  80. package/types/eventsource.d.ts +0 -2
  81. package/types/fetch.d.ts +8 -8
  82. package/types/formdata.d.ts +7 -7
  83. package/types/global-dispatcher.d.ts +4 -4
  84. package/types/global-origin.d.ts +5 -5
  85. package/types/handlers.d.ts +4 -4
  86. package/types/header.d.ts +157 -1
  87. package/types/index.d.ts +42 -46
  88. package/types/interceptors.d.ts +10 -8
  89. package/types/mock-agent.d.ts +18 -18
  90. package/types/mock-client.d.ts +4 -4
  91. package/types/mock-errors.d.ts +3 -3
  92. package/types/mock-interceptor.d.ts +19 -19
  93. package/types/mock-pool.d.ts +4 -4
  94. package/types/patch.d.ts +0 -42
  95. package/types/pool-stats.d.ts +8 -8
  96. package/types/pool.d.ts +12 -12
  97. package/types/proxy-agent.d.ts +4 -4
  98. package/types/readable.d.ts +14 -9
  99. package/types/retry-agent.d.ts +1 -1
  100. package/types/retry-handler.d.ts +8 -8
  101. package/types/util.d.ts +3 -3
  102. package/types/utility.d.ts +7 -0
  103. package/types/webidl.d.ts +22 -4
  104. package/types/websocket.d.ts +1 -3
  105. package/docs/docs/api/DispatchInterceptor.md +0 -60
  106. package/lib/interceptor/redirect-interceptor.js +0 -21
  107. package/lib/web/fetch/file.js +0 -126
  108. package/lib/web/fileapi/encoding.js +0 -290
  109. package/lib/web/fileapi/filereader.js +0 -344
  110. package/lib/web/fileapi/progressevent.js +0 -78
  111. package/lib/web/fileapi/symbols.js +0 -10
  112. package/lib/web/fileapi/util.js +0 -391
  113. package/lib/web/websocket/symbols.js +0 -12
  114. package/types/file.d.ts +0 -39
  115. package/types/filereader.d.ts +0 -54
@@ -1,391 +0,0 @@
1
- 'use strict'
2
-
3
- const {
4
- kState,
5
- kError,
6
- kResult,
7
- kAborted,
8
- kLastProgressEventFired
9
- } = require('./symbols')
10
- const { ProgressEvent } = require('./progressevent')
11
- const { getEncoding } = require('./encoding')
12
- const { serializeAMimeType, parseMIMEType } = require('../fetch/data-url')
13
- const { types } = require('node:util')
14
- const { StringDecoder } = require('string_decoder')
15
- const { btoa } = require('node:buffer')
16
-
17
- /** @type {PropertyDescriptor} */
18
- const staticPropertyDescriptors = {
19
- enumerable: true,
20
- writable: false,
21
- configurable: false
22
- }
23
-
24
- /**
25
- * @see https://w3c.github.io/FileAPI/#readOperation
26
- * @param {import('./filereader').FileReader} fr
27
- * @param {import('buffer').Blob} blob
28
- * @param {string} type
29
- * @param {string?} encodingName
30
- */
31
- function readOperation (fr, blob, type, encodingName) {
32
- // 1. If fr’s state is "loading", throw an InvalidStateError
33
- // DOMException.
34
- if (fr[kState] === 'loading') {
35
- throw new DOMException('Invalid state', 'InvalidStateError')
36
- }
37
-
38
- // 2. Set fr’s state to "loading".
39
- fr[kState] = 'loading'
40
-
41
- // 3. Set fr’s result to null.
42
- fr[kResult] = null
43
-
44
- // 4. Set fr’s error to null.
45
- fr[kError] = null
46
-
47
- // 5. Let stream be the result of calling get stream on blob.
48
- /** @type {import('stream/web').ReadableStream} */
49
- const stream = blob.stream()
50
-
51
- // 6. Let reader be the result of getting a reader from stream.
52
- const reader = stream.getReader()
53
-
54
- // 7. Let bytes be an empty byte sequence.
55
- /** @type {Uint8Array[]} */
56
- const bytes = []
57
-
58
- // 8. Let chunkPromise be the result of reading a chunk from
59
- // stream with reader.
60
- let chunkPromise = reader.read()
61
-
62
- // 9. Let isFirstChunk be true.
63
- let isFirstChunk = true
64
-
65
- // 10. In parallel, while true:
66
- // Note: "In parallel" just means non-blocking
67
- // Note 2: readOperation itself cannot be async as double
68
- // reading the body would then reject the promise, instead
69
- // of throwing an error.
70
- ;(async () => {
71
- while (!fr[kAborted]) {
72
- // 1. Wait for chunkPromise to be fulfilled or rejected.
73
- try {
74
- const { done, value } = await chunkPromise
75
-
76
- // 2. If chunkPromise is fulfilled, and isFirstChunk is
77
- // true, queue a task to fire a progress event called
78
- // loadstart at fr.
79
- if (isFirstChunk && !fr[kAborted]) {
80
- queueMicrotask(() => {
81
- fireAProgressEvent('loadstart', fr)
82
- })
83
- }
84
-
85
- // 3. Set isFirstChunk to false.
86
- isFirstChunk = false
87
-
88
- // 4. If chunkPromise is fulfilled with an object whose
89
- // done property is false and whose value property is
90
- // a Uint8Array object, run these steps:
91
- if (!done && types.isUint8Array(value)) {
92
- // 1. Let bs be the byte sequence represented by the
93
- // Uint8Array object.
94
-
95
- // 2. Append bs to bytes.
96
- bytes.push(value)
97
-
98
- // 3. If roughly 50ms have passed since these steps
99
- // were last invoked, queue a task to fire a
100
- // progress event called progress at fr.
101
- if (
102
- (
103
- fr[kLastProgressEventFired] === undefined ||
104
- Date.now() - fr[kLastProgressEventFired] >= 50
105
- ) &&
106
- !fr[kAborted]
107
- ) {
108
- fr[kLastProgressEventFired] = Date.now()
109
- queueMicrotask(() => {
110
- fireAProgressEvent('progress', fr)
111
- })
112
- }
113
-
114
- // 4. Set chunkPromise to the result of reading a
115
- // chunk from stream with reader.
116
- chunkPromise = reader.read()
117
- } else if (done) {
118
- // 5. Otherwise, if chunkPromise is fulfilled with an
119
- // object whose done property is true, queue a task
120
- // to run the following steps and abort this algorithm:
121
- queueMicrotask(() => {
122
- // 1. Set fr’s state to "done".
123
- fr[kState] = 'done'
124
-
125
- // 2. Let result be the result of package data given
126
- // bytes, type, blob’s type, and encodingName.
127
- try {
128
- const result = packageData(bytes, type, blob.type, encodingName)
129
-
130
- // 4. Else:
131
-
132
- if (fr[kAborted]) {
133
- return
134
- }
135
-
136
- // 1. Set fr’s result to result.
137
- fr[kResult] = result
138
-
139
- // 2. Fire a progress event called load at the fr.
140
- fireAProgressEvent('load', fr)
141
- } catch (error) {
142
- // 3. If package data threw an exception error:
143
-
144
- // 1. Set fr’s error to error.
145
- fr[kError] = error
146
-
147
- // 2. Fire a progress event called error at fr.
148
- fireAProgressEvent('error', fr)
149
- }
150
-
151
- // 5. If fr’s state is not "loading", fire a progress
152
- // event called loadend at the fr.
153
- if (fr[kState] !== 'loading') {
154
- fireAProgressEvent('loadend', fr)
155
- }
156
- })
157
-
158
- break
159
- }
160
- } catch (error) {
161
- if (fr[kAborted]) {
162
- return
163
- }
164
-
165
- // 6. Otherwise, if chunkPromise is rejected with an
166
- // error error, queue a task to run the following
167
- // steps and abort this algorithm:
168
- queueMicrotask(() => {
169
- // 1. Set fr’s state to "done".
170
- fr[kState] = 'done'
171
-
172
- // 2. Set fr’s error to error.
173
- fr[kError] = error
174
-
175
- // 3. Fire a progress event called error at fr.
176
- fireAProgressEvent('error', fr)
177
-
178
- // 4. If fr’s state is not "loading", fire a progress
179
- // event called loadend at fr.
180
- if (fr[kState] !== 'loading') {
181
- fireAProgressEvent('loadend', fr)
182
- }
183
- })
184
-
185
- break
186
- }
187
- }
188
- })()
189
- }
190
-
191
- /**
192
- * @see https://w3c.github.io/FileAPI/#fire-a-progress-event
193
- * @see https://dom.spec.whatwg.org/#concept-event-fire
194
- * @param {string} e The name of the event
195
- * @param {import('./filereader').FileReader} reader
196
- */
197
- function fireAProgressEvent (e, reader) {
198
- // The progress event e does not bubble. e.bubbles must be false
199
- // The progress event e is NOT cancelable. e.cancelable must be false
200
- const event = new ProgressEvent(e, {
201
- bubbles: false,
202
- cancelable: false
203
- })
204
-
205
- reader.dispatchEvent(event)
206
- }
207
-
208
- /**
209
- * @see https://w3c.github.io/FileAPI/#blob-package-data
210
- * @param {Uint8Array[]} bytes
211
- * @param {string} type
212
- * @param {string?} mimeType
213
- * @param {string?} encodingName
214
- */
215
- function packageData (bytes, type, mimeType, encodingName) {
216
- // 1. A Blob has an associated package data algorithm, given
217
- // bytes, a type, a optional mimeType, and a optional
218
- // encodingName, which switches on type and runs the
219
- // associated steps:
220
-
221
- switch (type) {
222
- case 'DataURL': {
223
- // 1. Return bytes as a DataURL [RFC2397] subject to
224
- // the considerations below:
225
- // * Use mimeType as part of the Data URL if it is
226
- // available in keeping with the Data URL
227
- // specification [RFC2397].
228
- // * If mimeType is not available return a Data URL
229
- // without a media-type. [RFC2397].
230
-
231
- // https://datatracker.ietf.org/doc/html/rfc2397#section-3
232
- // dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
233
- // mediatype := [ type "/" subtype ] *( ";" parameter )
234
- // data := *urlchar
235
- // parameter := attribute "=" value
236
- let dataURL = 'data:'
237
-
238
- const parsed = parseMIMEType(mimeType || 'application/octet-stream')
239
-
240
- if (parsed !== 'failure') {
241
- dataURL += serializeAMimeType(parsed)
242
- }
243
-
244
- dataURL += ';base64,'
245
-
246
- const decoder = new StringDecoder('latin1')
247
-
248
- for (const chunk of bytes) {
249
- dataURL += btoa(decoder.write(chunk))
250
- }
251
-
252
- dataURL += btoa(decoder.end())
253
-
254
- return dataURL
255
- }
256
- case 'Text': {
257
- // 1. Let encoding be failure
258
- let encoding = 'failure'
259
-
260
- // 2. If the encodingName is present, set encoding to the
261
- // result of getting an encoding from encodingName.
262
- if (encodingName) {
263
- encoding = getEncoding(encodingName)
264
- }
265
-
266
- // 3. If encoding is failure, and mimeType is present:
267
- if (encoding === 'failure' && mimeType) {
268
- // 1. Let type be the result of parse a MIME type
269
- // given mimeType.
270
- const type = parseMIMEType(mimeType)
271
-
272
- // 2. If type is not failure, set encoding to the result
273
- // of getting an encoding from type’s parameters["charset"].
274
- if (type !== 'failure') {
275
- encoding = getEncoding(type.parameters.get('charset'))
276
- }
277
- }
278
-
279
- // 4. If encoding is failure, then set encoding to UTF-8.
280
- if (encoding === 'failure') {
281
- encoding = 'UTF-8'
282
- }
283
-
284
- // 5. Decode bytes using fallback encoding encoding, and
285
- // return the result.
286
- return decode(bytes, encoding)
287
- }
288
- case 'ArrayBuffer': {
289
- // Return a new ArrayBuffer whose contents are bytes.
290
- const sequence = combineByteSequences(bytes)
291
-
292
- return sequence.buffer
293
- }
294
- case 'BinaryString': {
295
- // Return bytes as a binary string, in which every byte
296
- // is represented by a code unit of equal value [0..255].
297
- let binaryString = ''
298
-
299
- const decoder = new StringDecoder('latin1')
300
-
301
- for (const chunk of bytes) {
302
- binaryString += decoder.write(chunk)
303
- }
304
-
305
- binaryString += decoder.end()
306
-
307
- return binaryString
308
- }
309
- }
310
- }
311
-
312
- /**
313
- * @see https://encoding.spec.whatwg.org/#decode
314
- * @param {Uint8Array[]} ioQueue
315
- * @param {string} encoding
316
- */
317
- function decode (ioQueue, encoding) {
318
- const bytes = combineByteSequences(ioQueue)
319
-
320
- // 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
321
- const BOMEncoding = BOMSniffing(bytes)
322
-
323
- let slice = 0
324
-
325
- // 2. If BOMEncoding is non-null:
326
- if (BOMEncoding !== null) {
327
- // 1. Set encoding to BOMEncoding.
328
- encoding = BOMEncoding
329
-
330
- // 2. Read three bytes from ioQueue, if BOMEncoding is
331
- // UTF-8; otherwise read two bytes.
332
- // (Do nothing with those bytes.)
333
- slice = BOMEncoding === 'UTF-8' ? 3 : 2
334
- }
335
-
336
- // 3. Process a queue with an instance of encoding’s
337
- // decoder, ioQueue, output, and "replacement".
338
-
339
- // 4. Return output.
340
-
341
- const sliced = bytes.slice(slice)
342
- return new TextDecoder(encoding).decode(sliced)
343
- }
344
-
345
- /**
346
- * @see https://encoding.spec.whatwg.org/#bom-sniff
347
- * @param {Uint8Array} ioQueue
348
- */
349
- function BOMSniffing (ioQueue) {
350
- // 1. Let BOM be the result of peeking 3 bytes from ioQueue,
351
- // converted to a byte sequence.
352
- const [a, b, c] = ioQueue
353
-
354
- // 2. For each of the rows in the table below, starting with
355
- // the first one and going down, if BOM starts with the
356
- // bytes given in the first column, then return the
357
- // encoding given in the cell in the second column of that
358
- // row. Otherwise, return null.
359
- if (a === 0xEF && b === 0xBB && c === 0xBF) {
360
- return 'UTF-8'
361
- } else if (a === 0xFE && b === 0xFF) {
362
- return 'UTF-16BE'
363
- } else if (a === 0xFF && b === 0xFE) {
364
- return 'UTF-16LE'
365
- }
366
-
367
- return null
368
- }
369
-
370
- /**
371
- * @param {Uint8Array[]} sequences
372
- */
373
- function combineByteSequences (sequences) {
374
- const size = sequences.reduce((a, b) => {
375
- return a + b.byteLength
376
- }, 0)
377
-
378
- let offset = 0
379
-
380
- return sequences.reduce((a, b) => {
381
- a.set(b, offset)
382
- offset += b.byteLength
383
- return a
384
- }, new Uint8Array(size))
385
- }
386
-
387
- module.exports = {
388
- staticPropertyDescriptors,
389
- readOperation,
390
- fireAProgressEvent
391
- }
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- module.exports = {
4
- kWebSocketURL: Symbol('url'),
5
- kReadyState: Symbol('ready state'),
6
- kController: Symbol('controller'),
7
- kResponse: Symbol('response'),
8
- kBinaryType: Symbol('binary type'),
9
- kSentClose: Symbol('sent close'),
10
- kReceivedClose: Symbol('received close'),
11
- kByteParser: Symbol('byte parser')
12
- }
package/types/file.d.ts DELETED
@@ -1,39 +0,0 @@
1
- // Based on https://github.com/octet-stream/form-data/blob/2d0f0dc371517444ce1f22cdde13f51995d0953a/lib/File.ts (MIT)
2
- /// <reference types="node" />
3
-
4
- import { Blob } from 'buffer'
5
-
6
- export interface BlobPropertyBag {
7
- type?: string
8
- endings?: 'native' | 'transparent'
9
- }
10
-
11
- export interface FilePropertyBag extends BlobPropertyBag {
12
- /**
13
- * The last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). Files without a known last modified date return the current date.
14
- */
15
- lastModified?: number
16
- }
17
-
18
- export declare class File extends Blob {
19
- /**
20
- * Creates a new File instance.
21
- *
22
- * @param fileBits An `Array` strings, or [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), [`ArrayBufferView`](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView), [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects, or a mix of any of such objects, that will be put inside the [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File).
23
- * @param fileName The name of the file.
24
- * @param options An options object containing optional attributes for the file.
25
- */
26
- constructor(fileBits: ReadonlyArray<string | NodeJS.ArrayBufferView | Blob>, fileName: string, options?: FilePropertyBag)
27
-
28
- /**
29
- * Name of the file referenced by the File object.
30
- */
31
- readonly name: string
32
-
33
- /**
34
- * The last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). Files without a known last modified date return the current date.
35
- */
36
- readonly lastModified: number
37
-
38
- readonly [Symbol.toStringTag]: string
39
- }
@@ -1,54 +0,0 @@
1
- /// <reference types="node" />
2
-
3
- import { Blob } from 'buffer'
4
- import { DOMException, Event, EventInit, EventTarget } from './patch'
5
-
6
- export declare class FileReader {
7
- __proto__: EventTarget & FileReader
8
-
9
- constructor ()
10
-
11
- readAsArrayBuffer (blob: Blob): void
12
- readAsBinaryString (blob: Blob): void
13
- readAsText (blob: Blob, encoding?: string): void
14
- readAsDataURL (blob: Blob): void
15
-
16
- abort (): void
17
-
18
- static readonly EMPTY = 0
19
- static readonly LOADING = 1
20
- static readonly DONE = 2
21
-
22
- readonly EMPTY = 0
23
- readonly LOADING = 1
24
- readonly DONE = 2
25
-
26
- readonly readyState: number
27
-
28
- readonly result: string | ArrayBuffer | null
29
-
30
- readonly error: DOMException | null
31
-
32
- onloadstart: null | ((this: FileReader, event: ProgressEvent) => void)
33
- onprogress: null | ((this: FileReader, event: ProgressEvent) => void)
34
- onload: null | ((this: FileReader, event: ProgressEvent) => void)
35
- onabort: null | ((this: FileReader, event: ProgressEvent) => void)
36
- onerror: null | ((this: FileReader, event: ProgressEvent) => void)
37
- onloadend: null | ((this: FileReader, event: ProgressEvent) => void)
38
- }
39
-
40
- export interface ProgressEventInit extends EventInit {
41
- lengthComputable?: boolean
42
- loaded?: number
43
- total?: number
44
- }
45
-
46
- export declare class ProgressEvent {
47
- __proto__: Event & ProgressEvent
48
-
49
- constructor (type: string, eventInitDict?: ProgressEventInit)
50
-
51
- readonly lengthComputable: boolean
52
- readonly loaded: number
53
- readonly total: number
54
- }