utils.js 3.9 KB

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