11'use strict'
22
33const { 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' )
55const util = require ( '../../core/util' )
66const nodeUtil = require ( 'node:util' )
77const { kEnumerableProperty } = util
@@ -26,24 +26,9 @@ const { URLSerializer } = require('./data-url')
2626const { kConstruct } = require ( '../../core/symbols' )
2727const assert = require ( 'node:assert' )
2828const { types } = require ( 'node:util' )
29- const { isDisturbed, isErrored } = require ( 'node:stream' )
3029
3130const textEncoder = new TextEncoder ( 'utf-8' )
3231
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-
4732// https://fetch.spec.whatwg.org/#response-class
4833class Response {
4934 // Creates network error Response.
@@ -327,7 +312,7 @@ function cloneResponse (response) {
327312 // 3. If response’s body is non-null, then set newResponse’s body to the
328313 // result of cloning response’s body.
329314 if ( response . body != null ) {
330- newResponse . body = cloneBody ( response . body )
315+ newResponse . body = cloneBody ( newResponse , response . body )
331316 }
332317
333318 // 4. Return newResponse.
@@ -532,7 +517,7 @@ function fromInnerResponse (innerResponse, guard) {
532517 // a primitive or an object, even undefined. If the held value is an object, the registry keeps
533518 // a strong reference to it (so it can pass it to the cleanup callback later). Reworded from
534519 // 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 ) )
536521 }
537522
538523 return response
0 commit comments