Skip to content

Commit

Permalink
Do not reuse streaming decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Oct 23, 2023
1 parent 850e104 commit c11c187
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c11c187

Please sign in to comment.