lazy-compilation-node.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* global __resourceQuery */
  2. "use strict";
  3. var urlBase = decodeURIComponent(__resourceQuery.slice(1));
  4. /**
  5. * @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options
  6. * @returns {() => void} function to destroy response
  7. */
  8. exports.keepAlive = function (options) {
  9. var data = options.data;
  10. var onError = options.onError;
  11. var active = options.active;
  12. var module = options.module;
  13. /** @type {import("http").IncomingMessage} */
  14. var response;
  15. var request = (
  16. urlBase.startsWith("https") ? require("https") : require("http")
  17. ).request(
  18. urlBase + data,
  19. {
  20. agent: false,
  21. headers: { accept: "text/event-stream" }
  22. },
  23. function (res) {
  24. response = res;
  25. response.on("error", errorHandler);
  26. if (!active && !module.hot) {
  27. console.log(
  28. "Hot Module Replacement is not enabled. Waiting for process restart..."
  29. );
  30. }
  31. }
  32. );
  33. /**
  34. * @param {Error} err error
  35. */
  36. function errorHandler(err) {
  37. err.message =
  38. "Problem communicating active modules to the server: " + err.message;
  39. onError(err);
  40. }
  41. request.on("error", errorHandler);
  42. request.end();
  43. return function () {
  44. response.destroy();
  45. };
  46. };