diff --git a/src/gridfs/download.ts b/src/gridfs/download.ts index 5ad2d7122f9..03fd7ed8e89 100644 --- a/src/gridfs/download.ts +++ b/src/gridfs/download.ts @@ -13,7 +13,7 @@ import { import type { FindOptions } from '../operations/find'; import type { ReadPreference } from '../read_preference'; import type { Sort } from '../sort'; -import { CSOTTimeoutContext, getRemainingTimeMSOrThrow } from '../timeout'; +import { CSOTTimeoutContext } from '../timeout'; import type { Callback } from '../utils'; import type { GridFSChunk } from './upload'; @@ -204,7 +204,7 @@ export class GridFSBucketReadStream extends Readable { async abort(): Promise { this.push(null); this.destroy(); - const remainingTimeMS = getRemainingTimeMSOrThrow(this.s.timeoutContext); + const remainingTimeMS = this.s.timeoutContext?.getRemainingTimeMSOrThrow(); await this.s.cursor?.close({ timeoutMS: remainingTimeMS }); } } @@ -362,8 +362,7 @@ function init(stream: GridFSBucketReadStream): void { } } - const remainingTimeMS = getRemainingTimeMSOrThrow( - stream.s.timeoutContext, + const remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` ); @@ -391,8 +390,7 @@ function init(stream: GridFSBucketReadStream): void { return; }; - const remainingTimeMS = getRemainingTimeMSOrThrow( - stream.s.timeoutContext, + const remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` ); findOneOptions.timeoutMS = remainingTimeMS; diff --git a/src/gridfs/index.ts b/src/gridfs/index.ts index a731ff2fc0d..21306a935d5 100644 --- a/src/gridfs/index.ts +++ b/src/gridfs/index.ts @@ -6,7 +6,7 @@ import { MongoOperationTimeoutError, MongoRuntimeError } from '../error'; import { type Filter, TypedEventEmitter } from '../mongo_types'; import type { ReadPreference } from '../read_preference'; import type { Sort } from '../sort'; -import { CSOTTimeoutContext, getRemainingTimeMSOrThrow } from '../timeout'; +import { CSOTTimeoutContext } from '../timeout'; import { WriteConcern, type WriteConcernOptions } from '../write_concern'; import type { FindOptions } from './../operations/find'; import { @@ -247,8 +247,7 @@ export class GridFSBucket extends TypedEventEmitter { if (timeoutContext) { await this.s._filesCollection.drop({ timeoutMS: timeoutContext.remainingTimeMS }); - const remainingTimeMS = getRemainingTimeMSOrThrow( - timeoutContext, + const remainingTimeMS = timeoutContext.getRemainingTimeMSOrThrow( `Timed out after ${timeoutMS}ms` ); await this.s._chunksCollection.drop({ timeoutMS: remainingTimeMS }); diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index 101180269f3..e3e7ccf4af0 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -9,7 +9,7 @@ import { MongoError, MongoOperationTimeoutError } from '../error'; -import { CSOTTimeoutContext, getRemainingTimeMSOrThrow } from '../timeout'; +import { CSOTTimeoutContext } from '../timeout'; import { type Callback, squashError } from '../utils'; import type { WriteConcernOptions } from '../write_concern'; import { WriteConcern } from './../write_concern'; @@ -216,8 +216,7 @@ export class GridFSBucketWriteStream extends Writable { } this.state.aborted = true; - const remainingTimeMS = getRemainingTimeMSOrThrow( - this.timeoutContext, + const remainingTimeMS = this.timeoutContext?.getRemainingTimeMSOrThrow( `Upload timed out after ${this.timeoutContext?.timeoutMS}ms` ); await this.chunks.deleteMany({ files_id: this.id, timeoutMS: remainingTimeMS }); @@ -246,8 +245,7 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise const index = { files_id: 1, n: 1 }; let remainingTimeMS; - remainingTimeMS = getRemainingTimeMSOrThrow( - stream.timeoutContext, + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` ); @@ -276,8 +274,7 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise }); if (!hasChunksIndex) { - remainingTimeMS = getRemainingTimeMSOrThrow( - stream.timeoutContext, + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` ); await stream.chunks.createIndex(index, { @@ -341,8 +338,7 @@ function checkDone(stream: GridFSBucketWriteStream, callback: Callback): void { } async function checkIndexes(stream: GridFSBucketWriteStream): Promise { - let remainingTimeMS = getRemainingTimeMSOrThrow( - stream.timeoutContext, + let remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` ); const doc = await stream.files.findOne( @@ -361,8 +357,7 @@ async function checkIndexes(stream: GridFSBucketWriteStream): Promise { const index = { filename: 1, uploadDate: 1 }; let indexes; - remainingTimeMS = getRemainingTimeMSOrThrow( - stream.timeoutContext, + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` ); const listIndexesOptions = { @@ -388,8 +383,7 @@ async function checkIndexes(stream: GridFSBucketWriteStream): Promise { }); if (!hasFileIndex) { - remainingTimeMS = getRemainingTimeMSOrThrow( - stream.timeoutContext, + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` ); diff --git a/src/timeout.ts b/src/timeout.ts index 6149e5b99d8..f694b5f4f4f 100644 --- a/src/timeout.ts +++ b/src/timeout.ts @@ -305,6 +305,18 @@ export class CSOTTimeoutContext extends TimeoutContext { this._serverSelectionTimeout?.clear(); this._connectionCheckoutTimeout?.clear(); } + + /** + * @internal + * Throws a MongoOperationTimeoutError if the context has expired. + * If the context has not expired, returns the `remainingTimeMS` + **/ + getRemainingTimeMSOrThrow(message?: string): number { + const { remainingTimeMS } = this; + if (remainingTimeMS <= 0) + throw new MongoOperationTimeoutError(message ?? `Expired after ${this.timeoutMS}ms`); + return remainingTimeMS; + } } /** @internal */ @@ -352,21 +364,3 @@ export class LegacyTimeoutContext extends TimeoutContext { return; } } - -/** - * @internal - * When `timeoutContext` is defined, throws a MongoOperationTimeoutError if the context has expired. - * If the context has not expired, returns the `remainingTimeMS` - * If the `timeoutContext` is not defined, returns undefined - **/ -export function getRemainingTimeMSOrThrow( - timeoutContext?: CSOTTimeoutContext, - message?: string -): number | undefined { - if (!timeoutContext) return undefined; - - const { remainingTimeMS } = timeoutContext; - if (remainingTimeMS != null && remainingTimeMS <= 0) - throw new MongoOperationTimeoutError(message ?? `Expired after ${timeoutContext.timeoutMS}ms`); - return remainingTimeMS; -}