Skip to content

Commit 8ba178e

Browse files
committed
feat(httpModule): Allow custom HTTP module to be given in options
1 parent 5136fd5 commit 8ba178e

File tree

10 files changed

+61
-108
lines changed

10 files changed

+61
-108
lines changed

examples/server.http2.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
"use strict";
1414

15-
var http2 = require("http2");
1615
var browserSync = require("browser-sync").create();
1716

1817
browserSync.init({
@@ -21,5 +20,5 @@ browserSync.init({
2120
baseDir: "app"
2221
},
2322
https: true,
24-
httpModule: http2
23+
httpModule: "http2"
2524
});

lib/default-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ module.exports = {
127127
/**
128128
* Override http module to allow using 3rd party server modules (such as http2)
129129
* @property httpModule
130-
* @type Object|Function
130+
* @type string
131131
* @default undefined
132132
* @since 2.17.6
133133
*/

lib/public/init.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ module.exports = function (browserSync, name, pjson) {
2727

2828
args.config.version = pjson.version;
2929

30-
/**
31-
* Preserve the httpModule property's functions.
32-
* the http2 module exports an object of functions and the merge function seems
33-
* to want to destroy that, but if the base object is a function it seems fine
34-
* TODO: find a better or more generic way to handle this
35-
*/
36-
if(args.config.httpModule && !_.isFunction(args.config.httpModule)) {
37-
args.config.httpModule = Object.assign(function() {}, args.config.httpModule);
38-
}
39-
4030
return browserSync.init(merge(args.config), args.cb);
4131
};
4232
};

lib/server/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,42 @@ var serverUtils = {
4646
getServer: function (app, options) {
4747
return {
4848
server: (function () {
49-
var httpModule = options.get("httpModule") || (options.get("scheme") === "https" ? https : http);
49+
50+
var httpModule = serverUtils.getHttpModule(options);
5051

5152
if (options.get("scheme") === "https") {
5253
var pfxPath = options.getIn(["https", "pfx"]);
5354
return pfxPath ?
5455
httpModule.createServer(serverUtils.getPFX(pfxPath), app) :
5556
httpModule.createServer(serverUtils.getKeyAndCert(options), app);
5657
}
58+
5759
return httpModule.createServer(app);
5860
})(),
5961
app: app
6062
};
6163
},
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+
},
6285
getMiddlewares: function (bs) {
6386

6487
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)

test/specs/e2e/server/e2e.server.httpModule.js

Lines changed: 25 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,34 @@
33
var browserSync = require("../../../../index");
44

55
var assert = require("chai").assert;
6-
var sinon = require("sinon");
6+
var request = require("supertest");
77

88
describe("E2E httpModule options test", function () {
99

10-
this.timeout(15000);
11-
12-
var bs;
13-
var mockHttpModule, mockHttpServer, createServerSpy, https;
14-
15-
describe("httpModule undefined", function () {
16-
17-
before(function (done) {
18-
19-
browserSync.reset();
20-
https = require("https");
21-
22-
createServerSpy = sinon.spy(https, "createServer");
23-
24-
var config = {
25-
server: {
26-
baseDir: "test/fixtures",
27-
},
28-
https: true,
29-
open: false,
30-
logLevel: "silent"
31-
};
32-
33-
bs = browserSync.init(config, done).instance;
34-
});
35-
36-
after(function () {
37-
bs.cleanup();
38-
});
39-
40-
it("creates server using the default https module", function () {
41-
sinon.assert.calledOnce(createServerSpy);
42-
});
43-
44-
it("should be using the server from the https module", function () {
45-
assert.equal(bs.server instanceof https.Server, true);
46-
});
47-
});
48-
49-
describe("httpModule defined", function () {
50-
51-
before(function (done) {
52-
53-
browserSync.reset();
54-
55-
mockHttpServer = {
56-
on: function() { },
57-
listen: function() { },
58-
listeners: function() { return []; },
59-
removeAllListeners: function() { },
60-
close: function() { }
61-
};
62-
63-
mockHttpModule = {
64-
createServer: function() {
65-
return mockHttpServer;
66-
}
67-
};
68-
69-
createServerSpy = sinon.spy(mockHttpModule, "createServer");
70-
71-
var config = {
72-
server: {
73-
baseDir: "test/fixtures",
74-
},
75-
https: true,
76-
httpModule: mockHttpModule,
77-
open: false,
78-
logLevel: "silent"
79-
};
80-
81-
bs = browserSync.init(config, done).instance;
82-
});
83-
84-
after(function () {
85-
bs.cleanup();
86-
});
87-
88-
it("creates server using provided httpModule", function () {
89-
sinon.assert.calledOnce(createServerSpy);
90-
});
91-
92-
it("should be using the server created by the provided httpModule", function () {
93-
assert.equal(bs.server, mockHttpServer);
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+
});
9434
});
9535
});
9636
});

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)