Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emit more events #1395

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
attach responded to the socket
  • Loading branch information
tinovyatkin committed Oct 18, 2019
commit 4d70048c724bfaf65089547f5fe8b6d579fb96e9
2 changes: 2 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ Note:
})
```

Note: `respond` event may not be emitted in case of premature socket close (due to a middleware timeout, for example).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exceptions like this make it less useful and more difficult to maintain

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an exception - it's a logic of this event because the response in this case never happened.


### Event: 'responded'

Emitted when the response stream is finished. Good place to cleanup any resources attached to `ctx.state` for example:
Expand Down
3 changes: 2 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ module.exports = class Application extends Emitter {
if (null != err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old code will execute onerror no matter the err is null or not. So I think this is a breaking change.

Copy link
Contributor Author

@tinovyatkin tinovyatkin Oct 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengmk2 onerror in current implementation just returns immediately if err is null, so, nothing breaks here actually.

ctx.onerror(err);
this.emit(responding);
} else this.emit('responded', ctx);
}
};
const handleResponse = () => {
this.emit(responding);
respond(ctx);
};
this.emit('request', ctx);
onFinished(res, onerror);
onFinished(ctx.socket, () => { this.emit('responded', ctx); });
return fnMiddleware(ctx).then(handleResponse).catch(onerror);
}

Expand Down
20 changes: 20 additions & 0 deletions test/application/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ describe('app emits events', () => {
assert.deepStrictEqual(emitted, ['request', 'error', 'respond', 'responded']);
assert.strictEqual(onceEvents, 1);
});

it('should emit correct events on middleware timeout', async() => {
const app = new Koa();
const emitted = [];
['request', 'respond', 'responded', 'error', 'timeout'].forEach(event => app.on(event, () => emitted.push(event)));

app.use(async(ctx, next) => {
await new Promise(resolve => setTimeout(resolve, 2000));
ctx.body = 'Timeout';
});

const server = app.listen();
server.setTimeout(1000, socket => { app.emit('timeout'); socket.end('HTTP/1.1 408 Request Timeout\r\n\r\n'); });

await request(server)
.get('/')
.expect(408);

assert.deepStrictEqual(emitted, ['request', 'responded', 'timeout']);
});
});