1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
"use strict";
var fs = require("fs"),
path = require("path"),
vm = require("vm"),
noop = require(path.join(__dirname, "noop.js")),
events = /^(error|message)$/;
function trim(arg) {
return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
}
function explode(arg) {
return trim(arg).split(new RegExp("\\s*,\\s*"));
}
function toFunction(arg) {
var args = trim(arg.replace(/^.*\(/, "").replace(/[\t|\r|\n|\"|\']+/g, "").replace(/\).*/, "")),
body = trim(arg.replace(/^.*\{/, "").replace(/\}$/, ""));
return Function.apply(Function, explode(args).concat([body]));
}
// Bootstraps the Worker
process.once("message", function (obj) {
var exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8");
global.self = {
close: function close() {
process.exit(0);
},
postMessage: function postMessage(msg) {
process.send(JSON.stringify({ data: msg }, null, 0));
},
onmessage: void 0,
onerror: function onerror(err) {
process.send(JSON.stringify({ error: err.message, stack: err.stack }, null, 0));
},
addEventListener: function addEventListener(event, fn) {
if (events.test(event)) {
global["on" + event] = global.self["on" + event] = fn;
}
}
};
global.__dirname = obj.cwd;
global.__filename = __filename;
global.require = require;
global.importScripts = function () {
for (var _len = arguments.length, files = Array(_len), _key = 0; _key < _len; _key++) {
files[_key] = arguments[_key];
}
if (files.length > 0) {
vm.createScript(files.map(function (file) {
return fs.readFileSync(file, "utf8");
}).join("\n")).runInThisContext();
}
};
Object.keys(global.self).forEach(function (key) {
global[key] = global.self[key];
});
process.on("message", function (msg) {
try {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg));
} catch (err) {
(global.onerror || global.self.onerror || noop)(err);
}
});
process.on("error", function (err) {
(global.onerror || global.self.onerror || noop)(err);
});
if (typeof exp === "function") {
exp();
} else {
vm.createScript(exp).runInThisContext();
}
});
|