ADD: init types for streamtest (#27001)

This commit is contained in:
Harris Lummis
2018-07-02 15:37:59 -07:00
committed by Mohamed Hegazy
parent 0537223e2b
commit ed918315eb
4 changed files with 249 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
// Type definitions for streamtest 1.2
// Project: https://github.com/nfroidure/streamtest
// Definitions by: Harris Lummis <https://github.com/lummish>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="node" />
import { PassThrough, Readable, ReadableOptions, Writable, WritableOptions } from "stream";
export type Chunk = string | Buffer | Uint8Array;
export type NonNull = {} | undefined;
/** Allowed version names for streamtest */
export type VersionName = "v1" | "v2";
/**
* List of supported versions (currently v1 and v2). v1 should be used for
* Node < 0.10 streams.
*/
export const versions: VersionName[];
/** v1 methods (Node version < 0.10) */
export const v1: typeof v2;
/** v2 methods (Node version >= 0.10) */
export namespace v2 {
/**
* Create a readable stream streaming 'chunks' each after 'timeout'
* milliseconds and then end. Useful for testing buffer based streams.
* @param chunks The input chunks for the readable stream
* @param timeout The timeout (in milliseconds) used to space chunks.
* Defaults to 0.
* @returns A new readable stream
*/
function fromChunks(chunks: Chunk[], timeout?: number): Readable;
/**
* Create a readable stream streaming 'objects' each after 'timeout'
* milliseconds and then end. Useful for testing objectMode based streams.
* @param chunks The input objects for the readable stream
* @param timeout The timeout (in milliseconds) used to space chunks.
* Defaults to 0.
* @returns A new readable stream
*/
function fromObjects(objects: NonNull[], timeout?: number): Readable;
/**
* Create a readable stream streaming 'chunks' each after 'timeout'
* milliseconds, emit 'err,' and then end. Useful for testing buffer based
* streams.
* @param err The error to emit
* @param chunks The input objects for the readable stream
* @param timeout The timeout (in milliseconds) used to space chunks.
* Defaults to 0.
* @returns A new readable stream
*/
function fromErroredChunks(err: Error, chunks: Chunk[], timeout?: number): Readable;
/**
* Create a readable stream streaming 'objects' each after 'timeout'
* milliseconds, emit 'err,' and then end. Useful for testing objectMode
* based streams.
* @param err The error to emit
* @param chunks The input objects for the readable stream
* @param timeout The timeout (in milliseconds) used to space chunks.
* Defaults to 0.
* @returns A new readable stream
*/
function fromErroredObjects(err: Error, objects: NonNull[], timeout?: number): Readable;
/**
* Create a writable stream collecting written chunks and call the passed
* callback function when it finishes.
* @param cb The callback function. Takes an error as its first argument and
* an array of chunks as its second
* @returns A new writable stream
*/
function toChunks(cb: (err: Error, chunks: Chunk[]) => any): Writable;
/**
* Create a writable stream collecting written chunks and call the passed
* callback function when it finishes.
* @param cb The callback function. Takes an error as its first argument and
* an array of objects as its second
* @returns A new writable stream
*/
function toObjects(cb: (err: Error, objects: NonNull[]) => any): Writable;
/**
* Create a writable stream collecting written chunks and call the passed
* callback function when it finishes.
* @param cb The callback function. Takes an error as its first argument and
* an string as its second
* @returns A new writable stream
*/
function toText(cb: (err: Error, text: string) => any): Writable;
/**
* Create a new readable stream
* @param options The options used to create the stream
* @returns A new readable stream
*/
function readable(options?: ReadableOptions): Readable;
/**
* Create a new writable stream
* @param options The options used to create the stream
* @returns A new writable stream
*/
function writable(options?: WritableOptions): Writable;
/**
* Create a synchronous PassThrough stream
* @returns A PassThrough stream
*/
function syncReadableChunks(): PassThrough;
/**
* Create a synchronous PassThrough stream in object mode
* @returns A PassThrough stream
*/
function syncReadableObjects(): PassThrough;
/**
* Write chunks to a stream synchronously
* @param stream The stream to write to
* @param chunks The chunks to write
*/
function syncWrite(stream: Writable, chunks: Chunk[]): void;
/**
* Write chunks to a stream synchronously and emit an error when done
* @param stream The stream to write to
* @param err The error to emit
* @param chunks The chunks to write
*/
function syncError(stream: Writable, err: Error, chunks: Chunk[]): void;
}
+99
View File
@@ -0,0 +1,99 @@
import { PassThrough, Readable, Writable, Stream } from "stream";
import * as StreamTest from "streamtest";
StreamTest.versions; // $ExpectType VersionName[]
const fromObjectsTestArray = ['hello', { a: 1, b: false }, 1];
function runTest() {
StreamTest.versions.forEach((version) => {
StreamTest[version].fromChunks(['hello']); // $ExpectType Readable
StreamTest[version].fromChunks(['hello'], 10); // $ExpectType Readable
StreamTest[version].fromObjects(fromObjectsTestArray); // $ExpectType Readable
StreamTest[version].fromObjects(fromObjectsTestArray, 10); // $ExpectType Readable
const errorChunkStream1 = StreamTest[version].fromErroredChunks(
new Error('bad thing'),
[Buffer.from('some data')],
);
errorChunkStream1; // $ExpectType Readable
errorChunkStream1.on('error', (err) => {
return; // keep on working
});
const errorChunkStream2 = StreamTest[version].fromErroredChunks(
new Error('bad thing'),
[Buffer.from('some data')],
10
);
errorChunkStream2; // $ExpectType Readable
errorChunkStream2.on('error', (err) => {
return; // keep on working
});
const errorObjectStream1 = StreamTest[version].fromErroredObjects(
new Error('bad thing'),
fromObjectsTestArray,
);
errorObjectStream1; // $ExpectType Readable
errorObjectStream1.on('error', (err) => {
return; // keep on working
});
const errorObjectStream2 = StreamTest[version].fromErroredObjects(
new Error('bad thing'),
fromObjectsTestArray,
10,
);
errorObjectStream2; // $ExpectType Readable
errorObjectStream2.on('error', (err) => {
return; // keep on working
});
const toChunks = StreamTest[version].toChunks((err, chunks) => {
if (err) {
throw err;
}
return;
});
toChunks; // $ExpectType Writable
const toObjects = StreamTest[version].toObjects((err, objects) => {
if (err) {
throw err;
}
return;
});
toObjects; // $ExpectType Writable
const toText = StreamTest[version].toText((err, text) => {
if (err) {
throw err;
}
return;
});
toText; // $ExpectType Writable
StreamTest[version].syncReadableChunks(); // $ExpectType PassThrough
StreamTest[version].syncReadableObjects(); // $ExpectType PassThrough
StreamTest[version].syncWrite(StreamTest[version].writable(), ['a chunk']);
try {
StreamTest[version].syncError(
StreamTest[version].writable(),
new Error('bad thing'),
['a chunk']
);
} catch (err) {
// If error not anticipated, throw again
if (err.message !== 'bad thing') {
throw err;
}
}
});
}
runTest();
+23
View File
@@ -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",
"streamtest-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }