@dynatrace/js-runtime
Version:
This package provides the Dynatrace JavaScript runtime used by the [Dynatrace App Toolkit](https://www.npmjs.com/package/dt-app).
58 lines (57 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.invokeDtRuntime = invokeDtRuntime;
const child_process_1 = require("child_process");
const path_1 = require("path");
const runtime_1 = require("./runtime");
process.once('message', async (message) => {
try {
const result = await execute(message);
process.send?.({ result });
}
catch (error) {
console.error('Could not execute app code:', error.message);
process.send?.({ error });
}
});
/** Execute the given task via the native api-runtime */
async function execute(serializedTask) {
if (typeof serializedTask !== 'object') {
throw new TypeError('Expected execution task serialized as object');
}
if ((0, runtime_1.isCurrentProcessDebuggedByVscode)()) {
try {
await (0, runtime_1.startInspectorServer)(0 /* InspectorType.VsCode */);
}
catch (error) {
console.error("Failed starting inspector server:", error.message);
}
}
return await (0, runtime_1.executeFunctionLocal)(serializedTask);
}
/** Forks a child process and invokes the native napi-runtime for function execution */
async function invokeDtRuntime(task) {
const child = (0, child_process_1.fork)((0, path_1.join)(__dirname, "index.js"), { serialization: "advanced" });
return await new Promise((resolve, reject) => {
child.send(task);
child.on('message', (message) => {
const { error, result } = message;
if (error !== undefined) {
reject(error);
}
else if (result !== undefined) {
resolve(result);
}
});
child.on("error", error => {
reject(error);
});
child.on('close', (code, signal) => {
if (code !== 0) {
// Either `code` or `signal` is set, the other is always null
let reason = code ? `with exit code ${code}` : `due to signal: ${signal}`;
reject(new Error(`DT Runtime exited unexpectedly ${reason}`));
}
});
});
}