Skip to content

Commit 845472d

Browse files
committed
change the way static server works
now routes, directory & serve static are handled as simple middleware additions
1 parent 8a595d4 commit 845472d

10 files changed

Lines changed: 158 additions & 255 deletions

File tree

lib/server/proxy-server.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,34 @@ module.exports = function createProxyServer (bs, scripts) {
6767
var target = opt.get("target");
6868
var proxyReq = getProxyReqFunctions(opt.get("proxyReq"), opt, bs);
6969
var proxyRes = getProxyResFunctions(opt.get("proxyRes"), opt);
70-
var app = utils.getBaseApp(bs, scripts);
71-
72-
app.stack.push({
73-
id: "Browsersync Proxy",
74-
route: opt.get("route"),
75-
handle: function (req, res) {
76-
proxy.web(req, res, {
77-
target: target
78-
});
79-
}
80-
});
70+
71+
/**
72+
* Add the proxy middleware to any given by the user
73+
*/
74+
var middlewareAdded = bs.options
75+
.update("middleware", function (mw) {
76+
return mw.concat({
77+
id: "Browsersync Proxy",
78+
route: opt.get("route"),
79+
handle: function (req, res) {
80+
proxy.web(req, res, {
81+
target: target
82+
});
83+
}
84+
})
85+
});
86+
87+
var app = utils.getBaseApp(bs, middlewareAdded, scripts);
8188

8289
/**
8390
* @type {*|{server, app}}
8491
*/
85-
var p = utils.getServer(app, bs.options);
86-
p.proxy = proxy;
92+
var browserSyncServer = utils.getServer(app, bs.options);
93+
browserSyncServer.proxy = proxy;
8794

8895
if (opt.get("ws")) {
8996
// debug(`+ ws upgrade for: ${x.get("target")}`);
90-
p.server.on("upgrade", function (req, socket, head) {
97+
browserSyncServer.server.on("upgrade", function (req, socket, head) {
9198
proxy.ws(req, socket, head);
9299
});
93100
}
@@ -124,7 +131,7 @@ module.exports = function createProxyServer (bs, scripts) {
124131
});
125132
}
126133

127-
return p;
134+
return browserSyncServer;
128135
};
129136

130137
/**

lib/server/static-server.js

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"use strict";
22

33
var connect = require("connect");
4-
var utils = require("./utils.js");
4+
var serverUtils = require("./utils.js");
5+
var resolve = require("path").resolve;
6+
var utils = require("../utils.js");
7+
var serveStatic = require("serve-static");
8+
var serveIndex = require("serve-index");
59

610
/**
711
* @param {BrowserSync} bs
@@ -13,54 +17,60 @@ module.exports = function createServer (bs, scripts) {
1317
var options = bs.options;
1418
var server = options.get("server");
1519
var middleware = options.get("middleware");
16-
var basedir = server.get("baseDir");
17-
var serveStaticOptions = server.get("serveStaticOptions");
20+
var basedirs = utils.arrayify(server.get("baseDir"));
21+
var serveStaticOptions = server.get("serveStaticOptions").toJS(); // passed to 3rd party
1822

19-
var app = connect();
23+
var _serveStatic = 0;
24+
var _routes = 0;
25+
var middlewareAdded = bs.options
26+
/**
27+
* Add directory Middleware if given in server.directory
28+
*/
29+
.update("middleware", function (mw) {
30+
if (!server.has("directory")) {
31+
return mw;
32+
}
2033

21-
/**
22-
* Handle Old IE
23-
*/
24-
app.use(utils.handleOldIE);
34+
return mw.concat({
35+
route: "",
36+
handle: serveIndex(resolve(basedirs[0]), {icons:true}),
37+
id: "Browsersync Server Directory Middleware"
38+
});
39+
})
40+
/**
41+
* Add middleware for server.baseDir Option
42+
*/
43+
.update("middleware", function (mw) {
44+
return mw.concat(basedirs.map(function (root) {
45+
return {
46+
route: "",
47+
id: "Browsersync Server ServeStatic Middleware - " + _serveStatic++,
48+
handle: serveStatic(resolve(root), serveStaticOptions)
49+
}
50+
}));
51+
})
52+
/**
53+
* Add middleware for server.routes
54+
*/
55+
.update("middleware", function (mw) {
2556

26-
/**
27-
* Serve the Client-side JS from both version and static paths
28-
*/
29-
app.use(options.getIn(["scriptPaths", "versioned"]), scripts)
30-
.use(options.getIn(["scriptPaths", "path"]), scripts);
57+
if (!server.has("routes")) {
58+
return mw;
59+
}
3160

32-
/**
33-
* Add directory middleware
34-
*/
35-
if (server.get("directory")) {
36-
utils.addDirectory(app, basedir);
37-
}
38-
39-
/**
40-
* Add snippet injection middleware
41-
* This also includes any additional middleware given from the user
42-
*/
43-
app.use(bs.snippetMw.middleware);
61+
return mw.concat(server.get("routes").map(function (root, urlPath) {
62+
return {
63+
route: urlPath,
64+
id: "Browsersync Server Routes Middleware - " + _routes++,
65+
handle: serveStatic(resolve(root))
66+
}
67+
}));
68+
});
4469

45-
/**
46-
* Add user-provided middlewares
47-
*/
48-
utils.addMiddleware(app, middleware);
49-
50-
/**
51-
* Add Serve static middlewares
52-
*/
53-
utils.addBaseDir(app, basedir, serveStaticOptions);
54-
55-
/**
56-
* Add further Serve static middlewares for routes
57-
*/
58-
if (server.get("routes")) {
59-
utils.addRoutes(app, server.get("routes").toJS());
60-
}
70+
var app = serverUtils.getBaseApp(bs, middlewareAdded, scripts);
6171

6272
/**
6373
* Finally, return the server + App
6474
*/
65-
return utils.getServer(app, bs.options);
75+
return serverUtils.getServer(app, bs.options);
6676
};

lib/server/utils.js

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,12 @@
22

33
var fs = require("fs");
44
var filePath = require("path");
5-
var serveIndex = require("serve-index");
6-
var serveStatic = require("serve-static");
7-
var _ = require("lodash");
85
var connect = require("connect");
96
var http = require("http");
107
var https = require("https");
11-
var Immutable = require("immutable");
12-
var isList = Immutable.List.isList;
13-
var snippetUtils = require("./../snippet").utils;
8+
var snippet = require("./../snippet").utils;
149

1510
var utils = {
16-
/**
17-
* @param app
18-
* @param middleware
19-
* @returns {*}
20-
*/
21-
addMiddleware: function (app, middleware) {
22-
23-
middleware.forEach(function (item) {
24-
app.use(item);
25-
});
26-
27-
return app;
28-
},
29-
/**
30-
* @param app
31-
* @param base
32-
* @param opts
33-
*/
34-
addBaseDir: function (app, base, opts) {
35-
36-
opts = opts.toJS();
37-
38-
if (isList(base)) {
39-
base.forEach(function (item) {
40-
app.use(serveStatic(filePath.resolve(item), opts));
41-
});
42-
} else {
43-
if (_.isString(base)) {
44-
app.use(serveStatic(filePath.resolve(base), opts));
45-
}
46-
}
47-
},
48-
/**
49-
* @param app
50-
* @param base
51-
*/
52-
addDirectory: function (app, base) {
53-
if (isList(base)) {
54-
base = base.get(0);
55-
}
56-
app.use(serveIndex(filePath.resolve(base), {icons:true}));
57-
},
58-
/**
59-
* @param app
60-
* @param {Object} routes
61-
*/
62-
addRoutes: function (app, routes) {
63-
Object.keys(routes).forEach(function (key) {
64-
if (_.isString(key) && _.isString(routes[key])) {
65-
app.use(key, serveStatic(filePath.resolve(routes[key])));
66-
}
67-
});
68-
},
6911
/**
7012
* @param options
7113
* @returns {{key, cert}}
@@ -85,16 +27,6 @@ var utils = {
8527
pfx: fs.readFileSync(filePath)
8628
};
8729
},
88-
/**
89-
* @param req
90-
* @param res
91-
* @param next
92-
* @returns {*}
93-
*/
94-
handleOldIE: function (req, res, next) {
95-
snippetUtils.isOldIe(req);
96-
return next();
97-
},
9830
/**
9931
* Get either an http or https server
10032
*/
@@ -112,7 +44,7 @@ var utils = {
11244
app: app
11345
};
11446
},
115-
getBaseApp: function (bs, scripts) {
47+
getBaseApp: function (bs, options, scripts) {
11648

11749
var app = connect();
11850

@@ -123,7 +55,7 @@ var utils = {
12355
{
12456
id: "Browsersync IE8 Support",
12557
route: "",
126-
handle: utils.handleOldIE
58+
handle: snippet.isOldIe(options.get("excludedFileTypes").toJS())
12759
},
12860
{
12961
id: "Browsersync Response Modifier",
@@ -145,8 +77,13 @@ var utils = {
14577
/**
14678
* Add user-provided middlewares
14779
*/
148-
bs.options.get("middleware").forEach(function (item) {
149-
app.use(item);
80+
options.get("middleware").forEach(function (item) {
81+
if (typeof item === "function") {
82+
return app.use(item);
83+
}
84+
if ((item.route !== undefined) && item.handle) {
85+
app.stack.push(item);
86+
}
15087
});
15188

15289
return app;

lib/snippet.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ var config = require("./config");
55

66
var lrSnippet = require("resp-modifier");
77
var path = require("path");
8-
var List = require("immutable").List;
98
var _ = require("lodash");
9+
var utils = require("./utils");
1010
var fs = require("fs");
1111

1212
/**
1313
* Utils for snippet injection
1414
*/
15-
var utils = {
15+
var snippetUtils = {
1616
/**
1717
* @param {String} url
1818
* @param {Array} excludeList
@@ -51,46 +51,48 @@ var utils = {
5151
};
5252
},
5353
getSnippetMiddleware: function (snippet, options, rewriteRules) {
54-
return lrSnippet.create(utils.getRules(snippet, options, rewriteRules));
54+
return lrSnippet.create(snippetUtils.getRules(snippet, options, rewriteRules));
5555
},
5656
getRules: function (snippet, options, rewriteRules) {
5757

58-
var rules = [utils.getRegex(snippet, options)];
58+
var rules = [snippetUtils.getRegex(snippet, options)];
5959

6060
if (rewriteRules) {
6161
rules = rules.concat(rewriteRules);
6262
}
6363

6464
return {
6565
rules: rules,
66-
blacklist: utils.arrify(options.get("blacklist")),
67-
whitelist: utils.arrify(options.get("whitelist"))
66+
blacklist: utils.arrayify(options.get("blacklist")),
67+
whitelist: utils.arrayify(options.get("whitelist"))
6868
};
6969
},
7070
/**
7171
* @param {Object} req
7272
* @param {Array} [excludeList]
7373
* @returns {Object}
7474
*/
75-
isOldIe: function (req, excludeList) {
76-
var ua = req.headers["user-agent"];
77-
var match = /MSIE (\d)\.\d/.exec(ua);
78-
if (match) {
79-
if (parseInt(match[1], 10) < 9) {
80-
if (!utils.isExcluded(req.url, excludeList)) {
81-
req.headers["accept"] = "text/html";
75+
isOldIe: function (excludeList) {
76+
return function (req, res, next) {
77+
var ua = req.headers["user-agent"];
78+
var match = /MSIE (\d)\.\d/.exec(ua);
79+
if (match) {
80+
if (parseInt(match[1], 10) < 9) {
81+
if (!snippetUtils.isExcluded(req.url, excludeList)) {
82+
req.headers["accept"] = "text/html";
83+
}
8284
}
8385
}
86+
next();
8487
}
85-
return req;
8688
},
8789
/**
8890
* @param {Number} port
8991
* @param {BrowserSync.options} options
9092
* @returns {String}
9193
*/
9294
getClientJs: function (port, options) {
93-
var socket = utils.getSocketScript();
95+
var socket = snippetUtils.getSocketScript();
9496
var noConflictJs = "window.___browserSync___oldSocketIo = window.io;";
9597
return noConflictJs + socket + ";" + connectUtils.socketConnector(options);
9698
},
@@ -99,16 +101,6 @@ var utils = {
99101
*/
100102
getSocketScript: function () {
101103
return fs.readFileSync(path.join(__dirname, config.socketIoScript), "utf-8");
102-
},
103-
/**
104-
* @param {Immutable.List|Array|String} incoming
105-
* @returns {Array}
106-
*/
107-
arrify: function (incoming) {
108-
if (List.isList(incoming)) {
109-
return incoming.toArray();
110-
}
111-
return [].concat(incoming).filter(Boolean);
112104
}
113105
};
114-
module.exports.utils = utils;
106+
module.exports.utils = snippetUtils;

0 commit comments

Comments
 (0)