Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 12, 2019
1 parent f346c0d commit 0bf7c48
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 10 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
96 changes: 96 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/// <reference types="node"/>
import {Hash} from 'crypto';
import {Readable as ReadableStream} from 'stream';

export type ToStringEncoding = 'hex' | 'base64' | 'latin1';
export type HashaInput = string | string[] | Buffer | Buffer[];
export type HashaEncoding = ToStringEncoding | 'buffer';

// TODO: Remove this clutter after https://github.com/Microsoft/TypeScript/issues/29729 is resolved
export type AlgorithmName = string & {algorithm?: unknown};

export 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?: 'md5' | 'sha1' | 'sha256' | 'sha512' | 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.
*/
(input: HashaInput): string;
(input: HashaInput, options: Options<ToStringEncoding>): string;
(input: HashaInput, options: Options<'buffer'>): Buffer;

/**
* Create a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash).
*
* @returns The created hash transform stream.
*/
stream(options?: Options<HashaEncoding>): Hash;

/**
* Calculate the hash for a stream.
*
* @param stream - A stream you want to hash.
* @returns The calculated hash.
*/
fromStream(stream: ReadableStream): Promise<string | null>;
fromStream(
stream: ReadableStream,
options?: Options<ToStringEncoding>
): Promise<string | null>;
fromStream(
stream: ReadableStream,
options?: Options<'buffer'>
): Promise<Buffer | null>;

/**
* Calculate the hash for a file.
*
* @param filePath - Path to a file you want to hash.
* @returns The calculated file hash.
*/
fromFile(filePath: string): Promise<string | null>;
fromFile(
filePath: string,
options: Options<ToStringEncoding>
): Promise<string | null>;
fromFile(
filePath: string,
options: 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: Options<ToStringEncoding>): string;
fromFileSync(filePath: string, options: Options<'buffer'>): Buffer;
};

export default hasha;
29 changes: 29 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {expectType} from 'tsd-check';
import hasha from '.';

expectType<string>(hasha('unicorn'));
expectType<string>(hasha('unicorn', {algorithm: 'md5'}));
expectType<string>(hasha('unicorn', {encoding: 'latin1'}));
expectType<Buffer>(hasha('unicorn', {encoding: 'buffer'}));

process.stdin.pipe(hasha.stream()).pipe(process.stdout);

expectType<Promise<string | null>>(hasha.fromStream(process.stdin));
expectType<Promise<string | null>>(
hasha.fromStream(process.stdin, {encoding: 'hex'})
);
expectType<Promise<Buffer | null>>(
hasha.fromStream(process.stdin, {encoding: 'buffer'})
);

expectType<Promise<string | null>>(hasha.fromFile('unicorn.png'));
expectType<Promise<string | null>>(
hasha.fromFile('unicorn.png', {encoding: 'base64'})
);
expectType<Promise<Buffer | null>>(
hasha.fromFile('unicorn.png', {encoding: 'buffer'})
);

expectType<string>(hasha.fromFileSync('unicorn.png'));
expectType<string>(hasha.fromFileSync('unicorn.png', {encoding: 'base64'}));
expectType<Buffer>(hasha.fromFileSync('unicorn.png', {encoding: 'buffer'}));
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"hash",
Expand Down Expand Up @@ -44,8 +45,10 @@
"is-stream": "^1.0.1"
},
"devDependencies": {
"ava": "^1.2.1",
"@types/node": "^11.10.5",
"ava": "^1.3.1",
"proxyquire": "^2.1.0",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const hasha = require('hasha');

## API

See the Node.js [`crypto` docs](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm) for more about hashing.
See the Node.js [`crypto` docs](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) for more about hashing.

### hasha(input, [options])

Expand Down Expand Up @@ -85,7 +85,7 @@ Encoding of the returned hash.

Type: `string`<br>
Default: `sha512`<br>
Values: `md5` `sha1` `sha256` `sha512` *([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm))*
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)*

Expand All @@ -95,15 +95,15 @@ Returns a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_clas

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

Returns a `Promise` for a hash.
Returns a `Promise` for the calculated hash.

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

Returns a `Promise` for a hash.
Returns a `Promise` for the calculated file hash.

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

Returns a hash.
Returns the calculated file hash.


## Related
Expand Down

0 comments on commit 0bf7c48

Please sign in to comment.