Skip to content

Commit

Permalink
fix(NODE-3199): unable to bundle driver due to uncaught require (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jul 20, 2021
1 parent 9244b17 commit 60efe9d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/core/auth/mongodb_aws.js
Expand Up @@ -9,6 +9,7 @@ const url = require('url');

let aws4;
try {
// Ensure you always wrap an optional require in the try block NODE-3199
aws4 = require('aws4');
} catch (e) {
// don't do anything;
Expand Down
1 change: 1 addition & 0 deletions lib/core/auth/scram.js
Expand Up @@ -11,6 +11,7 @@ const Binary = BSON.Binary;

let saslprep;
try {
// Ensure you always wrap an optional require in the try block NODE-3199
saslprep = require('saslprep');
} catch (e) {
// don't do anything;
Expand Down
1 change: 1 addition & 0 deletions lib/core/index.js
Expand Up @@ -5,6 +5,7 @@ const require_optional = require('optional-require')(require);
const EJSON = require('./utils').retrieveEJSON();

try {
// Ensure you always wrap an optional require in the try block NODE-3199
// Attempt to grab the native BSON parser
const BSONNative = require_optional('bson-ext');
// If we got the native parser, use it instead of the
Expand Down
1 change: 1 addition & 0 deletions lib/core/utils.js
Expand Up @@ -27,6 +27,7 @@ function retrieveKerberos() {
let kerberos;

try {
// Ensure you always wrap an optional require in the try block NODE-3199
kerberos = requireOptional('kerberos');
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
Expand Down
11 changes: 8 additions & 3 deletions lib/encrypter.js
Expand Up @@ -3,22 +3,27 @@ const MongoClient = require('./mongo_client');
const BSON = require('./core/connection/utils').retrieveBSON();
const MongoError = require('./core/error').MongoError;

let mongodbClientEncryption = undefined;
try {
require.resolve('mongodb-client-encryption');
// Ensure you always wrap an optional require in the try block NODE-3199
mongodbClientEncryption = require('mongodb-client-encryption');
} catch (err) {
throw new MongoError(
'Auto-encryption requested, but the module is not installed. ' +
'Please add `mongodb-client-encryption` as a dependency of your project'
);
}

const mongodbClientEncryption = require('mongodb-client-encryption');
if (typeof mongodbClientEncryption.extension !== 'function') {
if (
mongodbClientEncryption === undefined ||
typeof mongodbClientEncryption.extension !== 'function'
) {
throw new MongoError(
'loaded version of `mongodb-client-encryption` does not have property `extension`. ' +
'Please make sure you are loading the correct version of `mongodb-client-encryption`'
);
}

const AutoEncrypter = mongodbClientEncryption.extension(require('../index')).AutoEncrypter;

const kInternalClient = Symbol('internalClient');
Expand Down
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 60efe9d

Please sign in to comment.