http-protocol.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var queryString = require("qs");
  3. var proto = exports;
  4. var instanceMethods = ["exit", "notify", "pause", "resume"];
  5. var getBody = require("raw-body");
  6. const permittedSocketEvents = [
  7. "file:reload",
  8. "browser:reload",
  9. "browser:notify",
  10. "browser:location",
  11. "options:set"
  12. ];
  13. /**
  14. * Does the requested method expect an instance of BrowserSync
  15. * or raw access to the emitter?
  16. * @param method
  17. * @returns {boolean}
  18. */
  19. function methodRequiresInstance(method) {
  20. return instanceMethods.indexOf(method) > -1;
  21. }
  22. /**
  23. * Use BrowserSync options + querystring to create a
  24. * full HTTP/HTTTPS url.
  25. *
  26. * Eg. http://localhost:3000/__browser_sync__?method=reload
  27. * Eg. http://localhost:3000/__browser_sync__?method=reload&args=core.css
  28. * Eg. http://localhost:3000/__browser_sync__?method=reload&args=core.css&args=core.min
  29. *
  30. * @param args
  31. * @param url
  32. * @returns {string}
  33. */
  34. proto.getUrl = function (args, url) {
  35. return [
  36. url,
  37. require("./config").httpProtocol.path,
  38. "?",
  39. queryString.stringify(args)
  40. ].join("");
  41. };
  42. /**
  43. * Return a middleware for handling the requests
  44. * @param {BrowserSync} bs
  45. * @returns {Function}
  46. */
  47. proto.middleware = function (bs) {
  48. return function (req, res) {
  49. if (req.method === "POST") {
  50. return getBody(req, function (err, body) {
  51. if (err) {
  52. const output = ["Error: could not parse JSON."];
  53. res.writeHead(500, { "Content-Type": "text/plain" });
  54. return res.end(output.join("\n"));
  55. }
  56. try {
  57. const [name, payload] = JSON.parse(body.toString());
  58. bs.io.sockets.emit(name, payload);
  59. return res.end(`Browsersync HTTP Protocol received: ${name} ${JSON.stringify(payload)}`);
  60. }
  61. catch (e) {
  62. const output = [`Error: ${e.message}`];
  63. res.writeHead(500, { "Content-Type": "text/plain" });
  64. return res.end(output.join("\n"));
  65. }
  66. });
  67. }
  68. var params = queryString.parse(req.url.replace(/^.*\?/, ""));
  69. var output;
  70. if (!Object.keys(params).length) {
  71. output = [
  72. "Error: No Parameters were provided.",
  73. "Example: http://localhost:3000/__browser_sync__?method=reload&args=core.css"
  74. ];
  75. res.writeHead(500, { "Content-Type": "text/plain" });
  76. res.end(output.join("\n"));
  77. return;
  78. }
  79. try {
  80. var bsOrEmitter = methodRequiresInstance(params.method)
  81. ? bs
  82. : bs.events;
  83. require("./public/" + params.method)(bsOrEmitter).apply(null, [
  84. params.args
  85. ]);
  86. output = [
  87. "Called public API method `.%s()`".replace("%s", params.method),
  88. "With args: " + JSON.stringify(params.args)
  89. ];
  90. res.end(output.join("\n"));
  91. }
  92. catch (e) {
  93. res.writeHead(404, { "Content-Type": "text/plain" });
  94. res.write("Public API method `" + params.method + "` not found.");
  95. res.end();
  96. return;
  97. }
  98. };
  99. };
  100. //# sourceMappingURL=http-protocol.js.map