http2-express 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -13
- package/package.json +12 -5
- package/src/core.js +18 -1
- package/src/http2-request.js +6 -0
- package/src/http2-response.js +6 -0
- package/src/index.d.ts +41 -0
- package/src/init-http2-express.js +6 -0
- package/src/util.js +18 -0
package/README.md
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
# http2-express
|
|
2
2
|
This package adds HTTP/2 support to express.js applications.
|
|
3
3
|
|
|
4
|
+
**NOTICE**: Replacement for old `http2-express-bridge`: you only change the package name in the import.
|
|
5
|
+
|
|
6
|
+
**http2-express** is a small package that lets an Express app run on Node's built-in `http2` server. Express is built on the HTTP/1.1 request and response classes and doesn't work with `http2` directly. This package makes the two compatible: you pass it the `express` module, get back a normal Express app, and hand that app to `http2.createSecureServer()` (or `http2.createServer()` for plain h2c).
|
|
7
|
+
|
|
8
|
+
```javascript
|
|
9
|
+
const app = http2Express(express);
|
|
10
|
+
http2.createSecureServer(options, app).listen(443);
|
|
11
|
+
```
|
|
12
|
+
**Limitation**: server push is not supported, because browsers have dropped it (Chrome removed it in version 106).
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
```bash
|
|
16
|
+
npm i http2-express
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Notice**: It is recommended to use Node.js **version 19** or higher.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
- [CommonJS (CJS) usage](#common-js-cjs-usage).
|
|
23
|
+
- [ES Modules (ESM) usage](#es-modules-esm-usage).
|
|
24
|
+
- [TypeScript usage](#typescript-usage).
|
|
25
|
+
|
|
26
|
+
## Version History
|
|
27
|
+
**Version 1.1.2** includes **TypeScript** type definitions (no separate `@types` package needed for http2-express). The package works with both JavaScript and TypeScript, and with Express 4 and Express 5.
|
|
28
|
+
<br>
|
|
29
|
+
<br>
|
|
30
|
+
|
|
4
31
|
**Version 1.1.1** Fixes the following bugs:
|
|
5
32
|
1. Fixed the crash issue when a stream is already destroyed.
|
|
6
33
|
2. Removed the side-effecting hostname getter that mutated this.headers['host']. The host header is now initialized once, upfront, in middleware.
|
|
@@ -21,17 +48,7 @@ This version is also backward compatible with Express 4.
|
|
|
21
48
|
<br>
|
|
22
49
|
<br>
|
|
23
50
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
## Installation
|
|
27
|
-
```bash
|
|
28
|
-
npm i http2-express
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
**Notice**: It is recommended to use Node.js **version 19** or higher.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
## Usage
|
|
51
|
+
## Common JS (CJS) usage
|
|
35
52
|
```javascript
|
|
36
53
|
const express = require('express');
|
|
37
54
|
const http2 = require('http2');
|
|
@@ -62,7 +79,114 @@ The code above creates an HTTP/2 server. An HTTP/2 server performs better than t
|
|
|
62
79
|
|
|
63
80
|
**Notice**: If option property "allowHTTP1" is set to true, then your server will be backward compatible with HTTP/1.1. If false, then only the new HTTP/2 is supported. Browsers and HTTP Clients that don't support HTTP/2 will error out if "allowHTTP1" is false.
|
|
64
81
|
|
|
65
|
-
ESModule "import" syntax is also supported.
|
|
66
82
|
|
|
67
|
-
|
|
83
|
+
## ES Modules (ESM) usage
|
|
84
|
+
The package works in both CommonJS (`require`) and ES module (`import`) projects, with Express 4 and Express 5. In an ESM project (`"type": "module"` in your `package.json`, or `.mjs` files), use a default import:
|
|
85
|
+
```javascript
|
|
86
|
+
import express from 'express';
|
|
87
|
+
import http2 from 'http2';
|
|
88
|
+
import fs from 'fs';
|
|
89
|
+
import http2Express from 'http2-express';
|
|
90
|
+
|
|
91
|
+
const app = http2Express(express);
|
|
92
|
+
|
|
93
|
+
app.get('/', (req, res) => {
|
|
94
|
+
res.send('Hello World!');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// ES modules have no __dirname; resolve files relative to this module with import.meta.url.
|
|
98
|
+
const options = {
|
|
99
|
+
key: fs.readFileSync(new URL('./certs/server-key.pem', import.meta.url)),
|
|
100
|
+
cert: fs.readFileSync(new URL('./certs/server-cert.pem', import.meta.url)),
|
|
101
|
+
passphrase: 'your certificate passphrase',
|
|
102
|
+
allowHTTP1: false
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const server = http2.createSecureServer(options, app);
|
|
106
|
+
|
|
107
|
+
server.listen(44320, () => {
|
|
108
|
+
console.info('Listening on port 44320...');
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## TypeScript usage
|
|
113
|
+
Type definitions ship with the package. Install the Express and Node.js types alongside it (use the `@types/express` major version that matches your Express version):
|
|
114
|
+
```bash
|
|
115
|
+
npm i -D typescript @types/node @types/express
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import express from 'express';
|
|
120
|
+
import http2 from 'http2';
|
|
121
|
+
import fs from 'fs';
|
|
122
|
+
import http2Express from 'http2-express';
|
|
123
|
+
|
|
124
|
+
const app = http2Express(express);
|
|
125
|
+
|
|
126
|
+
app.get('/', (req, res) => {
|
|
127
|
+
res.send('Hello World!');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const options: http2.SecureServerOptions = {
|
|
131
|
+
key: fs.readFileSync('path to your certificate key'),
|
|
132
|
+
cert: fs.readFileSync('path to your certificate'),
|
|
133
|
+
passphrase: 'your certificate passphrase',
|
|
134
|
+
allowHTTP1: false
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const server = http2.createSecureServer(options, app);
|
|
138
|
+
|
|
139
|
+
server.listen(44320, () => {
|
|
140
|
+
console.info('Listening on port 44320...');
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
`http2Express(express)` returns your regular, fully typed Express application (typed by whichever `@types/express` version you have installed) that can also be passed straight to `http2.createServer()` / `http2.createSecureServer()`. `esModuleInterop` must be enabled for the default `import` form above (it is on by default in current TypeScript); otherwise use `import http2Express = require('http2-express');`.
|
|
144
|
+
<br>
|
|
145
|
+
<br>
|
|
146
|
+
|
|
147
|
+
## JSDoc type checking in JavaScript (no build step)
|
|
148
|
+
In plain JavaScript ESM or CJS, add `// @ts-check` at the top of a file (or `"checkJs": true` in a `jsconfig.json`) and your editor (VS Code, WebStorm, ...) or `tsc --noEmit` will check your code against the bundled type definitions. Nothing is compiled; the JavaScript runs as-is. This works the same in CommonJS (`require`) and ES module (`import`) files.
|
|
149
|
+
```javascript
|
|
150
|
+
// @ts-check
|
|
151
|
+
const express = require('express');
|
|
152
|
+
const http2 = require('http2');
|
|
153
|
+
const http2Express = require('http2-express');
|
|
154
|
+
|
|
155
|
+
// The type is inferred automatically: your Express app, plus the HTTP/2 request listener signature.
|
|
156
|
+
const app = http2Express(express);
|
|
157
|
+
|
|
158
|
+
app.get('/', (req, res) => {
|
|
159
|
+
res.send('Hello World!'); // req and res are fully typed; a typo like res.sendd() is flagged
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const server = http2.createServer(app);
|
|
163
|
+
```
|
|
164
|
+
You normally don't need to write any type annotations, because the types are inferred as shown above.
|
|
165
|
+
|
|
166
|
+
Optional: if you pass the app to a function in another place, TypeScript can't infer the parameter's type, so describe it with a JSDoc `@param`. This continues the example above and uses the same `app`:
|
|
167
|
+
```javascript
|
|
168
|
+
/**
|
|
169
|
+
* The import(...) below is only a JSDoc type reference (it is not a runtime import),
|
|
170
|
+
* so it also works in CommonJS files that use require().
|
|
171
|
+
* @param {import('http2-express').Http2ExpressApplication<import('express').Express>} application
|
|
172
|
+
*/
|
|
173
|
+
function registerHealthRoute(application) {
|
|
174
|
+
application.get('/health', (req, res) => {
|
|
175
|
+
res.send('ok'); // req and res are typed here too
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
registerHealthRoute(app); // `app` from the example above
|
|
180
|
+
```
|
|
181
|
+
The same `@types/express` / `@types/node` dev dependencies listed above are needed for editor/`tsc` checking. If you use TypeScript 7 and your project contains the npm `punycode` package, add `"maxNodeModuleJsDepth": 0` to your `jsconfig.json` to stop it from checking JavaScript inside `node_modules`.
|
|
182
|
+
|
|
183
|
+
The package's own source is annotated with JSDoc too, and `npm run typecheck` checks it, together with the type tests in `test/`.
|
|
184
|
+
|
|
185
|
+
## Examples
|
|
186
|
+
Complete working examples are in these [github](https://github.com/markjovis/http2-express) folders (each is a standalone project; run it with `npm start`, or check its types with `npm run typecheck`):
|
|
68
187
|
|
|
188
|
+
| Folder | Language | Module system |
|
|
189
|
+
| --- | --- | --- |
|
|
190
|
+
| `example-express4-cjs`, `example-express5-cjs` | JavaScript (with `// @ts-check` and a `jsconfig.json`) | CommonJS (`require`) |
|
|
191
|
+
| `example-express4-esm`, `example-express5-esm` | JavaScript (with `// @ts-check` and a `jsconfig.json`) | ES modules (`import`) |
|
|
192
|
+
| `example-express4-ts`, `example-express5-ts` | TypeScript | compiled/run as CommonJS |
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "commonjs",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "HTTP/2 support for Express",
|
|
6
|
-
"version": "1.1.
|
|
6
|
+
"version": "1.1.2",
|
|
7
7
|
"author": "Mark Jovis",
|
|
8
8
|
"contributors": [
|
|
9
9
|
"grainrigi"
|
|
@@ -20,17 +20,24 @@
|
|
|
20
20
|
"server"
|
|
21
21
|
],
|
|
22
22
|
"main": "src/index.js",
|
|
23
|
-
"
|
|
23
|
+
"types": "src/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"src/"
|
|
26
|
+
],
|
|
24
27
|
"scripts": {
|
|
25
|
-
"
|
|
28
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
26
29
|
},
|
|
27
|
-
|
|
30
|
+
"peerDependencies": {
|
|
28
31
|
"express": ">=4.0.0"
|
|
29
32
|
},
|
|
30
33
|
"devDependencies": {
|
|
31
34
|
"@eslint/js": "^10.0.1",
|
|
35
|
+
"@types/express": "^5.0.6",
|
|
36
|
+
"@types/node": "^20.19.43",
|
|
32
37
|
"eslint": "^10.5.0",
|
|
33
|
-
"
|
|
38
|
+
"express": "^5.3.0",
|
|
39
|
+
"globals": "^17.6.0",
|
|
40
|
+
"typescript": "^7.0.2"
|
|
34
41
|
},
|
|
35
42
|
"engines": {
|
|
36
43
|
"node": ">= 20.0.0"
|
package/src/core.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
1
2
|
'use strict';
|
|
2
3
|
|
|
3
4
|
const EventEmitter = require('events');
|
|
@@ -7,6 +8,12 @@ const createHttp2Response = require('./http2-response');
|
|
|
7
8
|
const initHttp2Express = require('./init-http2-express');
|
|
8
9
|
const { setPoweredBy, isHttp2Request } = require('./util');
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Creates an Express application (v4 or v5) with HTTP/2 support.
|
|
13
|
+
* The public, strongly typed signature lives in `index.d.ts`.
|
|
14
|
+
* @param {Record<string, any>} express The `express` module.
|
|
15
|
+
* @returns {any} The HTTP/2-enabled Express application.
|
|
16
|
+
*/
|
|
10
17
|
const createHttp2Express = (express) => {
|
|
11
18
|
const {
|
|
12
19
|
application,
|
|
@@ -27,7 +34,7 @@ const createHttp2Express = (express) => {
|
|
|
27
34
|
const originalInit = application.init;
|
|
28
35
|
application.init = function init() {
|
|
29
36
|
originalInit.call(this);
|
|
30
|
-
this.router.use((req, res, next) => {
|
|
37
|
+
this.router.use((/** @type {any} */ req, /** @type {any} */ res, /** @type {() => void} */ next) => {
|
|
31
38
|
setPoweredBy(this, res);
|
|
32
39
|
// HTTP/2 carries the host in :authority. Populate headers['host'] once,
|
|
33
40
|
// upfront, so Express's req.hostname getter and any header-inspection
|
|
@@ -60,9 +67,19 @@ const createHttp2Express = (express) => {
|
|
|
60
67
|
// Safe under Node.js's single-threaded event loop: the overrides are set and
|
|
61
68
|
// consumed within the same synchronous call stack (no await between them and
|
|
62
69
|
// Express 5's prototype reads inside app.handle).
|
|
70
|
+
/** @type {any} */
|
|
63
71
|
let oneshotRequestOverride = null;
|
|
72
|
+
/** @type {any} */
|
|
64
73
|
let oneshotResponseOverride = null;
|
|
65
74
|
|
|
75
|
+
/**
|
|
76
|
+
* The request listener returned to the caller. Typed as `any` because it is
|
|
77
|
+
* built dynamically by copying Express's application members onto it.
|
|
78
|
+
* @param {any} req
|
|
79
|
+
* @param {any} res
|
|
80
|
+
* @param {(err?: unknown) => void} [next]
|
|
81
|
+
* @type {any}
|
|
82
|
+
*/
|
|
66
83
|
const app = function (req, res, next) {
|
|
67
84
|
if (isExpress5 && isHttp2Request(req)) {
|
|
68
85
|
oneshotRequestOverride = app.http2Request;
|
package/src/http2-request.js
CHANGED
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const { Http2ServerRequest } = require('http2');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Builds an HTTP/2 flavoured copy of Express's `request` prototype.
|
|
7
|
+
* @param {object} request Express's `request` prototype object.
|
|
8
|
+
* @returns {import('http2').Http2ServerRequest} The HTTP/2 request prototype.
|
|
9
|
+
*/
|
|
5
10
|
const createHttp2Request = (request) => {
|
|
11
|
+
/** @type {any} */
|
|
6
12
|
const http2Request = Object.create(Http2ServerRequest.prototype);
|
|
7
13
|
|
|
8
14
|
// Copying properties and descriptors from request to http2Request.
|
package/src/http2-response.js
CHANGED
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const { Http2ServerResponse } = require('http2');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Builds an HTTP/2 flavoured copy of Express's `response` prototype.
|
|
7
|
+
* @param {object} response Express's `response` prototype object.
|
|
8
|
+
* @returns {import('http2').Http2ServerResponse} The HTTP/2 response prototype.
|
|
9
|
+
*/
|
|
5
10
|
const createHttp2Response = (response) => {
|
|
11
|
+
/** @type {any} */
|
|
6
12
|
const http2Response = Object.create(Http2ServerResponse.prototype);
|
|
7
13
|
|
|
8
14
|
// Copying properties and descriptors from response to http2Response.
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Request listener signature used by `http2.createServer()` and
|
|
5
|
+
* `http2.createSecureServer()`.
|
|
6
|
+
*/
|
|
7
|
+
type Http2RequestListener = (req: Http2ServerRequest, res: Http2ServerResponse) => void;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* An Express application (as created by `express()`) that can also be passed
|
|
11
|
+
* directly to `http2.createServer()` / `http2.createSecureServer()` as the
|
|
12
|
+
* request listener. All the usual Express members (`get`, `use`, `listen`,
|
|
13
|
+
* `set`, ...) remain available.
|
|
14
|
+
*/
|
|
15
|
+
type Http2ExpressApplication<App> = App & Http2RequestListener;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates an Express application with HTTP/2 support.
|
|
19
|
+
*
|
|
20
|
+
* Works with both Express 4 and Express 5. The returned application type comes
|
|
21
|
+
* from the `express` module you pass in, so it follows whichever
|
|
22
|
+
* `@types/express` version your project uses.
|
|
23
|
+
*
|
|
24
|
+
* @param express The `express` module itself (not the result of calling it).
|
|
25
|
+
* @returns An Express application usable as an `http2` server request listener.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* import express from 'express';
|
|
29
|
+
* import http2 from 'http2';
|
|
30
|
+
* import http2Express from 'http2-express';
|
|
31
|
+
*
|
|
32
|
+
* const app = http2Express(express);
|
|
33
|
+
* const server = http2.createSecureServer({ key, cert }, app);
|
|
34
|
+
*/
|
|
35
|
+
declare function http2Express<App>(express: () => App): Http2ExpressApplication<App>;
|
|
36
|
+
|
|
37
|
+
declare namespace http2Express {
|
|
38
|
+
export { http2Express as default, Http2ExpressApplication, Http2RequestListener };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export = http2Express;
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
const { isHttp2Request, setPoweredBy } = require('./util');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Creates the Express middleware that swaps the request/response prototypes to
|
|
7
|
+
* the HTTP/2 or HTTP/1 variants.
|
|
8
|
+
* @param {any} app The HTTP/2-enabled Express application.
|
|
9
|
+
* @returns {(req: any, res: any, next: (err?: unknown) => void) => void}
|
|
10
|
+
*/
|
|
5
11
|
const initHttp2Express = (app) => (req, res, next) => {
|
|
6
12
|
setPoweredBy(app, res);
|
|
7
13
|
req.res = res;
|
package/src/util.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Minimal shape of an incoming request that is needed to detect HTTP/2.
|
|
5
|
+
* Satisfied by `http.IncomingMessage` and `http2.Http2ServerRequest`.
|
|
6
|
+
* @typedef {object} RequestLike
|
|
7
|
+
* @property {string} httpVersion
|
|
8
|
+
* @property {{ session?: { alpnProtocol?: string } }} [stream]
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sets the `X-Powered-By` header when the app has `x-powered-by` enabled.
|
|
13
|
+
* @param {{ enabled(name: string): boolean }} app An Express application.
|
|
14
|
+
* @param {{ setHeader(name: string, value: string): unknown }} res The response.
|
|
15
|
+
* @returns {void}
|
|
16
|
+
*/
|
|
3
17
|
exports.setPoweredBy = (app, res) => {
|
|
4
18
|
if (app.enabled('x-powered-by')) {
|
|
5
19
|
res.setHeader('X-Powered-By', 'HTTP/2 Express (http2-express)');
|
|
@@ -9,6 +23,10 @@ exports.setPoweredBy = (app, res) => {
|
|
|
9
23
|
// Using optional chaining so a destroyed/reset stream never throws TypeError.
|
|
10
24
|
// req.stream?.session?.alpnProtocol returns undefined (not a crash) when the
|
|
11
25
|
// stream or session has already been closed.
|
|
26
|
+
/**
|
|
27
|
+
* @param {RequestLike} req
|
|
28
|
+
* @returns {boolean} `true` when the request arrived over HTTP/2 (h2 or h2c).
|
|
29
|
+
*/
|
|
12
30
|
exports.isHttp2Request = (req) => {
|
|
13
31
|
if (req.httpVersion !== '2.0') return false;
|
|
14
32
|
const alpnProtocol = req.stream?.session?.alpnProtocol;
|