Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mongo_client_ops.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. 'use strict';
  2. const authenticate = require('../authenticate');
  3. const deprecate = require('util').deprecate;
  4. const Logger = require('mongodb-core').Logger;
  5. const MongoError = require('mongodb-core').MongoError;
  6. const Mongos = require('../topologies/mongos');
  7. const parse = require('mongodb-core').parseConnectionString;
  8. const ReadPreference = require('mongodb-core').ReadPreference;
  9. const ReplSet = require('../topologies/replset');
  10. const Server = require('../topologies/server');
  11. const ServerSessionPool = require('mongodb-core').Sessions.ServerSessionPool;
  12. const monitoringEvents = [
  13. 'timeout',
  14. 'close',
  15. 'serverOpening',
  16. 'serverDescriptionChanged',
  17. 'serverHeartbeatStarted',
  18. 'serverHeartbeatSucceeded',
  19. 'serverHeartbeatFailed',
  20. 'serverClosed',
  21. 'topologyOpening',
  22. 'topologyClosed',
  23. 'topologyDescriptionChanged',
  24. 'commandStarted',
  25. 'commandSucceeded',
  26. 'commandFailed',
  27. 'joined',
  28. 'left',
  29. 'ping',
  30. 'ha',
  31. 'all',
  32. 'fullsetup',
  33. 'open'
  34. ];
  35. const ignoreOptionNames = ['native_parser'];
  36. const legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
  37. const legacyParse = deprecate(
  38. require('../url_parser'),
  39. 'current URL string parser is deprecated, and will be removed in a future version. ' +
  40. 'To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.'
  41. );
  42. const validOptionNames = [
  43. 'poolSize',
  44. 'ssl',
  45. 'sslValidate',
  46. 'sslCA',
  47. 'sslCert',
  48. 'sslKey',
  49. 'sslPass',
  50. 'sslCRL',
  51. 'autoReconnect',
  52. 'noDelay',
  53. 'keepAlive',
  54. 'keepAliveInitialDelay',
  55. 'connectTimeoutMS',
  56. 'family',
  57. 'socketTimeoutMS',
  58. 'reconnectTries',
  59. 'reconnectInterval',
  60. 'ha',
  61. 'haInterval',
  62. 'replicaSet',
  63. 'secondaryAcceptableLatencyMS',
  64. 'acceptableLatencyMS',
  65. 'connectWithNoPrimary',
  66. 'authSource',
  67. 'w',
  68. 'wtimeout',
  69. 'j',
  70. 'forceServerObjectId',
  71. 'serializeFunctions',
  72. 'ignoreUndefined',
  73. 'raw',
  74. 'bufferMaxEntries',
  75. 'readPreference',
  76. 'pkFactory',
  77. 'promiseLibrary',
  78. 'readConcern',
  79. 'maxStalenessSeconds',
  80. 'loggerLevel',
  81. 'logger',
  82. 'promoteValues',
  83. 'promoteBuffers',
  84. 'promoteLongs',
  85. 'domainsEnabled',
  86. 'checkServerIdentity',
  87. 'validateOptions',
  88. 'appname',
  89. 'auth',
  90. 'user',
  91. 'password',
  92. 'authMechanism',
  93. 'compression',
  94. 'fsync',
  95. 'readPreferenceTags',
  96. 'numberOfRetries',
  97. 'auto_reconnect',
  98. 'minSize',
  99. 'monitorCommands',
  100. 'retryWrites',
  101. 'useNewUrlParser'
  102. ];
  103. function addListeners(mongoClient, topology) {
  104. topology.on('authenticated', createListener(mongoClient, 'authenticated'));
  105. topology.on('error', createListener(mongoClient, 'error'));
  106. topology.on('timeout', createListener(mongoClient, 'timeout'));
  107. topology.on('close', createListener(mongoClient, 'close'));
  108. topology.on('parseError', createListener(mongoClient, 'parseError'));
  109. topology.once('open', createListener(mongoClient, 'open'));
  110. topology.once('fullsetup', createListener(mongoClient, 'fullsetup'));
  111. topology.once('all', createListener(mongoClient, 'all'));
  112. topology.on('reconnect', createListener(mongoClient, 'reconnect'));
  113. }
  114. function assignTopology(client, topology) {
  115. client.topology = topology;
  116. topology.s.sessionPool = new ServerSessionPool(topology.s.coreTopology);
  117. }
  118. // Clear out all events
  119. function clearAllEvents(topology) {
  120. monitoringEvents.forEach(event => topology.removeAllListeners(event));
  121. }
  122. // Collect all events in order from SDAM
  123. function collectEvents(mongoClient, topology) {
  124. const MongoClient = require('../mongo_client');
  125. const collectedEvents = [];
  126. if (mongoClient instanceof MongoClient) {
  127. monitoringEvents.forEach(event => {
  128. topology.on(event, (object1, object2) => {
  129. if (event === 'open') {
  130. collectedEvents.push({ event: event, object1: mongoClient });
  131. } else {
  132. collectedEvents.push({ event: event, object1: object1, object2: object2 });
  133. }
  134. });
  135. });
  136. }
  137. return collectedEvents;
  138. }
  139. /**
  140. * Connect to MongoDB using a url as documented at
  141. *
  142. * docs.mongodb.org/manual/reference/connection-string/
  143. *
  144. * Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
  145. *
  146. * @method
  147. * @param {MongoClient} mongoClient The MongoClient instance with which to connect.
  148. * @param {string} url The connection URI string
  149. * @param {object} [options] Optional settings. See MongoClient.prototype.connect for a list of options.
  150. * @param {MongoClient~connectCallback} [callback] The command result callback
  151. */
  152. function connect(mongoClient, url, options, callback) {
  153. options = Object.assign({}, options);
  154. // If callback is null throw an exception
  155. if (callback == null) {
  156. throw new Error('no callback function provided');
  157. }
  158. // Get a logger for MongoClient
  159. const logger = Logger('MongoClient', options);
  160. // Did we pass in a Server/ReplSet/Mongos
  161. if (url instanceof Server || url instanceof ReplSet || url instanceof Mongos) {
  162. return connectWithUrl(mongoClient, url, options, connectCallback);
  163. }
  164. const parseFn = options.useNewUrlParser ? parse : legacyParse;
  165. const transform = options.useNewUrlParser ? transformUrlOptions : legacyTransformUrlOptions;
  166. parseFn(url, options, (err, _object) => {
  167. // Do not attempt to connect if parsing error
  168. if (err) return callback(err);
  169. // Flatten
  170. const object = transform(_object);
  171. // Parse the string
  172. const _finalOptions = createUnifiedOptions(object, options);
  173. // Check if we have connection and socket timeout set
  174. if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
  175. if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;
  176. if (_finalOptions.db_options && _finalOptions.db_options.auth) {
  177. delete _finalOptions.db_options.auth;
  178. }
  179. // Store the merged options object
  180. mongoClient.s.options = _finalOptions;
  181. // Failure modes
  182. if (object.servers.length === 0) {
  183. return callback(new Error('connection string must contain at least one seed host'));
  184. }
  185. // Do we have a replicaset then skip discovery and go straight to connectivity
  186. if (_finalOptions.replicaSet || _finalOptions.rs_name) {
  187. return createTopology(
  188. mongoClient,
  189. 'replicaset',
  190. _finalOptions,
  191. connectHandler(mongoClient, _finalOptions, connectCallback)
  192. );
  193. } else if (object.servers.length > 1) {
  194. return createTopology(
  195. mongoClient,
  196. 'mongos',
  197. _finalOptions,
  198. connectHandler(mongoClient, _finalOptions, connectCallback)
  199. );
  200. } else {
  201. return createServer(
  202. mongoClient,
  203. _finalOptions,
  204. connectHandler(mongoClient, _finalOptions, connectCallback)
  205. );
  206. }
  207. });
  208. function connectCallback(err, topology) {
  209. const warningMessage = `seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name`;
  210. if (err && err.message === 'no mongos proxies found in seed list') {
  211. if (logger.isWarn()) {
  212. logger.warn(warningMessage);
  213. }
  214. // Return a more specific error message for MongoClient.connect
  215. return callback(new MongoError(warningMessage));
  216. }
  217. // Return the error and db instance
  218. callback(err, topology);
  219. }
  220. }
  221. function connectHandler(client, options, callback) {
  222. return (err, topology) => {
  223. if (err) {
  224. return handleConnectCallback(err, topology, callback);
  225. }
  226. // No authentication just reconnect
  227. if (!options.auth) {
  228. return handleConnectCallback(err, topology, callback);
  229. }
  230. // Authenticate
  231. authenticate(client, options.user, options.password, options, (err, success) => {
  232. if (success) {
  233. handleConnectCallback(null, topology, callback);
  234. } else {
  235. if (topology) topology.close();
  236. const authError = err ? err : new Error('Could not authenticate user ' + options.auth[0]);
  237. handleConnectCallback(authError, topology, callback);
  238. }
  239. });
  240. };
  241. }
  242. /**
  243. * Connect to MongoDB using a url as documented at
  244. *
  245. * docs.mongodb.org/manual/reference/connection-string/
  246. *
  247. * Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
  248. *
  249. * @method
  250. * @param {MongoClient} mongoClient The MongoClient instance with which to connect.
  251. * @param {MongoClient~connectCallback} [callback] The command result callback
  252. */
  253. function connectOp(mongoClient, err, callback) {
  254. // Did we have a validation error
  255. if (err) return callback(err);
  256. // Fallback to callback based connect
  257. connect(mongoClient, mongoClient.s.url, mongoClient.s.options, err => {
  258. if (err) return callback(err);
  259. callback(null, mongoClient);
  260. });
  261. }
  262. function connectWithUrl(mongoClient, url, options, connectCallback) {
  263. // Set the topology
  264. assignTopology(mongoClient, url);
  265. // Add listeners
  266. addListeners(mongoClient, url);
  267. // Propagate the events to the client
  268. relayEvents(mongoClient, url);
  269. let finalOptions = Object.assign({}, options);
  270. // If we have a readPreference passed in by the db options, convert it from a string
  271. if (typeof options.readPreference === 'string' || typeof options.read_preference === 'string') {
  272. finalOptions.readPreference = new ReadPreference(
  273. options.readPreference || options.read_preference
  274. );
  275. }
  276. // Connect
  277. return url.connect(
  278. finalOptions,
  279. connectHandler(mongoClient, finalOptions, (err, topology) => {
  280. if (err) return connectCallback(err, topology);
  281. if (finalOptions.user || finalOptions.password || finalOptions.authMechanism) {
  282. return authenticate(
  283. mongoClient,
  284. finalOptions.user,
  285. finalOptions.password,
  286. finalOptions,
  287. err => {
  288. if (err) return connectCallback(err, topology);
  289. connectCallback(err, topology);
  290. }
  291. );
  292. }
  293. connectCallback(err, topology);
  294. })
  295. );
  296. }
  297. function createListener(mongoClient, event) {
  298. const eventSet = new Set(['all', 'fullsetup', 'open', 'reconnect']);
  299. return (v1, v2) => {
  300. if (eventSet.has(event)) {
  301. return mongoClient.emit(event, mongoClient);
  302. }
  303. mongoClient.emit(event, v1, v2);
  304. };
  305. }
  306. function createServer(mongoClient, options, callback) {
  307. // Pass in the promise library
  308. options.promiseLibrary = mongoClient.s.promiseLibrary;
  309. // Set default options
  310. const servers = translateOptions(options);
  311. const server = servers[0];
  312. // Propagate the events to the client
  313. const collectedEvents = collectEvents(mongoClient, server);
  314. // Connect to topology
  315. server.connect(options, (err, topology) => {
  316. if (err) {
  317. server.close(true);
  318. return callback(err);
  319. }
  320. // Clear out all the collected event listeners
  321. clearAllEvents(server);
  322. // Relay all the events
  323. relayEvents(mongoClient, server);
  324. // Add listeners
  325. addListeners(mongoClient, server);
  326. // Check if we are really speaking to a mongos
  327. const ismaster = topology.lastIsMaster();
  328. // Set the topology
  329. assignTopology(mongoClient, topology);
  330. // Do we actually have a mongos
  331. if (ismaster && ismaster.msg === 'isdbgrid') {
  332. // Destroy the current connection
  333. topology.close();
  334. // Create mongos connection instead
  335. return createTopology(mongoClient, 'mongos', options, callback);
  336. }
  337. // Fire all the events
  338. replayEvents(mongoClient, collectedEvents);
  339. // Otherwise callback
  340. callback(err, topology);
  341. });
  342. }
  343. function createTopology(mongoClient, topologyType, options, callback) {
  344. // Pass in the promise library
  345. options.promiseLibrary = mongoClient.s.promiseLibrary;
  346. const translationOptions = {};
  347. if (topologyType === 'unified') translationOptions.createServers = false;
  348. // Set default options
  349. const servers = translateOptions(options, translationOptions);
  350. // Create the topology
  351. let topology;
  352. if (topologyType === 'mongos') {
  353. topology = new Mongos(servers, options);
  354. } else if (topologyType === 'replicaset') {
  355. topology = new ReplSet(servers, options);
  356. }
  357. // Add listeners
  358. addListeners(mongoClient, topology);
  359. // Propagate the events to the client
  360. relayEvents(mongoClient, topology);
  361. // Open the connection
  362. topology.connect(options, (err, newTopology) => {
  363. if (err) {
  364. topology.close(true);
  365. return callback(err);
  366. }
  367. assignTopology(mongoClient, newTopology);
  368. callback(null, newTopology);
  369. });
  370. }
  371. function createUnifiedOptions(finalOptions, options) {
  372. const childOptions = [
  373. 'mongos',
  374. 'server',
  375. 'db',
  376. 'replset',
  377. 'db_options',
  378. 'server_options',
  379. 'rs_options',
  380. 'mongos_options'
  381. ];
  382. const noMerge = ['readconcern', 'compression'];
  383. for (const name in options) {
  384. if (noMerge.indexOf(name.toLowerCase()) !== -1) {
  385. finalOptions[name] = options[name];
  386. } else if (childOptions.indexOf(name.toLowerCase()) !== -1) {
  387. finalOptions = mergeOptions(finalOptions, options[name], false);
  388. } else {
  389. if (
  390. options[name] &&
  391. typeof options[name] === 'object' &&
  392. !Buffer.isBuffer(options[name]) &&
  393. !Array.isArray(options[name])
  394. ) {
  395. finalOptions = mergeOptions(finalOptions, options[name], true);
  396. } else {
  397. finalOptions[name] = options[name];
  398. }
  399. }
  400. }
  401. return finalOptions;
  402. }
  403. function handleConnectCallback(err, topology, callback) {
  404. return process.nextTick(() => {
  405. try {
  406. callback(err, topology);
  407. } catch (err) {
  408. if (topology) topology.close();
  409. throw err;
  410. }
  411. });
  412. }
  413. function legacyTransformUrlOptions(object) {
  414. return mergeOptions(createUnifiedOptions({}, object), object, false);
  415. }
  416. /**
  417. * Logout user from server, fire off on all connections and remove all auth info.
  418. *
  419. * @method
  420. * @param {MongoClient} mongoClient The MongoClient instance on which to logout.
  421. * @param {object} [options] Optional settings. See MongoClient.prototype.logout for a list of options.
  422. * @param {Db~resultCallback} [callback] The command result callback
  423. */
  424. function logout(mongoClient, dbName, callback) {
  425. mongoClient.topology.logout(dbName, err => {
  426. if (err) return callback(err);
  427. callback(null, true);
  428. });
  429. }
  430. function mergeOptions(target, source, flatten) {
  431. for (const name in source) {
  432. if (source[name] && typeof source[name] === 'object' && flatten) {
  433. target = mergeOptions(target, source[name], flatten);
  434. } else {
  435. target[name] = source[name];
  436. }
  437. }
  438. return target;
  439. }
  440. function relayEvents(mongoClient, topology) {
  441. const serverOrCommandEvents = [
  442. 'serverOpening',
  443. 'serverDescriptionChanged',
  444. 'serverHeartbeatStarted',
  445. 'serverHeartbeatSucceeded',
  446. 'serverHeartbeatFailed',
  447. 'serverClosed',
  448. 'topologyOpening',
  449. 'topologyClosed',
  450. 'topologyDescriptionChanged',
  451. 'commandStarted',
  452. 'commandSucceeded',
  453. 'commandFailed',
  454. 'joined',
  455. 'left',
  456. 'ping',
  457. 'ha'
  458. ];
  459. serverOrCommandEvents.forEach(event => {
  460. topology.on(event, (object1, object2) => {
  461. mongoClient.emit(event, object1, object2);
  462. });
  463. });
  464. }
  465. //
  466. // Replay any events due to single server connection switching to Mongos
  467. //
  468. function replayEvents(mongoClient, events) {
  469. for (let i = 0; i < events.length; i++) {
  470. mongoClient.emit(events[i].event, events[i].object1, events[i].object2);
  471. }
  472. }
  473. const LEGACY_OPTIONS_MAP = validOptionNames.reduce((obj, name) => {
  474. obj[name.toLowerCase()] = name;
  475. return obj;
  476. }, {});
  477. function transformUrlOptions(_object) {
  478. let object = Object.assign({ servers: _object.hosts }, _object.options);
  479. for (let name in object) {
  480. const camelCaseName = LEGACY_OPTIONS_MAP[name];
  481. if (camelCaseName) {
  482. object[camelCaseName] = object[name];
  483. }
  484. }
  485. if (_object.auth) {
  486. const auth = _object.auth;
  487. for (let i in auth) {
  488. if (auth[i]) {
  489. object[i] = auth[i];
  490. }
  491. }
  492. if (auth.username) {
  493. object.auth = auth;
  494. object.user = auth.username;
  495. }
  496. if (auth.db) {
  497. object.authSource = object.authSource || auth.db;
  498. }
  499. }
  500. if (_object.defaultDatabase) {
  501. object.dbName = _object.defaultDatabase;
  502. }
  503. if (object.maxpoolsize) {
  504. object.poolSize = object.maxpoolsize;
  505. }
  506. if (object.readconcernlevel) {
  507. object.readConcern = { level: object.readconcernlevel };
  508. }
  509. if (object.wtimeoutms) {
  510. object.wtimeout = object.wtimeoutms;
  511. }
  512. return object;
  513. }
  514. function translateOptions(options, translationOptions) {
  515. translationOptions = Object.assign({}, { createServers: true }, translationOptions);
  516. // If we have a readPreference passed in by the db options
  517. if (typeof options.readPreference === 'string' || typeof options.read_preference === 'string') {
  518. options.readPreference = new ReadPreference(options.readPreference || options.read_preference);
  519. }
  520. // Do we have readPreference tags, add them
  521. if (options.readPreference && (options.readPreferenceTags || options.read_preference_tags)) {
  522. options.readPreference.tags = options.readPreferenceTags || options.read_preference_tags;
  523. }
  524. // Do we have maxStalenessSeconds
  525. if (options.maxStalenessSeconds) {
  526. options.readPreference.maxStalenessSeconds = options.maxStalenessSeconds;
  527. }
  528. // Set the socket and connection timeouts
  529. if (options.socketTimeoutMS == null) options.socketTimeoutMS = 360000;
  530. if (options.connectTimeoutMS == null) options.connectTimeoutMS = 30000;
  531. if (!translationOptions.createServers) {
  532. return;
  533. }
  534. // Create server instances
  535. return options.servers.map(serverObj => {
  536. return serverObj.domain_socket
  537. ? new Server(serverObj.domain_socket, 27017, options)
  538. : new Server(serverObj.host, serverObj.port, options);
  539. });
  540. }
  541. // Validate options object
  542. function validOptions(options) {
  543. const _validOptions = validOptionNames.concat(legacyOptionNames);
  544. for (const name in options) {
  545. if (ignoreOptionNames.indexOf(name) !== -1) {
  546. continue;
  547. }
  548. if (_validOptions.indexOf(name) === -1 && options.validateOptions) {
  549. return new MongoError(`option ${name} is not supported`);
  550. } else if (_validOptions.indexOf(name) === -1) {
  551. console.warn(`the options [${name}] is not supported`);
  552. }
  553. if (legacyOptionNames.indexOf(name) !== -1) {
  554. console.warn(
  555. `the server/replset/mongos/db options are deprecated, ` +
  556. `all their options are supported at the top level of the options object [${validOptionNames}]`
  557. );
  558. }
  559. }
  560. }
  561. module.exports = { connectOp, logout, validOptions };