Skip to content

Commit

Permalink
refactor: use maybePromise for all MongoClient fake operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 24, 2020
1 parent 6f71507 commit f6a8ceb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 87 deletions.
5 changes: 4 additions & 1 deletion lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ function makeClientMetadata(options) {
return metadata;
}

const noop = () => {};

module.exports = {
uuidV4,
calculateDurationInMs,
Expand All @@ -270,5 +272,6 @@ module.exports = {
tagsStrictEqual,
errorStrictEqual,
makeStateMachine,
makeClientMetadata
makeClientMetadata,
noop
};
58 changes: 48 additions & 10 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
const ChangeStream = require('./change_stream');
const Db = require('./db');
const EventEmitter = require('events').EventEmitter;
const executeOperation = require('./operations/execute_operation');
const inherits = require('util').inherits;
const MongoError = require('./core').MongoError;
const deprecate = require('util').deprecate;
const WriteConcern = require('./write_concern');
const MongoDBNamespace = require('./utils').MongoDBNamespace;
const ReadPreference = require('./core/topologies/read_preference');

// Operations
const ConnectOperation = require('./operations/connect');
const CloseOperation = require('./operations/close');
const maybePromise = require('./utils').maybePromise;
const NativeTopology = require('./topologies/native_topology');
const connect = require('./operations/connect').connect;
const validOptions = require('./operations/connect').validOptions;

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

const operation = new ConnectOperation(this);
const client = this;
return maybePromise(callback, cb => {
const err = validOptions(client.s.options);
if (err) return cb(err);

return executeOperation(this, operation, callback);
connect(client, client.s.url, client.s.options, err => {
if (err) return cb(err);
cb(null, client);
});
});
};

MongoClient.prototype.logout = deprecate(function(options, callback) {
Expand All @@ -232,9 +238,41 @@ MongoClient.prototype.logout = deprecate(function(options, callback) {
* @return {Promise} returns Promise if no callback passed
*/
MongoClient.prototype.close = function(force, callback) {
if (typeof force === 'function') (callback = force), (force = false);
const operation = new CloseOperation(this, force);
return executeOperation(this, operation, callback);
if (typeof force === 'function') {
callback = force;
force = false;
}

const client = this;
return maybePromise(callback, cb => {
const completeClose = err => {
client.emit('close', client);

if (!(client.topology instanceof NativeTopology)) {
for (const item of client.s.dbCache) {
item[1].emit('close', client);
}
}

client.removeAllListeners('close');
cb(err);
};

if (client.topology == null) {
completeClose();
return;
}

client.topology.close(force, err => {
const autoEncrypter = client.topology.s.options.autoEncrypter;
if (!autoEncrypter) {
completeClose(err);
return;
}

autoEncrypter.teardown(force, err2 => completeClose(err || err2));
});
});
};

/**
Expand Down
50 changes: 0 additions & 50 deletions lib/operations/close.js

This file was deleted.

27 changes: 1 addition & 26 deletions lib/operations/connect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const OperationBase = require('./operation').OperationBase;
const defineAspects = require('./operation').defineAspects;
const Aspect = require('./operation').Aspect;
const deprecate = require('util').deprecate;
const Logger = require('../core').Logger;
const MongoCredentials = require('../core').MongoCredentials;
Expand Down Expand Up @@ -191,28 +188,6 @@ const LEGACY_OPTIONS_MAP = validOptionNames.reduce((obj, name) => {
return obj;
}, {});

class ConnectOperation extends OperationBase {
constructor(mongoClient) {
super();

this.mongoClient = mongoClient;
}

execute(callback) {
const mongoClient = this.mongoClient;
const err = validOptions(mongoClient.s.options);

// Did we have a validation error
if (err) return callback(err);
// Fallback to callback based connect
connect(mongoClient, mongoClient.s.url, mongoClient.s.options, err => {
if (err) return callback(err);
callback(null, mongoClient);
});
}
}
defineAspects(ConnectOperation, [Aspect.SKIP_SESSION]);

function addListeners(mongoClient, topology) {
topology.on('authenticated', createListener(mongoClient, 'authenticated'));
topology.on('error', createListener(mongoClient, 'error'));
Expand Down Expand Up @@ -820,4 +795,4 @@ function translateOptions(options, translationOptions) {
});
}

module.exports = ConnectOperation;
module.exports = { validOptions, connect };

0 comments on commit f6a8ceb

Please sign in to comment.