Commit ba864ad 1 parent b57f967 commit ba864ad Copy full SHA for ba864ad
File tree 1 file changed +9
-3
lines changed
1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -31563,8 +31563,9 @@ const hasFinalizationRegistry = globalThis.FinalizationRegistry && process.versi
31563
31563
let registry
31564
31564
31565
31565
if (hasFinalizationRegistry) {
31566
- registry = new FinalizationRegistry((stream) => {
31567
- if (!stream.locked && !isDisturbed(stream) && !isErrored(stream)) {
31566
+ registry = new FinalizationRegistry((weakRef) => {
31567
+ const stream = weakRef.deref()
31568
+ if (stream && !stream.locked && !isDisturbed(stream) && !isErrored(stream)) {
31568
31569
stream.cancel('Response object has been garbage collected').catch(noop)
31569
31570
}
31570
31571
})
@@ -32055,7 +32056,12 @@ function fromInnerResponse (innerResponse, guard) {
32055
32056
setHeadersGuard(response[kHeaders], guard)
32056
32057
32057
32058
if (hasFinalizationRegistry && innerResponse.body?.stream) {
32058
- registry.register(response, innerResponse.body.stream)
32059
+ // If the target (response) is reclaimed, the cleanup callback may be called at some point with
32060
+ // the held value provided for it (innerResponse.body.stream). The held value can be any value:
32061
+ // a primitive or an object, even undefined. If the held value is an object, the registry keeps
32062
+ // a strong reference to it (so it can pass it to the cleanup callback later). Reworded from
32063
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
32064
+ registry.register(response, new WeakRef(innerResponse.body.stream))
32059
32065
}
32060
32066
32061
32067
return response
You can’t perform that action at this time.
0 commit comments