Skip to content

Commit 5136fd5

Browse files
committed
Merge branch 'AddSupportForOverridingHttpModule' of github.com:itslenny/browser-sync into AddSupportForOverridingHttpModule
2 parents 4af1a4d + 558c6da commit 5136fd5

File tree

5 files changed

+146
-4
lines changed

5 files changed

+146
-4
lines changed

examples/server.http2.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 http2 = require("http2");
16+
var browserSync = require("browser-sync").create();
17+
18+
browserSync.init({
19+
files: ["app/css/*.css"],
20+
server: {
21+
baseDir: "app"
22+
},
23+
https: true,
24+
httpModule: http2
25+
});

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 Object|Function
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/public/init.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ 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+
3040
return browserSync.init(merge(args.config), args.cb);
3141
};
3242
};

lib/server/utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ 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+
var httpModule = options.get("httpModule") || (options.get("scheme") === "https" ? https : http);
50+
4851
if (options.get("scheme") === "https") {
4952
var pfxPath = options.getIn(["https", "pfx"]);
5053
return pfxPath ?
51-
https.createServer(serverUtils.getPFX(pfxPath), app) :
52-
https.createServer(serverUtils.getKeyAndCert(options), app);
54+
httpModule.createServer(serverUtils.getPFX(pfxPath), app) :
55+
httpModule.createServer(serverUtils.getKeyAndCert(options), app);
5356
}
54-
return http.createServer(app);
57+
return httpModule.createServer(app);
5558
})(),
5659
app: app
5760
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"use strict";
2+
3+
var browserSync = require("../../../../index");
4+
5+
var assert = require("chai").assert;
6+
var sinon = require("sinon");
7+
8+
describe("E2E httpModule options test", function () {
9+
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);
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)