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

Skip to content

Commit a0b80ba

Browse files
conico974khuezy
authored andcommitted
fix streaming not working after a while
1 parent 3411429 commit a0b80ba

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class StreamingServerResponse extends http.ServerResponse {
4141
}
4242
try {
4343
this.fixHeaders(this[HEADERS]);
44+
this._wroteHeader = true;
4445
this.responseStream = awslambda.HttpResponseStream.from(
4546
this.responseStream,
4647
{
@@ -49,7 +50,6 @@ export class StreamingServerResponse extends http.ServerResponse {
4950
},
5051
);
5152

52-
this._wroteHeader = true;
5353
debug("writeHead", this[HEADERS]);
5454
} catch (e) {
5555
this.responseStream.end();
@@ -76,12 +76,15 @@ export class StreamingServerResponse extends http.ServerResponse {
7676
this.internalWrite(chunk);
7777
}
7878

79+
if (!this._hasWritten) {
80+
// We need to send data here, otherwise the stream will not end at all
81+
this.internalWrite(new Uint8Array(8));
82+
}
83+
7984
setImmediate(() => {
80-
if (!this._hasWritten) {
81-
// We need to send data here, otherwise the stream will not end at all
82-
this.internalWrite(new Uint8Array(8));
83-
}
84-
this.responseStream.end();
85+
this.responseStream.end(() => {
86+
debug("stream end", chunk);
87+
});
8588
});
8689
debug("stream end", chunk);
8790
return this;
@@ -114,15 +117,17 @@ export class StreamingServerResponse extends http.ServerResponse {
114117
_writableState: {},
115118
writable: true,
116119
// @ts-ignore
117-
on: Function.prototype,
120+
on: this.responseStream.on.bind(this.responseStream),
118121
// @ts-ignore
119-
removeListener: Function.prototype,
122+
removeListener: this.responseStream.removeListener.bind(
123+
this.responseStream,
124+
),
120125
// @ts-ignore
121-
destroy: Function.prototype,
126+
destroy: this.responseStream.destroy.bind(this.responseStream),
122127
// @ts-ignore
123-
cork: Function.prototype,
128+
cork: this.responseStream.cork.bind(this.responseStream),
124129
// @ts-ignore
125-
uncork: Function.prototype,
130+
uncork: this.responseStream.uncork.bind(this.responseStream),
126131
// @ts-ignore
127132
write: (data, encoding, cb) => {
128133
if (typeof encoding === "function") {
@@ -141,8 +146,8 @@ export class StreamingServerResponse extends http.ServerResponse {
141146

142147
this.responseStream.on("error", (err) => {
143148
this.emit("error", err);
144-
this.responseStream.end();
145149
error("error", err);
150+
this.responseStream.end();
146151
});
147152
}
148153
}

packages/open-next/src/adapters/plugins/streaming.replacement.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export const lambdaHandler = awslambda.streamifyResponse(async function (
6565
if ("type" in preprocessResult) {
6666
//TODO: replace this line
6767
const headers = preprocessResult.headers as Record<string, string>;
68-
console.log("headers", headers);
6968
const res = createServerResponse("GET", headers);
7069
// setImmediate(() => {
7170
// console.log("preprocessResult.headers", headers);

0 commit comments

Comments
 (0)