mongodb 3.3.1 → 3.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/HISTORY.md +82 -0
  2. package/lib/aggregation_cursor.js +1 -1
  3. package/lib/bulk/common.js +166 -73
  4. package/lib/bulk/ordered.js +1 -1
  5. package/lib/bulk/unordered.js +1 -0
  6. package/lib/change_stream.js +2 -3
  7. package/lib/collection.js +70 -15
  8. package/lib/command_cursor.js +1 -1
  9. package/lib/core/connection/connect.js +20 -5
  10. package/lib/core/connection/connection.js +7 -3
  11. package/lib/core/connection/msg.js +3 -2
  12. package/lib/core/connection/pool.js +192 -206
  13. package/lib/core/cursor.js +1 -1
  14. package/lib/core/error.js +6 -1
  15. package/lib/core/sdam/monitoring.js +8 -1
  16. package/lib/core/sdam/server.js +90 -34
  17. package/lib/core/sdam/server_description.js +29 -0
  18. package/lib/core/sdam/topology.js +200 -91
  19. package/lib/core/sdam/topology_description.js +5 -3
  20. package/lib/core/topologies/mongos.js +72 -24
  21. package/lib/core/topologies/replset.js +34 -15
  22. package/lib/core/topologies/replset_state.js +5 -5
  23. package/lib/core/topologies/server.js +13 -12
  24. package/lib/core/topologies/shared.js +11 -15
  25. package/lib/core/uri_parser.js +8 -2
  26. package/lib/core/utils.js +40 -2
  27. package/lib/cursor.js +1 -1
  28. package/lib/db.js +3 -3
  29. package/lib/gridfs-stream/download.js +12 -5
  30. package/lib/gridfs-stream/index.js +1 -1
  31. package/lib/mongo_client.js +3 -3
  32. package/lib/operations/close.js +6 -2
  33. package/lib/operations/common_functions.js +4 -0
  34. package/lib/operations/connect.js +16 -13
  35. package/lib/operations/execute_operation.js +28 -12
  36. package/lib/operations/replace_one.js +1 -1
  37. package/lib/topologies/mongos.js +1 -1
  38. package/lib/topologies/replset.js +1 -1
  39. package/lib/topologies/server.js +1 -1
  40. package/lib/topologies/topology_base.js +4 -5
  41. package/package.json +11 -5
@@ -39,7 +39,7 @@ const CHANGE_DOMAIN_TYPES = {
39
39
  * @property {ResumeToken} [resumeAfter] Allows you to start a changeStream after a specified event. See {@link https://docs.mongodb.com/master/changeStreams/#resumeafter-for-change-streams|ChangeStream documentation}.
40
40
  * @property {ResumeToken} [startAfter] Similar to resumeAfter, but will allow you to start after an invalidated event. See {@link https://docs.mongodb.com/master/changeStreams/#startafter-for-change-streams|ChangeStream documentation}.
41
41
  * @property {OperationTime} [startAtOperationTime] Will start the changeStream after the specified operationTime.
42
- * @property {number} [batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
42
+ * @property {number} [batchSize=1000] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
43
43
  * @property {object} [collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
44
44
  * @property {ReadPreference} [readPreference] The read preference. Defaults to the read preference of the database or collection. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}.
45
45
  */
@@ -367,10 +367,9 @@ function createChangeStreamCursor(self, options) {
367
367
 
368
368
  const pipeline = [{ $changeStream: changeStreamStageOptions }].concat(self.pipeline);
369
369
  const cursorOptions = applyKnownOptions({}, options, CURSOR_OPTIONS);
370
- const changeStreamOptions = Object.assign({ batchSize: 1 }, options);
371
370
  const changeStreamCursor = new ChangeStreamCursor(
372
371
  self.topology,
373
- new AggregateOperation(self.parent, pipeline, changeStreamOptions),
372
+ new AggregateOperation(self.parent, pipeline, options),
374
373
  cursorOptions
375
374
  );
376
375
 
package/lib/collection.js CHANGED
@@ -260,7 +260,7 @@ const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot'];
260
260
  * @param {boolean} [options.snapshot=false] DEPRECATED: Snapshot query.
261
261
  * @param {boolean} [options.timeout=false] Specify if the cursor can timeout.
262
262
  * @param {boolean} [options.tailable=false] Specify if the cursor is tailable.
263
- * @param {number} [options.batchSize=0] Set the batchSize for the getMoreCommand when iterating over the query results.
263
+ * @param {number} [options.batchSize=1000] Set the batchSize for the getMoreCommand when iterating over the query results.
264
264
  * @param {boolean} [options.returnKey=false] Only return the index key.
265
265
  * @param {number} [options.maxScan] DEPRECATED: Limit the number of items to scan.
266
266
  * @param {number} [options.min] Set index bounds.
@@ -667,7 +667,7 @@ Collection.prototype.insert = deprecate(function(docs, options, callback) {
667
667
  * @property {Object} upsertedId The upserted id.
668
668
  * @property {ObjectId} upsertedId._id The upserted _id returned from the server.
669
669
  * @property {Object} message
670
- * @property {Array} ops
670
+ * @property {object[]} [ops] In a response to {@link Collection#replaceOne replaceOne}, contains the new value of the document on the server. This is the same document that was originally passed in, and is only here for legacy purposes.
671
671
  */
672
672
 
673
673
  /**
@@ -690,6 +690,7 @@ Collection.prototype.insert = deprecate(function(docs, options, callback) {
690
690
  * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
691
691
  * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
692
692
  * @param {ClientSession} [options.session] optional session to use for this operation
693
+ * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.
693
694
  * @param {Collection~updateWriteOpCallback} [callback] The command result callback
694
695
  * @return {Promise} returns Promise if no callback passed
695
696
  */
@@ -728,8 +729,9 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
728
729
  * @param {boolean} [options.j=false] Specify a journal write concern.
729
730
  * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
730
731
  * @param {ClientSession} [options.session] optional session to use for this operation
732
+ * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.
731
733
  * @param {Collection~updateWriteOpCallback} [callback] The command result callback
732
- * @return {Promise<Collection~updatewriteOpResultObject>} returns Promise if no callback passed
734
+ * @return {Promise<Collection~updateWriteOpResult>} returns Promise if no callback passed
733
735
  */
734
736
  Collection.prototype.replaceOne = function(filter, doc, options, callback) {
735
737
  if (typeof options === 'function') (callback = options), (options = {});
@@ -758,8 +760,9 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) {
758
760
  * @param {boolean} [options.j=false] Specify a journal write concern.
759
761
  * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
760
762
  * @param {ClientSession} [options.session] optional session to use for this operation
763
+ * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.
761
764
  * @param {Collection~updateWriteOpCallback} [callback] The command result callback
762
- * @return {Promise<Collection~updateWriteOpResultObject>} returns Promise if no callback passed
765
+ * @return {Promise<Collection~updateWriteOpResult>} returns Promise if no callback passed
763
766
  */
764
767
  Collection.prototype.updateMany = function(filter, update, options, callback) {
765
768
  if (typeof options === 'function') (callback = options), (options = {});
@@ -799,6 +802,7 @@ Collection.prototype.updateMany = function(filter, update, options, callback) {
799
802
  * @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
800
803
  * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
801
804
  * @param {ClientSession} [options.session] optional session to use for this operation
805
+ * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.
802
806
  * @param {Collection~writeOpCallback} [callback] The command result callback
803
807
  * @throws {MongoError}
804
808
  * @return {Promise} returns Promise if no callback passed
@@ -985,7 +989,7 @@ Collection.prototype.save = deprecate(function(doc, options, callback) {
985
989
  * @param {boolean} [options.snapshot=false] DEPRECATED: Snapshot query.
986
990
  * @param {boolean} [options.timeout=false] Specify if the cursor can timeout.
987
991
  * @param {boolean} [options.tailable=false] Specify if the cursor is tailable.
988
- * @param {number} [options.batchSize=0] Set the batchSize for the getMoreCommand when iterating over the query results.
992
+ * @param {number} [options.batchSize=1] Set the batchSize for the getMoreCommand when iterating over the query results.
989
993
  * @param {boolean} [options.returnKey=false] Only return the index key.
990
994
  * @param {number} [options.maxScan] DEPRECATED: Limit the number of items to scan.
991
995
  * @param {number} [options.min] Set index bounds.
@@ -1119,7 +1123,7 @@ Collection.prototype.isCapped = function(options, callback) {
1119
1123
  /**
1120
1124
  * Creates an index on the db and collection collection.
1121
1125
  * @method
1122
- * @param {(string|object)} fieldOrSpec Defines the index.
1126
+ * @param {(string|array|object)} fieldOrSpec Defines the index.
1123
1127
  * @param {object} [options] Optional settings.
1124
1128
  * @param {(number|string)} [options.w] The write concern.
1125
1129
  * @param {number} [options.wtimeout] The write concern timeout.
@@ -1138,6 +1142,25 @@ Collection.prototype.isCapped = function(options, callback) {
1138
1142
  * @param {ClientSession} [options.session] optional session to use for this operation
1139
1143
  * @param {Collection~resultCallback} [callback] The command result callback
1140
1144
  * @return {Promise} returns Promise if no callback passed
1145
+ * @example
1146
+ * const collection = client.db('foo').collection('bar');
1147
+ *
1148
+ * await collection.createIndex({ a: 1, b: -1 });
1149
+ *
1150
+ * // Alternate syntax for { c: 1, d: -1 } that ensures order of indexes
1151
+ * await collection.createIndex([ [c, 1], [d, -1] ]);
1152
+ *
1153
+ * // Equivalent to { e: 1 }
1154
+ * await collection.createIndex('e');
1155
+ *
1156
+ * // Equivalent to { f: 1, g: 1 }
1157
+ * await collection.createIndex(['f', 'g'])
1158
+ *
1159
+ * // Equivalent to { h: 1, i: -1 }
1160
+ * await collection.createIndex([ { h: 1 }, { i: -1 } ]);
1161
+ *
1162
+ * // Equivalent to { j: 1, k: -1, l: 2d }
1163
+ * await collection.createIndex(['j', ['k', -1], { l: '2d' }])
1141
1164
  */
1142
1165
  Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
1143
1166
  if (typeof options === 'function') (callback = options), (options = {});
@@ -1153,16 +1176,43 @@ Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
1153
1176
  return executeOperation(this.s.topology, createIndexOperation, callback);
1154
1177
  };
1155
1178
 
1179
+ /**
1180
+ * @typedef {object} Collection~IndexDefinition
1181
+ * @description A definition for an index. Used by the createIndex command.
1182
+ * @see https://docs.mongodb.com/manual/reference/command/createIndexes/
1183
+ */
1184
+
1156
1185
  /**
1157
1186
  * Creates multiple indexes in the collection, this method is only supported for
1158
1187
  * MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported
1159
- * error. Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
1188
+ * error.
1189
+ *
1190
+ * **Note**: Unlike {@link Collection#createIndex createIndex}, this function takes in raw index specifications.
1191
+ * Index specifications are defined {@link http://docs.mongodb.org/manual/reference/command/createIndexes/ here}.
1192
+ *
1160
1193
  * @method
1161
- * @param {array} indexSpecs An array of index specifications to be created
1194
+ * @param {Collection~IndexDefinition[]} indexSpecs An array of index specifications to be created
1162
1195
  * @param {Object} [options] Optional settings
1163
1196
  * @param {ClientSession} [options.session] optional session to use for this operation
1164
1197
  * @param {Collection~resultCallback} [callback] The command result callback
1165
1198
  * @return {Promise} returns Promise if no callback passed
1199
+ * @example
1200
+ * const collection = client.db('foo').collection('bar');
1201
+ * await collection.createIndexes([
1202
+ * // Simple index on field fizz
1203
+ * {
1204
+ * key: { fizz: 1 },
1205
+ * }
1206
+ * // wildcard index
1207
+ * {
1208
+ * key: { '$**': 1 }
1209
+ * },
1210
+ * // named index on darmok and jalad
1211
+ * {
1212
+ * key: { darmok: 1, jalad: -1 }
1213
+ * name: 'tanagra'
1214
+ * }
1215
+ * ]);
1166
1216
  */
1167
1217
  Collection.prototype.createIndexes = function(indexSpecs, options, callback) {
1168
1218
  if (typeof options === 'function') (callback = options), (options = {});
@@ -1256,7 +1306,7 @@ Collection.prototype.reIndex = function(options, callback) {
1256
1306
  *
1257
1307
  * @method
1258
1308
  * @param {object} [options] Optional settings.
1259
- * @param {number} [options.batchSize] The batchSize for the returned command cursor or if pre 2.8 the systems batch collection
1309
+ * @param {number} [options.batchSize=1000] The batchSize for the returned command cursor or if pre 2.8 the systems batch collection
1260
1310
  * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
1261
1311
  * @param {ClientSession} [options.session] optional session to use for this operation
1262
1312
  * @return {CommandCursor}
@@ -1355,7 +1405,12 @@ Collection.prototype.indexInformation = function(options, callback) {
1355
1405
  */
1356
1406
 
1357
1407
  /**
1358
- * Count number of matching documents in the db to a query.
1408
+ * An estimated count of matching documents in the db to a query.
1409
+ *
1410
+ * **NOTE:** This method has been deprecated, since it does not provide an accurate count of the documents
1411
+ * in a collection. To obtain an accurate count of documents in the collection, use {@link Collection#countDocuments countDocuments}.
1412
+ * To obtain an estimated count of all documents in the collection, use {@link Collection#estimatedDocumentCount estimatedDocumentCount}.
1413
+ *
1359
1414
  * @method
1360
1415
  * @param {object} [query={}] The query for the count.
1361
1416
  * @param {object} [options] Optional settings.
@@ -1451,7 +1506,7 @@ Collection.prototype.countDocuments = function(query, options, callback) {
1451
1506
  * The distinct command returns a list of distinct values for the given key across a collection.
1452
1507
  * @method
1453
1508
  * @param {string} key Field of the document to find distinct values for.
1454
- * @param {object} query The query for filtering the set of documents to which we apply the distinct filter.
1509
+ * @param {object} [query] The query for filtering the set of documents to which we apply the distinct filter.
1455
1510
  * @param {object} [options] Optional settings.
1456
1511
  * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
1457
1512
  * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
@@ -1510,7 +1565,7 @@ Collection.prototype.stats = function(options, callback) {
1510
1565
  /**
1511
1566
  * @typedef {Object} Collection~findAndModifyWriteOpResult
1512
1567
  * @property {object} value Document returned from the `findAndModify` command. If no documents were found, `value` will be `null` by default (`returnOriginal: true`), even if a document was upserted; if `returnOriginal` was false, the upserted document will be returned in that case.
1513
- * @property {object} lastErrorObject The raw lastErrorObject returned from the command.
1568
+ * @property {object} lastErrorObject The raw lastErrorObject returned from the command. See {@link https://docs.mongodb.com/manual/reference/command/findAndModify/index.html#lasterrorobject|findAndModify command documentation}.
1514
1569
  * @property {Number} ok Is 1 if the command executed correctly.
1515
1570
  */
1516
1571
 
@@ -1716,7 +1771,7 @@ Collection.prototype.findAndRemove = deprecate(function(query, sort, options, ca
1716
1771
  * @param {object} [options] Optional settings.
1717
1772
  * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
1718
1773
  * @param {object} [options.cursor] Return the query as cursor, on 2.6 > it returns as a real cursor on pre 2.6 it returns as an emulated cursor.
1719
- * @param {number} [options.cursor.batchSize] The batchSize for the cursor
1774
+ * @param {number} [options.cursor.batchSize=1000] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
1720
1775
  * @param {boolean} [options.explain=false] Explain returns the aggregation execution plan (requires mongodb 2.6 >).
1721
1776
  * @param {boolean} [options.allowDiskUse=false] allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 >).
1722
1777
  * @param {number} [options.maxTimeMS] maxTimeMS specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point.
@@ -1792,7 +1847,7 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
1792
1847
  * @param {string} [options.fullDocument='default'] Allowed values: ‘default’, ‘updateLookup’. When set to ‘updateLookup’, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
1793
1848
  * @param {object} [options.resumeAfter] Specifies the logical starting point for the new change stream. This should be the _id field from a previously returned change stream document.
1794
1849
  * @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query
1795
- * @param {number} [options.batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
1850
+ * @param {number} [options.batchSize=1000] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
1796
1851
  * @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
1797
1852
  * @param {ReadPreference} [options.readPreference] The read preference. Defaults to the read preference of the database or collection. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}.
1798
1853
  * @param {Timestamp} [options.startAtOperationTime] receive change events that occur after the specified timestamp
@@ -1825,7 +1880,7 @@ Collection.prototype.watch = function(pipeline, options) {
1825
1880
  * @method
1826
1881
  * @param {object} [options] Optional settings.
1827
1882
  * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
1828
- * @param {number} [options.batchSize] Set the batchSize for the getMoreCommand when iterating over the query results.
1883
+ * @param {number} [options.batchSize=1000] Set the batchSize for the getMoreCommand when iterating over the query results.
1829
1884
  * @param {number} [options.numCursors=1] The maximum number of parallel command cursors to return (the number of returned cursors will be in the range 1:numCursors)
1830
1885
  * @param {boolean} [options.raw=false] Return all BSON documents as Raw Buffer documents.
1831
1886
  * @param {Collection~parallelCollectionScanCallback} [callback] The command result callback
@@ -90,7 +90,7 @@ class CommandCursor extends Cursor {
90
90
  /**
91
91
  * Set the batch size for the cursor.
92
92
  * @method
93
- * @param {number} value The batchSize for the cursor.
93
+ * @param {number} value The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/find/|find command documentation}.
94
94
  * @throws {MongoError}
95
95
  * @return {CommandCursor}
96
96
  */
@@ -15,6 +15,7 @@ const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION
15
15
  let AUTH_PROVIDERS;
16
16
 
17
17
  function connect(options, callback) {
18
+ const ConnectionType = options && options.connectionType ? options.connectionType : Connection;
18
19
  if (AUTH_PROVIDERS == null) {
19
20
  AUTH_PROVIDERS = defaultAuthProviders(options.bson);
20
21
  }
@@ -26,7 +27,7 @@ function connect(options, callback) {
26
27
  return;
27
28
  }
28
29
 
29
- performInitialHandshake(new Connection(socket, options), options, callback);
30
+ performInitialHandshake(new ConnectionType(socket, options), options, callback);
30
31
  });
31
32
 
32
33
  return;
@@ -40,13 +41,13 @@ function connect(options, callback) {
40
41
  return;
41
42
  }
42
43
 
43
- performInitialHandshake(new Connection(ipv4Socket, options), options, callback);
44
+ performInitialHandshake(new ConnectionType(ipv4Socket, options), options, callback);
44
45
  });
45
46
 
46
47
  return;
47
48
  }
48
49
 
49
- performInitialHandshake(new Connection(ipv6Socket, options), options, callback);
50
+ performInitialHandshake(new ConnectionType(ipv6Socket, options), options, callback);
50
51
  });
51
52
  }
52
53
 
@@ -314,11 +315,25 @@ function runCommand(conn, ns, command, options, callback) {
314
315
  numberToReturn: 1
315
316
  });
316
317
 
318
+ const noop = () => {};
319
+ function _callback(err, result) {
320
+ callback(err, result);
321
+ callback = noop;
322
+ }
323
+
317
324
  function errorHandler(err) {
318
325
  conn.resetSocketTimeout();
319
326
  CONNECTION_ERROR_EVENTS.forEach(eventName => conn.removeListener(eventName, errorHandler));
320
327
  conn.removeListener('message', messageHandler);
321
- callback(err, null);
328
+
329
+ if (err == null) {
330
+ err = new MongoError(`runCommand failed for connection to '${conn.address}'`);
331
+ }
332
+
333
+ // ignore all future errors
334
+ conn.on('error', noop);
335
+
336
+ _callback(err, null);
322
337
  }
323
338
 
324
339
  function messageHandler(msg) {
@@ -331,7 +346,7 @@ function runCommand(conn, ns, command, options, callback) {
331
346
  conn.removeListener('message', messageHandler);
332
347
 
333
348
  msg.parse({ promoteValues: true });
334
- callback(null, msg.documents[0]);
349
+ _callback(null, msg.documents[0]);
335
350
  }
336
351
 
337
352
  conn.setSocketTimeout(socketTimeout);
@@ -56,10 +56,13 @@ class Connection extends EventEmitter {
56
56
  /**
57
57
  * Creates a new Connection instance
58
58
  *
59
+ * **NOTE**: Internal class, do not instantiate directly
60
+ *
59
61
  * @param {Socket} socket The socket this connection wraps
60
- * @param {Object} [options] Optional settings
61
- * @param {string} [options.host] The host the socket is connected to
62
- * @param {number} [options.port] The port used for the socket connection
62
+ * @param {Object} options Various settings
63
+ * @param {object} options.bson An implementation of bson serialize and deserialize
64
+ * @param {string} [options.host='localhost'] The host the socket is connected to
65
+ * @param {number} [options.port=27017] The port used for the socket connection
63
66
  * @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
64
67
  * @param {number} [options.keepAliveInitialDelay=300000] Initial delay before TCP keep alive enabled
65
68
  * @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
@@ -67,6 +70,7 @@ class Connection extends EventEmitter {
67
70
  * @param {boolean} [options.promoteLongs] Convert Long values from the db into Numbers if they fit into 53 bits
68
71
  * @param {boolean} [options.promoteValues] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
69
72
  * @param {boolean} [options.promoteBuffers] Promotes Binary BSON values to native Node Buffers.
73
+ * @param {number} [options.maxBsonMessageSize=0x4000000] Largest possible size of a BSON message (for legacy purposes)
70
74
  */
71
75
  constructor(socket, options) {
72
76
  super();
@@ -27,6 +27,7 @@
27
27
  // [uint32 checksum;]
28
28
  // };
29
29
 
30
+ const Buffer = require('safe-buffer').Buffer;
30
31
  const opcodes = require('../wireprotocol/shared').opcodes;
31
32
  const databaseNamespace = require('../wireprotocol/shared').databaseNamespace;
32
33
  const ReadPreference = require('../topologies/read_preference');
@@ -90,7 +91,7 @@ class Msg {
90
91
  flags |= OPTS_EXHAUST_ALLOWED;
91
92
  }
92
93
 
93
- const header = new Buffer(
94
+ const header = Buffer.alloc(
94
95
  4 * 4 + // Header
95
96
  4 // Flags
96
97
  );
@@ -110,7 +111,7 @@ class Msg {
110
111
  }
111
112
 
112
113
  makeDocumentSegment(buffers, document) {
113
- const payloadTypeBuffer = new Buffer(1);
114
+ const payloadTypeBuffer = Buffer.alloc(1);
114
115
  payloadTypeBuffer[0] = 0;
115
116
 
116
117
  const documentBuffer = this.serializeBson(document);