diff --git a/src/gridfs/download.ts b/src/gridfs/download.ts index 03fd7ed8e8..f4b1f8450b 100644 --- a/src/gridfs/download.ts +++ b/src/gridfs/download.ts @@ -362,9 +362,14 @@ function init(stream: GridFSBucketReadStream): void { } } - const remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( - `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` - ); + let remainingTimeMS: number | undefined; + try { + remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( + `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` + ); + } catch (error) { + return stream.destroy(error); + } stream.s.cursor = stream.s.chunks .find(filter, { @@ -390,9 +395,17 @@ function init(stream: GridFSBucketReadStream): void { return; }; - const remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( - `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` - ); + let remainingTimeMS: number | undefined; + try { + remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( + `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` + ); + } catch (error) { + if (!stream.destroyed) + stream.destroy(error); + return; + } + findOneOptions.timeoutMS = remainingTimeMS; stream.s.files.findOne(stream.s.filter, findOneOptions).then(handleReadResult, error => { diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index e3e7ccf4af..9a499799db 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -171,7 +171,7 @@ export class GridFSBucketWriteStream extends Writable { } ); } else { - callback(); + return process.nextTick(callback); } }