diff --git a/lib/web/fetch/body.js b/lib/web/fetch/body.js index 55718ac7c81..a8c996b50eb 100644 --- a/lib/web/fetch/body.js +++ b/lib/web/fetch/body.js @@ -414,7 +414,7 @@ async function consumeBody (object, convertBytesToJSValue, instance) { // 1. If object is unusable, then return a promise rejected // with a TypeError. - if (bodyUnusable(object[kState].body)) { + if (bodyUnusable(object)) { throw new TypeError('Body is unusable: Body has already been read') } @@ -454,7 +454,9 @@ async function consumeBody (object, convertBytesToJSValue, instance) { } // https://fetch.spec.whatwg.org/#body-unusable -function bodyUnusable (body) { +function bodyUnusable (object) { + const body = object[kState].body + // An object including the Body interface mixin is // said to be unusable if its body is non-null and // its body’s stream is disturbed or locked. @@ -496,5 +498,6 @@ module.exports = { extractBody, safelyExtractBody, cloneBody, - mixinBody + mixinBody, + bodyUnusable } diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index bc436aa9705..b63d12efd38 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -2,7 +2,7 @@ 'use strict' -const { extractBody, mixinBody, cloneBody } = require('./body') +const { extractBody, mixinBody, cloneBody, bodyUnusable } = require('./body') const { Headers, fill: fillHeaders, HeadersList, setHeadersGuard, getHeadersGuard, setHeadersList, getHeadersList } = require('./headers') const { FinalizationRegistry } = require('./dispatcher-weakref')() const util = require('../../core/util') @@ -557,7 +557,7 @@ class Request { // 40. If initBody is null and inputBody is non-null, then: if (initBody == null && inputBody != null) { // 1. If input is unusable, then throw a TypeError. - if (util.isDisturbed(inputBody.stream) || inputBody.stream.locked) { + if (bodyUnusable(input)) { throw new TypeError( 'Cannot construct a Request with a Request object that has already been used.' ) @@ -759,7 +759,7 @@ class Request { webidl.brandCheck(this, Request) // 1. If this is unusable, then throw a TypeError. - if (this.bodyUsed || this.body?.locked) { + if (bodyUnusable(this)) { throw new TypeError('unusable') } diff --git a/lib/web/fetch/response.js b/lib/web/fetch/response.js index 603410a4a63..9909066fd38 100644 --- a/lib/web/fetch/response.js +++ b/lib/web/fetch/response.js @@ -1,7 +1,7 @@ 'use strict' const { Headers, HeadersList, fill, getHeadersGuard, setHeadersGuard, setHeadersList } = require('./headers') -const { extractBody, cloneBody, mixinBody } = require('./body') +const { extractBody, cloneBody, mixinBody, bodyUnusable } = require('./body') const util = require('../../core/util') const nodeUtil = require('node:util') const { kEnumerableProperty } = util @@ -244,7 +244,7 @@ class Response { webidl.brandCheck(this, Response) // 1. If this is unusable, then throw a TypeError. - if (this.bodyUsed || this.body?.locked) { + if (bodyUnusable(this)) { throw webidl.errors.exception({ header: 'Response.clone', message: 'Body has already been consumed.'