-
Notifications
You must be signed in to change notification settings - Fork 984
Description
Bug Report
Restify Version
restify@5.0.0
Node.js Version
8.1.4
Expected behaviour
Calling res.getHeaders() would return the appropriate header object.
Actual behaviour
./node_modules/restify/lib/response.js:119
Response.prototype.getHeaders = function getHeaders() {
^
RangeError: Maximum call stack size exceeded
at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:119:52)
at ServerResponse.get (_http_outgoing.js:130:17)
at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
at ServerResponse.get (_http_outgoing.js:130:17)
at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
at ServerResponse.get (_http_outgoing.js:130:17)
at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
at ServerResponse.get (_http_outgoing.js:130:17)
at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
at ServerResponse.get (_http_outgoing.js:130:17)
Repro case
const restify = require('restify');
const server = restify.createServer();
server.get('/', function (req, res, next) {
res.send(200, res.getHeaders());
next();
});
server.listen(8080, function () {});
Cause
The use of _headers is deprecated and it appears that it is currently a wrapper for getHeaders() Public API in Node 8.1.4.
https://github.com/nodejs/node/blob/v8.1.4/lib/_http_outgoing.js#L128-L144
The problem is the inheritance that Restify 5.0 displays with http.ServerResponse. At https://github.com/restify/node-restify/blob/5.x/lib/response.js#L22 Response is set as the appropriate object.
https://github.com/restify/node-restify/blob/5.x/lib/response.js#L119-L121 Then goes to override the Public API function and causes an infinite loop.
Are you willing and able to fix this?
Simply commenting out https://github.com/restify/node-restify/blob/5.x/lib/response.js#L119-L121 would fix the issue in Node 8 environments as the method already exists.
Node 4.x and Node 6.x do not have a response.getHeaders function and this function seems legitimate in Restify.
A simple replacement would be to check the Response object prior to addition:
if (typeof Response.prototype.getHeaders !== 'function') {
Response.prototype.getHeaders = function getHeaders() {
return (this._headers || {});
};
}