index.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. /// <reference types="node" />
  5. import { EventEmitter } from 'node:events';
  6. import { StringDecoder } from 'node:string_decoder';
  7. /**
  8. * Same as StringDecoder, but exposing the `lastNeed` flag on the type
  9. */
  10. type SD = StringDecoder & {
  11. lastNeed: boolean;
  12. };
  13. export type { SD, Pipe, PipeProxyErrors };
  14. /**
  15. * Return true if the argument is a Minipass stream, Node stream, or something
  16. * else that Minipass can interact with.
  17. */
  18. export declare const isStream: (s: any) => s is NodeJS.WriteStream | NodeJS.ReadStream | Minipass<any, any, any> | (NodeJS.ReadStream & {
  19. fd: number;
  20. }) | (EventEmitter & {
  21. pause(): any;
  22. resume(): any;
  23. pipe(...destArgs: any[]): any;
  24. }) | (NodeJS.WriteStream & {
  25. fd: number;
  26. }) | (EventEmitter & {
  27. end(): any;
  28. write(chunk: any, ...args: any[]): any;
  29. });
  30. /**
  31. * Return true if the argument is a valid {@link Minipass.Readable}
  32. */
  33. export declare const isReadable: (s: any) => s is Minipass.Readable;
  34. /**
  35. * Return true if the argument is a valid {@link Minipass.Writable}
  36. */
  37. export declare const isWritable: (s: any) => s is Minipass.Readable;
  38. declare const EOF: unique symbol;
  39. declare const MAYBE_EMIT_END: unique symbol;
  40. declare const EMITTED_END: unique symbol;
  41. declare const EMITTING_END: unique symbol;
  42. declare const EMITTED_ERROR: unique symbol;
  43. declare const CLOSED: unique symbol;
  44. declare const READ: unique symbol;
  45. declare const FLUSH: unique symbol;
  46. declare const FLUSHCHUNK: unique symbol;
  47. declare const ENCODING: unique symbol;
  48. declare const DECODER: unique symbol;
  49. declare const FLOWING: unique symbol;
  50. declare const PAUSED: unique symbol;
  51. declare const RESUME: unique symbol;
  52. declare const BUFFER: unique symbol;
  53. declare const PIPES: unique symbol;
  54. declare const BUFFERLENGTH: unique symbol;
  55. declare const BUFFERPUSH: unique symbol;
  56. declare const BUFFERSHIFT: unique symbol;
  57. declare const OBJECTMODE: unique symbol;
  58. declare const DESTROYED: unique symbol;
  59. declare const ERROR: unique symbol;
  60. declare const EMITDATA: unique symbol;
  61. declare const EMITEND: unique symbol;
  62. declare const EMITEND2: unique symbol;
  63. declare const ASYNC: unique symbol;
  64. declare const ABORT: unique symbol;
  65. declare const ABORTED: unique symbol;
  66. declare const SIGNAL: unique symbol;
  67. declare const DATALISTENERS: unique symbol;
  68. declare const DISCARDED: unique symbol;
  69. /**
  70. * Options that may be passed to stream.pipe()
  71. */
  72. export interface PipeOptions {
  73. /**
  74. * end the destination stream when the source stream ends
  75. */
  76. end?: boolean;
  77. /**
  78. * proxy errors from the source stream to the destination stream
  79. */
  80. proxyErrors?: boolean;
  81. }
  82. /**
  83. * Internal class representing a pipe to a destination stream.
  84. *
  85. * @internal
  86. */
  87. declare class Pipe<T extends unknown> {
  88. src: Minipass<T>;
  89. dest: Minipass<any, T>;
  90. opts: PipeOptions;
  91. ondrain: () => any;
  92. constructor(src: Minipass<T>, dest: Minipass.Writable, opts: PipeOptions);
  93. unpipe(): void;
  94. proxyErrors(_er: any): void;
  95. end(): void;
  96. }
  97. /**
  98. * Internal class representing a pipe to a destination stream where
  99. * errors are proxied.
  100. *
  101. * @internal
  102. */
  103. declare class PipeProxyErrors<T> extends Pipe<T> {
  104. unpipe(): void;
  105. constructor(src: Minipass<T>, dest: Minipass.Writable, opts: PipeOptions);
  106. }
  107. export declare namespace Minipass {
  108. /**
  109. * Encoding used to create a stream that outputs strings rather than
  110. * Buffer objects.
  111. */
  112. export type Encoding = BufferEncoding | 'buffer' | null;
  113. /**
  114. * Any stream that Minipass can pipe into
  115. */
  116. export type Writable = Minipass<any, any, any> | NodeJS.WriteStream | (NodeJS.WriteStream & {
  117. fd: number;
  118. }) | (EventEmitter & {
  119. end(): any;
  120. write(chunk: any, ...args: any[]): any;
  121. });
  122. /**
  123. * Any stream that can be read from
  124. */
  125. export type Readable = Minipass<any, any, any> | NodeJS.ReadStream | (NodeJS.ReadStream & {
  126. fd: number;
  127. }) | (EventEmitter & {
  128. pause(): any;
  129. resume(): any;
  130. pipe(...destArgs: any[]): any;
  131. });
  132. /**
  133. * Utility type that can be iterated sync or async
  134. */
  135. export type DualIterable<T> = Iterable<T> & AsyncIterable<T>;
  136. type EventArguments = Record<string | symbol, unknown[]>;
  137. /**
  138. * The listing of events that a Minipass class can emit.
  139. * Extend this when extending the Minipass class, and pass as
  140. * the third template argument. The key is the name of the event,
  141. * and the value is the argument list.
  142. *
  143. * Any undeclared events will still be allowed, but the handler will get
  144. * arguments as `unknown[]`.
  145. */
  146. export interface Events<RType extends any = Buffer> extends EventArguments {
  147. readable: [];
  148. data: [chunk: RType];
  149. error: [er: unknown];
  150. abort: [reason: unknown];
  151. drain: [];
  152. resume: [];
  153. end: [];
  154. finish: [];
  155. prefinish: [];
  156. close: [];
  157. [DESTROYED]: [er?: unknown];
  158. [ERROR]: [er: unknown];
  159. }
  160. /**
  161. * String or buffer-like data that can be joined and sliced
  162. */
  163. export type ContiguousData = Buffer | ArrayBufferLike | ArrayBufferView | string;
  164. export type BufferOrString = Buffer | string;
  165. /**
  166. * Options passed to the Minipass constructor.
  167. */
  168. export type SharedOptions = {
  169. /**
  170. * Defer all data emission and other events until the end of the
  171. * current tick, similar to Node core streams
  172. */
  173. async?: boolean;
  174. /**
  175. * A signal which will abort the stream
  176. */
  177. signal?: AbortSignal;
  178. /**
  179. * Output string encoding. Set to `null` or `'buffer'` (or omit) to
  180. * emit Buffer objects rather than strings.
  181. *
  182. * Conflicts with `objectMode`
  183. */
  184. encoding?: BufferEncoding | null | 'buffer';
  185. /**
  186. * Output data exactly as it was written, supporting non-buffer/string
  187. * data (such as arbitrary objects, falsey values, etc.)
  188. *
  189. * Conflicts with `encoding`
  190. */
  191. objectMode?: boolean;
  192. };
  193. /**
  194. * Options for a string encoded output
  195. */
  196. export type EncodingOptions = SharedOptions & {
  197. encoding: BufferEncoding;
  198. objectMode?: false;
  199. };
  200. /**
  201. * Options for contiguous data buffer output
  202. */
  203. export type BufferOptions = SharedOptions & {
  204. encoding?: null | 'buffer';
  205. objectMode?: false;
  206. };
  207. /**
  208. * Options for objectMode arbitrary output
  209. */
  210. export type ObjectModeOptions = SharedOptions & {
  211. objectMode: true;
  212. encoding?: null;
  213. };
  214. /**
  215. * Utility type to determine allowed options based on read type
  216. */
  217. export type Options<T> = ObjectModeOptions | (T extends string ? EncodingOptions : T extends Buffer ? BufferOptions : SharedOptions);
  218. export {};
  219. }
  220. /**
  221. * Main export, the Minipass class
  222. *
  223. * `RType` is the type of data emitted, defaults to Buffer
  224. *
  225. * `WType` is the type of data to be written, if RType is buffer or string,
  226. * then any {@link Minipass.ContiguousData} is allowed.
  227. *
  228. * `Events` is the set of event handler signatures that this object
  229. * will emit, see {@link Minipass.Events}
  230. */
  231. export declare class Minipass<RType extends unknown = Buffer, WType extends unknown = RType extends Minipass.BufferOrString ? Minipass.ContiguousData : RType, Events extends Minipass.Events<RType> = Minipass.Events<RType>> extends EventEmitter implements Minipass.DualIterable<RType> {
  232. [FLOWING]: boolean;
  233. [PAUSED]: boolean;
  234. [PIPES]: Pipe<RType>[];
  235. [BUFFER]: RType[];
  236. [OBJECTMODE]: boolean;
  237. [ENCODING]: BufferEncoding | null;
  238. [ASYNC]: boolean;
  239. [DECODER]: SD | null;
  240. [EOF]: boolean;
  241. [EMITTED_END]: boolean;
  242. [EMITTING_END]: boolean;
  243. [CLOSED]: boolean;
  244. [EMITTED_ERROR]: unknown;
  245. [BUFFERLENGTH]: number;
  246. [DESTROYED]: boolean;
  247. [SIGNAL]?: AbortSignal;
  248. [ABORTED]: boolean;
  249. [DATALISTENERS]: number;
  250. [DISCARDED]: boolean;
  251. /**
  252. * true if the stream can be written
  253. */
  254. writable: boolean;
  255. /**
  256. * true if the stream can be read
  257. */
  258. readable: boolean;
  259. /**
  260. * If `RType` is Buffer, then options do not need to be provided.
  261. * Otherwise, an options object must be provided to specify either
  262. * {@link Minipass.SharedOptions.objectMode} or
  263. * {@link Minipass.SharedOptions.encoding}, as appropriate.
  264. */
  265. constructor(...args: [Minipass.ObjectModeOptions] | (RType extends Buffer ? [] | [Minipass.Options<RType>] : [Minipass.Options<RType>]));
  266. /**
  267. * The amount of data stored in the buffer waiting to be read.
  268. *
  269. * For Buffer strings, this will be the total byte length.
  270. * For string encoding streams, this will be the string character length,
  271. * according to JavaScript's `string.length` logic.
  272. * For objectMode streams, this is a count of the items waiting to be
  273. * emitted.
  274. */
  275. get bufferLength(): number;
  276. /**
  277. * The `BufferEncoding` currently in use, or `null`
  278. */
  279. get encoding(): BufferEncoding | null;
  280. /**
  281. * @deprecated - This is a read only property
  282. */
  283. set encoding(_enc: BufferEncoding | null);
  284. /**
  285. * @deprecated - Encoding may only be set at instantiation time
  286. */
  287. setEncoding(_enc: Minipass.Encoding): void;
  288. /**
  289. * True if this is an objectMode stream
  290. */
  291. get objectMode(): boolean;
  292. /**
  293. * @deprecated - This is a read-only property
  294. */
  295. set objectMode(_om: boolean);
  296. /**
  297. * true if this is an async stream
  298. */
  299. get ['async'](): boolean;
  300. /**
  301. * Set to true to make this stream async.
  302. *
  303. * Once set, it cannot be unset, as this would potentially cause incorrect
  304. * behavior. Ie, a sync stream can be made async, but an async stream
  305. * cannot be safely made sync.
  306. */
  307. set ['async'](a: boolean);
  308. [ABORT](): void;
  309. /**
  310. * True if the stream has been aborted.
  311. */
  312. get aborted(): boolean;
  313. /**
  314. * No-op setter. Stream aborted status is set via the AbortSignal provided
  315. * in the constructor options.
  316. */
  317. set aborted(_: boolean);
  318. /**
  319. * Write data into the stream
  320. *
  321. * If the chunk written is a string, and encoding is not specified, then
  322. * `utf8` will be assumed. If the stream encoding matches the encoding of
  323. * a written string, and the state of the string decoder allows it, then
  324. * the string will be passed through to either the output or the internal
  325. * buffer without any processing. Otherwise, it will be turned into a
  326. * Buffer object for processing into the desired encoding.
  327. *
  328. * If provided, `cb` function is called immediately before return for
  329. * sync streams, or on next tick for async streams, because for this
  330. * base class, a chunk is considered "processed" once it is accepted
  331. * and either emitted or buffered. That is, the callback does not indicate
  332. * that the chunk has been eventually emitted, though of course child
  333. * classes can override this function to do whatever processing is required
  334. * and call `super.write(...)` only once processing is completed.
  335. */
  336. write(chunk: WType, cb?: () => void): boolean;
  337. write(chunk: WType, encoding?: Minipass.Encoding, cb?: () => void): boolean;
  338. /**
  339. * Low-level explicit read method.
  340. *
  341. * In objectMode, the argument is ignored, and one item is returned if
  342. * available.
  343. *
  344. * `n` is the number of bytes (or in the case of encoding streams,
  345. * characters) to consume. If `n` is not provided, then the entire buffer
  346. * is returned, or `null` is returned if no data is available.
  347. *
  348. * If `n` is greater that the amount of data in the internal buffer,
  349. * then `null` is returned.
  350. */
  351. read(n?: number | null): RType | null;
  352. [READ](n: number | null, chunk: RType): RType;
  353. /**
  354. * End the stream, optionally providing a final write.
  355. *
  356. * See {@link Minipass#write} for argument descriptions
  357. */
  358. end(cb?: () => void): this;
  359. end(chunk: WType, cb?: () => void): this;
  360. end(chunk: WType, encoding?: Minipass.Encoding, cb?: () => void): this;
  361. [RESUME](): void;
  362. /**
  363. * Resume the stream if it is currently in a paused state
  364. *
  365. * If called when there are no pipe destinations or `data` event listeners,
  366. * this will place the stream in a "discarded" state, where all data will
  367. * be thrown away. The discarded state is removed if a pipe destination or
  368. * data handler is added, if pause() is called, or if any synchronous or
  369. * asynchronous iteration is started.
  370. */
  371. resume(): void;
  372. /**
  373. * Pause the stream
  374. */
  375. pause(): void;
  376. /**
  377. * true if the stream has been forcibly destroyed
  378. */
  379. get destroyed(): boolean;
  380. /**
  381. * true if the stream is currently in a flowing state, meaning that
  382. * any writes will be immediately emitted.
  383. */
  384. get flowing(): boolean;
  385. /**
  386. * true if the stream is currently in a paused state
  387. */
  388. get paused(): boolean;
  389. [BUFFERPUSH](chunk: RType): void;
  390. [BUFFERSHIFT](): RType;
  391. [FLUSH](noDrain?: boolean): void;
  392. [FLUSHCHUNK](chunk: RType): boolean;
  393. /**
  394. * Pipe all data emitted by this stream into the destination provided.
  395. *
  396. * Triggers the flow of data.
  397. */
  398. pipe<W extends Minipass.Writable>(dest: W, opts?: PipeOptions): W;
  399. /**
  400. * Fully unhook a piped destination stream.
  401. *
  402. * If the destination stream was the only consumer of this stream (ie,
  403. * there are no other piped destinations or `'data'` event listeners)
  404. * then the flow of data will stop until there is another consumer or
  405. * {@link Minipass#resume} is explicitly called.
  406. */
  407. unpipe<W extends Minipass.Writable>(dest: W): void;
  408. /**
  409. * Alias for {@link Minipass#on}
  410. */
  411. addListener<Event extends keyof Events>(ev: Event, handler: (...args: Events[Event]) => any): this;
  412. /**
  413. * Mostly identical to `EventEmitter.on`, with the following
  414. * behavior differences to prevent data loss and unnecessary hangs:
  415. *
  416. * - Adding a 'data' event handler will trigger the flow of data
  417. *
  418. * - Adding a 'readable' event handler when there is data waiting to be read
  419. * will cause 'readable' to be emitted immediately.
  420. *
  421. * - Adding an 'endish' event handler ('end', 'finish', etc.) which has
  422. * already passed will cause the event to be emitted immediately and all
  423. * handlers removed.
  424. *
  425. * - Adding an 'error' event handler after an error has been emitted will
  426. * cause the event to be re-emitted immediately with the error previously
  427. * raised.
  428. */
  429. on<Event extends keyof Events>(ev: Event, handler: (...args: Events[Event]) => any): this;
  430. /**
  431. * Alias for {@link Minipass#off}
  432. */
  433. removeListener<Event extends keyof Events>(ev: Event, handler: (...args: Events[Event]) => any): this;
  434. /**
  435. * Mostly identical to `EventEmitter.off`
  436. *
  437. * If a 'data' event handler is removed, and it was the last consumer
  438. * (ie, there are no pipe destinations or other 'data' event listeners),
  439. * then the flow of data will stop until there is another consumer or
  440. * {@link Minipass#resume} is explicitly called.
  441. */
  442. off<Event extends keyof Events>(ev: Event, handler: (...args: Events[Event]) => any): this;
  443. /**
  444. * Mostly identical to `EventEmitter.removeAllListeners`
  445. *
  446. * If all 'data' event handlers are removed, and they were the last consumer
  447. * (ie, there are no pipe destinations), then the flow of data will stop
  448. * until there is another consumer or {@link Minipass#resume} is explicitly
  449. * called.
  450. */
  451. removeAllListeners<Event extends keyof Events>(ev?: Event): this;
  452. /**
  453. * true if the 'end' event has been emitted
  454. */
  455. get emittedEnd(): boolean;
  456. [MAYBE_EMIT_END](): void;
  457. /**
  458. * Mostly identical to `EventEmitter.emit`, with the following
  459. * behavior differences to prevent data loss and unnecessary hangs:
  460. *
  461. * If the stream has been destroyed, and the event is something other
  462. * than 'close' or 'error', then `false` is returned and no handlers
  463. * are called.
  464. *
  465. * If the event is 'end', and has already been emitted, then the event
  466. * is ignored. If the stream is in a paused or non-flowing state, then
  467. * the event will be deferred until data flow resumes. If the stream is
  468. * async, then handlers will be called on the next tick rather than
  469. * immediately.
  470. *
  471. * If the event is 'close', and 'end' has not yet been emitted, then
  472. * the event will be deferred until after 'end' is emitted.
  473. *
  474. * If the event is 'error', and an AbortSignal was provided for the stream,
  475. * and there are no listeners, then the event is ignored, matching the
  476. * behavior of node core streams in the presense of an AbortSignal.
  477. *
  478. * If the event is 'finish' or 'prefinish', then all listeners will be
  479. * removed after emitting the event, to prevent double-firing.
  480. */
  481. emit<Event extends keyof Events>(ev: Event, ...args: Events[Event]): boolean;
  482. [EMITDATA](data: RType): boolean;
  483. [EMITEND](): boolean;
  484. [EMITEND2](): boolean;
  485. /**
  486. * Return a Promise that resolves to an array of all emitted data once
  487. * the stream ends.
  488. */
  489. collect(): Promise<RType[] & {
  490. dataLength: number;
  491. }>;
  492. /**
  493. * Return a Promise that resolves to the concatenation of all emitted data
  494. * once the stream ends.
  495. *
  496. * Not allowed on objectMode streams.
  497. */
  498. concat(): Promise<RType>;
  499. /**
  500. * Return a void Promise that resolves once the stream ends.
  501. */
  502. promise(): Promise<void>;
  503. /**
  504. * Asynchronous `for await of` iteration.
  505. *
  506. * This will continue emitting all chunks until the stream terminates.
  507. */
  508. [Symbol.asyncIterator](): AsyncGenerator<RType, void, void>;
  509. /**
  510. * Synchronous `for of` iteration.
  511. *
  512. * The iteration will terminate when the internal buffer runs out, even
  513. * if the stream has not yet terminated.
  514. */
  515. [Symbol.iterator](): Generator<RType, void, void>;
  516. /**
  517. * Destroy a stream, preventing it from being used for any further purpose.
  518. *
  519. * If the stream has a `close()` method, then it will be called on
  520. * destruction.
  521. *
  522. * After destruction, any attempt to write data, read data, or emit most
  523. * events will be ignored.
  524. *
  525. * If an error argument is provided, then it will be emitted in an
  526. * 'error' event.
  527. */
  528. destroy(er?: unknown): this;
  529. /**
  530. * Alias for {@link isStream}
  531. *
  532. * Former export location, maintained for backwards compatibility.
  533. *
  534. * @deprecated
  535. */
  536. static get isStream(): (s: any) => s is NodeJS.WriteStream | NodeJS.ReadStream | Minipass<any, any, any> | (NodeJS.ReadStream & {
  537. fd: number;
  538. }) | (EventEmitter & {
  539. pause(): any;
  540. resume(): any;
  541. pipe(...destArgs: any[]): any;
  542. }) | (NodeJS.WriteStream & {
  543. fd: number;
  544. }) | (EventEmitter & {
  545. end(): any;
  546. write(chunk: any, ...args: any[]): any;
  547. });
  548. }
  549. //# sourceMappingURL=index.d.ts.map