-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_crypto.js
43 lines (39 loc) · 1.03 KB
/
thread_crypto.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
const {
Worker,
isMainThread,
workerData,
parentPort,
} = require("worker_threads");
const crypto = require("crypto");
if (isMainThread) {
function runService() {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, { workerData: "world" });
worker.on("message", resolve);
worker.on("error", reject);
worker.on("exit", (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
}
async function run() {
const result = await runService("world");
console.log(result);
}
run().catch((err) => console.error(err));
console.log("I should run immediately");
} else {
const codes = [];
for (let i = 0; i < 5000; i++) {
const val = doHeavyStuff(`${workerData}-${i + 1}`);
codes.push(val);
}
function doHeavyStuff(item) {
return crypto
.createHmac("sha256", "secret")
.update(new Array(10000).fill(item).join("."))
.digest("hex");
}
parentPort.postMessage(codes);
}