mongo_client.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MongoClient = exports.ServerApiVersion = void 0;
  4. const util_1 = require("util");
  5. const bson_1 = require("./bson");
  6. const change_stream_1 = require("./change_stream");
  7. const mongo_credentials_1 = require("./cmap/auth/mongo_credentials");
  8. const providers_1 = require("./cmap/auth/providers");
  9. const connection_string_1 = require("./connection_string");
  10. const constants_1 = require("./constants");
  11. const db_1 = require("./db");
  12. const error_1 = require("./error");
  13. const mongo_logger_1 = require("./mongo_logger");
  14. const mongo_types_1 = require("./mongo_types");
  15. const read_preference_1 = require("./read_preference");
  16. const server_selection_1 = require("./sdam/server_selection");
  17. const topology_1 = require("./sdam/topology");
  18. const sessions_1 = require("./sessions");
  19. const utils_1 = require("./utils");
  20. /** @public */
  21. exports.ServerApiVersion = Object.freeze({
  22. v1: '1'
  23. });
  24. /** @internal */
  25. const kOptions = Symbol('options');
  26. /**
  27. * The **MongoClient** class is a class that allows for making Connections to MongoDB.
  28. * @public
  29. *
  30. * @remarks
  31. * The programmatically provided options take precedence over the URI options.
  32. *
  33. * @example
  34. * ```ts
  35. * import { MongoClient } from 'mongodb';
  36. *
  37. * // Enable command monitoring for debugging
  38. * const client = new MongoClient('mongodb://localhost:27017', { monitorCommands: true });
  39. *
  40. * client.on('commandStarted', started => console.log(started));
  41. * client.db().collection('pets');
  42. * await client.insertOne({ name: 'spot', kind: 'dog' });
  43. * ```
  44. */
  45. class MongoClient extends mongo_types_1.TypedEventEmitter {
  46. constructor(url, options) {
  47. super();
  48. this[kOptions] = (0, connection_string_1.parseOptions)(url, this, options);
  49. this.mongoLogger = new mongo_logger_1.MongoLogger(this[kOptions].mongoLoggerOptions);
  50. // eslint-disable-next-line @typescript-eslint/no-this-alias
  51. const client = this;
  52. // The internal state
  53. this.s = {
  54. url,
  55. bsonOptions: (0, bson_1.resolveBSONOptions)(this[kOptions]),
  56. namespace: (0, utils_1.ns)('admin'),
  57. hasBeenClosed: false,
  58. sessionPool: new sessions_1.ServerSessionPool(this),
  59. activeSessions: new Set(),
  60. get options() {
  61. return client[kOptions];
  62. },
  63. get readConcern() {
  64. return client[kOptions].readConcern;
  65. },
  66. get writeConcern() {
  67. return client[kOptions].writeConcern;
  68. },
  69. get readPreference() {
  70. return client[kOptions].readPreference;
  71. },
  72. get isMongoClient() {
  73. return true;
  74. }
  75. };
  76. }
  77. /** @see MongoOptions */
  78. get options() {
  79. return Object.freeze({ ...this[kOptions] });
  80. }
  81. get serverApi() {
  82. return this[kOptions].serverApi && Object.freeze({ ...this[kOptions].serverApi });
  83. }
  84. /**
  85. * Intended for APM use only
  86. * @internal
  87. */
  88. get monitorCommands() {
  89. return this[kOptions].monitorCommands;
  90. }
  91. set monitorCommands(value) {
  92. this[kOptions].monitorCommands = value;
  93. }
  94. /**
  95. * @deprecated This method will be removed in the next major version.
  96. */
  97. get autoEncrypter() {
  98. return this[kOptions].autoEncrypter;
  99. }
  100. get readConcern() {
  101. return this.s.readConcern;
  102. }
  103. get writeConcern() {
  104. return this.s.writeConcern;
  105. }
  106. get readPreference() {
  107. return this.s.readPreference;
  108. }
  109. get bsonOptions() {
  110. return this.s.bsonOptions;
  111. }
  112. /**
  113. * Connect to MongoDB using a url
  114. *
  115. * @see docs.mongodb.org/manual/reference/connection-string/
  116. */
  117. async connect() {
  118. if (this.connectionLock) {
  119. return this.connectionLock;
  120. }
  121. try {
  122. this.connectionLock = this._connect();
  123. await this.connectionLock;
  124. }
  125. finally {
  126. // release
  127. this.connectionLock = undefined;
  128. }
  129. return this;
  130. }
  131. /**
  132. * Create a topology to open the connection, must be locked to avoid topology leaks in concurrency scenario.
  133. * Locking is enforced by the connect method.
  134. *
  135. * @internal
  136. */
  137. async _connect() {
  138. if (this.topology && this.topology.isConnected()) {
  139. return this;
  140. }
  141. const options = this[kOptions];
  142. if (typeof options.srvHost === 'string') {
  143. const hosts = await (0, connection_string_1.resolveSRVRecord)(options);
  144. for (const [index, host] of hosts.entries()) {
  145. options.hosts[index] = host;
  146. }
  147. }
  148. // It is important to perform validation of hosts AFTER SRV resolution, to check the real hostname,
  149. // but BEFORE we even attempt connecting with a potentially not allowed hostname
  150. if (options.credentials?.mechanism === providers_1.AuthMechanism.MONGODB_OIDC) {
  151. const allowedHosts = options.credentials?.mechanismProperties?.ALLOWED_HOSTS || mongo_credentials_1.DEFAULT_ALLOWED_HOSTS;
  152. const isServiceAuth = !!options.credentials?.mechanismProperties?.PROVIDER_NAME;
  153. if (!isServiceAuth) {
  154. for (const host of options.hosts) {
  155. if (!(0, utils_1.hostMatchesWildcards)(host.toHostPort().host, allowedHosts)) {
  156. throw new error_1.MongoInvalidArgumentError(`Host '${host}' is not valid for OIDC authentication with ALLOWED_HOSTS of '${allowedHosts.join(',')}'`);
  157. }
  158. }
  159. }
  160. }
  161. this.topology = new topology_1.Topology(this, options.hosts, options);
  162. // Events can be emitted before initialization is complete so we have to
  163. // save the reference to the topology on the client ASAP if the event handlers need to access it
  164. this.topology.once(topology_1.Topology.OPEN, () => this.emit('open', this));
  165. for (const event of constants_1.MONGO_CLIENT_EVENTS) {
  166. this.topology.on(event, (...args) => this.emit(event, ...args));
  167. }
  168. const topologyConnect = async () => {
  169. try {
  170. await (0, util_1.promisify)(callback => this.topology?.connect(options, callback))();
  171. }
  172. catch (error) {
  173. this.topology?.close({ force: true });
  174. throw error;
  175. }
  176. };
  177. if (this.autoEncrypter) {
  178. const initAutoEncrypter = (0, util_1.promisify)(callback => this.autoEncrypter?.init(callback));
  179. await initAutoEncrypter();
  180. await topologyConnect();
  181. await options.encrypter.connectInternalClient();
  182. }
  183. else {
  184. await topologyConnect();
  185. }
  186. return this;
  187. }
  188. /**
  189. * Close the client and its underlying connections
  190. *
  191. * @param force - Force close, emitting no events
  192. */
  193. async close(force = false) {
  194. // There's no way to set hasBeenClosed back to false
  195. Object.defineProperty(this.s, 'hasBeenClosed', {
  196. value: true,
  197. enumerable: true,
  198. configurable: false,
  199. writable: false
  200. });
  201. const activeSessionEnds = Array.from(this.s.activeSessions, session => session.endSession());
  202. this.s.activeSessions.clear();
  203. await Promise.all(activeSessionEnds);
  204. if (this.topology == null) {
  205. return;
  206. }
  207. // If we would attempt to select a server and get nothing back we short circuit
  208. // to avoid the server selection timeout.
  209. const selector = (0, server_selection_1.readPreferenceServerSelector)(read_preference_1.ReadPreference.primaryPreferred);
  210. const topologyDescription = this.topology.description;
  211. const serverDescriptions = Array.from(topologyDescription.servers.values());
  212. const servers = selector(topologyDescription, serverDescriptions);
  213. if (servers.length !== 0) {
  214. const endSessions = Array.from(this.s.sessionPool.sessions, ({ id }) => id);
  215. if (endSessions.length !== 0) {
  216. await this.db('admin')
  217. .command({ endSessions }, { readPreference: read_preference_1.ReadPreference.primaryPreferred, noResponse: true })
  218. .catch(() => null); // outcome does not matter
  219. }
  220. }
  221. // clear out references to old topology
  222. const topology = this.topology;
  223. this.topology = undefined;
  224. await new Promise((resolve, reject) => {
  225. topology.close({ force }, error => {
  226. if (error)
  227. return reject(error);
  228. const { encrypter } = this[kOptions];
  229. if (encrypter) {
  230. return encrypter.close(this, force, error => {
  231. if (error)
  232. return reject(error);
  233. resolve();
  234. });
  235. }
  236. resolve();
  237. });
  238. });
  239. }
  240. /**
  241. * Create a new Db instance sharing the current socket connections.
  242. *
  243. * @param dbName - The name of the database we want to use. If not provided, use database name from connection string.
  244. * @param options - Optional settings for Db construction
  245. */
  246. db(dbName, options) {
  247. options = options ?? {};
  248. // Default to db from connection string if not provided
  249. if (!dbName) {
  250. dbName = this.options.dbName;
  251. }
  252. // Copy the options and add out internal override of the not shared flag
  253. const finalOptions = Object.assign({}, this[kOptions], options);
  254. // Return the db object
  255. const db = new db_1.Db(this, dbName, finalOptions);
  256. // Return the database
  257. return db;
  258. }
  259. /**
  260. * Connect to MongoDB using a url
  261. *
  262. * @remarks
  263. * The programmatically provided options take precedence over the URI options.
  264. *
  265. * @see https://www.mongodb.com/docs/manual/reference/connection-string/
  266. */
  267. static async connect(url, options) {
  268. const client = new this(url, options);
  269. return client.connect();
  270. }
  271. /** Starts a new session on the server */
  272. startSession(options) {
  273. const session = new sessions_1.ClientSession(this, this.s.sessionPool, { explicit: true, ...options }, this[kOptions]);
  274. this.s.activeSessions.add(session);
  275. session.once('ended', () => {
  276. this.s.activeSessions.delete(session);
  277. });
  278. return session;
  279. }
  280. async withSession(optionsOrOperation, callback) {
  281. const options = {
  282. // Always define an owner
  283. owner: Symbol(),
  284. // If it's an object inherit the options
  285. ...(typeof optionsOrOperation === 'object' ? optionsOrOperation : {})
  286. };
  287. const withSessionCallback = typeof optionsOrOperation === 'function' ? optionsOrOperation : callback;
  288. if (withSessionCallback == null) {
  289. throw new error_1.MongoInvalidArgumentError('Missing required callback parameter');
  290. }
  291. const session = this.startSession(options);
  292. try {
  293. await withSessionCallback(session);
  294. }
  295. finally {
  296. try {
  297. await session.endSession();
  298. }
  299. catch {
  300. // We are not concerned with errors from endSession()
  301. }
  302. }
  303. }
  304. /**
  305. * Create a new Change Stream, watching for new changes (insertions, updates,
  306. * replacements, deletions, and invalidations) in this cluster. Will ignore all
  307. * changes to system collections, as well as the local, admin, and config databases.
  308. *
  309. * @remarks
  310. * watch() accepts two generic arguments for distinct use cases:
  311. * - The first is to provide the schema that may be defined for all the data within the current cluster
  312. * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument
  313. *
  314. * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
  315. * @param options - Optional settings for the command
  316. * @typeParam TSchema - Type of the data being detected by the change stream
  317. * @typeParam TChange - Type of the whole change stream document emitted
  318. */
  319. watch(pipeline = [], options = {}) {
  320. // Allow optionally not specifying a pipeline
  321. if (!Array.isArray(pipeline)) {
  322. options = pipeline;
  323. pipeline = [];
  324. }
  325. return new change_stream_1.ChangeStream(this, pipeline, (0, utils_1.resolveOptions)(this, options));
  326. }
  327. }
  328. exports.MongoClient = MongoClient;
  329. //# sourceMappingURL=mongo_client.js.map