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

add support for native http2 module #1911

Open
wants to merge 1 commit 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: 1 addition & 1 deletion examples/server.http2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Install:
* npm install browser-sync http2
* npm install browser-sync
*
* Run:
* node <yourfile.js>
Expand Down
44 changes: 28 additions & 16 deletions packages/browser-sync/lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,54 @@ var serverUtils = {
getServer: function(app, options) {
return {
server: (function() {
var httpModule = serverUtils.getHttpModule(options);
const serverFactory = serverUtils.getServerFactory(options);

if (
options.get("scheme") === "https" ||
options.get("httpModule") === "http2"
options.get("scheme") === "https"
) {
var opts = serverUtils.getHttpsOptions(options);
return httpModule.createServer(opts.toJS(), app);
return serverFactory(opts.toJS(), app);
}

return httpModule.createServer(app);
return serverFactory(app);
})(),
app: app
};
},
getHttpModule: function(options) {
getServerFactory: function(options) {
/**
* Users may provide a string to be used by nodes
* Users may provide a string to be used by node's
* require lookup.
*/
var httpModule = options.get("httpModule");

if (typeof httpModule === "string") {
const httpModuleOption = options.get("httpModule");
let httpModule;
if (typeof httpModuleOption === "string") {
/**
* Note, this could throw, but let that happen as
* the error message good enough.
*/
var maybe = require.resolve(httpModule);
return require(maybe);
var maybe = require.resolve(httpModuleOption);
httpModule = require(maybe);
}

if (options.get("scheme") === "https") {
return https;
else if (options.get("scheme") === "https") {
httpModule = https;
}
else {
httpModule = http;
}

return http;
const secure =
options.get("scheme") === "https" ||
// for backwards-compatibility, we assume that if http2 then
// also using TLS (most browsers don't support non-secure http2)
httpModuleOption === "http2";
if (secure && typeof httpModule.createSecureServer === 'function') {
// e.g. node's native http2 module has a separate createSecureServer method:
return httpModule.createSecureServer.bind(httpModule);
}
else {
return httpModule.createServer.bind(httpModule);
}
},
getMiddlewares: function(bs) {
var clientJs = bs.pluginManager.hook("client:js", {
Expand Down
Loading