Improve through2 declarations (#11499)

* [through2] Fix typos

* [through2] Change return types to stream.Transform

`through2` creates `stream.Transform`-flavored streams, so switching the return types to `stream.Transform` gives us richer typing information than using `NodeJS.ReadWriteStream`, which is missing methods (e.g. `stream.Transform#push`) and the ability to use object mode.

* [through2] Add through2.ctor method

* [through2] Update header and docs
This commit is contained in:
Ben Chauvette
2016-09-26 07:52:03 -07:00
committed by Andy
parent d0a34b8791
commit 55d9114bbd
2 changed files with 67 additions and 10 deletions

View File

@@ -1,9 +1,11 @@
/// <reference path="./through2.d.ts" />
/// <reference path="../node/node.d.ts" />
import stream = require('stream');
import through2 = require('through2');
var rws: NodeJS.ReadWriteStream;
var rws: stream.Transform;
var Rws: through2.Through2Constructor;
rws = through2({
objectMode: true,
@@ -53,3 +55,48 @@ rws = through2.obj(function (entry: any, enc: string, callback: (err: any) => vo
}, (flashCallback) => {
flashCallback();
});
// ctor
Rws = through2.ctor({
objectMode: true,
allowHalfOpen: true
}, function (entry: any, enc: string, callback: () => void) {
this.push('foo');
callback();
}, () => {
});
rws = Rws();
rws = new Rws();
rws = new Rws({ objectMode: true, allowHalfOpen: true });
Rws = through2.ctor(function (entry: any, enc: string, callback: () => void) {
this.push('foo');
callback();
}, () => {
});
rws = Rws();
rws = new Rws();
rws = new Rws({ objectMode: true, allowHalfOpen: true });
Rws = through2.ctor(function (entry: any, enc: string, callback: (error: any, data?: any) => void) {
callback(null, 'foo');
}, (flushCallback: () => void) => {
flushCallback();
});
rws = Rws();
rws = new Rws();
rws = new Rws({ objectMode: true, allowHalfOpen: true });
Rws = through2.ctor(function (entry: any, enc: string, callback: () => void) {
this.push('foo');
callback();
});
rws = Rws();
rws = new Rws();
rws = new Rws({ objectMode: true, allowHalfOpen: true });

View File

@@ -1,6 +1,6 @@
// Type definitions for through2 v 2.0.0
// Project: https://github.com/rvagg/through2
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, jedmao <https://github.com/jedmao>, Georgios Valotasios <https://github.com/valotas>
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, jedmao <https://github.com/jedmao>, Georgios Valotasios <https://github.com/valotas>, Ben Chauvette <https://github.com/bdchauvette>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
@@ -9,20 +9,30 @@ declare module 'through2' {
import stream = require('stream');
type TransfofmCallback = (err?: any, data?: any) => void;
type TransformFunction = (chunk: any, enc: string, callback: TransfofmCallback) => void;
type FlashCallback = (flushCallback: () => void) => void;
type TransformCallback = (err?: any, data?: any) => void;
type TransformFunction = (chunk: any, enc: string, callback: TransformCallback) => void;
type FlushCallback = (flushCallback: () => void) => void;
function through2(transform?: TransformFunction, flush?: FlashCallback): NodeJS.ReadWriteStream;
function through2(transform?: TransformFunction, flush?: FlushCallback): stream.Transform;
function through2(opts?: stream.DuplexOptions, transform?: TransformFunction, flush?: FlashCallback): NodeJS.ReadWriteStream;
function through2(opts?: stream.DuplexOptions, transform?: TransformFunction, flush?: FlushCallback): stream.Transform;
namespace through2 {
export interface Through2Constructor extends stream.Transform {
new(opts?: stream.DuplexOptions): stream.Transform;
(opts?: stream.DuplexOptions): stream.Transform;
}
export function obj(transform?: TransformFunction, flush?: FlashCallback): NodeJS.ReadWriteStream;
export function push(data: any): void;
/**
* Convenvience method for creating object streams
*/
export function obj(transform?: TransformFunction, flush?: FlushCallback): stream.Transform;
/**
* Creates a constructor for a custom Transform. This is useful when you
* want to use the same transform logic in multiple instances.
*/
export function ctor(opts?: stream.DuplexOptions, transfrom?: TransformFunction, flush?: FlushCallback): Through2Constructor;
}
export = through2;