-
Notifications
You must be signed in to change notification settings - Fork 615
fix: Build Uint8Array from ReadableStream without experimental Response constructor #1121
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
import { StreamCollector } from "@aws-sdk/types"; | ||
|
||
export const streamCollector: StreamCollector = ( | ||
stream: ReadableStream | ||
export const streamCollector: StreamCollector = async ( | ||
stream: ReadableStream<Uint8Array> | ||
): Promise<Uint8Array> => { | ||
return new Response(stream) | ||
.arrayBuffer() | ||
.then(arrayBuffer => new Uint8Array(arrayBuffer)); | ||
let res = new Uint8Array(0); | ||
const reader = stream.getReader(); | ||
let isDone = false; | ||
while(!isDone) { | ||
const { done, value } = await reader.read(); | ||
if(value) { | ||
const prior = res; | ||
res = new Uint8Array(prior.length + value.length); | ||
res.set(prior); | ||
res.set(value, prior.length); | ||
} | ||
isDone = done; | ||
} | ||
return res; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of assigning the typed array multiple times, can we push the chunks into an
Array<Uint8Array>
and only keep track of the total length here? When the stream is done, we can create the concatenated buffer one time and assign those chunks value.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we can. I played around with that implementation at first. either is fine, but my (limited) understanding of TypedArray is that they are using the same underlying memory so this line is much more "free" than allocating new arrays and pushing into them.
Essentially we are weighing the computational cost of n
new Uint8Array
vs the storage cost ofUint8Array[n]
. They should both theoretically be cheap.LMK if you have a strong preference for the Uint8Array[], I'll get right on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, it looks good to me! I don't have strong preference either, I think the current implementation is cleaner.
⛴
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, here's the other implementation:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In hindsight, .reduce does exactly what the current implementation does, and has no benefits. I think you were asking for a standard for loop (e.g.
const res = new Uint8Array(combinedLengthOfAllChunks)
)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you go:
I agree that current is cleaner. But LMK what you prefer.