Skip to content

Commit

Permalink
feat(server): add serveFile method for plugin use
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jan 13, 2015
1 parent f77fd80 commit c500787
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ module.exports = {
done(null, {
instance: {
clientJs: clientJs,
server: server
server: server.server,
app: server.app
}
});
},
Expand Down
13 changes: 13 additions & 0 deletions lib/browser-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 32 additions & 21 deletions lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
};

/**
Expand All @@ -61,26 +64,25 @@ 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) {
return exports.createProxy(options, clientScripts, bs);
}

if (server) {
return createServer(server, options, clientScripts, bs);
return createServer(server, options, clientScripts);
}
};

/**
* @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";
Expand All @@ -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);
Expand All @@ -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
};
}

/**
Expand All @@ -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
};
};

/**
Expand Down
93 changes: 93 additions & 0 deletions test/specs/plugins/user.plugins.serve.files.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit c500787

Please sign in to comment.