@@ -42,13 +42,27 @@ export class StreamingServerResponse extends http.ServerResponse {
42
42
try {
43
43
this . fixHeaders ( this [ HEADERS ] ) ;
44
44
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" ,
51
52
) ;
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
+ // );
52
66
53
67
debug ( "writeHead" , this [ HEADERS ] ) ;
54
68
} catch ( e ) {
@@ -76,22 +90,23 @@ export class StreamingServerResponse extends http.ServerResponse {
76
90
this . internalWrite ( chunk ) ;
77
91
}
78
92
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
81
95
this . internalWrite ( new Uint8Array ( 8 ) ) ;
82
96
}
83
97
84
- setImmediate ( ( ) => {
98
+ process . nextTick ( ( ) => {
85
99
this . responseStream . end ( ( ) => {
100
+ // The callback seems necessary here
86
101
debug ( "stream end" , chunk ) ;
87
102
} ) ;
88
103
} ) ;
89
- debug ( "stream end" , chunk ) ;
104
+ // debug("stream end", chunk);
90
105
return this ;
91
106
}
92
107
93
108
private internalWrite ( chunk : any ) {
94
- setImmediate ( ( ) => {
109
+ process . nextTick ( ( ) => {
95
110
this . responseStream . write ( chunk ) ;
96
111
this . _hasWritten = true ;
97
112
} ) ;
0 commit comments