Skip to content

Commit 9e87db0

Browse files
asdf
1 parent 41ec63b commit 9e87db0

12 files changed

+53
-59
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@
271271
{
272272
// Settings for generated definition files
273273
"files": [
274-
"mongodb.d.ts"
274+
"**/*.d.ts",
275+
"lib/*.d.ts"
275276
],
276277
"parser": "@typescript-eslint/parser",
277278
"rules": {

api-extractor.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"dtsRollup": {
1111
"enabled": true,
1212
"untrimmedFilePath": "",
13-
"publicTrimmedFilePath": "<projectFolder>/<unscopedPackageName>.d.ts"
13+
"publicTrimmedFilePath": "<projectFolder>/<unscopedPackageName>.d.ts",
14+
"betaTrimmedFilePath": "<projectFolder>/lib/beta.d.ts"
1415
},
1516
"tsdocMetadata": {
1617
"enabled": false

etc/clean_definition_files.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (fs.existsSync(libPath)) {
2121
const definitionFiles = Array.from(walk(libPath)).filter(filePath => {
2222
return filePath.endsWith('.d.ts') || filePath.endsWith('.d.ts.map');
2323
});
24-
for (const definitionFile of definitionFiles) {
24+
for (const definitionFile of definitionFiles.filter(file => !file.endsWith('beta.d.ts'))) {
2525
fs.unlinkSync(definitionFile);
2626
}
2727
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,10 @@
77
"src",
88
"etc/prepare.js",
99
"mongodb.d.ts",
10-
"mongodb-next.d.ts",
1110
"tsconfig.json"
1211
],
13-
"exports": {
14-
".": {
15-
"import": "./lib/index.js",
16-
"types": "./mongodb.d.ts",
17-
"require": "./lib/index.js",
18-
"node": "./lib/index.js",
19-
"default": "./lib/index.js"
20-
},
21-
"./next": {
22-
"import": "./lib/index.js",
23-
"types": "./mongodb-next.d.ts",
24-
"require": "./lib/index.js",
25-
"node": "./lib/index.js",
26-
"default": "./lib/index.js"
27-
}
28-
},
12+
"main": "lib/index.js",
13+
"types": "mongodb.d.ts",
2914
"repository": {
3015
"type": "git",
3116
"url": "[email protected]:mongodb/node-mongodb-native.git"
@@ -88,7 +73,7 @@
8873
"@types/express": "^4.17.21",
8974
"@types/kerberos": "^1.1.5",
9075
"@types/mocha": "^10.0.6",
91-
"@types/node": "^20.12.7",
76+
"@types/node": "^20.14.10",
9277
"@types/saslprep": "^1.0.3",
9378
"@types/semver": "^7.5.8",
9479
"@types/sinon": "^17.0.3",
@@ -141,7 +126,7 @@
141126
"scripts": {
142127
"build:evergreen": "node .evergreen/generate_evergreen_tasks.js",
143128
"build:ts": "node ./node_modules/typescript/bin/tsc",
144-
"build:dts": "npm run build:ts && api-extractor run && node etc/clean_definition_files.cjs && ts-node polyfill.ts",
129+
"build:dts": "npm run build:ts && api-extractor run && node etc/clean_definition_files.cjs && eslint --no-ignore --fix mongodb.d.ts lib/beta.d.ts",
145130
"build:docs": "./etc/docs/build.ts",
146131
"build:typedoc": "typedoc",
147132
"build:nightly": "node ./.github/scripts/nightly.mjs",

src/beta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './index';

src/change_stream.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
MongoRuntimeError
1515
} from './error';
1616
import { MongoClient } from './mongo_client';
17+
import { AsyncDisposable } from './resource_management';
1718
import { type InferIdType, TypedEventEmitter } from './mongo_types';
1819
import type { AggregateOptions } from './operations/aggregate';
1920
import type { CollationOptions, OperationParent } from './operations/command';
@@ -525,9 +526,6 @@ export interface UpdateDescription<TSchema extends Document = Document> {
525526
disambiguatedPaths?: Document;
526527
}
527528

528-
// @ts-expect-error Assigning to a readonly property.
529-
Symbol.asyncDispose ??= Symbol('asyncDispose');
530-
531529
/** @public */
532530
export type ChangeStreamEvents<
533531
TSchema extends Document = Document,
@@ -553,10 +551,8 @@ export class ChangeStream<
553551
extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>>
554552
implements AsyncDisposable
555553
{
556-
async [Symbol.asyncDispose]() {
557-
console.error('ChangeStream[Symbol.asyncDispose]() called.');
558-
await this.close();
559-
}
554+
/** @beta */
555+
declare [Symbol.asyncDispose]: () => Promise<void>;
560556

561557
pipeline: Document[];
562558
/**
@@ -997,3 +993,7 @@ export class ChangeStream<
997993
}
998994
}
999995
}
996+
997+
Symbol.asyncDispose && (ChangeStream.prototype[Symbol.asyncDispose] = async function() {
998+
await this.close();
999+
})

src/cursor/abstract_cursor.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { once } from 'events';
21
import { Readable, Transform } from 'stream';
32

43
import { type BSONSerializeOptions, type Document, Long, pluckBSONSerializeOptions } from '../bson';
@@ -21,6 +20,7 @@ import { ReadPreference, type ReadPreferenceLike } from '../read_preference';
2120
import type { Server } from '../sdam/server';
2221
import { ClientSession, maybeClearPinnedConnection } from '../sessions';
2322
import { type MongoDBNamespace, squashError } from '../utils';
23+
import { AsyncDisposable } from '../resource_management';
2424

2525
/**
2626
* @internal
@@ -123,14 +123,11 @@ export type AbstractCursorEvents = {
123123
[AbstractCursor.CLOSE](): void;
124124
};
125125

126-
// @ts-expect-error Assigning to a readonly property.
127-
Symbol.asyncDispose ??= Symbol('asyncDispose');
128-
129126
/** @public */
130127
export abstract class AbstractCursor<
131128
TSchema = any,
132129
CursorEvents extends AbstractCursorEvents = AbstractCursorEvents
133-
> extends TypedEventEmitter<CursorEvents> {
130+
> extends TypedEventEmitter<CursorEvents> implements AsyncDisposable {
134131
/** @internal */
135132
private cursorId: Long | null;
136133
/** @internal */
@@ -279,11 +276,8 @@ export abstract class AbstractCursor<
279276
return !!this.cursorClient.topology?.loadBalanced;
280277
}
281278

282-
async [Symbol.asyncDispose]() {
283-
const name = this.constructor.name;
284-
console.error(name + '[Symbol.asyncDispose]() called.');
285-
await this.close();
286-
}
279+
/** @beta */
280+
declare [Symbol.asyncDispose]: () => Promise<void>;
287281

288282
/** Returns current buffered documents length */
289283
bufferedCount(): number {
@@ -926,3 +920,7 @@ class ReadableCursorStream extends Readable {
926920
);
927921
}
928922
}
923+
924+
Symbol.asyncDispose && (AbstractCursor.prototype[Symbol.asyncDispose] = async function() {
925+
await this.close();
926+
})

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export { CURSOR_FLAGS } from './cursor/abstract_cursor';
107107
export { MongoErrorLabel } from './error';
108108
export { ExplainVerbosity } from './explain';
109109
export { ServerApiVersion } from './mongo_client';
110+
export { AsyncDisposable } from './resource_management';
110111
export { ReturnDocument } from './operations/find_and_modify';
111112
export { ProfilingLevel } from './operations/set_profiling_level';
112113
export { ReadConcernLevel } from './read_concern';

src/mongo_client.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
squashError
5555
} from './utils';
5656
import type { W, WriteConcern, WriteConcernSettings } from './write_concern';
57+
import { AsyncDisposable } from './resource_management';
5758

5859
/** @public */
5960
export const ServerApiVersion = Object.freeze({
@@ -325,9 +326,6 @@ export type MongoClientEvents = Pick<TopologyEvents, (typeof MONGO_CLIENT_EVENTS
325326

326327
const kOptions = Symbol('options');
327328

328-
// @ts-expect-error Assigning to a readonly property.
329-
Symbol.asyncDispose ??= Symbol('asyncDispose');
330-
331329
/**
332330
* The **MongoClient** class is a class that allows for making Connections to MongoDB.
333331
* @public
@@ -407,10 +405,8 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
407405
this.checkForNonGenuineHosts();
408406
}
409407

410-
async [Symbol.asyncDispose]() {
411-
console.error('MongoClient[Symbol.asyncDispose]() called.');
412-
await this.close();
413-
}
408+
/** @beta */
409+
declare [Symbol.asyncDispose]: () => Promise<void>;
414410

415411
/** @internal */
416412
private checkForNonGenuineHosts() {
@@ -766,6 +762,10 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
766762
}
767763
}
768764

765+
Symbol.asyncDispose && (MongoClient.prototype[Symbol.asyncDispose] = async function() {
766+
await this.close();
767+
})
768+
769769
/**
770770
* Parsed Mongo Client Options.
771771
*

src/resource_management.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @public
3+
* @experimental
4+
*/
5+
export interface AsyncDisposable {
6+
/** @beta */
7+
[Symbol.asyncDispose]: () => Promise<void>;
8+
}

src/sessions.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { executeOperation } from './operations/execute_operation';
2727
import { RunAdminCommandOperation } from './operations/run_command';
2828
import { ReadConcernLevel } from './read_concern';
2929
import { ReadPreference } from './read_preference';
30+
import { AsyncDisposable } from './resource_management';
3031
import { _advanceClusterTime, type ClusterTime, TopologyType } from './sdam/common';
3132
import {
3233
isTransactionCommand,
@@ -99,16 +100,13 @@ export interface EndSessionOptions {
99100
forceClear?: boolean;
100101
}
101102

102-
// @ts-expect-error Assigning to a readonly property.
103-
Symbol.asyncDispose ??= Symbol('asyncDispose');
104-
105103
/**
106104
* A class representing a client session on the server
107105
*
108106
* NOTE: not meant to be instantiated directly.
109107
* @public
110108
*/
111-
export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
109+
export class ClientSession extends TypedEventEmitter<ClientSessionEvents> implements AsyncDisposable {
112110
/** @internal */
113111
client: MongoClient;
114112
/** @internal */
@@ -289,11 +287,8 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
289287
maybeClearPinnedConnection(this, { force: true, ...options });
290288
}
291289
}
292-
293-
async [Symbol.asyncDispose]() {
294-
console.error('ClientSession[Symbol.asyncDispose]() called.');
295-
await this.endSession({ force: true });
296-
}
290+
/** @beta */
291+
declare [Symbol.asyncDispose]: () => Promise<void>;
297292

298293
/**
299294
* Advances the operationTime for a ClientSession.
@@ -492,6 +487,10 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
492487
}
493488
}
494489

490+
Symbol.asyncDispose && (ClientSession.prototype[Symbol.asyncDispose] = async function() {
491+
await this.endSession({ force: true });
492+
})
493+
495494
const MAX_WITH_TRANSACTION_TIMEOUT = 120000;
496495
const NON_DETERMINISTIC_WRITE_CONCERN_ERRORS = new Set([
497496
'CannotSatisfyWriteConcern',

0 commit comments

Comments
 (0)