123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748 |
- "use strict";
- var _a;
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.updateSessionFromResponse = exports.applySession = exports.ServerSessionPool = exports.ServerSession = exports.maybeClearPinnedConnection = exports.ClientSession = void 0;
- const bson_1 = require("./bson");
- const metrics_1 = require("./cmap/metrics");
- const shared_1 = require("./cmap/wire_protocol/shared");
- const constants_1 = require("./constants");
- const error_1 = require("./error");
- const mongo_types_1 = require("./mongo_types");
- const execute_operation_1 = require("./operations/execute_operation");
- const run_command_1 = require("./operations/run_command");
- const promise_provider_1 = require("./promise_provider");
- const read_concern_1 = require("./read_concern");
- const read_preference_1 = require("./read_preference");
- const common_1 = require("./sdam/common");
- const transactions_1 = require("./transactions");
- const utils_1 = require("./utils");
- const minWireVersionForShardedTransactions = 8;
- const kServerSession = Symbol('serverSession');
- const kSnapshotTime = Symbol('snapshotTime');
- const kSnapshotEnabled = Symbol('snapshotEnabled');
- const kPinnedConnection = Symbol('pinnedConnection');
- const kTxnNumberIncrement = Symbol('txnNumberIncrement');
- class ClientSession extends mongo_types_1.TypedEventEmitter {
-
- constructor(client, sessionPool, options, clientOptions) {
- super();
-
- this[_a] = false;
- if (client == null) {
-
- throw new error_1.MongoRuntimeError('ClientSession requires a MongoClient');
- }
- if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) {
-
- throw new error_1.MongoRuntimeError('ClientSession requires a ServerSessionPool');
- }
- options = options !== null && options !== void 0 ? options : {};
- if (options.snapshot === true) {
- this[kSnapshotEnabled] = true;
- if (options.causalConsistency === true) {
- throw new error_1.MongoInvalidArgumentError('Properties "causalConsistency" and "snapshot" are mutually exclusive');
- }
- }
- this.client = client;
- this.sessionPool = sessionPool;
- this.hasEnded = false;
- this.clientOptions = clientOptions;
- this.explicit = !!options.explicit;
- this[kServerSession] = this.explicit ? this.sessionPool.acquire() : null;
- this[kTxnNumberIncrement] = 0;
- this.supports = {
- causalConsistency: options.snapshot !== true && options.causalConsistency !== false
- };
- this.clusterTime = options.initialClusterTime;
- this.operationTime = undefined;
- this.owner = options.owner;
- this.defaultTransactionOptions = Object.assign({}, options.defaultTransactionOptions);
- this.transaction = new transactions_1.Transaction();
- }
-
- get id() {
- var _b;
- return (_b = this[kServerSession]) === null || _b === void 0 ? void 0 : _b.id;
- }
- get serverSession() {
- let serverSession = this[kServerSession];
- if (serverSession == null) {
- if (this.explicit) {
- throw new error_1.MongoRuntimeError('Unexpected null serverSession for an explicit session');
- }
- if (this.hasEnded) {
- throw new error_1.MongoRuntimeError('Unexpected null serverSession for an ended implicit session');
- }
- serverSession = this.sessionPool.acquire();
- this[kServerSession] = serverSession;
- }
- return serverSession;
- }
-
- get snapshotEnabled() {
- return this[kSnapshotEnabled];
- }
- get loadBalanced() {
- var _b;
- return ((_b = this.client.topology) === null || _b === void 0 ? void 0 : _b.description.type) === common_1.TopologyType.LoadBalanced;
- }
-
- get pinnedConnection() {
- return this[kPinnedConnection];
- }
-
- pin(conn) {
- if (this[kPinnedConnection]) {
- throw TypeError('Cannot pin multiple connections to the same session');
- }
- this[kPinnedConnection] = conn;
- conn.emit(constants_1.PINNED, this.inTransaction() ? metrics_1.ConnectionPoolMetrics.TXN : metrics_1.ConnectionPoolMetrics.CURSOR);
- }
-
- unpin(options) {
- if (this.loadBalanced) {
- return maybeClearPinnedConnection(this, options);
- }
- this.transaction.unpinServer();
- }
- get isPinned() {
- return this.loadBalanced ? !!this[kPinnedConnection] : this.transaction.isPinned;
- }
- endSession(options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- const finalOptions = { force: true, ...options };
- return (0, utils_1.maybePromise)(callback, done => {
- if (this.hasEnded) {
- maybeClearPinnedConnection(this, finalOptions);
- return done();
- }
- const completeEndSession = () => {
- maybeClearPinnedConnection(this, finalOptions);
- const serverSession = this[kServerSession];
- if (serverSession != null) {
-
- this.sessionPool.release(serverSession);
-
- Object.defineProperty(this, kServerSession, {
- value: ServerSession.clone(serverSession)
- });
- }
-
- this.hasEnded = true;
- this.emit('ended', this);
-
- done();
- };
- if (this.inTransaction()) {
-
-
- this.abortTransaction(err => {
- if (err)
- return done(err);
- completeEndSession();
- });
- return;
- }
- completeEndSession();
- });
- }
-
- advanceOperationTime(operationTime) {
- if (this.operationTime == null) {
- this.operationTime = operationTime;
- return;
- }
- if (operationTime.greaterThan(this.operationTime)) {
- this.operationTime = operationTime;
- }
- }
-
- advanceClusterTime(clusterTime) {
- var _b, _c;
- if (!clusterTime || typeof clusterTime !== 'object') {
- throw new error_1.MongoInvalidArgumentError('input cluster time must be an object');
- }
- if (!clusterTime.clusterTime || clusterTime.clusterTime._bsontype !== 'Timestamp') {
- throw new error_1.MongoInvalidArgumentError('input cluster time "clusterTime" property must be a valid BSON Timestamp');
- }
- if (!clusterTime.signature ||
- ((_b = clusterTime.signature.hash) === null || _b === void 0 ? void 0 : _b._bsontype) !== 'Binary' ||
- (typeof clusterTime.signature.keyId !== 'number' &&
- ((_c = clusterTime.signature.keyId) === null || _c === void 0 ? void 0 : _c._bsontype) !== 'Long')
- ) {
- throw new error_1.MongoInvalidArgumentError('input cluster time must have a valid "signature" property with BSON Binary hash and BSON Long keyId');
- }
- (0, common_1._advanceClusterTime)(this, clusterTime);
- }
-
- equals(session) {
- if (!(session instanceof ClientSession)) {
- return false;
- }
- if (this.id == null || session.id == null) {
- return false;
- }
- return this.id.id.buffer.equals(session.id.id.buffer);
- }
-
- incrementTransactionNumber() {
- this[kTxnNumberIncrement] += 1;
- }
-
- inTransaction() {
- return this.transaction.isActive;
- }
-
- startTransaction(options) {
- var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
- if (this[kSnapshotEnabled]) {
- throw new error_1.MongoCompatibilityError('Transactions are not allowed with snapshot sessions');
- }
- if (this.inTransaction()) {
- throw new error_1.MongoTransactionError('Transaction already in progress');
- }
- if (this.isPinned && this.transaction.isCommitted) {
- this.unpin();
- }
- const topologyMaxWireVersion = (0, utils_1.maxWireVersion)(this.client.topology);
- if ((0, shared_1.isSharded)(this.client.topology) &&
- topologyMaxWireVersion != null &&
- topologyMaxWireVersion < minWireVersionForShardedTransactions) {
- throw new error_1.MongoCompatibilityError('Transactions are not supported on sharded clusters in MongoDB < 4.2.');
- }
-
- this.incrementTransactionNumber();
-
- this.transaction = new transactions_1.Transaction({
- readConcern: (_c = (_b = options === null || options === void 0 ? void 0 : options.readConcern) !== null && _b !== void 0 ? _b : this.defaultTransactionOptions.readConcern) !== null && _c !== void 0 ? _c : (_d = this.clientOptions) === null || _d === void 0 ? void 0 : _d.readConcern,
- writeConcern: (_f = (_e = options === null || options === void 0 ? void 0 : options.writeConcern) !== null && _e !== void 0 ? _e : this.defaultTransactionOptions.writeConcern) !== null && _f !== void 0 ? _f : (_g = this.clientOptions) === null || _g === void 0 ? void 0 : _g.writeConcern,
- readPreference: (_j = (_h = options === null || options === void 0 ? void 0 : options.readPreference) !== null && _h !== void 0 ? _h : this.defaultTransactionOptions.readPreference) !== null && _j !== void 0 ? _j : (_k = this.clientOptions) === null || _k === void 0 ? void 0 : _k.readPreference,
- maxCommitTimeMS: (_l = options === null || options === void 0 ? void 0 : options.maxCommitTimeMS) !== null && _l !== void 0 ? _l : this.defaultTransactionOptions.maxCommitTimeMS
- });
- this.transaction.transition(transactions_1.TxnState.STARTING_TRANSACTION);
- }
- commitTransaction(callback) {
- return (0, utils_1.maybePromise)(callback, cb => endTransaction(this, 'commitTransaction', cb));
- }
- abortTransaction(callback) {
- return (0, utils_1.maybePromise)(callback, cb => endTransaction(this, 'abortTransaction', cb));
- }
-
- toBSON() {
- throw new error_1.MongoRuntimeError('ClientSession cannot be serialized to BSON.');
- }
-
- withTransaction(fn, options) {
- const startTime = (0, utils_1.now)();
- return attemptTransaction(this, startTime, fn, options);
- }
- }
- exports.ClientSession = ClientSession;
- _a = kSnapshotEnabled;
- const MAX_WITH_TRANSACTION_TIMEOUT = 120000;
- const NON_DETERMINISTIC_WRITE_CONCERN_ERRORS = new Set([
- 'CannotSatisfyWriteConcern',
- 'UnknownReplWriteConcern',
- 'UnsatisfiableWriteConcern'
- ]);
- function hasNotTimedOut(startTime, max) {
- return (0, utils_1.calculateDurationInMs)(startTime) < max;
- }
- function isUnknownTransactionCommitResult(err) {
- const isNonDeterministicWriteConcernError = err instanceof error_1.MongoServerError &&
- err.codeName &&
- NON_DETERMINISTIC_WRITE_CONCERN_ERRORS.has(err.codeName);
- return (isMaxTimeMSExpiredError(err) ||
- (!isNonDeterministicWriteConcernError &&
- err.code !== error_1.MONGODB_ERROR_CODES.UnsatisfiableWriteConcern &&
- err.code !== error_1.MONGODB_ERROR_CODES.UnknownReplWriteConcern));
- }
- function maybeClearPinnedConnection(session, options) {
-
- const conn = session[kPinnedConnection];
- const error = options === null || options === void 0 ? void 0 : options.error;
- if (session.inTransaction() &&
- error &&
- error instanceof error_1.MongoError &&
- error.hasErrorLabel(error_1.MongoErrorLabel.TransientTransactionError)) {
- return;
- }
- const topology = session.client.topology;
-
-
- if (conn && topology != null) {
- const servers = Array.from(topology.s.servers.values());
- const loadBalancer = servers[0];
- if ((options === null || options === void 0 ? void 0 : options.error) == null || (options === null || options === void 0 ? void 0 : options.force)) {
- loadBalancer.s.pool.checkIn(conn);
- conn.emit(constants_1.UNPINNED, session.transaction.state !== transactions_1.TxnState.NO_TRANSACTION
- ? metrics_1.ConnectionPoolMetrics.TXN
- : metrics_1.ConnectionPoolMetrics.CURSOR);
- if (options === null || options === void 0 ? void 0 : options.forceClear) {
- loadBalancer.s.pool.clear(conn.serviceId);
- }
- }
- session[kPinnedConnection] = undefined;
- }
- }
- exports.maybeClearPinnedConnection = maybeClearPinnedConnection;
- function isMaxTimeMSExpiredError(err) {
- if (err == null || !(err instanceof error_1.MongoServerError)) {
- return false;
- }
- return (err.code === error_1.MONGODB_ERROR_CODES.MaxTimeMSExpired ||
- (err.writeConcernError && err.writeConcernError.code === error_1.MONGODB_ERROR_CODES.MaxTimeMSExpired));
- }
- function attemptTransactionCommit(session, startTime, fn, options) {
- return session.commitTransaction().catch((err) => {
- if (err instanceof error_1.MongoError &&
- hasNotTimedOut(startTime, MAX_WITH_TRANSACTION_TIMEOUT) &&
- !isMaxTimeMSExpiredError(err)) {
- if (err.hasErrorLabel(error_1.MongoErrorLabel.UnknownTransactionCommitResult)) {
- return attemptTransactionCommit(session, startTime, fn, options);
- }
- if (err.hasErrorLabel(error_1.MongoErrorLabel.TransientTransactionError)) {
- return attemptTransaction(session, startTime, fn, options);
- }
- }
- throw err;
- });
- }
- const USER_EXPLICIT_TXN_END_STATES = new Set([
- transactions_1.TxnState.NO_TRANSACTION,
- transactions_1.TxnState.TRANSACTION_COMMITTED,
- transactions_1.TxnState.TRANSACTION_ABORTED
- ]);
- function userExplicitlyEndedTransaction(session) {
- return USER_EXPLICIT_TXN_END_STATES.has(session.transaction.state);
- }
- function attemptTransaction(session, startTime, fn, options) {
- const Promise = promise_provider_1.PromiseProvider.get();
- session.startTransaction(options);
- let promise;
- try {
- promise = fn(session);
- }
- catch (err) {
- promise = Promise.reject(err);
- }
- if (!(0, utils_1.isPromiseLike)(promise)) {
- session.abortTransaction();
- throw new error_1.MongoInvalidArgumentError('Function provided to `withTransaction` must return a Promise');
- }
- return promise.then(() => {
- if (userExplicitlyEndedTransaction(session)) {
- return;
- }
- return attemptTransactionCommit(session, startTime, fn, options);
- }, err => {
- function maybeRetryOrThrow(err) {
- if (err instanceof error_1.MongoError &&
- err.hasErrorLabel(error_1.MongoErrorLabel.TransientTransactionError) &&
- hasNotTimedOut(startTime, MAX_WITH_TRANSACTION_TIMEOUT)) {
- return attemptTransaction(session, startTime, fn, options);
- }
- if (isMaxTimeMSExpiredError(err)) {
- err.addErrorLabel(error_1.MongoErrorLabel.UnknownTransactionCommitResult);
- }
- throw err;
- }
- if (session.inTransaction()) {
- return session.abortTransaction().then(() => maybeRetryOrThrow(err));
- }
- return maybeRetryOrThrow(err);
- });
- }
- function endTransaction(session, commandName, callback) {
-
- const txnState = session.transaction.state;
- if (txnState === transactions_1.TxnState.NO_TRANSACTION) {
- callback(new error_1.MongoTransactionError('No transaction started'));
- return;
- }
- if (commandName === 'commitTransaction') {
- if (txnState === transactions_1.TxnState.STARTING_TRANSACTION ||
- txnState === transactions_1.TxnState.TRANSACTION_COMMITTED_EMPTY) {
-
- session.transaction.transition(transactions_1.TxnState.TRANSACTION_COMMITTED_EMPTY);
- callback();
- return;
- }
- if (txnState === transactions_1.TxnState.TRANSACTION_ABORTED) {
- callback(new error_1.MongoTransactionError('Cannot call commitTransaction after calling abortTransaction'));
- return;
- }
- }
- else {
- if (txnState === transactions_1.TxnState.STARTING_TRANSACTION) {
-
- session.transaction.transition(transactions_1.TxnState.TRANSACTION_ABORTED);
- callback();
- return;
- }
- if (txnState === transactions_1.TxnState.TRANSACTION_ABORTED) {
- callback(new error_1.MongoTransactionError('Cannot call abortTransaction twice'));
- return;
- }
- if (txnState === transactions_1.TxnState.TRANSACTION_COMMITTED ||
- txnState === transactions_1.TxnState.TRANSACTION_COMMITTED_EMPTY) {
- callback(new error_1.MongoTransactionError('Cannot call abortTransaction after calling commitTransaction'));
- return;
- }
- }
-
- const command = { [commandName]: 1 };
-
- let writeConcern;
- if (session.transaction.options.writeConcern) {
- writeConcern = Object.assign({}, session.transaction.options.writeConcern);
- }
- else if (session.clientOptions && session.clientOptions.writeConcern) {
- writeConcern = { w: session.clientOptions.writeConcern.w };
- }
- if (txnState === transactions_1.TxnState.TRANSACTION_COMMITTED) {
- writeConcern = Object.assign({ wtimeout: 10000 }, writeConcern, { w: 'majority' });
- }
- if (writeConcern) {
- Object.assign(command, { writeConcern });
- }
- if (commandName === 'commitTransaction' && session.transaction.options.maxTimeMS) {
- Object.assign(command, { maxTimeMS: session.transaction.options.maxTimeMS });
- }
- function commandHandler(error, result) {
- if (commandName !== 'commitTransaction') {
- session.transaction.transition(transactions_1.TxnState.TRANSACTION_ABORTED);
- if (session.loadBalanced) {
- maybeClearPinnedConnection(session, { force: false });
- }
-
- return callback();
- }
- session.transaction.transition(transactions_1.TxnState.TRANSACTION_COMMITTED);
- if (error instanceof error_1.MongoError) {
- if (error.hasErrorLabel(error_1.MongoErrorLabel.RetryableWriteError) ||
- error instanceof error_1.MongoWriteConcernError ||
- isMaxTimeMSExpiredError(error)) {
- if (isUnknownTransactionCommitResult(error)) {
- error.addErrorLabel(error_1.MongoErrorLabel.UnknownTransactionCommitResult);
-
- session.unpin({ error });
- }
- }
- else if (error.hasErrorLabel(error_1.MongoErrorLabel.TransientTransactionError)) {
- session.unpin({ error });
- }
- }
- callback(error, result);
- }
- if (session.transaction.recoveryToken) {
- command.recoveryToken = session.transaction.recoveryToken;
- }
-
- (0, execute_operation_1.executeOperation)(session.client, new run_command_1.RunAdminCommandOperation(undefined, command, {
- session,
- readPreference: read_preference_1.ReadPreference.primary,
- bypassPinningCheck: true
- }), (error, result) => {
- if (command.abortTransaction) {
-
- session.unpin();
- }
- if (error instanceof error_1.MongoError && error.hasErrorLabel(error_1.MongoErrorLabel.RetryableWriteError)) {
-
- if (command.commitTransaction) {
-
- session.unpin({ force: true });
- command.writeConcern = Object.assign({ wtimeout: 10000 }, command.writeConcern, {
- w: 'majority'
- });
- }
- return (0, execute_operation_1.executeOperation)(session.client, new run_command_1.RunAdminCommandOperation(undefined, command, {
- session,
- readPreference: read_preference_1.ReadPreference.primary,
- bypassPinningCheck: true
- }), commandHandler);
- }
- commandHandler(error, result);
- });
- }
- class ServerSession {
-
- constructor() {
- this.id = { id: new bson_1.Binary((0, utils_1.uuidV4)(), bson_1.Binary.SUBTYPE_UUID) };
- this.lastUse = (0, utils_1.now)();
- this.txnNumber = 0;
- this.isDirty = false;
- }
-
- hasTimedOut(sessionTimeoutMinutes) {
-
-
- const idleTimeMinutes = Math.round((((0, utils_1.calculateDurationInMs)(this.lastUse) % 86400000) % 3600000) / 60000);
- return idleTimeMinutes > sessionTimeoutMinutes - 1;
- }
-
- static clone(serverSession) {
- const arrayBuffer = new ArrayBuffer(16);
- const idBytes = Buffer.from(arrayBuffer);
- idBytes.set(serverSession.id.id.buffer);
- const id = new bson_1.Binary(idBytes, serverSession.id.id.sub_type);
-
- return Object.setPrototypeOf({
- id: { id },
- lastUse: serverSession.lastUse,
- txnNumber: serverSession.txnNumber,
- isDirty: serverSession.isDirty
- }, ServerSession.prototype);
- }
- }
- exports.ServerSession = ServerSession;
- class ServerSessionPool {
- constructor(topology) {
- if (topology == null) {
- throw new error_1.MongoRuntimeError('ServerSessionPool requires a topology');
- }
- this.topology = topology;
- this.sessions = [];
- }
-
- endAllPooledSessions(callback) {
- if (this.sessions.length) {
- this.topology.endSessions(this.sessions.map((session) => session.id), () => {
- this.sessions = [];
- if (typeof callback === 'function') {
- callback();
- }
- });
- return;
- }
- if (typeof callback === 'function') {
- callback();
- }
- }
-
- acquire() {
- const sessionTimeoutMinutes = this.topology.logicalSessionTimeoutMinutes || 10;
- while (this.sessions.length) {
- const session = this.sessions.shift();
- if (session && (this.topology.loadBalanced || !session.hasTimedOut(sessionTimeoutMinutes))) {
- return session;
- }
- }
- return new ServerSession();
- }
-
- release(session) {
- const sessionTimeoutMinutes = this.topology.logicalSessionTimeoutMinutes;
- if (this.topology.loadBalanced && !sessionTimeoutMinutes) {
- this.sessions.unshift(session);
- }
- if (!sessionTimeoutMinutes) {
- return;
- }
- while (this.sessions.length) {
- const pooledSession = this.sessions[this.sessions.length - 1];
- if (pooledSession.hasTimedOut(sessionTimeoutMinutes)) {
- this.sessions.pop();
- }
- else {
- break;
- }
- }
- if (!session.hasTimedOut(sessionTimeoutMinutes)) {
- if (session.isDirty) {
- return;
- }
-
- this.sessions.unshift(session);
- }
- }
- }
- exports.ServerSessionPool = ServerSessionPool;
- function applySession(session, command, options) {
- var _b, _c;
- if (session.hasEnded) {
- return new error_1.MongoExpiredSessionError();
- }
-
- const serverSession = session.serverSession;
- if (serverSession == null) {
- return new error_1.MongoRuntimeError('Unable to acquire server session');
- }
- if (((_b = options.writeConcern) === null || _b === void 0 ? void 0 : _b.w) === 0) {
- if (session && session.explicit) {
-
- return new error_1.MongoAPIError('Cannot have explicit session with unacknowledged writes');
- }
- return;
- }
-
- serverSession.lastUse = (0, utils_1.now)();
- command.lsid = serverSession.id;
- const inTxnOrTxnCommand = session.inTransaction() || (0, transactions_1.isTransactionCommand)(command);
- const isRetryableWrite = !!options.willRetryWrite;
- if (isRetryableWrite || inTxnOrTxnCommand) {
- serverSession.txnNumber += session[kTxnNumberIncrement];
- session[kTxnNumberIncrement] = 0;
- command.txnNumber = bson_1.Long.fromNumber(serverSession.txnNumber);
- }
- if (!inTxnOrTxnCommand) {
- if (session.transaction.state !== transactions_1.TxnState.NO_TRANSACTION) {
- session.transaction.transition(transactions_1.TxnState.NO_TRANSACTION);
- }
- if (session.supports.causalConsistency &&
- session.operationTime &&
- (0, utils_1.commandSupportsReadConcern)(command, options)) {
- command.readConcern = command.readConcern || {};
- Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
- }
- else if (session[kSnapshotEnabled]) {
- command.readConcern = command.readConcern || { level: read_concern_1.ReadConcernLevel.snapshot };
- if (session[kSnapshotTime] != null) {
- Object.assign(command.readConcern, { atClusterTime: session[kSnapshotTime] });
- }
- }
- return;
- }
-
-
- command.autocommit = false;
- if (session.transaction.state === transactions_1.TxnState.STARTING_TRANSACTION) {
- session.transaction.transition(transactions_1.TxnState.TRANSACTION_IN_PROGRESS);
- command.startTransaction = true;
- const readConcern = session.transaction.options.readConcern || ((_c = session === null || session === void 0 ? void 0 : session.clientOptions) === null || _c === void 0 ? void 0 : _c.readConcern);
- if (readConcern) {
- command.readConcern = readConcern;
- }
- if (session.supports.causalConsistency && session.operationTime) {
- command.readConcern = command.readConcern || {};
- Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
- }
- }
- return;
- }
- exports.applySession = applySession;
- function updateSessionFromResponse(session, document) {
- var _b;
- if (document.$clusterTime) {
- (0, common_1._advanceClusterTime)(session, document.$clusterTime);
- }
- if (document.operationTime && session && session.supports.causalConsistency) {
- session.advanceOperationTime(document.operationTime);
- }
- if (document.recoveryToken && session && session.inTransaction()) {
- session.transaction._recoveryToken = document.recoveryToken;
- }
- if ((session === null || session === void 0 ? void 0 : session[kSnapshotEnabled]) && session[kSnapshotTime] == null) {
-
-
- const atClusterTime = ((_b = document.cursor) === null || _b === void 0 ? void 0 : _b.atClusterTime) || document.atClusterTime;
- if (atClusterTime) {
- session[kSnapshotTime] = atClusterTime;
- }
- }
- }
- exports.updateSessionFromResponse = updateSessionFromResponse;
|