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