From 185c61d90dede77db49faff19a7427af1c1f8cec Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Thu, 9 Jan 2025 09:50:53 -0300 Subject: [PATCH 01/10] Add TypeScript typings --- index.d.ts | 240 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 12 ++- 2 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..a17a7af --- /dev/null +++ b/index.d.ts @@ -0,0 +1,240 @@ +import EventEmitter from 'bare-events' +import Buffer, { BufferEncoding } from 'bare-buffer' + +// duplicated from 'bare-events' +declare interface EventMap { + [event: string | symbol]: unknown[] +} + +type StreamOptions = { + destroy?: Stream['_destroy'] + eagerOpen?: boolean + open?: Stream['_open'] + predestroy?: Stream['_predestroy'] + signal?: AbortSignal +} + +declare interface Stream + extends EventEmitter { + _open(cb: (err?: Error | null) => void): void + _destroy: ( + this: this, + err: Error | null, + cb: (err?: Error | null) => void + ) => void + _predestroy(): void + + get readable(): boolean + get writable(): boolean + get destroyed(): boolean + get destroying(): boolean + + destroy(err?: Error | null): void +} + +declare class Stream { + constructor(opts?: StreamOptions) +} + +type ReadableOptions = { + byteLength?: (data: unknown) => number + byteLengthReadable?: (data: unknown) => number + encoding?: BufferEncoding + highWaterMark?: number + map?: (data: unknown) => unknown + mapReadable?: (data: unknown) => unknown + read?: Readable['_read'] +} & StreamOptions + +declare interface Readable + extends Stream<{ + data: [data: T | null] + close: [] + end: [] + error: [err: Error] + readable: [] + piping: [dest: unknown] + }> { + _read(this: this, size: number): void + + push(chunk: T | null, encoding?: BufferEncoding): boolean + + unshift(chunk: T | null, encoding?: BufferEncoding): boolean + + read(): T | null + + resume(): this + pause(): this + + pipe(dest: A, cb?: (err: Error) => void): A + + setEncoding(encoding: BufferEncoding): void + + [Symbol.asyncIterator](): AsyncIterator +} + +declare class Readable { + static from(data: unknown | unknown[], opts?: ReadableOptions): Readable + + static isBackpressured(rs: Readable): boolean + static isPaused(rs: Readable): boolean + + constructor(opts?: ReadableOptions) +} + +type WritableOptions = { + final?: Writable['_final'] + mapWritable?: (data: unknown) => unknown + write?: Writable['_write'] + writev?: Writable['_writev'] +} & StreamOptions + +declare interface Writable + extends Stream<{ + drain: [] + finish: [] + close: [] + error: [err: Error] + pipe: [src: Readable] + }> { + readonly destroyed: boolean + + _writev(this: this, batch: T[], cb: (err?: Error | null) => void): void + _write( + this: this, + data: T, + encoding: BufferEncoding, + cb: (err?: Error | null) => void + ): void + _final(this: this, cb: (err?: Error | null) => void): void + + write(chunk: T, encoding?: BufferEncoding, cb?: () => void): boolean + write(chunk: T, cb?: () => void): boolean + + end(chunk: T, encoding?: BufferEncoding, cb?: () => void): this + end(chunk: T, cb?: () => void): this + end(cb?: () => void): this + + cork(): void + uncork(): void +} + +declare class Writable { + static isBackpressured(ws: Writable): Promise + static drained(ws: Writable): Promise + + constructor(opts?: WritableOptions) +} + +type DuplexOptions = ReadableOptions & WritableOptions + +declare interface Duplex + extends Stream<{ + close: [] + data: [data: T | null] + drain: [] + end: [] + error: [err: Error] + finish: [] + pipe: [src: Readable] + piping: [dest: unknown] + readable: [] + }> { + _read(this: this, size: number): void + _writev(this: this, batch: T[], cb: (err?: Error | null) => void): void + _write(this: this, data: T, cb: (err?: Error | null) => void): void + _final(this: this, cb: (err?: Error | null) => void): void + + write(chunk: T, encoding?: BufferEncoding, cb?: () => void): boolean + write(chunk: T, cb?: () => void): boolean + + end(chunk: T, encoding?: BufferEncoding, cb?: () => void): this + end(chunk: T, cb?: () => void): this + end(cb?: () => void): this + + push(chunk: T | null, encoding?: BufferEncoding): boolean + unshift(chunk: T | null, encoding?: BufferEncoding): boolean + + read(): T | null + + resume(): this + pause(): this + + pipe(dest: A, cb?: (err: Error) => void): A + + setEncoding(encoding: BufferEncoding): void + + cork(): void + uncork(): void + + [Symbol.asyncIterator](): AsyncIterator +} + +declare class Duplex { + constructor(opts?: DuplexOptions) +} + +type TransformOptions = { + flush?: Transform['_flush'] + transform?: Transform['_transform'] +} & DuplexOptions + +declare interface Transform extends Duplex { + _transform( + this: this, + data: T, + encoding: BufferEncoding, + cb: (err?: Error | null) => void + ): void + _flush(cb: (this: this, err: Error | null) => void): void +} + +declare class Transform { + constructor(opts?: TransformOptions) +} + +declare class Pipeline { + constructor(src: Stream, dst: Stream, cb: (err?: Error | null) => void) + + finished(): void + + done(stream: Stream, err: Error): void +} + +declare namespace Stream { + export { + Pipeline, + Stream, + Readable, + Writable, + Duplex, + Transform, + Transform as PassThrough + } + + export function finished( + stream: Stream, + opts: { cleanup?: boolean }, + cb: (err?: Error | null) => void + ): () => void + + export function finished( + stream: Stream, + cb: (err?: Error | null) => void + ): () => void + + export function isStream(stream: unknown): stream is Stream + + export function isEnded(stream: Stream): boolean + + export function isFinished(stream: Stream): boolean + + export function isDisturbed(stream: Stream): boolean + + export function getStreamError( + stream: Stream, + opts?: { all?: boolean } + ): Error | null +} + +export = Stream diff --git a/package.json b/package.json index 7493b73..1c3fec8 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,10 @@ "version": "2.6.1", "description": "Streaming data for JavaScript", "exports": { - ".": "./index.js", + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, "./package": "./package.json", "./promises": "./promises.js", "./web": "./web.js", @@ -11,6 +14,7 @@ }, "files": [ "index.js", + "index.d.ts", "promises.js", "web.js", "global.js" @@ -32,8 +36,14 @@ "streamx": "^2.21.0" }, "devDependencies": { + "bare-buffer": "^2.7.1", + "bare-events": "^2.5.3", "brittle": "^3.5.2", "prettier": "^3.3.3", "prettier-config-standard": "^7.0.0" + }, + "peerDependencies": { + "bare-buffer": "*", + "bare-events": "*" } } From 8304841fd40c2b10a306e34becc08559a59b47be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 12:02:15 +0100 Subject: [PATCH 02/10] Review --- index.d.ts | 252 ++++++++++++++++++++++----------------------------- index.js | 48 ++++++++-- package.json | 6 +- test.js | 2 + test/all.js | 2 - 5 files changed, 150 insertions(+), 160 deletions(-) create mode 100644 test.js delete mode 100644 test/all.js diff --git a/index.d.ts b/index.d.ts index a17a7af..0c24830 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,209 +1,174 @@ -import EventEmitter from 'bare-events' +import EventEmitter, { EventMap } from 'bare-events' import Buffer, { BufferEncoding } from 'bare-buffer' -// duplicated from 'bare-events' -declare interface EventMap { - [event: string | symbol]: unknown[] +type StreamEncoding = BufferEncoding | 'buffer' + +interface StreamCallback { + (err: Error | null): void +} + +interface StreamEvents extends EventMap { + close: [] + error: [err: Error] } -type StreamOptions = { - destroy?: Stream['_destroy'] +interface StreamOptions { eagerOpen?: boolean - open?: Stream['_open'] - predestroy?: Stream['_predestroy'] signal?: AbortSignal + open?(this: S, cb: StreamCallback): void + predestroy?(this: S): void + destroy?(this: S, err: Error | null, cb: StreamCallback): void } -declare interface Stream +interface Stream extends EventEmitter { - _open(cb: (err?: Error | null) => void): void - _destroy: ( - this: this, - err: Error | null, - cb: (err?: Error | null) => void - ) => void + _open(cb: StreamCallback): void _predestroy(): void + _destroy(err: Error | null, cb: StreamCallback): void - get readable(): boolean - get writable(): boolean - get destroyed(): boolean - get destroying(): boolean + readonly readable: boolean + readonly writable: boolean + readonly destroyed: boolean + readonly destroying: boolean destroy(err?: Error | null): void } -declare class Stream { - constructor(opts?: StreamOptions) +interface ReadableEvents extends StreamEvents { + data: [data: Buffer | string] + end: [] + readable: [] + piping: [dest: Writable] } -type ReadableOptions = { - byteLength?: (data: unknown) => number - byteLengthReadable?: (data: unknown) => number +interface ReadableOptions + extends StreamOptions { encoding?: BufferEncoding highWaterMark?: number - map?: (data: unknown) => unknown - mapReadable?: (data: unknown) => unknown - read?: Readable['_read'] -} & StreamOptions + read?(this: S, size: number): void +} -declare interface Readable - extends Stream<{ - data: [data: T | null] - close: [] - end: [] - error: [err: Error] - readable: [] - piping: [dest: unknown] - }> { - _read(this: this, size: number): void +interface Readable + extends Stream, + AsyncIterable { + _read(size: number): void - push(chunk: T | null, encoding?: BufferEncoding): boolean + push(data: string, encoding?: BufferEncoding): boolean + push(data: Buffer | null): boolean - unshift(chunk: T | null, encoding?: BufferEncoding): boolean + unshift(data: string, encoding?: BufferEncoding): boolean + unshift(data: Buffer | null): boolean - read(): T | null + read(): Buffer | string | null resume(): this pause(): this - pipe(dest: A, cb?: (err: Error) => void): A + pipe(dest: S, cb?: StreamCallback): S setEncoding(encoding: BufferEncoding): void - - [Symbol.asyncIterator](): AsyncIterator } declare class Readable { - static from(data: unknown | unknown[], opts?: ReadableOptions): Readable + constructor(opts?: ReadableOptions) + + static from( + data: (Buffer | string)[] | AsyncIterable, + opts?: ReadableOptions + ): Readable static isBackpressured(rs: Readable): boolean + static isPaused(rs: Readable): boolean +} - constructor(opts?: ReadableOptions) +interface WritableEvents extends StreamEvents { + drain: [] + finish: [] + pipe: [src: Readable] } -type WritableOptions = { - final?: Writable['_final'] - mapWritable?: (data: unknown) => unknown - write?: Writable['_write'] - writev?: Writable['_writev'] -} & StreamOptions - -declare interface Writable - extends Stream<{ - drain: [] - finish: [] - close: [] - error: [err: Error] - pipe: [src: Readable] - }> { - readonly destroyed: boolean +interface WritableOptions + extends StreamOptions { + write?( + this: S, + data: Buffer, + encoding: StreamEncoding, + cb: StreamCallback + ): void + writev?( + this: S, + batch: { chunk: Buffer; encoding: StreamEncoding }[], + cb: StreamCallback + ): void + final?(this: S, cb: StreamCallback): void +} - _writev(this: this, batch: T[], cb: (err?: Error | null) => void): void - _write( - this: this, - data: T, - encoding: BufferEncoding, - cb: (err?: Error | null) => void +interface Writable + extends Stream { + _write(data: Buffer, encoding: StreamEncoding, cb: StreamCallback): void + _writev( + batch: { chunk: Buffer; encoding: StreamEncoding }[], + cb: StreamCallback ): void - _final(this: this, cb: (err?: Error | null) => void): void + _final(cb: StreamCallback): void + + readonly destroyed: boolean - write(chunk: T, encoding?: BufferEncoding, cb?: () => void): boolean - write(chunk: T, cb?: () => void): boolean + write(data: string, encoding?: BufferEncoding, cb?: StreamCallback): boolean + write(data: Buffer, cb?: StreamCallback): boolean - end(chunk: T, encoding?: BufferEncoding, cb?: () => void): this - end(chunk: T, cb?: () => void): this - end(cb?: () => void): this + end(cb?: StreamCallback): this + end(data: string, encoding?: BufferEncoding, cb?: StreamCallback): this + end(data: Buffer, cb?: StreamCallback): this cork(): void uncork(): void } declare class Writable { - static isBackpressured(ws: Writable): Promise - static drained(ws: Writable): Promise - constructor(opts?: WritableOptions) -} - -type DuplexOptions = ReadableOptions & WritableOptions - -declare interface Duplex - extends Stream<{ - close: [] - data: [data: T | null] - drain: [] - end: [] - error: [err: Error] - finish: [] - pipe: [src: Readable] - piping: [dest: unknown] - readable: [] - }> { - _read(this: this, size: number): void - _writev(this: this, batch: T[], cb: (err?: Error | null) => void): void - _write(this: this, data: T, cb: (err?: Error | null) => void): void - _final(this: this, cb: (err?: Error | null) => void): void - write(chunk: T, encoding?: BufferEncoding, cb?: () => void): boolean - write(chunk: T, cb?: () => void): boolean + static isBackpressured(ws: Writable): boolean - end(chunk: T, encoding?: BufferEncoding, cb?: () => void): this - end(chunk: T, cb?: () => void): this - end(cb?: () => void): this - - push(chunk: T | null, encoding?: BufferEncoding): boolean - unshift(chunk: T | null, encoding?: BufferEncoding): boolean - - read(): T | null - - resume(): this - pause(): this + static drained(ws: Writable): Promise +} - pipe(dest: A, cb?: (err: Error) => void): A +interface DuplexEvents extends ReadableEvents, WritableEvents {} - setEncoding(encoding: BufferEncoding): void +interface DuplexOptions + extends ReadableOptions, + WritableOptions {} - cork(): void - uncork(): void +interface Duplex + extends Readable, + Writable {} - [Symbol.asyncIterator](): AsyncIterator -} +interface TransformEvents extends DuplexEvents {} -declare class Duplex { - constructor(opts?: DuplexOptions) +interface TransformOptions + extends DuplexOptions { + transform?( + this: S, + data: Buffer, + encoding: StreamEncoding, + cb: StreamCallback + ): void + flush?(this: S, cb: StreamCallback): void } -type TransformOptions = { - flush?: Transform['_flush'] - transform?: Transform['_transform'] -} & DuplexOptions - -declare interface Transform extends Duplex { - _transform( - this: this, - data: T, - encoding: BufferEncoding, - cb: (err?: Error | null) => void - ): void - _flush(cb: (this: this, err: Error | null) => void): void +interface Transform + extends Duplex { + _transform(data: Buffer, encoding: StreamEncoding, cb: StreamCallback): void + _flush(cb: StreamCallback): void } declare class Transform { constructor(opts?: TransformOptions) } -declare class Pipeline { - constructor(src: Stream, dst: Stream, cb: (err?: Error | null) => void) - - finished(): void - - done(stream: Stream, err: Error): void -} - declare namespace Stream { export { - Pipeline, Stream, Readable, Writable, @@ -215,13 +180,10 @@ declare namespace Stream { export function finished( stream: Stream, opts: { cleanup?: boolean }, - cb: (err?: Error | null) => void + cb: StreamCallback ): () => void - export function finished( - stream: Stream, - cb: (err?: Error | null) => void - ): () => void + export function finished(stream: Stream, cb: Stream): () => void export function isStream(stream: unknown): stream is Stream diff --git a/index.js b/index.js index 1585b90..db40c25 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,13 @@ exports.Stream = exports exports.Readable = class Readable extends stream.Readable { constructor(opts = {}) { - super(opts) + super({ + ...opts, + byteLength: null, + byteLengthReadable: null, + map: null, + mapReadable: null + }) if (this._construct) this._open = this._construct @@ -49,7 +55,13 @@ exports.Readable = class Readable extends stream.Readable { exports.Writable = class Writable extends stream.Writable { constructor(opts = {}) { - super({ ...opts, byteLengthWritable }) + super({ + ...opts, + byteLength: null, + byteLengthWritable, + map: null, + mapWritable: null + }) if (this._construct) this._open = this._construct @@ -77,7 +89,7 @@ exports.Writable = class Writable extends stream.Writable { const result = super.write({ chunk, encoding }) - if (cb) stream.Writable.drained(this).then(cb, cb) + if (cb) stream.Writable.drained(this).then(() => cb(null), cb) return result } @@ -103,7 +115,7 @@ exports.Writable = class Writable extends stream.Writable { ? super.end({ chunk, encoding }) : super.end() - if (cb) this.once('end', cb) + if (cb) this.once('end', () => cb(null)) return result } @@ -111,7 +123,15 @@ exports.Writable = class Writable extends stream.Writable { exports.Duplex = class Duplex extends stream.Duplex { constructor(opts = {}) { - super({ ...opts, byteLengthWritable }) + super({ + ...opts, + byteLength: null, + byteLengthReadable: null, + byteLengthWritable, + map: null, + mapReadable: null, + mapWritable: null + }) if (this._construct) this._open = this._construct @@ -159,7 +179,7 @@ exports.Duplex = class Duplex extends stream.Duplex { const result = super.write({ chunk, encoding }) - if (cb) stream.Writable.drained(this).then(cb, cb) + if (cb) stream.Writable.drained(this).then(() => cb(null), cb) return result } @@ -185,7 +205,7 @@ exports.Duplex = class Duplex extends stream.Duplex { ? super.end({ chunk, encoding }) : super.end() - if (cb) this.once('end', cb) + if (cb) this.once('end', () => cb(null)) return result } @@ -193,7 +213,15 @@ exports.Duplex = class Duplex extends stream.Duplex { exports.Transform = class Transform extends stream.Transform { constructor(opts = {}) { - super({ ...opts, byteLengthWritable }) + super({ + ...opts, + byteLength: null, + byteLengthReadable: null, + byteLengthWritable, + map: null, + mapReadable: null, + mapWritable: null + }) if (this._transform !== stream.Transform.prototype._transform) { this._transform = transform.bind(this, this._transform) @@ -233,7 +261,7 @@ exports.Transform = class Transform extends stream.Transform { const result = super.write({ chunk, encoding }) - if (cb) stream.Writable.drained(this).then(cb, cb) + if (cb) stream.Writable.drained(this).then(() => cb(null), cb) return result } @@ -259,7 +287,7 @@ exports.Transform = class Transform extends stream.Transform { ? super.end({ chunk, encoding }) : super.end() - if (cb) this.once('end', cb) + if (cb) this.once('end', () => cb(null)) return result } diff --git a/package.json b/package.json index 1c3fec8..69eacb0 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "global.js" ], "scripts": { - "test": "prettier . --check && bare test/all.js" + "test": "prettier . --check && bare test.js" }, "repository": { "type": "git", @@ -36,8 +36,8 @@ "streamx": "^2.21.0" }, "devDependencies": { - "bare-buffer": "^2.7.1", - "bare-events": "^2.5.3", + "bare-buffer": "^3.0.0", + "bare-events": "^2.5.4", "brittle": "^3.5.2", "prettier": "^3.3.3", "prettier-config-standard": "^7.0.0" diff --git a/test.js b/test.js new file mode 100644 index 0000000..73f59bf --- /dev/null +++ b/test.js @@ -0,0 +1,2 @@ +require('./test/basic') +require('./test/web') diff --git a/test/all.js b/test/all.js deleted file mode 100644 index 1075a78..0000000 --- a/test/all.js +++ /dev/null @@ -1,2 +0,0 @@ -require('./basic') -require('./web') From 90e1f4c22fbbad93f0c3a22b1158be966a24d75e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 12:03:43 +0100 Subject: [PATCH 03/10] Tweak --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 0c24830..f3ba16e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -74,7 +74,7 @@ declare class Readable { static from( data: (Buffer | string)[] | AsyncIterable, - opts?: ReadableOptions + opts?: ReadableOptions ): Readable static isBackpressured(rs: Readable): boolean From ee5954cb0bf163db3efcbd9e840a1bb14ae33afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 12:04:29 +0100 Subject: [PATCH 04/10] Declare `Stream` class --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index f3ba16e..ed40373 100644 --- a/index.d.ts +++ b/index.d.ts @@ -34,6 +34,8 @@ interface Stream destroy(err?: Error | null): void } +declare class Stream {} + interface ReadableEvents extends StreamEvents { data: [data: Buffer | string] end: [] From 60903f18c1b73b96a8a2eaa7ccaff710ba219032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 12:27:01 +0100 Subject: [PATCH 05/10] Add missing class extensions --- index.d.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index ed40373..e517b98 100644 --- a/index.d.ts +++ b/index.d.ts @@ -71,7 +71,9 @@ interface Readable setEncoding(encoding: BufferEncoding): void } -declare class Readable { +declare class Readable< + M extends ReadableEvents = ReadableEvents +> extends Stream { constructor(opts?: ReadableOptions) static from( @@ -128,7 +130,9 @@ interface Writable uncork(): void } -declare class Writable { +declare class Writable< + M extends WritableEvents = WritableEvents +> extends Stream { constructor(opts?: WritableOptions) static isBackpressured(ws: Writable): boolean @@ -146,6 +150,8 @@ interface Duplex extends Readable, Writable {} +declare class Duplex extends Stream {} + interface TransformEvents extends DuplexEvents {} interface TransformOptions @@ -165,7 +171,9 @@ interface Transform _flush(cb: StreamCallback): void } -declare class Transform { +declare class Transform< + M extends TransformEvents = TransformEvents +> extends Duplex { constructor(opts?: TransformOptions) } From 00d9c96f374b8c197c19dc703f4838676a2ff6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 12:27:48 +0100 Subject: [PATCH 06/10] Add missing constructor --- index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index e517b98..008d067 100644 --- a/index.d.ts +++ b/index.d.ts @@ -150,7 +150,9 @@ interface Duplex extends Readable, Writable {} -declare class Duplex extends Stream {} +declare class Duplex extends Stream { + constructor(opts?: DuplexOptions) +} interface TransformEvents extends DuplexEvents {} From cc1d9932042017d9adff7dc8637dc112899f2b50 Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Fri, 10 Jan 2025 14:22:52 -0300 Subject: [PATCH 07/10] Add `pipeline` typings --- index.d.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.d.ts b/index.d.ts index 008d067..3ef4ad6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -189,6 +189,14 @@ declare namespace Stream { Transform as PassThrough } + export function pipeline(streams: Stream[], done?: StreamCallback): Stream + + export function pipeline( + ...args: [stream: Stream, ...streams: Stream[], done: StreamCallback] + ): Stream + + export function pipeline(stream: Stream, ...streams: Stream[]): Stream + export function finished( stream: Stream, opts: { cleanup?: boolean }, From 49e535f43a70f70d7745e4a0f10a3b59ddd464b5 Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Fri, 10 Jan 2025 14:24:02 -0300 Subject: [PATCH 08/10] Adjustments --- index.d.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3ef4ad6..2b6f50e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -77,7 +77,10 @@ declare class Readable< constructor(opts?: ReadableOptions) static from( - data: (Buffer | string)[] | AsyncIterable, + data: + | (Buffer | string) + | (Buffer | string)[] + | AsyncIterable, opts?: ReadableOptions ): Readable @@ -203,7 +206,7 @@ declare namespace Stream { cb: StreamCallback ): () => void - export function finished(stream: Stream, cb: Stream): () => void + export function finished(stream: Stream, cb: StreamCallback): () => void export function isStream(stream: unknown): stream is Stream From 2e82db8b1534f699476eaf7d3acf16291a1c0356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 18:41:23 +0100 Subject: [PATCH 09/10] Add `Pipeline` helper type --- index.d.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2b6f50e..9b38a35 100644 --- a/index.d.ts +++ b/index.d.ts @@ -182,6 +182,12 @@ declare class Transform< constructor(opts?: TransformOptions) } +type Pipeline = [ + src: Readable, + ...transforms: Duplex[], + dest: S +] + declare namespace Stream { export { Stream, @@ -192,13 +198,16 @@ declare namespace Stream { Transform as PassThrough } - export function pipeline(streams: Stream[], done?: StreamCallback): Stream + export function pipeline( + streams: Pipeline, + cb?: StreamCallback + ): S - export function pipeline( - ...args: [stream: Stream, ...streams: Stream[], done: StreamCallback] - ): Stream + export function pipeline(...args: [...Pipeline]): S - export function pipeline(stream: Stream, ...streams: Stream[]): Stream + export function pipeline( + ...args: [...Pipeline, cb: StreamCallback] + ): S export function finished( stream: Stream, From f9f5a01c270ff00c6228b9176040553082a374bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 10 Jan 2025 18:45:03 +0100 Subject: [PATCH 10/10] Remove unneeded spread --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 9b38a35..814ef5e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -203,7 +203,7 @@ declare namespace Stream { cb?: StreamCallback ): S - export function pipeline(...args: [...Pipeline]): S + export function pipeline(...args: Pipeline): S export function pipeline( ...args: [...Pipeline, cb: StreamCallback]