connect.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.connect = void 0;
  4. const connection_string_1 = require("../connection_string");
  5. const constants_1 = require("../constants");
  6. const error_1 = require("../error");
  7. const topology_1 = require("../sdam/topology");
  8. function connect(mongoClient, options, callback) {
  9. if (!callback) {
  10. throw new error_1.MongoInvalidArgumentError('Callback function must be provided');
  11. }
  12. // If a connection already been established, we can terminate early
  13. if (mongoClient.topology && mongoClient.topology.isConnected()) {
  14. return callback(undefined, mongoClient);
  15. }
  16. const logger = mongoClient.logger;
  17. const connectCallback = err => {
  18. const warningMessage = 'seed list contains no mongos proxies, replicaset connections requires ' +
  19. 'the parameter replicaSet to be supplied in the URI or options object, ' +
  20. 'mongodb://server:port/db?replicaSet=name';
  21. if (err && err.message === 'no mongos proxies found in seed list') {
  22. if (logger.isWarn()) {
  23. logger.warn(warningMessage);
  24. }
  25. // Return a more specific error message for MongoClient.connect
  26. // TODO(NODE-3483)
  27. return callback(new error_1.MongoRuntimeError(warningMessage));
  28. }
  29. callback(err, mongoClient);
  30. };
  31. if (typeof options.srvHost === 'string') {
  32. return (0, connection_string_1.resolveSRVRecord)(options, (err, hosts) => {
  33. if (err || !hosts)
  34. return callback(err);
  35. for (const [index, host] of hosts.entries()) {
  36. options.hosts[index] = host;
  37. }
  38. return createTopology(mongoClient, options, connectCallback);
  39. });
  40. }
  41. return createTopology(mongoClient, options, connectCallback);
  42. }
  43. exports.connect = connect;
  44. function createTopology(mongoClient, options, callback) {
  45. // Create the topology
  46. const topology = new topology_1.Topology(options.hosts, options);
  47. // Events can be emitted before initialization is complete so we have to
  48. // save the reference to the topology on the client ASAP if the event handlers need to access it
  49. mongoClient.topology = topology;
  50. topology.client = mongoClient;
  51. topology.once(topology_1.Topology.OPEN, () => mongoClient.emit('open', mongoClient));
  52. for (const event of constants_1.MONGO_CLIENT_EVENTS) {
  53. topology.on(event, (...args) => mongoClient.emit(event, ...args));
  54. }
  55. // initialize CSFLE if requested
  56. if (mongoClient.autoEncrypter) {
  57. mongoClient.autoEncrypter.init(err => {
  58. if (err) {
  59. return callback(err);
  60. }
  61. topology.connect(options, err => {
  62. if (err) {
  63. topology.close({ force: true });
  64. return callback(err);
  65. }
  66. options.encrypter.connectInternalClient(error => {
  67. if (error)
  68. return callback(error);
  69. callback(undefined, topology);
  70. });
  71. });
  72. });
  73. return;
  74. }
  75. // otherwise connect normally
  76. topology.connect(options, err => {
  77. if (err) {
  78. topology.close({ force: true });
  79. return callback(err);
  80. }
  81. callback(undefined, topology);
  82. return;
  83. });
  84. }
  85. //# sourceMappingURL=connect.js.map