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

stop handling then when next is called #1942

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ function call(handler, err, req, res, _next) {
} else if (!hasError && arity < 4) {
// request-handling middleware
process.nextTick(function nextTick() {
const result = handler(req, res, next);
if (result && typeof result.then === 'function') {
let nextCalled = false;
const result = handler(req, res, (...params) => {
nextCalled = true;
next(...params);
});
if (!nextCalled && result && typeof result.then === 'function') {
result.then(resolve, reject);
}
});
Expand Down
35 changes: 35 additions & 0 deletions test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2996,6 +2996,41 @@ test('req and res should use own logger by if set during .first', function(t) {
});
});

test('should not call then when next called', function(t) {
let counter = 0;
SERVER.use(function first(req, res, next) {
next();
return Promise.resolve();
});

SERVER.get('/ping', function echoId(req, res, next) {
counter++;
t.equal(counter, 1);
next();
});

CLIENT.get('/ping', function() {
t.end();
});
});

test('should stop after next(false)', function(t) {
let counter = 0;
SERVER.use(function first(req, res, next) {
next(false);
return Promise.resolve();
});

SERVER.get('/ping', function echoId(req, res, next) {
counter++;
next();
});
CLIENT.get('/ping', function() {
t.equal(counter, 0);
t.end();
});
});

test('should throw if handleUncaughtExceptions is invalid', function(t) {
t.throws(() => {
restify.createServer({
Expand Down