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

Fix multiple read attempts in fetch client #876

Merged
merged 2 commits into from
Oct 11, 2023
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
22 changes: 13 additions & 9 deletions packages/connect/src/protocol/async-iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,13 @@ export async function* pipe<I, O>(
): AsyncIterable<O> {
const [transforms, opt] = pickTransforms(rest);
let abortable: Abortable | undefined;
let iterable: AsyncIterable<unknown> = source;
const sourceIt = source[Symbol.asyncIterator]();
const cachedSource = {
[Symbol.asyncIterator]() {
return sourceIt;
},
};
let iterable: AsyncIterable<unknown> = cachedSource;
if (opt?.propagateDownStreamError === true) {
iterable = abortable = makeIterableAbortable(iterable);
}
Expand Down Expand Up @@ -594,14 +600,12 @@ export async function* pipe<I, O>(
// Call return on the source iterable to indicate
// that we will no longer consume it and it should
// cleanup any allocated resources.
source[Symbol.asyncIterator]()
.return?.()
.catch(() => {
// return returns a promise, which we don't care about.
//
// Uncaught promises are thrown at sometime/somewhere by the event loop,
// this is to ensure error is caught and ignored.
});
sourceIt.return?.().catch(() => {
// return returns a promise, which we don't care about.
//
// Uncaught promises are thrown at sometime/somewhere by the event loop,
// this is to ensure error is caught and ignored.
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/src/protocol/universal-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ function iterableToReadableStream(
function iterableFromReadableStream(
body: ReadableStream<Uint8Array> | null,
): AsyncIterable<Uint8Array> {
const reader = body?.getReader();
return {
[Symbol.asyncIterator](): AsyncIterator<Uint8Array> {
const reader = body?.getReader();
return {
async next() {
if (reader !== undefined) {
Expand Down