@@ -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 )
312+ } , instance , false )
313313 } ,
314314
315315 arrayBuffer ( ) {
@@ -318,20 +318,21 @@ function bodyMixinMethods (instance) {
318318 // given a byte sequence bytes: return a new ArrayBuffer
319319 // whose contents are bytes.
320320 return consumeBody ( this , ( bytes ) => {
321- return new Uint8Array ( bytes ) . buffer
322- } , instance )
321+ // Note: arrayBuffer already cloned.
322+ return bytes . buffer
323+ } , instance , true )
323324 } ,
324325
325326 text ( ) {
326327 // The text() method steps are to return the result of running
327328 // consume body with this and UTF-8 decode.
328- return consumeBody ( this , utf8DecodeBytes , instance )
329+ return consumeBody ( this , utf8DecodeBytes , instance , false )
329330 } ,
330331
331332 json ( ) {
332333 // The json() method steps are to return the result of running
333334 // consume body with this and parse JSON from bytes.
334- return consumeBody ( this , parseJSONFromBytes , instance )
335+ return consumeBody ( this , parseJSONFromBytes , instance , false )
335336 } ,
336337
337338 formData ( ) {
@@ -383,7 +384,7 @@ function bodyMixinMethods (instance) {
383384 throw new TypeError (
384385 'Content-Type was not one of "multipart/form-data" or "application/x-www-form-urlencoded".'
385386 )
386- } , instance )
387+ } , instance , false )
387388 }
388389 }
389390
@@ -399,8 +400,9 @@ function mixinBody (prototype) {
399400 * @param {Response|Request } object
400401 * @param {(value: unknown) => unknown } convertBytesToJSValue
401402 * @param {Response|Request } instance
403+ * @param {boolean } [shouldClone]
402404 */
403- async function consumeBody ( object , convertBytesToJSValue , instance ) {
405+ async function consumeBody ( object , convertBytesToJSValue , instance , shouldClone ) {
404406 webidl . brandCheck ( object , instance )
405407
406408 // 1. If object is unusable, then return a promise rejected
@@ -432,13 +434,13 @@ async function consumeBody (object, convertBytesToJSValue, instance) {
432434 // 5. If object’s body is null, then run successSteps with an
433435 // empty byte sequence.
434436 if ( object [ kState ] . body == null ) {
435- successSteps ( new Uint8Array ( ) )
437+ successSteps ( Buffer . allocUnsafe ( 0 ) )
436438 return promise . promise
437439 }
438440
439441 // 6. Otherwise, fully read object’s body given successSteps,
440442 // errorSteps, and object’s relevant global object.
441- await fullyReadBody ( object [ kState ] . body , successSteps , errorSteps )
443+ await fullyReadBody ( object [ kState ] . body , successSteps , errorSteps , shouldClone )
442444
443445 // 7. Return promise.
444446 return promise . promise
0 commit comments