Merge branch '5.0' into 5-merge
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ctcpip · committed by ctcpip · parents c96c690, 82fc12a · Browse files · 8 files·+157 added−138 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+157 added−138 removed
- tests:+121 added−115 removed
- other:+36 added−23 removed
Provenance
- Commit
dde1f7d- Tree
0cd4817- Author
ctcpip <ctcpip@users.noreply.github.com>- Committer
ctcpip <ctcpip@users.noreply.github.com>- Parent 1
c96c690- Parent 2
82fc12a
8 files changed+157 added−138 removed
modified:lib/application.js+2 added-2 removed
19 unmodified lines20 var http = require('http');21 var compileETag = require('./utils').compileETag;22 var compileQueryParser = require('./utils').compileQueryParser;23 var compileTrust = require('./utils').compileTrust;24-var flatten = require('array-flatten').flatten24 var merge = require('utils-merge');25 var resolve = require('path').resolve;26 var once = require('once')27 var Router = require('router');4 unmodified lines32 * @private33 */34 35 var slice = Array.prototype.slice;36+var flatten = Array.prototype.flat;37 38 /**39 * Application prototype.40 */167 unmodified lines208 path = fn;209 }210 }211 212- var fns = flatten(slice.call(arguments, offset));212+ var fns = flatten.call(slice.call(arguments, offset), Infinity);213 214 if (fns.length === 0) {215 throw new TypeError('app.use() requires a middleware function')216 }426 unmodified linesmodified:lib/response.js+23 added-18 removed
13 unmodified lines14 15 var Buffer = require('safe-buffer').Buffer16 var contentDisposition = require('content-disposition');17 var createError = require('http-errors')18-var deprecate = require('depd')('express');18 var encodeUrl = require('encodeurl');19 var escapeHtml = require('escape-html');20 var http = require('http');21 var onFinished = require('on-finished');26 unmodified lines48 49 module.exports = res50 51 /**53- * Set status `code`.52+ * Set the HTTP status code for the response.53 *55- * @param {Number} code56- * @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.61 * @public62 */63 64 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 integer66+ if (!Number.isInteger(code)) {67+ throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);68 }69+ // Check if the status code is outside of Node's valid range70+ 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+74 this.statusCode = code;75 return this;76 };77 106 unmodified lines184 }185 }186 187 // freshness178- if (req.fresh) this.statusCode = 304;188+ if (req.fresh) this.status(304);189 190 // strip irrelevant headers191 if (204 === this.statusCode || 304 === this.statusCode) {192 this.removeHeader('Content-Type');123 unmodified lines316 317 res.sendStatus = function sendStatus(statusCode) {318 var body = statuses.message[statusCode] || String(statusCode)319 310- this.statusCode = statusCode;320+ this.status(statusCode);321 this.type('txt');322 323 return this.send(body);324 };374 unmodified lines699 * @public700 */701 702 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 past704+ const opts = { path: '/', ...options, expires: new Date(1)};705+ // ensure maxAge is not passed706+ delete opts.maxAge707 708 return this.cookie(name, '', opts);709 };710 134 unmodified lines845 }846 });847 848 // Respond844- this.statusCode = status;849+ this.status(status);850 this.set('Content-Length', Buffer.byteLength(body));851 852 if (this.req.method === 'HEAD') {853 this.end();187 unmodified linesmodified:test/res.clearCookie.js+6 added-12 removed
31 unmodified lines3233343536373637383941404142434446454647485051525349505152575354555657626364585960616231 unmodified lines .expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT') .expect(200, done) })
it('should set expires when passed', function(done) { var expiresAt = new Date() it('should ignore maxAge', function(done){ var app = express();
app.use(function(req, res){ res.clearCookie('sid', { expires: expiresAt }).end(); res.clearCookie('sid', { path: '/admin', maxAge: 1000 }).end(); });
request(app) .get('/') .expect('Set-Cookie', 'sid=; Path=/; Expires=' + expiresAt.toUTCString() ) .expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT') .expect(200, done) })
it('should set both maxAge and expires when passed', function(done) { var maxAgeInMs = 10000 var expiresAt = new Date() var expectedExpires = new Date(expiresAt.getTime() + maxAgeInMs) it('should ignore user supplied expires param', function(done){ var app = express();
app.use(function(req, res){ res.clearCookie('sid', { expires: expiresAt, maxAge: maxAgeInMs }).end(); res.clearCookie('sid', { path: '/admin', expires: new Date() }).end(); });
request(app) .get('/') // yes, this is the behavior. When we set a max-age, we also set expires to a date 10 sec ahead of expires // even if we set max-age only, we will also set an expires 10 sec in the future .expect('Set-Cookie', 'sid=; Max-Age=10; Path=/; Expires=' + expectedExpires.toUTCString()) .expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT') .expect(200, done) }) })})modified:test/res.sendStatus.js+12 added
26 unmodified lines27282930313233343536373839404142434426 unmodified lines request(app) .get('/') .expect(599, '599', done); })
it('should raise error for invalid status code', function (done) { var app = express()
app.use(function (req, res) { res.sendStatus(undefined).end() })
request(app) .get('/') .expect(500, /TypeError: Invalid status code/, done) }) })})modified:test/res.status.js+103 added-103 removed
1 'use strict'2+const express = require('../.');3+const request = require('supertest');4 3-var express = require('../')4-var request = require('supertest')5-6-var isIoJs = process.release7- ? process.release.name === 'io.js'8- : ['v1.', 'v2.', 'v3.'].indexOf(process.version.slice(0, 3)) !== -19-5 describe('res', function () {6 describe('.status(code)', function () {12- // This test fails in node 4.0.013- // https://github.com/expressjs/express/pull/2237/checks14- // As this will all be removed when https://github.com/expressjs/express/pull/421215- // lands I am skipping for now and we can delete with that PR16- describe.skip('when "code" is undefined', function () {17- it('should raise error for invalid status code', function (done) {18- var app = express()7 20- app.use(function (req, res) {21- res.status(undefined).end()22- })8+ it('should set the status code when valid', function (done) {9+ var app = express();10 24- request(app)25- .get('/')26- .expect(500, /Invalid status code/, function (err) {27- if (isIoJs) {28- done(err ? null : new Error('expected error'))29- } else {30- done(err)31- }32- })33- })34- })11+ app.use(function (req, res) {12+ res.status(200).end();13+ });14 36- describe.skip('when "code" is null', function () {37- it('should raise error for invalid status code', function (done) {15+ request(app)16+ .get('/')17+ .expect(200, done);18+ });19+20+ describe('accept valid ranges', function() {21+ // not testing w/ 100, because that has specific meaning and behavior in Node as Expect: 100-continue22+ it('should set the response status code to 101', function (done) {23 var app = express()24 25 app.use(function (req, res) {41- res.status(null).end()26+ res.status(101).end()27 })28 29 request(app)30 .get('/')46- .expect(500, /Invalid status code/, function (err) {47- if (isIoJs) {48- done(err ? null : new Error('expected error'))49- } else {50- done(err)51- }52- })31+ .expect(101, done)32 })54- })33 56- describe('when "code" is 201', function () {34 it('should set the response status code to 201', function (done) {35 var app = express()36 37 app.use(function (req, res) {3 unmodified lines41 request(app)42 .get('/')43 .expect(201, done)44 })68- })45 70- describe('when "code" is 302', function () {46 it('should set the response status code to 302', function (done) {47 var app = express()48 49 app.use(function (req, res) {3 unmodified lines53 request(app)54 .get('/')55 .expect(302, done)56 })82- })57 84- describe('when "code" is 403', function () {58 it('should set the response status code to 403', function (done) {59 var app = express()60 61 app.use(function (req, res) {3 unmodified lines65 request(app)66 .get('/')67 .expect(403, done)68 })96- })69 98- describe('when "code" is 501', function () {70 it('should set the response status code to 501', function (done) {71 var app = express()72 73 app.use(function (req, res) {3 unmodified lines77 request(app)78 .get('/')79 .expect(501, done)80 })110- })81 112- describe('when "code" is "410"', function () {113- it('should set the response status code to 410', function (done) {82+ it('should set the response status code to 700', function (done) {83 var app = express()84 85 app.use(function (req, res) {117- res.status('410').end()86+ res.status(700).end()87 })88 89 request(app)90 .get('/')122- .expect(410, done)91+ .expect(700, done)92 })124- })93 126- describe.skip('when "code" is 410.1', function () {127- it('should set the response status code to 410', function (done) {94+ it('should set the response status code to 800', function (done) {95 var app = express()96 97 app.use(function (req, res) {131- res.status(410.1).end()98+ res.status(800).end()99 })100 101 request(app)102 .get('/')136- .expect(410, function (err) {137- if (isIoJs) {138- done(err ? null : new Error('expected error'))139- } else {140- done(err)141- }142- })103+ .expect(800, done)104 })144- })105 146- describe.skip('when "code" is 1000', function () {147- it('should raise error for invalid status code', function (done) {106+ it('should set the response status code to 900', function (done) {107 var app = express()108 109 app.use(function (req, res) {151- res.status(1000).end()110+ res.status(900).end()111 })112 113 request(app)114 .get('/')156- .expect(500, /Invalid status code/, function (err) {157- if (isIoJs) {158- done(err ? null : new Error('expected error'))159- } else {160- done(err)161- }162- })115+ .expect(900, done)116 })117 })118 166- describe.skip('when "code" is 99', function () {167- it('should raise error for invalid status code', function (done) {168- var app = express()119+ describe('invalid status codes', function () {120+ it('should raise error for status code below 100', function (done) {121+ var app = express();122 123 app.use(function (req, res) {171- res.status(99).end()172- })124+ res.status(99).end();125+ });126 127 request(app)128 .get('/')176- .expect(500, /Invalid status code/, function (err) {177- if (isIoJs) {178- done(err ? null : new Error('expected error'))179- } else {180- done(err)181- }182- })183- })184- })129+ .expect(500, /Invalid status code/, done);130+ });131 186- describe.skip('when "code" is -401', function () {187- it('should raise error for invalid status code', function (done) {188- var app = express()132+ it('should raise error for status code above 999', function (done) {133+ var app = express();134 135 app.use(function (req, res) {191- res.status(-401).end()192- })136+ res.status(1000).end();137+ });138 139 request(app)140 .get('/')196- .expect(500, /Invalid status code/, function (err) {197- if (isIoJs) {198- done(err ? null : new Error('expected error'))199- } else {200- done(err)201- }202- })203- })204- })205- })206-})141+ .expect(500, /Invalid status code/, done);142+ });143+144+ it('should raise error for non-integer status codes', function (done) {145+ var app = express();146+147+ app.use(function (req, res) {148+ res.status(200.1).end();149+ });150+151+ request(app)152+ .get('/')153+ .expect(500, /Invalid status code/, done);154+ });155+156+ it('should raise error for undefined status code', function (done) {157+ var app = express();158+159+ app.use(function (req, res) {160+ res.status(undefined).end();161+ });162+163+ request(app)164+ .get('/')165+ .expect(500, /Invalid status code/, done);166+ });167+168+ it('should raise error for null status code', function (done) {169+ var app = express();170+171+ app.use(function (req, res) {172+ res.status(null).end();173+ });174+175+ request(app)176+ .get('/')177+ .expect(500, /Invalid status code/, done);178+ });179+180+ it('should raise error for string status code', function (done) {181+ var app = express();182+183+ app.use(function (req, res) {184+ res.status("200").end();185+ });186+187+ request(app)188+ .get('/')189+ .expect(500, /Invalid status code/, done);190+ });191+192+ it('should raise error for NaN status code', function (done) {193+ var app = express();194+195+ app.use(function (req, res) {196+ res.status(NaN).end();197+ });198+199+ request(app)200+ .get('/')201+ .expect(500, /Invalid status code/, done);202+ });203+ });204+ });205+});206+modified:.eslintrc.yml+1 added-1 removed
123345673 unmodified linesroot: trueenv: es6: true es2022: true node: truerules: eol-last: error eqeqeq: [error, allow-null]3 unmodified linesmodified:History.md+9 added
1+unreleased2+=========================3+* breaking:4+ * `res.status()` accepts only integers, and input must be greater than 99 and less than 10005+ * will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range6+ * will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs7+* change:8+ - `res.clearCookie` will ignore user provided `maxAge` and `expires` options9+10 5.0.0-beta.3 / 2024-03-2511 =========================12 13 This incorporates all changes after 4.19.1 up to 4.19.2.3788 unmodified linesmodified:package.json+1 added-2 removed
27 unmodified lines28 "api"29 ],30 "dependencies": {31 "accepts": "~1.3.8",32- "array-flatten": "3.0.0",32 "body-parser": "2.0.0-beta.2",33 "content-disposition": "0.5.4",34 "content-type": "~1.0.4",35 "cookie": "0.6.0",44 unmodified lines80 "supertest": "6.3.0",81 "vhost": "~3.0.2"82 },83 "engines": {85- "node": ">= 4"84+ "node": ">= 18"85 },86 "files": [87 "LICENSE",88 "History.md",12 unmodified lines