diff --git a/types/readable-stream/index.d.ts b/types/readable-stream/index.d.ts new file mode 100644 index 0000000000..b63e1bae9d --- /dev/null +++ b/types/readable-stream/index.d.ts @@ -0,0 +1,266 @@ +// Type definitions for readable-stream 2.3 +// Project: https://github.com/nodejs/readable-stream +// Definitions by: TeamworkGuy2 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +import * as events from "events"; +import * as stream from "stream"; +import * as SafeBuffer from "safe-buffer"; +import { NodeStringDecoder } from "string_decoder"; + +declare class _Readable extends stream.Readable { + // static ReadableState: _Readable.ReadableState; + _readableState: _Readable.ReadableState; + destroyed: boolean; + + constructor(options?: _Readable.ReadableOptions); + + destroy(err?: Error, callback?: (error: Error | null) => void): this; + _undestroy(): void; +} + +declare namespace _Readable { + // ==== BufferList ==== + interface Entry { + data: D; + next: Entry | null; + } + + interface BufferList { + head: Entry; + tail: Entry; + length: number; + + push(v: D): void; + unshift(v: D): void; + shift(): D; + clear(): void; + join(s: any): string; + concat(n: number): D; + } + + // ==== destroy ==== + interface Destroy { + destroy(this: Readable | Writable, error: Error | null, callback?: (error: Error | null) => void): Readable | Writable; + undestroy(this: Readable | Writable): void; + } + + // ==== _stream_duplex ==== + type DuplexOptions = ReadableOptions & WritableOptions & { + allowHalfOpen?: boolean; + readable?: boolean; + writable?: boolean; + read?(this: Duplex, size: number): void; + write?(this: Duplex, chunk: any, encoding: string, callback: (error?: Error | null) => void): void; + writev?(this: Duplex, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void; + final?(this: Duplex, callback: (error?: Error | null) => void): void; + destroy?(this: Duplex, error: Error | null, callback: (error: Error | null) => void): void; + }; + + class Duplex extends Writable implements /*extends*/_Readable, stream.Duplex { + allowHalfOpen: boolean; + destroyed: boolean; + // Readable + readable: boolean; + readonly readableHighWaterMark: number; + readonly readableLength: number; + _readableState: ReadableState; + + _read(size?: number): void; + read(size?: number): any; + setEncoding(enc: string): this; + resume(): this; + pause(): this; + isPaused(): boolean; + unpipe(dest?: NodeJS.WritableStream): this; + unshift(chunk: any): boolean; + wrap(oldStream: NodeJS.ReadableStream): this; + push(chunk: any, encoding?: string): boolean; + _destroy(err: Error | null, callback: (error: Error | null) => void): void; + destroy(err?: Error, callback?: (error: Error | null) => void): this; + pipe(dest: S, pipeOpts?: { end?: boolean }): S; + addListener(ev: string | symbol, fn: (...args: any[]) => void): this; + on(ev: string | symbol, fn: (...args: any[]) => void): this; + + _undestroy(): void; + [Symbol.asyncIterator](): AsyncIterableIterator; + // end-Readable + + constructor(options?: DuplexOptions); + } + + // ==== _stream_passthrough ==== + class PassThrough extends Transform implements stream.PassThrough { + constructor(options?: TransformOptions); + + _transform(chunk: T, encoding: string | null | undefined, callback: (error: any, data: T) => void): void; + } + + // ==== _stream_readable ==== + interface ReadableStateOptions { + defaultEncoding?: string; + encoding?: string; + highWaterMark?: number; + objectMode?: boolean; + readableObjectMode?: boolean; + readableHighWaterMark?: number; + } + + interface ReadableState { + objectMode: boolean; + highWaterMark: number; + buffer: BufferList; + length: number; + pipes: any; // NodeJS.WritableStream | any[]; // TODO + pipesCount: number; + flowing: any; + ended: boolean; + endEmitted: boolean; + reading: boolean; + sync: boolean; + needReadable: boolean; + emittedReadable: boolean; + readableListening: boolean; + resumeScheduled: boolean; + destroyed: boolean; + awaitDrain: number; + defaultEncoding: string; + readingMore: boolean; + decoder: NodeStringDecoder | null; + encoding: string | null; + + // new (options: ReadableStateOptions, stream: _Readable): ReadableState; + } + + type ReadableOptions = ReadableStateOptions & { + read?(this: _Readable, size: number): void; + destroy?(this: _Readable, error: Error | null, callback: (error: Error | null) => void): void; + }; + + class Readable extends _Readable { + constructor(options?: ReadableOptions); + } + + // ==== _stream_transform ==== + type TransformOptions = DuplexOptions & { + read?(this: Transform, size: number): void; + write?(this: Transform, chunk: any, encoding: string, callback: (error?: Error | null) => void): void; + writev?(this: Transform, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void; + final?(this: Transform, callback: (error?: Error | null) => void): void; + destroy?(this: Transform, error: Error | null, callback: (error: Error | null) => void): void; + transform?(this: Transform, chunk: any, encoding: string, callback: (error?: Error, data?: any) => void): void; + flush?(this: Transform, callback: (er: any, data: any) => void): void; + }; + + class Transform extends Duplex implements stream.Transform { + _transformState: { + afterTransform: (this: Transform, er: any, data: any) => void | boolean; + needTransform: boolean; + transforming: boolean; + writecb: ((err: any) => any) | null; + writechunk: any; // TODO + writeencoding: string | null; + }; + + constructor(options?: TransformOptions); + + _transform(chunk: any, encoding: string, callback: (error?: Error, data?: any) => void): void; + _flush(callback: (error?: Error, data?: any) => void): void; + } + + // ==== _stream_writable ==== + interface CorkedRequest { + next: any; + entry: any; + finish(): void; + } + + interface BufferRequest { + chunk: any; // TODO + encoding: string; + isBuf: boolean; + callback: (error?: Error | null) => void; + next: BufferRequest | null; + } + + interface WritableStateOptions { + decodeStrings?: boolean; + defaultEncoding?: string; + highWaterMark?: number; + objectMode?: boolean; + writableObjectMode?: boolean; + writableHighWaterMark?: number; + } + + interface WritableState { + buffer: BufferRequest[]; + objectMode: boolean; + highWaterMark: number; + finalCalled: boolean; + needDrain: boolean; + ending: boolean; + ended: boolean; + finished: boolean; + destroyed: boolean; + decodeStrings: boolean; + defaultEncoding: string; + length: number; + writing: boolean; + corked: number; + sync: boolean; + bufferProcessing: boolean; + writelen: number; + pendingcb: number; + prefinished: boolean; + errorEmitted: boolean; + bufferedRequestCount: number; + writecb: ((err?: Error | null) => void) | null; + onwrite: (er?: Error | null) => any; + bufferedRequest: BufferRequest | null; + lastBufferedRequest: BufferRequest | null; + corkedRequestsFree: CorkedRequest; + + // new (options: WritableStateOptions, stream: Writable): WritableState; + + getBuffer(): BufferRequest[]; + } + + type WritableOptions = WritableStateOptions & { + write?(this: Writable, chunk: any, encoding: string, callback: (error?: Error | null) => void): void; + writev?(this: Writable, chunk: ArrayLike<{ chunk: any; encoding: string }>, callback: (error?: Error | null) => void): void; + destroy?(this: Writable, error: Error | null, callback: (error: Error | null) => void): void; + final?(this: Writable, callback: (error?: Error | null) => void): void; + }; + + class Writable extends stream.Writable { + // static WritableState: WritableState; + // private static realHasInstance: (obj: any) => boolean; + destroyed: boolean; + _writableState: WritableState; + + constructor(options?: WritableOptions); + + destroy(error?: Error, callback?: (error?: Error | null) => void): this; + _undestroy(): void; + } + + class Stream extends _Readable { + constructor(options?: ReadableOptions); + } + + // if (process.env.READABLE_STREAM === 'disable' && Stream) + let NodeBaseExport: stream.Readable & { + Readable: stream.Readable; + Writable: stream.Writable; + Duplex: stream.Duplex; + Transform: stream.Transform; + PassThrough: stream.PassThrough; + Stream: stream; + }; +} + +export = _Readable; +export as namespace _Readable; diff --git a/types/readable-stream/readable-stream-tests.ts b/types/readable-stream/readable-stream-tests.ts new file mode 100644 index 0000000000..34cdc462d5 --- /dev/null +++ b/types/readable-stream/readable-stream-tests.ts @@ -0,0 +1,50 @@ +import stream = require("stream"); +import RStream = require("readable-stream"); + +function testTypes() { + const ANY: any = null; + const _readableOpts: stream.ReadableOptions = ANY as RStream.ReadableOptions; + const _writableOpts: stream.WritableOptions = ANY as RStream.WritableOptions; + const _transformOpts: stream.TransformOptions = ANY as RStream.TransformOptions; + const _duplexOpts: stream.DuplexOptions = ANY as RStream.DuplexOptions; + + const _readable: typeof stream.Readable = RStream.Readable; + const _writable: typeof stream.Writable = RStream.Writable; + const _transform: typeof stream.Transform = RStream.Transform; + const _duplex: typeof stream.Duplex = RStream.Duplex; +} + +function test() { + const rs: stream.Stream = (null as any) as RStream.Stream; + const RS_Readable = RStream; + const RS_Writable = RStream.Writable; + const RS_Transform = RStream.Transform; + const RS_Duplex = RStream.Duplex; + + const streamR = new RS_Readable({ + objectMode: true, + read(size) { }, + destroy(error, cb) { } + }); + + streamR.once("end", () => { + process.nextTick(() => { + streamR.emit("close"); + }); + }); + + const row = null; + const i = 0; + if (streamR.push(row)) streamR.emit("result", row, i); + else streamR.emit('error', new Error("a possible exception")); // Pass on any errors + streamR.push(null); // pushing null, indicating EOF + + const streamW = new RS_Writable({ + write(chunk, enc, cb) { }, + writev(chunks, cb) { }, + destroy(error, cb) { }, + final(cb) { } + }); + streamW.write(new Buffer("test")); + streamW.emit("finish"); +} diff --git a/types/readable-stream/tsconfig.json b/types/readable-stream/tsconfig.json new file mode 100644 index 0000000000..0c769a8761 --- /dev/null +++ b/types/readable-stream/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", + "readable-stream-tests.ts" + ] +} \ No newline at end of file diff --git a/types/readable-stream/tslint.json b/types/readable-stream/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/readable-stream/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }