Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(EJSON): export the result of optionally requiring EJSON
Browse files Browse the repository at this point in the history
Optionally require and export EJSON from core for use in native's APM logging feature.
  • Loading branch information
rweinberger authored Aug 17, 2018
1 parent 86701ed commit 645d73d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var BSON = require('bson');
var require_optional = require('require_optional');
const EJSON = require('./lib/utils').retrieveEJSON();

try {
// Attempt to grab the native BSON parser
Expand Down Expand Up @@ -31,6 +32,7 @@ module.exports = {
ReadPreference: require('./lib/topologies/read_preference'),
Sessions: require('./lib/sessions'),
BSON: BSON,
EJSON: EJSON,
// Raw operations
Query: require('./lib/connection/commands').Query,
// Auth mechanisms
Expand Down
27 changes: 26 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ try {
console.warn(err.message);
}

// Throw an error if an attempt to use EJSON is made when it is not installed
const noEJSONError = function() {
throw new Error('The `mongodb-extjson` package was not found. Please install it and try again.');
};

// Facilitate loading EJSON optionally
const retrieveEJSON = function() {
let EJSON = null;
try {
EJSON = requireOptional('mongodb-extjson');
} catch (error) {} // eslint-disable-line
if (!EJSON) {
EJSON = {
parse: noEJSONError,
deserialize: noEJSONError,
serialize: noEJSONError,
stringify: noEJSONError,
setBSONModule: noEJSONError,
BSON: noEJSONError
};
}
return EJSON;
};

/*
* Checks that collation is supported by server.
*
Expand All @@ -66,5 +90,6 @@ module.exports = {
relayEvents,
Kerberos,
MongoAuthProcess,
collationNotSupported
collationNotSupported,
retrieveEJSON
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"peerOptionalDependencies": {
"kerberos": ">= 0.0.23 < 1.0.0",
"mongodb-extjson": "^2.1.2",
"snappy": "^6.0.1",
"bson-ext": "^2.0.0"
},
Expand Down

0 comments on commit 645d73d

Please sign in to comment.