Skip to content

Commit c6e0005

Browse files
anonrigjasnellvicb
authored
improve performance of responses (#991)
Co-authored-by: James M Snell <[email protected]> Co-authored-by: Victor Berchet <[email protected]>
1 parent 784fb89 commit c6e0005

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
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

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

Lines changed: 11 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,16 @@ 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 = new ReadableStream({
109+
pull(controller) {
110+
if (!res._chunks || res._chunks.length === 0) {
111+
controller.close();
112+
return;
113+
}
114+
115+
controller.enqueue(res._chunks.shift());
116+
},
117+
});
111118
return {
112119
type: "core",
113120
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)