Created
August 11, 2019 18:59
-
-
Save caesaneer/2b8a8b2ea2448ab00e1e82ab9a15b327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ════════════════════════════════════════════════════════════════════════════════════════════════╡ | |
const { parentPort, workerData } = require('worker_threads') | |
const crypto = require('crypto') | |
const util = require('util') | |
const fs = require('fs') | |
// ════════════════════════════════════════════════════════════════════════════════════════════════╡ | |
const asyncFs = util.promisify(fs.readFile) | |
parentPort.on('message', async (times) => { | |
if (typeof times !== 'number') { | |
throw new Error('times must be a number.') | |
} | |
const result = await asyncFs('./data/test.dat', 'utf8') | |
let resStr = '' | |
for (let i = 0; i < times; i += 1) { | |
const x = crypto.createHash('sha256') | |
x.update(result, 'utf8') | |
resStr = x.digest('hex') | |
} | |
// Access the workerData. | |
// console.log('workerData is', workerData) | |
// return the result to main thread. | |
parentPort.postMessage(resStr) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment