diff --git a/types/sha/index.d.ts b/types/sha/index.d.ts new file mode 100644 index 0000000000..954036341b --- /dev/null +++ b/types/sha/index.d.ts @@ -0,0 +1,45 @@ +// Type definitions for sha 2.0 +// Project: https://github.com/ForbesLindesay/sha +// Definitions by: Oscar Busk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { Transform } from 'stream'; + +export type CheckCallback = (err: Error | null) => R; +export type GetCallback = (err: Error | null, actual: string) => void; + +export interface ShaOptions { + /** defaults to `sha1` and can be any of the algorithms supported by `crypto.createHash` */ + algorithm?: string; +} + +/** + * Asynchronously check that `fileName` has a "hash" of `expected`. The callback will be called with either `null` + * or an error (indicating that they did not match). + */ +export function check(fileName: string, expected: string, cb: CheckCallback): void | R; +export function check(fileName: string, expected: string, options: ShaOptions, cb: CheckCallback): void | R; +/** + * Synchronously check that `fileName` has a "hash" of `expected`. Throws if they do not match. + */ +export function checkSync(fileName: string, expected: string, options?: ShaOptions): void; +/** + * Asynchronously get the "hash" of `fileName`. The callback will be called with an optional `error` object and the + * (lower cased) hex digest of the hash. + */ +export function get(fileName: string, cb: GetCallback): void; +export function get(fileName: string, options: ShaOptions, cb: GetCallback): void; +/** Synchronously get the "hash" of `fileName`. */ +export function getSync(fileName: string, options?: ShaOptions): string; +/** + * Check the hash of a stream without ever buffering it. This is a pass through stream so you can do things like: + * + * fs.createReadStream('src') + * .pipe(sha.stream('expected')) + * .pipe(fs.createWriteStream('dest')) + * + * `dest` will be a complete copy of `src` and an error will be emitted if the hash did not match `'expected'`. + */ +export function stream(expected: string, options?: ShaOptions): Transform; diff --git a/types/sha/sha-tests.ts b/types/sha/sha-tests.ts new file mode 100644 index 0000000000..3231d598e1 --- /dev/null +++ b/types/sha/sha-tests.ts @@ -0,0 +1,97 @@ +import * as sha from 'sha'; +import * as fs from 'fs'; + +const algorithm = 'sha256'; +interface CustType { n: number; } +const C: CustType = { n: 1 }; + +//////////////////////////////////////////////////////////////////////////////////////// +// sha.check + +sha.check('./file'); // $ExpectError +sha.check('./file', '1a2b3c4d'); // $ExpectError +sha.check('./file', { dir: 1 }, () => C); // $ExpectError +sha.check('./file', '1a2b3c4d', () => C); // $ExpectType void | CustType +sha.check('./file', '1a2b3c4d', {}); // $ExpectError +sha.check('./file', '1a2b3c4d', {}, () => C); // $ExpectType void | CustType +sha.check('./file', '1a2b3c4d', { algorithm }, () => C); // $ExpectType void | CustType +sha.check('./file', '1a2b3c4d', { sha: true }, () => C); // $ExpectError +// $ExpectType void | CustType +sha.check('./file', '1a2b3c4d', (er) => { + if (er) { + er; // $ExpectType Error + } else { + er; // $ExpectType null + } + return C; +}); +// $ExpectType void | CustType +sha.check('./file', '1a2b3c4d', { algorithm }, (er) => { + if (er) { + er; // $ExpectType Error + } else { + er; // $ExpectType null + } + return C; +}); + +//////////////////////////////////////////////////////////////////////////////////////// +// sha.checkSync + +sha.checkSync('./file'); // $ExpectError +sha.checkSync('./file', { dir: 1 }); // $ExpectError +sha.checkSync('./file', '1a2b3c4d'); // $ExpectType void +sha.checkSync('./file', '1a2b3c4d', {}); // $ExpectType void +sha.checkSync('./file', '1a2b3c4d', { algorithm }); // $ExpectType void +sha.checkSync('./file', '1a2b3c4d', { sha: true }); // $ExpectError + +//////////////////////////////////////////////////////////////////////////////////////// +// sha.get + +sha.get('./file'); // $ExpectError +sha.get('./file', () => C); // $ExpectType void +sha.get('./file', {}); // $ExpectError +sha.get('./file', {}, () => C); // $ExpectType void +sha.get('./file', { algorithm }, () => C); // $ExpectType void +sha.get('./file', { error: true }, () => C); // $ExpectError +// $ExpectType void +sha.get('./file', (err, actual) => { + if (err) { + err; // $ExpectType Error + } else { + err; // $ExpectType null + actual; // $ExpectType string + } +}); +// $ExpectType void +sha.get('./file', { algorithm }, (err, actual) => { + if (err) { + err; // $ExpectType Error + } else { + err; // $ExpectType null + actual; // $ExpectType string + } +}); + +//////////////////////////////////////////////////////////////////////////////////////// +// sha.getSync + +sha.getSync('./file'); // $ExpectType string +sha.getSync('./file', {}); // $ExpectType string +sha.getSync('./file', { algorithm }); // $ExpectType string +sha.getSync('./file', { error: true }); // $ExpectError + +//////////////////////////////////////////////////////////////////////////////////////// +// sha.stream + +// $ExpectType Transform +const s = sha.stream('1a2b3c4d'); + +fs.createReadStream('./file') + .pipe(s) + .pipe(fs.createWriteStream('dest')); + +sha.stream({ name: 'hello' }); // $ExpectError +sha.stream('1a2b3c4d', {}); // $ExpectType Transform +sha.stream('1a2b3c4d', { algorithm }); // $ExpectType Transform +sha.stream('1a2b3c4d', { error: true }); // $ExpectError diff --git a/types/sha/tsconfig.json b/types/sha/tsconfig.json new file mode 100644 index 0000000000..3089a20d75 --- /dev/null +++ b/types/sha/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sha-tests.ts" + ] +} diff --git a/types/sha/tslint.json b/types/sha/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sha/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }