From 1a1441cd462bb3b09a5d0fd75bd21a661762f684 Mon Sep 17 00:00:00 2001 From: conico974 Date: Mon, 8 Jul 2024 20:38:44 +0200 Subject: [PATCH] Add missing method from NextResponse for next 12 (#463) * add missing method from NextResponse for next 12 * Create early-parrots-think.md --- .changeset/early-parrots-think.md | 5 ++ packages/open-next/src/core/routing/util.ts | 2 +- .../open-next/src/http/openNextResponse.ts | 56 +++++++++++++++---- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 .changeset/early-parrots-think.md diff --git a/.changeset/early-parrots-think.md b/.changeset/early-parrots-think.md new file mode 100644 index 00000000..a4dac39b --- /dev/null +++ b/.changeset/early-parrots-think.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +Add missing method from NextResponse for next 12 diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index 829265f7..fb9a5e39 100644 --- a/packages/open-next/src/core/routing/util.ts +++ b/packages/open-next/src/core/routing/util.ts @@ -81,7 +81,7 @@ export function convertRes(res: OpenNextNodeResponse) { : headers["content-type"], ); const encoding = isBase64Encoded ? "base64" : "utf8"; - const body = res.body.toString(encoding); + const body = res.getBody().toString(encoding); return { statusCode, headers, diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts index f3abfba0..3ad18a04 100644 --- a/packages/open-next/src/http/openNextResponse.ts +++ b/packages/open-next/src/http/openNextResponse.ts @@ -96,7 +96,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { globalThis.__als ?.getStore() ?.pendingPromiseRunner.add(onEnd(this.headers)); - const bodyLength = this.body.length; + const bodyLength = this.getBody().length; this.streamCreator?.onFinish(bodyLength); }); } @@ -151,14 +151,6 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { return this.headers; } - getFixedHeaders(): OutgoingHttpHeaders { - // Do we want to apply this on writeHead? - this.fixHeaders(this.headers); - // This way we ensure that the cookies are correct - this.headers[SET_COOKIE_HEADER] = this._cookies; - return this.headers; - } - getHeader(name: string): OutgoingHttpHeader | undefined { return this.headers[name.toLowerCase()]; } @@ -265,7 +257,19 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { return this; } - get body() { + /** + * OpenNext specific method + */ + + getFixedHeaders(): OutgoingHttpHeaders { + // Do we want to apply this on writeHead? + this.fixHeaders(this.headers); + // This way we ensure that the cookies are correct + this.headers[SET_COOKIE_HEADER] = this._cookies; + return this.headers; + } + + getBody() { return Buffer.concat(this._chunks); } @@ -297,7 +301,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { //There is another known issue with aws lambda streaming where the request reach the lambda only way after the request has been sent by the client. For this there is absolutely nothing we can do, contact aws support if that's your case _flush(callback: TransformCallback): void { if ( - this.body.length < 1 && + this.getBody().length < 1 && // We use an env variable here because not all aws account have the same behavior // On some aws accounts the response will hang if the body is empty // We are modifying the response body here, this is not a good practice @@ -309,6 +313,11 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { callback(); } + /** + * Next specific methods + * On earlier versions of next.js, those methods are mandatory to make everything work + */ + get sent() { return this.finished || this.headersSent; } @@ -324,7 +333,30 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { } send() { - const body = this.body; + const body = this.getBody(); this.end(body); } + + body(value: string) { + this.write(value); + return this; + } + + onClose(callback: () => void) { + this.on("close", callback); + } + + redirect(destination: string, statusCode: number) { + this.setHeader("Location", destination); + this.statusCode = statusCode; + + // Since IE11 doesn't support the 308 header add backwards + // compatibility using refresh header + if (statusCode === 308) { + this.setHeader("Refresh", `0;url=${destination}`); + } + + //TODO: test to see if we need to call end here + return this; + } }