Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function createResponseFromOptions(options: void | Options) {
function startReadingFromUniversalStream(
response: FlightResponse,
stream: ReadableStream,
onDone: () => void,
): void {
// This is the same as startReadingFromStream except this allows WebSocketStreams which
// return ArrayBuffer and string chunks instead of Uint8Array chunks. We could potentially
Expand All @@ -116,8 +117,7 @@ function startReadingFromUniversalStream(
...
}): void | Promise<void> {
if (done) {
close(response);
return;
return onDone();
}
if (value instanceof ArrayBuffer) {
// WebSockets can produce ArrayBuffer values in ReadableStreams.
Expand All @@ -139,7 +139,7 @@ function startReadingFromUniversalStream(
function startReadingFromStream(
response: FlightResponse,
stream: ReadableStream,
isSecondaryStream: boolean,
onDone: () => void,
): void {
const streamState = createStreamState();
const reader = stream.getReader();
Expand All @@ -152,11 +152,7 @@ function startReadingFromStream(
...
}): void | Promise<void> {
if (done) {
// If we're the secondary stream, then we don't close the response until the debug channel closes.
if (!isSecondaryStream) {
close(response);
}
return;
return onDone();
}
const buffer: Uint8Array = (value: any);
processBinaryChunk(response, streamState, buffer);
Expand All @@ -178,10 +174,20 @@ function createFromReadableStream<T>(
options.debugChannel &&
options.debugChannel.readable
) {
startReadingFromUniversalStream(response, options.debugChannel.readable);
startReadingFromStream(response, stream, true);
let streamDoneCount = 0;
const handleDone = () => {
if (++streamDoneCount === 2) {
close(response);
}
};
startReadingFromUniversalStream(
response,
options.debugChannel.readable,
handleDone,
);
startReadingFromStream(response, stream, handleDone);
} else {
startReadingFromStream(response, stream, false);
startReadingFromStream(response, stream, () => close(response));
}
return getRoot(response);
}
Expand All @@ -199,13 +205,20 @@ function createFromFetch<T>(
options.debugChannel &&
options.debugChannel.readable
) {
let streamDoneCount = 0;
const handleDone = () => {
if (++streamDoneCount === 2) {
close(response);
}
};
startReadingFromUniversalStream(
response,
options.debugChannel.readable,
handleDone,
);
startReadingFromStream(response, (r.body: any), true);
startReadingFromStream(response, (r.body: any), handleDone);
} else {
startReadingFromStream(response, (r.body: any), false);
startReadingFromStream(response, (r.body: any), () => close(response));
}
},
function (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type Options = {
function startReadingFromStream(
response: Response,
stream: Readable,
isSecondaryStream: boolean,
onEnd: () => void,
): void {
const streamState = createStreamState();

Expand All @@ -79,13 +79,7 @@ function startReadingFromStream(
reportGlobalError(response, error);
});

stream.on('end', () => {
// If we're the secondary stream, then we don't close the response until the
// debug channel closes.
if (!isSecondaryStream) {
close(response);
}
});
stream.on('end', onEnd);
}

function createFromNodeStream<T>(
Expand All @@ -112,10 +106,16 @@ function createFromNodeStream<T>(
);

if (__DEV__ && options && options.debugChannel) {
startReadingFromStream(response, options.debugChannel, false);
startReadingFromStream(response, stream, true);
let streamEndedCount = 0;
const handleEnd = () => {
if (++streamEndedCount === 2) {
close(response);
}
};
startReadingFromStream(response, options.debugChannel, handleEnd);
startReadingFromStream(response, stream, handleEnd);
} else {
startReadingFromStream(response, stream, false);
startReadingFromStream(response, stream, () => close(response));
}

return getRoot(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function createDebugCallbackFromWritableStream(
function startReadingFromUniversalStream(
response: FlightResponse,
stream: ReadableStream,
onDone: () => void,
): void {
// This is the same as startReadingFromStream except this allows WebSocketStreams which
// return ArrayBuffer and string chunks instead of Uint8Array chunks. We could potentially
Expand All @@ -117,8 +118,7 @@ function startReadingFromUniversalStream(
...
}): void | Promise<void> {
if (done) {
close(response);
return;
return onDone();
}
if (value instanceof ArrayBuffer) {
// WebSockets can produce ArrayBuffer values in ReadableStreams.
Expand All @@ -140,7 +140,7 @@ function startReadingFromUniversalStream(
function startReadingFromStream(
response: FlightResponse,
stream: ReadableStream,
isSecondaryStream: boolean,
onDone: () => void,
): void {
const streamState = createStreamState();
const reader = stream.getReader();
Expand All @@ -153,11 +153,7 @@ function startReadingFromStream(
...
}): void | Promise<void> {
if (done) {
// If we're the secondary stream, then we don't close the response until the debug channel closes.
if (!isSecondaryStream) {
close(response);
}
return;
return onDone();
}
const buffer: Uint8Array = (value: any);
processBinaryChunk(response, streamState, buffer);
Expand Down Expand Up @@ -208,10 +204,20 @@ export function createFromReadableStream<T>(
options.debugChannel &&
options.debugChannel.readable
) {
startReadingFromUniversalStream(response, options.debugChannel.readable);
startReadingFromStream(response, stream, true);
let streamDoneCount = 0;
const handleDone = () => {
if (++streamDoneCount === 2) {
close(response);
}
};
startReadingFromUniversalStream(
response,
options.debugChannel.readable,
handleDone,
);
startReadingFromStream(response, stream, handleDone);
} else {
startReadingFromStream(response, stream, false);
startReadingFromStream(response, stream, () => close(response));
}
return getRoot(response);
}
Expand Down Expand Up @@ -250,13 +256,20 @@ export function createFromFetch<T>(
options.debugChannel &&
options.debugChannel.readable
) {
let streamDoneCount = 0;
const handleDone = () => {
if (++streamDoneCount === 2) {
close(response);
}
};
startReadingFromUniversalStream(
response,
options.debugChannel.readable,
handleDone,
);
startReadingFromStream(response, (r.body: any), true);
startReadingFromStream(response, (r.body: any), handleDone);
} else {
startReadingFromStream(response, (r.body: any), false);
startReadingFromStream(response, (r.body: any), () => close(response));
}
},
function (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function createResponseFromOptions(options?: Options) {
function startReadingFromStream(
response: FlightResponse,
stream: ReadableStream,
isSecondaryStream: boolean,
onDone: () => void,
): void {
const streamState = createStreamState();
const reader = stream.getReader();
Expand All @@ -115,12 +115,7 @@ function startReadingFromStream(
...
}): void | Promise<void> {
if (done) {
// If we're the secondary stream, then we don't close the response until
// the debug channel closes.
if (!isSecondaryStream) {
close(response);
}
return;
return onDone();
}
const buffer: Uint8Array = (value: any);
processBinaryChunk(response, streamState, buffer);
Expand All @@ -144,10 +139,16 @@ export function createFromReadableStream<T>(
options.debugChannel &&
options.debugChannel.readable
) {
startReadingFromStream(response, options.debugChannel.readable, false);
startReadingFromStream(response, stream, true);
let streamDoneCount = 0;
const handleDone = () => {
if (++streamDoneCount === 2) {
close(response);
}
};
startReadingFromStream(response, options.debugChannel.readable, handleDone);
startReadingFromStream(response, stream, handleDone);
} else {
startReadingFromStream(response, stream, false);
startReadingFromStream(response, stream, () => close(response));
}

return getRoot(response);
Expand All @@ -166,10 +167,20 @@ export function createFromFetch<T>(
options.debugChannel &&
options.debugChannel.readable
) {
startReadingFromStream(response, options.debugChannel.readable, false);
startReadingFromStream(response, (r.body: any), true);
let streamDoneCount = 0;
const handleDone = () => {
if (++streamDoneCount === 2) {
close(response);
}
};
startReadingFromStream(
response,
options.debugChannel.readable,
handleDone,
);
startReadingFromStream(response, (r.body: any), handleDone);
} else {
startReadingFromStream(response, (r.body: any), false);
startReadingFromStream(response, (r.body: any), () => close(response));
}
},
function (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type Options = {
function startReadingFromStream(
response: Response,
stream: Readable,
isSecondaryStream: boolean,
onEnd: () => void,
): void {
const streamState = createStreamState();

Expand All @@ -75,13 +75,7 @@ function startReadingFromStream(
reportGlobalError(response, error);
});

stream.on('end', () => {
// If we're the secondary stream, then we don't close the response until the
// debug channel closes.
if (!isSecondaryStream) {
close(response);
}
});
stream.on('end', onEnd);
}

export function createFromNodeStream<T>(
Expand All @@ -104,10 +98,16 @@ export function createFromNodeStream<T>(
);

if (__DEV__ && options && options.debugChannel) {
startReadingFromStream(response, options.debugChannel, false);
startReadingFromStream(response, stream, true);
let streamEndedCount = 0;
const handleEnd = () => {
if (++streamEndedCount === 2) {
close(response);
}
};
startReadingFromStream(response, options.debugChannel, handleEnd);
startReadingFromStream(response, stream, handleEnd);
} else {
startReadingFromStream(response, stream, false);
startReadingFromStream(response, stream, () => close(response));
}

return getRoot(response);
Expand Down
Loading
Loading