Skip to content

Commit f6a8ceb

Browse files
committed
refactor: use maybePromise for all MongoClient fake operations
1 parent 6f71507 commit f6a8ceb

File tree

4 files changed

+53
-87
lines changed

4 files changed

+53
-87
lines changed

lib/core/utils.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ function makeClientMetadata(options) {
255255
return metadata;
256256
}
257257

258+
const noop = () => {};
259+
258260
module.exports = {
259261
uuidV4,
260262
calculateDurationInMs,
@@ -270,5 +272,6 @@ module.exports = {
270272
tagsStrictEqual,
271273
errorStrictEqual,
272274
makeStateMachine,
273-
makeClientMetadata
275+
makeClientMetadata,
276+
noop
274277
};

lib/mongo_client.js

+48-10
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
const ChangeStream = require('./change_stream');
44
const Db = require('./db');
55
const EventEmitter = require('events').EventEmitter;
6-
const executeOperation = require('./operations/execute_operation');
76
const inherits = require('util').inherits;
87
const MongoError = require('./core').MongoError;
98
const deprecate = require('util').deprecate;
109
const WriteConcern = require('./write_concern');
1110
const MongoDBNamespace = require('./utils').MongoDBNamespace;
1211
const ReadPreference = require('./core/topologies/read_preference');
13-
14-
// Operations
15-
const ConnectOperation = require('./operations/connect');
16-
const CloseOperation = require('./operations/close');
12+
const maybePromise = require('./utils').maybePromise;
13+
const NativeTopology = require('./topologies/native_topology');
14+
const connect = require('./operations/connect').connect;
15+
const validOptions = require('./operations/connect').validOptions;
1716

1817
/**
1918
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
@@ -214,9 +213,16 @@ MongoClient.prototype.connect = function(callback) {
214213
throw new TypeError('`connect` only accepts a callback');
215214
}
216215

217-
const operation = new ConnectOperation(this);
216+
const client = this;
217+
return maybePromise(callback, cb => {
218+
const err = validOptions(client.s.options);
219+
if (err) return cb(err);
218220

219-
return executeOperation(this, operation, callback);
221+
connect(client, client.s.url, client.s.options, err => {
222+
if (err) return cb(err);
223+
cb(null, client);
224+
});
225+
});
220226
};
221227

222228
MongoClient.prototype.logout = deprecate(function(options, callback) {
@@ -232,9 +238,41 @@ MongoClient.prototype.logout = deprecate(function(options, callback) {
232238
* @return {Promise} returns Promise if no callback passed
233239
*/
234240
MongoClient.prototype.close = function(force, callback) {
235-
if (typeof force === 'function') (callback = force), (force = false);
236-
const operation = new CloseOperation(this, force);
237-
return executeOperation(this, operation, callback);
241+
if (typeof force === 'function') {
242+
callback = force;
243+
force = false;
244+
}
245+
246+
const client = this;
247+
return maybePromise(callback, cb => {
248+
const completeClose = err => {
249+
client.emit('close', client);
250+
251+
if (!(client.topology instanceof NativeTopology)) {
252+
for (const item of client.s.dbCache) {
253+
item[1].emit('close', client);
254+
}
255+
}
256+
257+
client.removeAllListeners('close');
258+
cb(err);
259+
};
260+
261+
if (client.topology == null) {
262+
completeClose();
263+
return;
264+
}
265+
266+
client.topology.close(force, err => {
267+
const autoEncrypter = client.topology.s.options.autoEncrypter;
268+
if (!autoEncrypter) {
269+
completeClose(err);
270+
return;
271+
}
272+
273+
autoEncrypter.teardown(force, err2 => completeClose(err || err2));
274+
});
275+
});
238276
};
239277

240278
/**

lib/operations/close.js

-50
This file was deleted.

lib/operations/connect.js

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict';
22

3-
const OperationBase = require('./operation').OperationBase;
4-
const defineAspects = require('./operation').defineAspects;
5-
const Aspect = require('./operation').Aspect;
63
const deprecate = require('util').deprecate;
74
const Logger = require('../core').Logger;
85
const MongoCredentials = require('../core').MongoCredentials;
@@ -191,28 +188,6 @@ const LEGACY_OPTIONS_MAP = validOptionNames.reduce((obj, name) => {
191188
return obj;
192189
}, {});
193190

194-
class ConnectOperation extends OperationBase {
195-
constructor(mongoClient) {
196-
super();
197-
198-
this.mongoClient = mongoClient;
199-
}
200-
201-
execute(callback) {
202-
const mongoClient = this.mongoClient;
203-
const err = validOptions(mongoClient.s.options);
204-
205-
// Did we have a validation error
206-
if (err) return callback(err);
207-
// Fallback to callback based connect
208-
connect(mongoClient, mongoClient.s.url, mongoClient.s.options, err => {
209-
if (err) return callback(err);
210-
callback(null, mongoClient);
211-
});
212-
}
213-
}
214-
defineAspects(ConnectOperation, [Aspect.SKIP_SESSION]);
215-
216191
function addListeners(mongoClient, topology) {
217192
topology.on('authenticated', createListener(mongoClient, 'authenticated'));
218193
topology.on('error', createListener(mongoClient, 'error'));
@@ -820,4 +795,4 @@ function translateOptions(options, translationOptions) {
820795
});
821796
}
822797

823-
module.exports = ConnectOperation;
798+
module.exports = { validOptions, connect };

0 commit comments

Comments
 (0)