$
This commit is contained in:
167
node_modules/hasha/index.d.ts
generated
vendored
Normal file
167
node_modules/hasha/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
/// <reference types="node"/>
|
||||
import {LiteralUnion} from 'type-fest';
|
||||
import {Hash} from 'crypto';
|
||||
|
||||
declare namespace hasha {
|
||||
type ToStringEncoding = 'hex' | 'base64' | 'latin1';
|
||||
type HashaInput = Buffer | string | Array<Buffer | string>;
|
||||
type HashaEncoding = ToStringEncoding | 'buffer';
|
||||
|
||||
type AlgorithmName = LiteralUnion<
|
||||
'md5' | 'sha1' | 'sha256' | 'sha512',
|
||||
string
|
||||
>;
|
||||
|
||||
interface Options<EncodingType = HashaEncoding> {
|
||||
/**
|
||||
Encoding of the returned hash.
|
||||
|
||||
@default 'hex'
|
||||
*/
|
||||
readonly encoding?: EncodingType;
|
||||
|
||||
/**
|
||||
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)_
|
||||
|
||||
@default 'sha512'
|
||||
*/
|
||||
readonly algorithm?: AlgorithmName;
|
||||
}
|
||||
}
|
||||
|
||||
declare const hasha: {
|
||||
/**
|
||||
Calculate the hash for a `string`, `Buffer`, or an array thereof.
|
||||
|
||||
@param input - Data you want to hash.
|
||||
|
||||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer.
|
||||
|
||||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.
|
||||
|
||||
@returns A hash.
|
||||
|
||||
@example
|
||||
```
|
||||
import hasha = require('hasha');
|
||||
|
||||
hasha('unicorn');
|
||||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
|
||||
```
|
||||
*/
|
||||
(input: hasha.HashaInput): string;
|
||||
(
|
||||
input: hasha.HashaInput,
|
||||
options: hasha.Options<hasha.ToStringEncoding>
|
||||
): string;
|
||||
(input: hasha.HashaInput, options: hasha.Options<'buffer'>): Buffer;
|
||||
|
||||
/**
|
||||
Asynchronously calculate the hash for a `string`, `Buffer`, or an array thereof.
|
||||
|
||||
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.
|
||||
|
||||
@param input - Data you want to hash.
|
||||
|
||||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer.
|
||||
|
||||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.
|
||||
|
||||
@returns A hash.
|
||||
|
||||
@example
|
||||
```
|
||||
import hasha = require('hasha');
|
||||
|
||||
(async () => {
|
||||
console.log(await hasha.async('unicorn'));
|
||||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
|
||||
})();
|
||||
```
|
||||
*/
|
||||
async(input: hasha.HashaInput): Promise<string>;
|
||||
async(
|
||||
input: hasha.HashaInput,
|
||||
options: hasha.Options<hasha.ToStringEncoding>
|
||||
): Promise<string>;
|
||||
async(input: hasha.HashaInput, options: hasha.Options<'buffer'>): Promise<Buffer>;
|
||||
|
||||
/**
|
||||
Create a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash).
|
||||
|
||||
@returns The created hash transform stream.
|
||||
|
||||
@example
|
||||
```
|
||||
import hasha = require('hasha');
|
||||
|
||||
// Hash the process input and output the hash sum
|
||||
process.stdin.pipe(hasha.stream()).pipe(process.stdout);
|
||||
```
|
||||
*/
|
||||
stream(options?: hasha.Options<hasha.HashaEncoding>): Hash;
|
||||
|
||||
/**
|
||||
Calculate the hash for a stream.
|
||||
|
||||
@param stream - A stream you want to hash.
|
||||
@returns The calculated hash.
|
||||
*/
|
||||
fromStream(stream: NodeJS.ReadableStream): Promise<string | null>;
|
||||
fromStream(
|
||||
stream: NodeJS.ReadableStream,
|
||||
options?: hasha.Options<hasha.ToStringEncoding>
|
||||
): Promise<string | null>;
|
||||
fromStream(
|
||||
stream: NodeJS.ReadableStream,
|
||||
options?: hasha.Options<'buffer'>
|
||||
): Promise<Buffer | null>;
|
||||
|
||||
/**
|
||||
Calculate the hash for a file.
|
||||
|
||||
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.
|
||||
|
||||
@param filePath - Path to a file you want to hash.
|
||||
@returns The calculated file hash.
|
||||
|
||||
@example
|
||||
```
|
||||
import hasha = require('hasha');
|
||||
|
||||
(async () => {
|
||||
// Get the MD5 hash of an image
|
||||
const hash = await hasha.fromFile('unicorn.png', {algorithm: 'md5'});
|
||||
|
||||
console.log(hash);
|
||||
//=> '1abcb33beeb811dca15f0ac3e47b88d9'
|
||||
})();
|
||||
```
|
||||
*/
|
||||
fromFile(filePath: string): Promise<string | null>;
|
||||
fromFile(
|
||||
filePath: string,
|
||||
options: hasha.Options<hasha.ToStringEncoding>
|
||||
): Promise<string | null>;
|
||||
fromFile(
|
||||
filePath: string,
|
||||
options: hasha.Options<'buffer'>
|
||||
): Promise<Buffer | null>;
|
||||
|
||||
/**
|
||||
Synchronously calculate the hash for a file.
|
||||
|
||||
@param filePath - Path to a file you want to hash.
|
||||
@returns The calculated file hash.
|
||||
*/
|
||||
fromFileSync(filePath: string): string;
|
||||
fromFileSync(
|
||||
filePath: string,
|
||||
options: hasha.Options<hasha.ToStringEncoding>
|
||||
): string;
|
||||
fromFileSync(filePath: string, options: hasha.Options<'buffer'>): Buffer;
|
||||
};
|
||||
|
||||
export = hasha;
|
150
node_modules/hasha/index.js
generated
vendored
Normal file
150
node_modules/hasha/index.js
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const isStream = require('is-stream');
|
||||
|
||||
const {Worker} = (() => {
|
||||
try {
|
||||
return require('worker_threads');
|
||||
} catch (_) {
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
let worker; // Lazy
|
||||
let taskIdCounter = 0;
|
||||
const tasks = new Map();
|
||||
|
||||
const recreateWorkerError = sourceError => {
|
||||
const error = new Error(sourceError.message);
|
||||
|
||||
for (const [key, value] of Object.entries(sourceError)) {
|
||||
if (key !== 'message') {
|
||||
error[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (message.error === undefined) {
|
||||
task.resolve(message.value);
|
||||
} else {
|
||||
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;
|
||||
});
|
||||
};
|
||||
|
||||
const taskWorker = (method, args, transferList) => new Promise((resolve, reject) => {
|
||||
const id = taskIdCounter++;
|
||||
tasks.set(id, {resolve, reject});
|
||||
|
||||
if (worker === undefined) {
|
||||
createWorker();
|
||||
}
|
||||
|
||||
worker.ref();
|
||||
worker.postMessage({id, method, args}, transferList);
|
||||
});
|
||||
|
||||
const hasha = (input, options = {}) => {
|
||||
let outputEncoding = options.encoding || 'hex';
|
||||
|
||||
if (outputEncoding === 'buffer') {
|
||||
outputEncoding = undefined;
|
||||
}
|
||||
|
||||
const hash = crypto.createHash(options.algorithm || 'sha512');
|
||||
|
||||
const update = buffer => {
|
||||
const inputEncoding = typeof buffer === 'string' ? 'utf8' : undefined;
|
||||
hash.update(buffer, inputEncoding);
|
||||
};
|
||||
|
||||
if (Array.isArray(input)) {
|
||||
input.forEach(update);
|
||||
} else {
|
||||
update(input);
|
||||
}
|
||||
|
||||
return hash.digest(outputEncoding);
|
||||
};
|
||||
|
||||
hasha.stream = (options = {}) => {
|
||||
let outputEncoding = options.encoding || 'hex';
|
||||
|
||||
if (outputEncoding === 'buffer') {
|
||||
outputEncoding = undefined;
|
||||
}
|
||||
|
||||
const stream = crypto.createHash(options.algorithm || 'sha512');
|
||||
stream.setEncoding(outputEncoding);
|
||||
return stream;
|
||||
};
|
||||
|
||||
hasha.fromStream = async (stream, options = {}) => {
|
||||
if (!isStream(stream)) {
|
||||
throw new TypeError('Expected a stream');
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// TODO: Use `stream.pipeline` and `stream.finished` when targeting Node.js 10
|
||||
stream
|
||||
.on('error', reject)
|
||||
.pipe(hasha.stream(options))
|
||||
.on('error', reject)
|
||||
.on('finish', function () {
|
||||
resolve(this.read());
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (Worker === undefined) {
|
||||
hasha.fromFile = async (filePath, options) => hasha.fromStream(fs.createReadStream(filePath), options);
|
||||
hasha.async = async (input, options) => hasha(input, options);
|
||||
} else {
|
||||
hasha.fromFile = async (filePath, {algorithm = 'sha512', encoding = 'hex'} = {}) => {
|
||||
const hash = await taskWorker('hashFile', [algorithm, filePath]);
|
||||
|
||||
if (encoding === 'buffer') {
|
||||
return Buffer.from(hash);
|
||||
}
|
||||
|
||||
return Buffer.from(hash).toString(encoding);
|
||||
};
|
||||
|
||||
hasha.async = async (input, {algorithm = 'sha512', encoding = 'hex'} = {}) => {
|
||||
if (encoding === 'buffer') {
|
||||
encoding = undefined;
|
||||
}
|
||||
|
||||
const hash = await taskWorker('hash', [algorithm, input]);
|
||||
|
||||
if (encoding === undefined) {
|
||||
return Buffer.from(hash);
|
||||
}
|
||||
|
||||
return Buffer.from(hash).toString(encoding);
|
||||
};
|
||||
}
|
||||
|
||||
hasha.fromFileSync = (filePath, options) => hasha(fs.readFileSync(filePath), options);
|
||||
|
||||
module.exports = hasha;
|
9
node_modules/hasha/license
generated
vendored
Normal file
9
node_modules/hasha/license
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
61
node_modules/hasha/package.json
generated
vendored
Normal file
61
node_modules/hasha/package.json
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"name": "hasha",
|
||||
"version": "5.2.0",
|
||||
"description": "Hashing made simple. Get the hash of a buffer/string/stream/file.",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/hasha",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"thread.js"
|
||||
],
|
||||
"keywords": [
|
||||
"hash",
|
||||
"hashing",
|
||||
"crypto",
|
||||
"hex",
|
||||
"base64",
|
||||
"md5",
|
||||
"sha1",
|
||||
"sha256",
|
||||
"sha512",
|
||||
"sum",
|
||||
"stream",
|
||||
"file",
|
||||
"fs",
|
||||
"buffer",
|
||||
"string",
|
||||
"text",
|
||||
"rev",
|
||||
"revving",
|
||||
"simple",
|
||||
"easy"
|
||||
],
|
||||
"dependencies": {
|
||||
"is-stream": "^2.0.0",
|
||||
"type-fest": "^0.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^12.7.5",
|
||||
"ava": "^2.4.0",
|
||||
"proxyquire": "^2.1.0",
|
||||
"tsd": "^0.8.0",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"xo": {
|
||||
"rules": {
|
||||
"import/no-unresolved": "off"
|
||||
}
|
||||
}
|
||||
}
|
133
node_modules/hasha/readme.md
generated
vendored
Normal file
133
node_modules/hasha/readme.md
generated
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
<h1 align="center">
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<img width="380" src="media/logo.svg" alt="hasha">
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
</h1>
|
||||
|
||||
> Hashing made simple. Get the hash of a buffer/string/stream/file.
|
||||
|
||||
[](https://travis-ci.org/sindresorhus/hasha)
|
||||
|
||||
Convenience wrapper around the core [`crypto` Hash class](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm) with simpler API and better defaults.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install hasha
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const hasha = require('hasha');
|
||||
|
||||
hasha('unicorn');
|
||||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
|
||||
```
|
||||
|
||||
```js
|
||||
const hasha = require('hasha');
|
||||
|
||||
(async () => {
|
||||
console.log(await hasha.async('unicorn'));
|
||||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
|
||||
})();
|
||||
```
|
||||
|
||||
```js
|
||||
const hasha = require('hasha');
|
||||
|
||||
// Hash the process input and output the hash sum
|
||||
process.stdin.pipe(hasha.stream()).pipe(process.stdout);
|
||||
```
|
||||
|
||||
```js
|
||||
const hasha = require('hasha');
|
||||
|
||||
(async () => {
|
||||
// Get the MD5 hash of an image
|
||||
const hash = await hasha.fromFile('unicorn.png', {algorithm: 'md5'});
|
||||
|
||||
console.log(hash);
|
||||
//=> '1abcb33beeb811dca15f0ac3e47b88d9'
|
||||
})();
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
See the Node.js [`crypto` docs](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) for more about hashing.
|
||||
|
||||
### hasha(input, [options])
|
||||
|
||||
Returns a hash.
|
||||
|
||||
#### input
|
||||
|
||||
Type: `Buffer | string | Array<Buffer | string>`
|
||||
|
||||
Buffer you want to hash.
|
||||
|
||||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer.
|
||||
|
||||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `object`
|
||||
|
||||
##### encoding
|
||||
|
||||
Type: `string`<br>
|
||||
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))*
|
||||
|
||||
*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?)
|
||||
|
||||
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?)
|
||||
|
||||
Returns a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash).
|
||||
|
||||
### hasha.fromStream(stream, options?)
|
||||
|
||||
Returns a `Promise` for the calculated hash.
|
||||
|
||||
### 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?)
|
||||
|
||||
Returns the calculated file hash.
|
||||
|
||||
|
||||
## Related
|
||||
|
||||
- [hasha-cli](https://github.com/sindresorhus/hasha-cli) - CLI for this module
|
||||
- [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
|
57
node_modules/hasha/thread.js
generated
vendored
Normal file
57
node_modules/hasha/thread.js
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const {parentPort} = require('worker_threads');
|
||||
|
||||
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)
|
||||
.on('finish', () => {
|
||||
const {buffer} = hasher.read();
|
||||
resolve({value: buffer, transferList: [buffer]});
|
||||
});
|
||||
}),
|
||||
hash: async (algorithm, input) => {
|
||||
const hasher = crypto.createHash(algorithm);
|
||||
|
||||
if (Array.isArray(input)) {
|
||||
for (const part of input) {
|
||||
hasher.update(part);
|
||||
}
|
||||
} else {
|
||||
hasher.update(input);
|
||||
}
|
||||
|
||||
const hash = hasher.digest().buffer;
|
||||
return {value: hash, transferList: [hash]};
|
||||
}
|
||||
};
|
||||
|
||||
parentPort.on('message', async message => {
|
||||
try {
|
||||
const {method, args} = message;
|
||||
const handler = handlers[method];
|
||||
|
||||
if (handler === undefined) {
|
||||
throw new Error(`Unknown method '${method}'`);
|
||||
}
|
||||
|
||||
const {value, transferList} = await handler(...args);
|
||||
parentPort.postMessage({id: message.id, value}, transferList);
|
||||
} catch (error) {
|
||||
const newError = {message: error.message, stack: error.stack};
|
||||
|
||||
for (const [key, value] of Object.entries(error)) {
|
||||
if (typeof value !== 'object') {
|
||||
newError[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
parentPort.postMessage({id: message.id, error: newError});
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user