Skip to content

Commit 60efe9d

Browse files
authored
fix(NODE-3199): unable to bundle driver due to uncaught require (#2903)
1 parent 9244b17 commit 60efe9d

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

lib/core/auth/mongodb_aws.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const url = require('url');
99

1010
let aws4;
1111
try {
12+
// Ensure you always wrap an optional require in the try block NODE-3199
1213
aws4 = require('aws4');
1314
} catch (e) {
1415
// don't do anything;

lib/core/auth/scram.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Binary = BSON.Binary;
1111

1212
let saslprep;
1313
try {
14+
// Ensure you always wrap an optional require in the try block NODE-3199
1415
saslprep = require('saslprep');
1516
} catch (e) {
1617
// don't do anything;

lib/core/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const require_optional = require('optional-require')(require);
55
const EJSON = require('./utils').retrieveEJSON();
66

77
try {
8+
// Ensure you always wrap an optional require in the try block NODE-3199
89
// Attempt to grab the native BSON parser
910
const BSONNative = require_optional('bson-ext');
1011
// If we got the native parser, use it instead of the

lib/core/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function retrieveKerberos() {
2727
let kerberos;
2828

2929
try {
30+
// Ensure you always wrap an optional require in the try block NODE-3199
3031
kerberos = requireOptional('kerberos');
3132
} catch (err) {
3233
if (err.code === 'MODULE_NOT_FOUND') {

lib/encrypter.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@ const MongoClient = require('./mongo_client');
33
const BSON = require('./core/connection/utils').retrieveBSON();
44
const MongoError = require('./core/error').MongoError;
55

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

15-
const mongodbClientEncryption = require('mongodb-client-encryption');
16-
if (typeof mongodbClientEncryption.extension !== 'function') {
17+
if (
18+
mongodbClientEncryption === undefined ||
19+
typeof mongodbClientEncryption.extension !== 'function'
20+
) {
1721
throw new MongoError(
1822
'loaded version of `mongodb-client-encryption` does not have property `extension`. ' +
1923
'Please make sure you are loading the correct version of `mongodb-client-encryption`'
2024
);
2125
}
26+
2227
const AutoEncrypter = mongodbClientEncryption.extension(require('../index')).AutoEncrypter;
2328

2429
const kInternalClient = Symbol('internalClient');

package-lock.json

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)