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

Commit 645d73d

Browse files
authored
fix(EJSON): export the result of optionally requiring EJSON
Optionally require and export EJSON from core for use in native's APM logging feature.
1 parent 86701ed commit 645d73d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: index.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var BSON = require('bson');
44
var require_optional = require('require_optional');
5+
const EJSON = require('./lib/utils').retrieveEJSON();
56

67
try {
78
// Attempt to grab the native BSON parser
@@ -31,6 +32,7 @@ module.exports = {
3132
ReadPreference: require('./lib/topologies/read_preference'),
3233
Sessions: require('./lib/sessions'),
3334
BSON: BSON,
35+
EJSON: EJSON,
3436
// Raw operations
3537
Query: require('./lib/connection/commands').Query,
3638
// Auth mechanisms

Diff for: lib/utils.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ try {
4848
console.warn(err.message);
4949
}
5050

51+
// Throw an error if an attempt to use EJSON is made when it is not installed
52+
const noEJSONError = function() {
53+
throw new Error('The `mongodb-extjson` package was not found. Please install it and try again.');
54+
};
55+
56+
// Facilitate loading EJSON optionally
57+
const retrieveEJSON = function() {
58+
let EJSON = null;
59+
try {
60+
EJSON = requireOptional('mongodb-extjson');
61+
} catch (error) {} // eslint-disable-line
62+
if (!EJSON) {
63+
EJSON = {
64+
parse: noEJSONError,
65+
deserialize: noEJSONError,
66+
serialize: noEJSONError,
67+
stringify: noEJSONError,
68+
setBSONModule: noEJSONError,
69+
BSON: noEJSONError
70+
};
71+
}
72+
return EJSON;
73+
};
74+
5175
/*
5276
* Checks that collation is supported by server.
5377
*
@@ -66,5 +90,6 @@ module.exports = {
6690
relayEvents,
6791
Kerberos,
6892
MongoAuthProcess,
69-
collationNotSupported
93+
collationNotSupported,
94+
retrieveEJSON
7095
};

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"peerOptionalDependencies": {
4242
"kerberos": ">= 0.0.23 < 1.0.0",
43+
"mongodb-extjson": "^2.1.2",
4344
"snappy": "^6.0.1",
4445
"bson-ext": "^2.0.0"
4546
},

0 commit comments

Comments
 (0)