From ce7ded41d055cad66744c3f211d8eb21c9c02d62 Mon Sep 17 00:00:00 2001 From: JeremyTCD Date: Fri, 6 Dec 2019 13:36:34 +0800 Subject: [PATCH] Fix connection drops - Remove default connection timeout - https://github.com/nodejs/node/pull/27558. - Add client error listener. --- .../Servers/OutOfProcess/Http/HttpServer.ts | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/HttpServer.ts b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/HttpServer.ts index 76444cd..22d5ae9 100644 --- a/src/NodeJS/Javascript/Servers/OutOfProcess/Http/HttpServer.ts +++ b/src/NodeJS/Javascript/Servers/OutOfProcess/Http/HttpServer.ts @@ -23,8 +23,19 @@ patchLStat(); // Set by NodeJSProcessFactory let projectDir = process.cwd(); +// Create server +const server = http.createServer(serverOnRequestListener); + +// In Node.js 13+ this is the new default, however for earlier versions it is 120 seconds +server.setTimeout(0); + +// If a connection drops we want as much information as possible +server.on('clientError', serverOnClientError); + // Start server -const server = http.createServer((req, res) => { +server.listen(parseInt(args.port), 'localhost', serverOnListeningListener); + +function serverOnRequestListener(req, res) { let bodyChunks = []; req. on('data', chunk => bodyChunks.push(chunk)). @@ -158,12 +169,24 @@ const server = http.createServer((req, res) => { respondWithError(res, error); } }); -}).listen(parseInt(args.port), 'localhost', function () { +} + +function serverOnClientError(error: Error, socket: stream.Duplex) { + let errorString = error.toString(); + let httpResponseMessage = `HTTP/1.1 400 Bad Request\r +Content-Length: ${Buffer.byteLength(errorString, 'utf8')} +Content-Type: text/html\r +\r +${errorString}`; + socket.end(httpResponseMessage); +} + +function serverOnListeningListener() { // Signal to HttpNodeHost which loopback IP address (IPv4 or IPv6) and port it should make its HTTP connections on // and that we are ready to process invocations. let info = server.address() as AddressInfo; console.log(`[Jering.Javascript.NodeJS: Listening on IP - ${info.address} Port - ${info.port}]`); -}); +} function getTempIdentifier(invocationRequest: InvocationRequest): string { if (invocationRequest.newCacheIdentifier == null) {