diff --git a/lib/chain.js b/lib/chain.js index c1c08cb6..f1fc5caf 100644 --- a/lib/chain.js +++ b/lib/chain.js @@ -140,7 +140,7 @@ Chain.prototype.run = function run(req, res, done) { var handler = self._stack[index++]; // all done or request closed - if (!handler || req.closed()) { + if (!handler || req.connectionState() === 'close') { process.nextTick(function nextTick() { return done(err, req, res); }); diff --git a/lib/plugins/static.js b/lib/plugins/static.js index 0fcdfd26..0fa6babe 100644 --- a/lib/plugins/static.js +++ b/lib/plugins/static.js @@ -93,7 +93,10 @@ function serveStatic(options) { var re = new RegExp('^' + escapeRE(p) + '/?.*'); function serveFileFromStats(file, err, stats, isGzip, req, res, next) { - if (typeof req.closed === 'function' && req.closed()) { + if ( + typeof req.connectionState === 'function' && + req.connectionState() === 'close' + ) { next(false); return; } diff --git a/lib/request.js b/lib/request.js index eddd0591..a4c54d0e 100644 --- a/lib/request.js +++ b/lib/request.js @@ -2,6 +2,7 @@ 'use strict'; +const { emitWarning } = require('node:process'); var url = require('url'); var sprintf = require('util').format; @@ -846,6 +847,11 @@ function patch(Request) { * @returns {Boolean} is closed */ Request.prototype.closed = function closed() { + emitWarning( + 'restify req.closed is deprecated, will be removed on Restify 10', + 'RestifyDeprecationWarning', + 'RestifyDEPReqClosed' + ); var self = this; return self.connectionState() === 'close'; }; diff --git a/test/chain.test.js b/test/chain.test.js index 403cf0d2..494a2a2f 100644 --- a/test/chain.test.js +++ b/test/chain.test.js @@ -29,8 +29,8 @@ test('calls all the handlers', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -58,8 +58,8 @@ test('abort with Error in next', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -85,7 +85,7 @@ test('abort with false in next', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { + connectionState: function() { return false; } }, @@ -112,8 +112,8 @@ test('abort with closed request', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return closed; + connectionState: function() { + return closed ? 'close' : ''; } }, {}, @@ -143,8 +143,8 @@ test('cals error middleware', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -170,8 +170,8 @@ test('onceNext prevents double next calls', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -208,8 +208,8 @@ test('throws error for double next calls in strictNext mode', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -234,8 +234,8 @@ test('calls req.startHandlerTimer', function(t) { t.done(); }, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -257,8 +257,8 @@ test('calls req.endHandlerTimer', function(t) { t.equal(handleName, 'foo'); t.done(); }, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -299,8 +299,8 @@ test('waits async handlers', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -329,8 +329,8 @@ test('abort with rejected promise', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -359,8 +359,8 @@ test('abort with rejected promise without error', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; }, path: function() { return '/'; @@ -395,8 +395,8 @@ test('abort with throw inside async function', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, diff --git a/test/chainComposer.test.js b/test/chainComposer.test.js index 63fddb4a..6259f2a7 100644 --- a/test/chainComposer.test.js +++ b/test/chainComposer.test.js @@ -29,8 +29,8 @@ test('chainComposer creates a valid chain for a handler array ', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, @@ -53,8 +53,8 @@ test('chainComposer creates a valid chain for a single handler', function(t) { { startHandlerTimer: function() {}, endHandlerTimer: function() {}, - closed: function() { - return false; + connectionState: function() { + return ''; } }, {}, diff --git a/test/request.test.js b/test/request.test.js index 44764505..a2d216fb 100644 --- a/test/request.test.js +++ b/test/request.test.js @@ -275,3 +275,27 @@ test('should emit restifyDone event when request is fully served with error', fu clientDone = true; }); }); + +test('should emit warning if closed is called', function(t) { + let warningCalled = false; + SERVER.get('/ping/:name', function(req, res, next) { + function testWarning(warning) { + t.equal(warning.name, 'RestifyDeprecationWarning'); + t.equal(warning.code, 'RestifyDEPReqClosed'); + t.ok(warning.stack); + warningCalled = true; + + res.send('ok'); + return next(); + } + process.once('warning', testWarning); + t.notOk(req.closed()); + }); + + CLIENT.get('/ping/lagavulin', function(err, _, res) { + t.ifError(err); + t.equal(res.statusCode, 200); + t.ok(warningCalled); + t.end(); + }); +}); diff --git a/test/router.test.js b/test/router.test.js index c1a8132e..fc49979a 100644 --- a/test/router.test.js +++ b/test/router.test.js @@ -16,8 +16,8 @@ var helper = require('./lib/helper.js'); var test = helper.test; var mockReq = { params: {}, - closed: function() { - return false; + connectionState: function() { + return ''; }, startHandlerTimer: function() {}, endHandlerTimer: function() {}