Skip to content

Commit

Permalink
Fix node crashing when used without stream (#498)
Browse files Browse the repository at this point in the history
* fix for null return

* update version

* fix body already consumed for redirect

* update version

* Create strange-pugs-sort.md
  • Loading branch information
conico974 committed Aug 26, 2024
1 parent c91c83b commit 85af1ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-pugs-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Fix node crashing when used without stream
11 changes: 5 additions & 6 deletions packages/open-next/src/core/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from "http/index.js";
import { InternalEvent, InternalResult } from "types/open-next";
import { DetachedPromiseRunner } from "utils/promise";
import { fromReadableStream } from "utils/stream";

import { debug, error, warn } from "../adapters/logger";
import { convertRes, createServerResponse, proxyRequest } from "./routing/util";
Expand Down Expand Up @@ -73,12 +72,12 @@ export async function openNextHandler(
);
res.statusCode = preprocessResult.statusCode;
res.flushHeaders();
const body = await fromReadableStream(
preprocessResult.body,
preprocessResult.isBase64Encoded,
);
res.write(body);
const [bodyToConsume, bodyToReturn] = preprocessResult.body.tee();
for await (const chunk of bodyToConsume) {
res.write(chunk);
}
res.end();
preprocessResult.body = bodyToReturn;
}
return preprocessResult;
} else {
Expand Down
4 changes: 3 additions & 1 deletion packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export function convertRes(res: OpenNextNodeResponse): InternalResult {
? headers["content-type"][0]
: headers["content-type"],
);
const body = Readable.toWeb(res);
// We cannot convert the OpenNextNodeResponse to a ReadableStream directly
// You can look in the `aws-lambda.ts` file for some context
const body = Readable.toWeb(Readable.from(res.getBody()));
return {
type: "core",
statusCode,
Expand Down
23 changes: 22 additions & 1 deletion packages/open-next/src/wrappers/aws-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Writable } from "node:stream";

import type {
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
Expand All @@ -6,6 +8,7 @@ import type {
CloudFrontRequestEvent,
CloudFrontRequestResult,
} from "aws-lambda";
import { StreamCreator } from "http/openNextResponse";
import type { WrapperHandler } from "types/open-next";

import { WarmerEvent, WarmerResponse } from "../adapters/warmer-function";
Expand Down Expand Up @@ -40,7 +43,25 @@ const handler: WrapperHandler =

const internalEvent = await converter.convertFrom(event);

const response = await handler(internalEvent);
//TODO: create a simple reproduction and open an issue in the node repo
//This is a workaround, there is an issue in node that causes node to crash silently if the OpenNextNodeResponse stream is not consumed
//This does not happen everytime, it's probably caused by suspended component in ssr (either via <Suspense> or loading.tsx)
//Everyone that wish to create their own wrapper without a StreamCreator should implement this workaround
//This is not necessary if the underlying handler does not use OpenNextNodeResponse (At the moment, OpenNextNodeResponse is used by the node runtime servers and the image server)
const fakeStream: StreamCreator = {
writeHeaders: () => {
return new Writable({
write: (_chunk, _encoding, callback) => {
callback();
},
});
},
onFinish: () => {
// Do nothing
},
};

const response = await handler(internalEvent, fakeStream);

return converter.convertTo(response, event);
};
Expand Down

0 comments on commit 85af1ce

Please sign in to comment.