ipc.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.receive = exports.send = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_fs_1 = require("@ionic/utils-fs");
  6. const utils_subprocess_1 = require("@ionic/utils-subprocess");
  7. const debug_1 = tslib_1.__importDefault(require("debug"));
  8. const https_1 = require("https");
  9. const path_1 = require("path");
  10. const cli_1 = require("./util/cli");
  11. const debug = (0, debug_1.default)('capacitor:ipc');
  12. /**
  13. * Send an IPC message to a forked process.
  14. */
  15. async function send(msg) {
  16. const dir = cli_1.ENV_PATHS.log;
  17. await (0, utils_fs_1.mkdirp)(dir);
  18. const logPath = (0, path_1.resolve)(dir, 'ipc.log');
  19. debug('Sending %O IPC message to forked process (logs: %O)', msg.type, logPath);
  20. const fd = await (0, utils_fs_1.open)(logPath, 'a');
  21. const p = (0, utils_subprocess_1.fork)(process.argv[1], ['📡'], { stdio: ['ignore', fd, fd, 'ipc'] });
  22. p.send(msg);
  23. p.disconnect();
  24. p.unref();
  25. }
  26. exports.send = send;
  27. /**
  28. * Receive and handle an IPC message.
  29. *
  30. * Assume minimal context and keep external dependencies to a minimum.
  31. */
  32. async function receive(msg) {
  33. debug('Received %O IPC message', msg.type);
  34. if (msg.type === 'telemetry') {
  35. const now = new Date().toISOString();
  36. const { data } = msg;
  37. // This request is only made if telemetry is on.
  38. const req = (0, https_1.request)({
  39. hostname: 'api.ionicjs.com',
  40. port: 443,
  41. path: '/events/metrics',
  42. method: 'POST',
  43. headers: {
  44. 'Content-Type': 'application/json',
  45. },
  46. }, response => {
  47. debug('Sent %O metric to events service (status: %O)', data.name, response.statusCode);
  48. if (response.statusCode !== 204) {
  49. response.on('data', chunk => {
  50. debug('Bad response from events service. Request body: %O', chunk.toString());
  51. });
  52. }
  53. });
  54. const body = {
  55. metrics: [data],
  56. sent_at: now,
  57. };
  58. req.end(JSON.stringify(body));
  59. }
  60. }
  61. exports.receive = receive;