aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-27 22:56:35 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-27 22:56:35 +0200
commitbd17ead09a1baad43e8947d729f43257b4fb6d08 (patch)
tree6163df4124db907cf1e62be4e1ffc458edd85d8a
parent478a089e521de81ca47fc54518bc6a241823d9ae (diff)
missing files
-rw-r--r--src/crypto/nodeWorker.ts73
-rw-r--r--src/crypto/nodeWorkerEntry.ts79
-rw-r--r--src/crypto/startWorker.js41
-rw-r--r--src/timer.ts77
4 files changed, 270 insertions, 0 deletions
diff --git a/src/crypto/nodeWorker.ts b/src/crypto/nodeWorker.ts
new file mode 100644
index 000000000..afa2930a8
--- /dev/null
+++ b/src/crypto/nodeWorker.ts
@@ -0,0 +1,73 @@
+/*
+ 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/>
+ */
+
+const path = require("path");
+const fork = require("child_process").fork;
+
+const nodeWorkerEntry = path.join(__dirname, "nodeWorkerEntry.js");
+
+export class Worker {
+ child: any;
+ onmessage: undefined | ((m: any) => void);
+ onerror: undefined | ((m: any) => void);
+ constructor(scriptFilename: string) {
+ this.child = fork(nodeWorkerEntry);
+ this.onerror = undefined;
+ this.onmessage = undefined;
+
+ this.child.on("error", (e: any) => {
+ if (this.onerror) {
+ this.onerror(e);
+ }
+ });
+
+ this.child.on("message", (msg: any) => {
+ const message = JSON.parse(msg);
+
+ if (!message.error && this.onmessage) {
+ this.onmessage(message);
+ }
+
+ if (message.error && this.onerror) {
+ const error = new Error(message.error);
+ error.stack = message.stack;
+
+ this.onerror(error);
+ }
+ });
+
+ this.child.send({scriptFilename,cwd: process.cwd()});
+ }
+
+ addEventListener(event: "message" | "error", fn: (x: any) => void): void {
+ switch (event) {
+ case "message":
+ this.onmessage = fn;
+ break;
+ case "error":
+ this.onerror = fn;
+ break;
+ }
+ }
+
+ postMessage (msg: any) {
+ this.child.send(JSON.stringify({data: msg}));
+ }
+
+ terminate () {
+ this.child.kill("SIGINT");
+ }
+}
diff --git a/src/crypto/nodeWorkerEntry.ts b/src/crypto/nodeWorkerEntry.ts
new file mode 100644
index 000000000..aa6f57599
--- /dev/null
+++ b/src/crypto/nodeWorkerEntry.ts
@@ -0,0 +1,79 @@
+/*
+ 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/>
+ */
+
+
+const fs = require("fs");
+const vm = require("vm");
+
+process.once("message", (obj: any) => {
+ const g: any = global as any;
+
+ (g as any).self = {
+ close: () => {
+ process.exit(0);
+ },
+ postMessage: (msg: any) => {
+ const str: string = JSON.stringify({data: msg});
+ if (process.send) {
+ process.send(str);
+ }
+ },
+ onmessage: undefined,
+ onerror: (err: any) => {
+ const str: string = JSON.stringify({error: err.message, stack: err.stack});
+ if (process.send) {
+ process.send(str);
+ }
+ },
+ addEventListener: (event: "error" | "message", fn: (x: any) => void) => {
+ if (event == "error") {
+ g.onerror = fn;
+ } else if (event == "message") {
+ g.onmessage = fn;
+ }
+ },
+ };
+
+ g.__dirname = obj.cwd;
+ g.__filename = __filename;
+ //g.require = require;
+ //g.module = module;
+ //g.exports = module.exports;
+
+ g.importScripts = (...files: string[]) => {
+ if (files.length > 0) {
+ vm.createScript(files.map(file => fs.readFileSync(file, "utf8")).join("\n")).runInThisContext();
+ }
+ };
+
+ Object.keys(g.self).forEach(key => {
+ g[key] = g.self[key];
+ });
+
+ process.on("message", (msg: any) => {
+ try {
+ (g.onmessage || g.self.onmessage || (() => {}))(JSON.parse(msg));
+ } catch (err) {
+ (g.onerror || g.self.onerror || (() => {}))(err);
+ }
+ });
+
+ process.on("error", (err: any) => {
+ (g.onerror || g.self.onerror || (() => {}))(err);
+ });
+
+ require(obj.scriptFilename);
+});
diff --git a/src/crypto/startWorker.js b/src/crypto/startWorker.js
new file mode 100644
index 000000000..a64152c64
--- /dev/null
+++ b/src/crypto/startWorker.js
@@ -0,0 +1,41 @@
+/*
+ This file is part of TALER
+ (C) 2017 Inria and 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/>
+ */
+
+
+// @ts-nocheck
+
+
+/**
+ * Start a crypto worker, using different worker
+ * mechanisms depending on the environment.
+ *
+ * @returns {Worker}
+ */
+export function startWorker() {
+ let workerCtor;
+ let workerPath;
+ if (typeof Worker !== "undefined") {
+ // we're in the browser
+ workerCtor = Worker;
+ workerPath = "/dist/cryptoWorker-bundle.js";
+ } else if (typeof "require" !== "undefined") {
+ workerCtor = require("./nodeWorker").Worker;
+ workerPath = __dirname + "/cryptoWorker.js";
+ } else {
+ throw Error("Can't create worker, unknown environment");
+ }
+ return new workerCtor(workerPath);
+}
diff --git a/src/timer.ts b/src/timer.ts
new file mode 100644
index 000000000..105fc8c7a
--- /dev/null
+++ b/src/timer.ts
@@ -0,0 +1,77 @@
+/*
+ This file is part of TALER
+ (C) 2017 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/>
+ */
+
+/**
+ * Cross-platform timers.
+ *
+ * NodeJS and the browser use slightly different timer API,
+ * this abstracts over these differences.
+ */
+
+/**
+ * Cancelable timer.
+ */
+export interface TimerHandle {
+ clear(): void;
+}
+
+class IntervalHandle {
+ constructor(public h: any) {
+ }
+
+ clear() {
+ clearTimeout(this.h);
+ }
+}
+
+class TimeoutHandle {
+ constructor(public h: any) {
+ }
+
+ clear() {
+ clearTimeout(this.h);
+ }
+}
+
+/**
+ * Get a performance counter in milliseconds.
+ */
+export let performanceNow = (() => {
+ if (typeof "process" !== "undefined") {
+ return () => {
+ const t = process.hrtime();
+ return t[0] * 1e9 + t[1];
+ }
+ } else if (typeof "performance" !== "undefined") {
+ return () => performance.now();
+ } else {
+ return () => 0;
+ }
+})();
+
+/**
+ * Call a function every time the delay given in milliseconds passes.
+ */
+export function every(delayMs: number, callback: () => void): TimerHandle {
+ return new IntervalHandle(setInterval(callback, delayMs));
+}
+
+/**
+ * Call a function after the delay given in milliseconds passes.
+ */
+export function after(delayMs: number, callback: () => void): TimerHandle {
+ return new TimeoutHandle(setInterval(callback, delayMs));
+}