Skip to content

Commit 405a2ce

Browse files
KhafraDevmcollina
authored andcommitted
use bodyUnusable to check if body is unusable (#3460)
1 parent bc46332 commit 405a2ce

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lib/web/fetch/body.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ async function consumeBody (object, convertBytesToJSValue, instance) {
414414

415415
// 1. If object is unusable, then return a promise rejected
416416
// with a TypeError.
417-
if (bodyUnusable(object[kState].body)) {
417+
if (bodyUnusable(object)) {
418418
throw new TypeError('Body is unusable: Body has already been read')
419419
}
420420

@@ -454,7 +454,9 @@ async function consumeBody (object, convertBytesToJSValue, instance) {
454454
}
455455

456456
// https://fetch.spec.whatwg.org/#body-unusable
457-
function bodyUnusable (body) {
457+
function bodyUnusable (object) {
458+
const body = object[kState].body
459+
458460
// An object including the Body interface mixin is
459461
// said to be unusable if its body is non-null and
460462
// its body’s stream is disturbed or locked.
@@ -496,5 +498,6 @@ module.exports = {
496498
extractBody,
497499
safelyExtractBody,
498500
cloneBody,
499-
mixinBody
501+
mixinBody,
502+
bodyUnusable
500503
}

lib/web/fetch/request.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict'
44

5-
const { extractBody, mixinBody, cloneBody } = require('./body')
5+
const { extractBody, mixinBody, cloneBody, bodyUnusable } = require('./body')
66
const { Headers, fill: fillHeaders, HeadersList, setHeadersGuard, getHeadersGuard, setHeadersList, getHeadersList } = require('./headers')
77
const { FinalizationRegistry } = require('./dispatcher-weakref')()
88
const util = require('../../core/util')
@@ -557,7 +557,7 @@ class Request {
557557
// 40. If initBody is null and inputBody is non-null, then:
558558
if (initBody == null && inputBody != null) {
559559
// 1. If input is unusable, then throw a TypeError.
560-
if (util.isDisturbed(inputBody.stream) || inputBody.stream.locked) {
560+
if (bodyUnusable(input)) {
561561
throw new TypeError(
562562
'Cannot construct a Request with a Request object that has already been used.'
563563
)
@@ -759,7 +759,7 @@ class Request {
759759
webidl.brandCheck(this, Request)
760760

761761
// 1. If this is unusable, then throw a TypeError.
762-
if (this.bodyUsed || this.body?.locked) {
762+
if (bodyUnusable(this)) {
763763
throw new TypeError('unusable')
764764
}
765765

lib/web/fetch/response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { Headers, HeadersList, fill, getHeadersGuard, setHeadersGuard, setHeadersList } = require('./headers')
4-
const { extractBody, cloneBody, mixinBody } = require('./body')
4+
const { extractBody, cloneBody, mixinBody, bodyUnusable } = require('./body')
55
const util = require('../../core/util')
66
const nodeUtil = require('node:util')
77
const { kEnumerableProperty } = util
@@ -244,7 +244,7 @@ class Response {
244244
webidl.brandCheck(this, Response)
245245

246246
// 1. If this is unusable, then throw a TypeError.
247-
if (this.bodyUsed || this.body?.locked) {
247+
if (bodyUnusable(this)) {
248248
throw webidl.errors.exception({
249249
header: 'Response.clone',
250250
message: 'Body has already been consumed.'

0 commit comments

Comments
 (0)