Skip to content

Commit dde1f7d

Browse files
committed
Merge branch '5.0' into 5-merge
2 parents c96c690 + 82fc12a commit dde1f7d

File tree

8 files changed

+157
-138
lines changed

8 files changed

+157
-138
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
root: true
22
env:
3-
es6: true
3+
es2022: true
44
node: true
55
rules:
66
eol-last: error

History.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
unreleased
2+
=========================
3+
* breaking:
4+
* `res.status()` accepts only integers, and input must be greater than 99 and less than 1000
5+
* will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range
6+
* will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs
7+
* change:
8+
- `res.clearCookie` will ignore user provided `maxAge` and `expires` options
9+
110
5.0.0-beta.3 / 2024-03-25
211
=========================
312

lib/application.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ var http = require('http');
2121
var compileETag = require('./utils').compileETag;
2222
var compileQueryParser = require('./utils').compileQueryParser;
2323
var compileTrust = require('./utils').compileTrust;
24-
var flatten = require('array-flatten').flatten
2524
var merge = require('utils-merge');
2625
var resolve = require('path').resolve;
2726
var once = require('once')
@@ -34,6 +33,7 @@ var setPrototypeOf = require('setprototypeof')
3433
*/
3534

3635
var slice = Array.prototype.slice;
36+
var flatten = Array.prototype.flat;
3737

3838
/**
3939
* Application prototype.
@@ -209,7 +209,7 @@ app.use = function use(fn) {
209209
}
210210
}
211211

212-
var fns = flatten(slice.call(arguments, offset));
212+
var fns = flatten.call(slice.call(arguments, offset), Infinity);
213213

214214
if (fns.length === 0) {
215215
throw new TypeError('app.use() requires a middleware function')

lib/response.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
var Buffer = require('safe-buffer').Buffer
1616
var contentDisposition = require('content-disposition');
1717
var createError = require('http-errors')
18-
var deprecate = require('depd')('express');
1918
var encodeUrl = require('encodeurl');
2019
var escapeHtml = require('escape-html');
2120
var http = require('http');
@@ -50,17 +49,28 @@ var res = Object.create(http.ServerResponse.prototype)
5049
module.exports = res
5150

5251
/**
53-
* Set status `code`.
52+
* Set the HTTP status code for the response.
5453
*
55-
* @param {Number} code
56-
* @return {ServerResponse}
54+
* Expects an integer value between 100 and 999 inclusive.
55+
* Throws an error if the provided status code is not an integer or if it's outside the allowable range.
56+
*
57+
* @param {number} code - The HTTP status code to set.
58+
* @return {ServerResponse} - Returns itself for chaining methods.
59+
* @throws {TypeError} If `code` is not an integer.
60+
* @throws {RangeError} If `code` is outside the range 100 to 999.
5761
* @public
5862
*/
5963

6064
res.status = function status(code) {
61-
if ((typeof code === 'string' || Math.floor(code) !== code) && code > 99 && code < 1000) {
62-
deprecate('res.status(' + JSON.stringify(code) + '): use res.status(' + Math.floor(code) + ') instead')
65+
// Check if the status code is not an integer
66+
if (!Number.isInteger(code)) {
67+
throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);
6368
}
69+
// Check if the status code is outside of Node's valid range
70+
if (code < 100 || code > 999) {
71+
throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);
72+
}
73+
6474
this.statusCode = code;
6575
return this;
6676
};
@@ -175,7 +185,7 @@ res.send = function send(body) {
175185
}
176186

177187
// freshness
178-
if (req.fresh) this.statusCode = 304;
188+
if (req.fresh) this.status(304);
179189

180190
// strip irrelevant headers
181191
if (204 === this.statusCode || 304 === this.statusCode) {
@@ -307,7 +317,7 @@ res.jsonp = function jsonp(obj) {
307317
res.sendStatus = function sendStatus(statusCode) {
308318
var body = statuses.message[statusCode] || String(statusCode)
309319

310-
this.statusCode = statusCode;
320+
this.status(statusCode);
311321
this.type('txt');
312322

313323
return this.send(body);
@@ -690,15 +700,10 @@ res.get = function(field){
690700
*/
691701

692702
res.clearCookie = function clearCookie(name, options) {
693-
if (options) {
694-
if (options.maxAge) {
695-
deprecate('res.clearCookie: Passing "options.maxAge" is deprecated. In v5.0.0 of Express, this option will be ignored, as res.clearCookie will automatically set cookies to expire immediately. Please update your code to omit this option.');
696-
}
697-
if (options.expires) {
698-
deprecate('res.clearCookie: Passing "options.expires" is deprecated. In v5.0.0 of Express, this option will be ignored, as res.clearCookie will automatically set cookies to expire immediately. Please update your code to omit this option.');
699-
}
700-
}
701-
var opts = merge({ expires: new Date(1), path: '/' }, options);
703+
// Force cookie expiration by setting expires to the past
704+
const opts = { path: '/', ...options, expires: new Date(1)};
705+
// ensure maxAge is not passed
706+
delete opts.maxAge
702707

703708
return this.cookie(name, '', opts);
704709
};
@@ -841,7 +846,7 @@ res.redirect = function redirect(url) {
841846
});
842847

843848
// Respond
844-
this.statusCode = status;
849+
this.status(status);
845850
this.set('Content-Length', Buffer.byteLength(body));
846851

847852
if (this.req.method === 'HEAD') {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
],
3030
"dependencies": {
3131
"accepts": "~1.3.8",
32-
"array-flatten": "3.0.0",
3332
"body-parser": "2.0.0-beta.2",
3433
"content-disposition": "0.5.4",
3534
"content-type": "~1.0.4",
@@ -82,7 +81,7 @@
8281
"vhost": "~3.0.2"
8382
},
8483
"engines": {
85-
"node": ">= 4"
84+
"node": ">= 18"
8685
},
8786
"files": [
8887
"LICENSE",

test/res.clearCookie.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,29 @@ describe('res', function(){
3333
.expect(200, done)
3434
})
3535

36-
it('should set expires when passed', function(done) {
37-
var expiresAt = new Date()
36+
it('should ignore maxAge', function(done){
3837
var app = express();
3938

4039
app.use(function(req, res){
41-
res.clearCookie('sid', { expires: expiresAt }).end();
40+
res.clearCookie('sid', { path: '/admin', maxAge: 1000 }).end();
4241
});
4342

4443
request(app)
4544
.get('/')
46-
.expect('Set-Cookie', 'sid=; Path=/; Expires=' + expiresAt.toUTCString() )
45+
.expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
4746
.expect(200, done)
4847
})
4948

50-
it('should set both maxAge and expires when passed', function(done) {
51-
var maxAgeInMs = 10000
52-
var expiresAt = new Date()
53-
var expectedExpires = new Date(expiresAt.getTime() + maxAgeInMs)
49+
it('should ignore user supplied expires param', function(done){
5450
var app = express();
5551

5652
app.use(function(req, res){
57-
res.clearCookie('sid', { expires: expiresAt, maxAge: maxAgeInMs }).end();
53+
res.clearCookie('sid', { path: '/admin', expires: new Date() }).end();
5854
});
5955

6056
request(app)
6157
.get('/')
62-
// yes, this is the behavior. When we set a max-age, we also set expires to a date 10 sec ahead of expires
63-
// even if we set max-age only, we will also set an expires 10 sec in the future
64-
.expect('Set-Cookie', 'sid=; Max-Age=10; Path=/; Expires=' + expectedExpires.toUTCString())
58+
.expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
6559
.expect(200, done)
6660
})
6761
})

test/res.sendStatus.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,17 @@ describe('res', function () {
2828
.get('/')
2929
.expect(599, '599', done);
3030
})
31+
32+
it('should raise error for invalid status code', function (done) {
33+
var app = express()
34+
35+
app.use(function (req, res) {
36+
res.sendStatus(undefined).end()
37+
})
38+
39+
request(app)
40+
.get('/')
41+
.expect(500, /TypeError: Invalid status code/, done)
42+
})
3143
})
3244
})

0 commit comments

Comments
 (0)