Skip to content

Commit

Permalink
refactor: minor modifications required to use core with native
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed May 11, 2019
1 parent befc02c commit 16f9a1c
Show file tree
Hide file tree
Showing 131 changed files with 819 additions and 850 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
],
"env": {
"node": true,
"mocha": true
"mocha": true,
"es6": true
},
"globals": {
"Promise": true,
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

// Core module
const core = require('mongodb-core');
const core = require('./lib/core');
const Instrumentation = require('./lib/apm');

// Set up the connect function
Expand All @@ -19,7 +19,7 @@ connect.Collection = require('./lib/collection');
connect.Server = require('./lib/topologies/server');
connect.ReplSet = require('./lib/topologies/replset');
connect.Mongos = require('./lib/topologies/mongos');
connect.ReadPreference = require('mongodb-core').ReadPreference;
connect.ReadPreference = core.ReadPreference;
connect.GridStore = require('./lib/gridfs/grid_store');
connect.Chunk = require('./lib/gridfs/chunk');
connect.Logger = core.Logger;
Expand All @@ -28,8 +28,8 @@ connect.CommandCursor = require('./lib/command_cursor');
connect.Cursor = require('./lib/cursor');
connect.GridFSBucket = require('./lib/gridfs-stream');
// Exported to be used in tests not to be used anywhere else
connect.CoreServer = require('mongodb-core').Server;
connect.CoreConnection = require('mongodb-core').Connection;
connect.CoreServer = core.Server;
connect.CoreConnection = core.Connection;

// BSON types exported
connect.Binary = core.BSON.Binary;
Expand Down
2 changes: 1 addition & 1 deletion lib/aggregation_cursor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const inherits = require('util').inherits;
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('./core').MongoError;
const Readable = require('stream').Readable;
const CoreCursor = require('./cursor');
const deprecate = require('util').deprecate;
Expand Down
10 changes: 5 additions & 5 deletions lib/bulk/common.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const Long = require('mongodb-core').BSON.Long;
const MongoError = require('mongodb-core').MongoError;
const ObjectID = require('mongodb-core').BSON.ObjectID;
const BSON = require('mongodb-core').BSON;
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
const Long = require('../core').BSON.Long;
const MongoError = require('../core').MongoError;
const ObjectID = require('../core').BSON.ObjectID;
const BSON = require('../core').BSON;
const MongoWriteConcernError = require('../core').MongoWriteConcernError;
const toError = require('../utils').toError;
const handleCallback = require('../utils').handleCallback;
const applyRetryableWrites = require('../utils').applyRetryableWrites;
Expand Down
2 changes: 1 addition & 1 deletion lib/change_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const EventEmitter = require('events');
const isResumableError = require('./error').isResumableError;
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('./core').MongoError;
const ReadConcern = require('./read_concern');
const MongoDBNamespace = require('./utils').MongoDBNamespace;

Expand Down
6 changes: 3 additions & 3 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
const deprecate = require('util').deprecate;
const deprecateOptions = require('./utils').deprecateOptions;
const checkCollectionName = require('./utils').checkCollectionName;
const ObjectID = require('mongodb-core').BSON.ObjectID;
const MongoError = require('mongodb-core').MongoError;
const ObjectID = require('./core').BSON.ObjectID;
const MongoError = require('./core').MongoError;
const toError = require('./utils').toError;
const handleCallback = require('./utils').handleCallback;
const normalizeHintField = require('./utils').normalizeHintField;
const decorateCommand = require('./utils').decorateCommand;
const decorateWithCollation = require('./utils').decorateWithCollation;
const decorateWithReadConcern = require('./utils').decorateWithReadConcern;
const formattedOrderClause = require('./utils').formattedOrderClause;
const ReadPreference = require('mongodb-core').ReadPreference;
const ReadPreference = require('./core').ReadPreference;
const unordered = require('./bulk/unordered');
const ordered = require('./bulk/ordered');
const ChangeStream = require('./change_stream');
Expand Down
4 changes: 2 additions & 2 deletions lib/command_cursor.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const inherits = require('util').inherits;
const ReadPreference = require('mongodb-core').ReadPreference;
const MongoError = require('mongodb-core').MongoError;
const ReadPreference = require('./core').ReadPreference;
const MongoError = require('./core').MongoError;
const Readable = require('stream').Readable;
const CoreCursor = require('./cursor');
const SUPPORTS = require('./utils').SUPPORTS;
Expand Down
27 changes: 0 additions & 27 deletions lib/core/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,6 @@ const Long = BSON.Long;
* allowing for iteration over the results returned from the underlying query.
*
* **CURSORS Cannot directly be instantiated**
* @example
* var Server = require('mongodb-core').Server
* , ReadPreference = require('mongodb-core').ReadPreference
* , assert = require('assert');
*
* var server = new Server({host: 'localhost', port: 27017});
* // Wait for the connection event
* server.on('connect', function(server) {
* assert.equal(null, err);
*
* // Execute the write
* var cursor = _server.cursor('integration_tests.inserts_example4', {
* find: 'integration_tests.example4'
* , query: {a:1}
* }, {
* readPreference: new ReadPreference('secondary');
* });
*
* // Get the first document
* cursor.next(function(err, doc) {
* assert.equal(null, err);
* server.destroy();
* });
* });
*
* // Start connecting
* server.connect();
*/

/**
Expand Down
50 changes: 50 additions & 0 deletions lib/core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

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

try {
// Attempt to grab the native BSON parser
const BSONNative = require_optional('bson-ext');
// If we got the native parser, use it instead of the
// Javascript one
if (BSONNative) {
BSON = BSONNative;
}
} catch (err) {} // eslint-disable-line

module.exports = {
// Errors
MongoError: require('./error').MongoError,
MongoNetworkError: require('./error').MongoNetworkError,
MongoParseError: require('./error').MongoParseError,
MongoTimeoutError: require('./error').MongoTimeoutError,
MongoWriteConcernError: require('./error').MongoWriteConcernError,
mongoErrorContextSymbol: require('./error').mongoErrorContextSymbol,
// Core
Connection: require('./connection/connection'),
Server: require('./topologies/server'),
ReplSet: require('./topologies/replset'),
Mongos: require('./topologies/mongos'),
Logger: require('./connection/logger'),
Cursor: require('./cursor'),
ReadPreference: require('./topologies/read_preference'),
Sessions: require('./sessions'),
BSON: BSON,
EJSON: EJSON,
Topology: require('./sdam/topology'),
// Raw operations
Query: require('./connection/commands').Query,
// Auth mechanisms
MongoCredentials: require('./auth/mongo_credentials').MongoCredentials,
defaultAuthProviders: require('./auth/defaultAuthProviders').defaultAuthProviders,
MongoCR: require('./auth/mongocr'),
X509: require('./auth/x509'),
Plain: require('./auth/plain'),
GSSAPI: require('./auth/gssapi'),
ScramSHA1: require('./auth/scram').ScramSHA1,
ScramSHA256: require('./auth/scram').ScramSHA256,
// Utilities
parseConnectionString: require('./uri_parser')
};
14 changes: 0 additions & 14 deletions lib/core/topologies/mongos.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ const BSON = retrieveBSON();
/**
* @fileOverview The **Mongos** class is a class that represents a Mongos Proxy topology and is
* used to construct connections.
*
* @example
* var Mongos = require('mongodb-core').Mongos
* , ReadPreference = require('mongodb-core').ReadPreference
* , assert = require('assert');
*
* var server = new Mongos([{host: 'localhost', port: 30000}]);
* // Wait for the connection event
* server.on('connect', function(server) {
* server.destroy();
* });
*
* // Start connecting
* server.connect();
*/

//
Expand Down
23 changes: 1 addition & 22 deletions lib/core/topologies/read_preference.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,8 @@
* @param {array} tags The tags object
* @param {object} [options] Additional read preference options
* @param {number} [options.maxStalenessSeconds] Max secondary read staleness in seconds, Minimum value is 90 seconds.
* @return {ReadPreference}
* @example
* const ReplSet = require('mongodb-core').ReplSet,
* ReadPreference = require('mongodb-core').ReadPreference,
* assert = require('assert');
*
* const server = new ReplSet([{host: 'localhost', port: 30000}], {setName: 'rs'});
* // Wait for the connection event
* server.on('connect', function(server) {
* const cursor = server.cursor(
* 'db.test',
* { find: 'db.test', query: {} },
* { readPreference: new ReadPreference('secondary') }
* );
*
* cursor.next(function(err, doc) {
* server.destroy();
* });
* });
*
* // Start connecting
* server.connect();
* @see https://docs.mongodb.com/manual/core/read-preference/
* @return {ReadPreference}
*/
const ReadPreference = function(mode, tags, options) {
// TODO(major): tags MUST be an array of tagsets
Expand Down
2 changes: 1 addition & 1 deletion lib/core/topologies/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function emitSDAMEvent(self, event, description) {
}

// Get package.json variable
var driverVersion = require('../../package.json').version;
var driverVersion = require('../../../package.json').version;
var nodejsversion = f('Node.js %s, %s', process.version, os.endianness());
var type = os.type();
var name = process.platform;
Expand Down
10 changes: 5 additions & 5 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const deprecate = require('util').deprecate;
const handleCallback = require('./utils').handleCallback;
const SUPPORTS = require('./utils').SUPPORTS;
const MongoDBNamespace = require('./utils').MongoDBNamespace;
const ReadPreference = require('mongodb-core').ReadPreference;
const MongoError = require('mongodb-core').MongoError;
const ReadPreference = require('./core').ReadPreference;
const MongoError = require('./core').MongoError;
const Readable = require('stream').Readable;
const CoreCursor = require('mongodb-core').Cursor;
const Map = require('mongodb-core').BSON.Map;
const CoreCursor = require('./core').Cursor;
const Map = require('./core').BSON.Map;

const each = require('./operations/cursor_ops').each;

Expand Down Expand Up @@ -57,7 +57,7 @@ const executeOperationV2 = require('./operations/execute_operation_v2');
*/

/**
* Namespace provided by the mongodb-core and node.js
* Namespace provided by the code module
* @external CoreCursor
* @external Readable
*/
Expand Down
8 changes: 4 additions & 4 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const CommandCursor = require('./command_cursor');
const handleCallback = require('./utils').handleCallback;
const filterOptions = require('./utils').filterOptions;
const toError = require('./utils').toError;
const ReadPreference = require('mongodb-core').ReadPreference;
const MongoError = require('mongodb-core').MongoError;
const ObjectID = require('mongodb-core').ObjectID;
const Logger = require('mongodb-core').Logger;
const ReadPreference = require('./core').ReadPreference;
const MongoError = require('./core').MongoError;
const ObjectID = require('./core').ObjectID;
const Logger = require('./core').Logger;
const Collection = require('./collection');
const mergeOptionsAndWriteConcern = require('./utils').mergeOptionsAndWriteConcern;
const executeOperation = require('./utils').executeOperation;
Expand Down
4 changes: 2 additions & 2 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const MongoNetworkError = require('mongodb-core').MongoNetworkError;
const mongoErrorContextSymbol = require('mongodb-core').mongoErrorContextSymbol;
const MongoNetworkError = require('./core').MongoNetworkError;
const mongoErrorContextSymbol = require('./core').mongoErrorContextSymbol;

const GET_MORE_NON_RESUMABLE_CODES = new Set([
136, // CappedPositionLost
Expand Down
2 changes: 1 addition & 1 deletion lib/gridfs-stream/upload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var core = require('mongodb-core');
var core = require('../core');
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
Expand Down
4 changes: 2 additions & 2 deletions lib/gridfs/chunk.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var Binary = require('mongodb-core').BSON.Binary,
ObjectID = require('mongodb-core').BSON.ObjectID;
var Binary = require('../core').BSON.Binary,
ObjectID = require('../core').BSON.ObjectID;

var Buffer = require('safe-buffer').Buffer;

Expand Down
8 changes: 4 additions & 4 deletions lib/gridfs/grid_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
* });
*/
const Chunk = require('./chunk');
const ObjectID = require('mongodb-core').BSON.ObjectID;
const ReadPreference = require('mongodb-core').ReadPreference;
const ObjectID = require('../core').BSON.ObjectID;
const ReadPreference = require('../core').ReadPreference;
const Buffer = require('safe-buffer').Buffer;
const fs = require('fs');
const f = require('util').format;
const util = require('util');
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('../core').MongoError;
const inherits = util.inherits;
const Duplex = require('stream').Duplex;
const shallowClone = require('../utils').shallowClone;
Expand All @@ -56,7 +56,7 @@ const deprecationFn = deprecate(() => {},
'GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead');

/**
* Namespace provided by the mongodb-core and node.js
* Namespace provided by the core module
* @external Duplex
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Db = require('./db');
const EventEmitter = require('events').EventEmitter;
const executeOperation = require('./utils').executeOperation;
const inherits = require('util').inherits;
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('./core').MongoError;
const deprecate = require('util').deprecate;
const WriteConcern = require('./write_concern');

Expand Down
4 changes: 2 additions & 2 deletions lib/operations/add_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const count = require('./collection_ops').count;
const crypto = require('crypto');
const handleCallback = require('../utils').handleCallback;
const loadDb = require('../dynamic_loaders').loadDb;
const MongoError = require('mongodb-core').MongoError;
const ReadPreference = require('mongodb-core').ReadPreference;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
const toError = require('../utils').toError;
const updateOne = require('./collection_ops').updateOne;

Expand Down
2 changes: 1 addition & 1 deletion lib/operations/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const applyWriteConcern = require('../utils').applyWriteConcern;
const decorateWithCollation = require('../utils').decorateWithCollation;
const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('../core').MongoError;
const resolveReadPreference = require('../utils').resolveReadPreference;
const toError = require('../utils').toError;

Expand Down
2 changes: 1 addition & 1 deletion lib/operations/aggregate_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const applyWriteConcern = require('../utils').applyWriteConcern;
const decorateWithCollation = require('../utils').decorateWithCollation;
const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('../core').MongoError;
const resolveReadPreference = require('../utils').resolveReadPreference;
const toError = require('../utils').toError;

Expand Down
2 changes: 1 addition & 1 deletion lib/operations/bulk_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const applyRetryableWrites = require('../utils').applyRetryableWrites;
const applyWriteConcern = require('../utils').applyWriteConcern;
const MongoError = require('mongodb-core').MongoError;
const MongoError = require('../core').MongoError;
const OperationBase = require('./operation').OperationBase;

class BulkWriteOperation extends OperationBase {
Expand Down
Loading

0 comments on commit 16f9a1c

Please sign in to comment.