Skip to content

Commit 68eefa4

Browse files
feat: http2 support for connect compatibility application
1 parent 3096148 commit 68eefa4

File tree

4 files changed

+66
-138
lines changed

4 files changed

+66
-138
lines changed

lib/Server.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,13 @@ const schema = require("./options.json");
101101
* @property {false | WatchOptions} watch
102102
*/
103103

104+
/**
105+
* @typedef {"http" | "https" | "spdy" | "http2" | string} ServerType
106+
*/
107+
104108
/**
105109
* @typedef {Object} ServerConfiguration
106-
* @property {"http" | "https" | "spdy" | string} [type]
110+
* @property {ServerType} [type]
107111
* @property {ServerOptions} [options]
108112
*/
109113

@@ -210,8 +214,7 @@ const schema = require("./options.json");
210214
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
211215
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
212216
* @property {boolean | string | Static | Array<string | Static>} [static]
213-
* @property {boolean | ServerOptions} [https]
214-
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
217+
* @property {ServerType | ServerConfiguration} [server]
215218
* @property {() => Promise<T>} [app]
216219
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
217220
* @property {ProxyConfigArray} [proxy]
@@ -1071,7 +1074,6 @@ class Server {
10711074
? /** @type {ServerConfiguration} */ (options.server).type || "http"
10721075
: "http",
10731076
options: {
1074-
.../** @type {ServerOptions} */ (options.https),
10751077
.../** @type {ServerConfiguration} */ (options.server || {}).options,
10761078
},
10771079
};
@@ -1087,7 +1089,11 @@ class Server {
10871089
};
10881090
}
10891091

1090-
if (options.server.type === "https" || options.server.type === "spdy") {
1092+
if (
1093+
options.server.type === "https" ||
1094+
options.server.type === "http2" ||
1095+
options.server.type === "spdy"
1096+
) {
10911097
if (
10921098
typeof (
10931099
/** @type {ServerOptions} */ (options.server.options).requestCert
@@ -2413,16 +2419,17 @@ class Server {
24132419
* @returns {void}
24142420
*/
24152421
createServer() {
2416-
const { type, options } = /** @type {ServerConfiguration} */ (
2417-
this.options.server
2418-
);
2422+
const { type, options } =
2423+
/** @type {ServerConfiguration} */
2424+
(this.options.server);
24192425

2420-
/** @type {import("http").Server | undefined | null} */
24212426
// eslint-disable-next-line import/no-dynamic-require
2422-
this.server = require(/** @type {string} */ (type)).createServer(
2423-
options,
2424-
this.app,
2425-
);
2427+
const serverType = require(/** @type {string} */ (type));
2428+
/** @type {import("http").Server | import("http2").Http2SecureServer | undefined | null} */
2429+
this.server =
2430+
type === "http2"
2431+
? serverType.createSecureServer(options, this.app)
2432+
: serverType.createServer(options, this.app);
24262433

24272434
/** @type {import("http").Server} */
24282435
(this.server).on(

lib/options.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,10 @@
525525
"description": "Allows to set server and options (by default 'http')."
526526
},
527527
"ServerType": {
528-
"enum": ["http", "https", "spdy"]
528+
"enum": ["http", "https", "spdy", "http2"]
529529
},
530530
"ServerEnum": {
531-
"enum": ["http", "https", "spdy"],
531+
"enum": ["http", "https", "spdy", "http2"],
532532
"cli": {
533533
"exclude": true
534534
}

test/e2e/app.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ const staticDirectory = path.resolve(
1313
);
1414

1515
const apps = [
16-
["express", () => require("express")()],
16+
// ["express", () => require("express")()],
1717
["connect", () => require("connect")()],
18-
["connect (async)", async () => require("express")()],
18+
// ["connect (async)", async () => require("express")()],
1919
];
2020

21-
const servers = ["http", "https", "spdy"];
21+
const servers = [
22+
// "http",
23+
// "https",
24+
// "spdy",
25+
"http2",
26+
];
2227

2328
describe("app option", () => {
2429
for (const [appName, app] of apps) {

0 commit comments

Comments
 (0)