Skip to content

Commit

Permalink
declare return type for concat(), promise(), collect()
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 11, 2023
1 parent 6baaade commit a7ba1e3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ export class Minipass<
* Return a Promise that resolves to an array of all emitted data once
* the stream ends.
*/
async collect() {
async collect(): Promise<RType[] & { dataLength: number }> {
const buf: RType[] & { dataLength: number } = Object.assign([], {
dataLength: 0,
})
Expand All @@ -1165,20 +1165,22 @@ export class Minipass<
*
* Not allowed on objectMode streams.
*/
async concat() {
async concat(): Promise<RType> {
if (this[OBJECTMODE]) {
throw new Error('cannot concat in objectMode')
}
const buf = await this.collect()
return this[ENCODING]
? buf.join('')
: Buffer.concat(buf as Buffer[], buf.dataLength)
return (
this[ENCODING]
? buf.join('')
: Buffer.concat(buf as Buffer[], buf.dataLength)
) as RType
}

/**
* Return a void Promise that resolves once the stream ends.
*/
async promise() {
async promise(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.on(DESTROYED, () => reject(new Error('stream destroyed')))
this.on('error', er => reject(er))
Expand Down

0 comments on commit a7ba1e3

Please sign in to comment.