setHeaders is not being passed a correct response and crashing
See original GitHub issueVersion:
7.3.3
Environment:
- Operating system: MacOS
- Browser: React Native
- Node.js:
v12.9.0
Expected result: Server works as expected, not crashing. Actual result: Server crashes with error:
/server/node_modules/setheader/index.js:50
var described = Object.getOwnPropertyDescriptor(res[symbol], key);
^
TypeError: Cannot convert undefined or null to object
at Function.getOwnPropertyDescriptor (<anonymous>)
at setHeader (/server/node_modules/setheader/index.js:50:26)
at Primus.nocache (/server/node_modules/primus/middleware/no-cache.js:13:3)
at iterate (/server/node_modules/primus/transformer.js:133:20)
at iterate (/server/node_modules/primus/transformer.js:134:14)
at iterate (/server/node_modules/primus/transformer.js:134:14)
at iterate (/server/node_modules/primus/transformer.js:134:14)
at done (/server/node_modules/primus/transformer.js:141:7)
at Primus.control (/server/node_modules/access-control/index.js:91:39)
at iterate (/server/node_modules/primus/transformer.js:138:14)
at iterate (/server/node_modules/primus/transformer.js:134:14)
at child.forEach (/server/node_modules/primus/transformer.js:143:4)
at child.request (/server/node_modules/primus/transformer.js:180:8)
at Server.emit (events.js:209:13)
at Server.EventEmitter.emit (domain.js:476:20)
at parserOnIncoming (_http_server.js:733:12)
Steps to reproduce: Code:
const server = http.createServer(app);
export const primus = new Primus(server,
{
transformer: 'engine.io',
pathname: 'ws'
}
);
primus.authorize(primusAuth);
primus.on('initialised', function() {
console.log('Primus initialized');
});
primus.on('error', function error(err) {
console.error('Error: ', err.stack);
});
primus.on('connection', socketOnConnection);
primus.on('disconnection', socketOnDisconnection);
The authorization middleware function is just a simple implementation of auth on redis:
export async function primusAuth(req, done) {
console.log(`primus auth: ${req.query.authToken}`);
if (req.query.authToken) {
const authToken = req.query.authToken;
if (!authToken) {
return done({
message: 'Auth token not found'
});
}
const userId = await redis.hget(
fields.AUTH_TOKEN_PREFIX + authToken,
fields.USER_ID,
);
if (!userId) {
return done({
message: 'Invalid auth token'
});
}
const user = await User
.query()
.findById(userId);
if (!user) {
return done({
message: 'Invalid user id'
});
}
// auth passed
return done();
}
done({
message: 'Authorization required'
});
}
This server setup worked perfectly before upgrading npm.
Doing some simple debugging on my own, I found out that the http response that is being passed to the no-cache middleware here: https://github.com/primus/primus/blob/master/middleware/no-cache.js#L13
doesn’t have the right header key of outHeadersKey
.
Any ideas what is wrong?
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Error: Can't set headers after they are sent to the client
Two responses have now been sent and the Can't set headers error message occurs. The fix, in this case, would be to remove...
Read more >Understanding Node Error [ERR_HTTP_HEADERS_SENT]
This post describes why the Node Error [ERR_HTTP_HEADERS_SENT] cannot set headers after they are sent.
Read more >Server Crashing with "Cannot set headers after they ... - GitHub
Include the actual command and output and/or stack trace. Not sure sorry as there is a lot of traffic on the server, just...
Read more >Bonus Lecture - "Error: Can't set headers after they are sent."
Enroll now: https://pirple.thinkific.com/courses/the-nodejs-master-class.
Read more >Outlook crashes or stops responding when used with Office 365
If you go through all of those steps and Outlook is still crashing or not responding, ... If the issue recurs, remove the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Snoozy can we re-open this issue? as v12 is not too new now
@beenotung this appears to be fixed in version 7.3.5, due to the upgrade to 1.0.2 of setHeader.