diff --git a/pump/index.d.ts b/pump/index.d.ts new file mode 100644 index 0000000000..7361399a6a --- /dev/null +++ b/pump/index.d.ts @@ -0,0 +1,18 @@ +// Type definitions for pump 1.0 +// Project: https://github.com/mafintosh/pump +// Definitions by: Tomek Łaziuk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare function pump(streams: pump.Stream[], callback?: pump.Callback): pump.Stream[]; + +// callback have to be passed as last argument +declare function pump(...streams: Array): pump.Stream[]; + +declare namespace pump { + export type Callback = (err: Error) => any; + export type Stream = NodeJS.ReadableStream | NodeJS.WritableStream; +} + +export = pump; diff --git a/pump/pump-tests.ts b/pump/pump-tests.ts new file mode 100644 index 0000000000..0c8e91b2c0 --- /dev/null +++ b/pump/pump-tests.ts @@ -0,0 +1,52 @@ +import * as pump from 'pump'; +import { createReadStream, createWriteStream } from 'fs'; +import { Transform } from 'stream'; + +const rs = createReadStream('/dev/random'); +const ws = createWriteStream('/dev/null'); + +function toHex() { + const reverse: Transform = new Transform(); + + (reverse as any)._transform = (chunk: any, enc: any, callback: any) => { + reverse.push(chunk.toString('hex')); + callback(); + }; + + return reverse; +} + +let wsClosed = false; +let rsClosed = false; +let callbackCalled = false; + +function check() { + if (wsClosed && rsClosed && callbackCalled) console.log(`pump finished`); +} + +ws.on('close', () => { + wsClosed = true; + check(); +}); + +rs.on('close', () => { + rsClosed = true; + check(); +}); + +pump(rs, toHex(), toHex(), toHex(), ws, () => { + callbackCalled = true; + check(); +}); + +setTimeout(() => { + rs.destroy(); +}, 1000); + +setTimeout(() => { + throw new Error('timeout'); +}, 5000); + +pump(createReadStream('/dev/random'), toHex(), createWriteStream('/dev/null')); + +pump([createReadStream('/dev/random'), toHex(), createWriteStream('/dev/null')]); diff --git a/pump/tsconfig.json b/pump/tsconfig.json new file mode 100644 index 0000000000..b7d71ceb05 --- /dev/null +++ b/pump/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "pump-tests.ts" + ] +} diff --git a/pump/tslint.json b/pump/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/pump/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }