Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 709aeea

Browse files
committed
enable brotli compression
1 parent 8d73166 commit 709aeea

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

packages/open-next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"access": "public"
44
},
55
"name": "open-next",
6-
"version": "0.0.0-streaming",
6+
"version": "0.0.0-streaming.2",
77
"bin": {
88
"open-next": "./dist/index.js"
99
},

packages/open-next/src/adapters/http/responseStreaming.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import http from "node:http";
22
import { Socket } from "node:net";
3+
import zlib from "node:zlib";
34

45
import { debug, error } from "../logger.js";
56
import type { ResponseStream } from "../types/aws-lambda.js";
@@ -68,9 +69,6 @@ export class StreamingServerResponse extends http.ServerResponse {
6869
this.internalWrite(data, isSse);
6970

7071
cb?.();
71-
72-
console.log(`${n++}: ${d}`);
73-
7472
return true;
7573
},
7674
};
@@ -122,6 +120,7 @@ export class StreamingServerResponse extends http.ServerResponse {
122120
this[HEADERS] = {
123121
...this[HEADERS],
124122
...this._initialHeaders,
123+
"content-encoding": "br",
125124
};
126125

127126
debug("writeHead", this[HEADERS]);
@@ -139,20 +138,23 @@ export class StreamingServerResponse extends http.ServerResponse {
139138
statusCode: statusCode as number,
140139
headers: this[HEADERS],
141140
});
142-
141+
// Try to flush the buffer to the client to invoke
142+
// the streaming. This does not work 100% of the time.
143+
setImmediate(() => {
144+
this.responseStream.write("\n\n");
145+
this.responseStream.uncork();
146+
});
143147
setImmediate(() => {
144148
this.responseStream.write(prelude);
145149
});
146150

147151
setImmediate(() => {
148152
this.responseStream.write(new Uint8Array(8));
149-
});
150-
151-
// Try to flush the buffer to the client to invoke
152-
// the streaming. This does not work 100% of the time.
153-
process.nextTick(() => {
154-
this.responseStream.write("\n\n");
155-
this.responseStream.uncork();
153+
const br = zlib.createBrotliCompress({
154+
flush: zlib.constants.BROTLI_OPERATION_FLUSH,
155+
});
156+
br.pipe(this.responseStream);
157+
this.responseStream = br as unknown as ResponseStream;
156158
});
157159

158160
debug("writeHead", this[HEADERS]);

0 commit comments

Comments
 (0)