From 7b8d5a69e46c5f6bf5e9286a878cfebfda93b12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20=C5=81aziuk?= Date: Tue, 7 Feb 2017 13:13:50 +0100 Subject: [PATCH 1/3] pump --- pump/index.d.ts | 18 +++++++++++++++++ pump/pump-tests.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++ pump/tsconfig.json | 20 +++++++++++++++++++ pump/tslint.json | 1 + 4 files changed, 87 insertions(+) create mode 100644 pump/index.d.ts create mode 100644 pump/pump-tests.ts create mode 100644 pump/tsconfig.json create mode 100644 pump/tslint.json diff --git a/pump/index.d.ts b/pump/index.d.ts new file mode 100644 index 0000000000..735f5a0dba --- /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..57b8b27665 --- /dev/null +++ b/pump/pump-tests.ts @@ -0,0 +1,48 @@ +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) process.exit(0); +} + +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); 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" } From 42ccab4293c3f7132357d5992327f003f6c14bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20=C5=81aziuk?= Date: Tue, 7 Feb 2017 13:15:50 +0100 Subject: [PATCH 2/3] return type fix --- pump/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pump/index.d.ts b/pump/index.d.ts index 735f5a0dba..7361399a6a 100644 --- a/pump/index.d.ts +++ b/pump/index.d.ts @@ -5,7 +5,7 @@ /// -declare function pump(streams: pump.Stream[], callback?: pump.Callback): pump.Stream; +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[]; From b0204a425281d99f0696e6c207cfd31d3ed6c8b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20=C5=81aziuk?= Date: Thu, 9 Feb 2017 21:07:50 +0100 Subject: [PATCH 3/3] tests --- pump/pump-tests.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pump/pump-tests.ts b/pump/pump-tests.ts index 57b8b27665..0c8e91b2c0 100644 --- a/pump/pump-tests.ts +++ b/pump/pump-tests.ts @@ -21,7 +21,7 @@ let rsClosed = false; let callbackCalled = false; function check() { - if (wsClosed && rsClosed && callbackCalled) process.exit(0); + if (wsClosed && rsClosed && callbackCalled) console.log(`pump finished`); } ws.on('close', () => { @@ -46,3 +46,7 @@ setTimeout(() => { setTimeout(() => { throw new Error('timeout'); }, 5000); + +pump(createReadStream('/dev/random'), toHex(), createWriteStream('/dev/null')); + +pump([createReadStream('/dev/random'), toHex(), createWriteStream('/dev/null')]);