encrypter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Encrypter = void 0;
  4. const auto_encrypter_1 = require("./client-side-encryption/auto_encrypter");
  5. const constants_1 = require("./constants");
  6. const deps_1 = require("./deps");
  7. const error_1 = require("./error");
  8. const mongo_client_1 = require("./mongo_client");
  9. /** @internal */
  10. class Encrypter {
  11. constructor(client, uri, options) {
  12. if (typeof options.autoEncryption !== 'object') {
  13. throw new error_1.MongoInvalidArgumentError('Option "autoEncryption" must be specified');
  14. }
  15. // initialize to null, if we call getInternalClient, we may set this it is important to not overwrite those function calls.
  16. this.internalClient = null;
  17. this.bypassAutoEncryption = !!options.autoEncryption.bypassAutoEncryption;
  18. this.needsConnecting = false;
  19. if (options.maxPoolSize === 0 && options.autoEncryption.keyVaultClient == null) {
  20. options.autoEncryption.keyVaultClient = client;
  21. }
  22. else if (options.autoEncryption.keyVaultClient == null) {
  23. options.autoEncryption.keyVaultClient = this.getInternalClient(client, uri, options);
  24. }
  25. if (this.bypassAutoEncryption) {
  26. options.autoEncryption.metadataClient = undefined;
  27. }
  28. else if (options.maxPoolSize === 0) {
  29. options.autoEncryption.metadataClient = client;
  30. }
  31. else {
  32. options.autoEncryption.metadataClient = this.getInternalClient(client, uri, options);
  33. }
  34. if (options.proxyHost) {
  35. options.autoEncryption.proxyOptions = {
  36. proxyHost: options.proxyHost,
  37. proxyPort: options.proxyPort,
  38. proxyUsername: options.proxyUsername,
  39. proxyPassword: options.proxyPassword
  40. };
  41. }
  42. this.autoEncrypter = new auto_encrypter_1.AutoEncrypter(client, options.autoEncryption);
  43. }
  44. getInternalClient(client, uri, options) {
  45. let internalClient = this.internalClient;
  46. if (internalClient == null) {
  47. const clonedOptions = {};
  48. for (const key of [
  49. ...Object.getOwnPropertyNames(options),
  50. ...Object.getOwnPropertySymbols(options)
  51. ]) {
  52. if (['autoEncryption', 'minPoolSize', 'servers', 'caseTranslate', 'dbName'].includes(key))
  53. continue;
  54. Reflect.set(clonedOptions, key, Reflect.get(options, key));
  55. }
  56. clonedOptions.minPoolSize = 0;
  57. internalClient = new mongo_client_1.MongoClient(uri, clonedOptions);
  58. this.internalClient = internalClient;
  59. for (const eventName of constants_1.MONGO_CLIENT_EVENTS) {
  60. for (const listener of client.listeners(eventName)) {
  61. internalClient.on(eventName, listener);
  62. }
  63. }
  64. client.on('newListener', (eventName, listener) => {
  65. internalClient?.on(eventName, listener);
  66. });
  67. this.needsConnecting = true;
  68. }
  69. return internalClient;
  70. }
  71. async connectInternalClient() {
  72. const internalClient = this.internalClient;
  73. if (this.needsConnecting && internalClient != null) {
  74. this.needsConnecting = false;
  75. await internalClient.connect();
  76. }
  77. }
  78. async close(client) {
  79. let error;
  80. try {
  81. await this.autoEncrypter.close();
  82. }
  83. catch (autoEncrypterError) {
  84. error = autoEncrypterError;
  85. }
  86. const internalClient = this.internalClient;
  87. if (internalClient != null && client !== internalClient) {
  88. return await internalClient.close();
  89. }
  90. if (error != null) {
  91. throw error;
  92. }
  93. }
  94. static checkForMongoCrypt() {
  95. const mongodbClientEncryption = (0, deps_1.getMongoDBClientEncryption)();
  96. if ('kModuleError' in mongodbClientEncryption) {
  97. throw new error_1.MongoMissingDependencyError('Auto-encryption requested, but the module is not installed. ' +
  98. 'Please add `mongodb-client-encryption` as a dependency of your project', {
  99. cause: mongodbClientEncryption['kModuleError'],
  100. dependencyName: 'mongodb-client-encryption'
  101. });
  102. }
  103. }
  104. }
  105. exports.Encrypter = Encrypter;
  106. //# sourceMappingURL=encrypter.js.map