Skip to content

Commit 035524e

Browse files
authored
fix #3337 (#3338)
* fix #3337 * fixup
1 parent f6b9b44 commit 035524e

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

lib/web/fetch/body.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ function bodyMixinMethods (instance) {
309309
// Return a Blob whose contents are bytes and type attribute
310310
// is mimeType.
311311
return new Blob([bytes], { type: mimeType })
312-
}, instance, false)
312+
}, instance)
313313
},
314314

315315
arrayBuffer () {
@@ -318,21 +318,20 @@ function bodyMixinMethods (instance) {
318318
// given a byte sequence bytes: return a new ArrayBuffer
319319
// whose contents are bytes.
320320
return consumeBody(this, (bytes) => {
321-
// Note: arrayBuffer already cloned.
322-
return bytes.buffer
323-
}, instance, true)
321+
return new Uint8Array(bytes).buffer
322+
}, instance)
324323
},
325324

326325
text () {
327326
// The text() method steps are to return the result of running
328327
// consume body with this and UTF-8 decode.
329-
return consumeBody(this, utf8DecodeBytes, instance, false)
328+
return consumeBody(this, utf8DecodeBytes, instance)
330329
},
331330

332331
json () {
333332
// The json() method steps are to return the result of running
334333
// consume body with this and parse JSON from bytes.
335-
return consumeBody(this, parseJSONFromBytes, instance, false)
334+
return consumeBody(this, parseJSONFromBytes, instance)
336335
},
337336

338337
formData () {
@@ -384,16 +383,16 @@ function bodyMixinMethods (instance) {
384383
throw new TypeError(
385384
'Content-Type was not one of "multipart/form-data" or "application/x-www-form-urlencoded".'
386385
)
387-
}, instance, false)
386+
}, instance)
388387
},
389388

390389
bytes () {
391390
// The bytes() method steps are to return the result of running consume body
392391
// with this and the following step given a byte sequence bytes: return the
393392
// result of creating a Uint8Array from bytes in this’s relevant realm.
394393
return consumeBody(this, (bytes) => {
395-
return new Uint8Array(bytes.buffer, 0, bytes.byteLength)
396-
}, instance, true)
394+
return new Uint8Array(bytes)
395+
}, instance)
397396
}
398397
}
399398

@@ -409,9 +408,8 @@ function mixinBody (prototype) {
409408
* @param {Response|Request} object
410409
* @param {(value: unknown) => unknown} convertBytesToJSValue
411410
* @param {Response|Request} instance
412-
* @param {boolean} [shouldClone]
413411
*/
414-
async function consumeBody (object, convertBytesToJSValue, instance, shouldClone) {
412+
async function consumeBody (object, convertBytesToJSValue, instance) {
415413
webidl.brandCheck(object, instance)
416414

417415
// 1. If object is unusable, then return a promise rejected
@@ -449,7 +447,7 @@ async function consumeBody (object, convertBytesToJSValue, instance, shouldClone
449447

450448
// 6. Otherwise, fully read object’s body given successSteps,
451449
// errorSteps, and object’s relevant global object.
452-
await fullyReadBody(object[kState].body, successSteps, errorSteps, shouldClone)
450+
await fullyReadBody(object[kState].body, successSteps, errorSteps)
453451

454452
// 7. Return promise.
455453
return promise.promise

lib/web/fetch/util.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ function iteratorMixin (name, object, kInternalIterator, keyIndex = 0, valueInde
10291029
/**
10301030
* @see https://fetch.spec.whatwg.org/#body-fully-read
10311031
*/
1032-
async function fullyReadBody (body, processBody, processBodyError, shouldClone) {
1032+
async function fullyReadBody (body, processBody, processBodyError) {
10331033
// 1. If taskDestination is null, then set taskDestination to
10341034
// the result of starting a new parallel queue.
10351035

@@ -1055,7 +1055,7 @@ async function fullyReadBody (body, processBody, processBodyError, shouldClone)
10551055

10561056
// 5. Read all bytes from reader, given successSteps and errorSteps.
10571057
try {
1058-
successSteps(await readAllBytes(reader, shouldClone))
1058+
successSteps(await readAllBytes(reader))
10591059
} catch (e) {
10601060
errorSteps(e)
10611061
}
@@ -1103,9 +1103,8 @@ function isomorphicEncode (input) {
11031103
* @see https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes
11041104
* @see https://streams.spec.whatwg.org/#read-loop
11051105
* @param {ReadableStreamDefaultReader} reader
1106-
* @param {boolean} [shouldClone]
11071106
*/
1108-
async function readAllBytes (reader, shouldClone) {
1107+
async function readAllBytes (reader) {
11091108
const bytes = []
11101109
let byteLength = 0
11111110

@@ -1114,13 +1113,6 @@ async function readAllBytes (reader, shouldClone) {
11141113

11151114
if (done) {
11161115
// 1. Call successSteps with bytes.
1117-
if (bytes.length === 1) {
1118-
const { buffer, byteOffset, byteLength } = bytes[0]
1119-
if (shouldClone === false) {
1120-
return Buffer.from(buffer, byteOffset, byteLength)
1121-
}
1122-
return Buffer.from(buffer.slice(byteOffset, byteOffset + byteLength), 0, byteLength)
1123-
}
11241116
return Buffer.concat(bytes, byteLength)
11251117
}
11261118

0 commit comments

Comments
 (0)