-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(http-server): adds http/2 support #4989
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
import assert from 'assert'; | ||
import http, {IncomingMessage, ServerResponse} from 'http'; | ||
import http2 from 'http2'; | ||
import https from 'https'; | ||
import {AddressInfo, ListenOptions} from 'net'; | ||
import os from 'os'; | ||
|
@@ -19,6 +20,14 @@ export type RequestListener = ( | |
res: ServerResponse, | ||
) => void; | ||
|
||
/** | ||
* Request listener function for http2/https2 requests | ||
*/ | ||
export type Http2RequestListener = ( | ||
req: http2.Http2ServerRequest, | ||
res: http2.Http2ServerResponse, | ||
) => void; | ||
|
||
/** | ||
* Base options that are common to http and https servers | ||
*/ | ||
|
@@ -52,17 +61,37 @@ export interface HttpsOptions extends BaseHttpOptions, https.ServerOptions { | |
protocol: 'https'; | ||
} | ||
|
||
/** | ||
* HTTP/2 HTTP server options | ||
*/ | ||
export interface Http2Options extends BaseHttpOptions, http2.ServerOptions { | ||
protocol: 'http2'; | ||
} | ||
|
||
/** | ||
* HTTP/2 HTTPS server options | ||
*/ | ||
export interface Https2Options | ||
extends BaseHttpOptions, | ||
http2.SecureServerOptions { | ||
protocol: 'https2'; | ||
} | ||
|
||
/** | ||
* Possible server options | ||
* | ||
*/ | ||
export type HttpServerOptions = HttpOptions | HttpsOptions; | ||
export type HttpServerOptions = | ||
| HttpOptions | ||
| HttpsOptions | ||
| Http2Options | ||
| Https2Options; | ||
|
||
/** | ||
* Supported protocols | ||
* | ||
*/ | ||
export type HttpProtocol = 'http' | 'https'; // Will be extended to `http2` in the future | ||
export type HttpProtocol = 'http' | 'https' | 'http2' | 'https2'; | ||
|
||
/** | ||
* HTTP / HTTPS server used by LoopBack's RestServer | ||
|
@@ -71,8 +100,12 @@ export class HttpServer { | |
private _listening = false; | ||
private _protocol: HttpProtocol; | ||
private _address: string | AddressInfo; | ||
private requestListener: RequestListener; | ||
readonly server: http.Server | https.Server; | ||
private requestListener: RequestListener | Http2RequestListener; | ||
readonly server: | ||
bajtos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| http.Server | ||
| https.Server | ||
| http2.Http2Server | ||
| http2.Http2SecureServer; | ||
private _stoppable: stoppable.StoppableServer; | ||
private serverOptions: HttpServerOptions; | ||
|
||
|
@@ -81,7 +114,7 @@ export class HttpServer { | |
* @param serverOptions | ||
*/ | ||
constructor( | ||
requestListener: RequestListener, | ||
requestListener: RequestListener | Http2RequestListener, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this constructor API sub-optimal, because it allows caller to mix HTTP v1 request listener with HTTP v2 server options and vice versa. Can we use function overloads to implement more tight type checks please? class HttpServer {
// ...
constructor(
requestListener: RequestListener,
serverOptions?: HttpOptions | HttpsOptions
);
constructor(
requestListener: Http2RequestListener,
serverOptions?: Http2Options | Https2Options
);
constructor(
requestListener: RequestListener | Http2RequestListener,
serverOptions?: HttpServerOptions,
) {
// the implementation
}
} |
||
serverOptions?: HttpServerOptions, | ||
) { | ||
this.requestListener = requestListener; | ||
|
@@ -95,19 +128,47 @@ export class HttpServer { | |
// Remove `port` so that `path` is honored | ||
delete this.serverOptions.port; | ||
} | ||
this._protocol = serverOptions ? serverOptions.protocol ?? 'http' : 'http'; | ||
if (this._protocol === 'https') { | ||
this.server = https.createServer( | ||
this.serverOptions as https.ServerOptions, | ||
this.requestListener, | ||
); | ||
} else { | ||
this.server = http.createServer(this.requestListener); | ||
this._protocol = serverOptions?.protocol ?? 'http'; | ||
switch (this._protocol) { | ||
case 'https': | ||
this.server = https.createServer( | ||
this.serverOptions as https.ServerOptions, | ||
this.requestListener as RequestListener, | ||
); | ||
break; | ||
case 'http': | ||
this.server = http.createServer( | ||
this.requestListener as RequestListener, | ||
); | ||
break; | ||
case 'http2': | ||
this.server = http2.createServer( | ||
this.requestListener as Http2RequestListener, | ||
); | ||
break; | ||
case 'https2': | ||
this.server = http2.createSecureServer( | ||
this.requestListener as Http2RequestListener, | ||
); | ||
break; | ||
} | ||
// Set up graceful stop for http server | ||
if (typeof this.serverOptions.gracePeriodForClose === 'number') { | ||
let serverNormalized; | ||
|
||
switch (this._protocol) { | ||
case 'http': | ||
case 'http2': | ||
serverNormalized = this.server as http.Server; | ||
break; | ||
case 'https': | ||
case 'https2': | ||
serverNormalized = this.server as https.Server; | ||
break; | ||
} | ||
|
||
this._stoppable = stoppable( | ||
this.server, | ||
serverNormalized, | ||
this.serverOptions.gracePeriodForClose, | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: can we please use
http2s
("http2" + "secure")? I findhttps2
weird to read.Note that HTTP/2 spec puts the version number before additional flags too, e.g
h2c
for clear-text connections (nothc2
). See e.g. https://stackoverflow.com/a/37373039/69868Similarly the interface should be called
Http2SecureOptions
or perhapsHttp2sOptions
if you prefer.