Skip to content

Commit 67d58ef

Browse files
authored
Merge pull request #1250 from BrowserSync/AddSupportForOverridingHttpModule
Add support for overriding http module
2 parents 4af1a4d + 8ba178e commit 67d58ef

File tree

9 files changed

+108
-13
lines changed

9 files changed

+108
-13
lines changed

examples/server.http2.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
*
3+
* Install:
4+
* npm install browser-sync http2
5+
*
6+
* Run:
7+
* node <yourfile.js>
8+
*
9+
* This example will create a server using http2 using the default information & use the `app` directory as the root
10+
*
11+
*/
12+
13+
"use strict";
14+
15+
var browserSync = require("browser-sync").create();
16+
17+
browserSync.init({
18+
files: ["app/css/*.css"],
19+
server: {
20+
baseDir: "app"
21+
},
22+
https: true,
23+
httpModule: "http2"
24+
});

lib/default-config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ module.exports = {
124124
* @since 1.3.0
125125
*/
126126

127+
/**
128+
* Override http module to allow using 3rd party server modules (such as http2)
129+
* @property httpModule
130+
* @type string
131+
* @default undefined
132+
* @since 2.17.6
133+
*/
134+
127135
/**
128136
* Clicks, Scrolls & Form inputs on any device will be mirrored to all others.
129137
* @property ghostMode

lib/server/utils.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,48 @@ var serverUtils = {
4040
};
4141
},
4242
/**
43-
* Get either an http or https server
43+
* Get either http or https server
44+
* or use the httpModule provided in options if present
4445
*/
4546
getServer: function (app, options) {
4647
return {
4748
server: (function () {
49+
50+
var httpModule = serverUtils.getHttpModule(options);
51+
4852
if (options.get("scheme") === "https") {
4953
var pfxPath = options.getIn(["https", "pfx"]);
5054
return pfxPath ?
51-
https.createServer(serverUtils.getPFX(pfxPath), app) :
52-
https.createServer(serverUtils.getKeyAndCert(options), app);
55+
httpModule.createServer(serverUtils.getPFX(pfxPath), app) :
56+
httpModule.createServer(serverUtils.getKeyAndCert(options), app);
5357
}
54-
return http.createServer(app);
58+
59+
return httpModule.createServer(app);
5560
})(),
5661
app: app
5762
};
5863
},
64+
getHttpModule: function (options) {
65+
/**
66+
* Users may provide a string to be used by nodes
67+
* require lookup.
68+
*/
69+
var httpModule = options.get("httpModule");
70+
71+
if (typeof httpModule === "string") {
72+
/**
73+
* Note, this could throw, but let that happen as
74+
* the error message good enough.
75+
*/
76+
return require(httpModule);
77+
}
78+
79+
if (options.get("scheme") === "https") {
80+
return https;
81+
}
82+
83+
return http;
84+
},
5985
getMiddlewares: function (bs) {
6086

6187
var clientJs = bs.pluginManager.hook("client:js", {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"scripts": {
2222
"env": "node ./test/env.js",
2323
"test": "npm run lint && npm run env && npm run unit",
24-
"lint": "eslint index.js lib bin examples test/specs gulpfile.js",
24+
"lint": "eslint index.js lib bin examples test/specs gulpfile.js --fix",
2525
"pro": "protractor test/protractor/config.single.js",
2626
"pro-local": "node test/protractor/setup.js",
2727
"unit": "mocha --recursive test/specs --timeout 10000 --bail",
@@ -73,6 +73,7 @@
7373
"gulp-contribs": "0.0.3",
7474
"gulp-conventional-changelog": "1.1.0",
7575
"gulp-filter": "4.0.0",
76+
"http2": "^3.3.6",
7677
"istanbul": "0.4.5",
7778
"istanbul-coveralls": "1.0.3",
7879
"lodash-cli": "4.15.0",

test/specs/e2e/middleware/middleware.option.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe("Accepting middleware as a plain object", function () {
103103
};
104104

105105
browserSync.init(config, function (err, bs) {
106-
request(bs.server)
106+
request(bs.options.getIn(["urls", "local"]))
107107
.get("/")
108108
.set("accept", "text/html")
109109
.expect(200)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
3+
var browserSync = require("../../../../index");
4+
5+
var assert = require("chai").assert;
6+
var request = require("supertest");
7+
8+
describe("E2E httpModule options test", function () {
9+
10+
it("creates server using provided httpModule", function (done) {
11+
12+
browserSync.reset();
13+
14+
var config = {
15+
server: {
16+
baseDir: "test/fixtures"
17+
},
18+
https: true,
19+
httpModule: "http2",
20+
open: false,
21+
logLevel: "silent"
22+
};
23+
24+
browserSync.init(config, function (err, bs) {
25+
request(bs.options.getIn(["urls", "local"]))
26+
.get("/index.html")
27+
.set("accept", "text/html")
28+
.expect(200)
29+
.end(function (err, res) {
30+
assert.include(res.text, bs.options.get("snippet"));
31+
bs.cleanup();
32+
done();
33+
});
34+
});
35+
});
36+
});

test/specs/e2e/server/e2e.server.secure.custom.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("E2E TLS server with custom certs test", function () {
4040
});
4141

4242
it("serves files with the snippet added", function (done) {
43-
request(instance.options.getIn(['urls', 'local']))
43+
request(instance.options.getIn(["urls", "local"]))
4444
.get("/index.html")
4545
.set("accept", "text/html")
4646
.expect(200)
@@ -53,7 +53,7 @@ describe("E2E TLS server with custom certs test", function () {
5353

5454
it("serves the client script", function (done) {
5555

56-
request(instance.options.getIn(['urls', 'local']))
56+
request(instance.options.getIn(["urls", "local"]))
5757
.get(instance.options.getIn(["scriptPaths", "versioned"]))
5858
.expect(200)
5959
.end(function (err, res) {

test/specs/e2e/server/e2e.server.secure.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("E2E TLS server options test", function () {
4141

4242
assert.isString(bs.options.get("snippet"));
4343

44-
request(bs.options.getIn(['urls', 'local']))
44+
request(bs.options.getIn(["urls", "local"]))
4545
.get("/index.html")
4646
.set("accept", "text/html")
4747
.expect(200)
@@ -82,7 +82,7 @@ describe("E2E TLS server test (1)", function () {
8282

8383
assert.isString(bs.options.get("snippet"));
8484

85-
request(bs.options.getIn(['urls', 'local']))
85+
request(bs.options.getIn(["urls", "local"]))
8686
.get("/index.html")
8787
.set("accept", "text/html")
8888
.expect(200)
@@ -94,7 +94,7 @@ describe("E2E TLS server test (1)", function () {
9494

9595
it("serves the client script", function (done) {
9696

97-
request(bs.options.getIn(['urls', 'local']))
97+
request(bs.options.getIn(["urls", "local"]))
9898
.get(bs.options.getIn(["scriptPaths", "versioned"]))
9999
.expect(200)
100100
.end(function (err, res) {

test/specs/e2e/server/e2e.server.secure.pfx.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("E2E TLS server with PFX certs test", function () {
4242

4343
assert.isString(instance.options.get("snippet"));
4444

45-
request(instance.options.getIn(['urls', 'local']))
45+
request(instance.options.getIn(["urls", "local"]))
4646
.get("/index.html")
4747
.set("accept", "text/html")
4848
.expect(200)
@@ -54,7 +54,7 @@ describe("E2E TLS server with PFX certs test", function () {
5454

5555
it("serves the client script", function (done) {
5656

57-
request(instance.options.getIn(['urls', 'local']))
57+
request(instance.options.getIn(["urls", "local"]))
5858
.get(instance.options.getIn(["scriptPaths", "versioned"]))
5959
.expect(200)
6060
.end(function (err, res) {

0 commit comments

Comments
 (0)