From 94b18e24b6c011233dc006cb73ebab243a2890ca Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Fri, 3 Nov 2023 22:33:46 +0000 Subject: [PATCH] flaska: Forcibly close timed out sockets --- flaska.mjs | 23 +++++++++++++++++++---- package.json | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/flaska.mjs b/flaska.mjs index 679df06..1f8d176 100644 --- a/flaska.mjs +++ b/flaska.mjs @@ -1061,6 +1061,22 @@ ctx.state.nonce = nonce; } } + create() { + this.compile() + this.server = this.http.createServer(this.requestStart.bind(this)) + + this.server.on('connection', function (socket) { + // Set socket idle timeout in milliseconds + socket.setTimeout(1000 * 60 * 5) // 5 minutes + + // Wait for timeout event (socket will emit it when idle timeout elapses) + socket.on('timeout', function () { + // Call destroy again + socket.destroy(); + }) + }) + } + listen(port, orgIp, orgcb) { let ip = orgIp let cb = orgcb @@ -1071,8 +1087,8 @@ ctx.state.nonce = nonce; if (typeof(port) !== 'number') { throw new Error('Flaska.listen() called with non-number in port') } - this.compile() - this.server = this.http.createServer(this.requestStart.bind(this)) + + this.create() this.server.listen(port, ip, cb) } @@ -1082,8 +1098,7 @@ ctx.state.nonce = nonce; return Promise.reject(new Error('Flaska.listen() called with non-number in port')) } - this.compile() - this.server = this.http.createServer(this.requestStart.bind(this)) + this.create() if (this.server.listenAsync && typeof(this.server.listenAsync) === 'function') { return this.server.listenAsync(port, ip) diff --git a/package.json b/package.json index 05bd337..ec66df1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flaska", - "version": "1.3.3", + "version": "1.3.4", "description": "Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.", "main": "flaska.mjs", "scripts": {