Skip to content

Commit 49f24c7

Browse files
committed
improve performance of responses
1 parent e0e7057 commit 49f24c7

File tree

9 files changed

+38
-17
lines changed

9 files changed

+38
-17
lines changed

.changeset/twelve-nails-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
Improves the performance of generating responses

.github/actions/pnpm-setup/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ runs:
66
steps:
77
# Install nodejs. https://github.com/actions/setup-node
88
- name: Setup Node.js
9-
uses: actions/setup-node@v4
9+
uses: actions/setup-node@v5
1010
with:
11-
node-version: 18.x
11+
node-version: 20.x
1212

1313
# Install pnpm. https://github.com/pnpm/action-setup
1414
- uses: pnpm/action-setup@v4

.github/workflows/e2e.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ jobs:
9090
with:
9191
version: 9
9292

93-
- name: Set up NodeJS v18
94-
uses: actions/setup-node@v4
93+
- name: Set up NodeJS v20
94+
uses: actions/setup-node@v5
9595
with:
9696
cache: pnpm # cache pnpm store
97-
node-version: 18.18.2
97+
node-version: 20.19.5
9898

9999
- name: Install packages
100100
run: pnpm install

.github/workflows/pre-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: actions/checkout@v4
2121

2222
# Setup .npmrc file to publish to npm
23-
- uses: actions/setup-node@v4
23+
- uses: actions/setup-node@v5
2424
with:
2525
registry-url: "https://registry.npmjs.org"
2626

.github/workflows/v2-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
uses: actions/checkout@v4
1313

1414
# Setup .npmrc file to publish to npm
15-
- uses: actions/setup-node@v4
15+
- uses: actions/setup-node@v5
1616
with:
1717
registry-url: "https://registry.npmjs.org"
1818

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import crypto from "node:crypto";
22
import type { OutgoingHttpHeaders } from "node:http";
33
import { parse as parseQs, stringify as stringifyQs } from "node:querystring";
4-
import { Readable } from "node:stream";
54

65
import { BuildId, HtmlPages, NextConfig } from "config/index.js";
76
import type { IncomingMessage } from "http/index.js";
@@ -18,6 +17,7 @@ import type {
1817
StreamCreator,
1918
} from "types/open-next.js";
2019

20+
import { ReadableStream } from "node:stream/web";
2121
import { debug, error } from "../../adapters/logger.js";
2222
import { isBinaryContentType } from "../../utils/binary.js";
2323
import { localizePath } from "./i18n/index.js";
@@ -105,9 +105,14 @@ export function convertRes(res: OpenNextNodeResponse): InternalResult {
105105
const isBase64Encoded =
106106
isBinaryContentType(headers["content-type"]) ||
107107
!!headers["content-encoding"];
108-
// We cannot convert the OpenNextNodeResponse to a ReadableStream directly
109-
// You can look in the `aws-lambda.ts` file for some context
110-
const body = Readable.toWeb(Readable.from(res.getBody()));
108+
const body =
109+
res._chunks == null
110+
? new ReadableStream({
111+
start(controller) {
112+
controller.close();
113+
},
114+
})
115+
: ReadableStream.from(res._chunks);
111116
return {
112117
type: "core",
113118
statusCode,

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
282282
return Buffer.concat(this._chunks);
283283
}
284284

285+
getBodyLength(): number {
286+
let size = 0;
287+
for (const chunk of this._chunks) {
288+
size += chunk.length;
289+
}
290+
return size;
291+
}
292+
285293
private _internalWrite(chunk: any, encoding: BufferEncoding) {
286294
this._chunks.push(Buffer.from(chunk, encoding));
287295
this.push(chunk, encoding);
@@ -310,7 +318,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
310318
globalThis.__openNextAls
311319
?.getStore()
312320
?.pendingPromiseRunner.add(this.onEnd(this.headers));
313-
const bodyLength = this.getBody().length;
321+
const bodyLength = this.getBodyLength();
314322
this.streamCreator?.onFinish?.(bodyLength);
315323

316324
//This is only here because of aws broken streaming implementation.
@@ -366,8 +374,10 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
366374
}
367375

368376
send() {
369-
const body = this.getBody();
370-
this.end(body);
377+
for (const chunk of this._chunks) {
378+
this.write(chunk);
379+
}
380+
this.end();
371381
}
372382

373383
body(value: string) {

packages/tests-unit/tests/adapters/cache.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,9 @@ describe("CacheHandler", () => {
515515
it("Should not throw when set cache throws", async () => {
516516
incrementalCache.set.mockRejectedValueOnce(new Error("Error"));
517517

518-
expect(
519-
async () => await cache.set("key", { kind: "REDIRECT", props: {} }),
520-
).not.toThrow();
518+
await expect(
519+
cache.set("key", { kind: "REDIRECT", props: {} }),
520+
).resolves.not.toThrow();
521521
});
522522
});
523523

packages/tests-unit/tests/core/routing/util.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function createResponse(res: Partial<Res>) {
5151
getFixedHeaders: () => res.headers ?? {},
5252
body: res.body ?? "",
5353
getBody: () => Buffer.from(res.body ?? ""),
54+
_chunks: res.body ? [Buffer.from(res.body)] : [],
5455
};
5556
}
5657

0 commit comments

Comments
 (0)