You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stream.d.ts 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. declare module "stream" {
  2. import * as events from "events";
  3. class internal extends events.EventEmitter {
  4. pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean; }): T;
  5. }
  6. namespace internal {
  7. class Stream extends internal { }
  8. interface ReadableOptions {
  9. highWaterMark?: number;
  10. encoding?: string;
  11. objectMode?: boolean;
  12. read?(this: Readable, size: number): void;
  13. destroy?(this: Readable, error: Error | null, callback: (error: Error | null) => void): void;
  14. autoDestroy?: boolean;
  15. }
  16. class Readable extends Stream implements NodeJS.ReadableStream {
  17. readable: boolean;
  18. readonly readableHighWaterMark: number;
  19. readonly readableLength: number;
  20. constructor(opts?: ReadableOptions);
  21. _read(size: number): void;
  22. read(size?: number): any;
  23. setEncoding(encoding: string): this;
  24. pause(): this;
  25. resume(): this;
  26. isPaused(): boolean;
  27. unpipe(destination?: NodeJS.WritableStream): this;
  28. unshift(chunk: any): void;
  29. wrap(oldStream: NodeJS.ReadableStream): this;
  30. push(chunk: any, encoding?: string): boolean;
  31. _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
  32. destroy(error?: Error): void;
  33. /**
  34. * Event emitter
  35. * The defined events on documents including:
  36. * 1. close
  37. * 2. data
  38. * 3. end
  39. * 4. readable
  40. * 5. error
  41. */
  42. addListener(event: "close", listener: () => void): this;
  43. addListener(event: "data", listener: (chunk: any) => void): this;
  44. addListener(event: "end", listener: () => void): this;
  45. addListener(event: "readable", listener: () => void): this;
  46. addListener(event: "error", listener: (err: Error) => void): this;
  47. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  48. emit(event: "close"): boolean;
  49. emit(event: "data", chunk: any): boolean;
  50. emit(event: "end"): boolean;
  51. emit(event: "readable"): boolean;
  52. emit(event: "error", err: Error): boolean;
  53. emit(event: string | symbol, ...args: any[]): boolean;
  54. on(event: "close", listener: () => void): this;
  55. on(event: "data", listener: (chunk: any) => void): this;
  56. on(event: "end", listener: () => void): this;
  57. on(event: "readable", listener: () => void): this;
  58. on(event: "error", listener: (err: Error) => void): this;
  59. on(event: string | symbol, listener: (...args: any[]) => void): this;
  60. once(event: "close", listener: () => void): this;
  61. once(event: "data", listener: (chunk: any) => void): this;
  62. once(event: "end", listener: () => void): this;
  63. once(event: "readable", listener: () => void): this;
  64. once(event: "error", listener: (err: Error) => void): this;
  65. once(event: string | symbol, listener: (...args: any[]) => void): this;
  66. prependListener(event: "close", listener: () => void): this;
  67. prependListener(event: "data", listener: (chunk: any) => void): this;
  68. prependListener(event: "end", listener: () => void): this;
  69. prependListener(event: "readable", listener: () => void): this;
  70. prependListener(event: "error", listener: (err: Error) => void): this;
  71. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  72. prependOnceListener(event: "close", listener: () => void): this;
  73. prependOnceListener(event: "data", listener: (chunk: any) => void): this;
  74. prependOnceListener(event: "end", listener: () => void): this;
  75. prependOnceListener(event: "readable", listener: () => void): this;
  76. prependOnceListener(event: "error", listener: (err: Error) => void): this;
  77. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  78. removeListener(event: "close", listener: () => void): this;
  79. removeListener(event: "data", listener: (chunk: any) => void): this;
  80. removeListener(event: "end", listener: () => void): this;
  81. removeListener(event: "readable", listener: () => void): this;
  82. removeListener(event: "error", listener: (err: Error) => void): this;
  83. removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
  84. [Symbol.asyncIterator](): AsyncIterableIterator<any>;
  85. }
  86. interface WritableOptions {
  87. highWaterMark?: number;
  88. decodeStrings?: boolean;
  89. defaultEncoding?: string;
  90. objectMode?: boolean;
  91. emitClose?: boolean;
  92. write?(this: Writable, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  93. writev?(this: Writable, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
  94. destroy?(this: Writable, error: Error | null, callback: (error: Error | null) => void): void;
  95. final?(this: Writable, callback: (error?: Error | null) => void): void;
  96. autoDestroy?: boolean;
  97. }
  98. class Writable extends Stream implements NodeJS.WritableStream {
  99. writable: boolean;
  100. readonly writableHighWaterMark: number;
  101. readonly writableLength: number;
  102. constructor(opts?: WritableOptions);
  103. _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  104. _writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
  105. _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
  106. _final(callback: (error?: Error | null) => void): void;
  107. write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
  108. write(chunk: any, encoding: string, cb?: (error: Error | null | undefined) => void): boolean;
  109. setDefaultEncoding(encoding: string): this;
  110. end(cb?: () => void): void;
  111. end(chunk: any, cb?: () => void): void;
  112. end(chunk: any, encoding: string, cb?: () => void): void;
  113. cork(): void;
  114. uncork(): void;
  115. destroy(error?: Error): void;
  116. /**
  117. * Event emitter
  118. * The defined events on documents including:
  119. * 1. close
  120. * 2. drain
  121. * 3. error
  122. * 4. finish
  123. * 5. pipe
  124. * 6. unpipe
  125. */
  126. addListener(event: "close", listener: () => void): this;
  127. addListener(event: "drain", listener: () => void): this;
  128. addListener(event: "error", listener: (err: Error) => void): this;
  129. addListener(event: "finish", listener: () => void): this;
  130. addListener(event: "pipe", listener: (src: Readable) => void): this;
  131. addListener(event: "unpipe", listener: (src: Readable) => void): this;
  132. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  133. emit(event: "close"): boolean;
  134. emit(event: "drain"): boolean;
  135. emit(event: "error", err: Error): boolean;
  136. emit(event: "finish"): boolean;
  137. emit(event: "pipe", src: Readable): boolean;
  138. emit(event: "unpipe", src: Readable): boolean;
  139. emit(event: string | symbol, ...args: any[]): boolean;
  140. on(event: "close", listener: () => void): this;
  141. on(event: "drain", listener: () => void): this;
  142. on(event: "error", listener: (err: Error) => void): this;
  143. on(event: "finish", listener: () => void): this;
  144. on(event: "pipe", listener: (src: Readable) => void): this;
  145. on(event: "unpipe", listener: (src: Readable) => void): this;
  146. on(event: string | symbol, listener: (...args: any[]) => void): this;
  147. once(event: "close", listener: () => void): this;
  148. once(event: "drain", listener: () => void): this;
  149. once(event: "error", listener: (err: Error) => void): this;
  150. once(event: "finish", listener: () => void): this;
  151. once(event: "pipe", listener: (src: Readable) => void): this;
  152. once(event: "unpipe", listener: (src: Readable) => void): this;
  153. once(event: string | symbol, listener: (...args: any[]) => void): this;
  154. prependListener(event: "close", listener: () => void): this;
  155. prependListener(event: "drain", listener: () => void): this;
  156. prependListener(event: "error", listener: (err: Error) => void): this;
  157. prependListener(event: "finish", listener: () => void): this;
  158. prependListener(event: "pipe", listener: (src: Readable) => void): this;
  159. prependListener(event: "unpipe", listener: (src: Readable) => void): this;
  160. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  161. prependOnceListener(event: "close", listener: () => void): this;
  162. prependOnceListener(event: "drain", listener: () => void): this;
  163. prependOnceListener(event: "error", listener: (err: Error) => void): this;
  164. prependOnceListener(event: "finish", listener: () => void): this;
  165. prependOnceListener(event: "pipe", listener: (src: Readable) => void): this;
  166. prependOnceListener(event: "unpipe", listener: (src: Readable) => void): this;
  167. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  168. removeListener(event: "close", listener: () => void): this;
  169. removeListener(event: "drain", listener: () => void): this;
  170. removeListener(event: "error", listener: (err: Error) => void): this;
  171. removeListener(event: "finish", listener: () => void): this;
  172. removeListener(event: "pipe", listener: (src: Readable) => void): this;
  173. removeListener(event: "unpipe", listener: (src: Readable) => void): this;
  174. removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
  175. }
  176. interface DuplexOptions extends ReadableOptions, WritableOptions {
  177. allowHalfOpen?: boolean;
  178. readableObjectMode?: boolean;
  179. writableObjectMode?: boolean;
  180. read?(this: Duplex, size: number): void;
  181. write?(this: Duplex, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  182. writev?(this: Duplex, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
  183. final?(this: Duplex, callback: (error?: Error | null) => void): void;
  184. destroy?(this: Duplex, error: Error | null, callback: (error: Error | null) => void): void;
  185. }
  186. // Note: Duplex extends both Readable and Writable.
  187. class Duplex extends Readable implements Writable {
  188. writable: boolean;
  189. readonly writableHighWaterMark: number;
  190. readonly writableLength: number;
  191. constructor(opts?: DuplexOptions);
  192. _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  193. _writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
  194. _destroy(error: Error | null, callback: (error: Error | null) => void): void;
  195. _final(callback: (error?: Error | null) => void): void;
  196. write(chunk: any, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;
  197. write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
  198. setDefaultEncoding(encoding: string): this;
  199. end(cb?: () => void): void;
  200. end(chunk: any, cb?: () => void): void;
  201. end(chunk: any, encoding?: string, cb?: () => void): void;
  202. cork(): void;
  203. uncork(): void;
  204. }
  205. type TransformCallback = (error?: Error | null, data?: any) => void;
  206. interface TransformOptions extends DuplexOptions {
  207. read?(this: Transform, size: number): void;
  208. write?(this: Transform, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  209. writev?(this: Transform, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
  210. final?(this: Transform, callback: (error?: Error | null) => void): void;
  211. destroy?(this: Transform, error: Error | null, callback: (error: Error | null) => void): void;
  212. transform?(this: Transform, chunk: any, encoding: string, callback: TransformCallback): void;
  213. flush?(this: Transform, callback: TransformCallback): void;
  214. }
  215. class Transform extends Duplex {
  216. constructor(opts?: TransformOptions);
  217. _transform(chunk: any, encoding: string, callback: TransformCallback): void;
  218. _flush(callback: TransformCallback): void;
  219. }
  220. class PassThrough extends Transform { }
  221. function finished(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, callback: (err?: NodeJS.ErrnoException | null) => void): () => void;
  222. namespace finished {
  223. function __promisify__(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream): Promise<void>;
  224. }
  225. function pipeline<T extends NodeJS.WritableStream>(stream1: NodeJS.ReadableStream, stream2: T, callback?: (err: NodeJS.ErrnoException | null) => void): T;
  226. function pipeline<T extends NodeJS.WritableStream>(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: T, callback?: (err: NodeJS.ErrnoException | null) => void): T;
  227. function pipeline<T extends NodeJS.WritableStream>(
  228. stream1: NodeJS.ReadableStream,
  229. stream2: NodeJS.ReadWriteStream,
  230. stream3: NodeJS.ReadWriteStream,
  231. stream4: T,
  232. callback?: (err: NodeJS.ErrnoException | null) => void,
  233. ): T;
  234. function pipeline<T extends NodeJS.WritableStream>(
  235. stream1: NodeJS.ReadableStream,
  236. stream2: NodeJS.ReadWriteStream,
  237. stream3: NodeJS.ReadWriteStream,
  238. stream4: NodeJS.ReadWriteStream,
  239. stream5: T,
  240. callback?: (err: NodeJS.ErrnoException | null) => void,
  241. ): T;
  242. function pipeline(streams: Array<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>, callback?: (err: NodeJS.ErrnoException | null) => void): NodeJS.WritableStream;
  243. function pipeline(
  244. stream1: NodeJS.ReadableStream,
  245. stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
  246. ...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream | ((err: NodeJS.ErrnoException | null) => void)>,
  247. ): NodeJS.WritableStream;
  248. namespace pipeline {
  249. function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.WritableStream): Promise<void>;
  250. function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: NodeJS.WritableStream): Promise<void>;
  251. function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: NodeJS.ReadWriteStream, stream4: NodeJS.WritableStream): Promise<void>;
  252. function __promisify__(
  253. stream1: NodeJS.ReadableStream,
  254. stream2: NodeJS.ReadWriteStream,
  255. stream3: NodeJS.ReadWriteStream,
  256. stream4: NodeJS.ReadWriteStream,
  257. stream5: NodeJS.WritableStream,
  258. ): Promise<void>;
  259. function __promisify__(streams: Array<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>): Promise<void>;
  260. function __promisify__(
  261. stream1: NodeJS.ReadableStream,
  262. stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
  263. ...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream>,
  264. ): Promise<void>;
  265. }
  266. interface Pipe { }
  267. }
  268. export = internal;
  269. }