Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use bodyUnusable to check if body is unusable #3460

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -555,7 +555,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 @@ -757,7 +757,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
Loading