missing files

This commit is contained in:
Florian Dold 2017-05-27 22:56:35 +02:00
parent 478a089e52
commit bd17ead09a
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 270 additions and 0 deletions

73
src/crypto/nodeWorker.ts Normal file
View File

@ -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");
}
}

View File

@ -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);
});

41
src/crypto/startWorker.js Normal file
View File

@ -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);
}

77
src/timer.ts Normal file
View File

@ -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));
}