Skip to content

Commit

Permalink
Skip the creation of a transform stream in fetch (#3093)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Apr 11, 2024
1 parent 2e128c1 commit 83a0fb3
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,23 +1080,17 @@ function fetchFinale (fetchParams, response) {
if (internalResponse.body == null) {
processResponseEndOfBody()
} else {
// mcollina: all the following steps of the specs are skipped.
// The internal transform stream is not needed.
//
// 1. Let transformStream be a new TransformStream.
// 2. Let identityTransformAlgorithm be an algorithm which, given chunk, enqueues chunk in transformStream.
// 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm
// set to processResponseEndOfBody.
const transformStream = new TransformStream({
start () { },
transform (chunk, controller) {
controller.enqueue(chunk)
},
flush: processResponseEndOfBody
})

// 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.
internalResponse.body.stream.pipeThrough(transformStream)

const byteStream = new ReadableStream({
readableStream: transformStream.readable,
readableStream: internalResponse.body.stream,
async start () {
this._bodyReader = this.readableStream.getReader()
},
Expand All @@ -1105,7 +1099,12 @@ function fetchFinale (fetchParams, response) {
const { done, value } = await this._bodyReader.read()

if (done) {
queueMicrotask(() => readableStreamClose(controller))
queueMicrotask(() => {
readableStreamClose(controller)

// Added to in lieu of steps 1-4 of the spec
processResponseEndOfBody()
})
break
}

Expand Down

0 comments on commit 83a0fb3

Please sign in to comment.