connection_string.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DEFAULT_OPTIONS = exports.OPTIONS = void 0;
  4. exports.resolveSRVRecord = resolveSRVRecord;
  5. exports.parseOptions = parseOptions;
  6. const dns = require("dns");
  7. const mongodb_connection_string_url_1 = require("mongodb-connection-string-url");
  8. const url_1 = require("url");
  9. const mongo_credentials_1 = require("./cmap/auth/mongo_credentials");
  10. const providers_1 = require("./cmap/auth/providers");
  11. const compression_1 = require("./cmap/wire_protocol/compression");
  12. const encrypter_1 = require("./encrypter");
  13. const error_1 = require("./error");
  14. const mongo_client_1 = require("./mongo_client");
  15. const mongo_logger_1 = require("./mongo_logger");
  16. const read_concern_1 = require("./read_concern");
  17. const read_preference_1 = require("./read_preference");
  18. const monitor_1 = require("./sdam/monitor");
  19. const utils_1 = require("./utils");
  20. const write_concern_1 = require("./write_concern");
  21. const VALID_TXT_RECORDS = ['authSource', 'replicaSet', 'loadBalanced'];
  22. const LB_SINGLE_HOST_ERROR = 'loadBalanced option only supported with a single host in the URI';
  23. const LB_REPLICA_SET_ERROR = 'loadBalanced option not supported with a replicaSet option';
  24. const LB_DIRECT_CONNECTION_ERROR = 'loadBalanced option not supported when directConnection is provided';
  25. function retryDNSTimeoutFor(api) {
  26. return async function dnsReqRetryTimeout(lookupAddress) {
  27. try {
  28. return await dns.promises[api](lookupAddress);
  29. }
  30. catch (firstDNSError) {
  31. if (firstDNSError.code === dns.TIMEOUT) {
  32. return await dns.promises[api](lookupAddress);
  33. }
  34. else {
  35. throw firstDNSError;
  36. }
  37. }
  38. };
  39. }
  40. const resolveSrv = retryDNSTimeoutFor('resolveSrv');
  41. const resolveTxt = retryDNSTimeoutFor('resolveTxt');
  42. /**
  43. * Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal
  44. * connection string.
  45. *
  46. * @param uri - The connection string to parse
  47. * @param options - Optional user provided connection string options
  48. */
  49. async function resolveSRVRecord(options) {
  50. if (typeof options.srvHost !== 'string') {
  51. throw new error_1.MongoAPIError('Option "srvHost" must not be empty');
  52. }
  53. // Asynchronously start TXT resolution so that we do not have to wait until
  54. // the SRV record is resolved before starting a second DNS query.
  55. const lookupAddress = options.srvHost;
  56. const txtResolutionPromise = resolveTxt(lookupAddress);
  57. txtResolutionPromise.then(undefined, utils_1.squashError); // rejections will be handled later
  58. const hostname = `_${options.srvServiceName}._tcp.${lookupAddress}`;
  59. // Resolve the SRV record and use the result as the list of hosts to connect to.
  60. const addresses = await resolveSrv(hostname);
  61. if (addresses.length === 0) {
  62. throw new error_1.MongoAPIError('No addresses found at host');
  63. }
  64. for (const { name } of addresses) {
  65. (0, utils_1.checkParentDomainMatch)(name, lookupAddress);
  66. }
  67. const hostAddresses = addresses.map(r => utils_1.HostAddress.fromString(`${r.name}:${r.port ?? 27017}`));
  68. validateLoadBalancedOptions(hostAddresses, options, true);
  69. // Use the result of resolving the TXT record and add options from there if they exist.
  70. let record;
  71. try {
  72. record = await txtResolutionPromise;
  73. }
  74. catch (error) {
  75. if (error.code !== 'ENODATA' && error.code !== 'ENOTFOUND') {
  76. throw error;
  77. }
  78. return hostAddresses;
  79. }
  80. if (record.length > 1) {
  81. throw new error_1.MongoParseError('Multiple text records not allowed');
  82. }
  83. const txtRecordOptions = new url_1.URLSearchParams(record[0].join(''));
  84. const txtRecordOptionKeys = [...txtRecordOptions.keys()];
  85. if (txtRecordOptionKeys.some(key => !VALID_TXT_RECORDS.includes(key))) {
  86. throw new error_1.MongoParseError(`Text record may only set any of: ${VALID_TXT_RECORDS.join(', ')}`);
  87. }
  88. if (VALID_TXT_RECORDS.some(option => txtRecordOptions.get(option) === '')) {
  89. throw new error_1.MongoParseError('Cannot have empty URI params in DNS TXT Record');
  90. }
  91. const source = txtRecordOptions.get('authSource') ?? undefined;
  92. const replicaSet = txtRecordOptions.get('replicaSet') ?? undefined;
  93. const loadBalanced = txtRecordOptions.get('loadBalanced') ?? undefined;
  94. if (!options.userSpecifiedAuthSource &&
  95. source &&
  96. options.credentials &&
  97. !providers_1.AUTH_MECHS_AUTH_SRC_EXTERNAL.has(options.credentials.mechanism)) {
  98. options.credentials = mongo_credentials_1.MongoCredentials.merge(options.credentials, { source });
  99. }
  100. if (!options.userSpecifiedReplicaSet && replicaSet) {
  101. options.replicaSet = replicaSet;
  102. }
  103. if (loadBalanced === 'true') {
  104. options.loadBalanced = true;
  105. }
  106. if (options.replicaSet && options.srvMaxHosts > 0) {
  107. throw new error_1.MongoParseError('Cannot combine replicaSet option with srvMaxHosts');
  108. }
  109. validateLoadBalancedOptions(hostAddresses, options, true);
  110. return hostAddresses;
  111. }
  112. /**
  113. * Checks if TLS options are valid
  114. *
  115. * @param allOptions - All options provided by user or included in default options map
  116. * @throws MongoAPIError if TLS options are invalid
  117. */
  118. function checkTLSOptions(allOptions) {
  119. if (!allOptions)
  120. return;
  121. const check = (a, b) => {
  122. if (allOptions.has(a) && allOptions.has(b)) {
  123. throw new error_1.MongoAPIError(`The '${a}' option cannot be used with the '${b}' option`);
  124. }
  125. };
  126. check('tlsInsecure', 'tlsAllowInvalidCertificates');
  127. check('tlsInsecure', 'tlsAllowInvalidHostnames');
  128. }
  129. function getBoolean(name, value) {
  130. if (typeof value === 'boolean')
  131. return value;
  132. switch (value) {
  133. case 'true':
  134. return true;
  135. case 'false':
  136. return false;
  137. default:
  138. throw new error_1.MongoParseError(`${name} must be either "true" or "false"`);
  139. }
  140. }
  141. function getIntFromOptions(name, value) {
  142. const parsedInt = (0, utils_1.parseInteger)(value);
  143. if (parsedInt != null) {
  144. return parsedInt;
  145. }
  146. throw new error_1.MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`);
  147. }
  148. function getUIntFromOptions(name, value) {
  149. const parsedValue = getIntFromOptions(name, value);
  150. if (parsedValue < 0) {
  151. throw new error_1.MongoParseError(`${name} can only be a positive int value, got: ${value}`);
  152. }
  153. return parsedValue;
  154. }
  155. function* entriesFromString(value) {
  156. if (value === '') {
  157. return;
  158. }
  159. const keyValuePairs = value.split(',');
  160. for (const keyValue of keyValuePairs) {
  161. const [key, value] = keyValue.split(/:(.*)/);
  162. if (value == null) {
  163. throw new error_1.MongoParseError('Cannot have undefined values in key value pairs');
  164. }
  165. yield [key, value];
  166. }
  167. }
  168. class CaseInsensitiveMap extends Map {
  169. constructor(entries = []) {
  170. super(entries.map(([k, v]) => [k.toLowerCase(), v]));
  171. }
  172. has(k) {
  173. return super.has(k.toLowerCase());
  174. }
  175. get(k) {
  176. return super.get(k.toLowerCase());
  177. }
  178. set(k, v) {
  179. return super.set(k.toLowerCase(), v);
  180. }
  181. delete(k) {
  182. return super.delete(k.toLowerCase());
  183. }
  184. }
  185. function parseOptions(uri, mongoClient = undefined, options = {}) {
  186. if (mongoClient != null && !(mongoClient instanceof mongo_client_1.MongoClient)) {
  187. options = mongoClient;
  188. mongoClient = undefined;
  189. }
  190. // validate BSONOptions
  191. if (options.useBigInt64 && typeof options.promoteLongs === 'boolean' && !options.promoteLongs) {
  192. throw new error_1.MongoAPIError('Must request either bigint or Long for int64 deserialization');
  193. }
  194. if (options.useBigInt64 && typeof options.promoteValues === 'boolean' && !options.promoteValues) {
  195. throw new error_1.MongoAPIError('Must request either bigint or Long for int64 deserialization');
  196. }
  197. const url = new mongodb_connection_string_url_1.default(uri);
  198. const { hosts, isSRV } = url;
  199. const mongoOptions = Object.create(null);
  200. mongoOptions.hosts = isSRV ? [] : hosts.map(utils_1.HostAddress.fromString);
  201. const urlOptions = new CaseInsensitiveMap();
  202. if (url.pathname !== '/' && url.pathname !== '') {
  203. const dbName = decodeURIComponent(url.pathname[0] === '/' ? url.pathname.slice(1) : url.pathname);
  204. if (dbName) {
  205. urlOptions.set('dbName', [dbName]);
  206. }
  207. }
  208. if (url.username !== '') {
  209. const auth = {
  210. username: decodeURIComponent(url.username)
  211. };
  212. if (typeof url.password === 'string') {
  213. auth.password = decodeURIComponent(url.password);
  214. }
  215. urlOptions.set('auth', [auth]);
  216. }
  217. for (const key of url.searchParams.keys()) {
  218. const values = url.searchParams.getAll(key);
  219. const isReadPreferenceTags = /readPreferenceTags/i.test(key);
  220. if (!isReadPreferenceTags && values.length > 1) {
  221. throw new error_1.MongoInvalidArgumentError(`URI option "${key}" cannot appear more than once in the connection string`);
  222. }
  223. if (!isReadPreferenceTags && values.includes('')) {
  224. throw new error_1.MongoAPIError(`URI option "${key}" cannot be specified with no value`);
  225. }
  226. if (!urlOptions.has(key)) {
  227. urlOptions.set(key, values);
  228. }
  229. }
  230. const objectOptions = new CaseInsensitiveMap(Object.entries(options).filter(([, v]) => v != null));
  231. // Validate options that can only be provided by one of uri or object
  232. if (urlOptions.has('serverApi')) {
  233. throw new error_1.MongoParseError('URI cannot contain `serverApi`, it can only be passed to the client');
  234. }
  235. const uriMechanismProperties = urlOptions.get('authMechanismProperties');
  236. if (uriMechanismProperties) {
  237. for (const property of uriMechanismProperties) {
  238. if (/(^|,)ALLOWED_HOSTS:/.test(property)) {
  239. throw new error_1.MongoParseError('Auth mechanism property ALLOWED_HOSTS is not allowed in the connection string.');
  240. }
  241. }
  242. }
  243. if (objectOptions.has('loadBalanced')) {
  244. throw new error_1.MongoParseError('loadBalanced is only a valid option in the URI');
  245. }
  246. // All option collection
  247. const allProvidedOptions = new CaseInsensitiveMap();
  248. const allProvidedKeys = new Set([...urlOptions.keys(), ...objectOptions.keys()]);
  249. for (const key of allProvidedKeys) {
  250. const values = [];
  251. const objectOptionValue = objectOptions.get(key);
  252. if (objectOptionValue != null) {
  253. values.push(objectOptionValue);
  254. }
  255. const urlValues = urlOptions.get(key) ?? [];
  256. values.push(...urlValues);
  257. allProvidedOptions.set(key, values);
  258. }
  259. if (allProvidedOptions.has('tls') || allProvidedOptions.has('ssl')) {
  260. const tlsAndSslOpts = (allProvidedOptions.get('tls') || [])
  261. .concat(allProvidedOptions.get('ssl') || [])
  262. .map(getBoolean.bind(null, 'tls/ssl'));
  263. if (new Set(tlsAndSslOpts).size !== 1) {
  264. throw new error_1.MongoParseError('All values of tls/ssl must be the same.');
  265. }
  266. }
  267. checkTLSOptions(allProvidedOptions);
  268. const unsupportedOptions = (0, utils_1.setDifference)(allProvidedKeys, Array.from(Object.keys(exports.OPTIONS)).map(s => s.toLowerCase()));
  269. if (unsupportedOptions.size !== 0) {
  270. const optionWord = unsupportedOptions.size > 1 ? 'options' : 'option';
  271. const isOrAre = unsupportedOptions.size > 1 ? 'are' : 'is';
  272. throw new error_1.MongoParseError(`${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported`);
  273. }
  274. // Option parsing and setting
  275. for (const [key, descriptor] of Object.entries(exports.OPTIONS)) {
  276. const values = allProvidedOptions.get(key);
  277. if (!values || values.length === 0) {
  278. if (exports.DEFAULT_OPTIONS.has(key)) {
  279. setOption(mongoOptions, key, descriptor, [exports.DEFAULT_OPTIONS.get(key)]);
  280. }
  281. }
  282. else {
  283. const { deprecated } = descriptor;
  284. if (deprecated) {
  285. const deprecatedMsg = typeof deprecated === 'string' ? `: ${deprecated}` : '';
  286. (0, utils_1.emitWarning)(`${key} is a deprecated option${deprecatedMsg}`);
  287. }
  288. setOption(mongoOptions, key, descriptor, values);
  289. }
  290. }
  291. if (mongoOptions.credentials) {
  292. const isGssapi = mongoOptions.credentials.mechanism === providers_1.AuthMechanism.MONGODB_GSSAPI;
  293. const isX509 = mongoOptions.credentials.mechanism === providers_1.AuthMechanism.MONGODB_X509;
  294. const isAws = mongoOptions.credentials.mechanism === providers_1.AuthMechanism.MONGODB_AWS;
  295. const isOidc = mongoOptions.credentials.mechanism === providers_1.AuthMechanism.MONGODB_OIDC;
  296. if ((isGssapi || isX509) &&
  297. allProvidedOptions.has('authSource') &&
  298. mongoOptions.credentials.source !== '$external') {
  299. // If authSource was explicitly given and its incorrect, we error
  300. throw new error_1.MongoParseError(`authMechanism ${mongoOptions.credentials.mechanism} requires an authSource of '$external'`);
  301. }
  302. if (!(isGssapi || isX509 || isAws || isOidc) &&
  303. mongoOptions.dbName &&
  304. !allProvidedOptions.has('authSource')) {
  305. // inherit the dbName unless GSSAPI or X509, then silently ignore dbName
  306. // and there was no specific authSource given
  307. mongoOptions.credentials = mongo_credentials_1.MongoCredentials.merge(mongoOptions.credentials, {
  308. source: mongoOptions.dbName
  309. });
  310. }
  311. if (isAws) {
  312. const { username, password } = mongoOptions.credentials;
  313. if (username || password) {
  314. throw new error_1.MongoAPIError('username and password cannot be provided when using MONGODB-AWS. Credentials must be provided in a manner that can be read by the AWS SDK.');
  315. }
  316. if (mongoOptions.credentials.mechanismProperties.AWS_SESSION_TOKEN) {
  317. throw new error_1.MongoAPIError('AWS_SESSION_TOKEN cannot be provided when using MONGODB-AWS. Credentials must be provided in a manner that can be read by the AWS SDK.');
  318. }
  319. }
  320. mongoOptions.credentials.validate();
  321. // Check if the only auth related option provided was authSource, if so we can remove credentials
  322. if (mongoOptions.credentials.password === '' &&
  323. mongoOptions.credentials.username === '' &&
  324. mongoOptions.credentials.mechanism === providers_1.AuthMechanism.MONGODB_DEFAULT &&
  325. Object.keys(mongoOptions.credentials.mechanismProperties).length === 0) {
  326. delete mongoOptions.credentials;
  327. }
  328. }
  329. if (!mongoOptions.dbName) {
  330. // dbName default is applied here because of the credential validation above
  331. mongoOptions.dbName = 'test';
  332. }
  333. validateLoadBalancedOptions(hosts, mongoOptions, isSRV);
  334. if (mongoClient && mongoOptions.autoEncryption) {
  335. encrypter_1.Encrypter.checkForMongoCrypt();
  336. mongoOptions.encrypter = new encrypter_1.Encrypter(mongoClient, uri, options);
  337. mongoOptions.autoEncrypter = mongoOptions.encrypter.autoEncrypter;
  338. }
  339. // Potential SRV Overrides and SRV connection string validations
  340. mongoOptions.userSpecifiedAuthSource =
  341. objectOptions.has('authSource') || urlOptions.has('authSource');
  342. mongoOptions.userSpecifiedReplicaSet =
  343. objectOptions.has('replicaSet') || urlOptions.has('replicaSet');
  344. if (isSRV) {
  345. // SRV Record is resolved upon connecting
  346. mongoOptions.srvHost = hosts[0];
  347. if (mongoOptions.directConnection) {
  348. throw new error_1.MongoAPIError('SRV URI does not support directConnection');
  349. }
  350. if (mongoOptions.srvMaxHosts > 0 && typeof mongoOptions.replicaSet === 'string') {
  351. throw new error_1.MongoParseError('Cannot use srvMaxHosts option with replicaSet');
  352. }
  353. // SRV turns on TLS by default, but users can override and turn it off
  354. const noUserSpecifiedTLS = !objectOptions.has('tls') && !urlOptions.has('tls');
  355. const noUserSpecifiedSSL = !objectOptions.has('ssl') && !urlOptions.has('ssl');
  356. if (noUserSpecifiedTLS && noUserSpecifiedSSL) {
  357. mongoOptions.tls = true;
  358. }
  359. }
  360. else {
  361. const userSpecifiedSrvOptions = urlOptions.has('srvMaxHosts') ||
  362. objectOptions.has('srvMaxHosts') ||
  363. urlOptions.has('srvServiceName') ||
  364. objectOptions.has('srvServiceName');
  365. if (userSpecifiedSrvOptions) {
  366. throw new error_1.MongoParseError('Cannot use srvMaxHosts or srvServiceName with a non-srv connection string');
  367. }
  368. }
  369. if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {
  370. throw new error_1.MongoParseError('directConnection option requires exactly one host');
  371. }
  372. if (!mongoOptions.proxyHost &&
  373. (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)) {
  374. throw new error_1.MongoParseError('Must specify proxyHost if other proxy options are passed');
  375. }
  376. if ((mongoOptions.proxyUsername && !mongoOptions.proxyPassword) ||
  377. (!mongoOptions.proxyUsername && mongoOptions.proxyPassword)) {
  378. throw new error_1.MongoParseError('Can only specify both of proxy username/password or neither');
  379. }
  380. const proxyOptions = ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword'].map(key => urlOptions.get(key) ?? []);
  381. if (proxyOptions.some(options => options.length > 1)) {
  382. throw new error_1.MongoParseError('Proxy options cannot be specified multiple times in the connection string');
  383. }
  384. mongoOptions.mongoLoggerOptions = mongo_logger_1.MongoLogger.resolveOptions({
  385. MONGODB_LOG_COMMAND: process.env.MONGODB_LOG_COMMAND,
  386. MONGODB_LOG_TOPOLOGY: process.env.MONGODB_LOG_TOPOLOGY,
  387. MONGODB_LOG_SERVER_SELECTION: process.env.MONGODB_LOG_SERVER_SELECTION,
  388. MONGODB_LOG_CONNECTION: process.env.MONGODB_LOG_CONNECTION,
  389. MONGODB_LOG_CLIENT: process.env.MONGODB_LOG_CLIENT,
  390. MONGODB_LOG_ALL: process.env.MONGODB_LOG_ALL,
  391. MONGODB_LOG_MAX_DOCUMENT_LENGTH: process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH,
  392. MONGODB_LOG_PATH: process.env.MONGODB_LOG_PATH
  393. }, {
  394. mongodbLogPath: mongoOptions.mongodbLogPath,
  395. mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities,
  396. mongodbLogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength
  397. });
  398. return mongoOptions;
  399. }
  400. /**
  401. * #### Throws if LB mode is true:
  402. * - hosts contains more than one host
  403. * - there is a replicaSet name set
  404. * - directConnection is set
  405. * - if srvMaxHosts is used when an srv connection string is passed in
  406. *
  407. * @throws MongoParseError
  408. */
  409. function validateLoadBalancedOptions(hosts, mongoOptions, isSrv) {
  410. if (mongoOptions.loadBalanced) {
  411. if (hosts.length > 1) {
  412. throw new error_1.MongoParseError(LB_SINGLE_HOST_ERROR);
  413. }
  414. if (mongoOptions.replicaSet) {
  415. throw new error_1.MongoParseError(LB_REPLICA_SET_ERROR);
  416. }
  417. if (mongoOptions.directConnection) {
  418. throw new error_1.MongoParseError(LB_DIRECT_CONNECTION_ERROR);
  419. }
  420. if (isSrv && mongoOptions.srvMaxHosts > 0) {
  421. throw new error_1.MongoParseError('Cannot limit srv hosts with loadBalanced enabled');
  422. }
  423. }
  424. return;
  425. }
  426. function setOption(mongoOptions, key, descriptor, values) {
  427. const { target, type, transform } = descriptor;
  428. const name = target ?? key;
  429. switch (type) {
  430. case 'boolean':
  431. mongoOptions[name] = getBoolean(name, values[0]);
  432. break;
  433. case 'int':
  434. mongoOptions[name] = getIntFromOptions(name, values[0]);
  435. break;
  436. case 'uint':
  437. mongoOptions[name] = getUIntFromOptions(name, values[0]);
  438. break;
  439. case 'string':
  440. if (values[0] == null) {
  441. break;
  442. }
  443. // The value should always be a string here, but since the array is typed as unknown
  444. // there still needs to be an explicit cast.
  445. // eslint-disable-next-line @typescript-eslint/no-base-to-string
  446. mongoOptions[name] = String(values[0]);
  447. break;
  448. case 'record':
  449. if (!(0, utils_1.isRecord)(values[0])) {
  450. throw new error_1.MongoParseError(`${name} must be an object`);
  451. }
  452. mongoOptions[name] = values[0];
  453. break;
  454. case 'any':
  455. mongoOptions[name] = values[0];
  456. break;
  457. default: {
  458. if (!transform) {
  459. throw new error_1.MongoParseError('Descriptors missing a type must define a transform');
  460. }
  461. const transformValue = transform({ name, options: mongoOptions, values });
  462. mongoOptions[name] = transformValue;
  463. break;
  464. }
  465. }
  466. }
  467. exports.OPTIONS = {
  468. appName: {
  469. type: 'string'
  470. },
  471. auth: {
  472. target: 'credentials',
  473. transform({ name, options, values: [value] }) {
  474. if (!(0, utils_1.isRecord)(value, ['username', 'password'])) {
  475. throw new error_1.MongoParseError(`${name} must be an object with 'username' and 'password' properties`);
  476. }
  477. return mongo_credentials_1.MongoCredentials.merge(options.credentials, {
  478. username: value.username,
  479. password: value.password
  480. });
  481. }
  482. },
  483. authMechanism: {
  484. target: 'credentials',
  485. transform({ options, values: [value] }) {
  486. const mechanisms = Object.values(providers_1.AuthMechanism);
  487. const [mechanism] = mechanisms.filter(m => m.match(RegExp(String.raw `\b${value}\b`, 'i')));
  488. if (!mechanism) {
  489. throw new error_1.MongoParseError(`authMechanism one of ${mechanisms}, got ${value}`);
  490. }
  491. let source = options.credentials?.source;
  492. if (mechanism === providers_1.AuthMechanism.MONGODB_PLAIN ||
  493. providers_1.AUTH_MECHS_AUTH_SRC_EXTERNAL.has(mechanism)) {
  494. // some mechanisms have '$external' as the Auth Source
  495. source = '$external';
  496. }
  497. let password = options.credentials?.password;
  498. if (mechanism === providers_1.AuthMechanism.MONGODB_X509 && password === '') {
  499. password = undefined;
  500. }
  501. return mongo_credentials_1.MongoCredentials.merge(options.credentials, {
  502. mechanism,
  503. source,
  504. password
  505. });
  506. }
  507. },
  508. // Note that if the authMechanismProperties contain a TOKEN_RESOURCE that has a
  509. // comma in it, it MUST be supplied as a MongoClient option instead of in the
  510. // connection string.
  511. authMechanismProperties: {
  512. target: 'credentials',
  513. transform({ options, values }) {
  514. // We can have a combination of options passed in the URI and options passed
  515. // as an object to the MongoClient. So we must transform the string options
  516. // as well as merge them together with a potentially provided object.
  517. let mechanismProperties = Object.create(null);
  518. for (const optionValue of values) {
  519. if (typeof optionValue === 'string') {
  520. for (const [key, value] of entriesFromString(optionValue)) {
  521. try {
  522. mechanismProperties[key] = getBoolean(key, value);
  523. }
  524. catch {
  525. mechanismProperties[key] = value;
  526. }
  527. }
  528. }
  529. else {
  530. if (!(0, utils_1.isRecord)(optionValue)) {
  531. throw new error_1.MongoParseError('AuthMechanismProperties must be an object');
  532. }
  533. mechanismProperties = { ...optionValue };
  534. }
  535. }
  536. return mongo_credentials_1.MongoCredentials.merge(options.credentials, {
  537. mechanismProperties
  538. });
  539. }
  540. },
  541. authSource: {
  542. target: 'credentials',
  543. transform({ options, values: [value] }) {
  544. const source = String(value);
  545. return mongo_credentials_1.MongoCredentials.merge(options.credentials, { source });
  546. }
  547. },
  548. autoEncryption: {
  549. type: 'record'
  550. },
  551. autoSelectFamily: {
  552. type: 'boolean',
  553. default: true
  554. },
  555. autoSelectFamilyAttemptTimeout: {
  556. type: 'uint'
  557. },
  558. bsonRegExp: {
  559. type: 'boolean'
  560. },
  561. serverApi: {
  562. target: 'serverApi',
  563. transform({ values: [version] }) {
  564. const serverApiToValidate = typeof version === 'string' ? { version } : version;
  565. const versionToValidate = serverApiToValidate && serverApiToValidate.version;
  566. if (!versionToValidate) {
  567. throw new error_1.MongoParseError(`Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values(mongo_client_1.ServerApiVersion).join('", "')}"]`);
  568. }
  569. if (!Object.values(mongo_client_1.ServerApiVersion).some(v => v === versionToValidate)) {
  570. throw new error_1.MongoParseError(`Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values(mongo_client_1.ServerApiVersion).join('", "')}"]`);
  571. }
  572. return serverApiToValidate;
  573. }
  574. },
  575. checkKeys: {
  576. type: 'boolean'
  577. },
  578. compressors: {
  579. default: 'none',
  580. target: 'compressors',
  581. transform({ values }) {
  582. const compressionList = new Set();
  583. for (const compVal of values) {
  584. const compValArray = typeof compVal === 'string' ? compVal.split(',') : compVal;
  585. if (!Array.isArray(compValArray)) {
  586. throw new error_1.MongoInvalidArgumentError('compressors must be an array or a comma-delimited list of strings');
  587. }
  588. for (const c of compValArray) {
  589. if (Object.keys(compression_1.Compressor).includes(String(c))) {
  590. compressionList.add(String(c));
  591. }
  592. else {
  593. throw new error_1.MongoInvalidArgumentError(`${c} is not a valid compression mechanism. Must be one of: ${Object.keys(compression_1.Compressor)}.`);
  594. }
  595. }
  596. }
  597. return [...compressionList];
  598. }
  599. },
  600. connectTimeoutMS: {
  601. default: 30000,
  602. type: 'uint'
  603. },
  604. dbName: {
  605. type: 'string'
  606. },
  607. directConnection: {
  608. default: false,
  609. type: 'boolean'
  610. },
  611. driverInfo: {
  612. default: {},
  613. type: 'record'
  614. },
  615. enableUtf8Validation: { type: 'boolean', default: true },
  616. family: {
  617. transform({ name, values: [value] }) {
  618. const transformValue = getIntFromOptions(name, value);
  619. if (transformValue === 4 || transformValue === 6) {
  620. return transformValue;
  621. }
  622. throw new error_1.MongoParseError(`Option 'family' must be 4 or 6 got ${transformValue}.`);
  623. }
  624. },
  625. fieldsAsRaw: {
  626. type: 'record'
  627. },
  628. forceServerObjectId: {
  629. default: false,
  630. type: 'boolean'
  631. },
  632. fsync: {
  633. deprecated: 'Please use journal instead',
  634. target: 'writeConcern',
  635. transform({ name, options, values: [value] }) {
  636. const wc = write_concern_1.WriteConcern.fromOptions({
  637. writeConcern: {
  638. ...options.writeConcern,
  639. fsync: getBoolean(name, value)
  640. }
  641. });
  642. if (!wc)
  643. throw new error_1.MongoParseError(`Unable to make a writeConcern from fsync=${value}`);
  644. return wc;
  645. }
  646. },
  647. heartbeatFrequencyMS: {
  648. default: 10000,
  649. type: 'uint'
  650. },
  651. ignoreUndefined: {
  652. type: 'boolean'
  653. },
  654. j: {
  655. deprecated: 'Please use journal instead',
  656. target: 'writeConcern',
  657. transform({ name, options, values: [value] }) {
  658. const wc = write_concern_1.WriteConcern.fromOptions({
  659. writeConcern: {
  660. ...options.writeConcern,
  661. journal: getBoolean(name, value)
  662. }
  663. });
  664. if (!wc)
  665. throw new error_1.MongoParseError(`Unable to make a writeConcern from journal=${value}`);
  666. return wc;
  667. }
  668. },
  669. journal: {
  670. target: 'writeConcern',
  671. transform({ name, options, values: [value] }) {
  672. const wc = write_concern_1.WriteConcern.fromOptions({
  673. writeConcern: {
  674. ...options.writeConcern,
  675. journal: getBoolean(name, value)
  676. }
  677. });
  678. if (!wc)
  679. throw new error_1.MongoParseError(`Unable to make a writeConcern from journal=${value}`);
  680. return wc;
  681. }
  682. },
  683. loadBalanced: {
  684. default: false,
  685. type: 'boolean'
  686. },
  687. localThresholdMS: {
  688. default: 15,
  689. type: 'uint'
  690. },
  691. maxConnecting: {
  692. default: 2,
  693. transform({ name, values: [value] }) {
  694. const maxConnecting = getUIntFromOptions(name, value);
  695. if (maxConnecting === 0) {
  696. throw new error_1.MongoInvalidArgumentError('maxConnecting must be > 0 if specified');
  697. }
  698. return maxConnecting;
  699. }
  700. },
  701. maxIdleTimeMS: {
  702. default: 0,
  703. type: 'uint'
  704. },
  705. maxPoolSize: {
  706. default: 100,
  707. type: 'uint'
  708. },
  709. maxStalenessSeconds: {
  710. target: 'readPreference',
  711. transform({ name, options, values: [value] }) {
  712. const maxStalenessSeconds = getUIntFromOptions(name, value);
  713. if (options.readPreference) {
  714. return read_preference_1.ReadPreference.fromOptions({
  715. readPreference: { ...options.readPreference, maxStalenessSeconds }
  716. });
  717. }
  718. else {
  719. return new read_preference_1.ReadPreference('secondary', undefined, { maxStalenessSeconds });
  720. }
  721. }
  722. },
  723. minInternalBufferSize: {
  724. type: 'uint'
  725. },
  726. minPoolSize: {
  727. default: 0,
  728. type: 'uint'
  729. },
  730. minHeartbeatFrequencyMS: {
  731. default: 500,
  732. type: 'uint'
  733. },
  734. monitorCommands: {
  735. default: false,
  736. type: 'boolean'
  737. },
  738. name: {
  739. target: 'driverInfo',
  740. transform({ values: [value], options }) {
  741. return { ...options.driverInfo, name: String(value) };
  742. }
  743. },
  744. noDelay: {
  745. default: true,
  746. type: 'boolean'
  747. },
  748. pkFactory: {
  749. default: utils_1.DEFAULT_PK_FACTORY,
  750. transform({ values: [value] }) {
  751. if ((0, utils_1.isRecord)(value, ['createPk']) && typeof value.createPk === 'function') {
  752. return value;
  753. }
  754. throw new error_1.MongoParseError(`Option pkFactory must be an object with a createPk function, got ${value}`);
  755. }
  756. },
  757. promoteBuffers: {
  758. type: 'boolean'
  759. },
  760. promoteLongs: {
  761. type: 'boolean'
  762. },
  763. promoteValues: {
  764. type: 'boolean'
  765. },
  766. useBigInt64: {
  767. type: 'boolean'
  768. },
  769. proxyHost: {
  770. type: 'string'
  771. },
  772. proxyPassword: {
  773. type: 'string'
  774. },
  775. proxyPort: {
  776. type: 'uint'
  777. },
  778. proxyUsername: {
  779. type: 'string'
  780. },
  781. raw: {
  782. default: false,
  783. type: 'boolean'
  784. },
  785. readConcern: {
  786. transform({ values: [value], options }) {
  787. if (value instanceof read_concern_1.ReadConcern || (0, utils_1.isRecord)(value, ['level'])) {
  788. return read_concern_1.ReadConcern.fromOptions({ ...options.readConcern, ...value });
  789. }
  790. throw new error_1.MongoParseError(`ReadConcern must be an object, got ${JSON.stringify(value)}`);
  791. }
  792. },
  793. readConcernLevel: {
  794. target: 'readConcern',
  795. transform({ values: [level], options }) {
  796. return read_concern_1.ReadConcern.fromOptions({
  797. ...options.readConcern,
  798. level: level
  799. });
  800. }
  801. },
  802. readPreference: {
  803. default: read_preference_1.ReadPreference.primary,
  804. transform({ values: [value], options }) {
  805. if (value instanceof read_preference_1.ReadPreference) {
  806. return read_preference_1.ReadPreference.fromOptions({
  807. readPreference: { ...options.readPreference, ...value },
  808. ...value
  809. });
  810. }
  811. if ((0, utils_1.isRecord)(value, ['mode'])) {
  812. const rp = read_preference_1.ReadPreference.fromOptions({
  813. readPreference: { ...options.readPreference, ...value },
  814. ...value
  815. });
  816. if (rp)
  817. return rp;
  818. else
  819. throw new error_1.MongoParseError(`Cannot make read preference from ${JSON.stringify(value)}`);
  820. }
  821. if (typeof value === 'string') {
  822. const rpOpts = {
  823. hedge: options.readPreference?.hedge,
  824. maxStalenessSeconds: options.readPreference?.maxStalenessSeconds
  825. };
  826. return new read_preference_1.ReadPreference(value, options.readPreference?.tags, rpOpts);
  827. }
  828. throw new error_1.MongoParseError(`Unknown ReadPreference value: ${value}`);
  829. }
  830. },
  831. readPreferenceTags: {
  832. target: 'readPreference',
  833. transform({ values, options }) {
  834. const tags = Array.isArray(values[0])
  835. ? values[0]
  836. : values;
  837. const readPreferenceTags = [];
  838. for (const tag of tags) {
  839. const readPreferenceTag = Object.create(null);
  840. if (typeof tag === 'string') {
  841. for (const [k, v] of entriesFromString(tag)) {
  842. readPreferenceTag[k] = v;
  843. }
  844. }
  845. if ((0, utils_1.isRecord)(tag)) {
  846. for (const [k, v] of Object.entries(tag)) {
  847. readPreferenceTag[k] = v;
  848. }
  849. }
  850. readPreferenceTags.push(readPreferenceTag);
  851. }
  852. return read_preference_1.ReadPreference.fromOptions({
  853. readPreference: options.readPreference,
  854. readPreferenceTags
  855. });
  856. }
  857. },
  858. replicaSet: {
  859. type: 'string'
  860. },
  861. retryReads: {
  862. default: true,
  863. type: 'boolean'
  864. },
  865. retryWrites: {
  866. default: true,
  867. type: 'boolean'
  868. },
  869. serializeFunctions: {
  870. type: 'boolean'
  871. },
  872. serverMonitoringMode: {
  873. default: 'auto',
  874. transform({ values: [value] }) {
  875. if (!Object.values(monitor_1.ServerMonitoringMode).includes(value)) {
  876. throw new error_1.MongoParseError('serverMonitoringMode must be one of `auto`, `poll`, or `stream`');
  877. }
  878. return value;
  879. }
  880. },
  881. serverSelectionTimeoutMS: {
  882. default: 30000,
  883. type: 'uint'
  884. },
  885. servername: {
  886. type: 'string'
  887. },
  888. socketTimeoutMS: {
  889. // TODO(NODE-6491): deprecated: 'Please use timeoutMS instead',
  890. default: 0,
  891. type: 'uint'
  892. },
  893. srvMaxHosts: {
  894. type: 'uint',
  895. default: 0
  896. },
  897. srvServiceName: {
  898. type: 'string',
  899. default: 'mongodb'
  900. },
  901. ssl: {
  902. target: 'tls',
  903. type: 'boolean'
  904. },
  905. timeoutMS: {
  906. type: 'uint'
  907. },
  908. tls: {
  909. type: 'boolean'
  910. },
  911. tlsAllowInvalidCertificates: {
  912. target: 'rejectUnauthorized',
  913. transform({ name, values: [value] }) {
  914. // allowInvalidCertificates is the inverse of rejectUnauthorized
  915. return !getBoolean(name, value);
  916. }
  917. },
  918. tlsAllowInvalidHostnames: {
  919. target: 'checkServerIdentity',
  920. transform({ name, values: [value] }) {
  921. // tlsAllowInvalidHostnames means setting the checkServerIdentity function to a noop
  922. return getBoolean(name, value) ? () => undefined : undefined;
  923. }
  924. },
  925. tlsCAFile: {
  926. type: 'string'
  927. },
  928. tlsCRLFile: {
  929. type: 'string'
  930. },
  931. tlsCertificateKeyFile: {
  932. type: 'string'
  933. },
  934. tlsCertificateKeyFilePassword: {
  935. target: 'passphrase',
  936. type: 'any'
  937. },
  938. tlsInsecure: {
  939. transform({ name, options, values: [value] }) {
  940. const tlsInsecure = getBoolean(name, value);
  941. if (tlsInsecure) {
  942. options.checkServerIdentity = () => undefined;
  943. options.rejectUnauthorized = false;
  944. }
  945. else {
  946. options.checkServerIdentity = options.tlsAllowInvalidHostnames
  947. ? () => undefined
  948. : undefined;
  949. options.rejectUnauthorized = options.tlsAllowInvalidCertificates ? false : true;
  950. }
  951. return tlsInsecure;
  952. }
  953. },
  954. w: {
  955. target: 'writeConcern',
  956. transform({ values: [value], options }) {
  957. return write_concern_1.WriteConcern.fromOptions({ writeConcern: { ...options.writeConcern, w: value } });
  958. }
  959. },
  960. waitQueueTimeoutMS: {
  961. // TODO(NODE-6491): deprecated: 'Please use timeoutMS instead',
  962. default: 0,
  963. type: 'uint'
  964. },
  965. writeConcern: {
  966. target: 'writeConcern',
  967. transform({ values: [value], options }) {
  968. if ((0, utils_1.isRecord)(value) || value instanceof write_concern_1.WriteConcern) {
  969. return write_concern_1.WriteConcern.fromOptions({
  970. writeConcern: {
  971. ...options.writeConcern,
  972. ...value
  973. }
  974. });
  975. }
  976. else if (value === 'majority' || typeof value === 'number') {
  977. return write_concern_1.WriteConcern.fromOptions({
  978. writeConcern: {
  979. ...options.writeConcern,
  980. w: value
  981. }
  982. });
  983. }
  984. throw new error_1.MongoParseError(`Invalid WriteConcern cannot parse: ${JSON.stringify(value)}`);
  985. }
  986. },
  987. wtimeout: {
  988. deprecated: 'Please use wtimeoutMS instead',
  989. target: 'writeConcern',
  990. transform({ values: [value], options }) {
  991. const wc = write_concern_1.WriteConcern.fromOptions({
  992. writeConcern: {
  993. ...options.writeConcern,
  994. wtimeout: getUIntFromOptions('wtimeout', value)
  995. }
  996. });
  997. if (wc)
  998. return wc;
  999. throw new error_1.MongoParseError(`Cannot make WriteConcern from wtimeout`);
  1000. }
  1001. },
  1002. wtimeoutMS: {
  1003. target: 'writeConcern',
  1004. transform({ values: [value], options }) {
  1005. const wc = write_concern_1.WriteConcern.fromOptions({
  1006. writeConcern: {
  1007. ...options.writeConcern,
  1008. wtimeoutMS: getUIntFromOptions('wtimeoutMS', value)
  1009. }
  1010. });
  1011. if (wc)
  1012. return wc;
  1013. throw new error_1.MongoParseError(`Cannot make WriteConcern from wtimeout`);
  1014. }
  1015. },
  1016. zlibCompressionLevel: {
  1017. default: 0,
  1018. type: 'int'
  1019. },
  1020. mongodbLogPath: {
  1021. transform({ values: [value] }) {
  1022. if (!((typeof value === 'string' && ['stderr', 'stdout'].includes(value)) ||
  1023. (value &&
  1024. typeof value === 'object' &&
  1025. 'write' in value &&
  1026. typeof value.write === 'function'))) {
  1027. throw new error_1.MongoAPIError(`Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`);
  1028. }
  1029. return value;
  1030. }
  1031. },
  1032. mongodbLogComponentSeverities: {
  1033. transform({ values: [value] }) {
  1034. if (typeof value !== 'object' || !value) {
  1035. throw new error_1.MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);
  1036. }
  1037. for (const [k, v] of Object.entries(value)) {
  1038. if (typeof v !== 'string' || typeof k !== 'string') {
  1039. throw new error_1.MongoAPIError(`User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`);
  1040. }
  1041. if (!Object.values(mongo_logger_1.MongoLoggableComponent).some(val => val === k) && k !== 'default') {
  1042. throw new error_1.MongoAPIError(`User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}`);
  1043. }
  1044. if (!Object.values(mongo_logger_1.SeverityLevel).some(val => val === v)) {
  1045. throw new error_1.MongoAPIError(`Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}`);
  1046. }
  1047. }
  1048. return value;
  1049. }
  1050. },
  1051. mongodbLogMaxDocumentLength: { type: 'uint' },
  1052. // Custom types for modifying core behavior
  1053. connectionType: { type: 'any' },
  1054. srvPoller: { type: 'any' },
  1055. // Accepted Node.js Options
  1056. allowPartialTrustChain: { type: 'any' },
  1057. minDHSize: { type: 'any' },
  1058. pskCallback: { type: 'any' },
  1059. secureContext: { type: 'any' },
  1060. enableTrace: { type: 'any' },
  1061. requestCert: { type: 'any' },
  1062. rejectUnauthorized: { type: 'any' },
  1063. checkServerIdentity: { type: 'any' },
  1064. keepAliveInitialDelay: { type: 'any' },
  1065. ALPNProtocols: { type: 'any' },
  1066. SNICallback: { type: 'any' },
  1067. session: { type: 'any' },
  1068. requestOCSP: { type: 'any' },
  1069. localAddress: { type: 'any' },
  1070. localPort: { type: 'any' },
  1071. hints: { type: 'any' },
  1072. lookup: { type: 'any' },
  1073. ca: { type: 'any' },
  1074. cert: { type: 'any' },
  1075. ciphers: { type: 'any' },
  1076. crl: { type: 'any' },
  1077. ecdhCurve: { type: 'any' },
  1078. key: { type: 'any' },
  1079. passphrase: { type: 'any' },
  1080. pfx: { type: 'any' },
  1081. secureProtocol: { type: 'any' },
  1082. index: { type: 'any' },
  1083. // Legacy options from v3 era
  1084. __skipPingOnConnect: { type: 'boolean' }
  1085. };
  1086. exports.DEFAULT_OPTIONS = new CaseInsensitiveMap(Object.entries(exports.OPTIONS)
  1087. .filter(([, descriptor]) => descriptor.default != null)
  1088. .map(([k, d]) => [k, d.default]));
  1089. //# sourceMappingURL=connection_string.js.map