http2-express 1.0.0
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/.babelrc +3 -0
- package/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/core.js +102 -0
- package/dist/http2Request.js +40 -0
- package/dist/http2Response.js +25 -0
- package/dist/index.js +5 -0
- package/dist/initHttp2Express.js +24 -0
- package/example/.babelrc +3 -0
- package/example/certs/server-cert.pem +35 -0
- package/example/certs/server-key.pem +54 -0
- package/example/package-lock.json +5710 -0
- package/example/package.json +48 -0
- package/example/src/server.js +24 -0
- package/package.json +47 -0
- package/src/core.js +111 -0
- package/src/http2Request.js +40 -0
- package/src/http2Response.js +25 -0
- package/src/index.js +4 -0
- package/src/initHttp2Express.js +22 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "http2-express",
|
|
3
|
+
"description": "HTTP/2 support for Express",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"author": "Mark Jovis",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Mark Jovis <mark.jovis@hotmail.com>"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"express",
|
|
12
|
+
"http2",
|
|
13
|
+
"https",
|
|
14
|
+
"push",
|
|
15
|
+
"support",
|
|
16
|
+
"compatibility",
|
|
17
|
+
"api",
|
|
18
|
+
"server"
|
|
19
|
+
],
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"module": "dist/index.js",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"start": "NODE_ENV=localdev nodemon src/server.js -e js,json",
|
|
24
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@babel/cli": "^7.23.9",
|
|
28
|
+
"@babel/eslint-parser": "^7.23.10",
|
|
29
|
+
"@babel/preset-env": "^7.23.9",
|
|
30
|
+
"eslint": "^8.57.0",
|
|
31
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
32
|
+
"eslint-plugin-import": "^2.29.1"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"express": "^4.21.1"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">= 20.0.0"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/markjovis/http2-express.git"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/markjovis/http2-express/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/markjovis/http2-express#readme"
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const http2 = require('http2');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
const http2Express = require('../../dist');
|
|
6
|
+
|
|
7
|
+
const app = http2Express(express);
|
|
8
|
+
|
|
9
|
+
app.get('/', (req, res) => {
|
|
10
|
+
res.send('Hello World!');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const options = {
|
|
14
|
+
key: fs.readFileSync('certs/server-key.pem'),
|
|
15
|
+
cert: fs.readFileSync('certs/server-cert.pem'),
|
|
16
|
+
passphrase: 'Test123',
|
|
17
|
+
allowHTTP1: false
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const server = http2.createSecureServer(options, app);
|
|
21
|
+
|
|
22
|
+
server.listen(44320, () => {
|
|
23
|
+
console.info('Listening on port 44320...');
|
|
24
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "http2-express",
|
|
3
|
+
"description": "HTTP/2 support for Express",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"author": "Mark Jovis",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Mark Jovis <mark.jovis@hotmail.com>"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"express",
|
|
12
|
+
"http2",
|
|
13
|
+
"https",
|
|
14
|
+
"push",
|
|
15
|
+
"support",
|
|
16
|
+
"compatibility",
|
|
17
|
+
"api",
|
|
18
|
+
"server"
|
|
19
|
+
],
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"module": "dist/index.js",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "babel src -d dist",
|
|
24
|
+
"prepublishOnly": "npm run build",
|
|
25
|
+
"createTypeFiles": "npx -p typescript tsc src/*.js --declaration --allowJs --emitDeclarationOnly --outDir types",
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@babel/cli": "^7.23.9",
|
|
30
|
+
"@babel/eslint-parser": "^7.23.10",
|
|
31
|
+
"@babel/preset-env": "^7.23.9",
|
|
32
|
+
"eslint": "^8.57.0",
|
|
33
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
34
|
+
"eslint-plugin-import": "^2.29.1"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">= 20.0.0"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/markjovis/http2-express.git"
|
|
42
|
+
},
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/markjovis/http2-express/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/markjovis/http2-express#readme"
|
|
47
|
+
}
|
package/src/core.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/* eslint-disable func-names */
|
|
2
|
+
/* eslint-disable no-underscore-dangle */
|
|
3
|
+
const EventEmitter = require('events');
|
|
4
|
+
|
|
5
|
+
const createHttp2Request = require('./http2Request');
|
|
6
|
+
const createHttp2Response = require('./http2Response');
|
|
7
|
+
const initHttp2Express = require('./initHttp2Express');
|
|
8
|
+
|
|
9
|
+
const createHttp2Express = (express) => {
|
|
10
|
+
const {
|
|
11
|
+
application,
|
|
12
|
+
request,
|
|
13
|
+
response,
|
|
14
|
+
Router,
|
|
15
|
+
query
|
|
16
|
+
} = express;
|
|
17
|
+
|
|
18
|
+
application.lazyrouter = function lazyrouter() {
|
|
19
|
+
if (!this._router) {
|
|
20
|
+
this._router = new Router({
|
|
21
|
+
caseSensitive: this.enabled('case sensitive routing'),
|
|
22
|
+
strict: this.enabled('strict routing')
|
|
23
|
+
});
|
|
24
|
+
this._router.use(query(this.get('query parser fn')));
|
|
25
|
+
this._router.use(initHttp2Express(this));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const app = function (req, res, next) {
|
|
30
|
+
app.handle(req, res, next);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Copying properties and descriptors from EventEmitter.prototype to app.
|
|
34
|
+
Object.getOwnPropertyNames(EventEmitter.prototype).forEach((key) => {
|
|
35
|
+
const descriptor = Object.getOwnPropertyDescriptor(EventEmitter.prototype, key);
|
|
36
|
+
if (descriptor) {
|
|
37
|
+
Object.defineProperty(app, key, descriptor);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Copy symbols from EventEmitter.prototype to app.
|
|
42
|
+
Object.getOwnPropertySymbols(EventEmitter.prototype).forEach((symbol) => {
|
|
43
|
+
const descriptor = Object.getOwnPropertyDescriptor(EventEmitter.prototype, symbol);
|
|
44
|
+
if (descriptor) {
|
|
45
|
+
Object.defineProperty(app, symbol, descriptor);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Copying properties from application to app.
|
|
50
|
+
Object.getOwnPropertyNames(application).forEach((key) => {
|
|
51
|
+
const descriptor = Object.getOwnPropertyDescriptor(application, key);
|
|
52
|
+
if (descriptor) {
|
|
53
|
+
Object.defineProperty(app, key, descriptor);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Copy symbols from application to app.
|
|
58
|
+
Object.getOwnPropertySymbols(application).forEach((symbol) => {
|
|
59
|
+
const descriptor = Object.getOwnPropertyDescriptor(application, symbol);
|
|
60
|
+
if (descriptor) {
|
|
61
|
+
Object.defineProperty(app, symbol, descriptor);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
response.push = () => {};
|
|
66
|
+
|
|
67
|
+
// Expose the prototype that will get set on requests.
|
|
68
|
+
app.request = Object.create(request, {
|
|
69
|
+
app: {
|
|
70
|
+
configurable: true,
|
|
71
|
+
enumerable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: app
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Expose the prototype that will get set on responses.
|
|
78
|
+
app.response = Object.create(response, {
|
|
79
|
+
app: {
|
|
80
|
+
configurable: true,
|
|
81
|
+
enumerable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: app
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const http2Request = createHttp2Request(request);
|
|
88
|
+
const http2Response = createHttp2Response(response);
|
|
89
|
+
|
|
90
|
+
app.http2Request = Object.create(http2Request, {
|
|
91
|
+
app: {
|
|
92
|
+
configurable: true,
|
|
93
|
+
enumerable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: app
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
app.http2Response = Object.create(http2Response, {
|
|
100
|
+
app: {
|
|
101
|
+
configurable: true,
|
|
102
|
+
enumerable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: app
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
app.init();
|
|
108
|
+
return app;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
module.exports = createHttp2Express;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* eslint-disable dot-notation */
|
|
2
|
+
const { Http2ServerRequest } = require('http2');
|
|
3
|
+
|
|
4
|
+
const createHttp2Request = (request) => {
|
|
5
|
+
const http2Request = Object.create(Http2ServerRequest.prototype);
|
|
6
|
+
|
|
7
|
+
// Copying properties and descriptors from request to http2Request.
|
|
8
|
+
Object.getOwnPropertyNames(request).forEach((key) => {
|
|
9
|
+
const descriptor = Object.getOwnPropertyDescriptor(request, key);
|
|
10
|
+
if (descriptor) {
|
|
11
|
+
Object.defineProperty(http2Request, key, descriptor);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Retain symbols, if any, from request.
|
|
16
|
+
Object.getOwnPropertySymbols(request).forEach((symbol) => {
|
|
17
|
+
const descriptor = Object.getOwnPropertyDescriptor(request, symbol);
|
|
18
|
+
if (descriptor) {
|
|
19
|
+
Object.defineProperty(http2Request, symbol, descriptor);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const requestHostName = Object.getOwnPropertyDescriptor(http2Request, 'hostname')?.get;
|
|
24
|
+
|
|
25
|
+
// Redefine hostname property with custom getter.
|
|
26
|
+
if (requestHostName) {
|
|
27
|
+
Object.defineProperty(http2Request, 'hostname', {
|
|
28
|
+
get() {
|
|
29
|
+
if (!this.headers['host'] && this.authority) {
|
|
30
|
+
this.headers['host'] = this.authority;
|
|
31
|
+
}
|
|
32
|
+
return requestHostName.call(this);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return http2Request;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
module.exports = createHttp2Request;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const { Http2ServerResponse } = require('http2');
|
|
2
|
+
|
|
3
|
+
const createHttp2Response = (response) => {
|
|
4
|
+
const http2Response = Object.create(Http2ServerResponse.prototype);
|
|
5
|
+
|
|
6
|
+
// Copying properties and descriptors from response to http2Response.
|
|
7
|
+
Object.getOwnPropertyNames(response).forEach((key) => {
|
|
8
|
+
const descriptor = Object.getOwnPropertyDescriptor(response, key);
|
|
9
|
+
if (descriptor) {
|
|
10
|
+
Object.defineProperty(http2Response, key, descriptor);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
// Copy symbols from response to http2Response.
|
|
15
|
+
Object.getOwnPropertySymbols(response).forEach((symbol) => {
|
|
16
|
+
const descriptor = Object.getOwnPropertyDescriptor(response, symbol);
|
|
17
|
+
if (descriptor) {
|
|
18
|
+
Object.defineProperty(http2Response, symbol, descriptor);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return http2Response;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
module.exports = createHttp2Response;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const initHttp2Express = (app) => (req, res, next) => {
|
|
2
|
+
if (app.enabled('x-powered-by')) {
|
|
3
|
+
res.setHeader('X-Powered-By', 'HTTP/2 Express (http2-express)');
|
|
4
|
+
}
|
|
5
|
+
req.res = res;
|
|
6
|
+
res.req = req;
|
|
7
|
+
req.next = next;
|
|
8
|
+
|
|
9
|
+
const { socket } = req.httpVersion === '2.0' ? req.stream.session : req;
|
|
10
|
+
if (socket.alpnProtocol && (socket.alpnProtocol === 'h2' || socket.alpnProtocol === 'h2c')) {
|
|
11
|
+
Object.setPrototypeOf(req, app.http2Request);
|
|
12
|
+
Object.setPrototypeOf(res, app.http2Response);
|
|
13
|
+
} else {
|
|
14
|
+
Object.setPrototypeOf(req, app.request);
|
|
15
|
+
Object.setPrototypeOf(res, app.response);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
res.locals = res.locals || Object.create(null);
|
|
19
|
+
next();
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = initHttp2Express;
|