@stormking/emere
Version:
a nightmarejs replacement for scraping websites using electron.
49 lines (48 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = run;
const node_child_process_1 = require("node:child_process");
const message_1 = require("./message");
const defaultElectron = require('electron');
async function run(runScript) {
// console.log('run emere', runScript);
const proc = (0, node_child_process_1.spawn)(defaultElectron, [__dirname + '/runner.js'], {
// cwd: 'node_modules/electron/',
stdio: [null, 'inherit', 'inherit', 'ipc'],
// env: process.env
});
const cleanup = () => {
if (!proc.killed) {
proc.kill('SIGTERM');
}
};
let resolve, reject;
let p = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
const host = (0, message_1.init)(proc);
host.on('request', (msg, cb) => {
if (msg.action === 'getCommands') {
cb(null, runScript);
}
else if (msg.action === 'returnData') {
if (msg.params.data) {
resolve(Buffer.from(msg.params.data));
}
else {
reject(new Error(msg.params.error || 'unknown error'));
}
cb(null, true);
cleanup();
}
});
proc.on('close', () => {
reject(new Error('electron closed without response'));
});
const timeout = runScript.timeout || 10000;
setTimeout(() => {
cleanup();
}, timeout);
return p;
}