Skip to content

Commit e7b4752

Browse files
committed
fix: handle BigInt status codes in res.status() and res.sendStatus()
Coerce BigInt to Number for compatibility with HTTP status codes. Fixes: TypeError when passing BigInt (e.g. 200n) to res.status() or res.sendStatus(), because JSON.stringify() cannot serialize BigInt values. Now BigInt status codes like 200n are automatically converted to 200 for proper HTTP status handling.
1 parent 6c4249f commit e7b4752

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

lib/response.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ module.exports = res
6262
*/
6363

6464
res.status = function status(code) {
65+
// Coerce BigInt to Number for compatibility
66+
if (typeof code === 'bigint') {
67+
code = Number(code);
68+
}
69+
6570
// Check if the status code is not an integer
6671
if (!Number.isInteger(code)) {
6772
throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);

test/res.sendStatus.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,17 @@ describe('res', function () {
4040
.get('/')
4141
.expect(500, /TypeError: Invalid status code/, done)
4242
})
43+
44+
it('should work with BigInt status code', function (done) {
45+
var app = express();
46+
47+
app.use(function(req, res){
48+
res.sendStatus(200n);
49+
});
50+
51+
request(app)
52+
.get('/')
53+
.expect(200, 'OK', done);
54+
})
4355
})
4456
})

test/res.status.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ describe('res', function () {
189189
.expect(500, /Invalid status code/, done);
190190
});
191191

192+
it('should accept BigInt status code', function (done) {
193+
var app = express();
194+
195+
app.use(function (req, res) {
196+
res.status(200n).end();
197+
});
198+
199+
request(app)
200+
.get('/')
201+
.expect(200, done);
202+
});
203+
192204
it('should raise error for NaN status code', function (done) {
193205
var app = express();
194206

0 commit comments

Comments
 (0)