Skip to content

Commit

Permalink
convert getRemainingTimeMSOrThrow to method on CSOTTimeoutContext
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Sep 30, 2024
1 parent 411b082 commit e5ffffb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
10 changes: 4 additions & 6 deletions src/gridfs/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -204,7 +204,7 @@ export class GridFSBucketReadStream extends Readable {
async abort(): Promise<void> {
this.push(null);
this.destroy();
const remainingTimeMS = getRemainingTimeMSOrThrow(this.s.timeoutContext);
const remainingTimeMS = this.s.timeoutContext?.getRemainingTimeMSOrThrow();
await this.s.cursor?.close({ timeoutMS: remainingTimeMS });
}
}
Expand Down Expand Up @@ -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`
);

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/gridfs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -247,8 +247,7 @@ export class GridFSBucket extends TypedEventEmitter<GridFSBucketEvents> {

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 });
Expand Down
20 changes: 7 additions & 13 deletions src/gridfs/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -246,8 +245,7 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise<void>
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`
);

Expand Down Expand Up @@ -276,8 +274,7 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise<void>
});

if (!hasChunksIndex) {
remainingTimeMS = getRemainingTimeMSOrThrow(
stream.timeoutContext,
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
);
await stream.chunks.createIndex(index, {
Expand Down Expand Up @@ -341,8 +338,7 @@ function checkDone(stream: GridFSBucketWriteStream, callback: Callback): void {
}

async function checkIndexes(stream: GridFSBucketWriteStream): Promise<void> {
let remainingTimeMS = getRemainingTimeMSOrThrow(
stream.timeoutContext,
let remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
);
const doc = await stream.files.findOne(
Expand All @@ -361,8 +357,7 @@ async function checkIndexes(stream: GridFSBucketWriteStream): Promise<void> {
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 = {
Expand All @@ -388,8 +383,7 @@ async function checkIndexes(stream: GridFSBucketWriteStream): Promise<void> {
});

if (!hasFileIndex) {
remainingTimeMS = getRemainingTimeMSOrThrow(
stream.timeoutContext,
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
);

Expand Down
30 changes: 12 additions & 18 deletions src/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

0 comments on commit e5ffffb

Please sign in to comment.