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) { assertType(size); }, destroy(error, cb) { assertType(error); assertType<(err: Error | null) => void>(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) { assertType(chunk); assertType(enc); assertType<(err?: Error | null) => void>(cb); }, writev(chunks, cb) { assertType>(chunks); assertType<(err?: Error | null) => void>(cb); }, destroy(error, cb) { assertType(error); assertType<(err: Error | null) => void>(cb); }, final(cb) { assertType<(err?: Error | null) => void>(cb); } }); streamW.write(new Buffer("test")); streamW.emit("finish"); const streamT = new RS_Transform({ transform(chunk, enc, cb) { assertType(chunk); assertType(enc); assertType<(err?: Error, data?: any) => void>(cb); } }); assertType(streamT.allowHalfOpen); assertType(streamT.readable); assertType(streamT.writable); streamT.unpipe(streamW); streamT._transformState.afterTransform = (err, data) => { }; const streamD = new RS_Duplex({ read(size: number) { assertType(size); }, write(chunk: any, enc: string, cb: (err?: Error | null) => void) { assertType(chunk); assertType(enc); assertType<(err?: Error | null) => void>(cb); } }); assertType(streamD.allowHalfOpen); assertType(streamD.readable); assertType(streamD.writable); streamD.pipe(streamW); rs.addListener("read", (...args: any[]) => console.log(args)); rs.emit("read", 1, 2, 3); rs.pipe(streamW); const rState = (null as any) as RStream.ReadableState; rState.buffer.push(new Buffer("buffer-write")); assertType(rState.destroyed); assertType(rState.awaitDrain); if (rState.decoder != null) { rState.decoder.write(new Buffer("decoder-write")); } const wState = (null as any) as RStream.WritableState; assertType(wState.bufferProcessing); assertType(wState.corked); const wBufNext = wState.buffer[0].next; if (wBufNext != null) { wBufNext.callback(new Error("test")); } if (wState.bufferedRequest != null) { wState.bufferedRequest.callback(null); } const rOpts: RStream.ReadableOptions = { defaultEncoding: "utf8", encoding: "utf8", highWaterMark: 100, readableHighWaterMark: 100, readableObjectMode: false, read(size: number) { } }; const wOpts: RStream.WritableOptions = { decodeStrings: true, defaultEncoding: "utf8", highWaterMark: 100, writableHighWaterMark: 100, writableObjectMode: false, write(chunk: any, enc: string, cb: (err?: Error | null) => void) { } }; } function assertType(value: T, msg?: string): T { if (!(typeof value)) throw new Error(msg); return value; }