@@ -1,3 +1,28 @@ | |||
3.13.1 / 2019-04-05 | |||
------------------- | |||
- Fix possible code execution in (already unsafe) `.load()`, #480. | |||
3.13.0 / 2019-03-20 | |||
------------------- | |||
- Security fix: `safeLoad()` can hang when arrays with nested refs | |||
used as key. Now throws exception for nested arrays. #475. | |||
3.12.2 / 2019-02-26 | |||
------------------- | |||
- Fix `noArrayIndent` option for root level, #468. | |||
3.12.1 / 2019-01-05 | |||
------------------- | |||
- Added `noArrayIndent` option, #432. | |||
3.12.0 / 2018-06-02 | |||
------------------- | |||
@@ -104,7 +104,7 @@ options: | |||
- `filename` _(default: null)_ - string to be used as a file path in | |||
error/warning messages. | |||
- `onWarning` _(default: null)_ - function to call on warning messages. | |||
Loader will throw on warnings if this function is not provided. | |||
Loader will call this function with an instance of `YAMLException` for each warning. | |||
- `schema` _(default: `DEFAULT_SAFE_SCHEMA`)_ - specifies a schema to use. | |||
- `FAILSAFE_SCHEMA` - only strings, arrays and plain objects: | |||
http://www.yaml.org/spec/1.2/spec.html#id2802346 | |||
@@ -170,6 +170,7 @@ disable exceptions by setting the `skipInvalid` option to `true`. | |||
options: | |||
- `indent` _(default: 2)_ - indentation width to use (in spaces). | |||
- `noArrayIndent` _(default: false)_ - when true, will not add an indentation level to array elements | |||
- `skipInvalid` _(default: false)_ - do not throw on invalid types (like function | |||
in the safe schema) and skip pairs and single values with such types. | |||
- `flowLevel` (default: -1) - specifies level of nesting, when to switch from |
@@ -1,4 +1,4 @@ | |||
/* js-yaml 3.12.0 https://github.com/nodeca/js-yaml */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsyaml = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | |||
/* js-yaml 3.13.1 https://github.com/nodeca/js-yaml */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsyaml = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | |||
'use strict'; | |||
@@ -208,16 +208,17 @@ function encodeHex(character) { | |||
} | |||
function State(options) { | |||
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA; | |||
this.indent = Math.max(1, (options['indent'] || 2)); | |||
this.skipInvalid = options['skipInvalid'] || false; | |||
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']); | |||
this.styleMap = compileStyleMap(this.schema, options['styles'] || null); | |||
this.sortKeys = options['sortKeys'] || false; | |||
this.lineWidth = options['lineWidth'] || 80; | |||
this.noRefs = options['noRefs'] || false; | |||
this.noCompatMode = options['noCompatMode'] || false; | |||
this.condenseFlow = options['condenseFlow'] || false; | |||
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA; | |||
this.indent = Math.max(1, (options['indent'] || 2)); | |||
this.noArrayIndent = options['noArrayIndent'] || false; | |||
this.skipInvalid = options['skipInvalid'] || false; | |||
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']); | |||
this.styleMap = compileStyleMap(this.schema, options['styles'] || null); | |||
this.sortKeys = options['sortKeys'] || false; | |||
this.lineWidth = options['lineWidth'] || 80; | |||
this.noRefs = options['noRefs'] || false; | |||
this.noCompatMode = options['noCompatMode'] || false; | |||
this.condenseFlow = options['condenseFlow'] || false; | |||
this.implicitTypes = this.schema.compiledImplicit; | |||
this.explicitTypes = this.schema.compiledExplicit; | |||
@@ -837,13 +838,14 @@ function writeNode(state, level, object, block, compact, iskey) { | |||
} | |||
} | |||
} else if (type === '[object Array]') { | |||
var arrayLevel = (state.noArrayIndent && (level > 0)) ? level - 1 : level; | |||
if (block && (state.dump.length !== 0)) { | |||
writeBlockSequence(state, level, state.dump, compact); | |||
writeBlockSequence(state, arrayLevel, state.dump, compact); | |||
if (duplicate) { | |||
state.dump = '&ref_' + duplicateIndex + state.dump; | |||
} | |||
} else { | |||
writeFlowSequence(state, level, state.dump); | |||
writeFlowSequence(state, arrayLevel, state.dump); | |||
if (duplicate) { | |||
state.dump = '&ref_' + duplicateIndex + ' ' + state.dump; | |||
} | |||
@@ -1005,6 +1007,8 @@ var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i; | |||
var PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i; | |||
function _class(obj) { return Object.prototype.toString.call(obj); } | |||
function is_EOL(c) { | |||
return (c === 0x0A/* LF */) || (c === 0x0D/* CR */); | |||
} | |||
@@ -1260,6 +1264,31 @@ function mergeMappings(state, destination, source, overridableKeys) { | |||
function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, startLine, startPos) { | |||
var index, quantity; | |||
// The output is a plain object here, so keys can only be strings. | |||
// We need to convert keyNode to a string, but doing so can hang the process | |||
// (deeply nested arrays that explode exponentially using aliases). | |||
if (Array.isArray(keyNode)) { | |||
keyNode = Array.prototype.slice.call(keyNode); | |||
for (index = 0, quantity = keyNode.length; index < quantity; index += 1) { | |||
if (Array.isArray(keyNode[index])) { | |||
throwError(state, 'nested arrays are not supported inside keys'); | |||
} | |||
if (typeof keyNode === 'object' && _class(keyNode[index]) === '[object Object]') { | |||
keyNode[index] = '[object Object]'; | |||
} | |||
} | |||
} | |||
// Avoid code execution in load() via toString property | |||
// (still use its own toString for arrays, timestamps, | |||
// and whatever user schema extensions happen to have @@toStringTag) | |||
if (typeof keyNode === 'object' && _class(keyNode) === '[object Object]') { | |||
keyNode = '[object Object]'; | |||
} | |||
keyNode = String(keyNode); | |||
if (_result === null) { |
@@ -105,16 +105,17 @@ function encodeHex(character) { | |||
} | |||
function State(options) { | |||
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA; | |||
this.indent = Math.max(1, (options['indent'] || 2)); | |||
this.skipInvalid = options['skipInvalid'] || false; | |||
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']); | |||
this.styleMap = compileStyleMap(this.schema, options['styles'] || null); | |||
this.sortKeys = options['sortKeys'] || false; | |||
this.lineWidth = options['lineWidth'] || 80; | |||
this.noRefs = options['noRefs'] || false; | |||
this.noCompatMode = options['noCompatMode'] || false; | |||
this.condenseFlow = options['condenseFlow'] || false; | |||
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA; | |||
this.indent = Math.max(1, (options['indent'] || 2)); | |||
this.noArrayIndent = options['noArrayIndent'] || false; | |||
this.skipInvalid = options['skipInvalid'] || false; | |||
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']); | |||
this.styleMap = compileStyleMap(this.schema, options['styles'] || null); | |||
this.sortKeys = options['sortKeys'] || false; | |||
this.lineWidth = options['lineWidth'] || 80; | |||
this.noRefs = options['noRefs'] || false; | |||
this.noCompatMode = options['noCompatMode'] || false; | |||
this.condenseFlow = options['condenseFlow'] || false; | |||
this.implicitTypes = this.schema.compiledImplicit; | |||
this.explicitTypes = this.schema.compiledExplicit; | |||
@@ -734,13 +735,14 @@ function writeNode(state, level, object, block, compact, iskey) { | |||
} | |||
} | |||
} else if (type === '[object Array]') { | |||
var arrayLevel = (state.noArrayIndent && (level > 0)) ? level - 1 : level; | |||
if (block && (state.dump.length !== 0)) { | |||
writeBlockSequence(state, level, state.dump, compact); | |||
writeBlockSequence(state, arrayLevel, state.dump, compact); | |||
if (duplicate) { | |||
state.dump = '&ref_' + duplicateIndex + state.dump; | |||
} | |||
} else { | |||
writeFlowSequence(state, level, state.dump); | |||
writeFlowSequence(state, arrayLevel, state.dump); | |||
if (duplicate) { | |||
state.dump = '&ref_' + duplicateIndex + ' ' + state.dump; | |||
} |
@@ -30,6 +30,8 @@ var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i; | |||
var PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i; | |||
function _class(obj) { return Object.prototype.toString.call(obj); } | |||
function is_EOL(c) { | |||
return (c === 0x0A/* LF */) || (c === 0x0D/* CR */); | |||
} | |||
@@ -285,6 +287,31 @@ function mergeMappings(state, destination, source, overridableKeys) { | |||
function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, startLine, startPos) { | |||
var index, quantity; | |||
// The output is a plain object here, so keys can only be strings. | |||
// We need to convert keyNode to a string, but doing so can hang the process | |||
// (deeply nested arrays that explode exponentially using aliases). | |||
if (Array.isArray(keyNode)) { | |||
keyNode = Array.prototype.slice.call(keyNode); | |||
for (index = 0, quantity = keyNode.length; index < quantity; index += 1) { | |||
if (Array.isArray(keyNode[index])) { | |||
throwError(state, 'nested arrays are not supported inside keys'); | |||
} | |||
if (typeof keyNode === 'object' && _class(keyNode[index]) === '[object Object]') { | |||
keyNode[index] = '[object Object]'; | |||
} | |||
} | |||
} | |||
// Avoid code execution in load() via toString property | |||
// (still use its own toString for arrays, timestamps, | |||
// and whatever user schema extensions happen to have @@toStringTag) | |||
if (typeof keyNode === 'object' && _class(keyNode) === '[object Object]') { | |||
keyNode = '[object Object]'; | |||
} | |||
keyNode = String(keyNode); | |||
if (_result === null) { |
@@ -1,27 +1,27 @@ | |||
{ | |||
"_from": "js-yaml@^3.12.0", | |||
"_id": "js-yaml@3.12.0", | |||
"_from": "js-yaml@3.13.1", | |||
"_id": "js-yaml@3.13.1", | |||
"_inBundle": false, | |||
"_integrity": "sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A==", | |||
"_integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", | |||
"_location": "/js-yaml", | |||
"_phantomChildren": {}, | |||
"_requested": { | |||
"type": "range", | |||
"type": "version", | |||
"registry": true, | |||
"raw": "js-yaml@^3.12.0", | |||
"raw": "js-yaml@3.13.1", | |||
"name": "js-yaml", | |||
"escapedName": "js-yaml", | |||
"rawSpec": "^3.12.0", | |||
"rawSpec": "3.13.1", | |||
"saveSpec": null, | |||
"fetchSpec": "^3.12.0" | |||
"fetchSpec": "3.13.1" | |||
}, | |||
"_requiredBy": [ | |||
"/eslint" | |||
], | |||
"_resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.0.tgz", | |||
"_shasum": "eaed656ec8344f10f527c6bfa1b6e2244de167d1", | |||
"_spec": "js-yaml@^3.12.0", | |||
"_where": "/home/erik/Documents/workspace_brackets/BME_Project_Ohm/om/node_modules/eslint", | |||
"_resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", | |||
"_shasum": "aff151b30bfdfa8e49e05da22e7415e9dfa37847", | |||
"_spec": "js-yaml@3.13.1", | |||
"_where": "/Users/xenia/Desktop/B_ME5/Projekt/om/node_modules/eslint", | |||
"author": { | |||
"name": "Vladimir Zapparov", | |||
"email": "dervus.grim@gmail.com" | |||
@@ -89,5 +89,5 @@ | |||
"scripts": { | |||
"test": "make test" | |||
}, | |||
"version": "3.12.0" | |||
"version": "3.13.1" | |||
} |
@@ -1,3 +1,64 @@ | |||
5.4.10 / 2019-02-05 | |||
=================== | |||
* docs: add search bar and /search page #6706 | |||
* fix: support dotted aliases #7478 [chrischen](https://github.com/chrischen) | |||
* fix(document): copy atomics when setting document array to an existing document array #7472 | |||
* chore: upgrade to mongodb driver 3.1.13 #7488 | |||
* docs: remove confusing references to executing a query "immediately" #7461 | |||
* docs(guides+schematypes): link to custom schematypes docs #7407 | |||
5.4.9 / 2019-02-01 | |||
================== | |||
* fix(document): make `remove()`, `updateOne()`, and `update()` use the document's associated session #7455 | |||
* fix(document): support passing args to hooked custom methods #7456 | |||
* fix(document): avoid double calling single nested getters on `toObject()` #7442 | |||
* fix(discriminator): handle global plugins modifying top-level discriminator options with applyPluginsToDiscriminators: true #7458 | |||
* docs(documents): improve explanation of documents and use more modern syntax #7463 | |||
* docs(middleware+api): fix a couple typos in examples #7474 [arniu](https://github.com/arniu) | |||
5.4.8 / 2019-01-30 | |||
================== | |||
* fix(query): fix unhandled error when casting object in array filters #7431 | |||
* fix(query): cast query $elemMatch to discriminator schema if discriminator key set #7449 | |||
* docs: add table of contents to all guides #7430 | |||
5.4.7 / 2019-01-26 | |||
================== | |||
* fix(populate): set `populated()` when using virtual populate #7440 | |||
* fix(discriminator): defer applying plugins to embedded discriminators until model compilation so global plugins work #7435 | |||
* fix(schema): report correct pathtype underneath map so setting dotted paths underneath maps works #7448 | |||
* fix: get debug from options using the get helper #7451 #7446 [LucGranato](https://github.com/LucGranato) | |||
* fix: use correct variable name #7443 [esben-semmle](https://github.com/esben-semmle) | |||
* docs: fix broken QueryCursor link #7438 [shihabmridha](https://github.com/shihabmridha) | |||
5.4.6 / 2019-01-22 | |||
================== | |||
* fix(utils): make minimize leave empty objects in arrays instead of setting the array element to undefined #7322 | |||
* fix(document): support passing `{document, query}` options to Schema#pre(regex) and Schema#post(regex) #7423 | |||
* docs: add migrating to 5 guide to docs #7434 | |||
* docs(deprecations): add instructions for fixing `count()` deprecation #7419 | |||
* docs(middleware): add description and example for aggregate hooks #7402 | |||
4.13.18 / 2019-01-21 | |||
==================== | |||
* fix(model): handle setting populated path set via `Document#populate()` #7302 | |||
* fix(cast): backport fix from #7290 to 4.x | |||
5.4.5 / 2019-01-18 | |||
================== | |||
* fix(populate): handle nested array `foreignField` with virtual populate #7374 | |||
* fix(query): support not passing any arguments to `orFail()` #7409 | |||
* docs(query): document what the resolved value for `deleteOne()`, `deleteMany()`, and `remove()` contains #7324 | |||
* fix(array): allow opting out of converting non-arrays into arrays with `castNonArrays` option #7371 | |||
* fix(query): ensure updateOne() doesnt unintentionally double call Schema#post(regexp) #7418 | |||
5.4.4 / 2019-01-14 | |||
================== | |||
* fix(query): run casting on arrayFilters option #7079 | |||
* fix(document): support skipping timestamps on save() with `save({ timestamps: false })` #7357 | |||
* fix(model): apply custom where on `Document#remove()` so we attach the shardKey #7393 | |||
* docs(mongoose): document `mongoose.connections` #7338 | |||
5.4.3 / 2019-01-09 | |||
================== | |||
* fix(populate): handle `count` option when using `Document#populate()` on a virtual #7380 |
@@ -12,7 +12,7 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed | |||
[mongoosejs.com](http://mongoosejs.com/) | |||
[Mongoose 5.0.0](https://github.com/Automattic/mongoose/blob/master/History.md#500--2018-01-17) was released on January 17, 2018. You can find more details on backwards breaking changes in 5.0.0 on [GitHub](https://github.com/Automattic/mongoose/blob/master/migrating_to_5.md). | |||
[Mongoose 5.0.0](https://github.com/Automattic/mongoose/blob/master/History.md#500--2018-01-17) was released on January 17, 2018. You can find more details on [backwards breaking changes in 5.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_5.html). | |||
## Support | |||
@@ -570,7 +570,15 @@ function init(self, obj, doc, prefix) { | |||
Document.prototype.update = function update() { | |||
const args = utils.args(arguments); | |||
args.unshift({_id: this._id}); | |||
return this.constructor.update.apply(this.constructor, args); | |||
const query = this.constructor.update.apply(this.constructor, args); | |||
if (this.$session() != null) { | |||
if (!('session' in query.options)) { | |||
query.options.session = this.$session(); | |||
} | |||
} | |||
return query; | |||
}; | |||
/** | |||
@@ -603,6 +611,12 @@ Document.prototype.updateOne = function updateOne(doc, options, callback) { | |||
this.constructor._middleware.execPost('updateOne', this, [], {}, cb); | |||
}); | |||
if (this.$session() != null) { | |||
if (!('session' in query.options)) { | |||
query.options.session = this.$session(); | |||
} | |||
} | |||
if (callback != null) { | |||
return query.exec(callback); | |||
} | |||
@@ -1502,11 +1516,11 @@ Document.prototype.$isDefault = function(path) { | |||
* | |||
* ####Example: | |||
* product.remove(function (err, product) { | |||
* product.isDeleted(); // true | |||
* product.$isDeleted(); // true | |||
* product.remove(); // no-op, doesn't send anything to the db | |||
* | |||
* product.isDeleted(false); | |||
* product.isDeleted(); // false | |||
* product.$isDeleted(false); | |||
* product.$isDeleted(); // false | |||
* product.remove(); // will execute a remove against the db | |||
* }) | |||
* | |||
@@ -2791,7 +2805,13 @@ function applyGetters(self, json, type, options) { | |||
part = parts[ii]; | |||
v = cur[part]; | |||
if (ii === last) { | |||
branch[part] = clone(self.get(path), options); | |||
const val = self.get(path); | |||
// Ignore single nested docs: getters will run because of `clone()` | |||
// before `applyGetters()` in `$toObject()`. Quirk because single | |||
// nested subdocs are hydrated docs in `_doc` as opposed to POJOs. | |||
if (val != null && val.$__ == null) { | |||
branch[part] = clone(val, options); | |||
} | |||
} else if (v == null) { | |||
if (part in cur) { | |||
branch[part] = v; | |||
@@ -3104,43 +3124,41 @@ Document.prototype.depopulate = function(path) { | |||
if (typeof path === 'string') { | |||
path = path.split(' '); | |||
} | |||
let i; | |||
let populatedIds; | |||
const virtualKeys = this.$$populatedVirtuals ? Object.keys(this.$$populatedVirtuals) : []; | |||
const populated = get(this, '$__.populated', {}); | |||
if (arguments.length === 0) { | |||
// Depopulate all | |||
const keys = this.$__.populated ? Object.keys(this.$__.populated) : []; | |||
for (i = 0; i < virtualKeys.length; i++) { | |||
for (let i = 0; i < virtualKeys.length; i++) { | |||
delete this.$$populatedVirtuals[virtualKeys[i]]; | |||
delete this._doc[virtualKeys[i]]; | |||
delete populated[virtualKeys[i]]; | |||
} | |||
for (i = 0; i < keys.length; i++) { | |||
const keys = Object.keys(populated); | |||
for (let i = 0; i < keys.length; i++) { | |||
populatedIds = this.populated(keys[i]); | |||
if (!populatedIds) { | |||
continue; | |||
} | |||
delete this.$__.populated[keys[i]]; | |||
delete populated[keys[i]]; | |||
this.$set(keys[i], populatedIds); | |||
} | |||
return this; | |||
} | |||
for (i = 0; i < path.length; i++) { | |||
for (let i = 0; i < path.length; i++) { | |||
populatedIds = this.populated(path[i]); | |||
if (!populatedIds) { | |||
if (virtualKeys.indexOf(path[i]) === -1) { | |||
continue; | |||
} else { | |||
delete this.$$populatedVirtuals[path[i]]; | |||
delete this._doc[path[i]]; | |||
continue; | |||
} | |||
delete populated[path[i]]; | |||
if (virtualKeys.indexOf(path[i]) !== -1) { | |||
delete this.$$populatedVirtuals[path[i]]; | |||
delete this._doc[path[i]]; | |||
} else { | |||
this.$set(path[i], populatedIds); | |||
} | |||
delete this.$__.populated[path[i]]; | |||
this.$set(path[i], populatedIds); | |||
} | |||
return this; | |||
}; |
@@ -117,7 +117,7 @@ function iter(i) { | |||
const collection = this.collection; | |||
const args = arguments; | |||
const _this = this; | |||
const debug = _this.conn.base.options.debug; | |||
const debug = get(_this, 'conn.base.options.debug'); | |||
// If user force closed, queueing will hang forever. See #5664 | |||
if (this.opts.$wasForceClosed) { |
@@ -13,15 +13,15 @@ const util = require('util'); | |||
* @inherits MongooseError | |||
*/ | |||
function DocumentNotFoundError(query) { | |||
function DocumentNotFoundError(filter) { | |||
let msg; | |||
const messages = MongooseError.messages; | |||
if (messages.DocumentNotFoundError != null) { | |||
msg = typeof messages.DocumentNotFoundError === 'function' ? | |||
messages.DocumentNotFoundError(query) : | |||
messages.DocumentNotFoundError(filter) : | |||
messages.DocumentNotFoundError; | |||
} else { | |||
msg = 'No document found for query "' + util.inspect(query) + '"'; | |||
msg = 'No document found for query "' + util.inspect(filter) + '"'; | |||
} | |||
MongooseError.call(this, msg); | |||
@@ -33,7 +33,9 @@ function DocumentNotFoundError(query) { | |||
this.stack = new Error().stack; | |||
} | |||
this.query = query; | |||
this.filter = filter; | |||
// Backwards compat | |||
this.query = filter; | |||
} | |||
/*! |
@@ -109,10 +109,10 @@ function applyHooks(model, schema, options) { | |||
objToDecorate[method] = function() { | |||
const args = Array.prototype.slice.call(arguments); | |||
const cb = utils.last(args); | |||
const argsWithoutCallback = cb == null ? args : | |||
args.slice(0, args.length - 1); | |||
const argsWithoutCallback = typeof cb === 'function' ? | |||
args.slice(0, args.length - 1) : args; | |||
return utils.promiseOrCallback(cb, callback => { | |||
this[`$__${method}`].apply(this, | |||
return this[`$__${method}`].apply(this, | |||
argsWithoutCallback.concat([callback])); | |||
}, model.events); | |||
}; |
@@ -1,7 +1,7 @@ | |||
'use strict'; | |||
const defineKey = require('../document/compile').defineKey; | |||
const get = require('../../helpers/get'); | |||
const get = require('../get'); | |||
const utils = require('../../utils'); | |||
const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = { | |||
@@ -15,23 +15,27 @@ const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = { | |||
* ignore | |||
*/ | |||
module.exports = function discriminator(model, name, schema, tiedValue) { | |||
module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins) { | |||
if (!(schema && schema.instanceOfSchema)) { | |||
throw new Error('You must pass a valid discriminator Schema'); | |||
} | |||
const applyPluginsToDiscriminators = get(model.base, | |||
'options.applyPluginsToDiscriminators', false); | |||
// Even if `applyPluginsToDiscriminators` isn't set, we should still apply | |||
// global plugins to schemas embedded in the discriminator schema (gh-7370) | |||
model.base._applyPlugins(schema, { skipTopLevel: !applyPluginsToDiscriminators }); | |||
if (model.schema.discriminatorMapping && | |||
!model.schema.discriminatorMapping.isRoot) { | |||
throw new Error('Discriminator "' + name + | |||
'" can only be a discriminator of the root model'); | |||
} | |||
if (applyPlugins) { | |||
const applyPluginsToDiscriminators = get(model.base, | |||
'options.applyPluginsToDiscriminators', false); | |||
// Even if `applyPluginsToDiscriminators` isn't set, we should still apply | |||
// global plugins to schemas embedded in the discriminator schema (gh-7370) | |||
model.base._applyPlugins(schema, { | |||
skipTopLevel: !applyPluginsToDiscriminators | |||
}); | |||
} | |||
const key = model.schema.options.discriminatorKey; | |||
const baseSchemaAddition = {}; |
@@ -0,0 +1,55 @@ | |||
'use strict'; | |||
module.exports = function castFilterPath(query, schematype, val) { | |||
const ctx = query; | |||
const any$conditionals = Object.keys(val).some(function(k) { | |||
return k.charAt(0) === '$' && k !== '$id' && k !== '$ref'; | |||
}); | |||
if (!any$conditionals) { | |||
return schematype.castForQueryWrapper({ | |||
val: val, | |||
context: ctx | |||
}); | |||
} | |||
const ks = Object.keys(val); | |||
let k = ks.length; | |||
while (k--) { | |||
const $cond = ks[k]; | |||
const nested = val[$cond]; | |||
if ($cond === '$not') { | |||
if (nested && schematype && !schematype.caster) { | |||
const _keys = Object.keys(nested); | |||
if (_keys.length && _keys[0].charAt(0) === '$') { | |||
for (const key in nested) { | |||
nested[key] = schematype.castForQueryWrapper({ | |||
$conditional: key, | |||
val: nested[key], | |||
context: ctx | |||
}); | |||
} | |||
} else { | |||
val[$cond] = schematype.castForQueryWrapper({ | |||
$conditional: $cond, | |||
val: nested, | |||
context: ctx | |||
}); | |||
} | |||
continue; | |||
} | |||
// cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context); | |||
} else { | |||
val[$cond] = schematype.castForQueryWrapper({ | |||
$conditional: $cond, | |||
val: nested, | |||
context: ctx | |||
}); | |||
} | |||
} | |||
return val; | |||
}; |
@@ -0,0 +1,62 @@ | |||
'use strict'; | |||
const castFilterPath = require('../query/castFilterPath'); | |||
const modifiedPaths = require('./modifiedPaths'); | |||
module.exports = function castArrayFilters(query) { | |||
const arrayFilters = query.options.arrayFilters; | |||
if (!Array.isArray(arrayFilters)) { | |||
return; | |||
} | |||
const update = query.getUpdate(); | |||
const schema = query.schema; | |||
const updatedPaths = modifiedPaths(update); | |||
const updatedPathsByFilter = Object.keys(updatedPaths).reduce((cur, path) => { | |||
const matches = path.match(/\$\[[^\]]+\]/g); | |||
if (matches == null) { | |||
return cur; | |||
} | |||
for (const match of matches) { | |||
const firstMatch = path.indexOf(match); | |||
if (firstMatch !== path.lastIndexOf(match)) { | |||
throw new Error(`Path '${path}' contains the same array filter multiple times`); | |||
} | |||
cur[match.substring(2, match.length - 1)] = path.substr(0, firstMatch - 1); | |||
} | |||
return cur; | |||
}, {}); | |||
for (const filter of arrayFilters) { | |||
if (filter == null) { | |||
throw new Error(`Got null array filter in ${arrayFilters}`); | |||
} | |||
const firstKey = Object.keys(filter)[0]; | |||
if (filter[firstKey] == null) { | |||
continue; | |||
} | |||
const dot = firstKey.indexOf('.'); | |||
let filterPath = dot === -1 ? | |||
updatedPathsByFilter[firstKey] + '.0' : | |||
updatedPathsByFilter[firstKey.substr(0, dot)] + '.0' + firstKey.substr(dot); | |||
if (filterPath == null) { | |||
throw new Error(`Filter path not found for ${firstKey}`); | |||
} | |||
// If there are multiple array filters in the path being updated, make sure | |||
// to replace them so we can get the schema path. | |||
filterPath = filterPath.replace(/\$\[[^\]]+\]/g, '0'); | |||
const schematype = schema.path(filterPath); | |||
if (typeof filter[firstKey] === 'object') { | |||
filter[firstKey] = castFilterPath(query, schematype, filter[firstKey]); | |||
} else { | |||
filter[firstKey] = schematype.castForQuery(filter[firstKey]); | |||
} | |||
} | |||
}; |
@@ -19,6 +19,7 @@ const Types = require('./types'); | |||
const Query = require('./query'); | |||
const Model = require('./model'); | |||
const Document = require('./document'); | |||
const get = require('./helpers/get'); | |||
const legacyPluralize = require('mongoose-legacy-pluralize'); | |||
const utils = require('./utils'); | |||
const pkg = require('../package.json'); | |||
@@ -555,17 +556,33 @@ Mongoose.prototype._applyPlugins = function(schema, options) { | |||
if (schema.$globalPluginsApplied) { | |||
return; | |||
} | |||
let i; | |||
let len; | |||
schema.$globalPluginsApplied = true; | |||
if (!options || !options.skipTopLevel) { | |||
for (i = 0, len = this.plugins.length; i < len; ++i) { | |||
for (let i = 0; i < this.plugins.length; ++i) { | |||
schema.plugin(this.plugins[i][0], this.plugins[i][1]); | |||
} | |||
schema.$globalPluginsApplied = true; | |||
} | |||
for (i = 0, len = schema.childSchemas.length; i < len; ++i) { | |||
for (let i = 0; i < schema.childSchemas.length; ++i) { | |||
this._applyPlugins(schema.childSchemas[i].schema); | |||
} | |||
const discriminators = schema.discriminators; | |||
if (discriminators == null) { | |||
return; | |||
} | |||
const applyPluginsToDiscriminators = get(this, | |||
'options.applyPluginsToDiscriminators', false); | |||
const keys = Object.keys(discriminators); | |||
for (let i = 0; i < keys.length; ++i) { | |||
const discriminatorKey = keys[i]; | |||
const discriminatorSchema = discriminators[discriminatorKey]; | |||
this._applyPlugins(discriminatorSchema, { skipTopLevel: !applyPluginsToDiscriminators }); | |||
} | |||
}; | |||
/** | |||
@@ -586,7 +603,7 @@ Mongoose.prototype.plugin = function(fn, opts) { | |||
}; | |||
/** | |||
* The default connection of the mongoose module. | |||
* The Mongoose module's default connection. Equivalent to `mongoose.connections][0]`, see [`connections`](#mongoose_Mongoose-connections). | |||
* | |||
* ####Example: | |||
* | |||
@@ -596,10 +613,11 @@ Mongoose.prototype.plugin = function(fn, opts) { | |||
* | |||
* This is the connection used by default for every model created using [mongoose.model](#index_Mongoose-model). | |||
* | |||
* To create a new connection, use [`createConnection()`](#mongoose_Mongoose-createConnection). | |||
* | |||
* @memberOf Mongoose | |||
* @instance | |||
* @property connection | |||
* @return {Connection} | |||
* @property {Connection} connection | |||
* @api public | |||
*/ | |||
@@ -614,6 +632,29 @@ Mongoose.prototype.__defineSetter__('connection', function(v) { | |||
} | |||
}); | |||
/** | |||
* An array containing all [connections](connections.html) associated with this | |||
* Mongoose instance. By default, there is 1 connection. Calling | |||
* [`createConnection()`](#mongoose_Mongoose-createConnection) adds a connection | |||
* to this array. | |||
* | |||
* ####Example: | |||
* | |||
* const mongoose = require('mongoose'); | |||
* mongoose.connections.length; // 1, just the default connection | |||
* mongoose.connections[0] === mongoose.connection; // true | |||
* | |||
* mongoose.createConnection('mongodb://localhost:27017/test'); | |||
* mongoose.connections.length; // 2 | |||
* | |||
* @memberOf Mongoose | |||
* @instance | |||
* @property {Array} connections | |||
* @api public | |||
*/ | |||
Mongoose.prototype.connections; | |||
/*! | |||
* Driver dependent APIs | |||
*/ |
@@ -171,13 +171,12 @@ Model.prototype.baseModelName; | |||
* MyModel.events.on('error', err => console.log(err.message)); | |||
* | |||
* // Prints a 'CastError' because of the above handler | |||
* await MyModel.findOne({ _id: 'notanid' }).catch({} => {}); | |||
* await MyModel.findOne({ _id: 'notanid' }).catch(noop); | |||
* | |||
* @api public | |||
* @fires error whenever any query or model function errors | |||
* @property events | |||
* @memberOf Model | |||
* @static | |||
* @static events | |||
*/ | |||
Model.events; | |||
@@ -193,15 +192,28 @@ Model.events; | |||
Model._middleware; | |||
/*! | |||
* ignore | |||
*/ | |||
function _applyCustomWhere(doc, where) { | |||
if (doc.$where == null) { | |||
return; | |||
} | |||
const keys = Object.keys(doc.$where); | |||
const len = keys.length; | |||
for (let i = 0; i < len; ++i) { | |||
where[keys[i]] = doc.$where[keys[i]]; | |||
} | |||
} | |||
/*! | |||
* ignore | |||
*/ | |||
Model.prototype.$__handleSave = function(options, callback) { | |||
const _this = this; | |||
let i; | |||
let keys; | |||
let len; | |||
let saveOptions = {}; | |||
if ('safe' in options) { | |||
@@ -285,13 +297,7 @@ Model.prototype.$__handleSave = function(options, callback) { | |||
return; | |||
} | |||
if (this.$where) { | |||
keys = Object.keys(this.$where); | |||
len = keys.length; | |||
for (i = 0; i < len; ++i) { | |||
where[keys[i]] = this.$where[keys[i]]; | |||
} | |||
} | |||
_applyCustomWhere(this, where); | |||
this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, function(err, ret) { | |||
if (err) { | |||
@@ -420,6 +426,7 @@ function generateVersionError(doc, modifiedPaths) { | |||
* @param {Boolean} [options.j] set to true for MongoDB to wait until this `save()` has been [journaled before resolving the returned promise](https://docs.mongodb.com/manual/reference/write-concern/#j-option). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern) | |||
* @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://docs.mongodb.com/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern). | |||
* @param {Boolean} [options.checkKeys=true] the MongoDB driver prevents you from saving keys that start with '$' or contain '.' by default. Set this option to `false` to skip that check. See [restrictions on field names](https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names) | |||
* @param {Boolean} [options.timestamps=true] if `false` and [timestamps](./guide.html#timestamps) are enabled, skip timestamps for this `save()`. | |||
* @param {Function} [fn] optional callback | |||
* @return {Promise|undefined} Returns undefined if used with callback or a Promise otherwise. | |||
* @api public | |||
@@ -458,8 +465,11 @@ Model.prototype.save = function(options, fn) { | |||
return cb(parallelSave); | |||
} | |||
this.$__.saveOptions = options; | |||
this.$__save(options, error => { | |||
this.$__.saving = undefined; | |||
delete this.$__.saveOptions; | |||
if (error) { | |||
this.$__handleReject(error); | |||
@@ -915,6 +925,15 @@ Model.prototype.$__remove = function $__remove(options, cb) { | |||
return cb(where); | |||
} | |||
_applyCustomWhere(this, where); | |||
if (this.$session() != null) { | |||
options = options || {}; | |||
if (!('session' in options)) { | |||
options.session = this.$session(); | |||
} | |||
} | |||
this[modelCollectionSymbol].deleteOne(where, options, err => { | |||
if (!err) { | |||
this.$__.isDeleted = true; | |||
@@ -985,12 +1004,13 @@ Model.discriminator = function(name, schema, value) { | |||
} | |||
} | |||
schema = discriminator(this, name, schema, value); | |||
schema = discriminator(this, name, schema, value, true); | |||
if (this.db.models[name]) { | |||
throw new OverwriteModelError(name); | |||
} | |||
schema.$isRootDiscriminator = true; | |||
schema.$globalPluginsApplied = true; | |||
model = this.db.model(model || name, schema, this.collection.name); | |||
this.discriminators[name] = model; | |||
@@ -1567,15 +1587,32 @@ Model.discriminators; | |||
* @return {Object} the translated 'pure' fields/conditions | |||
*/ | |||
Model.translateAliases = function translateAliases(fields) { | |||
const aliases = this.schema.aliases; | |||
if (typeof fields === 'object') { | |||
// Fields is an object (query conditions or document fields) | |||
for (const key in fields) { | |||
if (aliases[key]) { | |||
fields[aliases[key]] = fields[key]; | |||
delete fields[key]; | |||
let alias; | |||
const translated = []; | |||
const fieldKeys = key.split('.'); | |||
let currentSchema = this.schema; | |||
for (const field in fieldKeys) { | |||
const name = fieldKeys[field]; | |||
if (currentSchema && currentSchema.aliases[name]) { | |||
alias = currentSchema.aliases[name]; | |||
// Alias found, | |||
translated.push(alias); | |||
} else { | |||
// Alias not found, so treat as un-aliased key | |||
translated.push(name); | |||
} | |||
// Check if aliased path is a schema | |||
if (currentSchema.paths[alias]) | |||
currentSchema = currentSchema.paths[alias].schema; | |||
else | |||
currentSchema = null; | |||
} | |||
fields[translated.join('.')] = fields[key]; | |||
delete fields[key]; // We'll be using the translated key instead | |||
} | |||
return fields; | |||
@@ -1705,16 +1742,16 @@ Model.deleteMany = function deleteMany(conditions, options, callback) { | |||
* // named john and at least 18 | |||
* MyModel.find({ name: 'john', age: { $gte: 18 }}); | |||
* | |||
* // executes immediately, passing results to callback | |||
* // executes, passing results to callback | |||
* MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {}); | |||
* | |||
* // name LIKE john and only selecting the "name" and "friends" fields, executing immediately | |||
* // executes, name LIKE john and only selecting the "name" and "friends" fields | |||
* MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { }) | |||
* | |||
* // passing options | |||
* MyModel.find({ name: /john/i }, null, { skip: 10 }) | |||
* | |||
* // passing options and executing immediately | |||
* // passing options and executes | |||
* MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {}); | |||
* | |||
* // executing a query explicitly | |||
@@ -1787,7 +1824,7 @@ Model.find = function find(conditions, projection, options, callback) { | |||
* | |||
* ####Example: | |||
* | |||
* // find adventure by id and execute immediately | |||
* // find adventure by id and execute | |||
* Adventure.findById(id, function (err, adventure) {}); | |||
* | |||
* // same as above | |||
@@ -2013,7 +2050,7 @@ Model.count = function count(conditions, callback) { | |||
/** | |||
* Creates a Query for a `distinct` operation. | |||
* | |||
* Passing a `callback` immediately executes the query. | |||
* Passing a `callback` executes the query. | |||
* | |||
* ####Example | |||
* | |||
@@ -2103,7 +2140,7 @@ Model.$where = function $where() { | |||
/** | |||
* Issues a mongodb findAndModify update command. | |||
* | |||
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes immediately if `callback` is passed else a Query object is returned. | |||
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes if `callback` is passed else a Query object is returned. | |||
* | |||
* ####Options: | |||
* | |||
@@ -2236,8 +2273,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) { | |||
* | |||
* Finds a matching document, updates it according to the `update` arg, | |||
* passing any `options`, and returns the found document (if any) to the | |||
* callback. The query executes immediately if `callback` is passed else a | |||
* Query object is returned. | |||
* callback. The query executes if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -2335,7 +2371,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) { | |||
* Finds a matching document, removes it, and passes the found document | |||
* (if any) to the callback. | |||
* | |||
* Executes immediately if `callback` is passed else a Query object is returned. | |||
* Executes the query if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -2456,7 +2492,7 @@ Model.findByIdAndDelete = function(id, options, callback) { | |||
* Finds a matching document, replaces it with the provided doc, and passes the | |||
* returned doc to the callback. | |||
* | |||
* Executes immediately if `callback` is passed else a Query object is returned. | |||
* Executes the query if `callback` is passed. | |||
* | |||
* This function triggers the following query middleware. | |||
* | |||
@@ -2525,7 +2561,7 @@ Model.findOneAndReplace = function(conditions, options, callback) { | |||
* | |||
* Finds a matching document, removes it, passing the found document (if any) to the callback. | |||
* | |||
* Executes immediately if `callback` is passed else a Query object is returned. | |||
* Executes the query if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -2608,7 +2644,7 @@ Model.findOneAndRemove = function(conditions, options, callback) { | |||
* | |||
* Finds a matching document, removes it, passing the found document (if any) to the callback. | |||
* | |||
* Executes immediately if `callback` is passed, else a `Query` object is returned. | |||
* Executes the query if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -3851,6 +3887,7 @@ function populate(model, docs, options, callback) { | |||
for (const foreignField of mod.foreignField) { | |||
_val = utils.getValue(foreignField, val); | |||
if (Array.isArray(_val)) { | |||
_val = utils.array.flatten(_val); | |||
const _valLength = _val.length; | |||
for (let j = 0; j < _valLength; ++j) { | |||
let __val = _val[j]; | |||
@@ -3937,6 +3974,8 @@ function assignVals(o) { | |||
justOne: o.justOne | |||
}); | |||
const originalIds = [].concat(o.rawIds); | |||
// replace the original ids in our intermediate _ids structure | |||
// with the documents found by query | |||
assignRawDocsToIdStructure(o.rawIds, o.rawDocs, o.rawOrder, populateOptions); | |||
@@ -3978,6 +4017,7 @@ function assignVals(o) { | |||
} | |||
if (o.isVirtual && docs[i] instanceof Model) { | |||
docs[i].populated(o.path, o.justOne ? originalIds[0] : originalIds, o.allOptions); | |||
// If virtual populate and doc is already init-ed, need to walk through | |||
// the actual doc to set rather than setting `_doc` directly | |||
mpath.set(o.path, rawIds[i], docs[i], setValue); |
@@ -16,6 +16,10 @@ module.exports = function shardingPlugin(schema) { | |||
applyWhere.call(this); | |||
next(); | |||
}); | |||
schema.pre('remove', function(next) { | |||
applyWhere.call(this); | |||
next(); | |||
}); | |||
schema.post('save', function() { | |||
storeShard.call(this); | |||
}); |
@@ -5,12 +5,14 @@ | |||
*/ | |||
const CastError = require('./error/cast'); | |||
const DocumentNotFoundError = require('./error/notFound'); | |||
const Kareem = require('kareem'); | |||
const ObjectParameterError = require('./error/objectParameter'); | |||
const QueryCursor = require('./cursor/QueryCursor'); | |||
const ReadPreference = require('./driver').get().ReadPreference; | |||
const applyWriteConcern = require('./helpers/schema/applyWriteConcern'); | |||
const cast = require('./cast'); | |||
const castArrayFilters = require('./helpers/update/castArrayFilters'); | |||
const castUpdate = require('./helpers/query/castUpdate'); | |||
const completeMany = require('./helpers/query/completeMany'); | |||
const get = require('./helpers/get'); | |||
@@ -1728,6 +1730,18 @@ Query.prototype._castConditions = function() { | |||
} | |||
}; | |||
/*! | |||
* ignore | |||
*/ | |||
function _castArrayFilters(query) { | |||
try { | |||
castArrayFilters(query); | |||
} catch (err) { | |||
query.error(err); | |||
} | |||
} | |||
/** | |||
* Thunk around find() | |||
* | |||
@@ -2388,39 +2402,52 @@ Query.prototype.sort = function(arg) { | |||
}; | |||
/** | |||
* Declare and/or execute this query as a remove() operation. | |||
* Declare and/or execute this query as a remove() operation. `remove()` is | |||
* deprecated, you should use [`deleteOne()`](#query_Query-deleteOne) | |||
* or [`deleteMany()`](#query_Query-deleteMany) instead. | |||
* | |||
* This function does not trigger any middleware | |||
* | |||
* ####Example | |||
* | |||
* Model.remove({ artist: 'Anne Murray' }, callback) | |||
* Character.remove({ name: /Stark/ }, callback); | |||
* | |||
* This function calls the MongoDB driver's [`Collection#remove()` function](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#remove). | |||
* The returned [promise](https://mongoosejs.com/docs/queries.html) resolves to an | |||
* object that contains 3 properties: | |||
* | |||
* - `ok`: `1` if no errors occurred | |||
* - `deletedCount`: the number of documents deleted | |||
* - `n`: the number of documents deleted. Equal to `deletedCount`. | |||
* | |||
* ####Example | |||
* | |||
* const res = await Character.remove({ name: /Stark/ }); | |||
* // Number of docs deleted | |||
* res.deletedCount; | |||
* | |||
* ####Note | |||
* | |||
* The operation is only executed when a callback is passed. To force execution without a callback, you must first call `remove()` and then execute it by using the `exec()` method. | |||
* Calling `remove()` creates a [Mongoose query](./queries.html), and a query | |||
* does not execute until you either pass a callback, call [`Query#then()`](#query_Query-then), | |||
* or call [`Query#exec()`](#query_Query-exec). | |||
* | |||
* // not executed | |||
* var query = Model.find().remove({ name: 'Anne Murray' }) | |||
* const query = Character.remove({ name: /Stark/ }); | |||
* | |||
* // executed | |||
* query.remove({ name: 'Anne Murray' }, callback) | |||
* query.remove({ name: 'Anne Murray' }).remove(callback) | |||
* Character.remove({ name: /Stark/ }, callback); | |||
* Character.remove({ name: /Stark/ }).remove(callback); | |||
* | |||
* // executed without a callback | |||
* query.exec() | |||
* | |||
* // summary | |||
* query.remove(conds, fn); // executes | |||
* query.remove(conds) | |||
* query.remove(fn) // executes | |||
* query.remove() | |||
* Character.exec(); | |||
* | |||
* @param {Object|Query} [filter] mongodb selector | |||
* @param {Function} [callback] optional params are (error, writeOpResult) | |||
* @param {Function} [callback] optional params are (error, mongooseDeleteResult) | |||
* @return {Query} this | |||
* @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult | |||
* @see remove http://docs.mongodb.org/manual/reference/method/db.collection.remove/ | |||
* @deprecated | |||
* @see deleteWriteOpResult http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~deleteWriteOpResult | |||
* @see MongoDB driver remove http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#remove | |||
* @api public | |||
*/ | |||
@@ -2462,7 +2489,7 @@ Query.prototype._remove = wrapThunk(function(callback) { | |||
callback = _wrapThunkCallback(this, callback); | |||
return Query.base.remove.call(this, helpers.handleWriteOpResult(callback)); | |||
return Query.base.remove.call(this, helpers.handleDeleteWriteOpResult(callback)); | |||
}); | |||
/** | |||
@@ -2474,14 +2501,28 @@ Query.prototype._remove = wrapThunk(function(callback) { | |||
* | |||
* ####Example | |||
* | |||
* Character.deleteOne({ name: 'Eddard Stark' }, callback) | |||
* Character.deleteOne({ name: 'Eddard Stark' }).then(next) | |||
* Character.deleteOne({ name: 'Eddard Stark' }, callback); | |||
* Character.deleteOne({ name: 'Eddard Stark' }).then(next); | |||
* | |||
* This function calls the MongoDB driver's [`Collection#deleteOne()` function](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteOne). | |||
* The returned [promise](https://mongoosejs.com/docs/queries.html) resolves to an | |||
* object that contains 3 properties: | |||
* | |||
* - `ok`: `1` if no errors occurred | |||
* - `deletedCount`: the number of documents deleted | |||
* - `n`: the number of documents deleted. Equal to `deletedCount`. | |||
* | |||
* ####Example | |||
* | |||
* const res = await Character.deleteOne({ name: 'Eddard Stark' }); | |||
* // `1` if MongoDB deleted a doc, `0` if no docs matched the filter `{ name: ... }` | |||
* res.deletedCount; | |||
* | |||
* @param {Object|Query} [filter] mongodb selector | |||
* @param {Function} [callback] optional params are (error, writeOpResult) | |||
* @param {Function} [callback] optional params are (error, mongooseDeleteResult) | |||
* @return {Query} this | |||
* @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult | |||
* @see remove http://docs.mongodb.org/manual/reference/method/db.collection.remove/ | |||
* @see deleteWriteOpResult http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~deleteWriteOpResult | |||
* @see MongoDB Driver deleteOne http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteOne | |||
* @api public | |||
*/ | |||
@@ -2524,7 +2565,7 @@ Query.prototype._deleteOne = wrapThunk(function(callback) { | |||
callback = _wrapThunkCallback(this, callback); | |||
return Query.base.deleteOne.call(this, helpers.handleWriteOpResult(callback)); | |||
return Query.base.deleteOne.call(this, helpers.handleDeleteWriteOpResult(callback)); | |||
}); | |||
/** | |||
@@ -2539,11 +2580,25 @@ Query.prototype._deleteOne = wrapThunk(function(callback) { | |||
* Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, callback) | |||
* Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }).then(next) | |||
* | |||
* This function calls the MongoDB driver's [`Collection#deleteOne()` function](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteMany). | |||
* The returned [promise](https://mongoosejs.com/docs/queries.html) resolves to an | |||
* object that contains 3 properties: | |||
* | |||
* - `ok`: `1` if no errors occurred | |||
* - `deletedCount`: the number of documents deleted | |||
* - `n`: the number of documents deleted. Equal to `deletedCount`. | |||
* | |||
* ####Example | |||
* | |||
* const res = await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }); | |||
* // `0` if no docs matched the filter, number of docs deleted otherwise | |||
* res.deletedCount; | |||
* | |||
* @param {Object|Query} [filter] mongodb selector | |||
* @param {Function} [callback] optional params are (error, writeOpResult) | |||
* @param {Function} [callback] optional params are (error, mongooseDeleteResult) | |||
* @return {Query} this | |||
* @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult | |||
* @see remove http://docs.mongodb.org/manual/reference/method/db.collection.remove/ | |||
* @see deleteWriteOpResult http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~deleteWriteOpResult | |||
* @see MongoDB Driver deleteMany http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteMany | |||
* @api public | |||
*/ | |||
@@ -2586,7 +2641,7 @@ Query.prototype._deleteMany = wrapThunk(function(callback) { | |||
callback = _wrapThunkCallback(this, callback); | |||
return Query.base.deleteMany.call(this, helpers.handleWriteOpResult(callback)); | |||
return Query.base.deleteMany.call(this, helpers.handleDeleteWriteOpResult(callback)); | |||
}); | |||
/*! | |||
@@ -2647,7 +2702,9 @@ function prepareDiscriminatorCriteria(query) { | |||
/** | |||
* Issues a mongodb [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) update command. | |||
* | |||
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes immediately if `callback` is passed. | |||
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found | |||
* document (if any) to the callback. The query executes if | |||
* `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -2778,7 +2835,8 @@ Query.prototype._findOneAndUpdate = wrapThunk(function(callback) { | |||
/** | |||
* Issues a mongodb [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) remove command. | |||
* | |||
* Finds a matching document, removes it, passing the found document (if any) to the callback. Executes immediately if `callback` is passed. | |||
* Finds a matching document, removes it, passing the found document (if any) to | |||
* the callback. Executes if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -2856,7 +2914,8 @@ Query.prototype.findOneAndRemove = function(conditions, options, callback) { | |||
/** | |||
* Issues a MongoDB [findOneAndDelete](https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndDelete/) command. | |||
* | |||
* Finds a matching document, removes it, and passes the found document (if any) to the callback. Executes immediately if `callback` is passed. | |||
* Finds a matching document, removes it, and passes the found document (if any) | |||
* to the callback. Executes if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -2978,7 +3037,8 @@ Query.prototype._findOneAndDelete = wrapThunk(function(callback) { | |||
/** | |||
* Issues a MongoDB [findOneAndReplace](https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/) command. | |||
* | |||
* Finds a matching document, removes it, and passes the found document (if any) to the callback. Executes immediately if `callback` is passed. | |||
* Finds a matching document, removes it, and passes the found document (if any) | |||
* to the callback. Executes if `callback` is passed. | |||
* | |||
* This function triggers the following middleware. | |||
* | |||
@@ -3147,6 +3207,8 @@ Query.prototype._findAndModify = function(type, callback) { | |||
return callback(castedQuery); | |||
} | |||
_castArrayFilters(this); | |||
const opts = this._optionsForExec(model); | |||
if ('strict' in opts) { | |||
@@ -3385,6 +3447,8 @@ function _updateThunk(op, callback) { | |||
this._castConditions(); | |||
_castArrayFilters(this); | |||
if (this.error() != null) { | |||
callback(this.error()); | |||
return null; | |||
@@ -3886,7 +3950,7 @@ Query.prototype.map = function(fn) { | |||
* @method orFail | |||
* @memberOf Query | |||
* @instance | |||
* @param {Function|Error} [err] optional error to throw if no docs match `filter` | |||
* @param {Function|Error} [err] optional error to throw if no docs match `filter`. If not specified, `orFail()` will throw a `DocumentNotFoundError` | |||
* @return {Query} this | |||
*/ | |||
@@ -3895,42 +3959,36 @@ Query.prototype.orFail = function(err) { | |||
switch (this.op) { | |||
case 'find': | |||
if (res.length === 0) { | |||
err = typeof err === 'function' ? err.call(this) : err; | |||
throw err; | |||
throw _orFailError(err, this); | |||
} | |||
break; | |||
case 'findOne': | |||
if (res == null) { | |||
err = typeof err === 'function' ? err.call(this) : err; | |||
throw err; | |||
throw _orFailError(err, this); | |||
} | |||
break; | |||
case 'update': | |||
case 'updateMany': | |||
case 'updateOne': | |||
if (get(res, 'result.nModified') === 0) { | |||
err = typeof err === 'function' ? err.call(this) : err; | |||
throw err; | |||
throw _orFailError(err, this); | |||
} | |||
break; | |||
case 'findOneAndDelete': | |||
if (get(res, 'lastErrorObject.n') === 0) { | |||
err = typeof err === 'function' ? err.call(this) : err; | |||
throw err; | |||
throw _orFailError(err, this); | |||
} | |||
break; | |||
case 'findOneAndUpdate': | |||
if (get(res, 'lastErrorObject.updatedExisting') === false) { | |||
err = typeof err === 'function' ? err.call(this) : err; | |||
throw err; | |||
throw _orFailError(err, this); | |||
} | |||
break; | |||
case 'deleteMany': | |||
case 'deleteOne': | |||
case 'remove': | |||
if (res.n === 0) { | |||
err = typeof err === 'function' ? err.call(this) : err; | |||
throw err; | |||
throw _orFailError(err, this); | |||
} | |||
break; | |||
default: | |||
@@ -3942,6 +4000,22 @@ Query.prototype.orFail = function(err) { | |||
return this; | |||
}; | |||
/*! | |||
* Get the error to throw for `orFail()` | |||
*/ | |||
function _orFailError(err, query) { | |||
if (typeof err === 'function') { | |||
err = err.call(query); | |||
} | |||
if (err == null) { | |||
err = new DocumentNotFoundError(query.getQuery()); | |||
} | |||
return err; | |||
} | |||
/** | |||
* Executes the query | |||
* |
@@ -296,11 +296,12 @@ function makeLean(val) { | |||
* Handle the `WriteOpResult` from the server | |||
*/ | |||
exports.handleWriteOpResult = function handleWriteOpResult(callback) { | |||
return function _handleWriteOpResult(error, res) { | |||
exports.handleDeleteWriteOpResult = function handleDeleteWriteOpResult(callback) { | |||
return function _handleDeleteWriteOpResult(error, res) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
return callback(null, res.result); | |||
return callback(null, | |||
Object.assign({}, res.result, {deletedCount: res.deletedCount })); | |||
}; | |||
}; |
@@ -23,9 +23,11 @@ const validateRef = require('./helpers/populate/validateRef'); | |||
let MongooseTypes; | |||
const allMiddleware = require('./helpers/query/applyQueryMiddleware'). | |||
middlewareFunctions. | |||
concat(require('./helpers/model/applyHooks').middlewareFunctions); | |||
const queryHooks = require('./helpers/query/applyQueryMiddleware'). | |||
middlewareFunctions; | |||
const documentHooks = require('./helpers/model/applyHooks').middlewareFunctions; | |||
const hookNames = queryHooks.concat(documentHooks). | |||
reduce((s, hook) => s.add(hook), new Set()); | |||
let id = 0; | |||
@@ -851,7 +853,7 @@ Schema.prototype.pathType = function(path) { | |||
} | |||
const re = new RegExp('^' + _path.replace(/\.\$\*/g, '.[^.]+') + '$'); | |||
if (re.test(path)) { | |||
return this.paths[_path]; | |||
return 'real'; | |||
} | |||
} | |||
@@ -918,6 +920,10 @@ Schema.prototype.setupTimestamp = function(timestamps) { | |||
this.add(schemaAdditions); | |||
this.pre('save', function(next) { | |||
if (get(this, '$__.saveOptions.timestamps') === false) { | |||
return next(); | |||
} | |||
const defaultTimestamp = (this.ownerDocument ? this.ownerDocument() : this). | |||
constructor.base.now(); | |||
const auto_id = this._id && this._id.auto; | |||
@@ -975,7 +981,7 @@ Schema.prototype.setupTimestamp = function(timestamps) { | |||
function getPositionalPathType(self, path) { | |||
const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean); | |||
if (subpaths.length < 2) { | |||
return self.paths.hasOwnProperty(subpaths[0]) ? self.paths[subpath[0]] : null; | |||
return self.paths.hasOwnProperty(subpaths[0]) ? self.paths[subpaths[0]] : null; | |||
} | |||
let val = self.path(subpaths[0]); | |||
@@ -985,12 +991,10 @@ function getPositionalPathType(self, path) { | |||
} | |||
const last = subpaths.length - 1; | |||
let subpath; | |||
let i = 1; | |||
for (; i < subpaths.length; ++i) { | |||
for (let i = 1; i < subpaths.length; ++i) { | |||
isNested = false; | |||
subpath = subpaths[i]; | |||
const subpath = subpaths[i]; | |||
if (i === last && val && !/\D/.test(subpath)) { | |||
if (val.$isMongooseDocumentArray) { | |||
@@ -1095,7 +1099,7 @@ Schema.prototype.queue = function(name, args) { | |||
Schema.prototype.pre = function(name) { | |||
if (name instanceof RegExp) { | |||
const remainingArgs = Array.prototype.slice.call(arguments, 1); | |||
for (const fn of allMiddleware) { | |||
for (const fn of hookNames) { | |||
if (name.test(fn)) { | |||
this.pre.apply(this, [fn].concat(remainingArgs)); | |||
} | |||
@@ -1146,7 +1150,7 @@ Schema.prototype.pre = function(name) { | |||
Schema.prototype.post = function(name) { | |||
if (name instanceof RegExp) { | |||
const remainingArgs = Array.prototype.slice.call(arguments, 1); | |||
for (const fn of allMiddleware) { | |||
for (const fn of hookNames) { | |||
if (name.test(fn)) { | |||
this.post.apply(this, [fn].concat(remainingArgs)); | |||
} |
@@ -128,6 +128,17 @@ function SchemaArray(key, cast, options, schemaOptions) { | |||
*/ | |||
SchemaArray.schemaName = 'Array'; | |||
/** | |||
* Options for all arrays. | |||
* | |||
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting. | |||
* | |||
* @static options | |||
* @api public | |||
*/ | |||
SchemaArray.options = { castNonArrays: true }; | |||
/*! | |||
* Inherits from SchemaType. | |||
*/ | |||
@@ -217,12 +228,17 @@ SchemaArray.prototype.cast = function(value, doc, init) { | |||
return value; | |||
} | |||
// gh-2442: if we're loading this from the db and its not an array, mark | |||
// the whole array as modified. | |||
if (!!doc && !!init) { | |||
doc.markModified(this.path); | |||
if (init || SchemaArray.options.castNonArrays) { | |||
// gh-2442: if we're loading this from the db and its not an array, mark | |||
// the whole array as modified. | |||
if (!!doc && !!init) { | |||
doc.markModified(this.path); | |||
} | |||
return this.cast([value], doc, init); | |||
} | |||
return this.cast([value], doc, init); | |||
throw new CastError('Array', util.inspect(value), this.path); | |||
}; | |||
/*! | |||
@@ -336,16 +352,25 @@ function cast$all(val) { | |||
function cast$elemMatch(val) { | |||
const keys = Object.keys(val); | |||
const numKeys = keys.length; | |||
let key; | |||
let value; | |||
for (let i = 0; i < numKeys; ++i) { | |||
key = keys[i]; | |||
value = val[key]; | |||
const key = keys[i]; | |||
const value = val[key]; | |||
if (key.indexOf('$') === 0 && value) { | |||
val[key] = this.castForQuery(key, value); | |||
} | |||
} | |||
// Is this an embedded discriminator and is the discriminator key set? | |||
// If so, use the discriminator schema. See gh-7449 | |||
const discriminatorKey = get(this, | |||
'casterConstructor.schema.options.discriminatorKey'); | |||
const discriminators = get(this, 'casterConstructor.schema.discriminators', {}); | |||
if (discriminatorKey != null && | |||
val[discriminatorKey] != null && | |||
discriminators[val[discriminatorKey]] != null) { | |||
return cast(discriminators[val[discriminatorKey]], val); | |||
} | |||
return cast(this.casterConstructor.schema, val); | |||
} | |||
@@ -61,6 +61,17 @@ function DocumentArray(key, schema, options, schemaOptions) { | |||
*/ | |||
DocumentArray.schemaName = 'DocumentArray'; | |||
/** | |||
* Options for all document arrays. | |||
* | |||
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting. | |||
* | |||
* @static options | |||
* @api public | |||
*/ | |||
DocumentArray.options = { castNonArrays: true }; | |||
/*! | |||
* Inherits from ArrayType. | |||
*/ | |||
@@ -310,6 +321,9 @@ DocumentArray.prototype.cast = function(value, doc, init, prev, options) { | |||
const _opts = { transform: false, virtuals: false }; | |||
if (!Array.isArray(value)) { | |||
if (!init && !DocumentArray.options.castNonArrays) { | |||
throw new CastError('DocumentArray', util.inspect(value), this.path); | |||
} | |||
// gh-2442 mark whole array as modified if we're initializing a doc from | |||
// the db and the path isn't an array in the document | |||
if (!!doc && init) { |
@@ -1249,8 +1249,13 @@ SchemaType.prototype._castForQuery = function(val) { | |||
}; | |||
/** | |||
* Override the function the required validator uses to check whether a string | |||
* passes the `required` check. | |||
* Override the function the required validator uses to check whether a value | |||
* passes the `required` check. Override this on the individual SchemaType. | |||
* | |||
* ####Example: | |||
* | |||
* // Use this to allow empty strings to pass the `required` validator | |||
* mongoose.Schema.Types.String.checkRequired(v => typeof v === 'string'); | |||
* | |||
* @param {Function} fn | |||
* @return {Function} |
@@ -44,12 +44,6 @@ function MongooseDocumentArray(values, path, doc) { | |||
// TODO: replace this with `new CoreMongooseArray().concat()` when we remove | |||
// support for node 4.x and 5.x, see https://i.imgur.com/UAAHk4S.png | |||
const arr = new CoreMongooseArray(); | |||
if (Array.isArray(values)) { | |||
values.forEach(v => { | |||
arr.push(v); | |||
}); | |||
} | |||
arr._path = path; | |||
const props = { | |||
isMongooseDocumentArray: true, | |||
@@ -59,6 +53,18 @@ function MongooseDocumentArray(values, path, doc) { | |||
_handlers: void 0 | |||
}; | |||
if (Array.isArray(values)) { | |||
if (values instanceof CoreMongooseArray && | |||
values._path === path && | |||
values._parent === doc) { | |||
props._atomics = Object.assign({}, values._atomics); | |||
} | |||
values.forEach(v => { | |||
arr.push(v); | |||
}); | |||
} | |||
arr._path = path; | |||
// Values always have to be passed to the constructor to initialize, since | |||
// otherwise MongooseArray#push will mark the array as modified to the parent. | |||
const keysMA = Object.keys(MongooseArray.mixin); |
@@ -176,12 +176,13 @@ exports.last = function(arr) { | |||
* | |||
* @param {Object} obj the object to clone | |||
* @param {Object} options | |||
* @param {Boolean} isArrayChild true if cloning immediately underneath an array. Special case for minimize. | |||
* @return {Object} the cloned object | |||
* @api private | |||
*/ | |||
exports.clone = function clone(obj, options) { | |||
if (obj === undefined || obj === null) { | |||
exports.clone = function clone(obj, options, isArrayChild) { | |||
if (obj == null) { | |||
return obj; | |||
} | |||
@@ -199,7 +200,7 @@ exports.clone = function clone(obj, options) { | |||
if (obj.constructor) { | |||
switch (exports.getFunctionName(obj.constructor)) { | |||
case 'Object': | |||
return cloneObject(obj, options); | |||
return cloneObject(obj, options, isArrayChild); | |||
case 'Date': | |||
return new obj.constructor(+obj); | |||
case 'RegExp': | |||
@@ -222,12 +223,14 @@ exports.clone = function clone(obj, options) { | |||
if (!obj.constructor && exports.isObject(obj)) { | |||
// object created with Object.create(null) | |||
return cloneObject(obj, options); | |||
return cloneObject(obj, options, isArrayChild); | |||
} | |||
if (obj.valueOf) { | |||
return obj.valueOf(); | |||
} | |||
return cloneObject(obj, options, isArrayChild); | |||
}; | |||
const clone = exports.clone; | |||
@@ -279,19 +282,18 @@ exports.promiseOrCallback = function promiseOrCallback(callback, fn, ee) { | |||
* ignore | |||
*/ | |||
function cloneObject(obj, options) { | |||
function cloneObject(obj, options, isArrayChild) { | |||
const minimize = options && options.minimize; | |||
const ret = {}; | |||
let hasKeys; | |||
let val; | |||
let k; | |||
for (k in obj) { | |||
for (const k in obj) { | |||
if (specialProperties.has(k)) { | |||
continue; | |||
} | |||
val = clone(obj[k], options); | |||
// Don't pass `isArrayChild` down | |||
const val = clone(obj[k], options); | |||
if (!minimize || (typeof val !== 'undefined')) { | |||
hasKeys || (hasKeys = true); | |||
@@ -299,13 +301,13 @@ function cloneObject(obj, options) { | |||
} | |||
} | |||
return minimize ? hasKeys && ret : ret; | |||
return minimize && !isArrayChild ? hasKeys && ret : ret; | |||
} | |||
function cloneArray(arr, options) { | |||
const ret = []; | |||
for (let i = 0, l = arr.length; i < l; i++) { | |||
ret.push(clone(arr[i], options)); | |||
ret.push(clone(arr[i], options, true)); | |||
} | |||
return ret; | |||
} |
@@ -16,6 +16,7 @@ | |||
* @param {string|function} [options.foreignField] the foreign field to populate on if this is a populated virtual. | |||
* @param {boolean} [options.justOne=false] by default, a populated virtual is an array. If you set `justOne`, the populated virtual will be a single doc or `null`. | |||
* @param {boolean} [options.getters=false] if you set this to `true`, Mongoose will call any custom getters you defined on this virtual | |||
* @param {boolean} [options.count=false] if you set this to `true`, `populate()` will set this virtual to the number of populated documents, as opposed to the documents themselves, using [`Query#countDocuments()`](./api.html#query_Query-countDocuments) | |||
* @api public | |||
*/ | |||
@@ -31,7 +32,7 @@ function VirtualType(options, name) { | |||
* | |||
* @param {Function} fn | |||
* @return {VirtualType} this | |||
* @api public | |||
* @api private | |||
*/ | |||
VirtualType.prototype._applyDefaultGetters = function() { |
@@ -0,0 +1,201 @@ | |||
Apache License | |||
Version 2.0, January 2004 | |||
http://www.apache.org/licenses/ | |||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | |||
1. Definitions. | |||
"License" shall mean the terms and conditions for use, reproduction, | |||
and distribution as defined by Sections 1 through 9 of this document. | |||
"Licensor" shall mean the copyright owner or entity authorized by | |||
the copyright owner that is granting the License. | |||
"Legal Entity" shall mean the union of the acting entity and all | |||
other entities that control, are controlled by, or are under common | |||
control with that entity. For the purposes of this definition, | |||
"control" means (i) the power, direct or indirect, to cause the | |||
direction or management of such entity, whether by contract or | |||
otherwise, or (ii) ownership of fifty percent (50%) or more of the | |||
outstanding shares, or (iii) beneficial ownership of such entity. | |||
"You" (or "Your") shall mean an individual or Legal Entity | |||
exercising permissions granted by this License. | |||
"Source" form shall mean the preferred form for making modifications, | |||
including but not limited to software source code, documentation | |||
source, and configuration files. | |||
"Object" form shall mean any form resulting from mechanical | |||
transformation or translation of a Source form, including but | |||
not limited to compiled object code, generated documentation, | |||
and conversions to other media types. | |||
"Work" shall mean the work of authorship, whether in Source or | |||
Object form, made available under the License, as indicated by a | |||
copyright notice that is included in or attached to the work | |||
(an example is provided in the Appendix below). | |||
"Derivative Works" shall mean any work, whether in Source or Object | |||
form, that is based on (or derived from) the Work and for which the | |||
editorial revisions, annotations, elaborations, or other modifications | |||
represent, as a whole, an original work of authorship. For the purposes | |||
of this License, Derivative Works shall not include works that remain | |||
separable from, or merely link (or bind by name) to the interfaces of, | |||
the Work and Derivative Works thereof. | |||
"Contribution" shall mean any work of authorship, including | |||
the original version of the Work and any modifications or additions | |||
to that Work or Derivative Works thereof, that is intentionally | |||
submitted to Licensor for inclusion in the Work by the copyright owner | |||
or by an individual or Legal Entity authorized to submit on behalf of | |||
the copyright owner. For the purposes of this definition, "submitted" | |||
means any form of electronic, verbal, or written communication sent | |||
to the Licensor or its representatives, including but not limited to | |||
communication on electronic mailing lists, source code control systems, | |||
and issue tracking systems that are managed by, or on behalf of, the | |||
Licensor for the purpose of discussing and improving the Work, but | |||
excluding communication that is conspicuously marked or otherwise | |||
designated in writing by the copyright owner as "Not a Contribution." | |||
"Contributor" shall mean Licensor and any individual or Legal Entity | |||
on behalf of whom a Contribution has been received by Licensor and | |||
subsequently incorporated within the Work. | |||
2. Grant of Copyright License. Subject to the terms and conditions of | |||
this License, each Contributor hereby grants to You a perpetual, | |||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |||
copyright license to reproduce, prepare Derivative Works of, | |||
publicly display, publicly perform, sublicense, and distribute the | |||
Work and such Derivative Works in Source or Object form. | |||
3. Grant of Patent License. Subject to the terms and conditions of | |||
this License, each Contributor hereby grants to You a perpetual, | |||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |||
(except as stated in this section) patent license to make, have made, | |||
use, offer to sell, sell, import, and otherwise transfer the Work, | |||
where such license applies only to those patent claims licensable | |||
by such Contributor that are necessarily infringed by their | |||
Contribution(s) alone or by combination of their Contribution(s) | |||
with the Work to which such Contribution(s) was submitted. If You | |||
institute patent litigation against any entity (including a | |||
cross-claim or counterclaim in a lawsuit) alleging that the Work | |||
or a Contribution incorporated within the Work constitutes direct | |||
or contributory patent infringement, then any patent licenses | |||
granted to You under this License for that Work shall terminate | |||
as of the date such litigation is filed. | |||
4. Redistribution. You may reproduce and distribute copies of the | |||
Work or Derivative Works thereof in any medium, with or without | |||
modifications, and in Source or Object form, provided that You | |||
meet the following conditions: | |||
(a) You must give any other recipients of the Work or | |||
Derivative Works a copy of this License; and | |||
(b) You must cause any modified files to carry prominent notices | |||
stating that You changed the files; and | |||
(c) You must retain, in the Source form of any Derivative Works | |||
that You distribute, all copyright, patent, trademark, and | |||
attribution notices from the Source form of the Work, | |||
excluding those notices that do not pertain to any part of | |||
the Derivative Works; and | |||
(d) If the Work includes a "NOTICE" text file as part of its | |||
distribution, then any Derivative Works that You distribute must | |||
include a readable copy of the attribution notices contained | |||
within such NOTICE file, excluding those notices that do not | |||
pertain to any part of the Derivative Works, in at least one | |||
of the following places: within a NOTICE text file distributed | |||
as part of the Derivative Works; within the Source form or | |||
documentation, if provided along with the Derivative Works; or, | |||
within a display generated by the Derivative Works, if and | |||
wherever such third-party notices normally appear. The contents | |||
of the NOTICE file are for informational purposes only and | |||
do not modify the License. You may add Your own attribution | |||
notices within Derivative Works that You distribute, alongside | |||
or as an addendum to the NOTICE text from the Work, provided | |||
that such additional attribution notices cannot be construed | |||
as modifying the License. | |||
You may add Your own copyright statement to Your modifications and | |||
may provide additional or different license terms and conditions | |||
for use, reproduction, or distribution of Your modifications, or | |||
for any such Derivative Works as a whole, provided Your use, | |||
reproduction, and distribution of the Work otherwise complies with | |||
the conditions stated in this License. | |||
5. Submission of Contributions. Unless You explicitly state otherwise, | |||
any Contribution intentionally submitted for inclusion in the Work | |||
by You to the Licensor shall be under the terms and conditions of | |||
this License, without any additional terms or conditions. | |||
Notwithstanding the above, nothing herein shall supersede or modify | |||
the terms of any separate license agreement you may have executed | |||
with Licensor regarding such Contributions. | |||
6. Trademarks. This License does not grant permission to use the trade | |||
names, trademarks, service marks, or product names of the Licensor, | |||
except as required for reasonable and customary use in describing the | |||
origin of the Work and reproducing the content of the NOTICE file. | |||
7. Disclaimer of Warranty. Unless required by applicable law or | |||
agreed to in writing, Licensor provides the Work (and each | |||
Contributor provides its Contributions) on an "AS IS" BASIS, | |||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |||
implied, including, without limitation, any warranties or conditions | |||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | |||
PARTICULAR PURPOSE. You are solely responsible for determining the | |||
appropriateness of using or redistributing the Work and assume any | |||
risks associated with Your exercise of permissions under this License. | |||
8. Limitation of Liability. In no event and under no legal theory, | |||
whether in tort (including negligence), contract, or otherwise, | |||
unless required by applicable law (such as deliberate and grossly | |||
negligent acts) or agreed to in writing, shall any Contributor be | |||
liable to You for damages, including any direct, indirect, special, | |||
incidental, or consequential damages of any character arising as a | |||
result of this License or out of the use or inability to use the | |||
Work (including but not limited to damages for loss of goodwill, | |||
work stoppage, computer failure or malfunction, or any and all | |||
other commercial damages or losses), even if such Contributor | |||
has been advised of the possibility of such damages. | |||
9. Accepting Warranty or Additional Liability. While redistributing | |||
the Work or Derivative Works thereof, You may choose to offer, | |||
and charge a fee for, acceptance of support, warranty, indemnity, | |||
or other liability obligations and/or rights consistent with this | |||
License. However, in accepting such obligations, You may act only | |||
on Your own behalf and on Your sole responsibility, not on behalf | |||
of any other Contributor, and only if You agree to indemnify, | |||
defend, and hold each Contributor harmless for any liability | |||
incurred by, or claims asserted against, such Contributor by reason | |||
of your accepting any such warranty or additional liability. | |||
END OF TERMS AND CONDITIONS | |||
APPENDIX: How to apply the Apache License to your work. | |||
To apply the Apache License to your work, attach the following | |||
boilerplate notice, with the fields enclosed by brackets "{}" | |||
replaced with your own identifying information. (Don't include | |||
the brackets!) The text should be enclosed in the appropriate | |||
comment syntax for the file format. We also recommend that a | |||
file or class name and description of purpose be included on the | |||
same "printed page" as the copyright notice for easier | |||
identification within third-party archives. | |||
Copyright {yyyy} {name of copyright owner} | |||
Licensed under the Apache License, Version 2.0 (the "License"); | |||
you may not use this file except in compliance with the License. | |||
You may obtain a copy of the License at | |||
http://www.apache.org/licenses/LICENSE-2.0 | |||
Unless required by applicable law or agreed to in writing, software | |||
distributed under the License is distributed on an "AS IS" BASIS, | |||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
See the License for the specific language governing permissions and | |||
limitations under the License. |
@@ -0,0 +1,228 @@ | |||
[![Build Status](https://secure.travis-ci.org/mongodb-js/mongodb-core.png)](http://travis-ci.org/mongodb-js/mongodb-core) | |||
[![Coverage Status](https://coveralls.io/repos/github/mongodb-js/mongodb-core/badge.svg?branch=1.3)](https://coveralls.io/github/mongodb-js/mongodb-core?branch=1.3) | |||
# Description | |||
The MongoDB Core driver is the low level part of the 2.0 or higher MongoDB driver and is meant for library developers not end users. It does not contain any abstractions or helpers outside of the basic management of MongoDB topology connections, CRUD operations and authentication. | |||
## MongoDB Node.JS Core Driver | |||
| what | where | | |||
|---------------|------------------------------------------------| | |||
| documentation | http://mongodb.github.io/node-mongodb-native/ | | |||
| apidoc | http://mongodb.github.io/node-mongodb-native/ | | |||
| source | https://github.com/mongodb-js/mongodb-core | | |||
| mongodb | http://www.mongodb.org/ | | |||
### Blogs of Engineers involved in the driver | |||
- Christian Kvalheim [@christkv](https://twitter.com/christkv) <http://christiankvalheim.com> | |||
### Bugs / Feature Requests | |||
Think you’ve found a bug? Want to see a new feature in node-mongodb-native? Please open a | |||
case in our issue management tool, JIRA: | |||
- Create an account and login <https://jira.mongodb.org>. | |||
- Navigate to the NODE project <https://jira.mongodb.org/browse/NODE>. | |||
- Click **Create Issue** - Please provide as much information as possible about the issue type and how to reproduce it. | |||
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the | |||
Core Server (i.e. SERVER) project are **public**. | |||
### Questions and Bug Reports | |||
* mailing list: https://groups.google.com/forum/#!forum/node-mongodb-native | |||
* jira: http://jira.mongodb.org/ | |||
### Change Log | |||
http://jira.mongodb.org/browse/NODE | |||
# QuickStart | |||
The quick start guide will show you how to set up a simple application using Core driver and MongoDB. It scope is only how to set up the driver and perform the simple crud operations. For more inn depth coverage we encourage reading the tutorials. | |||
## Create the package.json file | |||
Let's create a directory where our application will live. In our case we will put this under our projects directory. | |||
``` | |||
mkdir myproject | |||
cd myproject | |||
``` | |||
Create a **package.json** using your favorite text editor and fill it in. | |||
```json | |||
{ | |||
"name": "myproject", | |||
"version": "1.0.0", | |||
"description": "My first project", | |||
"main": "index.js", | |||
"repository": { | |||
"type": "git", | |||
"url": "git://github.com/christkv/myfirstproject.git" | |||
}, | |||
"dependencies": { | |||
"mongodb-core": "~1.0" | |||
}, | |||
"author": "Christian Kvalheim", | |||
"license": "Apache 2.0", | |||
"bugs": { | |||
"url": "https://github.com/christkv/myfirstproject/issues" | |||
}, | |||
"homepage": "https://github.com/christkv/myfirstproject" | |||
} | |||
``` | |||
Save the file and return to the shell or command prompt and use **NPM** to install all the dependencies. | |||
``` | |||
npm install | |||
``` | |||
You should see **NPM** download a lot of files. Once it's done you'll find all the downloaded packages under the **node_modules** directory. | |||
Booting up a MongoDB Server | |||
--------------------------- | |||
Let's boot up a MongoDB server instance. Download the right MongoDB version from [MongoDB](http://www.mongodb.org), open a new shell or command line and ensure the **mongod** command is in the shell or command line path. Now let's create a database directory (in our case under **/data**). | |||
``` | |||
mongod --dbpath=/data --port 27017 | |||
``` | |||
You should see the **mongod** process start up and print some status information. | |||
## Connecting to MongoDB | |||
Let's create a new **app.js** file that we will use to show the basic CRUD operations using the MongoDB driver. | |||
First let's add code to connect to the server. Notice that there is no concept of a database here and we use the topology directly to perform the connection. | |||
```js | |||
var Server = require('mongodb-core').Server | |||
, assert = require('assert'); | |||
// Set up server connection | |||
var server = new Server({ | |||
host: 'localhost' | |||
, port: 27017 | |||
, reconnect: true | |||
, reconnectInterval: 50 | |||
}); | |||
// Add event listeners | |||
server.on('connect', function(_server) { | |||
console.log('connected'); | |||
test.done(); | |||
}); | |||
server.on('close', function() { | |||
console.log('closed'); | |||
}); | |||
server.on('reconnect', function() { | |||
console.log('reconnect'); | |||
}); | |||
// Start connection | |||
server.connect(); | |||
``` | |||
To connect to a replicaset we would use the `ReplSet` class and for a set of Mongos proxies we use the `Mongos` class. Each topology class offer the same CRUD operations and you operate on the topology directly. Let's look at an example exercising all the different available CRUD operations. | |||
```js | |||
var Server = require('mongodb-core').Server | |||
, assert = require('assert'); | |||
// Set up server connection | |||
var server = new Server({ | |||
host: 'localhost' | |||
, port: 27017 | |||
, reconnect: true | |||
, reconnectInterval: 50 | |||
}); | |||
// Add event listeners | |||
server.on('connect', function(_server) { | |||
console.log('connected'); | |||
// Execute the ismaster command | |||
_server.command('system.$cmd', {ismaster: true}, function(err, result) { | |||
// Perform a document insert | |||
_server.insert('myproject.inserts1', [{a:1}, {a:2}], { | |||
writeConcern: {w:1}, ordered:true | |||
}, function(err, results) { | |||
assert.equal(null, err); | |||
assert.equal(2, results.result.n); | |||
// Perform a document update | |||
_server.update('myproject.inserts1', [{ | |||
q: {a: 1}, u: {'$set': {b:1}} | |||
}], { | |||
writeConcern: {w:1}, ordered:true | |||
}, function(err, results) { | |||
assert.equal(null, err); | |||
assert.equal(1, results.result.n); | |||
// Remove a document | |||
_server.remove('myproject.inserts1', [{ | |||
q: {a: 1}, limit: 1 | |||
}], { | |||
writeConcern: {w:1}, ordered:true | |||
}, function(err, results) { | |||
assert.equal(null, err); | |||
assert.equal(1, results.result.n); | |||
// Get a document | |||
var cursor = _server.cursor('integration_tests.inserts_example4', { | |||
find: 'integration_tests.example4' | |||
, query: {a:1} | |||
}); | |||
// Get the first document | |||
cursor.next(function(err, doc) { | |||
assert.equal(null, err); | |||
assert.equal(2, doc.a); | |||
// Execute the ismaster command | |||
_server.command("system.$cmd" | |||
, {ismaster: true}, function(err, result) { | |||
assert.equal(null, err) | |||
_server.destroy(); | |||
}); | |||
}); | |||
}); | |||
}); | |||
test.done(); | |||
}); | |||
}); | |||
server.on('close', function() { | |||
console.log('closed'); | |||
}); | |||
server.on('reconnect', function() { | |||
console.log('reconnect'); | |||
}); | |||
// Start connection | |||
server.connect(); | |||
``` | |||
The core driver does not contain any helpers or abstractions only the core crud operations. These consist of the following commands. | |||
* `insert`, Insert takes an array of 1 or more documents to be inserted against the topology and allows you to specify a write concern and if you wish to execute the inserts in order or out of order. | |||
* `update`, Update takes an array of 1 or more update commands to be executed against the server topology and also allows you to specify a write concern and if you wish to execute the updates in order or out of order. | |||
* `remove`, Remove takes an array of 1 or more remove commands to be executed against the server topology and also allows you to specify a write concern and if you wish to execute the removes in order or out of order. | |||
* `cursor`, Returns you a cursor for either the 'virtual' `find` command, a command that returns a cursor id or a plain cursor id. Read the cursor tutorial for more inn depth coverage. | |||
* `command`, Executes a command against MongoDB and returns the result. | |||
* `auth`, Authenticates the current topology using a supported authentication scheme. | |||
The Core Driver is a building block for library builders and is not meant for usage by end users as it lacks a lot of features the end user might need such as automatic buffering of operations when a primary is changing in a replicaset or the db and collections abstraction. | |||
## Next steps | |||
The next step is to get more in depth information about how the different aspects of the core driver works and how to leverage them to extend the functionality of the cursors. Please view the tutorials for more detailed information. |
@@ -0,0 +1,48 @@ | |||
'use strict'; | |||
var BSON = require('bson'); | |||
var require_optional = require('require_optional'); | |||
const EJSON = require('./lib/utils').retrieveEJSON(); | |||
try { | |||
// Attempt to grab the native BSON parser | |||
var 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('./lib/error').MongoError, | |||
MongoNetworkError: require('./lib/error').MongoNetworkError, | |||
MongoParseError: require('./lib/error').MongoParseError, | |||
MongoTimeoutError: require('./lib/error').MongoTimeoutError, | |||
MongoWriteConcernError: require('./lib/error').MongoWriteConcernError, | |||
mongoErrorContextSymbol: require('./lib/error').mongoErrorContextSymbol, | |||
// Core | |||
Connection: require('./lib/connection/connection'), | |||
Server: require('./lib/topologies/server'), | |||
ReplSet: require('./lib/topologies/replset'), | |||
Mongos: require('./lib/topologies/mongos'), | |||
Logger: require('./lib/connection/logger'), | |||
Cursor: require('./lib/cursor'), | |||
ReadPreference: require('./lib/topologies/read_preference'), | |||
Sessions: require('./lib/sessions'), | |||
BSON: BSON, | |||
EJSON: EJSON, | |||
// Raw operations | |||
Query: require('./lib/connection/commands').Query, | |||
// Auth mechanisms | |||
defaultAuthProviders: require('./lib/auth/defaultAuthProviders').defaultAuthProviders, | |||
MongoCR: require('./lib/auth/mongocr'), | |||
X509: require('./lib/auth/x509'), | |||
Plain: require('./lib/auth/plain'), | |||
GSSAPI: require('./lib/auth/gssapi'), | |||
ScramSHA1: require('./lib/auth/scram').ScramSHA1, | |||
ScramSHA256: require('./lib/auth/scram').ScramSHA256, | |||
// Utilities | |||
parseConnectionString: require('./lib/uri_parser') | |||
}; |
@@ -0,0 +1,29 @@ | |||
'use strict'; | |||
const MongoCR = require('./mongocr'); | |||
const X509 = require('./x509'); | |||
const Plain = require('./plain'); | |||
const GSSAPI = require('./gssapi'); | |||
const SSPI = require('./sspi'); | |||
const ScramSHA1 = require('./scram').ScramSHA1; | |||
const ScramSHA256 = require('./scram').ScramSHA256; | |||
/** | |||
* Returns the default authentication providers. | |||
* | |||
* @param {BSON} bson Bson definition | |||
* @returns {Object} a mapping of auth names to auth types | |||
*/ | |||
function defaultAuthProviders(bson) { | |||
return { | |||
mongocr: new MongoCR(bson), | |||
x509: new X509(bson), | |||
plain: new Plain(bson), | |||
gssapi: new GSSAPI(bson), | |||
sspi: new SSPI(bson), | |||
'scram-sha-1': new ScramSHA1(bson), | |||
'scram-sha-256': new ScramSHA256(bson) | |||
}; | |||
} | |||
module.exports = { defaultAuthProviders }; |
@@ -0,0 +1,381 @@ | |||
'use strict'; | |||
const f = require('util').format; | |||
const Query = require('../connection/commands').Query; | |||
const MongoError = require('../error').MongoError; | |||
const retrieveKerberos = require('../utils').retrieveKerberos; | |||
var AuthSession = function(db, username, password, options) { | |||
this.db = db; | |||
this.username = username; | |||
this.password = password; | |||
this.options = options; | |||
}; | |||
AuthSession.prototype.equal = function(session) { | |||
return ( | |||
session.db === this.db && | |||
session.username === this.username && | |||
session.password === this.password | |||
); | |||
}; | |||
/** | |||
* Creates a new GSSAPI authentication mechanism | |||
* @class | |||
* @return {GSSAPI} A cursor instance | |||
*/ | |||
var GSSAPI = function(bson) { | |||
this.bson = bson; | |||
this.authStore = []; | |||
}; | |||
/** | |||
* Authenticate | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {string} db Name of the database | |||
* @param {string} username Username | |||
* @param {string} password Password | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
GSSAPI.prototype.auth = function(server, connections, db, username, password, options, callback) { | |||
var self = this; | |||
let kerberos; | |||
try { | |||
kerberos = retrieveKerberos(); | |||
} catch (e) { | |||
return callback(e, null); | |||
} | |||
// TODO: remove this once we fix URI parsing | |||
var gssapiServiceName = options['gssapiservicename'] || options['gssapiServiceName'] || 'mongodb'; | |||
// Total connections | |||
var count = connections.length; | |||
if (count === 0) return callback(null, null); | |||
// Valid connections | |||
var numberOfValidConnections = 0; | |||
var errorObject = null; | |||
// For each connection we need to authenticate | |||
while (connections.length > 0) { | |||
// Execute MongoCR | |||
var execute = function(connection) { | |||
// Start Auth process for a connection | |||
GSSAPIInitialize( | |||
self, | |||
kerberos.processes.MongoAuthProcess, | |||
db, | |||
username, | |||
password, | |||
db, | |||
gssapiServiceName, | |||
server, | |||
connection, | |||
options, | |||
function(err, r) { | |||
// Adjust count | |||
count = count - 1; | |||
// If we have an error | |||
if (err) { | |||
errorObject = err; | |||
} else if (r.result['$err']) { | |||
errorObject = r.result; | |||
} else if (r.result['errmsg']) { | |||
errorObject = r.result; | |||
} else { | |||
numberOfValidConnections = numberOfValidConnections + 1; | |||
} | |||
// We have authenticated all connections | |||
if (count === 0 && numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password, options)); | |||
// Return correct authentication | |||
callback(null, true); | |||
} else if (count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using mongocr')); | |||
callback(errorObject, false); | |||
} | |||
} | |||
); | |||
}; | |||
var _execute = function(_connection) { | |||
process.nextTick(function() { | |||
execute(_connection); | |||
}); | |||
}; | |||
_execute(connections.shift()); | |||
} | |||
}; | |||
// | |||
// Initialize step | |||
var GSSAPIInitialize = function( | |||
self, | |||
MongoAuthProcess, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
gssapiServiceName, | |||
server, | |||
connection, | |||
options, | |||
callback | |||
) { | |||
// Create authenticator | |||
var mongo_auth_process = new MongoAuthProcess( | |||
connection.host, | |||
connection.port, | |||
gssapiServiceName, | |||
options | |||
); | |||
// Perform initialization | |||
mongo_auth_process.init(username, password, function(err) { | |||
if (err) return callback(err, false); | |||
// Perform the first step | |||
mongo_auth_process.transition('', function(err, payload) { | |||
if (err) return callback(err, false); | |||
// Call the next db step | |||
MongoDBGSSAPIFirstStep( | |||
self, | |||
mongo_auth_process, | |||
payload, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
server, | |||
connection, | |||
callback | |||
); | |||
}); | |||
}); | |||
}; | |||
// | |||
// Perform first step against mongodb | |||
var MongoDBGSSAPIFirstStep = function( | |||
self, | |||
mongo_auth_process, | |||
payload, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
server, | |||
connection, | |||
callback | |||
) { | |||
// Build the sasl start command | |||
var command = { | |||
saslStart: 1, | |||
mechanism: 'GSSAPI', | |||
payload: payload, | |||
autoAuthorize: 1 | |||
}; | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query(self.bson, '$external.$cmd', command, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
if (err) return callback(err, false); | |||
var doc = r.result; | |||
// Execute mongodb transition | |||
mongo_auth_process.transition(r.result.payload, function(err, payload) { | |||
if (err) return callback(err, false); | |||
// MongoDB API Second Step | |||
MongoDBGSSAPISecondStep( | |||
self, | |||
mongo_auth_process, | |||
payload, | |||
doc, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
server, | |||
connection, | |||
callback | |||
); | |||
}); | |||
} | |||
); | |||
}; | |||
// | |||
// Perform first step against mongodb | |||
var MongoDBGSSAPISecondStep = function( | |||
self, | |||
mongo_auth_process, | |||
payload, | |||
doc, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
server, | |||
connection, | |||
callback | |||
) { | |||
// Build Authentication command to send to MongoDB | |||
var command = { | |||
saslContinue: 1, | |||
conversationId: doc.conversationId, | |||
payload: payload | |||
}; | |||
// Execute the command | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query(self.bson, '$external.$cmd', command, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
if (err) return callback(err, false); | |||
var doc = r.result; | |||
// Call next transition for kerberos | |||
mongo_auth_process.transition(doc.payload, function(err, payload) { | |||
if (err) return callback(err, false); | |||
// Call the last and third step | |||
MongoDBGSSAPIThirdStep( | |||
self, | |||
mongo_auth_process, | |||
payload, | |||
doc, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
server, | |||
connection, | |||
callback | |||
); | |||
}); | |||
} | |||
); | |||
}; | |||
var MongoDBGSSAPIThirdStep = function( | |||
self, | |||
mongo_auth_process, | |||
payload, | |||
doc, | |||
db, | |||
username, | |||
password, | |||
authdb, | |||
server, | |||
connection, | |||
callback | |||
) { | |||
// Build final command | |||
var command = { | |||
saslContinue: 1, | |||
conversationId: doc.conversationId, | |||
payload: payload | |||
}; | |||
// Execute the command | |||
server( | |||
connection, | |||
new Query(self.bson, '$external.$cmd', command, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
if (err) return callback(err, false); | |||
mongo_auth_process.transition(null, function(err) { | |||
if (err) return callback(err, null); | |||
callback(null, r); | |||
}); | |||
} | |||
); | |||
}; | |||
// Add to store only if it does not exist | |||
var addAuthSession = function(authStore, session) { | |||
var found = false; | |||
for (var i = 0; i < authStore.length; i++) { | |||
if (authStore[i].equal(session)) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) authStore.push(session); | |||
}; | |||
/** | |||
* Remove authStore credentials | |||
* @method | |||
* @param {string} db Name of database we are removing authStore details about | |||
* @return {object} | |||
*/ | |||
GSSAPI.prototype.logout = function(dbName) { | |||
this.authStore = this.authStore.filter(function(x) { | |||
return x.db !== dbName; | |||
}); | |||
}; | |||
/** | |||
* Re authenticate pool | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
GSSAPI.prototype.reauthenticate = function(server, connections, callback) { | |||
var authStore = this.authStore.slice(0); | |||
var count = authStore.length; | |||
if (count === 0) return callback(null, null); | |||
// Iterate over all the auth details stored | |||
for (var i = 0; i < authStore.length; i++) { | |||
this.auth( | |||
server, | |||
connections, | |||
authStore[i].db, | |||
authStore[i].username, | |||
authStore[i].password, | |||
authStore[i].options, | |||
function(err) { | |||
count = count - 1; | |||
// Done re-authenticating | |||
if (count === 0) { | |||
callback(err, null); | |||
} | |||
} | |||
); | |||
} | |||
}; | |||
/** | |||
* This is a result from a authentication strategy | |||
* | |||
* @callback authResultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {boolean} result The result of the authentication process | |||
*/ | |||
module.exports = GSSAPI; |
@@ -0,0 +1,214 @@ | |||
'use strict'; | |||
var f = require('util').format, | |||
crypto = require('crypto'), | |||
Query = require('../connection/commands').Query, | |||
MongoError = require('../error').MongoError; | |||
var AuthSession = function(db, username, password) { | |||
this.db = db; | |||
this.username = username; | |||
this.password = password; | |||
}; | |||
AuthSession.prototype.equal = function(session) { | |||
return ( | |||
session.db === this.db && | |||
session.username === this.username && | |||
session.password === this.password | |||
); | |||
}; | |||
/** | |||
* Creates a new MongoCR authentication mechanism | |||
* @class | |||
* @return {MongoCR} A cursor instance | |||
*/ | |||
var MongoCR = function(bson) { | |||
this.bson = bson; | |||
this.authStore = []; | |||
}; | |||
// Add to store only if it does not exist | |||
var addAuthSession = function(authStore, session) { | |||
var found = false; | |||
for (var i = 0; i < authStore.length; i++) { | |||
if (authStore[i].equal(session)) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) authStore.push(session); | |||
}; | |||
/** | |||
* Authenticate | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {string} db Name of the database | |||
* @param {string} username Username | |||
* @param {string} password Password | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
MongoCR.prototype.auth = function(server, connections, db, username, password, callback) { | |||
var self = this; | |||
// Total connections | |||
var count = connections.length; | |||
if (count === 0) return callback(null, null); | |||
// Valid connections | |||
var numberOfValidConnections = 0; | |||
var errorObject = null; | |||
// For each connection we need to authenticate | |||
while (connections.length > 0) { | |||
// Execute MongoCR | |||
var executeMongoCR = function(connection) { | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query( | |||
self.bson, | |||
f('%s.$cmd', db), | |||
{ | |||
getnonce: 1 | |||
}, | |||
{ | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
} | |||
), | |||
function(err, r) { | |||
var nonce = null; | |||
var key = null; | |||
// Adjust the number of connections left | |||
// Get nonce | |||
if (err == null) { | |||
nonce = r.result.nonce; | |||
// Use node md5 generator | |||
var md5 = crypto.createHash('md5'); | |||
// Generate keys used for authentication | |||
md5.update(username + ':mongo:' + password, 'utf8'); | |||
var hash_password = md5.digest('hex'); | |||
// Final key | |||
md5 = crypto.createHash('md5'); | |||
md5.update(nonce + username + hash_password, 'utf8'); | |||
key = md5.digest('hex'); | |||
} | |||
// Execute command | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query( | |||
self.bson, | |||
f('%s.$cmd', db), | |||
{ | |||
authenticate: 1, | |||
user: username, | |||
nonce: nonce, | |||
key: key | |||
}, | |||
{ | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
} | |||
), | |||
function(err, r) { | |||
count = count - 1; | |||
// If we have an error | |||
if (err) { | |||
errorObject = err; | |||
} else if (r.result['$err']) { | |||
errorObject = r.result; | |||
} else if (r.result['errmsg']) { | |||
errorObject = r.result; | |||
} else { | |||
numberOfValidConnections = numberOfValidConnections + 1; | |||
} | |||
// We have authenticated all connections | |||
if (count === 0 && numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password)); | |||
// Return correct authentication | |||
callback(null, true); | |||
} else if (count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using mongocr')); | |||
callback(errorObject, false); | |||
} | |||
} | |||
); | |||
} | |||
); | |||
}; | |||
var _execute = function(_connection) { | |||
process.nextTick(function() { | |||
executeMongoCR(_connection); | |||
}); | |||
}; | |||
_execute(connections.shift()); | |||
} | |||
}; | |||
/** | |||
* Remove authStore credentials | |||
* @method | |||
* @param {string} db Name of database we are removing authStore details about | |||
* @return {object} | |||
*/ | |||
MongoCR.prototype.logout = function(dbName) { | |||
this.authStore = this.authStore.filter(function(x) { | |||
return x.db !== dbName; | |||
}); | |||
}; | |||
/** | |||
* Re authenticate pool | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
MongoCR.prototype.reauthenticate = function(server, connections, callback) { | |||
var authStore = this.authStore.slice(0); | |||
var count = authStore.length; | |||
if (count === 0) return callback(null, null); | |||
// Iterate over all the auth details stored | |||
for (var i = 0; i < authStore.length; i++) { | |||
this.auth( | |||
server, | |||
connections, | |||
authStore[i].db, | |||
authStore[i].username, | |||
authStore[i].password, | |||
function(err) { | |||
count = count - 1; | |||
// Done re-authenticating | |||
if (count === 0) { | |||
callback(err, null); | |||
} | |||
} | |||
); | |||
} | |||
}; | |||
/** | |||
* This is a result from a authentication strategy | |||
* | |||
* @callback authResultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {boolean} result The result of the authentication process | |||
*/ | |||
module.exports = MongoCR; |
@@ -0,0 +1,183 @@ | |||
'use strict'; | |||
var f = require('util').format, | |||
retrieveBSON = require('../connection/utils').retrieveBSON, | |||
Query = require('../connection/commands').Query, | |||
MongoError = require('../error').MongoError; | |||
var BSON = retrieveBSON(), | |||
Binary = BSON.Binary; | |||
var AuthSession = function(db, username, password) { | |||
this.db = db; | |||
this.username = username; | |||
this.password = password; | |||
}; | |||
AuthSession.prototype.equal = function(session) { | |||
return ( | |||
session.db === this.db && | |||
session.username === this.username && | |||
session.password === this.password | |||
); | |||
}; | |||
/** | |||
* Creates a new Plain authentication mechanism | |||
* @class | |||
* @return {Plain} A cursor instance | |||
*/ | |||
var Plain = function(bson) { | |||
this.bson = bson; | |||
this.authStore = []; | |||
}; | |||
/** | |||
* Authenticate | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {string} db Name of the database | |||
* @param {string} username Username | |||
* @param {string} password Password | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
Plain.prototype.auth = function(server, connections, db, username, password, callback) { | |||
var self = this; | |||
// Total connections | |||
var count = connections.length; | |||
if (count === 0) return callback(null, null); | |||
// Valid connections | |||
var numberOfValidConnections = 0; | |||
var errorObject = null; | |||
// For each connection we need to authenticate | |||
while (connections.length > 0) { | |||
// Execute MongoCR | |||
var execute = function(connection) { | |||
// Create payload | |||
var payload = new Binary(f('\x00%s\x00%s', username, password)); | |||
// Let's start the sasl process | |||
var command = { | |||
saslStart: 1, | |||
mechanism: 'PLAIN', | |||
payload: payload, | |||
autoAuthorize: 1 | |||
}; | |||
// Let's start the process | |||
server( | |||
connection, | |||
new Query(self.bson, '$external.$cmd', command, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
// Adjust count | |||
count = count - 1; | |||
// If we have an error | |||
if (err) { | |||
errorObject = err; | |||
} else if (r.result['$err']) { | |||
errorObject = r.result; | |||
} else if (r.result['errmsg']) { | |||
errorObject = r.result; | |||
} else { | |||
numberOfValidConnections = numberOfValidConnections + 1; | |||
} | |||
// We have authenticated all connections | |||
if (count === 0 && numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password)); | |||
// Return correct authentication | |||
callback(null, true); | |||
} else if (count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using mongocr')); | |||
callback(errorObject, false); | |||
} | |||
} | |||
); | |||
}; | |||
var _execute = function(_connection) { | |||
process.nextTick(function() { | |||
execute(_connection); | |||
}); | |||
}; | |||
_execute(connections.shift()); | |||
} | |||
}; | |||
// Add to store only if it does not exist | |||
var addAuthSession = function(authStore, session) { | |||
var found = false; | |||
for (var i = 0; i < authStore.length; i++) { | |||
if (authStore[i].equal(session)) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) authStore.push(session); | |||
}; | |||
/** | |||
* Remove authStore credentials | |||
* @method | |||
* @param {string} db Name of database we are removing authStore details about | |||
* @return {object} | |||
*/ | |||
Plain.prototype.logout = function(dbName) { | |||
this.authStore = this.authStore.filter(function(x) { | |||
return x.db !== dbName; | |||
}); | |||
}; | |||
/** | |||
* Re authenticate pool | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
Plain.prototype.reauthenticate = function(server, connections, callback) { | |||
var authStore = this.authStore.slice(0); | |||
var count = authStore.length; | |||
if (count === 0) return callback(null, null); | |||
// Iterate over all the auth details stored | |||
for (var i = 0; i < authStore.length; i++) { | |||
this.auth( | |||
server, | |||
connections, | |||
authStore[i].db, | |||
authStore[i].username, | |||
authStore[i].password, | |||
function(err) { | |||
count = count - 1; | |||
// Done re-authenticating | |||
if (count === 0) { | |||
callback(err, null); | |||
} | |||
} | |||
); | |||
} | |||
}; | |||
/** | |||
* This is a result from a authentication strategy | |||
* | |||
* @callback authResultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {boolean} result The result of the authentication process | |||
*/ | |||
module.exports = Plain; |
@@ -0,0 +1,442 @@ | |||
'use strict'; | |||
var f = require('util').format, | |||
crypto = require('crypto'), | |||
retrieveBSON = require('../connection/utils').retrieveBSON, | |||
Query = require('../connection/commands').Query, | |||
MongoError = require('../error').MongoError, | |||
Buffer = require('safe-buffer').Buffer; | |||
let saslprep; | |||
try { | |||
saslprep = require('saslprep'); | |||
} catch (e) { | |||
// don't do anything; | |||
} | |||
var BSON = retrieveBSON(), | |||
Binary = BSON.Binary; | |||
var AuthSession = function(db, username, password) { | |||
this.db = db; | |||
this.username = username; | |||
this.password = password; | |||
}; | |||
AuthSession.prototype.equal = function(session) { | |||
return ( | |||
session.db === this.db && | |||
session.username === this.username && | |||
session.password === this.password | |||
); | |||
}; | |||
var id = 0; | |||
/** | |||
* Creates a new ScramSHA authentication mechanism | |||
* @class | |||
* @return {ScramSHA} A cursor instance | |||
*/ | |||
var ScramSHA = function(bson, cryptoMethod) { | |||
this.bson = bson; | |||
this.authStore = []; | |||
this.id = id++; | |||
this.cryptoMethod = cryptoMethod || 'sha1'; | |||
}; | |||
var parsePayload = function(payload) { | |||
var dict = {}; | |||
var parts = payload.split(','); | |||
for (var i = 0; i < parts.length; i++) { | |||
var valueParts = parts[i].split('='); | |||
dict[valueParts[0]] = valueParts[1]; | |||
} | |||
return dict; | |||
}; | |||
var passwordDigest = function(username, password) { | |||
if (typeof username !== 'string') throw new MongoError('username must be a string'); | |||
if (typeof password !== 'string') throw new MongoError('password must be a string'); | |||
if (password.length === 0) throw new MongoError('password cannot be empty'); | |||
// Use node md5 generator | |||
var md5 = crypto.createHash('md5'); | |||
// Generate keys used for authentication | |||
md5.update(username + ':mongo:' + password, 'utf8'); | |||
return md5.digest('hex'); | |||
}; | |||
// XOR two buffers | |||
function xor(a, b) { | |||
if (!Buffer.isBuffer(a)) a = Buffer.from(a); | |||
if (!Buffer.isBuffer(b)) b = Buffer.from(b); | |||
const length = Math.max(a.length, b.length); | |||
const res = []; | |||
for (let i = 0; i < length; i += 1) { | |||
res.push(a[i] ^ b[i]); | |||
} | |||
return Buffer.from(res).toString('base64'); | |||
} | |||
function H(method, text) { | |||
return crypto | |||
.createHash(method) | |||
.update(text) | |||
.digest(); | |||
} | |||
function HMAC(method, key, text) { | |||
return crypto | |||
.createHmac(method, key) | |||
.update(text) | |||
.digest(); | |||
} | |||
var _hiCache = {}; | |||
var _hiCacheCount = 0; | |||
var _hiCachePurge = function() { | |||
_hiCache = {}; | |||
_hiCacheCount = 0; | |||
}; | |||
const hiLengthMap = { | |||
sha256: 32, | |||
sha1: 20 | |||
}; | |||
function HI(data, salt, iterations, cryptoMethod) { | |||
// omit the work if already generated | |||
const key = [data, salt.toString('base64'), iterations].join('_'); | |||
if (_hiCache[key] !== undefined) { | |||
return _hiCache[key]; | |||
} | |||
// generate the salt | |||
const saltedData = crypto.pbkdf2Sync( | |||
data, | |||
salt, | |||
iterations, | |||
hiLengthMap[cryptoMethod], | |||
cryptoMethod | |||
); | |||
// cache a copy to speed up the next lookup, but prevent unbounded cache growth | |||
if (_hiCacheCount >= 200) { | |||
_hiCachePurge(); | |||
} | |||
_hiCache[key] = saltedData; | |||
_hiCacheCount += 1; | |||
return saltedData; | |||
} | |||
/** | |||
* Authenticate | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {string} db Name of the database | |||
* @param {string} username Username | |||
* @param {string} password Password | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
ScramSHA.prototype.auth = function(server, connections, db, username, password, callback) { | |||
var self = this; | |||
// Total connections | |||
var count = connections.length; | |||
if (count === 0) return callback(null, null); | |||
// Valid connections | |||
var numberOfValidConnections = 0; | |||
var errorObject = null; | |||
const cryptoMethod = this.cryptoMethod; | |||
let mechanism = 'SCRAM-SHA-1'; | |||
let processedPassword; | |||
if (cryptoMethod === 'sha256') { | |||
mechanism = 'SCRAM-SHA-256'; | |||
let saslprepFn = (server.s && server.s.saslprep) || saslprep; | |||
if (saslprepFn) { | |||
processedPassword = saslprepFn(password); | |||
} else { | |||
console.warn('Warning: no saslprep library specified. Passwords will not be sanitized'); | |||
processedPassword = password; | |||
} | |||
} else { | |||
processedPassword = passwordDigest(username, password); | |||
} | |||
// Execute MongoCR | |||
var executeScram = function(connection) { | |||
// Clean up the user | |||
username = username.replace('=', '=3D').replace(',', '=2C'); | |||
// Create a random nonce | |||
var nonce = crypto.randomBytes(24).toString('base64'); | |||
// var nonce = 'MsQUY9iw0T9fx2MUEz6LZPwGuhVvWAhc' | |||
// NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8. | |||
// Since the username is not sasl-prep-d, we need to do this here. | |||
const firstBare = Buffer.concat([ | |||
Buffer.from('n=', 'utf8'), | |||
Buffer.from(username, 'utf8'), | |||
Buffer.from(',r=', 'utf8'), | |||
Buffer.from(nonce, 'utf8') | |||
]); | |||
// Build command structure | |||
var cmd = { | |||
saslStart: 1, | |||
mechanism: mechanism, | |||
payload: new Binary(Buffer.concat([Buffer.from('n,,', 'utf8'), firstBare])), | |||
autoAuthorize: 1 | |||
}; | |||
// Handle the error | |||
var handleError = function(err, r) { | |||
if (err) { | |||
numberOfValidConnections = numberOfValidConnections - 1; | |||
errorObject = err; | |||
return false; | |||
} else if (r.result['$err']) { | |||
errorObject = r.result; | |||
return false; | |||
} else if (r.result['errmsg']) { | |||
errorObject = r.result; | |||
return false; | |||
} else { | |||
numberOfValidConnections = numberOfValidConnections + 1; | |||
} | |||
return true; | |||
}; | |||
// Finish up | |||
var finish = function(_count, _numberOfValidConnections) { | |||
if (_count === 0 && _numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password)); | |||
// Return correct authentication | |||
return callback(null, true); | |||
} else if (_count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using scram')); | |||
return callback(errorObject, false); | |||
} | |||
}; | |||
var handleEnd = function(_err, _r) { | |||
// Handle any error | |||
handleError(_err, _r); | |||
// Adjust the number of connections | |||
count = count - 1; | |||
// Execute the finish | |||
finish(count, numberOfValidConnections); | |||
}; | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query(self.bson, f('%s.$cmd', db), cmd, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
// Do we have an error, handle it | |||
if (handleError(err, r) === false) { | |||
count = count - 1; | |||
if (count === 0 && numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password)); | |||
// Return correct authentication | |||
return callback(null, true); | |||
} else if (count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using scram')); | |||
return callback(errorObject, false); | |||
} | |||
return; | |||
} | |||
// Get the dictionary | |||
var dict = parsePayload(r.result.payload.value()); | |||
// Unpack dictionary | |||
var iterations = parseInt(dict.i, 10); | |||
var salt = dict.s; | |||
var rnonce = dict.r; | |||
// Set up start of proof | |||
var withoutProof = f('c=biws,r=%s', rnonce); | |||
var saltedPassword = HI( | |||
processedPassword, | |||
Buffer.from(salt, 'base64'), | |||
iterations, | |||
cryptoMethod | |||
); | |||
if (iterations && iterations < 4096) { | |||
const error = new MongoError(`Server returned an invalid iteration count ${iterations}`); | |||
return callback(error, false); | |||
} | |||
// Create the client key | |||
const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key'); | |||
// Create the stored key | |||
const storedKey = H(cryptoMethod, clientKey); | |||
// Create the authentication message | |||
const authMessage = [ | |||
firstBare, | |||
r.result.payload.value().toString('base64'), | |||
withoutProof | |||
].join(','); | |||
// Create client signature | |||
const clientSignature = HMAC(cryptoMethod, storedKey, authMessage); | |||
// Create client proof | |||
const clientProof = f('p=%s', xor(clientKey, clientSignature)); | |||
// Create client final | |||
const clientFinal = [withoutProof, clientProof].join(','); | |||
// Create continue message | |||
const cmd = { | |||
saslContinue: 1, | |||
conversationId: r.result.conversationId, | |||
payload: new Binary(Buffer.from(clientFinal)) | |||
}; | |||
// | |||
// Execute sasl continue | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query(self.bson, f('%s.$cmd', db), cmd, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
if (r && r.result.done === false) { | |||
var cmd = { | |||
saslContinue: 1, | |||
conversationId: r.result.conversationId, | |||
payload: Buffer.alloc(0) | |||
}; | |||
// Write the commmand on the connection | |||
server( | |||
connection, | |||
new Query(self.bson, f('%s.$cmd', db), cmd, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
handleEnd(err, r); | |||
} | |||
); | |||
} else { | |||
handleEnd(err, r); | |||
} | |||
} | |||
); | |||
} | |||
); | |||
}; | |||
var _execute = function(_connection) { | |||
process.nextTick(function() { | |||
executeScram(_connection); | |||
}); | |||
}; | |||
// For each connection we need to authenticate | |||
while (connections.length > 0) { | |||
_execute(connections.shift()); | |||
} | |||
}; | |||
// Add to store only if it does not exist | |||
var addAuthSession = function(authStore, session) { | |||
var found = false; | |||
for (var i = 0; i < authStore.length; i++) { | |||
if (authStore[i].equal(session)) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) authStore.push(session); | |||
}; | |||
/** | |||
* Remove authStore credentials | |||
* @method | |||
* @param {string} db Name of database we are removing authStore details about | |||
* @return {object} | |||
*/ | |||
ScramSHA.prototype.logout = function(dbName) { | |||
this.authStore = this.authStore.filter(function(x) { | |||
return x.db !== dbName; | |||
}); | |||
}; | |||
/** | |||
* Re authenticate pool | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
ScramSHA.prototype.reauthenticate = function(server, connections, callback) { | |||
var authStore = this.authStore.slice(0); | |||
var count = authStore.length; | |||
// No connections | |||
if (count === 0) return callback(null, null); | |||
// Iterate over all the auth details stored | |||
for (var i = 0; i < authStore.length; i++) { | |||
this.auth( | |||
server, | |||
connections, | |||
authStore[i].db, | |||
authStore[i].username, | |||
authStore[i].password, | |||
function(err) { | |||
count = count - 1; | |||
// Done re-authenticating | |||
if (count === 0) { | |||
callback(err, null); | |||
} | |||
} | |||
); | |||
} | |||
}; | |||
class ScramSHA1 extends ScramSHA { | |||
constructor(bson) { | |||
super(bson, 'sha1'); | |||
} | |||
} | |||
class ScramSHA256 extends ScramSHA { | |||
constructor(bson) { | |||
super(bson, 'sha256'); | |||
} | |||
} | |||
module.exports = { ScramSHA1, ScramSHA256 }; |
@@ -0,0 +1,262 @@ | |||
'use strict'; | |||
const f = require('util').format; | |||
const Query = require('../connection/commands').Query; | |||
const MongoError = require('../error').MongoError; | |||
const retrieveKerberos = require('../utils').retrieveKerberos; | |||
var AuthSession = function(db, username, password, options) { | |||
this.db = db; | |||
this.username = username; | |||
this.password = password; | |||
this.options = options; | |||
}; | |||
AuthSession.prototype.equal = function(session) { | |||
return ( | |||
session.db === this.db && | |||
session.username === this.username && | |||
session.password === this.password | |||
); | |||
}; | |||
/** | |||
* Creates a new SSPI authentication mechanism | |||
* @class | |||
* @return {SSPI} A cursor instance | |||
*/ | |||
var SSPI = function(bson) { | |||
this.bson = bson; | |||
this.authStore = []; | |||
}; | |||
/** | |||
* Authenticate | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {string} db Name of the database | |||
* @param {string} username Username | |||
* @param {string} password Password | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
SSPI.prototype.auth = function(server, connections, db, username, password, options, callback) { | |||
var self = this; | |||
let kerberos; | |||
try { | |||
kerberos = retrieveKerberos(); | |||
} catch (e) { | |||
return callback(e, null); | |||
} | |||
var gssapiServiceName = options['gssapiServiceName'] || 'mongodb'; | |||
// Total connections | |||
var count = connections.length; | |||
if (count === 0) return callback(null, null); | |||
// Valid connections | |||
var numberOfValidConnections = 0; | |||
var errorObject = null; | |||
// For each connection we need to authenticate | |||
while (connections.length > 0) { | |||
// Execute MongoCR | |||
var execute = function(connection) { | |||
// Start Auth process for a connection | |||
SSIPAuthenticate( | |||
self, | |||
kerberos.processes.MongoAuthProcess, | |||
username, | |||
password, | |||
gssapiServiceName, | |||
server, | |||
connection, | |||
options, | |||
function(err, r) { | |||
// Adjust count | |||
count = count - 1; | |||
// If we have an error | |||
if (err) { | |||
errorObject = err; | |||
} else if (r && typeof r === 'object' && r.result['$err']) { | |||
errorObject = r.result; | |||
} else if (r && typeof r === 'object' && r.result['errmsg']) { | |||
errorObject = r.result; | |||
} else { | |||
numberOfValidConnections = numberOfValidConnections + 1; | |||
} | |||
// We have authenticated all connections | |||
if (count === 0 && numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password, options)); | |||
// Return correct authentication | |||
callback(null, true); | |||
} else if (count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using mongocr')); | |||
callback(errorObject, false); | |||
} | |||
} | |||
); | |||
}; | |||
var _execute = function(_connection) { | |||
process.nextTick(function() { | |||
execute(_connection); | |||
}); | |||
}; | |||
_execute(connections.shift()); | |||
} | |||
}; | |||
function SSIPAuthenticate( | |||
self, | |||
MongoAuthProcess, | |||
username, | |||
password, | |||
gssapiServiceName, | |||
server, | |||
connection, | |||
options, | |||
callback | |||
) { | |||
const authProcess = new MongoAuthProcess( | |||
connection.host, | |||
connection.port, | |||
gssapiServiceName, | |||
options | |||
); | |||
function authCommand(command, authCb) { | |||
const query = new Query(self.bson, '$external.$cmd', command, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}); | |||
server(connection, query, authCb); | |||
} | |||
authProcess.init(username, password, err => { | |||
if (err) return callback(err, false); | |||
authProcess.transition('', (err, payload) => { | |||
if (err) return callback(err, false); | |||
const command = { | |||
saslStart: 1, | |||
mechanism: 'GSSAPI', | |||
payload, | |||
autoAuthorize: 1 | |||
}; | |||
authCommand(command, (err, result) => { | |||
if (err) return callback(err, false); | |||
const doc = result.result; | |||
authProcess.transition(doc.payload, (err, payload) => { | |||
if (err) return callback(err, false); | |||
const command = { | |||
saslContinue: 1, | |||
conversationId: doc.conversationId, | |||
payload | |||
}; | |||
authCommand(command, (err, result) => { | |||
if (err) return callback(err, false); | |||
const doc = result.result; | |||
authProcess.transition(doc.payload, (err, payload) => { | |||
if (err) return callback(err, false); | |||
const command = { | |||
saslContinue: 1, | |||
conversationId: doc.conversationId, | |||
payload | |||
}; | |||
authCommand(command, (err, response) => { | |||
if (err) return callback(err, false); | |||
authProcess.transition(null, err => { | |||
if (err) return callback(err, null); | |||
callback(null, response); | |||
}); | |||
}); | |||
}); | |||
}); | |||
}); | |||
}); | |||
}); | |||
}); | |||
} | |||
// Add to store only if it does not exist | |||
var addAuthSession = function(authStore, session) { | |||
var found = false; | |||
for (var i = 0; i < authStore.length; i++) { | |||
if (authStore[i].equal(session)) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) authStore.push(session); | |||
}; | |||
/** | |||
* Remove authStore credentials | |||
* @method | |||
* @param {string} db Name of database we are removing authStore details about | |||
* @return {object} | |||
*/ | |||
SSPI.prototype.logout = function(dbName) { | |||
this.authStore = this.authStore.filter(function(x) { | |||
return x.db !== dbName; | |||
}); | |||
}; | |||
/** | |||
* Re authenticate pool | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
SSPI.prototype.reauthenticate = function(server, connections, callback) { | |||
var authStore = this.authStore.slice(0); | |||
var count = authStore.length; | |||
if (count === 0) return callback(null, null); | |||
// Iterate over all the auth details stored | |||
for (var i = 0; i < authStore.length; i++) { | |||
this.auth( | |||
server, | |||
connections, | |||
authStore[i].db, | |||
authStore[i].username, | |||
authStore[i].password, | |||
authStore[i].options, | |||
function(err) { | |||
count = count - 1; | |||
// Done re-authenticating | |||
if (count === 0) { | |||
callback(err, null); | |||
} | |||
} | |||
); | |||
} | |||
}; | |||
/** | |||
* This is a result from a authentication strategy | |||
* | |||
* @callback authResultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {boolean} result The result of the authentication process | |||
*/ | |||
module.exports = SSPI; |
@@ -0,0 +1,179 @@ | |||
'use strict'; | |||
var f = require('util').format, | |||
Query = require('../connection/commands').Query, | |||
MongoError = require('../error').MongoError; | |||
var AuthSession = function(db, username, password) { | |||
this.db = db; | |||
this.username = username; | |||
this.password = password; | |||
}; | |||
AuthSession.prototype.equal = function(session) { | |||
return ( | |||
session.db === this.db && | |||
session.username === this.username && | |||
session.password === this.password | |||
); | |||
}; | |||
/** | |||
* Creates a new X509 authentication mechanism | |||
* @class | |||
* @return {X509} A cursor instance | |||
*/ | |||
var X509 = function(bson) { | |||
this.bson = bson; | |||
this.authStore = []; | |||
}; | |||
/** | |||
* Authenticate | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {string} db Name of the database | |||
* @param {string} username Username | |||
* @param {string} password Password | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
X509.prototype.auth = function(server, connections, db, username, password, callback) { | |||
var self = this; | |||
// Total connections | |||
var count = connections.length; | |||
if (count === 0) return callback(null, null); | |||
// Valid connections | |||
var numberOfValidConnections = 0; | |||
var errorObject = null; | |||
// For each connection we need to authenticate | |||
while (connections.length > 0) { | |||
// Execute MongoCR | |||
var execute = function(connection) { | |||
// Let's start the sasl process | |||
var command = { | |||
authenticate: 1, | |||
mechanism: 'MONGODB-X509' | |||
}; | |||
// Add username if specified | |||
if (username) { | |||
command.user = username; | |||
} | |||
// Let's start the process | |||
server( | |||
connection, | |||
new Query(self.bson, '$external.$cmd', command, { | |||
numberToSkip: 0, | |||
numberToReturn: 1 | |||
}), | |||
function(err, r) { | |||
// Adjust count | |||
count = count - 1; | |||
// If we have an error | |||
if (err) { | |||
errorObject = err; | |||
} else if (r.result['$err']) { | |||
errorObject = r.result; | |||
} else if (r.result['errmsg']) { | |||
errorObject = r.result; | |||
} else { | |||
numberOfValidConnections = numberOfValidConnections + 1; | |||
} | |||
// We have authenticated all connections | |||
if (count === 0 && numberOfValidConnections > 0) { | |||
// Store the auth details | |||
addAuthSession(self.authStore, new AuthSession(db, username, password)); | |||
// Return correct authentication | |||
callback(null, true); | |||
} else if (count === 0) { | |||
if (errorObject == null) | |||
errorObject = new MongoError(f('failed to authenticate using mongocr')); | |||
callback(errorObject, false); | |||
} | |||
} | |||
); | |||
}; | |||
var _execute = function(_connection) { | |||
process.nextTick(function() { | |||
execute(_connection); | |||
}); | |||
}; | |||
_execute(connections.shift()); | |||
} | |||
}; | |||
// Add to store only if it does not exist | |||
var addAuthSession = function(authStore, session) { | |||
var found = false; | |||
for (var i = 0; i < authStore.length; i++) { | |||
if (authStore[i].equal(session)) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) authStore.push(session); | |||
}; | |||
/** | |||
* Remove authStore credentials | |||
* @method | |||
* @param {string} db Name of database we are removing authStore details about | |||
* @return {object} | |||
*/ | |||
X509.prototype.logout = function(dbName) { | |||
this.authStore = this.authStore.filter(function(x) { | |||
return x.db !== dbName; | |||
}); | |||
}; | |||
/** | |||
* Re authenticate pool | |||
* @method | |||
* @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on | |||
* @param {[]Connections} connections Connections to authenticate using this authenticator | |||
* @param {authResultCallback} callback The callback to return the result from the authentication | |||
* @return {object} | |||
*/ | |||
X509.prototype.reauthenticate = function(server, connections, callback) { | |||
var authStore = this.authStore.slice(0); | |||
var count = authStore.length; | |||
if (count === 0) return callback(null, null); | |||
// Iterate over all the auth details stored | |||
for (var i = 0; i < authStore.length; i++) { | |||
this.auth( | |||
server, | |||
connections, | |||
authStore[i].db, | |||
authStore[i].username, | |||
authStore[i].password, | |||
function(err) { | |||
count = count - 1; | |||
// Done re-authenticating | |||
if (count === 0) { | |||
callback(err, null); | |||
} | |||
} | |||
); | |||
} | |||
}; | |||
/** | |||
* This is a result from a authentication strategy | |||
* | |||
* @callback authResultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {boolean} result The result of the authentication process | |||
*/ | |||
module.exports = X509; |
@@ -0,0 +1,228 @@ | |||
'use strict'; | |||
const KillCursor = require('../connection/commands').KillCursor; | |||
const GetMore = require('../connection/commands').GetMore; | |||
const calculateDurationInMs = require('../utils').calculateDurationInMs; | |||
/** Commands that we want to redact because of the sensitive nature of their contents */ | |||
const SENSITIVE_COMMANDS = new Set([ | |||
'authenticate', | |||
'saslStart', | |||
'saslContinue', | |||
'getnonce', | |||
'createUser', | |||
'updateUser', | |||
'copydbgetnonce', | |||
'copydbsaslstart', | |||
'copydb' | |||
]); | |||
// helper methods | |||
const extractCommandName = command => Object.keys(command)[0]; | |||
const namespace = command => command.ns; | |||
const databaseName = command => command.ns.split('.')[0]; | |||
const collectionName = command => command.ns.split('.')[1]; | |||
const generateConnectionId = pool => `${pool.options.host}:${pool.options.port}`; | |||
const maybeRedact = (commandName, result) => (SENSITIVE_COMMANDS.has(commandName) ? {} : result); | |||
const LEGACY_FIND_QUERY_MAP = { | |||
$query: 'filter', | |||
$orderby: 'sort', | |||
$hint: 'hint', | |||
$comment: 'comment', | |||
$maxScan: 'maxScan', | |||
$max: 'max', | |||
$min: 'min', | |||
$returnKey: 'returnKey', | |||
$showDiskLoc: 'showRecordId', | |||
$maxTimeMS: 'maxTimeMS', | |||
$snapshot: 'snapshot' | |||
}; | |||
const LEGACY_FIND_OPTIONS_MAP = { | |||
numberToSkip: 'skip', | |||
numberToReturn: 'batchSize', | |||
returnFieldsSelector: 'projection' | |||
}; | |||
const OP_QUERY_KEYS = [ | |||
'tailable', | |||
'oplogReplay', | |||
'noCursorTimeout', | |||
'awaitData', | |||
'partial', | |||
'exhaust' | |||
]; | |||
/** | |||
* Extract the actual command from the query, possibly upconverting if it's a legacy | |||
* format | |||
* | |||
* @param {Object} command the command | |||
*/ | |||
const extractCommand = command => { | |||
if (command instanceof GetMore) { | |||
return { | |||
getMore: command.cursorId, | |||
collection: collectionName(command), | |||
batchSize: command.numberToReturn | |||
}; | |||
} | |||
if (command instanceof KillCursor) { | |||
return { | |||
killCursors: collectionName(command), | |||
cursors: command.cursorIds | |||
}; | |||
} | |||
if (command.query && command.query.$query) { | |||
let result; | |||
if (command.ns === 'admin.$cmd') { | |||
// upconvert legacy command | |||
result = Object.assign({}, command.query.$query); | |||
} else { | |||
// upconvert legacy find command | |||
result = { find: collectionName(command) }; | |||
Object.keys(LEGACY_FIND_QUERY_MAP).forEach(key => { | |||
if (typeof command.query[key] !== 'undefined') | |||
result[LEGACY_FIND_QUERY_MAP[key]] = command.query[key]; | |||
}); | |||
} | |||
Object.keys(LEGACY_FIND_OPTIONS_MAP).forEach(key => { | |||
if (typeof command[key] !== 'undefined') result[LEGACY_FIND_OPTIONS_MAP[key]] = command[key]; | |||
}); | |||
OP_QUERY_KEYS.forEach(key => { | |||
if (command[key]) result[key] = command[key]; | |||
}); | |||
if (typeof command.pre32Limit !== 'undefined') { | |||
result.limit = command.pre32Limit; | |||
} | |||
if (command.query.$explain) { | |||
return { explain: result }; | |||
} | |||
return result; | |||
} | |||
return command.query ? command.query : command; | |||
}; | |||
const extractReply = (command, reply) => { | |||
if (command instanceof GetMore) { | |||
return { | |||
ok: 1, | |||
cursor: { | |||
id: reply.message.cursorId, | |||
ns: namespace(command), | |||
nextBatch: reply.message.documents | |||
} | |||
}; | |||
} | |||
if (command instanceof KillCursor) { | |||
return { | |||
ok: 1, | |||
cursorsUnknown: command.cursorIds | |||
}; | |||
} | |||
// is this a legacy find command? | |||
if (command.query && typeof command.query.$query !== 'undefined') { | |||
return { | |||
ok: 1, | |||
cursor: { | |||
id: reply.message.cursorId, | |||
ns: namespace(command), | |||
firstBatch: reply.message.documents | |||
} | |||
}; | |||
} | |||
return reply.result; | |||
}; | |||
/** An event indicating the start of a given command */ | |||
class CommandStartedEvent { | |||
/** | |||
* Create a started event | |||
* | |||
* @param {Pool} pool the pool that originated the command | |||
* @param {Object} command the command | |||
*/ | |||
constructor(pool, command) { | |||
const cmd = extractCommand(command); | |||
const commandName = extractCommandName(cmd); | |||
// NOTE: remove in major revision, this is not spec behavior | |||
if (SENSITIVE_COMMANDS.has(commandName)) { | |||
this.commandObj = {}; | |||
this.commandObj[commandName] = true; | |||
} | |||
Object.assign(this, { | |||
command: cmd, | |||
databaseName: databaseName(command), | |||
commandName, | |||
requestId: command.requestId, | |||
connectionId: generateConnectionId(pool) | |||
}); | |||
} | |||
} | |||
/** An event indicating the success of a given command */ | |||
class CommandSucceededEvent { | |||
/** | |||
* Create a succeeded event | |||
* | |||
* @param {Pool} pool the pool that originated the command | |||
* @param {Object} command the command | |||
* @param {Object} reply the reply for this command from the server | |||
* @param {Array} started a high resolution tuple timestamp of when the command was first sent, to calculate duration | |||
*/ | |||
constructor(pool, command, reply, started) { | |||
const cmd = extractCommand(command); | |||
const commandName = extractCommandName(cmd); | |||
Object.assign(this, { | |||
duration: calculateDurationInMs(started), | |||
commandName, | |||
reply: maybeRedact(commandName, extractReply(command, reply)), | |||
requestId: command.requestId, | |||
connectionId: generateConnectionId(pool) | |||
}); | |||
} | |||
} | |||
/** An event indicating the failure of a given command */ | |||
class CommandFailedEvent { | |||
/** | |||
* Create a failure event | |||
* | |||
* @param {Pool} pool the pool that originated the command | |||
* @param {Object} command the command | |||
* @param {MongoError|Object} error the generated error or a server error response | |||
* @param {Array} started a high resolution tuple timestamp of when the command was first sent, to calculate duration | |||
*/ | |||
constructor(pool, command, error, started) { | |||
const cmd = extractCommand(command); | |||
const commandName = extractCommandName(cmd); | |||
Object.assign(this, { | |||
duration: calculateDurationInMs(started), | |||
commandName, | |||
failure: maybeRedact(commandName, error), | |||
requestId: command.requestId, | |||
connectionId: generateConnectionId(pool) | |||
}); | |||
} | |||
} | |||
module.exports = { | |||
CommandStartedEvent, | |||
CommandSucceededEvent, | |||
CommandFailedEvent | |||
}; |
@@ -0,0 +1,34 @@ | |||
'use strict'; | |||
/** | |||
* Creates a new CommandResult instance | |||
* @class | |||
* @param {object} result CommandResult object | |||
* @param {Connection} connection A connection instance associated with this result | |||
* @return {CommandResult} A cursor instance | |||
*/ | |||
var CommandResult = function(result, connection, message) { | |||
this.result = result; | |||
this.connection = connection; | |||
this.message = message; | |||
}; | |||
/** | |||
* Convert CommandResult to JSON | |||
* @method | |||
* @return {object} | |||
*/ | |||
CommandResult.prototype.toJSON = function() { | |||
return this.result; | |||
}; | |||
/** | |||
* Convert CommandResult to String representation | |||
* @method | |||
* @return {string} | |||
*/ | |||
CommandResult.prototype.toString = function() { | |||
return JSON.stringify(this.toJSON()); | |||
}; | |||
module.exports = CommandResult; |
@@ -0,0 +1,546 @@ | |||
'use strict'; | |||
var retrieveBSON = require('./utils').retrieveBSON; | |||
var BSON = retrieveBSON(); | |||
var Long = BSON.Long; | |||
const MongoError = require('../error').MongoError; | |||
const Buffer = require('safe-buffer').Buffer; | |||
// Incrementing request id | |||
var _requestId = 0; | |||
// Wire command operation ids | |||
var opcodes = require('../wireprotocol/shared').opcodes; | |||
// Query flags | |||
var OPTS_TAILABLE_CURSOR = 2; | |||
var OPTS_SLAVE = 4; | |||
var OPTS_OPLOG_REPLAY = 8; | |||
var OPTS_NO_CURSOR_TIMEOUT = 16; | |||
var OPTS_AWAIT_DATA = 32; | |||
var OPTS_EXHAUST = 64; | |||
var OPTS_PARTIAL = 128; | |||
// Response flags | |||
var CURSOR_NOT_FOUND = 1; | |||
var QUERY_FAILURE = 2; | |||
var SHARD_CONFIG_STALE = 4; | |||
var AWAIT_CAPABLE = 8; | |||
/************************************************************** | |||
* QUERY | |||
**************************************************************/ | |||
var Query = function(bson, ns, query, options) { | |||
var self = this; | |||
// Basic options needed to be passed in | |||
if (ns == null) throw new Error('ns must be specified for query'); | |||
if (query == null) throw new Error('query must be specified for query'); | |||
// Validate that we are not passing 0x00 in the collection name | |||
if (ns.indexOf('\x00') !== -1) { | |||
throw new Error('namespace cannot contain a null character'); | |||
} | |||
// Basic options | |||
this.bson = bson; | |||
this.ns = ns; | |||
this.query = query; | |||
// Additional options | |||
this.numberToSkip = options.numberToSkip || 0; | |||
this.numberToReturn = options.numberToReturn || 0; | |||
this.returnFieldSelector = options.returnFieldSelector || null; | |||
this.requestId = Query.getRequestId(); | |||
// special case for pre-3.2 find commands, delete ASAP | |||
this.pre32Limit = options.pre32Limit; | |||
// Serialization option | |||
this.serializeFunctions = | |||
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; | |||
this.ignoreUndefined = | |||
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false; | |||
this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16; | |||
this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : true; | |||
this.batchSize = self.numberToReturn; | |||
// Flags | |||
this.tailable = false; | |||
this.slaveOk = typeof options.slaveOk === 'boolean' ? options.slaveOk : false; | |||
this.oplogReplay = false; | |||
this.noCursorTimeout = false; | |||
this.awaitData = false; | |||
this.exhaust = false; | |||
this.partial = false; | |||
}; | |||
// | |||
// Assign a new request Id | |||
Query.prototype.incRequestId = function() { | |||
this.requestId = _requestId++; | |||
}; | |||
// | |||
// Assign a new request Id | |||
Query.nextRequestId = function() { | |||
return _requestId + 1; | |||
}; | |||
// | |||
// Uses a single allocated buffer for the process, avoiding multiple memory allocations | |||
Query.prototype.toBin = function() { | |||
var self = this; | |||
var buffers = []; | |||
var projection = null; | |||
// Set up the flags | |||
var flags = 0; | |||
if (this.tailable) { | |||
flags |= OPTS_TAILABLE_CURSOR; | |||
} | |||
if (this.slaveOk) { | |||
flags |= OPTS_SLAVE; | |||
} | |||
if (this.oplogReplay) { | |||
flags |= OPTS_OPLOG_REPLAY; | |||
} | |||
if (this.noCursorTimeout) { | |||
flags |= OPTS_NO_CURSOR_TIMEOUT; | |||
} | |||
if (this.awaitData) { | |||
flags |= OPTS_AWAIT_DATA; | |||
} | |||
if (this.exhaust) { | |||
flags |= OPTS_EXHAUST; | |||
} | |||
if (this.partial) { | |||
flags |= OPTS_PARTIAL; | |||
} | |||
// If batchSize is different to self.numberToReturn | |||
if (self.batchSize !== self.numberToReturn) self.numberToReturn = self.batchSize; | |||
// Allocate write protocol header buffer | |||
var header = Buffer.alloc( | |||
4 * 4 + // Header | |||
4 + // Flags | |||
Buffer.byteLength(self.ns) + | |||
1 + // namespace | |||
4 + // numberToSkip | |||
4 // numberToReturn | |||
); | |||
// Add header to buffers | |||
buffers.push(header); | |||
// Serialize the query | |||
var query = self.bson.serialize(this.query, { | |||
checkKeys: this.checkKeys, | |||
serializeFunctions: this.serializeFunctions, | |||
ignoreUndefined: this.ignoreUndefined | |||
}); | |||
// Add query document | |||
buffers.push(query); | |||
if (self.returnFieldSelector && Object.keys(self.returnFieldSelector).length > 0) { | |||
// Serialize the projection document | |||
projection = self.bson.serialize(this.returnFieldSelector, { | |||
checkKeys: this.checkKeys, | |||
serializeFunctions: this.serializeFunctions, | |||
ignoreUndefined: this.ignoreUndefined | |||
}); | |||
// Add projection document | |||
buffers.push(projection); | |||
} | |||
// Total message size | |||
var totalLength = header.length + query.length + (projection ? projection.length : 0); | |||
// Set up the index | |||
var index = 4; | |||
// Write total document length | |||
header[3] = (totalLength >> 24) & 0xff; | |||
header[2] = (totalLength >> 16) & 0xff; | |||
header[1] = (totalLength >> 8) & 0xff; | |||
header[0] = totalLength & 0xff; | |||
// Write header information requestId | |||
header[index + 3] = (this.requestId >> 24) & 0xff; | |||
header[index + 2] = (this.requestId >> 16) & 0xff; | |||
header[index + 1] = (this.requestId >> 8) & 0xff; | |||
header[index] = this.requestId & 0xff; | |||
index = index + 4; | |||
// Write header information responseTo | |||
header[index + 3] = (0 >> 24) & 0xff; | |||
header[index + 2] = (0 >> 16) & 0xff; | |||
header[index + 1] = (0 >> 8) & 0xff; | |||
header[index] = 0 & 0xff; | |||
index = index + 4; | |||
// Write header information OP_QUERY | |||
header[index + 3] = (opcodes.OP_QUERY >> 24) & 0xff; | |||
header[index + 2] = (opcodes.OP_QUERY >> 16) & 0xff; | |||
header[index + 1] = (opcodes.OP_QUERY >> 8) & 0xff; | |||
header[index] = opcodes.OP_QUERY & 0xff; | |||
index = index + 4; | |||
// Write header information flags | |||
header[index + 3] = (flags >> 24) & 0xff; | |||
header[index + 2] = (flags >> 16) & 0xff; | |||
header[index + 1] = (flags >> 8) & 0xff; | |||
header[index] = flags & 0xff; | |||
index = index + 4; | |||
// Write collection name | |||
index = index + header.write(this.ns, index, 'utf8') + 1; | |||
header[index - 1] = 0; | |||
// Write header information flags numberToSkip | |||
header[index + 3] = (this.numberToSkip >> 24) & 0xff; | |||
header[index + 2] = (this.numberToSkip >> 16) & 0xff; | |||
header[index + 1] = (this.numberToSkip >> 8) & 0xff; | |||
header[index] = this.numberToSkip & 0xff; | |||
index = index + 4; | |||
// Write header information flags numberToReturn | |||
header[index + 3] = (this.numberToReturn >> 24) & 0xff; | |||
header[index + 2] = (this.numberToReturn >> 16) & 0xff; | |||
header[index + 1] = (this.numberToReturn >> 8) & 0xff; | |||
header[index] = this.numberToReturn & 0xff; | |||
index = index + 4; | |||
// Return the buffers | |||
return buffers; | |||
}; | |||
Query.getRequestId = function() { | |||
return ++_requestId; | |||
}; | |||
/************************************************************** | |||
* GETMORE | |||
**************************************************************/ | |||
var GetMore = function(bson, ns, cursorId, opts) { | |||
opts = opts || {}; | |||
this.numberToReturn = opts.numberToReturn || 0; | |||
this.requestId = _requestId++; | |||
this.bson = bson; | |||
this.ns = ns; | |||
this.cursorId = cursorId; | |||
}; | |||
// | |||
// Uses a single allocated buffer for the process, avoiding multiple memory allocations | |||
GetMore.prototype.toBin = function() { | |||
var length = 4 + Buffer.byteLength(this.ns) + 1 + 4 + 8 + 4 * 4; | |||
// Create command buffer | |||
var index = 0; | |||
// Allocate buffer | |||
var _buffer = Buffer.alloc(length); | |||
// Write header information | |||
// index = write32bit(index, _buffer, length); | |||
_buffer[index + 3] = (length >> 24) & 0xff; | |||
_buffer[index + 2] = (length >> 16) & 0xff; | |||
_buffer[index + 1] = (length >> 8) & 0xff; | |||
_buffer[index] = length & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, requestId); | |||
_buffer[index + 3] = (this.requestId >> 24) & 0xff; | |||
_buffer[index + 2] = (this.requestId >> 16) & 0xff; | |||
_buffer[index + 1] = (this.requestId >> 8) & 0xff; | |||
_buffer[index] = this.requestId & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, 0); | |||
_buffer[index + 3] = (0 >> 24) & 0xff; | |||
_buffer[index + 2] = (0 >> 16) & 0xff; | |||
_buffer[index + 1] = (0 >> 8) & 0xff; | |||
_buffer[index] = 0 & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, OP_GETMORE); | |||
_buffer[index + 3] = (opcodes.OP_GETMORE >> 24) & 0xff; | |||
_buffer[index + 2] = (opcodes.OP_GETMORE >> 16) & 0xff; | |||
_buffer[index + 1] = (opcodes.OP_GETMORE >> 8) & 0xff; | |||
_buffer[index] = opcodes.OP_GETMORE & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, 0); | |||
_buffer[index + 3] = (0 >> 24) & 0xff; | |||
_buffer[index + 2] = (0 >> 16) & 0xff; | |||
_buffer[index + 1] = (0 >> 8) & 0xff; | |||
_buffer[index] = 0 & 0xff; | |||
index = index + 4; | |||
// Write collection name | |||
index = index + _buffer.write(this.ns, index, 'utf8') + 1; | |||
_buffer[index - 1] = 0; | |||
// Write batch size | |||
// index = write32bit(index, _buffer, numberToReturn); | |||
_buffer[index + 3] = (this.numberToReturn >> 24) & 0xff; | |||
_buffer[index + 2] = (this.numberToReturn >> 16) & 0xff; | |||
_buffer[index + 1] = (this.numberToReturn >> 8) & 0xff; | |||
_buffer[index] = this.numberToReturn & 0xff; | |||
index = index + 4; | |||
// Write cursor id | |||
// index = write32bit(index, _buffer, cursorId.getLowBits()); | |||
_buffer[index + 3] = (this.cursorId.getLowBits() >> 24) & 0xff; | |||
_buffer[index + 2] = (this.cursorId.getLowBits() >> 16) & 0xff; | |||
_buffer[index + 1] = (this.cursorId.getLowBits() >> 8) & 0xff; | |||
_buffer[index] = this.cursorId.getLowBits() & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, cursorId.getHighBits()); | |||
_buffer[index + 3] = (this.cursorId.getHighBits() >> 24) & 0xff; | |||
_buffer[index + 2] = (this.cursorId.getHighBits() >> 16) & 0xff; | |||
_buffer[index + 1] = (this.cursorId.getHighBits() >> 8) & 0xff; | |||
_buffer[index] = this.cursorId.getHighBits() & 0xff; | |||
index = index + 4; | |||
// Return buffer | |||
return _buffer; | |||
}; | |||
/************************************************************** | |||
* KILLCURSOR | |||
**************************************************************/ | |||
var KillCursor = function(bson, ns, cursorIds) { | |||
this.ns = ns; | |||
this.requestId = _requestId++; | |||
this.cursorIds = cursorIds; | |||
}; | |||
// | |||
// Uses a single allocated buffer for the process, avoiding multiple memory allocations | |||
KillCursor.prototype.toBin = function() { | |||
var length = 4 + 4 + 4 * 4 + this.cursorIds.length * 8; | |||
// Create command buffer | |||
var index = 0; | |||
var _buffer = Buffer.alloc(length); | |||
// Write header information | |||
// index = write32bit(index, _buffer, length); | |||
_buffer[index + 3] = (length >> 24) & 0xff; | |||
_buffer[index + 2] = (length >> 16) & 0xff; | |||
_buffer[index + 1] = (length >> 8) & 0xff; | |||
_buffer[index] = length & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, requestId); | |||
_buffer[index + 3] = (this.requestId >> 24) & 0xff; | |||
_buffer[index + 2] = (this.requestId >> 16) & 0xff; | |||
_buffer[index + 1] = (this.requestId >> 8) & 0xff; | |||
_buffer[index] = this.requestId & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, 0); | |||
_buffer[index + 3] = (0 >> 24) & 0xff; | |||
_buffer[index + 2] = (0 >> 16) & 0xff; | |||
_buffer[index + 1] = (0 >> 8) & 0xff; | |||
_buffer[index] = 0 & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, OP_KILL_CURSORS); | |||
_buffer[index + 3] = (opcodes.OP_KILL_CURSORS >> 24) & 0xff; | |||
_buffer[index + 2] = (opcodes.OP_KILL_CURSORS >> 16) & 0xff; | |||
_buffer[index + 1] = (opcodes.OP_KILL_CURSORS >> 8) & 0xff; | |||
_buffer[index] = opcodes.OP_KILL_CURSORS & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, 0); | |||
_buffer[index + 3] = (0 >> 24) & 0xff; | |||
_buffer[index + 2] = (0 >> 16) & 0xff; | |||
_buffer[index + 1] = (0 >> 8) & 0xff; | |||
_buffer[index] = 0 & 0xff; | |||
index = index + 4; | |||
// Write batch size | |||
// index = write32bit(index, _buffer, this.cursorIds.length); | |||
_buffer[index + 3] = (this.cursorIds.length >> 24) & 0xff; | |||
_buffer[index + 2] = (this.cursorIds.length >> 16) & 0xff; | |||
_buffer[index + 1] = (this.cursorIds.length >> 8) & 0xff; | |||
_buffer[index] = this.cursorIds.length & 0xff; | |||
index = index + 4; | |||
// Write all the cursor ids into the array | |||
for (var i = 0; i < this.cursorIds.length; i++) { | |||
// Write cursor id | |||
// index = write32bit(index, _buffer, cursorIds[i].getLowBits()); | |||
_buffer[index + 3] = (this.cursorIds[i].getLowBits() >> 24) & 0xff; | |||
_buffer[index + 2] = (this.cursorIds[i].getLowBits() >> 16) & 0xff; | |||
_buffer[index + 1] = (this.cursorIds[i].getLowBits() >> 8) & 0xff; | |||
_buffer[index] = this.cursorIds[i].getLowBits() & 0xff; | |||
index = index + 4; | |||
// index = write32bit(index, _buffer, cursorIds[i].getHighBits()); | |||
_buffer[index + 3] = (this.cursorIds[i].getHighBits() >> 24) & 0xff; | |||
_buffer[index + 2] = (this.cursorIds[i].getHighBits() >> 16) & 0xff; | |||
_buffer[index + 1] = (this.cursorIds[i].getHighBits() >> 8) & 0xff; | |||
_buffer[index] = this.cursorIds[i].getHighBits() & 0xff; | |||
index = index + 4; | |||
} | |||
// Return buffer | |||
return _buffer; | |||
}; | |||
var Response = function(bson, message, msgHeader, msgBody, opts) { | |||
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false }; | |||
this.parsed = false; | |||
this.raw = message; | |||
this.data = msgBody; | |||
this.bson = bson; | |||
this.opts = opts; | |||
// Read the message header | |||
this.length = msgHeader.length; | |||
this.requestId = msgHeader.requestId; | |||
this.responseTo = msgHeader.responseTo; | |||
this.opCode = msgHeader.opCode; | |||
this.fromCompressed = msgHeader.fromCompressed; | |||
// Read the message body | |||
this.responseFlags = msgBody.readInt32LE(0); | |||
this.cursorId = new Long(msgBody.readInt32LE(4), msgBody.readInt32LE(8)); | |||
this.startingFrom = msgBody.readInt32LE(12); | |||
this.numberReturned = msgBody.readInt32LE(16); | |||
// Preallocate document array | |||
this.documents = new Array(this.numberReturned); | |||
// Flag values | |||
this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0; | |||
this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0; | |||
this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0; | |||
this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0; | |||
this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true; | |||
this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true; | |||
this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false; | |||
}; | |||
Response.prototype.isParsed = function() { | |||
return this.parsed; | |||
}; | |||
Response.prototype.parse = function(options) { | |||
// Don't parse again if not needed | |||
if (this.parsed) return; | |||
options = options || {}; | |||
// Allow the return of raw documents instead of parsing | |||
var raw = options.raw || false; | |||
var documentsReturnedIn = options.documentsReturnedIn || null; | |||
var promoteLongs = | |||
typeof options.promoteLongs === 'boolean' ? options.promoteLongs : this.opts.promoteLongs; | |||
var promoteValues = | |||
typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues; | |||
var promoteBuffers = | |||
typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : this.opts.promoteBuffers; | |||
var bsonSize, _options; | |||
// Set up the options | |||
_options = { | |||
promoteLongs: promoteLongs, | |||
promoteValues: promoteValues, | |||
promoteBuffers: promoteBuffers | |||
}; | |||
// Position within OP_REPLY at which documents start | |||
// (See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-reply) | |||
this.index = 20; | |||
// | |||
// Single document and documentsReturnedIn set | |||
// | |||
if (this.numberReturned === 1 && documentsReturnedIn != null && raw) { | |||
// Calculate the bson size | |||
bsonSize = | |||
this.data[this.index] | | |||
(this.data[this.index + 1] << 8) | | |||
(this.data[this.index + 2] << 16) | | |||
(this.data[this.index + 3] << 24); | |||
// Slice out the buffer containing the command result document | |||
var document = this.data.slice(this.index, this.index + bsonSize); | |||
// Set up field we wish to keep as raw | |||
var fieldsAsRaw = {}; | |||
fieldsAsRaw[documentsReturnedIn] = true; | |||
_options.fieldsAsRaw = fieldsAsRaw; | |||
// Deserialize but keep the array of documents in non-parsed form | |||
var doc = this.bson.deserialize(document, _options); | |||
if (doc instanceof Error) { | |||
throw doc; | |||
} | |||
if (doc.errmsg) { | |||
throw new MongoError(doc.errmsg); | |||
} | |||
if (!doc.cursor) { | |||
throw new MongoError('Cursor not found'); | |||
} | |||
// Get the documents | |||
this.documents = doc.cursor[documentsReturnedIn]; | |||
this.numberReturned = this.documents.length; | |||
// Ensure we have a Long valie cursor id | |||
this.cursorId = | |||
typeof doc.cursor.id === 'number' ? Long.fromNumber(doc.cursor.id) : doc.cursor.id; | |||
// Adjust the index | |||
this.index = this.index + bsonSize; | |||
// Set as parsed | |||
this.parsed = true; | |||
return; | |||
} | |||
// | |||
// Parse Body | |||
// | |||
for (var i = 0; i < this.numberReturned; i++) { | |||
bsonSize = | |||
this.data[this.index] | | |||
(this.data[this.index + 1] << 8) | | |||
(this.data[this.index + 2] << 16) | | |||
(this.data[this.index + 3] << 24); | |||
// If we have raw results specified slice the return document | |||
if (raw) { | |||
this.documents[i] = this.data.slice(this.index, this.index + bsonSize); | |||
} else { | |||
this.documents[i] = this.bson.deserialize( | |||
this.data.slice(this.index, this.index + bsonSize), | |||
_options | |||
); | |||
} | |||
// Adjust the index | |||
this.index = this.index + bsonSize; | |||
} | |||
// Set parsed | |||
this.parsed = true; | |||
}; | |||
module.exports = { | |||
Query: Query, | |||
GetMore: GetMore, | |||
Response: Response, | |||
KillCursor: KillCursor | |||
}; |
@@ -0,0 +1,805 @@ | |||
'use strict'; | |||
var inherits = require('util').inherits, | |||
EventEmitter = require('events').EventEmitter, | |||
net = require('net'), | |||
tls = require('tls'), | |||
crypto = require('crypto'), | |||
f = require('util').format, | |||
debugOptions = require('./utils').debugOptions, | |||
parseHeader = require('../wireprotocol/shared').parseHeader, | |||
decompress = require('../wireprotocol/compression').decompress, | |||
Response = require('./commands').Response, | |||
MongoNetworkError = require('../error').MongoNetworkError, | |||
Logger = require('./logger'), | |||
OP_COMPRESSED = require('../wireprotocol/shared').opcodes.OP_COMPRESSED, | |||
MESSAGE_HEADER_SIZE = require('../wireprotocol/shared').MESSAGE_HEADER_SIZE, | |||
Buffer = require('safe-buffer').Buffer; | |||
var _id = 0; | |||
var debugFields = [ | |||
'host', | |||
'port', | |||
'size', | |||
'keepAlive', | |||
'keepAliveInitialDelay', | |||
'noDelay', | |||
'connectionTimeout', | |||
'socketTimeout', | |||
'singleBufferSerializtion', | |||
'ssl', | |||
'ca', | |||
'crl', | |||
'cert', | |||
'rejectUnauthorized', | |||
'promoteLongs', | |||
'promoteValues', | |||
'promoteBuffers', | |||
'checkServerIdentity' | |||
]; | |||
var connectionAccountingSpy = undefined; | |||
var connectionAccounting = false; | |||
var connections = {}; | |||
/** | |||
* Creates a new Connection instance | |||
* @class | |||
* @param {string} options.host The server host | |||
* @param {number} options.port The server port | |||
* @param {number} [options.family=null] IP version for DNS lookup, passed down to Node's [`dns.lookup()` function](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback). If set to `6`, will only look for ipv6 addresses. | |||
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled | |||
* @param {number} [options.keepAliveInitialDelay=300000] Initial delay before TCP keep alive enabled | |||
* @param {boolean} [options.noDelay=true] TCP Connection no delay | |||
* @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting | |||
* @param {number} [options.socketTimeout=360000] TCP Socket timeout setting | |||
* @param {boolean} [options.singleBufferSerializtion=true] Serialize into single buffer, trade of peak memory for serialization speed | |||
* @param {boolean} [options.ssl=false] Use SSL for connection | |||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function. | |||
* @param {Buffer} [options.ca] SSL Certificate store binary buffer | |||
* @param {Buffer} [options.crl] SSL Certificate revocation store binary buffer | |||
* @param {Buffer} [options.cert] SSL Certificate binary buffer | |||
* @param {Buffer} [options.key] SSL Key file binary buffer | |||
* @param {string} [options.passphrase] SSL Certificate pass phrase | |||
* @param {boolean} [options.rejectUnauthorized=true] Reject unauthorized server certificates | |||
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits | |||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types. | |||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers. | |||
* @fires Connection#connect | |||
* @fires Connection#close | |||
* @fires Connection#error | |||
* @fires Connection#timeout | |||
* @fires Connection#parseError | |||
* @return {Connection} A cursor instance | |||
*/ | |||
var Connection = function(messageHandler, options) { | |||
// Add event listener | |||
EventEmitter.call(this); | |||
// Set empty if no options passed | |||
this.options = options || {}; | |||
// Identification information | |||
this.id = _id++; | |||
// Logger instance | |||
this.logger = Logger('Connection', options); | |||
// No bson parser passed in | |||
if (!options.bson) throw new Error('must pass in valid bson parser'); | |||
// Get bson parser | |||
this.bson = options.bson; | |||
// Grouping tag used for debugging purposes | |||
this.tag = options.tag; | |||
// Message handler | |||
this.messageHandler = messageHandler; | |||
// Max BSON message size | |||
this.maxBsonMessageSize = options.maxBsonMessageSize || 1024 * 1024 * 16 * 4; | |||
// Debug information | |||
if (this.logger.isDebug()) | |||
this.logger.debug( | |||
f( | |||
'creating connection %s with options [%s]', | |||
this.id, | |||
JSON.stringify(debugOptions(debugFields, options)) | |||
) | |||
); | |||
// Default options | |||
this.port = options.port || 27017; | |||
this.host = options.host || 'localhost'; | |||
this.family = typeof options.family === 'number' ? options.family : void 0; | |||
this.keepAlive = typeof options.keepAlive === 'boolean' ? options.keepAlive : true; | |||
this.keepAliveInitialDelay = | |||
typeof options.keepAliveInitialDelay === 'number' ? options.keepAliveInitialDelay : 300000; | |||
this.noDelay = typeof options.noDelay === 'boolean' ? options.noDelay : true; | |||
this.connectionTimeout = | |||
typeof options.connectionTimeout === 'number' ? options.connectionTimeout : 30000; | |||
this.socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000; | |||
// Is the keepAliveInitialDelay > socketTimeout set it to half of socketTimeout | |||
if (this.keepAliveInitialDelay > this.socketTimeout) { | |||
this.keepAliveInitialDelay = Math.round(this.socketTimeout / 2); | |||
} | |||
// If connection was destroyed | |||
this.destroyed = false; | |||
// Check if we have a domain socket | |||
this.domainSocket = this.host.indexOf('/') !== -1; | |||
// Serialize commands using function | |||
this.singleBufferSerializtion = | |||
typeof options.singleBufferSerializtion === 'boolean' ? options.singleBufferSerializtion : true; | |||
this.serializationFunction = this.singleBufferSerializtion ? 'toBinUnified' : 'toBin'; | |||
// SSL options | |||
this.ca = options.ca || null; | |||
this.crl = options.crl || null; | |||
this.cert = options.cert || null; | |||
this.key = options.key || null; | |||
this.passphrase = options.passphrase || null; | |||
this.ciphers = options.ciphers || null; | |||
this.ecdhCurve = options.ecdhCurve || null; | |||
this.ssl = typeof options.ssl === 'boolean' ? options.ssl : false; | |||
this.rejectUnauthorized = | |||
typeof options.rejectUnauthorized === 'boolean' ? options.rejectUnauthorized : true; | |||
this.checkServerIdentity = | |||
typeof options.checkServerIdentity === 'boolean' || | |||
typeof options.checkServerIdentity === 'function' | |||
? options.checkServerIdentity | |||
: true; | |||
// If ssl not enabled | |||
if (!this.ssl) this.rejectUnauthorized = false; | |||
// Response options | |||
this.responseOptions = { | |||
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true, | |||
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true, | |||
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false | |||
}; | |||
// Flushing | |||
this.flushing = false; | |||
this.queue = []; | |||
// Internal state | |||
this.connection = null; | |||
this.writeStream = null; | |||
// Create hash method | |||
var hash = crypto.createHash('sha1'); | |||
hash.update(f('%s:%s', this.host, this.port)); | |||
// Create a hash name | |||
this.hashedName = hash.digest('hex'); | |||
// All operations in flight on the connection | |||
this.workItems = []; | |||
}; | |||
inherits(Connection, EventEmitter); | |||
Connection.prototype.setSocketTimeout = function(value) { | |||
if (this.connection) { | |||
this.connection.setTimeout(value); | |||
} | |||
}; | |||
Connection.prototype.resetSocketTimeout = function() { | |||
if (this.connection) { | |||
this.connection.setTimeout(this.socketTimeout); | |||
} | |||
}; | |||
Connection.enableConnectionAccounting = function(spy) { | |||
if (spy) { | |||
connectionAccountingSpy = spy; | |||
} | |||
connectionAccounting = true; | |||
connections = {}; | |||
}; | |||
Connection.disableConnectionAccounting = function() { | |||
connectionAccounting = false; | |||
connectionAccountingSpy = undefined; | |||
}; | |||
Connection.connections = function() { | |||
return connections; | |||
}; | |||
function deleteConnection(id) { | |||
// console.log("=== deleted connection " + id + " :: " + (connections[id] ? connections[id].port : '')) | |||
delete connections[id]; | |||
if (connectionAccountingSpy) { | |||
connectionAccountingSpy.deleteConnection(id); | |||
} | |||
} | |||
function addConnection(id, connection) { | |||
// console.log("=== added connection " + id + " :: " + connection.port) | |||
connections[id] = connection; | |||
if (connectionAccountingSpy) { | |||
connectionAccountingSpy.addConnection(id, connection); | |||
} | |||
} | |||
// | |||
// Connection handlers | |||
var errorHandler = function(self) { | |||
return function(err) { | |||
if (connectionAccounting) deleteConnection(self.id); | |||
// Debug information | |||
if (self.logger.isDebug()) | |||
self.logger.debug( | |||
f( | |||
'connection %s for [%s:%s] errored out with [%s]', | |||
self.id, | |||
self.host, | |||
self.port, | |||
JSON.stringify(err) | |||
) | |||
); | |||
// Emit the error | |||
if (self.listeners('error').length > 0) self.emit('error', new MongoNetworkError(err), self); | |||
}; | |||
}; | |||
var timeoutHandler = function(self) { | |||
return function() { | |||
if (connectionAccounting) deleteConnection(self.id); | |||
// Debug information | |||
if (self.logger.isDebug()) | |||
self.logger.debug(f('connection %s for [%s:%s] timed out', self.id, self.host, self.port)); | |||
// Emit timeout error | |||
self.emit( | |||
'timeout', | |||
new MongoNetworkError(f('connection %s to %s:%s timed out', self.id, self.host, self.port)), | |||
self | |||
); | |||
}; | |||
}; | |||
var closeHandler = function(self) { | |||
return function(hadError) { | |||
if (connectionAccounting) deleteConnection(self.id); | |||
// Debug information | |||
if (self.logger.isDebug()) | |||
self.logger.debug(f('connection %s with for [%s:%s] closed', self.id, self.host, self.port)); | |||
// Emit close event | |||
if (!hadError) { | |||
self.emit( | |||
'close', | |||
new MongoNetworkError(f('connection %s to %s:%s closed', self.id, self.host, self.port)), | |||
self | |||
); | |||
} | |||
}; | |||
}; | |||
// Handle a message once it is received | |||
var emitMessageHandler = function(self, message) { | |||
var msgHeader = parseHeader(message); | |||
if (msgHeader.opCode === OP_COMPRESSED) { | |||
msgHeader.fromCompressed = true; | |||
var index = MESSAGE_HEADER_SIZE; | |||
msgHeader.opCode = message.readInt32LE(index); | |||
index += 4; | |||
msgHeader.length = message.readInt32LE(index); | |||
index += 4; | |||
var compressorID = message[index]; | |||
index++; | |||
decompress(compressorID, message.slice(index), function(err, decompressedMsgBody) { | |||
if (err) { | |||
throw err; | |||
} | |||
if (decompressedMsgBody.length !== msgHeader.length) { | |||
throw new Error( | |||
'Decompressing a compressed message from the server failed. The message is corrupt.' | |||
); | |||
} | |||
self.messageHandler( | |||
new Response(self.bson, message, msgHeader, decompressedMsgBody, self.responseOptions), | |||
self | |||
); | |||
}); | |||
} else { | |||
self.messageHandler( | |||
new Response( | |||
self.bson, | |||
message, | |||
msgHeader, | |||
message.slice(MESSAGE_HEADER_SIZE), | |||
self.responseOptions | |||
), | |||
self | |||
); | |||
} | |||
}; | |||
var dataHandler = function(self) { | |||
return function(data) { | |||
// Parse until we are done with the data | |||
while (data.length > 0) { | |||
// If we still have bytes to read on the current message | |||
if (self.bytesRead > 0 && self.sizeOfMessage > 0) { | |||
// Calculate the amount of remaining bytes | |||
var remainingBytesToRead = self.sizeOfMessage - self.bytesRead; | |||
// Check if the current chunk contains the rest of the message | |||
if (remainingBytesToRead > data.length) { | |||
// Copy the new data into the exiting buffer (should have been allocated when we know the message size) | |||
data.copy(self.buffer, self.bytesRead); | |||
// Adjust the number of bytes read so it point to the correct index in the buffer | |||
self.bytesRead = self.bytesRead + data.length; | |||
// Reset state of buffer | |||
data = Buffer.alloc(0); | |||
} else { | |||
// Copy the missing part of the data into our current buffer | |||
data.copy(self.buffer, self.bytesRead, 0, remainingBytesToRead); | |||
// Slice the overflow into a new buffer that we will then re-parse | |||
data = data.slice(remainingBytesToRead); | |||
// Emit current complete message | |||
try { | |||
var emitBuffer = self.buffer; | |||
// Reset state of buffer | |||
self.buffer = null; | |||
self.sizeOfMessage = 0; | |||
self.bytesRead = 0; | |||
self.stubBuffer = null; | |||
emitMessageHandler(self, emitBuffer); | |||
} catch (err) { | |||
var errorObject = { | |||
err: 'socketHandler', | |||
trace: err, | |||
bin: self.buffer, | |||
parseState: { | |||
sizeOfMessage: self.sizeOfMessage, | |||
bytesRead: self.bytesRead, | |||
stubBuffer: self.stubBuffer | |||
} | |||
}; | |||
// We got a parse Error fire it off then keep going | |||
self.emit('parseError', errorObject, self); | |||
} | |||
} | |||
} else { | |||
// Stub buffer is kept in case we don't get enough bytes to determine the | |||
// size of the message (< 4 bytes) | |||
if (self.stubBuffer != null && self.stubBuffer.length > 0) { | |||
// If we have enough bytes to determine the message size let's do it | |||
if (self.stubBuffer.length + data.length > 4) { | |||
// Prepad the data | |||
var newData = Buffer.alloc(self.stubBuffer.length + data.length); | |||
self.stubBuffer.copy(newData, 0); | |||
data.copy(newData, self.stubBuffer.length); | |||
// Reassign for parsing | |||
data = newData; | |||
// Reset state of buffer | |||
self.buffer = null; | |||
self.sizeOfMessage = 0; | |||
self.bytesRead = 0; | |||
self.stubBuffer = null; | |||
} else { | |||
// Add the the bytes to the stub buffer | |||
var newStubBuffer = Buffer.alloc(self.stubBuffer.length + data.length); | |||
// Copy existing stub buffer | |||
self.stubBuffer.copy(newStubBuffer, 0); | |||
// Copy missing part of the data | |||
data.copy(newStubBuffer, self.stubBuffer.length); | |||
// Exit parsing loop | |||
data = Buffer.alloc(0); | |||
} | |||
} else { | |||
if (data.length > 4) { | |||
// Retrieve the message size | |||
// var sizeOfMessage = data.readUInt32LE(0); | |||
var sizeOfMessage = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); | |||
// If we have a negative sizeOfMessage emit error and return | |||
if (sizeOfMessage < 0 || sizeOfMessage > self.maxBsonMessageSize) { | |||
errorObject = { | |||
err: 'socketHandler', | |||
trace: '', | |||
bin: self.buffer, | |||
parseState: { | |||
sizeOfMessage: sizeOfMessage, | |||
bytesRead: self.bytesRead, | |||
stubBuffer: self.stubBuffer | |||
} | |||
}; | |||
// We got a parse Error fire it off then keep going | |||
self.emit('parseError', errorObject, self); | |||
return; | |||
} | |||
// Ensure that the size of message is larger than 0 and less than the max allowed | |||
if ( | |||
sizeOfMessage > 4 && | |||
sizeOfMessage < self.maxBsonMessageSize && | |||
sizeOfMessage > data.length | |||
) { | |||
self.buffer = Buffer.alloc(sizeOfMessage); | |||
// Copy all the data into the buffer | |||
data.copy(self.buffer, 0); | |||
// Update bytes read | |||
self.bytesRead = data.length; | |||
// Update sizeOfMessage | |||
self.sizeOfMessage = sizeOfMessage; | |||
// Ensure stub buffer is null | |||
self.stubBuffer = null; | |||
// Exit parsing loop | |||
data = Buffer.alloc(0); | |||
} else if ( | |||
sizeOfMessage > 4 && | |||
sizeOfMessage < self.maxBsonMessageSize && | |||
sizeOfMessage === data.length | |||
) { | |||
try { | |||
emitBuffer = data; | |||
// Reset state of buffer | |||
self.buffer = null; | |||
self.sizeOfMessage = 0; | |||
self.bytesRead = 0; | |||
self.stubBuffer = null; | |||
// Exit parsing loop | |||
data = Buffer.alloc(0); | |||
// Emit the message | |||
emitMessageHandler(self, emitBuffer); | |||
} catch (err) { | |||
self.emit('parseError', err, self); | |||
} | |||
} else if (sizeOfMessage <= 4 || sizeOfMessage > self.maxBsonMessageSize) { | |||
errorObject = { | |||
err: 'socketHandler', | |||
trace: null, | |||
bin: data, | |||
parseState: { | |||
sizeOfMessage: sizeOfMessage, | |||
bytesRead: 0, | |||
buffer: null, | |||
stubBuffer: null | |||
} | |||
}; | |||
// We got a parse Error fire it off then keep going | |||
self.emit('parseError', errorObject, self); | |||
// Clear out the state of the parser | |||
self.buffer = null; | |||
self.sizeOfMessage = 0; | |||
self.bytesRead = 0; | |||
self.stubBuffer = null; | |||
// Exit parsing loop | |||
data = Buffer.alloc(0); | |||
} else { | |||
emitBuffer = data.slice(0, sizeOfMessage); | |||
// Reset state of buffer | |||
self.buffer = null; | |||
self.sizeOfMessage = 0; | |||
self.bytesRead = 0; | |||
self.stubBuffer = null; | |||
// Copy rest of message | |||
data = data.slice(sizeOfMessage); | |||
// Emit the message | |||
emitMessageHandler(self, emitBuffer); | |||
} | |||
} else { | |||
// Create a buffer that contains the space for the non-complete message | |||
self.stubBuffer = Buffer.alloc(data.length); | |||
// Copy the data to the stub buffer | |||
data.copy(self.stubBuffer, 0); | |||
// Exit parsing loop | |||
data = Buffer.alloc(0); | |||
} | |||
} | |||
} | |||
} | |||
}; | |||
}; | |||
// List of socket level valid ssl options | |||
var legalSslSocketOptions = [ | |||
'pfx', | |||
'key', | |||
'passphrase', | |||
'cert', | |||
'ca', | |||
'ciphers', | |||
'NPNProtocols', | |||
'ALPNProtocols', | |||
'servername', | |||
'ecdhCurve', | |||
'secureProtocol', | |||
'secureContext', | |||
'session', | |||
'minDHSize' | |||
]; | |||
function merge(options1, options2) { | |||
// Merge in any allowed ssl options | |||
for (var name in options2) { | |||
if (options2[name] != null && legalSslSocketOptions.indexOf(name) !== -1) { | |||
options1[name] = options2[name]; | |||
} | |||
} | |||
} | |||
function makeSSLConnection(self, _options) { | |||
let sslOptions = { | |||
socket: self.connection, | |||
rejectUnauthorized: self.rejectUnauthorized | |||
}; | |||
// Merge in options | |||
merge(sslOptions, self.options); | |||
merge(sslOptions, _options); | |||
// Set options for ssl | |||
if (self.ca) sslOptions.ca = self.ca; | |||
if (self.crl) sslOptions.crl = self.crl; | |||
if (self.cert) sslOptions.cert = self.cert; | |||
if (self.key) sslOptions.key = self.key; | |||
if (self.passphrase) sslOptions.passphrase = self.passphrase; | |||
// Override checkServerIdentity behavior | |||
if (self.checkServerIdentity === false) { | |||
// Skip the identiy check by retuning undefined as per node documents | |||
// https://nodejs.org/api/tls.html#tls_tls_connect_options_callback | |||
sslOptions.checkServerIdentity = function() { | |||
return undefined; | |||
}; | |||
} else if (typeof self.checkServerIdentity === 'function') { | |||
sslOptions.checkServerIdentity = self.checkServerIdentity; | |||
} | |||
// Set default sni servername to be the same as host | |||
if (sslOptions.servername == null) { | |||
sslOptions.servername = self.host; | |||
} | |||
// Attempt SSL connection | |||
const connection = tls.connect(self.port, self.host, sslOptions, function() { | |||
// Error on auth or skip | |||
if (connection.authorizationError && self.rejectUnauthorized) { | |||
return self.emit('error', connection.authorizationError, self, { ssl: true }); | |||
} | |||
// Set socket timeout instead of connection timeout | |||
connection.setTimeout(self.socketTimeout); | |||
// We are done emit connect | |||
self.emit('connect', self); | |||
}); | |||
// Set the options for the connection | |||
connection.setKeepAlive(self.keepAlive, self.keepAliveInitialDelay); | |||
connection.setTimeout(self.connectionTimeout); | |||
connection.setNoDelay(self.noDelay); | |||
return connection; | |||
} | |||
function makeUnsecureConnection(self, family) { | |||
// Create new connection instance | |||
let connection_options; | |||
if (self.domainSocket) { | |||
connection_options = { path: self.host }; | |||
} else { | |||
connection_options = { port: self.port, host: self.host }; | |||
connection_options.family = family; | |||
} | |||
const connection = net.createConnection(connection_options); | |||
// Set the options for the connection | |||
connection.setKeepAlive(self.keepAlive, self.keepAliveInitialDelay); | |||
connection.setTimeout(self.connectionTimeout); | |||
connection.setNoDelay(self.noDelay); | |||
connection.once('connect', function() { | |||
// Set socket timeout instead of connection timeout | |||
connection.setTimeout(self.socketTimeout); | |||
// Emit connect event | |||
self.emit('connect', self); | |||
}); | |||
return connection; | |||
} | |||
function doConnect(self, family, _options, _errorHandler) { | |||
self.connection = self.ssl | |||
? makeSSLConnection(self, _options) | |||
: makeUnsecureConnection(self, family); | |||
// Add handlers for events | |||
self.connection.once('error', _errorHandler); | |||
self.connection.once('timeout', timeoutHandler(self)); | |||
self.connection.once('close', closeHandler(self)); | |||
self.connection.on('data', dataHandler(self)); | |||
} | |||
/** | |||
* Connect | |||
* @method | |||
*/ | |||
Connection.prototype.connect = function(_options) { | |||
_options = _options || {}; | |||
// Set the connections | |||
if (connectionAccounting) addConnection(this.id, this); | |||
// Check if we are overriding the promoteLongs | |||
if (typeof _options.promoteLongs === 'boolean') { | |||
this.responseOptions.promoteLongs = _options.promoteLongs; | |||
this.responseOptions.promoteValues = _options.promoteValues; | |||
this.responseOptions.promoteBuffers = _options.promoteBuffers; | |||
} | |||
const _errorHandler = errorHandler(this); | |||
if (this.family !== void 0) { | |||
return doConnect(this, this.family, _options, _errorHandler); | |||
} | |||
return doConnect(this, 6, _options, err => { | |||
if (this.logger.isDebug()) { | |||
this.logger.debug( | |||
f( | |||
'connection %s for [%s:%s] errored out with [%s]', | |||
this.id, | |||
this.host, | |||
this.port, | |||
JSON.stringify(err) | |||
) | |||
); | |||
} | |||
// clean up existing event handlers | |||
this.connection.removeAllListeners('error'); | |||
this.connection.removeAllListeners('timeout'); | |||
this.connection.removeAllListeners('close'); | |||
this.connection.removeAllListeners('data'); | |||
this.connection = undefined; | |||
return doConnect(this, 4, _options, _errorHandler); | |||
}); | |||
}; | |||
/** | |||
* Unref this connection | |||
* @method | |||
* @return {boolean} | |||
*/ | |||
Connection.prototype.unref = function() { | |||
if (this.connection) this.connection.unref(); | |||
else { | |||
var self = this; | |||
this.once('connect', function() { | |||
self.connection.unref(); | |||
}); | |||
} | |||
}; | |||
/** | |||
* Destroy connection | |||
* @method | |||
*/ | |||
Connection.prototype.destroy = function() { | |||
// Set the connections | |||
if (connectionAccounting) deleteConnection(this.id); | |||
if (this.connection) { | |||
// Catch posssible exception thrown by node 0.10.x | |||
try { | |||
this.connection.end(); | |||
} catch (err) {} // eslint-disable-line | |||
// Destroy connection | |||
this.connection.destroy(); | |||
} | |||
this.destroyed = true; | |||
}; | |||
/** | |||
* Write to connection | |||
* @method | |||
* @param {Command} command Command to write out need to implement toBin and toBinUnified | |||
*/ | |||
Connection.prototype.write = function(buffer) { | |||
var i; | |||
// Debug Log | |||
if (this.logger.isDebug()) { | |||
if (!Array.isArray(buffer)) { | |||
this.logger.debug( | |||
f('writing buffer [%s] to %s:%s', buffer.toString('hex'), this.host, this.port) | |||
); | |||
} else { | |||
for (i = 0; i < buffer.length; i++) | |||
this.logger.debug( | |||
f('writing buffer [%s] to %s:%s', buffer[i].toString('hex'), this.host, this.port) | |||
); | |||
} | |||
} | |||
// Double check that the connection is not destroyed | |||
if (this.connection.destroyed === false) { | |||
// Write out the command | |||
if (!Array.isArray(buffer)) { | |||
this.connection.write(buffer, 'binary'); | |||
return true; | |||
} | |||
// Iterate over all buffers and write them in order to the socket | |||
for (i = 0; i < buffer.length; i++) this.connection.write(buffer[i], 'binary'); | |||
return true; | |||
} | |||
// Connection is destroyed return write failed | |||
return false; | |||
}; | |||
/** | |||
* Return id of connection as a string | |||
* @method | |||
* @return {string} | |||
*/ | |||
Connection.prototype.toString = function() { | |||
return '' + this.id; | |||
}; | |||
/** | |||
* Return json object of connection | |||
* @method | |||
* @return {object} | |||
*/ | |||
Connection.prototype.toJSON = function() { | |||
return { id: this.id, host: this.host, port: this.port }; | |||
}; | |||
/** | |||
* Is the connection connected | |||
* @method | |||
* @return {boolean} | |||
*/ | |||
Connection.prototype.isConnected = function() { | |||
if (this.destroyed) return false; | |||
return !this.connection.destroyed && this.connection.writable; | |||
}; | |||
/** | |||
* A server connect event, used to verify that the connection is up and running | |||
* | |||
* @event Connection#connect | |||
* @type {Connection} | |||
*/ | |||
/** | |||
* The server connection closed, all pool connections closed | |||
* | |||
* @event Connection#close | |||
* @type {Connection} | |||
*/ | |||
/** | |||
* The server connection caused an error, all pool connections closed | |||
* | |||
* @event Connection#error | |||
* @type {Connection} | |||
*/ | |||
/** | |||
* The server connection timed out, all pool connections closed | |||
* | |||
* @event Connection#timeout | |||
* @type {Connection} | |||
*/ | |||
/** | |||
* The driver experienced an invalid message, all pool connections closed | |||
* | |||
* @event Connection#parseError | |||
* @type {Connection} | |||
*/ | |||
module.exports = Connection; |
@@ -0,0 +1,246 @@ | |||
'use strict'; | |||
var f = require('util').format, | |||
MongoError = require('../error').MongoError; | |||
// Filters for classes | |||
var classFilters = {}; | |||
var filteredClasses = {}; | |||
var level = null; | |||
// Save the process id | |||
var pid = process.pid; | |||
// current logger | |||
var currentLogger = null; | |||
/** | |||
* Creates a new Logger instance | |||
* @class | |||
* @param {string} className The Class name associated with the logging instance | |||
* @param {object} [options=null] Optional settings. | |||
* @param {Function} [options.logger=null] Custom logger function; | |||
* @param {string} [options.loggerLevel=error] Override default global log level. | |||
* @return {Logger} a Logger instance. | |||
*/ | |||
var Logger = function(className, options) { | |||
if (!(this instanceof Logger)) return new Logger(className, options); | |||
options = options || {}; | |||
// Current reference | |||
this.className = className; | |||
// Current logger | |||
if (options.logger) { | |||
currentLogger = options.logger; | |||
} else if (currentLogger == null) { | |||
currentLogger = console.log; | |||
} | |||
// Set level of logging, default is error | |||
if (options.loggerLevel) { | |||
level = options.loggerLevel || 'error'; | |||
} | |||
// Add all class names | |||
if (filteredClasses[this.className] == null) classFilters[this.className] = true; | |||
}; | |||
/** | |||
* Log a message at the debug level | |||
* @method | |||
* @param {string} message The message to log | |||
* @param {object} object additional meta data to log | |||
* @return {null} | |||
*/ | |||
Logger.prototype.debug = function(message, object) { | |||
if ( | |||
this.isDebug() && | |||
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) || | |||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className])) | |||
) { | |||
var dateTime = new Date().getTime(); | |||
var msg = f('[%s-%s:%s] %s %s', 'DEBUG', this.className, pid, dateTime, message); | |||
var state = { | |||
type: 'debug', | |||
message: message, | |||
className: this.className, | |||
pid: pid, | |||
date: dateTime | |||
}; | |||
if (object) state.meta = object; | |||
currentLogger(msg, state); | |||
} | |||
}; | |||
/** | |||
* Log a message at the warn level | |||
* @method | |||
* @param {string} message The message to log | |||
* @param {object} object additional meta data to log | |||
* @return {null} | |||
*/ | |||
(Logger.prototype.warn = function(message, object) { | |||
if ( | |||
this.isWarn() && | |||
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) || | |||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className])) | |||
) { | |||
var dateTime = new Date().getTime(); | |||
var msg = f('[%s-%s:%s] %s %s', 'WARN', this.className, pid, dateTime, message); | |||
var state = { | |||
type: 'warn', | |||
message: message, | |||
className: this.className, | |||
pid: pid, | |||
date: dateTime | |||
}; | |||
if (object) state.meta = object; | |||
currentLogger(msg, state); | |||
} | |||
}), | |||
/** | |||
* Log a message at the info level | |||
* @method | |||
* @param {string} message The message to log | |||
* @param {object} object additional meta data to log | |||
* @return {null} | |||
*/ | |||
(Logger.prototype.info = function(message, object) { | |||
if ( | |||
this.isInfo() && | |||
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) || | |||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className])) | |||
) { | |||
var dateTime = new Date().getTime(); | |||
var msg = f('[%s-%s:%s] %s %s', 'INFO', this.className, pid, dateTime, message); | |||
var state = { | |||
type: 'info', | |||
message: message, | |||
className: this.className, | |||
pid: pid, | |||
date: dateTime | |||
}; | |||
if (object) state.meta = object; | |||
currentLogger(msg, state); | |||
} | |||
}), | |||
/** | |||
* Log a message at the error level | |||
* @method | |||
* @param {string} message The message to log | |||
* @param {object} object additional meta data to log | |||
* @return {null} | |||
*/ | |||
(Logger.prototype.error = function(message, object) { | |||
if ( | |||
this.isError() && | |||
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) || | |||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className])) | |||
) { | |||
var dateTime = new Date().getTime(); | |||
var msg = f('[%s-%s:%s] %s %s', 'ERROR', this.className, pid, dateTime, message); | |||
var state = { | |||
type: 'error', | |||
message: message, | |||
className: this.className, | |||
pid: pid, | |||
date: dateTime | |||
}; | |||
if (object) state.meta = object; | |||
currentLogger(msg, state); | |||
} | |||
}), | |||
/** | |||
* Is the logger set at info level | |||
* @method | |||
* @return {boolean} | |||
*/ | |||
(Logger.prototype.isInfo = function() { | |||
return level === 'info' || level === 'debug'; | |||
}), | |||
/** | |||
* Is the logger set at error level | |||
* @method | |||
* @return {boolean} | |||
*/ | |||
(Logger.prototype.isError = function() { | |||
return level === 'error' || level === 'info' || level === 'debug'; | |||
}), | |||
/** | |||
* Is the logger set at error level | |||
* @method | |||
* @return {boolean} | |||
*/ | |||
(Logger.prototype.isWarn = function() { | |||
return level === 'error' || level === 'warn' || level === 'info' || level === 'debug'; | |||
}), | |||
/** | |||
* Is the logger set at debug level | |||
* @method | |||
* @return {boolean} | |||
*/ | |||
(Logger.prototype.isDebug = function() { | |||
return level === 'debug'; | |||
}); | |||
/** | |||
* Resets the logger to default settings, error and no filtered classes | |||
* @method | |||
* @return {null} | |||
*/ | |||
Logger.reset = function() { | |||
level = 'error'; | |||
filteredClasses = {}; | |||
}; | |||
/** | |||
* Get the current logger function | |||
* @method | |||
* @return {function} | |||
*/ | |||
Logger.currentLogger = function() { | |||
return currentLogger; | |||
}; | |||
/** | |||
* Set the current logger function | |||
* @method | |||
* @param {function} logger Logger function. | |||
* @return {null} | |||
*/ | |||
Logger.setCurrentLogger = function(logger) { | |||
if (typeof logger !== 'function') throw new MongoError('current logger must be a function'); | |||
currentLogger = logger; | |||
}; | |||
/** | |||
* Set what classes to log. | |||
* @method | |||
* @param {string} type The type of filter (currently only class) | |||
* @param {string[]} values The filters to apply | |||
* @return {null} | |||
*/ | |||
Logger.filter = function(type, values) { | |||
if (type === 'class' && Array.isArray(values)) { | |||
filteredClasses = {}; | |||
values.forEach(function(x) { | |||
filteredClasses[x] = true; | |||
}); | |||
} | |||
}; | |||
/** | |||
* Set the current log level | |||
* @method | |||
* @param {string} level Set current log level (debug, info, error) | |||
* @return {null} | |||
*/ | |||
Logger.setLevel = function(_level) { | |||
if (_level !== 'info' && _level !== 'error' && _level !== 'debug' && _level !== 'warn') { | |||
throw new Error(f('%s is an illegal logging level', _level)); | |||
} | |||
level = _level; | |||
}; | |||
module.exports = Logger; |
@@ -0,0 +1,113 @@ | |||
'use strict'; | |||
var f = require('util').format, | |||
require_optional = require('require_optional'); | |||
// Set property function | |||
var setProperty = function(obj, prop, flag, values) { | |||
Object.defineProperty(obj, prop.name, { | |||
enumerable: true, | |||
set: function(value) { | |||
if (typeof value !== 'boolean') throw new Error(f('%s required a boolean', prop.name)); | |||
// Flip the bit to 1 | |||
if (value === true) values.flags |= flag; | |||
// Flip the bit to 0 if it's set, otherwise ignore | |||
if (value === false && (values.flags & flag) === flag) values.flags ^= flag; | |||
prop.value = value; | |||
}, | |||
get: function() { | |||
return prop.value; | |||
} | |||
}); | |||
}; | |||
// Set property function | |||
var getProperty = function(obj, propName, fieldName, values, func) { | |||
Object.defineProperty(obj, propName, { | |||
enumerable: true, | |||
get: function() { | |||
// Not parsed yet, parse it | |||
if (values[fieldName] == null && obj.isParsed && !obj.isParsed()) { | |||
obj.parse(); | |||
} | |||
// Do we have a post processing function | |||
if (typeof func === 'function') return func(values[fieldName]); | |||
// Return raw value | |||
return values[fieldName]; | |||
} | |||
}); | |||
}; | |||
// Set simple property | |||
var getSingleProperty = function(obj, name, value) { | |||
Object.defineProperty(obj, name, { | |||
enumerable: true, | |||
get: function() { | |||
return value; | |||
} | |||
}); | |||
}; | |||
// Shallow copy | |||
var copy = function(fObj, tObj) { | |||
tObj = tObj || {}; | |||
for (var name in fObj) tObj[name] = fObj[name]; | |||
return tObj; | |||
}; | |||
var debugOptions = function(debugFields, options) { | |||
var finaloptions = {}; | |||
debugFields.forEach(function(n) { | |||
finaloptions[n] = options[n]; | |||
}); | |||
return finaloptions; | |||
}; | |||
var retrieveBSON = function() { | |||
var BSON = require('bson'); | |||
BSON.native = false; | |||
try { | |||
var optionalBSON = require_optional('bson-ext'); | |||
if (optionalBSON) { | |||
optionalBSON.native = true; | |||
return optionalBSON; | |||
} | |||
} catch (err) {} // eslint-disable-line | |||
return BSON; | |||
}; | |||
// Throw an error if an attempt to use Snappy is made when Snappy is not installed | |||
var noSnappyWarning = function() { | |||
throw new Error( | |||
'Attempted to use Snappy compression, but Snappy is not installed. Install or disable Snappy compression and try again.' | |||
); | |||
}; | |||
// Facilitate loading Snappy optionally | |||
var retrieveSnappy = function() { | |||
var snappy = null; | |||
try { | |||
snappy = require_optional('snappy'); | |||
} catch (error) {} // eslint-disable-line | |||
if (!snappy) { | |||
snappy = { | |||
compress: noSnappyWarning, | |||
uncompress: noSnappyWarning, | |||
compressSync: noSnappyWarning, | |||
uncompressSync: noSnappyWarning | |||
}; | |||
} | |||
return snappy; | |||
}; | |||
exports.setProperty = setProperty; | |||
exports.getProperty = getProperty; | |||
exports.getSingleProperty = getSingleProperty; | |||
exports.copy = copy; | |||
exports.debugOptions = debugOptions; | |||
exports.retrieveBSON = retrieveBSON; | |||
exports.retrieveSnappy = retrieveSnappy; |
@@ -0,0 +1,766 @@ | |||
'use strict'; | |||
const Logger = require('./connection/logger'); | |||
const retrieveBSON = require('./connection/utils').retrieveBSON; | |||
const MongoError = require('./error').MongoError; | |||
const MongoNetworkError = require('./error').MongoNetworkError; | |||
const mongoErrorContextSymbol = require('./error').mongoErrorContextSymbol; | |||
const f = require('util').format; | |||
const collationNotSupported = require('./utils').collationNotSupported; | |||
const BSON = retrieveBSON(); | |||
const Long = BSON.Long; | |||
/** | |||
* This is a cursor results callback | |||
* | |||
* @callback resultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {object} document | |||
*/ | |||
/** | |||
* @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB | |||
* 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(); | |||
*/ | |||
/** | |||
* Creates a new Cursor, not to be used directly | |||
* @class | |||
* @param {object} bson An instance of the BSON parser | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {{object}|Long} cmd The selector (can be a command or a cursorId) | |||
* @param {object} [options=null] Optional settings. | |||
* @param {object} [options.batchSize=1000] Batchsize for the operation | |||
* @param {array} [options.documents=[]] Initial documents list for cursor | |||
* @param {object} [options.transforms=null] Transform methods for the cursor results | |||
* @param {function} [options.transforms.query] Transform the value returned from the initial query | |||
* @param {function} [options.transforms.doc] Transform each document returned from Cursor.prototype.next | |||
* @param {object} topology The server topology instance. | |||
* @param {object} topologyOptions The server topology options. | |||
* @return {Cursor} A cursor instance | |||
* @property {number} cursorBatchSize The current cursorBatchSize for the cursor | |||
* @property {number} cursorLimit The current cursorLimit for the cursor | |||
* @property {number} cursorSkip The current cursorSkip for the cursor | |||
*/ | |||
var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) { | |||
options = options || {}; | |||
// Cursor pool | |||
this.pool = null; | |||
// Cursor server | |||
this.server = null; | |||
// Do we have a not connected handler | |||
this.disconnectHandler = options.disconnectHandler; | |||
// Set local values | |||
this.bson = bson; | |||
this.ns = ns; | |||
this.cmd = cmd; | |||
this.options = options; | |||
this.topology = topology; | |||
// All internal state | |||
this.cursorState = { | |||
cursorId: null, | |||
cmd: cmd, | |||
documents: options.documents || [], | |||
cursorIndex: 0, | |||
dead: false, | |||
killed: false, | |||
init: false, | |||
notified: false, | |||
limit: options.limit || cmd.limit || 0, | |||
skip: options.skip || cmd.skip || 0, | |||
batchSize: options.batchSize || cmd.batchSize || 1000, | |||
currentLimit: 0, | |||
// Result field name if not a cursor (contains the array of results) | |||
transforms: options.transforms, | |||
raw: options.raw || (cmd && cmd.raw) | |||
}; | |||
if (typeof options.session === 'object') { | |||
this.cursorState.session = options.session; | |||
} | |||
// Add promoteLong to cursor state | |||
if (typeof topologyOptions.promoteLongs === 'boolean') { | |||
this.cursorState.promoteLongs = topologyOptions.promoteLongs; | |||
} else if (typeof options.promoteLongs === 'boolean') { | |||
this.cursorState.promoteLongs = options.promoteLongs; | |||
} | |||
// Add promoteValues to cursor state | |||
if (typeof topologyOptions.promoteValues === 'boolean') { | |||
this.cursorState.promoteValues = topologyOptions.promoteValues; | |||
} else if (typeof options.promoteValues === 'boolean') { | |||
this.cursorState.promoteValues = options.promoteValues; | |||
} | |||
// Add promoteBuffers to cursor state | |||
if (typeof topologyOptions.promoteBuffers === 'boolean') { | |||
this.cursorState.promoteBuffers = topologyOptions.promoteBuffers; | |||
} else if (typeof options.promoteBuffers === 'boolean') { | |||
this.cursorState.promoteBuffers = options.promoteBuffers; | |||
} | |||
if (topologyOptions.reconnect) { | |||
this.cursorState.reconnect = topologyOptions.reconnect; | |||
} | |||
// Logger | |||
this.logger = Logger('Cursor', topologyOptions); | |||
// | |||
// Did we pass in a cursor id | |||
if (typeof cmd === 'number') { | |||
this.cursorState.cursorId = Long.fromNumber(cmd); | |||
this.cursorState.lastCursorId = this.cursorState.cursorId; | |||
} else if (cmd instanceof Long) { | |||
this.cursorState.cursorId = cmd; | |||
this.cursorState.lastCursorId = cmd; | |||
} | |||
}; | |||
Cursor.prototype.setCursorBatchSize = function(value) { | |||
this.cursorState.batchSize = value; | |||
}; | |||
Cursor.prototype.cursorBatchSize = function() { | |||
return this.cursorState.batchSize; | |||
}; | |||
Cursor.prototype.setCursorLimit = function(value) { | |||
this.cursorState.limit = value; | |||
}; | |||
Cursor.prototype.cursorLimit = function() { | |||
return this.cursorState.limit; | |||
}; | |||
Cursor.prototype.setCursorSkip = function(value) { | |||
this.cursorState.skip = value; | |||
}; | |||
Cursor.prototype.cursorSkip = function() { | |||
return this.cursorState.skip; | |||
}; | |||
Cursor.prototype._endSession = function(options, callback) { | |||
if (typeof options === 'function') { | |||
callback = options; | |||
options = {}; | |||
} | |||
options = options || {}; | |||
const session = this.cursorState.session; | |||
if (session && (options.force || session.owner === this)) { | |||
this.cursorState.session = undefined; | |||
session.endSession(callback); | |||
return true; | |||
} | |||
if (callback) { | |||
callback(); | |||
} | |||
return false; | |||
}; | |||
// | |||
// Handle callback (including any exceptions thrown) | |||
var handleCallback = function(callback, err, result) { | |||
try { | |||
callback(err, result); | |||
} catch (err) { | |||
process.nextTick(function() { | |||
throw err; | |||
}); | |||
} | |||
}; | |||
// Internal methods | |||
Cursor.prototype._getmore = function(callback) { | |||
if (this.logger.isDebug()) | |||
this.logger.debug(f('schedule getMore call for query [%s]', JSON.stringify(this.query))); | |||
// Set the current batchSize | |||
var batchSize = this.cursorState.batchSize; | |||
if ( | |||
this.cursorState.limit > 0 && | |||
this.cursorState.currentLimit + batchSize > this.cursorState.limit | |||
) { | |||
batchSize = this.cursorState.limit - this.cursorState.currentLimit; | |||
} | |||
this.server.wireProtocolHandler.getMore( | |||
this.server, | |||
this.ns, | |||
this.cursorState, | |||
batchSize, | |||
this.options, | |||
callback | |||
); | |||
}; | |||
/** | |||
* Clone the cursor | |||
* @method | |||
* @return {Cursor} | |||
*/ | |||
Cursor.prototype.clone = function() { | |||
return this.topology.cursor(this.ns, this.cmd, this.options); | |||
}; | |||
/** | |||
* Checks if the cursor is dead | |||
* @method | |||
* @return {boolean} A boolean signifying if the cursor is dead or not | |||
*/ | |||
Cursor.prototype.isDead = function() { | |||
return this.cursorState.dead === true; | |||
}; | |||
/** | |||
* Checks if the cursor was killed by the application | |||
* @method | |||
* @return {boolean} A boolean signifying if the cursor was killed by the application | |||
*/ | |||
Cursor.prototype.isKilled = function() { | |||
return this.cursorState.killed === true; | |||
}; | |||
/** | |||
* Checks if the cursor notified it's caller about it's death | |||
* @method | |||
* @return {boolean} A boolean signifying if the cursor notified the callback | |||
*/ | |||
Cursor.prototype.isNotified = function() { | |||
return this.cursorState.notified === true; | |||
}; | |||
/** | |||
* Returns current buffered documents length | |||
* @method | |||
* @return {number} The number of items in the buffered documents | |||
*/ | |||
Cursor.prototype.bufferedCount = function() { | |||
return this.cursorState.documents.length - this.cursorState.cursorIndex; | |||
}; | |||
/** | |||
* Returns current buffered documents | |||
* @method | |||
* @return {Array} An array of buffered documents | |||
*/ | |||
Cursor.prototype.readBufferedDocuments = function(number) { | |||
var unreadDocumentsLength = this.cursorState.documents.length - this.cursorState.cursorIndex; | |||
var length = number < unreadDocumentsLength ? number : unreadDocumentsLength; | |||
var elements = this.cursorState.documents.slice( | |||
this.cursorState.cursorIndex, | |||
this.cursorState.cursorIndex + length | |||
); | |||
// Transform the doc with passed in transformation method if provided | |||
if (this.cursorState.transforms && typeof this.cursorState.transforms.doc === 'function') { | |||
// Transform all the elements | |||
for (var i = 0; i < elements.length; i++) { | |||
elements[i] = this.cursorState.transforms.doc(elements[i]); | |||
} | |||
} | |||
// Ensure we do not return any more documents than the limit imposed | |||
// Just return the number of elements up to the limit | |||
if ( | |||
this.cursorState.limit > 0 && | |||
this.cursorState.currentLimit + elements.length > this.cursorState.limit | |||
) { | |||
elements = elements.slice(0, this.cursorState.limit - this.cursorState.currentLimit); | |||
this.kill(); | |||
} | |||
// Adjust current limit | |||
this.cursorState.currentLimit = this.cursorState.currentLimit + elements.length; | |||
this.cursorState.cursorIndex = this.cursorState.cursorIndex + elements.length; | |||
// Return elements | |||
return elements; | |||
}; | |||
/** | |||
* Kill the cursor | |||
* @method | |||
* @param {resultCallback} callback A callback function | |||
*/ | |||
Cursor.prototype.kill = function(callback) { | |||
// Set cursor to dead | |||
this.cursorState.dead = true; | |||
this.cursorState.killed = true; | |||
// Remove documents | |||
this.cursorState.documents = []; | |||
// If no cursor id just return | |||
if ( | |||
this.cursorState.cursorId == null || | |||
this.cursorState.cursorId.isZero() || | |||
this.cursorState.init === false | |||
) { | |||
if (callback) callback(null, null); | |||
return; | |||
} | |||
this.server.wireProtocolHandler.killCursor(this.server, this.ns, this.cursorState, callback); | |||
}; | |||
/** | |||
* Resets the cursor | |||
* @method | |||
* @return {null} | |||
*/ | |||
Cursor.prototype.rewind = function() { | |||
if (this.cursorState.init) { | |||
if (!this.cursorState.dead) { | |||
this.kill(); | |||
} | |||
this.cursorState.currentLimit = 0; | |||
this.cursorState.init = false; | |||
this.cursorState.dead = false; | |||
this.cursorState.killed = false; | |||
this.cursorState.notified = false; | |||
this.cursorState.documents = []; | |||
this.cursorState.cursorId = null; | |||
this.cursorState.cursorIndex = 0; | |||
} | |||
}; | |||
/** | |||
* Validate if the pool is dead and return error | |||
*/ | |||
var isConnectionDead = function(self, callback) { | |||
if (self.pool && self.pool.isDestroyed()) { | |||
self.cursorState.killed = true; | |||
const err = new MongoNetworkError( | |||
f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port) | |||
); | |||
_setCursorNotifiedImpl(self, () => callback(err)); | |||
return true; | |||
} | |||
return false; | |||
}; | |||
/** | |||
* Validate if the cursor is dead but was not explicitly killed by user | |||
*/ | |||
var isCursorDeadButNotkilled = function(self, callback) { | |||
// Cursor is dead but not marked killed, return null | |||
if (self.cursorState.dead && !self.cursorState.killed) { | |||
self.cursorState.killed = true; | |||
setCursorNotified(self, callback); | |||
return true; | |||
} | |||
return false; | |||
}; | |||
/** | |||
* Validate if the cursor is dead and was killed by user | |||
*/ | |||
var isCursorDeadAndKilled = function(self, callback) { | |||
if (self.cursorState.dead && self.cursorState.killed) { | |||
handleCallback(callback, new MongoError('cursor is dead')); | |||
return true; | |||
} | |||
return false; | |||
}; | |||
/** | |||
* Validate if the cursor was killed by the user | |||
*/ | |||
var isCursorKilled = function(self, callback) { | |||
if (self.cursorState.killed) { | |||
setCursorNotified(self, callback); | |||
return true; | |||
} | |||
return false; | |||
}; | |||
/** | |||
* Mark cursor as being dead and notified | |||
*/ | |||
var setCursorDeadAndNotified = function(self, callback) { | |||
self.cursorState.dead = true; | |||
setCursorNotified(self, callback); | |||
}; | |||
/** | |||
* Mark cursor as being notified | |||
*/ | |||
var setCursorNotified = function(self, callback) { | |||
_setCursorNotifiedImpl(self, () => handleCallback(callback, null, null)); | |||
}; | |||
var _setCursorNotifiedImpl = function(self, callback) { | |||
self.cursorState.notified = true; | |||
self.cursorState.documents = []; | |||
self.cursorState.cursorIndex = 0; | |||
if (self._endSession) { | |||
return self._endSession(undefined, () => callback()); | |||
} | |||
return callback(); | |||
}; | |||
var nextFunction = function(self, callback) { | |||
// We have notified about it | |||
if (self.cursorState.notified) { | |||
return callback(new Error('cursor is exhausted')); | |||
} | |||
// Cursor is killed return null | |||
if (isCursorKilled(self, callback)) return; | |||
// Cursor is dead but not marked killed, return null | |||
if (isCursorDeadButNotkilled(self, callback)) return; | |||
// We have a dead and killed cursor, attempting to call next should error | |||
if (isCursorDeadAndKilled(self, callback)) return; | |||
// We have just started the cursor | |||
if (!self.cursorState.init) { | |||
return initializeCursor(self, callback); | |||
} | |||
if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) { | |||
// Ensure we kill the cursor on the server | |||
self.kill(); | |||
// Set cursor in dead and notified state | |||
return setCursorDeadAndNotified(self, callback); | |||
} else if ( | |||
self.cursorState.cursorIndex === self.cursorState.documents.length && | |||
!Long.ZERO.equals(self.cursorState.cursorId) | |||
) { | |||
// Ensure an empty cursor state | |||
self.cursorState.documents = []; | |||
self.cursorState.cursorIndex = 0; | |||
// Check if topology is destroyed | |||
if (self.topology.isDestroyed()) | |||
return callback( | |||
new MongoNetworkError('connection destroyed, not possible to instantiate cursor') | |||
); | |||
// Check if connection is dead and return if not possible to | |||
// execute a getmore on this connection | |||
if (isConnectionDead(self, callback)) return; | |||
// Execute the next get more | |||
self._getmore(function(err, doc, connection) { | |||
if (err) { | |||
if (err instanceof MongoError) { | |||
err[mongoErrorContextSymbol].isGetMore = true; | |||
} | |||
return handleCallback(callback, err); | |||
} | |||
if (self.cursorState.cursorId && self.cursorState.cursorId.isZero() && self._endSession) { | |||
self._endSession(); | |||
} | |||
// Save the returned connection to ensure all getMore's fire over the same connection | |||
self.connection = connection; | |||
// Tailable cursor getMore result, notify owner about it | |||
// No attempt is made here to retry, this is left to the user of the | |||
// core module to handle to keep core simple | |||
if ( | |||
self.cursorState.documents.length === 0 && | |||
self.cmd.tailable && | |||
Long.ZERO.equals(self.cursorState.cursorId) | |||
) { | |||
// No more documents in the tailed cursor | |||
return handleCallback( | |||
callback, | |||
new MongoError({ | |||
message: 'No more documents in tailed cursor', | |||
tailable: self.cmd.tailable, | |||
awaitData: self.cmd.awaitData | |||
}) | |||
); | |||
} else if ( | |||
self.cursorState.documents.length === 0 && | |||
self.cmd.tailable && | |||
!Long.ZERO.equals(self.cursorState.cursorId) | |||
) { | |||
return nextFunction(self, callback); | |||
} | |||
if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) { | |||
return setCursorDeadAndNotified(self, callback); | |||
} | |||
nextFunction(self, callback); | |||
}); | |||
} else if ( | |||
self.cursorState.documents.length === self.cursorState.cursorIndex && | |||
self.cmd.tailable && | |||
Long.ZERO.equals(self.cursorState.cursorId) | |||
) { | |||
return handleCallback( | |||
callback, | |||
new MongoError({ | |||
message: 'No more documents in tailed cursor', | |||
tailable: self.cmd.tailable, | |||
awaitData: self.cmd.awaitData | |||
}) | |||
); | |||
} else if ( | |||
self.cursorState.documents.length === self.cursorState.cursorIndex && | |||
Long.ZERO.equals(self.cursorState.cursorId) | |||
) { | |||
setCursorDeadAndNotified(self, callback); | |||
} else { | |||
if (self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) { | |||
// Ensure we kill the cursor on the server | |||
self.kill(); | |||
// Set cursor in dead and notified state | |||
return setCursorDeadAndNotified(self, callback); | |||
} | |||
// Increment the current cursor limit | |||
self.cursorState.currentLimit += 1; | |||
// Get the document | |||
var doc = self.cursorState.documents[self.cursorState.cursorIndex++]; | |||
// Doc overflow | |||
if (!doc || doc.$err) { | |||
// Ensure we kill the cursor on the server | |||
self.kill(); | |||
// Set cursor in dead and notified state | |||
return setCursorDeadAndNotified(self, function() { | |||
handleCallback(callback, new MongoError(doc ? doc.$err : undefined)); | |||
}); | |||
} | |||
// Transform the doc with passed in transformation method if provided | |||
if (self.cursorState.transforms && typeof self.cursorState.transforms.doc === 'function') { | |||
doc = self.cursorState.transforms.doc(doc); | |||
} | |||
// Return the document | |||
handleCallback(callback, null, doc); | |||
} | |||
}; | |||
function initializeCursor(cursor, callback) { | |||
// Topology is not connected, save the call in the provided store to be | |||
// Executed at some point when the handler deems it's reconnected | |||
if (!cursor.topology.isConnected(cursor.options)) { | |||
// Only need this for single server, because repl sets and mongos | |||
// will always continue trying to reconnect | |||
if (cursor.topology._type === 'server' && !cursor.topology.s.options.reconnect) { | |||
// Reconnect is disabled, so we'll never reconnect | |||
return callback(new MongoError('no connection available')); | |||
} | |||
if (cursor.disconnectHandler != null) { | |||
if (cursor.topology.isDestroyed()) { | |||
// Topology was destroyed, so don't try to wait for it to reconnect | |||
return callback(new MongoError('Topology was destroyed')); | |||
} | |||
return cursor.disconnectHandler.addObjectAndMethod( | |||
'cursor', | |||
cursor, | |||
'next', | |||
[callback], | |||
callback | |||
); | |||
} | |||
} | |||
return cursor.topology.selectServer(cursor.options, (err, server) => { | |||
if (err) { | |||
const disconnectHandler = cursor.disconnectHandler; | |||
if (disconnectHandler != null) { | |||
return disconnectHandler.addObjectAndMethod('cursor', cursor, 'next', [callback], callback); | |||
} | |||
return callback(err); | |||
} | |||
cursor.server = server; | |||
cursor.cursorState.init = true; | |||
if (collationNotSupported(cursor.server, cursor.cmd)) { | |||
return callback(new MongoError(`server ${cursor.server.name} does not support collation`)); | |||
} | |||
function done() { | |||
if ( | |||
cursor.cursorState.cursorId && | |||
cursor.cursorState.cursorId.isZero() && | |||
cursor._endSession | |||
) { | |||
cursor._endSession(); | |||
} | |||
if ( | |||
cursor.cursorState.documents.length === 0 && | |||
cursor.cursorState.cursorId && | |||
cursor.cursorState.cursorId.isZero() && | |||
!cursor.cmd.tailable && | |||
!cursor.cmd.awaitData | |||
) { | |||
return setCursorNotified(cursor, callback); | |||
} | |||
nextFunction(cursor, callback); | |||
} | |||
// NOTE: this is a special internal method for cloning a cursor, consider removing | |||
if (cursor.cursorState.cursorId != null) { | |||
return done(); | |||
} | |||
const queryCallback = (err, r) => { | |||
if (err) return callback(err); | |||
const result = r.message; | |||
if (result.queryFailure) { | |||
return callback(new MongoError(result.documents[0]), null); | |||
} | |||
// Check if we have a command cursor | |||
if ( | |||
Array.isArray(result.documents) && | |||
result.documents.length === 1 && | |||
(!cursor.cmd.find || (cursor.cmd.find && cursor.cmd.virtual === false)) && | |||
(typeof result.documents[0].cursor !== 'string' || | |||
result.documents[0]['$err'] || | |||
result.documents[0]['errmsg'] || | |||
Array.isArray(result.documents[0].result)) | |||
) { | |||
// We have an error document, return the error | |||
if (result.documents[0]['$err'] || result.documents[0]['errmsg']) { | |||
return callback(new MongoError(result.documents[0]), null); | |||
} | |||
// We have a cursor document | |||
if (result.documents[0].cursor != null && typeof result.documents[0].cursor !== 'string') { | |||
var id = result.documents[0].cursor.id; | |||
// If we have a namespace change set the new namespace for getmores | |||
if (result.documents[0].cursor.ns) { | |||
cursor.ns = result.documents[0].cursor.ns; | |||
} | |||
// Promote id to long if needed | |||
cursor.cursorState.cursorId = typeof id === 'number' ? Long.fromNumber(id) : id; | |||
cursor.cursorState.lastCursorId = cursor.cursorState.cursorId; | |||
cursor.cursorState.operationTime = result.documents[0].operationTime; | |||
// If we have a firstBatch set it | |||
if (Array.isArray(result.documents[0].cursor.firstBatch)) { | |||
cursor.cursorState.documents = result.documents[0].cursor.firstBatch; //.reverse(); | |||
} | |||
// Return after processing command cursor | |||
return done(result); | |||
} | |||
if (Array.isArray(result.documents[0].result)) { | |||
cursor.cursorState.documents = result.documents[0].result; | |||
cursor.cursorState.cursorId = Long.ZERO; | |||
return done(result); | |||
} | |||
} | |||
// Otherwise fall back to regular find path | |||
cursor.cursorState.cursorId = result.cursorId; | |||
cursor.cursorState.documents = result.documents; | |||
cursor.cursorState.lastCursorId = result.cursorId; | |||
// Transform the results with passed in transformation method if provided | |||
if ( | |||
cursor.cursorState.transforms && | |||
typeof cursor.cursorState.transforms.query === 'function' | |||
) { | |||
cursor.cursorState.documents = cursor.cursorState.transforms.query(result); | |||
} | |||
// Return callback | |||
done(result); | |||
}; | |||
if (cursor.logger.isDebug()) { | |||
cursor.logger.debug( | |||
`issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify( | |||
cursor.query | |||
)}]` | |||
); | |||
} | |||
if (cursor.cmd.find != null) { | |||
cursor.server.wireProtocolHandler.query( | |||
cursor.server, | |||
cursor.ns, | |||
cursor.cmd, | |||
cursor.cursorState, | |||
cursor.options, | |||
queryCallback | |||
); | |||
return; | |||
} | |||
cursor.query = cursor.server.wireProtocolHandler.command( | |||
cursor.server, | |||
cursor.ns, | |||
cursor.cmd, | |||
cursor.options, | |||
queryCallback | |||
); | |||
}); | |||
} | |||
/** | |||
* Retrieve the next document from the cursor | |||
* @method | |||
* @param {resultCallback} callback A callback function | |||
*/ | |||
Cursor.prototype.next = function(callback) { | |||
nextFunction(this, callback); | |||
}; | |||
module.exports = Cursor; |
@@ -0,0 +1,146 @@ | |||
'use strict'; | |||
const mongoErrorContextSymbol = Symbol('mongoErrorContextSymbol'); | |||
/** | |||
* Creates a new MongoError | |||
* | |||
* @augments Error | |||
* @param {Error|string|object} message The error message | |||
* @property {string} message The error message | |||
* @property {string} stack The error call stack | |||
*/ | |||
class MongoError extends Error { | |||
constructor(message) { | |||
if (message instanceof Error) { | |||
super(message.message); | |||
this.stack = message.stack; | |||
} else { | |||
if (typeof message === 'string') { | |||
super(message); | |||
} else { | |||
super(message.message || message.errmsg || message.$err || 'n/a'); | |||
for (var name in message) { | |||
this[name] = message[name]; | |||
} | |||
} | |||
Error.captureStackTrace(this, this.constructor); | |||
} | |||
this.name = 'MongoError'; | |||
this[mongoErrorContextSymbol] = this[mongoErrorContextSymbol] || {}; | |||
} | |||
/** | |||
* Creates a new MongoError object | |||
* | |||
* @param {Error|string|object} options The options used to create the error. | |||
* @return {MongoError} A MongoError instance | |||
* @deprecated Use `new MongoError()` instead. | |||
*/ | |||
static create(options) { | |||
return new MongoError(options); | |||
} | |||
} | |||
/** | |||
* Creates a new MongoNetworkError | |||
* | |||
* @param {Error|string|object} message The error message | |||
* @property {string} message The error message | |||
* @property {string} stack The error call stack | |||
*/ | |||
class MongoNetworkError extends MongoError { | |||
constructor(message) { | |||
super(message); | |||
this.name = 'MongoNetworkError'; | |||
// This is added as part of the transactions specification | |||
this.errorLabels = ['TransientTransactionError']; | |||
} | |||
} | |||
/** | |||
* An error used when attempting to parse a value (like a connection string) | |||
* | |||
* @param {Error|string|object} message The error message | |||
* @property {string} message The error message | |||
*/ | |||
class MongoParseError extends MongoError { | |||
constructor(message) { | |||
super(message); | |||
this.name = 'MongoParseError'; | |||
} | |||
} | |||
/** | |||
* An error signifying a timeout event | |||
* | |||
* @param {Error|string|object} message The error message | |||
* @property {string} message The error message | |||
*/ | |||
class MongoTimeoutError extends MongoError { | |||
constructor(message) { | |||
super(message); | |||
this.name = 'MongoTimeoutError'; | |||
} | |||
} | |||
/** | |||
* An error thrown when the server reports a writeConcernError | |||
* | |||
* @param {Error|string|object} message The error message | |||
* @param {object} result The result document (provided if ok: 1) | |||
* @property {string} message The error message | |||
* @property {object} [result] The result document (provided if ok: 1) | |||
*/ | |||
class MongoWriteConcernError extends MongoError { | |||
constructor(message, result) { | |||
super(message); | |||
this.name = 'MongoWriteConcernError'; | |||
if (result != null) { | |||
this.result = result; | |||
} | |||
} | |||
} | |||
// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms | |||
const RETRYABLE_ERROR_CODES = new Set([ | |||
6, // HostUnreachable | |||
7, // HostNotFound | |||
89, // NetworkTimeout | |||
91, // ShutdownInProgress | |||
189, // PrimarySteppedDown | |||
9001, // SocketException | |||
10107, // NotMaster | |||
11600, // InterruptedAtShutdown | |||
11602, // InterruptedDueToReplStateChange | |||
13435, // NotMasterNoSlaveOk | |||
13436 // NotMasterOrSecondary | |||
]); | |||
/** | |||
* Determines whether an error is something the driver should attempt to retry | |||
* | |||
* @param {MongoError|Error} error | |||
*/ | |||
function isRetryableError(error) { | |||
return ( | |||
RETRYABLE_ERROR_CODES.has(error.code) || | |||
error instanceof MongoNetworkError || | |||
error.message.match(/not master/) || | |||
error.message.match(/node is recovering/) | |||
); | |||
} | |||
module.exports = { | |||
MongoError, | |||
MongoNetworkError, | |||
MongoParseError, | |||
MongoTimeoutError, | |||
MongoWriteConcernError, | |||
mongoErrorContextSymbol, | |||
isRetryableError | |||
}; |
@@ -0,0 +1,749 @@ | |||
'use strict'; | |||
const Logger = require('../connection/logger'); | |||
const BSON = require('../connection/utils').retrieveBSON(); | |||
const MongoError = require('../error').MongoError; | |||
const MongoNetworkError = require('../error').MongoNetworkError; | |||
const mongoErrorContextSymbol = require('../error').mongoErrorContextSymbol; | |||
const Long = BSON.Long; | |||
const deprecate = require('util').deprecate; | |||
const readPreferenceServerSelector = require('./server_selectors').readPreferenceServerSelector; | |||
const ReadPreference = require('../topologies/read_preference'); | |||
/** | |||
* Handle callback (including any exceptions thrown) | |||
*/ | |||
function handleCallback(callback, err, result) { | |||
try { | |||
callback(err, result); | |||
} catch (err) { | |||
process.nextTick(function() { | |||
throw err; | |||
}); | |||
} | |||
} | |||
/** | |||
* This is a cursor results callback | |||
* | |||
* @callback resultCallback | |||
* @param {error} error An error object. Set to null if no error present | |||
* @param {object} document | |||
*/ | |||
/** | |||
* An internal class that embodies a cursor on MongoDB, allowing for iteration over the | |||
* results returned from a query. | |||
* | |||
* @property {number} cursorBatchSize The current cursorBatchSize for the cursor | |||
* @property {number} cursorLimit The current cursorLimit for the cursor | |||
* @property {number} cursorSkip The current cursorSkip for the cursor | |||
*/ | |||
class Cursor { | |||
/** | |||
* Create a cursor | |||
* | |||
* @param {object} bson An instance of the BSON parser | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {{object}|Long} cmd The selector (can be a command or a cursorId) | |||
* @param {object} [options=null] Optional settings. | |||
* @param {object} [options.batchSize=1000] Batchsize for the operation | |||
* @param {array} [options.documents=[]] Initial documents list for cursor | |||
* @param {object} [options.transforms=null] Transform methods for the cursor results | |||
* @param {function} [options.transforms.query] Transform the value returned from the initial query | |||
* @param {function} [options.transforms.doc] Transform each document returned from Cursor.prototype.next | |||
* @param {object} topology The server topology instance. | |||
* @param {object} topologyOptions The server topology options. | |||
*/ | |||
constructor(bson, ns, cmd, options, topology, topologyOptions) { | |||
options = options || {}; | |||
// Cursor pool | |||
this.pool = null; | |||
// Cursor server | |||
this.server = null; | |||
// Do we have a not connected handler | |||
this.disconnectHandler = options.disconnectHandler; | |||
// Set local values | |||
this.bson = bson; | |||
this.ns = ns; | |||
this.cmd = cmd; | |||
this.options = options; | |||
this.topology = topology; | |||
// All internal state | |||
this.s = { | |||
cursorId: null, | |||
cmd: cmd, | |||
documents: options.documents || [], | |||
cursorIndex: 0, | |||
dead: false, | |||
killed: false, | |||
init: false, | |||
notified: false, | |||
limit: options.limit || cmd.limit || 0, | |||
skip: options.skip || cmd.skip || 0, | |||
batchSize: options.batchSize || cmd.batchSize || 1000, | |||
currentLimit: 0, | |||
// Result field name if not a cursor (contains the array of results) | |||
transforms: options.transforms | |||
}; | |||
if (typeof options.session === 'object') { | |||
this.s.session = options.session; | |||
} | |||
// Add promoteLong to cursor state | |||
if (typeof topologyOptions.promoteLongs === 'boolean') { | |||
this.s.promoteLongs = topologyOptions.promoteLongs; | |||
} else if (typeof options.promoteLongs === 'boolean') { | |||
this.s.promoteLongs = options.promoteLongs; | |||
} | |||
// Add promoteValues to cursor state | |||
if (typeof topologyOptions.promoteValues === 'boolean') { | |||
this.s.promoteValues = topologyOptions.promoteValues; | |||
} else if (typeof options.promoteValues === 'boolean') { | |||
this.s.promoteValues = options.promoteValues; | |||
} | |||
// Add promoteBuffers to cursor state | |||
if (typeof topologyOptions.promoteBuffers === 'boolean') { | |||
this.s.promoteBuffers = topologyOptions.promoteBuffers; | |||
} else if (typeof options.promoteBuffers === 'boolean') { | |||
this.s.promoteBuffers = options.promoteBuffers; | |||
} | |||
if (topologyOptions.reconnect) { | |||
this.s.reconnect = topologyOptions.reconnect; | |||
} | |||
// Logger | |||
this.logger = Logger('Cursor', topologyOptions); | |||
// | |||
// Did we pass in a cursor id | |||
if (typeof cmd === 'number') { | |||
this.s.cursorId = Long.fromNumber(cmd); | |||
this.s.lastCursorId = this.s.cursorId; | |||
} else if (cmd instanceof Long) { | |||
this.s.cursorId = cmd; | |||
this.s.lastCursorId = cmd; | |||
} | |||
} | |||
setCursorBatchSize(value) { | |||
this.s.batchSize = value; | |||
} | |||
cursorBatchSize() { | |||
return this.s.batchSize; | |||
} | |||
setCursorLimit(value) { | |||
this.s.limit = value; | |||
} | |||
cursorLimit() { | |||
return this.s.limit; | |||
} | |||
setCursorSkip(value) { | |||
this.s.skip = value; | |||
} | |||
cursorSkip() { | |||
return this.s.skip; | |||
} | |||
_endSession(options, callback) { | |||
if (typeof options === 'function') { | |||
callback = options; | |||
options = {}; | |||
} | |||
options = options || {}; | |||
const session = this.s.session; | |||
if (session && (options.force || session.owner === this)) { | |||
this.s.session = undefined; | |||
session.endSession(callback); | |||
return true; | |||
} | |||
if (callback) { | |||
callback(); | |||
} | |||
return false; | |||
} | |||
/** | |||
* Clone the cursor | |||
* @method | |||
* @return {Cursor} | |||
*/ | |||
clone() { | |||
return this.topology.cursor(this.ns, this.cmd, this.options); | |||
} | |||
/** | |||
* Checks if the cursor is dead | |||
* @method | |||
* @return {boolean} A boolean signifying if the cursor is dead or not | |||
*/ | |||
isDead() { | |||
return this.s.dead === true; | |||
} | |||
/** | |||
* Checks if the cursor was killed by the application | |||
* @method | |||
* @return {boolean} A boolean signifying if the cursor was killed by the application | |||
*/ | |||
isKilled() { | |||
return this.s.killed === true; | |||
} | |||
/** | |||
* Checks if the cursor notified it's caller about it's death | |||
* @method | |||
* @return {boolean} A boolean signifying if the cursor notified the callback | |||
*/ | |||
isNotified() { | |||
return this.s.notified === true; | |||
} | |||
/** | |||
* Returns current buffered documents length | |||
* @method | |||
* @return {number} The number of items in the buffered documents | |||
*/ | |||
bufferedCount() { | |||
return this.s.documents.length - this.s.cursorIndex; | |||
} | |||
/** | |||
* Kill the cursor | |||
* | |||
* @param {resultCallback} callback A callback function | |||
*/ | |||
kill(callback) { | |||
// Set cursor to dead | |||
this.s.dead = true; | |||
this.s.killed = true; | |||
// Remove documents | |||
this.s.documents = []; | |||
// If no cursor id just return | |||
if (this.s.cursorId == null || this.s.cursorId.isZero() || this.s.init === false) { | |||
if (callback) callback(null, null); | |||
return; | |||
} | |||
// Default pool | |||
const pool = this.s.server.s.pool; | |||
// Execute command | |||
this.s.server.s.wireProtocolHandler.killCursor(this.bson, this.ns, this.s, pool, callback); | |||
} | |||
/** | |||
* Resets the cursor | |||
*/ | |||
rewind() { | |||
if (this.s.init) { | |||
if (!this.s.dead) { | |||
this.kill(); | |||
} | |||
this.s.currentLimit = 0; | |||
this.s.init = false; | |||
this.s.dead = false; | |||
this.s.killed = false; | |||
this.s.notified = false; | |||
this.s.documents = []; | |||
this.s.cursorId = null; | |||
this.s.cursorIndex = 0; | |||
} | |||
} | |||
/** | |||
* Returns current buffered documents | |||
* @method | |||
* @return {Array} An array of buffered documents | |||
*/ | |||
readBufferedDocuments(number) { | |||
const unreadDocumentsLength = this.s.documents.length - this.s.cursorIndex; | |||
const length = number < unreadDocumentsLength ? number : unreadDocumentsLength; | |||
let elements = this.s.documents.slice(this.s.cursorIndex, this.s.cursorIndex + length); | |||
// Transform the doc with passed in transformation method if provided | |||
if (this.s.transforms && typeof this.s.transforms.doc === 'function') { | |||
// Transform all the elements | |||
for (let i = 0; i < elements.length; i++) { | |||
elements[i] = this.s.transforms.doc(elements[i]); | |||
} | |||
} | |||
// Ensure we do not return any more documents than the limit imposed | |||
// Just return the number of elements up to the limit | |||
if (this.s.limit > 0 && this.s.currentLimit + elements.length > this.s.limit) { | |||
elements = elements.slice(0, this.s.limit - this.s.currentLimit); | |||
this.kill(); | |||
} | |||
// Adjust current limit | |||
this.s.currentLimit = this.s.currentLimit + elements.length; | |||
this.s.cursorIndex = this.s.cursorIndex + elements.length; | |||
// Return elements | |||
return elements; | |||
} | |||
/** | |||
* Retrieve the next document from the cursor | |||
* | |||
* @param {resultCallback} callback A callback function | |||
*/ | |||
next(callback) { | |||
nextFunction(this, callback); | |||
} | |||
} | |||
Cursor.prototype._find = deprecate( | |||
callback => _find(this, callback), | |||
'_find() is deprecated, please stop using it' | |||
); | |||
Cursor.prototype._getmore = deprecate( | |||
callback => _getmore(this, callback), | |||
'_getmore() is deprecated, please stop using it' | |||
); | |||
function _getmore(cursor, callback) { | |||
if (cursor.logger.isDebug()) { | |||
cursor.logger.debug(`schedule getMore call for query [${JSON.stringify(cursor.query)}]`); | |||
} | |||
// Determine if it's a raw query | |||
const raw = cursor.options.raw || cursor.cmd.raw; | |||
// Set the current batchSize | |||
let batchSize = cursor.s.batchSize; | |||
if (cursor.s.limit > 0 && cursor.s.currentLimit + batchSize > cursor.s.limit) { | |||
batchSize = cursor.s.limit - cursor.s.currentLimit; | |||
} | |||
// Default pool | |||
const pool = cursor.s.server.s.pool; | |||
// We have a wire protocol handler | |||
cursor.s.server.s.wireProtocolHandler.getMore( | |||
cursor.bson, | |||
cursor.ns, | |||
cursor.s, | |||
batchSize, | |||
raw, | |||
pool, | |||
cursor.options, | |||
callback | |||
); | |||
} | |||
function _find(cursor, callback) { | |||
if (cursor.logger.isDebug()) { | |||
cursor.logger.debug( | |||
`issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify( | |||
cursor.query | |||
)}]` | |||
); | |||
} | |||
const queryCallback = (err, r) => { | |||
if (err) return callback(err); | |||
// Get the raw message | |||
const result = r.message; | |||
// Query failure bit set | |||
if (result.queryFailure) { | |||
return callback(new MongoError(result.documents[0]), null); | |||
} | |||
// Check if we have a command cursor | |||
if ( | |||
Array.isArray(result.documents) && | |||
result.documents.length === 1 && | |||
(!cursor.cmd.find || (cursor.cmd.find && cursor.cmd.virtual === false)) && | |||
(result.documents[0].cursor !== 'string' || | |||
result.documents[0]['$err'] || | |||
result.documents[0]['errmsg'] || | |||
Array.isArray(result.documents[0].result)) | |||
) { | |||
// We have a an error document return the error | |||
if (result.documents[0]['$err'] || result.documents[0]['errmsg']) { | |||
return callback(new MongoError(result.documents[0]), null); | |||
} | |||
// We have a cursor document | |||
if (result.documents[0].cursor != null && typeof result.documents[0].cursor !== 'string') { | |||
const id = result.documents[0].cursor.id; | |||
// If we have a namespace change set the new namespace for getmores | |||
if (result.documents[0].cursor.ns) { | |||
cursor.ns = result.documents[0].cursor.ns; | |||
} | |||
// Promote id to long if needed | |||
cursor.s.cursorId = typeof id === 'number' ? Long.fromNumber(id) : id; | |||
cursor.s.lastCursorId = cursor.s.cursorId; | |||
// If we have a firstBatch set it | |||
if (Array.isArray(result.documents[0].cursor.firstBatch)) { | |||
cursor.s.documents = result.documents[0].cursor.firstBatch; | |||
} | |||
// Return after processing command cursor | |||
return callback(null, result); | |||
} | |||
if (Array.isArray(result.documents[0].result)) { | |||
cursor.s.documents = result.documents[0].result; | |||
cursor.s.cursorId = Long.ZERO; | |||
return callback(null, result); | |||
} | |||
} | |||
// Otherwise fall back to regular find path | |||
cursor.s.cursorId = result.cursorId; | |||
cursor.s.documents = result.documents; | |||
cursor.s.lastCursorId = result.cursorId; | |||
// Transform the results with passed in transformation method if provided | |||
if (cursor.s.transforms && typeof cursor.s.transforms.query === 'function') { | |||
cursor.s.documents = cursor.s.transforms.query(result); | |||
} | |||
// Return callback | |||
callback(null, result); | |||
}; | |||
// Options passed to the pool | |||
const queryOptions = {}; | |||
// If we have a raw query decorate the function | |||
if (cursor.options.raw || cursor.cmd.raw) { | |||
queryOptions.raw = cursor.options.raw || cursor.cmd.raw; | |||
} | |||
// Do we have documentsReturnedIn set on the query | |||
if (typeof cursor.query.documentsReturnedIn === 'string') { | |||
queryOptions.documentsReturnedIn = cursor.query.documentsReturnedIn; | |||
} | |||
// Add promote Long value if defined | |||
if (typeof cursor.s.promoteLongs === 'boolean') { | |||
queryOptions.promoteLongs = cursor.s.promoteLongs; | |||
} | |||
// Add promote values if defined | |||
if (typeof cursor.s.promoteValues === 'boolean') { | |||
queryOptions.promoteValues = cursor.s.promoteValues; | |||
} | |||
// Add promote values if defined | |||
if (typeof cursor.s.promoteBuffers === 'boolean') { | |||
queryOptions.promoteBuffers = cursor.s.promoteBuffers; | |||
} | |||
if (typeof cursor.s.session === 'object') { | |||
queryOptions.session = cursor.s.session; | |||
} | |||
// Write the initial command out | |||
cursor.s.server.s.pool.write(cursor.query, queryOptions, queryCallback); | |||
} | |||
/** | |||
* Validate if the pool is dead and return error | |||
*/ | |||
function isConnectionDead(cursor, callback) { | |||
if (cursor.pool && cursor.pool.isDestroyed()) { | |||
cursor.s.killed = true; | |||
const err = new MongoNetworkError( | |||
`connection to host ${cursor.pool.host}:${cursor.pool.port} was destroyed` | |||
); | |||
_setCursorNotifiedImpl(cursor, () => callback(err)); | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* Validate if the cursor is dead but was not explicitly killed by user | |||
*/ | |||
function isCursorDeadButNotkilled(cursor, callback) { | |||
// Cursor is dead but not marked killed, return null | |||
if (cursor.s.dead && !cursor.s.killed) { | |||
cursor.s.killed = true; | |||
setCursorNotified(cursor, callback); | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* Validate if the cursor is dead and was killed by user | |||
*/ | |||
function isCursorDeadAndKilled(cursor, callback) { | |||
if (cursor.s.dead && cursor.s.killed) { | |||
handleCallback(callback, new MongoError('cursor is dead')); | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* Validate if the cursor was killed by the user | |||
*/ | |||
function isCursorKilled(cursor, callback) { | |||
if (cursor.s.killed) { | |||
setCursorNotified(cursor, callback); | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* Mark cursor as being dead and notified | |||
*/ | |||
function setCursorDeadAndNotified(cursor, callback) { | |||
cursor.s.dead = true; | |||
setCursorNotified(cursor, callback); | |||
} | |||
/** | |||
* Mark cursor as being notified | |||
*/ | |||
function setCursorNotified(cursor, callback) { | |||
_setCursorNotifiedImpl(cursor, () => handleCallback(callback, null, null)); | |||
} | |||
function _setCursorNotifiedImpl(cursor, callback) { | |||
cursor.s.notified = true; | |||
cursor.s.documents = []; | |||
cursor.s.cursorIndex = 0; | |||
if (cursor._endSession) { | |||
return cursor._endSession(undefined, () => callback()); | |||
} | |||
return callback(); | |||
} | |||
function initializeCursorAndRetryNext(cursor, callback) { | |||
cursor.topology.selectServer( | |||
readPreferenceServerSelector(cursor.options.readPreference || ReadPreference.primary), | |||
(err, server) => { | |||
if (err) { | |||
callback(err, null); | |||
return; | |||
} | |||
cursor.s.server = server; | |||
cursor.s.init = true; | |||
// check if server supports collation | |||
// NOTE: this should be a part of the selection predicate! | |||
if (cursor.cmd && cursor.cmd.collation && cursor.server.description.maxWireVersion < 5) { | |||
callback(new MongoError(`server ${cursor.server.name} does not support collation`)); | |||
return; | |||
} | |||
try { | |||
cursor.query = cursor.s.server.s.wireProtocolHandler.command( | |||
cursor.bson, | |||
cursor.ns, | |||
cursor.cmd, | |||
cursor.s, | |||
cursor.topology, | |||
cursor.options | |||
); | |||
nextFunction(cursor, callback); | |||
} catch (err) { | |||
callback(err); | |||
return; | |||
} | |||
} | |||
); | |||
} | |||
function nextFunction(cursor, callback) { | |||
// We have notified about it | |||
if (cursor.s.notified) { | |||
return callback(new Error('cursor is exhausted')); | |||
} | |||
// Cursor is killed return null | |||
if (isCursorKilled(cursor, callback)) return; | |||
// Cursor is dead but not marked killed, return null | |||
if (isCursorDeadButNotkilled(cursor, callback)) return; | |||
// We have a dead and killed cursor, attempting to call next should error | |||
if (isCursorDeadAndKilled(cursor, callback)) return; | |||
// We have just started the cursor | |||
if (!cursor.s.init) { | |||
return initializeCursorAndRetryNext(cursor, callback); | |||
} | |||
// If we don't have a cursorId execute the first query | |||
if (cursor.s.cursorId == null) { | |||
// Check if pool is dead and return if not possible to | |||
// execute the query against the db | |||
if (isConnectionDead(cursor, callback)) return; | |||
// query, cmd, options, s, callback | |||
return _find(cursor, function(err) { | |||
if (err) return handleCallback(callback, err, null); | |||
if (cursor.s.cursorId && cursor.s.cursorId.isZero() && cursor._endSession) { | |||
cursor._endSession(); | |||
} | |||
if ( | |||
cursor.s.documents.length === 0 && | |||
cursor.s.cursorId && | |||
cursor.s.cursorId.isZero() && | |||
!cursor.cmd.tailable && | |||
!cursor.cmd.awaitData | |||
) { | |||
return setCursorNotified(cursor, callback); | |||
} | |||
nextFunction(cursor, callback); | |||
}); | |||
} | |||
if (cursor.s.documents.length === cursor.s.cursorIndex && Long.ZERO.equals(cursor.s.cursorId)) { | |||
setCursorDeadAndNotified(cursor, callback); | |||
return; | |||
} | |||
if (cursor.s.limit > 0 && cursor.s.currentLimit >= cursor.s.limit) { | |||
// Ensure we kill the cursor on the server | |||
cursor.kill(); | |||
// Set cursor in dead and notified state | |||
setCursorDeadAndNotified(cursor, callback); | |||
return; | |||
} | |||
if ( | |||
cursor.s.documents.length === cursor.s.cursorIndex && | |||
cursor.cmd.tailable && | |||
Long.ZERO.equals(cursor.s.cursorId) | |||
) { | |||
return handleCallback( | |||
callback, | |||
new MongoError({ | |||
message: 'No more documents in tailed cursor', | |||
tailable: cursor.cmd.tailable, | |||
awaitData: cursor.cmd.awaitData | |||
}) | |||
); | |||
} | |||
if (cursor.s.cursorIndex === cursor.s.documents.length && !Long.ZERO.equals(cursor.s.cursorId)) { | |||
// Ensure an empty cursor state | |||
cursor.s.documents = []; | |||
cursor.s.cursorIndex = 0; | |||
// Check if connection is dead and return if not possible to | |||
if (isConnectionDead(cursor, callback)) return; | |||
// Execute the next get more | |||
return _getmore(cursor, function(err, doc, connection) { | |||
if (err) { | |||
if (err instanceof MongoError) { | |||
err[mongoErrorContextSymbol].isGetMore = true; | |||
} | |||
return handleCallback(callback, err); | |||
} | |||
if (cursor.s.cursorId && cursor.s.cursorId.isZero() && cursor._endSession) { | |||
cursor._endSession(); | |||
} | |||
// Save the returned connection to ensure all getMore's fire over the same connection | |||
cursor.connection = connection; | |||
// Tailable cursor getMore result, notify owner about it | |||
// No attempt is made here to retry, this is left to the user of the | |||
// core module to handle to keep core simple | |||
if ( | |||
cursor.s.documents.length === 0 && | |||
cursor.cmd.tailable && | |||
Long.ZERO.equals(cursor.s.cursorId) | |||
) { | |||
// No more documents in the tailed cursor | |||
return handleCallback( | |||
callback, | |||
new MongoError({ | |||
message: 'No more documents in tailed cursor', | |||
tailable: cursor.cmd.tailable, | |||
awaitData: cursor.cmd.awaitData | |||
}) | |||
); | |||
} else if ( | |||
cursor.s.documents.length === 0 && | |||
cursor.cmd.tailable && | |||
!Long.ZERO.equals(cursor.s.cursorId) | |||
) { | |||
return nextFunction(cursor, callback); | |||
} | |||
if (cursor.s.limit > 0 && cursor.s.currentLimit >= cursor.s.limit) { | |||
return setCursorDeadAndNotified(cursor, callback); | |||
} | |||
nextFunction(cursor, callback); | |||
}); | |||
} | |||
if (cursor.s.limit > 0 && cursor.s.currentLimit >= cursor.s.limit) { | |||
// Ensure we kill the cursor on the server | |||
cursor.kill(); | |||
// Set cursor in dead and notified state | |||
return setCursorDeadAndNotified(cursor, callback); | |||
} | |||
// Increment the current cursor limit | |||
cursor.s.currentLimit += 1; | |||
// Get the document | |||
let doc = cursor.s.documents[cursor.s.cursorIndex++]; | |||
// Doc overflow | |||
if (!doc || doc.$err) { | |||
// Ensure we kill the cursor on the server | |||
cursor.kill(); | |||
// Set cursor in dead and notified state | |||
return setCursorDeadAndNotified(cursor, function() { | |||
handleCallback(callback, new MongoError(doc ? doc.$err : undefined)); | |||
}); | |||
} | |||
// Transform the doc with passed in transformation method if provided | |||
if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') { | |||
doc = cursor.s.transforms.doc(doc); | |||
} | |||
// Return the document | |||
handleCallback(callback, null, doc); | |||
} | |||
module.exports = Cursor; |
@@ -0,0 +1,217 @@ | |||
'use strict'; | |||
const ServerDescription = require('./server_description').ServerDescription; | |||
const calculateDurationInMs = require('../utils').calculateDurationInMs; | |||
/** | |||
* Published when server description changes, but does NOT include changes to the RTT. | |||
* | |||
* @property {Object} topologyId A unique identifier for the topology | |||
* @property {ServerAddress} address The address (host/port pair) of the server | |||
* @property {ServerDescription} previousDescription The previous server description | |||
* @property {ServerDescription} newDescription The new server description | |||
*/ | |||
class ServerDescriptionChangedEvent { | |||
constructor(topologyId, address, previousDescription, newDescription) { | |||
Object.assign(this, { topologyId, address, previousDescription, newDescription }); | |||
} | |||
} | |||
/** | |||
* Published when server is initialized. | |||
* | |||
* @property {Object} topologyId A unique identifier for the topology | |||
* @property {ServerAddress} address The address (host/port pair) of the server | |||
*/ | |||
class ServerOpeningEvent { | |||
constructor(topologyId, address) { | |||
Object.assign(this, { topologyId, address }); | |||
} | |||
} | |||
/** | |||
* Published when server is closed. | |||
* | |||
* @property {ServerAddress} address The address (host/port pair) of the server | |||
* @property {Object} topologyId A unique identifier for the topology | |||
*/ | |||
class ServerClosedEvent { | |||
constructor(topologyId, address) { | |||
Object.assign(this, { topologyId, address }); | |||
} | |||
} | |||
/** | |||
* Published when topology description changes. | |||
* | |||
* @property {Object} topologyId | |||
* @property {TopologyDescription} previousDescription The old topology description | |||
* @property {TopologyDescription} newDescription The new topology description | |||
*/ | |||
class TopologyDescriptionChangedEvent { | |||
constructor(topologyId, previousDescription, newDescription) { | |||
Object.assign(this, { topologyId, previousDescription, newDescription }); | |||
} | |||
} | |||
/** | |||
* Published when topology is initialized. | |||
* | |||
* @param {Object} topologyId A unique identifier for the topology | |||
*/ | |||
class TopologyOpeningEvent { | |||
constructor(topologyId) { | |||
Object.assign(this, { topologyId }); | |||
} | |||
} | |||
/** | |||
* Published when topology is closed. | |||
* | |||
* @param {Object} topologyId A unique identifier for the topology | |||
*/ | |||
class TopologyClosedEvent { | |||
constructor(topologyId) { | |||
Object.assign(this, { topologyId }); | |||
} | |||
} | |||
/** | |||
* Fired when the server monitor’s ismaster command is started - immediately before | |||
* the ismaster command is serialized into raw BSON and written to the socket. | |||
* | |||
* @property {Object} connectionId The connection id for the command | |||
*/ | |||
class ServerHeartbeatStartedEvent { | |||
constructor(connectionId) { | |||
Object.assign(this, { connectionId }); | |||
} | |||
} | |||
/** | |||
* Fired when the server monitor’s ismaster succeeds. | |||
* | |||
* @param {Number} duration The execution time of the event in ms | |||
* @param {Object} reply The command reply | |||
* @param {Object} connectionId The connection id for the command | |||
*/ | |||
class ServerHeartbeatSucceededEvent { | |||
constructor(duration, reply, connectionId) { | |||
Object.assign(this, { duration, reply, connectionId }); | |||
} | |||
} | |||
/** | |||
* Fired when the server monitor’s ismaster fails, either with an “ok: 0” or a socket exception. | |||
* | |||
* @param {Number} duration The execution time of the event in ms | |||
* @param {MongoError|Object} failure The command failure | |||
* @param {Object} connectionId The connection id for the command | |||
*/ | |||
class ServerHeartbeatFailedEvent { | |||
constructor(duration, failure, connectionId) { | |||
Object.assign(this, { duration, failure, connectionId }); | |||
} | |||
} | |||
/** | |||
* Performs a server check as described by the SDAM spec. | |||
* | |||
* NOTE: This method automatically reschedules itself, so that there is always an active | |||
* monitoring process | |||
* | |||
* @param {Server} server The server to monitor | |||
*/ | |||
function monitorServer(server) { | |||
// executes a single check of a server | |||
const checkServer = callback => { | |||
let start = process.hrtime(); | |||
// emit a signal indicating we have started the heartbeat | |||
server.emit('serverHeartbeatStarted', new ServerHeartbeatStartedEvent(server.name)); | |||
server.command( | |||
'admin.$cmd', | |||
{ ismaster: true }, | |||
{ | |||
monitoring: true, | |||
socketTimeout: server.s.options.connectionTimeout || 2000 | |||
}, | |||
function(err, result) { | |||
let duration = calculateDurationInMs(start); | |||
if (err) { | |||
server.emit( | |||
'serverHeartbeatFailed', | |||
new ServerHeartbeatFailedEvent(duration, err, server.name) | |||
); | |||
return callback(err, null); | |||
} | |||
const isMaster = result.result; | |||
server.emit( | |||
'serverHeartbeatSucceded', | |||
new ServerHeartbeatSucceededEvent(duration, isMaster, server.name) | |||
); | |||
return callback(null, isMaster); | |||
} | |||
); | |||
}; | |||
const successHandler = isMaster => { | |||
server.s.monitoring = false; | |||
// emit an event indicating that our description has changed | |||
server.emit('descriptionReceived', new ServerDescription(server.description.address, isMaster)); | |||
// schedule the next monitoring process | |||
server.s.monitorId = setTimeout( | |||
() => monitorServer(server), | |||
server.s.options.heartbeatFrequencyMS | |||
); | |||
}; | |||
// run the actual monitoring loop | |||
server.s.monitoring = true; | |||
checkServer((err, isMaster) => { | |||
if (!err) { | |||
successHandler(isMaster); | |||
return; | |||
} | |||
// According to the SDAM specification's "Network error during server check" section, if | |||
// an ismaster call fails we reset the server's pool. If a server was once connected, | |||
// change its type to `Unknown` only after retrying once. | |||
// TODO: we need to reset the pool here | |||
return checkServer((err, isMaster) => { | |||
if (err) { | |||
server.s.monitoring = false; | |||
// revert to `Unknown` by emitting a default description with no isMaster | |||
server.emit('descriptionReceived', new ServerDescription(server.description.address)); | |||
// do not reschedule monitoring in this case | |||
return; | |||
} | |||
successHandler(isMaster); | |||
}); | |||
}); | |||
} | |||
module.exports = { | |||
ServerDescriptionChangedEvent, | |||
ServerOpeningEvent, | |||
ServerClosedEvent, | |||
TopologyDescriptionChangedEvent, | |||
TopologyOpeningEvent, | |||
TopologyClosedEvent, | |||
ServerHeartbeatStartedEvent, | |||
ServerHeartbeatSucceededEvent, | |||
ServerHeartbeatFailedEvent, | |||
monitorServer | |||
}; |
@@ -0,0 +1,411 @@ | |||
'use strict'; | |||
const EventEmitter = require('events'); | |||
const MongoError = require('../error').MongoError; | |||
const Pool = require('../connection/pool'); | |||
const relayEvents = require('../utils').relayEvents; | |||
const calculateDurationInMs = require('../utils').calculateDurationInMs; | |||
const Query = require('../connection/commands').Query; | |||
const TwoSixWireProtocolSupport = require('../wireprotocol/2_6_support'); | |||
const ThreeTwoWireProtocolSupport = require('../wireprotocol/3_2_support'); | |||
const BSON = require('../connection/utils').retrieveBSON(); | |||
const createClientInfo = require('../topologies/shared').createClientInfo; | |||
const Logger = require('../connection/logger'); | |||
const ServerDescription = require('./server_description').ServerDescription; | |||
const ReadPreference = require('../topologies/read_preference'); | |||
const monitorServer = require('./monitoring').monitorServer; | |||
/** | |||
* | |||
* @fires Server#serverHeartbeatStarted | |||
* @fires Server#serverHeartbeatSucceeded | |||
* @fires Server#serverHeartbeatFailed | |||
*/ | |||
class Server extends EventEmitter { | |||
/** | |||
* Create a server | |||
* | |||
* @param {ServerDescription} description | |||
* @param {Object} options | |||
*/ | |||
constructor(description, options) { | |||
super(); | |||
this.s = { | |||
// the server description | |||
description, | |||
// a saved copy of the incoming options | |||
options, | |||
// the server logger | |||
logger: Logger('Server', options), | |||
// the bson parser | |||
bson: options.bson || new BSON(), | |||
// client metadata for the initial handshake | |||
clientInfo: createClientInfo(options), | |||
// state variable to determine if there is an active server check in progress | |||
monitoring: false, | |||
// the connection pool | |||
pool: null | |||
}; | |||
} | |||
get description() { | |||
return this.s.description; | |||
} | |||
get name() { | |||
return this.s.description.address; | |||
} | |||
/** | |||
* Initiate server connect | |||
* | |||
* @param {Array} [options.auth] Array of auth options to apply on connect | |||
*/ | |||
connect(options) { | |||
options = options || {}; | |||
// do not allow connect to be called on anything that's not disconnected | |||
if (this.s.pool && !this.s.pool.isDisconnected() && !this.s.pool.isDestroyed()) { | |||
throw new MongoError(`Server instance in invalid state ${this.s.pool.state}`); | |||
} | |||
// create a pool | |||
this.s.pool = new Pool(this, Object.assign(this.s.options, options, { bson: this.s.bson })); | |||
// Set up listeners | |||
this.s.pool.on('connect', connectEventHandler(this)); | |||
this.s.pool.on('close', closeEventHandler(this)); | |||
// this.s.pool.on('error', errorEventHandler(this)); | |||
// this.s.pool.on('timeout', timeoutEventHandler(this)); | |||
// this.s.pool.on('parseError', errorEventHandler(this)); | |||
// this.s.pool.on('reconnect', reconnectEventHandler(this)); | |||
// this.s.pool.on('reconnectFailed', errorEventHandler(this)); | |||
// relay all command monitoring events | |||
relayEvents(this.s.pool, this, ['commandStarted', 'commandSucceeded', 'commandFailed']); | |||
// If auth settings have been provided, use them | |||
if (options.auth) { | |||
this.s.pool.connect.apply(this.s.pool, options.auth); | |||
return; | |||
} | |||
this.s.pool.connect(); | |||
} | |||
/** | |||
* Destroy the server connection | |||
* | |||
* @param {Boolean} [options.emitClose=false] Emit close event on destroy | |||
* @param {Boolean} [options.emitDestroy=false] Emit destroy event on destroy | |||
* @param {Boolean} [options.force=false] Force destroy the pool | |||
*/ | |||
destroy(callback) { | |||
if (typeof callback === 'function') { | |||
callback(null, null); | |||
} | |||
} | |||
/** | |||
* Immediately schedule monitoring of this server. If there already an attempt being made | |||
* this will be a no-op. | |||
*/ | |||
monitor() { | |||
if (this.s.monitoring) return; | |||
if (this.s.monitorId) clearTimeout(this.s.monitorId); | |||
monitorServer(this); | |||
} | |||
/** | |||
* Execute a command | |||
* | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {object} cmd The command hash | |||
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.checkKeys=false] Specify if the bson parser should validate keys. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {Boolean} [options.fullResult=false] Return the full envelope instead of just the result document. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
command(ns, cmd, options, callback) { | |||
if (typeof options === 'function') { | |||
(callback = options), (options = {}), (options = options || {}); | |||
} | |||
const error = basicReadValidations(this, options); | |||
if (error) { | |||
return callback(error, null); | |||
} | |||
// Clone the options | |||
options = Object.assign({}, options, { wireProtocolCommand: false }); | |||
// Debug log | |||
if (this.s.logger.isDebug()) { | |||
this.s.logger.debug( | |||
`executing command [${JSON.stringify({ ns, cmd, options })}] against ${this.name}` | |||
); | |||
} | |||
// Check if we have collation support | |||
if (this.description.maxWireVersion < 5 && cmd.collation) { | |||
callback(new MongoError(`server ${this.name} does not support collation`)); | |||
return; | |||
} | |||
// Create the query object | |||
const query = this.s.wireProtocolHandler.command(this, ns, cmd, {}, options); | |||
// Set slave OK of the query | |||
query.slaveOk = options.readPreference ? options.readPreference.slaveOk() : false; | |||
// write options | |||
const writeOptions = { | |||
raw: typeof options.raw === 'boolean' ? options.raw : false, | |||
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true, | |||
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true, | |||
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false, | |||
command: true, | |||
monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false, | |||
fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false, | |||
requestId: query.requestId, | |||
socketTimeout: typeof options.socketTimeout === 'number' ? options.socketTimeout : null, | |||
session: options.session || null | |||
}; | |||
// write the operation to the pool | |||
this.s.pool.write(query, writeOptions, callback); | |||
} | |||
/** | |||
* Insert one or more documents | |||
* @method | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {array} ops An array of documents to insert | |||
* @param {boolean} [options.ordered=true] Execute in order or out of order | |||
* @param {object} [options.writeConcern={}] Write concern for the operation | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
insert(ns, ops, options, callback) { | |||
executeWriteOperation({ server: this, op: 'insert', ns, ops }, options, callback); | |||
} | |||
/** | |||
* Perform one or more update operations | |||
* @method | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {array} ops An array of updates | |||
* @param {boolean} [options.ordered=true] Execute in order or out of order | |||
* @param {object} [options.writeConcern={}] Write concern for the operation | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
update(ns, ops, options, callback) { | |||
executeWriteOperation({ server: this, op: 'update', ns, ops }, options, callback); | |||
} | |||
/** | |||
* Perform one or more remove operations | |||
* @method | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {array} ops An array of removes | |||
* @param {boolean} [options.ordered=true] Execute in order or out of order | |||
* @param {object} [options.writeConcern={}] Write concern for the operation | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
remove(ns, ops, options, callback) { | |||
executeWriteOperation({ server: this, op: 'remove', ns, ops }, options, callback); | |||
} | |||
} | |||
function basicWriteValidations(server) { | |||
if (!server.s.pool) { | |||
return new MongoError('server instance is not connected'); | |||
} | |||
if (server.s.pool.isDestroyed()) { | |||
return new MongoError('server instance pool was destroyed'); | |||
} | |||
return null; | |||
} | |||
function basicReadValidations(server, options) { | |||
const error = basicWriteValidations(server, options); | |||
if (error) { | |||
return error; | |||
} | |||
if (options.readPreference && !(options.readPreference instanceof ReadPreference)) { | |||
return new MongoError('readPreference must be an instance of ReadPreference'); | |||
} | |||
} | |||
function executeWriteOperation(args, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// TODO: once we drop Node 4, use destructuring either here or in arguments. | |||
const server = args.server; | |||
const op = args.op; | |||
const ns = args.ns; | |||
const ops = Array.isArray(args.ops) ? args.ops : [args.ops]; | |||
const error = basicWriteValidations(server, options); | |||
if (error) { | |||
callback(error, null); | |||
return; | |||
} | |||
// Check if we have collation support | |||
if (server.description.maxWireVersion < 5 && options.collation) { | |||
callback(new MongoError(`server ${this.name} does not support collation`)); | |||
return; | |||
} | |||
// Execute write | |||
return server.s.wireProtocolHandler[op](server.s.pool, ns, server.s.bson, ops, options, callback); | |||
} | |||
function saslSupportedMechs(options) { | |||
if (!options) { | |||
return {}; | |||
} | |||
const authArray = options.auth || []; | |||
const authMechanism = authArray[0] || options.authMechanism; | |||
const authSource = authArray[1] || options.authSource || options.dbName || 'admin'; | |||
const user = authArray[2] || options.user; | |||
if (typeof authMechanism === 'string' && authMechanism.toUpperCase() !== 'DEFAULT') { | |||
return {}; | |||
} | |||
if (!user) { | |||
return {}; | |||
} | |||
return { saslSupportedMechs: `${authSource}.${user}` }; | |||
} | |||
function extractIsMasterError(err, result) { | |||
if (err) return err; | |||
if (result && result.result && result.result.ok === 0) { | |||
return new MongoError(result.result); | |||
} | |||
} | |||
function executeServerHandshake(server, callback) { | |||
// construct an `ismaster` query | |||
const compressors = | |||
server.s.options.compression && server.s.options.compression.compressors | |||
? server.s.options.compression.compressors | |||
: []; | |||
const queryOptions = { numberToSkip: 0, numberToReturn: -1, checkKeys: false, slaveOk: true }; | |||
const query = new Query( | |||
server.s.bson, | |||
'admin.$cmd', | |||
Object.assign( | |||
{ ismaster: true, client: server.s.clientInfo, compression: compressors }, | |||
saslSupportedMechs(server.s.options) | |||
), | |||
queryOptions | |||
); | |||
// execute the query | |||
server.s.pool.write( | |||
query, | |||
{ socketTimeout: server.s.options.connectionTimeout || 2000 }, | |||
callback | |||
); | |||
} | |||
function configureWireProtocolHandler(ismaster) { | |||
// 3.2 wire protocol handler | |||
if (ismaster.maxWireVersion >= 4) { | |||
return new ThreeTwoWireProtocolSupport(); | |||
} | |||
// default to 2.6 wire protocol handler | |||
return new TwoSixWireProtocolSupport(); | |||
} | |||
function connectEventHandler(server) { | |||
return function() { | |||
// log information of received information if in info mode | |||
// if (server.s.logger.isInfo()) { | |||
// var object = err instanceof MongoError ? JSON.stringify(err) : {}; | |||
// server.s.logger.info(`server ${server.name} fired event ${event} out with message ${object}`); | |||
// } | |||
// begin initial server handshake | |||
const start = process.hrtime(); | |||
executeServerHandshake(server, (err, response) => { | |||
// Set initial lastIsMasterMS - is this needed? | |||
server.s.lastIsMasterMS = calculateDurationInMs(start); | |||
const serverError = extractIsMasterError(err, response); | |||
if (serverError) { | |||
server.emit('error', serverError); | |||
return; | |||
} | |||
// extract the ismaster from the server response | |||
const isMaster = response.result; | |||
// compression negotation | |||
if (isMaster && isMaster.compression) { | |||
const localCompressionInfo = server.s.options.compression; | |||
const localCompressors = localCompressionInfo.compressors; | |||
for (var i = 0; i < localCompressors.length; i++) { | |||
if (isMaster.compression.indexOf(localCompressors[i]) > -1) { | |||
server.s.pool.options.agreedCompressor = localCompressors[i]; | |||
break; | |||
} | |||
} | |||
if (localCompressionInfo.zlibCompressionLevel) { | |||
server.s.pool.options.zlibCompressionLevel = localCompressionInfo.zlibCompressionLevel; | |||
} | |||
} | |||
// configure the wire protocol handler | |||
server.s.wireProtocolHandler = configureWireProtocolHandler(isMaster); | |||
// log the connection event if requested | |||
if (server.s.logger.isInfo()) { | |||
server.s.logger.info( | |||
`server ${server.name} connected with ismaster [${JSON.stringify(isMaster)}]` | |||
); | |||
} | |||
// emit an event indicating that our description has changed | |||
server.emit( | |||
'descriptionReceived', | |||
new ServerDescription(server.description.address, isMaster) | |||
); | |||
// emit a connect event | |||
server.emit('connect', isMaster); | |||
}); | |||
}; | |||
} | |||
function closeEventHandler(server) { | |||
return function() { | |||
server.emit('close'); | |||
}; | |||
} | |||
module.exports = Server; |
@@ -0,0 +1,141 @@ | |||
'use strict'; | |||
// An enumeration of server types we know about | |||
const ServerType = { | |||
Standalone: 'Standalone', | |||
Mongos: 'Mongos', | |||
PossiblePrimary: 'PossiblePrimary', | |||
RSPrimary: 'RSPrimary', | |||
RSSecondary: 'RSSecondary', | |||
RSArbiter: 'RSArbiter', | |||
RSOther: 'RSOther', | |||
RSGhost: 'RSGhost', | |||
Unknown: 'Unknown' | |||
}; | |||
const WRITABLE_SERVER_TYPES = new Set([ | |||
ServerType.RSPrimary, | |||
ServerType.Standalone, | |||
ServerType.Mongos | |||
]); | |||
const ISMASTER_FIELDS = [ | |||
'minWireVersion', | |||
'maxWireVersion', | |||
'me', | |||
'hosts', | |||
'passives', | |||
'arbiters', | |||
'tags', | |||
'setName', | |||
'setVersion', | |||
'electionId', | |||
'primary', | |||
'logicalSessionTimeoutMinutes' | |||
]; | |||
/** | |||
* The client's view of a single server, based on the most recent ismaster outcome. | |||
* | |||
* Internal type, not meant to be directly instantiated | |||
*/ | |||
class ServerDescription { | |||
/** | |||
* Create a ServerDescription | |||
* @param {String} address The address of the server | |||
* @param {Object} [ismaster] An optional ismaster response for this server | |||
* @param {Object} [options] Optional settings | |||
* @param {Number} [options.roundTripTime] The round trip time to ping this server (in ms) | |||
*/ | |||
constructor(address, ismaster, options) { | |||
options = options || {}; | |||
ismaster = Object.assign( | |||
{ | |||
minWireVersion: 0, | |||
maxWireVersion: 0, | |||
hosts: [], | |||
passives: [], | |||
arbiters: [], | |||
tags: [] | |||
}, | |||
ismaster | |||
); | |||
this.address = address; | |||
this.error = null; | |||
this.roundTripTime = options.roundTripTime || 0; | |||
this.lastUpdateTime = Date.now(); | |||
this.lastWriteDate = ismaster.lastWrite ? ismaster.lastWrite.lastWriteDate : null; | |||
this.opTime = ismaster.lastWrite ? ismaster.lastWrite.opTime : null; | |||
this.type = parseServerType(ismaster); | |||
// direct mappings | |||
ISMASTER_FIELDS.forEach(field => { | |||
if (typeof ismaster[field] !== 'undefined') this[field] = ismaster[field]; | |||
}); | |||
// normalize case for hosts | |||
this.hosts = this.hosts.map(host => host.toLowerCase()); | |||
this.passives = this.passives.map(host => host.toLowerCase()); | |||
this.arbiters = this.arbiters.map(host => host.toLowerCase()); | |||
} | |||
get allHosts() { | |||
return this.hosts.concat(this.arbiters).concat(this.passives); | |||
} | |||
/** | |||
* @return {Boolean} Is this server available for reads | |||
*/ | |||
get isReadable() { | |||
return this.type === ServerType.RSSecondary || this.isWritable; | |||
} | |||
/** | |||
* @return {Boolean} Is this server available for writes | |||
*/ | |||
get isWritable() { | |||
return WRITABLE_SERVER_TYPES.has(this.type); | |||
} | |||
} | |||
/** | |||
* Parses an `ismaster` message and determines the server type | |||
* | |||
* @param {Object} ismaster The `ismaster` message to parse | |||
* @return {ServerType} | |||
*/ | |||
function parseServerType(ismaster) { | |||
if (!ismaster || !ismaster.ok) { | |||
return ServerType.Unknown; | |||
} | |||
if (ismaster.isreplicaset) { | |||
return ServerType.RSGhost; | |||
} | |||
if (ismaster.msg && ismaster.msg === 'isdbgrid') { | |||
return ServerType.Mongos; | |||
} | |||
if (ismaster.setName) { | |||
if (ismaster.hidden) { | |||
return ServerType.RSOther; | |||
} else if (ismaster.ismaster) { | |||
return ServerType.RSPrimary; | |||
} else if (ismaster.secondary) { | |||
return ServerType.RSSecondary; | |||
} else if (ismaster.arbiterOnly) { | |||
return ServerType.RSArbiter; | |||
} else { | |||
return ServerType.RSOther; | |||
} | |||
} | |||
return ServerType.Standalone; | |||
} | |||
module.exports = { | |||
ServerDescription, | |||
ServerType | |||
}; |
@@ -0,0 +1,206 @@ | |||
'use strict'; | |||
const ServerType = require('./server_description').ServerType; | |||
const TopologyType = require('./topology_description').TopologyType; | |||
const ReadPreference = require('../topologies/read_preference'); | |||
const MongoError = require('../error').MongoError; | |||
// max staleness constants | |||
const IDLE_WRITE_PERIOD = 10000; | |||
const SMALLEST_MAX_STALENESS_SECONDS = 90; | |||
function writableServerSelector() { | |||
return function(topologyDescription, servers) { | |||
return latencyWindowReducer(topologyDescription, servers.filter(s => s.isWritable)); | |||
}; | |||
} | |||
// reducers | |||
function maxStalenessReducer(readPreference, topologyDescription, servers) { | |||
if (readPreference.maxStalenessSeconds == null || readPreference.maxStalenessSeconds < 0) { | |||
return servers; | |||
} | |||
const maxStaleness = readPreference.maxStalenessSeconds; | |||
const maxStalenessVariance = | |||
(topologyDescription.heartbeatFrequencyMS + IDLE_WRITE_PERIOD) / 1000; | |||
if (maxStaleness < maxStalenessVariance) { | |||
throw MongoError(`maxStalenessSeconds must be at least ${maxStalenessVariance} seconds`); | |||
} | |||
if (maxStaleness < SMALLEST_MAX_STALENESS_SECONDS) { | |||
throw new MongoError( | |||
`maxStalenessSeconds must be at least ${SMALLEST_MAX_STALENESS_SECONDS} seconds` | |||
); | |||
} | |||
if (topologyDescription.type === TopologyType.ReplicaSetWithPrimary) { | |||
const primary = servers.filter(primaryFilter)[0]; | |||
return servers.reduce((result, server) => { | |||
const stalenessMS = | |||
server.lastUpdateTime - | |||
server.lastWriteDate - | |||
(primary.lastUpdateTime - primary.lastWriteDate) + | |||
topologyDescription.heartbeatFrequencyMS; | |||
const staleness = stalenessMS / 1000; | |||
if (staleness <= readPreference.maxStalenessSeconds) result.push(server); | |||
return result; | |||
}, []); | |||
} else if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) { | |||
const sMax = servers.reduce((max, s) => (s.lastWriteDate > max.lastWriteDate ? s : max)); | |||
return servers.reduce((result, server) => { | |||
const stalenessMS = | |||
sMax.lastWriteDate - server.lastWriteDate + topologyDescription.heartbeatFrequencyMS; | |||
const staleness = stalenessMS / 1000; | |||
if (staleness <= readPreference.maxStalenessSeconds) result.push(server); | |||
return result; | |||
}, []); | |||
} | |||
return servers; | |||
} | |||
function tagSetMatch(tagSet, serverTags) { | |||
const keys = Object.keys(tagSet); | |||
const serverTagKeys = Object.keys(serverTags); | |||
for (let i = 0; i < keys.length; ++i) { | |||
const key = keys[i]; | |||
if (serverTagKeys.indexOf(key) === -1 || serverTags[key] !== tagSet[key]) { | |||
return false; | |||
} | |||
} | |||
return true; | |||
} | |||
function tagSetReducer(readPreference, servers) { | |||
if ( | |||
readPreference.tags == null || | |||
(Array.isArray(readPreference.tags) && readPreference.tags.length === 0) | |||
) { | |||
return servers; | |||
} | |||
for (let i = 0; i < readPreference.tags.length; ++i) { | |||
const tagSet = readPreference.tags[i]; | |||
const serversMatchingTagset = servers.reduce((matched, server) => { | |||
if (tagSetMatch(tagSet, server.tags)) matched.push(server); | |||
return matched; | |||
}, []); | |||
if (serversMatchingTagset.length) { | |||
return serversMatchingTagset; | |||
} | |||
} | |||
return []; | |||
} | |||
function latencyWindowReducer(topologyDescription, servers) { | |||
const low = servers.reduce( | |||
(min, server) => (min === -1 ? server.roundTripTime : Math.min(server.roundTripTime, min)), | |||
-1 | |||
); | |||
const high = low + topologyDescription.localThresholdMS; | |||
return servers.reduce((result, server) => { | |||
if (server.roundTripTime <= high && server.roundTripTime >= low) result.push(server); | |||
return result; | |||
}, []); | |||
} | |||
// filters | |||
function primaryFilter(server) { | |||
return server.type === ServerType.RSPrimary; | |||
} | |||
function secondaryFilter(server) { | |||
return server.type === ServerType.RSSecondary; | |||
} | |||
function nearestFilter(server) { | |||
return server.type === ServerType.RSSecondary || server.type === ServerType.RSPrimary; | |||
} | |||
function knownFilter(server) { | |||
return server.type !== ServerType.Unknown; | |||
} | |||
function readPreferenceServerSelector(readPreference) { | |||
if (!readPreference.isValid()) { | |||
throw new TypeError('Invalid read preference specified'); | |||
} | |||
return function(topologyDescription, servers) { | |||
const commonWireVersion = topologyDescription.commonWireVersion; | |||
if ( | |||
commonWireVersion && | |||
(readPreference.minWireVersion && readPreference.minWireVersion > commonWireVersion) | |||
) { | |||
throw new MongoError( | |||
`Minimum wire version '${ | |||
readPreference.minWireVersion | |||
}' required, but found '${commonWireVersion}'` | |||
); | |||
} | |||
if ( | |||
topologyDescription.type === TopologyType.Single || | |||
topologyDescription.type === TopologyType.Sharded | |||
) { | |||
return latencyWindowReducer(topologyDescription, servers.filter(knownFilter)); | |||
} | |||
if (readPreference.mode === ReadPreference.PRIMARY) { | |||
return servers.filter(primaryFilter); | |||
} | |||
if (readPreference.mode === ReadPreference.SECONDARY) { | |||
return latencyWindowReducer( | |||
topologyDescription, | |||
tagSetReducer( | |||
readPreference, | |||
maxStalenessReducer(readPreference, topologyDescription, servers) | |||
) | |||
).filter(secondaryFilter); | |||
} else if (readPreference.mode === ReadPreference.NEAREST) { | |||
return latencyWindowReducer( | |||
topologyDescription, | |||
tagSetReducer( | |||
readPreference, | |||
maxStalenessReducer(readPreference, topologyDescription, servers) | |||
) | |||
).filter(nearestFilter); | |||
} else if (readPreference.mode === ReadPreference.SECONDARY_PREFERRED) { | |||
const result = latencyWindowReducer( | |||
topologyDescription, | |||
tagSetReducer( | |||
readPreference, | |||
maxStalenessReducer(readPreference, topologyDescription, servers) | |||
) | |||
).filter(secondaryFilter); | |||
return result.length === 0 ? servers.filter(primaryFilter) : result; | |||
} else if (readPreference.mode === ReadPreference.PRIMARY_PREFERRED) { | |||
const result = servers.filter(primaryFilter); | |||
if (result.length) { | |||
return result; | |||
} | |||
return latencyWindowReducer( | |||
topologyDescription, | |||
tagSetReducer( | |||
readPreference, | |||
maxStalenessReducer(readPreference, topologyDescription, servers) | |||
) | |||
).filter(secondaryFilter); | |||
} | |||
}; | |||
} | |||
module.exports = { | |||
writableServerSelector, | |||
readPreferenceServerSelector | |||
}; |
@@ -0,0 +1,666 @@ | |||
'use strict'; | |||
const EventEmitter = require('events'); | |||
const ServerDescription = require('./server_description').ServerDescription; | |||
const TopologyDescription = require('./topology_description').TopologyDescription; | |||
const TopologyType = require('./topology_description').TopologyType; | |||
const monitoring = require('./monitoring'); | |||
const calculateDurationInMs = require('../utils').calculateDurationInMs; | |||
const MongoTimeoutError = require('../error').MongoTimeoutError; | |||
const MongoNetworkError = require('../error').MongoNetworkError; | |||
const Server = require('./server'); | |||
const relayEvents = require('../utils').relayEvents; | |||
const ReadPreference = require('../topologies/read_preference'); | |||
const readPreferenceServerSelector = require('./server_selectors').readPreferenceServerSelector; | |||
const writableServerSelector = require('./server_selectors').writableServerSelector; | |||
const isRetryableWritesSupported = require('../topologies/shared').isRetryableWritesSupported; | |||
const Cursor = require('./cursor'); | |||
const deprecate = require('util').deprecate; | |||
const BSON = require('../connection/utils').retrieveBSON(); | |||
const createCompressionInfo = require('../topologies/shared').createCompressionInfo; | |||
// Global state | |||
let globalTopologyCounter = 0; | |||
// Constants | |||
const TOPOLOGY_DEFAULTS = { | |||
localThresholdMS: 15, | |||
serverSelectionTimeoutMS: 10000, | |||
heartbeatFrequencyMS: 30000, | |||
minHeartbeatIntervalMS: 500 | |||
}; | |||
/** | |||
* A container of server instances representing a connection to a MongoDB topology. | |||
* | |||
* @fires Topology#serverOpening | |||
* @fires Topology#serverClosed | |||
* @fires Topology#serverDescriptionChanged | |||
* @fires Topology#topologyOpening | |||
* @fires Topology#topologyClosed | |||
* @fires Topology#topologyDescriptionChanged | |||
* @fires Topology#serverHeartbeatStarted | |||
* @fires Topology#serverHeartbeatSucceeded | |||
* @fires Topology#serverHeartbeatFailed | |||
*/ | |||
class Topology extends EventEmitter { | |||
/** | |||
* Create a topology | |||
* | |||
* @param {Array|String} [seedlist] a string list, or array of Server instances to connect to | |||
* @param {Object} [options] Optional settings | |||
* @param {Number} [options.localThresholdMS=15] The size of the latency window for selecting among multiple suitable servers | |||
* @param {Number} [options.serverSelectionTimeoutMS=30000] How long to block for server selection before throwing an error | |||
* @param {Number} [options.heartbeatFrequencyMS=10000] The frequency with which topology updates are scheduled | |||
*/ | |||
constructor(seedlist, options) { | |||
super(); | |||
if (typeof options === 'undefined') { | |||
options = seedlist; | |||
seedlist = []; | |||
// this is for legacy single server constructor support | |||
if (options.host) { | |||
seedlist.push({ host: options.host, port: options.port }); | |||
} | |||
} | |||
seedlist = seedlist || []; | |||
options = Object.assign({}, TOPOLOGY_DEFAULTS, options); | |||
const topologyType = topologyTypeFromSeedlist(seedlist, options); | |||
const topologyId = globalTopologyCounter++; | |||
const serverDescriptions = seedlist.reduce((result, seed) => { | |||
const address = seed.port ? `${seed.host}:${seed.port}` : `${seed.host}:27017`; | |||
result.set(address, new ServerDescription(address)); | |||
return result; | |||
}, new Map()); | |||
this.s = { | |||
// the id of this topology | |||
id: topologyId, | |||
// passed in options | |||
options: Object.assign({}, options), | |||
// initial seedlist of servers to connect to | |||
seedlist: seedlist, | |||
// the topology description | |||
description: new TopologyDescription( | |||
topologyType, | |||
serverDescriptions, | |||
options.replicaSet, | |||
null, | |||
null, | |||
options | |||
), | |||
serverSelectionTimeoutMS: options.serverSelectionTimeoutMS, | |||
heartbeatFrequencyMS: options.heartbeatFrequencyMS, | |||
minHeartbeatIntervalMS: options.minHeartbeatIntervalMS, | |||
// allow users to override the cursor factory | |||
Cursor: options.cursorFactory || Cursor, | |||
// the bson parser | |||
bson: | |||
options.bson || | |||
new BSON([ | |||
BSON.Binary, | |||
BSON.Code, | |||
BSON.DBRef, | |||
BSON.Decimal128, | |||
BSON.Double, | |||
BSON.Int32, | |||
BSON.Long, | |||
BSON.Map, | |||
BSON.MaxKey, | |||
BSON.MinKey, | |||
BSON.ObjectId, | |||
BSON.BSONRegExp, | |||
BSON.Symbol, | |||
BSON.Timestamp | |||
]) | |||
}; | |||
// amend options for server instance creation | |||
this.s.options.compression = { compressors: createCompressionInfo(options) }; | |||
} | |||
/** | |||
* @return A `TopologyDescription` for this topology | |||
*/ | |||
get description() { | |||
return this.s.description; | |||
} | |||
/** | |||
* All raw connections | |||
* @method | |||
* @return {Connection[]} | |||
*/ | |||
connections() { | |||
return Array.from(this.s.servers.values()).reduce((result, server) => { | |||
return result.concat(server.s.pool.allConnections()); | |||
}, []); | |||
} | |||
/** | |||
* Initiate server connect | |||
* | |||
* @param {Object} [options] Optional settings | |||
* @param {Array} [options.auth=null] Array of auth options to apply on connect | |||
*/ | |||
connect(/* options */) { | |||
// emit SDAM monitoring events | |||
this.emit('topologyOpening', new monitoring.TopologyOpeningEvent(this.s.id)); | |||
// emit an event for the topology change | |||
this.emit( | |||
'topologyDescriptionChanged', | |||
new monitoring.TopologyDescriptionChangedEvent( | |||
this.s.id, | |||
new TopologyDescription(TopologyType.Unknown), // initial is always Unknown | |||
this.s.description | |||
) | |||
); | |||
connectServers(this, Array.from(this.s.description.servers.values())); | |||
this.s.connected = true; | |||
} | |||
/** | |||
* Close this topology | |||
*/ | |||
close(callback) { | |||
// destroy all child servers | |||
this.s.servers.forEach(server => server.destroy()); | |||
// emit an event for close | |||
this.emit('topologyClosed', new monitoring.TopologyClosedEvent(this.s.id)); | |||
this.s.connected = false; | |||
if (typeof callback === 'function') { | |||
callback(null, null); | |||
} | |||
} | |||
/** | |||
* Selects a server according to the selection predicate provided | |||
* | |||
* @param {function} [selector] An optional selector to select servers by, defaults to a random selection within a latency window | |||
* @return {Server} An instance of a `Server` meeting the criteria of the predicate provided | |||
*/ | |||
selectServer(selector, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = Object.assign( | |||
{}, | |||
{ serverSelectionTimeoutMS: this.s.serverSelectionTimeoutMS }, | |||
options | |||
); | |||
selectServers( | |||
this, | |||
selector, | |||
options.serverSelectionTimeoutMS, | |||
process.hrtime(), | |||
(err, servers) => { | |||
if (err) return callback(err, null); | |||
callback(null, randomSelection(servers)); | |||
} | |||
); | |||
} | |||
/** | |||
* Update the internal TopologyDescription with a ServerDescription | |||
* | |||
* @param {object} serverDescription The server to update in the internal list of server descriptions | |||
*/ | |||
serverUpdateHandler(serverDescription) { | |||
if (!this.s.description.hasServer(serverDescription.address)) { | |||
return; | |||
} | |||
// these will be used for monitoring events later | |||
const previousTopologyDescription = this.s.description; | |||
const previousServerDescription = this.s.description.servers.get(serverDescription.address); | |||
// first update the TopologyDescription | |||
this.s.description = this.s.description.update(serverDescription); | |||
// emit monitoring events for this change | |||
this.emit( | |||
'serverDescriptionChanged', | |||
new monitoring.ServerDescriptionChangedEvent( | |||
this.s.id, | |||
serverDescription.address, | |||
previousServerDescription, | |||
this.s.description.servers.get(serverDescription.address) | |||
) | |||
); | |||
// update server list from updated descriptions | |||
updateServers(this, serverDescription); | |||
this.emit( | |||
'topologyDescriptionChanged', | |||
new monitoring.TopologyDescriptionChangedEvent( | |||
this.s.id, | |||
previousTopologyDescription, | |||
this.s.description | |||
) | |||
); | |||
} | |||
/** | |||
* Authenticate using a specified mechanism | |||
* | |||
* @param {String} mechanism The auth mechanism used for authentication | |||
* @param {String} db The db we are authenticating against | |||
* @param {Object} options Optional settings for the authenticating mechanism | |||
* @param {authResultCallback} callback A callback function | |||
*/ | |||
auth(mechanism, db, options, callback) { | |||
callback(null, null); | |||
} | |||
/** | |||
* Logout from a database | |||
* | |||
* @param {String} db The db we are logging out from | |||
* @param {authResultCallback} callback A callback function | |||
*/ | |||
logout(db, callback) { | |||
callback(null, null); | |||
} | |||
// Basic operation support. Eventually this should be moved into command construction | |||
// during the command refactor. | |||
/** | |||
* Insert one or more documents | |||
* | |||
* @param {String} ns The full qualified namespace for this operation | |||
* @param {Array} ops An array of documents to insert | |||
* @param {Boolean} [options.ordered=true] Execute in order or out of order | |||
* @param {Object} [options.writeConcern] Write concern for the operation | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields | |||
* @param {ClientSession} [options.session] Session to use for the operation | |||
* @param {boolean} [options.retryWrites] Enable retryable writes for this operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
insert(ns, ops, options, callback) { | |||
executeWriteOperation({ topology: this, op: 'insert', ns, ops }, options, callback); | |||
} | |||
/** | |||
* Perform one or more update operations | |||
* | |||
* @param {string} ns The fully qualified namespace for this operation | |||
* @param {array} ops An array of updates | |||
* @param {boolean} [options.ordered=true] Execute in order or out of order | |||
* @param {object} [options.writeConcern] Write concern for the operation | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields | |||
* @param {ClientSession} [options.session] Session to use for the operation | |||
* @param {boolean} [options.retryWrites] Enable retryable writes for this operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
update(ns, ops, options, callback) { | |||
executeWriteOperation({ topology: this, op: 'update', ns, ops }, options, callback); | |||
} | |||
/** | |||
* Perform one or more remove operations | |||
* | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {array} ops An array of removes | |||
* @param {boolean} [options.ordered=true] Execute in order or out of order | |||
* @param {object} [options.writeConcern={}] Write concern for the operation | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {boolean} [options.retryWrites] Enable retryable writes for this operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
remove(ns, ops, options, callback) { | |||
executeWriteOperation({ topology: this, op: 'remove', ns, ops }, options, callback); | |||
} | |||
/** | |||
* Execute a command | |||
* | |||
* @method | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {object} cmd The command hash | |||
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it | |||
* @param {Connection} [options.connection] Specify connection object to execute command against | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {opResultCallback} callback A callback function | |||
*/ | |||
command(ns, cmd, options, callback) { | |||
if (typeof options === 'function') { | |||
(callback = options), (options = {}), (options = options || {}); | |||
} | |||
const readPreference = options.readPreference ? options.readPreference : ReadPreference.primary; | |||
this.selectServer(readPreferenceServerSelector(readPreference), (err, server) => { | |||
if (err) { | |||
callback(err, null); | |||
return; | |||
} | |||
server.command(ns, cmd, options, callback); | |||
}); | |||
} | |||
/** | |||
* Create a new cursor | |||
* | |||
* @method | |||
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1) | |||
* @param {object|Long} cmd Can be either a command returning a cursor or a cursorId | |||
* @param {object} [options] Options for the cursor | |||
* @param {object} [options.batchSize=0] Batchsize for the operation | |||
* @param {array} [options.documents=[]] Initial documents list for cursor | |||
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it | |||
* @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {ClientSession} [options.session=null] Session to use for the operation | |||
* @param {object} [options.topology] The internal topology of the created cursor | |||
* @returns {Cursor} | |||
*/ | |||
cursor(ns, cmd, options) { | |||
options = options || {}; | |||
const topology = options.topology || this; | |||
const CursorClass = options.cursorFactory || this.s.Cursor; | |||
return new CursorClass(this.s.bson, ns, cmd, options, topology, this.s.options); | |||
} | |||
} | |||
// legacy aliases | |||
Topology.prototype.destroy = deprecate( | |||
Topology.prototype.close, | |||
'destroy() is deprecated, please use close() instead' | |||
); | |||
function topologyTypeFromSeedlist(seedlist, options) { | |||
if (seedlist.length === 1 && !options.replicaSet) return TopologyType.Single; | |||
if (options.replicaSet) return TopologyType.ReplicaSetNoPrimary; | |||
return TopologyType.Unknown; | |||
} | |||
function randomSelection(array) { | |||
return array[Math.floor(Math.random() * array.length)]; | |||
} | |||
/** | |||
* Selects servers using the provided selector | |||
* | |||
* @private | |||
* @param {Topology} topology The topology to select servers from | |||
* @param {function} selector The actual predicate used for selecting servers | |||
* @param {Number} timeout The max time we are willing wait for selection | |||
* @param {Number} start A high precision timestamp for the start of the selection process | |||
* @param {function} callback The callback used to convey errors or the resultant servers | |||
*/ | |||
function selectServers(topology, selector, timeout, start, callback) { | |||
const serverDescriptions = Array.from(topology.description.servers.values()); | |||
let descriptions; | |||
try { | |||
descriptions = selector | |||
? selector(topology.description, serverDescriptions) | |||
: serverDescriptions; | |||
} catch (e) { | |||
return callback(e, null); | |||
} | |||
if (descriptions.length) { | |||
const servers = descriptions.map(description => topology.s.servers.get(description.address)); | |||
return callback(null, servers); | |||
} | |||
const duration = calculateDurationInMs(start); | |||
if (duration >= timeout) { | |||
return callback(new MongoTimeoutError(`Server selection timed out after ${timeout} ms`)); | |||
} | |||
const retrySelection = () => { | |||
// ensure all server monitors attempt monitoring immediately | |||
topology.s.servers.forEach(server => server.monitor()); | |||
const iterationTimer = setTimeout(() => { | |||
callback(new MongoTimeoutError('Server selection timed out due to monitoring')); | |||
}, topology.s.minHeartbeatIntervalMS); | |||
topology.once('topologyDescriptionChanged', () => { | |||
// successful iteration, clear the check timer | |||
clearTimeout(iterationTimer); | |||
// topology description has changed due to monitoring, reattempt server selection | |||
selectServers(topology, selector, timeout, start, callback); | |||
}); | |||
}; | |||
// ensure we are connected | |||
if (!topology.s.connected) { | |||
topology.connect(); | |||
// we want to make sure we're still within the requested timeout window | |||
const failToConnectTimer = setTimeout(() => { | |||
callback(new MongoTimeoutError('Server selection timed out waiting to connect')); | |||
}, timeout - duration); | |||
topology.once('connect', () => { | |||
clearTimeout(failToConnectTimer); | |||
retrySelection(); | |||
}); | |||
return; | |||
} | |||
retrySelection(); | |||
} | |||
/** | |||
* Create `Server` instances for all initially known servers, connect them, and assign | |||
* them to the passed in `Topology`. | |||
* | |||
* @param {Topology} topology The topology responsible for the servers | |||
* @param {ServerDescription[]} serverDescriptions A list of server descriptions to connect | |||
*/ | |||
function connectServers(topology, serverDescriptions) { | |||
topology.s.servers = serverDescriptions.reduce((servers, serverDescription) => { | |||
// publish an open event for each ServerDescription created | |||
topology.emit( | |||
'serverOpening', | |||
new monitoring.ServerOpeningEvent(topology.s.id, serverDescription.address) | |||
); | |||
const server = new Server(serverDescription, topology.s.options); | |||
relayEvents(server, topology, [ | |||
'serverHeartbeatStarted', | |||
'serverHeartbeatSucceeded', | |||
'serverHeartbeatFailed' | |||
]); | |||
server.on('descriptionReceived', topology.serverUpdateHandler.bind(topology)); | |||
server.on('connect', serverConnectEventHandler(server, topology)); | |||
servers.set(serverDescription.address, server); | |||
server.connect(); | |||
return servers; | |||
}, new Map()); | |||
} | |||
function updateServers(topology, currentServerDescription) { | |||
// update the internal server's description | |||
if (topology.s.servers.has(currentServerDescription.address)) { | |||
const server = topology.s.servers.get(currentServerDescription.address); | |||
server.s.description = currentServerDescription; | |||
} | |||
// add new servers for all descriptions we currently don't know about locally | |||
for (const serverDescription of topology.description.servers.values()) { | |||
if (!topology.s.servers.has(serverDescription.address)) { | |||
topology.emit( | |||
'serverOpening', | |||
new monitoring.ServerOpeningEvent(topology.s.id, serverDescription.address) | |||
); | |||
const server = new Server(serverDescription, topology.s.options); | |||
relayEvents(server, topology, [ | |||
'serverHeartbeatStarted', | |||
'serverHeartbeatSucceeded', | |||
'serverHeartbeatFailed' | |||
]); | |||
server.on('descriptionReceived', topology.serverUpdateHandler.bind(topology)); | |||
server.on('connect', serverConnectEventHandler(server, topology)); | |||
topology.s.servers.set(serverDescription.address, server); | |||
server.connect(); | |||
} | |||
} | |||
// for all servers no longer known, remove their descriptions and destroy their instances | |||
for (const entry of topology.s.servers) { | |||
const serverAddress = entry[0]; | |||
if (topology.description.hasServer(serverAddress)) { | |||
continue; | |||
} | |||
const server = topology.s.servers.get(serverAddress); | |||
topology.s.servers.delete(serverAddress); | |||
server.destroy(() => | |||
topology.emit('serverClosed', new monitoring.ServerClosedEvent(topology.s.id, serverAddress)) | |||
); | |||
} | |||
} | |||
function serverConnectEventHandler(server, topology) { | |||
return function(/* ismaster */) { | |||
topology.emit('connect', topology); | |||
}; | |||
} | |||
function executeWriteOperation(args, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// TODO: once we drop Node 4, use destructuring either here or in arguments. | |||
const topology = args.topology; | |||
const op = args.op; | |||
const ns = args.ns; | |||
const ops = args.ops; | |||
const willRetryWrite = | |||
!args.retrying && | |||
options.retryWrites && | |||
options.session && | |||
isRetryableWritesSupported(topology) && | |||
!options.session.inTransaction(); | |||
topology.selectServer(writableServerSelector(), (err, server) => { | |||
if (err) { | |||
callback(err, null); | |||
return; | |||
} | |||
const handler = (err, result) => { | |||
if (!err) return callback(null, result); | |||
if (!(err instanceof MongoNetworkError) && !err.message.match(/not master/)) { | |||
return callback(err); | |||
} | |||
if (willRetryWrite) { | |||
const newArgs = Object.assign({}, args, { retrying: true }); | |||
return executeWriteOperation(newArgs, options, callback); | |||
} | |||
return callback(err); | |||
}; | |||
if (callback.operationId) { | |||
handler.operationId = callback.operationId; | |||
} | |||
// increment and assign txnNumber | |||
if (willRetryWrite) { | |||
options.session.incrementTransactionNumber(); | |||
options.willRetryWrite = willRetryWrite; | |||
} | |||
// execute the write operation | |||
server[op](ns, ops, options, handler); | |||
// we need to increment the statement id if we're in a transaction | |||
if (options.session && options.session.inTransaction()) { | |||
options.session.incrementStatementId(ops.length); | |||
} | |||
}); | |||
} | |||
/** | |||
* A server opening SDAM monitoring event | |||
* | |||
* @event Topology#serverOpening | |||
* @type {ServerOpeningEvent} | |||
*/ | |||
/** | |||
* A server closed SDAM monitoring event | |||
* | |||
* @event Topology#serverClosed | |||
* @type {ServerClosedEvent} | |||
*/ | |||
/** | |||
* A server description SDAM change monitoring event | |||
* | |||
* @event Topology#serverDescriptionChanged | |||
* @type {ServerDescriptionChangedEvent} | |||
*/ | |||
/** | |||
* A topology open SDAM event | |||
* | |||
* @event Topology#topologyOpening | |||
* @type {TopologyOpeningEvent} | |||
*/ | |||
/** | |||
* A topology closed SDAM event | |||
* | |||
* @event Topology#topologyClosed | |||
* @type {TopologyClosedEvent} | |||
*/ | |||
/** | |||
* A topology structure SDAM change event | |||
* | |||
* @event Topology#topologyDescriptionChanged | |||
* @type {TopologyDescriptionChangedEvent} | |||
*/ | |||
/** | |||
* A topology serverHeartbeatStarted SDAM event | |||
* | |||
* @event Topology#serverHeartbeatStarted | |||
* @type {ServerHeartbeatStartedEvent} | |||
*/ | |||
/** | |||
* A topology serverHeartbeatFailed SDAM event | |||
* | |||
* @event Topology#serverHeartbeatFailed | |||
* @type {ServerHearbeatFailedEvent} | |||
*/ | |||
/** | |||
* A topology serverHeartbeatSucceeded SDAM change event | |||
* | |||
* @event Topology#serverHeartbeatSucceeded | |||
* @type {ServerHeartbeatSucceededEvent} | |||
*/ | |||
module.exports = Topology; |
@@ -0,0 +1,364 @@ | |||
'use strict'; | |||
const ServerType = require('./server_description').ServerType; | |||
const ServerDescription = require('./server_description').ServerDescription; | |||
const ReadPreference = require('../topologies/read_preference'); | |||
// contstants related to compatability checks | |||
const MIN_SUPPORTED_SERVER_VERSION = '2.6'; | |||
const MIN_SUPPORTED_WIRE_VERSION = 2; | |||
const MAX_SUPPORTED_WIRE_VERSION = 5; | |||
// An enumeration of topology types we know about | |||
const TopologyType = { | |||
Single: 'Single', | |||
ReplicaSetNoPrimary: 'ReplicaSetNoPrimary', | |||
ReplicaSetWithPrimary: 'ReplicaSetWithPrimary', | |||
Sharded: 'Sharded', | |||
Unknown: 'Unknown' | |||
}; | |||
// Representation of a deployment of servers | |||
class TopologyDescription { | |||
/** | |||
* Create a TopologyDescription | |||
* | |||
* @param {string} topologyType | |||
* @param {Map<string, ServerDescription>} serverDescriptions the a map of address to ServerDescription | |||
* @param {string} setName | |||
* @param {number} maxSetVersion | |||
* @param {ObjectId} maxElectionId | |||
*/ | |||
constructor(topologyType, serverDescriptions, setName, maxSetVersion, maxElectionId, options) { | |||
options = options || {}; | |||
// TODO: consider assigning all these values to a temporary value `s` which | |||
// we use `Object.freeze` on, ensuring the internal state of this type | |||
// is immutable. | |||
this.type = topologyType || TopologyType.Unknown; | |||
this.setName = setName || null; | |||
this.maxSetVersion = maxSetVersion || null; | |||
this.maxElectionId = maxElectionId || null; | |||
this.servers = serverDescriptions || new Map(); | |||
this.stale = false; | |||
this.compatible = true; | |||
this.compatibilityError = null; | |||
this.logicalSessionTimeoutMinutes = null; | |||
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS || 0; | |||
this.localThresholdMS = options.localThresholdMS || 0; | |||
this.options = options; | |||
// determine server compatibility | |||
for (const serverDescription of this.servers.values()) { | |||
if (serverDescription.minWireVersion > MAX_SUPPORTED_WIRE_VERSION) { | |||
this.compatible = false; | |||
this.compatibilityError = `Server at ${serverDescription.address} requires wire version ${ | |||
serverDescription.minWireVersion | |||
}, but this version of the driver only supports up to ${MAX_SUPPORTED_WIRE_VERSION}.`; | |||
} | |||
if (serverDescription.maxWireVersion < MIN_SUPPORTED_WIRE_VERSION) { | |||
this.compatible = false; | |||
this.compatibilityError = `Server at ${serverDescription.address} reports wire version ${ | |||
serverDescription.maxWireVersion | |||
}, but this version of the driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION}).`; | |||
break; | |||
} | |||
} | |||
// Whenever a client updates the TopologyDescription from an ismaster response, it MUST set | |||
// TopologyDescription.logicalSessionTimeoutMinutes to the smallest logicalSessionTimeoutMinutes | |||
// value among ServerDescriptions of all data-bearing server types. If any have a null | |||
// logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be | |||
// set to null. | |||
const readableServers = Array.from(this.servers.values()).filter(s => s.isReadable); | |||
this.logicalSessionTimeoutMinutes = readableServers.reduce((result, server) => { | |||
if (server.logicalSessionTimeoutMinutes == null) return null; | |||
if (result == null) return server.logicalSessionTimeoutMinutes; | |||
return Math.min(result, server.logicalSessionTimeoutMinutes); | |||
}, null); | |||
} | |||
/** | |||
* @returns The minimum reported wire version of all known servers | |||
*/ | |||
get commonWireVersion() { | |||
return Array.from(this.servers.values()) | |||
.filter(server => server.type !== ServerType.Unknown) | |||
.reduce( | |||
(min, server) => | |||
min == null ? server.maxWireVersion : Math.min(min, server.maxWireVersion), | |||
null | |||
); | |||
} | |||
/** | |||
* Returns a copy of this description updated with a given ServerDescription | |||
* | |||
* @param {ServerDescription} serverDescription | |||
*/ | |||
update(serverDescription) { | |||
const address = serverDescription.address; | |||
// NOTE: there are a number of prime targets for refactoring here | |||
// once we support destructuring assignments | |||
// potentially mutated values | |||
let topologyType = this.type; | |||
let setName = this.setName; | |||
let maxSetVersion = this.maxSetVersion; | |||
let maxElectionId = this.maxElectionId; | |||
const serverType = serverDescription.type; | |||
let serverDescriptions = new Map(this.servers); | |||
// update the actual server description | |||
serverDescriptions.set(address, serverDescription); | |||
if (topologyType === TopologyType.Single) { | |||
// once we are defined as single, that never changes | |||
return new TopologyDescription( | |||
TopologyType.Single, | |||
serverDescriptions, | |||
setName, | |||
maxSetVersion, | |||
maxElectionId, | |||
this.options | |||
); | |||
} | |||
if (topologyType === TopologyType.Unknown) { | |||
if (serverType === ServerType.Standalone) { | |||
serverDescriptions.delete(address); | |||
} else { | |||
topologyType = topologyTypeForServerType(serverType); | |||
} | |||
} | |||
if (topologyType === TopologyType.Sharded) { | |||
if ([ServerType.Mongos, ServerType.Unknown].indexOf(serverType) === -1) { | |||
serverDescriptions.delete(address); | |||
} | |||
} | |||
if (topologyType === TopologyType.ReplicaSetNoPrimary) { | |||
if ([ServerType.Mongos, ServerType.Unknown].indexOf(serverType) >= 0) { | |||
serverDescriptions.delete(address); | |||
} | |||
if (serverType === ServerType.RSPrimary) { | |||
const result = updateRsFromPrimary( | |||
serverDescriptions, | |||
setName, | |||
serverDescription, | |||
maxSetVersion, | |||
maxElectionId | |||
); | |||
(topologyType = result[0]), | |||
(setName = result[1]), | |||
(maxSetVersion = result[2]), | |||
(maxElectionId = result[3]); | |||
} else if ( | |||
[ServerType.RSSecondary, ServerType.RSArbiter, ServerType.RSOther].indexOf(serverType) >= 0 | |||
) { | |||
const result = updateRsNoPrimaryFromMember(serverDescriptions, setName, serverDescription); | |||
(topologyType = result[0]), (setName = result[1]); | |||
} | |||
} | |||
if (topologyType === TopologyType.ReplicaSetWithPrimary) { | |||
if ([ServerType.Standalone, ServerType.Mongos].indexOf(serverType) >= 0) { | |||
serverDescriptions.delete(address); | |||
topologyType = checkHasPrimary(serverDescriptions); | |||
} else if (serverType === ServerType.RSPrimary) { | |||
const result = updateRsFromPrimary( | |||
serverDescriptions, | |||
setName, | |||
serverDescription, | |||
maxSetVersion, | |||
maxElectionId | |||
); | |||
(topologyType = result[0]), | |||
(setName = result[1]), | |||
(maxSetVersion = result[2]), | |||
(maxElectionId = result[3]); | |||
} else if ( | |||
[ServerType.RSSecondary, ServerType.RSArbiter, ServerType.RSOther].indexOf(serverType) >= 0 | |||
) { | |||
topologyType = updateRsWithPrimaryFromMember( | |||
serverDescriptions, | |||
setName, | |||
serverDescription | |||
); | |||
} else { | |||
topologyType = checkHasPrimary(serverDescriptions); | |||
} | |||
} | |||
return new TopologyDescription( | |||
topologyType, | |||
serverDescriptions, | |||
setName, | |||
maxSetVersion, | |||
maxElectionId, | |||
this.options | |||
); | |||
} | |||
/** | |||
* Determines if the topology has a readable server available. See the table in the | |||
* following section for behaviour rules. | |||
* | |||
* @param {ReadPreference} [readPreference] An optional read preference for determining if a readable server is present | |||
* @return {Boolean} Whether there is a readable server in this topology | |||
*/ | |||
hasReadableServer(/* readPreference */) { | |||
// To be implemented when server selection is implemented | |||
} | |||
/** | |||
* Determines if the topology has a writable server available. See the table in the | |||
* following section for behaviour rules. | |||
* | |||
* @return {Boolean} Whether there is a writable server in this topology | |||
*/ | |||
hasWritableServer() { | |||
return this.hasReadableServer(ReadPreference.primary); | |||
} | |||
/** | |||
* Determines if the topology has a definition for the provided address | |||
* | |||
* @param {String} address | |||
* @return {Boolean} Whether the topology knows about this server | |||
*/ | |||
hasServer(address) { | |||
return this.servers.has(address); | |||
} | |||
} | |||
function topologyTypeForServerType(serverType) { | |||
if (serverType === ServerType.Mongos) return TopologyType.Sharded; | |||
if (serverType === ServerType.RSPrimary) return TopologyType.ReplicaSetWithPrimary; | |||
return TopologyType.ReplicaSetNoPrimary; | |||
} | |||
function updateRsFromPrimary( | |||
serverDescriptions, | |||
setName, | |||
serverDescription, | |||
maxSetVersion, | |||
maxElectionId | |||
) { | |||
setName = setName || serverDescription.setName; | |||
if (setName !== serverDescription.setName) { | |||
serverDescriptions.delete(serverDescription.address); | |||
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; | |||
} | |||
const electionIdOID = serverDescription.electionId ? serverDescription.electionId.$oid : null; | |||
const maxElectionIdOID = maxElectionId ? maxElectionId.$oid : null; | |||
if (serverDescription.setVersion != null && electionIdOID != null) { | |||
if (maxSetVersion != null && maxElectionIdOID != null) { | |||
if (maxSetVersion > serverDescription.setVersion || maxElectionIdOID > electionIdOID) { | |||
// this primary is stale, we must remove it | |||
serverDescriptions.set( | |||
serverDescription.address, | |||
new ServerDescription(serverDescription.address) | |||
); | |||
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; | |||
} | |||
} | |||
maxElectionId = serverDescription.electionId; | |||
} | |||
if ( | |||
serverDescription.setVersion != null && | |||
(maxSetVersion == null || serverDescription.setVersion > maxSetVersion) | |||
) { | |||
maxSetVersion = serverDescription.setVersion; | |||
} | |||
// We've heard from the primary. Is it the same primary as before? | |||
for (const address of serverDescriptions.keys()) { | |||
const server = serverDescriptions.get(address); | |||
if (server.type === ServerType.RSPrimary && server.address !== serverDescription.address) { | |||
// Reset old primary's type to Unknown. | |||
serverDescriptions.set(address, new ServerDescription(server.address)); | |||
// There can only be one primary | |||
break; | |||
} | |||
} | |||
// Discover new hosts from this primary's response. | |||
serverDescription.allHosts.forEach(address => { | |||
if (!serverDescriptions.has(address)) { | |||
serverDescriptions.set(address, new ServerDescription(address)); | |||
} | |||
}); | |||
// Remove hosts not in the response. | |||
const currentAddresses = Array.from(serverDescriptions.keys()); | |||
const responseAddresses = serverDescription.allHosts; | |||
currentAddresses.filter(addr => responseAddresses.indexOf(addr) === -1).forEach(address => { | |||
serverDescriptions.delete(address); | |||
}); | |||
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; | |||
} | |||
function updateRsWithPrimaryFromMember(serverDescriptions, setName, serverDescription) { | |||
if (setName == null) { | |||
throw new TypeError('setName is required'); | |||
} | |||
if ( | |||
setName !== serverDescription.setName || | |||
(serverDescription.me && serverDescription.address !== serverDescription.me) | |||
) { | |||
serverDescriptions.delete(serverDescription.address); | |||
} | |||
return checkHasPrimary(serverDescriptions); | |||
} | |||
function updateRsNoPrimaryFromMember(serverDescriptions, setName, serverDescription) { | |||
let topologyType = TopologyType.ReplicaSetNoPrimary; | |||
setName = setName || serverDescription.setName; | |||
if (setName !== serverDescription.setName) { | |||
serverDescriptions.delete(serverDescription.address); | |||
return [topologyType, setName]; | |||
} | |||
serverDescription.allHosts.forEach(address => { | |||
if (!serverDescriptions.has(address)) { | |||
serverDescriptions.set(address, new ServerDescription(address)); | |||
} | |||
}); | |||
if (serverDescription.me && serverDescription.address !== serverDescription.me) { | |||
serverDescriptions.delete(serverDescription.address); | |||
} | |||
return [topologyType, setName]; | |||
} | |||
function checkHasPrimary(serverDescriptions) { | |||
for (const addr of serverDescriptions.keys()) { | |||
if (serverDescriptions.get(addr).type === ServerType.RSPrimary) { | |||
return TopologyType.ReplicaSetWithPrimary; | |||
} | |||
} | |||
return TopologyType.ReplicaSetNoPrimary; | |||
} | |||
module.exports = { | |||
TopologyType, | |||
TopologyDescription | |||
}; |
@@ -0,0 +1,459 @@ | |||
'use strict'; | |||
const retrieveBSON = require('./connection/utils').retrieveBSON; | |||
const EventEmitter = require('events'); | |||
const BSON = retrieveBSON(); | |||
const Binary = BSON.Binary; | |||
const uuidV4 = require('./utils').uuidV4; | |||
const MongoError = require('./error').MongoError; | |||
const isRetryableError = require('././error').isRetryableError; | |||
const MongoNetworkError = require('./error').MongoNetworkError; | |||
const MongoWriteConcernError = require('./error').MongoWriteConcernError; | |||
const Transaction = require('./transactions').Transaction; | |||
const TxnState = require('./transactions').TxnState; | |||
function assertAlive(session, callback) { | |||
if (session.serverSession == null) { | |||
const error = new MongoError('Cannot use a session that has ended'); | |||
if (typeof callback === 'function') { | |||
callback(error, null); | |||
return false; | |||
} | |||
throw error; | |||
} | |||
return true; | |||
} | |||
/** | |||
* Options to pass when creating a Client Session | |||
* @typedef {Object} SessionOptions | |||
* @property {boolean} [causalConsistency=true] Whether causal consistency should be enabled on this session | |||
* @property {TransactionOptions} [defaultTransactionOptions] The default TransactionOptions to use for transactions started on this session. | |||
*/ | |||
/** | |||
* A BSON document reflecting the lsid of a {@link ClientSession} | |||
* @typedef {Object} SessionId | |||
*/ | |||
/** | |||
* A class representing a client session on the server | |||
* WARNING: not meant to be instantiated directly. | |||
* @class | |||
* @hideconstructor | |||
*/ | |||
class ClientSession extends EventEmitter { | |||
/** | |||
* Create a client session. | |||
* WARNING: not meant to be instantiated directly | |||
* | |||
* @param {Topology} topology The current client's topology (Internal Class) | |||
* @param {ServerSessionPool} sessionPool The server session pool (Internal Class) | |||
* @param {SessionOptions} [options] Optional settings | |||
* @param {Object} [clientOptions] Optional settings provided when creating a client in the porcelain driver | |||
*/ | |||
constructor(topology, sessionPool, options, clientOptions) { | |||
super(); | |||
if (topology == null) { | |||
throw new Error('ClientSession requires a topology'); | |||
} | |||
if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) { | |||
throw new Error('ClientSession requires a ServerSessionPool'); | |||
} | |||
options = options || {}; | |||
this.topology = topology; | |||
this.sessionPool = sessionPool; | |||
this.hasEnded = false; | |||
this.serverSession = sessionPool.acquire(); | |||
this.clientOptions = clientOptions; | |||
this.supports = { | |||
causalConsistency: | |||
typeof options.causalConsistency !== 'undefined' ? options.causalConsistency : true | |||
}; | |||
options = options || {}; | |||
if (typeof options.initialClusterTime !== 'undefined') { | |||
this.clusterTime = options.initialClusterTime; | |||
} else { | |||
this.clusterTime = null; | |||
} | |||
this.operationTime = null; | |||
this.explicit = !!options.explicit; | |||
this.owner = options.owner; | |||
this.defaultTransactionOptions = Object.assign({}, options.defaultTransactionOptions); | |||
this.transaction = new Transaction(); | |||
} | |||
/** | |||
* The server id associated with this session | |||
* @type {SessionId} | |||
*/ | |||
get id() { | |||
return this.serverSession.id; | |||
} | |||
/** | |||
* Ends this session on the server | |||
* | |||
* @param {Object} [options] Optional settings. Currently reserved for future use | |||
* @param {Function} [callback] Optional callback for completion of this operation | |||
*/ | |||
endSession(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
if (this.hasEnded) { | |||
if (typeof callback === 'function') callback(null, null); | |||
return; | |||
} | |||
if (this.serverSession && this.inTransaction()) { | |||
this.abortTransaction(); // pass in callback? | |||
} | |||
// mark the session as ended, and emit a signal | |||
this.hasEnded = true; | |||
this.emit('ended', this); | |||
// release the server session back to the pool | |||
this.sessionPool.release(this.serverSession); | |||
// spec indicates that we should ignore all errors for `endSessions` | |||
if (typeof callback === 'function') callback(null, null); | |||
} | |||
/** | |||
* Advances the operationTime for a ClientSession. | |||
* | |||
* @param {Timestamp} operationTime the `BSON.Timestamp` of the operation type it is desired to advance to | |||
*/ | |||
advanceOperationTime(operationTime) { | |||
if (this.operationTime == null) { | |||
this.operationTime = operationTime; | |||
return; | |||
} | |||
if (operationTime.greaterThan(this.operationTime)) { | |||
this.operationTime = operationTime; | |||
} | |||
} | |||
/** | |||
* Used to determine if this session equals another | |||
* @param {ClientSession} session | |||
* @return {boolean} true if the sessions are equal | |||
*/ | |||
equals(session) { | |||
if (!(session instanceof ClientSession)) { | |||
return false; | |||
} | |||
return this.id.id.buffer.equals(session.id.id.buffer); | |||
} | |||
/** | |||
* Increment the transaction number on the internal ServerSession | |||
*/ | |||
incrementTransactionNumber() { | |||
this.serverSession.txnNumber++; | |||
} | |||
/** | |||
* @returns {boolean} whether this session is currently in a transaction or not | |||
*/ | |||
inTransaction() { | |||
return this.transaction.isActive; | |||
} | |||
/** | |||
* Starts a new transaction with the given options. | |||
* | |||
* @param {TransactionOptions} options Options for the transaction | |||
*/ | |||
startTransaction(options) { | |||
assertAlive(this); | |||
if (this.inTransaction()) { | |||
throw new MongoError('Transaction already in progress'); | |||
} | |||
// increment txnNumber | |||
this.incrementTransactionNumber(); | |||
// create transaction state | |||
this.transaction = new Transaction( | |||
Object.assign({}, this.clientOptions, options || this.defaultTransactionOptions) | |||
); | |||
this.transaction.transition(TxnState.STARTING_TRANSACTION); | |||
} | |||
/** | |||
* Commits the currently active transaction in this session. | |||
* | |||
* @param {Function} [callback] optional callback for completion of this operation | |||
* @return {Promise} A promise is returned if no callback is provided | |||
*/ | |||
commitTransaction(callback) { | |||
if (typeof callback === 'function') { | |||
endTransaction(this, 'commitTransaction', callback); | |||
return; | |||
} | |||
return new Promise((resolve, reject) => { | |||
endTransaction( | |||
this, | |||
'commitTransaction', | |||
(err, reply) => (err ? reject(err) : resolve(reply)) | |||
); | |||
}); | |||
} | |||
/** | |||
* Aborts the currently active transaction in this session. | |||
* | |||
* @param {Function} [callback] optional callback for completion of this operation | |||
* @return {Promise} A promise is returned if no callback is provided | |||
*/ | |||
abortTransaction(callback) { | |||
if (typeof callback === 'function') { | |||
endTransaction(this, 'abortTransaction', callback); | |||
return; | |||
} | |||
return new Promise((resolve, reject) => { | |||
endTransaction( | |||
this, | |||
'abortTransaction', | |||
(err, reply) => (err ? reject(err) : resolve(reply)) | |||
); | |||
}); | |||
} | |||
/** | |||
* This is here to ensure that ClientSession is never serialized to BSON. | |||
* @ignore | |||
*/ | |||
toBSON() { | |||
throw new Error('ClientSession cannot be serialized to BSON.'); | |||
} | |||
} | |||
function endTransaction(session, commandName, callback) { | |||
if (!assertAlive(session, callback)) { | |||
// checking result in case callback was called | |||
return; | |||
} | |||
// handle any initial problematic cases | |||
let txnState = session.transaction.state; | |||
if (txnState === TxnState.NO_TRANSACTION) { | |||
callback(new MongoError('No transaction started')); | |||
return; | |||
} | |||
if (commandName === 'commitTransaction') { | |||
if ( | |||
txnState === TxnState.STARTING_TRANSACTION || | |||
txnState === TxnState.TRANSACTION_COMMITTED_EMPTY | |||
) { | |||
// the transaction was never started, we can safely exit here | |||
session.transaction.transition(TxnState.TRANSACTION_COMMITTED_EMPTY); | |||
callback(null, null); | |||
return; | |||
} | |||
if (txnState === TxnState.TRANSACTION_ABORTED) { | |||
callback(new MongoError('Cannot call commitTransaction after calling abortTransaction')); | |||
return; | |||
} | |||
} else { | |||
if (txnState === TxnState.STARTING_TRANSACTION) { | |||
// the transaction was never started, we can safely exit here | |||
session.transaction.transition(TxnState.TRANSACTION_ABORTED); | |||
callback(null, null); | |||
return; | |||
} | |||
if (txnState === TxnState.TRANSACTION_ABORTED) { | |||
callback(new MongoError('Cannot call abortTransaction twice')); | |||
return; | |||
} | |||
if ( | |||
txnState === TxnState.TRANSACTION_COMMITTED || | |||
txnState === TxnState.TRANSACTION_COMMITTED_EMPTY | |||
) { | |||
callback(new MongoError('Cannot call abortTransaction after calling commitTransaction')); | |||
return; | |||
} | |||
} | |||
// construct and send the command | |||
const command = { [commandName]: 1 }; | |||
// apply a writeConcern if specified | |||
if (session.transaction.options.writeConcern) { | |||
Object.assign(command, { writeConcern: session.transaction.options.writeConcern }); | |||
} else if (session.clientOptions && session.clientOptions.w) { | |||
Object.assign(command, { writeConcern: { w: session.clientOptions.w } }); | |||
} | |||
function commandHandler(e, r) { | |||
if (commandName === 'commitTransaction') { | |||
session.transaction.transition(TxnState.TRANSACTION_COMMITTED); | |||
if ( | |||
e && | |||
(e instanceof MongoNetworkError || | |||
e instanceof MongoWriteConcernError || | |||
isRetryableError(e)) | |||
) { | |||
if (e.errorLabels) { | |||
const idx = e.errorLabels.indexOf('TransientTransactionError'); | |||
if (idx !== -1) { | |||
e.errorLabels.splice(idx, 1); | |||
} | |||
} else { | |||
e.errorLabels = []; | |||
} | |||
e.errorLabels.push('UnknownTransactionCommitResult'); | |||
} | |||
} else { | |||
session.transaction.transition(TxnState.TRANSACTION_ABORTED); | |||
} | |||
callback(e, r); | |||
} | |||
// The spec indicates that we should ignore all errors on `abortTransaction` | |||
function transactionError(err) { | |||
return commandName === 'commitTransaction' ? err : null; | |||
} | |||
// send the command | |||
session.topology.command('admin.$cmd', command, { session }, (err, reply) => { | |||
if (err && isRetryableError(err)) { | |||
return session.topology.command('admin.$cmd', command, { session }, (_err, _reply) => | |||
commandHandler(transactionError(_err), _reply) | |||
); | |||
} | |||
commandHandler(transactionError(err), reply); | |||
}); | |||
} | |||
/** | |||
* Reflects the existence of a session on the server. Can be reused by the session pool. | |||
* WARNING: not meant to be instantiated directly. For internal use only. | |||
* @ignore | |||
*/ | |||
class ServerSession { | |||
constructor() { | |||
this.id = { id: new Binary(uuidV4(), Binary.SUBTYPE_UUID) }; | |||
this.lastUse = Date.now(); | |||
this.txnNumber = 0; | |||
} | |||
/** | |||
* Determines if the server session has timed out. | |||
* @ignore | |||
* @param {Date} sessionTimeoutMinutes The server's "logicalSessionTimeoutMinutes" | |||
* @return {boolean} true if the session has timed out. | |||
*/ | |||
hasTimedOut(sessionTimeoutMinutes) { | |||
// Take the difference of the lastUse timestamp and now, which will result in a value in | |||
// milliseconds, and then convert milliseconds to minutes to compare to `sessionTimeoutMinutes` | |||
const idleTimeMinutes = Math.round( | |||
(((Date.now() - this.lastUse) % 86400000) % 3600000) / 60000 | |||
); | |||
return idleTimeMinutes > sessionTimeoutMinutes - 1; | |||
} | |||
} | |||
/** | |||
* Maintains a pool of Server Sessions. | |||
* For internal use only | |||
* @ignore | |||
*/ | |||
class ServerSessionPool { | |||
constructor(topology) { | |||
if (topology == null) { | |||
throw new Error('ServerSessionPool requires a topology'); | |||
} | |||
this.topology = topology; | |||
this.sessions = []; | |||
} | |||
/** | |||
* Ends all sessions in the session pool. | |||
* @ignore | |||
*/ | |||
endAllPooledSessions() { | |||
if (this.sessions.length) { | |||
this.topology.endSessions(this.sessions.map(session => session.id)); | |||
this.sessions = []; | |||
} | |||
} | |||
/** | |||
* Acquire a Server Session from the pool. | |||
* Iterates through each session in the pool, removing any stale sessions | |||
* along the way. The first non-stale session found is removed from the | |||
* pool and returned. If no non-stale session is found, a new ServerSession | |||
* is created. | |||
* @ignore | |||
* @returns {ServerSession} | |||
*/ | |||
acquire() { | |||
const sessionTimeoutMinutes = this.topology.logicalSessionTimeoutMinutes; | |||
while (this.sessions.length) { | |||
const session = this.sessions.shift(); | |||
if (!session.hasTimedOut(sessionTimeoutMinutes)) { | |||
return session; | |||
} | |||
} | |||
return new ServerSession(); | |||
} | |||
/** | |||
* Release a session to the session pool | |||
* Adds the session back to the session pool if the session has not timed out yet. | |||
* This method also removes any stale sessions from the pool. | |||
* @ignore | |||
* @param {ServerSession} session The session to release to the pool | |||
*/ | |||
release(session) { | |||
const sessionTimeoutMinutes = this.topology.logicalSessionTimeoutMinutes; | |||
while (this.sessions.length) { | |||
const session = this.sessions[this.sessions.length - 1]; | |||
if (session.hasTimedOut(sessionTimeoutMinutes)) { | |||
this.sessions.pop(); | |||
} else { | |||
break; | |||
} | |||
} | |||
if (!session.hasTimedOut(sessionTimeoutMinutes)) { | |||
this.sessions.unshift(session); | |||
} | |||
} | |||
} | |||
module.exports = { | |||
ClientSession, | |||
ServerSession, | |||
ServerSessionPool, | |||
TxnState | |||
}; |
@@ -0,0 +1,61 @@ | |||
'use strict'; | |||
var fs = require('fs'); | |||
/* Note: because this plugin uses process.on('uncaughtException'), only one | |||
* of these can exist at any given time. This plugin and anything else that | |||
* uses process.on('uncaughtException') will conflict. */ | |||
exports.attachToRunner = function(runner, outputFile) { | |||
var smokeOutput = { results: [] }; | |||
var runningTests = {}; | |||
var integraPlugin = { | |||
beforeTest: function(test, callback) { | |||
test.startTime = Date.now(); | |||
runningTests[test.name] = test; | |||
callback(); | |||
}, | |||
afterTest: function(test, callback) { | |||
smokeOutput.results.push({ | |||
status: test.status, | |||
start: test.startTime, | |||
end: Date.now(), | |||
test_file: test.name, | |||
exit_code: 0, | |||
url: '' | |||
}); | |||
delete runningTests[test.name]; | |||
callback(); | |||
}, | |||
beforeExit: function(obj, callback) { | |||
fs.writeFile(outputFile, JSON.stringify(smokeOutput), function() { | |||
callback(); | |||
}); | |||
} | |||
}; | |||
// In case of exception, make sure we write file | |||
process.on('uncaughtException', function(err) { | |||
// Mark all currently running tests as failed | |||
for (var testName in runningTests) { | |||
smokeOutput.results.push({ | |||
status: 'fail', | |||
start: runningTests[testName].startTime, | |||
end: Date.now(), | |||
test_file: testName, | |||
exit_code: 0, | |||
url: '' | |||
}); | |||
} | |||
// write file | |||
fs.writeFileSync(outputFile, JSON.stringify(smokeOutput)); | |||
// Standard NodeJS uncaught exception handler | |||
console.error(err.stack); | |||
process.exit(1); | |||
}); | |||
runner.plugin(integraPlugin); | |||
return integraPlugin; | |||
}; |
@@ -0,0 +1,193 @@ | |||
'use strict'; | |||
/** | |||
* The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is | |||
* used to construct connections. | |||
* @class | |||
* @param {string} mode A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest) | |||
* @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/ | |||
*/ | |||
const ReadPreference = function(mode, tags, options) { | |||
// TODO(major): tags MUST be an array of tagsets | |||
if (tags && !Array.isArray(tags)) { | |||
console.warn( | |||
'ReadPreference tags must be an array, this will change in the next major version' | |||
); | |||
if (typeof tags.maxStalenessSeconds !== 'undefined') { | |||
// this is likely an options object | |||
options = tags; | |||
tags = undefined; | |||
} else { | |||
tags = [tags]; | |||
} | |||
} | |||
this.mode = mode; | |||
this.tags = tags; | |||
options = options || {}; | |||
if (options.maxStalenessSeconds != null) { | |||
if (options.maxStalenessSeconds <= 0) { | |||
throw new TypeError('maxStalenessSeconds must be a positive integer'); | |||
} | |||
this.maxStalenessSeconds = options.maxStalenessSeconds; | |||
// NOTE: The minimum required wire version is 5 for this read preference. If the existing | |||
// topology has a lower value then a MongoError will be thrown during server selection. | |||
this.minWireVersion = 5; | |||
} | |||
if (this.mode === ReadPreference.PRIMARY || this.mode === true) { | |||
if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) { | |||
throw new TypeError('Primary read preference cannot be combined with tags'); | |||
} | |||
if (this.maxStalenessSeconds) { | |||
throw new TypeError('Primary read preference cannot be combined with maxStalenessSeconds'); | |||
} | |||
} | |||
}; | |||
// Support the deprecated `preference` property introduced in the porcelain layer | |||
Object.defineProperty(ReadPreference.prototype, 'preference', { | |||
enumerable: true, | |||
get: function() { | |||
return this.mode; | |||
} | |||
}); | |||
/* | |||
* Read preference mode constants | |||
*/ | |||
ReadPreference.PRIMARY = 'primary'; | |||
ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred'; | |||
ReadPreference.SECONDARY = 'secondary'; | |||
ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred'; | |||
ReadPreference.NEAREST = 'nearest'; | |||
const VALID_MODES = [ | |||
ReadPreference.PRIMARY, | |||
ReadPreference.PRIMARY_PREFERRED, | |||
ReadPreference.SECONDARY, | |||
ReadPreference.SECONDARY_PREFERRED, | |||
ReadPreference.NEAREST, | |||
true, | |||
false, | |||
null | |||
]; | |||
/** | |||
* Validate if a mode is legal | |||
* | |||
* @method | |||
* @param {string} mode The string representing the read preference mode. | |||
* @return {boolean} True if a mode is valid | |||
*/ | |||
ReadPreference.isValid = function(mode) { | |||
return VALID_MODES.indexOf(mode) !== -1; | |||
}; | |||
/** | |||
* Validate if a mode is legal | |||
* | |||
* @method | |||
* @param {string} mode The string representing the read preference mode. | |||
* @return {boolean} True if a mode is valid | |||
*/ | |||
ReadPreference.prototype.isValid = function(mode) { | |||
return ReadPreference.isValid(typeof mode === 'string' ? mode : this.mode); | |||
}; | |||
const needSlaveOk = ['primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest']; | |||
/** | |||
* Indicates that this readPreference needs the "slaveOk" bit when sent over the wire | |||
* @method | |||
* @return {boolean} | |||
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query | |||
*/ | |||
ReadPreference.prototype.slaveOk = function() { | |||
return needSlaveOk.indexOf(this.mode) !== -1; | |||
}; | |||
/** | |||
* Are the two read preference equal | |||
* @method | |||
* @param {ReadPreference} readPreference The read preference with which to check equality | |||
* @return {boolean} True if the two ReadPreferences are equivalent | |||
*/ | |||
ReadPreference.prototype.equals = function(readPreference) { | |||
return readPreference.mode === this.mode; | |||
}; | |||
/** | |||
* Return JSON representation | |||
* @method | |||
* @return {Object} A JSON representation of the ReadPreference | |||
*/ | |||
ReadPreference.prototype.toJSON = function() { | |||
const readPreference = { mode: this.mode }; | |||
if (Array.isArray(this.tags)) readPreference.tags = this.tags; | |||
if (this.maxStalenessSeconds) readPreference.maxStalenessSeconds = this.maxStalenessSeconds; | |||
return readPreference; | |||
}; | |||
/** | |||
* Primary read preference | |||
* @member | |||
* @type {ReadPreference} | |||
*/ | |||
ReadPreference.primary = new ReadPreference('primary'); | |||
/** | |||
* Primary Preferred read preference | |||
* @member | |||
* @type {ReadPreference} | |||
*/ | |||
ReadPreference.primaryPreferred = new ReadPreference('primaryPreferred'); | |||
/** | |||
* Secondary read preference | |||
* @member | |||
* @type {ReadPreference} | |||
*/ | |||
ReadPreference.secondary = new ReadPreference('secondary'); | |||
/** | |||
* Secondary Preferred read preference | |||
* @member | |||
* @type {ReadPreference} | |||
*/ | |||
ReadPreference.secondaryPreferred = new ReadPreference('secondaryPreferred'); | |||
/** | |||
* Nearest read preference | |||
* @member | |||
* @type {ReadPreference} | |||
*/ | |||
ReadPreference.nearest = new ReadPreference('nearest'); | |||
module.exports = ReadPreference; |
@@ -0,0 +1,434 @@ | |||
'use strict'; | |||
const os = require('os'); | |||
const f = require('util').format; | |||
const ReadPreference = require('./read_preference'); | |||
const Buffer = require('safe-buffer').Buffer; | |||
/** | |||
* Emit event if it exists | |||
* @method | |||
*/ | |||
function emitSDAMEvent(self, event, description) { | |||
if (self.listeners(event).length > 0) { | |||
self.emit(event, description); | |||
} | |||
} | |||
// Get package.json variable | |||
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; | |||
var architecture = process.arch; | |||
var release = os.release(); | |||
function createClientInfo(options) { | |||
// Build default client information | |||
var clientInfo = options.clientInfo | |||
? clone(options.clientInfo) | |||
: { | |||
driver: { | |||
name: 'nodejs-core', | |||
version: driverVersion | |||
}, | |||
os: { | |||
type: type, | |||
name: name, | |||
architecture: architecture, | |||
version: release | |||
} | |||
}; | |||
// Is platform specified | |||
if (clientInfo.platform && clientInfo.platform.indexOf('mongodb-core') === -1) { | |||
clientInfo.platform = f('%s, mongodb-core: %s', clientInfo.platform, driverVersion); | |||
} else if (!clientInfo.platform) { | |||
clientInfo.platform = nodejsversion; | |||
} | |||
// Do we have an application specific string | |||
if (options.appname) { | |||
// Cut at 128 bytes | |||
var buffer = Buffer.from(options.appname); | |||
// Return the truncated appname | |||
var appname = buffer.length > 128 ? buffer.slice(0, 128).toString('utf8') : options.appname; | |||
// Add to the clientInfo | |||
clientInfo.application = { name: appname }; | |||
} | |||
return clientInfo; | |||
} | |||
function createCompressionInfo(options) { | |||
if (!options.compression || !options.compression.compressors) { | |||
return []; | |||
} | |||
// Check that all supplied compressors are valid | |||
options.compression.compressors.forEach(function(compressor) { | |||
if (compressor !== 'snappy' && compressor !== 'zlib') { | |||
throw new Error('compressors must be at least one of snappy or zlib'); | |||
} | |||
}); | |||
return options.compression.compressors; | |||
} | |||
function clone(object) { | |||
return JSON.parse(JSON.stringify(object)); | |||
} | |||
var getPreviousDescription = function(self) { | |||
if (!self.s.serverDescription) { | |||
self.s.serverDescription = { | |||
address: self.name, | |||
arbiters: [], | |||
hosts: [], | |||
passives: [], | |||
type: 'Unknown' | |||
}; | |||
} | |||
return self.s.serverDescription; | |||
}; | |||
var emitServerDescriptionChanged = function(self, description) { | |||
if (self.listeners('serverDescriptionChanged').length > 0) { | |||
// Emit the server description changed events | |||
self.emit('serverDescriptionChanged', { | |||
topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id, | |||
address: self.name, | |||
previousDescription: getPreviousDescription(self), | |||
newDescription: description | |||
}); | |||
self.s.serverDescription = description; | |||
} | |||
}; | |||
var getPreviousTopologyDescription = function(self) { | |||
if (!self.s.topologyDescription) { | |||
self.s.topologyDescription = { | |||
topologyType: 'Unknown', | |||
servers: [ | |||
{ | |||
address: self.name, | |||
arbiters: [], | |||
hosts: [], | |||
passives: [], | |||
type: 'Unknown' | |||
} | |||
] | |||
}; | |||
} | |||
return self.s.topologyDescription; | |||
}; | |||
var emitTopologyDescriptionChanged = function(self, description) { | |||
if (self.listeners('topologyDescriptionChanged').length > 0) { | |||
// Emit the server description changed events | |||
self.emit('topologyDescriptionChanged', { | |||
topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id, | |||
address: self.name, | |||
previousDescription: getPreviousTopologyDescription(self), | |||
newDescription: description | |||
}); | |||
self.s.serverDescription = description; | |||
} | |||
}; | |||
var changedIsMaster = function(self, currentIsmaster, ismaster) { | |||
var currentType = getTopologyType(self, currentIsmaster); | |||
var newType = getTopologyType(self, ismaster); | |||
if (newType !== currentType) return true; | |||
return false; | |||
}; | |||
var getTopologyType = function(self, ismaster) { | |||
if (!ismaster) { | |||
ismaster = self.ismaster; | |||
} | |||
if (!ismaster) return 'Unknown'; | |||
if (ismaster.ismaster && ismaster.msg === 'isdbgrid') return 'Mongos'; | |||
if (ismaster.ismaster && !ismaster.hosts) return 'Standalone'; | |||
if (ismaster.ismaster) return 'RSPrimary'; | |||
if (ismaster.secondary) return 'RSSecondary'; | |||
if (ismaster.arbiterOnly) return 'RSArbiter'; | |||
return 'Unknown'; | |||
}; | |||
var inquireServerState = function(self) { | |||
return function(callback) { | |||
if (self.s.state === 'destroyed') return; | |||
// Record response time | |||
var start = new Date().getTime(); | |||
// emitSDAMEvent | |||
emitSDAMEvent(self, 'serverHeartbeatStarted', { connectionId: self.name }); | |||
// Attempt to execute ismaster command | |||
self.command('admin.$cmd', { ismaster: true }, { monitoring: true }, function(err, r) { | |||
if (!err) { | |||
// Legacy event sender | |||
self.emit('ismaster', r, self); | |||
// Calculate latencyMS | |||
var latencyMS = new Date().getTime() - start; | |||
// Server heart beat event | |||
emitSDAMEvent(self, 'serverHeartbeatSucceeded', { | |||
durationMS: latencyMS, | |||
reply: r.result, | |||
connectionId: self.name | |||
}); | |||
// Did the server change | |||
if (changedIsMaster(self, self.s.ismaster, r.result)) { | |||
// Emit server description changed if something listening | |||
emitServerDescriptionChanged(self, { | |||
address: self.name, | |||
arbiters: [], | |||
hosts: [], | |||
passives: [], | |||
type: !self.s.inTopology ? 'Standalone' : getTopologyType(self) | |||
}); | |||
} | |||
// Updat ismaster view | |||
self.s.ismaster = r.result; | |||
// Set server response time | |||
self.s.isMasterLatencyMS = latencyMS; | |||
} else { | |||
emitSDAMEvent(self, 'serverHeartbeatFailed', { | |||
durationMS: latencyMS, | |||
failure: err, | |||
connectionId: self.name | |||
}); | |||
} | |||
// Peforming an ismaster monitoring callback operation | |||
if (typeof callback === 'function') { | |||
return callback(err, r); | |||
} | |||
// Perform another sweep | |||
self.s.inquireServerStateTimeout = setTimeout(inquireServerState(self), self.s.haInterval); | |||
}); | |||
}; | |||
}; | |||
// | |||
// Clone the options | |||
var cloneOptions = function(options) { | |||
var opts = {}; | |||
for (var name in options) { | |||
opts[name] = options[name]; | |||
} | |||
return opts; | |||
}; | |||
function Interval(fn, time) { | |||
var timer = false; | |||
this.start = function() { | |||
if (!this.isRunning()) { | |||
timer = setInterval(fn, time); | |||
} | |||
return this; | |||
}; | |||
this.stop = function() { | |||
clearInterval(timer); | |||
timer = false; | |||
return this; | |||
}; | |||
this.isRunning = function() { | |||
return timer !== false; | |||
}; | |||
} | |||
function Timeout(fn, time) { | |||
var timer = false; | |||
this.start = function() { | |||
if (!this.isRunning()) { | |||
timer = setTimeout(fn, time); | |||
} | |||
return this; | |||
}; | |||
this.stop = function() { | |||
clearTimeout(timer); | |||
timer = false; | |||
return this; | |||
}; | |||
this.isRunning = function() { | |||
if (timer && timer._called) return false; | |||
return timer !== false; | |||
}; | |||
} | |||
function diff(previous, current) { | |||
// Difference document | |||
var diff = { | |||
servers: [] | |||
}; | |||
// Previous entry | |||
if (!previous) { | |||
previous = { servers: [] }; | |||
} | |||
// Check if we have any previous servers missing in the current ones | |||
for (var i = 0; i < previous.servers.length; i++) { | |||
var found = false; | |||
for (var j = 0; j < current.servers.length; j++) { | |||
if (current.servers[j].address.toLowerCase() === previous.servers[i].address.toLowerCase()) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
if (!found) { | |||
// Add to the diff | |||
diff.servers.push({ | |||
address: previous.servers[i].address, | |||
from: previous.servers[i].type, | |||
to: 'Unknown' | |||
}); | |||
} | |||
} | |||
// Check if there are any severs that don't exist | |||
for (j = 0; j < current.servers.length; j++) { | |||
found = false; | |||
// Go over all the previous servers | |||
for (i = 0; i < previous.servers.length; i++) { | |||
if (previous.servers[i].address.toLowerCase() === current.servers[j].address.toLowerCase()) { | |||
found = true; | |||
break; | |||
} | |||
} | |||
// Add the server to the diff | |||
if (!found) { | |||
diff.servers.push({ | |||
address: current.servers[j].address, | |||
from: 'Unknown', | |||
to: current.servers[j].type | |||
}); | |||
} | |||
} | |||
// Got through all the servers | |||
for (i = 0; i < previous.servers.length; i++) { | |||
var prevServer = previous.servers[i]; | |||
// Go through all current servers | |||
for (j = 0; j < current.servers.length; j++) { | |||
var currServer = current.servers[j]; | |||
// Matching server | |||
if (prevServer.address.toLowerCase() === currServer.address.toLowerCase()) { | |||
// We had a change in state | |||
if (prevServer.type !== currServer.type) { | |||
diff.servers.push({ | |||
address: prevServer.address, | |||
from: prevServer.type, | |||
to: currServer.type | |||
}); | |||
} | |||
} | |||
} | |||
} | |||
// Return difference | |||
return diff; | |||
} | |||
/** | |||
* Shared function to determine clusterTime for a given topology | |||
* | |||
* @param {*} topology | |||
* @param {*} clusterTime | |||
*/ | |||
function resolveClusterTime(topology, $clusterTime) { | |||
if (topology.clusterTime == null) { | |||
topology.clusterTime = $clusterTime; | |||
} else { | |||
if ($clusterTime.clusterTime.greaterThan(topology.clusterTime.clusterTime)) { | |||
topology.clusterTime = $clusterTime; | |||
} | |||
} | |||
} | |||
// NOTE: this is a temporary move until the topologies can be more formally refactored | |||
// to share code. | |||
const SessionMixins = { | |||
endSessions: function(sessions, callback) { | |||
if (!Array.isArray(sessions)) { | |||
sessions = [sessions]; | |||
} | |||
// TODO: | |||
// When connected to a sharded cluster the endSessions command | |||
// can be sent to any mongos. When connected to a replica set the | |||
// endSessions command MUST be sent to the primary if the primary | |||
// is available, otherwise it MUST be sent to any available secondary. | |||
// Is it enough to use: ReadPreference.primaryPreferred ? | |||
this.command( | |||
'admin.$cmd', | |||
{ endSessions: sessions }, | |||
{ readPreference: ReadPreference.primaryPreferred }, | |||
() => { | |||
// intentionally ignored, per spec | |||
if (typeof callback === 'function') callback(); | |||
} | |||
); | |||
} | |||
}; | |||
const RETRYABLE_WIRE_VERSION = 6; | |||
/** | |||
* Determines whether the provided topology supports retryable writes | |||
* | |||
* @param {Mongos|Replset} topology | |||
*/ | |||
const isRetryableWritesSupported = function(topology) { | |||
const maxWireVersion = topology.lastIsMaster().maxWireVersion; | |||
if (maxWireVersion < RETRYABLE_WIRE_VERSION) { | |||
return false; | |||
} | |||
if (!topology.logicalSessionTimeoutMinutes) { | |||
return false; | |||
} | |||
return true; | |||
}; | |||
module.exports.SessionMixins = SessionMixins; | |||
module.exports.resolveClusterTime = resolveClusterTime; | |||
module.exports.inquireServerState = inquireServerState; | |||
module.exports.getTopologyType = getTopologyType; | |||
module.exports.emitServerDescriptionChanged = emitServerDescriptionChanged; | |||
module.exports.emitTopologyDescriptionChanged = emitTopologyDescriptionChanged; | |||
module.exports.cloneOptions = cloneOptions; | |||
module.exports.createClientInfo = createClientInfo; | |||
module.exports.createCompressionInfo = createCompressionInfo; | |||
module.exports.clone = clone; | |||
module.exports.diff = diff; | |||
module.exports.Interval = Interval; | |||
module.exports.Timeout = Timeout; | |||
module.exports.isRetryableWritesSupported = isRetryableWritesSupported; |
@@ -0,0 +1,134 @@ | |||
'use strict'; | |||
const MongoError = require('./error').MongoError; | |||
let TxnState; | |||
let stateMachine; | |||
(() => { | |||
const NO_TRANSACTION = 'NO_TRANSACTION'; | |||
const STARTING_TRANSACTION = 'STARTING_TRANSACTION'; | |||
const TRANSACTION_IN_PROGRESS = 'TRANSACTION_IN_PROGRESS'; | |||
const TRANSACTION_COMMITTED = 'TRANSACTION_COMMITTED'; | |||
const TRANSACTION_COMMITTED_EMPTY = 'TRANSACTION_COMMITTED_EMPTY'; | |||
const TRANSACTION_ABORTED = 'TRANSACTION_ABORTED'; | |||
TxnState = { | |||
NO_TRANSACTION, | |||
STARTING_TRANSACTION, | |||
TRANSACTION_IN_PROGRESS, | |||
TRANSACTION_COMMITTED, | |||
TRANSACTION_COMMITTED_EMPTY, | |||
TRANSACTION_ABORTED | |||
}; | |||
stateMachine = { | |||
[NO_TRANSACTION]: [NO_TRANSACTION, STARTING_TRANSACTION], | |||
[STARTING_TRANSACTION]: [ | |||
TRANSACTION_IN_PROGRESS, | |||
TRANSACTION_COMMITTED, | |||
TRANSACTION_COMMITTED_EMPTY, | |||
TRANSACTION_ABORTED | |||
], | |||
[TRANSACTION_IN_PROGRESS]: [ | |||
TRANSACTION_IN_PROGRESS, | |||
TRANSACTION_COMMITTED, | |||
TRANSACTION_ABORTED | |||
], | |||
[TRANSACTION_COMMITTED]: [ | |||
TRANSACTION_COMMITTED, | |||
TRANSACTION_COMMITTED_EMPTY, | |||
STARTING_TRANSACTION, | |||
NO_TRANSACTION | |||
], | |||
[TRANSACTION_ABORTED]: [STARTING_TRANSACTION, NO_TRANSACTION], | |||
[TRANSACTION_COMMITTED_EMPTY]: [TRANSACTION_COMMITTED_EMPTY, NO_TRANSACTION] | |||
}; | |||
})(); | |||
/** | |||
* The MongoDB ReadConcern, which allows for control of the consistency and isolation properties | |||
* of the data read from replica sets and replica set shards. | |||
* @typedef {Object} ReadConcern | |||
* @property {'local'|'available'|'majority'|'linearizable'|'snapshot'} level The readConcern Level | |||
* @see https://docs.mongodb.com/manual/reference/read-concern/ | |||
*/ | |||
/** | |||
* A MongoDB WriteConcern, which describes the level of acknowledgement | |||
* requested from MongoDB for write operations. | |||
* @typedef {Object} WriteConcern | |||
* @property {number|'majority'|string} [w=1] requests acknowledgement that the write operation has | |||
* propagated to a specified number of mongod hosts | |||
* @property {boolean} [j=false] requests acknowledgement from MongoDB that the write operation has | |||
* been written to the journal | |||
* @property {number} [wtimeout] a time limit, in milliseconds, for the write concern | |||
* @see https://docs.mongodb.com/manual/reference/write-concern/ | |||
*/ | |||
/** | |||
* Configuration options for a transaction. | |||
* @typedef {Object} TransactionOptions | |||
* @property {ReadConcern} [readConcern] A default read concern for commands in this transaction | |||
* @property {WriteConcern} [writeConcern] A default writeConcern for commands in this transaction | |||
* @property {ReadPreference} [readPreference] A default read preference for commands in this transaction | |||
*/ | |||
/** | |||
* A class maintaining state related to a server transaction. Internal Only | |||
* @ignore | |||
*/ | |||
class Transaction { | |||
/** | |||
* Create a transaction | |||
* | |||
* @ignore | |||
* @param {TransactionOptions} [options] Optional settings | |||
*/ | |||
constructor(options) { | |||
options = options || {}; | |||
this.state = TxnState.NO_TRANSACTION; | |||
this.options = {}; | |||
if (options.writeConcern || typeof options.w !== 'undefined') { | |||
const w = options.writeConcern ? options.writeConcern.w : options.w; | |||
if (w <= 0) { | |||
throw new MongoError('Transactions do not support unacknowledged write concern'); | |||
} | |||
this.options.writeConcern = options.writeConcern ? options.writeConcern : { w: options.w }; | |||
} | |||
if (options.readConcern) this.options.readConcern = options.readConcern; | |||
if (options.readPreference) this.options.readPreference = options.readPreference; | |||
} | |||
/** | |||
* @ignore | |||
* @return Whether this session is presently in a transaction | |||
*/ | |||
get isActive() { | |||
return ( | |||
[TxnState.STARTING_TRANSACTION, TxnState.TRANSACTION_IN_PROGRESS].indexOf(this.state) !== -1 | |||
); | |||
} | |||
/** | |||
* Transition the transaction in the state machine | |||
* @ignore | |||
* @param {TxnState} state The new state to transition to | |||
*/ | |||
transition(nextState) { | |||
const nextStates = stateMachine[this.state]; | |||
if (nextStates && nextStates.indexOf(nextState) !== -1) { | |||
this.state = nextState; | |||
return; | |||
} | |||
throw new MongoError( | |||
`Attempted illegal state transition from [${this.state}] to [${nextState}]` | |||
); | |||
} | |||
} | |||
module.exports = { TxnState, Transaction }; |
@@ -0,0 +1,536 @@ | |||
'use strict'; | |||
const URL = require('url'); | |||
const qs = require('querystring'); | |||
const dns = require('dns'); | |||
const MongoParseError = require('./error').MongoParseError; | |||
const ReadPreference = require('./topologies/read_preference'); | |||
/** | |||
* The following regular expression validates a connection string and breaks the | |||
* provide string into the following capture groups: [protocol, username, password, hosts] | |||
*/ | |||
const HOSTS_RX = /(mongodb(?:\+srv|)):\/\/(?: (?:[^:]*) (?: : ([^@]*) )? @ )?([^/?]*)(?:\/|)(.*)/; | |||
/** | |||
* Determines whether a provided address matches the provided parent domain in order | |||
* to avoid certain attack vectors. | |||
* | |||
* @param {String} srvAddress The address to check against a domain | |||
* @param {String} parentDomain The domain to check the provided address against | |||
* @return {Boolean} Whether the provided address matches the parent domain | |||
*/ | |||
function matchesParentDomain(srvAddress, parentDomain) { | |||
const regex = /^.*?\./; | |||
const srv = `.${srvAddress.replace(regex, '')}`; | |||
const parent = `.${parentDomain.replace(regex, '')}`; | |||
return srv.endsWith(parent); | |||
} | |||
/** | |||
* Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal | |||
* connection string. | |||
* | |||
* @param {string} uri The connection string to parse | |||
* @param {object} options Optional user provided connection string options | |||
* @param {function} callback | |||
*/ | |||
function parseSrvConnectionString(uri, options, callback) { | |||
const result = URL.parse(uri, true); | |||
if (result.hostname.split('.').length < 3) { | |||
return callback(new MongoParseError('URI does not have hostname, domain name and tld')); | |||
} | |||
result.domainLength = result.hostname.split('.').length; | |||
if (result.pathname && result.pathname.match(',')) { | |||
return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames')); | |||
} | |||
if (result.port) { | |||
return callback(new MongoParseError(`Ports not accepted with '${PROTOCOL_MONGODB_SRV}' URIs`)); | |||
} | |||
// Resolve the SRV record and use the result as the list of hosts to connect to. | |||
const lookupAddress = result.host; | |||
dns.resolveSrv(`_mongodb._tcp.${lookupAddress}`, (err, addresses) => { | |||
if (err) return callback(err); | |||
if (addresses.length === 0) { | |||
return callback(new MongoParseError('No addresses found at host')); | |||
} | |||
for (let i = 0; i < addresses.length; i++) { | |||
if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) { | |||
return callback( | |||
new MongoParseError('Server record does not share hostname with parent URI') | |||
); | |||
} | |||
} | |||
// Convert the original URL to a non-SRV URL. | |||
result.protocol = 'mongodb'; | |||
result.host = addresses.map(address => `${address.name}:${address.port}`).join(','); | |||
// Default to SSL true if it's not specified. | |||
if ( | |||
!('ssl' in options) && | |||
(!result.search || !('ssl' in result.query) || result.query.ssl === null) | |||
) { | |||
result.query.ssl = true; | |||
} | |||
// Resolve TXT record and add options from there if they exist. | |||
dns.resolveTxt(lookupAddress, (err, record) => { | |||
if (err) { | |||
if (err.code !== 'ENODATA') { | |||
return callback(err); | |||
} | |||
record = null; | |||
} | |||
if (record) { | |||
if (record.length > 1) { | |||
return callback(new MongoParseError('Multiple text records not allowed')); | |||
} | |||
record = qs.parse(record[0].join('')); | |||
if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) { | |||
return callback( | |||
new MongoParseError('Text record must only set `authSource` or `replicaSet`') | |||
); | |||
} | |||
Object.assign(result.query, record); | |||
} | |||
// Set completed options back into the URL object. | |||
result.search = qs.stringify(result.query); | |||
const finalString = URL.format(result); | |||
parseConnectionString(finalString, options, callback); | |||
}); | |||
}); | |||
} | |||
/** | |||
* Parses a query string item according to the connection string spec | |||
* | |||
* @param {string} key The key for the parsed value | |||
* @param {Array|String} value The value to parse | |||
* @return {Array|Object|String} The parsed value | |||
*/ | |||
function parseQueryStringItemValue(key, value) { | |||
if (Array.isArray(value)) { | |||
// deduplicate and simplify arrays | |||
value = value.filter((v, idx) => value.indexOf(v) === idx); | |||
if (value.length === 1) value = value[0]; | |||
} else if (value.indexOf(':') > 0) { | |||
value = value.split(',').reduce((result, pair) => { | |||
const parts = pair.split(':'); | |||
result[parts[0]] = parseQueryStringItemValue(key, parts[1]); | |||
return result; | |||
}, {}); | |||
} else if (value.indexOf(',') > 0) { | |||
value = value.split(',').map(v => { | |||
return parseQueryStringItemValue(key, v); | |||
}); | |||
} else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') { | |||
value = value.toLowerCase() === 'true'; | |||
} else if (!Number.isNaN(value) && !STRING_OPTIONS.has(key)) { | |||
const numericValue = parseFloat(value); | |||
if (!Number.isNaN(numericValue)) { | |||
value = parseFloat(value); | |||
} | |||
} | |||
return value; | |||
} | |||
// Options that are known boolean types | |||
const BOOLEAN_OPTIONS = new Set([ | |||
'slaveok', | |||
'slave_ok', | |||
'sslvalidate', | |||
'fsync', | |||
'safe', | |||
'retrywrites', | |||
'j' | |||
]); | |||
// Known string options, only used to bypass Number coercion in `parseQueryStringItemValue` | |||
const STRING_OPTIONS = new Set(['authsource', 'replicaset']); | |||
// Supported text representations of auth mechanisms | |||
// NOTE: this list exists in native already, if it is merged here we should deduplicate | |||
const AUTH_MECHANISMS = new Set([ | |||
'GSSAPI', | |||
'MONGODB-X509', | |||
'MONGODB-CR', | |||
'DEFAULT', | |||
'SCRAM-SHA-1', | |||
'SCRAM-SHA-256', | |||
'PLAIN' | |||
]); | |||
// Lookup table used to translate normalized (lower-cased) forms of connection string | |||
// options to their expected camelCase version | |||
const CASE_TRANSLATION = { | |||
replicaset: 'replicaSet', | |||
connecttimeoutms: 'connectTimeoutMS', | |||
sockettimeoutms: 'socketTimeoutMS', | |||
maxpoolsize: 'maxPoolSize', | |||
minpoolsize: 'minPoolSize', | |||
maxidletimems: 'maxIdleTimeMS', | |||
waitqueuemultiple: 'waitQueueMultiple', | |||
waitqueuetimeoutms: 'waitQueueTimeoutMS', | |||
wtimeoutms: 'wtimeoutMS', | |||
readconcern: 'readConcern', | |||
readconcernlevel: 'readConcernLevel', | |||
readpreference: 'readPreference', | |||
maxstalenessseconds: 'maxStalenessSeconds', | |||
readpreferencetags: 'readPreferenceTags', | |||
authsource: 'authSource', | |||
authmechanism: 'authMechanism', | |||
authmechanismproperties: 'authMechanismProperties', | |||
gssapiservicename: 'gssapiServiceName', | |||
localthresholdms: 'localThresholdMS', | |||
serverselectiontimeoutms: 'serverSelectionTimeoutMS', | |||
serverselectiontryonce: 'serverSelectionTryOnce', | |||
heartbeatfrequencyms: 'heartbeatFrequencyMS', | |||
appname: 'appName', | |||
retrywrites: 'retryWrites', | |||
uuidrepresentation: 'uuidRepresentation', | |||
zlibcompressionlevel: 'zlibCompressionLevel' | |||
}; | |||
/** | |||
* Sets the value for `key`, allowing for any required translation | |||
* | |||
* @param {object} obj The object to set the key on | |||
* @param {string} key The key to set the value for | |||
* @param {*} value The value to set | |||
* @param {object} options The options used for option parsing | |||
*/ | |||
function applyConnectionStringOption(obj, key, value, options) { | |||
// simple key translation | |||
if (key === 'journal') { | |||
key = 'j'; | |||
} else if (key === 'wtimeoutms') { | |||
key = 'wtimeout'; | |||
} | |||
// more complicated translation | |||
if (BOOLEAN_OPTIONS.has(key)) { | |||
value = value === 'true' || value === true; | |||
} else if (key === 'appname') { | |||
value = decodeURIComponent(value); | |||
} else if (key === 'readconcernlevel') { | |||
key = 'readconcern'; | |||
value = { level: value }; | |||
} | |||
// simple validation | |||
if (key === 'compressors') { | |||
value = Array.isArray(value) ? value : [value]; | |||
if (!value.every(c => c === 'snappy' || c === 'zlib')) { | |||
throw new MongoParseError( | |||
'Value for `compressors` must be at least one of: `snappy`, `zlib`' | |||
); | |||
} | |||
} | |||
if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) { | |||
throw new MongoParseError( | |||
'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`' | |||
); | |||
} | |||
if (key === 'readpreference' && !ReadPreference.isValid(value)) { | |||
throw new MongoParseError( | |||
'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`' | |||
); | |||
} | |||
if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) { | |||
throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9'); | |||
} | |||
// special cases | |||
if (key === 'compressors' || key === 'zlibcompressionlevel') { | |||
obj.compression = obj.compression || {}; | |||
obj = obj.compression; | |||
} | |||
if (key === 'authmechanismproperties') { | |||
if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME; | |||
if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM; | |||
if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') { | |||
obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME; | |||
} | |||
} | |||
// set the actual value | |||
if (options.caseTranslate && CASE_TRANSLATION[key]) { | |||
obj[CASE_TRANSLATION[key]] = value; | |||
return; | |||
} | |||
obj[key] = value; | |||
} | |||
const USERNAME_REQUIRED_MECHANISMS = new Set([ | |||
'GSSAPI', | |||
'MONGODB-CR', | |||
'PLAIN', | |||
'SCRAM-SHA-1', | |||
'SCRAM-SHA-256' | |||
]); | |||
/** | |||
* Modifies the parsed connection string object taking into account expectations we | |||
* have for authentication-related options. | |||
* | |||
* @param {object} parsed The parsed connection string result | |||
* @return The parsed connection string result possibly modified for auth expectations | |||
*/ | |||
function applyAuthExpectations(parsed) { | |||
if (parsed.options == null) { | |||
return; | |||
} | |||
const options = parsed.options; | |||
const authSource = options.authsource || options.authSource; | |||
if (authSource != null) { | |||
parsed.auth = Object.assign({}, parsed.auth, { db: authSource }); | |||
} | |||
const authMechanism = options.authmechanism || options.authMechanism; | |||
if (authMechanism != null) { | |||
if ( | |||
USERNAME_REQUIRED_MECHANISMS.has(authMechanism) && | |||
(!parsed.auth || parsed.auth.username == null) | |||
) { | |||
throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``); | |||
} | |||
if (authMechanism === 'GSSAPI') { | |||
if (authSource != null && authSource !== '$external') { | |||
throw new MongoParseError( | |||
`Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.` | |||
); | |||
} | |||
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' }); | |||
} | |||
if (authMechanism === 'MONGODB-X509') { | |||
if (parsed.auth && parsed.auth.password != null) { | |||
throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``); | |||
} | |||
if (authSource != null && authSource !== '$external') { | |||
throw new MongoParseError( | |||
`Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.` | |||
); | |||
} | |||
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' }); | |||
} | |||
if (authMechanism === 'PLAIN') { | |||
if (parsed.auth && parsed.auth.db == null) { | |||
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' }); | |||
} | |||
} | |||
} | |||
// default to `admin` if nothing else was resolved | |||
if (parsed.auth && parsed.auth.db == null) { | |||
parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' }); | |||
} | |||
return parsed; | |||
} | |||
/** | |||
* Parses a query string according the connection string spec. | |||
* | |||
* @param {String} query The query string to parse | |||
* @param {object} [options] The options used for options parsing | |||
* @return {Object|Error} The parsed query string as an object, or an error if one was encountered | |||
*/ | |||
function parseQueryString(query, options) { | |||
const result = {}; | |||
let parsedQueryString = qs.parse(query); | |||
for (const key in parsedQueryString) { | |||
const value = parsedQueryString[key]; | |||
if (value === '' || value == null) { | |||
throw new MongoParseError('Incomplete key value pair for option'); | |||
} | |||
const normalizedKey = key.toLowerCase(); | |||
const parsedValue = parseQueryStringItemValue(normalizedKey, value); | |||
applyConnectionStringOption(result, normalizedKey, parsedValue, options); | |||
} | |||
// special cases for known deprecated options | |||
if (result.wtimeout && result.wtimeoutms) { | |||
delete result.wtimeout; | |||
console.warn('Unsupported option `wtimeout` specified'); | |||
} | |||
return Object.keys(result).length ? result : null; | |||
} | |||
const PROTOCOL_MONGODB = 'mongodb'; | |||
const PROTOCOL_MONGODB_SRV = 'mongodb+srv'; | |||
const SUPPORTED_PROTOCOLS = [PROTOCOL_MONGODB, PROTOCOL_MONGODB_SRV]; | |||
/** | |||
* Parses a MongoDB connection string | |||
* | |||
* @param {*} uri the MongoDB connection string to parse | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.caseTranslate] Whether the parser should translate options back into camelCase after normalization | |||
* @param {parseCallback} callback | |||
*/ | |||
function parseConnectionString(uri, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = Object.assign({}, { caseTranslate: true }, options); | |||
// Check for bad uris before we parse | |||
try { | |||
URL.parse(uri); | |||
} catch (e) { | |||
return callback(new MongoParseError('URI malformed, cannot be parsed')); | |||
} | |||
const cap = uri.match(HOSTS_RX); | |||
if (!cap) { | |||
return callback(new MongoParseError('Invalid connection string')); | |||
} | |||
const protocol = cap[1]; | |||
if (SUPPORTED_PROTOCOLS.indexOf(protocol) === -1) { | |||
return callback(new MongoParseError('Invalid protocol provided')); | |||
} | |||
if (protocol === PROTOCOL_MONGODB_SRV) { | |||
return parseSrvConnectionString(uri, options, callback); | |||
} | |||
const dbAndQuery = cap[4].split('?'); | |||
const db = dbAndQuery.length > 0 ? dbAndQuery[0] : null; | |||
const query = dbAndQuery.length > 1 ? dbAndQuery[1] : null; | |||
let parsedOptions; | |||
try { | |||
parsedOptions = parseQueryString(query, options); | |||
} catch (parseError) { | |||
return callback(parseError); | |||
} | |||
parsedOptions = Object.assign({}, parsedOptions, options); | |||
const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null }; | |||
if (parsedOptions.auth) { | |||
// maintain support for legacy options passed into `MongoClient` | |||
if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username; | |||
if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user; | |||
if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password; | |||
} | |||
if (cap[4].split('?')[0].indexOf('@') !== -1) { | |||
return callback(new MongoParseError('Unescaped slash in userinfo section')); | |||
} | |||
const authorityParts = cap[3].split('@'); | |||
if (authorityParts.length > 2) { | |||
return callback(new MongoParseError('Unescaped at-sign in authority section')); | |||
} | |||
if (authorityParts.length > 1) { | |||
const authParts = authorityParts.shift().split(':'); | |||
if (authParts.length > 2) { | |||
return callback(new MongoParseError('Unescaped colon in authority section')); | |||
} | |||
auth.username = qs.unescape(authParts[0]); | |||
auth.password = authParts[1] ? qs.unescape(authParts[1]) : null; | |||
} | |||
let hostParsingError = null; | |||
const hosts = authorityParts | |||
.shift() | |||
.split(',') | |||
.map(host => { | |||
let parsedHost = URL.parse(`mongodb://${host}`); | |||
if (parsedHost.path === '/:') { | |||
hostParsingError = new MongoParseError('Double colon in host identifier'); | |||
return null; | |||
} | |||
// heuristically determine if we're working with a domain socket | |||
if (host.match(/\.sock/)) { | |||
parsedHost.hostname = qs.unescape(host); | |||
parsedHost.port = null; | |||
} | |||
if (Number.isNaN(parsedHost.port)) { | |||
hostParsingError = new MongoParseError('Invalid port (non-numeric string)'); | |||
return; | |||
} | |||
const result = { | |||
host: parsedHost.hostname, | |||
port: parsedHost.port ? parseInt(parsedHost.port) : 27017 | |||
}; | |||
if (result.port === 0) { | |||
hostParsingError = new MongoParseError('Invalid port (zero) with hostname'); | |||
return; | |||
} | |||
if (result.port > 65535) { | |||
hostParsingError = new MongoParseError('Invalid port (larger than 65535) with hostname'); | |||
return; | |||
} | |||
if (result.port < 0) { | |||
hostParsingError = new MongoParseError('Invalid port (negative number)'); | |||
return; | |||
} | |||
return result; | |||
}) | |||
.filter(host => !!host); | |||
if (hostParsingError) { | |||
return callback(hostParsingError); | |||
} | |||
if (hosts.length === 0 || hosts[0].host === '' || hosts[0].host === null) { | |||
return callback(new MongoParseError('No hostname or hostnames provided in connection string')); | |||
} | |||
const result = { | |||
hosts: hosts, | |||
auth: auth.db || auth.username ? auth : null, | |||
options: Object.keys(parsedOptions).length ? parsedOptions : null | |||
}; | |||
if (result.auth && result.auth.db) { | |||
result.defaultDatabase = result.auth.db; | |||
} | |||
try { | |||
applyAuthExpectations(result); | |||
} catch (authError) { | |||
return callback(authError); | |||
} | |||
callback(null, result); | |||
} | |||
module.exports = parseConnectionString; |
@@ -0,0 +1,97 @@ | |||
'use strict'; | |||
const crypto = require('crypto'); | |||
const requireOptional = require('require_optional'); | |||
/** | |||
* Generate a UUIDv4 | |||
*/ | |||
const uuidV4 = () => { | |||
const result = crypto.randomBytes(16); | |||
result[6] = (result[6] & 0x0f) | 0x40; | |||
result[8] = (result[8] & 0x3f) | 0x80; | |||
return result; | |||
}; | |||
/** | |||
* Returns the duration calculated from two high resolution timers in milliseconds | |||
* | |||
* @param {Object} started A high resolution timestamp created from `process.hrtime()` | |||
* @returns {Number} The duration in milliseconds | |||
*/ | |||
const calculateDurationInMs = started => { | |||
const hrtime = process.hrtime(started); | |||
return (hrtime[0] * 1e9 + hrtime[1]) / 1e6; | |||
}; | |||
/** | |||
* Relays events for a given listener and emitter | |||
* | |||
* @param {EventEmitter} listener the EventEmitter to listen to the events from | |||
* @param {EventEmitter} emitter the EventEmitter to relay the events to | |||
*/ | |||
function relayEvents(listener, emitter, events) { | |||
events.forEach(eventName => listener.on(eventName, event => emitter.emit(eventName, event))); | |||
} | |||
function retrieveKerberos() { | |||
let kerberos; | |||
try { | |||
kerberos = requireOptional('kerberos'); | |||
} catch (err) { | |||
if (err.code === 'MODULE_NOT_FOUND') { | |||
throw new Error('The `kerberos` module was not found. Please install it and try again.'); | |||
} | |||
throw err; | |||
} | |||
return kerberos; | |||
} | |||
// 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` module was not found. Please install it and try again.'); | |||
}; | |||
// Facilitate loading EJSON optionally | |||
function retrieveEJSON() { | |||
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. | |||
* | |||
* @param {Server} [server] to check against | |||
* @param {object} [cmd] object where collation may be specified | |||
* @param {function} [callback] callback function | |||
* @return true if server does not support collation | |||
*/ | |||
function collationNotSupported(server, cmd) { | |||
return cmd && cmd.collation && server.ismaster && server.ismaster.maxWireVersion < 5; | |||
} | |||
module.exports = { | |||
uuidV4, | |||
calculateDurationInMs, | |||
relayEvents, | |||
collationNotSupported, | |||
retrieveEJSON, | |||
retrieveKerberos | |||
}; |
@@ -0,0 +1,267 @@ | |||
'use strict'; | |||
const retrieveBSON = require('../connection/utils').retrieveBSON; | |||
const KillCursor = require('../connection/commands').KillCursor; | |||
const GetMore = require('../connection/commands').GetMore; | |||
const Query = require('../connection/commands').Query; | |||
const MongoError = require('../error').MongoError; | |||
const getReadPreference = require('./shared').getReadPreference; | |||
const applyCommonQueryOptions = require('./shared').applyCommonQueryOptions; | |||
const isMongos = require('./shared').isMongos; | |||
const databaseNamespace = require('./shared').databaseNamespace; | |||
const collectionNamespace = require('./shared').collectionNamespace; | |||
const BSON = retrieveBSON(); | |||
const Long = BSON.Long; | |||
class WireProtocol { | |||
insert(server, ns, ops, options, callback) { | |||
executeWrite(this, server, 'insert', 'documents', ns, ops, options, callback); | |||
} | |||
update(server, ns, ops, options, callback) { | |||
executeWrite(this, server, 'update', 'updates', ns, ops, options, callback); | |||
} | |||
remove(server, ns, ops, options, callback) { | |||
executeWrite(this, server, 'delete', 'deletes', ns, ops, options, callback); | |||
} | |||
killCursor(server, ns, cursorState, callback) { | |||
const bson = server.s.bson; | |||
const pool = server.s.pool; | |||
const cursorId = cursorState.cursorId; | |||
const killCursor = new KillCursor(bson, ns, [cursorId]); | |||
const options = { | |||
immediateRelease: true, | |||
noResponse: true | |||
}; | |||
if (typeof cursorState.session === 'object') { | |||
options.session = cursorState.session; | |||
} | |||
if (pool && pool.isConnected()) { | |||
try { | |||
pool.write(killCursor, options, callback); | |||
} catch (err) { | |||
if (typeof callback === 'function') { | |||
callback(err, null); | |||
} else { | |||
console.warn(err); | |||
} | |||
} | |||
} | |||
} | |||
getMore(server, ns, cursorState, batchSize, options, callback) { | |||
const bson = server.s.bson; | |||
const getMore = new GetMore(bson, ns, cursorState.cursorId, { numberToReturn: batchSize }); | |||
function queryCallback(err, result) { | |||
if (err) return callback(err); | |||
const response = result.message; | |||
// If we have a timed out query or a cursor that was killed | |||
if (response.cursorNotFound) { | |||
return callback(new MongoError('Cursor does not exist, was killed, or timed out'), null); | |||
} | |||
const cursorId = | |||
typeof response.cursorId === 'number' | |||
? Long.fromNumber(response.cursorId) | |||
: response.cursorId; | |||
cursorState.documents = response.documents; | |||
cursorState.cursorId = cursorId; | |||
callback(null, null, response.connection); | |||
} | |||
const queryOptions = applyCommonQueryOptions({}, cursorState); | |||
server.s.pool.write(getMore, queryOptions, queryCallback); | |||
} | |||
query(server, ns, cmd, cursorState, options, callback) { | |||
if (cursorState.cursorId != null) { | |||
return; | |||
} | |||
const query = setupClassicFind(server, ns, cmd, cursorState, options); | |||
const queryOptions = applyCommonQueryOptions({}, cursorState); | |||
if (typeof query.documentsReturnedIn === 'string') { | |||
queryOptions.documentsReturnedIn = query.documentsReturnedIn; | |||
} | |||
server.s.pool.write(query, queryOptions, callback); | |||
} | |||
command(server, ns, cmd, options, callback) { | |||
if (cmd == null) { | |||
return callback(new MongoError(`command ${JSON.stringify(cmd)} does not return a cursor`)); | |||
} | |||
options = options || {}; | |||
const bson = server.s.bson; | |||
const pool = server.s.pool; | |||
const readPreference = getReadPreference(cmd, options); | |||
let finalCmd = Object.assign({}, cmd); | |||
if (finalCmd.readConcern) { | |||
if (finalCmd.readConcern.level !== 'local') { | |||
return callback( | |||
new MongoError( | |||
`server ${JSON.stringify(finalCmd)} command does not support a readConcern level of ${ | |||
finalCmd.readConcern.level | |||
}` | |||
) | |||
); | |||
} | |||
delete finalCmd['readConcern']; | |||
} | |||
if (isMongos(server) && readPreference && readPreference.preference !== 'primary') { | |||
finalCmd = { | |||
$query: finalCmd, | |||
$readPreference: readPreference.toJSON() | |||
}; | |||
} | |||
const commandOptions = Object.assign( | |||
{ | |||
command: true, | |||
numberToSkip: 0, | |||
numberToReturn: -1, | |||
checkKeys: false | |||
}, | |||
options | |||
); | |||
// This value is not overridable | |||
commandOptions.slaveOk = readPreference.slaveOk(); | |||
try { | |||
const query = new Query(bson, `${databaseNamespace(ns)}.$cmd`, finalCmd, commandOptions); | |||
pool.write(query, commandOptions, callback); | |||
} catch (err) { | |||
callback(err); | |||
} | |||
} | |||
} | |||
function executeWrite(handler, server, type, opsField, ns, ops, options, callback) { | |||
if (ops.length === 0) throw new MongoError('insert must contain at least one document'); | |||
if (typeof options === 'function') { | |||
callback = options; | |||
options = {}; | |||
options = options || {}; | |||
} | |||
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true; | |||
const writeConcern = options.writeConcern; | |||
const writeCommand = {}; | |||
writeCommand[type] = collectionNamespace(ns); | |||
writeCommand[opsField] = ops; | |||
writeCommand.ordered = ordered; | |||
if (writeConcern && Object.keys(writeConcern).length > 0) { | |||
writeCommand.writeConcern = writeConcern; | |||
} | |||
if (options.bypassDocumentValidation === true) { | |||
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation; | |||
} | |||
const commandOptions = Object.assign( | |||
{ | |||
checkKeys: type === 'insert', | |||
numberToReturn: 1 | |||
}, | |||
options | |||
); | |||
handler.command(server, ns, writeCommand, commandOptions, callback); | |||
} | |||
function setupClassicFind(server, ns, cmd, cursorState, options) { | |||
options = options || {}; | |||
const bson = server.s.bson; | |||
const readPreference = getReadPreference(cmd, options); | |||
cursorState.batchSize = cmd.batchSize || cursorState.batchSize; | |||
let numberToReturn = 0; | |||
if (cursorState.limit === 0) { | |||
numberToReturn = cursorState.batchSize; | |||
} else if ( | |||
cursorState.limit < 0 || | |||
cursorState.limit < cursorState.batchSize || | |||
(cursorState.limit > 0 && cursorState.batchSize === 0) | |||
) { | |||
numberToReturn = cursorState.limit; | |||
} else { | |||
numberToReturn = cursorState.batchSize; | |||
} | |||
const numberToSkip = cursorState.skip || 0; | |||
const findCmd = {}; | |||
if (isMongos(server) && readPreference) { | |||
findCmd['$readPreference'] = readPreference.toJSON(); | |||
} | |||
if (cmd.sort) findCmd['$orderby'] = cmd.sort; | |||
if (cmd.hint) findCmd['$hint'] = cmd.hint; | |||
if (cmd.snapshot) findCmd['$snapshot'] = cmd.snapshot; | |||
if (typeof cmd.returnKey !== 'undefined') findCmd['$returnKey'] = cmd.returnKey; | |||
if (cmd.maxScan) findCmd['$maxScan'] = cmd.maxScan; | |||
if (cmd.min) findCmd['$min'] = cmd.min; | |||
if (cmd.max) findCmd['$max'] = cmd.max; | |||
if (typeof cmd.showDiskLoc !== 'undefined') findCmd['$showDiskLoc'] = cmd.showDiskLoc; | |||
if (cmd.comment) findCmd['$comment'] = cmd.comment; | |||
if (cmd.maxTimeMS) findCmd['$maxTimeMS'] = cmd.maxTimeMS; | |||
if (cmd.explain) { | |||
// nToReturn must be 0 (match all) or negative (match N and close cursor) | |||
// nToReturn > 0 will give explain results equivalent to limit(0) | |||
numberToReturn = -Math.abs(cmd.limit || 0); | |||
findCmd['$explain'] = true; | |||
} | |||
findCmd['$query'] = cmd.query; | |||
if (cmd.readConcern && cmd.readConcern.level !== 'local') { | |||
throw new MongoError( | |||
`server find command does not support a readConcern level of ${cmd.readConcern.level}` | |||
); | |||
} | |||
if (cmd.readConcern) { | |||
cmd = Object.assign({}, cmd); | |||
delete cmd['readConcern']; | |||
} | |||
const serializeFunctions = | |||
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; | |||
const ignoreUndefined = | |||
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false; | |||
const query = new Query(bson, ns, findCmd, { | |||
numberToSkip: numberToSkip, | |||
numberToReturn: numberToReturn, | |||
pre32Limit: typeof cmd.limit !== 'undefined' ? cmd.limit : undefined, | |||
checkKeys: false, | |||
returnFieldSelector: cmd.fields, | |||
serializeFunctions: serializeFunctions, | |||
ignoreUndefined: ignoreUndefined | |||
}); | |||
if (typeof cmd.tailable === 'boolean') query.tailable = cmd.tailable; | |||
if (typeof cmd.oplogReplay === 'boolean') query.oplogReplay = cmd.oplogReplay; | |||
if (typeof cmd.noCursorTimeout === 'boolean') query.noCursorTimeout = cmd.noCursorTimeout; | |||
if (typeof cmd.awaitData === 'boolean') query.awaitData = cmd.awaitData; | |||
if (typeof cmd.partial === 'boolean') query.partial = cmd.partial; | |||
query.slaveOk = readPreference.slaveOk(); | |||
return query; | |||
} | |||
module.exports = WireProtocol; |
@@ -0,0 +1,397 @@ | |||
'use strict'; | |||
const Query = require('../connection/commands').Query; | |||
const retrieveBSON = require('../connection/utils').retrieveBSON; | |||
const MongoError = require('../error').MongoError; | |||
const MongoNetworkError = require('../error').MongoNetworkError; | |||
const getReadPreference = require('./shared').getReadPreference; | |||
const BSON = retrieveBSON(); | |||
const Long = BSON.Long; | |||
const ReadPreference = require('../topologies/read_preference'); | |||
const TxnState = require('../transactions').TxnState; | |||
const isMongos = require('./shared').isMongos; | |||
const databaseNamespace = require('./shared').databaseNamespace; | |||
const collectionNamespace = require('./shared').collectionNamespace; | |||
class WireProtocol { | |||
insert(server, ns, ops, options, callback) { | |||
executeWrite(this, server, 'insert', 'documents', ns, ops, options, callback); | |||
} | |||
update(server, ns, ops, options, callback) { | |||
executeWrite(this, server, 'update', 'updates', ns, ops, options, callback); | |||
} | |||
remove(server, ns, ops, options, callback) { | |||
executeWrite(this, server, 'delete', 'deletes', ns, ops, options, callback); | |||
} | |||
killCursor(server, ns, cursorState, callback) { | |||
callback = typeof callback === 'function' ? callback : () => {}; | |||
const cursorId = cursorState.cursorId; | |||
const killCursorCmd = { | |||
killCursors: collectionNamespace(ns), | |||
cursors: [cursorId] | |||
}; | |||
const options = {}; | |||
if (typeof cursorState.session === 'object') options.session = cursorState.session; | |||
this.command(server, ns, killCursorCmd, options, (err, result) => { | |||
if (err) { | |||
return callback(err); | |||
} | |||
const response = result.message; | |||
if (response.cursorNotFound) { | |||
return callback(new MongoNetworkError('cursor killed or timed out'), null); | |||
} | |||
if (!Array.isArray(response.documents) || response.documents.length === 0) { | |||
return callback( | |||
new MongoError(`invalid killCursors result returned for cursor id ${cursorId}`) | |||
); | |||
} | |||
callback(null, response.documents[0]); | |||
}); | |||
} | |||
getMore(server, ns, cursorState, batchSize, options, callback) { | |||
options = options || {}; | |||
const getMoreCmd = { | |||
getMore: cursorState.cursorId, | |||
collection: collectionNamespace(ns), | |||
batchSize: Math.abs(batchSize) | |||
}; | |||
if (cursorState.cmd.tailable && typeof cursorState.cmd.maxAwaitTimeMS === 'number') { | |||
getMoreCmd.maxTimeMS = cursorState.cmd.maxAwaitTimeMS; | |||
} | |||
function queryCallback(err, result) { | |||
if (err) return callback(err); | |||
const response = result.message; | |||
// If we have a timed out query or a cursor that was killed | |||
if (response.cursorNotFound) { | |||
return callback(new MongoNetworkError('cursor killed or timed out'), null); | |||
} | |||
// Raw, return all the extracted documents | |||
if (cursorState.raw) { | |||
cursorState.documents = response.documents; | |||
cursorState.cursorId = response.cursorId; | |||
return callback(null, response.documents); | |||
} | |||
// We have an error detected | |||
if (response.documents[0].ok === 0) { | |||
return callback(new MongoError(response.documents[0])); | |||
} | |||
// Ensure we have a Long valid cursor id | |||
const cursorId = | |||
typeof response.documents[0].cursor.id === 'number' | |||
? Long.fromNumber(response.documents[0].cursor.id) | |||
: response.documents[0].cursor.id; | |||
cursorState.documents = response.documents[0].cursor.nextBatch; | |||
cursorState.cursorId = cursorId; | |||
callback(null, response.documents[0], response.connection); | |||
} | |||
const commandOptions = Object.assign( | |||
{ | |||
returnFieldSelector: null, | |||
documentsReturnedIn: 'nextBatch' | |||
}, | |||
options | |||
); | |||
this.command(server, ns, getMoreCmd, commandOptions, queryCallback); | |||
} | |||
query(server, ns, cmd, cursorState, options, callback) { | |||
options = options || {}; | |||
if (cursorState.cursorId != null) { | |||
return callback(); | |||
} | |||
if (cmd == null) { | |||
return callback(new MongoError(`command ${JSON.stringify(cmd)} does not return a cursor`)); | |||
} | |||
const readPreference = getReadPreference(cmd, options); | |||
const findCmd = prepareFindCommand(server, ns, cmd, cursorState, options); | |||
// NOTE: This actually modifies the passed in cmd, and our code _depends_ on this | |||
// side-effect. Change this ASAP | |||
cmd.virtual = false; | |||
const commandOptions = Object.assign( | |||
{ | |||
documentsReturnedIn: 'firstBatch', | |||
numberToReturn: 1, | |||
slaveOk: readPreference.slaveOk() | |||
}, | |||
options | |||
); | |||
if (cmd.readPreference) commandOptions.readPreference = readPreference; | |||
this.command(server, ns, findCmd, commandOptions, callback); | |||
} | |||
command(server, ns, cmd, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
if (cmd == null) { | |||
return callback(new MongoError(`command ${JSON.stringify(cmd)} does not return a cursor`)); | |||
} | |||
const bson = server.s.bson; | |||
const pool = server.s.pool; | |||
const readPreference = getReadPreference(cmd, options); | |||
let finalCmd = Object.assign({}, cmd); | |||
if (isMongos(server) && readPreference && readPreference.preference !== 'primary') { | |||
finalCmd = { | |||
$query: finalCmd, | |||
$readPreference: readPreference.toJSON() | |||
}; | |||
} | |||
const err = decorateWithSessionsData(finalCmd, options.session, options); | |||
if (err) { | |||
return callback(err); | |||
} | |||
const commandOptions = Object.assign( | |||
{ | |||
command: true, | |||
numberToSkip: 0, | |||
numberToReturn: -1, | |||
checkKeys: false | |||
}, | |||
options | |||
); | |||
// This value is not overridable | |||
commandOptions.slaveOk = readPreference.slaveOk(); | |||
try { | |||
const query = new Query(bson, `${databaseNamespace(ns)}.$cmd`, finalCmd, commandOptions); | |||
pool.write(query, commandOptions, callback); | |||
} catch (err) { | |||
callback(err); | |||
} | |||
} | |||
} | |||
function isTransactionCommand(command) { | |||
return !!(command.commitTransaction || command.abortTransaction); | |||
} | |||
/** | |||
* Optionally decorate a command with sessions specific keys | |||
* | |||
* @param {Object} command the command to decorate | |||
* @param {ClientSession} session the session tracking transaction state | |||
* @param {Object} [options] Optional settings passed to calling operation | |||
* @param {Function} [callback] Optional callback passed from calling operation | |||
* @return {MongoError|null} An error, if some error condition was met | |||
*/ | |||
function decorateWithSessionsData(command, session, options) { | |||
if (!session) { | |||
return; | |||
} | |||
// first apply non-transaction-specific sessions data | |||
const serverSession = session.serverSession; | |||
const inTransaction = session.inTransaction() || isTransactionCommand(command); | |||
const isRetryableWrite = options.willRetryWrite; | |||
if (serverSession.txnNumber && (isRetryableWrite || inTransaction)) { | |||
command.txnNumber = BSON.Long.fromNumber(serverSession.txnNumber); | |||
} | |||
// now attempt to apply transaction-specific sessions data | |||
if (!inTransaction) { | |||
if (session.transaction.state !== TxnState.NO_TRANSACTION) { | |||
session.transaction.transition(TxnState.NO_TRANSACTION); | |||
} | |||
// for causal consistency | |||
if (session.supports.causalConsistency && session.operationTime) { | |||
command.readConcern = command.readConcern || {}; | |||
Object.assign(command.readConcern, { afterClusterTime: session.operationTime }); | |||
} | |||
return; | |||
} | |||
if (options.readPreference && !options.readPreference.equals(ReadPreference.primary)) { | |||
return new MongoError( | |||
`Read preference in a transaction must be primary, not: ${options.readPreference.mode}` | |||
); | |||
} | |||
// `autocommit` must always be false to differentiate from retryable writes | |||
command.autocommit = false; | |||
if (session.transaction.state === TxnState.STARTING_TRANSACTION) { | |||
session.transaction.transition(TxnState.TRANSACTION_IN_PROGRESS); | |||
command.startTransaction = true; | |||
const readConcern = | |||
session.transaction.options.readConcern || session.clientOptions.readConcern; | |||
if (readConcern) { | |||
command.readConcern = readConcern; | |||
} | |||
if (session.supports.causalConsistency && session.operationTime) { | |||
command.readConcern = command.readConcern || {}; | |||
Object.assign(command.readConcern, { afterClusterTime: session.operationTime }); | |||
} | |||
} | |||
} | |||
function executeWrite(handler, server, type, opsField, ns, ops, options, callback) { | |||
if (ops.length === 0) throw new MongoError('insert must contain at least one document'); | |||
if (typeof options === 'function') { | |||
callback = options; | |||
options = {}; | |||
options = options || {}; | |||
} | |||
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true; | |||
const writeConcern = options.writeConcern; | |||
const writeCommand = {}; | |||
writeCommand[type] = collectionNamespace(ns); | |||
writeCommand[opsField] = ops; | |||
writeCommand.ordered = ordered; | |||
if (writeConcern && Object.keys(writeConcern).length > 0) { | |||
writeCommand.writeConcern = writeConcern; | |||
} | |||
if (options.collation) { | |||
for (let i = 0; i < writeCommand[opsField].length; i++) { | |||
if (!writeCommand[opsField][i].collation) { | |||
writeCommand[opsField][i].collation = options.collation; | |||
} | |||
} | |||
} | |||
if (options.bypassDocumentValidation === true) { | |||
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation; | |||
} | |||
const commandOptions = Object.assign( | |||
{ | |||
checkKeys: type === 'insert', | |||
numberToReturn: 1 | |||
}, | |||
options | |||
); | |||
handler.command(server, ns, writeCommand, commandOptions, callback); | |||
} | |||
function prepareFindCommand(server, ns, cmd, cursorState) { | |||
cursorState.batchSize = cmd.batchSize || cursorState.batchSize; | |||
let findCmd = { | |||
find: collectionNamespace(ns) | |||
}; | |||
if (cmd.query) { | |||
if (cmd.query['$query']) { | |||
findCmd.filter = cmd.query['$query']; | |||
} else { | |||
findCmd.filter = cmd.query; | |||
} | |||
} | |||
let sortValue = cmd.sort; | |||
if (Array.isArray(sortValue)) { | |||
const sortObject = {}; | |||
if (sortValue.length > 0 && !Array.isArray(sortValue[0])) { | |||
let sortDirection = sortValue[1]; | |||
if (sortDirection === 'asc') { | |||
sortDirection = 1; | |||
} else if (sortDirection === 'desc') { | |||
sortDirection = -1; | |||
} | |||
sortObject[sortValue[0]] = sortDirection; | |||
} else { | |||
for (let i = 0; i < sortValue.length; i++) { | |||
let sortDirection = sortValue[i][1]; | |||
if (sortDirection === 'asc') { | |||
sortDirection = 1; | |||
} else if (sortDirection === 'desc') { | |||
sortDirection = -1; | |||
} | |||
sortObject[sortValue[i][0]] = sortDirection; | |||
} | |||
} | |||
sortValue = sortObject; | |||
} | |||
if (cmd.sort) findCmd.sort = sortValue; | |||
if (cmd.fields) findCmd.projection = cmd.fields; | |||
if (cmd.hint) findCmd.hint = cmd.hint; | |||
if (cmd.skip) findCmd.skip = cmd.skip; | |||
if (cmd.limit) findCmd.limit = cmd.limit; | |||
if (cmd.limit < 0) { | |||
findCmd.limit = Math.abs(cmd.limit); | |||
findCmd.singleBatch = true; | |||
} | |||
if (typeof cmd.batchSize === 'number') { | |||
if (cmd.batchSize < 0) { | |||
if (cmd.limit !== 0 && Math.abs(cmd.batchSize) < Math.abs(cmd.limit)) { | |||
findCmd.limit = Math.abs(cmd.batchSize); | |||
} | |||
findCmd.singleBatch = true; | |||
} | |||
findCmd.batchSize = Math.abs(cmd.batchSize); | |||
} | |||
if (cmd.comment) findCmd.comment = cmd.comment; | |||
if (cmd.maxScan) findCmd.maxScan = cmd.maxScan; | |||
if (cmd.maxTimeMS) findCmd.maxTimeMS = cmd.maxTimeMS; | |||
if (cmd.min) findCmd.min = cmd.min; | |||
if (cmd.max) findCmd.max = cmd.max; | |||
findCmd.returnKey = cmd.returnKey ? cmd.returnKey : false; | |||
findCmd.showRecordId = cmd.showDiskLoc ? cmd.showDiskLoc : false; | |||
if (cmd.snapshot) findCmd.snapshot = cmd.snapshot; | |||
if (cmd.tailable) findCmd.tailable = cmd.tailable; | |||
if (cmd.oplogReplay) findCmd.oplogReplay = cmd.oplogReplay; | |||
if (cmd.noCursorTimeout) findCmd.noCursorTimeout = cmd.noCursorTimeout; | |||
if (cmd.awaitData) findCmd.awaitData = cmd.awaitData; | |||
if (cmd.awaitdata) findCmd.awaitData = cmd.awaitdata; | |||
if (cmd.partial) findCmd.partial = cmd.partial; | |||
if (cmd.collation) findCmd.collation = cmd.collation; | |||
if (cmd.readConcern) findCmd.readConcern = cmd.readConcern; | |||
// If we have explain, we need to rewrite the find command | |||
// to wrap it in the explain command | |||
if (cmd.explain) { | |||
findCmd = { | |||
explain: findCmd | |||
}; | |||
} | |||
return findCmd; | |||
} | |||
module.exports = WireProtocol; |
@@ -0,0 +1,73 @@ | |||
'use strict'; | |||
var Snappy = require('../connection/utils').retrieveSnappy(), | |||
zlib = require('zlib'); | |||
var compressorIDs = { | |||
snappy: 1, | |||
zlib: 2 | |||
}; | |||
var uncompressibleCommands = [ | |||
'ismaster', | |||
'saslStart', | |||
'saslContinue', | |||
'getnonce', | |||
'authenticate', | |||
'createUser', | |||
'updateUser', | |||
'copydbSaslStart', | |||
'copydbgetnonce', | |||
'copydb' | |||
]; | |||
// Facilitate compressing a message using an agreed compressor | |||
var compress = function(self, dataToBeCompressed, callback) { | |||
switch (self.options.agreedCompressor) { | |||
case 'snappy': | |||
Snappy.compress(dataToBeCompressed, callback); | |||
break; | |||
case 'zlib': | |||
// Determine zlibCompressionLevel | |||
var zlibOptions = {}; | |||
if (self.options.zlibCompressionLevel) { | |||
zlibOptions.level = self.options.zlibCompressionLevel; | |||
} | |||
zlib.deflate(dataToBeCompressed, zlibOptions, callback); | |||
break; | |||
default: | |||
throw new Error( | |||
'Attempt to compress message using unknown compressor "' + | |||
self.options.agreedCompressor + | |||
'".' | |||
); | |||
} | |||
}; | |||
// Decompress a message using the given compressor | |||
var decompress = function(compressorID, compressedData, callback) { | |||
if (compressorID < 0 || compressorID > compressorIDs.length) { | |||
throw new Error( | |||
'Server sent message compressed using an unsupported compressor. (Received compressor ID ' + | |||
compressorID + | |||
')' | |||
); | |||
} | |||
switch (compressorID) { | |||
case compressorIDs.snappy: | |||
Snappy.uncompress(compressedData, callback); | |||
break; | |||
case compressorIDs.zlib: | |||
zlib.inflate(compressedData, callback); | |||
break; | |||
default: | |||
callback(null, compressedData); | |||
} | |||
}; | |||
module.exports = { | |||
compressorIDs: compressorIDs, | |||
uncompressibleCommands: uncompressibleCommands, | |||
compress: compress, | |||
decompress: decompress | |||
}; |
@@ -0,0 +1,101 @@ | |||
'use strict'; | |||
var ReadPreference = require('../topologies/read_preference'), | |||
MongoError = require('../error').MongoError; | |||
var MESSAGE_HEADER_SIZE = 16; | |||
// OPCODE Numbers | |||
// Defined at https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#request-opcodes | |||
var opcodes = { | |||
OP_REPLY: 1, | |||
OP_UPDATE: 2001, | |||
OP_INSERT: 2002, | |||
OP_QUERY: 2004, | |||
OP_GETMORE: 2005, | |||
OP_DELETE: 2006, | |||
OP_KILL_CURSORS: 2007, | |||
OP_COMPRESSED: 2012 | |||
}; | |||
var getReadPreference = function(cmd, options) { | |||
// Default to command version of the readPreference | |||
var readPreference = cmd.readPreference || new ReadPreference('primary'); | |||
// If we have an option readPreference override the command one | |||
if (options.readPreference) { | |||
readPreference = options.readPreference; | |||
} | |||
if (typeof readPreference === 'string') { | |||
readPreference = new ReadPreference(readPreference); | |||
} | |||
if (!(readPreference instanceof ReadPreference)) { | |||
throw new MongoError('read preference must be a ReadPreference instance'); | |||
} | |||
return readPreference; | |||
}; | |||
// Parses the header of a wire protocol message | |||
var parseHeader = function(message) { | |||
return { | |||
length: message.readInt32LE(0), | |||
requestId: message.readInt32LE(4), | |||
responseTo: message.readInt32LE(8), | |||
opCode: message.readInt32LE(12) | |||
}; | |||
}; | |||
function applyCommonQueryOptions(queryOptions, options) { | |||
Object.assign(queryOptions, { | |||
raw: typeof options.raw === 'boolean' ? options.raw : false, | |||
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true, | |||
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true, | |||
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false, | |||
monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false, | |||
fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false | |||
}); | |||
if (typeof options.socketTimeout === 'number') { | |||
queryOptions.socketTimeout = options.socketTimeout; | |||
} | |||
if (options.session) { | |||
queryOptions.session = options.session; | |||
} | |||
if (typeof options.documentsReturnedIn === 'string') { | |||
queryOptions.documentsReturnedIn = options.documentsReturnedIn; | |||
} | |||
return queryOptions; | |||
} | |||
function isMongos(server) { | |||
if (server.type === 'mongos') return true; | |||
if (server.parent && server.parent.type === 'mongos') return true; | |||
// NOTE: handle unified topology | |||
return false; | |||
} | |||
function databaseNamespace(ns) { | |||
return ns.split('.')[0]; | |||
} | |||
function collectionNamespace(ns) { | |||
return ns | |||
.split('.') | |||
.slice(1) | |||
.join('.'); | |||
} | |||
module.exports = { | |||
getReadPreference, | |||
MESSAGE_HEADER_SIZE, | |||
opcodes, | |||
parseHeader, | |||
applyCommonQueryOptions, | |||
isMongos, | |||
databaseNamespace, | |||
collectionNamespace | |||
}; |
@@ -0,0 +1,94 @@ | |||
{ | |||
"_args": [ | |||
[ | |||
"mongodb-core@3.1.11", | |||
"/Users/xenia/Desktop/B_ME5/Projekt/om" | |||
] | |||
], | |||
"_from": "mongodb-core@3.1.11", | |||
"_id": "mongodb-core@3.1.11", | |||
"_inBundle": false, | |||
"_integrity": "sha512-rD2US2s5qk/ckbiiGFHeu+yKYDXdJ1G87F6CG3YdaZpzdOm5zpoAZd/EKbPmFO6cQZ+XVXBXBJ660sSI0gc6qg==", | |||
"_location": "/mongoose/mongodb-core", | |||
"_phantomChildren": {}, | |||
"_requested": { | |||
"type": "version", | |||
"registry": true, | |||
"raw": "mongodb-core@3.1.11", | |||
"name": "mongodb-core", | |||
"escapedName": "mongodb-core", | |||
"rawSpec": "3.1.11", | |||
"saveSpec": null, | |||
"fetchSpec": "3.1.11" | |||
}, | |||
"_requiredBy": [ | |||
"/mongoose", | |||
"/mongoose/mongodb" | |||
], | |||
"_resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.11.tgz", | |||
"_spec": "3.1.11", | |||
"_where": "/Users/xenia/Desktop/B_ME5/Projekt/om", | |||
"author": { | |||
"name": "Christian Kvalheim" | |||
}, | |||
"bugs": { | |||
"url": "https://github.com/mongodb-js/mongodb-core/issues" | |||
}, | |||
"dependencies": { | |||
"bson": "^1.1.0", | |||
"require_optional": "^1.0.1", | |||
"safe-buffer": "^5.1.2", | |||
"saslprep": "^1.0.0" | |||
}, | |||
"description": "Core MongoDB driver functionality, no bells and whistles and meant for integration not end applications", | |||
"devDependencies": { | |||
"chai": "^4.1.2", | |||
"chai-subset": "^1.6.0", | |||
"co": "^4.6.0", | |||
"eslint": "^4.6.1", | |||
"eslint-plugin-prettier": "^2.2.0", | |||
"jsdoc": "3.5.4", | |||
"mongodb-extjson": "^2.1.2", | |||
"mongodb-mock-server": "^1.0.0", | |||
"mongodb-test-runner": "^1.1.18", | |||
"prettier": "~1.12.0", | |||
"sinon": "^6.0.0", | |||
"snappy": "^6.1.1", | |||
"standard-version": "^4.4.0" | |||
}, | |||
"files": [ | |||
"index.js", | |||
"lib" | |||
], | |||
"homepage": "https://github.com/mongodb-js/mongodb-core", | |||
"keywords": [ | |||
"mongodb", | |||
"core" | |||
], | |||
"license": "Apache-2.0", | |||
"main": "index.js", | |||
"name": "mongodb-core", | |||
"optionalDependencies": { | |||
"saslprep": "^1.0.0" | |||
}, | |||
"peerOptionalDependencies": { | |||
"kerberos": "^1.0.0", | |||
"mongodb-extjson": "^2.1.2", | |||
"snappy": "^6.1.1", | |||
"bson-ext": "^2.0.0" | |||
}, | |||
"repository": { | |||
"type": "git", | |||
"url": "git://github.com/mongodb-js/mongodb-core.git" | |||
}, | |||
"scripts": { | |||
"atlas": "node ./test/atlas.js", | |||
"changelog": "conventional-changelog -p angular -i HISTORY.md -s", | |||
"coverage": "nyc node test/runner.js -t functional -l && node_modules/.bin/nyc report --reporter=text-lcov | node_modules/.bin/coveralls", | |||
"format": "prettier --print-width 100 --tab-width 2 --single-quote --write index.js test/**/*.js lib/**/*.js", | |||
"lint": "eslint index.js lib test", | |||
"release": "standard-version -i HISTORY.md", | |||
"test": "npm run lint && mongodb-test-runner -t 60000 test/tests" | |||
}, | |||
"version": "3.1.11" | |||
} |
@@ -0,0 +1,201 @@ | |||
Apache License | |||
Version 2.0, January 2004 | |||
http://www.apache.org/licenses/ | |||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | |||
1. Definitions. | |||
"License" shall mean the terms and conditions for use, reproduction, | |||
and distribution as defined by Sections 1 through 9 of this document. | |||
"Licensor" shall mean the copyright owner or entity authorized by | |||
the copyright owner that is granting the License. | |||
"Legal Entity" shall mean the union of the acting entity and all | |||
other entities that control, are controlled by, or are under common | |||
control with that entity. For the purposes of this definition, | |||
"control" means (i) the power, direct or indirect, to cause the | |||
direction or management of such entity, whether by contract or | |||
otherwise, or (ii) ownership of fifty percent (50%) or more of the | |||
outstanding shares, or (iii) beneficial ownership of such entity. | |||
"You" (or "Your") shall mean an individual or Legal Entity | |||
exercising permissions granted by this License. | |||
"Source" form shall mean the preferred form for making modifications, | |||
including but not limited to software source code, documentation | |||
source, and configuration files. | |||
"Object" form shall mean any form resulting from mechanical | |||
transformation or translation of a Source form, including but | |||
not limited to compiled object code, generated documentation, | |||
and conversions to other media types. | |||
"Work" shall mean the work of authorship, whether in Source or | |||
Object form, made available under the License, as indicated by a | |||
copyright notice that is included in or attached to the work | |||
(an example is provided in the Appendix below). | |||
"Derivative Works" shall mean any work, whether in Source or Object | |||
form, that is based on (or derived from) the Work and for which the | |||
editorial revisions, annotations, elaborations, or other modifications | |||
represent, as a whole, an original work of authorship. For the purposes | |||
of this License, Derivative Works shall not include works that remain | |||
separable from, or merely link (or bind by name) to the interfaces of, | |||
the Work and Derivative Works thereof. | |||
"Contribution" shall mean any work of authorship, including | |||
the original version of the Work and any modifications or additions | |||
to that Work or Derivative Works thereof, that is intentionally | |||
submitted to Licensor for inclusion in the Work by the copyright owner | |||
or by an individual or Legal Entity authorized to submit on behalf of | |||
the copyright owner. For the purposes of this definition, "submitted" | |||
means any form of electronic, verbal, or written communication sent | |||
to the Licensor or its representatives, including but not limited to | |||
communication on electronic mailing lists, source code control systems, | |||
and issue tracking systems that are managed by, or on behalf of, the | |||
Licensor for the purpose of discussing and improving the Work, but | |||
excluding communication that is conspicuously marked or otherwise | |||
designated in writing by the copyright owner as "Not a Contribution." | |||
"Contributor" shall mean Licensor and any individual or Legal Entity | |||
on behalf of whom a Contribution has been received by Licensor and | |||
subsequently incorporated within the Work. | |||
2. Grant of Copyright License. Subject to the terms and conditions of | |||
this License, each Contributor hereby grants to You a perpetual, | |||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |||
copyright license to reproduce, prepare Derivative Works of, | |||
publicly display, publicly perform, sublicense, and distribute the | |||
Work and such Derivative Works in Source or Object form. | |||
3. Grant of Patent License. Subject to the terms and conditions of | |||
this License, each Contributor hereby grants to You a perpetual, | |||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | |||
(except as stated in this section) patent license to make, have made, | |||
use, offer to sell, sell, import, and otherwise transfer the Work, | |||
where such license applies only to those patent claims licensable | |||
by such Contributor that are necessarily infringed by their | |||
Contribution(s) alone or by combination of their Contribution(s) | |||
with the Work to which such Contribution(s) was submitted. If You | |||
institute patent litigation against any entity (including a | |||
cross-claim or counterclaim in a lawsuit) alleging that the Work | |||
or a Contribution incorporated within the Work constitutes direct | |||
or contributory patent infringement, then any patent licenses | |||
granted to You under this License for that Work shall terminate | |||
as of the date such litigation is filed. | |||
4. Redistribution. You may reproduce and distribute copies of the | |||
Work or Derivative Works thereof in any medium, with or without | |||
modifications, and in Source or Object form, provided that You | |||
meet the following conditions: | |||
(a) You must give any other recipients of the Work or | |||
Derivative Works a copy of this License; and | |||
(b) You must cause any modified files to carry prominent notices | |||
stating that You changed the files; and | |||
(c) You must retain, in the Source form of any Derivative Works | |||
that You distribute, all copyright, patent, trademark, and | |||
attribution notices from the Source form of the Work, | |||
excluding those notices that do not pertain to any part of | |||
the Derivative Works; and | |||
(d) If the Work includes a "NOTICE" text file as part of its | |||
distribution, then any Derivative Works that You distribute must | |||
include a readable copy of the attribution notices contained | |||
within such NOTICE file, excluding those notices that do not | |||
pertain to any part of the Derivative Works, in at least one | |||
of the following places: within a NOTICE text file distributed | |||
as part of the Derivative Works; within the Source form or | |||
documentation, if provided along with the Derivative Works; or, | |||
within a display generated by the Derivative Works, if and | |||
wherever such third-party notices normally appear. The contents | |||
of the NOTICE file are for informational purposes only and | |||
do not modify the License. You may add Your own attribution | |||
notices within Derivative Works that You distribute, alongside | |||
or as an addendum to the NOTICE text from the Work, provided | |||
that such additional attribution notices cannot be construed | |||
as modifying the License. | |||
You may add Your own copyright statement to Your modifications and | |||
may provide additional or different license terms and conditions | |||
for use, reproduction, or distribution of Your modifications, or | |||
for any such Derivative Works as a whole, provided Your use, | |||
reproduction, and distribution of the Work otherwise complies with | |||
the conditions stated in this License. | |||
5. Submission of Contributions. Unless You explicitly state otherwise, | |||
any Contribution intentionally submitted for inclusion in the Work | |||
by You to the Licensor shall be under the terms and conditions of | |||
this License, without any additional terms or conditions. | |||
Notwithstanding the above, nothing herein shall supersede or modify | |||
the terms of any separate license agreement you may have executed | |||
with Licensor regarding such Contributions. | |||
6. Trademarks. This License does not grant permission to use the trade | |||
names, trademarks, service marks, or product names of the Licensor, | |||
except as required for reasonable and customary use in describing the | |||
origin of the Work and reproducing the content of the NOTICE file. | |||
7. Disclaimer of Warranty. Unless required by applicable law or | |||
agreed to in writing, Licensor provides the Work (and each | |||
Contributor provides its Contributions) on an "AS IS" BASIS, | |||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |||
implied, including, without limitation, any warranties or conditions | |||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | |||
PARTICULAR PURPOSE. You are solely responsible for determining the | |||
appropriateness of using or redistributing the Work and assume any | |||
risks associated with Your exercise of permissions under this License. | |||
8. Limitation of Liability. In no event and under no legal theory, | |||
whether in tort (including negligence), contract, or otherwise, | |||
unless required by applicable law (such as deliberate and grossly | |||
negligent acts) or agreed to in writing, shall any Contributor be | |||
liable to You for damages, including any direct, indirect, special, | |||
incidental, or consequential damages of any character arising as a | |||
result of this License or out of the use or inability to use the | |||
Work (including but not limited to damages for loss of goodwill, | |||
work stoppage, computer failure or malfunction, or any and all | |||
other commercial damages or losses), even if such Contributor | |||
has been advised of the possibility of such damages. | |||
9. Accepting Warranty or Additional Liability. While redistributing | |||
the Work or Derivative Works thereof, You may choose to offer, | |||
and charge a fee for, acceptance of support, warranty, indemnity, | |||
or other liability obligations and/or rights consistent with this | |||
License. However, in accepting such obligations, You may act only | |||
on Your own behalf and on Your sole responsibility, not on behalf | |||
of any other Contributor, and only if You agree to indemnify, | |||
defend, and hold each Contributor harmless for any liability | |||
incurred by, or claims asserted against, such Contributor by reason | |||
of your accepting any such warranty or additional liability. | |||
END OF TERMS AND CONDITIONS | |||
APPENDIX: How to apply the Apache License to your work. | |||
To apply the Apache License to your work, attach the following | |||
boilerplate notice, with the fields enclosed by brackets "{}" | |||
replaced with your own identifying information. (Don't include | |||
the brackets!) The text should be enclosed in the appropriate | |||
comment syntax for the file format. We also recommend that a | |||
file or class name and description of purpose be included on the | |||
same "printed page" as the copyright notice for easier | |||
identification within third-party archives. | |||
Copyright {yyyy} {name of copyright owner} | |||
Licensed under the Apache License, Version 2.0 (the "License"); | |||
you may not use this file except in compliance with the License. | |||
You may obtain a copy of the License at | |||
http://www.apache.org/licenses/LICENSE-2.0 | |||
Unless required by applicable law or agreed to in writing, software | |||
distributed under the License is distributed on an "AS IS" BASIS, | |||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
See the License for the specific language governing permissions and | |||
limitations under the License. |
@@ -0,0 +1,493 @@ | |||
[![npm](https://nodei.co/npm/mongodb.png?downloads=true&downloadRank=true)](https://nodei.co/npm/mongodb/) [![npm](https://nodei.co/npm-dl/mongodb.png?months=6&height=3)](https://nodei.co/npm/mongodb/) | |||
[![Build Status](https://secure.travis-ci.org/mongodb/node-mongodb-native.svg?branch=2.1)](http://travis-ci.org/mongodb/node-mongodb-native) | |||
[![Coverage Status](https://coveralls.io/repos/github/mongodb/node-mongodb-native/badge.svg?branch=2.1)](https://coveralls.io/github/mongodb/node-mongodb-native?branch=2.1) | |||
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mongodb/node-mongodb-native?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) | |||
# Description | |||
The official [MongoDB](https://www.mongodb.com/) driver for Node.js. Provides a high-level API on top of [mongodb-core](https://www.npmjs.com/package/mongodb-core) that is meant for end users. | |||
**NOTE: v3.x was recently released with breaking API changes. You can find a list of changes [here](CHANGES_3.0.0.md).** | |||
## MongoDB Node.JS Driver | |||
| what | where | | |||
|---------------|------------------------------------------------| | |||
| documentation | http://mongodb.github.io/node-mongodb-native | | |||
| api-doc | http://mongodb.github.io/node-mongodb-native/3.1/api | | |||
| source | https://github.com/mongodb/node-mongodb-native | | |||
| mongodb | http://www.mongodb.org | | |||
### Bugs / Feature Requests | |||
Think you’ve found a bug? Want to see a new feature in `node-mongodb-native`? Please open a | |||
case in our issue management tool, JIRA: | |||
- Create an account and login [jira.mongodb.org](https://jira.mongodb.org). | |||
- Navigate to the NODE project [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE). | |||
- Click **Create Issue** - Please provide as much information as possible about the issue type and how to reproduce it. | |||
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the | |||
Core Server (i.e. SERVER) project are **public**. | |||
### Questions and Bug Reports | |||
* Mailing List: [groups.google.com/forum/#!forum/node-mongodb-native](https://groups.google.com/forum/#!forum/node-mongodb-native) | |||
* JIRA: [jira.mongodb.org](http://jira.mongodb.org) | |||
### Change Log | |||
Change history can be found in [`HISTORY.md`](HISTORY.md). | |||
# Installation | |||
The recommended way to get started using the Node.js 3.0 driver is by using the `npm` (Node Package Manager) to install the dependency in your project. | |||
## MongoDB Driver | |||
Given that you have created your own project using `npm init` we install the MongoDB driver and its dependencies by executing the following `npm` command. | |||
```bash | |||
npm install mongodb --save | |||
``` | |||
This will download the MongoDB driver and add a dependency entry in your `package.json` file. | |||
You can also use the [Yarn](https://yarnpkg.com/en) package manager. | |||
## Troubleshooting | |||
The MongoDB driver depends on several other packages. These are: | |||
* [mongodb-core](https://github.com/mongodb-js/mongodb-core) | |||
* [bson](https://github.com/mongodb/js-bson) | |||
* [kerberos](https://github.com/mongodb-js/kerberos) | |||
* [node-gyp](https://github.com/nodejs/node-gyp) | |||
The `kerberos` package is a C++ extension that requires a build environment to be installed on your system. You must be able to build Node.js itself in order to compile and install the `kerberos` module. Furthermore, the `kerberos` module requires the MIT Kerberos package to correctly compile on UNIX operating systems. Consult your UNIX operation system package manager for what libraries to install. | |||
**Windows already contains the SSPI API used for Kerberos authentication. However, you will need to install a full compiler tool chain using Visual Studio C++ to correctly install the Kerberos extension.** | |||
### Diagnosing on UNIX | |||
If you don’t have the build-essentials, this module won’t build. In the case of Linux, you will need gcc, g++, Node.js with all the headers and Python. The easiest way to figure out what’s missing is by trying to build the Kerberos project. You can do this by performing the following steps. | |||
```bash | |||
git clone https://github.com/mongodb-js/kerberos | |||
cd kerberos | |||
npm install | |||
``` | |||
If all the steps complete, you have the right toolchain installed. If you get the error "node-gyp not found," you need to install `node-gyp` globally: | |||
```bash | |||
npm install -g node-gyp | |||
``` | |||
If it correctly compiles and runs the tests you are golden. We can now try to install the `mongod` driver by performing the following command. | |||
```bash | |||
cd yourproject | |||
npm install mongodb --save | |||
``` | |||
If it still fails the next step is to examine the npm log. Rerun the command but in this case in verbose mode. | |||
```bash | |||
npm --loglevel verbose install mongodb | |||
``` | |||
This will print out all the steps npm is performing while trying to install the module. | |||
### Diagnosing on Windows | |||
A compiler tool chain known to work for compiling `kerberos` on Windows is the following. | |||
* Visual Studio C++ 2010 (do not use higher versions) | |||
* Windows 7 64bit SDK | |||
* Python 2.7 or higher | |||
Open the Visual Studio command prompt. Ensure `node.exe` is in your path and install `node-gyp`. | |||
```bash | |||
npm install -g node-gyp | |||
``` | |||
Next, you will have to build the project manually to test it. Clone the repo, install dependencies and rebuild: | |||
```bash | |||
git clone https://github.com/christkv/kerberos.git | |||
cd kerberos | |||
npm install | |||
node-gyp rebuild | |||
``` | |||
This should rebuild the driver successfully if you have everything set up correctly. | |||
### Other possible issues | |||
Your Python installation might be hosed making gyp break. Test your deployment environment first by trying to build Node.js itself on the server in question, as this should unearth any issues with broken packages (and there are a lot of broken packages out there). | |||
Another tip is to ensure your user has write permission to wherever the Node.js modules are being installed. | |||
## Quick Start | |||
This guide will show you how to set up a simple application using Node.js and MongoDB. Its scope is only how to set up the driver and perform the simple CRUD operations. For more in-depth coverage, see the [tutorials](docs/reference/content/tutorials/main.md). | |||
### Create the `package.json` file | |||
First, create a directory where your application will live. | |||
```bash | |||
mkdir myproject | |||
cd myproject | |||
``` | |||
Enter the following command and answer the questions to create the initial structure for your new project: | |||
```bash | |||
npm init | |||
``` | |||
Next, install the driver dependency. | |||
```bash | |||
npm install mongodb --save | |||
``` | |||
You should see **NPM** download a lot of files. Once it's done you'll find all the downloaded packages under the **node_modules** directory. | |||
### Start a MongoDB Server | |||
For complete MongoDB installation instructions, see [the manual](https://docs.mongodb.org/manual/installation/). | |||
1. Download the right MongoDB version from [MongoDB](https://www.mongodb.org/downloads) | |||
2. Create a database directory (in this case under **/data**). | |||
3. Install and start a ``mongod`` process. | |||
```bash | |||
mongod --dbpath=/data | |||
``` | |||
You should see the **mongod** process start up and print some status information. | |||
### Connect to MongoDB | |||
Create a new **app.js** file and add the following code to try out some basic CRUD | |||
operations using the MongoDB driver. | |||
Add code to connect to the server and the database **myproject**: | |||
```js | |||
const MongoClient = require('mongodb').MongoClient; | |||
const assert = require('assert'); | |||
// Connection URL | |||
const url = 'mongodb://localhost:27017'; | |||
// Database Name | |||
const dbName = 'myproject'; | |||
// Use connect method to connect to the server | |||
MongoClient.connect(url, function(err, client) { | |||
assert.equal(null, err); | |||
console.log("Connected successfully to server"); | |||
const db = client.db(dbName); | |||
client.close(); | |||
}); | |||
``` | |||
Run your app from the command line with: | |||
```bash | |||
node app.js | |||
``` | |||
The application should print **Connected successfully to server** to the console. | |||
### Insert a Document | |||
Add to **app.js** the following function which uses the **insertMany** | |||
method to add three documents to the **documents** collection. | |||
```js | |||
const insertDocuments = function(db, callback) { | |||
// Get the documents collection | |||
const collection = db.collection('documents'); | |||
// Insert some documents | |||
collection.insertMany([ | |||
{a : 1}, {a : 2}, {a : 3} | |||
], function(err, result) { | |||
assert.equal(err, null); | |||
assert.equal(3, result.result.n); | |||
assert.equal(3, result.ops.length); | |||
console.log("Inserted 3 documents into the collection"); | |||
callback(result); | |||
}); | |||
} | |||
``` | |||
The **insert** command returns an object with the following fields: | |||
* **result** Contains the result document from MongoDB | |||
* **ops** Contains the documents inserted with added **_id** fields | |||
* **connection** Contains the connection used to perform the insert | |||
Add the following code to call the **insertDocuments** function: | |||
```js | |||
const MongoClient = require('mongodb').MongoClient; | |||
const assert = require('assert'); | |||
// Connection URL | |||
const url = 'mongodb://localhost:27017'; | |||
// Database Name | |||
const dbName = 'myproject'; | |||
// Use connect method to connect to the server | |||
MongoClient.connect(url, function(err, client) { | |||
assert.equal(null, err); | |||
console.log("Connected successfully to server"); | |||
const db = client.db(dbName); | |||
insertDocuments(db, function() { | |||
client.close(); | |||
}); | |||
}); | |||
``` | |||
Run the updated **app.js** file: | |||
```bash | |||
node app.js | |||
``` | |||
The operation returns the following output: | |||
```bash | |||
Connected successfully to server | |||
Inserted 3 documents into the collection | |||
``` | |||
### Find All Documents | |||
Add a query that returns all the documents. | |||
```js | |||
const findDocuments = function(db, callback) { | |||
// Get the documents collection | |||
const collection = db.collection('documents'); | |||
// Find some documents | |||
collection.find({}).toArray(function(err, docs) { | |||
assert.equal(err, null); | |||
console.log("Found the following records"); | |||
console.log(docs) | |||
callback(docs); | |||
}); | |||
} | |||
``` | |||
This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback: | |||
```js | |||
const MongoClient = require('mongodb').MongoClient; | |||
const assert = require('assert'); | |||
// Connection URL | |||
const url = 'mongodb://localhost:27017'; | |||
// Database Name | |||
const dbName = 'myproject'; | |||
// Use connect method to connect to the server | |||
MongoClient.connect(url, function(err, client) { | |||
assert.equal(null, err); | |||
console.log("Connected correctly to server"); | |||
const db = client.db(dbName); | |||
insertDocuments(db, function() { | |||
findDocuments(db, function() { | |||
client.close(); | |||
}); | |||
}); | |||
}); | |||
``` | |||
### Find Documents with a Query Filter | |||
Add a query filter to find only documents which meet the query criteria. | |||
```js | |||
const findDocuments = function(db, callback) { | |||
// Get the documents collection | |||
const collection = db.collection('documents'); | |||
// Find some documents | |||
collection.find({'a': 3}).toArray(function(err, docs) { | |||
assert.equal(err, null); | |||
console.log("Found the following records"); | |||
console.log(docs); | |||
callback(docs); | |||
}); | |||
} | |||
``` | |||
Only the documents which match ``'a' : 3`` should be returned. | |||
### Update a document | |||
The following operation updates a document in the **documents** collection. | |||
```js | |||
const updateDocument = function(db, callback) { | |||
// Get the documents collection | |||
const collection = db.collection('documents'); | |||
// Update document where a is 2, set b equal to 1 | |||
collection.updateOne({ a : 2 } | |||
, { $set: { b : 1 } }, function(err, result) { | |||
assert.equal(err, null); | |||
assert.equal(1, result.result.n); | |||
console.log("Updated the document with the field a equal to 2"); | |||
callback(result); | |||
}); | |||
} | |||
``` | |||
The method updates the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Next, update the callback function from **MongoClient.connect** to include the update method. | |||
```js | |||
const MongoClient = require('mongodb').MongoClient; | |||
const assert = require('assert'); | |||
// Connection URL | |||
const url = 'mongodb://localhost:27017'; | |||
// Database Name | |||
const dbName = 'myproject'; | |||
// Use connect method to connect to the server | |||
MongoClient.connect(url, function(err, client) { | |||
assert.equal(null, err); | |||
console.log("Connected successfully to server"); | |||
const db = client.db(dbName); | |||
insertDocuments(db, function() { | |||
updateDocument(db, function() { | |||
client.close(); | |||
}); | |||
}); | |||
}); | |||
``` | |||
### Remove a document | |||
Remove the document where the field **a** is equal to **3**. | |||
```js | |||
const removeDocument = function(db, callback) { | |||
// Get the documents collection | |||
const collection = db.collection('documents'); | |||
// Delete document where a is 3 | |||
collection.deleteOne({ a : 3 }, function(err, result) { | |||
assert.equal(err, null); | |||
assert.equal(1, result.result.n); | |||
console.log("Removed the document with the field a equal to 3"); | |||
callback(result); | |||
}); | |||
} | |||
``` | |||
Add the new method to the **MongoClient.connect** callback function. | |||
```js | |||
const MongoClient = require('mongodb').MongoClient; | |||
const assert = require('assert'); | |||
// Connection URL | |||
const url = 'mongodb://localhost:27017'; | |||
// Database Name | |||
const dbName = 'myproject'; | |||
// Use connect method to connect to the server | |||
MongoClient.connect(url, function(err, client) { | |||
assert.equal(null, err); | |||
console.log("Connected successfully to server"); | |||
const db = client.db(dbName); | |||
insertDocuments(db, function() { | |||
updateDocument(db, function() { | |||
removeDocument(db, function() { | |||
client.close(); | |||
}); | |||
}); | |||
}); | |||
}); | |||
``` | |||
### Index a Collection | |||
[Indexes](https://docs.mongodb.org/manual/indexes/) can improve your application's | |||
performance. The following function creates an index on the **a** field in the | |||
**documents** collection. | |||
```js | |||
const indexCollection = function(db, callback) { | |||
db.collection('documents').createIndex( | |||
{ "a": 1 }, | |||
null, | |||
function(err, results) { | |||
console.log(results); | |||
callback(); | |||
} | |||
); | |||
}; | |||
``` | |||
Add the ``indexCollection`` method to your app: | |||
```js | |||
const MongoClient = require('mongodb').MongoClient; | |||
const assert = require('assert'); | |||
// Connection URL | |||
const url = 'mongodb://localhost:27017'; | |||
const dbName = 'myproject'; | |||
// Use connect method to connect to the server | |||
MongoClient.connect(url, function(err, client) { | |||
assert.equal(null, err); | |||
console.log("Connected successfully to server"); | |||
const db = client.db(dbName); | |||
insertDocuments(db, function() { | |||
indexCollection(db, function() { | |||
client.close(); | |||
}); | |||
}); | |||
}); | |||
``` | |||
For more detailed information, see the [tutorials](docs/reference/content/tutorials/main.md). | |||
## Next Steps | |||
* [MongoDB Documentation](http://mongodb.org) | |||
* [Read about Schemas](http://learnmongodbthehardway.com) | |||
* [Star us on GitHub](https://github.com/mongodb/node-mongodb-native) | |||
## License | |||
[Apache 2.0](LICENSE.md) | |||
© 2009-2012 Christian Amor Kvalheim | |||
© 2012-present MongoDB [Contributors](CONTRIBUTORS.md) |
@@ -0,0 +1,67 @@ | |||
'use strict'; | |||
// Core module | |||
const core = require('mongodb-core'); | |||
const Instrumentation = require('./lib/apm'); | |||
// Set up the connect function | |||
const connect = require('./lib/mongo_client').connect; | |||
// Expose error class | |||
connect.MongoError = core.MongoError; | |||
connect.MongoNetworkError = core.MongoNetworkError; | |||
// Actual driver classes exported | |||
connect.Admin = require('./lib/admin'); | |||
connect.MongoClient = require('./lib/mongo_client'); | |||
connect.Db = require('./lib/db'); | |||
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.GridStore = require('./lib/gridfs/grid_store'); | |||
connect.Chunk = require('./lib/gridfs/chunk'); | |||
connect.Logger = core.Logger; | |||
connect.AggregationCursor = require('./lib/aggregation_cursor'); | |||
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; | |||
// BSON types exported | |||
connect.Binary = core.BSON.Binary; | |||
connect.Code = core.BSON.Code; | |||
connect.Map = core.BSON.Map; | |||
connect.DBRef = core.BSON.DBRef; | |||
connect.Double = core.BSON.Double; | |||
connect.Int32 = core.BSON.Int32; | |||
connect.Long = core.BSON.Long; | |||
connect.MinKey = core.BSON.MinKey; | |||
connect.MaxKey = core.BSON.MaxKey; | |||
connect.ObjectID = core.BSON.ObjectID; | |||
connect.ObjectId = core.BSON.ObjectID; | |||
connect.Symbol = core.BSON.Symbol; | |||
connect.Timestamp = core.BSON.Timestamp; | |||
connect.BSONRegExp = core.BSON.BSONRegExp; | |||
connect.Decimal128 = core.BSON.Decimal128; | |||
// Add connect method | |||
connect.connect = connect; | |||
// Set up the instrumentation method | |||
connect.instrument = function(options, callback) { | |||
if (typeof options === 'function') { | |||
callback = options; | |||
options = {}; | |||
} | |||
const instrumentation = new Instrumentation(); | |||
instrumentation.instrument(connect.MongoClient, callback); | |||
return instrumentation; | |||
}; | |||
// Set our exports to be the connect function | |||
module.exports = connect; |
@@ -0,0 +1,293 @@ | |||
'use strict'; | |||
const executeOperation = require('./utils').executeOperation; | |||
const applyWriteConcern = require('./utils').applyWriteConcern; | |||
const addUser = require('./operations/db_ops').addUser; | |||
const executeDbAdminCommand = require('./operations/db_ops').executeDbAdminCommand; | |||
const removeUser = require('./operations/db_ops').removeUser; | |||
const replSetGetStatus = require('./operations/admin_ops').replSetGetStatus; | |||
const serverStatus = require('./operations/admin_ops').serverStatus; | |||
const validateCollection = require('./operations/admin_ops').validateCollection; | |||
/** | |||
* @fileOverview The **Admin** class is an internal class that allows convenient access to | |||
* the admin functionality and commands for MongoDB. | |||
* | |||
* **ADMIN Cannot directly be instantiated** | |||
* @example | |||
* const MongoClient = require('mongodb').MongoClient; | |||
* const test = require('assert'); | |||
* // Connection url | |||
* const url = 'mongodb://localhost:27017'; | |||
* // Database Name | |||
* const dbName = 'test'; | |||
* | |||
* // Connect using MongoClient | |||
* MongoClient.connect(url, function(err, client) { | |||
* // Use the admin database for the operation | |||
* const adminDb = client.db(dbName).admin(); | |||
* | |||
* // List all the available databases | |||
* adminDb.listDatabases(function(err, dbs) { | |||
* test.equal(null, err); | |||
* test.ok(dbs.databases.length > 0); | |||
* client.close(); | |||
* }); | |||
* }); | |||
*/ | |||
/** | |||
* Create a new Admin instance (INTERNAL TYPE, do not instantiate directly) | |||
* @class | |||
* @return {Admin} a collection instance. | |||
*/ | |||
function Admin(db, topology, promiseLibrary) { | |||
if (!(this instanceof Admin)) return new Admin(db, topology); | |||
// Internal state | |||
this.s = { | |||
db: db, | |||
topology: topology, | |||
promiseLibrary: promiseLibrary | |||
}; | |||
} | |||
/** | |||
* The callback format for results | |||
* @callback Admin~resultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {object} result The result object if the command was executed successfully. | |||
*/ | |||
/** | |||
* Execute a command | |||
* @method | |||
* @param {object} command The command hash | |||
* @param {object} [options] Optional settings. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query. | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.command = function(command, options, callback) { | |||
const args = Array.prototype.slice.call(arguments, 1); | |||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined; | |||
options = args.length ? args.shift() : {}; | |||
return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [ | |||
this.s.db, | |||
command, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Retrieve the server information for the current | |||
* instance of the db client | |||
* | |||
* @param {Object} [options] optional parameters for this operation | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.buildInfo = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
const cmd = { buildinfo: 1 }; | |||
return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [ | |||
this.s.db, | |||
cmd, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Retrieve the server information for the current | |||
* instance of the db client | |||
* | |||
* @param {Object} [options] optional parameters for this operation | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.serverInfo = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
const cmd = { buildinfo: 1 }; | |||
return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [ | |||
this.s.db, | |||
cmd, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Retrieve this db's server status. | |||
* | |||
* @param {Object} [options] optional parameters for this operation | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.serverStatus = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.db.s.topology, serverStatus, [this, options, callback]); | |||
}; | |||
/** | |||
* Ping the MongoDB server and retrieve results | |||
* | |||
* @param {Object} [options] optional parameters for this operation | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.ping = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
const cmd = { ping: 1 }; | |||
return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [ | |||
this.s.db, | |||
cmd, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Add a user to the database. | |||
* @method | |||
* @param {string} username The username. | |||
* @param {string} password The password. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.fsync=false] Specify a file sync write concern. | |||
* @param {object} [options.customData] Custom data associated with the user (only Mongodb 2.6 or higher) | |||
* @param {object[]} [options.roles] Roles associated with the created user (only Mongodb 2.6 or higher) | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.addUser = function(username, password, options, callback) { | |||
const args = Array.prototype.slice.call(arguments, 2); | |||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined; | |||
options = args.length ? args.shift() : {}; | |||
options = Object.assign({}, options); | |||
// Get the options | |||
options = applyWriteConcern(options, { db: this.s.db }); | |||
// Set the db name to admin | |||
options.dbName = 'admin'; | |||
return executeOperation(this.s.db.s.topology, addUser, [ | |||
this.s.db, | |||
username, | |||
password, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Remove a user from a database | |||
* @method | |||
* @param {string} username The username. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.fsync=false] Specify a file sync write concern. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.removeUser = function(username, options, callback) { | |||
const args = Array.prototype.slice.call(arguments, 1); | |||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined; | |||
options = args.length ? args.shift() : {}; | |||
options = Object.assign({}, options); | |||
// Get the options | |||
options = applyWriteConcern(options, { db: this.s.db }); | |||
// Set the db name | |||
options.dbName = 'admin'; | |||
return executeOperation(this.s.db.s.topology, removeUser, [ | |||
this.s.db, | |||
username, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Validate an existing collection | |||
* | |||
* @param {string} collectionName The name of the collection to validate. | |||
* @param {object} [options] Optional settings. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.validateCollection = function(collectionName, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.db.s.topology, validateCollection, [ | |||
this, | |||
collectionName, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* List the available databases | |||
* | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.nameOnly=false] Whether the command should return only db names, or names and size info. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.listDatabases = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
const cmd = { listDatabases: 1 }; | |||
if (options.nameOnly) cmd.nameOnly = Number(cmd.nameOnly); | |||
return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [ | |||
this.s.db, | |||
cmd, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Get ReplicaSet status | |||
* | |||
* @param {Object} [options] optional parameters for this operation | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Admin~resultCallback} [callback] The command result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Admin.prototype.replSetGetStatus = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.db.s.topology, replSetGetStatus, [this, options, callback]); | |||
}; | |||
module.exports = Admin; |
@@ -0,0 +1,407 @@ | |||
'use strict'; | |||
const inherits = require('util').inherits; | |||
const MongoError = require('mongodb-core').MongoError; | |||
const Readable = require('stream').Readable; | |||
const CoreCursor = require('./cursor'); | |||
/** | |||
* @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB | |||
* allowing for iteration over the results returned from the underlying query. It supports | |||
* one by one document iteration, conversion to an array or can be iterated as a Node 4.X | |||
* or higher stream | |||
* | |||
* **AGGREGATIONCURSOR Cannot directly be instantiated** | |||
* @example | |||
* const MongoClient = require('mongodb').MongoClient; | |||
* const test = require('assert'); | |||
* // Connection url | |||
* const url = 'mongodb://localhost:27017'; | |||
* // Database Name | |||
* const dbName = 'test'; | |||
* // Connect using MongoClient | |||
* MongoClient.connect(url, function(err, client) { | |||
* // Create a collection we want to drop later | |||
* const col = client.db(dbName).collection('createIndexExample1'); | |||
* // Insert a bunch of documents | |||
* col.insert([{a:1, b:1} | |||
* , {a:2, b:2}, {a:3, b:3} | |||
* , {a:4, b:4}], {w:1}, function(err, result) { | |||
* test.equal(null, err); | |||
* // Show that duplicate records got dropped | |||
* col.aggregation({}, {cursor: {}}).toArray(function(err, items) { | |||
* test.equal(null, err); | |||
* test.equal(4, items.length); | |||
* client.close(); | |||
* }); | |||
* }); | |||
* }); | |||
*/ | |||
/** | |||
* Namespace provided by the browser. | |||
* @external Readable | |||
*/ | |||
/** | |||
* Creates a new Aggregation Cursor instance (INTERNAL TYPE, do not instantiate directly) | |||
* @class AggregationCursor | |||
* @extends external:Readable | |||
* @fires AggregationCursor#data | |||
* @fires AggregationCursor#end | |||
* @fires AggregationCursor#close | |||
* @fires AggregationCursor#readable | |||
* @return {AggregationCursor} an AggregationCursor instance. | |||
*/ | |||
var AggregationCursor = function(bson, ns, cmd, options, topology, topologyOptions) { | |||
CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0)); | |||
var state = AggregationCursor.INIT; | |||
var streamOptions = {}; | |||
// MaxTimeMS | |||
var maxTimeMS = null; | |||
// Get the promiseLibrary | |||
var promiseLibrary = options.promiseLibrary || Promise; | |||
// Set up | |||
Readable.call(this, { objectMode: true }); | |||
// Internal state | |||
this.s = { | |||
// MaxTimeMS | |||
maxTimeMS: maxTimeMS, | |||
// State | |||
state: state, | |||
// Stream options | |||
streamOptions: streamOptions, | |||
// BSON | |||
bson: bson, | |||
// Namespace | |||
ns: ns, | |||
// Command | |||
cmd: cmd, | |||
// Options | |||
options: options, | |||
// Topology | |||
topology: topology, | |||
// Topology Options | |||
topologyOptions: topologyOptions, | |||
// Promise library | |||
promiseLibrary: promiseLibrary, | |||
// Optional ClientSession | |||
session: options.session | |||
}; | |||
}; | |||
/** | |||
* AggregationCursor stream data event, fired for each document in the cursor. | |||
* | |||
* @event AggregationCursor#data | |||
* @type {object} | |||
*/ | |||
/** | |||
* AggregationCursor stream end event | |||
* | |||
* @event AggregationCursor#end | |||
* @type {null} | |||
*/ | |||
/** | |||
* AggregationCursor stream close event | |||
* | |||
* @event AggregationCursor#close | |||
* @type {null} | |||
*/ | |||
/** | |||
* AggregationCursor stream readable event | |||
* | |||
* @event AggregationCursor#readable | |||
* @type {null} | |||
*/ | |||
// Inherit from Readable | |||
inherits(AggregationCursor, Readable); | |||
// Extend the Cursor | |||
for (var name in CoreCursor.prototype) { | |||
AggregationCursor.prototype[name] = CoreCursor.prototype[name]; | |||
} | |||
/** | |||
* Set the batch size for the cursor. | |||
* @method | |||
* @param {number} value The batchSize for the cursor. | |||
* @throws {MongoError} | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.batchSize = function(value) { | |||
if (this.s.state === AggregationCursor.CLOSED || this.isDead()) | |||
throw MongoError.create({ message: 'Cursor is closed', driver: true }); | |||
if (typeof value !== 'number') | |||
throw MongoError.create({ message: 'batchSize requires an integer', drvier: true }); | |||
if (this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value; | |||
this.setCursorBatchSize(value); | |||
return this; | |||
}; | |||
/** | |||
* Add a geoNear stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The geoNear stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.geoNear = function(document) { | |||
this.s.cmd.pipeline.push({ $geoNear: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a group stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The group stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.group = function(document) { | |||
this.s.cmd.pipeline.push({ $group: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a limit stage to the aggregation pipeline | |||
* @method | |||
* @param {number} value The state limit value. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.limit = function(value) { | |||
this.s.cmd.pipeline.push({ $limit: value }); | |||
return this; | |||
}; | |||
/** | |||
* Add a match stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The match stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.match = function(document) { | |||
this.s.cmd.pipeline.push({ $match: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a maxTimeMS stage to the aggregation pipeline | |||
* @method | |||
* @param {number} value The state maxTimeMS value. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.maxTimeMS = function(value) { | |||
if (this.s.topology.lastIsMaster().minWireVersion > 2) { | |||
this.s.cmd.maxTimeMS = value; | |||
} | |||
return this; | |||
}; | |||
/** | |||
* Add a out stage to the aggregation pipeline | |||
* @method | |||
* @param {number} destination The destination name. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.out = function(destination) { | |||
this.s.cmd.pipeline.push({ $out: destination }); | |||
return this; | |||
}; | |||
/** | |||
* Add a project stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The project stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.project = function(document) { | |||
this.s.cmd.pipeline.push({ $project: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a lookup stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The lookup stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.lookup = function(document) { | |||
this.s.cmd.pipeline.push({ $lookup: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a redact stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The redact stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.redact = function(document) { | |||
this.s.cmd.pipeline.push({ $redact: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a skip stage to the aggregation pipeline | |||
* @method | |||
* @param {number} value The state skip value. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.skip = function(value) { | |||
this.s.cmd.pipeline.push({ $skip: value }); | |||
return this; | |||
}; | |||
/** | |||
* Add a sort stage to the aggregation pipeline | |||
* @method | |||
* @param {object} document The sort stage document. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.sort = function(document) { | |||
this.s.cmd.pipeline.push({ $sort: document }); | |||
return this; | |||
}; | |||
/** | |||
* Add a unwind stage to the aggregation pipeline | |||
* @method | |||
* @param {number} field The unwind field name. | |||
* @return {AggregationCursor} | |||
*/ | |||
AggregationCursor.prototype.unwind = function(field) { | |||
this.s.cmd.pipeline.push({ $unwind: field }); | |||
return this; | |||
}; | |||
/** | |||
* Return the cursor logger | |||
* @method | |||
* @return {Logger} return the cursor logger | |||
* @ignore | |||
*/ | |||
AggregationCursor.prototype.getLogger = function() { | |||
return this.logger; | |||
}; | |||
AggregationCursor.prototype.get = AggregationCursor.prototype.toArray; | |||
/** | |||
* Get the next available document from the cursor, returns null if no more documents are available. | |||
* @function AggregationCursor.prototype.next | |||
* @param {AggregationCursor~resultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* Check if there is any document still available in the cursor | |||
* @function AggregationCursor.prototype.hasNext | |||
* @param {AggregationCursor~resultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* The callback format for results | |||
* @callback AggregationCursor~toArrayResultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {object[]} documents All the documents the satisfy the cursor. | |||
*/ | |||
/** | |||
* Returns an array of documents. The caller is responsible for making sure that there | |||
* is enough memory to store the results. Note that the array only contain partial | |||
* results when this cursor had been previouly accessed. In that case, | |||
* cursor.rewind() can be used to reset the cursor. | |||
* @method AggregationCursor.prototype.toArray | |||
* @param {AggregationCursor~toArrayResultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* The callback format for results | |||
* @callback AggregationCursor~resultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {(object|null)} result The result object if the command was executed successfully. | |||
*/ | |||
/** | |||
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**, | |||
* not all of the elements will be iterated if this cursor had been previouly accessed. | |||
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike | |||
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements | |||
* at any given time if batch size is specified. Otherwise, the caller is responsible | |||
* for making sure that the entire result can fit the memory. | |||
* @method AggregationCursor.prototype.each | |||
* @param {AggregationCursor~resultCallback} callback The result callback. | |||
* @throws {MongoError} | |||
* @return {null} | |||
*/ | |||
/** | |||
* Close the cursor, sending a AggregationCursor command and emitting close. | |||
* @method AggregationCursor.prototype.close | |||
* @param {AggregationCursor~resultCallback} [callback] The result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* Is the cursor closed | |||
* @method AggregationCursor.prototype.isClosed | |||
* @return {boolean} | |||
*/ | |||
/** | |||
* Execute the explain for the cursor | |||
* @method AggregationCursor.prototype.explain | |||
* @param {AggregationCursor~resultCallback} [callback] The result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* Clone the cursor | |||
* @function AggregationCursor.prototype.clone | |||
* @return {AggregationCursor} | |||
*/ | |||
/** | |||
* Resets the cursor | |||
* @function AggregationCursor.prototype.rewind | |||
* @return {AggregationCursor} | |||
*/ | |||
/** | |||
* The callback format for the forEach iterator method | |||
* @callback AggregationCursor~iteratorCallback | |||
* @param {Object} doc An emitted document for the iterator | |||
*/ | |||
/** | |||
* The callback error format for the forEach iterator method | |||
* @callback AggregationCursor~endCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
*/ | |||
/* | |||
* Iterates over all the documents for this cursor using the iterator, callback pattern. | |||
* @method AggregationCursor.prototype.forEach | |||
* @param {AggregationCursor~iteratorCallback} iterator The iteration callback. | |||
* @param {AggregationCursor~endCallback} callback The end callback. | |||
* @throws {MongoError} | |||
* @return {null} | |||
*/ | |||
AggregationCursor.INIT = 0; | |||
AggregationCursor.OPEN = 1; | |||
AggregationCursor.CLOSED = 2; | |||
module.exports = AggregationCursor; |
@@ -0,0 +1,31 @@ | |||
'use strict'; | |||
const EventEmitter = require('events').EventEmitter; | |||
class Instrumentation extends EventEmitter { | |||
constructor() { | |||
super(); | |||
} | |||
instrument(MongoClient, callback) { | |||
// store a reference to the original functions | |||
this.$MongoClient = MongoClient; | |||
const $prototypeConnect = (this.$prototypeConnect = MongoClient.prototype.connect); | |||
const instrumentation = this; | |||
MongoClient.prototype.connect = function(callback) { | |||
this.s.options.monitorCommands = true; | |||
this.on('commandStarted', event => instrumentation.emit('started', event)); | |||
this.on('commandSucceeded', event => instrumentation.emit('succeeded', event)); | |||
this.on('commandFailed', event => instrumentation.emit('failed', event)); | |||
return $prototypeConnect.call(this, callback); | |||
}; | |||
if (typeof callback === 'function') callback(null, this); | |||
} | |||
uninstrument() { | |||
this.$MongoClient.prototype.connect = this.$prototypeConnect; | |||
} | |||
} | |||
module.exports = Instrumentation; |
@@ -0,0 +1,136 @@ | |||
'use strict'; | |||
var shallowClone = require('./utils').shallowClone, | |||
handleCallback = require('./utils').handleCallback, | |||
MongoError = require('mongodb-core').MongoError, | |||
f = require('util').format; | |||
var authenticate = function(client, username, password, options, callback) { | |||
// Did the user destroy the topology | |||
if (client.topology && client.topology.isDestroyed()) | |||
return callback(new MongoError('topology was destroyed')); | |||
// the default db to authenticate against is 'self' | |||
// if authententicate is called from a retry context, it may be another one, like admin | |||
var authdb = options.dbName; | |||
authdb = options.authdb ? options.authdb : authdb; | |||
authdb = options.authSource ? options.authSource : authdb; | |||
// Callback | |||
var _callback = function(err, result) { | |||
if (client.listeners('authenticated').length > 0) { | |||
client.emit('authenticated', err, result); | |||
} | |||
// Return to caller | |||
handleCallback(callback, err, result); | |||
}; | |||
// authMechanism | |||
var authMechanism = options.authMechanism || ''; | |||
authMechanism = authMechanism.toUpperCase(); | |||
// If classic auth delegate to auth command | |||
if (authMechanism === 'MONGODB-CR') { | |||
client.topology.auth('mongocr', authdb, username, password, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else if (authMechanism === 'PLAIN') { | |||
client.topology.auth('plain', authdb, username, password, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else if (authMechanism === 'MONGODB-X509') { | |||
client.topology.auth('x509', authdb, username, password, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else if (authMechanism === 'SCRAM-SHA-1') { | |||
client.topology.auth('scram-sha-1', authdb, username, password, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else if (authMechanism === 'SCRAM-SHA-256') { | |||
client.topology.auth('scram-sha-256', authdb, username, password, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else if (authMechanism === 'GSSAPI') { | |||
if (process.platform === 'win32') { | |||
client.topology.auth('sspi', authdb, username, password, options, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else { | |||
client.topology.auth('gssapi', authdb, username, password, options, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} | |||
} else if (authMechanism === 'DEFAULT') { | |||
client.topology.auth('default', authdb, username, password, function(err) { | |||
if (err) return handleCallback(callback, err, false); | |||
_callback(null, true); | |||
}); | |||
} else { | |||
handleCallback( | |||
callback, | |||
MongoError.create({ | |||
message: f('authentication mechanism %s not supported', options.authMechanism), | |||
driver: true | |||
}) | |||
); | |||
} | |||
}; | |||
module.exports = function(self, username, password, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// Shallow copy the options | |||
options = shallowClone(options); | |||
// Set default mechanism | |||
if (!options.authMechanism) { | |||
options.authMechanism = 'DEFAULT'; | |||
} else if ( | |||
options.authMechanism !== 'GSSAPI' && | |||
options.authMechanism !== 'DEFAULT' && | |||
options.authMechanism !== 'MONGODB-CR' && | |||
options.authMechanism !== 'MONGODB-X509' && | |||
options.authMechanism !== 'SCRAM-SHA-1' && | |||
options.authMechanism !== 'SCRAM-SHA-256' && | |||
options.authMechanism !== 'PLAIN' | |||
) { | |||
return handleCallback( | |||
callback, | |||
MongoError.create({ | |||
message: | |||
'only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1 is supported by authMechanism', | |||
driver: true | |||
}) | |||
); | |||
} | |||
// If we have a callback fallback | |||
if (typeof callback === 'function') | |||
return authenticate(self, username, password, options, function(err, r) { | |||
// Support failed auth method | |||
if (err && err.message && err.message.indexOf('saslStart') !== -1) err.code = 59; | |||
// Reject error | |||
if (err) return callback(err, r); | |||
callback(null, r); | |||
}); | |||
// Return a promise | |||
return new self.s.promiseLibrary(function(resolve, reject) { | |||
authenticate(self, username, password, options, function(err, r) { | |||
// Support failed auth method | |||
if (err && err.message && err.message.indexOf('saslStart') !== -1) err.code = 59; | |||
// Reject error | |||
if (err) return reject(err); | |||
resolve(r); | |||
}); | |||
}); | |||
}; |
@@ -0,0 +1,182 @@ | |||
'use strict'; | |||
const common = require('./common'); | |||
const BulkOperationBase = common.BulkOperationBase; | |||
const utils = require('../utils'); | |||
const toError = utils.toError; | |||
const handleCallback = utils.handleCallback; | |||
const BulkWriteResult = common.BulkWriteResult; | |||
const Batch = common.Batch; | |||
const mergeBatchResults = common.mergeBatchResults; | |||
const executeOperation = utils.executeOperation; | |||
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError; | |||
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError; | |||
const bson = common.bson; | |||
const isPromiseLike = require('../utils').isPromiseLike; | |||
/** | |||
* Add to internal list of Operations | |||
* | |||
* @param {OrderedBulkOperation} bulkOperation | |||
* @param {number} docType number indicating the document type | |||
* @param {object} document | |||
* @return {OrderedBulkOperation} | |||
*/ | |||
function addToOperationsList(bulkOperation, docType, document) { | |||
// Get the bsonSize | |||
const bsonSize = bson.calculateObjectSize(document, { | |||
checkKeys: false | |||
}); | |||
// Throw error if the doc is bigger than the max BSON size | |||
if (bsonSize >= bulkOperation.s.maxBatchSizeBytes) | |||
throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes); | |||
// Create a new batch object if we don't have a current one | |||
if (bulkOperation.s.currentBatch == null) | |||
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex); | |||
const maxKeySize = bulkOperation.s.maxKeySize; | |||
// Check if we need to create a new batch | |||
if ( | |||
bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize || | |||
bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >= | |||
bulkOperation.s.maxBatchSizeBytes || | |||
bulkOperation.s.currentBatch.batchType !== docType | |||
) { | |||
// Save the batch to the execution stack | |||
bulkOperation.s.batches.push(bulkOperation.s.currentBatch); | |||
// Create a new batch | |||
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex); | |||
// Reset the current size trackers | |||
bulkOperation.s.currentBatchSize = 0; | |||
bulkOperation.s.currentBatchSizeBytes = 0; | |||
} | |||
if (docType === common.INSERT) { | |||
bulkOperation.s.bulkResult.insertedIds.push({ | |||
index: bulkOperation.s.currentIndex, | |||
_id: document._id | |||
}); | |||
} | |||
// We have an array of documents | |||
if (Array.isArray(document)) { | |||
throw toError('operation passed in cannot be an Array'); | |||
} | |||
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex); | |||
bulkOperation.s.currentBatch.operations.push(document); | |||
bulkOperation.s.currentBatchSize += 1; | |||
bulkOperation.s.currentBatchSizeBytes += maxKeySize + bsonSize; | |||
bulkOperation.s.currentIndex += 1; | |||
// Return bulkOperation | |||
return bulkOperation; | |||
} | |||
/** | |||
* Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly) | |||
* @class | |||
* @property {number} length Get the number of operations in the bulk. | |||
* @return {OrderedBulkOperation} a OrderedBulkOperation instance. | |||
*/ | |||
class OrderedBulkOperation extends BulkOperationBase { | |||
constructor(topology, collection, options) { | |||
options = options || {}; | |||
options = Object.assign(options, { addToOperationsList }); | |||
super(topology, collection, options, true); | |||
} | |||
/** | |||
* The callback format for results | |||
* @callback OrderedBulkOperation~resultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {BulkWriteResult} result The bulk write result. | |||
*/ | |||
/** | |||
* Execute the ordered bulk operation | |||
* | |||
* @method | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.fsync=false] Specify a file sync write concern. | |||
* @param {OrderedBulkOperation~resultCallback} [callback] The result callback | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
execute(_writeConcern, options, callback) { | |||
const ret = this.bulkExecute(_writeConcern, options, callback); | |||
if (isPromiseLike(ret)) { | |||
return ret; | |||
} | |||
options = ret.options; | |||
callback = ret.callback; | |||
return executeOperation(this.s.topology, executeCommands, [this, options, callback]); | |||
} | |||
} | |||
/** | |||
* Execute next write command in a chain | |||
* | |||
* @param {OrderedBulkOperation} bulkOperation | |||
* @param {object} options | |||
* @param {function} callback | |||
*/ | |||
function executeCommands(bulkOperation, options, callback) { | |||
if (bulkOperation.s.batches.length === 0) { | |||
return handleCallback(callback, null, new BulkWriteResult(bulkOperation.s.bulkResult)); | |||
} | |||
// Ordered execution of the command | |||
const batch = bulkOperation.s.batches.shift(); | |||
function resultHandler(err, result) { | |||
// Error is a driver related error not a bulk op error, terminate | |||
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) { | |||
return handleCallback(callback, err); | |||
} | |||
// If we have and error | |||
if (err) err.ok = 0; | |||
if (err instanceof MongoWriteConcernError) { | |||
return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, true, err, callback); | |||
} | |||
// Merge the results together | |||
const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult); | |||
const mergeResult = mergeBatchResults(true, batch, bulkOperation.s.bulkResult, err, result); | |||
if (mergeResult != null) { | |||
return handleCallback(callback, null, writeResult); | |||
} | |||
if (bulkOperation.handleWriteError(callback, writeResult)) return; | |||
// Execute the next command in line | |||
executeCommands(bulkOperation, options, callback); | |||
} | |||
bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback); | |||
} | |||
/** | |||
* Returns an unordered batch object | |||
* @ignore | |||
*/ | |||
function initializeOrderedBulkOp(topology, collection, options) { | |||
return new OrderedBulkOperation(topology, collection, options); | |||
} | |||
initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation; | |||
module.exports = initializeOrderedBulkOp; | |||
module.exports.Bulk = OrderedBulkOperation; |
@@ -0,0 +1,219 @@ | |||
'use strict'; | |||
const common = require('./common'); | |||
const BulkOperationBase = common.BulkOperationBase; | |||
const utils = require('../utils'); | |||
const toError = utils.toError; | |||
const handleCallback = utils.handleCallback; | |||
const BulkWriteResult = common.BulkWriteResult; | |||
const Batch = common.Batch; | |||
const mergeBatchResults = common.mergeBatchResults; | |||
const executeOperation = utils.executeOperation; | |||
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError; | |||
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError; | |||
const bson = common.bson; | |||
const isPromiseLike = require('../utils').isPromiseLike; | |||
/** | |||
* Add to internal list of Operations | |||
* | |||
* @param {UnorderedBulkOperation} bulkOperation | |||
* @param {number} docType number indicating the document type | |||
* @param {object} document | |||
* @return {UnorderedBulkOperation} | |||
*/ | |||
function addToOperationsList(bulkOperation, docType, document) { | |||
// Get the bsonSize | |||
const bsonSize = bson.calculateObjectSize(document, { | |||
checkKeys: false | |||
}); | |||
// Throw error if the doc is bigger than the max BSON size | |||
if (bsonSize >= bulkOperation.s.maxBatchSizeBytes) | |||
throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes); | |||
// Holds the current batch | |||
bulkOperation.s.currentBatch = null; | |||
// Get the right type of batch | |||
if (docType === common.INSERT) { | |||
bulkOperation.s.currentBatch = bulkOperation.s.currentInsertBatch; | |||
} else if (docType === common.UPDATE) { | |||
bulkOperation.s.currentBatch = bulkOperation.s.currentUpdateBatch; | |||
} else if (docType === common.REMOVE) { | |||
bulkOperation.s.currentBatch = bulkOperation.s.currentRemoveBatch; | |||
} | |||
const maxKeySize = bulkOperation.s.maxKeySize; | |||
// Create a new batch object if we don't have a current one | |||
if (bulkOperation.s.currentBatch == null) | |||
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex); | |||
// Check if we need to create a new batch | |||
if ( | |||
bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize || | |||
bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >= | |||
bulkOperation.s.maxBatchSizeBytes || | |||
bulkOperation.s.currentBatch.batchType !== docType | |||
) { | |||
// Save the batch to the execution stack | |||
bulkOperation.s.batches.push(bulkOperation.s.currentBatch); | |||
// Create a new batch | |||
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex); | |||
} | |||
// We have an array of documents | |||
if (Array.isArray(document)) { | |||
throw toError('operation passed in cannot be an Array'); | |||
} | |||
bulkOperation.s.currentBatch.operations.push(document); | |||
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex); | |||
bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1; | |||
// Save back the current Batch to the right type | |||
if (docType === common.INSERT) { | |||
bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch; | |||
bulkOperation.s.bulkResult.insertedIds.push({ | |||
index: bulkOperation.s.bulkResult.insertedIds.length, | |||
_id: document._id | |||
}); | |||
} else if (docType === common.UPDATE) { | |||
bulkOperation.s.currentUpdateBatch = bulkOperation.s.currentBatch; | |||
} else if (docType === common.REMOVE) { | |||
bulkOperation.s.currentRemoveBatch = bulkOperation.s.currentBatch; | |||
} | |||
// Update current batch size | |||
bulkOperation.s.currentBatch.size += 1; | |||
bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize; | |||
// Return bulkOperation | |||
return bulkOperation; | |||
} | |||
/** | |||
* Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly) | |||
* @class | |||
* @property {number} length Get the number of operations in the bulk. | |||
* @return {UnorderedBulkOperation} a UnorderedBulkOperation instance. | |||
*/ | |||
class UnorderedBulkOperation extends BulkOperationBase { | |||
constructor(topology, collection, options) { | |||
options = options || {}; | |||
options = Object.assign(options, { addToOperationsList }); | |||
super(topology, collection, options, false); | |||
} | |||
/** | |||
* The callback format for results | |||
* @callback UnorderedBulkOperation~resultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {BulkWriteResult} result The bulk write result. | |||
*/ | |||
/** | |||
* Execute the ordered bulk operation | |||
* | |||
* @method | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.fsync=false] Specify a file sync write concern. | |||
* @param {UnorderedBulkOperation~resultCallback} [callback] The result callback | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
execute(_writeConcern, options, callback) { | |||
const ret = this.bulkExecute(_writeConcern, options, callback); | |||
if (isPromiseLike(ret)) { | |||
return ret; | |||
} | |||
options = ret.options; | |||
callback = ret.callback; | |||
return executeOperation(this.s.topology, executeBatches, [this, options, callback]); | |||
} | |||
} | |||
/** | |||
* Execute the command | |||
* | |||
* @param {UnorderedBulkOperation} bulkOperation | |||
* @param {object} batch | |||
* @param {object} options | |||
* @param {function} callback | |||
*/ | |||
function executeBatch(bulkOperation, batch, options, callback) { | |||
function resultHandler(err, result) { | |||
// Error is a driver related error not a bulk op error, terminate | |||
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) { | |||
return handleCallback(callback, err); | |||
} | |||
// If we have and error | |||
if (err) err.ok = 0; | |||
if (err instanceof MongoWriteConcernError) { | |||
return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, false, err, callback); | |||
} | |||
handleCallback( | |||
callback, | |||
null, | |||
mergeBatchResults(false, batch, bulkOperation.s.bulkResult, err, result) | |||
); | |||
} | |||
bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback); | |||
} | |||
/** | |||
* Execute all the commands | |||
* | |||
* @param {UnorderedBulkOperation} bulkOperation | |||
* @param {object} options | |||
* @param {function} callback | |||
*/ | |||
function executeBatches(bulkOperation, options, callback) { | |||
let numberOfCommandsToExecute = bulkOperation.s.batches.length; | |||
let hasErrored = false; | |||
// Execute over all the batches | |||
for (let i = 0; i < bulkOperation.s.batches.length; i++) { | |||
executeBatch(bulkOperation, bulkOperation.s.batches[i], options, function(err) { | |||
if (hasErrored) { | |||
return; | |||
} | |||
if (err) { | |||
hasErrored = true; | |||
return handleCallback(callback, err); | |||
} | |||
// Count down the number of commands left to execute | |||
numberOfCommandsToExecute = numberOfCommandsToExecute - 1; | |||
// Execute | |||
if (numberOfCommandsToExecute === 0) { | |||
// Driver level error | |||
if (err) return handleCallback(callback, err); | |||
const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult); | |||
if (bulkOperation.handleWriteError(callback, writeResult)) return; | |||
return handleCallback(callback, null, writeResult); | |||
} | |||
}); | |||
} | |||
} | |||
/** | |||
* Returns an unordered batch object | |||
* @ignore | |||
*/ | |||
function initializeUnorderedBulkOp(topology, collection, options) { | |||
return new UnorderedBulkOperation(topology, collection, options); | |||
} | |||
initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation; | |||
module.exports = initializeUnorderedBulkOp; | |||
module.exports.Bulk = UnorderedBulkOperation; |
@@ -0,0 +1,469 @@ | |||
'use strict'; | |||
const EventEmitter = require('events'); | |||
const isResumableError = require('./error').isResumableError; | |||
const MongoError = require('mongodb-core').MongoError; | |||
var cursorOptionNames = ['maxAwaitTimeMS', 'collation', 'readPreference']; | |||
const CHANGE_DOMAIN_TYPES = { | |||
COLLECTION: Symbol('Collection'), | |||
DATABASE: Symbol('Database'), | |||
CLUSTER: Symbol('Cluster') | |||
}; | |||
/** | |||
* Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}. | |||
* @class ChangeStream | |||
* @since 3.0.0 | |||
* @param {(MongoClient|Db|Collection)} changeDomain The domain against which to create the change stream | |||
* @param {Array} pipeline An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents | |||
* @param {object} [options] Optional settings | |||
* @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. | |||
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query | |||
* @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. | |||
* @param {number} [options.batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}. | |||
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}. | |||
* @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}. | |||
* @fires ChangeStream#close | |||
* @fires ChangeStream#change | |||
* @fires ChangeStream#end | |||
* @fires ChangeStream#error | |||
* @return {ChangeStream} a ChangeStream instance. | |||
*/ | |||
class ChangeStream extends EventEmitter { | |||
constructor(changeDomain, pipeline, options) { | |||
super(); | |||
const Collection = require('./collection'); | |||
const Db = require('./db'); | |||
const MongoClient = require('./mongo_client'); | |||
this.pipeline = pipeline || []; | |||
this.options = options || {}; | |||
this.cursorNamespace = undefined; | |||
this.namespace = {}; | |||
if (changeDomain instanceof Collection) { | |||
this.type = CHANGE_DOMAIN_TYPES.COLLECTION; | |||
this.topology = changeDomain.s.db.serverConfig; | |||
this.namespace = { | |||
collection: changeDomain.collectionName, | |||
database: changeDomain.s.db.databaseName | |||
}; | |||
this.cursorNamespace = `${this.namespace.database}.${this.namespace.collection}`; | |||
} else if (changeDomain instanceof Db) { | |||
this.type = CHANGE_DOMAIN_TYPES.DATABASE; | |||
this.namespace = { collection: '', database: changeDomain.databaseName }; | |||
this.cursorNamespace = this.namespace.database; | |||
this.topology = changeDomain.serverConfig; | |||
} else if (changeDomain instanceof MongoClient) { | |||
this.type = CHANGE_DOMAIN_TYPES.CLUSTER; | |||
this.namespace = { collection: '', database: 'admin' }; | |||
this.cursorNamespace = this.namespace.database; | |||
this.topology = changeDomain.topology; | |||
} else { | |||
throw new TypeError( | |||
'changeDomain provided to ChangeStream constructor is not an instance of Collection, Db, or MongoClient' | |||
); | |||
} | |||
this.promiseLibrary = changeDomain.s.promiseLibrary; | |||
if (!this.options.readPreference && changeDomain.s.readPreference) { | |||
this.options.readPreference = changeDomain.s.readPreference; | |||
} | |||
// We need to get the operationTime as early as possible | |||
const isMaster = this.topology.lastIsMaster(); | |||
if (!isMaster) { | |||
throw new MongoError('Topology does not have an ismaster yet.'); | |||
} | |||
this.operationTime = isMaster.operationTime; | |||
// Create contained Change Stream cursor | |||
this.cursor = createChangeStreamCursor(this); | |||
// Listen for any `change` listeners being added to ChangeStream | |||
this.on('newListener', eventName => { | |||
if (eventName === 'change' && this.cursor && this.listenerCount('change') === 0) { | |||
this.cursor.on('data', change => | |||
processNewChange({ changeStream: this, change, eventEmitter: true }) | |||
); | |||
} | |||
}); | |||
// Listen for all `change` listeners being removed from ChangeStream | |||
this.on('removeListener', eventName => { | |||
if (eventName === 'change' && this.listenerCount('change') === 0 && this.cursor) { | |||
this.cursor.removeAllListeners('data'); | |||
} | |||
}); | |||
} | |||
/** | |||
* Check if there is any document still available in the Change Stream | |||
* @function ChangeStream.prototype.hasNext | |||
* @param {ChangeStream~resultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
hasNext(callback) { | |||
return this.cursor.hasNext(callback); | |||
} | |||
/** | |||
* Get the next available document from the Change Stream, returns null if no more documents are available. | |||
* @function ChangeStream.prototype.next | |||
* @param {ChangeStream~resultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
next(callback) { | |||
var self = this; | |||
if (this.isClosed()) { | |||
if (callback) return callback(new Error('Change Stream is not open.'), null); | |||
return self.promiseLibrary.reject(new Error('Change Stream is not open.')); | |||
} | |||
return this.cursor | |||
.next() | |||
.then(change => processNewChange({ changeStream: self, change, callback })) | |||
.catch(error => processNewChange({ changeStream: self, error, callback })); | |||
} | |||
/** | |||
* Is the cursor closed | |||
* @method ChangeStream.prototype.isClosed | |||
* @return {boolean} | |||
*/ | |||
isClosed() { | |||
if (this.cursor) { | |||
return this.cursor.isClosed(); | |||
} | |||
return true; | |||
} | |||
/** | |||
* Close the Change Stream | |||
* @method ChangeStream.prototype.close | |||
* @param {ChangeStream~resultCallback} [callback] The result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
close(callback) { | |||
if (!this.cursor) { | |||
if (callback) return callback(); | |||
return this.promiseLibrary.resolve(); | |||
} | |||
// Tidy up the existing cursor | |||
var cursor = this.cursor; | |||
delete this.cursor; | |||
return cursor.close(callback); | |||
} | |||
/** | |||
* This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream. | |||
* @method | |||
* @param {Writable} destination The destination for writing data | |||
* @param {object} [options] {@link https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options|Pipe options} | |||
* @return {null} | |||
*/ | |||
pipe(destination, options) { | |||
if (!this.pipeDestinations) { | |||
this.pipeDestinations = []; | |||
} | |||
this.pipeDestinations.push(destination); | |||
return this.cursor.pipe(destination, options); | |||
} | |||
/** | |||
* This method will remove the hooks set up for a previous pipe() call. | |||
* @param {Writable} [destination] The destination for writing data | |||
* @return {null} | |||
*/ | |||
unpipe(destination) { | |||
if (this.pipeDestinations && this.pipeDestinations.indexOf(destination) > -1) { | |||
this.pipeDestinations.splice(this.pipeDestinations.indexOf(destination), 1); | |||
} | |||
return this.cursor.unpipe(destination); | |||
} | |||
/** | |||
* Return a modified Readable stream including a possible transform method. | |||
* @method | |||
* @param {object} [options] Optional settings. | |||
* @param {function} [options.transform] A transformation method applied to each document emitted by the stream. | |||
* @return {Cursor} | |||
*/ | |||
stream(options) { | |||
this.streamOptions = options; | |||
return this.cursor.stream(options); | |||
} | |||
/** | |||
* This method will cause a stream in flowing mode to stop emitting data events. Any data that becomes available will remain in the internal buffer. | |||
* @return {null} | |||
*/ | |||
pause() { | |||
return this.cursor.pause(); | |||
} | |||
/** | |||
* This method will cause the readable stream to resume emitting data events. | |||
* @return {null} | |||
*/ | |||
resume() { | |||
return this.cursor.resume(); | |||
} | |||
} | |||
// Create a new change stream cursor based on self's configuration | |||
var createChangeStreamCursor = function(self) { | |||
if (self.resumeToken) { | |||
self.options.resumeAfter = self.resumeToken; | |||
} | |||
var changeStreamCursor = buildChangeStreamAggregationCommand(self); | |||
/** | |||
* Fired for each new matching change in the specified namespace. Attaching a `change` | |||
* event listener to a Change Stream will switch the stream into flowing mode. Data will | |||
* then be passed as soon as it is available. | |||
* | |||
* @event ChangeStream#change | |||
* @type {object} | |||
*/ | |||
if (self.listenerCount('change') > 0) { | |||
changeStreamCursor.on('data', function(change) { | |||
processNewChange({ changeStream: self, change, eventEmitter: true }); | |||
}); | |||
} | |||
/** | |||
* Change stream close event | |||
* | |||
* @event ChangeStream#close | |||
* @type {null} | |||
*/ | |||
changeStreamCursor.on('close', function() { | |||
self.emit('close'); | |||
}); | |||
/** | |||
* Change stream end event | |||
* | |||
* @event ChangeStream#end | |||
* @type {null} | |||
*/ | |||
changeStreamCursor.on('end', function() { | |||
self.emit('end'); | |||
}); | |||
/** | |||
* Fired when the stream encounters an error. | |||
* | |||
* @event ChangeStream#error | |||
* @type {Error} | |||
*/ | |||
changeStreamCursor.on('error', function(error) { | |||
processNewChange({ changeStream: self, error, eventEmitter: true }); | |||
}); | |||
if (self.pipeDestinations) { | |||
const cursorStream = changeStreamCursor.stream(self.streamOptions); | |||
for (let pipeDestination in self.pipeDestinations) { | |||
cursorStream.pipe(pipeDestination); | |||
} | |||
} | |||
return changeStreamCursor; | |||
}; | |||
function getResumeToken(self) { | |||
return self.resumeToken || self.options.resumeAfter; | |||
} | |||
function getStartAtOperationTime(self) { | |||
const isMaster = self.topology.lastIsMaster() || {}; | |||
return ( | |||
isMaster.maxWireVersion && isMaster.maxWireVersion >= 7 && self.options.startAtOperationTime | |||
); | |||
} | |||
var buildChangeStreamAggregationCommand = function(self) { | |||
const topology = self.topology; | |||
const namespace = self.namespace; | |||
const pipeline = self.pipeline; | |||
const options = self.options; | |||
const cursorNamespace = self.cursorNamespace; | |||
var changeStreamStageOptions = { | |||
fullDocument: options.fullDocument || 'default' | |||
}; | |||
const resumeToken = getResumeToken(self); | |||
const startAtOperationTime = getStartAtOperationTime(self); | |||
if (resumeToken) { | |||
changeStreamStageOptions.resumeAfter = resumeToken; | |||
} | |||
if (startAtOperationTime) { | |||
changeStreamStageOptions.startAtOperationTime = startAtOperationTime; | |||
} | |||
// Map cursor options | |||
var cursorOptions = {}; | |||
cursorOptionNames.forEach(function(optionName) { | |||
if (options[optionName]) { | |||
cursorOptions[optionName] = options[optionName]; | |||
} | |||
}); | |||
if (self.type === CHANGE_DOMAIN_TYPES.CLUSTER) { | |||
changeStreamStageOptions.allChangesForCluster = true; | |||
} | |||
var changeStreamPipeline = [{ $changeStream: changeStreamStageOptions }]; | |||
changeStreamPipeline = changeStreamPipeline.concat(pipeline); | |||
var command = { | |||
aggregate: self.type === CHANGE_DOMAIN_TYPES.COLLECTION ? namespace.collection : 1, | |||
pipeline: changeStreamPipeline, | |||
readConcern: { level: 'majority' }, | |||
cursor: { | |||
batchSize: options.batchSize || 1 | |||
} | |||
}; | |||
// Create and return the cursor | |||
return topology.cursor(cursorNamespace, command, cursorOptions); | |||
}; | |||
// This method performs a basic server selection loop, satisfying the requirements of | |||
// ChangeStream resumability until the new SDAM layer can be used. | |||
const SELECTION_TIMEOUT = 30000; | |||
function waitForTopologyConnected(topology, options, callback) { | |||
setTimeout(() => { | |||
if (options && options.start == null) options.start = process.hrtime(); | |||
const start = options.start || process.hrtime(); | |||
const timeout = options.timeout || SELECTION_TIMEOUT; | |||
const readPreference = options.readPreference; | |||
if (topology.isConnected({ readPreference })) return callback(null, null); | |||
const hrElapsed = process.hrtime(start); | |||
const elapsed = (hrElapsed[0] * 1e9 + hrElapsed[1]) / 1e6; | |||
if (elapsed > timeout) return callback(new MongoError('Timed out waiting for connection')); | |||
waitForTopologyConnected(topology, options, callback); | |||
}, 3000); // this is an arbitrary wait time to allow SDAM to transition | |||
} | |||
// Handle new change events. This method brings together the routes from the callback, event emitter, and promise ways of using ChangeStream. | |||
function processNewChange(args) { | |||
const changeStream = args.changeStream; | |||
const error = args.error; | |||
const change = args.change; | |||
const callback = args.callback; | |||
const eventEmitter = args.eventEmitter || false; | |||
// If the changeStream is closed, then it should not process a change. | |||
if (changeStream.isClosed()) { | |||
// We do not error in the eventEmitter case. | |||
if (eventEmitter) { | |||
return; | |||
} | |||
const error = new MongoError('ChangeStream is closed'); | |||
return typeof callback === 'function' | |||
? callback(error, null) | |||
: changeStream.promiseLibrary.reject(error); | |||
} | |||
const topology = changeStream.topology; | |||
const options = changeStream.cursor.options; | |||
if (error) { | |||
if (isResumableError(error) && !changeStream.attemptingResume) { | |||
changeStream.attemptingResume = true; | |||
if (!(getResumeToken(changeStream) || getStartAtOperationTime(changeStream))) { | |||
const startAtOperationTime = changeStream.cursor.cursorState.operationTime; | |||
changeStream.options = Object.assign({ startAtOperationTime }, changeStream.options); | |||
} | |||
// stop listening to all events from old cursor | |||
['data', 'close', 'end', 'error'].forEach(event => | |||
changeStream.cursor.removeAllListeners(event) | |||
); | |||
// close internal cursor, ignore errors | |||
changeStream.cursor.close(); | |||
// attempt recreating the cursor | |||
if (eventEmitter) { | |||
waitForTopologyConnected(topology, { readPreference: options.readPreference }, err => { | |||
if (err) return changeStream.emit('error', err); | |||
changeStream.cursor = createChangeStreamCursor(changeStream); | |||
}); | |||
return; | |||
} | |||
if (callback) { | |||
waitForTopologyConnected(topology, { readPreference: options.readPreference }, err => { | |||
if (err) return callback(err, null); | |||
changeStream.cursor = createChangeStreamCursor(changeStream); | |||
changeStream.next(callback); | |||
}); | |||
return; | |||
} | |||
return new Promise((resolve, reject) => { | |||
waitForTopologyConnected(topology, { readPreference: options.readPreference }, err => { | |||
if (err) return reject(err); | |||
resolve(); | |||
}); | |||
}) | |||
.then(() => (changeStream.cursor = createChangeStreamCursor(changeStream))) | |||
.then(() => changeStream.next()); | |||
} | |||
if (eventEmitter) return changeStream.emit('error', error); | |||
if (typeof callback === 'function') return callback(error, null); | |||
return changeStream.promiseLibrary.reject(error); | |||
} | |||
changeStream.attemptingResume = false; | |||
// Cache the resume token if it is present. If it is not present return an error. | |||
if (!change || !change._id) { | |||
var noResumeTokenError = new Error( | |||
'A change stream document has been received that lacks a resume token (_id).' | |||
); | |||
if (eventEmitter) return changeStream.emit('error', noResumeTokenError); | |||
if (typeof callback === 'function') return callback(noResumeTokenError, null); | |||
return changeStream.promiseLibrary.reject(noResumeTokenError); | |||
} | |||
changeStream.resumeToken = change._id; | |||
// Return the change | |||
if (eventEmitter) return changeStream.emit('change', change); | |||
if (typeof callback === 'function') return callback(error, change); | |||
return changeStream.promiseLibrary.resolve(change); | |||
} | |||
/** | |||
* The callback format for results | |||
* @callback ChangeStream~resultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {(object|null)} result The result object if the command was executed successfully. | |||
*/ | |||
module.exports = ChangeStream; |
@@ -0,0 +1,334 @@ | |||
'use strict'; | |||
const inherits = require('util').inherits; | |||
const ReadPreference = require('mongodb-core').ReadPreference; | |||
const MongoError = require('mongodb-core').MongoError; | |||
const Readable = require('stream').Readable; | |||
const CoreCursor = require('./cursor'); | |||
/** | |||
* @fileOverview The **CommandCursor** class is an internal class that embodies a | |||
* generalized cursor based on a MongoDB command allowing for iteration over the | |||
* results returned. It supports one by one document iteration, conversion to an | |||
* array or can be iterated as a Node 0.10.X or higher stream | |||
* | |||
* **CommandCursor Cannot directly be instantiated** | |||
* @example | |||
* const MongoClient = require('mongodb').MongoClient; | |||
* const test = require('assert'); | |||
* // Connection url | |||
* const url = 'mongodb://localhost:27017'; | |||
* // Database Name | |||
* const dbName = 'test'; | |||
* // Connect using MongoClient | |||
* MongoClient.connect(url, function(err, client) { | |||
* // Create a collection we want to drop later | |||
* const col = client.db(dbName).collection('listCollectionsExample1'); | |||
* // Insert a bunch of documents | |||
* col.insert([{a:1, b:1} | |||
* , {a:2, b:2}, {a:3, b:3} | |||
* , {a:4, b:4}], {w:1}, function(err, result) { | |||
* test.equal(null, err); | |||
* // List the database collections available | |||
* db.listCollections().toArray(function(err, items) { | |||
* test.equal(null, err); | |||
* client.close(); | |||
* }); | |||
* }); | |||
* }); | |||
*/ | |||
/** | |||
* Namespace provided by the browser. | |||
* @external Readable | |||
*/ | |||
/** | |||
* Creates a new Command Cursor instance (INTERNAL TYPE, do not instantiate directly) | |||
* @class CommandCursor | |||
* @extends external:Readable | |||
* @fires CommandCursor#data | |||
* @fires CommandCursor#end | |||
* @fires CommandCursor#close | |||
* @fires CommandCursor#readable | |||
* @return {CommandCursor} an CommandCursor instance. | |||
*/ | |||
var CommandCursor = function(bson, ns, cmd, options, topology, topologyOptions) { | |||
CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0)); | |||
var state = CommandCursor.INIT; | |||
var streamOptions = {}; | |||
// MaxTimeMS | |||
var maxTimeMS = null; | |||
// Get the promiseLibrary | |||
var promiseLibrary = options.promiseLibrary || Promise; | |||
// Set up | |||
Readable.call(this, { objectMode: true }); | |||
// Internal state | |||
this.s = { | |||
// MaxTimeMS | |||
maxTimeMS: maxTimeMS, | |||
// State | |||
state: state, | |||
// Stream options | |||
streamOptions: streamOptions, | |||
// BSON | |||
bson: bson, | |||
// Namespace | |||
ns: ns, | |||
// Command | |||
cmd: cmd, | |||
// Options | |||
options: options, | |||
// Topology | |||
topology: topology, | |||
// Topology Options | |||
topologyOptions: topologyOptions, | |||
// Promise library | |||
promiseLibrary: promiseLibrary, | |||
// Optional ClientSession | |||
session: options.session | |||
}; | |||
}; | |||
/** | |||
* CommandCursor stream data event, fired for each document in the cursor. | |||
* | |||
* @event CommandCursor#data | |||
* @type {object} | |||
*/ | |||
/** | |||
* CommandCursor stream end event | |||
* | |||
* @event CommandCursor#end | |||
* @type {null} | |||
*/ | |||
/** | |||
* CommandCursor stream close event | |||
* | |||
* @event CommandCursor#close | |||
* @type {null} | |||
*/ | |||
/** | |||
* CommandCursor stream readable event | |||
* | |||
* @event CommandCursor#readable | |||
* @type {null} | |||
*/ | |||
// Inherit from Readable | |||
inherits(CommandCursor, Readable); | |||
// Set the methods to inherit from prototype | |||
var methodsToInherit = [ | |||
'_next', | |||
'next', | |||
'hasNext', | |||
'each', | |||
'forEach', | |||
'toArray', | |||
'rewind', | |||
'bufferedCount', | |||
'readBufferedDocuments', | |||
'close', | |||
'isClosed', | |||
'kill', | |||
'setCursorBatchSize', | |||
'_find', | |||
'_getmore', | |||
'_killcursor', | |||
'isDead', | |||
'explain', | |||
'isNotified', | |||
'isKilled', | |||
'_endSession', | |||
'_initImplicitSession' | |||
]; | |||
// Only inherit the types we need | |||
for (var i = 0; i < methodsToInherit.length; i++) { | |||
CommandCursor.prototype[methodsToInherit[i]] = CoreCursor.prototype[methodsToInherit[i]]; | |||
} | |||
/** | |||
* Set the ReadPreference for the cursor. | |||
* @method | |||
* @param {(string|ReadPreference)} readPreference The new read preference for the cursor. | |||
* @throws {MongoError} | |||
* @return {Cursor} | |||
*/ | |||
CommandCursor.prototype.setReadPreference = function(readPreference) { | |||
if (this.s.state === CommandCursor.CLOSED || this.isDead()) { | |||
throw MongoError.create({ message: 'Cursor is closed', driver: true }); | |||
} | |||
if (this.s.state !== CommandCursor.INIT) { | |||
throw MongoError.create({ | |||
message: 'cannot change cursor readPreference after cursor has been accessed', | |||
driver: true | |||
}); | |||
} | |||
if (readPreference instanceof ReadPreference) { | |||
this.s.options.readPreference = readPreference; | |||
} else if (typeof readPreference === 'string') { | |||
this.s.options.readPreference = new ReadPreference(readPreference); | |||
} else { | |||
throw new TypeError('Invalid read preference: ' + readPreference); | |||
} | |||
return this; | |||
}; | |||
/** | |||
* Set the batch size for the cursor. | |||
* @method | |||
* @param {number} value The batchSize for the cursor. | |||
* @throws {MongoError} | |||
* @return {CommandCursor} | |||
*/ | |||
CommandCursor.prototype.batchSize = function(value) { | |||
if (this.s.state === CommandCursor.CLOSED || this.isDead()) | |||
throw MongoError.create({ message: 'Cursor is closed', driver: true }); | |||
if (typeof value !== 'number') | |||
throw MongoError.create({ message: 'batchSize requires an integer', driver: true }); | |||
if (this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value; | |||
this.setCursorBatchSize(value); | |||
return this; | |||
}; | |||
/** | |||
* Add a maxTimeMS stage to the aggregation pipeline | |||
* @method | |||
* @param {number} value The state maxTimeMS value. | |||
* @return {CommandCursor} | |||
*/ | |||
CommandCursor.prototype.maxTimeMS = function(value) { | |||
if (this.s.topology.lastIsMaster().minWireVersion > 2) { | |||
this.s.cmd.maxTimeMS = value; | |||
} | |||
return this; | |||
}; | |||
/** | |||
* Return the cursor logger | |||
* @method | |||
* @return {Logger} return the cursor logger | |||
* @ignore | |||
*/ | |||
CommandCursor.prototype.getLogger = function() { | |||
return this.logger; | |||
}; | |||
CommandCursor.prototype.get = CommandCursor.prototype.toArray; | |||
/** | |||
* Get the next available document from the cursor, returns null if no more documents are available. | |||
* @function CommandCursor.prototype.next | |||
* @param {CommandCursor~resultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* Check if there is any document still available in the cursor | |||
* @function CommandCursor.prototype.hasNext | |||
* @param {CommandCursor~resultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* The callback format for results | |||
* @callback CommandCursor~toArrayResultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {object[]} documents All the documents the satisfy the cursor. | |||
*/ | |||
/** | |||
* Returns an array of documents. The caller is responsible for making sure that there | |||
* is enough memory to store the results. Note that the array only contain partial | |||
* results when this cursor had been previouly accessed. | |||
* @method CommandCursor.prototype.toArray | |||
* @param {CommandCursor~toArrayResultCallback} [callback] The result callback. | |||
* @throws {MongoError} | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* The callback format for results | |||
* @callback CommandCursor~resultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {(object|null)} result The result object if the command was executed successfully. | |||
*/ | |||
/** | |||
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**, | |||
* not all of the elements will be iterated if this cursor had been previouly accessed. | |||
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike | |||
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements | |||
* at any given time if batch size is specified. Otherwise, the caller is responsible | |||
* for making sure that the entire result can fit the memory. | |||
* @method CommandCursor.prototype.each | |||
* @param {CommandCursor~resultCallback} callback The result callback. | |||
* @throws {MongoError} | |||
* @return {null} | |||
*/ | |||
/** | |||
* Close the cursor, sending a KillCursor command and emitting close. | |||
* @method CommandCursor.prototype.close | |||
* @param {CommandCursor~resultCallback} [callback] The result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
/** | |||
* Is the cursor closed | |||
* @method CommandCursor.prototype.isClosed | |||
* @return {boolean} | |||
*/ | |||
/** | |||
* Clone the cursor | |||
* @function CommandCursor.prototype.clone | |||
* @return {CommandCursor} | |||
*/ | |||
/** | |||
* Resets the cursor | |||
* @function CommandCursor.prototype.rewind | |||
* @return {CommandCursor} | |||
*/ | |||
/** | |||
* The callback format for the forEach iterator method | |||
* @callback CommandCursor~iteratorCallback | |||
* @param {Object} doc An emitted document for the iterator | |||
*/ | |||
/** | |||
* The callback error format for the forEach iterator method | |||
* @callback CommandCursor~endCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
*/ | |||
/* | |||
* Iterates over all the documents for this cursor using the iterator, callback pattern. | |||
* @method CommandCursor.prototype.forEach | |||
* @param {CommandCursor~iteratorCallback} iterator The iteration callback. | |||
* @param {CommandCursor~endCallback} callback The end callback. | |||
* @throws {MongoError} | |||
* @return {null} | |||
*/ | |||
CommandCursor.INIT = 0; | |||
CommandCursor.OPEN = 1; | |||
CommandCursor.CLOSED = 2; | |||
module.exports = CommandCursor; |
@@ -0,0 +1,10 @@ | |||
'use strict'; | |||
module.exports = { | |||
SYSTEM_NAMESPACE_COLLECTION: 'system.namespaces', | |||
SYSTEM_INDEX_COLLECTION: 'system.indexes', | |||
SYSTEM_PROFILE_COLLECTION: 'system.profile', | |||
SYSTEM_USER_COLLECTION: 'system.users', | |||
SYSTEM_COMMAND_COLLECTION: '$cmd', | |||
SYSTEM_JS_COLLECTION: 'system.js' | |||
}; |
@@ -0,0 +1,985 @@ | |||
'use strict'; | |||
const EventEmitter = require('events').EventEmitter; | |||
const inherits = require('util').inherits; | |||
const getSingleProperty = require('./utils').getSingleProperty; | |||
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 Collection = require('./collection'); | |||
const mergeOptionsAndWriteConcern = require('./utils').mergeOptionsAndWriteConcern; | |||
const executeOperation = require('./utils').executeOperation; | |||
const applyWriteConcern = require('./utils').applyWriteConcern; | |||
const resolveReadPreference = require('./utils').resolveReadPreference; | |||
const ChangeStream = require('./change_stream'); | |||
const deprecate = require('util').deprecate; | |||
const deprecateOptions = require('./utils').deprecateOptions; | |||
const CONSTANTS = require('./constants'); | |||
// Operations | |||
const addUser = require('./operations/db_ops').addUser; | |||
const collections = require('./operations/db_ops').collections; | |||
const createCollection = require('./operations/db_ops').createCollection; | |||
const createIndex = require('./operations/db_ops').createIndex; | |||
const createListener = require('./operations/db_ops').createListener; | |||
const dropCollection = require('./operations/db_ops').dropCollection; | |||
const dropDatabase = require('./operations/db_ops').dropDatabase; | |||
const ensureIndex = require('./operations/db_ops').ensureIndex; | |||
const evaluate = require('./operations/db_ops').evaluate; | |||
const executeCommand = require('./operations/db_ops').executeCommand; | |||
const executeDbAdminCommand = require('./operations/db_ops').executeDbAdminCommand; | |||
const indexInformation = require('./operations/db_ops').indexInformation; | |||
const listCollectionsTransforms = require('./operations/db_ops').listCollectionsTransforms; | |||
const profilingInfo = require('./operations/db_ops').profilingInfo; | |||
const profilingLevel = require('./operations/db_ops').profilingLevel; | |||
const removeUser = require('./operations/db_ops').removeUser; | |||
const setProfilingLevel = require('./operations/db_ops').setProfilingLevel; | |||
const validateDatabaseName = require('./operations/db_ops').validateDatabaseName; | |||
/** | |||
* @fileOverview The **Db** class is a class that represents a MongoDB Database. | |||
* | |||
* @example | |||
* const MongoClient = require('mongodb').MongoClient; | |||
* // Connection url | |||
* const url = 'mongodb://localhost:27017'; | |||
* // Database Name | |||
* const dbName = 'test'; | |||
* // Connect using MongoClient | |||
* MongoClient.connect(url, function(err, client) { | |||
* // Select the database by name | |||
* const testDb = client.db(dbName); | |||
* client.close(); | |||
* }); | |||
*/ | |||
// Allowed parameters | |||
const legalOptionNames = [ | |||
'w', | |||
'wtimeout', | |||
'fsync', | |||
'j', | |||
'readPreference', | |||
'readPreferenceTags', | |||
'native_parser', | |||
'forceServerObjectId', | |||
'pkFactory', | |||
'serializeFunctions', | |||
'raw', | |||
'bufferMaxEntries', | |||
'authSource', | |||
'ignoreUndefined', | |||
'promoteLongs', | |||
'promiseLibrary', | |||
'readConcern', | |||
'retryMiliSeconds', | |||
'numberOfRetries', | |||
'parentDb', | |||
'noListener', | |||
'loggerLevel', | |||
'logger', | |||
'promoteBuffers', | |||
'promoteLongs', | |||
'promoteValues', | |||
'compression', | |||
'retryWrites' | |||
]; | |||
/** | |||
* Creates a new Db instance | |||
* @class | |||
* @param {string} databaseName The name of the database this instance represents. | |||
* @param {(Server|ReplSet|Mongos)} topology The server topology for the database. | |||
* @param {object} [options] Optional settings. | |||
* @param {string} [options.authSource] If the database authentication is dependent on another databaseName. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver. | |||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object. | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields. | |||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers. | |||
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution. | |||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers. | |||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types. | |||
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys. | |||
* @param {object} [options.promiseLibrary] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible | |||
* @param {object} [options.readConcern] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) | |||
* @param {object} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) | |||
* @property {(Server|ReplSet|Mongos)} serverConfig Get the current db topology. | |||
* @property {number} bufferMaxEntries Current bufferMaxEntries value for the database | |||
* @property {string} databaseName The name of the database this instance represents. | |||
* @property {object} options The options associated with the db instance. | |||
* @property {boolean} native_parser The current value of the parameter native_parser. | |||
* @property {boolean} slaveOk The current slaveOk value for the db instance. | |||
* @property {object} writeConcern The current write concern values. | |||
* @property {object} topology Access the topology object (single server, replicaset or mongos). | |||
* @fires Db#close | |||
* @fires Db#reconnect | |||
* @fires Db#error | |||
* @fires Db#timeout | |||
* @fires Db#parseError | |||
* @fires Db#fullsetup | |||
* @return {Db} a Db instance. | |||
*/ | |||
function Db(databaseName, topology, options) { | |||
options = options || {}; | |||
if (!(this instanceof Db)) return new Db(databaseName, topology, options); | |||
EventEmitter.call(this); | |||
// Get the promiseLibrary | |||
const promiseLibrary = options.promiseLibrary || Promise; | |||
// Filter the options | |||
options = filterOptions(options, legalOptionNames); | |||
// Ensure we put the promiseLib in the options | |||
options.promiseLibrary = promiseLibrary; | |||
// Internal state of the db object | |||
this.s = { | |||
// Database name | |||
databaseName: databaseName, | |||
// DbCache | |||
dbCache: {}, | |||
// Children db's | |||
children: [], | |||
// Topology | |||
topology: topology, | |||
// Options | |||
options: options, | |||
// Logger instance | |||
logger: Logger('Db', options), | |||
// Get the bson parser | |||
bson: topology ? topology.bson : null, | |||
// Unpack read preference | |||
readPreference: options.readPreference, | |||
// Set buffermaxEntries | |||
bufferMaxEntries: typeof options.bufferMaxEntries === 'number' ? options.bufferMaxEntries : -1, | |||
// Parent db (if chained) | |||
parentDb: options.parentDb || null, | |||
// Set up the primary key factory or fallback to ObjectID | |||
pkFactory: options.pkFactory || ObjectID, | |||
// Get native parser | |||
nativeParser: options.nativeParser || options.native_parser, | |||
// Promise library | |||
promiseLibrary: promiseLibrary, | |||
// No listener | |||
noListener: typeof options.noListener === 'boolean' ? options.noListener : false, | |||
// ReadConcern | |||
readConcern: options.readConcern | |||
}; | |||
// Ensure we have a valid db name | |||
validateDatabaseName(this.s.databaseName); | |||
// Add a read Only property | |||
getSingleProperty(this, 'serverConfig', this.s.topology); | |||
getSingleProperty(this, 'bufferMaxEntries', this.s.bufferMaxEntries); | |||
getSingleProperty(this, 'databaseName', this.s.databaseName); | |||
// This is a child db, do not register any listeners | |||
if (options.parentDb) return; | |||
if (this.s.noListener) return; | |||
// Add listeners | |||
topology.on('error', createListener(this, 'error', this)); | |||
topology.on('timeout', createListener(this, 'timeout', this)); | |||
topology.on('close', createListener(this, 'close', this)); | |||
topology.on('parseError', createListener(this, 'parseError', this)); | |||
topology.once('open', createListener(this, 'open', this)); | |||
topology.once('fullsetup', createListener(this, 'fullsetup', this)); | |||
topology.once('all', createListener(this, 'all', this)); | |||
topology.on('reconnect', createListener(this, 'reconnect', this)); | |||
} | |||
inherits(Db, EventEmitter); | |||
// Topology | |||
Object.defineProperty(Db.prototype, 'topology', { | |||
enumerable: true, | |||
get: function() { | |||
return this.s.topology; | |||
} | |||
}); | |||
// Options | |||
Object.defineProperty(Db.prototype, 'options', { | |||
enumerable: true, | |||
get: function() { | |||
return this.s.options; | |||
} | |||
}); | |||
// slaveOk specified | |||
Object.defineProperty(Db.prototype, 'slaveOk', { | |||
enumerable: true, | |||
get: function() { | |||
if ( | |||
this.s.options.readPreference != null && | |||
(this.s.options.readPreference !== 'primary' || | |||
this.s.options.readPreference.mode !== 'primary') | |||
) { | |||
return true; | |||
} | |||
return false; | |||
} | |||
}); | |||
// get the write Concern | |||
Object.defineProperty(Db.prototype, 'writeConcern', { | |||
enumerable: true, | |||
get: function() { | |||
const ops = {}; | |||
if (this.s.options.w != null) ops.w = this.s.options.w; | |||
if (this.s.options.j != null) ops.j = this.s.options.j; | |||
if (this.s.options.fsync != null) ops.fsync = this.s.options.fsync; | |||
if (this.s.options.wtimeout != null) ops.wtimeout = this.s.options.wtimeout; | |||
return ops; | |||
} | |||
}); | |||
/** | |||
* Execute a command | |||
* @method | |||
* @param {object} command The command hash | |||
* @param {object} [options] Optional settings. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.command = function(command, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = Object.assign({}, options); | |||
return executeOperation(this.s.topology, executeCommand, [this, command, options, callback]); | |||
}; | |||
/** | |||
* Return the Admin db instance | |||
* @method | |||
* @return {Admin} return the new Admin db instance | |||
*/ | |||
Db.prototype.admin = function() { | |||
const Admin = require('./admin'); | |||
return new Admin(this, this.s.topology, this.s.promiseLibrary); | |||
}; | |||
/** | |||
* The callback format for the collection method, must be used if strict is specified | |||
* @callback Db~collectionResultCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {Collection} collection The collection instance. | |||
*/ | |||
const collectionKeys = [ | |||
'pkFactory', | |||
'readPreference', | |||
'serializeFunctions', | |||
'strict', | |||
'readConcern', | |||
'ignoreUndefined', | |||
'promoteValues', | |||
'promoteBuffers', | |||
'promoteLongs' | |||
]; | |||
/** | |||
* Fetch a specific collection (containing the actual collection information). If the application does not use strict mode you | |||
* can use it without a callback in the following way: `const collection = db.collection('mycollection');` | |||
* | |||
* @method | |||
* @param {string} name the collection name we wish to access. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers. | |||
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object. | |||
* @param {boolean} [options.strict=false] Returns an error if the collection does not exist | |||
* @param {object} [options.readConcern] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) | |||
* @param {object} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) | |||
* @param {Db~collectionResultCallback} [callback] The collection result callback | |||
* @return {Collection} return the new Collection instance if not in strict mode | |||
*/ | |||
Db.prototype.collection = function(name, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
options = Object.assign({}, options); | |||
// Set the promise library | |||
options.promiseLibrary = this.s.promiseLibrary; | |||
// If we have not set a collection level readConcern set the db level one | |||
options.readConcern = options.readConcern || this.s.readConcern; | |||
// Do we have ignoreUndefined set | |||
if (this.s.options.ignoreUndefined) { | |||
options.ignoreUndefined = this.s.options.ignoreUndefined; | |||
} | |||
// Merge in all needed options and ensure correct writeConcern merging from db level | |||
options = mergeOptionsAndWriteConcern(options, this.s.options, collectionKeys, true); | |||
// Execute | |||
if (options == null || !options.strict) { | |||
try { | |||
const collection = new Collection( | |||
this, | |||
this.s.topology, | |||
this.s.databaseName, | |||
name, | |||
this.s.pkFactory, | |||
options | |||
); | |||
if (callback) callback(null, collection); | |||
return collection; | |||
} catch (err) { | |||
if (err instanceof MongoError && callback) return callback(err); | |||
throw err; | |||
} | |||
} | |||
// Strict mode | |||
if (typeof callback !== 'function') { | |||
throw toError(`A callback is required in strict mode. While getting collection ${name}`); | |||
} | |||
// Did the user destroy the topology | |||
if (this.serverConfig && this.serverConfig.isDestroyed()) { | |||
return callback(new MongoError('topology was destroyed')); | |||
} | |||
const listCollectionOptions = Object.assign({}, options, { nameOnly: true }); | |||
// Strict mode | |||
this.listCollections({ name: name }, listCollectionOptions).toArray((err, collections) => { | |||
if (err != null) return handleCallback(callback, err, null); | |||
if (collections.length === 0) | |||
return handleCallback( | |||
callback, | |||
toError(`Collection ${name} does not exist. Currently in strict mode.`), | |||
null | |||
); | |||
try { | |||
return handleCallback( | |||
callback, | |||
null, | |||
new Collection(this, this.s.topology, this.s.databaseName, name, this.s.pkFactory, options) | |||
); | |||
} catch (err) { | |||
return handleCallback(callback, err, null); | |||
} | |||
}); | |||
}; | |||
/** | |||
* Create a new collection on a server with the specified options. Use this to create capped collections. | |||
* More information about command options available at https://docs.mongodb.com/manual/reference/command/create/ | |||
* | |||
* @method | |||
* @param {string} name the collection name we wish to access. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers. | |||
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object. | |||
* @param {boolean} [options.strict=false] Returns an error if the collection does not exist | |||
* @param {boolean} [options.capped=false] Create a capped collection. | |||
* @param {boolean} [options.autoIndexId=true] DEPRECATED: Create an index on the _id field of the document, True by default on MongoDB 2.6 - 3.0 | |||
* @param {number} [options.size] The size of the capped collection in bytes. | |||
* @param {number} [options.max] The maximum number of documents in the capped collection. | |||
* @param {number} [options.flags] Optional. Available for the MMAPv1 storage engine only to set the usePowerOf2Sizes and the noPadding flag. | |||
* @param {object} [options.storageEngine] Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection on MongoDB 3.0 or higher. | |||
* @param {object} [options.validator] Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation on MongoDB 3.2 or higher. | |||
* @param {string} [options.validationLevel] Determines how strictly MongoDB applies the validation rules to existing documents during an update on MongoDB 3.2 or higher. | |||
* @param {string} [options.validationAction] Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted on MongoDB 3.2 or higher. | |||
* @param {object} [options.indexOptionDefaults] Allows users to specify a default configuration for indexes when creating a collection on MongoDB 3.2 or higher. | |||
* @param {string} [options.viewOn] The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create on MongoDB 3.4 or higher. | |||
* @param {array} [options.pipeline] An array that consists of the aggregation pipeline stage. create creates the view by applying the specified pipeline to the viewOn collection or view on MongoDB 3.4 or higher. | |||
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~collectionResultCallback} [callback] The results callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.createCollection = deprecateOptions( | |||
{ | |||
name: 'Db.createCollection', | |||
deprecatedOptions: ['autoIndexId'], | |||
optionsIndex: 1 | |||
}, | |||
function(name, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
options.promiseLibrary = options.promiseLibrary || this.s.promiseLibrary; | |||
return executeOperation(this.s.topology, createCollection, [this, name, options, callback]); | |||
} | |||
); | |||
/** | |||
* Get all the db statistics. | |||
* | |||
* @method | |||
* @param {object} [options] Optional settings. | |||
* @param {number} [options.scale] Divide the returned sizes by scale value. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The collection result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.stats = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// Build command object | |||
const commandObject = { dbStats: true }; | |||
// Check if we have the scale value | |||
if (options['scale'] != null) commandObject['scale'] = options['scale']; | |||
// If we have a readPreference set | |||
if (options.readPreference == null && this.s.readPreference) { | |||
options.readPreference = this.s.readPreference; | |||
} | |||
// Execute the command | |||
return this.command(commandObject, options, callback); | |||
}; | |||
/** | |||
* Get the list of all collection information for the specified db. | |||
* | |||
* @method | |||
* @param {object} [filter={}] Query to filter collections by | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.nameOnly=false] Since 4.0: If true, will only return the collection name in the response, and will omit additional info | |||
* @param {number} [options.batchSize] The batchSize for the returned command cursor or if pre 2.8 the systems batch collection | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @return {CommandCursor} | |||
*/ | |||
Db.prototype.listCollections = function(filter, options) { | |||
filter = filter || {}; | |||
options = options || {}; | |||
// Shallow clone the object | |||
options = Object.assign({}, options); | |||
// Set the promise library | |||
options.promiseLibrary = this.s.promiseLibrary; | |||
// Ensure valid readPreference | |||
options.readPreference = resolveReadPreference(options, { | |||
db: this, | |||
default: ReadPreference.primary | |||
}); | |||
// Cursor options | |||
let cursor = options.batchSize ? { batchSize: options.batchSize } : {}; | |||
// We have a list collections command | |||
if (this.serverConfig.capabilities().hasListCollectionsCommand) { | |||
const nameOnly = typeof options.nameOnly === 'boolean' ? options.nameOnly : false; | |||
// Build the command | |||
const command = { listCollections: true, filter, cursor, nameOnly }; | |||
// Set the AggregationCursor constructor | |||
options.cursorFactory = CommandCursor; | |||
// Create the cursor | |||
cursor = this.s.topology.cursor(`${this.s.databaseName}.$cmd`, command, options); | |||
// Do we have a readPreference, apply it | |||
if (options.readPreference) { | |||
cursor.setReadPreference(options.readPreference); | |||
} | |||
// Return the cursor | |||
return cursor; | |||
} | |||
// We cannot use the listCollectionsCommand | |||
if (!this.serverConfig.capabilities().hasListCollectionsCommand) { | |||
// If we have legacy mode and have not provided a full db name filter it | |||
if ( | |||
typeof filter.name === 'string' && | |||
!new RegExp('^' + this.databaseName + '\\.').test(filter.name) | |||
) { | |||
filter = Object.assign({}, filter); | |||
filter.name = `${this.s.databaseName}.${filter.name}`; | |||
} | |||
} | |||
// No filter, filter by current database | |||
if (filter == null) { | |||
filter.name = `/${this.s.databaseName}/`; | |||
} | |||
// Rewrite the filter to use $and to filter out indexes | |||
if (filter.name) { | |||
filter = { $and: [{ name: filter.name }, { name: /^((?!\$).)*$/ }] }; | |||
} else { | |||
filter = { name: /^((?!\$).)*$/ }; | |||
} | |||
// Return options | |||
const _options = { transforms: listCollectionsTransforms(this.s.databaseName) }; | |||
// Get the cursor | |||
cursor = this.collection(CONSTANTS.SYSTEM_NAMESPACE_COLLECTION).find(filter, _options); | |||
// Do we have a readPreference, apply it | |||
if (options.readPreference) cursor.setReadPreference(options.readPreference); | |||
// Set the passed in batch size if one was provided | |||
if (options.batchSize) cursor = cursor.batchSize(options.batchSize); | |||
// We have a fallback mode using legacy systems collections | |||
return cursor; | |||
}; | |||
/** | |||
* Evaluate JavaScript on the server | |||
* | |||
* @method | |||
* @param {Code} code JavaScript to execute on server. | |||
* @param {(object|array)} parameters The parameters for the call. | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.nolock=false] Tell MongoDB not to block on the evaulation of the javascript. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The results callback | |||
* @deprecated Eval is deprecated on MongoDB 3.2 and forward | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.eval = deprecate(function(code, parameters, options, callback) { | |||
const args = Array.prototype.slice.call(arguments, 1); | |||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined; | |||
parameters = args.length ? args.shift() : parameters; | |||
options = args.length ? args.shift() || {} : {}; | |||
return executeOperation(this.s.topology, evaluate, [this, code, parameters, options, callback]); | |||
}, 'Db.eval is deprecated as of MongoDB version 3.2'); | |||
/** | |||
* Rename a collection. | |||
* | |||
* @method | |||
* @param {string} fromCollection Name of current collection to rename. | |||
* @param {string} toCollection New name of of the collection. | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.dropTarget=false] Drop the target name collection if it previously exists. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~collectionResultCallback} [callback] The results callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.renameCollection = function(fromCollection, toCollection, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// Add return new collection | |||
options.new_collection = true; | |||
const collection = this.collection(fromCollection); | |||
return executeOperation(this.s.topology, collection.rename.bind(collection), [ | |||
toCollection, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Drop a collection from the database, removing it permanently. New accesses will create a new collection. | |||
* | |||
* @method | |||
* @param {string} name Name of collection to drop | |||
* @param {Object} [options] Optional settings | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The results callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.dropCollection = function(name, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// Command to execute | |||
const cmd = { drop: name }; | |||
// Decorate with write concern | |||
applyWriteConcern(cmd, { db: this }, options); | |||
// options | |||
const opts = Object.assign({}, this.s.options, { readPreference: ReadPreference.PRIMARY }); | |||
if (options.session) opts.session = options.session; | |||
return executeOperation(this.s.topology, dropCollection, [this, cmd, opts, callback]); | |||
}; | |||
/** | |||
* Drop a database, removing it permanently from the server. | |||
* | |||
* @method | |||
* @param {Object} [options] Optional settings | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The results callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.dropDatabase = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// Drop database command | |||
const cmd = { dropDatabase: 1 }; | |||
// Decorate with write concern | |||
applyWriteConcern(cmd, { db: this }, options); | |||
// Ensure primary only | |||
const finalOptions = Object.assign({}, this.s.options, { | |||
readPreference: ReadPreference.PRIMARY | |||
}); | |||
if (options.session) { | |||
finalOptions.session = options.session; | |||
} | |||
return executeOperation(this.s.topology, dropDatabase, [this, cmd, finalOptions, callback]); | |||
}; | |||
/** | |||
* Fetch all collections for the current db. | |||
* | |||
* @method | |||
* @param {Object} [options] Optional settings | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~collectionsResultCallback} [callback] The results callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.collections = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, collections, [this, options, callback]); | |||
}; | |||
/** | |||
* Runs a command on the database as admin. | |||
* @method | |||
* @param {object} command The command hash | |||
* @param {object} [options] Optional settings. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.executeDbAdminCommand = function(selector, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
options.readPreference = resolveReadPreference(options); | |||
return executeOperation(this.s.topology, executeDbAdminCommand, [ | |||
this, | |||
selector, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Creates an index on the db and collection. | |||
* @method | |||
* @param {string} name Name of the collection to create the index on. | |||
* @param {(string|object)} fieldOrSpec Defines the index. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.unique=false] Creates an unique index. | |||
* @param {boolean} [options.sparse=false] Creates a sparse index. | |||
* @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible. | |||
* @param {boolean} [options.dropDups=false] A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value | |||
* @param {number} [options.min] For geospatial indexes set the lower bound for the co-ordinates. | |||
* @param {number} [options.max] For geospatial indexes set the high bound for the co-ordinates. | |||
* @param {number} [options.v] Specify the format version of the indexes. | |||
* @param {number} [options.expireAfterSeconds] Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) | |||
* @param {number} [options.name] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes) | |||
* @param {object} [options.partialFilterExpression] Creates a partial index based on the given filter object (MongoDB 3.2 or higher) | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options ? Object.assign({}, options) : {}; | |||
return executeOperation(this.s.topology, createIndex, [ | |||
this, | |||
name, | |||
fieldOrSpec, | |||
options, | |||
callback | |||
]); | |||
}; | |||
/** | |||
* Ensures that an index exists, if it does not it creates it | |||
* @method | |||
* @deprecated since version 2.0 | |||
* @param {string} name The index name | |||
* @param {(string|object)} fieldOrSpec Defines the index. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {boolean} [options.unique=false] Creates an unique index. | |||
* @param {boolean} [options.sparse=false] Creates a sparse index. | |||
* @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible. | |||
* @param {boolean} [options.dropDups=false] A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value | |||
* @param {number} [options.min] For geospatial indexes set the lower bound for the co-ordinates. | |||
* @param {number} [options.max] For geospatial indexes set the high bound for the co-ordinates. | |||
* @param {number} [options.v] Specify the format version of the indexes. | |||
* @param {number} [options.expireAfterSeconds] Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) | |||
* @param {number} [options.name] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes) | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.ensureIndex = deprecate(function(name, fieldOrSpec, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, ensureIndex, [ | |||
this, | |||
name, | |||
fieldOrSpec, | |||
options, | |||
callback | |||
]); | |||
}, 'Db.ensureIndex is deprecated as of MongoDB version 3.0 / driver version 2.0'); | |||
Db.prototype.addChild = function(db) { | |||
if (this.s.parentDb) return this.s.parentDb.addChild(db); | |||
this.s.children.push(db); | |||
}; | |||
/** | |||
* Add a user to the database. | |||
* @method | |||
* @param {string} username The username. | |||
* @param {string} password The password. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {object} [options.customData] Custom data associated with the user (only Mongodb 2.6 or higher) | |||
* @param {object[]} [options.roles] Roles associated with the created user (only Mongodb 2.6 or higher) | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.addUser = function(username, password, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, addUser, [this, username, password, options, callback]); | |||
}; | |||
/** | |||
* Remove a user from a database | |||
* @method | |||
* @param {string} username The username. | |||
* @param {object} [options] Optional settings. | |||
* @param {(number|string)} [options.w] The write concern. | |||
* @param {number} [options.wtimeout] The write concern timeout. | |||
* @param {boolean} [options.j=false] Specify a journal write concern. | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.removeUser = function(username, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, removeUser, [this, username, options, callback]); | |||
}; | |||
/** | |||
* Set the current profiling level of MongoDB | |||
* | |||
* @param {string} level The new profiling level (off, slow_only, all). | |||
* @param {Object} [options] Optional settings | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.setProfilingLevel = function(level, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, setProfilingLevel, [this, level, options, callback]); | |||
}; | |||
/** | |||
* Retrive the current profiling information for MongoDB | |||
* | |||
* @param {Object} [options] Optional settings | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback. | |||
* @return {Promise} returns Promise if no callback passed | |||
* @deprecated Query the system.profile collection directly. | |||
*/ | |||
Db.prototype.profilingInfo = deprecate(function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, profilingInfo, [this, options, callback]); | |||
}, 'Db.profilingInfo is deprecated. Query the system.profile collection directly.'); | |||
/** | |||
* Retrieve the current profiling Level for MongoDB | |||
* | |||
* @param {Object} [options] Optional settings | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.profilingLevel = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, profilingLevel, [this, options, callback]); | |||
}; | |||
/** | |||
* Retrieves this collections index info. | |||
* @method | |||
* @param {string} name The name of the collection. | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.full=false] Returns the full raw index information. | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
Db.prototype.indexInformation = function(name, options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
return executeOperation(this.s.topology, indexInformation, [this, name, options, callback]); | |||
}; | |||
/** | |||
* Unref all sockets | |||
* @method | |||
*/ | |||
Db.prototype.unref = function() { | |||
this.s.topology.unref(); | |||
}; | |||
/** | |||
* Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this database. Will ignore all changes to system collections. | |||
* @method | |||
* @since 3.1.0 | |||
* @param {Array} [pipeline] An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. | |||
* @param {object} [options] Optional settings | |||
* @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. | |||
* @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. | |||
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query | |||
* @param {number} [options.batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}. | |||
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}. | |||
* @param {ReadPreference} [options.readPreference] The read preference. Defaults to the read preference of the database. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}. | |||
* @param {Timestamp} [options.startAtClusterTime] receive change events that occur after the specified timestamp | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @return {ChangeStream} a ChangeStream instance. | |||
*/ | |||
Db.prototype.watch = function(pipeline, options) { | |||
pipeline = pipeline || []; | |||
options = options || {}; | |||
// Allow optionally not specifying a pipeline | |||
if (!Array.isArray(pipeline)) { | |||
options = pipeline; | |||
pipeline = []; | |||
} | |||
return new ChangeStream(this, pipeline, options); | |||
}; | |||
/** | |||
* Return the db logger | |||
* @method | |||
* @return {Logger} return the db logger | |||
* @ignore | |||
*/ | |||
Db.prototype.getLogger = function() { | |||
return this.s.logger; | |||
}; | |||
/** | |||
* Db close event | |||
* | |||
* Emitted after a socket closed against a single server or mongos proxy. | |||
* | |||
* @event Db#close | |||
* @type {MongoError} | |||
*/ | |||
/** | |||
* Db reconnect event | |||
* | |||
* * Server: Emitted when the driver has reconnected and re-authenticated. | |||
* * ReplicaSet: N/A | |||
* * Mongos: Emitted when the driver reconnects and re-authenticates successfully against a Mongos. | |||
* | |||
* @event Db#reconnect | |||
* @type {object} | |||
*/ | |||
/** | |||
* Db error event | |||
* | |||
* Emitted after an error occurred against a single server or mongos proxy. | |||
* | |||
* @event Db#error | |||
* @type {MongoError} | |||
*/ | |||
/** | |||
* Db timeout event | |||
* | |||
* Emitted after a socket timeout occurred against a single server or mongos proxy. | |||
* | |||
* @event Db#timeout | |||
* @type {MongoError} | |||
*/ | |||
/** | |||
* Db parseError event | |||
* | |||
* The parseError event is emitted if the driver detects illegal or corrupt BSON being received from the server. | |||
* | |||
* @event Db#parseError | |||
* @type {MongoError} | |||
*/ | |||
/** | |||
* Db fullsetup event, emitted when all servers in the topology have been connected to at start up time. | |||
* | |||
* * Server: Emitted when the driver has connected to the single server and has authenticated. | |||
* * ReplSet: Emitted after the driver has attempted to connect to all replicaset members. | |||
* * Mongos: Emitted after the driver has attempted to connect to all mongos proxies. | |||
* | |||
* @event Db#fullsetup | |||
* @type {Db} | |||
*/ | |||
// Constants | |||
Db.SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION; | |||
Db.SYSTEM_INDEX_COLLECTION = CONSTANTS.SYSTEM_INDEX_COLLECTION; | |||
Db.SYSTEM_PROFILE_COLLECTION = CONSTANTS.SYSTEM_PROFILE_COLLECTION; | |||
Db.SYSTEM_USER_COLLECTION = CONSTANTS.SYSTEM_USER_COLLECTION; | |||
Db.SYSTEM_COMMAND_COLLECTION = CONSTANTS.SYSTEM_COMMAND_COLLECTION; | |||
Db.SYSTEM_JS_COLLECTION = CONSTANTS.SYSTEM_JS_COLLECTION; | |||
module.exports = Db; |
@@ -0,0 +1,43 @@ | |||
'use strict'; | |||
const MongoNetworkError = require('mongodb-core').MongoNetworkError; | |||
const mongoErrorContextSymbol = require('mongodb-core').mongoErrorContextSymbol; | |||
const GET_MORE_NON_RESUMABLE_CODES = new Set([ | |||
136, // CappedPositionLost | |||
237, // CursorKilled | |||
11601 // Interrupted | |||
]); | |||
// From spec@https://github.com/mongodb/specifications/blob/35e466ddf25059cb30e4113de71cdebd3754657f/source/change-streams.rst#resumable-error: | |||
// | |||
// An error is considered resumable if it meets any of the following criteria: | |||
// - any error encountered which is not a server error (e.g. a timeout error or network error) | |||
// - any server error response from a getMore command excluding those containing the following error codes | |||
// - Interrupted: 11601 | |||
// - CappedPositionLost: 136 | |||
// - CursorKilled: 237 | |||
// - a server error response with an error message containing the substring "not master" or "node is recovering" | |||
// | |||
// An error on an aggregate command is not a resumable error. Only errors on a getMore command may be considered resumable errors. | |||
function isGetMoreError(error) { | |||
if (error[mongoErrorContextSymbol]) { | |||
return error[mongoErrorContextSymbol].isGetMore; | |||
} | |||
} | |||
function isResumableError(error) { | |||
if (!isGetMoreError(error)) { | |||
return false; | |||
} | |||
return !!( | |||
error instanceof MongoNetworkError || | |||
!GET_MORE_NON_RESUMABLE_CODES.has(error.code) || | |||
error.message.match(/not master/) || | |||
error.message.match(/node is recovering/) | |||
); | |||
} | |||
module.exports = { GET_MORE_NON_RESUMABLE_CODES, isResumableError }; |
@@ -0,0 +1,415 @@ | |||
'use strict'; | |||
var stream = require('stream'), | |||
util = require('util'); | |||
module.exports = GridFSBucketReadStream; | |||
/** | |||
* A readable stream that enables you to read buffers from GridFS. | |||
* | |||
* Do not instantiate this class directly. Use `openDownloadStream()` instead. | |||
* | |||
* @class | |||
* @param {Collection} chunks Handle for chunks collection | |||
* @param {Collection} files Handle for files collection | |||
* @param {Object} readPreference The read preference to use | |||
* @param {Object} filter The query to use to find the file document | |||
* @param {Object} [options] Optional settings. | |||
* @param {Number} [options.sort] Optional sort for the file find query | |||
* @param {Number} [options.skip] Optional skip for the file find query | |||
* @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from | |||
* @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before | |||
* @fires GridFSBucketReadStream#error | |||
* @fires GridFSBucketReadStream#file | |||
* @return {GridFSBucketReadStream} a GridFSBucketReadStream instance. | |||
*/ | |||
function GridFSBucketReadStream(chunks, files, readPreference, filter, options) { | |||
this.s = { | |||
bytesRead: 0, | |||
chunks: chunks, | |||
cursor: null, | |||
expected: 0, | |||
files: files, | |||
filter: filter, | |||
init: false, | |||
expectedEnd: 0, | |||
file: null, | |||
options: options, | |||
readPreference: readPreference | |||
}; | |||
stream.Readable.call(this); | |||
} | |||
util.inherits(GridFSBucketReadStream, stream.Readable); | |||
/** | |||
* An error occurred | |||
* | |||
* @event GridFSBucketReadStream#error | |||
* @type {Error} | |||
*/ | |||
/** | |||
* Fires when the stream loaded the file document corresponding to the | |||
* provided id. | |||
* | |||
* @event GridFSBucketReadStream#file | |||
* @type {object} | |||
*/ | |||
/** | |||
* Emitted when a chunk of data is available to be consumed. | |||
* | |||
* @event GridFSBucketReadStream#data | |||
* @type {object} | |||
*/ | |||
/** | |||
* Fired when the stream is exhausted (no more data events). | |||
* | |||
* @event GridFSBucketReadStream#end | |||
* @type {object} | |||
*/ | |||
/** | |||
* Fired when the stream is exhausted and the underlying cursor is killed | |||
* | |||
* @event GridFSBucketReadStream#close | |||
* @type {object} | |||
*/ | |||
/** | |||
* Reads from the cursor and pushes to the stream. | |||
* @method | |||
*/ | |||
GridFSBucketReadStream.prototype._read = function() { | |||
var _this = this; | |||
if (this.destroyed) { | |||
return; | |||
} | |||
waitForFile(_this, function() { | |||
doRead(_this); | |||
}); | |||
}; | |||
/** | |||
* Sets the 0-based offset in bytes to start streaming from. Throws | |||
* an error if this stream has entered flowing mode | |||
* (e.g. if you've already called `on('data')`) | |||
* @method | |||
* @param {Number} start Offset in bytes to start reading at | |||
* @return {GridFSBucketReadStream} | |||
*/ | |||
GridFSBucketReadStream.prototype.start = function(start) { | |||
throwIfInitialized(this); | |||
this.s.options.start = start; | |||
return this; | |||
}; | |||
/** | |||
* Sets the 0-based offset in bytes to start streaming from. Throws | |||
* an error if this stream has entered flowing mode | |||
* (e.g. if you've already called `on('data')`) | |||
* @method | |||
* @param {Number} end Offset in bytes to stop reading at | |||
* @return {GridFSBucketReadStream} | |||
*/ | |||
GridFSBucketReadStream.prototype.end = function(end) { | |||
throwIfInitialized(this); | |||
this.s.options.end = end; | |||
return this; | |||
}; | |||
/** | |||
* Marks this stream as aborted (will never push another `data` event) | |||
* and kills the underlying cursor. Will emit the 'end' event, and then | |||
* the 'close' event once the cursor is successfully killed. | |||
* | |||
* @method | |||
* @param {GridFSBucket~errorCallback} [callback] called when the cursor is successfully closed or an error occurred. | |||
* @fires GridFSBucketWriteStream#close | |||
* @fires GridFSBucketWriteStream#end | |||
*/ | |||
GridFSBucketReadStream.prototype.abort = function(callback) { | |||
var _this = this; | |||
this.push(null); | |||
this.destroyed = true; | |||
if (this.s.cursor) { | |||
this.s.cursor.close(function(error) { | |||
_this.emit('close'); | |||
callback && callback(error); | |||
}); | |||
} else { | |||
if (!this.s.init) { | |||
// If not initialized, fire close event because we will never | |||
// get a cursor | |||
_this.emit('close'); | |||
} | |||
callback && callback(); | |||
} | |||
}; | |||
/** | |||
* @ignore | |||
*/ | |||
function throwIfInitialized(self) { | |||
if (self.s.init) { | |||
throw new Error('You cannot change options after the stream has entered' + 'flowing mode!'); | |||
} | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function doRead(_this) { | |||
if (_this.destroyed) { | |||
return; | |||
} | |||
_this.s.cursor.next(function(error, doc) { | |||
if (_this.destroyed) { | |||
return; | |||
} | |||
if (error) { | |||
return __handleError(_this, error); | |||
} | |||
if (!doc) { | |||
_this.push(null); | |||
return _this.s.cursor.close(function(error) { | |||
if (error) { | |||
return __handleError(_this, error); | |||
} | |||
_this.emit('close'); | |||
}); | |||
} | |||
var bytesRemaining = _this.s.file.length - _this.s.bytesRead; | |||
var expectedN = _this.s.expected++; | |||
var expectedLength = Math.min(_this.s.file.chunkSize, bytesRemaining); | |||
if (doc.n > expectedN) { | |||
var errmsg = 'ChunkIsMissing: Got unexpected n: ' + doc.n + ', expected: ' + expectedN; | |||
return __handleError(_this, new Error(errmsg)); | |||
} | |||
if (doc.n < expectedN) { | |||
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n + ', expected: ' + expectedN; | |||
return __handleError(_this, new Error(errmsg)); | |||
} | |||
var buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer; | |||
if (buf.length !== expectedLength) { | |||
if (bytesRemaining <= 0) { | |||
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n; | |||
return __handleError(_this, new Error(errmsg)); | |||
} | |||
errmsg = | |||
'ChunkIsWrongSize: Got unexpected length: ' + buf.length + ', expected: ' + expectedLength; | |||
return __handleError(_this, new Error(errmsg)); | |||
} | |||
_this.s.bytesRead += buf.length; | |||
if (buf.length === 0) { | |||
return _this.push(null); | |||
} | |||
var sliceStart = null; | |||
var sliceEnd = null; | |||
if (_this.s.bytesToSkip != null) { | |||
sliceStart = _this.s.bytesToSkip; | |||
_this.s.bytesToSkip = 0; | |||
} | |||
if (expectedN === _this.s.expectedEnd && _this.s.bytesToTrim != null) { | |||
sliceEnd = _this.s.bytesToTrim; | |||
} | |||
// If the remaining amount of data left is < chunkSize read the right amount of data | |||
if (_this.s.options.end && _this.s.options.end - _this.s.bytesToSkip < buf.length) { | |||
sliceEnd = _this.s.options.end - _this.s.bytesToSkip; | |||
} | |||
if (sliceStart != null || sliceEnd != null) { | |||
buf = buf.slice(sliceStart || 0, sliceEnd || buf.length); | |||
} | |||
_this.push(buf); | |||
}); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function init(self) { | |||
var findOneOptions = {}; | |||
if (self.s.readPreference) { | |||
findOneOptions.readPreference = self.s.readPreference; | |||
} | |||
if (self.s.options && self.s.options.sort) { | |||
findOneOptions.sort = self.s.options.sort; | |||
} | |||
if (self.s.options && self.s.options.skip) { | |||
findOneOptions.skip = self.s.options.skip; | |||
} | |||
self.s.files.findOne(self.s.filter, findOneOptions, function(error, doc) { | |||
if (error) { | |||
return __handleError(self, error); | |||
} | |||
if (!doc) { | |||
var identifier = self.s.filter._id ? self.s.filter._id.toString() : self.s.filter.filename; | |||
var errmsg = 'FileNotFound: file ' + identifier + ' was not found'; | |||
var err = new Error(errmsg); | |||
err.code = 'ENOENT'; | |||
return __handleError(self, err); | |||
} | |||
// If document is empty, kill the stream immediately and don't | |||
// execute any reads | |||
if (doc.length <= 0) { | |||
self.push(null); | |||
return; | |||
} | |||
if (self.destroyed) { | |||
// If user destroys the stream before we have a cursor, wait | |||
// until the query is done to say we're 'closed' because we can't | |||
// cancel a query. | |||
self.emit('close'); | |||
return; | |||
} | |||
self.s.bytesToSkip = handleStartOption(self, doc, self.s.options); | |||
var filter = { files_id: doc._id }; | |||
// Currently (MongoDB 3.4.4) skip function does not support the index, | |||
// it needs to retrieve all the documents first and then skip them. (CS-25811) | |||
// As work around we use $gte on the "n" field. | |||
if (self.s.options && self.s.options.start != null) { | |||
var skip = Math.floor(self.s.options.start / doc.chunkSize); | |||
if (skip > 0) { | |||
filter['n'] = { $gte: skip }; | |||
} | |||
} | |||
self.s.cursor = self.s.chunks.find(filter).sort({ n: 1 }); | |||
if (self.s.readPreference) { | |||
self.s.cursor.setReadPreference(self.s.readPreference); | |||
} | |||
self.s.expectedEnd = Math.ceil(doc.length / doc.chunkSize); | |||
self.s.file = doc; | |||
self.s.bytesToTrim = handleEndOption(self, doc, self.s.cursor, self.s.options); | |||
self.emit('file', doc); | |||
}); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function waitForFile(_this, callback) { | |||
if (_this.s.file) { | |||
return callback(); | |||
} | |||
if (!_this.s.init) { | |||
init(_this); | |||
_this.s.init = true; | |||
} | |||
_this.once('file', function() { | |||
callback(); | |||
}); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function handleStartOption(stream, doc, options) { | |||
if (options && options.start != null) { | |||
if (options.start > doc.length) { | |||
throw new Error( | |||
'Stream start (' + | |||
options.start + | |||
') must not be ' + | |||
'more than the length of the file (' + | |||
doc.length + | |||
')' | |||
); | |||
} | |||
if (options.start < 0) { | |||
throw new Error('Stream start (' + options.start + ') must not be ' + 'negative'); | |||
} | |||
if (options.end != null && options.end < options.start) { | |||
throw new Error( | |||
'Stream start (' + | |||
options.start + | |||
') must not be ' + | |||
'greater than stream end (' + | |||
options.end + | |||
')' | |||
); | |||
} | |||
stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize; | |||
stream.s.expected = Math.floor(options.start / doc.chunkSize); | |||
return options.start - stream.s.bytesRead; | |||
} | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function handleEndOption(stream, doc, cursor, options) { | |||
if (options && options.end != null) { | |||
if (options.end > doc.length) { | |||
throw new Error( | |||
'Stream end (' + | |||
options.end + | |||
') must not be ' + | |||
'more than the length of the file (' + | |||
doc.length + | |||
')' | |||
); | |||
} | |||
if (options.start < 0) { | |||
throw new Error('Stream end (' + options.end + ') must not be ' + 'negative'); | |||
} | |||
var start = options.start != null ? Math.floor(options.start / doc.chunkSize) : 0; | |||
cursor.limit(Math.ceil(options.end / doc.chunkSize) - start); | |||
stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize); | |||
return Math.ceil(options.end / doc.chunkSize) * doc.chunkSize - options.end; | |||
} | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function __handleError(_this, error) { | |||
_this.emit('error', error); | |||
} |
@@ -0,0 +1,358 @@ | |||
'use strict'; | |||
var Emitter = require('events').EventEmitter; | |||
var GridFSBucketReadStream = require('./download'); | |||
var GridFSBucketWriteStream = require('./upload'); | |||
var shallowClone = require('../utils').shallowClone; | |||
var toError = require('../utils').toError; | |||
var util = require('util'); | |||
var executeOperation = require('../utils').executeOperation; | |||
var DEFAULT_GRIDFS_BUCKET_OPTIONS = { | |||
bucketName: 'fs', | |||
chunkSizeBytes: 255 * 1024 | |||
}; | |||
module.exports = GridFSBucket; | |||
/** | |||
* Constructor for a streaming GridFS interface | |||
* @class | |||
* @param {Db} db A db handle | |||
* @param {object} [options] Optional settings. | |||
* @param {string} [options.bucketName="fs"] The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot. | |||
* @param {number} [options.chunkSizeBytes=255 * 1024] Number of bytes stored in each chunk. Defaults to 255KB | |||
* @param {object} [options.writeConcern] Optional write concern to be passed to write operations, for instance `{ w: 1 }` | |||
* @param {object} [options.readPreference] Optional read preference to be passed to read operations | |||
* @fires GridFSBucketWriteStream#index | |||
* @return {GridFSBucket} | |||
*/ | |||
function GridFSBucket(db, options) { | |||
Emitter.apply(this); | |||
this.setMaxListeners(0); | |||
if (options && typeof options === 'object') { | |||
options = shallowClone(options); | |||
var keys = Object.keys(DEFAULT_GRIDFS_BUCKET_OPTIONS); | |||
for (var i = 0; i < keys.length; ++i) { | |||
if (!options[keys[i]]) { | |||
options[keys[i]] = DEFAULT_GRIDFS_BUCKET_OPTIONS[keys[i]]; | |||
} | |||
} | |||
} else { | |||
options = DEFAULT_GRIDFS_BUCKET_OPTIONS; | |||
} | |||
this.s = { | |||
db: db, | |||
options: options, | |||
_chunksCollection: db.collection(options.bucketName + '.chunks'), | |||
_filesCollection: db.collection(options.bucketName + '.files'), | |||
checkedIndexes: false, | |||
calledOpenUploadStream: false, | |||
promiseLibrary: db.s.promiseLibrary || Promise | |||
}; | |||
} | |||
util.inherits(GridFSBucket, Emitter); | |||
/** | |||
* When the first call to openUploadStream is made, the upload stream will | |||
* check to see if it needs to create the proper indexes on the chunks and | |||
* files collections. This event is fired either when 1) it determines that | |||
* no index creation is necessary, 2) when it successfully creates the | |||
* necessary indexes. | |||
* | |||
* @event GridFSBucket#index | |||
* @type {Error} | |||
*/ | |||
/** | |||
* Returns a writable stream (GridFSBucketWriteStream) for writing | |||
* buffers to GridFS. The stream's 'id' property contains the resulting | |||
* file's id. | |||
* @method | |||
* @param {string} filename The value of the 'filename' key in the files doc | |||
* @param {object} [options] Optional settings. | |||
* @param {number} [options.chunkSizeBytes] Optional overwrite this bucket's chunkSizeBytes for this file | |||
* @param {object} [options.metadata] Optional object to store in the file document's `metadata` field | |||
* @param {string} [options.contentType] Optional string to store in the file document's `contentType` field | |||
* @param {array} [options.aliases] Optional array of strings to store in the file document's `aliases` field | |||
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data | |||
* @return {GridFSBucketWriteStream} | |||
*/ | |||
GridFSBucket.prototype.openUploadStream = function(filename, options) { | |||
if (options) { | |||
options = shallowClone(options); | |||
} else { | |||
options = {}; | |||
} | |||
if (!options.chunkSizeBytes) { | |||
options.chunkSizeBytes = this.s.options.chunkSizeBytes; | |||
} | |||
return new GridFSBucketWriteStream(this, filename, options); | |||
}; | |||
/** | |||
* Returns a writable stream (GridFSBucketWriteStream) for writing | |||
* buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting | |||
* file's id. | |||
* @method | |||
* @param {string|number|object} id A custom id used to identify the file | |||
* @param {string} filename The value of the 'filename' key in the files doc | |||
* @param {object} [options] Optional settings. | |||
* @param {number} [options.chunkSizeBytes] Optional overwrite this bucket's chunkSizeBytes for this file | |||
* @param {object} [options.metadata] Optional object to store in the file document's `metadata` field | |||
* @param {string} [options.contentType] Optional string to store in the file document's `contentType` field | |||
* @param {array} [options.aliases] Optional array of strings to store in the file document's `aliases` field | |||
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data | |||
* @return {GridFSBucketWriteStream} | |||
*/ | |||
GridFSBucket.prototype.openUploadStreamWithId = function(id, filename, options) { | |||
if (options) { | |||
options = shallowClone(options); | |||
} else { | |||
options = {}; | |||
} | |||
if (!options.chunkSizeBytes) { | |||
options.chunkSizeBytes = this.s.options.chunkSizeBytes; | |||
} | |||
options.id = id; | |||
return new GridFSBucketWriteStream(this, filename, options); | |||
}; | |||
/** | |||
* Returns a readable stream (GridFSBucketReadStream) for streaming file | |||
* data from GridFS. | |||
* @method | |||
* @param {ObjectId} id The id of the file doc | |||
* @param {Object} [options] Optional settings. | |||
* @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from | |||
* @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before | |||
* @return {GridFSBucketReadStream} | |||
*/ | |||
GridFSBucket.prototype.openDownloadStream = function(id, options) { | |||
var filter = { _id: id }; | |||
options = { | |||
start: options && options.start, | |||
end: options && options.end | |||
}; | |||
return new GridFSBucketReadStream( | |||
this.s._chunksCollection, | |||
this.s._filesCollection, | |||
this.s.options.readPreference, | |||
filter, | |||
options | |||
); | |||
}; | |||
/** | |||
* Deletes a file with the given id | |||
* @method | |||
* @param {ObjectId} id The id of the file doc | |||
* @param {GridFSBucket~errorCallback} [callback] | |||
*/ | |||
GridFSBucket.prototype.delete = function(id, callback) { | |||
return executeOperation(this.s.db.s.topology, _delete, [this, id, callback], { | |||
skipSessions: true | |||
}); | |||
}; | |||
/** | |||
* @ignore | |||
*/ | |||
function _delete(_this, id, callback) { | |||
_this.s._filesCollection.deleteOne({ _id: id }, function(error, res) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
_this.s._chunksCollection.deleteMany({ files_id: id }, function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
// Delete orphaned chunks before returning FileNotFound | |||
if (!res.result.n) { | |||
var errmsg = 'FileNotFound: no file with id ' + id + ' found'; | |||
return callback(new Error(errmsg)); | |||
} | |||
callback(); | |||
}); | |||
}); | |||
} | |||
/** | |||
* Convenience wrapper around find on the files collection | |||
* @method | |||
* @param {Object} filter | |||
* @param {Object} [options] Optional settings for cursor | |||
* @param {number} [options.batchSize] Optional batch size for cursor | |||
* @param {number} [options.limit] Optional limit for cursor | |||
* @param {number} [options.maxTimeMS] Optional maxTimeMS for cursor | |||
* @param {boolean} [options.noCursorTimeout] Optionally set cursor's `noCursorTimeout` flag | |||
* @param {number} [options.skip] Optional skip for cursor | |||
* @param {object} [options.sort] Optional sort for cursor | |||
* @return {Cursor} | |||
*/ | |||
GridFSBucket.prototype.find = function(filter, options) { | |||
filter = filter || {}; | |||
options = options || {}; | |||
var cursor = this.s._filesCollection.find(filter); | |||
if (options.batchSize != null) { | |||
cursor.batchSize(options.batchSize); | |||
} | |||
if (options.limit != null) { | |||
cursor.limit(options.limit); | |||
} | |||
if (options.maxTimeMS != null) { | |||
cursor.maxTimeMS(options.maxTimeMS); | |||
} | |||
if (options.noCursorTimeout != null) { | |||
cursor.addCursorFlag('noCursorTimeout', options.noCursorTimeout); | |||
} | |||
if (options.skip != null) { | |||
cursor.skip(options.skip); | |||
} | |||
if (options.sort != null) { | |||
cursor.sort(options.sort); | |||
} | |||
return cursor; | |||
}; | |||
/** | |||
* Returns a readable stream (GridFSBucketReadStream) for streaming the | |||
* file with the given name from GridFS. If there are multiple files with | |||
* the same name, this will stream the most recent file with the given name | |||
* (as determined by the `uploadDate` field). You can set the `revision` | |||
* option to change this behavior. | |||
* @method | |||
* @param {String} filename The name of the file to stream | |||
* @param {Object} [options] Optional settings | |||
* @param {number} [options.revision=-1] The revision number relative to the oldest file with the given filename. 0 gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the newest. | |||
* @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from | |||
* @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before | |||
* @return {GridFSBucketReadStream} | |||
*/ | |||
GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) { | |||
var sort = { uploadDate: -1 }; | |||
var skip = null; | |||
if (options && options.revision != null) { | |||
if (options.revision >= 0) { | |||
sort = { uploadDate: 1 }; | |||
skip = options.revision; | |||
} else { | |||
skip = -options.revision - 1; | |||
} | |||
} | |||
var filter = { filename: filename }; | |||
options = { | |||
sort: sort, | |||
skip: skip, | |||
start: options && options.start, | |||
end: options && options.end | |||
}; | |||
return new GridFSBucketReadStream( | |||
this.s._chunksCollection, | |||
this.s._filesCollection, | |||
this.s.options.readPreference, | |||
filter, | |||
options | |||
); | |||
}; | |||
/** | |||
* Renames the file with the given _id to the given string | |||
* @method | |||
* @param {ObjectId} id the id of the file to rename | |||
* @param {String} filename new name for the file | |||
* @param {GridFSBucket~errorCallback} [callback] | |||
*/ | |||
GridFSBucket.prototype.rename = function(id, filename, callback) { | |||
return executeOperation(this.s.db.s.topology, _rename, [this, id, filename, callback], { | |||
skipSessions: true | |||
}); | |||
}; | |||
/** | |||
* @ignore | |||
*/ | |||
function _rename(_this, id, filename, callback) { | |||
var filter = { _id: id }; | |||
var update = { $set: { filename: filename } }; | |||
_this.s._filesCollection.updateOne(filter, update, function(error, res) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
if (!res.result.n) { | |||
return callback(toError('File with id ' + id + ' not found')); | |||
} | |||
callback(); | |||
}); | |||
} | |||
/** | |||
* Removes this bucket's files collection, followed by its chunks collection. | |||
* @method | |||
* @param {GridFSBucket~errorCallback} [callback] | |||
*/ | |||
GridFSBucket.prototype.drop = function(callback) { | |||
return executeOperation(this.s.db.s.topology, _drop, [this, callback], { | |||
skipSessions: true | |||
}); | |||
}; | |||
/** | |||
* Return the db logger | |||
* @method | |||
* @return {Logger} return the db logger | |||
* @ignore | |||
*/ | |||
GridFSBucket.prototype.getLogger = function() { | |||
return this.s.db.s.logger; | |||
}; | |||
/** | |||
* @ignore | |||
*/ | |||
function _drop(_this, callback) { | |||
_this.s._filesCollection.drop(function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
_this.s._chunksCollection.drop(function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
return callback(); | |||
}); | |||
}); | |||
} | |||
/** | |||
* Callback format for all GridFSBucket methods that can accept a callback. | |||
* @callback GridFSBucket~errorCallback | |||
* @param {MongoError} error An error instance representing any errors that occurred | |||
*/ |
@@ -0,0 +1,538 @@ | |||
'use strict'; | |||
var core = require('mongodb-core'); | |||
var crypto = require('crypto'); | |||
var stream = require('stream'); | |||
var util = require('util'); | |||
var Buffer = require('safe-buffer').Buffer; | |||
var ERROR_NAMESPACE_NOT_FOUND = 26; | |||
module.exports = GridFSBucketWriteStream; | |||
/** | |||
* A writable stream that enables you to write buffers to GridFS. | |||
* | |||
* Do not instantiate this class directly. Use `openUploadStream()` instead. | |||
* | |||
* @class | |||
* @param {GridFSBucket} bucket Handle for this stream's corresponding bucket | |||
* @param {string} filename The value of the 'filename' key in the files doc | |||
* @param {object} [options] Optional settings. | |||
* @param {string|number|object} [options.id] Custom file id for the GridFS file. | |||
* @param {number} [options.chunkSizeBytes] The chunk size to use, in bytes | |||
* @param {number} [options.w] The write concern | |||
* @param {number} [options.wtimeout] The write concern timeout | |||
* @param {number} [options.j] The journal write concern | |||
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data | |||
* @fires GridFSBucketWriteStream#error | |||
* @fires GridFSBucketWriteStream#finish | |||
* @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance. | |||
*/ | |||
function GridFSBucketWriteStream(bucket, filename, options) { | |||
options = options || {}; | |||
this.bucket = bucket; | |||
this.chunks = bucket.s._chunksCollection; | |||
this.filename = filename; | |||
this.files = bucket.s._filesCollection; | |||
this.options = options; | |||
// Signals the write is all done | |||
this.done = false; | |||
this.id = options.id ? options.id : core.BSON.ObjectId(); | |||
this.chunkSizeBytes = this.options.chunkSizeBytes; | |||
this.bufToStore = Buffer.alloc(this.chunkSizeBytes); | |||
this.length = 0; | |||
this.md5 = !options.disableMD5 && crypto.createHash('md5'); | |||
this.n = 0; | |||
this.pos = 0; | |||
this.state = { | |||
streamEnd: false, | |||
outstandingRequests: 0, | |||
errored: false, | |||
aborted: false, | |||
promiseLibrary: this.bucket.s.promiseLibrary | |||
}; | |||
if (!this.bucket.s.calledOpenUploadStream) { | |||
this.bucket.s.calledOpenUploadStream = true; | |||
var _this = this; | |||
checkIndexes(this, function() { | |||
_this.bucket.s.checkedIndexes = true; | |||
_this.bucket.emit('index'); | |||
}); | |||
} | |||
} | |||
util.inherits(GridFSBucketWriteStream, stream.Writable); | |||
/** | |||
* An error occurred | |||
* | |||
* @event GridFSBucketWriteStream#error | |||
* @type {Error} | |||
*/ | |||
/** | |||
* `end()` was called and the write stream successfully wrote the file | |||
* metadata and all the chunks to MongoDB. | |||
* | |||
* @event GridFSBucketWriteStream#finish | |||
* @type {object} | |||
*/ | |||
/** | |||
* Write a buffer to the stream. | |||
* | |||
* @method | |||
* @param {Buffer} chunk Buffer to write | |||
* @param {String} encoding Optional encoding for the buffer | |||
* @param {Function} callback Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush. | |||
* @return {Boolean} False if this write required flushing a chunk to MongoDB. True otherwise. | |||
*/ | |||
GridFSBucketWriteStream.prototype.write = function(chunk, encoding, callback) { | |||
var _this = this; | |||
return waitForIndexes(this, function() { | |||
return doWrite(_this, chunk, encoding, callback); | |||
}); | |||
}; | |||
/** | |||
* Places this write stream into an aborted state (all future writes fail) | |||
* and deletes all chunks that have already been written. | |||
* | |||
* @method | |||
* @param {GridFSBucket~errorCallback} callback called when chunks are successfully removed or error occurred | |||
* @return {Promise} if no callback specified | |||
*/ | |||
GridFSBucketWriteStream.prototype.abort = function(callback) { | |||
if (this.state.streamEnd) { | |||
var error = new Error('Cannot abort a stream that has already completed'); | |||
if (typeof callback === 'function') { | |||
return callback(error); | |||
} | |||
return this.state.promiseLibrary.reject(error); | |||
} | |||
if (this.state.aborted) { | |||
error = new Error('Cannot call abort() on a stream twice'); | |||
if (typeof callback === 'function') { | |||
return callback(error); | |||
} | |||
return this.state.promiseLibrary.reject(error); | |||
} | |||
this.state.aborted = true; | |||
this.chunks.deleteMany({ files_id: this.id }, function(error) { | |||
if (typeof callback === 'function') callback(error); | |||
}); | |||
}; | |||
/** | |||
* Tells the stream that no more data will be coming in. The stream will | |||
* persist the remaining data to MongoDB, write the files document, and | |||
* then emit a 'finish' event. | |||
* | |||
* @method | |||
* @param {Buffer} chunk Buffer to write | |||
* @param {String} encoding Optional encoding for the buffer | |||
* @param {Function} callback Function to call when all files and chunks have been persisted to MongoDB | |||
*/ | |||
GridFSBucketWriteStream.prototype.end = function(chunk, encoding, callback) { | |||
var _this = this; | |||
if (typeof chunk === 'function') { | |||
(callback = chunk), (chunk = null), (encoding = null); | |||
} else if (typeof encoding === 'function') { | |||
(callback = encoding), (encoding = null); | |||
} | |||
if (checkAborted(this, callback)) { | |||
return; | |||
} | |||
this.state.streamEnd = true; | |||
if (callback) { | |||
this.once('finish', function(result) { | |||
callback(null, result); | |||
}); | |||
} | |||
if (!chunk) { | |||
waitForIndexes(this, function() { | |||
writeRemnant(_this); | |||
}); | |||
return; | |||
} | |||
this.write(chunk, encoding, function() { | |||
writeRemnant(_this); | |||
}); | |||
}; | |||
/** | |||
* @ignore | |||
*/ | |||
function __handleError(_this, error, callback) { | |||
if (_this.state.errored) { | |||
return; | |||
} | |||
_this.state.errored = true; | |||
if (callback) { | |||
return callback(error); | |||
} | |||
_this.emit('error', error); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function createChunkDoc(filesId, n, data) { | |||
return { | |||
_id: core.BSON.ObjectId(), | |||
files_id: filesId, | |||
n: n, | |||
data: data | |||
}; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function checkChunksIndex(_this, callback) { | |||
_this.chunks.listIndexes().toArray(function(error, indexes) { | |||
if (error) { | |||
// Collection doesn't exist so create index | |||
if (error.code === ERROR_NAMESPACE_NOT_FOUND) { | |||
var index = { files_id: 1, n: 1 }; | |||
_this.chunks.createIndex(index, { background: false, unique: true }, function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
callback(); | |||
}); | |||
return; | |||
} | |||
return callback(error); | |||
} | |||
var hasChunksIndex = false; | |||
indexes.forEach(function(index) { | |||
if (index.key) { | |||
var keys = Object.keys(index.key); | |||
if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) { | |||
hasChunksIndex = true; | |||
} | |||
} | |||
}); | |||
if (hasChunksIndex) { | |||
callback(); | |||
} else { | |||
index = { files_id: 1, n: 1 }; | |||
var indexOptions = getWriteOptions(_this); | |||
indexOptions.background = false; | |||
indexOptions.unique = true; | |||
_this.chunks.createIndex(index, indexOptions, function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
callback(); | |||
}); | |||
} | |||
}); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function checkDone(_this, callback) { | |||
if (_this.done) return true; | |||
if (_this.state.streamEnd && _this.state.outstandingRequests === 0 && !_this.state.errored) { | |||
// Set done so we dont' trigger duplicate createFilesDoc | |||
_this.done = true; | |||
// Create a new files doc | |||
var filesDoc = createFilesDoc( | |||
_this.id, | |||
_this.length, | |||
_this.chunkSizeBytes, | |||
_this.md5 && _this.md5.digest('hex'), | |||
_this.filename, | |||
_this.options.contentType, | |||
_this.options.aliases, | |||
_this.options.metadata | |||
); | |||
if (checkAborted(_this, callback)) { | |||
return false; | |||
} | |||
_this.files.insertOne(filesDoc, getWriteOptions(_this), function(error) { | |||
if (error) { | |||
return __handleError(_this, error, callback); | |||
} | |||
_this.emit('finish', filesDoc); | |||
}); | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function checkIndexes(_this, callback) { | |||
_this.files.findOne({}, { _id: 1 }, function(error, doc) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
if (doc) { | |||
return callback(); | |||
} | |||
_this.files.listIndexes().toArray(function(error, indexes) { | |||
if (error) { | |||
// Collection doesn't exist so create index | |||
if (error.code === ERROR_NAMESPACE_NOT_FOUND) { | |||
var index = { filename: 1, uploadDate: 1 }; | |||
_this.files.createIndex(index, { background: false }, function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
checkChunksIndex(_this, callback); | |||
}); | |||
return; | |||
} | |||
return callback(error); | |||
} | |||
var hasFileIndex = false; | |||
indexes.forEach(function(index) { | |||
var keys = Object.keys(index.key); | |||
if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) { | |||
hasFileIndex = true; | |||
} | |||
}); | |||
if (hasFileIndex) { | |||
checkChunksIndex(_this, callback); | |||
} else { | |||
index = { filename: 1, uploadDate: 1 }; | |||
var indexOptions = getWriteOptions(_this); | |||
indexOptions.background = false; | |||
_this.files.createIndex(index, indexOptions, function(error) { | |||
if (error) { | |||
return callback(error); | |||
} | |||
checkChunksIndex(_this, callback); | |||
}); | |||
} | |||
}); | |||
}); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function createFilesDoc(_id, length, chunkSize, md5, filename, contentType, aliases, metadata) { | |||
var ret = { | |||
_id: _id, | |||
length: length, | |||
chunkSize: chunkSize, | |||
uploadDate: new Date(), | |||
filename: filename | |||
}; | |||
if (md5) { | |||
ret.md5 = md5; | |||
} | |||
if (contentType) { | |||
ret.contentType = contentType; | |||
} | |||
if (aliases) { | |||
ret.aliases = aliases; | |||
} | |||
if (metadata) { | |||
ret.metadata = metadata; | |||
} | |||
return ret; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function doWrite(_this, chunk, encoding, callback) { | |||
if (checkAborted(_this, callback)) { | |||
return false; | |||
} | |||
var inputBuf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding); | |||
_this.length += inputBuf.length; | |||
// Input is small enough to fit in our buffer | |||
if (_this.pos + inputBuf.length < _this.chunkSizeBytes) { | |||
inputBuf.copy(_this.bufToStore, _this.pos); | |||
_this.pos += inputBuf.length; | |||
callback && callback(); | |||
// Note that we reverse the typical semantics of write's return value | |||
// to be compatible with node's `.pipe()` function. | |||
// True means client can keep writing. | |||
return true; | |||
} | |||
// Otherwise, buffer is too big for current chunk, so we need to flush | |||
// to MongoDB. | |||
var inputBufRemaining = inputBuf.length; | |||
var spaceRemaining = _this.chunkSizeBytes - _this.pos; | |||
var numToCopy = Math.min(spaceRemaining, inputBuf.length); | |||
var outstandingRequests = 0; | |||
while (inputBufRemaining > 0) { | |||
var inputBufPos = inputBuf.length - inputBufRemaining; | |||
inputBuf.copy(_this.bufToStore, _this.pos, inputBufPos, inputBufPos + numToCopy); | |||
_this.pos += numToCopy; | |||
spaceRemaining -= numToCopy; | |||
if (spaceRemaining === 0) { | |||
if (_this.md5) { | |||
_this.md5.update(_this.bufToStore); | |||
} | |||
var doc = createChunkDoc(_this.id, _this.n, _this.bufToStore); | |||
++_this.state.outstandingRequests; | |||
++outstandingRequests; | |||
if (checkAborted(_this, callback)) { | |||
return false; | |||
} | |||
_this.chunks.insertOne(doc, getWriteOptions(_this), function(error) { | |||
if (error) { | |||
return __handleError(_this, error); | |||
} | |||
--_this.state.outstandingRequests; | |||
--outstandingRequests; | |||
if (!outstandingRequests) { | |||
_this.emit('drain', doc); | |||
callback && callback(); | |||
checkDone(_this); | |||
} | |||
}); | |||
spaceRemaining = _this.chunkSizeBytes; | |||
_this.pos = 0; | |||
++_this.n; | |||
} | |||
inputBufRemaining -= numToCopy; | |||
numToCopy = Math.min(spaceRemaining, inputBufRemaining); | |||
} | |||
// Note that we reverse the typical semantics of write's return value | |||
// to be compatible with node's `.pipe()` function. | |||
// False means the client should wait for the 'drain' event. | |||
return false; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function getWriteOptions(_this) { | |||
var obj = {}; | |||
if (_this.options.writeConcern) { | |||
obj.w = _this.options.writeConcern.w; | |||
obj.wtimeout = _this.options.writeConcern.wtimeout; | |||
obj.j = _this.options.writeConcern.j; | |||
} | |||
return obj; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function waitForIndexes(_this, callback) { | |||
if (_this.bucket.s.checkedIndexes) { | |||
return callback(false); | |||
} | |||
_this.bucket.once('index', function() { | |||
callback(true); | |||
}); | |||
return true; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function writeRemnant(_this, callback) { | |||
// Buffer is empty, so don't bother to insert | |||
if (_this.pos === 0) { | |||
return checkDone(_this, callback); | |||
} | |||
++_this.state.outstandingRequests; | |||
// Create a new buffer to make sure the buffer isn't bigger than it needs | |||
// to be. | |||
var remnant = Buffer.alloc(_this.pos); | |||
_this.bufToStore.copy(remnant, 0, 0, _this.pos); | |||
if (_this.md5) { | |||
_this.md5.update(remnant); | |||
} | |||
var doc = createChunkDoc(_this.id, _this.n, remnant); | |||
// If the stream was aborted, do not write remnant | |||
if (checkAborted(_this, callback)) { | |||
return false; | |||
} | |||
_this.chunks.insertOne(doc, getWriteOptions(_this), function(error) { | |||
if (error) { | |||
return __handleError(_this, error); | |||
} | |||
--_this.state.outstandingRequests; | |||
checkDone(_this); | |||
}); | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
function checkAborted(_this, callback) { | |||
if (_this.state.aborted) { | |||
if (typeof callback === 'function') { | |||
callback(new Error('this stream has been aborted')); | |||
} | |||
return true; | |||
} | |||
return false; | |||
} |
@@ -0,0 +1,236 @@ | |||
'use strict'; | |||
var Binary = require('mongodb-core').BSON.Binary, | |||
ObjectID = require('mongodb-core').BSON.ObjectID; | |||
var Buffer = require('safe-buffer').Buffer; | |||
/** | |||
* Class for representing a single chunk in GridFS. | |||
* | |||
* @class | |||
* | |||
* @param file {GridStore} The {@link GridStore} object holding this chunk. | |||
* @param mongoObject {object} The mongo object representation of this chunk. | |||
* | |||
* @throws Error when the type of data field for {@link mongoObject} is not | |||
* supported. Currently supported types for data field are instances of | |||
* {@link String}, {@link Array}, {@link Binary} and {@link Binary} | |||
* from the bson module | |||
* | |||
* @see Chunk#buildMongoObject | |||
*/ | |||
var Chunk = function(file, mongoObject, writeConcern) { | |||
if (!(this instanceof Chunk)) return new Chunk(file, mongoObject); | |||
this.file = file; | |||
var mongoObjectFinal = mongoObject == null ? {} : mongoObject; | |||
this.writeConcern = writeConcern || { w: 1 }; | |||
this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id; | |||
this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n; | |||
this.data = new Binary(); | |||
if (typeof mongoObjectFinal.data === 'string') { | |||
var buffer = Buffer.alloc(mongoObjectFinal.data.length); | |||
buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary'); | |||
this.data = new Binary(buffer); | |||
} else if (Array.isArray(mongoObjectFinal.data)) { | |||
buffer = Buffer.alloc(mongoObjectFinal.data.length); | |||
var data = mongoObjectFinal.data.join(''); | |||
buffer.write(data, 0, data.length, 'binary'); | |||
this.data = new Binary(buffer); | |||
} else if (mongoObjectFinal.data && mongoObjectFinal.data._bsontype === 'Binary') { | |||
this.data = mongoObjectFinal.data; | |||
} else if (!Buffer.isBuffer(mongoObjectFinal.data) && !(mongoObjectFinal.data == null)) { | |||
throw Error('Illegal chunk format'); | |||
} | |||
// Update position | |||
this.internalPosition = 0; | |||
}; | |||
/** | |||
* Writes a data to this object and advance the read/write head. | |||
* | |||
* @param data {string} the data to write | |||
* @param callback {function(*, GridStore)} This will be called after executing | |||
* this method. The first parameter will contain null and the second one | |||
* will contain a reference to this object. | |||
*/ | |||
Chunk.prototype.write = function(data, callback) { | |||
this.data.write(data, this.internalPosition, data.length, 'binary'); | |||
this.internalPosition = this.data.length(); | |||
if (callback != null) return callback(null, this); | |||
return this; | |||
}; | |||
/** | |||
* Reads data and advances the read/write head. | |||
* | |||
* @param length {number} The length of data to read. | |||
* | |||
* @return {string} The data read if the given length will not exceed the end of | |||
* the chunk. Returns an empty String otherwise. | |||
*/ | |||
Chunk.prototype.read = function(length) { | |||
// Default to full read if no index defined | |||
length = length == null || length === 0 ? this.length() : length; | |||
if (this.length() - this.internalPosition + 1 >= length) { | |||
var data = this.data.read(this.internalPosition, length); | |||
this.internalPosition = this.internalPosition + length; | |||
return data; | |||
} else { | |||
return ''; | |||
} | |||
}; | |||
Chunk.prototype.readSlice = function(length) { | |||
if (this.length() - this.internalPosition >= length) { | |||
var data = null; | |||
if (this.data.buffer != null) { | |||
//Pure BSON | |||
data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length); | |||
} else { | |||
//Native BSON | |||
data = Buffer.alloc(length); | |||
length = this.data.readInto(data, this.internalPosition); | |||
} | |||
this.internalPosition = this.internalPosition + length; | |||
return data; | |||
} else { | |||
return null; | |||
} | |||
}; | |||
/** | |||
* Checks if the read/write head is at the end. | |||
* | |||
* @return {boolean} Whether the read/write head has reached the end of this | |||
* chunk. | |||
*/ | |||
Chunk.prototype.eof = function() { | |||
return this.internalPosition === this.length() ? true : false; | |||
}; | |||
/** | |||
* Reads one character from the data of this chunk and advances the read/write | |||
* head. | |||
* | |||
* @return {string} a single character data read if the the read/write head is | |||
* not at the end of the chunk. Returns an empty String otherwise. | |||
*/ | |||
Chunk.prototype.getc = function() { | |||
return this.read(1); | |||
}; | |||
/** | |||
* Clears the contents of the data in this chunk and resets the read/write head | |||
* to the initial position. | |||
*/ | |||
Chunk.prototype.rewind = function() { | |||
this.internalPosition = 0; | |||
this.data = new Binary(); | |||
}; | |||
/** | |||
* Saves this chunk to the database. Also overwrites existing entries having the | |||
* same id as this chunk. | |||
* | |||
* @param callback {function(*, GridStore)} This will be called after executing | |||
* this method. The first parameter will contain null and the second one | |||
* will contain a reference to this object. | |||
*/ | |||
Chunk.prototype.save = function(options, callback) { | |||
var self = this; | |||
if (typeof options === 'function') { | |||
callback = options; | |||
options = {}; | |||
} | |||
self.file.chunkCollection(function(err, collection) { | |||
if (err) return callback(err); | |||
// Merge the options | |||
var writeOptions = { upsert: true }; | |||
for (var name in options) writeOptions[name] = options[name]; | |||
for (name in self.writeConcern) writeOptions[name] = self.writeConcern[name]; | |||
if (self.data.length() > 0) { | |||
self.buildMongoObject(function(mongoObject) { | |||
var options = { forceServerObjectId: true }; | |||
for (var name in self.writeConcern) { | |||
options[name] = self.writeConcern[name]; | |||
} | |||
collection.replaceOne({ _id: self.objectId }, mongoObject, writeOptions, function(err) { | |||
callback(err, self); | |||
}); | |||
}); | |||
} else { | |||
callback(null, self); | |||
} | |||
// }); | |||
}); | |||
}; | |||
/** | |||
* Creates a mongoDB object representation of this chunk. | |||
* | |||
* @param callback {function(Object)} This will be called after executing this | |||
* method. The object will be passed to the first parameter and will have | |||
* the structure: | |||
* | |||
* <pre><code> | |||
* { | |||
* '_id' : , // {number} id for this chunk | |||
* 'files_id' : , // {number} foreign key to the file collection | |||
* 'n' : , // {number} chunk number | |||
* 'data' : , // {bson#Binary} the chunk data itself | |||
* } | |||
* </code></pre> | |||
* | |||
* @see <a href="http://www.mongodb.org/display/DOCS/GridFS+Specification#GridFSSpecification-{{chunks}}">MongoDB GridFS Chunk Object Structure</a> | |||
*/ | |||
Chunk.prototype.buildMongoObject = function(callback) { | |||
var mongoObject = { | |||
files_id: this.file.fileId, | |||
n: this.chunkNumber, | |||
data: this.data | |||
}; | |||
// If we are saving using a specific ObjectId | |||
if (this.objectId != null) mongoObject._id = this.objectId; | |||
callback(mongoObject); | |||
}; | |||
/** | |||
* @return {number} the length of the data | |||
*/ | |||
Chunk.prototype.length = function() { | |||
return this.data.length(); | |||
}; | |||
/** | |||
* The position of the read/write head | |||
* @name position | |||
* @lends Chunk# | |||
* @field | |||
*/ | |||
Object.defineProperty(Chunk.prototype, 'position', { | |||
enumerable: true, | |||
get: function() { | |||
return this.internalPosition; | |||
}, | |||
set: function(value) { | |||
this.internalPosition = value; | |||
} | |||
}); | |||
/** | |||
* The default chunk size | |||
* @constant | |||
*/ | |||
Chunk.DEFAULT_CHUNK_SIZE = 1024 * 255; | |||
module.exports = Chunk; |
@@ -0,0 +1,472 @@ | |||
'use strict'; | |||
const ChangeStream = require('./change_stream'); | |||
const Db = require('./db'); | |||
const EventEmitter = require('events').EventEmitter; | |||
const executeOperation = require('./utils').executeOperation; | |||
const handleCallback = require('./utils').handleCallback; | |||
const inherits = require('util').inherits; | |||
const MongoError = require('mongodb-core').MongoError; | |||
// Operations | |||
const connectOp = require('./operations/mongo_client_ops').connectOp; | |||
const logout = require('./operations/mongo_client_ops').logout; | |||
const validOptions = require('./operations/mongo_client_ops').validOptions; | |||
/** | |||
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB. | |||
* | |||
* @example | |||
* // Connect using a MongoClient instance | |||
* const MongoClient = require('mongodb').MongoClient; | |||
* const test = require('assert'); | |||
* // Connection url | |||
* const url = 'mongodb://localhost:27017'; | |||
* // Database Name | |||
* const dbName = 'test'; | |||
* // Connect using MongoClient | |||
* const mongoClient = new MongoClient(url); | |||
* mongoClient.connect(function(err, client) { | |||
* const db = client.db(dbName); | |||
* client.close(); | |||
* }); | |||
* | |||
* @example | |||
* // Connect using the MongoClient.connect static method | |||
* const MongoClient = require('mongodb').MongoClient; | |||
* const test = require('assert'); | |||
* // Connection url | |||
* const url = 'mongodb://localhost:27017'; | |||
* // Database Name | |||
* const dbName = 'test'; | |||
* // Connect using MongoClient | |||
* MongoClient.connect(url, function(err, client) { | |||
* const db = client.db(dbName); | |||
* client.close(); | |||
* }); | |||
*/ | |||
/** | |||
* Creates a new MongoClient instance | |||
* @class | |||
* @param {string} url The connection URI string | |||
* @param {object} [options] Optional settings | |||
* @param {number} [options.poolSize=5] The maximum size of the individual server pool | |||
* @param {boolean} [options.ssl=false] Enable SSL connection. | |||
* @param {boolean} [options.sslValidate=true] Validate mongod server certificate against Certificate Authority | |||
* @param {buffer} [options.sslCA=undefined] SSL Certificate store binary buffer | |||
* @param {buffer} [options.sslCert=undefined] SSL Certificate binary buffer | |||
* @param {buffer} [options.sslKey=undefined] SSL Key file binary buffer | |||
* @param {string} [options.sslPass=undefined] SSL Certificate pass phrase | |||
* @param {buffer} [options.sslCRL=undefined] SSL Certificate revocation list binary buffer | |||
* @param {boolean} [options.autoReconnect=true] Enable autoReconnect for single server instances | |||
* @param {boolean} [options.noDelay=true] TCP Connection no delay | |||
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled | |||
* @param {number} [options.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket | |||
* @param {number} [options.connectTimeoutMS=30000] TCP Connection timeout setting | |||
* @param {number} [options.family] Version of IP stack. Can be 4, 6 or null (default). | |||
* If null, will attempt to connect with IPv6, and will fall back to IPv4 on failure | |||
* @param {number} [options.socketTimeoutMS=360000] TCP Socket timeout setting | |||
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times | |||
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries | |||
* @param {boolean} [options.ha=true] Control if high availability monitoring runs for Replicaset or Mongos proxies | |||
* @param {number} [options.haInterval=10000] The High availability period for replicaset inquiry | |||
* @param {string} [options.replicaSet=undefined] The Replicaset set name | |||
* @param {number} [options.secondaryAcceptableLatencyMS=15] Cutoff latency point in MS for Replicaset member selection | |||
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection | |||
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available | |||
* @param {string} [options.authSource=undefined] Define the database to authenticate against | |||
* @param {(number|string)} [options.w] The write concern | |||
* @param {number} [options.wtimeout] The write concern timeout | |||
* @param {boolean} [options.j=false] Specify a journal write concern | |||
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver | |||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields | |||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers | |||
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST) | |||
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys | |||
* @param {object} [options.promiseLibrary] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible | |||
* @param {object} [options.readConcern] Specify a read concern for the collection (only MongoDB 3.2 or higher supported) | |||
* @param {string} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) | |||
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed) | |||
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug) | |||
* @param {object} [options.logger=undefined] Custom logger object | |||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types | |||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers | |||
* @param {boolean} [options.promoteLongs=true] Promotes long values to number if they fit inside the 53 bits resolution | |||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit | |||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function | |||
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness | |||
* @param {string} [options.appname=undefined] The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections | |||
* @param {string} [options.auth.user=undefined] The username for auth | |||
* @param {string} [options.auth.password=undefined] The password for auth | |||
* @param {string} [options.authMechanism=undefined] Mechanism for authentication: MDEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1 | |||
* @param {object} [options.compression] Type of compression to use: snappy or zlib | |||
* @param {boolean} [options.fsync=false] Specify a file sync write concern | |||
* @param {array} [options.readPreferenceTags] Read preference tags | |||
* @param {number} [options.numberOfRetries=5] The number of retries for a tailable cursor | |||
* @param {boolean} [options.auto_reconnect=true] Enable auto reconnecting for single server instances | |||
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this client | |||
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections | |||
* @param {boolean} [options.useNewUrlParser=false] Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future. | |||
* @param {MongoClient~connectCallback} [callback] The command result callback | |||
* @return {MongoClient} a MongoClient instance | |||
*/ | |||
function MongoClient(url, options) { | |||
if (!(this instanceof MongoClient)) return new MongoClient(url, options); | |||
// Set up event emitter | |||
EventEmitter.call(this); | |||
// The internal state | |||
this.s = { | |||
url: url, | |||
options: options || {}, | |||
promiseLibrary: null, | |||
dbCache: {}, | |||
sessions: [] | |||
}; | |||
// Get the promiseLibrary | |||
const promiseLibrary = this.s.options.promiseLibrary || Promise; | |||
// Add the promise to the internal state | |||
this.s.promiseLibrary = promiseLibrary; | |||
} | |||
/** | |||
* @ignore | |||
*/ | |||
inherits(MongoClient, EventEmitter); | |||
/** | |||
* The callback format for results | |||
* @callback MongoClient~connectCallback | |||
* @param {MongoError} error An error instance representing the error during the execution. | |||
* @param {MongoClient} client The connected client. | |||
*/ | |||
/** | |||
* Connect to MongoDB using a url as documented at | |||
* | |||
* docs.mongodb.org/manual/reference/connection-string/ | |||
* | |||
* Note that for replicasets the replicaSet query parameter is required in the 2.0 driver | |||
* | |||
* @method | |||
* @param {MongoClient~connectCallback} [callback] The command result callback | |||
* @return {Promise<MongoClient>} returns Promise if no callback passed | |||
*/ | |||
MongoClient.prototype.connect = function(callback) { | |||
// Validate options object | |||
const err = validOptions(this.s.options); | |||
if (typeof callback === 'string') { | |||
throw new TypeError('`connect` only accepts a callback'); | |||
} | |||
return executeOperation(this, connectOp, [this, err, callback], { | |||
skipSessions: true | |||
}); | |||
}; | |||
/** | |||
* Logout user from server, fire off on all connections and remove all auth info | |||
* @method | |||
* @param {object} [options] Optional settings. | |||
* @param {string} [options.dbName] Logout against different database than current. | |||
* @param {Db~resultCallback} [callback] The command result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
MongoClient.prototype.logout = function(options, callback) { | |||
if (typeof options === 'function') (callback = options), (options = {}); | |||
options = options || {}; | |||
// Establish the correct database name | |||
const dbName = this.s.options.authSource ? this.s.options.authSource : this.s.options.dbName; | |||
return executeOperation(this, logout, [this, dbName, callback], { | |||
skipSessions: true | |||
}); | |||
}; | |||
/** | |||
* Close the db and its underlying connections | |||
* @method | |||
* @param {boolean} force Force close, emitting no events | |||
* @param {Db~noResultCallback} [callback] The result callback | |||
* @return {Promise} returns Promise if no callback passed | |||
*/ | |||
MongoClient.prototype.close = function(force, callback) { | |||
if (typeof force === 'function') (callback = force), (force = false); | |||
// Close the topology connection | |||
if (this.topology) { | |||
this.topology.close(force); | |||
} | |||
// Emit close event | |||
this.emit('close', this); | |||
// Fire close event on any cached db instances | |||
for (const name in this.s.dbCache) { | |||
this.s.dbCache[name].emit('close'); | |||
} | |||
// Remove listeners after emit | |||
this.removeAllListeners('close'); | |||
// Callback after next event loop tick | |||
if (typeof callback === 'function') | |||
return process.nextTick(() => { | |||
handleCallback(callback, null); | |||
}); | |||
// Return dummy promise | |||
return new this.s.promiseLibrary(resolve => { | |||
resolve(); | |||
}); | |||
}; | |||
/** | |||
* Create a new Db instance sharing the current socket connections. Be aware that the new db instances are | |||
* related in a parent-child relationship to the original instance so that events are correctly emitted on child | |||
* db instances. Child db instances are cached so performing db('db1') twice will return the same instance. | |||
* You can control these behaviors with the options noListener and returnNonCachedInstance. | |||
* | |||
* @method | |||
* @param {string} [dbName] The name of the database we want to use. If not provided, use database name from connection string. | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.noListener=false] Do not make the db an event listener to the original connection. | |||
* @param {boolean} [options.returnNonCachedInstance=false] Control if you want to return a cached instance or have a new one created | |||
* @return {Db} | |||
*/ | |||
MongoClient.prototype.db = function(dbName, options) { | |||
options = options || {}; | |||
// Default to db from connection string if not provided | |||
if (!dbName) { | |||
dbName = this.s.options.dbName; | |||
} | |||
// Copy the options and add out internal override of the not shared flag | |||
const finalOptions = Object.assign({}, this.s.options, options); | |||
// Do we have the db in the cache already | |||
if (this.s.dbCache[dbName] && finalOptions.returnNonCachedInstance !== true) { | |||
return this.s.dbCache[dbName]; | |||
} | |||
// Add promiseLibrary | |||
finalOptions.promiseLibrary = this.s.promiseLibrary; | |||
// If no topology throw an error message | |||
if (!this.topology) { | |||
throw new MongoError('MongoClient must be connected before calling MongoClient.prototype.db'); | |||
} | |||
// Return the db object | |||
const db = new Db(dbName, this.topology, finalOptions); | |||
// Add the db to the cache | |||
this.s.dbCache[dbName] = db; | |||
// Return the database | |||
return db; | |||
}; | |||
/** | |||
* Check if MongoClient is connected | |||
* | |||
* @method | |||
* @param {object} [options] Optional settings. | |||
* @param {boolean} [options.noListener=false] Do not make the db an event listener to the original connection. | |||
* @param {boolean} [options.returnNonCachedInstance=false] Control if you want to return a cached instance or have a new one created | |||
* @return {boolean} | |||
*/ | |||
MongoClient.prototype.isConnected = function(options) { | |||
options = options || {}; | |||
if (!this.topology) return false; | |||
return this.topology.isConnected(options); | |||
}; | |||
/** | |||
* Connect to MongoDB using a url as documented at | |||
* | |||
* docs.mongodb.org/manual/reference/connection-string/ | |||
* | |||
* Note that for replicasets the replicaSet query parameter is required in the 2.0 driver | |||
* | |||
* @method | |||
* @static | |||
* @param {string} url The connection URI string | |||
* @param {object} [options] Optional settings | |||
* @param {number} [options.poolSize=5] The maximum size of the individual server pool | |||
* @param {boolean} [options.ssl=false] Enable SSL connection. | |||
* @param {boolean} [options.sslValidate=true] Validate mongod server certificate against Certificate Authority | |||
* @param {buffer} [options.sslCA=undefined] SSL Certificate store binary buffer | |||
* @param {buffer} [options.sslCert=undefined] SSL Certificate binary buffer | |||
* @param {buffer} [options.sslKey=undefined] SSL Key file binary buffer | |||
* @param {string} [options.sslPass=undefined] SSL Certificate pass phrase | |||
* @param {buffer} [options.sslCRL=undefined] SSL Certificate revocation list binary buffer | |||
* @param {boolean} [options.autoReconnect=true] Enable autoReconnect for single server instances | |||
* @param {boolean} [options.noDelay=true] TCP Connection no delay | |||
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled | |||
* @param {boolean} [options.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket | |||
* @param {number} [options.connectTimeoutMS=30000] TCP Connection timeout setting | |||
* @param {number} [options.family] Version of IP stack. Can be 4, 6 or null (default). | |||
* If null, will attempt to connect with IPv6, and will fall back to IPv4 on failure | |||
* @param {number} [options.socketTimeoutMS=360000] TCP Socket timeout setting | |||
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times | |||
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries | |||
* @param {boolean} [options.ha=true] Control if high availability monitoring runs for Replicaset or Mongos proxies | |||
* @param {number} [options.haInterval=10000] The High availability period for replicaset inquiry | |||
* @param {string} [options.replicaSet=undefined] The Replicaset set name | |||
* @param {number} [options.secondaryAcceptableLatencyMS=15] Cutoff latency point in MS for Replicaset member selection | |||
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection | |||
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available | |||
* @param {string} [options.authSource=undefined] Define the database to authenticate against | |||
* @param {(number|string)} [options.w] The write concern | |||
* @param {number} [options.wtimeout] The write concern timeout | |||
* @param {boolean} [options.j=false] Specify a journal write concern | |||
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver | |||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object | |||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields | |||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers | |||
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited | |||
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST) | |||
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys | |||
* @param {object} [options.promiseLibrary] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible | |||
* @param {object} [options.readConcern] Specify a read concern for the collection (only MongoDB 3.2 or higher supported) | |||
* @param {string} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) | |||
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed) | |||
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug) | |||
* @param {object} [options.logger=undefined] Custom logger object | |||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types | |||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers | |||
* @param {boolean} [options.promoteLongs=true] Promotes long values to number if they fit inside the 53 bits resolution | |||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit | |||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function | |||
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness | |||
* @param {string} [options.appname=undefined] The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections | |||
* @param {string} [options.auth.user=undefined] The username for auth | |||
* @param {string} [options.auth.password=undefined] The password for auth | |||
* @param {string} [options.authMechanism=undefined] Mechanism for authentication: MDEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1 | |||
* @param {object} [options.compression] Type of compression to use: snappy or zlib | |||
* @param {boolean} [options.fsync=false] Specify a file sync write concern | |||
* @param {array} [options.readPreferenceTags] Read preference tags | |||
* @param {number} [options.numberOfRetries=5] The number of retries for a tailable cursor | |||
* @param {boolean} [options.auto_reconnect=true] Enable auto reconnecting for single server instances | |||
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections | |||
* @param {MongoClient~connectCallback} [callback] The command result callback | |||
* @return {Promise<MongoClient>} returns Promise if no callback passed | |||
*/ | |||
MongoClient.connect = function(url, options, callback) { | |||
const args = Array.prototype.slice.call(arguments, 1); | |||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined; | |||
options = args.length ? args.shift() : null; | |||
options = options || {}; | |||
// Create client | |||
const mongoClient = new MongoClient(url, options); | |||
// Execute the connect method | |||
return mongoClient.connect(callback); | |||
}; | |||
/** | |||
* Starts a new session on the server | |||
* | |||
* @param {SessionOptions} [options] optional settings for a driver session | |||
* @return {ClientSession} the newly established session | |||
*/ | |||
MongoClient.prototype.startSession = function(options) { | |||
options = Object.assign({ explicit: true }, options); | |||
if (!this.topology) { | |||
throw new MongoError('Must connect to a server before calling this method'); | |||
} | |||
if (!this.topology.hasSessionSupport()) { | |||
throw new MongoError('Current topology does not support sessions'); | |||
} | |||
return this.topology.startSession(options, this.s.options); | |||
}; | |||
/** | |||
* Runs a given operation with an implicitly created session. The lifetime of the session | |||
* will be handled without the need for user interaction. | |||
* | |||
* NOTE: presently the operation MUST return a Promise (either explicit or implicity as an async function) | |||
* | |||
* @param {Object} [options] Optional settings to be appled to implicitly created session | |||
* @param {Function} operation An operation to execute with an implicitly created session. The signature of this MUST be `(session) => {}` | |||
* @return {Promise} | |||
*/ | |||
MongoClient.prototype.withSession = function(options, operation) { | |||
if (typeof options === 'function') (operation = options), (options = undefined); | |||
const session = this.startSession(options); | |||
let cleanupHandler = (err, result, opts) => { | |||
// prevent multiple calls to cleanupHandler | |||
cleanupHandler = () => { | |||
throw new ReferenceError('cleanupHandler was called too many times'); | |||
}; | |||
opts = Object.assign({ throw: true }, opts); | |||
session.endSession(); | |||
if (err) { | |||
if (opts.throw) throw err; | |||
return Promise.reject(err); | |||
} | |||
}; | |||
try { | |||
const result = operation(session); | |||
return Promise.resolve(result) | |||
.then(result => cleanupHandler(null, result)) | |||
.catch(err => cleanupHandler(err, null, { throw: true })); | |||
} catch (err) { | |||
return cleanupHandler(err, null, { throw: false }); | |||
} | |||
}; | |||
/** | |||
* Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this cluster. Will ignore all changes to system collections, as well as the local, admin, | |||
* and config databases. | |||
* @method | |||
* @since 3.1.0 | |||
* @param {Array} [pipeline] An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. | |||
* @param {object} [options] Optional settings | |||
* @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. | |||
* @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. | |||
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query | |||
* @param {number} [options.batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}. | |||
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}. | |||
* @param {ReadPreference} [options.readPreference] The read preference. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}. | |||
* @param {Timestamp} [options.startAtClusterTime] receive change events that occur after the specified timestamp | |||
* @param {ClientSession} [options.session] optional session to use for this operation | |||
* @return {ChangeStream} a ChangeStream instance. | |||
*/ | |||
MongoClient.prototype.watch = function(pipeline, options) { | |||
pipeline = pipeline || []; | |||
options = options || {}; | |||
// Allow optionally not specifying a pipeline | |||
if (!Array.isArray(pipeline)) { | |||
options = pipeline; | |||
pipeline = []; | |||
} | |||
return new ChangeStream(this, pipeline, options); | |||
}; | |||
/** | |||
* Return the mongo client logger | |||
* @method | |||
* @return {Logger} return the mongo client logger | |||
* @ignore | |||
*/ | |||
MongoClient.prototype.getLogger = function() { | |||
return this.s.options.logger; | |||
}; | |||
module.exports = MongoClient; |
@@ -0,0 +1,62 @@ | |||
'use strict'; | |||
const executeCommand = require('./db_ops').executeCommand; | |||
const executeDbAdminCommand = require('./db_ops').executeDbAdminCommand; | |||
/** | |||
* Get ReplicaSet status | |||
* | |||
* @param {Admin} a collection instance. | |||
* @param {Object} [options] Optional settings. See Admin.prototype.replSetGetStatus for a list of options. | |||
* @param {Admin~resultCallback} [callback] The command result callback. | |||
*/ | |||
function replSetGetStatus(admin, options, callback) { | |||
executeDbAdminCommand(admin.s.db, { replSetGetStatus: 1 }, options, callback); | |||
} | |||
/** | |||
* Retrieve this db's server status. | |||
* | |||
* @param {Admin} a collection instance. | |||
* @param {Object} [options] Optional settings. See Admin.prototype.serverStatus for a list of options. | |||
* @param {Admin~resultCallback} [callback] The command result callback | |||
*/ | |||
function serverStatus(admin, options, callback) { | |||
executeDbAdminCommand(admin.s.db, { serverStatus: 1 }, options, callback); | |||
} | |||
/** | |||
* Validate an existing collection | |||
* | |||
* @param {Admin} a collection instance. | |||
* @param {string} collectionName The name of the collection to validate. | |||
* @param {Object} [options] Optional settings. See Admin.prototype.validateCollection for a list of options. | |||
* @param {Admin~resultCallback} [callback] The command result callback. | |||
*/ | |||
function validateCollection(admin, collectionName, options, callback) { | |||
const command = { validate: collectionName }; | |||
const keys = Object.keys(options); | |||
// Decorate command with extra options | |||
for (let i = 0; i < keys.length; i++) { | |||
if (options.hasOwnProperty(keys[i]) && keys[i] !== 'session') { | |||
command[keys[i]] = options[keys[i]]; | |||
} | |||
} | |||
executeCommand(admin.s.db, command, options, (err, doc) => { | |||
if (err != null) return callback(err, null); | |||
if (doc.ok === 0) return callback(new Error('Error with validate command'), null); | |||
if (doc.result != null && doc.result.constructor !== String) | |||
return callback(new Error('Error with validation data'), null); | |||
if (doc.result != null && doc.result.match(/exception|corrupt/) != null) | |||
return callback(new Error('Error: invalid collection ' + collectionName), null); | |||
if (doc.valid != null && !doc.valid) | |||
return callback(new Error('Error: invalid collection ' + collectionName), null); | |||
return callback(null, doc); | |||
}); | |||
} | |||
module.exports = { replSetGetStatus, serverStatus, validateCollection }; |
@@ -0,0 +1,250 @@ | |||
'use strict'; | |||
const buildCountCommand = require('./collection_ops').buildCountCommand; | |||
const formattedOrderClause = require('../utils').formattedOrderClause; | |||
const handleCallback = require('../utils').handleCallback; | |||
const MongoError = require('mongodb-core').MongoError; | |||
const push = Array.prototype.push; | |||
let cursor; | |||
function loadCursor() { | |||
if (!cursor) { | |||
cursor = require('../cursor'); | |||
} | |||
return cursor; | |||
} | |||
/** | |||
* Get the count of documents for this cursor. | |||
* | |||
* @method | |||
* @param {Cursor} cursor The Cursor instance on which to count. | |||
* @param {boolean} [applySkipLimit=true] Specifies whether the count command apply limit and skip settings should be applied on the cursor or in the provided options. | |||
* @param {object} [options] Optional settings. See Cursor.prototype.count for a list of options. | |||
* @param {Cursor~countResultCallback} [callback] The result callback. | |||
*/ | |||
function count(cursor, applySkipLimit, opts, callback) { | |||
if (applySkipLimit) { | |||
if (typeof cursor.cursorSkip() === 'number') opts.skip = cursor.cursorSkip(); | |||
if (typeof cursor.cursorLimit() === 'number') opts.limit = cursor.cursorLimit(); | |||
} | |||
// Ensure we have the right read preference inheritance | |||
if (opts.readPreference) { | |||
cursor.setReadPreference(opts.readPreference); | |||
} | |||
if ( | |||
typeof opts.maxTimeMS !== 'number' && | |||
cursor.s.cmd && | |||
typeof cursor.s.cmd.maxTimeMS === 'number' | |||
) { | |||
opts.maxTimeMS = cursor.s.cmd.maxTimeMS; | |||
} | |||
let options = {}; | |||
options.skip = opts.skip; | |||
options.limit = opts.limit; | |||
options.hint = opts.hint; | |||
options.maxTimeMS = opts.maxTimeMS; | |||
// Command | |||
const delimiter = cursor.s.ns.indexOf('.'); | |||
options.collectionName = cursor.s.ns.substr(delimiter + 1); | |||
let command; | |||
try { | |||
command = buildCountCommand(cursor, cursor.s.cmd.query, options); | |||
} catch (err) { | |||
return callback(err); | |||
} | |||
// Set cursor server to the same as the topology | |||
cursor.server = cursor.topology.s.coreTopology; | |||
// Execute the command | |||
cursor.s.topology.command( | |||
`${cursor.s.ns.substr(0, delimiter)}.$cmd`, | |||
command, | |||
cursor.s.options, | |||
(err, result) => { | |||
callback(err, result ? result.result.n : null); | |||
} | |||
); | |||
} | |||
/** | |||
* Iterates over all the documents for this cursor. See Cursor.prototype.each for more information. | |||
* | |||
* @method | |||
* @deprecated | |||
* @param {Cursor} cursor The Cursor instance on which to run. | |||
* @param {Cursor~resultCallback} callback The result callback. | |||
*/ | |||
function each(cursor, callback) { | |||
let Cursor = loadCursor(); | |||
if (!callback) throw MongoError.create({ message: 'callback is mandatory', driver: true }); | |||
if (cursor.isNotified()) return; | |||
if (cursor.s.state === Cursor.CLOSED || cursor.isDead()) { | |||
return handleCallback( | |||
callback, | |||
MongoError.create({ message: 'Cursor is closed', driver: true }) | |||
); | |||
} | |||
if (cursor.s.state === Cursor.INIT) cursor.s.state = Cursor.OPEN; | |||
// Define function to avoid global scope escape | |||
let fn = null; | |||
// Trampoline all the entries | |||
if (cursor.bufferedCount() > 0) { | |||
while ((fn = loop(cursor, callback))) fn(cursor, callback); | |||
each(cursor, callback); | |||
} else { | |||
cursor.next((err, item) => { | |||
if (err) return handleCallback(callback, err); | |||
if (item == null) { | |||
return cursor.close({ skipKillCursors: true }, () => handleCallback(callback, null, null)); | |||
} | |||
if (handleCallback(callback, null, item) === false) return; | |||
each(cursor, callback); | |||
}); | |||
} | |||
} | |||
/** | |||
* Check if there is any document still available in the cursor. | |||
* | |||
* @method | |||
* @param {Cursor} cursor The Cursor instance on which to run. | |||
* @param {Cursor~resultCallback} [callback] The result callback. | |||
*/ | |||
function hasNext(cursor, callback) { | |||
let Cursor = loadCursor(); | |||
if (cursor.s.currentDoc) { | |||
return callback(null, true); | |||
} | |||
if (cursor.isNotified()) { | |||
return callback(null, false); | |||
} | |||
nextObject(cursor, (err, doc) => { | |||
if (err) return callback(err, null); | |||
if (cursor.s.state === Cursor.CLOSED || cursor.isDead()) return callback(null, false); | |||
if (!doc) return callback(null, false); | |||
cursor.s.currentDoc = doc; | |||
callback(null, true); | |||
}); | |||
} | |||
// Trampoline emptying the number of retrieved items | |||
// without incurring a nextTick operation | |||
function loop(cursor, callback) { | |||
// No more items we are done | |||
if (cursor.bufferedCount() === 0) return; | |||
// Get the next document | |||
cursor._next(callback); | |||
// Loop | |||
return loop; | |||
} | |||
/** | |||
* Get the next available document from the cursor. Returns null if no more documents are available. | |||
* | |||
* @method | |||
* @param {Cursor} cursor The Cursor instance from which to get the next document. | |||
* @param {Cursor~resultCallback} [callback] The result callback. | |||
*/ | |||
function next(cursor, callback) { | |||
// Return the currentDoc if someone called hasNext first | |||
if (cursor.s.currentDoc) { | |||
const doc = cursor.s.currentDoc; | |||
cursor.s.currentDoc = null; | |||
return callback(null, doc); | |||
} | |||
// Return the next object | |||
nextObject(cursor, callback); | |||
} | |||
// Get the next available document from the cursor, returns null if no more documents are available. | |||
function nextObject(cursor, callback) { | |||
let Cursor = loadCursor(); | |||
if (cursor.s.state === Cursor.CLOSED || (cursor.isDead && cursor.isDead())) | |||
return handleCallback( | |||
callback, | |||
MongoError.create({ message: 'Cursor is closed', driver: true }) | |||
); | |||
if (cursor.s.state === Cursor.INIT && cursor.s.cmd.sort) { | |||
try { | |||
cursor.s.cmd.sort = formattedOrderClause(cursor.s.cmd.sort); | |||
} catch (err) { | |||
return handleCallback(callback, err); | |||
} | |||
} | |||
// Get the next object | |||
cursor._next((err, doc) => { | |||
cursor.s.state = Cursor.OPEN; | |||
if (err) return handleCallback(callback, err); | |||
handleCallback(callback, null, doc); | |||
}); | |||
} | |||
/** | |||
* Returns an array of documents. See Cursor.prototype.toArray for more information. | |||
* | |||
* @method | |||
* @param {Cursor} cursor The Cursor instance from which to get the next document. | |||
* @param {Cursor~toArrayResultCallback} [callback] The result callback. | |||
*/ | |||
function toArray(cursor, callback) { | |||
let Cursor = loadCursor(); | |||
const items = []; | |||
// Reset cursor | |||
cursor.rewind(); | |||
cursor.s.state = Cursor.INIT; | |||
// Fetch all the documents | |||
const fetchDocs = () => { | |||
cursor._next((err, doc) => { | |||
if (err) { | |||
return cursor._endSession | |||
? cursor._endSession(() => handleCallback(callback, err)) | |||
: handleCallback(callback, err); | |||
} | |||
if (doc == null) { | |||
return cursor.close({ skipKillCursors: true }, () => handleCallback(callback, null, items)); | |||
} | |||
// Add doc to items | |||
items.push(doc); | |||
// Get all buffered objects | |||
if (cursor.bufferedCount() > 0) { | |||
let docs = cursor.readBufferedDocuments(cursor.bufferedCount()); | |||
// Transform the doc if transform method added | |||
if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') { | |||
docs = docs.map(cursor.s.transforms.doc); | |||
} | |||
push.apply(items, docs); | |||
} | |||
// Attempt a fetch | |||
fetchDocs(); | |||
}); | |||
}; | |||
fetchDocs(); | |||
} | |||
module.exports = { count, each, hasNext, next, toArray }; |