Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 8, add TypeScript definition #15

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
93 changes: 93 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/// <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';

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))*
*
* *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?: string;
BendingBender marked this conversation as resolved.
Show resolved Hide resolved
}

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 A `Promise` for a hash.
BendingBender marked this conversation as resolved.
Show resolved Hide resolved
*/
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 A `Promise` for the 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 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"
}
}