Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 22, 2019
1 parent 7e6a0fd commit fdccdba
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ const recreateWorkerError = sourceError => {

const createWorker = () => {
worker = new Worker(path.join(__dirname, 'thread.js'));

worker.on('message', message => {
const task = tasks.get(message.id);
tasks.delete(message.id);

if (tasks.size === 0) {
worker.unref();
}
Expand All @@ -45,6 +47,7 @@ const createWorker = () => {
task.reject(recreateWorkerError(message.error));
}
});

worker.on('error', error => {
// Any error here is effectively an equivalent of segfault, and have no scope, so we just throw it on callback level
throw error;
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
"easy"
],
"dependencies": {
"is-stream": "^1.1.0",
"type-fest": "^0.3.0"
"is-stream": "^2.0.0",
"type-fest": "^0.8.0"
},
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"@types/node": "^12.7.5",
"ava": "^2.4.0",
"proxyquire": "^2.1.0",
"tsd": "^0.7.2",
"tsd": "^0.8.0",
"xo": "^0.24.0"
},
"xo": {
Expand Down
25 changes: 10 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,45 @@ Pass an array instead of concatenating strings and/or buffers. The output is the

#### options

Type: `Object`
Type: `object`

##### encoding

Type: `string`<br>
Default: `hex`<br>
Values: `hex` `base64` `buffer` `latin1`
Default: `'hex'`<br>
Values: `'hex'` `'base64'` `'buffer'` `'latin1'`

Encoding of the returned hash.

##### algorithm

Type: `string`<br>
Default: `sha512`<br>
Values: `md5` `sha1` `sha256` `sha512` *([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options))*
Default: `'sha512'`<br>
Values: `'md5'` `'sha1'` `'sha256'` `'sha512'` *([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options))*

*The `md5` algorithm is good for [file revving](https://github.com/sindresorhus/rev-hash), but you should never use `md5` or `sha1` for anything sensitive. [They're insecure.](http://googleonlinesecurity.blogspot.no/2014/09/gradually-sunsetting-sha-1.html)*

### hasha.async(input, [options])
### hasha.async(input, options?)

In Node.js 12 or later, the operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive.

Returns a hash asynchronously.

### hasha.stream([options])
### hasha.stream(options?)

Returns a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash).

### hasha.fromStream(stream, [options])
### hasha.fromStream(stream, options?)

Returns a `Promise` for the calculated hash.

### hasha.fromFile(filepath, [options])
### hasha.fromFile(filepath, options?)

In Node.js 12 or later, the operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive.

Returns a `Promise` for the calculated file hash.

### hasha.fromFileSync(filepath, [options])
### hasha.fromFileSync(filepath, options?)

Returns the calculated file hash.

Expand All @@ -131,8 +131,3 @@ Returns the calculated file hash.
- [crypto-hash](https://github.com/sindresorhus/crypto-hash) - Tiny hashing module that uses the native crypto API in Node.js and the browser
- [hash-obj](https://github.com/sindresorhus/hash-obj) - Get the hash of an object
- [md5-hex](https://github.com/sindresorhus/md5-hex) - Create a MD5 hash with hex encoding


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
1 change: 1 addition & 0 deletions thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const handlers = {
hashFile: (algorithm, filePath) => new Promise((resolve, reject) => {
const hasher = crypto.createHash(algorithm);
fs.createReadStream(filePath)
// TODO: Use `Stream.pipeline` when targeting Node.js 12.
.on('error', reject)
.pipe(hasher)
.on('error', reject)
Expand Down

0 comments on commit fdccdba

Please sign in to comment.