Skip to content

Commit 7d20349

Browse files
committed
add missing method from NextResponse for next 12
1 parent 001aaa0 commit 7d20349

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

packages/open-next/src/core/routing/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function convertRes(res: OpenNextNodeResponse) {
8181
: headers["content-type"],
8282
);
8383
const encoding = isBase64Encoded ? "base64" : "utf8";
84-
const body = res.body.toString(encoding);
84+
const body = res.getBody().toString(encoding);
8585
return {
8686
statusCode,
8787
headers,

packages/open-next/src/http/openNextResponse.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
9696
globalThis.__als
9797
?.getStore()
9898
?.pendingPromiseRunner.add(onEnd(this.headers));
99-
const bodyLength = this.body.length;
99+
const bodyLength = this.getBody().length;
100100
this.streamCreator?.onFinish(bodyLength);
101101
});
102102
}
@@ -151,14 +151,6 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
151151
return this.headers;
152152
}
153153

154-
getFixedHeaders(): OutgoingHttpHeaders {
155-
// Do we want to apply this on writeHead?
156-
this.fixHeaders(this.headers);
157-
// This way we ensure that the cookies are correct
158-
this.headers[SET_COOKIE_HEADER] = this._cookies;
159-
return this.headers;
160-
}
161-
162154
getHeader(name: string): OutgoingHttpHeader | undefined {
163155
return this.headers[name.toLowerCase()];
164156
}
@@ -265,7 +257,19 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
265257
return this;
266258
}
267259

268-
get body() {
260+
/**
261+
* OpenNext specific method
262+
*/
263+
264+
getFixedHeaders(): OutgoingHttpHeaders {
265+
// Do we want to apply this on writeHead?
266+
this.fixHeaders(this.headers);
267+
// This way we ensure that the cookies are correct
268+
this.headers[SET_COOKIE_HEADER] = this._cookies;
269+
return this.headers;
270+
}
271+
272+
getBody() {
269273
return Buffer.concat(this._chunks);
270274
}
271275

@@ -297,7 +301,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
297301
//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
298302
_flush(callback: TransformCallback): void {
299303
if (
300-
this.body.length < 1 &&
304+
this.getBody().length < 1 &&
301305
// We use an env variable here because not all aws account have the same behavior
302306
// On some aws accounts the response will hang if the body is empty
303307
// We are modifying the response body here, this is not a good practice
@@ -309,6 +313,11 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
309313
callback();
310314
}
311315

316+
/**
317+
* Next specific methods
318+
* On earlier versions of next.js, those methods are mandatory to make everything work
319+
*/
320+
312321
get sent() {
313322
return this.finished || this.headersSent;
314323
}
@@ -324,7 +333,30 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
324333
}
325334

326335
send() {
327-
const body = this.body;
336+
const body = this.getBody();
328337
this.end(body);
329338
}
339+
340+
body(value: string) {
341+
this.write(value);
342+
return this;
343+
}
344+
345+
onClose(callback: () => void) {
346+
this.on("close", callback);
347+
}
348+
349+
redirect(destination: string, statusCode: number) {
350+
this.setHeader("Location", destination);
351+
this.statusCode = statusCode;
352+
353+
// Since IE11 doesn't support the 308 header add backwards
354+
// compatibility using refresh header
355+
if (statusCode === 308) {
356+
this.setHeader("Refresh", `0;url=${destination}`);
357+
}
358+
359+
//TODO: test to see if we need to call end here
360+
return this;
361+
}
330362
}

0 commit comments

Comments
 (0)