Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: avoid creation of an intermediary ReadableStream #3095

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,14 @@ function mixinBody (prototype) {
async function consumeBody (object, convertBytesToJSValue, instance) {
webidl.brandCheck(object, instance)

throwIfAborted(object[kState])

// 1. If object is unusable, then return a promise rejected
// with a TypeError.
if (bodyUnusable(object[kState].body)) {
throw new TypeError('Body is unusable')
}

throwIfAborted(object[kState])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm certain this can also be removed. My guess is that when the stream is closed it'll fail in fullyReadBody.


// 2. Let promise be a new promise.
const promise = createDeferredPromise()

Expand Down
32 changes: 5 additions & 27 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const {
subresourceSet
} = require('./constants')
const EE = require('node:events')
const { Readable, pipeline } = require('node:stream')
const { Readable, pipeline, finished } = require('node:stream')
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor, bufferToLowerCasedHeaderName } = require('../../core/util')
const { dataURLProcessor, serializeAMimeType, minimizeSupportedMimeType } = require('./data-url')
const { getGlobalDispatcher } = require('../../global')
Expand Down Expand Up @@ -1082,39 +1082,17 @@ function fetchFinale (fetchParams, response) {
} else {
// mcollina: all the following steps of the specs are skipped.
// The internal transform stream is not needed.
//
// See https://github.com/nodejs/undici/pull/3093#issuecomment-2050198541

// 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.
// 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.

const byteStream = new ReadableStream({
readableStream: internalResponse.body.stream,
async start () {
this._bodyReader = this.readableStream.getReader()
},
async pull (controller) {
while (controller.desiredSize >= 0) {
const { done, value } = await this._bodyReader.read()

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

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

controller.enqueue(value)
}
},
type: 'bytes'
finished(internalResponse.body.stream, () => {
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
processResponseEndOfBody()
})

internalResponse.body.stream = byteStream
}
}

Expand Down
Loading