-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (88 loc) · 2.47 KB
/
index.js
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
84
85
86
87
88
89
const cp = require('child_process');
module.exports = ({ limit = 0.25 } = {}) => {
let worker = null;
let working = false;
const queue = [];
process.on('exit', cleanup);
return {
match(regExp, string) {
let flags;
regExp = regExp.toString();
// A regexp literal was typically passed and when it went through toString,
// it became /regexp-goes-here/flags-go-here. Parse that into a form we
// can feed to the RegExp constructor in the other process
const matches = regExp.match(/^\/(.*?)\/([a-z]*)$/);
if (matches) {
regExp = matches[1];
flags = matches[2];
}
return new Promise((resolve, reject) => {
if (!worker) {
worker = createWorker();
}
queue.push({
regExp,
flags,
string,
resolve,
reject
});
if (!working) {
matchOneViaWorker();
}
function createWorker() {
const worker = cp.fork(`${__dirname}/worker.js`, {
stdio: 'ignore'
});
// So the parent process can exit due to a lack of work to do,
// without explicitly closing the child
worker.unref();
worker.channel.unref();
return worker;
}
function matchOneViaWorker() {
let settled = false;
if (!queue.length) {
return;
}
working = true;
const {
regExp, string, resolve, reject
} = queue.shift();
worker.once('message', receive);
const timeout = setTimeout(function() {
if (!settled) {
worker.kill();
worker = createWorker();
const error = new Error(`A user-supplied regular expression took more than ${limit} seconds to evaluate.`);
error.name = 'timeout';
reject(error);
settled = true;
working = false;
matchOneViaWorker();
}
}, limit * 1000);
worker.send({
regExp,
flags,
string
});
function receive(message) {
clearTimeout(timeout);
if (!settled) {
settled = true;
working = false;
resolve(message.result);
matchOneViaWorker();
}
}
}
});
}
};
function cleanup() {
if (worker) {
worker.kill();
}
}
};