encrypter.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Encrypter = void 0;
  4. /* eslint-disable @typescript-eslint/no-var-requires */
  5. const bson_1 = require("./bson");
  6. const constants_1 = require("./constants");
  7. const error_1 = require("./error");
  8. const mongo_client_1 = require("./mongo_client");
  9. let AutoEncrypterClass;
  10. /** @internal */
  11. const kInternalClient = Symbol('internalClient');
  12. /** @internal */
  13. class Encrypter {
  14. constructor(client, uri, options) {
  15. if (typeof options.autoEncryption !== 'object') {
  16. throw new error_1.MongoInvalidArgumentError('Option "autoEncryption" must be specified');
  17. }
  18. this.bypassAutoEncryption = !!options.autoEncryption.bypassAutoEncryption;
  19. this.needsConnecting = false;
  20. if (options.maxPoolSize === 0 && options.autoEncryption.keyVaultClient == null) {
  21. options.autoEncryption.keyVaultClient = client;
  22. }
  23. else if (options.autoEncryption.keyVaultClient == null) {
  24. options.autoEncryption.keyVaultClient = this.getInternalClient(client, uri, options);
  25. }
  26. if (this.bypassAutoEncryption) {
  27. options.autoEncryption.metadataClient = undefined;
  28. }
  29. else if (options.maxPoolSize === 0) {
  30. options.autoEncryption.metadataClient = client;
  31. }
  32. else {
  33. options.autoEncryption.metadataClient = this.getInternalClient(client, uri, options);
  34. }
  35. if (options.proxyHost) {
  36. options.autoEncryption.proxyOptions = {
  37. proxyHost: options.proxyHost,
  38. proxyPort: options.proxyPort,
  39. proxyUsername: options.proxyUsername,
  40. proxyPassword: options.proxyPassword
  41. };
  42. }
  43. options.autoEncryption.bson = Object.create(null);
  44. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  45. options.autoEncryption.bson.serialize = bson_1.serialize;
  46. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  47. options.autoEncryption.bson.deserialize = bson_1.deserialize;
  48. this.autoEncrypter = new AutoEncrypterClass(client, options.autoEncryption);
  49. }
  50. getInternalClient(client, uri, options) {
  51. if (!this[kInternalClient]) {
  52. const clonedOptions = {};
  53. for (const key of Object.keys(options)) {
  54. if (['autoEncryption', 'minPoolSize', 'servers', 'caseTranslate', 'dbName'].includes(key))
  55. continue;
  56. Reflect.set(clonedOptions, key, Reflect.get(options, key));
  57. }
  58. clonedOptions.minPoolSize = 0;
  59. this[kInternalClient] = new mongo_client_1.MongoClient(uri, clonedOptions);
  60. for (const eventName of constants_1.MONGO_CLIENT_EVENTS) {
  61. for (const listener of client.listeners(eventName)) {
  62. this[kInternalClient].on(eventName, listener);
  63. }
  64. }
  65. client.on('newListener', (eventName, listener) => {
  66. this[kInternalClient].on(eventName, listener);
  67. });
  68. this.needsConnecting = true;
  69. }
  70. return this[kInternalClient];
  71. }
  72. connectInternalClient(callback) {
  73. if (this.needsConnecting) {
  74. this.needsConnecting = false;
  75. return this[kInternalClient].connect(callback);
  76. }
  77. return callback();
  78. }
  79. close(client, force, callback) {
  80. this.autoEncrypter.teardown(!!force, e => {
  81. if (this[kInternalClient] && client !== this[kInternalClient]) {
  82. return this[kInternalClient].close(force, callback);
  83. }
  84. callback(e);
  85. });
  86. }
  87. static checkForMongoCrypt() {
  88. let mongodbClientEncryption = undefined;
  89. try {
  90. // Ensure you always wrap an optional require in the try block NODE-3199
  91. mongodbClientEncryption = require('mongodb-client-encryption');
  92. }
  93. catch (err) {
  94. throw new error_1.MongoMissingDependencyError('Auto-encryption requested, but the module is not installed. ' +
  95. 'Please add `mongodb-client-encryption` as a dependency of your project');
  96. }
  97. AutoEncrypterClass = mongodbClientEncryption.extension(require('../lib/index')).AutoEncrypter;
  98. }
  99. }
  100. exports.Encrypter = Encrypter;
  101. //# sourceMappingURL=encrypter.js.map