1
1
'use strict'
2
2
3
3
const { Headers, HeadersList, fill, getHeadersGuard, setHeadersGuard, setHeadersList } = require ( './headers' )
4
- const { extractBody, cloneBody, mixinBody, bodyUnusable } = require ( './body' )
4
+ const { extractBody, cloneBody, mixinBody, hasFinalizationRegistry , streamRegistry , bodyUnusable } = require ( './body' )
5
5
const util = require ( '../../core/util' )
6
6
const nodeUtil = require ( 'node:util' )
7
7
const { kEnumerableProperty } = util
@@ -26,24 +26,9 @@ const { URLSerializer } = require('./data-url')
26
26
const { kConstruct } = require ( '../../core/symbols' )
27
27
const assert = require ( 'node:assert' )
28
28
const { types } = require ( 'node:util' )
29
- const { isDisturbed, isErrored } = require ( 'node:stream' )
30
29
31
30
const textEncoder = new TextEncoder ( 'utf-8' )
32
31
33
- const hasFinalizationRegistry = globalThis . FinalizationRegistry && process . version . indexOf ( 'v18' ) !== 0
34
- let registry
35
-
36
- if ( hasFinalizationRegistry ) {
37
- registry = new FinalizationRegistry ( ( weakRef ) => {
38
- const stream = weakRef . deref ( )
39
- if ( stream && ! stream . locked && ! isDisturbed ( stream ) && ! isErrored ( stream ) ) {
40
- stream . cancel ( 'Response object has been garbage collected' ) . catch ( noop )
41
- }
42
- } )
43
- }
44
-
45
- function noop ( ) { }
46
-
47
32
// https://fetch.spec.whatwg.org/#response-class
48
33
class Response {
49
34
// Creates network error Response.
@@ -327,7 +312,7 @@ function cloneResponse (response) {
327
312
// 3. If response’s body is non-null, then set newResponse’s body to the
328
313
// result of cloning response’s body.
329
314
if ( response . body != null ) {
330
- newResponse . body = cloneBody ( response . body )
315
+ newResponse . body = cloneBody ( newResponse , response . body )
331
316
}
332
317
333
318
// 4. Return newResponse.
@@ -532,7 +517,7 @@ function fromInnerResponse (innerResponse, guard) {
532
517
// a primitive or an object, even undefined. If the held value is an object, the registry keeps
533
518
// a strong reference to it (so it can pass it to the cleanup callback later). Reworded from
534
519
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
535
- registry . register ( response , new WeakRef ( innerResponse . body . stream ) )
520
+ streamRegistry . register ( response , new WeakRef ( innerResponse . body . stream ) )
536
521
}
537
522
538
523
return response
0 commit comments