Skip to content

Commit 89cf5ce

Browse files
committed
chore: cleanup helpers
1 parent c086b8c commit 89cf5ce

File tree

3 files changed

+52
-57
lines changed

3 files changed

+52
-57
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export abstract class AbstractCursor<
341341

342342
stream(options?: CursorStreamOptions): Readable & AsyncIterable<TSchema> {
343343
if (options?.transform) {
344-
const transform = makeSafeTransform(this, options.transform);
344+
const transform = this.makeSafeTransform(options.transform);
345345
const readable = new ReadableCursorStream(this);
346346

347347
const transformedStream = readable.pipe(
@@ -473,7 +473,7 @@ export abstract class AbstractCursor<
473473
* @param value - The flag boolean value.
474474
*/
475475
addCursorFlag(flag: CursorFlag, value: boolean): this {
476-
assertUninitialized(this);
476+
this.throwIfInitialized();
477477
if (!CURSOR_FLAGS.includes(flag)) {
478478
throw new MongoInvalidArgumentError(`Flag ${flag} is not one of ${CURSOR_FLAGS}`);
479479
}
@@ -529,14 +529,14 @@ export abstract class AbstractCursor<
529529
* @param transform - The mapping transformation method.
530530
*/
531531
map<T = any>(transform: (doc: TSchema) => T): AbstractCursor<T> {
532-
assertUninitialized(this);
532+
this.throwIfInitialized();
533533
const oldTransform = this[kTransform] as (doc: TSchema) => TSchema; // TODO(NODE-3283): Improve transform typing
534534
if (oldTransform) {
535535
this[kTransform] = doc => {
536536
return transform(oldTransform(doc));
537537
};
538538
} else {
539-
this[kTransform] = makeSafeTransform(this, transform);
539+
this[kTransform] = this.makeSafeTransform(transform);
540540
}
541541

542542
return this as unknown as AbstractCursor<T>;
@@ -548,7 +548,7 @@ export abstract class AbstractCursor<
548548
* @param readPreference - The new read preference for the cursor.
549549
*/
550550
withReadPreference(readPreference: ReadPreferenceLike): this {
551-
assertUninitialized(this);
551+
this.throwIfInitialized();
552552
if (readPreference instanceof ReadPreference) {
553553
this[kOptions].readPreference = readPreference;
554554
} else if (typeof readPreference === 'string') {
@@ -566,7 +566,7 @@ export abstract class AbstractCursor<
566566
* @param readPreference - The new read preference for the cursor.
567567
*/
568568
withReadConcern(readConcern: ReadConcernLike): this {
569-
assertUninitialized(this);
569+
this.throwIfInitialized();
570570
const resolvedReadConcern = ReadConcern.fromOptions({ readConcern });
571571
if (resolvedReadConcern) {
572572
this[kOptions].readConcern = resolvedReadConcern;
@@ -581,7 +581,7 @@ export abstract class AbstractCursor<
581581
* @param value - Number of milliseconds to wait before aborting the query.
582582
*/
583583
maxTimeMS(value: number): this {
584-
assertUninitialized(this);
584+
this.throwIfInitialized();
585585
if (typeof value !== 'number') {
586586
throw new MongoInvalidArgumentError('Argument for maxTimeMS must be a number');
587587
}
@@ -596,7 +596,7 @@ export abstract class AbstractCursor<
596596
* @param value - The number of documents to return per batch. See {@link https://www.mongodb.com/docs/manual/reference/command/find/|find command documentation}.
597597
*/
598598
batchSize(value: number): this {
599-
assertUninitialized(this);
599+
this.throwIfInitialized();
600600
if (this[kOptions].tailable) {
601601
throw new MongoTailableCursorError('Tailable cursor does not support batchSize');
602602
}
@@ -802,8 +802,33 @@ export abstract class AbstractCursor<
802802
this.hasEmittedClose = true;
803803
}
804804
}
805+
806+
private makeSafeTransform<TSchema>(transform: (doc: TSchema) => any): (doc: TSchema) => any {
807+
return doc => {
808+
try {
809+
const result = transform(doc);
810+
// eslint-disable-next-line no-restricted-syntax
811+
if (result === null) {
812+
const message =
813+
'Cursor returned a `null` document, but the cursor is not exhausted. Mapping documents to `null` is not supported in the cursor transform.';
814+
throw new MongoAPIError(message);
815+
}
816+
817+
return result;
818+
} catch (error) {
819+
// eslint-disable-next-line github/no-then
820+
this.close().then(undefined, squashError);
821+
throw error;
822+
}
823+
};
824+
}
825+
826+
protected throwIfInitialized() {
827+
if (this[kInitialized]) throw new MongoCursorInUseError();
828+
}
805829
}
806830

831+
/** A temporary helper to box up the many possible type issue of cursor ids */
807832
function getCursorId(response: Document) {
808833
return typeof response.cursor.id === 'number'
809834
? Long.fromNumber(response.cursor.id)
@@ -812,36 +837,6 @@ function getCursorId(response: Document) {
812837
: response.cursor.id;
813838
}
814839

815-
function makeSafeTransform<TSchema>(
816-
cursor: AbstractCursor,
817-
transform: (doc: TSchema) => any
818-
): (doc: TSchema) => any {
819-
return doc => {
820-
try {
821-
const result = transform(doc);
822-
// eslint-disable-next-line no-restricted-syntax
823-
if (result === null) {
824-
const message =
825-
'Cursor returned a `null` document, but the cursor is not exhausted. Mapping documents to `null` is not supported in the cursor transform.';
826-
throw new MongoAPIError(message);
827-
}
828-
829-
return result;
830-
} catch (error) {
831-
// eslint-disable-next-line github/no-then
832-
cursor.close().then(undefined, squashError);
833-
throw error;
834-
}
835-
};
836-
}
837-
838-
/** @internal */
839-
export function assertUninitialized(cursor: AbstractCursor): void {
840-
if (cursor[kInitialized]) {
841-
throw new MongoCursorInUseError();
842-
}
843-
}
844-
845840
class ReadableCursorStream extends Readable {
846841
private _cursor: AbstractCursor;
847842
private _readInProgress = false;

src/cursor/aggregation_cursor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Sort } from '../sort';
88
import type { MongoDBNamespace } from '../utils';
99
import { mergeOptions } from '../utils';
1010
import type { AbstractCursorOptions } from './abstract_cursor';
11-
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
11+
import { AbstractCursor } from './abstract_cursor';
1212

1313
/** @public */
1414
export interface AggregationCursorOptions extends AbstractCursorOptions, AggregateOptions {}
@@ -101,7 +101,7 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
101101
addStage(stage: Document): this;
102102
addStage<T = Document>(stage: Document): AggregationCursor<T>;
103103
addStage<T = Document>(stage: Document): AggregationCursor<T> {
104-
assertUninitialized(this);
104+
this.throwIfInitialized();
105105
this[kPipeline].push(stage);
106106
return this as unknown as AggregationCursor<T>;
107107
}

src/cursor/find_cursor.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Hint } from '../operations/operation';
1111
import type { ClientSession } from '../sessions';
1212
import { formatSort, type Sort, type SortDirection } from '../sort';
1313
import { emitWarningOnce, mergeOptions, type MongoDBNamespace, squashError } from '../utils';
14-
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
14+
import { AbstractCursor } from './abstract_cursor';
1515

1616
/** @internal */
1717
const kFilter = Symbol('filter');
@@ -163,7 +163,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
163163

164164
/** Set the cursor query */
165165
filter(filter: Document): this {
166-
assertUninitialized(this);
166+
this.throwIfInitialized();
167167
this[kFilter] = filter;
168168
return this;
169169
}
@@ -174,7 +174,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
174174
* @param hint - If specified, then the query system will only consider plans using the hinted index.
175175
*/
176176
hint(hint: Hint): this {
177-
assertUninitialized(this);
177+
this.throwIfInitialized();
178178
this[kBuiltOptions].hint = hint;
179179
return this;
180180
}
@@ -185,7 +185,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
185185
* @param min - Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The $min specifies the lower bound for all keys of a specific index in order.
186186
*/
187187
min(min: Document): this {
188-
assertUninitialized(this);
188+
this.throwIfInitialized();
189189
this[kBuiltOptions].min = min;
190190
return this;
191191
}
@@ -196,7 +196,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
196196
* @param max - Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). The $max specifies the upper bound for all keys of a specific index in order.
197197
*/
198198
max(max: Document): this {
199-
assertUninitialized(this);
199+
this.throwIfInitialized();
200200
this[kBuiltOptions].max = max;
201201
return this;
202202
}
@@ -209,7 +209,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
209209
* @param value - the returnKey value.
210210
*/
211211
returnKey(value: boolean): this {
212-
assertUninitialized(this);
212+
this.throwIfInitialized();
213213
this[kBuiltOptions].returnKey = value;
214214
return this;
215215
}
@@ -220,7 +220,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
220220
* @param value - The $showDiskLoc option has now been deprecated and replaced with the showRecordId field. $showDiskLoc will still be accepted for OP_QUERY stye find.
221221
*/
222222
showRecordId(value: boolean): this {
223-
assertUninitialized(this);
223+
this.throwIfInitialized();
224224
this[kBuiltOptions].showRecordId = value;
225225
return this;
226226
}
@@ -232,7 +232,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
232232
* @param value - The modifier value.
233233
*/
234234
addQueryModifier(name: string, value: string | boolean | number | Document): this {
235-
assertUninitialized(this);
235+
this.throwIfInitialized();
236236
if (name[0] !== '$') {
237237
throw new MongoInvalidArgumentError(`${name} is not a valid query modifier`);
238238
}
@@ -295,7 +295,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
295295
* @param value - The comment attached to this query.
296296
*/
297297
comment(value: string): this {
298-
assertUninitialized(this);
298+
this.throwIfInitialized();
299299
this[kBuiltOptions].comment = value;
300300
return this;
301301
}
@@ -306,7 +306,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
306306
* @param value - Number of milliseconds to wait before aborting the tailed query.
307307
*/
308308
maxAwaitTimeMS(value: number): this {
309-
assertUninitialized(this);
309+
this.throwIfInitialized();
310310
if (typeof value !== 'number') {
311311
throw new MongoInvalidArgumentError('Argument for maxAwaitTimeMS must be a number');
312312
}
@@ -321,7 +321,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
321321
* @param value - Number of milliseconds to wait before aborting the query.
322322
*/
323323
override maxTimeMS(value: number): this {
324-
assertUninitialized(this);
324+
this.throwIfInitialized();
325325
if (typeof value !== 'number') {
326326
throw new MongoInvalidArgumentError('Argument for maxTimeMS must be a number');
327327
}
@@ -371,7 +371,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
371371
* ```
372372
*/
373373
project<T extends Document = Document>(value: Document): FindCursor<T> {
374-
assertUninitialized(this);
374+
this.throwIfInitialized();
375375
this[kBuiltOptions].projection = value;
376376
return this as unknown as FindCursor<T>;
377377
}
@@ -383,7 +383,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
383383
* @param direction - The direction of the sorting (1 or -1).
384384
*/
385385
sort(sort: Sort | string, direction?: SortDirection): this {
386-
assertUninitialized(this);
386+
this.throwIfInitialized();
387387
if (this[kBuiltOptions].tailable) {
388388
throw new MongoTailableCursorError('Tailable cursor does not support sorting');
389389
}
@@ -399,7 +399,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
399399
* {@link https://www.mongodb.com/docs/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}
400400
*/
401401
allowDiskUse(allow = true): this {
402-
assertUninitialized(this);
402+
this.throwIfInitialized();
403403

404404
if (!this[kBuiltOptions].sort) {
405405
throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification');
@@ -421,7 +421,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
421421
* @param value - The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
422422
*/
423423
collation(value: CollationOptions): this {
424-
assertUninitialized(this);
424+
this.throwIfInitialized();
425425
this[kBuiltOptions].collation = value;
426426
return this;
427427
}
@@ -432,7 +432,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
432432
* @param value - The limit for the cursor query.
433433
*/
434434
limit(value: number): this {
435-
assertUninitialized(this);
435+
this.throwIfInitialized();
436436
if (this[kBuiltOptions].tailable) {
437437
throw new MongoTailableCursorError('Tailable cursor does not support limit');
438438
}
@@ -451,7 +451,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
451451
* @param value - The skip for the cursor query.
452452
*/
453453
skip(value: number): this {
454-
assertUninitialized(this);
454+
this.throwIfInitialized();
455455
if (this[kBuiltOptions].tailable) {
456456
throw new MongoTailableCursorError('Tailable cursor does not support skip');
457457
}

0 commit comments

Comments
 (0)