Skip to content
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

Support http2 #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ startCluster(options, () => {
| sticky | `Boolean` | sticky mode server |
| port | `Number` | port |
| https | `Object` | start a https server, note: `key` / `cert` should be full path to file |
| http2 | `Boolean` | start a http2 server |
| typescript | `Boolean` | enable loader's typescript support |


## License

[MIT](LICENSE)
20 changes: 17 additions & 3 deletions lib/app_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,28 @@ function startServer(err) {
app.removeListener('startTimeout', startTimeoutHandler);

let server;
let httpsOptions;
if (options.https) {
const httpsOptions = Object.assign({}, options.https, {
httpsOptions = Object.assign({}, options.https, {
key: fs.readFileSync(options.https.key),
cert: fs.readFileSync(options.https.cert),
});
server = require('https').createServer(httpsOptions, app.callback());
}

if (options.http2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need testcase

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will add test cases later. This may be a bit complicated as supertest does not support http2.

try {
const http2 = require('http2');
server = options.https ?
http2.createSecureServer(httpsOptions, app.callback()) :
http2.createServer(app.callback());
} catch (err) {
consoleLogger.error('[app_worker] server got error: %s, code: %s', err.message, err.code);
process.exit(1);
}
} else {
server = require('http').createServer(app.callback());
server = options.https ?
require('https').createServer(httpsOptions, app.callback()) :
require('http').createServer(app.callback());
}

server.once('error', err => {
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function(options) {
workers: null,
plugins: null,
https: false,
http2: false,
key: '',
cert: '',
typescript: false,
Expand Down Expand Up @@ -54,6 +55,7 @@ module.exports = function(options) {

options.port = parseInt(options.port, 10) || undefined;
options.workers = parseInt(options.workers, 10);
options.http2 = Boolean(options.http2);

// don't print depd message in production env.
// it will print to stderr.
Expand Down
7 changes: 7 additions & 0 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ describe('test/options.test.js', () => {
assert(options.https.cert);
});

it('should start with http2', () => {
const options = parseOptions({
http2: true,
});
assert(options.http2 === true);
});

it('should start with httpsOptions and listen 8443', () => {
const options = parseOptions({
https: {
Expand Down