index.d.ts 3.9 KB

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