From c50078717f291f3cb301b0bc315eac1b42f6d7b6 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Tue, 13 Jan 2015 12:09:40 +0000 Subject: [PATCH] feat(server): add serveFile method for plugin use --- lib/async.js | 3 +- lib/browser-sync.js | 13 +++ lib/server/index.js | 53 ++++++----- .../specs/plugins/user.plugins.serve.files.js | 93 +++++++++++++++++++ 4 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 test/specs/plugins/user.plugins.serve.files.js diff --git a/lib/async.js b/lib/async.js index 46dd184c1..a160f72e7 100644 --- a/lib/async.js +++ b/lib/async.js @@ -163,7 +163,8 @@ module.exports = { done(null, { instance: { clientJs: clientJs, - server: server + server: server.server, + app: server.app } }); }, diff --git a/lib/browser-sync.js b/lib/browser-sync.js index d28ca680c..e09c74128 100644 --- a/lib/browser-sync.js +++ b/lib/browser-sync.js @@ -218,6 +218,19 @@ BrowserSync.prototype.getMiddleware = function (type) { } }; +/** + * @param path + * @param props + */ +BrowserSync.prototype.serveFile = function (path, props) { + if (this.app) { + this.app.use(path, function (req, res) { + res.setHeader("Content-Type", props.type); + res.end(props.content); + }); + } +}; + /** * Middleware for socket connection (external usage) * @param port diff --git a/lib/server/index.js b/lib/server/index.js index 44ea0d6f1..131fe5e3d 100644 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -40,12 +40,15 @@ module.exports.plugin = function (bs, scripts) { } if (bsServer) { - bsServer.listen(bs.options.get("port")); + bsServer.server.listen(bs.options.get("port")); } debug("Running mode: %s", type.toUpperCase()); - return bsServer; + return { + server: bsServer.server, + app: bsServer.app + }; }; /** @@ -61,7 +64,7 @@ module.exports.createServer = function (options, clientScripts, bs) { var server = options.get("server"); if (!proxy && !server) { - return createSnippetServer(options, clientScripts, bs); + return createSnippetServer(options, clientScripts); } if (proxy) { @@ -69,7 +72,7 @@ module.exports.createServer = function (options, clientScripts, bs) { } if (server) { - return createServer(server, options, clientScripts, bs); + return createServer(server, options, clientScripts); } }; @@ -77,10 +80,9 @@ module.exports.createServer = function (options, clientScripts, bs) { * @param {Immutable.Map} server * @param {Immutable.Map} options * @param scripts - * @param context * @returns {*} */ -function createServer (server, options, scripts, context) { +function createServer (server, options, scripts) { var middleware = server.get("middleware"); var secure = options.get("scheme") === "https"; @@ -89,8 +91,7 @@ function createServer (server, options, scripts, context) { index = server.get("index") || "index.html"; - app = context.app = connect(); - context.connect = connect; + app = connect(); app.use(function (req, res, next) { snippetUtils.isOldIe(req); @@ -116,27 +117,34 @@ function createServer (server, options, scripts, context) { if (secure) { secureKey = fs.readFileSync(secure.key || __dirname + "/certs/server.key"); secureCert = fs.readFileSync(secure.cert || __dirname + "/certs/server.crt"); - return https.createServer({key: secureKey, cert: secureCert}, app); + return { + server: https.createServer({key: secureKey, cert: secureCert}, app), + app: app + }; } else { - return http.createServer(app); + return { + server: http.createServer(app), + app: app + }; } } /** * @param options * @param scripts - * @param context * @returns {*} */ -function createSnippetServer (options, scripts, context) { +function createSnippetServer (options, scripts) { - var app = context.app = connect(); - context.connect = connect; + var app = connect(); app.use(options.getIn(["scriptPaths", "versioned"]), scripts) .use(options.getIn(["scriptPaths", "path"]), scripts); - return http.createServer(app); + return { + server: http.createServer(app), + app: app + }; } /** @@ -154,16 +162,19 @@ module.exports.createProxy = function (options, scripts, bs) { }); var snippetOptions = options.get("snippetOptions").toJS(); - - return foxy(options.getIn(["proxy", "target"]), { - rules: snippetUtils.getRegex(options.get("snippet"), options.get("snippetOptions")), - ignorePaths: snippetOptions.ignorePaths, - middleware: snippetUtils.getProxyMiddleware(scripts, options.getIn(["scriptPaths", "versioned"])), - errHandler: function (err) { + var foxyServer = foxy(options.getIn(["proxy", "target"]), { + rules: snippetUtils.getRegex(options.get("snippet"), options.get("snippetOptions")), + ignorePaths: snippetOptions.ignorePaths, + middleware: snippetUtils.getProxyMiddleware(scripts, options.getIn(["scriptPaths", "versioned"])), + errHandler: function (err) { bs.logger.debug("{red:[proxy error]} %s", err.message); } } ); + + return { + server: foxyServer + }; }; /** diff --git a/test/specs/plugins/user.plugins.serve.files.js b/test/specs/plugins/user.plugins.serve.files.js new file mode 100644 index 000000000..677ecb9e0 --- /dev/null +++ b/test/specs/plugins/user.plugins.serve.files.js @@ -0,0 +1,93 @@ +"use strict"; + +var browserSync = require("../../../index"); + +var assert = require("chai").assert; +var request = require("supertest"); + +describe("Plugins: Should be able to call `serveFile` on the instance when in server mode", function () { + + var PLUGIN_NAME = "KITTENZ"; + var instance; + + before(function (done) { + + browserSync.reset(); + + var config = { + logLevel: "silent", + open: false, + server: "test/fixtures" + }; + + browserSync.use({ + plugin: function (opts, bs) { + bs.serveFile("/shane", { + type: "text/css", + content: "Hi there" + }); + done(); + }, + "plugin:name": PLUGIN_NAME + }); + + instance = browserSync(config).instance; + }); + after(function () { + instance.cleanup(); + }); + it("should serve the file", function (done) { + request(instance.server) + .get("/shane") + .set("accept", "text/html") + .expect(200) + .end(function (err, res) { + assert.include(res.text, "Hi there"); + assert.equal(res.headers["content-type"], "text/css"); + done(); + }); + }); +}); + +describe("Plugins: Should be able to call `serveFile` on the instance when in snippet mode", function () { + + var PLUGIN_NAME = "KITTENZ"; + var instance; + + before(function (done) { + + browserSync.reset(); + + var config = { + logLevel: "silent", + open: false + }; + + browserSync.use({ + plugin: function (opts, bs) { + bs.serveFile("/shane", { + type: "text/css", + content: "Hi there" + }); + done(); + }, + "plugin:name": PLUGIN_NAME + }); + + instance = browserSync(config).instance; + }); + after(function () { + instance.cleanup(); + }); + it("should serve the file", function (done) { + request(instance.server) + .get("/shane") + .set("accept", "text/html") + .expect(200) + .end(function (err, res) { + assert.include(res.text, "Hi there"); + assert.equal(res.headers["content-type"], "text/css"); + done(); + }); + }); +});