SERVER-105599: Make session.js a module (#38531)

GitOrigin-RevId: b87eff51cd102ddc9ad4c8fcd0ad29dedea637cf
This commit is contained in:
Steve McClure 2025-07-16 09:34:15 -04:00 committed by MongoDB Bot
parent fe60c6300c
commit 3cf20e020e
6 changed files with 1063 additions and 1071 deletions

View File

@ -105,7 +105,7 @@ export default [
testingReplication: true,
uncheckedParallelShellPidsString: true,
// src/mongo/shell/session.d.ts
// src/mongo/shell/session_global.js
DriverSession: true,
SessionOptions: true,
_DelegatingDriverSession: true,

View File

@ -366,6 +366,7 @@ extern const JSFile explainable_global;
extern const JSFile mongo;
extern const JSFile prelude;
extern const JSFile session;
extern const JSFile session_global;
extern const JSFile query;
extern const JSFile utils;
extern const JSFile utils_sh;
@ -380,6 +381,7 @@ void Scope::execCoreFiles() {
execSetup(JSFiles::db);
execSetup(JSFiles::explain_query);
execSetup(JSFiles::explainable);
execSetup(JSFiles::session);
// globals
execSetup(JSFiles::bulk_api_global);
@ -387,13 +389,13 @@ void Scope::execCoreFiles() {
execSetup(JSFiles::db_global);
execSetup(JSFiles::explain_query_global);
execSetup(JSFiles::explainable_global);
execSetup(JSFiles::session_global);
// scripts
execSetup(JSFiles::utils);
execSetup(JSFiles::utils_sh);
execSetup(JSFiles::utils_auth);
execSetup(JSFiles::mongo);
execSetup(JSFiles::session);
execSetup(JSFiles::query);
execSetup(JSFiles::error_codes);
execSetup(JSFiles::collection);

View File

@ -289,6 +289,7 @@ MONGOJS_CPP_JSFILES = [
"prelude.js",
"query.js",
"session.js",
"session_global.js",
"types.js",
"utils.js",
"utils_auth.js",

View File

@ -1,7 +0,0 @@
// type declarations for session.js
declare class DriverSession {}
declare class SessionOptions {}
declare class _DelegatingDriverSession {}
declare class _DummyDriverSession {}
declare class _ServerSession {}

View File

@ -4,14 +4,6 @@
* Roughly follows the driver sessions spec:
* https://github.com/mongodb/specifications/blob/master/source/sessions/driver-sessions.rst#abstract
*/
var {
DriverSession,
SessionOptions,
_DummyDriverSession,
_DelegatingDriverSession,
_ServerSession,
} = (function() {
"use strict";
const kShellDefaultShouldRetryWrites =
typeof _shouldRetryWrites === "function" ? _shouldRetryWrites() : false;
@ -47,8 +39,7 @@ var {
// If the user specified --retryWrites to the mongo shell, then we enable retryable
// writes automatically.
const _retryWrites = rawOptions.hasOwnProperty("retryWrites")
? rawOptions.retryWrites
const _retryWrites = rawOptions.hasOwnProperty("retryWrites") ? rawOptions.retryWrites
: kShellDefaultShouldRetryWrites;
this.getReadPreference = function getReadPreference() {
@ -286,8 +277,7 @@ var {
} else if (!isNonNullObject(clientClusterTime)) {
clusterTimeToGossip = sessionClusterTime;
} else {
clusterTimeToGossip =
(bsonWoCompare({_: clientClusterTime.clusterTime},
clusterTimeToGossip = (bsonWoCompare({_: clientClusterTime.clusterTime},
{_: sessionClusterTime.clusterTime}) >= 0)
? clientClusterTime
: sessionClusterTime;
@ -299,8 +289,7 @@ var {
if (serverSupports(kWireVersionSupportingCausalConsistency) &&
(client.isReplicaSetMember() || client.isMongos()) &&
(driverSession.getOptions().isCausalConsistency() ||
client.isCausalConsistency()) &&
(driverSession.getOptions().isCausalConsistency() || client.isCausalConsistency()) &&
this.canUseReadConcern(driverSession, cmdObj)) {
cmdObj = establishSessionReadConcern(cmdObj, driverSession);
}
@ -389,10 +378,8 @@ var {
if (res !== undefined && res.code === ErrorCodes.ReauthenticationRequired) {
try {
const accessToken = client._refreshAccessToken();
assert(client.getDB('$external').auth({
oidcAccessToken: accessToken,
mechanism: 'MONGODB-OIDC'
}));
assert(client.getDB('$external')
.auth({oidcAccessToken: accessToken, mechanism: 'MONGODB-OIDC'}));
continue;
} catch (e) {
// Could not automatically reauthenticate, return the error response
@ -407,19 +394,18 @@ var {
if (res === undefined) {
if (jsTest.options().logRetryAttempts) {
jsTest.log("Retrying " + cmdName +
jsTest.log(
"Retrying " + cmdName +
" with original command request: " + tojson(cmdObj) +
" due to network error, subsequent retries remaining: " +
numRetries);
" due to network error, subsequent retries remaining: " + numRetries);
}
continue;
}
if (isRetryableCode(res.code)) {
if (jsTest.options().logRetryAttempts) {
jsTest.log("Retrying " + cmdName +
" with original command request: " + tojson(cmdObj) +
" due to retryable error (code=" + res.code +
jsTest.log("Retrying " + cmdName + " with original command request: " +
tojson(cmdObj) + " due to retryable error (code=" + res.code +
"), subsequent retries remaining: " + numRetries);
}
if (client.isReplicaSetConnection()) {
@ -436,8 +422,7 @@ var {
if (writeError !== undefined) {
if (jsTest.options().logRetryAttempts) {
jsTest.log(
"Retrying " + cmdName +
jsTest.log("Retrying " + cmdName +
" with original command request: " + tojson(cmdObj) +
" due to retryable write error (code=" + writeError.code +
"), subsequent retries remaining: " + numRetries);
@ -453,9 +438,8 @@ var {
if (res.hasOwnProperty("writeConcernError") &&
isRetryableCode(res.writeConcernError.code)) {
if (jsTest.options().logRetryAttempts) {
jsTest.log("Retrying " + cmdName +
" with original command request: " + tojson(cmdObj) +
" due to retryable write concern error (code=" +
jsTest.log("Retrying " + cmdName + " with original command request: " +
tojson(cmdObj) + " due to retryable write concern error (code=" +
res.writeConcernError.code +
"), subsequent retries remaining: " + numRetries);
}
@ -620,8 +604,7 @@ var {
// catch it here.
if (!serverSupports(kWireVersionSupportingMultiDocumentTransactions)) {
setTxnState("inactive");
throw new Error(
"Transactions are only supported on server versions 4.0 and greater.");
throw new Error("Transactions are only supported on server versions 4.0 and greater.");
}
cmdObj = Object.assign({}, cmdObj);
@ -674,8 +657,7 @@ var {
throw new Error("Transaction already in progress on this session.");
}
if (!serverSupports(kWireVersionSupportingMultiDocumentTransactions)) {
throw new Error(
"Transactions are only supported on server versions 4.0 and greater.");
throw new Error("Transactions are only supported on server versions 4.0 and greater.");
}
_txnOptions = new TransactionOptions(txnOptsObj);
setTxnState("active");
@ -751,8 +733,7 @@ var {
let res;
try {
// run command against the admin database.
res = driverSession._getSessionAwareClient().runCommand(
driverSession, "admin", cmd, 0);
res = driverSession._getSessionAwareClient().runCommand(driverSession, "admin", cmd, 0);
} finally {
if (commandName === "commitTransaction") {
setTxnState("committed");
@ -907,8 +888,7 @@ var {
this.advanceClusterTime = function advanceClusterTime(clusterTime) {
if (!isNonNullObject(_clusterTime) ||
bsonWoCompare({_: clusterTime.clusterTime}, {_: _clusterTime.clusterTime}) >
0) {
bsonWoCompare({_: clusterTime.clusterTime}, {_: _clusterTime.clusterTime}) > 0) {
_clusterTime = clusterTime;
}
};
@ -977,8 +957,7 @@ var {
return this._serverSession.abortTransaction(this);
};
this.processCommandResponse_forTesting = function processCommandResponse_forTesting(
res) {
this.processCommandResponse_forTesting = function processCommandResponse_forTesting(res) {
processCommandResponse(this, client, res);
};
};
@ -1044,8 +1023,7 @@ var {
// The default session on the Mongo connection object should also report that retryable
// writes isn't enabled when interrogating the SessionOptions since `DummyDriverSession` won't
// ever assign a transaction number.
const DummyDriverSession =
makeDriverSessionConstructor( // Force clang-format to break this line.
const DummyDriverSession = makeDriverSessionConstructor( // Force clang-format to break this line.
{
createServerSession: function createServerSession(client) {
return {
@ -1103,11 +1081,14 @@ var {
DummyDriverSession.prototype = Object.create(DriverSession.prototype);
DummyDriverSession.prototype.constructor = DriverSession;
return {
DriverSession: DriverSession,
SessionOptions: SessionOptions,
_DummyDriverSession: DummyDriverSession,
_DelegatingDriverSession: DelegatingDriverSession,
_ServerSession: ServerSession,
const _DummyDriverSession = DummyDriverSession;
const _DelegatingDriverSession = DelegatingDriverSession;
const _ServerSession = ServerSession;
export {
DriverSession,
SessionOptions,
_DummyDriverSession,
_DelegatingDriverSession,
_ServerSession
};
})();

View File

@ -0,0 +1,15 @@
// Populate global variables from modules for backwards compatibility
import {
_DelegatingDriverSession,
_DummyDriverSession,
_ServerSession,
DriverSession,
SessionOptions
} from "src/mongo/shell/session.js";
globalThis.DriverSession = DriverSession;
globalThis.SessionOptions = SessionOptions;
globalThis._DummyDriverSession = _DummyDriverSession;
globalThis._DelegatingDriverSession = _DelegatingDriverSession;
globalThis._ServerSession = _ServerSession;