Skip to content

Commit

Permalink
Add missing method from NextResponse for next 12 (#463)
Browse files Browse the repository at this point in the history
* add missing method from NextResponse for next 12

* Create early-parrots-think.md
  • Loading branch information
conico974 authored Jul 8, 2024
1 parent b8ffa3a commit 1a1441c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-parrots-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Add missing method from NextResponse for next 12
2 changes: 1 addition & 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,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,
Expand Down
56 changes: 44 additions & 12 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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()];
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}
}

0 comments on commit 1a1441c

Please sign in to comment.