Skip to content

Commit 89d94ad

Browse files
committed
chore(AutoEncryption): delegate mongocryptd to mongodb-client-encryption
Delegates management of almost all items associated with autoEncryption to the autoEncryptor. autoEncryptor now handles spawning of mongocryptd and mongocryptd client * expose MongoTimeoutError * delegate mongocryptd to mongodb-client-encryption * incorporate feedback
1 parent c2d80b2 commit 89d94ad

File tree

3 files changed

+15
-35
lines changed

3 files changed

+15
-35
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const connect = require('./lib/mongo_client').connect;
1010
// Expose error class
1111
connect.MongoError = core.MongoError;
1212
connect.MongoNetworkError = core.MongoNetworkError;
13+
connect.MongoTimeoutError = core.MongoTimeoutError;
1314

1415
// Actual driver classes exported
1516
connect.Admin = require('./lib/admin');

lib/operations/close.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ class CloseOperation extends OperationBase {
2323
client.removeAllListeners('close');
2424
callback(err, null);
2525
};
26-
const mongocryptdClientClose = err => {
27-
const mongocryptdClient = client.s.mongocryptdClient;
28-
if (!mongocryptdClient) {
29-
completeClose(err);
30-
return;
31-
}
32-
33-
mongocryptdClient.close(force, err2 => completeClose(err || err2));
34-
};
3526

3627
if (client.topology == null) {
37-
mongocryptdClientClose();
28+
completeClose();
3829
return;
3930
}
4031

41-
client.topology.close(force, mongocryptdClientClose);
32+
client.topology.close(force, err => {
33+
const autoEncrypter = client.topology.s.options.autoEncrypter;
34+
if (!autoEncrypter) {
35+
completeClose(err);
36+
return;
37+
}
38+
39+
autoEncrypter.teardown(force, err2 => completeClose(err || err2));
40+
});
4241
}
4342
}
4443

lib/operations/connect.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const os = require('os');
43
const OperationBase = require('./operation').OperationBase;
54
const defineAspects = require('./operation').defineAspects;
65
const Aspect = require('./operation').Aspect;
@@ -482,35 +481,16 @@ function createTopology(mongoClient, topologyType, options, callback) {
482481
return;
483482
}
484483
try {
485-
AutoEncrypter = require('mongodb-client-encryption').AutoEncrypter;
484+
AutoEncrypter = require('mongodb-client-encryption')(require('../../index')).AutoEncrypter;
486485
} catch (err) {
487486
callback(err);
488487
return;
489488
}
490489

491-
const MongoClient = loadClient();
492-
let connectionString;
493-
if (options.autoEncryption.extraOptions && options.autoEncryption.extraOptions.mongocryptURI) {
494-
connectionString = options.autoEncryption.extraOptions.mongocryptURI;
495-
} else if (os.platform() === 'win32') {
496-
connectionString = 'mongodb://localhost:27020/?serverSelectionTimeoutMS=1000';
497-
} else {
498-
connectionString = 'mongodb://%2Ftmp%2Fmongocryptd.sock/?serverSelectionTimeoutMS=1000';
499-
}
500-
501-
const mongocryptdClient = new MongoClient(connectionString, {
502-
useNewUrlParser: true,
503-
useUnifiedTopology: true
504-
});
505-
mongoClient.s.mongocryptdClient = mongocryptdClient;
506-
mongocryptdClient.connect(err => {
490+
const mongoCryptOptions = Object.assign({}, options.autoEncryption);
491+
topology.s.options.autoEncrypter = new AutoEncrypter(mongoClient, mongoCryptOptions);
492+
topology.s.options.autoEncrypter.init(err => {
507493
if (err) return callback(err, null);
508-
509-
const mongoCryptOptions = Object.assign({}, options.autoEncryption, {
510-
mongocryptdClient
511-
});
512-
513-
topology.s.options.autoEncrypter = new AutoEncrypter(mongoClient, mongoCryptOptions);
514494
callback(null, newTopology);
515495
});
516496
});

0 commit comments

Comments
 (0)