hopefully fix so after gets called after every request

master
Jonatan Nilsson 2021-10-11 02:06:22 +00:00
parent 6b42415a41
commit ed5ca5e756
4 changed files with 24 additions and 8 deletions

View File

@ -276,7 +276,6 @@ export class Flaska {
ctx.log.error(err)
ctx.res.statusCode = ctx.statusCode = 400
ctx.res.end()
ctx.finished = true
}
this._onreserror = function(err, ctx) {
ctx.log.error(err)
@ -405,6 +404,9 @@ export class Flaska {
req.on('close', () => {
this.requestEnded(ctx)
})
res.on('finish', () => {
this.requestEnded(ctx)
})
try {
this._beforeCompiled(ctx)
@ -496,6 +498,8 @@ export class Flaska {
}
requestEnded(ctx) {
if (ctx.finished) return
ctx.finished = true
this._afterCompiled(ctx)
if (this._afterAsyncCompiled) {
return this._afterAsyncCompiled(ctx).then()

View File

@ -7,11 +7,17 @@ flaska.get('/', function(ctx) {
return new Promise(function(res, rej) {
ctx.body = { status: true }
setTimeout(res, 1000)
}).then(function() {
process.stdout.write(".")
})
})
flaska.after(function(ctx) {
if (ctx.aborted) {
process.stdout.write("A")
} else {
process.stdout.write(".")
}
})
flaska.listen(port, function() {
console.log('listening on port', port)
})

View File

@ -84,7 +84,6 @@ specialHandlers.forEach(function(type) {
let ctx = createCtx()
flaska['_' + type](new Error(), ctx)
assert.strictEqual(ctx.res.statusCode, 400)
assert.strictEqual(ctx.finished, true)
assert.strictEqual(ctx.res.end.callCount, 1)
assert.strictEqual(ctx.res.end.firstCall.length, 0)
})

View File

@ -23,7 +23,7 @@ t.describe('#requestStart()', function() {
try {
assert.ok(err)
assert.strictEqual(assertReq.on.callCount, 3)
assert.strictEqual(assertRes.on.callCount, 1)
assert.strictEqual(assertRes.on.callCount, 2)
assert.strictEqual(assertRes.on.firstCall[0], 'error')
@ -32,6 +32,13 @@ t.describe('#requestStart()', function() {
assert.strictEqual(onResError.callCount, 1)
assert.strictEqual(onResError.firstCall[0], assertErrorTwo)
assert.strictEqual(onResError.firstCall[1], ctx)
assert.strictEqual(assertRes.on.secondCall[0], 'finish')
assert.strictEqual(typeof(assertRes.on.secondCall[1]), 'function')
assert.strictEqual(onEnded.callCount, 0)
assertRes.on.secondCall[1]()
assert.strictEqual(onEnded.callCount, 1)
assert.strictEqual(onEnded.firstCall[0], ctx)
assert.strictEqual(assertReq.on.firstCall[0], 'error')
assert.strictEqual(typeof(assertReq.on.firstCall[1]), 'function')
@ -48,10 +55,10 @@ t.describe('#requestStart()', function() {
assert.strictEqual(assertReq.on.thirdCall[0], 'close')
assert.strictEqual(typeof(assertReq.on.thirdCall[1]), 'function')
assert.strictEqual(onEnded.called, false)
assert.strictEqual(onEnded.callCount, 1)
assertReq.on.thirdCall[1]()
assert.strictEqual(onEnded.called, true)
assert.strictEqual(onEnded.firstCall[0], ctx)
assert.strictEqual(onEnded.callCount, 2)
assert.strictEqual(onEnded.secondCall[0], ctx)
// Test abort and close