snippet.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var connectUtils = require("./connect-utils");
  3. var config = require("./config");
  4. var lrSnippet = require("resp-modifier");
  5. var path = require("path");
  6. var _ = require("./lodash.custom");
  7. var utils = require("./utils");
  8. var fs = require("fs");
  9. var path = require("path");
  10. /**
  11. * Utils for snippet injection
  12. */
  13. var snippetUtils = {
  14. /**
  15. * @param {String} url
  16. * @param {Array} excludeList
  17. * @returns {boolean}
  18. */
  19. isExcluded: function (url, excludeList) {
  20. var extension = path.extname(url);
  21. if (extension) {
  22. if (~url.indexOf("?")) {
  23. return true;
  24. }
  25. extension = extension.slice(1);
  26. return _.includes(excludeList, extension);
  27. }
  28. return false;
  29. },
  30. /**
  31. * @param {String} snippet
  32. * @param {Object} options
  33. * @returns {{match: RegExp, fn: Function}}
  34. */
  35. getRegex: function (snippet, options) {
  36. var fn = options.getIn(["rule", "fn"]);
  37. return {
  38. match: options.getIn(["rule", "match"]),
  39. fn: function (req, res, match) {
  40. return fn.apply(null, [snippet, match]);
  41. },
  42. once: true,
  43. id: "bs-snippet"
  44. };
  45. },
  46. getSnippetMiddleware: function (snippet, options, rewriteRules) {
  47. return lrSnippet.create(snippetUtils.getRules(snippet, options, rewriteRules));
  48. },
  49. getRules: function (snippet, options, rewriteRules) {
  50. var rules = [snippetUtils.getRegex(snippet, options)];
  51. if (rewriteRules) {
  52. rules = rules.concat(rewriteRules);
  53. }
  54. return {
  55. rules: rules,
  56. blacklist: utils.arrayify(options.get("blacklist")),
  57. whitelist: utils.arrayify(options.get("whitelist"))
  58. };
  59. },
  60. /**
  61. * @param {Object} req
  62. * @param {Array} [excludeList]
  63. * @returns {Object}
  64. */
  65. isOldIe: function (excludeList) {
  66. return function (req, res, next) {
  67. var ua = req.headers["user-agent"];
  68. var match = /MSIE (\d)\.\d/.exec(ua);
  69. if (match) {
  70. if (parseInt(match[1], 10) < 9) {
  71. if (!snippetUtils.isExcluded(req.url, excludeList)) {
  72. req.headers["accept"] = "text/html";
  73. }
  74. }
  75. }
  76. next();
  77. };
  78. },
  79. /**
  80. * @param {Number} port
  81. * @param {BrowserSync.options} options
  82. * @returns {String}
  83. */
  84. getClientJs: function (port, options) {
  85. return () => {
  86. const script = options.get("minify") ? "index.js" : "index.js";
  87. const client = fs.readFileSync(require.resolve("browser-sync-client/dist/" + script), "utf8");
  88. return [connectUtils.socketConnector(options), client].join(";\n");
  89. };
  90. }
  91. };
  92. module.exports.utils = snippetUtils;
  93. //# sourceMappingURL=snippet.js.map