undici 7.11.0 → 7.13.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 (47) hide show
  1. package/README.md +15 -11
  2. package/docs/docs/api/DiagnosticsChannel.md +7 -4
  3. package/docs/docs/api/Dispatcher.md +2 -2
  4. package/docs/docs/api/ProxyAgent.md +1 -1
  5. package/docs/docs/api/SnapshotAgent.md +616 -0
  6. package/docs/docs/api/WebSocket.md +27 -0
  7. package/index.js +5 -1
  8. package/lib/api/readable.js +49 -29
  9. package/lib/core/request.js +6 -1
  10. package/lib/core/tree.js +1 -1
  11. package/lib/core/util.js +0 -1
  12. package/lib/dispatcher/client-h1.js +8 -17
  13. package/lib/dispatcher/proxy-agent.js +67 -71
  14. package/lib/handler/cache-handler.js +4 -1
  15. package/lib/handler/redirect-handler.js +12 -2
  16. package/lib/interceptor/cache.js +2 -2
  17. package/lib/interceptor/dump.js +2 -1
  18. package/lib/interceptor/redirect.js +1 -1
  19. package/lib/mock/mock-agent.js +10 -4
  20. package/lib/mock/snapshot-agent.js +333 -0
  21. package/lib/mock/snapshot-recorder.js +517 -0
  22. package/lib/util/cache.js +1 -1
  23. package/lib/util/promise.js +28 -0
  24. package/lib/web/cache/cache.js +10 -8
  25. package/lib/web/fetch/body.js +35 -24
  26. package/lib/web/fetch/formdata-parser.js +0 -3
  27. package/lib/web/fetch/formdata.js +0 -4
  28. package/lib/web/fetch/index.js +221 -225
  29. package/lib/web/fetch/request.js +15 -7
  30. package/lib/web/fetch/response.js +5 -3
  31. package/lib/web/fetch/util.js +21 -23
  32. package/lib/web/webidl/index.js +1 -1
  33. package/lib/web/websocket/connection.js +0 -9
  34. package/lib/web/websocket/receiver.js +2 -12
  35. package/lib/web/websocket/stream/websocketstream.js +7 -4
  36. package/lib/web/websocket/websocket.js +57 -1
  37. package/package.json +2 -2
  38. package/types/agent.d.ts +0 -4
  39. package/types/client.d.ts +0 -2
  40. package/types/dispatcher.d.ts +0 -6
  41. package/types/h2c-client.d.ts +0 -2
  42. package/types/index.d.ts +3 -1
  43. package/types/mock-interceptor.d.ts +0 -1
  44. package/types/snapshot-agent.d.ts +107 -0
  45. package/types/webidl.d.ts +10 -0
  46. package/types/websocket.d.ts +2 -0
  47. package/lib/web/fetch/dispatcher-weakref.js +0 -5
@@ -4,19 +4,19 @@ const util = require('../../core/util')
4
4
  const {
5
5
  ReadableStreamFrom,
6
6
  readableStreamClose,
7
- createDeferredPromise,
8
7
  fullyReadBody,
9
8
  extractMimeType,
10
9
  utf8DecodeBytes
11
10
  } = require('./util')
12
11
  const { FormData, setFormDataState } = require('./formdata')
13
12
  const { webidl } = require('../webidl')
14
- const { Blob } = require('node:buffer')
15
13
  const assert = require('node:assert')
16
14
  const { isErrored, isDisturbed } = require('node:stream')
17
15
  const { isArrayBuffer } = require('node:util/types')
18
16
  const { serializeAMimeType } = require('./data-url')
19
17
  const { multipartFormDataParser } = require('./formdata-parser')
18
+ const { createDeferredPromise } = require('../../util/promise')
19
+
20
20
  let random
21
21
 
22
22
  try {
@@ -29,19 +29,22 @@ try {
29
29
  const textEncoder = new TextEncoder()
30
30
  function noop () {}
31
31
 
32
- const hasFinalizationRegistry = globalThis.FinalizationRegistry
33
- let streamRegistry
34
-
35
- if (hasFinalizationRegistry) {
36
- streamRegistry = new FinalizationRegistry((weakRef) => {
37
- const stream = weakRef.deref()
38
- if (stream && !stream.locked && !isDisturbed(stream) && !isErrored(stream)) {
39
- stream.cancel('Response object has been garbage collected').catch(noop)
40
- }
41
- })
42
- }
32
+ const streamRegistry = new FinalizationRegistry((weakRef) => {
33
+ const stream = weakRef.deref()
34
+ if (stream && !stream.locked && !isDisturbed(stream) && !isErrored(stream)) {
35
+ stream.cancel('Response object has been garbage collected').catch(noop)
36
+ }
37
+ })
43
38
 
44
- // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
39
+ /**
40
+ * Extract a body with type from a byte sequence or BodyInit object
41
+ *
42
+ * @param {import('../../../types').BodyInit} object - The BodyInit object to extract from
43
+ * @param {boolean} [keepalive=false] - If true, indicates that the body
44
+ * @returns {[{stream: ReadableStream, source: any, length: number | null}, string | null]} - Returns a tuple containing the body and its type
45
+ *
46
+ * @see https://fetch.spec.whatwg.org/#concept-bodyinit-extract
47
+ */
45
48
  function extractBody (object, keepalive = false) {
46
49
  // 1. Let stream be null.
47
50
  let stream = null
@@ -267,7 +270,22 @@ function extractBody (object, keepalive = false) {
267
270
  return [body, type]
268
271
  }
269
272
 
270
- // https://fetch.spec.whatwg.org/#bodyinit-safely-extract
273
+ /**
274
+ * @typedef {object} ExtractBodyResult
275
+ * @property {ReadableStream<Uint8Array<ArrayBuffer>>} stream - The ReadableStream containing the body data
276
+ * @property {any} source - The original source of the body data
277
+ * @property {number | null} length - The length of the body data, or null
278
+ */
279
+
280
+ /**
281
+ * Safely extract a body with type from a byte sequence or BodyInit object.
282
+ *
283
+ * @param {import('../../../types').BodyInit} object - The BodyInit object to extract from
284
+ * @param {boolean} [keepalive=false] - If true, indicates that the body
285
+ * @returns {[ExtractBodyResult, string | null]} - Returns a tuple containing the body and its type
286
+ *
287
+ * @see https://fetch.spec.whatwg.org/#bodyinit-safely-extract
288
+ */
271
289
  function safelyExtractBody (object, keepalive = false) {
272
290
  // To safely extract a body and a `Content-Type` value from
273
291
  // a byte sequence or BodyInit object object, run these steps:
@@ -275,9 +293,7 @@ function safelyExtractBody (object, keepalive = false) {
275
293
  // 1. If object is a ReadableStream object, then:
276
294
  if (webidl.is.ReadableStream(object)) {
277
295
  // Assert: object is neither disturbed nor locked.
278
- // istanbul ignore next
279
296
  assert(!util.isDisturbed(object), 'The body has already been consumed.')
280
- // istanbul ignore next
281
297
  assert(!object.locked, 'The stream is locked.')
282
298
  }
283
299
 
@@ -285,17 +301,13 @@ function safelyExtractBody (object, keepalive = false) {
285
301
  return extractBody(object, keepalive)
286
302
  }
287
303
 
288
- function cloneBody (instance, body) {
304
+ function cloneBody (body) {
289
305
  // To clone a body body, run these steps:
290
306
 
291
307
  // https://fetch.spec.whatwg.org/#concept-body-clone
292
308
 
293
309
  // 1. Let « out1, out2 » be the result of teeing body’s stream.
294
- const [out1, out2] = body.stream.tee()
295
-
296
- if (hasFinalizationRegistry) {
297
- streamRegistry.register(instance, new WeakRef(out1))
298
- }
310
+ const { 0: out1, 1: out2 } = body.stream.tee()
299
311
 
300
312
  // 2. Set body’s stream to out1.
301
313
  body.stream = out1
@@ -527,6 +539,5 @@ module.exports = {
527
539
  cloneBody,
528
540
  mixinBody,
529
541
  streamRegistry,
530
- hasFinalizationRegistry,
531
542
  bodyUnusable
532
543
  }
@@ -6,9 +6,6 @@ const { HTTP_TOKEN_CODEPOINTS, isomorphicDecode } = require('./data-url')
6
6
  const { makeEntry } = require('./formdata')
7
7
  const { webidl } = require('../webidl')
8
8
  const assert = require('node:assert')
9
- const { File: NodeFile } = require('node:buffer')
10
-
11
- const File = globalThis.File ?? NodeFile
12
9
 
13
10
  const formDataNameBuffer = Buffer.from('form-data; name="')
14
11
  const filenameBuffer = Buffer.from('filename')
@@ -3,12 +3,8 @@
3
3
  const { iteratorMixin } = require('./util')
4
4
  const { kEnumerableProperty } = require('../../core/util')
5
5
  const { webidl } = require('../webidl')
6
- const { File: NativeFile } = require('node:buffer')
7
6
  const nodeUtil = require('node:util')
8
7
 
9
- /** @type {globalThis['File']} */
10
- const File = globalThis.File ?? NativeFile
11
-
12
8
  // https://xhr.spec.whatwg.org/#formdata
13
9
  class FormData {
14
10
  #state = []