wallet-core/src/crypto/nodeWorkerEntry.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-05-27 22:56:35 +02:00
/*
This file is part of TALER
(C) 2016 GNUnet e.V.
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2017-05-28 01:10:54 +02:00
// tslint:disable:no-var-requires
2017-05-27 22:56:35 +02:00
const fs = require("fs");
const vm = require("vm");
process.once("message", (obj: any) => {
const g: any = global as any;
(g as any).self = {
2017-05-28 01:10:54 +02:00
addEventListener: (event: "error" | "message", fn: (x: any) => void) => {
if (event === "error") {
g.onerror = fn;
} else if (event === "message") {
g.onmessage = fn;
}
},
2017-05-27 22:56:35 +02:00
close: () => {
process.exit(0);
},
2017-05-28 01:10:54 +02:00
onerror: (err: any) => {
const str: string = JSON.stringify({error: err.message, stack: err.stack});
2017-05-27 22:56:35 +02:00
if (process.send) {
process.send(str);
}
},
onmessage: undefined,
2017-05-28 01:10:54 +02:00
postMessage: (msg: any) => {
const str: string = JSON.stringify({data: msg});
2017-05-27 22:56:35 +02:00
if (process.send) {
process.send(str);
}
},
};
g.__dirname = obj.cwd;
g.__filename = __filename;
g.importScripts = (...files: string[]) => {
if (files.length > 0) {
2017-05-28 01:10:54 +02:00
vm.createScript(files.map((file) => fs.readFileSync(file, "utf8")).join("\n")).runInThisContext();
2017-05-27 22:56:35 +02:00
}
};
2017-05-28 01:10:54 +02:00
Object.keys(g.self).forEach((key) => {
2017-05-27 22:56:35 +02:00
g[key] = g.self[key];
});
process.on("message", (msg: any) => {
try {
2017-05-28 01:10:54 +02:00
(g.onmessage || g.self.onmessage || (() => undefined))(JSON.parse(msg));
2017-05-27 22:56:35 +02:00
} catch (err) {
2017-05-28 01:10:54 +02:00
(g.onerror || g.self.onerror || (() => undefined))(err);
2017-05-27 22:56:35 +02:00
}
});
2017-08-14 04:59:43 +02:00
process.on("uncaughtException", (err: any) => {
2017-05-28 01:10:54 +02:00
(g.onerror || g.self.onerror || (() => undefined))(err);
2017-05-27 22:56:35 +02:00
});
require(obj.scriptFilename);
});