Skip to content

Commit

Permalink
use bodyUnusable to check if body is unusable (#3460)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and mcollina committed Aug 17, 2024
1 parent bc46332 commit 405a2ce
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -496,5 +498,6 @@ module.exports = {
extractBody,
safelyExtractBody,
cloneBody,
mixinBody
mixinBody,
bodyUnusable
}
6 changes: 3 additions & 3 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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.'
)
Expand Down Expand Up @@ -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')
}

Expand Down
4 changes: 2 additions & 2 deletions lib/web/fetch/response.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.'
Expand Down

0 comments on commit 405a2ce

Please sign in to comment.