hopefully fix so after gets called after every request

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

View file

@ -7,11 +7,17 @@ flaska.get('/', function(ctx) {
return new Promise(function(res, rej) { return new Promise(function(res, rej) {
ctx.body = { status: true } ctx.body = { status: true }
setTimeout(res, 1000) 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() { flaska.listen(port, function() {
console.log('listening on port', port) console.log('listening on port', port)
}) })

View file

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

View file

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