| 1 |
'use strict'; |
| 2 |
const fs = require('fs'); |
| 3 |
const path = require('path'); |
| 4 |
const crypto = require('crypto'); |
| 5 |
const isStream = require('is-stream'); |
| 6 |
|
| 7 |
const {Worker} = (() => { |
| 8 |
try { |
| 9 |
return require('worker_threads'); |
| 10 |
} catch (_) { |
| 11 |
return {}; |
| 12 |
} |
| 13 |
})(); |
| 14 |
|
| 15 |
let worker; // Lazy |
| 16 |
let taskIdCounter = 0; |
| 17 |
const tasks = new Map(); |
| 18 |
|
| 19 |
const recreateWorkerError = sourceError => { |
| 20 |
const error = new Error(sourceError.message); |
| 21 |
|
| 22 |
for (const [key, value] of Object.entries(sourceError)) { |
| 23 |
if (key !== 'message') { |
| 24 |
error[key] = value; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return error; |
| 29 |
}; |
| 30 |
|
| 31 |
const createWorker = () => { |
| 32 |
worker = new Worker(path.join(__dirname, 'thread.js')); |
| 33 |
|
| 34 |
worker.on('message', message => { |
| 35 |
const task = tasks.get(message.id); |
| 36 |
tasks.delete(message.id); |
| 37 |
|
| 38 |
if (tasks.size === 0) { |
| 39 |
worker.unref(); |
| 40 |
} |
| 41 |
|
| 42 |
if (message.error === undefined) { |
| 43 |
task.resolve(message.value); |
| 44 |
} else { |
| 45 |
task.reject(recreateWorkerError(message.error)); |
| 46 |
} |
| 47 |
}); |
| 48 |
|
| 49 |
worker.on('error', error => { |
| 50 |
// Any error here is effectively an equivalent of segfault, and have no scope, so we just throw it on callback level |
| 51 |
throw error; |
| 52 |
}); |
| 53 |
}; |
| 54 |
|
| 55 |
const taskWorker = (method, args, transferList) => new Promise((resolve, reject) => { |
| 56 |
const id = taskIdCounter++; |
| 57 |
tasks.set(id, {resolve, reject}); |
| 58 |
|
| 59 |
if (worker === undefined) { |
| 60 |
createWorker(); |
| 61 |
} |
| 62 |
|
| 63 |
worker.ref(); |
| 64 |
worker.postMessage({id, method, args}, transferList); |
| 65 |
}); |
| 66 |
|
| 67 |
const hasha = (input, options = {}) => { |
| 68 |
let outputEncoding = options.encoding || 'hex'; |
| 69 |
|
| 70 |
if (outputEncoding === 'buffer') { |
| 71 |
outputEncoding = undefined; |
| 72 |
} |
| 73 |
|
| 74 |
const hash = crypto.createHash(options.algorithm || 'sha512'); |
| 75 |
|
| 76 |
const update = buffer => { |
| 77 |
const inputEncoding = typeof buffer === 'string' ? 'utf8' : undefined; |
| 78 |
hash.update(buffer, inputEncoding); |
| 79 |
}; |
| 80 |
|
| 81 |
if (Array.isArray(input)) { |
| 82 |
input.forEach(update); |
| 83 |
} else { |
| 84 |
update(input); |
| 85 |
} |
| 86 |
|
| 87 |
return hash.digest(outputEncoding); |
| 88 |
}; |
| 89 |
|
| 90 |
hasha.stream = (options = {}) => { |
| 91 |
let outputEncoding = options.encoding || 'hex'; |
| 92 |
|
| 93 |
if (outputEncoding === 'buffer') { |
| 94 |
outputEncoding = undefined; |
| 95 |
} |
| 96 |
|
| 97 |
const stream = crypto.createHash(options.algorithm || 'sha512'); |
| 98 |
stream.setEncoding(outputEncoding); |
| 99 |
return stream; |
| 100 |
}; |
| 101 |
|
| 102 |
hasha.fromStream = async (stream, options = {}) => { |
| 103 |
if (!isStream(stream)) { |
| 104 |
throw new TypeError('Expected a stream'); |
| 105 |
} |
| 106 |
|
| 107 |
return new Promise((resolve, reject) => { |
| 108 |
// TODO: Use `stream.pipeline` and `stream.finished` when targeting Node.js 10 |
| 109 |
stream |
| 110 |
.on('error', reject) |
| 111 |
.pipe(hasha.stream(options)) |
| 112 |
.on('error', reject) |
| 113 |
.on('finish', function () { |
| 114 |
resolve(this.read()); |
| 115 |
}); |
| 116 |
}); |
| 117 |
}; |
| 118 |
|
| 119 |
if (Worker === undefined) { |
| 120 |
hasha.fromFile = async (filePath, options) => hasha.fromStream(fs.createReadStream(filePath), options); |
| 121 |
hasha.async = async (input, options) => hasha(input, options); |
| 122 |
} else { |
| 123 |
hasha.fromFile = async (filePath, {algorithm = 'sha512', encoding = 'hex'} = {}) => { |
| 124 |
const hash = await taskWorker('hashFile', [algorithm, filePath]); |
| 125 |
|
| 126 |
if (encoding === 'buffer') { |
| 127 |
return Buffer.from(hash); |
| 128 |
} |
| 129 |
|
| 130 |
return Buffer.from(hash).toString(encoding); |
| 131 |
}; |
| 132 |
|
| 133 |
hasha.async = async (input, {algorithm = 'sha512', encoding = 'hex'} = {}) => { |
| 134 |
if (encoding === 'buffer') { |
| 135 |
encoding = undefined; |
| 136 |
} |
| 137 |
|
| 138 |
const hash = await taskWorker('hash', [algorithm, input]); |
| 139 |
|
| 140 |
if (encoding === undefined) { |
| 141 |
return Buffer.from(hash); |
| 142 |
} |
| 143 |
|
| 144 |
return Buffer.from(hash).toString(encoding); |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
hasha.fromFileSync = (filePath, options) => hasha(fs.readFileSync(filePath), options); |
| 149 |
|
| 150 |
module.exports = hasha; |