utils.js
3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var minimatch = require("minimatch");
var utils = exports;
utils.applyRules = function overwriteBody (rules, body, req, res) {
return rules.reduce(function (body, rule) {
/**
* Try to use the replace string/fn first
*/
if (rule.replace || typeof rule.replace === "string") {
rule.fn = rule.replace;
}
if (typeof rule.fn === 'string') {
return body.replace(rule.match, rule.fn);
}
return body.replace(rule.match, function () {
var args = Array.prototype.slice.call(arguments);
if (typeof rule.fn === 'function') {
return rule.fn.apply(this, [req, res].concat(args))
}
return rule.fn;
});
}, body);
};
/**
* Extensions that will be ignored by default
* @type {Array}
*/
utils.defaultIgnoreTypes = [
// text files
"js", "json", "css",
// image files
"png", "jpg", "jpeg", "gif", "ico", "tif", "tiff", "bmp", "webp", "psd",
// vector & font
"svg", "woff", "ttf", "otf", "eot", "eps", "ps", "ai",
// audio
"mp3", "wav", "aac", "m4a", "m3u", "mid", "wma",
// video & other media
"mpg", "mpeg", "mp4", "m4v", "webm", "swf", "flv", "avi", "mov", "wmv",
// document files
"pdf", "doc", "docx", "xls", "xlsx", "pps", "ppt", "pptx", "odt", "ods", "odp", "pages", "key", "rtf", "txt", "csv",
// data files
"zip", "rar", "tar", "gz", "xml", "app", "exe", "jar", "dmg", "pkg", "iso"
].map(function (ext) {
return "\\." + ext + "(\\?.*)?$";
});
/**
* Check if a URL was white-listed
* @param url
* @param whitelist
* @returns {boolean}
*/
utils.isWhitelisted = function isWhitelisted(url, whitelist) {
if (whitelist.indexOf(url) > -1) {
return true;
}
return whitelist.some(function (pattern) {
return minimatch(url, pattern);
});
};
/**
* Check if a URL was white-listed with single path
* @param url
* @param rules
* @returns {Array}
*/
utils.isWhiteListedForSingle = function isWhiteListedForSingle(url, rules) {
return rules.filter(function (item) {
return item.paths && utils.isWhitelisted(url, utils.toArray(item.paths));
});
};
/**
* Determine if a response should be overwritten
* @param {String} url
* @param {Object} opts
* @returns {boolean}
*/
utils.inBlackList = function inBlackList(url, opts) {
// First check for an exact match
if (!url || opts.blacklist.indexOf(url) > -1) {
return true;
}
if (url.length === 1 && url === "/") {
return false;
}
// Check the path only
var split = url.split('?')[0];
// second, check that the URL does not contain a
// file extension that should be ignored by default
if (opts.ignore.some(function (pattern) {
return new RegExp(pattern).test(split);
})) {
return true;
}
// Finally, check any mini-match patterns for paths that have been excluded
if (opts.blacklist.some(function (pattern) {
return minimatch(url, pattern);
})) {
return true;
}
return false;
};
/**
* @param req
* @returns {Boolean}
*/
utils.hasAcceptHeaders = function hasAcceptHeaders(req) {
var acceptHeader = req.headers["accept"];
if (!acceptHeader) {
return false;
}
return acceptHeader.indexOf("html") > -1;
};
/**
* @param body
* @returns {boolean}
*/
utils.snip = function snip(body) {
if (!body) {
return false;
}
};
utils.toArray = function toArray(item) {
if (!item) {
return item;
}
if (!Array.isArray(item)) {
return [item];
}
return item;
};
utils.isHtml = function isHtml(str) {
if (!str) {
return false;
}
// Test to see if start of file contents matches:
// - Optional byte-order mark (BOM)
// - Zero or more spaces
// - Any sort of HTML tag, comment, or doctype tag (basically, <...>)
return /^(\uFEFF|\uFFFE)?\s*<[^>]+>/i.test(str);
};