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: destroy stream on error #493

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 22 additions & 16 deletions packages/remix-fastify/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type HttpResponse = RawReplyDefaultExpression<HttpServer>;

export type RequestHandler<Server extends HttpServer> = (
request: FastifyRequest<RouteGenericInterface, Server>,
reply: FastifyReply<RouteGenericInterface, Server>,
reply: FastifyReply<RouteGenericInterface, Server>
) => Promise<void>;

/**
Expand All @@ -36,14 +36,14 @@ export type RequestHandler<Server extends HttpServer> = (
*/
export type GetLoadContextFunction<
Server extends HttpServer,
AppLoadContext,
AppLoadContext
> = (
request: FastifyRequest<RouteGenericInterface, Server>,
reply: FastifyReply<RouteGenericInterface, Server>,
reply: FastifyReply<RouteGenericInterface, Server>
) => Promise<AppLoadContext> | AppLoadContext;

export function createHeaders(
requestHeaders: FastifyRequest["headers"],
requestHeaders: FastifyRequest["headers"]
): Headers {
let headers = new Headers();

Expand All @@ -63,7 +63,7 @@ export function createHeaders(
}

export function getUrl<Server extends HttpServer>(
request: FastifyRequest<RouteGenericInterface, Server>,
request: FastifyRequest<RouteGenericInterface, Server>
): string {
let origin = `${request.protocol}://${request.host}`;
// Use `request.originalUrl` so Remix and React Router are aware of the full path
Expand All @@ -76,7 +76,7 @@ export function createRequest<Server extends HttpServer>(
reply: FastifyReply<RouteGenericInterface, Server>,
createReadableStreamFromReadable:
| typeof RemixCreateReadableStreamFromReadable
| typeof RRCreateReadableStreamFromReadable,
| typeof RRCreateReadableStreamFromReadable
): Request {
let url = getUrl(request);

Expand Down Expand Up @@ -105,7 +105,7 @@ export function createRequest<Server extends HttpServer>(

export async function sendResponse<Server extends HttpServer>(
reply: FastifyReply<RouteGenericInterface, Server>,
nodeResponse: Response,
nodeResponse: Response
): Promise<void> {
reply.status(nodeResponse.status);

Expand All @@ -126,14 +126,20 @@ function responseToReadable(response: Response): Readable | null {

let reader = response.body.getReader();
let readable = new Readable();
readable._read = async () => {
let result = await reader.read();
if (!result.done) {
readable.push(Buffer.from(result.value));
} else {
readable.push(null);
}
};

return readable;
try {
readable._read = async () => {
let result = await reader.read();
if (!result.done) {
readable.push(Buffer.from(result.value));
} else {
readable.push(null);
}
};

return readable;
} catch (error) {
readable.destroy(error as Error);
throw error;
}
Copy link

Choose a reason for hiding this comment

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

This won't catch exception thrown from reader.read()

Copy link

Choose a reason for hiding this comment

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

@mcansh Can you fix this?

}
Loading