index.d.ts 4.1 KB

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