@capacitor/cli9.1.03.8 kB
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deliver = exports.receive = exports.send = void 0;
const tslib_1 = require("tslib");
const utils_subprocess_1 = require("@ionic/utils-subprocess");
const debug_1 = tslib_1.__importDefault(require("debug"));
const fs_extra_1 = require("fs-extra");
const https_1 = require("https");
const path_1 = require("path");
const cli_1 = require("./util/cli");
const debug = (0, debug_1.default)('capacitor:ipc');
/**
* How long an awaited delivery may take. Short enough that a developer never
* notices it, long enough for a request from a cold sandbox.
*/
const DELIVERY_TIMEOUT = 3000;
/**
* Send an IPC message to a forked process.
*/
async function send(msg) {
const dir = cli_1.ENV_PATHS.log;
await (0, fs_extra_1.mkdirp)(dir);
const logPath = (0, path_1.resolve)(dir, 'ipc.log');
debug('Sending %O IPC message to forked process (logs: %O)', msg.type, logPath);
const fd = await (0, fs_extra_1.open)(logPath, 'a');
const p = (0, utils_subprocess_1.fork)(process.argv[1], ['📡'], { stdio: ['ignore', fd, fd, 'ipc'] });
p.send(msg);
p.disconnect();
p.unref();
}
exports.send = send;
/**
* Receive and handle an IPC message.
*
* Assume minimal context and keep external dependencies to a minimum.
*/
async function receive(msg) {
debug('Received %O IPC message', msg.type);
if (msg.type === 'telemetry') {
await deliver(msg.data);
}
}
exports.receive = receive;
/**
* POST a metric to the events service.
*
* Never rejects and never writes to stderr. Telemetry failing is not something
* the caller can act on, and it must not change the command's outcome or its
* exit code — so every ending, including the timeout, resolves.
*/
function deliver(data, timeout = DELIVERY_TIMEOUT) {
return new Promise((resolve) => {
const body = JSON.stringify({ metrics: [data], sent_at: new Date().toISOString() });
let settled = false;
const finish = (reason) => {
if (settled) {
return;
}
settled = true;
clearTimeout(deadline);
debug('Delivery of %O metric %s', data.name, reason);
resolve();
};
// An overall deadline rather than `req.setTimeout`, which is a socket
// inactivity timer and only arms once the socket connects — so it never
// fires against an unroutable host, which is exactly the sandbox this
// bound exists for. Unref'd so a pending request can't hold the CLI open.
const deadline = setTimeout(() => {
req.destroy();
finish('timed out');
}, timeout);
deadline.unref();
// This request is only made if telemetry is on.
const req = (0, https_1.request)({
hostname: 'metrics-capacitor.outsystems.com',
port: 443,
path: '/metrics',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
}, (response) => {
debug('Sent %O metric to events service (status: %O)', data.name, response.statusCode);
if (response.statusCode === 202) {
// Draining is what lets the socket close; without it an awaited
// delivery sits until the timeout fires.
response.resume();
}
else {
response.on('data', (chunk) => {
debug('Bad response from events service. Request body: %O', chunk.toString());
});
}
response.on('end', () => finish('completed'));
response.on('error', () => finish('failed reading the response'));
});
req.on('error', (e) => finish(`failed: ${e.message}`));
req.end(body);
});
}
exports.deliver = deliver;
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.