From 79748aaf50f1ada186a0296dd8ce55ccf2322275 Mon Sep 17 00:00:00 2001 From: Daria Pardue <81593090+dariakp@users.noreply.github.com> Date: Mon, 13 Sep 2021 17:22:46 -0400 Subject: [PATCH] fix(NODE-3559): incorrect GridFS stream type (#2981) --- src/gridfs/download.ts | 2 +- src/gridfs/upload.ts | 18 +++++++++++------- test/types/gridfs_types.test-d.ts | 13 +++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 test/types/gridfs_types.test-d.ts diff --git a/src/gridfs/download.ts b/src/gridfs/download.ts index ccb7b0a6fbb..7e2925bb243 100644 --- a/src/gridfs/download.ts +++ b/src/gridfs/download.ts @@ -73,7 +73,7 @@ export interface GridFSBucketReadStreamPrivate { * Do not instantiate this class directly. Use `openDownloadStream()` instead. * @public */ -export class GridFSBucketReadStream extends Readable { +export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableStream { /** @internal */ s: GridFSBucketReadStreamPrivate; diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index 5f3cad54d6b..da812994b05 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -37,7 +37,7 @@ export interface GridFSBucketWriteStreamOptions extends WriteConcernOptions { * Do not instantiate this class directly. Use `openUploadStream()` instead. * @public */ -export class GridFSBucketWriteStream extends Writable { +export class GridFSBucketWriteStream extends Writable implements NodeJS.WritableStream { bucket: GridFSBucket; chunks: Collection; filename: string; @@ -118,12 +118,16 @@ export class GridFSBucketWriteStream extends Writable { * @param callback - Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush. * @returns False if this write required flushing a chunk to MongoDB. True otherwise. */ - write(chunk: Buffer): boolean; - write(chunk: Buffer, callback: Callback): boolean; - write(chunk: Buffer, encoding: BufferEncoding | undefined): boolean; - write(chunk: Buffer, encoding: BufferEncoding | undefined, callback: Callback): boolean; + write(chunk: Buffer | string): boolean; + write(chunk: Buffer | string, callback: Callback): boolean; + write(chunk: Buffer | string, encoding: BufferEncoding | undefined): boolean; write( - chunk: Buffer, + chunk: Buffer | string, + encoding: BufferEncoding | undefined, + callback: Callback + ): boolean; + write( + chunk: Buffer | string, encodingOrCallback?: Callback | BufferEncoding, callback?: Callback ): boolean { @@ -417,7 +421,7 @@ function createFilesDoc( function doWrite( stream: GridFSBucketWriteStream, - chunk: Buffer, + chunk: Buffer | string, encoding?: BufferEncoding, callback?: Callback ): boolean { diff --git a/test/types/gridfs_types.test-d.ts b/test/types/gridfs_types.test-d.ts new file mode 100644 index 00000000000..56b90e0f49e --- /dev/null +++ b/test/types/gridfs_types.test-d.ts @@ -0,0 +1,13 @@ +import { Readable } from 'stream'; +import { expectType } from 'tsd'; +import type { GridFSBucket, GridFSBucketWriteStream } from '../../src'; + +(function test(bucket: GridFSBucket) { + const readable = new Readable(); + + const uploadStream = bucket.openUploadStream('test'); + expectType(uploadStream); + + // should be pipeable as a WriteStream + readable.pipe(uploadStream); +})({} as GridFSBucket);