Skip to content

Commit

Permalink
Remove gulp, simplify options with white & blacklists
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Feb 3, 2015
1 parent 929d297 commit 3f778d8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 110 deletions.
File renamed without changes.
11 changes: 0 additions & 11 deletions gulpfile.js

This file was deleted.

200 changes: 118 additions & 82 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"use strict";

var minimatch = require("minimatch");

module.exports = function (opt) {
module.exports = function (opts) {

// options
opt = opt || {};
opts = opts || {};

var defaultIgnoreTypes = [
// text files
"js", "json", "css",
Expand All @@ -19,17 +23,13 @@ module.exports = function (opt) {
// data files
"zip", "rar", "tar", "gz", "xml", "app", "exe", "jar", "dmg", "pkg", "iso"
].map(function(ext) { return "\\." + ext + "(\\?.*)?$"; });
var ignore = opt.ignore || opt.excludeList || defaultIgnoreTypes;
var ignorePaths = opt.ignorePaths || [];
if (typeof ignorePaths === "string") {
ignorePaths = [ignorePaths];
}
var includePaths = opt.includePaths || false;
if (typeof includePaths === "string") {
includePaths = [includePaths];
}
var html = opt.html || _html;
var rules = opt.rules || [];

var ignore = opts.ignore || opts.excludeList || defaultIgnoreTypes;

var blacklist = toArray(opts.blacklist) || [];
var whitelist = toArray(opts.whitelist) || [];

var rules = opts.rules || [];

// helper functions
var regex = (function() {
Expand All @@ -39,7 +39,17 @@ module.exports = function (opt) {
return new RegExp(matches);
})();

function _html(str) {
function toArray(item) {
if (!item) {
return item;
}
if (!Array.isArray(item)) {
return [item];
}
return item;
}

function isHtml(str) {
if (!str) {
return false;
}
Expand All @@ -63,8 +73,7 @@ module.exports = function (opt) {
}
}

function snap(body) {

function overwriteBody(body) {
var _body = body;
rules.forEach(function(rule) {
if (rule.match.test(body)) {
Expand All @@ -78,15 +87,24 @@ module.exports = function (opt) {
return _body;
}

function accept(req) {
/**
* @param req
* @returns {*}
*/
function hasAcceptHeaders(req) {
var ha = req.headers["accept"];
if (!ha) {
return false;
}
return (~ha.indexOf("html"));
}

function check(url) {
/**
* Determine if a response should be overwritten
* @param url
* @returns {boolean}
*/
function shouldNotOverwrite(url) {

if (url.length === 1 && url === "/") {
return false;
Expand All @@ -98,13 +116,6 @@ module.exports = function (opt) {
return true;
}

// first, check the INCLUDES
if (includePaths) {
return ! includePaths.some(function (pattern) {
return minimatch(url, pattern);
});
}

// second, check that the URL does not contain a
// file extension that should be ignored by default
if (ignore.some(function (pattern) {
Expand All @@ -114,7 +125,7 @@ module.exports = function (opt) {
}

// Finally, check any mini-match patterns for paths that have been excluded
if (ignorePaths.some(function (pattern) {
if (blacklist.some(function (pattern) {
return minimatch(url, pattern);
})) {
return true;
Expand All @@ -123,86 +134,111 @@ module.exports = function (opt) {
return false;
}

// middleware
/**
* Check if a URL was white-listed
* @param url
* @returns {boolean}
*/
function isWhitelisted (url) {
return whitelist.some(function (pattern) {
return minimatch(url, pattern);
});
}

/**
* Middleware
*/
return function respModifier(req, res, next) {
if (res._livereload) {

if (res._respModifier) {
return next();
}
res._livereload = true;

res._respModifier = true;

var writeHead = res.writeHead;
var write = res.write;
var end = res.end;

if (!accept(req) || check(req.url)) {
return next();
if (isWhitelisted(req.url)) {
modifyResponse();
} else {
if (!hasAcceptHeaders(req) || shouldNotOverwrite(req.url)) {
return next();
} else {
modifyResponse();
}
}

req.headers["accept-encoding"] = "identity";
next();

function restore() {
res.writeHead = writeHead;
res.write = write;
res.end = end;
}
function modifyResponse() {

res.push = function(chunk) {
res.data = (res.data || "") + chunk;
};

res.inject = res.write = function(string, encoding) {
if (string !== undefined) {
var body = string instanceof Buffer ? string.toString(encoding) : string;
if (html(body) || html(res.data)) {
if (exists(body) && !snip(res.data)) {
var newString = snap(body);
res.push(newString);
req.headers["accept-encoding"] = "identity";

function restore() {
res.writeHead = writeHead;
res.write = write;
res.end = end;
}

res.push = function(chunk) {
res.data = (res.data || "") + chunk;
};

res.inject = res.write = function(string, encoding) {
if (string !== undefined) {
var body = string instanceof Buffer ? string.toString(encoding) : string;
if (isHtml(body) || isHtml(res.data)) {
if (exists(body) && !snip(res.data)) {
var newString = overwriteBody(body);
res.push(newString);
} else {
res.push(body);
}
return true;
} else {
res.push(body);
restore();
return write.call(res, string, encoding);
}
return true;
} else {
restore();
return write.call(res, string, encoding);
}
}
return true;
};

res.writeHead = function() {
var headers = arguments[arguments.length - 1];
if (headers && typeof headers === "object") {
for (var name in headers) {
if (/content-length/i.test(name)) {
delete headers[name];
return true;
};

res.writeHead = function() {
var headers = arguments[arguments.length - 1];
if (headers && typeof headers === "object") {
for (var name in headers) {
if (/content-length/i.test(name)) {
delete headers[name];
}
}
}
}

var header = res.getHeader( "content-length" );
if ( header ) {
res.removeHeader( "content-length" );
}
var header = res.getHeader( "content-length" );
if ( header ) {
res.removeHeader( "content-length" );
}

writeHead.apply(res, arguments);
};
writeHead.apply(res, arguments);
};

res.end = function(string, encoding) {
res.end = function(string, encoding) {

restore();
restore();

var result = res.inject(string, encoding);
var result = res.inject(string, encoding);

if (!result) {
return end.call(res, string, encoding);
}
if (!result) {
return end.call(res, string, encoding);
}

if (res.data !== undefined && !res._header) {
res.setHeader("content-length", Buffer.byteLength(res.data, encoding));
}
if (res.data !== undefined && !res._header) {
res.setHeader("content-length", Buffer.byteLength(res.data, encoding));
}

res.end(res.data, encoding);
};
next();
res.end(res.data, encoding);
};
}
};
};
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "resp-modifier",
"description": "Middleware for modifying a HTML response",
"version": "1.0.2",
"version": "2.0.0",
"author": {
"name": "Shane Osbourne",
"email": "[email protected]"
Expand All @@ -14,26 +14,24 @@
}
],
"files": [
"index.js",
"LICENSE-MIT"
"index.js"
],
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"test": "gulp lint && mocha"
"test": "jshint test/*.js index.js && mocha"
},
"dependencies": {
"minimatch": "^2.0.0"
"minimatch": "^2.0.1"
},
"devDependencies": {
"chai": "^1.10.0",
"express": "^4.10.7",
"gulp": "^3.8.10",
"gulp-jshint": "^1.9.0",
"express": "^4.11.2",
"jshint": "^2.6.0",
"mocha": "^2.1.0",
"multiline": "^1.0.1",
"serve-static": "^1.8.0",
"multiline": "^1.0.2",
"serve-static": "^1.8.1",
"supertest": "^0.15.0"
},
"keywords": []
Expand Down
3 changes: 1 addition & 2 deletions test/app.ignore.paths.js → test/app.blacklist.paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ app.use(livereload({
}
}
],
ignore: [".woff", ".flv"],
ignorePaths: ["templates/*.html"]
blacklist: ["templates/*.html"]
}));

var output = multiline(function () {/*
Expand Down
3 changes: 1 addition & 2 deletions test/app.render.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ if (app.get("env") === "development") {
return "matcher";
}
}
],
ignore: [".woff", ".flv"]
]
}));
}

Expand Down
5 changes: 2 additions & 3 deletions test/app.include.paths.js → test/app.whitelist.paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ app.use(livereload({
}
}
],
ignore: [".woff", ".flv"],
includePaths: ["app/**/*.html"]
whitelist: ["app/**/*.html"],
blacklist: ["**/home.html"]
}));

var output = multiline(function () {/*
Expand Down Expand Up @@ -76,7 +76,6 @@ describe("Only using included paths", function () {
.set("Accept", "text/html")
.expect(200)
.end(function (err, res) {
//console.log(res.text);
assert.notInclude(res.text, "TEST");
done();
});
Expand Down

0 comments on commit 3f778d8

Please sign in to comment.