-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 8, add TypeScript definition (#15)
- Loading branch information
1 parent
f346c0d
commit 0bf7c48
Showing
5 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ language: node_js | |
node_js: | ||
- '10' | ||
- '8' | ||
- '6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters