socket: Set time out and forcibly close timed out sockets. Fix tests for different node versions
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2023-11-03 22:51:51 +00:00
parent 598548d97b
commit 01a916eb2d
6 changed files with 35 additions and 15 deletions

View file

@ -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) { listen(port, orgIp, orgcb) {
let ip = orgIp let ip = orgIp
let cb = orgcb let cb = orgcb
@ -1071,8 +1087,8 @@ ctx.state.nonce = nonce;
if (typeof(port) !== 'number') { if (typeof(port) !== 'number') {
throw new Error('Flaska.listen() called with non-number in port') 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) 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')) return Promise.reject(new Error('Flaska.listen() called with non-number in port'))
} }
this.compile() this.create()
this.server = this.http.createServer(this.requestStart.bind(this))
if (this.server.listenAsync && typeof(this.server.listenAsync) === 'function') { if (this.server.listenAsync && typeof(this.server.listenAsync) === 'function') {
return this.server.listenAsync(port, ip) return this.server.listenAsync(port, ip)

View file

@ -1,6 +1,6 @@
{ {
"name": "flaska", "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.", "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", "main": "flaska.mjs",
"scripts": { "scripts": {

View file

@ -988,6 +988,7 @@ t.describe('#listenAsync()', function() {
createServer: function() { createServer: function() {
return { return {
listenAsync: stubListenAsync, listenAsync: stubListenAsync,
on: stub(),
} }
} }
}) })
@ -1011,6 +1012,7 @@ t.describe('#listenAsync()', function() {
createServer: function() { createServer: function() {
return { return {
listenAsync: stubListenAsync, listenAsync: stubListenAsync,
on: stub(),
} }
} }
}) })

View file

@ -6,7 +6,7 @@ const indexMap = [
'thirdCall', 'thirdCall',
] ]
export function fakeHttp(inj1, inj2) { export function fakeHttp(inj1, inj2, inj3) {
let intermediate = { let intermediate = {
createServer: function(cb) { createServer: function(cb) {
if (inj1) inj1(cb) if (inj1) inj1(cb)
@ -15,6 +15,9 @@ export function fakeHttp(inj1, inj2) {
listen: function(port, ip, cb) { listen: function(port, ip, cb) {
if (inj2) inj2(port, ip, cb) if (inj2) inj2(port, ip, cb)
else if (cb) cb() else if (cb) cb()
},
on: function(name, handler) {
if (inj3) inj3(name, handler)
} }
} }
} }

View file

@ -145,15 +145,15 @@ t.describe('/json', function() {
t.test('should fail if not a valid json', async function() { t.test('should fail if not a valid json', async function() {
reset() reset()
let err = await assert.isRejected(client.customRequest('POST', '/json', 'aaaa')) let err = await assert.isRejected(client.customRequest('POST', '/json', 'XXXXX'))
assert.strictEqual(err.body.status, 400) assert.strictEqual(err.body.status, 400)
assert.match(err.body.message, /invalid json/i) assert.match(err.body.message, /invalid json/i)
assert.match(err.body.message, /token a/i) assert.match(err.body.message, /token[^X]+X/i)
assert.strictEqual(err.body.request, 'aaaa') assert.strictEqual(err.body.request, 'XXXXX')
assert.strictEqual(log.error.callCount, 1) assert.strictEqual(log.error.callCount, 1)
assert.match(log.error.firstCall[0].message, /invalid json/i) assert.match(log.error.firstCall[0].message, /invalid json/i)
assert.match(log.error.firstCall[0].message, /token a/i) assert.match(log.error.firstCall[0].message, /token[^X]+X/i)
}) })
t.test('should handle incomplete requests correctly', async function() { t.test('should handle incomplete requests correctly', async function() {

View file

@ -498,8 +498,8 @@ t.describe('#JsonHandler()', function() {
finished = true finished = true
}) })
ctx.req.on.firstCall[1](Buffer.alloc(10, 'a')) ctx.req.on.firstCall[1](Buffer.alloc(10, 'X'))
ctx.req.on.firstCall[1](Buffer.alloc(10, 'a')) ctx.req.on.firstCall[1](Buffer.alloc(10, 'X'))
ctx.req.on.secondCall[1]() ctx.req.on.secondCall[1]()
await setTimeout(10) await setTimeout(10)
@ -510,11 +510,11 @@ t.describe('#JsonHandler()', function() {
assert.ok(err instanceof HttpError) assert.ok(err instanceof HttpError)
assert.strictEqual(err.status, 400) assert.strictEqual(err.status, 400)
assert.match(err.message, /JSON/) assert.match(err.message, /JSON/)
assert.match(err.message, /Unexpected token a in/i) assert.match(err.message, /Unexpected token[^X]+X/i)
assert.strictEqual(err.body.status, 400) assert.strictEqual(err.body.status, 400)
assert.match(err.body.message, /Invalid JSON/i) assert.match(err.body.message, /Invalid JSON/i)
assert.match(err.body.message, /Unexpected token a in/i) assert.match(err.body.message, /Unexpected token[^X]+X/i)
assert.strictEqual(err.body.request, 'aaaaaaaaaaaaaaaaaaaa') assert.strictEqual(err.body.request, 'XXXXXXXXXXXXXXXXXXXX')
}) })
t.test('should not throw if body is empty', async function() { t.test('should not throw if body is empty', async function() {