Skip to content

Commit ebc9148

Browse files
committed
improve performance of responses
1 parent e0e7057 commit ebc9148

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

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

Lines changed: 2 additions & 5 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,14 +105,11 @@ 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()));
111108
return {
112109
type: "core",
113110
statusCode,
114111
headers,
115-
body,
112+
body: ReadableStream.from(res._chunks),
116113
isBase64Encoded,
117114
};
118115
}

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) {

0 commit comments

Comments
 (0)