Skip to content

Commit

Permalink
feat(snippet): Add black/white lists - fixes #373
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Feb 3, 2015
1 parent bbf6b06 commit 6a2a296
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# BrowserSync [![Build status](https://ci.appveyor.com/api/projects/status/r5vung2ipn9uj4sy?svg=true)](https://ci.appveyor.com/project/shakyShane/browser-sync) [![Build Status](http://img.shields.io/travis/shakyShane/browser-sync/master.svg?style=flat)](https://travis-ci.org/shakyShane/browser-sync) [![Coverage Status](https://img.shields.io/coveralls/shakyShane/browser-sync.svg?style=flat)](https://coveralls.io/r/shakyShane/browser-sync?branch=master) [![NPM version](https://img.shields.io/npm/v/browser-sync.svg?style=flat)](https://www.npmjs.com/package/browser-sync)
# BrowserSync [![Build status](https://ci.appveyor.com/api/projects/status/r5vung2ipn9uj4sy?svg=true)](https://ci.appveyor.com/project/shakyShane/browser-sync) [![Build Status](https://travis-ci.org/BrowserSync/browser-sync.svg?branch=master)](https://travis-ci.org/BrowserSync/browser-sync) [![Coverage Status](https://img.shields.io/coveralls/shakyShane/browser-sync.svg?style=flat)](https://coveralls.io/r/shakyShane/browser-sync?branch=master) [![NPM version](https://img.shields.io/npm/v/browser-sync.svg?style=flat)](https://www.npmjs.com/package/browser-sync)

> Keep multiple browsers & devices in sync when building websites.
Expand Down
7 changes: 5 additions & 2 deletions lib/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ module.exports = {
* You can also provide patterns for certain urls
* that should be ignored from the snippet injection.
* @property snippetOptions
* @since 1.7.0
* @param {String|Array} [ignorePaths=undefined]
* @since 2.0.0
* @param {Array} [blacklis]
* @param {Array} [whitelist]
* @param {RegExp} [rule.match=/<body[^>]*>/i]
* @param {Function} [rule.fn=Function]
* @type Object
*/
snippetOptions: {
whitelist: [],
blacklist: [],
rule: {
match: /<body[^>]*>/i,
fn: function (snippet, match) {
Expand Down
37 changes: 35 additions & 2 deletions lib/public/init.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use strict";

var Immutable = require("immutable");
var merge = require("../cli/cli-options").merge;
var tfunk = require("eazy-logger").compile;
var _ = require("lodash");
var merge = require("../cli/cli-options").merge;
var tfunk = require("eazy-logger").compile;

/**
* @param {BrowserSync} browserSync
Expand Down Expand Up @@ -41,8 +42,40 @@ module.exports = function (browserSync, name, pjson) {
item.set("startPath", path);
}
}
fixSnippetOptions(item);
});

return browserSync.init(config, args.cb);
};
};

/**
* Back-compat options for snippetOptions.ignorePaths
*/
function fixSnippetOptions (item) {

var ignorePaths = item.getIn(["snippetOptions", "ignorePaths"]);
var includePaths = item.getIn(["snippetOptions", "whitelist"]);

if (ignorePaths) {
if (_.isString(ignorePaths)) {
ignorePaths = [ignorePaths];
}
ignorePaths = ignorePaths.map(ensureSlash);
item.setIn(["snippetOptions", "blacklist"], Immutable.List(ignorePaths));
}
if (includePaths) {
includePaths = includePaths.map(ensureSlash);
item.setIn(["snippetOptions", "whitelist"], Immutable.List(includePaths));
}
}

/**
* Enforce paths to begin with a forward slash
*/
function ensureSlash (item) {
if (item[0] !== "/") {
return "/" + item;
}
return item;
}
3 changes: 2 additions & 1 deletion lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ module.exports.createProxy = function (options, scripts, bs) {
var snippetOptions = options.get("snippetOptions").toJS();
var foxyServer = foxy(options.getIn(["proxy", "target"]), {
rules: snippetUtils.getRegex(options.get("snippet"), options.get("snippetOptions")),
ignorePaths: snippetOptions.ignorePaths,
whitelist: snippetOptions.whitelist,
blacklist: snippetOptions.blacklist,
middleware: mw,
errHandler: function (err) {
bs.logger.debug("{red:[proxy error]} %s", err.message);
Expand Down
3 changes: 2 additions & 1 deletion lib/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ var utils = {
getSnippetMiddleware: function (snippet, options) {
return lrSnippet({
rules: [utils.getRegex(snippet, options)],
ignorePaths: options.get("ignorePaths")
blacklist: options.get("blacklist").toJS(),
whitelist: options.get("whitelist").toJS()
});
},
/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
"easy-extender": "^2.2.0",
"eazy-logger": "^2.1.1",
"emitter-steward": "^0.0.1",
"foxy": "^8.1.1",
"foxy": "^9.0.0",
"glob-watcher": "^0.0.7",
"immutable": "^3.4.1",
"localtunnel": "^1.3.0",
"lodash": "^3.0.1",
"meow": "^3.0.0",
"opn": "^1.0.0",
"portscanner": "^1.0.0",
"resp-modifier": "^1.0.2",
"resp-modifier": "^2.0.1",
"serve-index": "^1.6.1",
"serve-static": "^1.4.2",
"socket.io": "^1.3.2",
Expand Down
74 changes: 71 additions & 3 deletions test/specs/e2e/e2e.options.snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ describe("E2E snippet ignore paths test (1)", function () {
before(function (done) {
browserSync.reset();
var config = {
server: {
server: {
baseDir: "test/fixtures"
},
open: false,
open: false,
logLevel: "silent",
snippetOptions: {
ignorePaths: "iframe.html"
ignorePaths: "iframe.html" //back-compat
}
};
instance = browserSync(config, done).instance;
Expand All @@ -38,6 +39,73 @@ describe("E2E snippet ignore paths test (1)", function () {
});
});
});
describe("E2E snippet blacklist paths test (1)", function () {

var instance;

before(function (done) {
browserSync.reset();
var config = {
server: {
baseDir: "test/fixtures"
},
open: false,
logLevel: "silent",
snippetOptions: {
blacklist: ["/iframe.html"] // correct syntax
}
};
instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("does not inject the snippet when excluded path hit", function (done) {
request(instance.server)
.get("/iframe.html")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
assert.notInclude(res.text, instance.options.get("snippet"));
done();
});
});
});
describe("E2E snippet blacklist paths test (1)", function () {

var instance;

before(function (done) {
browserSync.reset();
var config = {
server: {
baseDir: "test/fixtures"
},
open: false,
logLevel: "silent",
snippetOptions: {
whitelist: ["/iframe.html"] // correct syntax
}
};
instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("Always injects snippet when path matches in whitelist", function (done) {
request(instance.server)
.get("/iframe.html")
.expect(200)
.end(function (err, res) {
assert.include(res.text, instance.options.get("snippet"));
done();
});
});
});
describe("E2E snippet custom regex", function () {

var instance;
Expand Down
103 changes: 103 additions & 0 deletions test/specs/e2e/proxy/e2e.proxy.snippet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"use strict";

var browserSync = require("../../../../index");

var connect = require("connect");
var serveStatic = require("serve-static");
var request = require("supertest");
var assert = require("chai").assert;

describe("E2E proxy test with snippet options: Whitelist", function () {

var instance, server, options;

before(function (done) {

browserSync.reset();

var app = connect();
app.use(serveStatic("./test/fixtures"));
server = app.listen();
var proxytarget = "http://localhost:" + server.address().port;

var config = {
proxy: proxytarget,
logLevel: "silent",
open: false,
snippetOptions: {
whitelist: ["/index-large.html"]
}
};

instance = browserSync.init([], config, function (err, bs) {
options = bs.options;
done();
}).instance;
});

after(function () {
instance.cleanup();
server.close();
});

it("can init proxy & serve a page with whitelist option (injects snippet even without html headers)", function (done) {

assert.isDefined(instance.server);

request(instance.server)
.get("/index-large.html")
.expect(200)
.end(function (err, res) {
assert.include(res.text, options.get("snippet"));
done();
});
});
});

describe("E2E proxy test with snippet options: blacklist", function () {

var instance, server, options;

before(function (done) {

browserSync.reset();

var app = connect();
app.use(serveStatic("./test/fixtures"));
server = app.listen();
var proxytarget = "http://localhost:" + server.address().port;

var config = {
proxy: proxytarget,
logLevel: "silent",
open: false,
snippetOptions: {
blacklist: ["/index-large.html"]
}
};

instance = browserSync.init([], config, function (err, bs) {
options = bs.options;
done();
}).instance;
});

after(function () {
instance.cleanup();
server.close();
});

it("can init proxy & serve a page with whitelist option", function (done) {

assert.isDefined(instance.server);

request(instance.server)
.get("/index-large.html")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
assert.notInclude(res.text, options.get("snippet"));
done();
});
});
});

0 comments on commit 6a2a296

Please sign in to comment.