Skip to content

Commit

Permalink
reply.close(). Closes hapijs#1272
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Dec 31, 2013
1 parent 6ae0eb5 commit 879de08
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
7 changes: 4 additions & 3 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [`reply([result])`](#replyresult)
- [`reply.file(path, options)`](#replyfilepath-options)
- [`reply.view(template, [context, [options]])`](#replyviewtemplate-context-options)
- [`reply.close()`](#replyclose)
- [`reply.close([options])`](#replycloseoptions)
- [`reply.proxy(options)`](#replyproxyoptions)
- [Route prerequisites](#route-prerequisites)
- [Route not found](#route-not-found)
Expand Down Expand Up @@ -836,13 +836,14 @@ server.route({ method: 'GET', path: '/', handler: handler });
</html>
```

###### `reply.close()`
###### `reply.close([options])`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, or `reply.proxy()` is called._

Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back
directly via `request.raw.res` and that no further response action is needed (the router will ensure the `request.raw.res` was ended).
directly via `request.raw.res` and that no further response action is needed. Supports the following optional options:
- `end` - if `false`, the router will not call `request.raw.res.end())` to ensure the response was ended. Defaults to `true`.

No return value.

Expand Down
5 changes: 3 additions & 2 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ internals.decorateReply = function (request, finalize) {
return exports.response(result, request, finalize);
};

reply.close = function () {
reply.close = function (options) {

finalize({ closed: true });
options = options || {};
finalize({ closed: true, end: options.end !== false });
};

var viewsManager = request._route.env.views || request.server._views;
Expand Down
4 changes: 3 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ internals.Request.prototype._reply = function (exit) {
if (self._response && // Can be null if response coming from exit
self._response.closed) {

self.raw.res.end(); // End the response in case it wasn't already closed
if (self._response.end) {
self.raw.res.end(); // End the response in case it wasn't already closed
}
return finalize();
}

Expand Down
19 changes: 18 additions & 1 deletion test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2132,11 +2132,28 @@ describe('Response', function () {

describe('Closed', function () {

it('returns a reply', function (done) {
it('returns a reply with manual end', function (done) {

var handler = function (request, reply) {

request.raw.res.end();
reply.close({ end: false });
};

var server = new Hapi.Server();
server.route({ method: 'GET', path: '/', config: { handler: handler } });

server.inject('/', function (res) {

expect(res.result).to.equal('');
done();
});
});

it('returns a reply with auto end', function (done) {

var handler = function (request, reply) {

reply.close();
};

Expand Down

0 comments on commit 879de08

Please sign in to comment.