From 92691259a82d7319ef53ec760eb2aef70e9dc44e Mon Sep 17 00:00:00 2001 From: Joey Guerra Date: Fri, 22 Dec 2023 17:30:03 -0600 Subject: [PATCH] feat: Merge in changes from 5.0 beta fix: optional headers was not getting set on the response when calling res.sendFile which is called from res.download fix: Headers were not being removed on error during res.sendFile/res.download fix: downloads example BREAKING CHANGE: This merges in all the v5 changes made in the original express repo. --- History.md | 263 ++++--- Readme.md | 76 +- Security.md | 3 +- examples/cookies/index.js | 2 +- examples/downloads/index.js | 15 +- lib/application.js | 119 ++-- lib/express.js | 39 +- lib/middleware/init.js | 43 -- lib/middleware/query.js | 47 -- lib/request.js | 84 +-- lib/response.js | 250 ++----- lib/router/index.js | 672 ----------------- lib/router/layer.js | 181 ----- lib/router/route.js | 228 ------ lib/utils.js | 51 +- lib/view.js | 27 +- package-lock.json | 569 +++++++++------ package.json | 14 +- test/App.mjs | 12 - test/AppDel.mjs | 19 - test/AppOptions.mjs | 20 +- test/AppParam.mjs | 41 -- test/AppRouter.mjs | 439 ++++++------ test/AppRoutesError.mjs | 2 +- test/AppUse.mjs | 8 +- test/Config.mjs | 44 -- test/Exports.mjs | 5 - test/ExpressJson.mjs | 323 +++------ test/ExpressRaw.mjs | 184 +---- test/ExpressStatic.mjs | 16 +- test/ExpressText.mjs | 174 +---- test/ExpressUrlencoded.mjs | 221 ++---- test/ReqAcceptsCharset.mjs | 8 +- test/ReqAcceptsEncoding.mjs | 6 +- test/ReqAcceptsLanguage.mjs | 14 +- test/ReqHost.mjs | 4 +- test/ReqIs.mjs | 6 +- test/ReqParam.mjs | 62 -- test/ReqQuery.mjs | 23 +- test/ResCookie.mjs | 102 --- test/ResDownload.mjs | 319 +-------- test/ResFormat.mjs | 46 +- test/ResJson.mjs | 43 -- test/ResJsonp.mjs | 43 -- test/ResRedirect.mjs | 48 +- test/ResSend.mjs | 93 +-- test/ResSendFile.mjs | 1348 +++++------------------------------ test/ResVary.mjs | 5 +- test/Route.mjs | 33 +- test/Router.mjs | 191 ++--- test/Utils.mjs | 31 - test/acceptance/Cookies.mjs | 6 +- 52 files changed, 1500 insertions(+), 5122 deletions(-) delete mode 100644 lib/middleware/init.js delete mode 100644 lib/middleware/query.js delete mode 100644 lib/router/index.js delete mode 100644 lib/router/layer.js delete mode 100644 lib/router/route.js delete mode 100644 test/AppDel.mjs delete mode 100644 test/ReqParam.mjs diff --git a/History.md b/History.md index 932932d761..5380daf09c 100644 --- a/History.md +++ b/History.md @@ -1,91 +1,166 @@ -5.0.0 / 2023-12-11 -=================== - - * deps: removed safe-buffer - - engine: node >= v18.19.0 - - Use Node Buffer instead of `safe-buffer` - * deps: path-to-regexp@6.2.1 - - BREAKING CHANGE: upgraded from 0.1.7. Route mapping changes e.g. /*, /foo/ handling /foo, no longer work - - See https://github.com/pillarjs/path-to-regexp for documentation - * deps: removed array-flatten - - Replaced with Array.prototype.flat - - Removed `utils.flatten` - * deps: connect-redis@7.1.0 - * deps: marked@11.0.0 - * deps: removed mocha - - Replaced mocha with Node Test Runner - - Removed `nyc` - * Use ES Import for tests - - Renamed tests with `.mjs` file extension - - Renamed tests with Pascal Casing - * Fix routing requests without method - * deps: body-parser@1.20.2 - - Fix strict json error message on Node.js 19+ - - deps: content-type@~1.0.5 - - deps: raw-body@2.5.2 - -4.18.2 / 2022-10-08 -=================== - - * Fix regression routing a large stack in a single route - * deps: body-parser@1.20.1 - - deps: qs@6.11.0 - - perf: remove unnecessary object clone - * deps: qs@6.11.0 - -4.18.1 / 2022-04-29 -=================== - - * Fix hanging on large stack of sync routes - -4.18.0 / 2022-04-25 -=================== - - * Add "root" option to `res.download` - * Allow `options` without `filename` in `res.download` - * Deprecate string and non-integer arguments to `res.status` - * Fix behavior of `null`/`undefined` as `maxAge` in `res.cookie` - * Fix handling very large stacks of sync middleware - * Ignore `Object.prototype` values in settings through `app.set`/`app.get` - * Invoke `default` with same arguments as types in `res.format` - * Support proper 205 responses using `res.send` - * Use `http-errors` for `res.format` error - * deps: body-parser@1.20.0 - - Fix error message for json parse whitespace in `strict` - - Fix internal error when inflated body exceeds limit - - Prevent loss of async hooks context - - Prevent hanging when request already read - - deps: depd@2.0.0 - - deps: http-errors@2.0.0 - - deps: on-finished@2.4.1 - - deps: qs@6.10.3 - - deps: raw-body@2.5.1 - * deps: cookie@0.5.0 - - Add `priority` option - - Fix `expires` option to reject invalid dates - * deps: depd@2.0.0 - - Replace internal `eval` usage with `Function` constructor - - Use instance methods on `process` to check for listeners - * deps: finalhandler@1.2.0 - - Remove set content headers that break response - - deps: on-finished@2.4.1 - - deps: statuses@2.0.1 - * deps: on-finished@2.4.1 - - Prevent loss of async hooks context - * deps: qs@6.10.3 - * deps: send@0.18.0 - - Fix emitted 416 error missing headers property - - Limit the headers removed for 304 response - - deps: depd@2.0.0 - - deps: destroy@1.2.0 - - deps: http-errors@2.0.0 - - deps: on-finished@2.4.1 - - deps: statuses@2.0.1 - * deps: serve-static@1.15.0 - - deps: send@0.18.0 - * deps: statuses@2.0.1 - - Remove code 306 - - Rename `425 Unordered Collection` to standard `425 Too Early` +5.x +=== + +This incorporates all changes after 4.17.2 up to 4.17.3. + +5.0.0-beta.1 / 2022-02-14 +========================= + +This is the first Express 5.0 beta release, based off 4.17.2 and includes +changes from 5.0.0-alpha.8. + + * change: + - Default "query parser" setting to `'simple'` + - Requires Node.js 4+ + - Use `mime-types` for file to content type mapping + * deps: array-flatten@3.0.0 + * deps: body-parser@2.0.0-beta.1 + - `req.body` is no longer always initialized to `{}` + - `urlencoded` parser now defaults `extended` to `false` + - Use `on-finished` to determine when body read + * deps: router@2.0.0-beta.1 + - Add new `?`, `*`, and `+` parameter modifiers + - Internalize private `router.process_params` method + - Matching group expressions are only RegExp syntax + - Named matching groups no longer available by position in `req.params` + - Regular expressions can only be used in a matching group + - Remove `debug` dependency + - Special `*` path segment behavior removed + - deps: array-flatten@3.0.0 + - deps: parseurl@~1.3.3 + - deps: path-to-regexp@3.2.0 + - deps: setprototypeof@1.2.0 + * deps: send@1.0.0-beta.1 + - Change `dotfiles` option default to `'ignore'` + - Remove `hidden` option; use `dotfiles` option instead + - Use `mime-types` for file to content type mapping + - deps: debug@3.1.0 + * deps: serve-static@2.0.0-beta.1 + - Change `dotfiles` option default to `'ignore'` + - Remove `hidden` option; use `dotfiles` option instead + - Use `mime-types` for file to content type mapping + - deps: send@1.0.0-beta.1 + +5.0.0-alpha.8 / 2020-03-25 +========================== + +This is the eighth Express 5.0 alpha release, based off 4.17.1 and includes +changes from 5.0.0-alpha.7. + +5.0.0-alpha.7 / 2018-10-26 +========================== + +This is the seventh Express 5.0 alpha release, based off 4.16.4 and includes +changes from 5.0.0-alpha.6. + +The major change with this alpha is the basic support for returned, rejected +Promises in the router. + + * remove: + - `path-to-regexp` dependency + * deps: debug@3.1.0 + - Add `DEBUG_HIDE_DATE` environment variable + - Change timer to per-namespace instead of global + - Change non-TTY date format + - Remove `DEBUG_FD` environment variable support + - Support 256 namespace colors + * deps: router@2.0.0-alpha.1 + - Add basic support for returned, rejected Promises + - Fix JSDoc for `Router` constructor + - deps: debug@3.1.0 + - deps: parseurl@~1.3.2 + - deps: setprototypeof@1.1.0 + - deps: utils-merge@1.0.1 + +5.0.0-alpha.6 / 2017-09-24 +========================== + +This is the sixth Express 5.0 alpha release, based off 4.15.5 and includes +changes from 5.0.0-alpha.5. + + * remove: + - `res.redirect(url, status)` signature - use `res.redirect(status, url)` + - `res.send(status, body)` signature - use `res.status(status).send(body)` + * deps: router@~1.3.1 + - deps: debug@2.6.8 + +5.0.0-alpha.5 / 2017-03-06 +========================== + +This is the fifth Express 5.0 alpha release, based off 4.15.2 and includes +changes from 5.0.0-alpha.4. + +5.0.0-alpha.4 / 2017-03-01 +========================== + +This is the fourth Express 5.0 alpha release, based off 4.15.0 and includes +changes from 5.0.0-alpha.3. + + * remove: + - Remove Express 3.x middleware error stubs + * deps: router@~1.3.0 + - Add `next("router")` to exit from router + - Fix case where `router.use` skipped requests routes did not + - Skip routing when `req.url` is not set + - Use `%o` in path debug to tell types apart + - deps: debug@2.6.1 + - deps: setprototypeof@1.0.3 + - perf: add fast match path for `*` route + +5.0.0-alpha.3 / 2017-01-28 +========================== + +This is the third Express 5.0 alpha release, based off 4.14.1 and includes +changes from 5.0.0-alpha.2. + + * remove: + - `res.json(status, obj)` signature - use `res.status(status).json(obj)` + - `res.jsonp(status, obj)` signature - use `res.status(status).jsonp(obj)` + - `res.vary()` (no arguments) -- provide a field name as an argument + * deps: array-flatten@2.1.1 + * deps: path-is-absolute@1.0.1 + * deps: router@~1.1.5 + - deps: array-flatten@2.0.1 + - deps: methods@~1.1.2 + - deps: parseurl@~1.3.1 + - deps: setprototypeof@1.0.2 + +5.0.0-alpha.2 / 2015-07-06 +========================== + +This is the second Express 5.0 alpha release, based off 4.13.1 and includes +changes from 5.0.0-alpha.1. + + * remove: + - `app.param(fn)` + - `req.param()` -- use `req.params`, `req.body`, or `req.query` instead + * change: + - `res.render` callback is always async, even for sync view engines + - The leading `:` character in `name` for `app.param(name, fn)` is no longer removed + - Use `router` module for routing + - Use `path-is-absolute` module for absolute path detection + +5.0.0-alpha.1 / 2014-11-06 +========================== + +This is the first Express 5.0 alpha release, based off 4.10.1. + + * remove: + - `app.del` - use `app.delete` + - `req.acceptsCharset` - use `req.acceptsCharsets` + - `req.acceptsEncoding` - use `req.acceptsEncodings` + - `req.acceptsLanguage` - use `req.acceptsLanguages` + - `res.json(obj, status)` signature - use `res.json(status, obj)` + - `res.jsonp(obj, status)` signature - use `res.jsonp(status, obj)` + - `res.send(body, status)` signature - use `res.send(status, body)` + - `res.send(status)` signature - use `res.sendStatus(status)` + - `res.sendfile` - use `res.sendFile` instead + - `express.query` middleware + * change: + - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname + - `req.query` is now a getter instead of a plain property + * add: + - `app.router` is a reference to the base router 4.17.3 / 2022-02-16 =================== @@ -2137,7 +2212,7 @@ * deps: connect@2.21.0 - deprecate `connect(middleware)` -- use `app.use(middleware)` instead - deprecate `connect.createServer()` -- use `connect()` instead - - fix `res.setHeader()` patch to work with get -> append -> set pattern + - fix `res.setHeader()` patch to work with with get -> append -> set pattern - deps: compression@~1.0.8 - deps: errorhandler@~1.1.1 - deps: express-session@~1.5.0 @@ -2967,7 +3042,7 @@ Closes #805 * Added route `Collection`, ex: `app.get('/user/:id').remove();` * Added support for `app.param(fn)` to define param logic * Removed `app.param()` support for callback with return value - * Removed require.main check from express(1) generated app. Closes #670 + * Removed module.parent check from express(1) generated app. Closes #670 * Refactored router. Closes #639 2.3.6 / 2011-05-20 @@ -3348,8 +3423,8 @@ Shaw] * Added node v0.1.97 compatibility * Added support for deleting cookies via Request#cookie('key', null) * Updated haml submodule - * Fixed not-found page, now using charset utf-8 - * Fixed show-exceptions page, now using charset utf-8 + * Fixed not-found page, now using using charset utf-8 + * Fixed show-exceptions page, now using using charset utf-8 * Fixed view support due to fs.readFile Buffers * Changed; mime.type() no longer accepts ".type" due to node extname() changes @@ -3384,7 +3459,7 @@ Shaw] ================== * Added charset support via Request#charset (automatically assigned to 'UTF-8' when respond()'s - encoding is set to 'utf8' or 'utf-8'). + encoding is set to 'utf8' or 'utf-8'. * Added "encoding" option to Request#render(). Closes #299 * Added "dump exceptions" setting, which is enabled by default. * Added simple ejs template engine support @@ -3423,7 +3498,7 @@ Shaw] * Added [haml.js](http://github.com/visionmedia/haml.js) submodule; removed haml-js * Added callback function support to Request#halt() as 3rd/4th arg * Added preprocessing of route param wildcards using param(). Closes #251 - * Added view partial support (with collections etc.) + * Added view partial support (with collections etc) * Fixed bug preventing falsey params (such as ?page=0). Closes #286 * Fixed setting of multiple cookies. Closes #199 * Changed; view naming convention is now NAME.TYPE.ENGINE (for example page.html.haml) diff --git a/Readme.md b/Readme.md index d0f3cf56e6..b60d588c41 100644 --- a/Readme.md +++ b/Readme.md @@ -1,10 +1,12 @@ [![Express Logo](https://i.cloudup.com/zfY6lL7eFa-3000x3000.png)](http://expressjs.com/) - Fast, unopinionated, minimalist web framework for [Node.js](http://nodejs.org). + Fast, unopinionated, minimalist web framework for [node](http://nodejs.org). - [![NPM Version][npm-version-image]][npm-url] - [![NPM Install Size][npm-install-size-image]][npm-install-size-url] - [![NPM Downloads][npm-downloads-image]][npm-downloads-url] + [![NPM Version][npm-image]][npm-url] + [![NPM Downloads][downloads-image]][downloads-url] + [![Linux Build][ci-image]][ci-url] + [![Windows Build][appveyor-image]][appveyor-url] + [![Test Coverage][coveralls-image]][coveralls-url] ```js const express = require('express') @@ -31,7 +33,7 @@ the [`npm init` command](https://docs.npmjs.com/creating-a-package-json-file). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): -```console +```bash $ npm install express ``` @@ -51,7 +53,7 @@ for more information. ## Docs & Community * [Website and Documentation](http://expressjs.com/) - [[website repo](https://github.com/expressjs/expressjs.com)] - * [#express](https://web.libera.chat/#express) on [Libera Chat](https://libera.chat) IRC + * [#express](https://webchat.freenode.net/?channels=express) on freenode IRC * [GitHub Organization](https://github.com/expressjs) for Official Middleware & Modules * Visit the [Wiki](https://github.com/expressjs/express/wiki) * [Google Group](https://groups.google.com/group/express-js) for discussion @@ -59,31 +61,35 @@ for more information. **PROTIP** Be sure to read [Migrating from 3.x to 4.x](https://github.com/expressjs/express/wiki/Migrating-from-3.x-to-4.x) as well as [New features in 4.x](https://github.com/expressjs/express/wiki/New-features-in-4.x). +### Security Issues + +If you discover a security vulnerability in Express, please see [Security Policies and Procedures](Security.md). + ## Quick Start The quickest way to get started with express is to utilize the executable [`express(1)`](https://github.com/expressjs/generator) to generate an application as shown below: Install the executable. The executable's major version will match Express's: -```console +```bash $ npm install -g express-generator@4 ``` Create the app: -```console +```bash $ express /tmp/foo && cd /tmp/foo ``` Install dependencies: -```console +```bash $ npm install ``` Start the server: -```console +```bash $ npm start ``` @@ -103,43 +109,31 @@ $ npm start To view the examples, clone the Express repo and install the dependencies: -```console -$ git clone https://github.com/expressjs/express.git --depth 1 +```bash +$ git clone git://github.com/expressjs/express.git --depth 1 $ cd express $ npm install ``` Then run whichever example you want: -```console +```bash $ node examples/content-negotiation ``` -## Contributing - - [![Linux Build][github-actions-ci-image]][github-actions-ci-url] - [![Windows Build][appveyor-image]][appveyor-url] - [![Test Coverage][coveralls-image]][coveralls-url] - -The Express.js project welcomes all constructive contributions. Contributions take many forms, -from code for bug fixes and enhancements, to additions and fixes to documentation, additional -tests, triaging incoming pull requests and issues, and more! +## Tests -See the [Contributing Guide](Contributing.md) for more technical details on contributing. + To run the test suite, first install the dependencies, then run `npm test`: -### Security Issues - -If you discover a security vulnerability in Express, please see [Security Policies and Procedures](Security.md). - -### Running Tests - -To run the test suite, first install the dependencies, then run `npm test`: - -```console +```bash $ npm install $ npm test ``` +## Contributing + +[Contributing Guide](Contributing.md) + ## People The original author of Express is [TJ Holowaychuk](https://github.com/tj) @@ -152,15 +146,13 @@ The current lead maintainer is [Douglas Christopher Wilson](https://github.com/d [MIT](LICENSE) -[appveyor-image]: https://badgen.net/appveyor/ci/dougwilson/express/master?label=windows +[ci-image]: https://img.shields.io/github/workflow/status/expressjs/express/ci/master.svg?label=linux +[ci-url]: https://github.com/expressjs/express/actions?query=workflow%3Aci +[npm-image]: https://img.shields.io/npm/v/express.svg +[npm-url]: https://npmjs.org/package/express +[downloads-image]: https://img.shields.io/npm/dm/express.svg +[downloads-url]: https://npmcharts.com/compare/express?minimal=true +[appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/express/master.svg?label=windows [appveyor-url]: https://ci.appveyor.com/project/dougwilson/express -[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/express/master +[coveralls-image]: https://img.shields.io/coveralls/expressjs/express/master.svg [coveralls-url]: https://coveralls.io/r/expressjs/express?branch=master -[github-actions-ci-image]: https://badgen.net/github/checks/expressjs/express/master?label=linux -[github-actions-ci-url]: https://github.com/expressjs/express/actions/workflows/ci.yml -[npm-downloads-image]: https://badgen.net/npm/dm/express -[npm-downloads-url]: https://npmcharts.com/compare/express?minimal=true -[npm-install-size-image]: https://badgen.net/packagephobia/install/express -[npm-install-size-url]: https://packagephobia.com/result?p=express -[npm-url]: https://npmjs.org/package/express -[npm-version-image]: https://badgen.net/npm/v/express diff --git a/Security.md b/Security.md index cdcd7a6e0a..858dfffc5b 100644 --- a/Security.md +++ b/Security.md @@ -27,7 +27,8 @@ endeavor to keep you informed of the progress towards a fix and full announcement, and may ask for additional information or guidance. Report security bugs in third-party modules to the person or team maintaining -the module. +the module. You can also report a vulnerability through the +[Node Security Project](https://nodesecurity.io/report). ## Disclosure Policy diff --git a/examples/cookies/index.js b/examples/cookies/index.js index d2a4ea6220..fecda79d7a 100644 --- a/examples/cookies/index.js +++ b/examples/cookies/index.js @@ -38,7 +38,7 @@ app.get('/forget', function(req, res){ app.post('/', function(req, res){ var minute = 60000; - if (req.body.remember) res.cookie('remember', 1, { maxAge: minute }); + if (req.body?.remember) res.cookie('remember', 1, { maxAge: minute }); res.redirect('back'); }); diff --git a/examples/downloads/index.js b/examples/downloads/index.js index c560976621..49f424fbf8 100644 --- a/examples/downloads/index.js +++ b/examples/downloads/index.js @@ -12,7 +12,7 @@ var app = module.exports = express(); // path to where the files are stored on disk var FILES_DIR = path.join(__dirname, 'files') -app.get('/', function(req, res){ +app.get('/', (req, res) => { res.send('