From 2b0deacd51054f3069a96b1194dbfc2ba14076ec Mon Sep 17 00:00:00 2001 From: Khafra <42794878+KhafraDev@users.noreply.github.com> Date: Fri, 28 Oct 2022 17:32:17 -0400 Subject: [PATCH] fetch: update extractBody to better match spec --- lib/fetch/body.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/fetch/body.js b/lib/fetch/body.js index 7de30264d68..3989b26f620 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -2,7 +2,7 @@ const Busboy = require('busboy') const util = require('../core/util') -const { ReadableStreamFrom, toUSVString, isBlobLike, isReadableStreamLike, readableStreamClose } = require('./util') +const { ReadableStreamFrom, isBlobLike, isReadableStreamLike, readableStreamClose } = require('./util') const { FormData } = require('./formdata') const { kState } = require('./symbols') const { webidl } = require('./webidl') @@ -66,9 +66,13 @@ function extractBody (object, keepalive = false) { let type = null // 10. Switch on object: - if (object == null) { - // Note: The IDL processor cannot handle this situation. See - // https://crbug.com/335871. + if (typeof object === 'string') { + // Set source to the UTF-8 encoding of object. + // Note: setting source to a Uint8Array here breaks some mocking assumptions. + source = object + + // Set type to `text/plain;charset=UTF-8`. + type = 'text/plain;charset=UTF-8' } else if (object instanceof URLSearchParams) { // URLSearchParams @@ -157,6 +161,11 @@ function extractBody (object, keepalive = false) { if (object.type) { type = object.type } + } else if (object instanceof Uint8Array) { + // byte sequence + + // Set source to object. + source = object } else if (typeof object[Symbol.asyncIterator] === 'function') { // If keepalive is true, then throw a TypeError. if (keepalive) { @@ -172,17 +181,10 @@ function extractBody (object, keepalive = false) { stream = object instanceof ReadableStream ? object : ReadableStreamFrom(object) - } else { - // TODO: byte sequence? - // TODO: scalar value string? - // TODO: else? - source = toUSVString(object) - type = 'text/plain;charset=UTF-8' } // 11. If source is a byte sequence, then set action to a // step that returns source and length to source’s length. - // TODO: What is a "byte sequence?" if (typeof source === 'string' || util.isBuffer(source)) { length = Buffer.byteLength(source) }