index.d.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /// <reference types="node" />
  2. // Note: marking anything protected or private in the exported
  3. // class will limit Minipass's ability to be used as the base
  4. // for mixin classes.
  5. import { EventEmitter } from 'events'
  6. import { Stream } from 'stream'
  7. export namespace Minipass {
  8. export type Encoding = BufferEncoding | 'buffer' | null
  9. export interface Writable extends EventEmitter {
  10. end(): any
  11. write(chunk: any, ...args: any[]): any
  12. }
  13. export interface Readable extends EventEmitter {
  14. pause(): any
  15. resume(): any
  16. pipe(): any
  17. }
  18. export type DualIterable<T> = Iterable<T> & AsyncIterable<T>
  19. export type ContiguousData =
  20. | Buffer
  21. | ArrayBufferLike
  22. | ArrayBufferView
  23. | string
  24. export type BufferOrString = Buffer | string
  25. export interface SharedOptions {
  26. async?: boolean
  27. signal?: AbortSignal
  28. }
  29. export interface StringOptions extends SharedOptions {
  30. encoding: BufferEncoding
  31. objectMode?: boolean
  32. }
  33. export interface BufferOptions extends SharedOptions {
  34. encoding?: null | 'buffer'
  35. objectMode?: boolean
  36. }
  37. export interface ObjectModeOptions extends SharedOptions {
  38. objectMode: true
  39. }
  40. export interface PipeOptions {
  41. end?: boolean
  42. proxyErrors?: boolean
  43. }
  44. export type Options<T> = T extends string
  45. ? StringOptions
  46. : T extends Buffer
  47. ? BufferOptions
  48. : ObjectModeOptions
  49. }
  50. export class Minipass<
  51. RType extends any = Buffer,
  52. WType extends any = RType extends Minipass.BufferOrString
  53. ? Minipass.ContiguousData
  54. : RType
  55. >
  56. extends Stream
  57. implements Minipass.DualIterable<RType>
  58. {
  59. static isStream(stream: any): stream is Minipass.Readable | Minipass.Writable
  60. readonly bufferLength: number
  61. readonly flowing: boolean
  62. readonly writable: boolean
  63. readonly readable: boolean
  64. readonly aborted: boolean
  65. readonly paused: boolean
  66. readonly emittedEnd: boolean
  67. readonly destroyed: boolean
  68. /**
  69. * Technically writable, but mutating it can change the type,
  70. * so is not safe to do in TypeScript.
  71. */
  72. readonly objectMode: boolean
  73. async: boolean
  74. /**
  75. * Note: encoding is not actually read-only, and setEncoding(enc)
  76. * exists. However, this type definition will insist that TypeScript
  77. * programs declare the type of a Minipass stream up front, and if
  78. * that type is string, then an encoding MUST be set in the ctor. If
  79. * the type is Buffer, then the encoding must be missing, or set to
  80. * 'buffer' or null. If the type is anything else, then objectMode
  81. * must be set in the constructor options. So there is effectively
  82. * no allowed way that a TS program can set the encoding after
  83. * construction, as doing so will destroy any hope of type safety.
  84. * TypeScript does not provide many options for changing the type of
  85. * an object at run-time, which is what changing the encoding does.
  86. */
  87. readonly encoding: Minipass.Encoding
  88. // setEncoding(encoding: Encoding): void
  89. // Options required if not reading buffers
  90. constructor(
  91. ...args: RType extends Buffer
  92. ? [] | [Minipass.Options<RType>]
  93. : [Minipass.Options<RType>]
  94. )
  95. write(chunk: WType, cb?: () => void): boolean
  96. write(chunk: WType, encoding?: Minipass.Encoding, cb?: () => void): boolean
  97. read(size?: number): RType
  98. end(cb?: () => void): this
  99. end(chunk: any, cb?: () => void): this
  100. end(chunk: any, encoding?: Minipass.Encoding, cb?: () => void): this
  101. pause(): void
  102. resume(): void
  103. promise(): Promise<void>
  104. collect(): Promise<RType[]>
  105. concat(): RType extends Minipass.BufferOrString ? Promise<RType> : never
  106. destroy(er?: any): void
  107. pipe<W extends Minipass.Writable>(dest: W, opts?: Minipass.PipeOptions): W
  108. unpipe<W extends Minipass.Writable>(dest: W): void
  109. /**
  110. * alias for on()
  111. */
  112. addEventHandler(event: string, listener: (...args: any[]) => any): this
  113. on(event: string, listener: (...args: any[]) => any): this
  114. on(event: 'data', listener: (chunk: RType) => any): this
  115. on(event: 'error', listener: (error: any) => any): this
  116. on(
  117. event:
  118. | 'readable'
  119. | 'drain'
  120. | 'resume'
  121. | 'end'
  122. | 'prefinish'
  123. | 'finish'
  124. | 'close',
  125. listener: () => any
  126. ): this
  127. [Symbol.iterator](): Generator<RType, void, void>
  128. [Symbol.asyncIterator](): AsyncGenerator<RType, void, void>
  129. }