http-protocol.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. var 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. var output_1 = [
  53. "Error: could not parse JSON.",
  54. ];
  55. res.writeHead(500, { "Content-Type": "text/plain" });
  56. return res.end(output_1.join("\n"));
  57. }
  58. try {
  59. var _a = JSON.parse(body.toString()), name = _a[0], payload = _a[1];
  60. bs.io.sockets.emit(name, payload);
  61. return res.end("Browsersync HTTP Protocol received: " + name + " " + JSON.stringify(payload));
  62. }
  63. catch (e) {
  64. var output_2 = [
  65. "Error: " + e.message,
  66. ];
  67. res.writeHead(500, { "Content-Type": "text/plain" });
  68. return res.end(output_2.join("\n"));
  69. }
  70. });
  71. }
  72. var params = queryString.parse(req.url.replace(/^.*\?/, ""));
  73. var output;
  74. if (!Object.keys(params).length) {
  75. output = [
  76. "Error: No Parameters were provided.",
  77. "Example: http://localhost:3000/__browser_sync__?method=reload&args=core.css"
  78. ];
  79. res.writeHead(500, { "Content-Type": "text/plain" });
  80. res.end(output.join("\n"));
  81. return;
  82. }
  83. try {
  84. var bsOrEmitter = methodRequiresInstance(params.method)
  85. ? bs
  86. : bs.events;
  87. require("./public/" + params.method)(bsOrEmitter).apply(null, [
  88. params.args
  89. ]);
  90. output = [
  91. "Called public API method `.%s()`".replace("%s", params.method),
  92. "With args: " + JSON.stringify(params.args)
  93. ];
  94. res.end(output.join("\n"));
  95. }
  96. catch (e) {
  97. res.writeHead(404, { "Content-Type": "text/plain" });
  98. res.write("Public API method `" + params.method + "` not found.");
  99. res.end();
  100. return;
  101. }
  102. };
  103. };
  104. //# sourceMappingURL=http-protocol.js.map