Update Node_modules

This commit is contained in:
2019-07-02 16:05:15 +02:00
parent 95a0ff3913
commit 9dab5e2dbc
3495 changed files with 39728 additions and 465154 deletions
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
{}
-22
View File
@@ -1,22 +0,0 @@
sudo: required
language: node_js
node_js:
- 4
- 6
- 8
services:
- docker
before_script:
- docker run -d -p 27017:27017 mongo:3
- docker ps -a
script:
- npm run lint
- MONGODB_URL=mongodb://localhost/connect-mongo-test npm test
after_success:
- npm install coveralls && npm run cover | coveralls
+17
View File
@@ -1,3 +1,20 @@
3.0.0 / 2019-06-17
================
__Breaking changes:__
* __Drop__ Node.js 4 & 6 support
* __Add__ Node.js 10 & 12 support
* Upgrade `mongoose` to v5 and `mongodb` to v3 and drop old version support
* Replace deprecated mongo operation
* MongoStore need to supply client/clientPromise instead of db/dbPromise due to depns upgrade
__Others changes:__
* __Add__ Implement store.all function (#291)
* __Add__ Add option writeOperationOptions (#295)
* __Add__ Transparent crypto support (#314)
* Change test framework from Mocha to Jest
* Change linter from xo to eslint
2.0.3 / 2018-12-03
================
+44 -31
View File
@@ -7,19 +7,18 @@ MongoDB session store for [Connect](https://github.com/senchalabs/connect) and [
[![Build Status](https://travis-ci.org/jdesboeufs/connect-mongo.svg?branch=master)](https://travis-ci.org/jdesboeufs/connect-mongo)
[![Coverage Status](https://coveralls.io/repos/jdesboeufs/connect-mongo/badge.svg?branch=master&service=github)](https://coveralls.io/github/jdesboeufs/connect-mongo?branch=master)
[![Dependency Status](https://david-dm.org/jdesboeufs/connect-mongo.svg?style=flat)](https://david-dm.org/jdesboeufs/connect-mongo)
[![Greenkeeper badge](https://badges.greenkeeper.io/jdesboeufs/connect-mongo.svg)](https://greenkeeper.io/)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
## Compatibility
* Support Express up to `5.0`
* Support all Connect versions
* Support [Mongoose](http://mongoosejs.com/index.html) `>= 4.1.2+`
* Support [native MongoDB driver](http://mongodb.github.io/node-mongodb-native/) `>= 2.0.36`
* Support Node.js 4, 6, 8 and 10
* Support [MongoDB](https://www.mongodb.com/) `>= 3.0`
* Support [Mongoose](http://mongoosejs.com/index.html) `>= 5.0`
* Support [native MongoDB driver](http://mongodb.github.io/node-mongodb-native/) `>= 3.0`
* Support Node.js 8, 10 and 12
* Support [MongoDB](https://www.mongodb.com/) `3.2 - 4.0`
For extended compatibility, see previous versions.
For extended compatibility, see previous versions [v2.0.3](https://github.com/jdesboeufs/connect-mongo/tree/v2.0.3).
But please note that we are not maintaining v2.x.x anymore.
## Usage
@@ -37,19 +36,6 @@ app.use(session({
}));
```
Express `2.x`, `3.x` and Connect `1.x`, `2.x`:
```js
const MongoStore = require('connect-mongo')(express);
app.use(express.session({
secret: 'foo',
store: new MongoStore(options)
}));
```
For Connect `1.x` and `2.x`, just replace `express` by `connect`.
### Connection to MongoDB
In many circumstances, `connect-mongo` will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.
@@ -76,19 +62,17 @@ app.use(session({
}));
```
#### Re-use a native MongoDB driver connection (or a promise)
#### Re-use a native MongoDB driver client (or a promise)
In this case, you just have to give your `Db` instance to `connect-mongo`.
If the connection is not opened, `connect-mongo` will do it for you.
In this case, you just have to give your `MongoClient` instance to `connect-mongo`.
```js
/*
** There are many ways to create dbInstance.
** There are many ways to create MongoClient.
** You should refer to the driver documentation.
*/
app.use(session({
store: new MongoStore({ db: dbInstance })
store: new MongoStore({ client: clientInstance })
}));
```
@@ -96,7 +80,7 @@ Or just give a promise...
```js
app.use(session({
store: new MongoStore({ dbPromise: dbInstancePromise })
store: new MongoStore({ clientPromise: clientInstancePromise })
}));
```
@@ -129,7 +113,7 @@ A `MongoStore` instance will emit the following events:
| `touch` | A session has been touched (but not modified) | `sessionId` |
| `update` | A session has been updated | `sessionId` |
| `set` | A session has been created OR updated _(for compatibility purpose)_ | `sessionId` |
| `destroy` | A session has been destroyed | `sessionId` |
| `destroy` | A session has been destroyed manually | `sessionId` |
## Session expiration
@@ -140,8 +124,8 @@ Otherwise, it will create a new one, using `ttl` option.
```js
app.use(session({
store: new MongoStore({
url: 'mongodb://localhost/test-app',
ttl: 14 * 24 * 60 * 60 // = 14 days. Default
url: 'mongodb://localhost/test-app',
ttl: 14 * 24 * 60 * 60 // = 14 days. Default
})
}));
```
@@ -215,6 +199,18 @@ app.use(express.session({
by doing this, setting `touchAfter: 24 * 3600` you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)
## Transparent encryption/decryption of session data
When working with sensitive session data it is [recommended](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md) to use encryption
```js
const store = new MongoStore({
url: 'mongodb://localhost/test-app',
secret: 'squirrel'
})
```
## More options
- `collection` Collection (default: `sessions`)
@@ -229,11 +225,28 @@ by doing this, setting `touchAfter: 24 * 3600` you are saying to the session be
scenarios where you need to support different types of serializations
(e.g., objects and JSON strings) or need to modify the session before using
it in your app.
- `writeOperationOptions` Options object to pass to every MongoDB write operation call that
supports it (e.g. `update`, `remove`). Useful for adjusting the write concern.
Only exception: If `autoRemove` is set to `'interval'`, the write concern
from the `writeOperationOptions` object will get overwritten.
- `transformId` (optional) Transform original sessionId in whatever you want to use as storage key.
## Crypto options
- `secret` (optional) Enables transparent crypto in accordance with [OWASP session management recommendations](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md).
- `algorithm` (optional) Allows for changes to the default symmetric encryption cipher; default is `GCM`. See `crypto.getCiphers()` for supported algorithms.
- `hashing` (optional) May be used to change the default hashing algorithm; default is `sha512`. See `crypto.getHashes()` for supported hashing algorithms.
- `encodeas` (optional) Specify to change the session data cipher text encoding. Default is `hex`.
- `key_size` (optional) When using varying algorithms the key size may be used. Default is `32` based on the `AES` blocksize.
- `iv_size` (optional) This can be used to adjust the default [IV](https://csrc.nist.gov/glossary/term/IV) size if a different algorithm requires a different size. Default is `16`.
- `at_size` (optional) When using newer `AES` modes such as the default `GCM` or `CCM` an authentication tag size can be defined; default is `16`.
## Tests
npm test
```
docker run --rm -p 27017:27017 mongo:3.6
yarn install
yarn test
```
The tests use a database called `connect-mongo-test`.
-243
View File
@@ -1,243 +0,0 @@
<a name="1.0.9"></a>
## [1.0.9](https://github.com/mongodb/js-bson/compare/v1.0.8...v1.0.9) (2018-06-07)
### Bug Fixes
* **serializer:** remove use of `const` ([5feb12f](https://github.com/mongodb/js-bson/commit/5feb12f))
<a name="1.0.7"></a>
## [1.0.7](https://github.com/mongodb/js-bson/compare/v1.0.6...v1.0.7) (2018-06-06)
### Bug Fixes
* **binary:** add type checking for buffer ([26b05b5](https://github.com/mongodb/js-bson/commit/26b05b5))
* **bson:** fix custom inspect property ([080323b](https://github.com/mongodb/js-bson/commit/080323b))
* **readme:** clarify documentation about deserialize methods ([20f764c](https://github.com/mongodb/js-bson/commit/20f764c))
* **serialization:** normalize function stringification ([1320c10](https://github.com/mongodb/js-bson/commit/1320c10))
<a name="1.0.6"></a>
## [1.0.6](https://github.com/mongodb/js-bson/compare/v1.0.5...v1.0.6) (2018-03-12)
### Features
* **serialization:** support arbitrary sizes for the internal serialization buffer ([abe97bc](https://github.com/mongodb/js-bson/commit/abe97bc))
<a name="1.0.5"></a>
## 1.0.5 (2018-02-26)
### Bug Fixes
* **decimal128:** add basic guard against REDOS attacks ([bd61c45](https://github.com/mongodb/js-bson/commit/bd61c45))
* **objectid:** if pid is 1, use random value ([e188ae6](https://github.com/mongodb/js-bson/commit/e188ae6))
1.0.4 2016-01-11
----------------
- #204 remove Buffer.from as it's partially broken in early 4.x.x. series of node releases.
1.0.3 2016-01-03
----------------
- Fixed toString for ObjectId so it will work with inspect.
1.0.2 2016-01-02
----------------
- Minor optimizations for ObjectID to use Buffer.from where available.
1.0.1 2016-12-06
----------------
- Reverse behavior for undefined to be serialized as NULL. MongoDB 3.4 does not allow for undefined comparisons.
1.0.0 2016-12-06
----------------
- Introduced new BSON API and documentation.
0.5.7 2016-11-18
-----------------
- NODE-848 BSON Regex flags must be alphabetically ordered.
0.5.6 2016-10-19
-----------------
- NODE-833, Detects cyclic dependencies in documents and throws error if one is found.
- Fix(deserializer): corrected the check for (size + index) comparison… (Issue #195, https://github.com/JoelParke).
0.5.5 2016-09-15
-----------------
- Added DBPointer up conversion to DBRef
0.5.4 2016-08-23
-----------------
- Added promoteValues flag (default to true) allowing user to specify if deserialization should be into wrapper classes only.
0.5.3 2016-07-11
-----------------
- Throw error if ObjectId is not a string or a buffer.
0.5.2 2016-07-11
-----------------
- All values encoded big-endian style for ObjectId.
0.5.1 2016-07-11
-----------------
- Fixed encoding/decoding issue in ObjectId timestamp generation.
- Removed BinaryParser dependency from the serializer/deserializer.
0.5.0 2016-07-05
-----------------
- Added Decimal128 type and extended test suite to include entire bson corpus.
0.4.23 2016-04-08
-----------------
- Allow for proper detection of ObjectId or objects that look like ObjectId, improving compatibility across third party libraries.
- Remove one package from dependency due to having been pulled from NPM.
0.4.22 2016-03-04
-----------------
- Fix "TypeError: data.copy is not a function" in Electron (Issue #170, https://github.com/kangas).
- Fixed issue with undefined type on deserializing.
0.4.21 2016-01-12
-----------------
- Minor optimizations to avoid non needed object creation.
0.4.20 2015-10-15
-----------------
- Added bower file to repository.
- Fixed browser pid sometimes set greater than 0xFFFF on browsers (Issue #155, https://github.com/rahatarmanahmed)
0.4.19 2015-10-15
-----------------
- Remove all support for bson-ext.
0.4.18 2015-10-15
-----------------
- ObjectID equality check should return boolean instead of throwing exception for invalid oid string #139
- add option for deserializing binary into Buffer object #116
0.4.17 2015-10-15
-----------------
- Validate regexp string for null bytes and throw if there is one.
0.4.16 2015-10-07
-----------------
- Fixed issue with return statement in Map.js.
0.4.15 2015-10-06
-----------------
- Exposed Map correctly via index.js file.
0.4.14 2015-10-06
-----------------
- Exposed Map correctly via bson.js file.
0.4.13 2015-10-06
-----------------
- Added ES6 Map type serialization as well as a polyfill for ES5.
0.4.12 2015-09-18
-----------------
- Made ignore undefined an optional parameter.
0.4.11 2015-08-06
-----------------
- Minor fix for invalid key checking.
0.4.10 2015-08-06
-----------------
- NODE-38 Added new BSONRegExp type to allow direct serialization to MongoDB type.
- Some performance improvements by in lining code.
0.4.9 2015-08-06
----------------
- Undefined fields are omitted from serialization in objects.
0.4.8 2015-07-14
----------------
- Fixed size validation to ensure we can deserialize from dumped files.
0.4.7 2015-06-26
----------------
- Added ability to instruct deserializer to return raw BSON buffers for named array fields.
- Minor deserialization optimization by moving inlined function out.
0.4.6 2015-06-17
----------------
- Fixed serializeWithBufferAndIndex bug.
0.4.5 2015-06-17
----------------
- Removed any references to the shared buffer to avoid non GC collectible bson instances.
0.4.4 2015-06-17
----------------
- Fixed rethrowing of error when not RangeError.
0.4.3 2015-06-17
----------------
- Start buffer at 64K and double as needed, meaning we keep a low memory profile until needed.
0.4.2 2015-06-16
----------------
- More fixes for corrupt Bson
0.4.1 2015-06-16
----------------
- More fixes for corrupt Bson
0.4.0 2015-06-16
----------------
- New JS serializer serializing into a single buffer then copying out the new buffer. Performance is similar to current C++ parser.
- Removed bson-ext extension dependency for now.
0.3.2 2015-03-27
----------------
- Removed node-gyp from install script in package.json.
0.3.1 2015-03-27
----------------
- Return pure js version on native() call if failed to initialize.
0.3.0 2015-03-26
----------------
- Pulled out all C++ code into bson-ext and made it an optional dependency.
0.2.21 2015-03-21
-----------------
- Updated Nan to 1.7.0 to support io.js and node 0.12.0
0.2.19 2015-02-16
-----------------
- Updated Nan to 1.6.2 to support io.js and node 0.12.0
0.2.18 2015-01-20
-----------------
- Updated Nan to 1.5.1 to support io.js
0.2.16 2014-12-17
-----------------
- Made pid cycle on 0xffff to avoid weird overflows on creation of ObjectID's
0.2.12 2014-08-24
-----------------
- Fixes for fortify review of c++ extension
- toBSON correctly allows returns of non objects
0.2.3 2013-10-01
----------------
- Drying of ObjectId code for generation of id (Issue #54, https://github.com/moredip)
- Fixed issue where corrupt CString's could cause endless loop
- Support for Node 0.11.X > (Issue #49, https://github.com/kkoopa)
0.1.4 2012-09-25
----------------
- Added precompiled c++ native extensions for win32 ia32 and x64
-201
View File
@@ -1,201 +0,0 @@
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.
-170
View File
@@ -1,170 +0,0 @@
# BSON parser
BSON is short for Bin­ary JSON and is the bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. You can learn more about it in [the specification](http://bsonspec.org).
This browser version of the BSON parser is compiled using [webpack](https://webpack.js.org/) and the current version is pre-compiled in the `browser_build` directory.
This is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at [mongod-js/bson-ext](https://github.com/mongodb-js/bson-ext).
## Usage
To build a new version perform the following operations:
```
npm install
npm run build
```
A simple example of how to use BSON in the browser:
```html
<script src="./browser_build/bson.js"></script>
<script>
function start() {
// Get the Long type
var Long = BSON.Long;
// Create a bson parser instance
var bson = new BSON();
// Serialize document
var doc = { long: Long.fromNumber(100) }
// Serialize a document
var data = bson.serialize(doc)
// De serialize it again
var doc_2 = bson.deserialize(data)
}
</script>
```
A simple example of how to use BSON in `Node.js`:
```js
// Get BSON parser class
var BSON = require('bson')
// Get the Long type
var Long = BSON.Long;
// Create a bson parser instance
var bson = new BSON();
// Serialize document
var doc = { long: Long.fromNumber(100) }
// Serialize a document
var data = bson.serialize(doc)
console.log('data:', data)
// Deserialize the resulting Buffer
var doc_2 = bson.deserialize(data)
console.log('doc_2:', doc_2)
```
## Installation
`npm install bson`
## API
### BSON types
For all BSON types documentation, please refer to the following sources:
* [MongoDB BSON Type Reference](https://docs.mongodb.com/manual/reference/bson-types/)
* [BSON Spec](https://bsonspec.org/)
### BSON serialization and deserialiation
**`new BSON()`** - Creates a new BSON serializer/deserializer you can use to serialize and deserialize BSON.
#### BSON.serialize
The BSON `serialize` method takes a JavaScript object and an optional options object and returns a Node.js Buffer.
* `BSON.serialize(object, options)`
* @param {Object} object the JavaScript object to serialize.
* @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid.
* @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
* @param {Boolean} [options.ignoreUndefined=true]
* @return {Buffer} returns a Buffer instance.
#### BSON.serializeWithBufferAndIndex
The BSON `serializeWithBufferAndIndex` method takes an object, a target buffer instance and an optional options object and returns the end serialization index in the final buffer.
* `BSON.serializeWithBufferAndIndex(object, buffer, options)`
* @param {Object} object the JavaScript object to serialize.
* @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
* @param {Boolean} [options.checkKeys=false] the serializer will check if keys are valid.
* @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields.
* @param {Number} [options.index=0] the index in the buffer where we wish to start serializing into.
* @return {Number} returns the index pointing to the last written byte in the buffer.
#### BSON.calculateObjectSize
The BSON `calculateObjectSize` method takes a JavaScript object and an optional options object and returns the size of the BSON object.
* `BSON.calculateObjectSize(object, options)`
* @param {Object} object the JavaScript object to serialize.
* @param {Boolean} [options.serializeFunctions=false] serialize the JavaScript functions.
* @param {Boolean} [options.ignoreUndefined=true]
* @return {Buffer} returns a Buffer instance.
#### BSON.deserialize
The BSON `deserialize` method takes a Node.js Buffer and an optional options object and returns a deserialized JavaScript object.
* `BSON.deserialize(buffer, options)`
* @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
* @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
* @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
* @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
* @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
* @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
* @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
* @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
* @return {Object} returns the deserialized Javascript Object.
#### BSON.deserializeStream
The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and allow more control over deserialization of a Buffer containing concatenated BSON documents.
* `BSON.deserializeStream(buffer, startIndex, numberOfDocuments, documents, docStartIndex, options)`
* @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
* @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
* @param {Number} numberOfDocuments number of documents to deserialize.
* @param {Array} documents an array where to store the deserialized documents.
* @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
* @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
* @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
* @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
* @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
* @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a Node.js Buffer instance.
* @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
* @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
* @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
* @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
## FAQ
#### Why does `undefined` get converted to `null`?
The `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys.
#### How do I add custom serialization logic?
This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.
```javascript
var bson = new BSON();
class CustomSerialize {
toBSON() {
return 42;
}
}
const obj = { answer: new CustomSerialize() };
// "{ answer: 42 }"
console.log(bson.deserialize(bson.serialize(obj)));
```
-25
View File
@@ -1,25 +0,0 @@
{
"name": "bson",
"description": "A bson parser for node.js and the browser",
"keywords": [
"mongodb",
"bson",
"parser"
],
"author": "Christian Amor Kvalheim <christkv@gmail.com>",
"main": "./browser_build/bson.js",
"license": "Apache-2.0",
"moduleType": [
"globals",
"node"
],
"ignore": [
"**/.*",
"alternate_parsers",
"benchmarks",
"bower_components",
"node_modules",
"test",
"tools"
]
}
File diff suppressed because it is too large Load Diff
@@ -1,8 +0,0 @@
{ "name" : "bson"
, "description" : "A bson parser for node.js and the browser"
, "main": "../"
, "directories" : { "lib" : "../lib/bson" }
, "engines" : { "node" : ">=0.6.0" }
, "licenses" : [ { "type" : "Apache License, Version 2.0"
, "url" : "http://www.apache.org/licenses/LICENSE-2.0" } ]
}
-46
View File
@@ -1,46 +0,0 @@
var BSON = require('./lib/bson/bson'),
Binary = require('./lib/bson/binary'),
Code = require('./lib/bson/code'),
DBRef = require('./lib/bson/db_ref'),
Decimal128 = require('./lib/bson/decimal128'),
Double = require('./lib/bson/double'),
Int32 = require('./lib/bson/int_32'),
Long = require('./lib/bson/long'),
Map = require('./lib/bson/map'),
MaxKey = require('./lib/bson/max_key'),
MinKey = require('./lib/bson/min_key'),
ObjectId = require('./lib/bson/objectid'),
BSONRegExp = require('./lib/bson/regexp'),
Symbol = require('./lib/bson/symbol'),
Timestamp = require('./lib/bson/timestamp');
// BSON MAX VALUES
BSON.BSON_INT32_MAX = 0x7fffffff;
BSON.BSON_INT32_MIN = -0x80000000;
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
// JS MAX PRECISE VALUES
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
// Add BSON types to function creation
BSON.Binary = Binary;
BSON.Code = Code;
BSON.DBRef = DBRef;
BSON.Decimal128 = Decimal128;
BSON.Double = Double;
BSON.Int32 = Int32;
BSON.Long = Long;
BSON.Map = Map;
BSON.MaxKey = MaxKey;
BSON.MinKey = MinKey;
BSON.ObjectId = ObjectId;
BSON.ObjectID = ObjectId;
BSON.BSONRegExp = BSONRegExp;
BSON.Symbol = Symbol;
BSON.Timestamp = Timestamp;
// Return the BSON
module.exports = BSON;
-382
View File
@@ -1,382 +0,0 @@
/**
* Module dependencies.
* @ignore
*/
// Test if we're in Node via presence of "global" not absence of "window"
// to support hybrid environments like Electron
if (typeof global !== 'undefined') {
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
}
/**
* A class representation of the BSON Binary type.
*
* Sub types
* - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type.
* - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type.
* - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type.
* - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type.
* - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type.
* - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type.
*
* @class
* @param {Buffer} buffer a buffer object containing the binary data.
* @param {Number} [subType] the option binary type.
* @return {Binary}
*/
function Binary(buffer, subType) {
if (!(this instanceof Binary)) return new Binary(buffer, subType);
if (
buffer != null &&
!(typeof buffer === 'string') &&
!Buffer.isBuffer(buffer) &&
!(buffer instanceof Uint8Array) &&
!Array.isArray(buffer)
) {
throw new Error('only String, Buffer, Uint8Array or Array accepted');
}
this._bsontype = 'Binary';
if (buffer instanceof Number) {
this.sub_type = buffer;
this.position = 0;
} else {
this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
this.position = 0;
}
if (buffer != null && !(buffer instanceof Number)) {
// Only accept Buffer, Uint8Array or Arrays
if (typeof buffer === 'string') {
// Different ways of writing the length of the string for the different types
if (typeof Buffer !== 'undefined') {
this.buffer = new Buffer(buffer);
} else if (
typeof Uint8Array !== 'undefined' ||
Object.prototype.toString.call(buffer) === '[object Array]'
) {
this.buffer = writeStringToArray(buffer);
} else {
throw new Error('only String, Buffer, Uint8Array or Array accepted');
}
} else {
this.buffer = buffer;
}
this.position = buffer.length;
} else {
if (typeof Buffer !== 'undefined') {
this.buffer = new Buffer(Binary.BUFFER_SIZE);
} else if (typeof Uint8Array !== 'undefined') {
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
} else {
this.buffer = new Array(Binary.BUFFER_SIZE);
}
// Set position to start of buffer
this.position = 0;
}
}
/**
* Updates this binary with byte_value.
*
* @method
* @param {string} byte_value a single byte we wish to write.
*/
Binary.prototype.put = function put(byte_value) {
// If it's a string and a has more than one character throw an error
if (byte_value['length'] != null && typeof byte_value !== 'number' && byte_value.length !== 1)
throw new Error('only accepts single character String, Uint8Array or Array');
if ((typeof byte_value !== 'number' && byte_value < 0) || byte_value > 255)
throw new Error('only accepts number in a valid unsigned byte range 0-255');
// Decode the byte value once
var decoded_byte = null;
if (typeof byte_value === 'string') {
decoded_byte = byte_value.charCodeAt(0);
} else if (byte_value['length'] != null) {
decoded_byte = byte_value[0];
} else {
decoded_byte = byte_value;
}
if (this.buffer.length > this.position) {
this.buffer[this.position++] = decoded_byte;
} else {
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
// Create additional overflow buffer
var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
this.buffer[this.position++] = decoded_byte;
} else {
buffer = null;
// Create a new buffer (typed or normal array)
if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length));
} else {
buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length);
}
// We need to copy all the content to the new array
for (var i = 0; i < this.buffer.length; i++) {
buffer[i] = this.buffer[i];
}
// Reassign the buffer
this.buffer = buffer;
// Write the byte
this.buffer[this.position++] = decoded_byte;
}
}
};
/**
* Writes a buffer or string to the binary.
*
* @method
* @param {(Buffer|string)} string a string or buffer to be written to the Binary BSON object.
* @param {number} offset specify the binary of where to write the content.
* @return {null}
*/
Binary.prototype.write = function write(string, offset) {
offset = typeof offset === 'number' ? offset : this.position;
// If the buffer is to small let's extend the buffer
if (this.buffer.length < offset + string.length) {
var buffer = null;
// If we are in node.js
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
buffer = new Buffer(this.buffer.length + string.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
} else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
// Create a new buffer
buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length));
// Copy the content
for (var i = 0; i < this.position; i++) {
buffer[i] = this.buffer[i];
}
}
// Assign the new buffer
this.buffer = buffer;
}
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) {
string.copy(this.buffer, offset, 0, string.length);
this.position = offset + string.length > this.position ? offset + string.length : this.position;
// offset = string.length
} else if (
typeof Buffer !== 'undefined' &&
typeof string === 'string' &&
Buffer.isBuffer(this.buffer)
) {
this.buffer.write(string, offset, 'binary');
this.position = offset + string.length > this.position ? offset + string.length : this.position;
// offset = string.length;
} else if (
Object.prototype.toString.call(string) === '[object Uint8Array]' ||
(Object.prototype.toString.call(string) === '[object Array]' && typeof string !== 'string')
) {
for (i = 0; i < string.length; i++) {
this.buffer[offset++] = string[i];
}
this.position = offset > this.position ? offset : this.position;
} else if (typeof string === 'string') {
for (i = 0; i < string.length; i++) {
this.buffer[offset++] = string.charCodeAt(i);
}
this.position = offset > this.position ? offset : this.position;
}
};
/**
* Reads **length** bytes starting at **position**.
*
* @method
* @param {number} position read from the given position in the Binary.
* @param {number} length the number of bytes to read.
* @return {Buffer}
*/
Binary.prototype.read = function read(position, length) {
length = length && length > 0 ? length : this.position;
// Let's return the data based on the type we have
if (this.buffer['slice']) {
return this.buffer.slice(position, position + length);
} else {
// Create a buffer to keep the result
var buffer =
typeof Uint8Array !== 'undefined'
? new Uint8Array(new ArrayBuffer(length))
: new Array(length);
for (var i = 0; i < length; i++) {
buffer[i] = this.buffer[position++];
}
}
// Return the buffer
return buffer;
};
/**
* Returns the value of this binary as a string.
*
* @method
* @return {string}
*/
Binary.prototype.value = function value(asRaw) {
asRaw = asRaw == null ? false : asRaw;
// Optimize to serialize for the situation where the data == size of buffer
if (
asRaw &&
typeof Buffer !== 'undefined' &&
Buffer.isBuffer(this.buffer) &&
this.buffer.length === this.position
)
return this.buffer;
// If it's a node.js buffer object
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
return asRaw
? this.buffer.slice(0, this.position)
: this.buffer.toString('binary', 0, this.position);
} else {
if (asRaw) {
// we support the slice command use it
if (this.buffer['slice'] != null) {
return this.buffer.slice(0, this.position);
} else {
// Create a new buffer to copy content to
var newBuffer =
Object.prototype.toString.call(this.buffer) === '[object Uint8Array]'
? new Uint8Array(new ArrayBuffer(this.position))
: new Array(this.position);
// Copy content
for (var i = 0; i < this.position; i++) {
newBuffer[i] = this.buffer[i];
}
// Return the buffer
return newBuffer;
}
} else {
return convertArraytoUtf8BinaryString(this.buffer, 0, this.position);
}
}
};
/**
* Length.
*
* @method
* @return {number} the length of the binary.
*/
Binary.prototype.length = function length() {
return this.position;
};
/**
* @ignore
*/
Binary.prototype.toJSON = function() {
return this.buffer != null ? this.buffer.toString('base64') : '';
};
/**
* @ignore
*/
Binary.prototype.toString = function(format) {
return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : '';
};
/**
* Binary default subtype
* @ignore
*/
var BSON_BINARY_SUBTYPE_DEFAULT = 0;
/**
* @ignore
*/
var writeStringToArray = function(data) {
// Create a buffer
var buffer =
typeof Uint8Array !== 'undefined'
? new Uint8Array(new ArrayBuffer(data.length))
: new Array(data.length);
// Write the content to the buffer
for (var i = 0; i < data.length; i++) {
buffer[i] = data.charCodeAt(i);
}
// Write the string to the buffer
return buffer;
};
/**
* Convert Array ot Uint8Array to Binary String
*
* @ignore
*/
var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
var result = '';
for (var i = startIndex; i < endIndex; i++) {
result = result + String.fromCharCode(byteArray[i]);
}
return result;
};
Binary.BUFFER_SIZE = 256;
/**
* Default BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_DEFAULT = 0;
/**
* Function BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_FUNCTION = 1;
/**
* Byte Array BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_BYTE_ARRAY = 2;
/**
* OLD UUID BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_UUID_OLD = 3;
/**
* UUID BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_UUID = 4;
/**
* MD5 BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_MD5 = 5;
/**
* User BSON type
*
* @classconstant SUBTYPE_DEFAULT
**/
Binary.SUBTYPE_USER_DEFINED = 128;
/**
* Expose.
*/
module.exports = Binary;
module.exports.Binary = Binary;
-385
View File
@@ -1,385 +0,0 @@
'use strict';
var Map = require('./map'),
Long = require('./long'),
Double = require('./double'),
Timestamp = require('./timestamp'),
ObjectID = require('./objectid'),
BSONRegExp = require('./regexp'),
Symbol = require('./symbol'),
Int32 = require('./int_32'),
Code = require('./code'),
Decimal128 = require('./decimal128'),
MinKey = require('./min_key'),
MaxKey = require('./max_key'),
DBRef = require('./db_ref'),
Binary = require('./binary');
// Parts of the parser
var deserialize = require('./parser/deserializer'),
serializer = require('./parser/serializer'),
calculateObjectSize = require('./parser/calculate_size');
/**
* @ignore
* @api private
*/
// Default Max Size
var MAXSIZE = 1024 * 1024 * 17;
// Current Internal Temporary Serialization Buffer
var buffer = new Buffer(MAXSIZE);
var BSON = function() {};
/**
* Serialize a Javascript object.
*
* @param {Object} object the Javascript object to serialize.
* @param {Boolean} [options.checkKeys] the serializer will check if keys are valid.
* @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
* @param {Number} [options.minInternalBufferSize=1024*1024*17] minimum size of the internal temporary serialization buffer **(default:1024*1024*17)**.
* @return {Buffer} returns the Buffer object containing the serialized object.
* @api public
*/
BSON.prototype.serialize = function serialize(object, options) {
options = options || {};
// Unpack the options
var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
var serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
var minInternalBufferSize =
typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
// Resize the internal serialization buffer if needed
if (buffer.length < minInternalBufferSize) {
buffer = new Buffer(minInternalBufferSize);
}
// Attempt to serialize
var serializationIndex = serializer(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined,
[]
);
// Create the final buffer
var finishedBuffer = new Buffer(serializationIndex);
// Copy into the finished buffer
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
// Return the buffer
return finishedBuffer;
};
/**
* Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
*
* @param {Object} object the Javascript object to serialize.
* @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
* @param {Boolean} [options.checkKeys] the serializer will check if keys are valid.
* @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
* @param {Number} [options.index] the index in the buffer where we wish to start serializing into.
* @return {Number} returns the index pointing to the last written byte in the buffer.
* @api public
*/
BSON.prototype.serializeWithBufferAndIndex = function(object, finalBuffer, options) {
options = options || {};
// Unpack the options
var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
var serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
var startIndex = typeof options.index === 'number' ? options.index : 0;
// Attempt to serialize
var serializationIndex = serializer(
finalBuffer,
object,
checkKeys,
startIndex || 0,
0,
serializeFunctions,
ignoreUndefined
);
// Return the index
return serializationIndex - 1;
};
/**
* Deserialize data as BSON.
*
* @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
* @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
* @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
* @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
* @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
* @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a node.js Buffer instance.
* @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
* @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
* @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
* @return {Object} returns the deserialized Javascript Object.
* @api public
*/
BSON.prototype.deserialize = function(buffer, options) {
return deserialize(buffer, options);
};
/**
* Calculate the bson size for a passed in Javascript object.
*
* @param {Object} object the Javascript object to calculate the BSON byte size for.
* @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
* @return {Number} returns the number of bytes the BSON object will take up.
* @api public
*/
BSON.prototype.calculateObjectSize = function(object, options) {
options = options || {};
var serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
return calculateObjectSize(object, serializeFunctions, ignoreUndefined);
};
/**
* Deserialize stream data as BSON documents.
*
* @param {Buffer} data the buffer containing the serialized set of BSON documents.
* @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
* @param {Number} numberOfDocuments number of documents to deserialize.
* @param {Array} documents an array where to store the deserialized documents.
* @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
* @param {Object} [options] additional options used for the deserialization.
* @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
* @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
* @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
* @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
* @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a node.js Buffer instance.
* @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
* @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
* @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
* @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
* @api public
*/
BSON.prototype.deserializeStream = function(
data,
startIndex,
numberOfDocuments,
documents,
docStartIndex,
options
) {
options = options != null ? options : {};
var index = startIndex;
// Loop over all documents
for (var i = 0; i < numberOfDocuments; i++) {
// Find size of the document
var size =
data[index] | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24);
// Update options with index
options['index'] = index;
// Parse the document at this point
documents[docStartIndex + i] = this.deserialize(data, options);
// Adjust index by the document size
index = index + size;
}
// Return object containing end index of parsing and list of documents
return index;
};
/**
* @ignore
* @api private
*/
// BSON MAX VALUES
BSON.BSON_INT32_MAX = 0x7fffffff;
BSON.BSON_INT32_MIN = -0x80000000;
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
// JS MAX PRECISE VALUES
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
// Internal long versions
// var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
// var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
/**
* Number BSON Type
*
* @classconstant BSON_DATA_NUMBER
**/
BSON.BSON_DATA_NUMBER = 1;
/**
* String BSON Type
*
* @classconstant BSON_DATA_STRING
**/
BSON.BSON_DATA_STRING = 2;
/**
* Object BSON Type
*
* @classconstant BSON_DATA_OBJECT
**/
BSON.BSON_DATA_OBJECT = 3;
/**
* Array BSON Type
*
* @classconstant BSON_DATA_ARRAY
**/
BSON.BSON_DATA_ARRAY = 4;
/**
* Binary BSON Type
*
* @classconstant BSON_DATA_BINARY
**/
BSON.BSON_DATA_BINARY = 5;
/**
* ObjectID BSON Type
*
* @classconstant BSON_DATA_OID
**/
BSON.BSON_DATA_OID = 7;
/**
* Boolean BSON Type
*
* @classconstant BSON_DATA_BOOLEAN
**/
BSON.BSON_DATA_BOOLEAN = 8;
/**
* Date BSON Type
*
* @classconstant BSON_DATA_DATE
**/
BSON.BSON_DATA_DATE = 9;
/**
* null BSON Type
*
* @classconstant BSON_DATA_NULL
**/
BSON.BSON_DATA_NULL = 10;
/**
* RegExp BSON Type
*
* @classconstant BSON_DATA_REGEXP
**/
BSON.BSON_DATA_REGEXP = 11;
/**
* Code BSON Type
*
* @classconstant BSON_DATA_CODE
**/
BSON.BSON_DATA_CODE = 13;
/**
* Symbol BSON Type
*
* @classconstant BSON_DATA_SYMBOL
**/
BSON.BSON_DATA_SYMBOL = 14;
/**
* Code with Scope BSON Type
*
* @classconstant BSON_DATA_CODE_W_SCOPE
**/
BSON.BSON_DATA_CODE_W_SCOPE = 15;
/**
* 32 bit Integer BSON Type
*
* @classconstant BSON_DATA_INT
**/
BSON.BSON_DATA_INT = 16;
/**
* Timestamp BSON Type
*
* @classconstant BSON_DATA_TIMESTAMP
**/
BSON.BSON_DATA_TIMESTAMP = 17;
/**
* Long BSON Type
*
* @classconstant BSON_DATA_LONG
**/
BSON.BSON_DATA_LONG = 18;
/**
* MinKey BSON Type
*
* @classconstant BSON_DATA_MIN_KEY
**/
BSON.BSON_DATA_MIN_KEY = 0xff;
/**
* MaxKey BSON Type
*
* @classconstant BSON_DATA_MAX_KEY
**/
BSON.BSON_DATA_MAX_KEY = 0x7f;
/**
* Binary Default Type
*
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
**/
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
/**
* Binary Function Type
*
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
**/
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
/**
* Binary Byte Array Type
*
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
**/
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
/**
* Binary UUID Type
*
* @classconstant BSON_BINARY_SUBTYPE_UUID
**/
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
/**
* Binary MD5 Type
*
* @classconstant BSON_BINARY_SUBTYPE_MD5
**/
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
/**
* Binary User Defined Type
*
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
**/
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
// Return BSON
module.exports = BSON;
module.exports.Code = Code;
module.exports.Map = Map;
module.exports.Symbol = Symbol;
module.exports.BSON = BSON;
module.exports.DBRef = DBRef;
module.exports.Binary = Binary;
module.exports.ObjectID = ObjectID;
module.exports.Long = Long;
module.exports.Timestamp = Timestamp;
module.exports.Double = Double;
module.exports.Int32 = Int32;
module.exports.MinKey = MinKey;
module.exports.MaxKey = MaxKey;
module.exports.BSONRegExp = BSONRegExp;
module.exports.Decimal128 = Decimal128;
-24
View File
@@ -1,24 +0,0 @@
/**
* A class representation of the BSON Code type.
*
* @class
* @param {(string|function)} code a string or function.
* @param {Object} [scope] an optional scope for the function.
* @return {Code}
*/
var Code = function Code(code, scope) {
if (!(this instanceof Code)) return new Code(code, scope);
this._bsontype = 'Code';
this.code = code;
this.scope = scope;
};
/**
* @ignore
*/
Code.prototype.toJSON = function() {
return { scope: this.scope, code: this.code };
};
module.exports = Code;
module.exports.Code = Code;
-32
View File
@@ -1,32 +0,0 @@
/**
* A class representation of the BSON DBRef type.
*
* @class
* @param {string} namespace the collection name.
* @param {ObjectID} oid the reference ObjectID.
* @param {string} [db] optional db name, if omitted the reference is local to the current db.
* @return {DBRef}
*/
function DBRef(namespace, oid, db) {
if (!(this instanceof DBRef)) return new DBRef(namespace, oid, db);
this._bsontype = 'DBRef';
this.namespace = namespace;
this.oid = oid;
this.db = db;
}
/**
* @ignore
* @api private
*/
DBRef.prototype.toJSON = function() {
return {
$ref: this.namespace,
$id: this.oid,
$db: this.db == null ? '' : this.db
};
};
module.exports = DBRef;
module.exports.DBRef = DBRef;
-818
View File
@@ -1,818 +0,0 @@
'use strict';
var Long = require('./long');
var PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
var PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
var PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i;
var EXPONENT_MAX = 6111;
var EXPONENT_MIN = -6176;
var EXPONENT_BIAS = 6176;
var MAX_DIGITS = 34;
// Nan value bits as 32 bit values (due to lack of longs)
var NAN_BUFFER = [
0x7c,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
].reverse();
// Infinity value bits 32 bit values (due to lack of longs)
var INF_NEGATIVE_BUFFER = [
0xf8,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
].reverse();
var INF_POSITIVE_BUFFER = [
0x78,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
].reverse();
var EXPONENT_REGEX = /^([-+])?(\d+)?$/;
// Detect if the value is a digit
var isDigit = function(value) {
return !isNaN(parseInt(value, 10));
};
// Divide two uint128 values
var divideu128 = function(value) {
var DIVISOR = Long.fromNumber(1000 * 1000 * 1000);
var _rem = Long.fromNumber(0);
var i = 0;
if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
return { quotient: value, rem: _rem };
}
for (i = 0; i <= 3; i++) {
// Adjust remainder to match value of next dividend
_rem = _rem.shiftLeft(32);
// Add the divided to _rem
_rem = _rem.add(new Long(value.parts[i], 0));
value.parts[i] = _rem.div(DIVISOR).low_;
_rem = _rem.modulo(DIVISOR);
}
return { quotient: value, rem: _rem };
};
// Multiply two Long values and return the 128 bit value
var multiply64x2 = function(left, right) {
if (!left && !right) {
return { high: Long.fromNumber(0), low: Long.fromNumber(0) };
}
var leftHigh = left.shiftRightUnsigned(32);
var leftLow = new Long(left.getLowBits(), 0);
var rightHigh = right.shiftRightUnsigned(32);
var rightLow = new Long(right.getLowBits(), 0);
var productHigh = leftHigh.multiply(rightHigh);
var productMid = leftHigh.multiply(rightLow);
var productMid2 = leftLow.multiply(rightHigh);
var productLow = leftLow.multiply(rightLow);
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productMid = new Long(productMid.getLowBits(), 0)
.add(productMid2)
.add(productLow.shiftRightUnsigned(32));
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0));
// Return the 128 bit result
return { high: productHigh, low: productLow };
};
var lessThan = function(left, right) {
// Make values unsigned
var uhleft = left.high_ >>> 0;
var uhright = right.high_ >>> 0;
// Compare high bits first
if (uhleft < uhright) {
return true;
} else if (uhleft === uhright) {
var ulleft = left.low_ >>> 0;
var ulright = right.low_ >>> 0;
if (ulleft < ulright) return true;
}
return false;
};
// var longtoHex = function(value) {
// var buffer = new Buffer(8);
// var index = 0;
// // Encode the low 64 bits of the decimal
// // Encode low bits
// buffer[index++] = value.low_ & 0xff;
// buffer[index++] = (value.low_ >> 8) & 0xff;
// buffer[index++] = (value.low_ >> 16) & 0xff;
// buffer[index++] = (value.low_ >> 24) & 0xff;
// // Encode high bits
// buffer[index++] = value.high_ & 0xff;
// buffer[index++] = (value.high_ >> 8) & 0xff;
// buffer[index++] = (value.high_ >> 16) & 0xff;
// buffer[index++] = (value.high_ >> 24) & 0xff;
// return buffer.reverse().toString('hex');
// };
// var int32toHex = function(value) {
// var buffer = new Buffer(4);
// var index = 0;
// // Encode the low 64 bits of the decimal
// // Encode low bits
// buffer[index++] = value & 0xff;
// buffer[index++] = (value >> 8) & 0xff;
// buffer[index++] = (value >> 16) & 0xff;
// buffer[index++] = (value >> 24) & 0xff;
// return buffer.reverse().toString('hex');
// };
/**
* A class representation of the BSON Decimal128 type.
*
* @class
* @param {Buffer} bytes a buffer containing the raw Decimal128 bytes.
* @return {Double}
*/
var Decimal128 = function(bytes) {
this._bsontype = 'Decimal128';
this.bytes = bytes;
};
/**
* Create a Decimal128 instance from a string representation
*
* @method
* @param {string} string a numeric string representation.
* @return {Decimal128} returns a Decimal128 instance.
*/
Decimal128.fromString = function(string) {
// Parse state tracking
var isNegative = false;
var sawRadix = false;
var foundNonZero = false;
// Total number of significant digits (no leading or trailing zero)
var significantDigits = 0;
// Total number of significand digits read
var nDigitsRead = 0;
// Total number of digits (no leading zeros)
var nDigits = 0;
// The number of the digits after radix
var radixPosition = 0;
// The index of the first non-zero in *str*
var firstNonZero = 0;
// Digits Array
var digits = [0];
// The number of digits in digits
var nDigitsStored = 0;
// Insertion pointer for digits
var digitsInsert = 0;
// The index of the first non-zero digit
var firstDigit = 0;
// The index of the last digit
var lastDigit = 0;
// Exponent
var exponent = 0;
// loop index over array
var i = 0;
// The high 17 digits of the significand
var significandHigh = [0, 0];
// The low 17 digits of the significand
var significandLow = [0, 0];
// The biased exponent
var biasedExponent = 0;
// Read index
var index = 0;
// Trim the string
string = string.trim();
// Naively prevent against REDOS attacks.
// TODO: implementing a custom parsing for this, or refactoring the regex would yield
// further gains.
if (string.length >= 7000) {
throw new Error('' + string + ' not a valid Decimal128 string');
}
// Results
var stringMatch = string.match(PARSE_STRING_REGEXP);
var infMatch = string.match(PARSE_INF_REGEXP);
var nanMatch = string.match(PARSE_NAN_REGEXP);
// Validate the string
if ((!stringMatch && !infMatch && !nanMatch) || string.length === 0) {
throw new Error('' + string + ' not a valid Decimal128 string');
}
// Check if we have an illegal exponent format
if (stringMatch && stringMatch[4] && stringMatch[2] === undefined) {
throw new Error('' + string + ' not a valid Decimal128 string');
}
// Get the negative or positive sign
if (string[index] === '+' || string[index] === '-') {
isNegative = string[index++] === '-';
}
// Check if user passed Infinity or NaN
if (!isDigit(string[index]) && string[index] !== '.') {
if (string[index] === 'i' || string[index] === 'I') {
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
} else if (string[index] === 'N') {
return new Decimal128(new Buffer(NAN_BUFFER));
}
}
// Read all the digits
while (isDigit(string[index]) || string[index] === '.') {
if (string[index] === '.') {
if (sawRadix) {
return new Decimal128(new Buffer(NAN_BUFFER));
}
sawRadix = true;
index = index + 1;
continue;
}
if (nDigitsStored < 34) {
if (string[index] !== '0' || foundNonZero) {
if (!foundNonZero) {
firstNonZero = nDigitsRead;
}
foundNonZero = true;
// Only store 34 digits
digits[digitsInsert++] = parseInt(string[index], 10);
nDigitsStored = nDigitsStored + 1;
}
}
if (foundNonZero) {
nDigits = nDigits + 1;
}
if (sawRadix) {
radixPosition = radixPosition + 1;
}
nDigitsRead = nDigitsRead + 1;
index = index + 1;
}
if (sawRadix && !nDigitsRead) {
throw new Error('' + string + ' not a valid Decimal128 string');
}
// Read exponent if exists
if (string[index] === 'e' || string[index] === 'E') {
// Read exponent digits
var match = string.substr(++index).match(EXPONENT_REGEX);
// No digits read
if (!match || !match[2]) {
return new Decimal128(new Buffer(NAN_BUFFER));
}
// Get exponent
exponent = parseInt(match[0], 10);
// Adjust the index
index = index + match[0].length;
}
// Return not a number
if (string[index]) {
return new Decimal128(new Buffer(NAN_BUFFER));
}
// Done reading input
// Find first non-zero digit in digits
firstDigit = 0;
if (!nDigitsStored) {
firstDigit = 0;
lastDigit = 0;
digits[0] = 0;
nDigits = 1;
nDigitsStored = 1;
significantDigits = 0;
} else {
lastDigit = nDigitsStored - 1;
significantDigits = nDigits;
if (exponent !== 0 && significantDigits !== 1) {
while (string[firstNonZero + significantDigits - 1] === '0') {
significantDigits = significantDigits - 1;
}
}
}
// Normalization of exponent
// Correct exponent based on radix position, and shift significand as needed
// to represent user input
// Overflow prevention
if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
exponent = EXPONENT_MIN;
} else {
exponent = exponent - radixPosition;
}
// Attempt to normalize the exponent
while (exponent > EXPONENT_MAX) {
// Shift exponent to significand and decrease
lastDigit = lastDigit + 1;
if (lastDigit - firstDigit > MAX_DIGITS) {
// Check if we have a zero then just hard clamp, otherwise fail
var digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
} else {
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
}
}
exponent = exponent - 1;
}
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
// Shift last digit
if (lastDigit === 0) {
exponent = EXPONENT_MIN;
significantDigits = 0;
break;
}
if (nDigitsStored < nDigits) {
// adjust to match digits not stored
nDigits = nDigits - 1;
} else {
// adjust to round
lastDigit = lastDigit - 1;
}
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
} else {
// Check if we have a zero then just hard clamp, otherwise fail
digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
} else {
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
}
}
}
// Round
// We've normalized the exponent, but might still need to round.
if (lastDigit - firstDigit + 1 < significantDigits && string[significantDigits] !== '0') {
var endOfString = nDigitsRead;
// If we have seen a radix point, 'string' is 1 longer than we have
// documented with ndigits_read, so inc the position of the first nonzero
// digit and the position that digits are read to.
if (sawRadix && exponent === EXPONENT_MIN) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
var roundDigit = parseInt(string[firstNonZero + lastDigit + 1], 10);
var roundBit = 0;
if (roundDigit >= 5) {
roundBit = 1;
if (roundDigit === 5) {
roundBit = digits[lastDigit] % 2 === 1;
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
if (parseInt(string[i], 10)) {
roundBit = 1;
break;
}
}
}
}
if (roundBit) {
var dIdx = lastDigit;
for (; dIdx >= 0; dIdx--) {
if (++digits[dIdx] > 9) {
digits[dIdx] = 0;
// overflowed most significant digit
if (dIdx === 0) {
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
digits[dIdx] = 1;
} else {
return new Decimal128(
new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
);
}
}
} else {
break;
}
}
}
}
// Encode significand
// The high 17 digits of the significand
significandHigh = Long.fromNumber(0);
// The low 17 digits of the significand
significandLow = Long.fromNumber(0);
// read a zero
if (significantDigits === 0) {
significandHigh = Long.fromNumber(0);
significandLow = Long.fromNumber(0);
} else if (lastDigit - firstDigit < 17) {
dIdx = firstDigit;
significandLow = Long.fromNumber(digits[dIdx++]);
significandHigh = new Long(0, 0);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(Long.fromNumber(10));
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
}
} else {
dIdx = firstDigit;
significandHigh = Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit - 17; dIdx++) {
significandHigh = significandHigh.multiply(Long.fromNumber(10));
significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx]));
}
significandLow = Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(Long.fromNumber(10));
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
}
}
var significand = multiply64x2(significandHigh, Long.fromString('100000000000000000'));
significand.low = significand.low.add(significandLow);
if (lessThan(significand.low, significandLow)) {
significand.high = significand.high.add(Long.fromNumber(1));
}
// Biased exponent
biasedExponent = exponent + EXPONENT_BIAS;
var dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) };
// Encode combination, exponent, and significand.
if (
significand.high
.shiftRightUnsigned(49)
.and(Long.fromNumber(1))
.equals(Long.fromNumber)
) {
// Encode '11' into bits 1 to 3
dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61));
dec.high = dec.high.or(
Long.fromNumber(biasedExponent).and(Long.fromNumber(0x3fff).shiftLeft(47))
);
dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x7fffffffffff)));
} else {
dec.high = dec.high.or(Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49));
dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x1ffffffffffff)));
}
dec.low = significand.low;
// Encode sign
if (isNegative) {
dec.high = dec.high.or(Long.fromString('9223372036854775808'));
}
// Encode into a buffer
var buffer = new Buffer(16);
index = 0;
// Encode the low 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.low.low_ & 0xff;
buffer[index++] = (dec.low.low_ >> 8) & 0xff;
buffer[index++] = (dec.low.low_ >> 16) & 0xff;
buffer[index++] = (dec.low.low_ >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.low.high_ & 0xff;
buffer[index++] = (dec.low.high_ >> 8) & 0xff;
buffer[index++] = (dec.low.high_ >> 16) & 0xff;
buffer[index++] = (dec.low.high_ >> 24) & 0xff;
// Encode the high 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.high.low_ & 0xff;
buffer[index++] = (dec.high.low_ >> 8) & 0xff;
buffer[index++] = (dec.high.low_ >> 16) & 0xff;
buffer[index++] = (dec.high.low_ >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.high.high_ & 0xff;
buffer[index++] = (dec.high.high_ >> 8) & 0xff;
buffer[index++] = (dec.high.high_ >> 16) & 0xff;
buffer[index++] = (dec.high.high_ >> 24) & 0xff;
// Return the new Decimal128
return new Decimal128(buffer);
};
// Extract least significant 5 bits
var COMBINATION_MASK = 0x1f;
// Extract least significant 14 bits
var EXPONENT_MASK = 0x3fff;
// Value of combination field for Inf
var COMBINATION_INFINITY = 30;
// Value of combination field for NaN
var COMBINATION_NAN = 31;
// Value of combination field for NaN
// var COMBINATION_SNAN = 32;
// decimal128 exponent bias
EXPONENT_BIAS = 6176;
/**
* Create a string representation of the raw Decimal128 value
*
* @method
* @return {string} returns a Decimal128 string representation.
*/
Decimal128.prototype.toString = function() {
// Note: bits in this routine are referred to starting at 0,
// from the sign bit, towards the coefficient.
// bits 0 - 31
var high;
// bits 32 - 63
var midh;
// bits 64 - 95
var midl;
// bits 96 - 127
var low;
// bits 1 - 5
var combination;
// decoded biased exponent (14 bits)
var biased_exponent;
// the number of significand digits
var significand_digits = 0;
// the base-10 digits in the significand
var significand = new Array(36);
for (var i = 0; i < significand.length; i++) significand[i] = 0;
// read pointer into significand
var index = 0;
// unbiased exponent
var exponent;
// the exponent if scientific notation is used
var scientific_exponent;
// true if the number is zero
var is_zero = false;
// the most signifcant significand bits (50-46)
var significand_msb;
// temporary storage for significand decoding
var significand128 = { parts: new Array(4) };
// indexing variables
i;
var j, k;
// Output string
var string = [];
// Unpack index
index = 0;
// Buffer reference
var buffer = this.bytes;
// Unpack the low 64bits into a long
low =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
midl =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack the high 64bits into a long
midh =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
high =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack index
index = 0;
// Create the state of the decimal
var dec = {
low: new Long(low, midl),
high: new Long(midh, high)
};
if (dec.high.lessThan(Long.ZERO)) {
string.push('-');
}
// Decode combination field and exponent
combination = (high >> 26) & COMBINATION_MASK;
if (combination >> 3 === 3) {
// Check for 'special' values
if (combination === COMBINATION_INFINITY) {
return string.join('') + 'Infinity';
} else if (combination === COMBINATION_NAN) {
return 'NaN';
} else {
biased_exponent = (high >> 15) & EXPONENT_MASK;
significand_msb = 0x08 + ((high >> 14) & 0x01);
}
} else {
significand_msb = (high >> 14) & 0x07;
biased_exponent = (high >> 17) & EXPONENT_MASK;
}
exponent = biased_exponent - EXPONENT_BIAS;
// Create string of significand digits
// Convert the 114-bit binary number represented by
// (significand_high, significand_low) to at most 34 decimal
// digits through modulo and division.
significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14);
significand128.parts[1] = midh;
significand128.parts[2] = midl;
significand128.parts[3] = low;
if (
significand128.parts[0] === 0 &&
significand128.parts[1] === 0 &&
significand128.parts[2] === 0 &&
significand128.parts[3] === 0
) {
is_zero = true;
} else {
for (k = 3; k >= 0; k--) {
var least_digits = 0;
// Peform the divide
var result = divideu128(significand128);
significand128 = result.quotient;
least_digits = result.rem.low_;
// We now have the 9 least significant digits (in base 2).
// Convert and output to string.
if (!least_digits) continue;
for (j = 8; j >= 0; j--) {
// significand[k * 9 + j] = Math.round(least_digits % 10);
significand[k * 9 + j] = least_digits % 10;
// least_digits = Math.round(least_digits / 10);
least_digits = Math.floor(least_digits / 10);
}
}
}
// Output format options:
// Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd
// Regular - ddd.ddd
if (is_zero) {
significand_digits = 1;
significand[index] = 0;
} else {
significand_digits = 36;
i = 0;
while (!significand[index]) {
i++;
significand_digits = significand_digits - 1;
index = index + 1;
}
}
scientific_exponent = significand_digits - 1 + exponent;
// The scientific exponent checks are dictated by the string conversion
// specification and are somewhat arbitrary cutoffs.
//
// We must check exponent > 0, because if this is the case, the number
// has trailing zeros. However, we *cannot* output these trailing zeros,
// because doing so would change the precision of the value, and would
// change stored data if the string converted number is round tripped.
if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) {
// Scientific format
string.push(significand[index++]);
significand_digits = significand_digits - 1;
if (significand_digits) {
string.push('.');
}
for (i = 0; i < significand_digits; i++) {
string.push(significand[index++]);
}
// Exponent
string.push('E');
if (scientific_exponent > 0) {
string.push('+' + scientific_exponent);
} else {
string.push(scientific_exponent);
}
} else {
// Regular format with no decimal place
if (exponent >= 0) {
for (i = 0; i < significand_digits; i++) {
string.push(significand[index++]);
}
} else {
var radix_position = significand_digits + exponent;
// non-zero digits before radix
if (radix_position > 0) {
for (i = 0; i < radix_position; i++) {
string.push(significand[index++]);
}
} else {
string.push('0');
}
string.push('.');
// add leading zeros after radix
while (radix_position++ < 0) {
string.push('0');
}
for (i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
string.push(significand[index++]);
}
}
}
return string.join('');
};
Decimal128.prototype.toJSON = function() {
return { $numberDecimal: this.toString() };
};
module.exports = Decimal128;
module.exports.Decimal128 = Decimal128;
-33
View File
@@ -1,33 +0,0 @@
/**
* A class representation of the BSON Double type.
*
* @class
* @param {number} value the number we want to represent as a double.
* @return {Double}
*/
function Double(value) {
if (!(this instanceof Double)) return new Double(value);
this._bsontype = 'Double';
this.value = value;
}
/**
* Access the number value.
*
* @method
* @return {number} returns the wrapped double number.
*/
Double.prototype.valueOf = function() {
return this.value;
};
/**
* @ignore
*/
Double.prototype.toJSON = function() {
return this.value;
};
module.exports = Double;
module.exports.Double = Double;
-124
View File
@@ -1,124 +0,0 @@
// Copyright (c) 2008, Fair Oaks Labs, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
var readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
var e,
m,
bBE = endian === 'big',
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
nBits = -7,
i = bBE ? 0 : nBytes - 1,
d = bBE ? 1 : -1,
s = buffer[offset + i];
i += d;
e = s & ((1 << -nBits) - 1);
s >>= -nBits;
nBits += eLen;
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
m = e & ((1 << -nBits) - 1);
e >>= -nBits;
nBits += mLen;
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
if (e === 0) {
e = 1 - eBias;
} else if (e === eMax) {
return m ? NaN : (s ? -1 : 1) * Infinity;
} else {
m = m + Math.pow(2, mLen);
e = e - eBias;
}
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
};
var writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) {
var e,
m,
c,
bBE = endian === 'big',
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0,
i = bBE ? nBytes - 1 : 0,
d = bBE ? -1 : 1,
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
value = Math.abs(value);
if (isNaN(value) || value === Infinity) {
m = isNaN(value) ? 1 : 0;
e = eMax;
} else {
e = Math.floor(Math.log(value) / Math.LN2);
if (value * (c = Math.pow(2, -e)) < 1) {
e--;
c *= 2;
}
if (e + eBias >= 1) {
value += rt / c;
} else {
value += rt * Math.pow(2, 1 - eBias);
}
if (value * c >= 2) {
e++;
c /= 2;
}
if (e + eBias >= eMax) {
m = 0;
e = eMax;
} else if (e + eBias >= 1) {
m = (value * c - 1) * Math.pow(2, mLen);
e = e + eBias;
} else {
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
e = 0;
}
}
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
e = (e << mLen) | m;
eLen += mLen;
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
buffer[offset + i - d] |= s * 128;
};
exports.readIEEE754 = readIEEE754;
exports.writeIEEE754 = writeIEEE754;
-33
View File
@@ -1,33 +0,0 @@
/**
* A class representation of a BSON Int32 type.
*
* @class
* @param {number} value the number we want to represent as an int32.
* @return {Int32}
*/
var Int32 = function(value) {
if (!(this instanceof Int32)) return new Int32(value);
this._bsontype = 'Int32';
this.value = value;
};
/**
* Access the number value.
*
* @method
* @return {number} returns the wrapped int32 number.
*/
Int32.prototype.valueOf = function() {
return this.value;
};
/**
* @ignore
*/
Int32.prototype.toJSON = function() {
return this.value;
};
module.exports = Int32;
module.exports.Int32 = Int32;
-851
View File
@@ -1,851 +0,0 @@
// 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.
//
// Copyright 2009 Google Inc. All Rights Reserved
/**
* Defines a Long class for representing a 64-bit two's-complement
* integer value, which faithfully simulates the behavior of a Java "Long". This
* implementation is derived from LongLib in GWT.
*
* Constructs a 64-bit two's-complement integer, given its low and high 32-bit
* values as *signed* integers. See the from* functions below for more
* convenient ways of constructing Longs.
*
* The internal representation of a Long is the two given signed, 32-bit values.
* We use 32-bit pieces because these are the size of integers on which
* Javascript performs bit-operations. For operations like addition and
* multiplication, we split each number into 16-bit pieces, which can easily be
* multiplied within Javascript's floating-point representation without overflow
* or change in sign.
*
* In the algorithms below, we frequently reduce the negative case to the
* positive case by negating the input(s) and then post-processing the result.
* Note that we must ALWAYS check specially whether those values are MIN_VALUE
* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
* a positive number, it overflows back into a negative). Not handling this
* case would often result in infinite recursion.
*
* @class
* @param {number} low the low (signed) 32 bits of the Long.
* @param {number} high the high (signed) 32 bits of the Long.
* @return {Long}
*/
function Long(low, high) {
if (!(this instanceof Long)) return new Long(low, high);
this._bsontype = 'Long';
/**
* @type {number}
* @ignore
*/
this.low_ = low | 0; // force into 32 signed bits.
/**
* @type {number}
* @ignore
*/
this.high_ = high | 0; // force into 32 signed bits.
}
/**
* Return the int value.
*
* @method
* @return {number} the value, assuming it is a 32-bit integer.
*/
Long.prototype.toInt = function() {
return this.low_;
};
/**
* Return the Number value.
*
* @method
* @return {number} the closest floating-point representation to this value.
*/
Long.prototype.toNumber = function() {
return this.high_ * Long.TWO_PWR_32_DBL_ + this.getLowBitsUnsigned();
};
/**
* Return the JSON value.
*
* @method
* @return {string} the JSON representation.
*/
Long.prototype.toJSON = function() {
return this.toString();
};
/**
* Return the String value.
*
* @method
* @param {number} [opt_radix] the radix in which the text should be written.
* @return {string} the textual representation of this value.
*/
Long.prototype.toString = function(opt_radix) {
var radix = opt_radix || 10;
if (radix < 2 || 36 < radix) {
throw Error('radix out of range: ' + radix);
}
if (this.isZero()) {
return '0';
}
if (this.isNegative()) {
if (this.equals(Long.MIN_VALUE)) {
// We need to change the Long value before it can be negated, so we remove
// the bottom-most digit in this base and then recurse to do the rest.
var radixLong = Long.fromNumber(radix);
var div = this.div(radixLong);
var rem = div.multiply(radixLong).subtract(this);
return div.toString(radix) + rem.toInt().toString(radix);
} else {
return '-' + this.negate().toString(radix);
}
}
// Do several (6) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Long.fromNumber(Math.pow(radix, 6));
rem = this;
var result = '';
while (!rem.isZero()) {
var remDiv = rem.div(radixToPower);
var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
var digits = intval.toString(radix);
rem = remDiv;
if (rem.isZero()) {
return digits + result;
} else {
while (digits.length < 6) {
digits = '0' + digits;
}
result = '' + digits + result;
}
}
};
/**
* Return the high 32-bits value.
*
* @method
* @return {number} the high 32-bits as a signed value.
*/
Long.prototype.getHighBits = function() {
return this.high_;
};
/**
* Return the low 32-bits value.
*
* @method
* @return {number} the low 32-bits as a signed value.
*/
Long.prototype.getLowBits = function() {
return this.low_;
};
/**
* Return the low unsigned 32-bits value.
*
* @method
* @return {number} the low 32-bits as an unsigned value.
*/
Long.prototype.getLowBitsUnsigned = function() {
return this.low_ >= 0 ? this.low_ : Long.TWO_PWR_32_DBL_ + this.low_;
};
/**
* Returns the number of bits needed to represent the absolute value of this Long.
*
* @method
* @return {number} Returns the number of bits needed to represent the absolute value of this Long.
*/
Long.prototype.getNumBitsAbs = function() {
if (this.isNegative()) {
if (this.equals(Long.MIN_VALUE)) {
return 64;
} else {
return this.negate().getNumBitsAbs();
}
} else {
var val = this.high_ !== 0 ? this.high_ : this.low_;
for (var bit = 31; bit > 0; bit--) {
if ((val & (1 << bit)) !== 0) {
break;
}
}
return this.high_ !== 0 ? bit + 33 : bit + 1;
}
};
/**
* Return whether this value is zero.
*
* @method
* @return {boolean} whether this value is zero.
*/
Long.prototype.isZero = function() {
return this.high_ === 0 && this.low_ === 0;
};
/**
* Return whether this value is negative.
*
* @method
* @return {boolean} whether this value is negative.
*/
Long.prototype.isNegative = function() {
return this.high_ < 0;
};
/**
* Return whether this value is odd.
*
* @method
* @return {boolean} whether this value is odd.
*/
Long.prototype.isOdd = function() {
return (this.low_ & 1) === 1;
};
/**
* Return whether this Long equals the other
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} whether this Long equals the other
*/
Long.prototype.equals = function(other) {
return this.high_ === other.high_ && this.low_ === other.low_;
};
/**
* Return whether this Long does not equal the other.
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} whether this Long does not equal the other.
*/
Long.prototype.notEquals = function(other) {
return this.high_ !== other.high_ || this.low_ !== other.low_;
};
/**
* Return whether this Long is less than the other.
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} whether this Long is less than the other.
*/
Long.prototype.lessThan = function(other) {
return this.compare(other) < 0;
};
/**
* Return whether this Long is less than or equal to the other.
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} whether this Long is less than or equal to the other.
*/
Long.prototype.lessThanOrEqual = function(other) {
return this.compare(other) <= 0;
};
/**
* Return whether this Long is greater than the other.
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} whether this Long is greater than the other.
*/
Long.prototype.greaterThan = function(other) {
return this.compare(other) > 0;
};
/**
* Return whether this Long is greater than or equal to the other.
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} whether this Long is greater than or equal to the other.
*/
Long.prototype.greaterThanOrEqual = function(other) {
return this.compare(other) >= 0;
};
/**
* Compares this Long with the given one.
*
* @method
* @param {Long} other Long to compare against.
* @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
*/
Long.prototype.compare = function(other) {
if (this.equals(other)) {
return 0;
}
var thisNeg = this.isNegative();
var otherNeg = other.isNegative();
if (thisNeg && !otherNeg) {
return -1;
}
if (!thisNeg && otherNeg) {
return 1;
}
// at this point, the signs are the same, so subtraction will not overflow
if (this.subtract(other).isNegative()) {
return -1;
} else {
return 1;
}
};
/**
* The negation of this value.
*
* @method
* @return {Long} the negation of this value.
*/
Long.prototype.negate = function() {
if (this.equals(Long.MIN_VALUE)) {
return Long.MIN_VALUE;
} else {
return this.not().add(Long.ONE);
}
};
/**
* Returns the sum of this and the given Long.
*
* @method
* @param {Long} other Long to add to this one.
* @return {Long} the sum of this and the given Long.
*/
Long.prototype.add = function(other) {
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
var a48 = this.high_ >>> 16;
var a32 = this.high_ & 0xffff;
var a16 = this.low_ >>> 16;
var a00 = this.low_ & 0xffff;
var b48 = other.high_ >>> 16;
var b32 = other.high_ & 0xffff;
var b16 = other.low_ >>> 16;
var b00 = other.low_ & 0xffff;
var c48 = 0,
c32 = 0,
c16 = 0,
c00 = 0;
c00 += a00 + b00;
c16 += c00 >>> 16;
c00 &= 0xffff;
c16 += a16 + b16;
c32 += c16 >>> 16;
c16 &= 0xffff;
c32 += a32 + b32;
c48 += c32 >>> 16;
c32 &= 0xffff;
c48 += a48 + b48;
c48 &= 0xffff;
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
};
/**
* Returns the difference of this and the given Long.
*
* @method
* @param {Long} other Long to subtract from this.
* @return {Long} the difference of this and the given Long.
*/
Long.prototype.subtract = function(other) {
return this.add(other.negate());
};
/**
* Returns the product of this and the given Long.
*
* @method
* @param {Long} other Long to multiply with this.
* @return {Long} the product of this and the other.
*/
Long.prototype.multiply = function(other) {
if (this.isZero()) {
return Long.ZERO;
} else if (other.isZero()) {
return Long.ZERO;
}
if (this.equals(Long.MIN_VALUE)) {
return other.isOdd() ? Long.MIN_VALUE : Long.ZERO;
} else if (other.equals(Long.MIN_VALUE)) {
return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
}
if (this.isNegative()) {
if (other.isNegative()) {
return this.negate().multiply(other.negate());
} else {
return this.negate()
.multiply(other)
.negate();
}
} else if (other.isNegative()) {
return this.multiply(other.negate()).negate();
}
// If both Longs are small, use float multiplication
if (this.lessThan(Long.TWO_PWR_24_) && other.lessThan(Long.TWO_PWR_24_)) {
return Long.fromNumber(this.toNumber() * other.toNumber());
}
// Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products.
// We can skip products that would overflow.
var a48 = this.high_ >>> 16;
var a32 = this.high_ & 0xffff;
var a16 = this.low_ >>> 16;
var a00 = this.low_ & 0xffff;
var b48 = other.high_ >>> 16;
var b32 = other.high_ & 0xffff;
var b16 = other.low_ >>> 16;
var b00 = other.low_ & 0xffff;
var c48 = 0,
c32 = 0,
c16 = 0,
c00 = 0;
c00 += a00 * b00;
c16 += c00 >>> 16;
c00 &= 0xffff;
c16 += a16 * b00;
c32 += c16 >>> 16;
c16 &= 0xffff;
c16 += a00 * b16;
c32 += c16 >>> 16;
c16 &= 0xffff;
c32 += a32 * b00;
c48 += c32 >>> 16;
c32 &= 0xffff;
c32 += a16 * b16;
c48 += c32 >>> 16;
c32 &= 0xffff;
c32 += a00 * b32;
c48 += c32 >>> 16;
c32 &= 0xffff;
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
c48 &= 0xffff;
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
};
/**
* Returns this Long divided by the given one.
*
* @method
* @param {Long} other Long by which to divide.
* @return {Long} this Long divided by the given one.
*/
Long.prototype.div = function(other) {
if (other.isZero()) {
throw Error('division by zero');
} else if (this.isZero()) {
return Long.ZERO;
}
if (this.equals(Long.MIN_VALUE)) {
if (other.equals(Long.ONE) || other.equals(Long.NEG_ONE)) {
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
} else if (other.equals(Long.MIN_VALUE)) {
return Long.ONE;
} else {
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
var halfThis = this.shiftRight(1);
var approx = halfThis.div(other).shiftLeft(1);
if (approx.equals(Long.ZERO)) {
return other.isNegative() ? Long.ONE : Long.NEG_ONE;
} else {
var rem = this.subtract(other.multiply(approx));
var result = approx.add(rem.div(other));
return result;
}
}
} else if (other.equals(Long.MIN_VALUE)) {
return Long.ZERO;
}
if (this.isNegative()) {
if (other.isNegative()) {
return this.negate().div(other.negate());
} else {
return this.negate()
.div(other)
.negate();
}
} else if (other.isNegative()) {
return this.div(other.negate()).negate();
}
// Repeat the following until the remainder is less than other: find a
// floating-point that approximates remainder / other *from below*, add this
// into the result, and subtract it from the remainder. It is critical that
// the approximate value is less than or equal to the real value so that the
// remainder never becomes negative.
var res = Long.ZERO;
rem = this;
while (rem.greaterThanOrEqual(other)) {
// Approximate the result of division. This may be a little greater or
// smaller than the actual value.
approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
// We will tweak the approximate result by changing it in the 48-th digit or
// the smallest non-fractional digit, whichever is larger.
var log2 = Math.ceil(Math.log(approx) / Math.LN2);
var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
// Decrease the approximation until it is smaller than the remainder. Note
// that if it is too large, the product overflows and is negative.
var approxRes = Long.fromNumber(approx);
var approxRem = approxRes.multiply(other);
while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
approx -= delta;
approxRes = Long.fromNumber(approx);
approxRem = approxRes.multiply(other);
}
// We know the answer can't be zero... and actually, zero would cause
// infinite recursion since we would make no progress.
if (approxRes.isZero()) {
approxRes = Long.ONE;
}
res = res.add(approxRes);
rem = rem.subtract(approxRem);
}
return res;
};
/**
* Returns this Long modulo the given one.
*
* @method
* @param {Long} other Long by which to mod.
* @return {Long} this Long modulo the given one.
*/
Long.prototype.modulo = function(other) {
return this.subtract(this.div(other).multiply(other));
};
/**
* The bitwise-NOT of this value.
*
* @method
* @return {Long} the bitwise-NOT of this value.
*/
Long.prototype.not = function() {
return Long.fromBits(~this.low_, ~this.high_);
};
/**
* Returns the bitwise-AND of this Long and the given one.
*
* @method
* @param {Long} other the Long with which to AND.
* @return {Long} the bitwise-AND of this and the other.
*/
Long.prototype.and = function(other) {
return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_);
};
/**
* Returns the bitwise-OR of this Long and the given one.
*
* @method
* @param {Long} other the Long with which to OR.
* @return {Long} the bitwise-OR of this and the other.
*/
Long.prototype.or = function(other) {
return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_);
};
/**
* Returns the bitwise-XOR of this Long and the given one.
*
* @method
* @param {Long} other the Long with which to XOR.
* @return {Long} the bitwise-XOR of this and the other.
*/
Long.prototype.xor = function(other) {
return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
};
/**
* Returns this Long with bits shifted to the left by the given amount.
*
* @method
* @param {number} numBits the number of bits by which to shift.
* @return {Long} this shifted to the left by the given amount.
*/
Long.prototype.shiftLeft = function(numBits) {
numBits &= 63;
if (numBits === 0) {
return this;
} else {
var low = this.low_;
if (numBits < 32) {
var high = this.high_;
return Long.fromBits(low << numBits, (high << numBits) | (low >>> (32 - numBits)));
} else {
return Long.fromBits(0, low << (numBits - 32));
}
}
};
/**
* Returns this Long with bits shifted to the right by the given amount.
*
* @method
* @param {number} numBits the number of bits by which to shift.
* @return {Long} this shifted to the right by the given amount.
*/
Long.prototype.shiftRight = function(numBits) {
numBits &= 63;
if (numBits === 0) {
return this;
} else {
var high = this.high_;
if (numBits < 32) {
var low = this.low_;
return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >> numBits);
} else {
return Long.fromBits(high >> (numBits - 32), high >= 0 ? 0 : -1);
}
}
};
/**
* Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
*
* @method
* @param {number} numBits the number of bits by which to shift.
* @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits.
*/
Long.prototype.shiftRightUnsigned = function(numBits) {
numBits &= 63;
if (numBits === 0) {
return this;
} else {
var high = this.high_;
if (numBits < 32) {
var low = this.low_;
return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits);
} else if (numBits === 32) {
return Long.fromBits(high, 0);
} else {
return Long.fromBits(high >>> (numBits - 32), 0);
}
}
};
/**
* Returns a Long representing the given (32-bit) integer value.
*
* @method
* @param {number} value the 32-bit integer in question.
* @return {Long} the corresponding Long value.
*/
Long.fromInt = function(value) {
if (-128 <= value && value < 128) {
var cachedObj = Long.INT_CACHE_[value];
if (cachedObj) {
return cachedObj;
}
}
var obj = new Long(value | 0, value < 0 ? -1 : 0);
if (-128 <= value && value < 128) {
Long.INT_CACHE_[value] = obj;
}
return obj;
};
/**
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
*
* @method
* @param {number} value the number in question.
* @return {Long} the corresponding Long value.
*/
Long.fromNumber = function(value) {
if (isNaN(value) || !isFinite(value)) {
return Long.ZERO;
} else if (value <= -Long.TWO_PWR_63_DBL_) {
return Long.MIN_VALUE;
} else if (value + 1 >= Long.TWO_PWR_63_DBL_) {
return Long.MAX_VALUE;
} else if (value < 0) {
return Long.fromNumber(-value).negate();
} else {
return new Long((value % Long.TWO_PWR_32_DBL_) | 0, (value / Long.TWO_PWR_32_DBL_) | 0);
}
};
/**
* Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.
*
* @method
* @param {number} lowBits the low 32-bits.
* @param {number} highBits the high 32-bits.
* @return {Long} the corresponding Long value.
*/
Long.fromBits = function(lowBits, highBits) {
return new Long(lowBits, highBits);
};
/**
* Returns a Long representation of the given string, written using the given radix.
*
* @method
* @param {string} str the textual representation of the Long.
* @param {number} opt_radix the radix in which the text is written.
* @return {Long} the corresponding Long value.
*/
Long.fromString = function(str, opt_radix) {
if (str.length === 0) {
throw Error('number format error: empty string');
}
var radix = opt_radix || 10;
if (radix < 2 || 36 < radix) {
throw Error('radix out of range: ' + radix);
}
if (str.charAt(0) === '-') {
return Long.fromString(str.substring(1), radix).negate();
} else if (str.indexOf('-') >= 0) {
throw Error('number format error: interior "-" character: ' + str);
}
// Do several (8) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Long.fromNumber(Math.pow(radix, 8));
var result = Long.ZERO;
for (var i = 0; i < str.length; i += 8) {
var size = Math.min(8, str.length - i);
var value = parseInt(str.substring(i, i + size), radix);
if (size < 8) {
var power = Long.fromNumber(Math.pow(radix, size));
result = result.multiply(power).add(Long.fromNumber(value));
} else {
result = result.multiply(radixToPower);
result = result.add(Long.fromNumber(value));
}
}
return result;
};
// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
// from* methods on which they depend.
/**
* A cache of the Long representations of small integer values.
* @type {Object}
* @ignore
*/
Long.INT_CACHE_ = {};
// NOTE: the compiler should inline these constant values below and then remove
// these variables, so there should be no runtime penalty for these.
/**
* Number used repeated below in calculations. This must appear before the
* first call to any from* function below.
* @type {number}
* @ignore
*/
Long.TWO_PWR_16_DBL_ = 1 << 16;
/**
* @type {number}
* @ignore
*/
Long.TWO_PWR_24_DBL_ = 1 << 24;
/**
* @type {number}
* @ignore
*/
Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_;
/**
* @type {number}
* @ignore
*/
Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2;
/**
* @type {number}
* @ignore
*/
Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_;
/**
* @type {number}
* @ignore
*/
Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_;
/**
* @type {number}
* @ignore
*/
Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2;
/** @type {Long} */
Long.ZERO = Long.fromInt(0);
/** @type {Long} */
Long.ONE = Long.fromInt(1);
/** @type {Long} */
Long.NEG_ONE = Long.fromInt(-1);
/** @type {Long} */
Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0);
/** @type {Long} */
Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0);
/**
* @type {Long}
* @ignore
*/
Long.TWO_PWR_24_ = Long.fromInt(1 << 24);
/**
* Expose.
*/
module.exports = Long;
module.exports.Long = Long;
-128
View File
@@ -1,128 +0,0 @@
'use strict';
// We have an ES6 Map available, return the native instance
if (typeof global.Map !== 'undefined') {
module.exports = global.Map;
module.exports.Map = global.Map;
} else {
// We will return a polyfill
var Map = function(array) {
this._keys = [];
this._values = {};
for (var i = 0; i < array.length; i++) {
if (array[i] == null) continue; // skip null and undefined
var entry = array[i];
var key = entry[0];
var value = entry[1];
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
}
};
Map.prototype.clear = function() {
this._keys = [];
this._values = {};
};
Map.prototype.delete = function(key) {
var value = this._values[key];
if (value == null) return false;
// Delete entry
delete this._values[key];
// Remove the key from the ordered keys list
this._keys.splice(value.i, 1);
return true;
};
Map.prototype.entries = function() {
var self = this;
var index = 0;
return {
next: function() {
var key = self._keys[index++];
return {
value: key !== undefined ? [key, self._values[key].v] : undefined,
done: key !== undefined ? false : true
};
}
};
};
Map.prototype.forEach = function(callback, self) {
self = self || this;
for (var i = 0; i < this._keys.length; i++) {
var key = this._keys[i];
// Call the forEach callback
callback.call(self, this._values[key].v, key, self);
}
};
Map.prototype.get = function(key) {
return this._values[key] ? this._values[key].v : undefined;
};
Map.prototype.has = function(key) {
return this._values[key] != null;
};
Map.prototype.keys = function() {
var self = this;
var index = 0;
return {
next: function() {
var key = self._keys[index++];
return {
value: key !== undefined ? key : undefined,
done: key !== undefined ? false : true
};
}
};
};
Map.prototype.set = function(key, value) {
if (this._values[key]) {
this._values[key].v = value;
return this;
}
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
return this;
};
Map.prototype.values = function() {
var self = this;
var index = 0;
return {
next: function() {
var key = self._keys[index++];
return {
value: key !== undefined ? self._values[key].v : undefined,
done: key !== undefined ? false : true
};
}
};
};
// Last ismaster
Object.defineProperty(Map.prototype, 'size', {
enumerable: true,
get: function() {
return this._keys.length;
}
});
module.exports = Map;
module.exports.Map = Map;
}
-14
View File
@@ -1,14 +0,0 @@
/**
* A class representation of the BSON MaxKey type.
*
* @class
* @return {MaxKey} A MaxKey instance
*/
function MaxKey() {
if (!(this instanceof MaxKey)) return new MaxKey();
this._bsontype = 'MaxKey';
}
module.exports = MaxKey;
module.exports.MaxKey = MaxKey;
-14
View File
@@ -1,14 +0,0 @@
/**
* A class representation of the BSON MinKey type.
*
* @class
* @return {MinKey} A MinKey instance
*/
function MinKey() {
if (!(this instanceof MinKey)) return new MinKey();
this._bsontype = 'MinKey';
}
module.exports = MinKey;
module.exports.MinKey = MinKey;
-387
View File
@@ -1,387 +0,0 @@
// Custom inspect property name / symbol.
var inspect = 'inspect';
/**
* Machine id.
*
* Create a random 3-byte value (i.e. unique for this
* process). Other drivers use a md5 of the machine id here, but
* that would mean an asyc call to gethostname, so we don't bother.
* @ignore
*/
var MACHINE_ID = parseInt(Math.random() * 0xffffff, 10);
// Regular expression that checks for hex value
var checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
// Check if buffer exists
try {
if (Buffer && Buffer.from) {
var hasBufferType = true;
inspect = require('util').inspect.custom || 'inspect';
}
} catch (err) {
hasBufferType = false;
}
/**
* Create a new ObjectID instance
*
* @class
* @param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number.
* @property {number} generationTime The generation time of this ObjectId instance
* @return {ObjectID} instance of ObjectID.
*/
var ObjectID = function ObjectID(id) {
// Duck-typing to support ObjectId from different npm packages
if (id instanceof ObjectID) return id;
if (!(this instanceof ObjectID)) return new ObjectID(id);
this._bsontype = 'ObjectID';
// The most common usecase (blank id, new objectId instance)
if (id == null || typeof id === 'number') {
// Generate a new id
this.id = this.generate(id);
// If we are caching the hex string
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
// Return the object
return;
}
// Check if the passed in id is valid
var valid = ObjectID.isValid(id);
// Throw an error if it's not a valid setup
if (!valid && id != null) {
throw new Error(
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
} else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
return new ObjectID(new Buffer(id, 'hex'));
} else if (valid && typeof id === 'string' && id.length === 24) {
return ObjectID.createFromHexString(id);
} else if (id != null && id.length === 12) {
// assume 12 byte string
this.id = id;
} else if (id != null && id.toHexString) {
// Duck-typing to support ObjectId from different npm packages
return id;
} else {
throw new Error(
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
}
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
};
// Allow usage of ObjectId as well as ObjectID
// var ObjectId = ObjectID;
// Precomputed hex table enables speedy hex string conversion
var hexTable = [];
for (var i = 0; i < 256; i++) {
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16);
}
/**
* Return the ObjectID id as a 24 byte hex string representation
*
* @method
* @return {string} return the 24 byte hex string representation.
*/
ObjectID.prototype.toHexString = function() {
if (ObjectID.cacheHexString && this.__id) return this.__id;
var hexString = '';
if (!this.id || !this.id.length) {
throw new Error(
'invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [' +
JSON.stringify(this.id) +
']'
);
}
if (this.id instanceof _Buffer) {
hexString = convertToHex(this.id);
if (ObjectID.cacheHexString) this.__id = hexString;
return hexString;
}
for (var i = 0; i < this.id.length; i++) {
hexString += hexTable[this.id.charCodeAt(i)];
}
if (ObjectID.cacheHexString) this.__id = hexString;
return hexString;
};
/**
* Update the ObjectID index used in generating new ObjectID's on the driver
*
* @method
* @return {number} returns next index value.
* @ignore
*/
ObjectID.prototype.get_inc = function() {
return (ObjectID.index = (ObjectID.index + 1) % 0xffffff);
};
/**
* Update the ObjectID index used in generating new ObjectID's on the driver
*
* @method
* @return {number} returns next index value.
* @ignore
*/
ObjectID.prototype.getInc = function() {
return this.get_inc();
};
/**
* Generate a 12 byte id buffer used in ObjectID's
*
* @method
* @param {number} [time] optional parameter allowing to pass in a second based timestamp.
* @return {Buffer} return the 12 byte id buffer string.
*/
ObjectID.prototype.generate = function(time) {
if ('number' !== typeof time) {
time = ~~(Date.now() / 1000);
}
// Use pid
var pid =
(typeof process === 'undefined' || process.pid === 1
? Math.floor(Math.random() * 100000)
: process.pid) % 0xffff;
var inc = this.get_inc();
// Buffer used
var buffer = new Buffer(12);
// Encode time
buffer[3] = time & 0xff;
buffer[2] = (time >> 8) & 0xff;
buffer[1] = (time >> 16) & 0xff;
buffer[0] = (time >> 24) & 0xff;
// Encode machine
buffer[6] = MACHINE_ID & 0xff;
buffer[5] = (MACHINE_ID >> 8) & 0xff;
buffer[4] = (MACHINE_ID >> 16) & 0xff;
// Encode pid
buffer[8] = pid & 0xff;
buffer[7] = (pid >> 8) & 0xff;
// Encode index
buffer[11] = inc & 0xff;
buffer[10] = (inc >> 8) & 0xff;
buffer[9] = (inc >> 16) & 0xff;
// Return the buffer
return buffer;
};
/**
* Converts the id into a 24 byte hex string for printing
*
* @param {String} format The Buffer toString format parameter.
* @return {String} return the 24 byte hex string representation.
* @ignore
*/
ObjectID.prototype.toString = function(format) {
// Is the id a buffer then use the buffer toString method to return the format
if (this.id && this.id.copy) {
return this.id.toString(typeof format === 'string' ? format : 'hex');
}
// if(this.buffer )
return this.toHexString();
};
/**
* Converts to a string representation of this Id.
*
* @return {String} return the 24 byte hex string representation.
* @ignore
*/
ObjectID.prototype[inspect] = ObjectID.prototype.toString;
/**
* Converts to its JSON representation.
*
* @return {String} return the 24 byte hex string representation.
* @ignore
*/
ObjectID.prototype.toJSON = function() {
return this.toHexString();
};
/**
* Compares the equality of this ObjectID with `otherID`.
*
* @method
* @param {object} otherID ObjectID instance to compare against.
* @return {boolean} the result of comparing two ObjectID's
*/
ObjectID.prototype.equals = function equals(otherId) {
// var id;
if (otherId instanceof ObjectID) {
return this.toString() === otherId.toString();
} else if (
typeof otherId === 'string' &&
ObjectID.isValid(otherId) &&
otherId.length === 12 &&
this.id instanceof _Buffer
) {
return otherId === this.id.toString('binary');
} else if (typeof otherId === 'string' && ObjectID.isValid(otherId) && otherId.length === 24) {
return otherId.toLowerCase() === this.toHexString();
} else if (typeof otherId === 'string' && ObjectID.isValid(otherId) && otherId.length === 12) {
return otherId === this.id;
} else if (otherId != null && (otherId instanceof ObjectID || otherId.toHexString)) {
return otherId.toHexString() === this.toHexString();
} else {
return false;
}
};
/**
* Returns the generation date (accurate up to the second) that this ID was generated.
*
* @method
* @return {date} the generation date
*/
ObjectID.prototype.getTimestamp = function() {
var timestamp = new Date();
var time = this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24);
timestamp.setTime(Math.floor(time) * 1000);
return timestamp;
};
/**
* @ignore
*/
ObjectID.index = ~~(Math.random() * 0xffffff);
/**
* @ignore
*/
ObjectID.createPk = function createPk() {
return new ObjectID();
};
/**
* Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
*
* @method
* @param {number} time an integer number representing a number of seconds.
* @return {ObjectID} return the created ObjectID
*/
ObjectID.createFromTime = function createFromTime(time) {
var buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Encode time into first 4 bytes
buffer[3] = time & 0xff;
buffer[2] = (time >> 8) & 0xff;
buffer[1] = (time >> 16) & 0xff;
buffer[0] = (time >> 24) & 0xff;
// Return the new objectId
return new ObjectID(buffer);
};
// Lookup tables
//var encodeLookup = '0123456789abcdef'.split('');
var decodeLookup = [];
i = 0;
while (i < 10) decodeLookup[0x30 + i] = i++;
while (i < 16) decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++;
var _Buffer = Buffer;
var convertToHex = function(bytes) {
return bytes.toString('hex');
};
/**
* Creates an ObjectID from a hex string representation of an ObjectID.
*
* @method
* @param {string} hexString create a ObjectID from a passed in 24 byte hexstring.
* @return {ObjectID} return the created ObjectID
*/
ObjectID.createFromHexString = function createFromHexString(string) {
// Throw an error if it's not a valid setup
if (typeof string === 'undefined' || (string != null && string.length !== 24)) {
throw new Error(
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
}
// Use Buffer.from method if available
if (hasBufferType) return new ObjectID(new Buffer(string, 'hex'));
// Calculate lengths
var array = new _Buffer(12);
var n = 0;
var i = 0;
while (i < 24) {
array[n++] = (decodeLookup[string.charCodeAt(i++)] << 4) | decodeLookup[string.charCodeAt(i++)];
}
return new ObjectID(array);
};
/**
* Checks if a value is a valid bson ObjectId
*
* @method
* @return {boolean} return true if the value is a valid bson ObjectId, return false otherwise.
*/
ObjectID.isValid = function isValid(id) {
if (id == null) return false;
if (typeof id === 'number') {
return true;
}
if (typeof id === 'string') {
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
}
if (id instanceof ObjectID) {
return true;
}
if (id instanceof _Buffer) {
return true;
}
// Duck-Typing detection of ObjectId like objects
if (id.toHexString) {
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id));
}
return false;
};
/**
* @ignore
*/
Object.defineProperty(ObjectID.prototype, 'generationTime', {
enumerable: true,
get: function() {
return this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24);
},
set: function(value) {
// Encode time into first 4 bytes
this.id[3] = value & 0xff;
this.id[2] = (value >> 8) & 0xff;
this.id[1] = (value >> 16) & 0xff;
this.id[0] = (value >> 24) & 0xff;
}
});
/**
* Expose.
*/
module.exports = ObjectID;
module.exports.ObjectID = ObjectID;
module.exports.ObjectId = ObjectID;
@@ -1,255 +0,0 @@
'use strict';
var Long = require('../long').Long,
Double = require('../double').Double,
Timestamp = require('../timestamp').Timestamp,
ObjectID = require('../objectid').ObjectID,
Symbol = require('../symbol').Symbol,
BSONRegExp = require('../regexp').BSONRegExp,
Code = require('../code').Code,
Decimal128 = require('../decimal128'),
MinKey = require('../min_key').MinKey,
MaxKey = require('../max_key').MaxKey,
DBRef = require('../db_ref').DBRef,
Binary = require('../binary').Binary;
var normalizedFunctionString = require('./utils').normalizedFunctionString;
// To ensure that 0.4 of node works correctly
var isDate = function isDate(d) {
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
};
var calculateObjectSize = function calculateObjectSize(
object,
serializeFunctions,
ignoreUndefined
) {
var totalLength = 4 + 1;
if (Array.isArray(object)) {
for (var i = 0; i < object.length; i++) {
totalLength += calculateElement(
i.toString(),
object[i],
serializeFunctions,
true,
ignoreUndefined
);
}
} else {
// If we have toBSON defined, override the current object
if (object.toBSON) {
object = object.toBSON();
}
// Calculate size
for (var key in object) {
totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined);
}
}
return totalLength;
};
/**
* @ignore
* @api private
*/
function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefined) {
// If we have toBSON defined, override the current object
if (value && value.toBSON) {
value = value.toBSON();
}
switch (typeof value) {
case 'string':
return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1;
case 'number':
if (Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
if (value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
// 32 bit
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
} else {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
} else {
// 64 bit
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
case 'undefined':
if (isArray || !ignoreUndefined)
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
return 0;
case 'boolean':
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
case 'object':
if (
value == null ||
value instanceof MinKey ||
value instanceof MaxKey ||
value['_bsontype'] === 'MinKey' ||
value['_bsontype'] === 'MaxKey'
) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
} else if (value instanceof ObjectID || value['_bsontype'] === 'ObjectID') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
} else if (value instanceof Date || isDate(value)) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.length
);
} else if (
value instanceof Long ||
value instanceof Double ||
value instanceof Timestamp ||
value['_bsontype'] === 'Long' ||
value['_bsontype'] === 'Double' ||
value['_bsontype'] === 'Timestamp'
) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (value instanceof Decimal128 || value['_bsontype'] === 'Decimal128') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
} else if (value instanceof Code || value['_bsontype'] === 'Code') {
// Calculate size depending on the availability of a scope
if (value.scope != null && Object.keys(value.scope).length > 0) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
Buffer.byteLength(value.code.toString(), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(value.code.toString(), 'utf8') +
1
);
}
} else if (value instanceof Binary || value['_bsontype'] === 'Binary') {
// Check what kind of subtype we have
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
(value.position + 1 + 4 + 1 + 4)
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1)
);
}
} else if (value instanceof Symbol || value['_bsontype'] === 'Symbol') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
Buffer.byteLength(value.value, 'utf8') +
4 +
1 +
1
);
} else if (value instanceof DBRef || value['_bsontype'] === 'DBRef') {
// Set up correct object for serialization
var ordered_values = {
$ref: value.namespace,
$id: value.oid
};
// Add db reference if it exists
if (null != value.db) {
ordered_values['$db'] = value.db;
}
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined)
);
} else if (
value instanceof RegExp ||
Object.prototype.toString.call(value) === '[object RegExp]'
) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else if (value instanceof BSONRegExp || value['_bsontype'] === 'BSONRegExp') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.pattern, 'utf8') +
1 +
Buffer.byteLength(value.options, 'utf8') +
1
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
calculateObjectSize(value, serializeFunctions, ignoreUndefined) +
1
);
}
case 'function':
// WTF for 0.4.X where typeof /someregexp/ === 'function'
if (
value instanceof RegExp ||
Object.prototype.toString.call(value) === '[object RegExp]' ||
String.call(value) === '[object RegExp]'
) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else {
if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else if (serializeFunctions) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1
);
}
}
}
return 0;
}
var BSON = {};
// BSON MAX VALUES
BSON.BSON_INT32_MAX = 0x7fffffff;
BSON.BSON_INT32_MIN = -0x80000000;
// JS MAX PRECISE VALUES
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
module.exports = calculateObjectSize;
@@ -1,780 +0,0 @@
'use strict';
var Long = require('../long').Long,
Double = require('../double').Double,
Timestamp = require('../timestamp').Timestamp,
ObjectID = require('../objectid').ObjectID,
Symbol = require('../symbol').Symbol,
Code = require('../code').Code,
MinKey = require('../min_key').MinKey,
MaxKey = require('../max_key').MaxKey,
Decimal128 = require('../decimal128'),
Int32 = require('../int_32'),
DBRef = require('../db_ref').DBRef,
BSONRegExp = require('../regexp').BSONRegExp,
Binary = require('../binary').Binary;
var deserialize = function(buffer, options, isArray) {
options = options == null ? {} : options;
var index = options && options.index ? options.index : 0;
// Read the document size
var size =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
// Ensure buffer is valid size
if (size < 5 || buffer.length < size || size + index > buffer.length) {
throw new Error('corrupt bson message');
}
// Illegal end value
if (buffer[index + size - 1] !== 0) {
throw new Error("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00");
}
// Start deserializtion
return deserializeObject(buffer, index, options, isArray);
};
var deserializeObject = function(buffer, index, options, isArray) {
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
var cacheFunctionsCrc32 =
options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
if (!cacheFunctionsCrc32) var crc32 = null;
var fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
// Return raw bson buffer instead of parsing it
var raw = options['raw'] == null ? false : options['raw'];
// Return BSONRegExp objects instead of native regular expressions
var bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
// Controls the promotion of values vs wrapper classes
var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
// Set the start index
var startIndex = index;
// Validate that we have at least 4 bytes of buffer
if (buffer.length < 5) throw new Error('corrupt bson message < 5 bytes long');
// Read the document size
var size =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Ensure buffer is valid size
if (size < 5 || size > buffer.length) throw new Error('corrupt bson message');
// Create holding object
var object = isArray ? [] : {};
// Used for arrays to skip having to perform utf8 decoding
var arrayIndex = 0;
var done = false;
// While we have more left data left keep parsing
// while (buffer[index + 1] !== 0) {
while (!done) {
// Read the type
var elementType = buffer[index++];
// If we get a zero it's the last byte, exit
if (elementType === 0) break;
// Get the start search index
var i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString');
var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
index = i + 1;
if (elementType === BSON.BSON_DATA_STRING) {
var stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new Error('bad string length in bson');
object[name] = buffer.toString('utf8', index, index + stringSize - 1);
index = index + stringSize;
} else if (elementType === BSON.BSON_DATA_OID) {
var oid = new Buffer(12);
buffer.copy(oid, 0, index, index + 12);
object[name] = new ObjectID(oid);
index = index + 12;
} else if (elementType === BSON.BSON_DATA_INT && promoteValues === false) {
object[name] = new Int32(
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24)
);
} else if (elementType === BSON.BSON_DATA_INT) {
object[name] =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
} else if (elementType === BSON.BSON_DATA_NUMBER && promoteValues === false) {
object[name] = new Double(buffer.readDoubleLE(index));
index = index + 8;
} else if (elementType === BSON.BSON_DATA_NUMBER) {
object[name] = buffer.readDoubleLE(index);
index = index + 8;
} else if (elementType === BSON.BSON_DATA_DATE) {
var lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
object[name] = new Date(new Long(lowBits, highBits).toNumber());
} else if (elementType === BSON.BSON_DATA_BOOLEAN) {
if (buffer[index] !== 0 && buffer[index] !== 1) throw new Error('illegal boolean type value');
object[name] = buffer[index++] === 1;
} else if (elementType === BSON.BSON_DATA_OBJECT) {
var _index = index;
var objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (objectSize <= 0 || objectSize > buffer.length - index)
throw new Error('bad embedded document length in bson');
// We have a raw value
if (raw) {
object[name] = buffer.slice(index, index + objectSize);
} else {
object[name] = deserializeObject(buffer, _index, options, false);
}
index = index + objectSize;
} else if (elementType === BSON.BSON_DATA_ARRAY) {
_index = index;
objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
var arrayOptions = options;
// Stop index
var stopIndex = index + objectSize;
// All elements of array to be returned as raw bson
if (fieldsAsRaw && fieldsAsRaw[name]) {
arrayOptions = {};
for (var n in options) arrayOptions[n] = options[n];
arrayOptions['raw'] = true;
}
object[name] = deserializeObject(buffer, _index, arrayOptions, true);
index = index + objectSize;
if (buffer[index - 1] !== 0) throw new Error('invalid array terminator byte');
if (index !== stopIndex) throw new Error('corrupted array bson');
} else if (elementType === BSON.BSON_DATA_UNDEFINED) {
object[name] = undefined;
} else if (elementType === BSON.BSON_DATA_NULL) {
object[name] = null;
} else if (elementType === BSON.BSON_DATA_LONG) {
// Unpack the low and high bits
lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var long = new Long(lowBits, highBits);
// Promote the long if possible
if (promoteLongs && promoteValues === true) {
object[name] =
long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
? long.toNumber()
: long;
} else {
object[name] = long;
}
} else if (elementType === BSON.BSON_DATA_DECIMAL128) {
// Buffer to contain the decimal bytes
var bytes = new Buffer(16);
// Copy the next 16 bytes into the bytes buffer
buffer.copy(bytes, 0, index, index + 16);
// Update index
index = index + 16;
// Assign the new Decimal128 value
var decimal128 = new Decimal128(bytes);
// If we have an alternative mapper use that
object[name] = decimal128.toObject ? decimal128.toObject() : decimal128;
} else if (elementType === BSON.BSON_DATA_BINARY) {
var binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var totalBinarySize = binarySize;
var subType = buffer[index++];
// Did we have a negative binary size, throw
if (binarySize < 0) throw new Error('Negative binary type element size found');
// Is the length longer than the document
if (binarySize > buffer.length) throw new Error('Binary type size larger than document size');
// Decode as raw Buffer object if options specifies it
if (buffer['slice'] != null) {
// If we have subtype 2 skip the 4 bytes for the size
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new Error('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new Error('Binary type with subtype 0x02 contains to long binary size');
if (binarySize < totalBinarySize - 4)
throw new Error('Binary type with subtype 0x02 contains to short binary size');
}
if (promoteBuffers && promoteValues) {
object[name] = buffer.slice(index, index + binarySize);
} else {
object[name] = new Binary(buffer.slice(index, index + binarySize), subType);
}
} else {
var _buffer =
typeof Uint8Array !== 'undefined'
? new Uint8Array(new ArrayBuffer(binarySize))
: new Array(binarySize);
// If we have subtype 2 skip the 4 bytes for the size
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new Error('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new Error('Binary type with subtype 0x02 contains to long binary size');
if (binarySize < totalBinarySize - 4)
throw new Error('Binary type with subtype 0x02 contains to short binary size');
}
// Copy the data
for (i = 0; i < binarySize; i++) {
_buffer[i] = buffer[index + i];
}
if (promoteBuffers && promoteValues) {
object[name] = _buffer;
} else {
object[name] = new Binary(_buffer, subType);
}
}
// Update the index
index = index + binarySize;
} else if (elementType === BSON.BSON_DATA_REGEXP && bsonRegExp === false) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString');
// Return the C string
var source = buffer.toString('utf8', index, i);
// Create the regexp
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString');
// Return the C string
var regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// For each option add the corresponding one for javascript
var optionsArray = new Array(regExpOptions.length);
// Parse options
for (i = 0; i < regExpOptions.length; i++) {
switch (regExpOptions[i]) {
case 'm':
optionsArray[i] = 'm';
break;
case 's':
optionsArray[i] = 'g';
break;
case 'i':
optionsArray[i] = 'i';
break;
}
}
object[name] = new RegExp(source, optionsArray.join(''));
} else if (elementType === BSON.BSON_DATA_REGEXP && bsonRegExp === true) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString');
// Return the C string
source = buffer.toString('utf8', index, i);
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString');
// Return the C string
regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// Set the object
object[name] = new BSONRegExp(source, regExpOptions);
} else if (elementType === BSON.BSON_DATA_SYMBOL) {
stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new Error('bad string length in bson');
object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1));
index = index + stringSize;
} else if (elementType === BSON.BSON_DATA_TIMESTAMP) {
lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
object[name] = new Timestamp(lowBits, highBits);
} else if (elementType === BSON.BSON_DATA_MIN_KEY) {
object[name] = new MinKey();
} else if (elementType === BSON.BSON_DATA_MAX_KEY) {
object[name] = new MaxKey();
} else if (elementType === BSON.BSON_DATA_CODE) {
stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new Error('bad string length in bson');
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
// Got to do this to avoid V8 deoptimizing the call due to finding eval
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
} else {
object[name] = isolateEval(functionString);
}
} else {
object[name] = new Code(functionString);
}
// Update parse index position
index = index + stringSize;
} else if (elementType === BSON.BSON_DATA_CODE_W_SCOPE) {
var totalSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Element cannot be shorter than totalSize + stringSize + documentSize + terminator
if (totalSize < 4 + 4 + 4 + 1) {
throw new Error('code_w_scope total size shorter minimum expected length');
}
// Get the code string size
stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new Error('bad string length in bson');
// Javascript function
functionString = buffer.toString('utf8', index, index + stringSize - 1);
// Update parse index position
index = index + stringSize;
// Parse the element
_index = index;
// Decode the size of the object document
objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
// Decode the scope object
var scopeObject = deserializeObject(buffer, _index, options, false);
// Adjust the index
index = index + objectSize;
// Check if field length is to short
if (totalSize < 4 + 4 + objectSize + stringSize) {
throw new Error('code_w_scope total size is to short, truncating scope');
}
// Check if totalSize field is to long
if (totalSize > 4 + 4 + objectSize + stringSize) {
throw new Error('code_w_scope total size is to long, clips outer document');
}
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
// Got to do this to avoid V8 deoptimizing the call due to finding eval
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
} else {
object[name] = isolateEval(functionString);
}
object[name].scope = scopeObject;
} else {
object[name] = new Code(functionString, scopeObject);
}
} else if (elementType === BSON.BSON_DATA_DBPOINTER) {
// Get the code string size
stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new Error('bad string length in bson');
// Namespace
var namespace = buffer.toString('utf8', index, index + stringSize - 1);
// Update parse index position
index = index + stringSize;
// Read the oid
var oidBuffer = new Buffer(12);
buffer.copy(oidBuffer, 0, index, index + 12);
oid = new ObjectID(oidBuffer);
// Update the index
index = index + 12;
// Split the namespace
var parts = namespace.split('.');
var db = parts.shift();
var collection = parts.join('.');
// Upgrade to DBRef type
object[name] = new DBRef(collection, oid, db);
} else {
throw new Error(
'Detected unknown BSON type ' +
elementType.toString(16) +
' for fieldname "' +
name +
'", are you using the latest BSON parser'
);
}
}
// Check if the deserialization was against a valid array/object
if (size !== index - startIndex) {
if (isArray) throw new Error('corrupt array bson');
throw new Error('corrupt object bson');
}
// Check if we have a db ref object
if (object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
return object;
};
/**
* Ensure eval is isolated.
*
* @ignore
* @api private
*/
var isolateEvalWithHash = function(functionCache, hash, functionString, object) {
// Contains the value we are going to set
var value = null;
// Check for cache hit, eval if missing and return cached function
if (functionCache[hash] == null) {
eval('value = ' + functionString);
functionCache[hash] = value;
}
// Set the object
return functionCache[hash].bind(object);
};
/**
* Ensure eval is isolated.
*
* @ignore
* @api private
*/
var isolateEval = function(functionString) {
// Contains the value we are going to set
var value = null;
// Eval the function
eval('value = ' + functionString);
return value;
};
var BSON = {};
/**
* Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
*
* @ignore
* @api private
*/
var functionCache = (BSON.functionCache = {});
/**
* Number BSON Type
*
* @classconstant BSON_DATA_NUMBER
**/
BSON.BSON_DATA_NUMBER = 1;
/**
* String BSON Type
*
* @classconstant BSON_DATA_STRING
**/
BSON.BSON_DATA_STRING = 2;
/**
* Object BSON Type
*
* @classconstant BSON_DATA_OBJECT
**/
BSON.BSON_DATA_OBJECT = 3;
/**
* Array BSON Type
*
* @classconstant BSON_DATA_ARRAY
**/
BSON.BSON_DATA_ARRAY = 4;
/**
* Binary BSON Type
*
* @classconstant BSON_DATA_BINARY
**/
BSON.BSON_DATA_BINARY = 5;
/**
* Binary BSON Type
*
* @classconstant BSON_DATA_UNDEFINED
**/
BSON.BSON_DATA_UNDEFINED = 6;
/**
* ObjectID BSON Type
*
* @classconstant BSON_DATA_OID
**/
BSON.BSON_DATA_OID = 7;
/**
* Boolean BSON Type
*
* @classconstant BSON_DATA_BOOLEAN
**/
BSON.BSON_DATA_BOOLEAN = 8;
/**
* Date BSON Type
*
* @classconstant BSON_DATA_DATE
**/
BSON.BSON_DATA_DATE = 9;
/**
* null BSON Type
*
* @classconstant BSON_DATA_NULL
**/
BSON.BSON_DATA_NULL = 10;
/**
* RegExp BSON Type
*
* @classconstant BSON_DATA_REGEXP
**/
BSON.BSON_DATA_REGEXP = 11;
/**
* Code BSON Type
*
* @classconstant BSON_DATA_DBPOINTER
**/
BSON.BSON_DATA_DBPOINTER = 12;
/**
* Code BSON Type
*
* @classconstant BSON_DATA_CODE
**/
BSON.BSON_DATA_CODE = 13;
/**
* Symbol BSON Type
*
* @classconstant BSON_DATA_SYMBOL
**/
BSON.BSON_DATA_SYMBOL = 14;
/**
* Code with Scope BSON Type
*
* @classconstant BSON_DATA_CODE_W_SCOPE
**/
BSON.BSON_DATA_CODE_W_SCOPE = 15;
/**
* 32 bit Integer BSON Type
*
* @classconstant BSON_DATA_INT
**/
BSON.BSON_DATA_INT = 16;
/**
* Timestamp BSON Type
*
* @classconstant BSON_DATA_TIMESTAMP
**/
BSON.BSON_DATA_TIMESTAMP = 17;
/**
* Long BSON Type
*
* @classconstant BSON_DATA_LONG
**/
BSON.BSON_DATA_LONG = 18;
/**
* Long BSON Type
*
* @classconstant BSON_DATA_DECIMAL128
**/
BSON.BSON_DATA_DECIMAL128 = 19;
/**
* MinKey BSON Type
*
* @classconstant BSON_DATA_MIN_KEY
**/
BSON.BSON_DATA_MIN_KEY = 0xff;
/**
* MaxKey BSON Type
*
* @classconstant BSON_DATA_MAX_KEY
**/
BSON.BSON_DATA_MAX_KEY = 0x7f;
/**
* Binary Default Type
*
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
**/
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
/**
* Binary Function Type
*
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
**/
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
/**
* Binary Byte Array Type
*
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
**/
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
/**
* Binary UUID Type
*
* @classconstant BSON_BINARY_SUBTYPE_UUID
**/
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
/**
* Binary MD5 Type
*
* @classconstant BSON_BINARY_SUBTYPE_MD5
**/
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
/**
* Binary User Defined Type
*
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
**/
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
// BSON MAX VALUES
BSON.BSON_INT32_MAX = 0x7fffffff;
BSON.BSON_INT32_MIN = -0x80000000;
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
// JS MAX PRECISE VALUES
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
// Internal long versions
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
module.exports = deserialize;
File diff suppressed because it is too large Load Diff
-14
View File
@@ -1,14 +0,0 @@
'use strict';
/**
* Normalizes our expected stringified form of a function across versions of node
* @param {Function} fn The function to stringify
*/
function normalizedFunctionString(fn) {
return fn.toString().replace(/function *\(/, 'function (');
}
module.exports = {
normalizedFunctionString: normalizedFunctionString
};
-33
View File
@@ -1,33 +0,0 @@
/**
* A class representation of the BSON RegExp type.
*
* @class
* @return {BSONRegExp} A MinKey instance
*/
function BSONRegExp(pattern, options) {
if (!(this instanceof BSONRegExp)) return new BSONRegExp();
// Execute
this._bsontype = 'BSONRegExp';
this.pattern = pattern || '';
this.options = options || '';
// Validate options
for (var i = 0; i < this.options.length; i++) {
if (
!(
this.options[i] === 'i' ||
this.options[i] === 'm' ||
this.options[i] === 'x' ||
this.options[i] === 'l' ||
this.options[i] === 's' ||
this.options[i] === 'u'
)
) {
throw new Error('the regular expression options [' + this.options[i] + '] is not supported');
}
}
}
module.exports = BSONRegExp;
module.exports.BSONRegExp = BSONRegExp;
-50
View File
@@ -1,50 +0,0 @@
// Custom inspect property name / symbol.
var inspect = Buffer ? require('util').inspect.custom || 'inspect' : 'inspect';
/**
* A class representation of the BSON Symbol type.
*
* @class
* @deprecated
* @param {string} value the string representing the symbol.
* @return {Symbol}
*/
function Symbol(value) {
if (!(this instanceof Symbol)) return new Symbol(value);
this._bsontype = 'Symbol';
this.value = value;
}
/**
* Access the wrapped string value.
*
* @method
* @return {String} returns the wrapped string.
*/
Symbol.prototype.valueOf = function() {
return this.value;
};
/**
* @ignore
*/
Symbol.prototype.toString = function() {
return this.value;
};
/**
* @ignore
*/
Symbol.prototype[inspect] = function() {
return this.value;
};
/**
* @ignore
*/
Symbol.prototype.toJSON = function() {
return this.value;
};
module.exports = Symbol;
module.exports.Symbol = Symbol;
-854
View File
@@ -1,854 +0,0 @@
// 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.
//
// Copyright 2009 Google Inc. All Rights Reserved
/**
* This type is for INTERNAL use in MongoDB only and should not be used in applications.
* The appropriate corresponding type is the JavaScript Date type.
*
* Defines a Timestamp class for representing a 64-bit two's-complement
* integer value, which faithfully simulates the behavior of a Java "Timestamp". This
* implementation is derived from TimestampLib in GWT.
*
* Constructs a 64-bit two's-complement integer, given its low and high 32-bit
* values as *signed* integers. See the from* functions below for more
* convenient ways of constructing Timestamps.
*
* The internal representation of a Timestamp is the two given signed, 32-bit values.
* We use 32-bit pieces because these are the size of integers on which
* Javascript performs bit-operations. For operations like addition and
* multiplication, we split each number into 16-bit pieces, which can easily be
* multiplied within Javascript's floating-point representation without overflow
* or change in sign.
*
* In the algorithms below, we frequently reduce the negative case to the
* positive case by negating the input(s) and then post-processing the result.
* Note that we must ALWAYS check specially whether those values are MIN_VALUE
* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
* a positive number, it overflows back into a negative). Not handling this
* case would often result in infinite recursion.
*
* @class
* @param {number} low the low (signed) 32 bits of the Timestamp.
* @param {number} high the high (signed) 32 bits of the Timestamp.
*/
function Timestamp(low, high) {
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
this._bsontype = 'Timestamp';
/**
* @type {number}
* @ignore
*/
this.low_ = low | 0; // force into 32 signed bits.
/**
* @type {number}
* @ignore
*/
this.high_ = high | 0; // force into 32 signed bits.
}
/**
* Return the int value.
*
* @return {number} the value, assuming it is a 32-bit integer.
*/
Timestamp.prototype.toInt = function() {
return this.low_;
};
/**
* Return the Number value.
*
* @method
* @return {number} the closest floating-point representation to this value.
*/
Timestamp.prototype.toNumber = function() {
return this.high_ * Timestamp.TWO_PWR_32_DBL_ + this.getLowBitsUnsigned();
};
/**
* Return the JSON value.
*
* @method
* @return {string} the JSON representation.
*/
Timestamp.prototype.toJSON = function() {
return this.toString();
};
/**
* Return the String value.
*
* @method
* @param {number} [opt_radix] the radix in which the text should be written.
* @return {string} the textual representation of this value.
*/
Timestamp.prototype.toString = function(opt_radix) {
var radix = opt_radix || 10;
if (radix < 2 || 36 < radix) {
throw Error('radix out of range: ' + radix);
}
if (this.isZero()) {
return '0';
}
if (this.isNegative()) {
if (this.equals(Timestamp.MIN_VALUE)) {
// We need to change the Timestamp value before it can be negated, so we remove
// the bottom-most digit in this base and then recurse to do the rest.
var radixTimestamp = Timestamp.fromNumber(radix);
var div = this.div(radixTimestamp);
var rem = div.multiply(radixTimestamp).subtract(this);
return div.toString(radix) + rem.toInt().toString(radix);
} else {
return '-' + this.negate().toString(radix);
}
}
// Do several (6) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Timestamp.fromNumber(Math.pow(radix, 6));
rem = this;
var result = '';
while (!rem.isZero()) {
var remDiv = rem.div(radixToPower);
var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
var digits = intval.toString(radix);
rem = remDiv;
if (rem.isZero()) {
return digits + result;
} else {
while (digits.length < 6) {
digits = '0' + digits;
}
result = '' + digits + result;
}
}
};
/**
* Return the high 32-bits value.
*
* @method
* @return {number} the high 32-bits as a signed value.
*/
Timestamp.prototype.getHighBits = function() {
return this.high_;
};
/**
* Return the low 32-bits value.
*
* @method
* @return {number} the low 32-bits as a signed value.
*/
Timestamp.prototype.getLowBits = function() {
return this.low_;
};
/**
* Return the low unsigned 32-bits value.
*
* @method
* @return {number} the low 32-bits as an unsigned value.
*/
Timestamp.prototype.getLowBitsUnsigned = function() {
return this.low_ >= 0 ? this.low_ : Timestamp.TWO_PWR_32_DBL_ + this.low_;
};
/**
* Returns the number of bits needed to represent the absolute value of this Timestamp.
*
* @method
* @return {number} Returns the number of bits needed to represent the absolute value of this Timestamp.
*/
Timestamp.prototype.getNumBitsAbs = function() {
if (this.isNegative()) {
if (this.equals(Timestamp.MIN_VALUE)) {
return 64;
} else {
return this.negate().getNumBitsAbs();
}
} else {
var val = this.high_ !== 0 ? this.high_ : this.low_;
for (var bit = 31; bit > 0; bit--) {
if ((val & (1 << bit)) !== 0) {
break;
}
}
return this.high_ !== 0 ? bit + 33 : bit + 1;
}
};
/**
* Return whether this value is zero.
*
* @method
* @return {boolean} whether this value is zero.
*/
Timestamp.prototype.isZero = function() {
return this.high_ === 0 && this.low_ === 0;
};
/**
* Return whether this value is negative.
*
* @method
* @return {boolean} whether this value is negative.
*/
Timestamp.prototype.isNegative = function() {
return this.high_ < 0;
};
/**
* Return whether this value is odd.
*
* @method
* @return {boolean} whether this value is odd.
*/
Timestamp.prototype.isOdd = function() {
return (this.low_ & 1) === 1;
};
/**
* Return whether this Timestamp equals the other
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} whether this Timestamp equals the other
*/
Timestamp.prototype.equals = function(other) {
return this.high_ === other.high_ && this.low_ === other.low_;
};
/**
* Return whether this Timestamp does not equal the other.
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} whether this Timestamp does not equal the other.
*/
Timestamp.prototype.notEquals = function(other) {
return this.high_ !== other.high_ || this.low_ !== other.low_;
};
/**
* Return whether this Timestamp is less than the other.
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} whether this Timestamp is less than the other.
*/
Timestamp.prototype.lessThan = function(other) {
return this.compare(other) < 0;
};
/**
* Return whether this Timestamp is less than or equal to the other.
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} whether this Timestamp is less than or equal to the other.
*/
Timestamp.prototype.lessThanOrEqual = function(other) {
return this.compare(other) <= 0;
};
/**
* Return whether this Timestamp is greater than the other.
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} whether this Timestamp is greater than the other.
*/
Timestamp.prototype.greaterThan = function(other) {
return this.compare(other) > 0;
};
/**
* Return whether this Timestamp is greater than or equal to the other.
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} whether this Timestamp is greater than or equal to the other.
*/
Timestamp.prototype.greaterThanOrEqual = function(other) {
return this.compare(other) >= 0;
};
/**
* Compares this Timestamp with the given one.
*
* @method
* @param {Timestamp} other Timestamp to compare against.
* @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
*/
Timestamp.prototype.compare = function(other) {
if (this.equals(other)) {
return 0;
}
var thisNeg = this.isNegative();
var otherNeg = other.isNegative();
if (thisNeg && !otherNeg) {
return -1;
}
if (!thisNeg && otherNeg) {
return 1;
}
// at this point, the signs are the same, so subtraction will not overflow
if (this.subtract(other).isNegative()) {
return -1;
} else {
return 1;
}
};
/**
* The negation of this value.
*
* @method
* @return {Timestamp} the negation of this value.
*/
Timestamp.prototype.negate = function() {
if (this.equals(Timestamp.MIN_VALUE)) {
return Timestamp.MIN_VALUE;
} else {
return this.not().add(Timestamp.ONE);
}
};
/**
* Returns the sum of this and the given Timestamp.
*
* @method
* @param {Timestamp} other Timestamp to add to this one.
* @return {Timestamp} the sum of this and the given Timestamp.
*/
Timestamp.prototype.add = function(other) {
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
var a48 = this.high_ >>> 16;
var a32 = this.high_ & 0xffff;
var a16 = this.low_ >>> 16;
var a00 = this.low_ & 0xffff;
var b48 = other.high_ >>> 16;
var b32 = other.high_ & 0xffff;
var b16 = other.low_ >>> 16;
var b00 = other.low_ & 0xffff;
var c48 = 0,
c32 = 0,
c16 = 0,
c00 = 0;
c00 += a00 + b00;
c16 += c00 >>> 16;
c00 &= 0xffff;
c16 += a16 + b16;
c32 += c16 >>> 16;
c16 &= 0xffff;
c32 += a32 + b32;
c48 += c32 >>> 16;
c32 &= 0xffff;
c48 += a48 + b48;
c48 &= 0xffff;
return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
};
/**
* Returns the difference of this and the given Timestamp.
*
* @method
* @param {Timestamp} other Timestamp to subtract from this.
* @return {Timestamp} the difference of this and the given Timestamp.
*/
Timestamp.prototype.subtract = function(other) {
return this.add(other.negate());
};
/**
* Returns the product of this and the given Timestamp.
*
* @method
* @param {Timestamp} other Timestamp to multiply with this.
* @return {Timestamp} the product of this and the other.
*/
Timestamp.prototype.multiply = function(other) {
if (this.isZero()) {
return Timestamp.ZERO;
} else if (other.isZero()) {
return Timestamp.ZERO;
}
if (this.equals(Timestamp.MIN_VALUE)) {
return other.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO;
} else if (other.equals(Timestamp.MIN_VALUE)) {
return this.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO;
}
if (this.isNegative()) {
if (other.isNegative()) {
return this.negate().multiply(other.negate());
} else {
return this.negate()
.multiply(other)
.negate();
}
} else if (other.isNegative()) {
return this.multiply(other.negate()).negate();
}
// If both Timestamps are small, use float multiplication
if (this.lessThan(Timestamp.TWO_PWR_24_) && other.lessThan(Timestamp.TWO_PWR_24_)) {
return Timestamp.fromNumber(this.toNumber() * other.toNumber());
}
// Divide each Timestamp into 4 chunks of 16 bits, and then add up 4x4 products.
// We can skip products that would overflow.
var a48 = this.high_ >>> 16;
var a32 = this.high_ & 0xffff;
var a16 = this.low_ >>> 16;
var a00 = this.low_ & 0xffff;
var b48 = other.high_ >>> 16;
var b32 = other.high_ & 0xffff;
var b16 = other.low_ >>> 16;
var b00 = other.low_ & 0xffff;
var c48 = 0,
c32 = 0,
c16 = 0,
c00 = 0;
c00 += a00 * b00;
c16 += c00 >>> 16;
c00 &= 0xffff;
c16 += a16 * b00;
c32 += c16 >>> 16;
c16 &= 0xffff;
c16 += a00 * b16;
c32 += c16 >>> 16;
c16 &= 0xffff;
c32 += a32 * b00;
c48 += c32 >>> 16;
c32 &= 0xffff;
c32 += a16 * b16;
c48 += c32 >>> 16;
c32 &= 0xffff;
c32 += a00 * b32;
c48 += c32 >>> 16;
c32 &= 0xffff;
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
c48 &= 0xffff;
return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
};
/**
* Returns this Timestamp divided by the given one.
*
* @method
* @param {Timestamp} other Timestamp by which to divide.
* @return {Timestamp} this Timestamp divided by the given one.
*/
Timestamp.prototype.div = function(other) {
if (other.isZero()) {
throw Error('division by zero');
} else if (this.isZero()) {
return Timestamp.ZERO;
}
if (this.equals(Timestamp.MIN_VALUE)) {
if (other.equals(Timestamp.ONE) || other.equals(Timestamp.NEG_ONE)) {
return Timestamp.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
} else if (other.equals(Timestamp.MIN_VALUE)) {
return Timestamp.ONE;
} else {
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
var halfThis = this.shiftRight(1);
var approx = halfThis.div(other).shiftLeft(1);
if (approx.equals(Timestamp.ZERO)) {
return other.isNegative() ? Timestamp.ONE : Timestamp.NEG_ONE;
} else {
var rem = this.subtract(other.multiply(approx));
var result = approx.add(rem.div(other));
return result;
}
}
} else if (other.equals(Timestamp.MIN_VALUE)) {
return Timestamp.ZERO;
}
if (this.isNegative()) {
if (other.isNegative()) {
return this.negate().div(other.negate());
} else {
return this.negate()
.div(other)
.negate();
}
} else if (other.isNegative()) {
return this.div(other.negate()).negate();
}
// Repeat the following until the remainder is less than other: find a
// floating-point that approximates remainder / other *from below*, add this
// into the result, and subtract it from the remainder. It is critical that
// the approximate value is less than or equal to the real value so that the
// remainder never becomes negative.
var res = Timestamp.ZERO;
rem = this;
while (rem.greaterThanOrEqual(other)) {
// Approximate the result of division. This may be a little greater or
// smaller than the actual value.
approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
// We will tweak the approximate result by changing it in the 48-th digit or
// the smallest non-fractional digit, whichever is larger.
var log2 = Math.ceil(Math.log(approx) / Math.LN2);
var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
// Decrease the approximation until it is smaller than the remainder. Note
// that if it is too large, the product overflows and is negative.
var approxRes = Timestamp.fromNumber(approx);
var approxRem = approxRes.multiply(other);
while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
approx -= delta;
approxRes = Timestamp.fromNumber(approx);
approxRem = approxRes.multiply(other);
}
// We know the answer can't be zero... and actually, zero would cause
// infinite recursion since we would make no progress.
if (approxRes.isZero()) {
approxRes = Timestamp.ONE;
}
res = res.add(approxRes);
rem = rem.subtract(approxRem);
}
return res;
};
/**
* Returns this Timestamp modulo the given one.
*
* @method
* @param {Timestamp} other Timestamp by which to mod.
* @return {Timestamp} this Timestamp modulo the given one.
*/
Timestamp.prototype.modulo = function(other) {
return this.subtract(this.div(other).multiply(other));
};
/**
* The bitwise-NOT of this value.
*
* @method
* @return {Timestamp} the bitwise-NOT of this value.
*/
Timestamp.prototype.not = function() {
return Timestamp.fromBits(~this.low_, ~this.high_);
};
/**
* Returns the bitwise-AND of this Timestamp and the given one.
*
* @method
* @param {Timestamp} other the Timestamp with which to AND.
* @return {Timestamp} the bitwise-AND of this and the other.
*/
Timestamp.prototype.and = function(other) {
return Timestamp.fromBits(this.low_ & other.low_, this.high_ & other.high_);
};
/**
* Returns the bitwise-OR of this Timestamp and the given one.
*
* @method
* @param {Timestamp} other the Timestamp with which to OR.
* @return {Timestamp} the bitwise-OR of this and the other.
*/
Timestamp.prototype.or = function(other) {
return Timestamp.fromBits(this.low_ | other.low_, this.high_ | other.high_);
};
/**
* Returns the bitwise-XOR of this Timestamp and the given one.
*
* @method
* @param {Timestamp} other the Timestamp with which to XOR.
* @return {Timestamp} the bitwise-XOR of this and the other.
*/
Timestamp.prototype.xor = function(other) {
return Timestamp.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
};
/**
* Returns this Timestamp with bits shifted to the left by the given amount.
*
* @method
* @param {number} numBits the number of bits by which to shift.
* @return {Timestamp} this shifted to the left by the given amount.
*/
Timestamp.prototype.shiftLeft = function(numBits) {
numBits &= 63;
if (numBits === 0) {
return this;
} else {
var low = this.low_;
if (numBits < 32) {
var high = this.high_;
return Timestamp.fromBits(low << numBits, (high << numBits) | (low >>> (32 - numBits)));
} else {
return Timestamp.fromBits(0, low << (numBits - 32));
}
}
};
/**
* Returns this Timestamp with bits shifted to the right by the given amount.
*
* @method
* @param {number} numBits the number of bits by which to shift.
* @return {Timestamp} this shifted to the right by the given amount.
*/
Timestamp.prototype.shiftRight = function(numBits) {
numBits &= 63;
if (numBits === 0) {
return this;
} else {
var high = this.high_;
if (numBits < 32) {
var low = this.low_;
return Timestamp.fromBits((low >>> numBits) | (high << (32 - numBits)), high >> numBits);
} else {
return Timestamp.fromBits(high >> (numBits - 32), high >= 0 ? 0 : -1);
}
}
};
/**
* Returns this Timestamp with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
*
* @method
* @param {number} numBits the number of bits by which to shift.
* @return {Timestamp} this shifted to the right by the given amount, with zeros placed into the new leading bits.
*/
Timestamp.prototype.shiftRightUnsigned = function(numBits) {
numBits &= 63;
if (numBits === 0) {
return this;
} else {
var high = this.high_;
if (numBits < 32) {
var low = this.low_;
return Timestamp.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits);
} else if (numBits === 32) {
return Timestamp.fromBits(high, 0);
} else {
return Timestamp.fromBits(high >>> (numBits - 32), 0);
}
}
};
/**
* Returns a Timestamp representing the given (32-bit) integer value.
*
* @method
* @param {number} value the 32-bit integer in question.
* @return {Timestamp} the corresponding Timestamp value.
*/
Timestamp.fromInt = function(value) {
if (-128 <= value && value < 128) {
var cachedObj = Timestamp.INT_CACHE_[value];
if (cachedObj) {
return cachedObj;
}
}
var obj = new Timestamp(value | 0, value < 0 ? -1 : 0);
if (-128 <= value && value < 128) {
Timestamp.INT_CACHE_[value] = obj;
}
return obj;
};
/**
* Returns a Timestamp representing the given value, provided that it is a finite number. Otherwise, zero is returned.
*
* @method
* @param {number} value the number in question.
* @return {Timestamp} the corresponding Timestamp value.
*/
Timestamp.fromNumber = function(value) {
if (isNaN(value) || !isFinite(value)) {
return Timestamp.ZERO;
} else if (value <= -Timestamp.TWO_PWR_63_DBL_) {
return Timestamp.MIN_VALUE;
} else if (value + 1 >= Timestamp.TWO_PWR_63_DBL_) {
return Timestamp.MAX_VALUE;
} else if (value < 0) {
return Timestamp.fromNumber(-value).negate();
} else {
return new Timestamp(
(value % Timestamp.TWO_PWR_32_DBL_) | 0,
(value / Timestamp.TWO_PWR_32_DBL_) | 0
);
}
};
/**
* Returns a Timestamp representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.
*
* @method
* @param {number} lowBits the low 32-bits.
* @param {number} highBits the high 32-bits.
* @return {Timestamp} the corresponding Timestamp value.
*/
Timestamp.fromBits = function(lowBits, highBits) {
return new Timestamp(lowBits, highBits);
};
/**
* Returns a Timestamp representation of the given string, written using the given radix.
*
* @method
* @param {string} str the textual representation of the Timestamp.
* @param {number} opt_radix the radix in which the text is written.
* @return {Timestamp} the corresponding Timestamp value.
*/
Timestamp.fromString = function(str, opt_radix) {
if (str.length === 0) {
throw Error('number format error: empty string');
}
var radix = opt_radix || 10;
if (radix < 2 || 36 < radix) {
throw Error('radix out of range: ' + radix);
}
if (str.charAt(0) === '-') {
return Timestamp.fromString(str.substring(1), radix).negate();
} else if (str.indexOf('-') >= 0) {
throw Error('number format error: interior "-" character: ' + str);
}
// Do several (8) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Timestamp.fromNumber(Math.pow(radix, 8));
var result = Timestamp.ZERO;
for (var i = 0; i < str.length; i += 8) {
var size = Math.min(8, str.length - i);
var value = parseInt(str.substring(i, i + size), radix);
if (size < 8) {
var power = Timestamp.fromNumber(Math.pow(radix, size));
result = result.multiply(power).add(Timestamp.fromNumber(value));
} else {
result = result.multiply(radixToPower);
result = result.add(Timestamp.fromNumber(value));
}
}
return result;
};
// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
// from* methods on which they depend.
/**
* A cache of the Timestamp representations of small integer values.
* @type {Object}
* @ignore
*/
Timestamp.INT_CACHE_ = {};
// NOTE: the compiler should inline these constant values below and then remove
// these variables, so there should be no runtime penalty for these.
/**
* Number used repeated below in calculations. This must appear before the
* first call to any from* function below.
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_16_DBL_ = 1 << 16;
/**
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_24_DBL_ = 1 << 24;
/**
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_32_DBL_ = Timestamp.TWO_PWR_16_DBL_ * Timestamp.TWO_PWR_16_DBL_;
/**
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_31_DBL_ = Timestamp.TWO_PWR_32_DBL_ / 2;
/**
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_48_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_16_DBL_;
/**
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_64_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_32_DBL_;
/**
* @type {number}
* @ignore
*/
Timestamp.TWO_PWR_63_DBL_ = Timestamp.TWO_PWR_64_DBL_ / 2;
/** @type {Timestamp} */
Timestamp.ZERO = Timestamp.fromInt(0);
/** @type {Timestamp} */
Timestamp.ONE = Timestamp.fromInt(1);
/** @type {Timestamp} */
Timestamp.NEG_ONE = Timestamp.fromInt(-1);
/** @type {Timestamp} */
Timestamp.MAX_VALUE = Timestamp.fromBits(0xffffffff | 0, 0x7fffffff | 0);
/** @type {Timestamp} */
Timestamp.MIN_VALUE = Timestamp.fromBits(0, 0x80000000 | 0);
/**
* @type {Timestamp}
* @ignore
*/
Timestamp.TWO_PWR_24_ = Timestamp.fromInt(1 << 24);
/**
* Expose.
*/
module.exports = Timestamp;
module.exports.Timestamp = Timestamp;
-87
View File
@@ -1,87 +0,0 @@
{
"_from": "bson@~1.0.4",
"_id": "bson@1.0.9",
"_inBundle": false,
"_integrity": "sha512-IQX9/h7WdMBIW/q/++tGd+emQr0XMdeZ6icnT/74Xk9fnabWn+gZgpE+9V+gujL3hhJOoNrnDVY7tWdzc7NUTg==",
"_location": "/connect-mongo/bson",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "bson@~1.0.4",
"name": "bson",
"escapedName": "bson",
"rawSpec": "~1.0.4",
"saveSpec": null,
"fetchSpec": "~1.0.4"
},
"_requiredBy": [
"/connect-mongo/mongodb-core"
],
"_resolved": "https://registry.npmjs.org/bson/-/bson-1.0.9.tgz",
"_shasum": "12319f8323b1254739b7c6bef8d3e89ae05a2f57",
"_spec": "bson@~1.0.4",
"_where": "/home/erik/Documents/workspace_brackets/a1_BME_Project_Ohm/om/node_modules/connect-mongo/node_modules/mongodb-core",
"author": {
"name": "Christian Amor Kvalheim",
"email": "christkv@gmail.com"
},
"browser": "lib/bson/bson.js",
"bugs": {
"url": "https://github.com/mongodb/js-bson/issues"
},
"bundleDependencies": false,
"config": {
"native": false
},
"contributors": [],
"deprecated": false,
"description": "A bson parser for node.js and the browser",
"devDependencies": {
"babel-core": "^6.14.0",
"babel-loader": "^6.2.5",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.14.0",
"benchmark": "1.0.0",
"colors": "1.1.0",
"conventional-changelog-cli": "^1.3.5",
"nodeunit": "0.9.0",
"webpack": "^1.13.2",
"webpack-polyfills-plugin": "0.0.9"
},
"directories": {
"lib": "./lib/bson"
},
"engines": {
"node": ">=0.6.19"
},
"files": [
"lib",
"index.js",
"browser_build",
"bower.json"
],
"homepage": "https://github.com/mongodb/js-bson#readme",
"keywords": [
"mongodb",
"bson",
"parser"
],
"license": "Apache-2.0",
"main": "./index",
"name": "bson",
"repository": {
"type": "git",
"url": "git+https://github.com/mongodb/js-bson.git"
},
"scripts": {
"build": "webpack --config ./webpack.dist.config.js",
"changelog": "conventional-changelog -p angular -i HISTORY.md -s",
"format": "prettier --print-width 100 --tab-width 2 --single-quote --write 'test/**/*.js' 'lib/**/*.js'",
"lint": "eslint lib test",
"test": "nodeunit ./test/node"
},
"version": "1.0.9"
}
-1
View File
@@ -1 +0,0 @@
repo_token: 47iIZ0B3llo2Wc4dxWRltvgdImqcrVDTi
-15
View File
@@ -1,15 +0,0 @@
{
"extends": ["eslint:recommended"],
"env": {
"node": true,
"mocha": true
},
"ecmaFeatures": {
"es6": true,
},
"plugins": [
],
"rules": {
"no-console":0
}
}
-724
View File
@@ -1,724 +0,0 @@
<a name="2.1.19"></a>
## [2.1.19](https://github.com/christkv/mongodb-core/compare/v2.1.18...v2.1.19) (2018-02-26)
### Bug Fixes
* **replset:** remove primary from topology on failure to connect ([2a246b5](https://github.com/christkv/mongodb-core/commit/2a246b5))
<a name="2.1.18"></a>
## [2.1.18](https://github.com/christkv/mongodb-core/compare/v2.1.17...v2.1.18) (2018-01-02)
### Bug Fixes
* **auth:** don't redeclare BSON variable in plain auth ([4fc77e3](https://github.com/christkv/mongodb-core/commit/4fc77e3))
* **auth:** remove extra bson include ([4411d2c](https://github.com/christkv/mongodb-core/commit/4411d2c))
* **connection:** accept incoming missed ssl options from mongo-native-driver ([fd543eb](https://github.com/christkv/mongodb-core/commit/fd543eb))
* **connection:** added missed tls option ecdhCurve ([ca1d909](https://github.com/christkv/mongodb-core/commit/ca1d909))
* **connection:** default `family` to undefined rather than 4 ([09916ae](https://github.com/christkv/mongodb-core/commit/09916ae))
* **connection:** fixing leaky connection ([#234](https://github.com/christkv/mongodb-core/issues/234)) ([7633f10](https://github.com/christkv/mongodb-core/commit/7633f10))
* **cursor:** check for autoReconnect option only for single server ([c841eb5](https://github.com/christkv/mongodb-core/commit/c841eb5))
* **secondaries:** fixes connection with secondary readPreference ([5763f5c](https://github.com/christkv/mongodb-core/commit/5763f5c))
* **timeout:** fixed compatibility with node <=0.12.x ([c7c72b2](https://github.com/christkv/mongodb-core/commit/c7c72b2)), closes [mongodb-js/mongodb-core#247](https://github.com/mongodb-js/mongodb-core/issues/247) [mongodb-js/mongodb-core#247](https://github.com/mongodb-js/mongodb-core/issues/247) [mongodb-js/mongodb-core#248](https://github.com/mongodb-js/mongodb-core/issues/248) [#248](https://github.com/christkv/mongodb-core/issues/248)
### Features
* **replicaset:** More verbose replica set errors emission: ([#231](https://github.com/christkv/mongodb-core/issues/231)) ([de6d220](https://github.com/christkv/mongodb-core/commit/de6d220))
2.1.17 2017-10-11
-----------------
* fix a typo that completely broke SCRAM-SHA1 authentication
2.1.16 2017-10-11
-----------------
* avoid waiting for reconnect if reconnect disabled in Server topology
* avoid waiting for reconnect if reconnect disabled in Cursor
* NODE-990 cache the ScramSHA1 salted passwords up to 200 entries
* NODE-1153 ensure that errors are propagated on force destroy
* NODE-1153 ensure inUse and connecting queues are cleared on reauth
2.1.15 2017-08-08
-----------------
* Emit SDAM events on close and reconnect
2.1.14 2017-07-07
-----------------
* NODE-1073 updates scram.js hi() algorithm to utilize crypto.pbkdf2Sync()
* NODE-1049 only include primary server if there are no secondary servers for
readPrefrence secondaryPreferred
* moved `assign` polyfill to shared utils, replace usage of `extend` in tests
2.1.13 2017-06-19
-----------------
* NODE-1039 ensure we force destroy server instances, forcing queue to be flushed.
* Use actual server type in standalone SDAM events.
2.1.12 2017-06-02
-----------------
* NODE-1019 Set keepAlive to 300 seconds or 1/2 of socketTimeout if socketTimeout < keepAlive.
* Minor fix to report the correct state on error.
* NODE-1020 'family' was added to options to provide high priority for ipv6 addresses (Issue #1518, https://github.com/firej).
* Fix require_optional loading of bson-ext.
* Ensure no errors are thrown by replset if topology is destroyed before it finished connecting.
* NODE-999 SDAM fixes for Mongos and single Server event emitting.
* NODE-1014 Set socketTimeout to default to 360 seconds.
* NODE-1019 Set keepAlive to 300 seconds or 1/2 of socketTimeout if socketTimeout < keepAlive.
2.1.11 2017-05-22
-----------------
* NODE-987 Clear out old intervalIds on when calling topologyMonitor.
* NODE-987 Moved filtering to pingServer method and added test case.
* Check for connection destroyed just before writing out and flush out operations correctly if it is (Issue #179, https://github.com/jmholzinger).
* NODE-989 Refactored Replicaset monitoring to correcly monitor newly added servers, Also extracted setTimeout and setInterval to use custom wrappers Timeout and Interva.
2.1.10 2017-04-18
-----------------
* NODE-981 delegate auth to replset/mongos if inTopology is set.
* NODE-978 Wrap connection.end in try/catch for node 0.10.x issue causing exceptions to be thrown, Also surfaced getConnection for mongos and replset.
* Remove dynamic require (Issue #175, https://github.com/tellnes).
* NODE-696 Handle interrupted error for createIndexes.
* Fixed isse when user is executing find command using Server.command and it get interpreted as a wire protcol message, #172.
* NODE-966 promoteValues not being promoted correctly to getMore.
* Merged in fix for flushing out monitoring operations.
2.1.9 2017-03-17
----------------
* Return lastIsMaster correctly when connecting with secondaryOnlyConnectionAllowed is set to true and only a secondary is available in replica state.
* Clone options when passed to wireProtocol handler to avoid intermittent modifications causing errors.
* Ensure SSL error propegates better for Replset connections when there is a SSL validation error.
* NODE-957 Fixed issue where < batchSize not causing cursor to be closed on execution of first batch.
* NODE-958 Store reconnectConnection on pool object to allow destroy to close immediately.
2.1.8 2017-02-13
----------------
* NODE-925 ensure we reschedule operations while pool is < poolSize while pool is growing and there are no connections with not currently performing work.
* NODE-927 fixes issue where authentication was performed against arbiter instances.
* NODE-915 Normalize all host names to avoid comparison issues.
* Fixed issue where pool.destroy would never finish due to a single operation not being executed and keeping it open.
2.1.7 2017-01-24
----------------
* NODE-919 ReplicaSet connection does not close immediately (Issue #156).
* NODE-901 Fixed bug when normalizing host names.
* NODE-909 Fixed readPreference issue caused by direct connection to primary.
* NODE-910 Fixed issue when bufferMaxEntries == 0 and read preference set to nearest.
2.1.6 2017-01-13
----------------
* NODE-908 Keep auth contexts in replset and mongos topology to ensure correct application of authentication credentials when primary is first server to be detected causing an immediate connect event to happen.
2.1.5 2017-01-11
----------------
* updated bson and bson-ext dependencies to 1.0.4 to work past early node 4.x.x version having a broken Buffer.from implementation.
2.1.4 2017-01-03
----------------
* updated bson and bson-ext dependencies to 1.0.3 due to util.inspect issue with ObjectId optimizations.
2.1.3 2017-01-03
----------------
* Monitoring operations are re-scheduled in pool if it cannot find a connection that does not already have scheduled work on it, this is to avoid the monitoring socket timeout being applied to any existing operations on the socket due to pipelining
* Moved replicaset monitoring away from serial mode and to parallel mode.
* updated bson and bson-ext dependencies to 1.0.2.
2.1.2 2016-12-10
----------------
* Delay topologyMonitoring on successful attemptReconnect as no need to run a full scan immediately.
* Emit reconnect event in primary joining when in connected status for a replicaset.
2.1.1 2016-12-08
----------------
* Updated bson library to 1.0.1.
* Added optional support for bson-ext 1.0.1.
2.1.0 2016-12-05
----------------
* Updated bson library to 1.0.0.
* Added optional support for bson-ext 1.0.0.
* Expose property parserType allowing for identification of currently configured parser.
2.0.14 2016-11-29
-----------------
* Updated bson library to 0.5.7.
* Dont leak connection.workItems elments when killCursor is called (Issue #150, https://github.com/mdlavin).
* Remove unnecessary errors formatting (Issue #149, https://github.com/akryvomaz).
* Only check isConnected against availableConnections (Issue #142).
* NODE-838 Provide better error message on failed to connect on first retry for Mongos topology.
* Set default servername to host is not passed through for sni.
* Made monitoring happen on exclusive connection and using connectionTimeout to handle the wait time before failure (Issue #148).
* NODE-859 Make minimum value of maxStalenessSeconds 90 seconds.
* NODE-852 Fix Kerberos module deprecations on linux and windows and release new kerberos version.
* NODE-850 Update Max Staleness implementation.
* NODE-849 username no longer required for MONGODB-X509 auth.
* NODE-848 BSON Regex flags must be alphabetically ordered.
* NODE-846 Create notice for all third party libraries.
* NODE-843 Executing bulk operations overwrites write concern parameter.
* NODE-842 Re-sync SDAM and SDAM Monitoring tests from Specs repo.
* NODE-840 Resync CRUD spec tests.
* Unescapable while(true) loop (Issue #152).
2.0.13 2016-10-21
-----------------
* Updated bson library to 0.5.6.
- Included cyclic dependency detection
* Fire callback when topology was destroyed (Issue #147, https://github.com/vkarpov15).
* Refactoring to support pipelining ala 1.4.x branch will retaining the benefits of the growing/shrinking pool (Issue #146).
* Fix typo in serverHeartbeatFailed event name (Issue #143, https://github.com/jakesjews).
* NODE-798 Driver hangs on count command in replica set with one member (Issue #141, https://github.com/isayme).
2.0.12 2016-09-15
-----------------
* fix debug logging message not printing server name.
* fixed application metadata being sent by wrong ismaster.
* NODE-812 Fixed mongos stall due to proxy monitoring ismaster failure causing reconnect.
* NODE-818 Replicaset timeouts in initial connect sequence can "no primary found".
* Updated bson library to 0.5.5.
* Added DBPointer up conversion to DBRef.
2.0.11 2016-08-29
-----------------
* NODE-803, Fixed issue in how the latency window is calculated for Mongos topology causing issues for single proxy connections.
* Avoid timeout in attemptReconnect causing multiple attemptReconnect attempts to happen (Issue #134, https://github.com/dead-horse).
* Ensure promoteBuffers is propagated in same fashion as promoteValues and promoteLongs
2.0.10 2016-08-23
-----------------
* Added promoteValues flag (default to true) to allow user to specify they only want wrapped BSON values back instead of promotion to native types.
* Do not close mongos proxy connection on failed ismaster check in ha process (Issue #130).
2.0.9 2016-08-19
----------------
* Allow promoteLongs to be passed in through Response.parse method and overrides default set on the connection.
* NODE-798 Driver hangs on count command in replica set with one member.
* Allow promoteLongs to be passed in through Response.parse method and overrides default set on the connection.
* Allow passing in servername for TLS connections for SNI support.
2.0.8 2016-08-16
----------------
* Allow execution of store operations indepent of having both a primary and secondary available (Issue #123).
* Fixed command execution issue for mongos to ensure buffering of commands when no mongos available.
* Added hashed connection names and fullResult.
* Updated bson library to 0.5.3.
* Wrap callback in nextTick to ensure exceptions are thrown correctly.
2.0.7 2016-07-28
----------------
* Allow primary to be returned when secondaryPreferred is passed (Issue #117, https://github.com/dhendo).
* Added better warnings when passing in illegal seed list members to a Mongos topology.
* Minor attemptReconnect bug that would cause multiple attemptReconnect to run in parallel.
* Fix wrong opType passed to disconnectHandler.add (Issue #121, https://github.com/adrian-gierakowski)
* Implemented domain backward comp support enabled via domainsEnabled options on Server/ReplSet/Mongos and MongoClient.connect.
* Initial max staleness implementation for ReplSet and Mongos for 3.4 support.
* Added handling of collation for 3.4 support.
2.0.6 2016-07-19
----------------
* Destroy connection on socket timeout due to newer node versions not closing the socket.
2.0.5 2016-07-15
----------------
* Minor fixes to handle faster MongoClient connectivity from the driver, allowing single server instances to detect if they are a proxy.
* Added numberOfConsecutiveTimeouts to pool that will destroy the pool if the number of consecutive timeouts > reconnectTries.
* Print warning if seedlist servers host name does not match the one provided in it's ismaster.me field for Replicaset members.
* Fix issue where Replicaset connection would not succeeed if there the replicaset was a single primary server setup.
2.0.4 2016-07-11
-----------------
* Updated bson to version 0.5.1.
* handle situation where user is providing seedlist names that do not match host list. fix allows for a single full discovery connection sweep before erroring out.
* NODE-747 Polyfill for Object.assign for 0.12.x or 0.10.x.
* NODE-746 Improves replicaset errors for wrong setName.
2.0.3 2016-07-08
-----------------
* Implemented Server Selection Specification test suite.
* Added warning level to logger.
* Added warning message when sockeTimeout < haInterval for Replset/Mongos.
2.0.2 2016-07-06
-----------------
* Mongos emits close event on no proxies available or when reconnect attempt fails.
* Replset emits close event when no servers available or when attemptReconnect fails to reconnect.
* Don't throw in auth methods but return error in callback.
2.0.1 2016-07-05
-----------------
* Added missing logout method on mongos proxy topology.
* Fixed logger error serialization issue.
* Documentation fixes.
2.0.0 2016-07-05
-----------------
* Moved all authentication and handling of growing/shrinking of pool connections into actual pool.
* All authentication methods now handle both auth/reauthenticate and logout events.
* Introduced logout method to get rid of onAll option for logout command.
* Updated bson to 0.5.0 that includes Decimal128 support.
1.3.21 2016-05-30
-----------------
* Pool gets stuck if a connection marked for immediateRelease times out (Issue #99, https://github.com/nbrachet).
* Make authentication process retry up to authenticationRetries at authenticationRetryIntervalMS interval.
* Made ismaster replicaset calls operate with connectTimeout or monitorSocketTimeout to lower impact of big socketTimeouts on monitoring performance.
* Make sure connections mark as "immediateRelease" don't linger the inUserConnections list. Otherwise, after that connection times out, getAll() incorrectly returns more connections than are effectively present, causing the pool to not get restarted by reconnectServer. (Issue #99, https://github.com/nbrachet).
* Make cursor getMore or killCursor correctly trigger pool reconnect to single server if pool has not been destroyed.
* Make ismaster monitoring for single server connection default to avoid user confusion due to change in behavior.
1.3.20 2016-05-25
-----------------
* NODE-710 Allow setting driver loggerLevel and logger function from MongoClient options.
* Minor fix for SSL errors on connection attempts, minor fix to reconnect handler for the server.
* Don't write to socket before having registered the callback for commands, work around for windows issuing error events twice on node.js when socket gets destroyed by firewall.
* Fix minor issue where connectingServers would not be removed correctly causing single server connections to not auto-reconnect.
1.3.19 2016-05-17
-----------------
- Handle situation where a server connection in a replicaset sometimes fails to be destroyed properly due to being in the middle of authentication when the destroy method is called on the replicaset causing it to be orphaned and never collected.
- Set keepAlive to false by default to work around bug in node.js for Windows XP and Windows 2003.
- Ensure replicaset topology destroy is never called by SDAM.
- Ensure all paths are correctly returned on inspectServer in replset.
1.3.18 2016-04-27
-----------------
- Hardened cursor connection handling for getMore and killCursor to ensure mid operation connection kill does not throw null exception.
- Fixes for Node 6.0 support.
1.3.17 2016-04-26
-----------------
- Added improved handling of reconnect when topology is a single server.
- Added better handling of $query queries passed down for 3.2 or higher.
- Introduced getServerFrom method to topologies to let cursor grab a new pool for getMore and killCursors commands and not use connection pipelining.
- NODE-693 Move authentication to be after ismaster call to avoid authenticating against arbiters.
1.3.16 2016-04-07
-----------------
- Only call unref on destroy if it exists to ensure proper working destroy method on early node v0.10.x versions.
1.3.15 2016-04-06
-----------------
- NODE-687 Fixed issue where a server object failed to be destroyed if the replicaset state did not update successfully. This could leave active connections accumulating over time.
- Fixed some situations where all connections are flushed due to a single connection in the connection pool closing.
1.3.14 2016-04-01
-----------------
- Ensure server inquireServerState exits immediately on server.destroy call.
- Refactored readPreference handling in 2.4, 2.6 and 3.2 wire protocol handling.
1.3.13 2016-03-30
-----------------
- Handle missing cursor on getMore when going through a mongos proxy by pinning to socket connection and not server.
1.3.12 2016-03-29
-----------------
- Mongos pickProxies fall back to closest mongos if no proxies meet latency window specified.
1.3.11 2016-03-29
-----------------
- isConnected method for mongos uses same selection code as getServer.
- Exceptions in cursor getServer trapped and correctly delegated to high level handler.
1.3.10 2016-03-22
-----------------
- SDAM Monitoring emits diff for Replicasets to simplify detecting the state changes.
- SDAM Monitoring correctly emits Mongos as serverDescriptionEvent.
1.3.9 2016-03-20
----------------
- Removed monitoring exclusive connection, should resolve timeouts and reconnects on idle replicasets where haInteval > socketTimeout.
1.3.8 2016-03-18
----------------
- Implements the SDAM monitoring specification.
- Fix issue where cursor would error out and not be buffered when primary is not connected.
1.3.7 2016-03-16
----------------
- Fixed issue with replicasetInquirer where it could stop performing monitoring if there was no servers available.
1.3.6 2016-03-15
----------------
- Fixed raise condition where multiple replicasetInquirer operations could be started in parallel creating redundant connections.
1.3.5 2016-03-14
----------------
- Handle rogue SSL exceptions (Issue #85, https://github.com/durran).
1.3.4 2016-03-14
----------------
- Added unref options on server, replicaset and mongos (Issue #81, https://github.com/allevo)
- cursorNotFound flag always false (Issue #83, https://github.com/xgfd)
- refactor of events emission of fullsetup and all events (Issue #84, https://github.com/xizhibei)
1.3.3 2016-03-08
----------------
- Added support for promoteLongs option for command function.
- Return connection if no callback available
- Emit connect event when server reconnects after initial connection failed (Issue #76, https://github.com/vkarpov15)
- Introduced optional monitoringSocketTimeout option to allow better control of SDAM monitoring timeouts.
- Made monitoringSocketTimeout default to 30000 if no connectionTimeout value specified or if set to 0.
- Fixed issue where tailable cursor would not retry even though cursor was still alive.
- Disabled exhaust flag support to avoid issues where users could easily write code that would cause memory to run out.
- Handle the case where the first command result document returns an empty list of documents but a live cursor.
- Allow passing down off CANONICALIZE_HOST_NAME and SERVICE_REALM options for kerberos.
1.3.2 2016-02-09
----------------
- Harden MessageHandler in server.js to avoid issues where we cannot find a callback for an operation.
- Ensure RequestId can never be larger than Max Number integer size.
1.3.1 2016-02-05
----------------
- Removed annoying missing Kerberos error (NODE-654).
1.3.0 2016-02-03
----------------
- Added raw support for the command function on topologies.
- Fixed issue where raw results that fell on batchSize boundaries failed (Issue #72)
- Copy over all the properties to the callback returned from bindToDomain, (Issue #72)
- Added connection hash id to be able to reference connection host/name without leaking it outside of driver.
- NODE-638, Cannot authenticate database user with utf-8 password.
- Refactored pool to be worker queue based, minimizing the impact a slow query have on throughput as long as # slow queries < # connections in the pool.
- Pool now grows and shrinks correctly depending on demand not causing a full pool reconnect.
- Improvements in monitoring of a Replicaset where in certain situations the inquiry process could get exited.
- Switched to using Array.push instead of concat for use cases of a lot of documents.
- Fixed issue where re-authentication could loose the credentials if whole Replicaset disconnected at once.
- Added peer optional dependencies support using require_optional module.
1.2.32 2016-01-12
-----------------
- Bumped bson to V0.4.21 to allow using minor optimizations.
1.2.31 2016-01-04
-----------------
- Allow connection to secondary if primaryPreferred or secondaryPreferred (Issue #70, https://github.com/leichter)
1.2.30 2015-12-23
-----------------
- Pool allocates size + 1 connections when using replicasets, reserving additional pool connection for monitoring exclusively.
- Fixes bug when all replicaset members are down, that would cause it to fail to reconnect using the originally provided seedlist.
1.2.29 2015-12-17
-----------------
- Correctly emit close event when calling destroy on server topology.
1.2.28 2015-12-13
-----------------
- Backed out Prevent Maximum call stack exceeded by calling all callbacks on nextTick, (Issue #64, https://github.com/iamruinous) as it breaks node 0.10.x support.
1.2.27 2015-12-13
-----------------
- Added [options.checkServerIdentity=true] {boolean|function}. 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, (Issue #29).
- Prevent Maximum call stack exceeded by calling all callbacks on nextTick, (Issue #64, https://github.com/iamruinous).
- State is not defined in mongos, (Issue #63, https://github.com/flyingfisher).
- Fixed corner case issue on exhaust cursors on pre 3.0.x MongoDB.
1.2.26 2015-11-23
-----------------
- Converted test suite to use mongodb-topology-manager.
- Upgraded bson library to V0.4.20.
- Minor fixes for 3.2 readPreferences.
1.2.25 2015-11-23
-----------------
- Correctly error out when passed a seedlist of non-valid server members.
1.2.24 2015-11-20
-----------------
- Fix Automattic/mongoose#3481; flush callbacks on error, (Issue #57, https://github.com/vkarpov15).
- $explain query for wire protocol 2.6 and 2.4 does not set number of returned documents to -1 but to 0.
1.2.23 2015-11-16
-----------------
- ismaster runs against admin.$cmd instead of system.$cmd.
1.2.22 2015-11-16
-----------------
- Fixes to handle getMore command errors for MongoDB 3.2
- Allows the process to properly close upon a Db.close() call on the replica set by shutting down the haTimer and closing arbiter connections.
1.2.21 2015-11-07
-----------------
- Hardened the checking for replicaset equality checks.
- OpReplay flag correctly set on Wire protocol query.
- Mongos load balancing added, introduced localThresholdMS to control the feature.
- Kerberos now a peerDependency, making it not install it by default in Node 5.0 or higher.
1.2.20 2015-10-28
-----------------
- Fixed bug in arbiter connection capping code.
- NODE-599 correctly handle arrays of server tags in order of priority.
- Fix for 2.6 wire protocol handler related to readPreference handling.
- Added maxAwaitTimeMS support for 3.2 getMore to allow for custom timeouts on tailable cursors.
- Make CoreCursor check for $err before saying that 'next' succeeded (Issue #53, https://github.com/vkarpov15).
1.2.19 2015-10-15
-----------------
- Make batchSize always be > 0 for 3.2 wire protocol to make it work consistently with pre 3.2 servers.
- Locked to bson 0.4.19.
1.2.18 2015-10-15
-----------------
- Minor 3.2 fix for handling readPreferences on sharded commands.
- Minor fixes to correctly pass APM specification test suite.
1.2.17 2015-10-08
-----------------
- Connections to arbiters only maintain a single connection.
1.2.15 2015-10-06
-----------------
- Set slaveOk to true for getMore and killCursors commands.
- Don't swallow callback errors for 2.4 single server (Issue #49, https://github.com/vkarpov15).
- Apply toString('hex') to each buffer in an array when logging (Issue #48, https://github.com/nbrachet).
1.2.14 2015-09-28
-----------------
- NODE-547 only emit error if there are any listeners.
- Fixed APM issue with issuing readConcern.
1.2.13 2015-09-18
-----------------
- Added BSON serializer ignoreUndefined option for insert/update/remove/command/cursor.
1.2.12 2015-09-08
-----------------
- NODE-541 Added initial support for readConcern.
1.2.11 2015-08-31
-----------------
- NODE-535 If connectWithNoPrimary is true then primary-only connection is not allowed.
- NODE-534 Passive secondaries are not allowed for secondaryOnlyConnectionAllowed.
- Fixed filtering bug for logging (Issue 30, https://github.com/christkv/mongodb-core/issues/30).
1.2.10 2015-08-14
-----------------
- Added missing Mongos.prototype.parserType function.
1.2.9 2015-08-05
----------------
- NODE-525 Reset connectionTimeout after it's overwritten by tls.connect.
- NODE-518 connectTimeoutMS is doubled in 2.0.39.
1.2.8 2015-07-24
-----------------
- Minor fix to handle 2.4.x errors better by correctly return driver layer issues.
1.2.7 2015-07-16
-----------------
- Refactoring to allow to tap into find/getmore/killcursor in cursors for APM monitoring in driver.
1.2.6 2015-07-14
-----------------
- NODE-505 Query fails to find records that have a 'result' property with an array value.
1.2.5 2015-07-14
-----------------
- NODE-492 correctly handle hanging replicaset monitoring connections when server is unavailable due to network partitions or firewalls dropping packets, configureable using the connectionTimeoutMS setting.
1.2.4 2015-07-07
-----------------
- NODE-493 staggering the socket connections to avoid overwhelming the mongod process.
1.2.3 2015-06-26
-----------------
- Minor bug fixes.
1.2.2 2015-06-22
-----------------
- Fix issue with SCRAM authentication causing authentication to return true on failed authentication (Issue 26, https://github.com/cglass17).
1.2.1 2015-06-17
-----------------
- Ensure serializeFunctions passed down correctly to wire protocol.
1.2.0 2015-06-17
-----------------
- Switching to using the 0.4.x pure JS serializer, removing dependency on C++ parser.
- Refactoring wire protocol messages to avoid expensive size calculations of documents in favor of writing out an array of buffers to the sockets.
- NODE-486 fixed issue related to limit and skip when calling toArray in 2.0 driver.
- NODE-483 throw error if capabilities of topology is queries before topology has performed connection setup.
- NODE-487 fixed issue where killcursor command was not being sent correctly on limit and skip queries.
1.1.33 2015-05-31
-----------------
- NODE-478 Work around authentication race condition in mongos authentication due to multi step authentication methods like SCRAM.
1.1.32 2015-05-20
-----------------
- After reconnect, it updates the allowable reconnect retries to the option settings (Issue #23, https://github.com/owenallenaz)
1.1.31 2015-05-19
-----------------
- Minor fixes for issues with re-authentication of mongos.
1.1.30 2015-05-18
-----------------
- Correctly emit 'all' event when primary + all secondaries have connected.
1.1.29 2015-05-17
-----------------
- NODE-464 Only use a single socket against arbiters and hidden servers.
- Ensure we filter out hidden servers from any server queries.
1.1.28 2015-05-12
-----------------
- Fixed buffer compare for electionId for < node 12.0.2
1.1.27 2015-05-12
-----------------
- NODE-455 Update SDAM specification support to cover electionId and Mongos load balancing.
1.1.26 2015-05-06
-----------------
- NODE-456 Allow mongodb-core to pipeline commands (ex findAndModify+GLE) along the same connection and handle the returned results.
- Fixes to make mongodb-core work for node 0.8.x when using scram and setImmediate.
1.1.25 2015-04-24
-----------------
- Handle lack of callback in crud operations when returning error on application closed.
1.1.24 2015-04-22
-----------------
- Error out when topology has been destroyed either by connection retries being exhausted or destroy called on topology.
1.1.23 2015-04-15
-----------------
- Standardizing mongoErrors and its API (Issue #14)
- Creating a new connection is slow because of 100ms setTimeout() (Issue #17, https://github.com/vkarpov15)
- remove mkdirp and rimraf dependencies (Issue #12)
- Updated default value of param options.rejectUnauthorized to match documentation (Issue #16)
- ISSUE: NODE-417 Resolution. Improving behavior of thrown errors (Issue #14, https://github.com/owenallenaz)
- Fix cursor hanging when next() called on exhausted cursor (Issue #18, https://github.com/vkarpov15)
1.1.22 2015-04-10
-----------------
- Minor refactorings in cursor code to make extending the cursor simpler.
- NODE-417 Resolution. Improving behavior of thrown errors using Error.captureStackTrace.
1.1.21 2015-03-26
-----------------
- Updated bson module to 0.3.0 that extracted the c++ parser into bson-ext and made it an optional dependency.
1.1.20 2015-03-24
-----------------
- NODE-395 Socket Not Closing, db.close called before full set finished initalizing leading to server connections in progress not being closed properly.
1.1.19 2015-03-21
-----------------
- Made kerberos module ~0.0 to allow for quicker releases due to io.js of kerberos module.
1.1.18 2015-03-17
-----------------
- Added support for minHeartbeatFrequencyMS on server reconnect according to the SDAM specification.
1.1.17 2015-03-16
-----------------
- NODE-377, fixed issue where tags would correctly be checked on secondary and nearest to filter out eligible server candidates.
1.1.16 2015-03-06
-----------------
- rejectUnauthorized parameter is set to true for ssl certificates by default instead of false.
1.1.15 2015-03-04
-----------------
- Removed check for type in replset pickserver function.
1.1.14 2015-02-26
-----------------
- NODE-374 correctly adding passive secondaries to the list of eligable servers for reads
1.1.13 2015-02-24
-----------------
- NODE-365 mongoDB native node.js driver infinite reconnect attempts (fixed issue around handling of retry attempts)
1.1.12 2015-02-16
-----------------
- Fixed cursor transforms for buffered document reads from cursor.
1.1.11 2015-02-02
-----------------
- Remove the required setName for replicaset connections, if not set it will pick the first setName returned.
1.1.10 2015-31-01
-----------------
- Added tranforms.doc option to cursor to allow for pr. document transformations.
1.1.9 2015-21-01
----------------
- Updated BSON dependency to 0.2.18 to fix issues with io.js and node.
- Updated Kerberos dependency to 0.0.8 to fix issues with io.js and node.
- Don't treat findOne() as a command cursor.
- Refactored out state changes into methods to simplify read the next method.
1.1.8 2015-09-12
----------------
- Stripped out Object.defineProperty for performance reasons
- Applied more performance optimizations.
- properties cursorBatchSize, cursorSkip, cursorLimit are not methods setCursorBatchSize/cursorBatchSize, setCursorSkip/cursorSkip, setCursorLimit/cursorLimit
1.1.7 2014-18-12
----------------
- Use ns variable for getMore commands for command cursors to work properly with cursor version of listCollections and listIndexes.
1.1.6 2014-18-12
----------------
- Server manager fixed to support 2.2.X servers for travis test matrix.
1.1.5 2014-17-12
----------------
- Fall back to errmsg when creating MongoError for command errors
1.1.4 2014-17-12
----------------
- Added transform method support for cursor (initially just for initial query results) to support listCollections/listIndexes in 2.8.
- Fixed variable leak in scram.
- Fixed server manager to deal better with killing processes.
- Bumped bson to 0.2.16.
1.1.3 2014-01-12
----------------
- Fixed error handling issue with nonce generation in mongocr.
- Fixed issues with restarting servers when using ssl.
- Using strict for all classes.
- Cleaned up any escaping global variables.
1.1.2 2014-20-11
----------------
- Correctly encoding UTF8 collection names on wire protocol messages.
- Added emitClose parameter to topology destroy methods to allow users to specify that they wish the topology to emit the close event to any listeners.
1.1.1 2014-14-11
----------------
- Refactored code to use prototype instead of privileged methods.
- Fixed issue with auth where a runtime condition could leave replicaset members without proper authentication.
- Several deopt optimizations for v8 to improve performance and reduce GC pauses.
1.0.5 2014-29-10
----------------
- Fixed issue with wrong namespace being created for command cursors.
1.0.4 2014-24-10
----------------
- switched from using shift for the cursor due to bad slowdown on big batchSizes as shift causes entire array to be copied on each call.
1.0.3 2014-21-10
----------------
- fixed error issuing problem on cursor.next when iterating over a huge dataset with a very small batchSize.
1.0.2 2014-07-10
----------------
- fullsetup is now defined as a primary and secondary being available allowing for all read preferences to be satisfied.
- fixed issue with replset_state logging.
1.0.1 2014-07-10
----------------
- Dependency issue solved
1.0.0 2014-07-10
----------------
- Initial release of mongodb-core
-201
View File
@@ -1,201 +0,0 @@
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.
-11
View File
@@ -1,11 +0,0 @@
NODE = node
NPM = npm
JSDOC = jsdoc
name = all
generate_docs:
# cp -R ./HISTORY.md ./docs/content/meta/release-notes.md
hugo -s docs/reference -d ../../public
$(JSDOC) -c conf.json -t docs/jsdoc-template/ -d ./public/api
cp -R ./public/api/scripts ./public/.
cp -R ./public/api/styles ./public/.
-228
View File
@@ -1,228 +0,0 @@
[![Build Status](https://secure.travis-ci.org/christkv/mongodb-core.png)](http://travis-ci.org/christkv/mongodb-core)
[![Coverage Status](https://coveralls.io/repos/github/christkv/mongodb-core/badge.svg?branch=1.3)](https://coveralls.io/github/christkv/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/christkv/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.
-18
View File
@@ -1,18 +0,0 @@
Testing setup
=============
Single Server
-------------
mongod --dbpath=./db
Replicaset
----------
mongo --nodb
var x = new ReplSetTest({"useHostName":"false", "nodes" : {node0 : {}, node1 : {}, node2 : {}}})
x.startSet();
var config = x.getReplSetConfig()
x.initiate(config);
Mongos
------
var s = new ShardingTest( "auth1", 1 , 0 , 2 , {rs: true, noChunkSize : true});
@@ -1,41 +0,0 @@
Mongodb-core uses third-party libraries or other resources that may
be distributed under licenses different than the Mongodb-core software.
In the event that we accidentally failed to list a required notice,
please bring it to our attention through any of the ways detailed here:
mongodb-dev@googlegroups.com
The attached notices are provided for information only.
For any licenses that require disclosure of source, sources are available at
https://github.com/mongodb/node-mongodb-native.
1) License Notice for nan.h
---------------------------
The MIT License (MIT)
=====================
Copyright (c) 2016 NAN contributors
-----------------------------------
NAN contributors listed at <https://github.com/nodejs/nan#contributors>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to
do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-59
View File
@@ -1,59 +0,0 @@
{
"plugins": ["plugins/markdown", "docs/lib/jsdoc/examples_plugin.js"],
"source": {
"include": [
"test/tests/functional/operation_example_tests.js",
"lib/connection/command_result.js",
"lib/topologies/mongos.js",
"lib/topologies/read_preference.js",
"lib/topologies/replset.js",
"lib/topologies/server.js",
"lib/topologies/replset_state.js",
"lib/connection/logger.js",
"lib/connection/connection.js",
"lib/cursor.js",
"lib/error.js",
"node_modules/bson/lib/bson/binary.js",
"node_modules/bson/lib/bson/code.js",
"node_modules/bson/lib/bson/db_ref.js",
"node_modules/bson/lib/bson/double.js",
"node_modules/bson/lib/bson/long.js",
"node_modules/bson/lib/bson/objectid.js",
"node_modules/bson/lib/bson/symbol.js",
"node_modules/bson/lib/bson/timestamp.js",
"node_modules/bson/lib/bson/max_key.js",
"node_modules/bson/lib/bson/min_key.js"
]
},
"templates": {
"cleverLinks": true,
"monospaceLinks": true,
"default": {
"outputSourceFiles" : true
},
"applicationName": "Node.js MongoDB Driver API",
"disqus": true,
"googleAnalytics": "UA-29229787-1",
"openGraph": {
"title": "",
"type": "website",
"image": "",
"site_name": "",
"url": ""
},
"meta": {
"title": "",
"description": "",
"keyword": ""
},
"linenums": true
},
"markdown": {
"parser": "gfm",
"hardwrap": true,
"tags": ["examples"]
},
"examples": {
"indent": 4
}
}
-32
View File
@@ -1,32 +0,0 @@
var BSON = require('bson');
var require_optional = require('require_optional');
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) {}
module.exports = {
MongoError: require('./lib/error')
, 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')
, BSON: BSON
// Raw operations
, Query: require('./lib/connection/commands').Query
// Auth mechanisms
, 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')
}
-262
View File
@@ -1,262 +0,0 @@
"use strict";
var f = require('util').format
, require_optional = require('require_optional')
, Query = require('../connection/commands').Query
, MongoError = require('../error');
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;
}
// Kerberos class
var Kerberos = null;
var MongoAuthProcess = null;
// Try to grab the Kerberos class
try {
Kerberos = require_optional('kerberos').Kerberos;
// Authentication process for Mongo
MongoAuthProcess = require_optional('kerberos').processes.MongoAuthProcess;
} catch(err) {
}
/**
* 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;
// We don't have the Kerberos library
if(Kerberos == null) return callback(new Error("Kerberos library is not installed"));
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
GSSAPIInitialize(self, 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, 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;
@@ -1,181 +0,0 @@
"use strict";
var f = require('util').format
, crypto = require('crypto')
, Query = require('../connection/commands').Query
, MongoError = require('../error');
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;
-168
View File
@@ -1,168 +0,0 @@
"use strict";
var f = require('util').format
, retrieveBSON = require('../connection/utils').retrieveBSON
, Query = require('../connection/commands').Query
, MongoError = require('../error');
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;
-339
View File
@@ -1,339 +0,0 @@
"use strict";
var f = require('util').format
, crypto = require('crypto')
, retrieveBSON = require('../connection/utils').retrieveBSON
, Query = require('../connection/commands').Query
, MongoError = require('../error');
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 ScramSHA1 authentication mechanism
* @class
* @return {ScramSHA1} A cursor instance
*/
var ScramSHA1 = function(bson) {
this.bson = bson;
this.authStore = [];
this.id = id++;
}
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
var xor = function(a, b) {
if (!Buffer.isBuffer(a)) a = new Buffer(a)
if (!Buffer.isBuffer(b)) b = new Buffer(b)
var res = []
if (a.length > b.length) {
for (var i = 0; i < b.length; i++) {
res.push(a[i] ^ b[i])
}
} else {
for (i = 0; i < a.length; i++) {
res.push(a[i] ^ b[i])
}
}
return new Buffer(res);
}
var _hiCache = {};
var _hiCacheCount = 0;
var _hiCachePurge = function() {
_hiCache = {};
_hiCacheCount = 0;
};
var hi = function(data, salt, iterations) {
// omit the work if already generated
var key = [data, salt.toString('base64'), iterations].join('_');
if (_hiCache[key] !== undefined) {
return _hiCache[key];
}
// generate the salt
var saltedData = crypto.pbkdf2Sync(data, salt, iterations, 20, "sha1");
// 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}
*/
ScramSHA1.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;
// 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'
var firstBare = f("n=%s,r=%s", username, nonce);
// Build command structure
var cmd = {
saslStart: 1
, mechanism: 'SCRAM-SHA-1'
, payload: new Binary(f("n,,%s", 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 passwordDig = passwordDigest(username, password);
var saltedPassword = hi(passwordDig
, new Buffer(salt, 'base64')
, iterations);
// Create the client key
var hmac = crypto.createHmac('sha1', saltedPassword);
hmac.update(new Buffer("Client Key"));
var clientKey = new Buffer(hmac.digest('base64'), 'base64');
// Create the stored key
var hash = crypto.createHash('sha1');
hash.update(clientKey);
var storedKey = new Buffer(hash.digest('base64'), 'base64');
// Create the authentication message
var authMsg = [firstBare, r.result.payload.value().toString('base64'), withoutProof].join(',');
// Create client signature
hmac = crypto.createHmac('sha1', storedKey);
hmac.update(new Buffer(authMsg));
var clientSig = new Buffer(hmac.digest('base64'), 'base64');
// Create client proof
var clientProof = f("p=%s", new Buffer(xor(clientKey, clientSig)).toString('base64'));
// Create client final
var clientFinal = [withoutProof, clientProof].join(',');
//
// Create continue message
var cmd = {
saslContinue: 1
, conversationId: r.result.conversationId
, payload: new Binary(new Buffer(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: new Buffer(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}
*/
ScramSHA1.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}
*/
ScramSHA1.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);
}
});
}
}
module.exports = ScramSHA1;
-250
View File
@@ -1,250 +0,0 @@
"use strict";
var f = require('util').format
, require_optional = require('require_optional')
, Query = require('../connection/commands').Query
, MongoError = require('../error');
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;
}
// Kerberos class
var Kerberos = null;
var MongoAuthProcess = null;
// Try to grab the Kerberos class
try {
Kerberos = require_optional('kerberos').Kerberos
// Authentication process for Mongo
MongoAuthProcess = require_optional('kerberos').processes.MongoAuthProcess
} catch(err) {}
/**
* 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;
// We don't have the Kerberos library
if(Kerberos == null) return callback(new Error("Kerberos library is not installed"));
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, 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());
}
}
var SSIPAuthenticate = function(self, username, password, gssapiServiceName, server, connection, options, callback) {
// Build Authentication command to send to MongoDB
var command = {
saslStart: 1
, mechanism: 'GSSAPI'
, payload: ''
, autoAuthorize: 1
};
// Create authenticator
var mongo_auth_process = new MongoAuthProcess(connection.host, connection.port, gssapiServiceName, options);
// Execute first sasl step
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;
mongo_auth_process.init(username, password, function(err) {
if(err) return callback(err);
mongo_auth_process.transition(doc.payload, function(err, payload) {
if(err) return callback(err);
// Perform the next step against mongod
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);
var doc = r.result;
mongo_auth_process.transition(doc.payload, function(err, payload) {
if(err) return callback(err);
// Perform the next step against mongod
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);
var doc = r.result;
mongo_auth_process.transition(doc.payload, function(err, payload) {
// Perform the next step against mongod
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);
var doc = r.result;
if(doc.done) return callback(null, true);
callback(new Error("Authentication failed"), false);
});
});
});
});
});
});
});
});
}
// 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;
-164
View File
@@ -1,164 +0,0 @@
"use strict";
var f = require('util').format
, Query = require('../connection/commands').Query
, MongoError = require('../error');
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;
@@ -1,34 +0,0 @@
"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;
@@ -1,546 +0,0 @@
"use strict";
var retrieveBSON = require('../connection/utils').retrieveBSON;
var BSON = retrieveBSON();
var Long = BSON.Long;
// Incrementing request id
var _requestId = 0;
// Wire command operation ids
var OP_QUERY = 2004;
var OP_GETMORE = 2005;
var OP_KILL_CURSORS = 2007;
// 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 = 0;
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")) {
throw new Error("namespace cannot contain a null character");
}
// Basic options
this.bson = bson;
this.ns = ns;
this.query = query;
// Ensure empty options
this.options = options || {};
// Additional options
this.numberToSkip = options.numberToSkip || 0;
this.numberToReturn = options.numberToReturn || 0;
this.returnFieldSelector = options.returnFieldSelector || null;
this.requestId = Query.getRequestId();
// 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 = new Buffer(
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] = (OP_QUERY >> 24) & 0xff;
header[index + 2] = (OP_QUERY >> 16) & 0xff;
header[index + 1] = (OP_QUERY >> 8) & 0xff;
header[index] = (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 = new Buffer(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] = (OP_GETMORE >> 24) & 0xff;
_buffer[index + 2] = (OP_GETMORE >> 16) & 0xff;
_buffer[index + 1] = (OP_GETMORE >> 8) & 0xff;
_buffer[index] = (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, cursorIds) {
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 = new Buffer(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] = (OP_KILL_CURSORS >> 24) & 0xff;
_buffer[index + 2] = (OP_KILL_CURSORS >> 16) & 0xff;
_buffer[index + 1] = (OP_KILL_CURSORS >> 8) & 0xff;
_buffer[index] = (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, data, opts) {
opts = opts || {promoteLongs: true, promoteValues: true, promoteBuffers: false};
this.parsed = false;
//
// Parse Header
//
this.index = 0;
this.raw = data;
this.data = data;
this.bson = bson;
this.opts = opts;
// Read the message length
this.length = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// Fetch the request id for this reply
this.requestId = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// Fetch the id of the request that triggered the response
this.responseTo = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// Skip op-code field
this.index = this.index + 4;
// Unpack flags
this.responseFlags = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// Unpack the cursor
var lowBits = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
var highBits = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// Create long object
this.cursorId = new Long(lowBits, highBits);
// Unpack the starting from
this.startingFrom = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// Unpack the number of objects returned
this.numberReturned = data[this.index] | data[this.index + 1] << 8 | data[this.index + 2] << 16 | data[this.index + 3] << 24;
this.index = this.index + 4;
// 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
};
//
// 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);
// 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
}
@@ -1,619 +0,0 @@
"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
, Response = require('./commands').Response
, MongoError = require('../error')
, Logger = require('./logger');
var _id = 0;
var debugFields = ['host', 'port', 'size', 'keepAlive', 'keepAliveInitialDelay', 'noDelay'
, 'connectionTimeout', 'socketTimeout', 'singleBufferSerializtion', 'ssl', 'ca', 'crl', 'cert'
, 'rejectUnauthorized', 'promoteLongs', 'promoteValues', 'promoteBuffers', 'checkServerIdentity'];
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() {
connectionAccounting = true;
connections = {};
}
Connection.disableConnectionAccounting = function() {
connectionAccounting = false;
}
Connection.connections = function() {
return connections;
}
function deleteConnection(id) {
// console.log("=== deleted connection " + id + " :: " + (connections[id] ? connections[id].port : ''))
delete connections[id];
}
function addConnection(id, connection) {
// console.log("=== added connection " + id + " :: " + connection.port)
connections[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", MongoError.create(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"
, MongoError.create(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"
, MongoError.create(f("connection %s to %s:%s closed", self.id, self.host, self.port))
, 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 = new Buffer(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;
// Emit the buffer
self.messageHandler(new Response(self.bson, emitBuffer, self.responseOptions), self);
} 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 = new Buffer(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 = new Buffer(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 = new Buffer(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 = new Buffer(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 = new Buffer(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 = new Buffer(0);
// Emit the message
self.messageHandler(new Response(self.bson, emitBuffer, self.responseOptions), self);
} 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 = new Buffer(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
self.messageHandler(new Response(self.bson, emitBuffer, self.responseOptions), self);
}
} else {
// Create a buffer that contains the space for the non-complete message
self.stubBuffer = new Buffer(data.length)
// Copy the data to the stub buffer
data.copy(self.stubBuffer, 0);
// Exit parsing loop
data = new Buffer(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];
}
}
}
/**
* Connect
* @method
*/
Connection.prototype.connect = function(_options) {
var self = this;
_options = _options || {};
// Set the connections
if(connectionAccounting) addConnection(this.id, this);
// Check if we are overriding the promoteLongs
if(typeof _options.promoteLongs == 'boolean') {
self.responseOptions.promoteLongs = _options.promoteLongs;
self.responseOptions.promoteValues = _options.promoteValues;
self.responseOptions.promoteBuffers = _options.promoteBuffers;
}
// Create new connection instance
var connection_options;
if (self.domainSocket) {
connection_options = {path: self.host};
} else {
connection_options = {port: self.port, host: self.host};
if (self.family !== void 0) {
connection_options.family = self.family;
}
}
self.connection = net.createConnection(connection_options);
// Set the options for the connection
self.connection.setKeepAlive(self.keepAlive, self.keepAliveInitialDelay);
self.connection.setTimeout(self.connectionTimeout);
self.connection.setNoDelay(self.noDelay);
// If we have ssl enabled
if(self.ssl) {
var sslOptions = {
socket: self.connection
, rejectUnauthorized: self.rejectUnauthorized
}
// Merge in options
merge(sslOptions, this.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
self.connection = tls.connect(self.port, self.host, sslOptions, function() {
// Error on auth or skip
if(self.connection.authorizationError && self.rejectUnauthorized) {
return self.emit("error", self.connection.authorizationError, self, {ssl:true});
}
// Set socket timeout instead of connection timeout
self.connection.setTimeout(self.socketTimeout);
// We are done emit connect
self.emit('connect', self);
});
self.connection.setTimeout(self.connectionTimeout);
} else {
self.connection.once('connect', function() {
// Set socket timeout instead of connection timeout
self.connection.setTimeout(self.socketTimeout);
// Emit connect event
self.emit('connect', self);
});
}
// Add handlers for events
self.connection.once('error', errorHandler(self));
self.connection.once('timeout', timeoutHandler(self));
self.connection.once('close', closeHandler(self));
self.connection.on('data', dataHandler(self));
}
/**
* 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) {}
// 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;
@@ -1,228 +0,0 @@
"use strict";
var f = require('util').format
, MongoError = require('../error');
// 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;
File diff suppressed because it is too large Load Diff
@@ -1,86 +0,0 @@
"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) {}
return BSON;
}
exports.setProperty = setProperty;
exports.getProperty = getProperty;
exports.getSingleProperty = getSingleProperty;
exports.copy = copy;
exports.debugOptions = debugOptions;
exports.retrieveBSON = retrieveBSON;
-704
View File
@@ -1,704 +0,0 @@
"use strict";
var Logger = require('./connection/logger')
, retrieveBSON = require('./connection/utils').retrieveBSON
, MongoError = require('./error')
, f = require('util').format;
var BSON = retrieveBSON(),
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
}
// 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;
}
// 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;
}
//
// 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._find = function(callback) {
var self = this;
if(self.logger.isDebug()) {
self.logger.debug(f('issue initial query [%s] with flags [%s]'
, JSON.stringify(self.cmd)
, JSON.stringify(self.query)));
}
var queryCallback = function(err, r) {
if(err) return callback(err);
// Get the raw message
var result = r.message;
// Query failure bit set
if(result.queryFailure) {
return callback(MongoError.create(result.documents[0]), null);
}
// Check if we have a command cursor
if(Array.isArray(result.documents) && result.documents.length == 1
&& (!self.cmd.find || (self.cmd.find && self.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(MongoError.create(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) {
self.ns = result.documents[0].cursor.ns;
}
// Promote id to long if needed
self.cursorState.cursorId = typeof id == 'number' ? Long.fromNumber(id) : id;
self.cursorState.lastCursorId = self.cursorState.cursorId;
// If we have a firstBatch set it
if(Array.isArray(result.documents[0].cursor.firstBatch)) {
self.cursorState.documents = result.documents[0].cursor.firstBatch;//.reverse();
}
// Return after processing command cursor
return callback(null, null);
}
if(Array.isArray(result.documents[0].result)) {
self.cursorState.documents = result.documents[0].result;
self.cursorState.cursorId = Long.ZERO;
return callback(null, null);
}
}
// Otherwise fall back to regular find path
self.cursorState.cursorId = result.cursorId;
self.cursorState.documents = result.documents;
self.cursorState.lastCursorId = result.cursorId;
// Transform the results with passed in transformation method if provided
if(self.cursorState.transforms && typeof self.cursorState.transforms.query == 'function') {
self.cursorState.documents = self.cursorState.transforms.query(result);
}
// Return callback
callback(null, null);
}
// Options passed to the pool
var queryOptions = {};
// If we have a raw query decorate the function
if(self.options.raw || self.cmd.raw) {
// queryCallback.raw = self.options.raw || self.cmd.raw;
queryOptions.raw = self.options.raw || self.cmd.raw;
}
// Do we have documentsReturnedIn set on the query
if(typeof self.query.documentsReturnedIn == 'string') {
// queryCallback.documentsReturnedIn = self.query.documentsReturnedIn;
queryOptions.documentsReturnedIn = self.query.documentsReturnedIn;
}
// Add promote Long value if defined
if(typeof self.cursorState.promoteLongs == 'boolean') {
queryOptions.promoteLongs = self.cursorState.promoteLongs;
}
// Add promote values if defined
if(typeof self.cursorState.promoteValues == 'boolean') {
queryOptions.promoteValues = self.cursorState.promoteValues;
}
// Add promote values if defined
if(typeof self.cursorState.promoteBuffers == 'boolean') {
queryOptions.promoteBuffers = self.cursorState.promoteBuffers;
}
// Write the initial command out
self.server.s.pool.write(self.query, queryOptions, queryCallback);
}
Cursor.prototype._getmore = function(callback) {
if(this.logger.isDebug()) this.logger.debug(f('schedule getMore call for query [%s]', JSON.stringify(this.query)))
// Determine if it's a raw query
var raw = this.options.raw || this.cmd.raw;
// 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;
}
// Default pool
var pool = this.server.s.pool;
// We have a wire protocol handler
this.server.wireProtocolHandler.getMore(this.bson, this.ns, this.cursorState, batchSize, raw, pool, this.options, callback);
}
Cursor.prototype._killcursor = 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;
}
// Default pool
var pool = this.server.s.pool;
// Execute command
this.server.wireProtocolHandler.killCursor(this.bson, this.ns, this.cursorState.cursorId, pool, 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) {
this._killcursor(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.notified = true;
self.cursorState.killed = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
callback(MongoError.create(f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port)))
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.notified = true;
self.cursorState.killed = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
handleCallback(callback, null, null);
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, MongoError.create('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) {
self.cursorState.notified = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
handleCallback(callback, null, null);
return true;
}
return false;
}
/**
* Mark cursor as being dead and notified
*/
var setCursorDeadAndNotified = function(self, callback) {
self.cursorState.dead = true;
self.cursorState.notified = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
handleCallback(callback, null, null);
}
/**
* Mark cursor as being notified
*/
var setCursorNotified = function(self, callback) {
self.cursorState.notified = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
handleCallback(callback, null, null);
}
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) {
// 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(!self.topology.isConnected(self.options)) {
// Only need this for single server, because repl sets and mongos
// will always continue trying to reconnect
if (self.topology._type === 'server' && !self.topology.s.options.reconnect) {
// Reconnect is disabled, so we'll never reconnect
return callback(new MongoError('no connection available'));
}
if (self.disconnectHandler != null) {
if (self.topology.isDestroyed()) {
// Topology was destroyed, so don't try to wait for it to reconnect
return callback(new MongoError('Topology was destroyed'));
}
return self.disconnectHandler.addObjectAndMethod('cursor', self, 'next', [callback], callback);
}
}
try {
self.server = self.topology.getServer(self.options);
} catch(err) {
// Handle the error and add object to next method call
if(self.disconnectHandler != null) {
return self.disconnectHandler.addObjectAndMethod('cursor', self, 'next', [callback], callback);
}
// Otherwise return the error
return callback(err);
}
// Set as init
self.cursorState.init = true;
// Server does not support server
if(self.cmd
&& self.cmd.collation
&& self.server.ismaster.maxWireVersion < 5) {
return callback(new MongoError(f('server %s does not support collation', self.server.name)));
}
try {
self.query = self.server.wireProtocolHandler.command(self.bson, self.ns, self.cmd, self.cursorState, self.topology, self.options);
} catch(err) {
return callback(err);
}
}
// If we don't have a cursorId execute the first query
if(self.cursorState.cursorId == null) {
// Check if pool is dead and return if not possible to
// execute the query against the db
if(isConnectionDead(self, callback)) return;
// Check if topology is destroyed
if(self.topology.isDestroyed()) return callback(new MongoError('connection destroyed, not possible to instantiate cursor'));
// query, cmd, options, cursorState, callback
self._find(function(err) {
if(err) return handleCallback(callback, err, null);
if(self.cursorState.documents.length == 0
&& self.cursorState.cursorId && self.cursorState.cursorId.isZero()
&& !self.cmd.tailable && !self.cmd.awaitData) {
return setCursorNotified(self, callback);
}
nextFunction(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);
} 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 MongoError('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) return handleCallback(callback, err);
// 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, MongoError.create({
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, MongoError.create({
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);
}
}
/**
* 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;
-44
View File
@@ -1,44 +0,0 @@
"use strict";
/**
* Creates a new MongoError
* @class
* @augments Error
* @param {string} message The error message
* @return {MongoError} A MongoError instance
*/
function MongoError(message) {
this.name = 'MongoError';
this.message = message;
Error.captureStackTrace(this, MongoError);
}
/**
* Creates a new MongoError object
* @method
* @param {object} options The error options
* @return {MongoError} A MongoError instance
*/
MongoError.create = function(options) {
var err = null;
if(options instanceof Error) {
err = new MongoError(options.message);
err.stack = options.stack;
} else if(typeof options == 'string') {
err = new MongoError(options);
} else {
err = new MongoError(options.message || options.errmsg || options.$err || "n/a");
// Other options
for(var name in options) {
err[name] = options[name];
}
}
return err;
}
// Extend JavaScript error
MongoError.prototype = new Error;
module.exports = MongoError;
@@ -1,59 +0,0 @@
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;
};
File diff suppressed because it is too large Load Diff
@@ -1,118 +0,0 @@
"use strict";
var needSlaveOk = ['primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
/**
* @fileOverview The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
* used to construct connections.
*
* @example
* var ReplSet = require('mongodb-core').ReplSet
* , ReadPreference = require('mongodb-core').ReadPreference
* , assert = require('assert');
*
* var server = new ReplSet([{host: 'localhost', port: 30000}], {setName: 'rs'});
* // Wait for the connection event
* server.on('connect', function(server) {
* var cursor = server.cursor('db.test'
* , {find: 'db.test', query: {}}
* , {readPreference: new ReadPreference('secondary')});
* cursor.next(function(err, doc) {
* server.destroy();
* });
* });
*
* // Start connecting
* server.connect();
*/
/**
* Creates a new Pool instance
* @class
* @param {string} preference A string describing the preference (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 Stalleness in Seconds, Minimum value is 90 seconds.
* @property {string} preference The preference string (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
* @property {array} tags The tags object
* @property {object} options Additional read preference options
* @property {number} maxStalenessSeconds MaxStalenessSeconds value for the read preference
* @return {ReadPreference}
*/
var ReadPreference = function(preference, tags, options) {
this.preference = preference;
this.tags = tags;
this.options = options;
// Add the maxStalenessSeconds value to the read Preference
if(this.options && this.options.maxStalenessSeconds != null) {
this.options = options;
this.maxStalenessSeconds = this.options.maxStalenessSeconds >= 0
? this.options.maxStalenessSeconds : null;
} else if(tags && typeof tags == 'object') {
this.options = tags, tags = null;
}
}
/**
* This needs slaveOk bit set
* @method
* @return {boolean}
*/
ReadPreference.prototype.slaveOk = function() {
return needSlaveOk.indexOf(this.preference) != -1;
}
/**
* Are the two read preference equal
* @method
* @return {boolean}
*/
ReadPreference.prototype.equals = function(readPreference) {
return readPreference.preference == this.preference;
}
/**
* Return JSON representation
* @method
* @return {Object}
*/
ReadPreference.prototype.toJSON = function() {
var readPreference = {mode: this.preference};
if(Array.isArray(this.tags)) readPreference.tags = this.tags;
if(this.maxStalenessSeconds) readPreference.maxStalenessSeconds = this.maxStalenessSeconds;
return readPreference;
}
/**
* Primary read preference
* @method
* @return {ReadPreference}
*/
ReadPreference.primary = new ReadPreference('primary');
/**
* Primary Preferred read preference
* @method
* @return {ReadPreference}
*/
ReadPreference.primaryPreferred = new ReadPreference('primaryPreferred');
/**
* Secondary read preference
* @method
* @return {ReadPreference}
*/
ReadPreference.secondary = new ReadPreference('secondary');
/**
* Secondary Preferred read preference
* @method
* @return {ReadPreference}
*/
ReadPreference.secondaryPreferred = new ReadPreference('secondaryPreferred');
/**
* Nearest read preference
* @method
* @return {ReadPreference}
*/
ReadPreference.nearest = new ReadPreference('nearest');
module.exports = ReadPreference;
File diff suppressed because it is too large Load Diff
@@ -1,999 +0,0 @@
"use strict"
var inherits = require('util').inherits,
f = require('util').format,
diff = require('./shared').diff,
EventEmitter = require('events').EventEmitter,
Logger = require('../connection/logger'),
ReadPreference = require('./read_preference'),
MongoError = require('../error');
var TopologyType = {
'Single': 'Single', 'ReplicaSetNoPrimary': 'ReplicaSetNoPrimary',
'ReplicaSetWithPrimary': 'ReplicaSetWithPrimary', 'Sharded': 'Sharded',
'Unknown': 'Unknown'
};
var ServerType = {
'Standalone': 'Standalone', 'Mongos': 'Mongos', 'PossiblePrimary': 'PossiblePrimary',
'RSPrimary': 'RSPrimary', 'RSSecondary': 'RSSecondary', 'RSArbiter': 'RSArbiter',
'RSOther': 'RSOther', 'RSGhost': 'RSGhost', 'Unknown': 'Unknown'
};
var ReplSetState = function(options) {
options = options || {};
// Add event listener
EventEmitter.call(this);
// Topology state
this.topologyType = TopologyType.ReplicaSetNoPrimary;
this.setName = options.setName;
// Server set
this.set = {};
// Unpacked options
this.id = options.id;
this.setName = options.setName;
// Replicaset logger
this.logger = options.logger || Logger('ReplSet', options);
// Server selection index
this.index = 0;
// Acceptable latency
this.acceptableLatency = options.acceptableLatency || 15;
// heartbeatFrequencyMS
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS || 10000;
// Server side
this.primary = null;
this.secondaries = [];
this.arbiters = [];
this.passives = [];
this.ghosts = [];
// Current unknown hosts
this.unknownServers = [];
// In set status
this.set = {};
// Status
this.maxElectionId = null;
this.maxSetVersion = 0;
// Description of the Replicaset
this.replicasetDescription = {
"topologyType": "Unknown", "servers": []
};
}
inherits(ReplSetState, EventEmitter);
ReplSetState.prototype.hasPrimaryAndSecondary = function() {
return this.primary != null && this.secondaries.length > 0;
}
ReplSetState.prototype.hasPrimaryOrSecondary = function() {
return this.hasPrimary() || this.hasSecondary();
}
ReplSetState.prototype.hasPrimary = function() {
return this.primary != null;
}
ReplSetState.prototype.hasSecondary = function() {
return this.secondaries.length > 0;
}
ReplSetState.prototype.get = function(host) {
var servers = this.allServers();
for(var i = 0; i < servers.length; i++) {
if(servers[i].name.toLowerCase() === host.toLowerCase()) {
return servers[i];
}
}
return null;
}
ReplSetState.prototype.allServers = function(options) {
options = options || {};
var servers = this.primary ? [this.primary] : [];
servers = servers.concat(this.secondaries);
if(!options.ignoreArbiters) servers = servers.concat(this.arbiters);
servers = servers.concat(this.passives);
return servers;
}
ReplSetState.prototype.destroy = function(options) {
// Destroy all sockets
if(this.primary) this.primary.destroy(options);
this.secondaries.forEach(function(x) { x.destroy(options); });
this.arbiters.forEach(function(x) { x.destroy(options); });
this.passives.forEach(function(x) { x.destroy(options); });
this.ghosts.forEach(function(x) { x.destroy(options); });
// Clear out the complete state
this.secondaries = [];
this.arbiters = [];
this.passives = [];
this.ghosts = [];
this.unknownServers = [];
this.set = {};
this.primary = null;
// Emit the topology changed
emitTopologyDescriptionChanged(this);
}
ReplSetState.prototype.remove = function(server, options) {
options = options || {};
// Get the server name and lowerCase it
var serverName = server.name.toLowerCase();
// Only remove if the current server is not connected
var servers = this.primary ? [this.primary] : [];
servers = servers.concat(this.secondaries);
servers = servers.concat(this.arbiters);
servers = servers.concat(this.passives);
// Check if it's active and this is just a failed connection attempt
for(var i = 0; i < servers.length; i++) {
if(!options.force
&& servers[i].equals(server)
&& servers[i].isConnected
&& servers[i].isConnected()) {
return;
}
}
// If we have it in the set remove it
if(this.set[serverName]) {
this.set[serverName].type = ServerType.Unknown;
this.set[serverName].electionId = null;
this.set[serverName].setName = null;
this.set[serverName].setVersion = null;
}
// Remove type
var removeType = null;
// Remove from any lists
if(this.primary && this.primary.equals(server)) {
this.primary = null;
this.topologyType = TopologyType.ReplicaSetNoPrimary;
removeType = 'primary';
}
// Remove from any other server lists
removeType = removeFrom(server, this.secondaries) ? 'secondary' : removeType;
removeType = removeFrom(server, this.arbiters) ? 'arbiter' : removeType;
removeType = removeFrom(server, this.passives) ? 'secondary' : removeType;
removeFrom(server, this.ghosts);
removeFrom(server, this.unknownServers);
// Push to unknownServers
this.unknownServers.push(serverName);
// Do we have a removeType
if(removeType) {
this.emit('left', removeType, server);
}
}
ReplSetState.prototype.update = function(server) {
var self = this;
// Get the current ismaster
var ismaster = server.lastIsMaster();
// Get the server name and lowerCase it
var serverName = server.name.toLowerCase();
//
// Add any hosts
//
if(ismaster) {
// Join all the possible new hosts
var hosts = Array.isArray(ismaster.hosts) ? ismaster.hosts : [];
hosts = hosts.concat(Array.isArray(ismaster.arbiters) ? ismaster.arbiters : []);
hosts = hosts.concat(Array.isArray(ismaster.passives) ? ismaster.passives : []);
hosts = hosts.map(function(s) { return s.toLowerCase() });
// Add all hosts as unknownServers
for(var i = 0; i < hosts.length; i++) {
// Add to the list of unknown server
if(this.unknownServers.indexOf(hosts[i]) == -1
&& (!this.set[hosts[i]] || this.set[hosts[i]].type == ServerType.Unknown)) {
this.unknownServers.push(hosts[i].toLowerCase());
}
if(!this.set[hosts[i]]) {
this.set[hosts[i]] = {
type: ServerType.Unknown,
electionId: null,
setName: null,
setVersion: null
}
}
}
}
//
// Unknown server
//
if(!ismaster && !inList(ismaster, server, this.unknownServers)) {
self.set[serverName] = {
type: ServerType.Unknown, setVersion: null, electionId: null, setName: null
}
// Update set information about the server instance
self.set[serverName].type = ServerType.Unknown;
self.set[serverName].electionId = ismaster ? ismaster.electionId : ismaster;
self.set[serverName].setName = ismaster ? ismaster.setName : ismaster;
self.set[serverName].setVersion = ismaster ? ismaster.setVersion : ismaster;
if(self.unknownServers.indexOf(server.name) == -1) {
self.unknownServers.push(serverName);
}
// Set the topology
return false;
}
//
// Is this a mongos
//
if(ismaster && ismaster.msg == 'isdbgrid') {
return false;
}
// A RSOther instance
if((ismaster.setName && ismaster.hidden)
|| (ismaster.setName && !ismaster.ismaster && !ismaster.secondary && !ismaster.arbiterOnly && !ismaster.passive)) {
self.set[serverName] = {
type: ServerType.RSOther, setVersion: null,
electionId: null, setName: ismaster.setName
}
// Set the topology
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.ReplicaSetNoPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
return false;
}
// A RSGhost instance
if(ismaster.isreplicaset) {
self.set[serverName] = {
type: ServerType.RSGhost, setVersion: null,
electionId: null, setName: null
}
// Set the topology
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.ReplicaSetNoPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
// Set the topology
return false;
}
//
// Standalone server, destroy and return
//
if(ismaster && ismaster.ismaster && !ismaster.setName) {
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.Unknown;
this.remove(server, {force:true});
return false;
}
//
// Server in maintanance mode
//
if(ismaster && !ismaster.ismaster && !ismaster.secondary && !ismaster.arbiterOnly) {
this.remove(server, {force:true});
return false;
}
//
// If the .me field does not match the passed in server
//
if(ismaster.me && ismaster.me.toLowerCase() != serverName) {
if(this.logger.isWarn()) {
this.logger.warn(f('the seedlist server was removed due to its address %s not matching its ismaster.me address %s', server.name, ismaster.me));
}
// Delete from the set
delete this.set[serverName];
// Delete unknown servers
removeFrom(server, self.unknownServers);
// Destroy the instance
server.destroy();
// Set the type of topology we have
if(this.primary && !this.primary.equals(server)) {
this.topologyType = TopologyType.ReplicaSetWithPrimary;
} else {
this.topologyType = TopologyType.ReplicaSetNoPrimary;
}
//
// We have a potential primary
//
if(!this.primary && ismaster.primary) {
this.set[ismaster.primary.toLowerCase()] = {
type: ServerType.PossiblePrimary,
setName: null,
electionId: null,
setVersion: null,
}
}
return false;
}
//
// Primary handling
//
if(!this.primary && ismaster.ismaster && ismaster.setName) {
var ismasterElectionId = server.lastIsMaster().electionId;
if(this.setName && this.setName != ismaster.setName) {
this.topologyType = TopologyType.ReplicaSetNoPrimary;
return new MongoError(f('setName from ismaster does not match provided connection setName [%s] != [%s]', ismaster.setName, this.setName));
}
if(!this.maxElectionId && ismasterElectionId) {
this.maxElectionId = ismasterElectionId;
} else if(this.maxElectionId && ismasterElectionId) {
var result = compareObjectIds(this.maxElectionId, ismasterElectionId);
// Get the electionIds
var ismasterSetVersion = server.lastIsMaster().setVersion;
if(result == 1) {
this.topologyType = TopologyType.ReplicaSetNoPrimary;
return false;
} else if(result == 0 && ismasterSetVersion) {
if(ismasterSetVersion < this.maxSetVersion) {
this.topologyType = TopologyType.ReplicaSetNoPrimary;
return false;
}
}
this.maxSetVersion = ismasterSetVersion;
this.maxElectionId = ismasterElectionId;
}
// Hande normalization of server names
var normalizedHosts = ismaster.hosts.map(function(x) { return x.toLowerCase() });
var locationIndex = normalizedHosts.indexOf(serverName);
// Validate that the server exists in the host list
if(locationIndex != -1) {
self.primary = server;
self.set[serverName] = {
type: ServerType.RSPrimary,
setVersion: ismaster.setVersion,
electionId: ismaster.electionId,
setName: ismaster.setName
}
// Set the topology
this.topologyType = TopologyType.ReplicaSetWithPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
removeFrom(server, self.unknownServers);
removeFrom(server, self.secondaries);
removeFrom(server, self.passives);
self.emit('joined', 'primary', server);
} else {
this.topologyType = TopologyType.ReplicaSetNoPrimary;
}
emitTopologyDescriptionChanged(self);
return true;
} else if(ismaster.ismaster && ismaster.setName) {
// Get the electionIds
var currentElectionId = self.set[self.primary.name.toLowerCase()].electionId;
var currentSetVersion = self.set[self.primary.name.toLowerCase()].setVersion;
var currentSetName = self.set[self.primary.name.toLowerCase()].setName;
ismasterElectionId = server.lastIsMaster().electionId;
ismasterSetVersion = server.lastIsMaster().setVersion;
var ismasterSetName = server.lastIsMaster().setName;
// Is it the same server instance
if(this.primary.equals(server)
&& currentSetName == ismasterSetName) {
return false;
}
// If we do not have the same rs name
if(currentSetName && currentSetName != ismasterSetName) {
if(!this.primary.equals(server)) {
this.topologyType = TopologyType.ReplicaSetWithPrimary;
} else {
this.topologyType = TopologyType.ReplicaSetNoPrimary;
}
return false;
}
// Check if we need to replace the server
if(currentElectionId && ismasterElectionId) {
result = compareObjectIds(currentElectionId, ismasterElectionId);
if(result == 1) {
return false;
} else if(result == 0 && (currentSetVersion > ismasterSetVersion)) {
return false;
}
} else if(!currentElectionId && ismasterElectionId
&& ismasterSetVersion) {
if(ismasterSetVersion < this.maxSetVersion) {
return false;
}
}
if(!this.maxElectionId && ismasterElectionId) {
this.maxElectionId = ismasterElectionId;
} else if(this.maxElectionId && ismasterElectionId) {
result = compareObjectIds(this.maxElectionId, ismasterElectionId);
if(result == 1) {
return false;
} else if(result == 0 && currentSetVersion && ismasterSetVersion) {
if(ismasterSetVersion < this.maxSetVersion) {
return false;
}
} else {
if(ismasterSetVersion < this.maxSetVersion) {
return false;
}
}
this.maxElectionId = ismasterElectionId;
this.maxSetVersion = ismasterSetVersion;
} else {
this.maxSetVersion = ismasterSetVersion;
}
// Modify the entry to unknown
self.set[self.primary.name.toLowerCase()] = {
type: ServerType.Unknown, setVersion: null,
electionId: null, setName: null
}
// Signal primary left
self.emit('left', 'primary', this.primary);
// Destroy the instance
self.primary.destroy();
// Set the new instance
self.primary = server;
// Set the set information
self.set[serverName] = {
type: ServerType.RSPrimary, setVersion: ismaster.setVersion,
electionId: ismaster.electionId, setName: ismaster.setName
}
// Set the topology
this.topologyType = TopologyType.ReplicaSetWithPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
removeFrom(server, self.unknownServers);
removeFrom(server, self.secondaries);
removeFrom(server, self.passives);
self.emit('joined', 'primary', server);
emitTopologyDescriptionChanged(self);
return true;
}
// A possible instance
if(!this.primary && ismaster.primary) {
self.set[ismaster.primary.toLowerCase()] = {
type: ServerType.PossiblePrimary, setVersion: null,
electionId: null, setName: null
}
}
//
// Secondary handling
//
if(ismaster.secondary && ismaster.setName
&& !inList(ismaster, server, this.secondaries)
&& this.setName && this.setName == ismaster.setName) {
addToList(self, ServerType.RSSecondary, ismaster, server, this.secondaries);
// Set the topology
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.ReplicaSetNoPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
removeFrom(server, self.unknownServers);
// Remove primary
if(this.primary
&& this.primary.name.toLowerCase() == serverName) {
server.destroy();
this.primary = null;
self.emit('left', 'primary', server);
}
// Emit secondary joined replicaset
self.emit('joined', 'secondary', server);
emitTopologyDescriptionChanged(self);
return true;
}
//
// Arbiter handling
//
if(ismaster.arbiterOnly && ismaster.setName
&& !inList(ismaster, server, this.arbiters)
&& this.setName && this.setName == ismaster.setName) {
addToList(self, ServerType.RSArbiter, ismaster, server, this.arbiters);
// Set the topology
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.ReplicaSetNoPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
removeFrom(server, self.unknownServers);
self.emit('joined', 'arbiter', server);
emitTopologyDescriptionChanged(self);
return true;
}
//
// Passive handling
//
if(ismaster.passive && ismaster.setName
&& !inList(ismaster, server, this.passives)
&& this.setName && this.setName == ismaster.setName) {
addToList(self, ServerType.RSSecondary, ismaster, server, this.passives);
// Set the topology
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.ReplicaSetNoPrimary;
if(ismaster.setName) this.setName = ismaster.setName;
removeFrom(server, self.unknownServers);
// Remove primary
if(this.primary
&& this.primary.name.toLowerCase() == serverName) {
server.destroy();
this.primary = null;
self.emit('left', 'primary', server);
}
self.emit('joined', 'secondary', server);
emitTopologyDescriptionChanged(self);
return true;
}
//
// Remove the primary
//
if(this.set[serverName] && this.set[serverName].type == ServerType.RSPrimary) {
self.emit('left', 'primary', this.primary);
this.primary.destroy();
this.primary = null;
this.topologyType = TopologyType.ReplicaSetNoPrimary;
return false;
}
this.topologyType = this.primary ? TopologyType.ReplicaSetWithPrimary : TopologyType.ReplicaSetNoPrimary;
return false;
}
/**
* Recalculate single server max staleness
* @method
*/
ReplSetState.prototype.updateServerMaxStaleness = function(server, haInterval) {
// Locate the max secondary lastwrite
var max = 0;
// Go over all secondaries
for(var i = 0; i < this.secondaries.length; i++) {
max = Math.max(max, this.secondaries[i].lastWriteDate);
}
// Perform this servers staleness calculation
if(server.ismaster.maxWireVersion >= 5
&& server.ismaster.secondary
&& this.hasPrimary()) {
server.staleness = (server.lastUpdateTime - server.lastWriteDate)
- (this.primary.lastUpdateTime - this.primary.lastWriteDate)
+ haInterval;
} else if(server.ismaster.maxWireVersion >= 5
&& server.ismaster.secondary){
server.staleness = max - server.lastWriteDate + haInterval;
}
}
/**
* Recalculate all the staleness values for secodaries
* @method
*/
ReplSetState.prototype.updateSecondariesMaxStaleness = function(haInterval) {
for(var i = 0; i < this.secondaries.length; i++) {
this.updateServerMaxStaleness(this.secondaries[i], haInterval);
}
}
/**
* Pick a server by the passed in ReadPreference
* @method
* @param {ReadPreference} readPreference The ReadPreference instance to use
*/
ReplSetState.prototype.pickServer = function(readPreference) {
// If no read Preference set to primary by default
readPreference = readPreference || ReadPreference.primary;
// maxStalenessSeconds is not allowed with a primary read
if(readPreference.preference == 'primary' && readPreference.maxStalenessSeconds != null) {
return new MongoError('primary readPreference incompatible with maxStalenessSeconds');
}
// Check if we have any non compatible servers for maxStalenessSeconds
var allservers = this.primary ? [this.primary] : [];
allservers = allservers.concat(this.secondaries);
// Does any of the servers not support the right wire protocol version
// for maxStalenessSeconds when maxStalenessSeconds specified on readPreference. Then error out
if(readPreference.maxStalenessSeconds != null) {
for(var i = 0; i < allservers.length; i++) {
if(allservers[i].ismaster.maxWireVersion < 5) {
return new MongoError('maxStalenessSeconds not supported by at least one of the replicaset members');
}
}
}
// Do we have the nearest readPreference
if(readPreference.preference == 'nearest' && readPreference.maxStalenessSeconds == null) {
return pickNearest(this, readPreference);
} else if(readPreference.preference == 'nearest' && readPreference.maxStalenessSeconds != null) {
return pickNearestMaxStalenessSeconds(this, readPreference);
}
// Get all the secondaries
var secondaries = this.secondaries;
// Check if we can satisfy and of the basic read Preferences
if(readPreference.equals(ReadPreference.secondary)
&& secondaries.length == 0) {
return new MongoError("no secondary server available");
}
if(readPreference.equals(ReadPreference.secondaryPreferred)
&& secondaries.length == 0
&& this.primary == null) {
return new MongoError("no secondary or primary server available");
}
if(readPreference.equals(ReadPreference.primary)
&& this.primary == null) {
return new MongoError("no primary server available");
}
// Secondary preferred or just secondaries
if(readPreference.equals(ReadPreference.secondaryPreferred)
|| readPreference.equals(ReadPreference.secondary)) {
if(secondaries.length > 0 && readPreference.maxStalenessSeconds == null) {
// Pick nearest of any other servers available
var server = pickNearest(this, readPreference);
// No server in the window return primary
if(server) {
return server;
}
} else if(secondaries.length > 0 && readPreference.maxStalenessSeconds != null) {
// Pick nearest of any other servers available
server = pickNearestMaxStalenessSeconds(this, readPreference);
// No server in the window return primary
if(server) {
return server;
}
}
if(readPreference.equals(ReadPreference.secondaryPreferred)){
return this.primary;
}
return null;
}
// Primary preferred
if(readPreference.equals(ReadPreference.primaryPreferred)) {
server = null;
// We prefer the primary if it's available
if(this.primary) {
return this.primary;
}
// Pick a secondary
if(secondaries.length > 0 && readPreference.maxStalenessSeconds == null) {
server = pickNearest(this, readPreference);
} else if(secondaries.length > 0 && readPreference.maxStalenessSeconds != null) {
server = pickNearestMaxStalenessSeconds(this, readPreference);
}
// Did we find a server
if(server) return server;
}
// Return the primary
return this.primary;
}
//
// Filter serves by tags
var filterByTags = function(readPreference, servers) {
if(readPreference.tags == null) return servers;
var filteredServers = [];
var tagsArray = Array.isArray(readPreference.tags) ? readPreference.tags : [readPreference.tags];
// Iterate over the tags
for(var j = 0; j < tagsArray.length; j++) {
var tags = tagsArray[j];
// Iterate over all the servers
for(var i = 0; i < servers.length; i++) {
var serverTag = servers[i].lastIsMaster().tags || {};
// Did we find the a matching server
var found = true;
// Check if the server is valid
for(var name in tags) {
if(serverTag[name] != tags[name]) {
found = false;
}
}
// Add to candidate list
if(found) {
filteredServers.push(servers[i]);
}
}
}
// Returned filtered servers
return filteredServers;
}
function pickNearestMaxStalenessSeconds(self, readPreference) {
// Only get primary and secondaries as seeds
var servers = [];
var heartbeatFrequencyMS = self.heartbeatFrequencyMS;
// Get the maxStalenessMS
var maxStalenessMS = readPreference.maxStalenessSeconds * 1000;
// Check if the maxStalenessMS > 90 seconds
if(maxStalenessMS < 90 * 1000) {
return new MongoError('maxStalenessSeconds must be set to at least 90 seconds');
}
// Add primary to list if not a secondary read preference
if(self.primary
&& readPreference.preference != 'secondary'
&& readPreference.preference != 'secondaryPreferred') {
servers.push(self.primary);
}
// Add all the secondaries
for(var i = 0; i < self.secondaries.length; i++) {
servers.push(self.secondaries[i]);
}
// If we have a secondaryPreferred readPreference and no server add the primary
if(self.primary
&& servers.length == 0
&& readPreference.preference != 'secondaryPreferred') {
servers.push(self.primary);
}
// Filter by tags
servers = filterByTags(readPreference, servers);
//
// Locate lowest time (picked servers are lowest time + acceptable Latency margin)
// var lowest = servers.length > 0 ? servers[0].lastIsMasterMS : 0;
// Filter by latency
servers = servers.filter(function(s) {
return s.staleness <= maxStalenessMS;
});
// Sort by time
servers.sort(function(a, b) {
return a.lastIsMasterMS - b.lastIsMasterMS
});
// No servers, default to primary
if(servers.length == 0) {
return null
}
// Ensure index does not overflow the number of available servers
self.index = self.index % servers.length;
// Get the server
var server = servers[self.index];
// Add to the index
self.index = self.index + 1;
// Return the first server of the sorted and filtered list
return server;
}
function pickNearest(self, readPreference) {
// Only get primary and secondaries as seeds
var servers = [];
// Add primary to list if not a secondary read preference
if(self.primary
&& readPreference.preference != 'secondary'
&& readPreference.preference != 'secondaryPreferred') {
servers.push(self.primary);
}
// Add all the secondaries
for(var i = 0; i < self.secondaries.length; i++) {
servers.push(self.secondaries[i]);
}
// If we have a secondaryPreferred readPreference and no server add the primary
if(servers.length == 0
&& self.primary
&& readPreference.preference != 'secondaryPreferred') {
servers.push(self.primary);
}
// Filter by tags
servers = filterByTags(readPreference, servers);
// Sort by time
servers.sort(function(a, b) {
return a.lastIsMasterMS - b.lastIsMasterMS
});
// Locate lowest time (picked servers are lowest time + acceptable Latency margin)
var lowest = servers.length > 0 ? servers[0].lastIsMasterMS : 0;
// Filter by latency
servers = servers.filter(function(s) {
return s.lastIsMasterMS <= lowest + self.acceptableLatency;
});
// No servers, default to primary
if(servers.length == 0) {
return null
}
// Ensure index does not overflow the number of available servers
self.index = self.index % servers.length;
// Get the server
var server = servers[self.index];
// Add to the index
self.index = self.index + 1;
// Return the first server of the sorted and filtered list
return server;
}
function inList(ismaster, server, list) {
for(var i = 0; i < list.length; i++) {
if(list[i] && list[i].name
&& list[i].name.toLowerCase() == server.name.toLowerCase()) return true;
}
return false;
}
function addToList(self, type, ismaster, server, list) {
var serverName = server.name.toLowerCase();
// Update set information about the server instance
self.set[serverName].type = type;
self.set[serverName].electionId = ismaster ? ismaster.electionId : ismaster;
self.set[serverName].setName = ismaster ? ismaster.setName : ismaster;
self.set[serverName].setVersion = ismaster ? ismaster.setVersion : ismaster;
// Add to the list
list.push(server);
}
function compareObjectIds(id1, id2) {
var a = new Buffer(id1.toHexString(), 'hex');
var b = new Buffer(id2.toHexString(), 'hex');
if(a === b) {
return 0;
}
if(typeof Buffer.compare === 'function') {
return Buffer.compare(a, b);
}
var x = a.length;
var y = b.length;
var len = Math.min(x, y);
for (var i = 0; i < len; i++) {
if (a[i] !== b[i]) {
break;
}
}
if (i !== len) {
x = a[i];
y = b[i];
}
return x < y ? -1 : y < x ? 1 : 0;
}
function removeFrom(server, list) {
for(var i = 0; i < list.length; i++) {
if(list[i].equals && list[i].equals(server)) {
list.splice(i, 1);
return true;
} else if(typeof list[i] == 'string'
&& list[i].toLowerCase() == server.name.toLowerCase()) {
list.splice(i, 1);
return true;
}
}
return false;
}
function emitTopologyDescriptionChanged(self) {
if(self.listeners('topologyDescriptionChanged').length > 0) {
var topology = 'Unknown';
var setName = self.setName;
if(self.hasPrimaryAndSecondary()) {
topology = 'ReplicaSetWithPrimary';
} else if(!self.hasPrimary() && self.hasSecondary()) {
topology = 'ReplicaSetNoPrimary';
}
// Generate description
var description = {
topologyType: topology,
setName: setName,
servers: []
}
// Add the primary to the list
if(self.hasPrimary()) {
var desc = self.primary.getDescription();
desc.type = 'RSPrimary';
description.servers.push(desc);
}
// Add all the secondaries
description.servers = description.servers.concat(self.secondaries.map(function(x) {
var description = x.getDescription();
description.type = 'RSSecondary';
return description;
}));
// Add all the arbiters
description.servers = description.servers.concat(self.arbiters.map(function(x) {
var description = x.getDescription();
description.type = 'RSArbiter';
return description;
}));
// Add all the passives
description.servers = description.servers.concat(self.passives.map(function(x) {
var description = x.getDescription();
description.type = 'RSSecondary';
return description;
}));
// Get the diff
var diffResult = diff(self.replicasetDescription, description);
// Create the result
var result = {
topologyId: self.id,
previousDescription: self.replicasetDescription,
newDescription: description,
diff: diffResult,
};
// Emit the topologyDescription change
// if(diffResult.servers.length > 0) {
self.emit('topologyDescriptionChanged', result);
// }
// Set the new description
self.replicasetDescription = description;
}
}
module.exports = ReplSetState;
@@ -1,880 +0,0 @@
"use strict"
var inherits = require('util').inherits,
f = require('util').format,
EventEmitter = require('events').EventEmitter,
ReadPreference = require('./read_preference'),
Logger = require('../connection/logger'),
debugOptions = require('../connection/utils').debugOptions,
retrieveBSON = require('../connection/utils').retrieveBSON,
Pool = require('../connection/pool'),
Query = require('../connection/commands').Query,
MongoError = require('../error'),
PreTwoSixWireProtocolSupport = require('../wireprotocol/2_4_support'),
TwoSixWireProtocolSupport = require('../wireprotocol/2_6_support'),
ThreeTwoWireProtocolSupport = require('../wireprotocol/3_2_support'),
BasicCursor = require('../cursor'),
sdam = require('./shared'),
assign = require('../utils').assign,
createClientInfo = require('./shared').createClientInfo;
// Used for filtering out fields for loggin
var debugFields = ['reconnect', 'reconnectTries', 'reconnectInterval', 'emitError', 'cursorFactory', 'host'
, 'port', 'size', 'keepAlive', 'keepAliveInitialDelay', 'noDelay', 'connectionTimeout', 'checkServerIdentity'
, 'socketTimeout', 'singleBufferSerializtion', 'ssl', 'ca', 'crl', 'cert', 'key', 'rejectUnauthorized', 'promoteLongs', 'promoteValues'
, 'promoteBuffers', 'servername'];
// Server instance id
var id = 0;
var serverAccounting = false;
var servers = {};
var BSON = retrieveBSON();
/**
* Creates a new Server instance
* @class
* @param {boolean} [options.reconnect=true] Server will attempt to reconnect on loss of connection
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
* @param {number} [options.monitoring=true] Enable the server state monitoring (calling ismaster at monitoringInterval)
* @param {number} [options.monitoringInterval=5000] The interval of calling ismaster when monitoring is enabled.
* @param {Cursor} [options.cursorFactory=Cursor] The cursor factory class used for all query cursors
* @param {string} options.host The server host
* @param {number} options.port The server port
* @param {number} [options.size=5] Server connection pool size
* @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.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 {string} [options.servername=null] String containing the server name requested via TLS SNI.
* @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.
* @param {string} [options.appname=null] Application name, passed in on ismaster call and logged in mongod server logs. Maximum size 128 bytes.
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
* @return {Server} A cursor instance
* @fires Server#connect
* @fires Server#close
* @fires Server#error
* @fires Server#timeout
* @fires Server#parseError
* @fires Server#reconnect
* @fires Server#reconnectFailed
* @fires Server#serverHeartbeatStarted
* @fires Server#serverHeartbeatSucceeded
* @fires Server#serverHeartbeatFailed
* @fires Server#topologyOpening
* @fires Server#topologyClosed
* @fires Server#topologyDescriptionChanged
* @property {string} type the topology type.
* @property {string} parserType the parser type used (c++ or js).
*/
var Server = function(options) {
options = options || {};
// Add event listener
EventEmitter.call(this);
// Server instance id
this.id = id++;
// Internal state
this.s = {
// Options
options: options,
// Logger
logger: Logger('Server', options),
// Factory overrides
Cursor: options.cursorFactory || BasicCursor,
// BSON instance
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]),
// Pool
pool: null,
// Disconnect handler
disconnectHandler: options.disconnectHandler,
// Monitor thread (keeps the connection alive)
monitoring: typeof options.monitoring == 'boolean' ? options.monitoring : true,
// Is the server in a topology
inTopology: typeof options.inTopology == 'boolean' ? options.inTopology : false,
// Monitoring timeout
monitoringInterval: typeof options.monitoringInterval == 'number'
? options.monitoringInterval
: 5000,
// Topology id
topologyId: -1
}
// Curent ismaster
this.ismaster = null;
// Current ping time
this.lastIsMasterMS = -1;
// The monitoringProcessId
this.monitoringProcessId = null;
// Initial connection
this.initalConnect = true;
// Wire protocol handler, default to oldest known protocol handler
// this gets changed when the first ismaster is called.
this.wireProtocolHandler = new PreTwoSixWireProtocolSupport();
// Default type
this._type = 'server';
// Set the client info
this.clientInfo = createClientInfo(options);
// Max Stalleness values
// last time we updated the ismaster state
this.lastUpdateTime = 0;
// Last write time
this.lastWriteDate = 0;
// Stalleness
this.staleness = 0;
}
inherits(Server, EventEmitter);
Object.defineProperty(Server.prototype, 'type', {
enumerable:true, get: function() { return this._type; }
});
Object.defineProperty(Server.prototype, 'parserType', {
enumerable:true, get: function() {
return BSON.native ? "c++" : "js";
}
});
Server.enableServerAccounting = function() {
serverAccounting = true;
servers = {};
}
Server.disableServerAccounting = function() {
serverAccounting = false;
}
Server.servers = function() {
return servers;
}
Object.defineProperty(Server.prototype, 'name', {
enumerable:true,
get: function() { return this.s.options.host + ":" + this.s.options.port; }
});
function configureWireProtocolHandler(self, ismaster) {
// 3.2 wire protocol handler
if(ismaster.maxWireVersion >= 4) {
return new ThreeTwoWireProtocolSupport(new TwoSixWireProtocolSupport());
}
// 2.6 wire protocol handler
if(ismaster.maxWireVersion >= 2) {
return new TwoSixWireProtocolSupport();
}
// 2.4 or earlier wire protocol handler
return new PreTwoSixWireProtocolSupport();
}
function disconnectHandler(self, type, ns, cmd, options, 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(!self.s.pool.isConnected() && self.s.options.reconnect && self.s.disconnectHandler != null && !options.monitoring) {
self.s.disconnectHandler.add(type, ns, cmd, options, callback);
return true;
}
// If we have no connection error
if(!self.s.pool.isConnected()) {
callback(MongoError.create(f("no connection available to server %s", self.name)));
return true;
}
}
function monitoringProcess(self) {
return function() {
// Pool was destroyed do not continue process
if(self.s.pool.isDestroyed()) return;
// Emit monitoring Process event
self.emit('monitoring', self);
// Perform ismaster call
// Query options
var queryOptions = { numberToSkip: 0, numberToReturn: -1, checkKeys: false, slaveOk: true };
// Create a query instance
var query = new Query(self.s.bson, 'admin.$cmd', {ismaster:true}, queryOptions);
// Get start time
var start = new Date().getTime();
// Execute the ismaster query
self.s.pool.write(query, {
socketTimeout: (typeof self.s.options.connectionTimeout !== 'number') ? 2000 : self.s.options.connectionTimeout,
monitoring: true,
}, function(err, result) {
// Set initial lastIsMasterMS
self.lastIsMasterMS = new Date().getTime() - start;
if(self.s.pool.isDestroyed()) return;
// Update the ismaster view if we have a result
if(result) {
self.ismaster = result.result;
}
// Re-schedule the monitoring process
self.monitoringProcessId = setTimeout(monitoringProcess(self), self.s.monitoringInterval);
});
}
}
var eventHandler = function(self, event) {
return function(err) {
// Log information of received information if in info mode
if(self.s.logger.isInfo()) {
var object = err instanceof MongoError ? JSON.stringify(err) : {}
self.s.logger.info(f('server %s fired event %s out with message %s'
, self.name, event, object));
}
// Handle connect event
if(event == 'connect') {
// Issue an ismaster command at connect
// Query options
var queryOptions = { numberToSkip: 0, numberToReturn: -1, checkKeys: false, slaveOk: true };
// Create a query instance
var query = new Query(self.s.bson, 'admin.$cmd', {ismaster:true, client: self.clientInfo}, queryOptions);
// Get start time
var start = new Date().getTime();
// Execute the ismaster query
self.s.pool.write(query, {
socketTimeout: self.s.options.connectionTimeout || 2000,
}, function(err, result) {
// Set initial lastIsMasterMS
self.lastIsMasterMS = new Date().getTime() - start;
if(err) {
self.destroy();
if(self.listeners('error').length > 0) self.emit('error', err);
return;
}
// Ensure no error emitted after initial connect when reconnecting
self.initalConnect = false;
// Save the ismaster
self.ismaster = result.result;
// It's a proxy change the type so
// the wireprotocol will send $readPreference
if(self.ismaster.msg == 'isdbgrid') {
self._type = 'mongos';
}
// Add the correct wire protocol handler
self.wireProtocolHandler = configureWireProtocolHandler(self, self.ismaster);
// Have we defined self monitoring
if(self.s.monitoring) {
self.monitoringProcessId = setTimeout(monitoringProcess(self), self.s.monitoringInterval);
}
// Emit server description changed if something listening
sdam.emitServerDescriptionChanged(self, {
address: self.name, arbiters: [], hosts: [], passives: [], type: sdam.getTopologyType(self)
});
if(!self.s.inTopology) {
// Emit topology description changed if something listening
sdam.emitTopologyDescriptionChanged(self, {
topologyType: 'Single', servers: [{address: self.name, arbiters: [], hosts: [], passives: [], type: sdam.getTopologyType(self)}]
});
}
// Log the ismaster if available
if(self.s.logger.isInfo()) {
self.s.logger.info(f('server %s connected with ismaster [%s]', self.name, JSON.stringify(self.ismaster)));
}
// Emit connect
self.emit('connect', self);
});
} else if(event == 'error' || event == 'parseError'
|| event == 'close' || event == 'timeout' || event == 'reconnect'
|| event == 'attemptReconnect' || 'reconnectFailed') {
// Remove server instance from accounting
if(serverAccounting && ['close', 'timeout', 'error', 'parseError', 'reconnectFailed'].indexOf(event) != -1) {
// Emit toplogy opening event if not in topology
if(!self.s.inTopology) {
self.emit('topologyOpening', { topologyId: self.id });
}
delete servers[self.id];
}
if (event === 'close') {
// Closing emits a server description changed event going to unknown.
sdam.emitServerDescriptionChanged(self, {
address: self.name, arbiters: [], hosts: [], passives: [], type: 'Unknown'
});
}
// Reconnect failed return error
if(event == 'reconnectFailed') {
self.emit('reconnectFailed', err);
// Emit error if any listeners
if(self.listeners('error').length > 0) {
self.emit('error', err);
}
// Terminate
return;
}
// On first connect fail
if(self.s.pool.state == 'disconnected' && self.initalConnect && ['close', 'timeout', 'error', 'parseError'].indexOf(event) != -1) {
self.initalConnect = false;
return self.emit('error', new MongoError(f('failed to connect to server [%s] on first connect [%s]', self.name, err)));
}
// Reconnect event, emit the server
if(event == 'reconnect') {
// Reconnecting emits a server description changed event going from unknown to the
// current server type.
sdam.emitServerDescriptionChanged(self, {
address: self.name, arbiters: [], hosts: [], passives: [], type: sdam.getTopologyType(self)
});
return self.emit(event, self);
}
// Emit the event
self.emit(event, err);
}
}
}
/**
* Initiate server connect
* @method
* @param {array} [options.auth=null] Array of auth options to apply on connect
*/
Server.prototype.connect = function(options) {
var self = this;
options = options || {};
// Set the connections
if(serverAccounting) servers[this.id] = this;
// Do not allow connect to be called on anything that's not disconnected
if(self.s.pool && !self.s.pool.isDisconnected() && !self.s.pool.isDestroyed()) {
throw MongoError.create(f('server instance in invalid state %s', self.s.pool.state));
}
// Create a pool
self.s.pool = new Pool(assign(self.s.options, options, {bson: this.s.bson}));
// Set up listeners
self.s.pool.on('close', eventHandler(self, 'close'));
self.s.pool.on('error', eventHandler(self, 'error'));
self.s.pool.on('timeout', eventHandler(self, 'timeout'));
self.s.pool.on('parseError', eventHandler(self, 'parseError'));
self.s.pool.on('connect', eventHandler(self, 'connect'));
self.s.pool.on('reconnect', eventHandler(self, 'reconnect'));
self.s.pool.on('reconnectFailed', eventHandler(self, 'reconnectFailed'));
// Emit toplogy opening event if not in topology
if(!self.s.inTopology) {
this.emit('topologyOpening', { topologyId: self.id });
}
// Emit opening server event
self.emit('serverOpening', {
topologyId: self.s.topologyId != -1 ? self.s.topologyId : self.id,
address: self.name
});
// Connect with optional auth settings
if(options.auth) {
self.s.pool.connect.apply(self.s.pool, options.auth);
} else {
self.s.pool.connect();
}
}
/**
* Get the server description
* @method
* @return {object}
*/
Server.prototype.getDescription = function() {
var ismaster = this.ismaster || {};
var description = {
type: sdam.getTopologyType(this),
address: this.name,
};
// Add fields if available
if(ismaster.hosts) description.hosts = ismaster.hosts;
if(ismaster.arbiters) description.arbiters = ismaster.arbiters;
if(ismaster.passives) description.passives = ismaster.passives;
if(ismaster.setName) description.setName = ismaster.setName;
return description;
}
/**
* Returns the last known ismaster document for this server
* @method
* @return {object}
*/
Server.prototype.lastIsMaster = function() {
return this.ismaster;
}
/**
* Unref all connections belong to this server
* @method
*/
Server.prototype.unref = function() {
this.s.pool.unref();
}
/**
* Figure out if the server is connected
* @method
* @return {boolean}
*/
Server.prototype.isConnected = function() {
if(!this.s.pool) return false;
return this.s.pool.isConnected();
}
/**
* Figure out if the server instance was destroyed by calling destroy
* @method
* @return {boolean}
*/
Server.prototype.isDestroyed = function() {
if(!this.s.pool) return false;
return this.s.pool.isDestroyed();
}
function basicWriteValidations(self) {
if(!self.s.pool) return MongoError.create('server instance is not connected');
if(self.s.pool.isDestroyed()) return MongoError.create('server instance pool was destroyed');
}
function basicReadValidations(self, options) {
basicWriteValidations(self, options);
if(options.readPreference && !(options.readPreference instanceof ReadPreference)) {
throw new Error("readPreference must be an instance of ReadPreference");
}
}
/**
* 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 {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 {opResultCallback} callback A callback function
*/
Server.prototype.command = function(ns, cmd, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {}, options = options || {};
var result = basicReadValidations(self, options);
if(result) return callback(result);
// Clone the options
options = assign({}, options, { wireProtocolCommand: false });
// Debug log
if(self.s.logger.isDebug()) self.s.logger.debug(f('executing command [%s] against %s', JSON.stringify({
ns: ns, cmd: cmd, options: debugOptions(debugFields, options)
}), self.name));
// If we are not connected or have a disconnectHandler specified
if(disconnectHandler(self, 'command', ns, cmd, options, callback)) return;
// Check if we have collation support
if(this.ismaster && this.ismaster.maxWireVersion < 5 && cmd.collation) {
return callback(new MongoError(f('server %s does not support collation', this.name)));
}
// Query options
var queryOptions = {
numberToSkip: 0,
numberToReturn: -1,
checkKeys: typeof options.checkKeys == 'boolean' ? options.checkKeys: false,
serializeFunctions: typeof options.serializeFunctions == 'boolean' ? options.serializeFunctions : false,
ignoreUndefined: typeof options.ignoreUndefined == 'boolean' ? options.ignoreUndefined : false
};
// Are we executing against a specific topology
var topology = options.topology || {};
// Create the query object
var query = self.wireProtocolHandler.command(self.s.bson, ns, cmd, {}, topology, options);
// Set slave OK of the query
query.slaveOk = options.readPreference ? options.readPreference.slaveOk() : false;
// Write options
var 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,
};
// Write the operation to the pool
self.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 {opResultCallback} callback A callback function
*/
Server.prototype.insert = function(ns, ops, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {}, options = options || {};
var result = basicWriteValidations(self, options);
if(result) return callback(result);
// If we are not connected or have a disconnectHandler specified
if(disconnectHandler(self, 'insert', ns, ops, options, callback)) return;
// Setup the docs as an array
ops = Array.isArray(ops) ? ops : [ops];
// Execute write
return self.wireProtocolHandler.insert(self.s.pool, self.ismaster, ns, self.s.bson, 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 {opResultCallback} callback A callback function
*/
Server.prototype.update = function(ns, ops, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {}, options = options || {};
var result = basicWriteValidations(self, options);
if(result) return callback(result);
// If we are not connected or have a disconnectHandler specified
if(disconnectHandler(self, 'update', ns, ops, options, callback)) return;
// Check if we have collation support
if(this.ismaster && this.ismaster.maxWireVersion < 5 && options.collation) {
return callback(new MongoError(f('server %s does not support collation', this.name)));
}
// Setup the docs as an array
ops = Array.isArray(ops) ? ops : [ops];
// Execute write
return self.wireProtocolHandler.update(self.s.pool, self.ismaster, ns, self.s.bson, 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 {opResultCallback} callback A callback function
*/
Server.prototype.remove = function(ns, ops, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {}, options = options || {};
var result = basicWriteValidations(self, options);
if(result) return callback(result);
// If we are not connected or have a disconnectHandler specified
if(disconnectHandler(self, 'remove', ns, ops, options, callback)) return;
// Check if we have collation support
if(this.ismaster && this.ismaster.maxWireVersion < 5 && options.collation) {
return callback(new MongoError(f('server %s does not support collation', this.name)));
}
// Setup the docs as an array
ops = Array.isArray(ops) ? ops : [ops];
// Execute write
return self.wireProtocolHandler.remove(self.s.pool, self.ismaster, ns, self.s.bson, ops, options, callback);
}
/**
* Get 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.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 {opResultCallback} callback A callback function
*/
Server.prototype.cursor = function(ns, cmd, cursorOptions) {
var s = this.s;
cursorOptions = cursorOptions || {};
// Set up final cursor type
var FinalCursor = cursorOptions.cursorFactory || s.Cursor;
// Return the cursor
return new FinalCursor(s.bson, ns, cmd, cursorOptions, this, s.options);
}
/**
* Logout from a database
* @method
* @param {string} db The db we are logging out from
* @param {authResultCallback} callback A callback function
*/
Server.prototype.logout = function(dbName, callback) {
this.s.pool.logout(dbName, callback);
}
/**
* Authenticate using a specified mechanism
* @method
* @param {string} mechanism The Auth mechanism we are invoking
* @param {string} db The db we are invoking the mechanism against
* @param {...object} param Parameters for the specific mechanism
* @param {authResultCallback} callback A callback function
*/
Server.prototype.auth = function(mechanism, db) {
var self = this;
// If we have the default mechanism we pick mechanism based on the wire
// protocol max version. If it's >= 3 then scram-sha1 otherwise mongodb-cr
if(mechanism == 'default' && self.ismaster && self.ismaster.maxWireVersion >= 3) {
mechanism = 'scram-sha-1';
} else if(mechanism == 'default') {
mechanism = 'mongocr';
}
// Slice all the arguments off
var args = Array.prototype.slice.call(arguments, 0);
// Set the mechanism
args[0] = mechanism;
// Get the callback
var callback = args[args.length - 1];
// If we are not connected or have a disconnectHandler specified
if(disconnectHandler(self, 'auth', db, args, {}, callback)) {
return;
}
// Do not authenticate if we are an arbiter
if(this.lastIsMaster() && this.lastIsMaster().arbiterOnly) {
return callback(null, true);
}
// Apply the arguments to the pool
self.s.pool.auth.apply(self.s.pool, args);
}
/**
* Compare two server instances
* @method
* @param {Server} server Server to compare equality against
* @return {boolean}
*/
Server.prototype.equals = function(server) {
if(typeof server == 'string') return this.name.toLowerCase() == server.toLowerCase();
if(server.name) return this.name.toLowerCase() == server.name.toLowerCase();
return false;
}
/**
* All raw connections
* @method
* @return {Connection[]}
*/
Server.prototype.connections = function() {
return this.s.pool.allConnections();
}
/**
* Get server
* @method
* @return {Server}
*/
Server.prototype.getServer = function() {
return this;
}
/**
* Get connection
* @method
* @return {Connection}
*/
Server.prototype.getConnection = function() {
return this.s.pool.get();
}
var listeners = ['close', 'error', 'timeout', 'parseError', 'connect'];
/**
* Destroy the server connection
* @method
* @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
*/
Server.prototype.destroy = function(options) {
options = options || {};
var self = this;
// Set the connections
if(serverAccounting) delete servers[this.id];
// Destroy the monitoring process if any
if(this.monitoringProcessId) {
clearTimeout(this.monitoringProcessId);
}
// No pool, return
if(!self.s.pool) return;
// Emit close event
if(options.emitClose) {
self.emit('close', self);
}
// Emit destroy event
if(options.emitDestroy) {
self.emit('destroy', self);
}
// Remove all listeners
listeners.forEach(function(event) {
self.s.pool.removeAllListeners(event);
});
// Emit opening server event
if(self.listeners('serverClosed').length > 0) self.emit('serverClosed', {
topologyId: self.s.topologyId != -1 ? self.s.topologyId : self.id, address: self.name
});
// Emit toplogy opening event if not in topology
if(self.listeners('topologyClosed').length > 0 && !self.s.inTopology) {
self.emit('topologyClosed', { topologyId: self.id });
}
if(self.s.logger.isDebug()) {
self.s.logger.debug(f('destroy called on server %s', self.name));
}
// Destroy the pool
this.s.pool.destroy(options.force);
}
/**
* A server connect event, used to verify that the connection is up and running
*
* @event Server#connect
* @type {Server}
*/
/**
* A server reconnect event, used to verify that the server topology has reconnected
*
* @event Server#reconnect
* @type {Server}
*/
/**
* A server opening SDAM monitoring event
*
* @event Server#serverOpening
* @type {object}
*/
/**
* A server closed SDAM monitoring event
*
* @event Server#serverClosed
* @type {object}
*/
/**
* A server description SDAM change monitoring event
*
* @event Server#serverDescriptionChanged
* @type {object}
*/
/**
* A topology open SDAM event
*
* @event Server#topologyOpening
* @type {object}
*/
/**
* A topology closed SDAM event
*
* @event Server#topologyClosed
* @type {object}
*/
/**
* A topology structure SDAM change event
*
* @event Server#topologyDescriptionChanged
* @type {object}
*/
/**
* Server reconnect failed
*
* @event Server#reconnectFailed
* @type {Error}
*/
/**
* Server connection pool closed
*
* @event Server#close
* @type {object}
*/
/**
* Server connection pool caused an error
*
* @event Server#error
* @type {Error}
*/
/**
* Server destroyed was called
*
* @event Server#destroy
* @type {Server}
*/
module.exports = Server;
@@ -1,334 +0,0 @@
"use strict"
var os = require('os'),
f = require('util').format;
/**
* 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 = new Buffer(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 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(function() {
fn();
if (timer && timer._called === undefined) {
// The artificial _called is set here for compatibility with node.js 0.10.x/0.12.x versions
timer._called = true;
}
}, 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(var j = 0; j < current.servers.length; j++) {
var found = false;
// Go over all the previous servers
for(var 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(var i = 0; i < previous.servers.length; i++) {
var prevServer = previous.servers[i];
// Go through all current servers
for(var 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;
}
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.clone = clone;
module.exports.diff = diff;
module.exports.Interval = Interval;
module.exports.Timeout = Timeout;
-32
View File
@@ -1,32 +0,0 @@
/**
* Copy the values of all enumerable own properties from one or more
* source objects to a target object. It will return the target object.
*/
var assign = Object.assign ? Object.assign : function assign(target) {
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}
var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
}
var keysArray = Object.keys(Object(nextSource));
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}
module.exports = {
assign: assign
};
@@ -1,570 +0,0 @@
"use strict";
var Insert = require('./commands').Insert
, Update = require('./commands').Update
, Remove = require('./commands').Remove
, copy = require('../connection/utils').copy
, retrieveBSON = require('../connection/utils').retrieveBSON
, KillCursor = require('../connection/commands').KillCursor
, GetMore = require('../connection/commands').GetMore
, Query = require('../connection/commands').Query
, f = require('util').format
, CommandResult = require('../connection/command_result')
, MongoError = require('../error')
, getReadPreference = require('./shared').getReadPreference;
var BSON = retrieveBSON(),
Long = BSON.Long;
// Write concern fields
var writeConcernFields = ['w', 'wtimeout', 'j', 'fsync'];
var WireProtocol = function() {}
//
// Needs to support legacy mass insert as well as ordered/unordered legacy
// emulation
//
WireProtocol.prototype.insert = function(pool, ismaster, ns, bson, ops, options, callback) {
options = options || {};
// Default is ordered execution
var ordered = typeof options.ordered == 'boolean' ? options.ordered : true;
ops = Array.isArray(ops) ? ops :[ops];
// If we have more than a 1000 ops fails
if(ops.length > 1000) return callback(new MongoError("exceeded maximum write batch size of 1000"));
// Write concern
var writeConcern = options.writeConcern || {w:1};
// We are unordered
if(!ordered || writeConcern.w == 0) {
return executeUnordered('insert', Insert, ismaster, ns, bson, pool, ops, options, callback);
}
return executeOrdered('insert', Insert, ismaster, ns, bson, pool, ops, options, callback);
}
WireProtocol.prototype.update = function(pool, ismaster, ns, bson, ops, options, callback) {
options = options || {};
// Default is ordered execution
var ordered = typeof options.ordered == 'boolean' ? options.ordered : true;
ops = Array.isArray(ops) ? ops :[ops];
// Write concern
var writeConcern = options.writeConcern || {w:1};
// We are unordered
if(!ordered || writeConcern.w == 0) {
return executeUnordered('update', Update, ismaster, ns, bson, pool, ops, options, callback);
}
return executeOrdered('update', Update, ismaster, ns, bson, pool, ops, options, callback);
}
WireProtocol.prototype.remove = function(pool, ismaster, ns, bson, ops, options, callback) {
options = options || {};
// Default is ordered execution
var ordered = typeof options.ordered == 'boolean' ? options.ordered : true;
ops = Array.isArray(ops) ? ops :[ops];
// Write concern
var writeConcern = options.writeConcern || {w:1};
// We are unordered
if(!ordered || writeConcern.w == 0) {
return executeUnordered('remove', Remove, ismaster, ns, bson, pool, ops, options, callback);
}
return executeOrdered('remove', Remove, ismaster, ns, bson, pool, ops, options, callback);
}
WireProtocol.prototype.killCursor = function(bson, ns, cursorId, pool, callback) {
// Create a kill cursor command
var killCursor = new KillCursor(bson, [cursorId]);
// Execute the kill cursor command
if(pool && pool.isConnected()) {
pool.write(killCursor, {
immediateRelease:true, noResponse: true
});
}
// Callback
if(typeof callback == 'function') callback(null, null);
}
WireProtocol.prototype.getMore = function(bson, ns, cursorState, batchSize, raw, connection, options, callback) {
// Create getMore command
var getMore = new GetMore(bson, ns, cursorState.cursorId, {numberToReturn: batchSize});
// Query callback
var queryCallback = function(err, result) {
if(err) return callback(err);
// Get the raw message
var r = result.message;
// If we have a timed out query or a cursor that was killed
if((r.responseFlags & (1 << 0)) != 0) {
return callback(new MongoError("cursor does not exist, was killed or timed out"), null);
}
// Ensure we have a Long valie cursor id
var cursorId = typeof r.cursorId == 'number'
? Long.fromNumber(r.cursorId)
: r.cursorId;
// Set all the values
cursorState.documents = r.documents;
cursorState.cursorId = cursorId;
// Return
callback(null, null, r.connection);
}
// Contains any query options
var queryOptions = {};
// If we have a raw query decorate the function
if(raw) {
queryOptions.raw = raw;
}
// Check if we need to promote longs
if(typeof cursorState.promoteLongs == 'boolean') {
queryOptions.promoteLongs = cursorState.promoteLongs;
}
if(typeof cursorState.promoteValues == 'boolean') {
queryOptions.promoteValues = cursorState.promoteValues;
}
if(typeof cursorState.promoteBuffers == 'boolean') {
queryOptions.promoteBuffers = cursorState.promoteBuffers;
}
// Write out the getMore command
connection.write(getMore, queryOptions, queryCallback);
}
WireProtocol.prototype.command = function(bson, ns, cmd, cursorState, topology, options) {
// Establish type of command
if(cmd.find) {
return setupClassicFind(bson, ns, cmd, cursorState, topology, options)
} else if(cursorState.cursorId != null) {
return;
} else if(cmd) {
return setupCommand(bson, ns, cmd, cursorState, topology, options);
} else {
throw new MongoError(f("command %s does not return a cursor", JSON.stringify(cmd)));
}
}
//
// Execute a find command
var setupClassicFind = function(bson, ns, cmd, cursorState, topology, options) {
// Ensure we have at least some options
options = options || {};
// Get the readPreference
var readPreference = getReadPreference(cmd, options);
// Set the optional batchSize
cursorState.batchSize = cmd.batchSize || cursorState.batchSize;
var numberToReturn = 0;
// Unpack the limit and batchSize values
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;
}
var numberToSkip = cursorState.skip || 0;
// Build actual find command
var findCmd = {};
// Using special modifier
var usesSpecialModifier = false;
// We have a Mongos topology, check if we need to add a readPreference
if(topology.type == 'mongos' && readPreference) {
findCmd['$readPreference'] = readPreference.toJSON();
usesSpecialModifier = true;
}
// Add special modifiers to the query
if(cmd.sort) findCmd['orderby'] = cmd.sort, usesSpecialModifier = true;
if(cmd.hint) findCmd['$hint'] = cmd.hint, usesSpecialModifier = true;
if(cmd.snapshot) findCmd['$snapshot'] = cmd.snapshot, usesSpecialModifier = true;
if(cmd.returnKey) findCmd['$returnKey'] = cmd.returnKey, usesSpecialModifier = true;
if(cmd.maxScan) findCmd['$maxScan'] = cmd.maxScan, usesSpecialModifier = true;
if(cmd.min) findCmd['$min'] = cmd.min, usesSpecialModifier = true;
if(cmd.max) findCmd['$max'] = cmd.max, usesSpecialModifier = true;
if(cmd.showDiskLoc) findCmd['$showDiskLoc'] = cmd.showDiskLoc, usesSpecialModifier = true;
if(cmd.comment) findCmd['$comment'] = cmd.comment, usesSpecialModifier = true;
if(cmd.maxTimeMS) findCmd['$maxTimeMS'] = cmd.maxTimeMS, usesSpecialModifier = true;
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);
usesSpecialModifier = true;
findCmd['$explain'] = true;
}
// If we have a special modifier
if(usesSpecialModifier) {
findCmd['$query'] = cmd.query;
} else {
findCmd = cmd.query;
}
// Throw on majority readConcern passed in
if(cmd.readConcern && cmd.readConcern.level != 'local') {
throw new MongoError(f('server find command does not support a readConcern level of %s', cmd.readConcern.level));
}
// Remove readConcern, ensure no failing commands
if(cmd.readConcern) {
cmd = copy(cmd);
delete cmd['readConcern'];
}
// Set up the serialize and ignoreUndefined fields
var serializeFunctions = typeof options.serializeFunctions == 'boolean'
? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined == 'boolean'
? options.ignoreUndefined : false;
// Build Query object
var query = new Query(bson, ns, findCmd, {
numberToSkip: numberToSkip, numberToReturn: numberToReturn
, checkKeys: false, returnFieldSelector: cmd.fields
, serializeFunctions: serializeFunctions, ignoreUndefined: ignoreUndefined
});
// Set query flags
query.slaveOk = readPreference.slaveOk();
// Set up the option bits for wire protocol
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;
// Return the query
return query;
}
//
// Set up a command cursor
var setupCommand = function(bson, ns, cmd, cursorState, topology, options) {
// Set empty options object
options = options || {}
// Get the readPreference
var readPreference = getReadPreference(cmd, options);
// Final query
var finalCmd = {};
for(var name in cmd) {
finalCmd[name] = cmd[name];
}
// Build command namespace
var parts = ns.split(/\./);
// Throw on majority readConcern passed in
if(cmd.readConcern && cmd.readConcern.level != 'local') {
throw new MongoError(f('server %s command does not support a readConcern level of %s', JSON.stringify(cmd), cmd.readConcern.level));
}
// Remove readConcern, ensure no failing commands
if(cmd.readConcern) delete cmd['readConcern'];
// Serialize functions
var serializeFunctions = typeof options.serializeFunctions == 'boolean'
? options.serializeFunctions : false;
// Set up the serialize and ignoreUndefined fields
var ignoreUndefined = typeof options.ignoreUndefined == 'boolean'
? options.ignoreUndefined : false;
// We have a Mongos topology, check if we need to add a readPreference
if(topology.type == 'mongos'
&& readPreference
&& readPreference.preference != 'primary') {
finalCmd = {
'$query': finalCmd,
'$readPreference': readPreference.toJSON()
};
}
// Build Query object
var query = new Query(bson, f('%s.$cmd', parts.shift()), finalCmd, {
numberToSkip: 0, numberToReturn: -1
, checkKeys: false, serializeFunctions: serializeFunctions
, ignoreUndefined: ignoreUndefined
});
// Set query flags
query.slaveOk = readPreference.slaveOk();
// Return the query
return query;
}
var hasWriteConcern = function(writeConcern) {
if(writeConcern.w
|| writeConcern.wtimeout
|| writeConcern.j == true
|| writeConcern.fsync == true
|| Object.keys(writeConcern).length == 0) {
return true;
}
return false;
}
var cloneWriteConcern = function(writeConcern) {
var wc = {};
if(writeConcern.w != null) wc.w = writeConcern.w;
if(writeConcern.wtimeout != null) wc.wtimeout = writeConcern.wtimeout;
if(writeConcern.j != null) wc.j = writeConcern.j;
if(writeConcern.fsync != null) wc.fsync = writeConcern.fsync;
return wc;
}
//
// Aggregate up all the results
//
var aggregateWriteOperationResults = function(opType, ops, results, connection) {
var finalResult = { ok: 1, n: 0 }
if(opType == 'update') {
finalResult.nModified = 0;
}
// Map all the results coming back
for(var i = 0; i < results.length; i++) {
var result = results[i];
var op = ops[i];
if((result.upserted || (result.updatedExisting == false)) && finalResult.upserted == null) {
finalResult.upserted = [];
}
// Push the upserted document to the list of upserted values
if(result.upserted) {
finalResult.upserted.push({index: i, _id: result.upserted});
}
// We have an upsert where we passed in a _id
if(result.updatedExisting == false && result.n == 1 && result.upserted == null) {
finalResult.upserted.push({index: i, _id: op.q._id});
} else if(result.updatedExisting == true) {
finalResult.nModified += result.n;
}
// We have an insert command
if(result.ok == 1 && opType == 'insert' && result.err == null) {
finalResult.n = finalResult.n + 1;
}
// We have a command error
if(result != null && result.ok == 0 || result.err || result.errmsg) {
if(result.ok == 0) finalResult.ok = 0;
finalResult.code = result.code;
finalResult.errmsg = result.errmsg || result.err || result.errMsg;
// Check if we have a write error
if(result.code == 11000
|| result.code == 11001
|| result.code == 12582
|| result.code == 16544
|| result.code == 16538
|| result.code == 16542
|| result.code == 14
|| result.code == 13511) {
if(finalResult.writeErrors == null) finalResult.writeErrors = [];
finalResult.writeErrors.push({
index: i
, code: result.code
, errmsg: result.errmsg || result.err || result.errMsg
});
} else {
finalResult.writeConcernError = {
code: result.code
, errmsg: result.errmsg || result.err || result.errMsg
}
}
} else if(typeof result.n == 'number') {
finalResult.n += result.n;
} else {
finalResult.n += 1;
}
// Result as expected
if(result != null && result.lastOp) finalResult.lastOp = result.lastOp;
}
// Return finalResult aggregated results
return new CommandResult(finalResult, connection);
}
//
// Execute all inserts in an ordered manner
//
var executeOrdered = function(opType ,command, ismaster, ns, bson, pool, ops, options, callback) {
var _ops = ops.slice(0);
// Collect all the getLastErrors
var getLastErrors = [];
// Execute an operation
var executeOp = function(list, _callback) {
// No more items in the list
if(list.length == 0) {
return process.nextTick(function() {
_callback(null, aggregateWriteOperationResults(opType, ops, getLastErrors, null));
});
}
// Get the first operation
var doc = list.shift();
// Create an insert command
var op = new command(Query.getRequestId(), ismaster, bson, ns, [doc], options);
// Write concern
var optionWriteConcern = options.writeConcern || {w:1};
// Final write concern
var writeConcern = cloneWriteConcern(optionWriteConcern);
// Get the db name
var db = ns.split('.').shift();
try {
// Add binary message to list of commands to execute
var commands = [op];
// Add getLastOrdered
var getLastErrorCmd = {getlasterror: 1};
// Merge all the fields
for(var i = 0; i < writeConcernFields.length; i++) {
if(writeConcern[writeConcernFields[i]] != null) {
getLastErrorCmd[writeConcernFields[i]] = writeConcern[writeConcernFields[i]];
}
}
// Create a getLastError command
var getLastErrorOp = new Query(bson, f("%s.$cmd", db), getLastErrorCmd, {numberToReturn: -1});
// Add getLastError command to list of ops to execute
commands.push(getLastErrorOp);
// getLastError callback
var getLastErrorCallback = function(err, result) {
if(err) return callback(err);
// Get the document
var doc = result.result;
// Save the getLastError document
getLastErrors.push(doc);
// If we have an error terminate
if(doc.ok == 0 || doc.err || doc.errmsg) {
return callback(null, aggregateWriteOperationResults(opType, ops, getLastErrors, result.connection));
}
// Execute the next op in the list
executeOp(list, callback);
}
// Write both commands out at the same time
pool.write(commands, getLastErrorCallback);
} catch(err) {
// We have a serialization error, rewrite as a write error to have same behavior as modern
// write commands
getLastErrors.push({ ok: 1, errmsg: typeof err == 'string' ? err : err.message, code: 14 });
// Return due to an error
process.nextTick(function() {
_callback(null, aggregateWriteOperationResults(opType, ops, getLastErrors, null));
});
}
}
// Execute the operations
executeOp(_ops, callback);
}
var executeUnordered = function(opType, command, ismaster, ns, bson, pool, ops, options, callback) {
// Total operations to write
var totalOps = ops.length;
// Collect all the getLastErrors
var getLastErrors = [];
// Write concern
var optionWriteConcern = options.writeConcern || {w:1};
// Final write concern
var writeConcern = cloneWriteConcern(optionWriteConcern);
// Driver level error
var error;
// Execute all the operations
for(var i = 0; i < ops.length; i++) {
// Create an insert command
var op = new command(Query.getRequestId(), ismaster, bson, ns, [ops[i]], options);
// Get db name
var db = ns.split('.').shift();
try {
// Add binary message to list of commands to execute
var commands = [op];
// If write concern 0 don't fire getLastError
if(hasWriteConcern(writeConcern)) {
var getLastErrorCmd = {getlasterror: 1};
// Merge all the fields
for(var j = 0; j < writeConcernFields.length; j++) {
if(writeConcern[writeConcernFields[j]] != null)
getLastErrorCmd[writeConcernFields[j]] = writeConcern[writeConcernFields[j]];
}
// Create a getLastError command
var getLastErrorOp = new Query(bson, f("%s.$cmd", db), getLastErrorCmd, {numberToReturn: -1});
// Add getLastError command to list of ops to execute
commands.push(getLastErrorOp);
// Give the result from getLastError the right index
var callbackOp = function(_index) {
return function(err, result) {
if(err) error = err;
// Update the number of operations executed
totalOps = totalOps - 1;
// Save the getLastError document
if(!err) getLastErrors[_index] = result.result;
// Check if we are done
if(totalOps == 0) {
process.nextTick(function() {
if(error) return callback(error);
callback(null, aggregateWriteOperationResults(opType, ops, getLastErrors, result.connection));
});
}
}
}
// Write both commands out at the same time
pool.write(commands, callbackOp(i));
} else {
pool.write(commands, {immediateRelease:true, noResponse:true});
}
} catch(err) {
// Update the number of operations executed
totalOps = totalOps - 1;
// We have a serialization error, rewrite as a write error to have same behavior as modern
// write commands
getLastErrors[i] = { ok: 1, errmsg: typeof err == 'string' ? err : err.message, code: 14 };
// Check if we are done
if(totalOps == 0) {
callback(null, aggregateWriteOperationResults(opType, ops, getLastErrors, null));
}
}
}
// Empty w:0 return
if(writeConcern
&& writeConcern.w == 0 && callback) {
callback(null, new CommandResult({ok:1}, null));
}
}
module.exports = WireProtocol;
@@ -1,334 +0,0 @@
"use strict";
var copy = require('../connection/utils').copy
, retrieveBSON = require('../connection/utils').retrieveBSON
, KillCursor = require('../connection/commands').KillCursor
, GetMore = require('../connection/commands').GetMore
, Query = require('../connection/commands').Query
, f = require('util').format
, MongoError = require('../error')
, getReadPreference = require('./shared').getReadPreference;
var BSON = retrieveBSON(),
Long = BSON.Long;
var WireProtocol = function() {}
//
// Execute a write operation
var executeWrite = function(pool, bson, 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 || {};
}
// Split the ns up to get db and collection
var p = ns.split(".");
var d = p.shift();
// Options
var ordered = typeof options.ordered == 'boolean' ? options.ordered : true;
var writeConcern = options.writeConcern;
// return skeleton
var writeCommand = {};
writeCommand[type] = p.join('.');
writeCommand[opsField] = ops;
writeCommand.ordered = ordered;
// Did we specify a write concern
if(writeConcern && Object.keys(writeConcern).length > 0) {
writeCommand.writeConcern = writeConcern;
}
// Do we have bypassDocumentValidation set, then enable it on the write command
if(typeof options.bypassDocumentValidation == 'boolean') {
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
}
// Options object
var opts = { command: true };
var queryOptions = { checkKeys : false, numberToSkip: 0, numberToReturn: 1 };
if(type == 'insert') queryOptions.checkKeys = true;
if(typeof options.checkKeys == 'boolean') queryOptions.checkKeys = options.checkKeys;
// Ensure we support serialization of functions
if(options.serializeFunctions) queryOptions.serializeFunctions = options.serializeFunctions;
// Do not serialize the undefined fields
if(options.ignoreUndefined) queryOptions.ignoreUndefined = options.ignoreUndefined;
try {
// Create write command
var cmd = new Query(bson, f("%s.$cmd", d), writeCommand, queryOptions);
// Execute command
pool.write(cmd, opts, callback);
} catch(err) {
callback(err);
}
}
//
// Needs to support legacy mass insert as well as ordered/unordered legacy
// emulation
//
WireProtocol.prototype.insert = function(pool, ismaster, ns, bson, ops, options, callback) {
executeWrite(pool, bson, 'insert', 'documents', ns, ops, options, callback);
}
WireProtocol.prototype.update = function(pool, ismaster, ns, bson, ops, options, callback) {
executeWrite(pool, bson, 'update', 'updates', ns, ops, options, callback);
}
WireProtocol.prototype.remove = function(pool, ismaster, ns, bson, ops, options, callback) {
executeWrite(pool, bson, 'delete', 'deletes', ns, ops, options, callback);
}
WireProtocol.prototype.killCursor = function(bson, ns, cursorId, pool, callback) {
// Create a kill cursor command
var killCursor = new KillCursor(bson, [cursorId]);
// Execute the kill cursor command
if(pool && pool.isConnected()) {
pool.write(killCursor, {
immediateRelease:true, noResponse: true
});
}
// Callback
if(typeof callback == 'function') callback(null, null);
}
WireProtocol.prototype.getMore = function(bson, ns, cursorState, batchSize, raw, connection, options, callback) {
// Create getMore command
var getMore = new GetMore(bson, ns, cursorState.cursorId, {numberToReturn: batchSize});
// Query callback
var queryCallback = function(err, result) {
if(err) return callback(err);
// Get the raw message
var r = result.message;
// If we have a timed out query or a cursor that was killed
if((r.responseFlags & (1 << 0)) != 0) {
return callback(new MongoError("cursor does not exist, was killed or timed out"), null);
}
// Ensure we have a Long valie cursor id
var cursorId = typeof r.cursorId == 'number'
? Long.fromNumber(r.cursorId)
: r.cursorId;
// Set all the values
cursorState.documents = r.documents;
cursorState.cursorId = cursorId;
// Return
callback(null, null, r.connection);
}
// Contains any query options
var queryOptions = {};
// If we have a raw query decorate the function
if(raw) {
queryOptions.raw = raw;
}
// Check if we need to promote longs
if(typeof cursorState.promoteLongs == 'boolean') {
queryOptions.promoteLongs = cursorState.promoteLongs;
}
if(typeof cursorState.promoteValues == 'boolean') {
queryOptions.promoteValues = cursorState.promoteValues;
}
if(typeof cursorState.promoteBuffers == 'boolean') {
queryOptions.promoteBuffers = cursorState.promoteBuffers;
}
// Write out the getMore command
connection.write(getMore, queryOptions, queryCallback);
}
WireProtocol.prototype.command = function(bson, ns, cmd, cursorState, topology, options) {
// Establish type of command
if(cmd.find) {
return setupClassicFind(bson, ns, cmd, cursorState, topology, options)
} else if(cursorState.cursorId != null) {
return;
} else if(cmd) {
return setupCommand(bson, ns, cmd, cursorState, topology, options);
} else {
throw new MongoError(f("command %s does not return a cursor", JSON.stringify(cmd)));
}
}
//
// Execute a find command
var setupClassicFind = function(bson, ns, cmd, cursorState, topology, options) {
// Ensure we have at least some options
options = options || {};
// Get the readPreference
var readPreference = getReadPreference(cmd, options);
// Set the optional batchSize
cursorState.batchSize = cmd.batchSize || cursorState.batchSize;
var numberToReturn = 0;
// Unpack the limit and batchSize values
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;
}
var numberToSkip = cursorState.skip || 0;
// Build actual find command
var findCmd = {};
// Using special modifier
var usesSpecialModifier = false;
// We have a Mongos topology, check if we need to add a readPreference
if(topology.type == 'mongos' && readPreference) {
findCmd['$readPreference'] = readPreference.toJSON();
usesSpecialModifier = true;
}
// Add special modifiers to the query
if(cmd.sort) findCmd['orderby'] = cmd.sort, usesSpecialModifier = true;
if(cmd.hint) findCmd['$hint'] = cmd.hint, usesSpecialModifier = true;
if(cmd.snapshot) findCmd['$snapshot'] = cmd.snapshot, usesSpecialModifier = true;
if(cmd.returnKey) findCmd['$returnKey'] = cmd.returnKey, usesSpecialModifier = true;
if(cmd.maxScan) findCmd['$maxScan'] = cmd.maxScan, usesSpecialModifier = true;
if(cmd.min) findCmd['$min'] = cmd.min, usesSpecialModifier = true;
if(cmd.max) findCmd['$max'] = cmd.max, usesSpecialModifier = true;
if(cmd.showDiskLoc) findCmd['$showDiskLoc'] = cmd.showDiskLoc, usesSpecialModifier = true;
if(cmd.comment) findCmd['$comment'] = cmd.comment, usesSpecialModifier = true;
if(cmd.maxTimeMS) findCmd['$maxTimeMS'] = cmd.maxTimeMS, usesSpecialModifier = true;
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);
usesSpecialModifier = true;
findCmd['$explain'] = true;
}
// If we have a special modifier
if(usesSpecialModifier) {
findCmd['$query'] = cmd.query;
} else {
findCmd = cmd.query;
}
// Throw on majority readConcern passed in
if(cmd.readConcern && cmd.readConcern.level != 'local') {
throw new MongoError(f('server find command does not support a readConcern level of %s', cmd.readConcern.level));
}
// Remove readConcern, ensure no failing commands
if(cmd.readConcern) {
cmd = copy(cmd);
delete cmd['readConcern'];
}
// Serialize functions
var serializeFunctions = typeof options.serializeFunctions == 'boolean'
? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined == 'boolean'
? options.ignoreUndefined : false;
// Build Query object
var query = new Query(bson, ns, findCmd, {
numberToSkip: numberToSkip, numberToReturn: numberToReturn
, checkKeys: false, returnFieldSelector: cmd.fields
, serializeFunctions: serializeFunctions
, ignoreUndefined: ignoreUndefined
});
// Set query flags
query.slaveOk = readPreference.slaveOk();
// Set up the option bits for wire protocol
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;
}
// Return the query
return query;
}
//
// Set up a command cursor
var setupCommand = function(bson, ns, cmd, cursorState, topology, options) {
// Set empty options object
options = options || {}
// Get the readPreference
var readPreference = getReadPreference(cmd, options);
// Final query
var finalCmd = {};
for(var name in cmd) {
finalCmd[name] = cmd[name];
}
// Build command namespace
var parts = ns.split(/\./);
// Serialize functions
var serializeFunctions = typeof options.serializeFunctions == 'boolean'
? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined == 'boolean'
? options.ignoreUndefined : false;
// Throw on majority readConcern passed in
if(cmd.readConcern && cmd.readConcern.level != 'local') {
throw new MongoError(f('server %s command does not support a readConcern level of %s', JSON.stringify(cmd), cmd.readConcern.level));
}
// Remove readConcern, ensure no failing commands
if(cmd.readConcern) delete cmd['readConcern'];
// We have a Mongos topology, check if we need to add a readPreference
if(topology.type == 'mongos'
&& readPreference
&& readPreference.preference != 'primary') {
finalCmd = {
'$query': finalCmd,
'$readPreference': readPreference.toJSON()
};
}
// Build Query object
var query = new Query(bson, f('%s.$cmd', parts.shift()), finalCmd, {
numberToSkip: 0, numberToReturn: -1
, checkKeys: false, serializeFunctions: serializeFunctions
, ignoreUndefined: ignoreUndefined
});
// Set query flags
query.slaveOk = readPreference.slaveOk();
// Return the query
return query;
}
module.exports = WireProtocol;
@@ -1,549 +0,0 @@
"use strict";
var Query = require('../connection/commands').Query
, retrieveBSON = require('../connection/utils').retrieveBSON
, f = require('util').format
, MongoError = require('../error')
, getReadPreference = require('./shared').getReadPreference;
var BSON = retrieveBSON(),
Long = BSON.Long;
var WireProtocol = function(legacyWireProtocol) {
this.legacyWireProtocol = legacyWireProtocol;
}
//
// Execute a write operation
var executeWrite = function(pool, bson, 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 || {};
}
// Split the ns up to get db and collection
var p = ns.split(".");
var d = p.shift();
// Options
var ordered = typeof options.ordered == 'boolean' ? options.ordered : true;
var writeConcern = options.writeConcern;
// return skeleton
var writeCommand = {};
writeCommand[type] = p.join('.');
writeCommand[opsField] = ops;
writeCommand.ordered = ordered;
// Did we specify a write concern
if(writeConcern && Object.keys(writeConcern).length > 0) {
writeCommand.writeConcern = writeConcern;
}
// If we have collation passed in
if(options.collation) {
for(var i = 0; i < writeCommand[opsField].length; i++) {
if(!writeCommand[opsField][i].collation) {
writeCommand[opsField][i].collation = options.collation;
}
}
}
// Do we have bypassDocumentValidation set, then enable it on the write command
if(typeof options.bypassDocumentValidation == 'boolean') {
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
}
// Options object
var opts = { command: true };
var queryOptions = { checkKeys : false, numberToSkip: 0, numberToReturn: 1 };
if(type == 'insert') queryOptions.checkKeys = true;
if(typeof options.checkKeys == 'boolean') queryOptions.checkKeys = options.checkKeys;
// Ensure we support serialization of functions
if(options.serializeFunctions) queryOptions.serializeFunctions = options.serializeFunctions;
// Do not serialize the undefined fields
if(options.ignoreUndefined) queryOptions.ignoreUndefined = options.ignoreUndefined;
try {
// Create write command
var cmd = new Query(bson, f("%s.$cmd", d), writeCommand, queryOptions);
// Execute command
pool.write(cmd, opts, callback);
} catch(err) {
callback(err);
}
}
//
// Needs to support legacy mass insert as well as ordered/unordered legacy
// emulation
//
WireProtocol.prototype.insert = function(pool, ismaster, ns, bson, ops, options, callback) {
executeWrite(pool, bson, 'insert', 'documents', ns, ops, options, callback);
}
WireProtocol.prototype.update = function(pool, ismaster, ns, bson, ops, options, callback) {
executeWrite(pool, bson, 'update', 'updates', ns, ops, options, callback);
}
WireProtocol.prototype.remove = function(pool, ismaster, ns, bson, ops, options, callback) {
executeWrite(pool, bson, 'delete', 'deletes', ns, ops, options, callback);
}
WireProtocol.prototype.killCursor = function(bson, ns, cursorId, pool, callback) {
// Build command namespace
var parts = ns.split(/\./);
// Command namespace
var commandns = f('%s.$cmd', parts.shift());
// Create getMore command
var killcursorCmd = {
killCursors: parts.join('.'),
cursors: [cursorId]
}
// Build Query object
var query = new Query(bson, commandns, killcursorCmd, {
numberToSkip: 0, numberToReturn: -1
, checkKeys: false, returnFieldSelector: null
});
// Set query flags
query.slaveOk = true;
// Kill cursor callback
var killCursorCallback = function(err, result) {
if(err) {
if(typeof callback != 'function') return;
return callback(err);
}
// Result
var r = result.message;
// If we have a timed out query or a cursor that was killed
if((r.responseFlags & (1 << 0)) != 0) {
if(typeof callback != 'function') return;
return callback(new MongoError("cursor killed or timed out"), null);
}
if(!Array.isArray(r.documents) || r.documents.length == 0) {
if(typeof callback != 'function') return;
return callback(new MongoError(f('invalid killCursors result returned for cursor id %s', cursorId)));
}
// Return the result
if(typeof callback == 'function') {
callback(null, r.documents[0]);
}
}
// Execute the kill cursor command
if(pool && pool.isConnected()) {
pool.write(query, {
command: true
}, killCursorCallback);
}
}
WireProtocol.prototype.getMore = function(bson, ns, cursorState, batchSize, raw, connection, options, callback) {
options = options || {};
// Build command namespace
var parts = ns.split(/\./);
// Command namespace
var commandns = f('%s.$cmd', parts.shift());
// Create getMore command
var getMoreCmd = {
getMore: cursorState.cursorId,
collection: parts.join('.'),
batchSize: Math.abs(batchSize)
}
if(cursorState.cmd.tailable
&& typeof cursorState.cmd.maxAwaitTimeMS == 'number') {
getMoreCmd.maxTimeMS = cursorState.cmd.maxAwaitTimeMS;
}
// Build Query object
var query = new Query(bson, commandns, getMoreCmd, {
numberToSkip: 0, numberToReturn: -1
, checkKeys: false, returnFieldSelector: null
});
// Set query flags
query.slaveOk = true;
// Query callback
var queryCallback = function(err, result) {
if(err) return callback(err);
// Get the raw message
var r = result.message;
// If we have a timed out query or a cursor that was killed
if((r.responseFlags & (1 << 0)) != 0) {
return callback(new MongoError("cursor killed or timed out"), null);
}
// Raw, return all the extracted documents
if(raw) {
cursorState.documents = r.documents;
cursorState.cursorId = r.cursorId;
return callback(null, r.documents);
}
// We have an error detected
if(r.documents[0].ok == 0) {
return callback(MongoError.create(r.documents[0]));
}
// Ensure we have a Long valid cursor id
var cursorId = typeof r.documents[0].cursor.id == 'number'
? Long.fromNumber(r.documents[0].cursor.id)
: r.documents[0].cursor.id;
// Set all the values
cursorState.documents = r.documents[0].cursor.nextBatch;
cursorState.cursorId = cursorId;
// Return the result
callback(null, r.documents[0], r.connection);
}
// Query options
var queryOptions = { command: true };
// If we have a raw query decorate the function
if(raw) {
queryOptions.raw = raw;
}
// Add the result field needed
queryOptions.documentsReturnedIn = 'nextBatch';
// Check if we need to promote longs
if(typeof cursorState.promoteLongs == 'boolean') {
queryOptions.promoteLongs = cursorState.promoteLongs;
}
if(typeof cursorState.promoteValues == 'boolean') {
queryOptions.promoteValues = cursorState.promoteValues;
}
if(typeof cursorState.promoteBuffers == 'boolean') {
queryOptions.promoteBuffers = cursorState.promoteBuffers;
}
// Write out the getMore command
connection.write(query, queryOptions, queryCallback);
}
WireProtocol.prototype.command = function(bson, ns, cmd, cursorState, topology, options) {
options = options || {}
// Check if this is a wire protocol command or not
var wireProtocolCommand = typeof options.wireProtocolCommand == 'boolean' ? options.wireProtocolCommand : true;
// Establish type of command
if(cmd.find && wireProtocolCommand) {
// Create the find command
var query = executeFindCommand(bson, ns, cmd, cursorState, topology, options)
// Mark the cmd as virtual
cmd.virtual = false;
// Signal the documents are in the firstBatch value
query.documentsReturnedIn = 'firstBatch';
// Return the query
return query;
} else if(cursorState.cursorId != null) {
return;
} else if(cmd) {
return setupCommand(bson, ns, cmd, cursorState, topology, options);
} else {
throw new MongoError(f("command %s does not return a cursor", JSON.stringify(cmd)));
}
}
// // Command
// {
// find: ns
// , query: <object>
// , limit: <n>
// , fields: <object>
// , skip: <n>
// , hint: <string>
// , explain: <boolean>
// , snapshot: <boolean>
// , batchSize: <n>
// , returnKey: <boolean>
// , maxScan: <n>
// , min: <n>
// , max: <n>
// , showDiskLoc: <boolean>
// , comment: <string>
// , maxTimeMS: <n>
// , raw: <boolean>
// , readPreference: <ReadPreference>
// , tailable: <boolean>
// , oplogReplay: <boolean>
// , noCursorTimeout: <boolean>
// , awaitdata: <boolean>
// , exhaust: <boolean>
// , partial: <boolean>
// }
// FIND/GETMORE SPEC
// {
// “find”: <string>,
// “filter”: { ... },
// “sort”: { ... },
// “projection”: { ... },
// “hint”: { ... },
// “skip”: <int>,
// “limit”: <int>,
// “batchSize”: <int>,
// “singleBatch”: <bool>,
// “comment”: <string>,
// “maxScan”: <int>,
// “maxTimeMS”: <int>,
// “max”: { ... },
// “min”: { ... },
// “returnKey”: <bool>,
// “showRecordId”: <bool>,
// “snapshot”: <bool>,
// “tailable”: <bool>,
// “oplogReplay”: <bool>,
// “noCursorTimeout”: <bool>,
// “awaitData”: <bool>,
// “partial”: <bool>,
// “$readPreference”: { ... }
// }
//
// Execute a find command
var executeFindCommand = function(bson, ns, cmd, cursorState, topology, options) {
// Ensure we have at least some options
options = options || {};
// Get the readPreference
var readPreference = getReadPreference(cmd, options);
// Set the optional batchSize
cursorState.batchSize = cmd.batchSize || cursorState.batchSize;
// Build command namespace
var parts = ns.split(/\./);
// Command namespace
var commandns = f('%s.$cmd', parts.shift());
// Build actual find command
var findCmd = {
find: parts.join('.')
};
// I we provided a filter
if(cmd.query) {
// Check if the user is passing in the $query parameter
if(cmd.query['$query']) {
findCmd.filter = cmd.query['$query'];
} else {
findCmd.filter = cmd.query;
}
}
// Sort value
var sortValue = cmd.sort;
// Handle issue of sort being an Array
if(Array.isArray(sortValue)) {
var sortObject = {};
if(sortValue.length > 0 && !Array.isArray(sortValue[0])) {
var sortDirection = sortValue[1];
// Translate the sort order text
if(sortDirection == 'asc') {
sortDirection = 1;
} else if(sortDirection == 'desc') {
sortDirection = -1;
}
// Set the sort order
sortObject[sortValue[0]] = sortDirection;
} else {
for(var i = 0; i < sortValue.length; i++) {
sortDirection = sortValue[i][1];
// Translate the sort order text
if(sortDirection == 'asc') {
sortDirection = 1;
} else if(sortDirection == 'desc') {
sortDirection = -1;
}
// Set the sort order
sortObject[sortValue[i][0]] = sortDirection;
}
}
sortValue = sortObject;
}
// Add sort to command
if(cmd.sort) findCmd.sort = sortValue;
// Add a projection to the command
if(cmd.fields) findCmd.projection = cmd.fields;
// Add a hint to the command
if(cmd.hint) findCmd.hint = cmd.hint;
// Add a skip
if(cmd.skip) findCmd.skip = cmd.skip;
// Add a limit
if(cmd.limit) findCmd.limit = cmd.limit;
// Check if we wish to have a singleBatch
if(cmd.limit < 0) {
findCmd.limit = Math.abs(cmd.limit);
findCmd.singleBatch = true;
}
// Add a batchSize
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 we have comment set
if(cmd.comment) findCmd.comment = cmd.comment;
// If we have maxScan
if(cmd.maxScan) findCmd.maxScan = cmd.maxScan;
// If we have maxTimeMS set
if(cmd.maxTimeMS) findCmd.maxTimeMS = cmd.maxTimeMS;
// If we have min
if(cmd.min) findCmd.min = cmd.min;
// If we have max
if(cmd.max) findCmd.max = cmd.max;
// If we have returnKey set
if(cmd.returnKey) findCmd.returnKey = cmd.returnKey;
// If we have showDiskLoc set
if(cmd.showDiskLoc) findCmd.showRecordId = cmd.showDiskLoc;
// If we have snapshot set
if(cmd.snapshot) findCmd.snapshot = cmd.snapshot;
// If we have tailable set
if(cmd.tailable) findCmd.tailable = cmd.tailable;
// If we have oplogReplay set
if(cmd.oplogReplay) findCmd.oplogReplay = cmd.oplogReplay;
// If we have noCursorTimeout set
if(cmd.noCursorTimeout) findCmd.noCursorTimeout = cmd.noCursorTimeout;
// If we have awaitData set
if(cmd.awaitData) findCmd.awaitData = cmd.awaitData;
if(cmd.awaitdata) findCmd.awaitData = cmd.awaitdata;
// If we have partial set
if(cmd.partial) findCmd.partial = cmd.partial;
// If we have collation passed in
if(cmd.collation) findCmd.collation = cmd.collation;
// If we have explain, we need to rewrite the find command
// to wrap it in the explain command
if(cmd.explain) {
findCmd = {
explain: findCmd
}
}
// Did we provide a readConcern
if(cmd.readConcern) findCmd.readConcern = cmd.readConcern;
// Set up the serialize and ignoreUndefined fields
var serializeFunctions = typeof options.serializeFunctions == 'boolean'
? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined == 'boolean'
? options.ignoreUndefined : false;
// We have a Mongos topology, check if we need to add a readPreference
if(topology.type == 'mongos'
&& readPreference
&& readPreference.preference != 'primary') {
findCmd = {
'$query': findCmd,
'$readPreference': readPreference.toJSON()
};
}
// Build Query object
var query = new Query(bson, commandns, findCmd, {
numberToSkip: 0, numberToReturn: 1
, checkKeys: false, returnFieldSelector: null
, serializeFunctions: serializeFunctions, ignoreUndefined: ignoreUndefined
});
// Set query flags
query.slaveOk = readPreference.slaveOk();
// Return the query
return query;
}
//
// Set up a command cursor
var setupCommand = function(bson, ns, cmd, cursorState, topology, options) {
// Set empty options object
options = options || {}
// Get the readPreference
var readPreference = getReadPreference(cmd, options);
// Final query
var finalCmd = {};
for(var name in cmd) {
finalCmd[name] = cmd[name];
}
// Build command namespace
var parts = ns.split(/\./);
// Serialize functions
var serializeFunctions = typeof options.serializeFunctions == 'boolean'
? options.serializeFunctions : false;
// Set up the serialize and ignoreUndefined fields
var ignoreUndefined = typeof options.ignoreUndefined == 'boolean'
? options.ignoreUndefined : false;
// We have a Mongos topology, check if we need to add a readPreference
if(topology.type == 'mongos'
&& readPreference
&& readPreference.preference != 'primary') {
finalCmd = {
'$query': finalCmd,
'$readPreference': readPreference.toJSON()
};
}
// Build Query object
var query = new Query(bson, f('%s.$cmd', parts.shift()), finalCmd, {
numberToSkip: 0, numberToReturn: -1
, checkKeys: false, serializeFunctions: serializeFunctions
, ignoreUndefined: ignoreUndefined
});
// Set query flags
query.slaveOk = readPreference.slaveOk();
// Return the query
return query;
}
module.exports = WireProtocol;
@@ -1,357 +0,0 @@
"use strict";
var MongoError = require('../error');
// Wire command operation ids
var OP_UPDATE = 2001;
var OP_INSERT = 2002;
var OP_DELETE = 2006;
var Insert = function(requestId, ismaster, bson, ns, documents, options) {
// Basic options needed to be passed in
if(ns == null) throw new MongoError("ns must be specified for query");
if(!Array.isArray(documents) || documents.length == 0) throw new MongoError("documents array must contain at least one document to insert");
// Validate that we are not passing 0x00 in the collection name
if(!!~ns.indexOf("\x00")) {
throw new MongoError("namespace cannot contain a null character");
}
// Set internal
this.requestId = requestId;
this.bson = bson;
this.ns = ns;
this.documents = documents;
this.ismaster = ismaster;
// Ensure empty options
options = options || {};
// Unpack options
this.serializeFunctions = typeof options.serializeFunctions == 'boolean' ? options.serializeFunctions : false;
this.ignoreUndefined = typeof options.ignoreUndefined == 'boolean' ? options.ignoreUndefined : false;
this.checkKeys = typeof options.checkKeys == 'boolean' ? options.checkKeys : true;
this.continueOnError = typeof options.continueOnError == 'boolean' ? options.continueOnError : false;
// Set flags
this.flags = this.continueOnError ? 1 : 0;
}
// To Binary
Insert.prototype.toBin = function() {
// Contains all the buffers to be written
var buffers = [];
// Header buffer
var header = new Buffer(
4 * 4 // Header
+ 4 // Flags
+ Buffer.byteLength(this.ns) + 1 // namespace
);
// Add header to buffers
buffers.push(header);
// Total length of the message
var totalLength = header.length;
// Serialize all the documents
for(var i = 0; i < this.documents.length; i++) {
var buffer = this.bson.serialize(this.documents[i], {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined,
});
// Document is larger than maxBsonObjectSize, terminate serialization
if(buffer.length > this.ismaster.maxBsonObjectSize) {
throw new MongoError("Document exceeds maximum allowed bson size of " + this.ismaster.maxBsonObjectSize + " bytes");
}
// Add to total length of wire protocol message
totalLength = totalLength + buffer.length;
// Add to buffer
buffers.push(buffer);
}
// Command is larger than maxMessageSizeBytes terminate serialization
if(totalLength > this.ismaster.maxMessageSizeBytes) {
throw new MongoError("Command exceeds maximum message size of " + this.ismaster.maxMessageSizeBytes + " bytes");
}
// Add all the metadata
var index = 0;
// Write header length
header[index + 3] = (totalLength >> 24) & 0xff;
header[index + 2] = (totalLength >> 16) & 0xff;
header[index + 1] = (totalLength >> 8) & 0xff;
header[index] = (totalLength) & 0xff;
index = index + 4;
// Write header 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;
// No flags
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;
// Operation
header[index + 3] = (OP_INSERT >> 24) & 0xff;
header[index + 2] = (OP_INSERT >> 16) & 0xff;
header[index + 1] = (OP_INSERT >> 8) & 0xff;
header[index] = (OP_INSERT) & 0xff;
index = index + 4;
// Flags
header[index + 3] = (this.flags >> 24) & 0xff;
header[index + 2] = (this.flags >> 16) & 0xff;
header[index + 1] = (this.flags >> 8) & 0xff;
header[index] = (this.flags) & 0xff;
index = index + 4;
// Write collection name
index = index + header.write(this.ns, index, 'utf8') + 1;
header[index - 1] = 0;
// Return the buffers
return buffers;
}
var Update = function(requestId, ismaster, bson, ns, update, options) {
// Basic options needed to be passed in
if(ns == null) throw new MongoError("ns must be specified for query");
// Ensure empty options
options = options || {};
// Set internal
this.requestId = requestId;
this.bson = bson;
this.ns = ns;
this.ismaster = ismaster;
// Unpack options
this.serializeFunctions = typeof options.serializeFunctions == 'boolean' ? options.serializeFunctions : false;
this.ignoreUndefined = typeof options.ignoreUndefined == 'boolean' ? options.ignoreUndefined : false;
this.checkKeys = typeof options.checkKeys == 'boolean' ? options.checkKeys : false;
// Unpack the update document
this.upsert = typeof update[0].upsert == 'boolean' ? update[0].upsert : false;
this.multi = typeof update[0].multi == 'boolean' ? update[0].multi : false;
this.q = update[0].q;
this.u = update[0].u;
// Create flag value
this.flags = this.upsert ? 1 : 0;
this.flags = this.multi ? this.flags | 2 : this.flags;
}
// To Binary
Update.prototype.toBin = function() {
// Contains all the buffers to be written
var buffers = [];
// Header buffer
var header = new Buffer(
4 * 4 // Header
+ 4 // ZERO
+ Buffer.byteLength(this.ns) + 1 // namespace
+ 4 // Flags
);
// Add header to buffers
buffers.push(header);
// Total length of the message
var totalLength = header.length;
// Serialize the selector
var selector = this.bson.serialize(this.q, {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined,
});
buffers.push(selector);
totalLength = totalLength + selector.length;
// Serialize the update
var update = this.bson.serialize(this.u, {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined,
});
buffers.push(update);
totalLength = totalLength + update.length;
// Index in header buffer
var index = 0;
// Write header length
header[index + 3] = (totalLength >> 24) & 0xff;
header[index + 2] = (totalLength >> 16) & 0xff;
header[index + 1] = (totalLength >> 8) & 0xff;
header[index] = (totalLength) & 0xff;
index = index + 4;
// Write header 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;
// No flags
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;
// Operation
header[index + 3] = (OP_UPDATE >> 24) & 0xff;
header[index + 2] = (OP_UPDATE >> 16) & 0xff;
header[index + 1] = (OP_UPDATE >> 8) & 0xff;
header[index] = (OP_UPDATE) & 0xff;
index = index + 4;
// Write ZERO
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 collection name
index = index + header.write(this.ns, index, 'utf8') + 1;
header[index - 1] = 0;
// Flags
header[index + 3] = (this.flags >> 24) & 0xff;
header[index + 2] = (this.flags >> 16) & 0xff;
header[index + 1] = (this.flags >> 8) & 0xff;
header[index] = (this.flags) & 0xff;
index = index + 4;
// Return the buffers
return buffers;
}
var Remove = function(requestId, ismaster, bson, ns, remove, options) {
// Basic options needed to be passed in
if(ns == null) throw new MongoError("ns must be specified for query");
// Ensure empty options
options = options || {};
// Set internal
this.requestId = requestId;
this.bson = bson;
this.ns = ns;
this.ismaster = ismaster;
// Unpack options
this.serializeFunctions = typeof options.serializeFunctions == 'boolean' ? options.serializeFunctions : false;
this.ignoreUndefined = typeof options.ignoreUndefined == 'boolean' ? options.ignoreUndefined : false;
this.checkKeys = typeof options.checkKeys == 'boolean' ? options.checkKeys : false;
// Unpack the update document
this.limit = typeof remove[0].limit == 'number' ? remove[0].limit : 1;
this.q = remove[0].q;
// Create flag value
this.flags = this.limit == 1 ? 1 : 0;
}
// To Binary
Remove.prototype.toBin = function() {
// Contains all the buffers to be written
var buffers = [];
// Header buffer
var header = new Buffer(
4 * 4 // Header
+ 4 // ZERO
+ Buffer.byteLength(this.ns) + 1 // namespace
+ 4 // Flags
);
// Add header to buffers
buffers.push(header);
// Total length of the message
var totalLength = header.length;
// Serialize the selector
var selector = this.bson.serialize(this.q, {
checkKeys: this.checkKeys,
serializeFunctions: this.serializeFunctions,
ignoreUndefined: this.ignoreUndefined,
});
buffers.push(selector);
totalLength = totalLength + selector.length;
// Index in header buffer
var index = 0;
// Write header length
header[index + 3] = (totalLength >> 24) & 0xff;
header[index + 2] = (totalLength >> 16) & 0xff;
header[index + 1] = (totalLength >> 8) & 0xff;
header[index] = (totalLength) & 0xff;
index = index + 4;
// Write header 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;
// No flags
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;
// Operation
header[index + 3] = (OP_DELETE >> 24) & 0xff;
header[index + 2] = (OP_DELETE >> 16) & 0xff;
header[index + 1] = (OP_DELETE >> 8) & 0xff;
header[index] = (OP_DELETE) & 0xff;
index = index + 4;
// Write ZERO
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 collection name
index = index + header.write(this.ns, index, 'utf8') + 1;
header[index - 1] = 0;
// Write ZERO
header[index + 3] = (this.flags >> 24) & 0xff;
header[index + 2] = (this.flags >> 16) & 0xff;
header[index + 1] = (this.flags >> 8) & 0xff;
header[index] = (this.flags) & 0xff;
index = index + 4;
// Return the buffers
return buffers;
}
module.exports = {
Insert: Insert
, Update: Update
, Remove: Remove
}
@@ -1,27 +0,0 @@
"use strict"
var ReadPreference = require('../topologies/read_preference'),
MongoError = require('../error');
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('readPreference must be a ReadPreference instance');
}
return readPreference;
}
module.exports = {
getReadPreference: getReadPreference
}
-78
View File
@@ -1,78 +0,0 @@
{
"_from": "mongodb-core@2.1.20",
"_id": "mongodb-core@2.1.20",
"_inBundle": false,
"_integrity": "sha512-IN57CX5/Q1bhDq6ShAR6gIv4koFsZP7L8WOK1S0lR0pVDQaScffSMV5jxubLsmZ7J+UdqmykKw4r9hG3XQEGgQ==",
"_location": "/connect-mongo/mongodb-core",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "mongodb-core@2.1.20",
"name": "mongodb-core",
"escapedName": "mongodb-core",
"rawSpec": "2.1.20",
"saveSpec": null,
"fetchSpec": "2.1.20"
},
"_requiredBy": [
"/connect-mongo/mongodb"
],
"_resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-2.1.20.tgz",
"_shasum": "fece8dd76b59ee7d7f2d313b65322c160492d8f1",
"_spec": "mongodb-core@2.1.20",
"_where": "/home/erik/Documents/workspace_brackets/a1_BME_Project_Ohm/om/node_modules/connect-mongo/node_modules/mongodb",
"author": {
"name": "Christian Kvalheim"
},
"bugs": {
"url": "https://github.com/christkv/mongodb-core/issues"
},
"bundleDependencies": false,
"dependencies": {
"bson": "~1.0.4",
"require_optional": "~1.0.0"
},
"deprecated": false,
"description": "Core MongoDB driver functionality, no bells and whistles and meant for integration not end applications",
"devDependencies": {
"co": "^4.5.4",
"conventional-changelog-cli": "^1.3.5",
"coveralls": "^2.11.6",
"es6-promise": "^3.0.2",
"gleak": "0.5.0",
"integra": "0.1.8",
"jsdoc": "3.3.0-alpha8",
"mkdirp": "0.5.0",
"mongodb-topology-manager": "1.0.x",
"mongodb-version-manager": "github:christkv/mongodb-version-manager#master",
"nyc": "^5.5.0",
"optimist": "latest",
"request": "2.65.0",
"rimraf": "2.2.6",
"semver": "4.1.0"
},
"homepage": "https://github.com/christkv/mongodb-core",
"keywords": [
"mongodb",
"core"
],
"license": "Apache-2.0",
"main": "index.js",
"name": "mongodb-core",
"peerOptionalDependencies": {
"kerberos": "~0.0",
"bson-ext": "1.0.5"
},
"repository": {
"type": "git",
"url": "git://github.com/christkv/mongodb-core.git"
},
"scripts": {
"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",
"lint": "eslint lib",
"test": "node test/runner.js -t functional"
},
"version": "2.1.20"
}
-71
View File
@@ -1,71 +0,0 @@
var ReplSet = require('./lib2/topologies/replset'),
ReadPreference = require('./lib2/topologies/read_preference');
// Attempt to connect
var server = new ReplSet([{
host: 'localhost', port: 31000
}, {
host: 'localhost', port: 31001
}], {
setName: 'rs'
});
function executeCursors(_server, cb) {
var count = 100;
for(var i = 0; i < 100; i++) {
// Execute the write
var cursor = _server.cursor('test.test', {
find: 'test.test'
, query: {a:1}
}, {readPreference: new ReadPreference('secondary')});
// Get the first document
cursor.next(function(err, doc) {
count = count - 1;
if(err) console.dir(err)
if(count == 0) return cb();
});
}
}
server.on('connect', function(_server) {
console.log("---------------------------------- 0")
// Attempt authentication
_server.auth('scram-sha-1', 'admin', 'root', 'root', function(err, r) {
console.log("---------------------------------- 1")
// console.dir(err)
// console.dir(r)
_server.insert('test.test', [{a:1}], function(err, r) {
console.log("---------------------------------- 2")
console.dir(err)
if(r)console.dir(r.result)
var name = null;
_server.on('joined', function(_t, _server) {
if(name == _server.name) {
console.log("=========== joined :: " + _t + " :: " + _server.name)
executeCursors(_server, function() {
});
}
})
// var s = _server.s.replicaSetState.secondaries[0];
// s.destroy({emitClose:true});
executeCursors(_server, function() {
console.log("============== 0")
// Attempt to force a server reconnect
var s = _server.s.replicaSetState.secondaries[0];
name = s.name;
s.destroy({emitClose:true});
// console.log("============== 1")
// _server.destroy();
// test.done();
});
});
});
});
server.connect();
-72
View File
@@ -1,72 +0,0 @@
var Server = require('./lib/topologies/server');
// Attempt to connect
var server = new Server({
host: 'localhost', port: 27017, socketTimeout: 500
});
// function executeCursors(_server, cb) {
// var count = 100;
//
// for(var i = 0; i < 100; i++) {
// // Execute the write
// var cursor = _server.cursor('test.test', {
// find: 'test.test'
// , query: {a:1}
// }, {readPreference: new ReadPreference('secondary')});
//
// // Get the first document
// cursor.next(function(err, doc) {
// count = count - 1;
// if(err) console.dir(err)
// if(count == 0) return cb();
// });
// }
// }
server.on('connect', function(_server) {
setInterval(function() {
_server.insert('test.test', [{a:1}], function(err, r) {
console.log("insert")
});
}, 1000)
// console.log("---------------------------------- 0")
// // Attempt authentication
// _server.auth('scram-sha-1', 'admin', 'root', 'root', function(err, r) {
// console.log("---------------------------------- 1")
// // console.dir(err)
// // console.dir(r)
//
// _server.insert('test.test', [{a:1}], function(err, r) {
// console.log("---------------------------------- 2")
// console.dir(err)
// if(r)console.dir(r.result)
// var name = null;
//
// _server.on('joined', function(_t, _server) {
// if(name == _server.name) {
// console.log("=========== joined :: " + _t + " :: " + _server.name)
// executeCursors(_server, function() {
// });
// }
// })
//
// // var s = _server.s.replicaSetState.secondaries[0];
// // s.destroy({emitClose:true});
// executeCursors(_server, function() {
// console.log("============== 0")
// // Attempt to force a server reconnect
// var s = _server.s.replicaSetState.secondaries[0];
// name = s.name;
// s.destroy({emitClose:true});
// // console.log("============== 1")
//
// // _server.destroy();
// // test.done();
// });
// });
// });
});
server.connect();
File diff suppressed because it is too large Load Diff
-1
View File
@@ -1 +0,0 @@
repo_token: GZFmqKPy7XEX0uOl9TDZFUoOQ5AHADMkU
-15
View File
@@ -1,15 +0,0 @@
{
"extends": ["eslint:recommended"],
"env": {
"node": true,
"mocha": true
},
"ecmaFeatures": {
"es6": true,
},
"plugins": [
],
"rules": {
"no-console":0
}
}
File diff suppressed because it is too large Load Diff
-201
View File
@@ -1,201 +0,0 @@
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.
-11
View File
@@ -1,11 +0,0 @@
NODE = node
NPM = npm
JSDOC = jsdoc
name = all
generate_docs:
# cp -R ./HISTORY.md ./docs/content/meta/release-notes.md
hugo -s docs/reference -d ../../public
$(JSDOC) -c conf.json -t docs/jsdoc-template/ -d ./public/api
cp -R ./public/api/scripts ./public/.
cp -R ./public/api/styles ./public/.
-415
View File
@@ -1,415 +0,0 @@
[![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.
## MongoDB Node.JS Driver
| what | where |
|---------------|------------------------------------------------|
| documentation | http://mongodb.github.io/node-mongodb-native/ |
| api-doc | http://mongodb.github.io/node-mongodb-native/2.2/api/ |
| source | https://github.com/mongodb/node-mongodb-native |
| 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
# Installation
The recommended way to get started using the Node.js 2.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 it's dependencies by executing the following `NPM` command.
```
npm install mongodb --save
```
This will download the MongoDB driver and add a dependency entry in your `package.json` file.
## Troubleshooting
The MongoDB driver depends on several other packages. These are.
* mongodb-core
* bson
* kerberos
* 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 to be able 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 what libraries to install.
{{% note class="important" %}}
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.
{{% /note %}}
### Diagnosing on UNIX
If you don’t have the build essentials it won’t build. In the case of linux you will need gcc and 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.
```
git clone https://github.com/christkv/kerberos.git
cd kerberos
npm install
```
If all the steps complete you have the right toolchain installed. If you get node-gyp not found you need to install it globally by doing.
```
npm install -g node-gyp
```
If correctly compiles and runs the tests you are golden. We can now try to install the mongod driver by performing the following command.
```
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.
```
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 known 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 visual studio command prompt. Ensure node.exe is in your path and install node-gyp.
```
npm install -g node-gyp
```
Next you will have to build the project manually to test it. Use any tool you use with git and grab the repo.
```
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. I always recommend that you test your deployment environment first by trying to build node 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 thing is to ensure your user has write permission to wherever the node modules are being installed.
QuickStart
==========
The quick start guide will show you how to setup 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 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
```
Enter the following command and answer the questions to create the initial structure for your new project
```
npm init
```
Next we need to edit the generated package.json file to add the dependency for the MongoDB driver. The package.json file below is just an example and your will look different depending on how you answered the questions after entering `npm init`
```
{
"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": "~2.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 and the database **myproject**.
```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
db.close();
});
```
Given that you booted up the **mongod** process earlier the application should connect successfully and print **Connected correctly to server** to the console.
Let's Add some code to show the different CRUD operations available.
Inserting a Document
--------------------
Let's create a function that will insert some documents for us.
```js
var insertDocuments = function(db, callback) {
// Get the documents collection
var 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 document collection");
callback(result);
});
}
```
The insert command will return a results object that contains several fields that might be useful.
* **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
Let's add call the **insertDocuments** command to the **MongoClient.connect** method callback.
```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
insertDocuments(db, function() {
db.close();
});
});
```
We can now run the update **app.js** file.
```
node app.js
```
You should see the following output after running the **app.js** file.
```
Connected correctly to server
Inserted 3 documents into the document collection
```
Updating a document
-------------------
Let's look at how to do a simple document update by adding a new field **b** to the document that has the field **a** set to **2**.
```js
var updateDocument = function(db, callback) {
// Get the documents collection
var 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 will update the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Let's update the callback function from **MongoClient.connect** to include the update method.
```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
insertDocuments(db, function() {
updateDocument(db, function() {
db.close();
});
});
});
```
Delete a document
-----------------
Next lets delete the document where the field **a** equals to **3**.
```js
var deleteDocument = function(db, callback) {
// Get the documents collection
var 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);
});
}
```
This will delete the first document where the field **a** equals to **3**. Let's add the method to the **MongoClient
.connect** callback function.
```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
insertDocuments(db, function() {
updateDocument(db, function() {
deleteDocument(db, function() {
db.close();
});
});
});
});
```
Finally let's retrieve all the documents using a simple find.
Find All Documents
------------------
We will finish up the Quickstart CRUD methods by performing a simple query that returns all the documents matching the query.
```js
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
assert.equal(2, docs.length);
console.log("Found the following records");
console.dir(docs);
callback(docs);
});
}
```
This query will return all the documents in the **documents** collection. Since we deleted a document the total
documents returned is **2**. Finally let's add the findDocument method to the **MongoClient.connect** callback.
```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
insertDocuments(db, function() {
updateDocument(db, function() {
deleteDocument(db, function() {
findDocuments(db, function() {
db.close();
});
});
});
});
});
```
This concludes the QuickStart of connecting and performing some Basic operations using the MongoDB Node.js driver. For more detailed information you can look at the tutorials covering more specific topics of interest.
## Next Steps
* [MongoDB Documentation](http://mongodb.org/)
* [Read about Schemas](http://learnmongodbthehardway.com/)
* [Star us on GitHub](https://github.com/mongodb/node-mongodb-native)
-41
View File
@@ -1,41 +0,0 @@
Mongodb-core uses third-party libraries or other resources that may
be distributed under licenses different than the Mongodb-core software.
In the event that we accidentally failed to list a required notice,
please bring it to our attention through any of the ways detailed here:
mongodb-dev@googlegroups.com
The attached notices are provided for information only.
For any licenses that require disclosure of source, sources are available at
https://github.com/mongodb/node-mongodb-native.
1) License Notice for nan.h
---------------------------
The MIT License (MIT)
=====================
Copyright (c) 2016 NAN contributors
-----------------------------------
NAN contributors listed at <https://github.com/nodejs/nan#contributors>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to
do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-52
View File
@@ -1,52 +0,0 @@
var ReplSetManager = require('mongodb-topology-manager').ReplSet,
f = require('util').format;
var rsOptions = {
server: {
keyFile: __dirname + '/test/functional/data/keyfile.txt',
auth: null,
replSet: 'rs'
},
client: {
replSet: 'rs'
}
}
// Set up the nodes
var nodes = [{
options: {
bind_ip: 'localhost', port: 31000,
dbpath: f('%s/../db/31000', __dirname),
}
}, {
options: {
bind_ip: 'localhost', port: 31001,
dbpath: f('%s/../db/31001', __dirname),
}
}, {
options: {
bind_ip: 'localhost', port: 31002,
dbpath: f('%s/../db/31002', __dirname),
}
}]
// Merge in any node start up options
for(var i = 0; i < nodes.length; i++) {
for(var name in rsOptions.server) {
nodes[i].options[name] = rsOptions.server[name];
}
}
// Create a manager
var replicasetManager = new ReplSetManager('mongod', nodes, rsOptions.client);
// Purge the set
replicasetManager.purge().then(function() {
// Start the server
replicasetManager.start().then(function() {
process.exit(0);
}).catch(function(e) {
console.log("====== ")
console.dir(e)
// // console.dir(e);
});
});
-75
View File
@@ -1,75 +0,0 @@
{
"plugins": ["plugins/markdown", "docs/lib/jsdoc/examples_plugin.js"],
"source": {
"include": [
"test/functional/operation_example_tests.js",
"test/functional/operation_promises_example_tests.js",
"test/functional/operation_generators_example_tests.js",
"test/functional/authentication_tests.js",
"test/functional/gridfs_stream_tests.js",
"lib/admin.js",
"lib/collection.js",
"lib/cursor.js",
"lib/aggregation_cursor.js",
"lib/command_cursor.js",
"lib/db.js",
"lib/mongo_client.js",
"lib/mongos.js",
"lib/read_preference.js",
"lib/replset.js",
"lib/server.js",
"lib/bulk/common.js",
"lib/bulk/ordered.js",
"lib/bulk/unordered.js",
"lib/gridfs/grid_store.js",
"node_modules/mongodb-core/lib/error.js",
"lib/gridfs-stream/index.js",
"lib/gridfs-stream/download.js",
"lib/gridfs-stream/upload.js",
"node_modules/mongodb-core/lib/connection/logger.js",
"node_modules/bson/lib/bson/binary.js",
"node_modules/bson/lib/bson/code.js",
"node_modules/bson/lib/bson/db_ref.js",
"node_modules/bson/lib/bson/double.js",
"node_modules/bson/lib/bson/long.js",
"node_modules/bson/lib/bson/objectid.js",
"node_modules/bson/lib/bson/symbol.js",
"node_modules/bson/lib/bson/timestamp.js",
"node_modules/bson/lib/bson/max_key.js",
"node_modules/bson/lib/bson/min_key.js",
"node_modules/bson/lib/bson/decimal128.js",
"node_modules/bson/lib/bson/int_32.js",
"node_modules/bson/lib/bson/regexp.js"
]
},
"templates": {
"cleverLinks": true,
"monospaceLinks": true,
"default": {
"outputSourceFiles" : true
},
"applicationName": "Node.js MongoDB Driver API",
"googleAnalytics": "UA-29229787-1",
"openGraph": {
"title": "",
"type": "website",
"image": "",
"site_name": "",
"url": ""
},
"meta": {
"title": "",
"description": "",
"keyword": ""
},
"linenums": true
},
"markdown": {
"parser": "gfm",
"hardwrap": true,
"tags": ["examples"]
},
"examples": {
"indent": 4
}
}
-56
View File
@@ -1,56 +0,0 @@
// Core module
var core = require('mongodb-core'),
Instrumentation = require('./lib/apm');
// Set up the connect function
var connect = require('./lib/mongo_client').connect;
// Expose error class
connect.MongoError = core.MongoError;
// 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/server');
connect.ReplSet = require('./lib/replset');
connect.Mongos = require('./lib/mongos');
connect.ReadPreference = require('./lib/read_preference');
connect.GridStore = require('./lib/gridfs/grid_store');
connect.Chunk = require('./lib/gridfs/chunk');
connect.Logger = core.Logger;
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 = {};
return new Instrumentation(core, options, callback);
}
// Set our exports to be the connect function
module.exports = connect;
-231
View File
@@ -1,231 +0,0 @@
var MongoClient = require('./').MongoClient,
assert = require('assert');
// var memwatch = require('memwatch-next');
// memwatch.on('leak', function(info) {
// console.log("======== leak")
// });
//
// memwatch.on('stats', function(stats) {
// console.log("======== stats")
// console.dir(stats)
// });
// // Take first snapshot
// var hd = new memwatch.HeapDiff();
MongoClient.connect('mongodb://localhost:27017/bench', function(err, db) {
var docs = [];
var total = 1000;
var count = total;
var measurements = [];
// Insert a bunch of documents
for(var i = 0; i < 100; i++) {
docs.push(JSON.parse(data));
}
var col = db.collection('inserts');
function execute(col, callback) {
var start = new Date().getTime();
col.find({}).limit(100).toArray(function(e, docs) {
measurements.push(new Date().getTime() - start);
assert.equal(null, e);
callback();
});
}
console.log("== insert documents")
col.insert(docs, function(e, r) {
docs = [];
assert.equal(null, e);
console.log("== start bench")
for(var i = 0; i < total; i++) {
execute(col, function(e) {
count = count - 1;
if(count == 0) {
// Calculate total execution time for operations
var totalTime = measurements.reduce(function(prev, curr) {
return prev + curr;
}, 0);
console.log("===========================================");
console.log("total time: " + totalTime)
// var diff = hd.end();
// console.log("===========================================");
// console.log(JSON.stringify(diff, null, 2))
db.close();
process.exit(0)
}
});
}
});
});
var data = JSON.stringify({
"data": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
],
"collection_name": "test",
"database_name": "command-monitoring-tests",
"tests": [
{
"description": "A successful mixed bulk write",
"operation": {
"name": "bulkWrite",
"arguments": {
"requests": [
{
"insertOne": {
"document": {
"_id": 4,
"x": 44
}
}
},
{
"updateOne": {
"filter": {
"_id": 3
},
"update": {
"set": {
"x": 333
}
}
}
}
]
}
},
"expectations": [
{
"command_started_event": {
"command": {
"insert": "test",
"documents": [
{
"_id": 4,
"x": 44
}
],
"ordered": true
},
"command_name": "insert",
"database_name": "command-monitoring-tests"
}
},
{
"command_succeeded_event": {
"reply": {
"ok": 1.0,
"n": 1
},
"command_name": "insert"
}
},
{
"command_started_event": {
"command": {
"update": "test",
"updates": [
{
"q": {
"_id": 3
},
"u": {
"set": {
"x": 333
}
},
"upsert": false,
"multi": false
}
],
"ordered": true
},
"command_name": "update",
"database_name": "command-monitoring-tests"
}
},
{
"command_succeeded_event": {
"reply": {
"ok": 1.0,
"n": 1
},
"command_name": "update"
}
}
]
},
{
"description": "A successful unordered bulk write with an unacknowledged write concern",
"operation": {
"name": "bulkWrite",
"arguments": {
"requests": [
{
"insertOne": {
"document": {
"_id": 4,
"x": 44
}
}
}
],
"ordered": false,
"writeConcern": {
"w": 0
}
}
},
"expectations": [
{
"command_started_event": {
"command": {
"insert": "test",
"documents": [
{
"_id": 4,
"x": 44
}
],
"ordered": false,
"writeConcern": {
"w": 0
}
},
"command_name": "insert",
"database_name": "command-monitoring-tests"
}
},
{
"command_succeeded_event": {
"reply": {
"ok": 1.0
},
"command_name": "insert"
}
}
]
}
]
});
-579
View File
@@ -1,579 +0,0 @@
"use strict";
var toError = require('./utils').toError,
Define = require('./metadata'),
shallowClone = require('./utils').shallowClone,
assign = require('./utils').assign,
authenticate = require('./authenticate');
/**
* @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
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* // Use the admin database for the operation
* var adminDb = db.admin();
*
* // List all the available databases
* adminDb.listDatabases(function(err, dbs) {
* test.equal(null, err);
* test.ok(dbs.databases.length > 0);
* db.close();
* });
* });
*/
/**
* Create a new Admin instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @return {Admin} a collection instance.
*/
var Admin = function(db, topology, promiseLibrary) {
if(!(this instanceof Admin)) return new Admin(db, topology);
// Internal state
this.s = {
db: db
, topology: topology
, promiseLibrary: promiseLibrary
}
}
var define = Admin.define = new Define('Admin', Admin, false);
/**
* 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=null] Optional settings.
* @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {number} [options.maxTimeMS=null] 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) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
if(typeof callback != 'function') args.push(callback);
options = args.length ? args.shift() : {};
// Execute using callback
if(typeof callback == 'function') return this.s.db.executeDbAdminCommand(command, options, function(err, doc) {
return callback != null ? callback(err, doc) : null;
});
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.executeDbAdminCommand(command, options, function(err, doc) {
if(err) return reject(err);
resolve(doc);
});
});
}
define.classMethod('command', {callback: true, promise:true});
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.buildInfo = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return this.serverInfo(callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.serverInfo(function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
define.classMethod('buildInfo', {callback: true, promise:true});
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.serverInfo = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return this.s.db.executeDbAdminCommand({buildinfo:1}, function(err, doc) {
if(err != null) return callback(err, null);
callback(null, doc);
});
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.executeDbAdminCommand({buildinfo:1}, function(err, doc) {
if(err) return reject(err);
resolve(doc);
});
});
}
define.classMethod('serverInfo', {callback: true, promise:true});
/**
* Retrieve this db's server status.
*
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.serverStatus = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return serverStatus(self, callback)
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
serverStatus(self, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
};
var serverStatus = function(self, callback) {
self.s.db.executeDbAdminCommand({serverStatus: 1}, function(err, doc) {
if(err == null && doc.ok === 1) {
callback(null, doc);
} else {
if(err) return callback(err, false);
return callback(toError(doc), false);
}
});
}
define.classMethod('serverStatus', {callback: true, promise:true});
/**
* Retrieve the current profiling Level for MongoDB
*
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.profilingLevel = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return profilingLevel(self, callback)
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
profilingLevel(self, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
};
var profilingLevel = function(self, callback) {
self.s.db.executeDbAdminCommand({profile:-1}, function(err, doc) {
if(err == null && doc.ok === 1) {
var was = doc.was;
if(was == 0) return callback(null, "off");
if(was == 1) return callback(null, "slow_only");
if(was == 2) return callback(null, "all");
return callback(new Error("Error: illegal profiling level value " + was), null);
} else {
err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
}
});
}
define.classMethod('profilingLevel', {callback: true, promise:true});
/**
* Ping the MongoDB server and retrieve results
*
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.ping = function(options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 0);
callback = args.pop();
if(typeof callback != 'function') args.push(callback);
// Execute using callback
if(typeof callback == 'function') return this.s.db.executeDbAdminCommand({ping: 1}, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.executeDbAdminCommand({ping: 1}, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
define.classMethod('ping', {callback: true, promise:true});
/**
* Authenticate a user against the server.
* @method
* @param {string} username The username.
* @param {string} [password] The password.
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
* @deprecated This method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
*/
Admin.prototype.authenticate = function(username, password, options, callback) {
console.warn("Admin.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.");
var finalArguments = [this.s.db];
if(typeof username == 'string') finalArguments.push(username);
if(typeof password == 'string') finalArguments.push(password);
if(typeof options == 'function') {
finalArguments.push({ authdb: 'admin' });
finalArguments.push(options);
} else {
finalArguments.push(assign({}, options, { authdb: 'admin' }));
}
if(typeof callback == 'function') finalArguments.push(callback);
// Execute authenticate method
return authenticate.apply(this.s.db, finalArguments);
}
define.classMethod('authenticate', {callback: true, promise:true});
/**
* Logout user from server, fire off on all connections and remove all auth info
* @method
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.logout = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return this.s.db.logout({dbName: 'admin'}, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.logout({dbName: 'admin'}, function(err) {
if(err) return reject(err);
resolve(true);
});
});
}
define.classMethod('logout', {callback: true, promise:true});
// Get write concern
var writeConcern = function(options, db) {
options = shallowClone(options);
// If options already contain write concerns return it
if(options.w || options.wtimeout || options.j || options.fsync) {
return options;
}
// Set db write concern if available
if(db.writeConcern) {
if(options.w) options.w = db.writeConcern.w;
if(options.wtimeout) options.wtimeout = db.writeConcern.wtimeout;
if(options.j) options.j = db.writeConcern.j;
if(options.fsync) options.fsync = db.writeConcern.fsync;
}
// Return modified options
return options;
}
/**
* Add a user to the database.
* @method
* @param {string} username The username.
* @param {string} password The password.
* @param {object} [options=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] 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=null] Custom data associated with the user (only Mongodb 2.6 or higher)
* @param {object[]} [options.roles=null] Roles associated with the created user (only Mongodb 2.6 or higher)
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.addUser = function(username, password, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 2);
callback = args.pop();
if(typeof callback != 'function') args.push(callback);
options = args.length ? args.shift() : {};
options = options || {};
// Get the options
options = writeConcern(options, self.s.db)
// Set the db name to admin
options.dbName = 'admin';
// Execute using callback
if(typeof callback == 'function')
return self.s.db.addUser(username, password, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.addUser(username, password, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
define.classMethod('addUser', {callback: true, promise:true});
/**
* Remove a user from a database
* @method
* @param {string} username The username.
* @param {object} [options=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] 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 {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.removeUser = function(username, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
if(typeof callback != 'function') args.push(callback);
options = args.length ? args.shift() : {};
options = options || {};
// Get the options
options = writeConcern(options, self.s.db)
// Set the db name
options.dbName = 'admin';
// Execute using callback
if(typeof callback == 'function')
return self.s.db.removeUser(username, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.removeUser(username, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
define.classMethod('removeUser', {callback: true, promise:true});
/**
* Set the current profiling level of MongoDB
*
* @param {string} level The new profiling level (off, slow_only, all).
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.setProfilingLevel = function(level, callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return setProfilingLevel(self, level, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
setProfilingLevel(self, level, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
};
var setProfilingLevel = function(self, level, callback) {
var command = {};
var profile = 0;
if(level == "off") {
profile = 0;
} else if(level == "slow_only") {
profile = 1;
} else if(level == "all") {
profile = 2;
} else {
return callback(new Error("Error: illegal profiling level value " + level));
}
// Set up the profile number
command['profile'] = profile;
self.s.db.executeDbAdminCommand(command, function(err, doc) {
if(err == null && doc.ok === 1)
return callback(null, level);
return err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
});
}
define.classMethod('setProfilingLevel', {callback: true, promise:true});
/**
* Retrieve the current profiling information for MongoDB
*
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
* @deprecated Query the system.profile collection directly.
*/
Admin.prototype.profilingInfo = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return profilingInfo(self, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
profilingInfo(self, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
};
var profilingInfo = function(self, callback) {
try {
self.s.topology.cursor("admin.system.profile", { find: 'system.profile', query: {}}, {}).toArray(callback);
} catch (err) {
return callback(err, null);
}
}
define.classMethod('profilingLevel', {callback: true, promise:true});
/**
* Validate an existing collection
*
* @param {string} collectionName The name of the collection to validate.
* @param {object} [options=null] Optional settings.
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.validateCollection = function(collectionName, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
if(typeof callback != 'function') args.push(callback);
options = args.length ? args.shift() : {};
options = options || {};
// Execute using callback
if(typeof callback == 'function')
return validateCollection(self, collectionName, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
validateCollection(self, collectionName, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
};
var validateCollection = function(self, collectionName, options, callback) {
var command = {validate: collectionName};
var keys = Object.keys(options);
// Decorate command with extra options
for(var i = 0; i < keys.length; i++) {
if(options.hasOwnProperty(keys[i])) {
command[keys[i]] = options[keys[i]];
}
}
self.s.db.command(command, function(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);
});
}
define.classMethod('validateCollection', {callback: true, promise:true});
/**
* List the available databases
*
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.listDatabases = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return self.s.db.executeDbAdminCommand({listDatabases:1}, {}, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.executeDbAdminCommand({listDatabases:1}, {}, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
define.classMethod('listDatabases', {callback: true, promise:true});
/**
* Get ReplicaSet status
*
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.replSetGetStatus = function(callback) {
var self = this;
// Execute using callback
if(typeof callback == 'function') return replSetGetStatus(self, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
replSetGetStatus(self, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
};
var replSetGetStatus = function(self, callback) {
self.s.db.executeDbAdminCommand({replSetGetStatus:1}, function(err, doc) {
if(err == null && doc.ok === 1)
return callback(null, doc);
if(err) return callback(err, false);
callback(toError(doc), false);
});
}
define.classMethod('replSetGetStatus', {callback: true, promise:true});
module.exports = Admin;
@@ -1,438 +0,0 @@
"use strict";
var inherits = require('util').inherits
, MongoError = require('mongodb-core').MongoError
, Readable = require('stream').Readable || require('readable-stream').Readable
, Define = require('./metadata')
, 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 0.10.X
* or higher stream
*
* **AGGREGATIONCURSOR Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* // Create a collection we want to drop later
* var col = db.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);
* db.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;
// No promise library selected fall back
if(!promiseLibrary) {
promiseLibrary = typeof global.Promise == 'function' ?
global.Promise : require('es6-promise').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
}
}
/**
* 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];
}
var define = AggregationCursor.define = new Define('AggregationCursor', AggregationCursor, true);
/**
* 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", driver:true });
if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
this.setCursorBatchSize(value);
return this;
}
define.classMethod('batchSize', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('geoNear', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('group', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('limit', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('match', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('out', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('project', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('lookup', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('redact', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('skip', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('sort', {callback: false, promise:false, returns: [AggregationCursor]});
/**
* 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;
}
define.classMethod('unwind', {callback: false, promise:false, returns: [AggregationCursor]});
AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
// Inherited methods
define.classMethod('toArray', {callback: true, promise:true});
define.classMethod('each', {callback: true, promise:false});
define.classMethod('forEach', {callback: true, promise:false});
define.classMethod('hasNext', {callback: true, promise:true});
define.classMethod('next', {callback: true, promise:true});
define.classMethod('close', {callback: true, promise:true});
define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
define.classMethod('rewind', {callback: false, promise:false});
define.classMethod('bufferedCount', {callback: false, promise:false, returns: [Number]});
define.classMethod('readBufferedDocuments', {callback: false, promise:false, returns: [Array]});
/**
* 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 previously 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 previously 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;
-596
View File
@@ -1,596 +0,0 @@
var EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits;
// Get prototypes
var AggregationCursor = require('./aggregation_cursor'),
CommandCursor = require('./command_cursor'),
OrderedBulkOperation = require('./bulk/ordered').OrderedBulkOperation,
UnorderedBulkOperation = require('./bulk/unordered').UnorderedBulkOperation,
GridStore = require('./gridfs/grid_store'),
Cursor = require('./cursor'),
Collection = require('./collection'),
Db = require('./db');
var basicOperationIdGenerator = {
operationId: 1,
next: function() {
return this.operationId++;
}
}
var basicTimestampGenerator = {
current: function() {
return new Date().getTime();
},
duration: function(start, end) {
return end - start;
}
}
var senstiveCommands = ['authenticate', 'saslStart', 'saslContinue', 'getnonce',
'createUser', 'updateUser', 'copydbgetnonce', 'copydbsaslstart', 'copydb'];
var Instrumentation = function(core, options, callback) {
options = options || {};
// Optional id generators
var operationIdGenerator = options.operationIdGenerator || basicOperationIdGenerator;
// Optional timestamp generator
var timestampGenerator = options.timestampGenerator || basicTimestampGenerator;
// Extend with event emitter functionality
EventEmitter.call(this);
// Contains all the instrumentation overloads
this.overloads = [];
// ---------------------------------------------------------
//
// Instrument prototype
//
// ---------------------------------------------------------
var instrumentPrototype = function(callback) {
var instrumentations = []
// Classes to support
var classes = [GridStore, OrderedBulkOperation, UnorderedBulkOperation,
CommandCursor, AggregationCursor, Cursor, Collection, Db];
// Add instrumentations to the available list
for(var i = 0; i < classes.length; i++) {
if(classes[i].define) {
instrumentations.push(classes[i].define.generate());
}
}
// Return the list of instrumentation points
callback(null, instrumentations);
}
// Did the user want to instrument the prototype
if(typeof callback == 'function') {
instrumentPrototype(callback);
}
// ---------------------------------------------------------
//
// Server
//
// ---------------------------------------------------------
// Reference
var self = this;
// Names of methods we need to wrap
var methods = ['command', 'insert', 'update', 'remove'];
// Prototype
var proto = core.Server.prototype;
// Core server method we are going to wrap
methods.forEach(function(x) {
var func = proto[x];
// Add to overloaded methods
self.overloads.push({proto: proto, name:x, func:func});
// The actual prototype
proto[x] = function() {
var requestId = core.Query.nextRequestId();
// Get the arguments
var args = Array.prototype.slice.call(arguments, 0);
var ns = args[0];
var commandObj = args[1];
var options = args[2] || {};
var keys = Object.keys(commandObj);
var commandName = keys[0];
var db = ns.split('.')[0];
// Get the collection
var col = ns.split('.');
col.shift();
col = col.join('.');
// Do we have a legacy insert/update/remove command
if(x == 'insert') { //} && !this.lastIsMaster().maxWireVersion) {
commandName = 'insert';
// Re-write the command
commandObj = {
insert: col, documents: commandObj
}
if(options.writeConcern && Object.keys(options.writeConcern).length > 0) {
commandObj.writeConcern = options.writeConcern;
}
commandObj.ordered = options.ordered != undefined ? options.ordered : true;
} else if(x == 'update') { // && !this.lastIsMaster().maxWireVersion) {
commandName = 'update';
// Re-write the command
commandObj = {
update: col, updates: commandObj
}
if(options.writeConcern && Object.keys(options.writeConcern).length > 0) {
commandObj.writeConcern = options.writeConcern;
}
commandObj.ordered = options.ordered != undefined ? options.ordered : true;
} else if(x == 'remove') { //&& !this.lastIsMaster().maxWireVersion) {
commandName = 'delete';
// Re-write the command
commandObj = {
delete: col, deletes: commandObj
}
if(options.writeConcern && Object.keys(options.writeConcern).length > 0) {
commandObj.writeConcern = options.writeConcern;
}
commandObj.ordered = options.ordered != undefined ? options.ordered : true;
}
// Get the callback
var callback = args.pop();
// Set current callback operation id from the current context or create
// a new one
var ourOpId = callback.operationId || operationIdGenerator.next();
// Get a connection reference for this server instance
var connection = this.s.pool.get()
// Emit the start event for the command
var command = {
// Returns the command.
command: commandObj,
// Returns the database name.
databaseName: db,
// Returns the command name.
commandName: commandName,
// Returns the driver generated request id.
requestId: requestId,
// Returns the driver generated operation id.
// This is used to link events together such as bulk write operations. OPTIONAL.
operationId: ourOpId,
// Returns the connection id for the command. For languages that do not have this,
// this MUST return the driver equivalent which MUST include the server address and port.
// The name of this field is flexible to match the object that is returned from the driver.
connectionId: connection
};
// Filter out any sensitive commands
if(senstiveCommands.indexOf(commandName.toLowerCase()) != -1) {
command.commandObj = {};
command.commandObj[commandName] = true;
}
// Emit the started event
self.emit('started', command)
// Start time
var startTime = timestampGenerator.current();
// Push our handler callback
args.push(function(err, r) {
var endTime = timestampGenerator.current();
var command = {
duration: timestampGenerator.duration(startTime, endTime),
commandName: commandName,
requestId: requestId,
operationId: ourOpId,
connectionId: connection
};
// If we have an error
if(err || (r && r.result && r.result.ok == 0)) {
command.failure = err || r.result.writeErrors || r.result;
// Filter out any sensitive commands
if(senstiveCommands.indexOf(commandName.toLowerCase()) != -1) {
command.failure = {};
}
self.emit('failed', command);
} else if(commandObj && commandObj.writeConcern
&& commandObj.writeConcern.w == 0) {
// If we have write concern 0
command.reply = {ok:1};
self.emit('succeeded', command);
} else {
command.reply = r && r.result ? r.result : r;
// Filter out any sensitive commands
if(senstiveCommands.indexOf(commandName.toLowerCase()) != -1) {
command.reply = {};
}
self.emit('succeeded', command);
}
// Return to caller
callback(err, r);
});
// Apply the call
func.apply(this, args);
}
});
// ---------------------------------------------------------
//
// Bulk Operations
//
// ---------------------------------------------------------
// Inject ourselves into the Bulk methods
methods = ['execute'];
var prototypes = [
require('./bulk/ordered').Bulk.prototype,
require('./bulk/unordered').Bulk.prototype
]
prototypes.forEach(function(proto) {
// Core server method we are going to wrap
methods.forEach(function(x) {
var func = proto[x];
// Add to overloaded methods
self.overloads.push({proto: proto, name:x, func:func});
// The actual prototype
proto[x] = function() {
// Get the arguments
var args = Array.prototype.slice.call(arguments, 0);
// Set an operation Id on the bulk object
this.operationId = operationIdGenerator.next();
// Get the callback
var callback = args.pop();
// If we have a callback use this
if(typeof callback == 'function') {
args.push(function(err, r) {
// Return to caller
callback(err, r);
});
// Apply the call
func.apply(this, args);
} else {
return func.apply(this, args);
}
}
});
});
// ---------------------------------------------------------
//
// Cursor
//
// ---------------------------------------------------------
// Inject ourselves into the Cursor methods
methods = ['_find', '_getmore', '_killcursor'];
prototypes = [
require('./cursor').prototype,
require('./command_cursor').prototype,
require('./aggregation_cursor').prototype
]
// Command name translation
var commandTranslation = {
'_find': 'find', '_getmore': 'getMore', '_killcursor': 'killCursors', '_explain': 'explain'
}
prototypes.forEach(function(proto) {
// Core server method we are going to wrap
methods.forEach(function(x) {
var func = proto[x];
// Add to overloaded methods
self.overloads.push({proto: proto, name:x, func:func});
// The actual prototype
proto[x] = function() {
var cursor = this;
var requestId = core.Query.nextRequestId();
var ourOpId = operationIdGenerator.next();
var parts = this.ns.split('.');
var db = parts[0];
// Get the collection
parts.shift();
var collection = parts.join('.');
// Set the command
var command = this.query;
var cmd = this.s.cmd;
// If we have a find method, set the operationId on the cursor
if(x == '_find') {
cursor.operationId = ourOpId;
}
// Do we have a find command rewrite it
if(x == '_getmore') {
command = {
getMore: this.cursorState.cursorId,
collection: collection,
batchSize: cmd.batchSize
}
if(cmd.maxTimeMS) command.maxTimeMS = cmd.maxTimeMS;
} else if(x == '_killcursor') {
command = {
killCursors: collection,
cursors: [this.cursorState.cursorId]
}
} else if(cmd.find) {
command = {
find: collection, filter: cmd.query
}
if(cmd.sort) command.sort = cmd.sort;
if(cmd.fields) command.projection = cmd.fields;
if(cmd.limit && cmd.limit < 0) {
command.limit = Math.abs(cmd.limit);
command.singleBatch = true;
} else if(cmd.limit) {
command.limit = Math.abs(cmd.limit);
}
// Options
if(cmd.skip) command.skip = cmd.skip;
if(cmd.hint) command.hint = cmd.hint;
if(cmd.batchSize) command.batchSize = cmd.batchSize;
if(typeof cmd.returnKey == 'boolean') command.returnKey = cmd.returnKey;
if(cmd.comment) command.comment = cmd.comment;
if(cmd.min) command.min = cmd.min;
if(cmd.max) command.max = cmd.max;
if(cmd.maxScan) command.maxScan = cmd.maxScan;
if(cmd.maxTimeMS) command.maxTimeMS = cmd.maxTimeMS;
// Flags
if(typeof cmd.awaitData == 'boolean') command.awaitData = cmd.awaitData;
if(typeof cmd.snapshot == 'boolean') command.snapshot = cmd.snapshot;
if(typeof cmd.tailable == 'boolean') command.tailable = cmd.tailable;
if(typeof cmd.oplogReplay == 'boolean') command.oplogReplay = cmd.oplogReplay;
if(typeof cmd.noCursorTimeout == 'boolean') command.noCursorTimeout = cmd.noCursorTimeout;
if(typeof cmd.partial == 'boolean') command.partial = cmd.partial;
if(typeof cmd.showDiskLoc == 'boolean') command.showRecordId = cmd.showDiskLoc;
// Read Concern
if(cmd.readConcern) command.readConcern = cmd.readConcern;
// Override method
if(cmd.explain) command.explain = cmd.explain;
if(cmd.exhaust) command.exhaust = cmd.exhaust;
// If we have a explain flag
if(cmd.explain) {
// Create fake explain command
command = {
explain: command,
verbosity: 'allPlansExecution'
}
// Set readConcern on the command if available
if(cmd.readConcern) command.readConcern = cmd.readConcern
// Set up the _explain name for the command
x = '_explain';
}
} else {
command = cmd;
}
// Set up the connection
var connectionId = null;
// Set local connection
if(this.connection) connectionId = this.connection;
if(!connectionId && this.server && this.server.getConnection) connectionId = this.server.getConnection();
// Get the command Name
var commandName = x == '_find' ? Object.keys(command)[0] : commandTranslation[x];
// Emit the start event for the command
command = {
// Returns the command.
command: command,
// Returns the database name.
databaseName: db,
// Returns the command name.
commandName: commandName,
// Returns the driver generated request id.
requestId: requestId,
// Returns the driver generated operation id.
// This is used to link events together such as bulk write operations. OPTIONAL.
operationId: this.operationId,
// Returns the connection id for the command. For languages that do not have this,
// this MUST return the driver equivalent which MUST include the server address and port.
// The name of this field is flexible to match the object that is returned from the driver.
connectionId: connectionId
};
// Get the arguments
var args = Array.prototype.slice.call(arguments, 0);
// Get the callback
var callback = args.pop();
// We do not have a callback but a Promise
if(typeof callback == 'function' || command.commandName == 'killCursors') {
var startTime = timestampGenerator.current();
// Emit the started event
self.emit('started', command)
// Emit succeeded event with killcursor if we have a legacy protocol
if(command.commandName == 'killCursors'
&& this.server.lastIsMaster()
&& this.server.lastIsMaster().maxWireVersion < 4) {
// Emit the succeeded command
command = {
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
commandName: commandName,
requestId: requestId,
operationId: cursor.operationId,
connectionId: cursor.server.getConnection(),
reply: [{ok:1}]
};
// Apply callback to the list of args
args.push(callback);
// Apply the call
func.apply(this, args);
// Emit the command
return self.emit('succeeded', command)
}
// Add our callback handler
args.push(function(err, r) {
if(err) {
// Command
var command = {
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
commandName: commandName,
requestId: requestId,
operationId: ourOpId,
connectionId: cursor.server.getConnection(),
failure: err };
// Emit the command
self.emit('failed', command)
} else {
// Do we have a getMore
if(commandName.toLowerCase() == 'getmore' && r == null) {
r = {
cursor: {
id: cursor.cursorState.cursorId,
ns: cursor.ns,
nextBatch: cursor.cursorState.documents
}, ok:1
}
} else if((commandName.toLowerCase() == 'find'
|| commandName.toLowerCase() == 'aggregate'
|| commandName.toLowerCase() == 'listcollections') && r == null) {
r = {
cursor: {
id: cursor.cursorState.cursorId,
ns: cursor.ns,
firstBatch: cursor.cursorState.documents
}, ok:1
}
} else if(commandName.toLowerCase() == 'killcursors' && r == null) {
r = {
cursorsUnknown:[cursor.cursorState.lastCursorId],
ok:1
}
}
// cursor id is zero, we can issue success command
command = {
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
commandName: commandName,
requestId: requestId,
operationId: cursor.operationId,
connectionId: cursor.server.getConnection(),
reply: r && r.result ? r.result : r
};
// Emit the command
self.emit('succeeded', command)
}
// Return
if(!callback) return;
// Return to caller
callback(err, r);
});
// Apply the call
func.apply(this, args);
} else {
// Assume promise, push back the missing value
args.push(callback);
// Get the promise
var promise = func.apply(this, args);
// Return a new promise
return new cursor.s.promiseLibrary(function(resolve, reject) {
var startTime = timestampGenerator.current();
// Emit the started event
self.emit('started', command)
// Execute the function
promise.then(function() {
// cursor id is zero, we can issue success command
var command = {
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
commandName: commandName,
requestId: requestId,
operationId: cursor.operationId,
connectionId: cursor.server.getConnection(),
reply: cursor.cursorState.documents
};
// Emit the command
self.emit('succeeded', command)
}).catch(function(err) {
// Command
var command = {
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
commandName: commandName,
requestId: requestId,
operationId: ourOpId,
connectionId: cursor.server.getConnection(),
failure: err };
// Emit the command
self.emit('failed', command)
// reject the promise
reject(err);
});
});
}
}
});
});
}
inherits(Instrumentation, EventEmitter);
Instrumentation.prototype.uninstrument = function() {
for(var i = 0; i < this.overloads.length; i++) {
var obj = this.overloads[i];
obj.proto[obj.name] = obj.func;
}
// Remove all listeners
this.removeAllListeners('started');
this.removeAllListeners('succeeded');
this.removeAllListeners('failed');
}
module.exports = Instrumentation;
-110
View File
@@ -1,110 +0,0 @@
var shallowClone = require('./utils').shallowClone
, handleCallback = require('./utils').handleCallback
, MongoError = require('mongodb-core').MongoError
, f = require('util').format;
var authenticate = function(self, username, password, options, callback) {
// Did the user destroy the topology
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
// the default db to authenticate against is 'self'
// if authenticate is called from a retry context, it may be another one, like admin
var authdb = options.dbName ? options.dbName : self.databaseName;
authdb = self.authSource ? self.authSource : authdb;
authdb = options.authdb ? options.authdb : authdb;
authdb = options.authSource ? options.authSource : authdb;
// Callback
var _callback = function(err, result) {
if(self.listeners('authenticated').length > 0) {
self.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') {
self.s.topology.auth('mongocr', authdb, username, password, function(err) {
if(err) return handleCallback(callback, err, false);
_callback(null, true);
});
} else if(authMechanism == 'PLAIN') {
self.s.topology.auth('plain', authdb, username, password, function(err) {
if(err) return handleCallback(callback, err, false);
_callback(null, true);
});
} else if(authMechanism == 'MONGODB-X509') {
self.s.topology.auth('x509', authdb, username, password, function(err) {
if(err) return handleCallback(callback, err, false);
_callback(null, true);
});
} else if(authMechanism == 'SCRAM-SHA-1') {
self.s.topology.auth('scram-sha-1', authdb, username, password, function(err) {
if(err) return handleCallback(callback, err, false);
_callback(null, true);
});
} else if(authMechanism == 'GSSAPI') {
if(process.platform == 'win32') {
self.s.topology.auth('sspi', authdb, username, password, options, function(err) {
if(err) return handleCallback(callback, err, false);
_callback(null, true);
});
} else {
self.s.topology.auth('gssapi', authdb, username, password, options, function(err) {
if(err) return handleCallback(callback, err, false);
_callback(null, true);
});
}
} else if(authMechanism == 'DEFAULT') {
self.s.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 = {};
// 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 != 'PLAIN') {
return handleCallback(callback, MongoError.create({message: "only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, SCRAM-SHA-1 or MONGODB-CR 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);
});
});
};
-440
View File
@@ -1,440 +0,0 @@
"use strict";
var Long = require('mongodb-core').BSON.Long,
Timestamp = require('mongodb-core').BSON.Timestamp;
// Error codes
var UNKNOWN_ERROR = 8;
var INVALID_BSON_ERROR = 22;
var WRITE_CONCERN_ERROR = 64;
var MULTIPLE_ERROR = 65;
// Insert types
var INSERT = 1;
var UPDATE = 2;
var REMOVE = 3
// Get write concern
var writeConcern = function(target, col, options) {
var writeConcern = {};
// Collection level write concern
if(col.writeConcern && col.writeConcern.w != null) writeConcern.w = col.writeConcern.w;
if(col.writeConcern && col.writeConcern.j != null) writeConcern.j = col.writeConcern.j;
if(col.writeConcern && col.writeConcern.fsync != null) writeConcern.fsync = col.writeConcern.fsync;
if(col.writeConcern && col.writeConcern.wtimeout != null) writeConcern.wtimeout = col.writeConcern.wtimeout;
// Options level write concern
if(options && options.w != null) writeConcern.w = options.w;
if(options && options.wtimeout != null) writeConcern.wtimeout = options.wtimeout;
if(options && options.j != null) writeConcern.j = options.j;
if(options && options.fsync != null) writeConcern.fsync = options.fsync;
// Return write concern
return writeConcern;
}
/**
* Helper function to define properties
* @ignore
*/
var defineReadOnlyProperty = function(self, name, value) {
Object.defineProperty(self, name, {
enumerable: true
, get: function() {
return value;
}
});
}
/**
* Keeps the state of a unordered batch so we can rewrite the results
* correctly after command execution
* @ignore
*/
var Batch = function(batchType, originalZeroIndex) {
this.originalZeroIndex = originalZeroIndex;
this.currentIndex = 0;
this.originalIndexes = [];
this.batchType = batchType;
this.operations = [];
this.size = 0;
this.sizeBytes = 0;
}
/**
* Wraps a legacy operation so we can correctly rewrite it's error
* @ignore
*/
var LegacyOp = function(batchType, operation, index) {
this.batchType = batchType;
this.index = index;
this.operation = operation;
}
/**
* Create a new BulkWriteResult instance (INTERNAL TYPE, do not instantiate directly)
*
* @class
* @property {boolean} ok Did bulk operation correctly execute
* @property {number} nInserted number of inserted documents
* @property {number} nUpdated number of documents updated logically
* @property {number} nUpserted Number of upserted documents
* @property {number} nModified Number of documents updated physically on disk
* @property {number} nRemoved Number of removed documents
* @return {BulkWriteResult} a BulkWriteResult instance
*/
var BulkWriteResult = function(bulkResult) {
defineReadOnlyProperty(this, "ok", bulkResult.ok);
defineReadOnlyProperty(this, "nInserted", bulkResult.nInserted);
defineReadOnlyProperty(this, "nUpserted", bulkResult.nUpserted);
defineReadOnlyProperty(this, "nMatched", bulkResult.nMatched);
defineReadOnlyProperty(this, "nModified", bulkResult.nModified);
defineReadOnlyProperty(this, "nRemoved", bulkResult.nRemoved);
/**
* Return an array of inserted ids
*
* @return {object[]}
*/
this.getInsertedIds = function() {
return bulkResult.insertedIds;
}
/**
* Return an array of upserted ids
*
* @return {object[]}
*/
this.getUpsertedIds = function() {
return bulkResult.upserted;
}
/**
* Return the upserted id at position x
*
* @param {number} index the number of the upserted id to return, returns undefined if no result for passed in index
* @return {object}
*/
this.getUpsertedIdAt = function(index) {
return bulkResult.upserted[index];
}
/**
* Return raw internal result
*
* @return {object}
*/
this.getRawResponse = function() {
return bulkResult;
}
/**
* Returns true if the bulk operation contains a write error
*
* @return {boolean}
*/
this.hasWriteErrors = function() {
return bulkResult.writeErrors.length > 0;
}
/**
* Returns the number of write errors off the bulk operation
*
* @return {number}
*/
this.getWriteErrorCount = function() {
return bulkResult.writeErrors.length;
}
/**
* Returns a specific write error object
*
* @param {number} index of the write error to return, returns null if there is no result for passed in index
* @return {WriteError}
*/
this.getWriteErrorAt = function(index) {
if(index < bulkResult.writeErrors.length) {
return bulkResult.writeErrors[index];
}
return null;
}
/**
* Retrieve all write errors
*
* @return {object[]}
*/
this.getWriteErrors = function() {
return bulkResult.writeErrors;
}
/**
* Retrieve lastOp if available
*
* @return {object}
*/
this.getLastOp = function() {
return bulkResult.lastOp;
}
/**
* Retrieve the write concern error if any
*
* @return {WriteConcernError}
*/
this.getWriteConcernError = function() {
if(bulkResult.writeConcernErrors.length == 0) {
return null;
} else if(bulkResult.writeConcernErrors.length == 1) {
// Return the error
return bulkResult.writeConcernErrors[0];
} else {
// Combine the errors
var errmsg = "";
for(var i = 0; i < bulkResult.writeConcernErrors.length; i++) {
var err = bulkResult.writeConcernErrors[i];
errmsg = errmsg + err.errmsg;
// TODO: Something better
if(i == 0) errmsg = errmsg + " and ";
}
return new WriteConcernError({ errmsg : errmsg, code : WRITE_CONCERN_ERROR });
}
}
this.toJSON = function() {
return bulkResult;
}
this.toString = function() {
return "BulkWriteResult(" + this.toJSON(bulkResult) + ")";
}
this.isOk = function() {
return bulkResult.ok == 1;
}
}
/**
* Create a new WriteConcernError instance (INTERNAL TYPE, do not instantiate directly)
*
* @class
* @property {number} code Write concern error code.
* @property {string} errmsg Write concern error message.
* @return {WriteConcernError} a WriteConcernError instance
*/
var WriteConcernError = function(err) {
if(!(this instanceof WriteConcernError)) return new WriteConcernError(err);
// Define properties
defineReadOnlyProperty(this, "code", err.code);
defineReadOnlyProperty(this, "errmsg", err.errmsg);
this.toJSON = function() {
return {code: err.code, errmsg: err.errmsg};
}
this.toString = function() {
return "WriteConcernError(" + err.errmsg + ")";
}
}
/**
* Create a new WriteError instance (INTERNAL TYPE, do not instantiate directly)
*
* @class
* @property {number} code Write concern error code.
* @property {number} index Write concern error original bulk operation index.
* @property {string} errmsg Write concern error message.
* @return {WriteConcernError} a WriteConcernError instance
*/
var WriteError = function(err) {
if(!(this instanceof WriteError)) return new WriteError(err);
// Define properties
defineReadOnlyProperty(this, "code", err.code);
defineReadOnlyProperty(this, "index", err.index);
defineReadOnlyProperty(this, "errmsg", err.errmsg);
//
// Define access methods
this.getOperation = function() {
return err.op;
}
this.toJSON = function() {
return {code: err.code, index: err.index, errmsg: err.errmsg, op: err.op};
}
this.toString = function() {
return "WriteError(" + JSON.stringify(this.toJSON()) + ")";
}
}
/**
* Merges results into shared data structure
* @ignore
*/
var mergeBatchResults = function(ordered, batch, bulkResult, err, result) {
// If we have an error set the result to be the err object
if(err) {
result = err;
} else if(result && result.result) {
result = result.result;
} else if(result == null) {
return;
}
// Do we have a top level error stop processing and return
if(result.ok == 0 && bulkResult.ok == 1) {
bulkResult.ok = 0;
var writeError = {
index: 0
, code: result.code || 0
, errmsg: result.message
, op: batch.operations[0]
};
bulkResult.writeErrors.push(new WriteError(writeError));
return;
} else if(result.ok == 0 && bulkResult.ok == 0) {
return;
}
// Deal with opTime if available
if(result.opTime || result.lastOp) {
var opTime = result.lastOp || result.opTime;
var lastOpTS = null;
var lastOpT = null;
// We have a time stamp
if(opTime && opTime._bsontype == 'Timestamp') {
if(bulkResult.lastOp == null) {
bulkResult.lastOp = opTime;
} else if(opTime.greaterThan(bulkResult.lastOp)) {
bulkResult.lastOp = opTime;
}
} else {
// Existing TS
if(bulkResult.lastOp) {
lastOpTS = typeof bulkResult.lastOp.ts == 'number'
? Long.fromNumber(bulkResult.lastOp.ts) : bulkResult.lastOp.ts;
lastOpT = typeof bulkResult.lastOp.t == 'number'
? Long.fromNumber(bulkResult.lastOp.t) : bulkResult.lastOp.t;
}
// Current OpTime TS
var opTimeTS = typeof opTime.ts == 'number'
? Long.fromNumber(opTime.ts) : opTime.ts;
var opTimeT = typeof opTime.t == 'number'
? Long.fromNumber(opTime.t) : opTime.t;
// Compare the opTime's
if(bulkResult.lastOp == null) {
bulkResult.lastOp = opTime;
} else if(opTimeTS.greaterThan(lastOpTS)) {
bulkResult.lastOp = opTime;
} else if(opTimeTS.equals(lastOpTS)) {
if(opTimeT.greaterThan(lastOpT)) {
bulkResult.lastOp = opTime;
}
}
}
}
// If we have an insert Batch type
if(batch.batchType == INSERT && result.n) {
bulkResult.nInserted = bulkResult.nInserted + result.n;
}
// If we have an insert Batch type
if(batch.batchType == REMOVE && result.n) {
bulkResult.nRemoved = bulkResult.nRemoved + result.n;
}
var nUpserted = 0;
// We have an array of upserted values, we need to rewrite the indexes
if(Array.isArray(result.upserted)) {
nUpserted = result.upserted.length;
for(var i = 0; i < result.upserted.length; i++) {
bulkResult.upserted.push({
index: result.upserted[i].index + batch.originalZeroIndex
, _id: result.upserted[i]._id
});
}
} else if(result.upserted) {
nUpserted = 1;
bulkResult.upserted.push({
index: batch.originalZeroIndex
, _id: result.upserted
});
}
// If we have an update Batch type
if(batch.batchType == UPDATE && result.n) {
var nModified = result.nModified;
bulkResult.nUpserted = bulkResult.nUpserted + nUpserted;
bulkResult.nMatched = bulkResult.nMatched + (result.n - nUpserted);
if(typeof nModified == 'number') {
bulkResult.nModified = bulkResult.nModified + nModified;
} else {
bulkResult.nModified = null;
}
}
if(Array.isArray(result.writeErrors)) {
for(i = 0; i < result.writeErrors.length; i++) {
writeError = {
index: batch.originalZeroIndex + result.writeErrors[i].index
, code: result.writeErrors[i].code
, errmsg: result.writeErrors[i].errmsg
, op: batch.operations[result.writeErrors[i].index]
};
bulkResult.writeErrors.push(new WriteError(writeError));
}
}
if(result.writeConcernError) {
bulkResult.writeConcernErrors.push(new WriteConcernError(result.writeConcernError));
}
}
//
// Clone the options
var cloneOptions = function(options) {
var clone = {};
var keys = Object.keys(options);
for(var i = 0; i < keys.length; i++) {
clone[keys[i]] = options[keys[i]];
}
return clone;
}
// Exports symbols
exports.BulkWriteResult = BulkWriteResult;
exports.WriteError = WriteError;
exports.Batch = Batch;
exports.LegacyOp = LegacyOp;
exports.mergeBatchResults = mergeBatchResults;
exports.cloneOptions = cloneOptions;
exports.writeConcern = writeConcern;
exports.INVALID_BSON_ERROR = INVALID_BSON_ERROR;
exports.WRITE_CONCERN_ERROR = WRITE_CONCERN_ERROR;
exports.MULTIPLE_ERROR = MULTIPLE_ERROR;
exports.UNKNOWN_ERROR = UNKNOWN_ERROR;
exports.INSERT = INSERT;
exports.UPDATE = UPDATE;
exports.REMOVE = REMOVE;
-539
View File
@@ -1,539 +0,0 @@
"use strict";
var common = require('./common')
, utils = require('../utils')
, toError = require('../utils').toError
, handleCallback = require('../utils').handleCallback
, shallowClone = utils.shallowClone
, BulkWriteResult = common.BulkWriteResult
, ObjectID = require('mongodb-core').BSON.ObjectID
, Define = require('../metadata')
, BSON = require('mongodb-core').BSON
, Batch = common.Batch
, mergeBatchResults = common.mergeBatchResults;
var 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]);
/**
* Create a FindOperatorsOrdered instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @return {FindOperatorsOrdered} a FindOperatorsOrdered instance.
*/
var FindOperatorsOrdered = function(self) {
this.s = self.s;
}
/**
* Add a single update document to the bulk operation
*
* @method
* @param {object} doc update operations
* @throws {MongoError}
* @return {OrderedBulkOperation}
*/
FindOperatorsOrdered.prototype.update = function(updateDocument) {
// Perform upsert
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
// Establish the update command
var document = {
q: this.s.currentOp.selector
, u: updateDocument
, multi: true
, upsert: upsert
}
// Clear out current Op
this.s.currentOp = null;
// Add the update document to the list
return addToOperationsList(this, common.UPDATE, document);
}
/**
* Add a single update one document to the bulk operation
*
* @method
* @param {object} doc update operations
* @throws {MongoError}
* @return {OrderedBulkOperation}
*/
FindOperatorsOrdered.prototype.updateOne = function(updateDocument) {
// Perform upsert
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
// Establish the update command
var document = {
q: this.s.currentOp.selector
, u: updateDocument
, multi: false
, upsert: upsert
}
// Clear out current Op
this.s.currentOp = null;
// Add the update document to the list
return addToOperationsList(this, common.UPDATE, document);
}
/**
* Add a replace one operation to the bulk operation
*
* @method
* @param {object} doc the new document to replace the existing one with
* @throws {MongoError}
* @return {OrderedBulkOperation}
*/
FindOperatorsOrdered.prototype.replaceOne = function(updateDocument) {
this.updateOne(updateDocument);
}
/**
* Upsert modifier for update bulk operation
*
* @method
* @throws {MongoError}
* @return {FindOperatorsOrdered}
*/
FindOperatorsOrdered.prototype.upsert = function() {
this.s.currentOp.upsert = true;
return this;
}
/**
* Add a remove one operation to the bulk operation
*
* @method
* @throws {MongoError}
* @return {OrderedBulkOperation}
*/
FindOperatorsOrdered.prototype.deleteOne = function() {
// Establish the update command
var document = {
q: this.s.currentOp.selector
, limit: 1
}
// Clear out current Op
this.s.currentOp = null;
// Add the remove document to the list
return addToOperationsList(this, common.REMOVE, document);
}
// Backward compatibility
FindOperatorsOrdered.prototype.removeOne = FindOperatorsOrdered.prototype.deleteOne;
/**
* Add a remove operation to the bulk operation
*
* @method
* @throws {MongoError}
* @return {OrderedBulkOperation}
*/
FindOperatorsOrdered.prototype.delete = function() {
// Establish the update command
var document = {
q: this.s.currentOp.selector
, limit: 0
}
// Clear out current Op
this.s.currentOp = null;
// Add the remove document to the list
return addToOperationsList(this, common.REMOVE, document);
}
// Backward compatibility
FindOperatorsOrdered.prototype.remove = FindOperatorsOrdered.prototype.delete;
// Add to internal list of documents
var addToOperationsList = function(_self, docType, document) {
// Get the bsonSize
var bsonSize = bson.calculateObjectSize(document, {
checkKeys: false,
});
// Throw error if the doc is bigger than the max BSON size
if(bsonSize >= _self.s.maxBatchSizeBytes) {
throw toError("document is larger than the maximum size " + _self.s.maxBatchSizeBytes);
}
// Create a new batch object if we don't have a current one
if(_self.s.currentBatch == null) _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
// Check if we need to create a new batch
if(((_self.s.currentBatchSize + 1) >= _self.s.maxWriteBatchSize)
|| ((_self.s.currentBatchSizeBytes + _self.s.currentBatchSizeBytes) >= _self.s.maxBatchSizeBytes)
|| (_self.s.currentBatch.batchType != docType)) {
// Save the batch to the execution stack
_self.s.batches.push(_self.s.currentBatch);
// Create a new batch
_self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
// Reset the current size trackers
_self.s.currentBatchSize = 0;
_self.s.currentBatchSizeBytes = 0;
} else {
// Update current batch size
_self.s.currentBatchSize = _self.s.currentBatchSize + 1;
_self.s.currentBatchSizeBytes = _self.s.currentBatchSizeBytes + bsonSize;
}
if(docType == common.INSERT) {
_self.s.bulkResult.insertedIds.push({index: _self.s.currentIndex, _id: document._id});
}
// We have an array of documents
if(Array.isArray(document)) {
throw toError("operation passed in cannot be an Array");
} else {
_self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
_self.s.currentBatch.operations.push(document)
_self.s.currentBatchSizeBytes = _self.s.currentBatchSizeBytes + bsonSize;
_self.s.currentIndex = _self.s.currentIndex + 1;
}
// Return self
return _self;
}
/**
* 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.
*/
function OrderedBulkOperation(topology, collection, options) {
options = options == null ? {} : options;
// TODO Bring from driver information in isMaster
var executed = false;
// Current item
var currentOp = null;
// Handle to the bson serializer, used to calculate running sizes
var bson = topology.bson;
// Namespace for the operation
var namespace = collection.collectionName;
// Set max byte size
var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
? topology.isMasterDoc.maxWriteBatchSize : 1000;
// Get the write concern
var writeConcern = common.writeConcern(shallowClone(options), collection, options);
// Get the promiseLibrary
var promiseLibrary = options.promiseLibrary;
// No promise library selected fall back
if(!promiseLibrary) {
promiseLibrary = typeof global.Promise == 'function' ?
global.Promise : require('es6-promise').Promise;
}
// Final results
var bulkResult = {
ok: 1
, writeErrors: []
, writeConcernErrors: []
, insertedIds: []
, nInserted: 0
, nUpserted: 0
, nMatched: 0
, nModified: 0
, nRemoved: 0
, upserted: []
};
// Internal state
this.s = {
// Final result
bulkResult: bulkResult
// Current batch state
, currentBatch: null
, currentIndex: 0
, currentBatchSize: 0
, currentBatchSizeBytes: 0
, batches: []
// Write concern
, writeConcern: writeConcern
// Max batch size options
, maxBatchSizeBytes: maxBatchSizeBytes
, maxWriteBatchSize: maxWriteBatchSize
// Namespace
, namespace: namespace
// BSON
, bson: bson
// Topology
, topology: topology
// Options
, options: options
// Current operation
, currentOp: currentOp
// Executed
, executed: executed
// Collection
, collection: collection
// Promise Library
, promiseLibrary: promiseLibrary
// Fundamental error
, err: null
// Bypass validation
, bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
}
}
var define = OrderedBulkOperation.define = new Define('OrderedBulkOperation', OrderedBulkOperation, false);
OrderedBulkOperation.prototype.raw = function(op) {
var key = Object.keys(op)[0];
// Set up the force server object id
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
// Update operations
if((op.updateOne && op.updateOne.q)
|| (op.updateMany && op.updateMany.q)
|| (op.replaceOne && op.replaceOne.q)) {
op[key].multi = op.updateOne || op.replaceOne ? false : true;
return addToOperationsList(this, common.UPDATE, op[key]);
}
// Crud spec update format
if(op.updateOne || op.updateMany || op.replaceOne) {
var multi = op.updateOne || op.replaceOne ? false : true;
var operation = {q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi}
operation.upsert = op[key].upsert ? true: false;
if(op.collation) operation.collation = op.collation;
return addToOperationsList(this, common.UPDATE, operation);
}
// Remove operations
if(op.removeOne || op.removeMany || (op.deleteOne && op.deleteOne.q) || op.deleteMany && op.deleteMany.q) {
op[key].limit = op.removeOne ? 1 : 0;
return addToOperationsList(this, common.REMOVE, op[key]);
}
// Crud spec delete operations, less efficient
if(op.deleteOne || op.deleteMany) {
var limit = op.deleteOne ? 1 : 0;
operation = {q: op[key].filter, limit: limit}
if(op.collation) operation.collation = op.collation;
return addToOperationsList(this, common.REMOVE, operation);
}
// Insert operations
if(op.insertOne && op.insertOne.document == null) {
if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
return addToOperationsList(this, common.INSERT, op.insertOne);
} else if(op.insertOne && op.insertOne.document) {
if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
return addToOperationsList(this, common.INSERT, op.insertOne.document);
}
if(op.insertMany) {
for(var i = 0; i < op.insertMany.length; i++) {
if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
addToOperationsList(this, common.INSERT, op.insertMany[i]);
}
return;
}
// No valid type of operation
throw toError("bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany");
}
/**
* Add a single insert document to the bulk operation
*
* @param {object} doc the document to insert
* @throws {MongoError}
* @return {OrderedBulkOperation}
*/
OrderedBulkOperation.prototype.insert = function(document) {
if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
return addToOperationsList(this, common.INSERT, document);
}
/**
* Initiate a find operation for an update/updateOne/remove/removeOne/replaceOne
*
* @method
* @param {object} selector The selector for the bulk operation.
* @throws {MongoError}
* @return {FindOperatorsOrdered}
*/
OrderedBulkOperation.prototype.find = function(selector) {
if (!selector) {
throw toError("Bulk find operation must specify a selector");
}
// Save a current selector
this.s.currentOp = {
selector: selector
}
return new FindOperatorsOrdered(this);
}
Object.defineProperty(OrderedBulkOperation.prototype, 'length', {
enumerable: true,
get: function() {
return this.s.currentIndex;
}
});
//
// Execute next write command in a chain
var executeCommands = function(self, callback) {
if(self.s.batches.length == 0) {
return handleCallback(callback, null, new BulkWriteResult(self.s.bulkResult));
}
// Ordered execution of the command
var batch = self.s.batches.shift();
var resultHandler = function(err, result) {
// Error is a driver related error not a bulk op error, terminate
if(err && err.driver || err && err.message) {
return handleCallback(callback, err);
}
// If we have and error
if(err) err.ok = 0;
// Merge the results together
var mergeResult = mergeBatchResults(true, batch, self.s.bulkResult, err, result);
if(mergeResult != null) {
return handleCallback(callback, null, new BulkWriteResult(self.s.bulkResult));
}
// If we are ordered and have errors and they are
// not all replication errors terminate the operation
if(self.s.bulkResult.writeErrors.length > 0) {
return handleCallback(callback, toError(self.s.bulkResult.writeErrors[0]), new BulkWriteResult(self.s.bulkResult));
}
// Execute the next command in line
executeCommands(self, callback);
}
var finalOptions = {ordered: true}
if(self.s.writeConcern != null) {
finalOptions.writeConcern = self.s.writeConcern;
}
// Set an operationIf if provided
if(self.operationId) {
resultHandler.operationId = self.operationId;
}
// Serialize functions
if(self.s.options.serializeFunctions) {
finalOptions.serializeFunctions = true
}
// Serialize functions
if(self.s.options.ignoreUndefined) {
finalOptions.ignoreUndefined = true
}
// Is the bypassDocumentValidation options specific
if(self.s.bypassDocumentValidation == true) {
finalOptions.bypassDocumentValidation = true;
}
try {
if(batch.batchType == common.INSERT) {
self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
} else if(batch.batchType == common.UPDATE) {
self.s.topology.update(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
} else if(batch.batchType == common.REMOVE) {
self.s.topology.remove(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
}
} catch(err) {
// Force top level error
err.ok = 0;
// Merge top level error and return
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
}
}
/**
* 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=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] 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
*/
OrderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
var self = this;
if (this.s.executed) {
var executedError = toError('batch cannot be re-executed');
return (typeof callback === 'function') ?
callback(executedError, null) : this.s.promiseLibrary.reject(executedError);
}
if (typeof _writeConcern === 'function') {
callback = _writeConcern;
} else if (_writeConcern && typeof _writeConcern === 'object') {
this.s.writeConcern = _writeConcern;
}
// If we have current batch
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch)
// If we have no operations in the bulk raise an error
if (this.s.batches.length == 0) {
var emptyBatchError = toError('Invalid Operation, no operations specified');
return (typeof callback === 'function') ?
callback(emptyBatchError, null) : this.s.promiseLibrary.reject(emptyBatchError);
}
// Execute using callback
if (typeof callback === 'function') {
return executeCommands(this, callback);
}
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
executeCommands(self, function(err, r) {
if (err) return reject(err);
resolve(r);
});
});
}
define.classMethod('execute', {callback: true, promise:false});
/**
* Returns an unordered batch object
* @ignore
*/
var initializeOrderedBulkOp = function(topology, collection, options) {
return new OrderedBulkOperation(topology, collection, options);
}
initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
module.exports = initializeOrderedBulkOp;
module.exports.Bulk = OrderedBulkOperation;
-541
View File
@@ -1,541 +0,0 @@
"use strict";
var common = require('./common')
, utils = require('../utils')
, toError = require('../utils').toError
, handleCallback = require('../utils').handleCallback
, shallowClone = utils.shallowClone
, BulkWriteResult = common.BulkWriteResult
, ObjectID = require('mongodb-core').BSON.ObjectID
, BSON = require('mongodb-core').BSON
, Define = require('../metadata')
, Batch = common.Batch
, mergeBatchResults = common.mergeBatchResults;
var 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]);
/**
* Create a FindOperatorsUnordered instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @property {number} length Get the number of operations in the bulk.
* @return {FindOperatorsUnordered} a FindOperatorsUnordered instance.
*/
var FindOperatorsUnordered = function(self) {
this.s = self.s;
}
/**
* Add a single update document to the bulk operation
*
* @method
* @param {object} doc update operations
* @throws {MongoError}
* @return {UnorderedBulkOperation}
*/
FindOperatorsUnordered.prototype.update = function(updateDocument) {
// Perform upsert
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
// Establish the update command
var document = {
q: this.s.currentOp.selector
, u: updateDocument
, multi: true
, upsert: upsert
}
// Clear out current Op
this.s.currentOp = null;
// Add the update document to the list
return addToOperationsList(this, common.UPDATE, document);
}
/**
* Add a single update one document to the bulk operation
*
* @method
* @param {object} doc update operations
* @throws {MongoError}
* @return {UnorderedBulkOperation}
*/
FindOperatorsUnordered.prototype.updateOne = function(updateDocument) {
// Perform upsert
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
// Establish the update command
var document = {
q: this.s.currentOp.selector
, u: updateDocument
, multi: false
, upsert: upsert
}
// Clear out current Op
this.s.currentOp = null;
// Add the update document to the list
return addToOperationsList(this, common.UPDATE, document);
}
/**
* Add a replace one operation to the bulk operation
*
* @method
* @param {object} doc the new document to replace the existing one with
* @throws {MongoError}
* @return {UnorderedBulkOperation}
*/
FindOperatorsUnordered.prototype.replaceOne = function(updateDocument) {
this.updateOne(updateDocument);
}
/**
* Upsert modifier for update bulk operation
*
* @method
* @throws {MongoError}
* @return {FindOperatorsUnordered}
*/
FindOperatorsUnordered.prototype.upsert = function() {
this.s.currentOp.upsert = true;
return this;
}
/**
* Add a remove one operation to the bulk operation
*
* @method
* @throws {MongoError}
* @return {UnorderedBulkOperation}
*/
FindOperatorsUnordered.prototype.removeOne = function() {
// Establish the update command
var document = {
q: this.s.currentOp.selector
, limit: 1
}
// Clear out current Op
this.s.currentOp = null;
// Add the remove document to the list
return addToOperationsList(this, common.REMOVE, document);
}
/**
* Add a remove operation to the bulk operation
*
* @method
* @throws {MongoError}
* @return {UnorderedBulkOperation}
*/
FindOperatorsUnordered.prototype.remove = function() {
// Establish the update command
var document = {
q: this.s.currentOp.selector
, limit: 0
}
// Clear out current Op
this.s.currentOp = null;
// Add the remove document to the list
return addToOperationsList(this, common.REMOVE, document);
}
//
// Add to the operations list
//
var addToOperationsList = function(_self, docType, document) {
// Get the bsonSize
var bsonSize = bson.calculateObjectSize(document, {
checkKeys: false,
});
// Throw error if the doc is bigger than the max BSON size
if(bsonSize >= _self.s.maxBatchSizeBytes) throw toError("document is larger than the maximum size " + _self.s.maxBatchSizeBytes);
// Holds the current batch
_self.s.currentBatch = null;
// Get the right type of batch
if(docType == common.INSERT) {
_self.s.currentBatch = _self.s.currentInsertBatch;
} else if(docType == common.UPDATE) {
_self.s.currentBatch = _self.s.currentUpdateBatch;
} else if(docType == common.REMOVE) {
_self.s.currentBatch = _self.s.currentRemoveBatch;
}
// Create a new batch object if we don't have a current one
if(_self.s.currentBatch == null) _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
// Check if we need to create a new batch
if(((_self.s.currentBatch.size + 1) >= _self.s.maxWriteBatchSize)
|| ((_self.s.currentBatch.sizeBytes + bsonSize) >= _self.s.maxBatchSizeBytes)
|| (_self.s.currentBatch.batchType != docType)) {
// Save the batch to the execution stack
_self.s.batches.push(_self.s.currentBatch);
// Create a new batch
_self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
}
// We have an array of documents
if(Array.isArray(document)) {
throw toError("operation passed in cannot be an Array");
} else {
_self.s.currentBatch.operations.push(document);
_self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
_self.s.currentIndex = _self.s.currentIndex + 1;
}
// Save back the current Batch to the right type
if(docType == common.INSERT) {
_self.s.currentInsertBatch = _self.s.currentBatch;
_self.s.bulkResult.insertedIds.push({index: _self.s.bulkResult.insertedIds.length, _id: document._id});
} else if(docType == common.UPDATE) {
_self.s.currentUpdateBatch = _self.s.currentBatch;
} else if(docType == common.REMOVE) {
_self.s.currentRemoveBatch = _self.s.currentBatch;
}
// Update current batch size
_self.s.currentBatch.size = _self.s.currentBatch.size + 1;
_self.s.currentBatch.sizeBytes = _self.s.currentBatch.sizeBytes + bsonSize;
// Return self
return _self;
}
/**
* 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.
*/
var UnorderedBulkOperation = function(topology, collection, options) {
options = options == null ? {} : options;
// Get the namespace for the write operations
var namespace = collection.collectionName;
// Used to mark operation as executed
var executed = false;
// Current item
// var currentBatch = null;
var currentOp = null;
// Handle to the bson serializer, used to calculate running sizes
var bson = topology.bson;
// Set max byte size
var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
? topology.isMasterDoc.maxWriteBatchSize : 1000;
// Get the write concern
var writeConcern = common.writeConcern(shallowClone(options), collection, options);
// Get the promiseLibrary
var promiseLibrary = options.promiseLibrary;
// No promise library selected fall back
if(!promiseLibrary) {
promiseLibrary = typeof global.Promise == 'function' ?
global.Promise : require('es6-promise').Promise;
}
// Final results
var bulkResult = {
ok: 1
, writeErrors: []
, writeConcernErrors: []
, insertedIds: []
, nInserted: 0
, nUpserted: 0
, nMatched: 0
, nModified: 0
, nRemoved: 0
, upserted: []
};
// Internal state
this.s = {
// Final result
bulkResult: bulkResult
// Current batch state
, currentInsertBatch: null
, currentUpdateBatch: null
, currentRemoveBatch: null
, currentBatch: null
, currentIndex: 0
, batches: []
// Write concern
, writeConcern: writeConcern
// Max batch size options
, maxBatchSizeBytes: maxBatchSizeBytes
, maxWriteBatchSize: maxWriteBatchSize
// Namespace
, namespace: namespace
// BSON
, bson: bson
// Topology
, topology: topology
// Options
, options: options
// Current operation
, currentOp: currentOp
// Executed
, executed: executed
// Collection
, collection: collection
// Promise Library
, promiseLibrary: promiseLibrary
// Bypass validation
, bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
}
}
var define = UnorderedBulkOperation.define = new Define('UnorderedBulkOperation', UnorderedBulkOperation, false);
/**
* Add a single insert document to the bulk operation
*
* @param {object} doc the document to insert
* @throws {MongoError}
* @return {UnorderedBulkOperation}
*/
UnorderedBulkOperation.prototype.insert = function(document) {
if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
return addToOperationsList(this, common.INSERT, document);
}
/**
* Initiate a find operation for an update/updateOne/remove/removeOne/replaceOne
*
* @method
* @param {object} selector The selector for the bulk operation.
* @throws {MongoError}
* @return {FindOperatorsUnordered}
*/
UnorderedBulkOperation.prototype.find = function(selector) {
if (!selector) {
throw toError("Bulk find operation must specify a selector");
}
// Save a current selector
this.s.currentOp = {
selector: selector
}
return new FindOperatorsUnordered(this);
}
Object.defineProperty(UnorderedBulkOperation.prototype, 'length', {
enumerable: true,
get: function() {
return this.s.currentIndex;
}
});
UnorderedBulkOperation.prototype.raw = function(op) {
var key = Object.keys(op)[0];
// Set up the force server object id
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
// Update operations
if((op.updateOne && op.updateOne.q)
|| (op.updateMany && op.updateMany.q)
|| (op.replaceOne && op.replaceOne.q)) {
op[key].multi = op.updateOne || op.replaceOne ? false : true;
return addToOperationsList(this, common.UPDATE, op[key]);
}
// Crud spec update format
if(op.updateOne || op.updateMany || op.replaceOne) {
var multi = op.updateOne || op.replaceOne ? false : true;
var operation = {q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi}
if(op[key].upsert) operation.upsert = true;
return addToOperationsList(this, common.UPDATE, operation);
}
// Remove operations
if(op.removeOne || op.removeMany || (op.deleteOne && op.deleteOne.q) || op.deleteMany && op.deleteMany.q) {
op[key].limit = op.removeOne ? 1 : 0;
return addToOperationsList(this, common.REMOVE, op[key]);
}
// Crud spec delete operations, less efficient
if(op.deleteOne || op.deleteMany) {
var limit = op.deleteOne ? 1 : 0;
operation = {q: op[key].filter, limit: limit}
return addToOperationsList(this, common.REMOVE, operation);
}
// Insert operations
if(op.insertOne && op.insertOne.document == null) {
if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
return addToOperationsList(this, common.INSERT, op.insertOne);
} else if(op.insertOne && op.insertOne.document) {
if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
return addToOperationsList(this, common.INSERT, op.insertOne.document);
}
if(op.insertMany) {
for(var i = 0; i < op.insertMany.length; i++) {
if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
addToOperationsList(this, common.INSERT, op.insertMany[i]);
}
return;
}
// No valid type of operation
throw toError("bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany");
}
//
// Execute the command
var executeBatch = function(self, batch, callback) {
var finalOptions = {ordered: false}
if(self.s.writeConcern != null) {
finalOptions.writeConcern = self.s.writeConcern;
}
var resultHandler = function(err, result) {
// Error is a driver related error not a bulk op error, terminate
if(err && err.driver || err && err.message) {
return handleCallback(callback, err);
}
// If we have and error
if(err) err.ok = 0;
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, result));
}
// Set an operationIf if provided
if(self.operationId) {
resultHandler.operationId = self.operationId;
}
// Serialize functions
if(self.s.options.serializeFunctions) {
finalOptions.serializeFunctions = true
}
// Is the bypassDocumentValidation options specific
if(self.s.bypassDocumentValidation == true) {
finalOptions.bypassDocumentValidation = true;
}
try {
if(batch.batchType == common.INSERT) {
self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
} else if(batch.batchType == common.UPDATE) {
self.s.topology.update(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
} else if(batch.batchType == common.REMOVE) {
self.s.topology.remove(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
}
} catch(err) {
// Force top level error
err.ok = 0;
// Merge top level error and return
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
}
}
//
// Execute all the commands
var executeBatches = function(self, callback) {
var numberOfCommandsToExecute = self.s.batches.length;
// Execute over all the batches
for(var i = 0; i < self.s.batches.length; i++) {
executeBatch(self, self.s.batches[i], function(err) {
// Driver layer error capture it
if(err) error = err;
// Count down the number of commands left to execute
numberOfCommandsToExecute = numberOfCommandsToExecute - 1;
// Execute
if(numberOfCommandsToExecute == 0) {
// Driver level error
if(error) return handleCallback(callback, error);
// Treat write errors
var error = self.s.bulkResult.writeErrors.length > 0 ? toError(self.s.bulkResult.writeErrors[0]) : null;
handleCallback(callback, error, new BulkWriteResult(self.s.bulkResult));
}
});
}
}
/**
* 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=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] 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
*/
UnorderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
var self = this;
if (this.s.executed) {
var executedError = toError('batch cannot be re-executed');
return (typeof callback === 'function') ?
callback(executedError, null) : this.s.promiseLibrary.reject(executedError);
}
if (typeof _writeConcern === 'function') {
callback = _writeConcern;
} else if (_writeConcern && typeof _writeConcern === 'object') {
this.s.writeConcern = _writeConcern;
}
// If we have current batch
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
// If we have no operations in the bulk raise an error
if (this.s.batches.length == 0) {
var emptyBatchError = toError('Invalid Operation, no operations specified');
return (typeof callback === 'function') ?
callback(emptyBatchError, null) : this.s.promiseLibrary.reject(emptyBatchError);
}
// Execute using callback
if (typeof callback === 'function') return executeBatches(this, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
executeBatches(self, function(err, r) {
if (err) return reject(err);
resolve(r);
});
});
}
define.classMethod('execute', {callback: true, promise:false});
/**
* Returns an unordered batch object
* @ignore
*/
var initializeUnorderedBulkOp = function(topology, collection, options) {
return new UnorderedBulkOperation(topology, collection, options);
}
initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
module.exports = initializeUnorderedBulkOp;
module.exports.Bulk = UnorderedBulkOperation;
File diff suppressed because it is too large Load Diff
-320
View File
@@ -1,320 +0,0 @@
"use strict";
var inherits = require('util').inherits
, ReadPreference = require('./read_preference')
, MongoError = require('mongodb-core').MongoError
, Readable = require('stream').Readable || require('readable-stream').Readable
, Define = require('./metadata')
, CoreCursor = require('./cursor')
, CoreReadPreference = require('mongodb-core').ReadPreference;
/**
* @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
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* // Create a collection we want to drop later
* var col = db.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);
* db.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;
// No promise library selected fall back
if(!promiseLibrary) {
promiseLibrary = typeof global.Promise == 'function' ?
global.Promise : require('es6-promise').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
}
}
/**
* 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'];
// Only inherit the types we need
for(var i = 0; i < methodsToInherit.length; i++) {
CommandCursor.prototype[methodsToInherit[i]] = CoreCursor.prototype[methodsToInherit[i]];
}
var define = CommandCursor.define = new Define('CommandCursor', CommandCursor, true);
/**
* 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(r) {
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(r instanceof ReadPreference) {
this.s.options.readPreference = new CoreReadPreference(r.mode, r.tags, {maxStalenessSeconds: r.maxStalenessSeconds});
} else if(typeof r == 'string') {
this.s.options.readPreference = new CoreReadPreference(r);
} else if(r instanceof CoreReadPreference) {
this.s.options.readPreference = r;
}
return this;
}
define.classMethod('setReadPreference', {callback: false, promise:false, returns: [CommandCursor]});
/**
* 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;
}
define.classMethod('batchSize', {callback: false, promise:false, returns: [CommandCursor]});
/**
* 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;
}
define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [CommandCursor]});
CommandCursor.prototype.get = CommandCursor.prototype.toArray;
define.classMethod('get', {callback: true, promise:false});
// Inherited methods
define.classMethod('toArray', {callback: true, promise:true});
define.classMethod('each', {callback: true, promise:false});
define.classMethod('forEach', {callback: true, promise:false});
define.classMethod('next', {callback: true, promise:true});
define.classMethod('hasNext', {callback: true, promise:true});
define.classMethod('close', {callback: true, promise:true});
define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
define.classMethod('rewind', {callback: false, promise:false});
define.classMethod('bufferedCount', {callback: false, promise:false, returns: [Number]});
define.classMethod('readBufferedDocuments', {callback: false, promise:false, returns: [Array]});
/**
* 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 previously 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 previously 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;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1,409 +0,0 @@
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=null] Optional settings.
* @param {Number} [options.sort=null] Optional sort for the file find query
* @param {Number} [options.skip=null] Optional skip for the file find query
* @param {Number} [options.start=null] Optional 0-based offset in bytes to start streaming from
* @param {Number} [options.end=null] 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);
}
@@ -1,367 +0,0 @@
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 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=null] 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=null] Optional write concern to be passed to write operations, for instance `{ w: 1 }`
* @param {object} [options.readPreference=null] 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 ||
(typeof global.Promise == 'function' ? global.Promise : require('es6-promise').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=null] Optional settings.
* @param {number} [options.chunkSizeBytes=null] Optional overwrite this bucket's chunkSizeBytes for this file
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
* @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=null] Optional settings.
* @param {number} [options.chunkSizeBytes=null] Optional overwrite this bucket's chunkSizeBytes for this file
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
* @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=null] Optional settings.
* @param {Number} [options.start=null] Optional 0-based offset in bytes to start streaming from
* @param {Number} [options.end=null] 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) {
if (typeof callback === 'function') {
return _delete(this, id, callback);
}
var _this = this;
return new this.s.promiseLibrary(function(resolve, reject) {
_delete(_this, id, function(error, res) {
if (error) {
reject(error);
} else {
resolve(res);
}
});
});
};
/**
* @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=null] Optional settings for cursor
* @param {number} [options.batchSize=null] Optional batch size for cursor
* @param {number} [options.limit=null] Optional limit for cursor
* @param {number} [options.maxTimeMS=null] Optional maxTimeMS for cursor
* @param {boolean} [options.noCursorTimeout=null] Optionally set cursor's `noCursorTimeout` flag
* @param {number} [options.skip=null] Optional skip for cursor
* @param {object} [options.sort=null] 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=null] 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=null] Optional 0-based offset in bytes to start streaming from
* @param {Number} [options.end=null] 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) {
if (typeof callback === 'function') {
return _rename(this, id, filename, callback);
}
var _this = this;
return new this.s.promiseLibrary(function(resolve, reject) {
_rename(_this, id, filename, function(error, res) {
if (error) {
reject(error);
} else {
resolve(res);
}
});
});
};
/**
* @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) {
if (typeof callback === 'function') {
return _drop(this, callback);
}
var _this = this;
return new this.s.promiseLibrary(function(resolve, reject) {
_drop(_this, function(error, res) {
if (error) {
reject(error);
} else {
resolve(res);
}
});
});
};
/**
* @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
*/
@@ -1,527 +0,0 @@
var core = require('mongodb-core');
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
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=null] Optional settings.
* @param {string|number|object} [options.id=null] Custom file id for the GridFS file.
* @param {number} [options.chunkSizeBytes=null] The chunk size to use, in bytes
* @param {number} [options.w=null] The write concern
* @param {number} [options.wtimeout=null] The write concern timeout
* @param {number} [options.j=null] The journal write concern
* @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 = new Buffer(this.chunkSizeBytes);
this.length = 0;
this.md5 = 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.digest('hex'), _this.filename, _this.options.contentType,
_this.options.aliases, _this.options.metadata);
if (checkAborted(_this, callback)) {
return false;
}
_this.files.insert(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(),
md5: md5,
filename: filename
};
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 : new Buffer(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) {
_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.insert(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 = new Buffer(_this.pos);
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
_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.insert(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;
}
-230
View File
@@ -1,230 +0,0 @@
"use strict";
var Binary = require('mongodb-core').BSON.Binary,
ObjectID = require('mongodb-core').BSON.ObjectID;
/**
* 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 = new Buffer(mongoObjectFinal.data.length);
buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
this.data = new Binary(buffer);
} else if(Array.isArray(mongoObjectFinal.data)) {
buffer = new Buffer(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 = new Buffer(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;

Some files were not shown because too many files have changed in this diff Show More