diff --git a/lib/fetch/body.js b/lib/fetch/body.js index b3fe266db14..fd8481b796d 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -27,7 +27,6 @@ let ReadableStream = globalThis.ReadableStream /** @type {globalThis['File']} */ const File = NativeFile ?? UndiciFile const textEncoder = new TextEncoder() -const textDecoderNoBom = new TextDecoder('utf-8', { ignoreBOM: true }) const textDecoder = new TextDecoder() // https://fetch.spec.whatwg.org/#concept-bodyinit-extract @@ -445,13 +444,16 @@ function bodyMixinMethods (instance) { let text = '' // application/x-www-form-urlencoded parser will keep the BOM. // https://url.spec.whatwg.org/#concept-urlencoded-parser + // Note that streaming decoder is stateful and cannot be reused + const streamingDecoder = new TextDecoder('utf-8', { ignoreBOM: true }) + for await (const chunk of consumeBody(this[kState].body)) { if (!isUint8Array(chunk)) { throw new TypeError('Expected Uint8Array chunk') } - text += textDecoderNoBom.decode(chunk, { stream: true }) + text += streamingDecoder.decode(chunk, { stream: true }) } - text += textDecoderNoBom.decode() + text += streamingDecoder.decode() entries = new URLSearchParams(text) } catch (err) { // istanbul ignore next: Unclear when new URLSearchParams can fail on a string.