connect.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.once(topology_1.Topology.OPEN, () => mongoClient.emit('open', mongoClient));
  51. for (const event of constants_1.MONGO_CLIENT_EVENTS) {
  52. topology.on(event, (...args) => mongoClient.emit(event, ...args));
  53. }
  54. // initialize CSFLE if requested
  55. if (mongoClient.autoEncrypter) {
  56. mongoClient.autoEncrypter.init(err => {
  57. if (err) {
  58. return callback(err);
  59. }
  60. topology.connect(options, err => {
  61. if (err) {
  62. topology.close({ force: true });
  63. return callback(err);
  64. }
  65. options.encrypter.connectInternalClient(error => {
  66. if (error)
  67. return callback(error);
  68. callback(undefined, topology);
  69. });
  70. });
  71. });
  72. return;
  73. }
  74. // otherwise connect normally
  75. topology.connect(options, err => {
  76. if (err) {
  77. topology.close({ force: true });
  78. return callback(err);
  79. }
  80. callback(undefined, topology);
  81. return;
  82. });
  83. }
  84. //# sourceMappingURL=connect.js.map