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

Skip to content

Commit f16b50f

Browse files
conico974khuezy
authored andcommitted
fix page static props and streaming not always starting
1 parent a0b80ba commit f16b50f

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,27 @@ export class StreamingServerResponse extends http.ServerResponse {
4242
try {
4343
this.fixHeaders(this[HEADERS]);
4444
this._wroteHeader = true;
45-
this.responseStream = awslambda.HttpResponseStream.from(
46-
this.responseStream,
47-
{
48-
statusCode: statusCode as number,
49-
headers: this[HEADERS],
50-
},
45+
// FIXME: This is extracted from the docker lambda node 18 runtime
46+
// https://gist.github.com/conico974/13afd708af20711b97df439b910ceb53#file-index-mjs-L921-L932
47+
// We replace their write with ours which are inside a process.nextTick
48+
// This way it seems to work all the time
49+
// I think we can't ship this code as it is, it could break at anytime if they decide to change the runtime and they already did it in the past
50+
this.responseStream.setContentType(
51+
"application/vnd.awslambda.http-integration-response",
5152
);
53+
const prelude = JSON.stringify({
54+
statusCode: statusCode as number,
55+
headers: this[HEADERS],
56+
});
57+
this.internalWrite(prelude);
58+
this.internalWrite(new Uint8Array(8));
59+
// this.responseStream = awslambda.HttpResponseStream.from(
60+
// this.responseStream,
61+
// {
62+
// statusCode: statusCode as number,
63+
// headers: this[HEADERS],
64+
// },
65+
// );
5266

5367
debug("writeHead", this[HEADERS]);
5468
} catch (e) {
@@ -76,22 +90,23 @@ export class StreamingServerResponse extends http.ServerResponse {
7690
this.internalWrite(chunk);
7791
}
7892

79-
if (!this._hasWritten) {
80-
// We need to send data here, otherwise the stream will not end at all
93+
if (!this._hasWritten && !chunk) {
94+
// We need to send data here if there is none, otherwise the stream will not end at all
8195
this.internalWrite(new Uint8Array(8));
8296
}
8397

84-
setImmediate(() => {
98+
process.nextTick(() => {
8599
this.responseStream.end(() => {
100+
// The callback seems necessary here
86101
debug("stream end", chunk);
87102
});
88103
});
89-
debug("stream end", chunk);
104+
// debug("stream end", chunk);
90105
return this;
91106
}
92107

93108
private internalWrite(chunk: any) {
94-
setImmediate(() => {
109+
process.nextTick(() => {
95110
this.responseStream.write(chunk);
96111
this._hasWritten = true;
97112
});

0 commit comments

Comments
 (0)