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