mongodb 6.12.0-dev.20241211.sha.2f9ad4d4 → 6.12.0-dev.20241218.sha.e972bb8f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sessions.ts CHANGED
@@ -83,17 +83,6 @@ export type ClientSessionEvents = {
83
83
  ended(session: ClientSession): void;
84
84
  };
85
85
 
86
- /** @internal */
87
- const kServerSession = Symbol('serverSession');
88
- /** @internal */
89
- const kSnapshotTime = Symbol('snapshotTime');
90
- /** @internal */
91
- const kSnapshotEnabled = Symbol('snapshotEnabled');
92
- /** @internal */
93
- const kPinnedConnection = Symbol('pinnedConnection');
94
- /** @internal Accumulates total number of increments to add to txnNumber when applying session to command */
95
- const kTxnNumberIncrement = Symbol('txnNumberIncrement');
96
-
97
86
  /** @public */
98
87
  export interface EndSessionOptions {
99
88
  /**
@@ -132,20 +121,22 @@ export class ClientSession
132
121
  owner?: symbol | AbstractCursor;
133
122
  defaultTransactionOptions: TransactionOptions;
134
123
  transaction: Transaction;
135
- /** @internal
124
+ /**
125
+ * @internal
136
126
  * Keeps track of whether or not the current transaction has attempted to be committed. Is
137
- * initially undefined. Gets set to false when startTransaction is called. When commitTransaction is sent to server, if the commitTransaction succeeds, it is then set to undefined, otherwise, set to true */
138
- commitAttempted?: boolean;
139
- /** @internal */
140
- private [kServerSession]: ServerSession | null;
127
+ * initially undefined. Gets set to false when startTransaction is called. When commitTransaction is sent to server, if the commitTransaction succeeds, it is then set to undefined, otherwise, set to true
128
+ */
129
+ private commitAttempted?: boolean;
130
+ public readonly snapshotEnabled: boolean;
131
+
141
132
  /** @internal */
142
- [kSnapshotTime]?: Timestamp;
133
+ private _serverSession: ServerSession | null;
143
134
  /** @internal */
144
- [kSnapshotEnabled] = false;
135
+ public snapshotTime?: Timestamp;
145
136
  /** @internal */
146
- [kPinnedConnection]?: Connection;
137
+ public pinnedConnection?: Connection;
147
138
  /** @internal */
148
- [kTxnNumberIncrement]: number;
139
+ public txnNumberIncrement: number;
149
140
  /**
150
141
  * @experimental
151
142
  * Specifies the time an operation in a given `ClientSession` will run until it throws a timeout error
@@ -183,13 +174,11 @@ export class ClientSession
183
174
 
184
175
  options = options ?? {};
185
176
 
186
- if (options.snapshot === true) {
187
- this[kSnapshotEnabled] = true;
188
- if (options.causalConsistency === true) {
189
- throw new MongoInvalidArgumentError(
190
- 'Properties "causalConsistency" and "snapshot" are mutually exclusive'
191
- );
192
- }
177
+ this.snapshotEnabled = options.snapshot === true;
178
+ if (options.causalConsistency === true && this.snapshotEnabled) {
179
+ throw new MongoInvalidArgumentError(
180
+ 'Properties "causalConsistency" and "snapshot" are mutually exclusive'
181
+ );
193
182
  }
194
183
 
195
184
  this.client = client;
@@ -199,8 +188,8 @@ export class ClientSession
199
188
  this.timeoutMS = options.defaultTimeoutMS ?? client.s.options?.timeoutMS;
200
189
 
201
190
  this.explicit = !!options.explicit;
202
- this[kServerSession] = this.explicit ? this.sessionPool.acquire() : null;
203
- this[kTxnNumberIncrement] = 0;
191
+ this._serverSession = this.explicit ? this.sessionPool.acquire() : null;
192
+ this.txnNumberIncrement = 0;
204
193
 
205
194
  const defaultCausalConsistencyValue = this.explicit && options.snapshot !== true;
206
195
  this.supports = {
@@ -218,11 +207,11 @@ export class ClientSession
218
207
 
219
208
  /** The server id associated with this session */
220
209
  get id(): ServerSessionId | undefined {
221
- return this[kServerSession]?.id;
210
+ return this.serverSession?.id;
222
211
  }
223
212
 
224
213
  get serverSession(): ServerSession {
225
- let serverSession = this[kServerSession];
214
+ let serverSession = this._serverSession;
226
215
  if (serverSession == null) {
227
216
  if (this.explicit) {
228
217
  throw new MongoRuntimeError('Unexpected null serverSession for an explicit session');
@@ -231,32 +220,22 @@ export class ClientSession
231
220
  throw new MongoRuntimeError('Unexpected null serverSession for an ended implicit session');
232
221
  }
233
222
  serverSession = this.sessionPool.acquire();
234
- this[kServerSession] = serverSession;
223
+ this._serverSession = serverSession;
235
224
  }
236
225
  return serverSession;
237
226
  }
238
227
 
239
- /** Whether or not this session is configured for snapshot reads */
240
- get snapshotEnabled(): boolean {
241
- return this[kSnapshotEnabled];
242
- }
243
-
244
228
  get loadBalanced(): boolean {
245
229
  return this.client.topology?.description.type === TopologyType.LoadBalanced;
246
230
  }
247
231
 
248
- /** @internal */
249
- get pinnedConnection(): Connection | undefined {
250
- return this[kPinnedConnection];
251
- }
252
-
253
232
  /** @internal */
254
233
  pin(conn: Connection): void {
255
- if (this[kPinnedConnection]) {
234
+ if (this.pinnedConnection) {
256
235
  throw TypeError('Cannot pin multiple connections to the same session');
257
236
  }
258
237
 
259
- this[kPinnedConnection] = conn;
238
+ this.pinnedConnection = conn;
260
239
  conn.emit(
261
240
  PINNED,
262
241
  this.inTransaction() ? ConnectionPoolMetrics.TXN : ConnectionPoolMetrics.CURSOR
@@ -273,7 +252,7 @@ export class ClientSession
273
252
  }
274
253
 
275
254
  get isPinned(): boolean {
276
- return this.loadBalanced ? !!this[kPinnedConnection] : this.transaction.isPinned;
255
+ return this.loadBalanced ? !!this.pinnedConnection : this.transaction.isPinned;
277
256
  }
278
257
 
279
258
  /**
@@ -295,12 +274,12 @@ export class ClientSession
295
274
  squashError(error);
296
275
  } finally {
297
276
  if (!this.hasEnded) {
298
- const serverSession = this[kServerSession];
277
+ const serverSession = this.serverSession;
299
278
  if (serverSession != null) {
300
279
  // release the server session back to the pool
301
280
  this.sessionPool.release(serverSession);
302
281
  // Store a clone of the server session for reference (debugging)
303
- this[kServerSession] = new ServerSession(serverSession);
282
+ this._serverSession = new ServerSession(serverSession);
304
283
  }
305
284
  // mark the session as ended, and emit a signal
306
285
  this.hasEnded = true;
@@ -391,7 +370,7 @@ export class ClientSession
391
370
  * This is because the serverSession is lazily acquired after a connection is obtained
392
371
  */
393
372
  incrementTransactionNumber(): void {
394
- this[kTxnNumberIncrement] += 1;
373
+ this.txnNumberIncrement += 1;
395
374
  }
396
375
 
397
376
  /** @returns whether this session is currently in a transaction or not */
@@ -410,7 +389,7 @@ export class ClientSession
410
389
  * @param options - Options for the transaction
411
390
  */
412
391
  startTransaction(options?: TransactionOptions): void {
413
- if (this[kSnapshotEnabled]) {
392
+ if (this.snapshotEnabled) {
414
393
  throw new MongoCompatibilityError('Transactions are not supported in snapshot sessions');
415
394
  }
416
395
 
@@ -908,7 +887,7 @@ export function maybeClearPinnedConnection(
908
887
  options?: EndSessionOptions
909
888
  ): void {
910
889
  // unpin a connection if it has been pinned
911
- const conn = session[kPinnedConnection];
890
+ const conn = session.pinnedConnection;
912
891
  const error = options?.error;
913
892
 
914
893
  if (
@@ -929,7 +908,7 @@ export function maybeClearPinnedConnection(
929
908
 
930
909
  if (options?.error == null || options?.force) {
931
910
  loadBalancer.pool.checkIn(conn);
932
- session[kPinnedConnection] = undefined;
911
+ session.pinnedConnection = undefined;
933
912
  conn.emit(
934
913
  UNPINNED,
935
914
  session.transaction.state !== TxnState.NO_TRANSACTION
@@ -1123,8 +1102,8 @@ export function applySession(
1123
1102
  const isRetryableWrite = !!options.willRetryWrite;
1124
1103
 
1125
1104
  if (isRetryableWrite || inTxnOrTxnCommand) {
1126
- serverSession.txnNumber += session[kTxnNumberIncrement];
1127
- session[kTxnNumberIncrement] = 0;
1105
+ serverSession.txnNumber += session.txnNumberIncrement;
1106
+ session.txnNumberIncrement = 0;
1128
1107
  // TODO(NODE-2674): Preserve int64 sent from MongoDB
1129
1108
  command.txnNumber = Long.fromNumber(serverSession.txnNumber);
1130
1109
  }
@@ -1141,10 +1120,10 @@ export function applySession(
1141
1120
  ) {
1142
1121
  command.readConcern = command.readConcern || {};
1143
1122
  Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
1144
- } else if (session[kSnapshotEnabled]) {
1123
+ } else if (session.snapshotEnabled) {
1145
1124
  command.readConcern = command.readConcern || { level: ReadConcernLevel.snapshot };
1146
- if (session[kSnapshotTime] != null) {
1147
- Object.assign(command.readConcern, { atClusterTime: session[kSnapshotTime] });
1125
+ if (session.snapshotTime != null) {
1126
+ Object.assign(command.readConcern, { atClusterTime: session.snapshotTime });
1148
1127
  }
1149
1128
  }
1150
1129
 
@@ -1187,12 +1166,12 @@ export function updateSessionFromResponse(session: ClientSession, document: Mong
1187
1166
  session.transaction._recoveryToken = document.recoveryToken;
1188
1167
  }
1189
1168
 
1190
- if (session?.[kSnapshotEnabled] && session[kSnapshotTime] == null) {
1169
+ if (session?.snapshotEnabled && session.snapshotTime == null) {
1191
1170
  // find and aggregate commands return atClusterTime on the cursor
1192
1171
  // distinct includes it in the response body
1193
1172
  const atClusterTime = document.atClusterTime;
1194
1173
  if (atClusterTime) {
1195
- session[kSnapshotTime] = atClusterTime;
1174
+ session.snapshotTime = atClusterTime;
1196
1175
  }
1197
1176
  }
1198
1177
  }