Skip to content

Commit f9a48b5

Browse files
committed
support http2 via http2 options
1 parent 29c2b82 commit f9a48b5

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ a node-style callback. The config object must have at minimum an `http` or
3030
| `https.sni` | See [SNI Support](#sni-support). |
3131
| `https.handler` | Handler for HTTPS requests. If you want to share a handler with all servers, use a top-level `handler` config property instead. |
3232
| `https.*` | Any other properties supported by [https.createServer](https://nodejs.org/dist/latest-v8.x/docs/api/https.html#https_https_createserver_options_requestlistener) can be added to the https object, except `secureProtocol` and `secureOptions` which are set to recommended values. |
33+
| `http2` | Optional object. If present, an HTTP/2 server is started. You may start multiple HTTP/2 servers by passing an array of objects |
34+
| `http2.allowHTTP1` | Enable [ALPN negotiation] allowing support for both HTTPS and HTTP/2 on the same socket. |
3335

3436
If successful, the `create-servers` callback is passed an object with the
3537
following properties:
@@ -286,3 +288,4 @@ var servers = createServers(
286288
[article]: https://certsimple.com/blog/a-plus-node-js-ssl
287289
[iojs]: https://github.com/iojs/io.js
288290
[ciphers]: https://iojs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
291+
[ALPN negotiation]: https://nodejs.org/api/http2.html#http2_alpn_negotiation

index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,29 @@ module.exports = async function createServers(options, listening) {
5151
return listening(err);
5252
}
5353

54-
const [[httpErr, http], [httpsErr, https]] = await Promise.all([
54+
const [
55+
[httpErr, http],
56+
[httpsErr, https],
57+
[http2Err, http2]] = await Promise.all([
5558
createHttp(options.http, options.log),
56-
createHttps(options.https, options.log)
59+
createHttps(options.https, options.log),
60+
createHttps(options.http2, options.log, true)
5761
]);
5862

5963
const servers = {};
6064
if (http) servers.http = http;
6165
if (https) servers.https = https;
66+
if (http2) servers.http2 = http2;
6267

63-
if (httpErr || httpsErr) {
64-
let errorSource = httpsErr || httpErr;
68+
if (httpErr || httpsErr || http2Err) {
69+
let errorSource = http2Err || httpsErr || httpErr;
6570
if (Array.isArray(errorSource)) {
6671
errorSource = errorSource[0];
6772
}
6873
return listening(
6974
errs.create({
7075
message: errorSource && errorSource.message,
76+
http2: http2Err,
7177
https: httpsErr,
7278
http: httpErr
7379
}),
@@ -81,14 +87,16 @@ module.exports = async function createServers(options, listening) {
8187
function normalizeOptions(options) {
8288
const http = normalizeHttpOptions(options.http, options);
8389
const https = normalizeHttpsOptions(options.https, options);
90+
const http2 = normalizeHttpsOptions(options.http2, options);
8491

85-
if (!http && !https) {
92+
if (!http && !https && !http2) {
8693
throw new Error('http and/or https are required options');
8794
}
8895

8996
return {
9097
http,
9198
https,
99+
http2,
92100
log: options.log || function() {}
93101
};
94102
}
@@ -308,14 +316,14 @@ async function createHttp(httpConfig, log) {
308316
// ### function createHttps ()
309317
// Attempts to create and listen on the HTTPS server.
310318
//
311-
async function createHttps(ssl, log) {
319+
async function createHttps(ssl, log, h2) {
312320
if (typeof ssl === 'undefined') {
313321
log('https | no options.https; no server');
314322
return [null, null];
315323
}
316324

317325
if (Array.isArray(ssl)) {
318-
return await createMultiple(createHttps, ssl, log);
326+
return await createMultiple(createHttps, ssl, log, h2);
319327
}
320328

321329
return await new Promise(resolve => {
@@ -350,7 +358,11 @@ async function createHttps(ssl, log) {
350358
}
351359

352360
log('https | listening on %d', port);
353-
server = https.createServer(finalHttpsOptions, ssl.handler);
361+
if(h2) {
362+
server = require('http2').createSecureServer(finalHttpsOptions, ssl.handler)
363+
} else {
364+
server = https.createServer(finalHttpsOptions, ssl.handler);
365+
}
354366

355367
if (typeof timeout === 'number') server.setTimeout(timeout);
356368
args = [server, port];

0 commit comments

Comments
 (0)