mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-13 03:02:49 +00:00
updated bunch of file paths and changed the way posts are loaded
This commit is contained in:
131
node_modules/bson/HISTORY
generated
vendored
Normal file
131
node_modules/bson/HISTORY
generated
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
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
node_modules/bson/LICENSE
generated
vendored
Normal file
201
node_modules/bson/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
70
node_modules/bson/README.md
generated
vendored
Normal file
70
node_modules/bson/README.md
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
Javascript + C++ BSON parser
|
||||
============================
|
||||
|
||||
This BSON parser is primarily meant to be used with the `mongodb` node.js driver.
|
||||
However, wonderful tools such as `onejs` can package up a BSON parser that will work in the browser.
|
||||
The current build is located in the `browser_build/bson.js` file.
|
||||
|
||||
A simple example of how to use BSON in the browser:
|
||||
|
||||
```html
|
||||
<html>
|
||||
<head>
|
||||
<script src="https://raw.github.com/mongodb/js-bson/master/browser_build/bson.js">
|
||||
</script>
|
||||
</head>
|
||||
<body onload="start();">
|
||||
<script>
|
||||
function start() {
|
||||
var BSON = bson().BSON;
|
||||
var Long = bson().Long;
|
||||
|
||||
var doc = {long: Long.fromNumber(100)}
|
||||
|
||||
// Serialize a document
|
||||
var data = BSON.serialize(doc, false, true, false);
|
||||
// De serialize it again
|
||||
var doc_2 = BSON.deserialize(data);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
A simple example of how to use BSON in `node.js`:
|
||||
|
||||
```javascript
|
||||
var bson = require("bson");
|
||||
var BSON = new bson.BSONPure.BSON();
|
||||
var Long = bson.BSONPure.Long;
|
||||
|
||||
var doc = {long: Long.fromNumber(100)}
|
||||
|
||||
// Serialize a document
|
||||
var data = BSON.serialize(doc, false, true, false);
|
||||
console.log("data:", data);
|
||||
|
||||
// Deserialize the resulting Buffer
|
||||
var doc_2 = BSON.deserialize(data);
|
||||
console.log("doc_2:", doc_2);
|
||||
```
|
||||
|
||||
The API consists of two simple methods to serialize/deserialize objects to/from BSON format:
|
||||
|
||||
* BSON.serialize(object, checkKeys, asBuffer, serializeFunctions)
|
||||
* @param {Object} object the Javascript object to serialize.
|
||||
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
||||
* @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
|
||||
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**
|
||||
* @return {TypedArray/Array} returns a TypedArray or Array depending on what your browser supports
|
||||
|
||||
* BSON.deserialize(buffer, options, isArray)
|
||||
* Options
|
||||
* **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
||||
* **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
||||
* **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
||||
* **promoteBuffers** {Boolean, default:false}, deserialize Binary data directly into node.js Buffer object.
|
||||
* @param {TypedArray/Array} a TypedArray/Array containing the BSON data
|
||||
* @param {Object} [options] additional options used for the deserialization.
|
||||
* @param {Boolean} [isArray] ignore used for recursive parsing.
|
||||
* @return {Object} returns the deserialized Javascript Object.
|
||||
1574
node_modules/bson/alternate_parsers/bson.js
generated
vendored
Normal file
1574
node_modules/bson/alternate_parsers/bson.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
429
node_modules/bson/alternate_parsers/faster_bson.js
generated
vendored
Normal file
429
node_modules/bson/alternate_parsers/faster_bson.js
generated
vendored
Normal file
@@ -0,0 +1,429 @@
|
||||
/// reduced to ~ 410 LOCs (parser only 300 vs. 1400+) with (some, needed) BSON classes "inlined".
|
||||
/// Compare ~ 4,300 (22KB vs. 157KB) in browser build at: https://github.com/mongodb/js-bson/blob/master/browser_build/bson.js
|
||||
|
||||
module.exports.calculateObjectSize = calculateObjectSize;
|
||||
|
||||
function calculateObjectSize(object) {
|
||||
var totalLength = (4 + 1); /// handles the obj.length prefix + terminating '0' ?!
|
||||
for(var key in object) { /// looks like it handles arrays under the same for...in loop!?
|
||||
totalLength += calculateElement(key, object[key])
|
||||
}
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
function calculateElement(name, value) {
|
||||
var len = 1; /// always starting with 1 for the data type byte!
|
||||
if (name) len += Buffer.byteLength(name, 'utf8') + 1; /// cstring: name + '0' termination
|
||||
|
||||
if (value === undefined || value === null) return len; /// just the type byte plus name cstring
|
||||
switch( value.constructor ) { /// removed all checks 'isBuffer' if Node.js Buffer class is present!?
|
||||
|
||||
case ObjectID: /// we want these sorted from most common case to least common/deprecated;
|
||||
return len + 12;
|
||||
case String:
|
||||
return len + 4 + Buffer.byteLength(value, 'utf8') +1; ///
|
||||
case Number:
|
||||
if (Math.floor(value) === value) { /// case: integer; pos.# more common, '&&' stops if 1st fails!
|
||||
if ( value <= 2147483647 && value >= -2147483647 ) // 32 bit
|
||||
return len + 4;
|
||||
else return len + 8; /// covers Long-ish JS integers as Longs!
|
||||
} else return len + 8; /// 8+1 --- covers Double & std. float
|
||||
case Boolean:
|
||||
return len + 1;
|
||||
|
||||
case Array:
|
||||
case Object:
|
||||
return len + calculateObjectSize(value);
|
||||
|
||||
case Buffer: /// replaces the entire Binary class!
|
||||
return len + 4 + value.length + 1;
|
||||
|
||||
case Regex: /// these are handled as strings by serializeFast() later, hence 'gim' opts = 3 + 1 chars
|
||||
return len + Buffer.byteLength(value.source, 'utf8') + 1
|
||||
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) +1;
|
||||
case Date:
|
||||
case Long:
|
||||
case Timestamp:
|
||||
case Double:
|
||||
return len + 8;
|
||||
|
||||
case MinKey:
|
||||
case MaxKey:
|
||||
return len; /// these two return the type byte and name cstring only!
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
module.exports.serializeFast = serializeFast;
|
||||
module.exports.serialize = function(object, checkKeys, asBuffer, serializeFunctions, index) {
|
||||
var buffer = new Buffer(calculateObjectSize(object));
|
||||
return serializeFast(object, checkKeys, buffer, 0);
|
||||
}
|
||||
|
||||
function serializeFast(object, checkKeys, buffer, i) { /// set checkKeys = false in query(..., options object to save performance IFF you're certain your keys are safe/system-set!
|
||||
var size = buffer.length;
|
||||
buffer[i++] = size & 0xff; buffer[i++] = (size >> 8) & 0xff; /// these get overwritten later!
|
||||
buffer[i++] = (size >> 16) & 0xff; buffer[i++] = (size >> 24) & 0xff;
|
||||
|
||||
if (object.constructor === Array) { /// any need to checkKeys here?!? since we're doing for rather than for...in, should be safe from extra (non-numeric) keys added to the array?!
|
||||
for(var j = 0; j < object.length; j++) {
|
||||
i = packElement(j.toString(), object[j], checkKeys, buffer, i);
|
||||
}
|
||||
} else { /// checkKeys is needed if any suspicion of end-user key tampering/"injection" (a la SQL)
|
||||
for(var key in object) { /// mostly there should never be direct access to them!?
|
||||
if (checkKeys && (key.indexOf('\x00') >= 0 || key === '$where') ) { /// = "no script"?!; could add back key.indexOf('$') or maybe check for 'eval'?!
|
||||
/// took out: || key.indexOf('.') >= 0... Don't we allow dot notation queries?!
|
||||
console.log('checkKeys error: ');
|
||||
return new Error('Illegal object key!');
|
||||
}
|
||||
i = packElement(key, object[key], checkKeys, buffer, i); /// checkKeys pass needed for recursion!
|
||||
}
|
||||
}
|
||||
buffer[i++] = 0; /// write terminating zero; !we do NOT -1 the index increase here as original does!
|
||||
return i;
|
||||
}
|
||||
|
||||
function packElement(name, value, checkKeys, buffer, i) { /// serializeFunctions removed! checkKeys needed for Array & Object cases pass through (calling serializeFast recursively!)
|
||||
if (value === undefined || value === null){
|
||||
buffer[i++] = 10; /// = BSON.BSON_DATA_NULL;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0; /// buffer.write(...) returns bytesWritten!
|
||||
return i;
|
||||
}
|
||||
switch(value.constructor) {
|
||||
|
||||
case ObjectID:
|
||||
buffer[i++] = 7; /// = BSON.BSON_DATA_OID;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
/// i += buffer.write(value.id, i, 'binary'); /// OLD: writes a String to a Buffer; 'binary' deprecated!!
|
||||
value.id.copy(buffer, i); /// NEW ObjectID version has this.id = Buffer at the ready!
|
||||
return i += 12;
|
||||
|
||||
case String:
|
||||
buffer[i++] = 2; /// = BSON.BSON_DATA_STRING;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
|
||||
var size = Buffer.byteLength(value) + 1; /// includes the terminating '0'!?
|
||||
buffer[i++] = size & 0xff; buffer[i++] = (size >> 8) & 0xff;
|
||||
buffer[i++] = (size >> 16) & 0xff; buffer[i++] = (size >> 24) & 0xff;
|
||||
|
||||
i += buffer.write(value, i, 'utf8'); buffer[i++] = 0;
|
||||
return i;
|
||||
|
||||
case Number:
|
||||
if ( ~~(value) === value) { /// double-Tilde is equiv. to Math.floor(value)
|
||||
if ( value <= 2147483647 && value >= -2147483647){ /// = BSON.BSON_INT32_MAX / MIN asf.
|
||||
buffer[i++] = 16; /// = BSON.BSON_DATA_INT;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
buffer[i++] = value & 0xff; buffer[i++] = (value >> 8) & 0xff;
|
||||
buffer[i++] = (value >> 16) & 0xff; buffer[i++] = (value >> 24) & 0xff;
|
||||
|
||||
// Else large-ish JS int!? to Long!?
|
||||
} else { /// if (value <= BSON.JS_INT_MAX && value >= BSON.JS_INT_MIN){ /// 9007199254740992 asf.
|
||||
buffer[i++] = 18; /// = BSON.BSON_DATA_LONG;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
var lowBits = ( value % 4294967296 ) | 0, highBits = ( value / 4294967296 ) | 0;
|
||||
|
||||
buffer[i++] = lowBits & 0xff; buffer[i++] = (lowBits >> 8) & 0xff;
|
||||
buffer[i++] = (lowBits >> 16) & 0xff; buffer[i++] = (lowBits >> 24) & 0xff;
|
||||
buffer[i++] = highBits & 0xff; buffer[i++] = (highBits >> 8) & 0xff;
|
||||
buffer[i++] = (highBits >> 16) & 0xff; buffer[i++] = (highBits >> 24) & 0xff;
|
||||
}
|
||||
} else { /// we have a float / Double
|
||||
buffer[i++] = 1; /// = BSON.BSON_DATA_NUMBER;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
/// OLD: writeIEEE754(buffer, value, i, 'little', 52, 8);
|
||||
buffer.writeDoubleLE(value, i); i += 8;
|
||||
}
|
||||
return i;
|
||||
|
||||
case Boolean:
|
||||
buffer[i++] = 8; /// = BSON.BSON_DATA_BOOLEAN;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
buffer[i++] = value ? 1 : 0;
|
||||
return i;
|
||||
|
||||
case Array:
|
||||
case Object:
|
||||
buffer[i++] = value.constructor === Array ? 4 : 3; /// = BSON.BSON_DATA_ARRAY / _OBJECT;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
|
||||
var endIndex = serializeFast(value, checkKeys, buffer, i); /// + 4); no longer needed b/c serializeFast writes a temp 4 bytes for length
|
||||
var size = endIndex - i;
|
||||
buffer[i++] = size & 0xff; buffer[i++] = (size >> 8) & 0xff;
|
||||
buffer[i++] = (size >> 16) & 0xff; buffer[i++] = (size >> 24) & 0xff;
|
||||
return endIndex;
|
||||
|
||||
/// case Binary: /// is basically identical unless special/deprecated options!
|
||||
case Buffer: /// solves ALL of our Binary needs without the BSON.Binary class!?
|
||||
buffer[i++] = 5; /// = BSON.BSON_DATA_BINARY;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
var size = value.length;
|
||||
buffer[i++] = size & 0xff; buffer[i++] = (size >> 8) & 0xff;
|
||||
buffer[i++] = (size >> 16) & 0xff; buffer[i++] = (size >> 24) & 0xff;
|
||||
|
||||
buffer[i++] = 0; /// write BSON.BSON_BINARY_SUBTYPE_DEFAULT;
|
||||
value.copy(buffer, i); ///, 0, size); << defaults to sourceStart=0, sourceEnd=sourceBuffer.length);
|
||||
i += size;
|
||||
return i;
|
||||
|
||||
case RegExp:
|
||||
buffer[i++] = 11; /// = BSON.BSON_DATA_REGEXP;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
i += buffer.write(value.source, i, 'utf8'); buffer[i++] = 0x00;
|
||||
|
||||
if (value.global) buffer[i++] = 0x73; // s = 'g' for JS Regex!
|
||||
if (value.ignoreCase) buffer[i++] = 0x69; // i
|
||||
if (value.multiline) buffer[i++] = 0x6d; // m
|
||||
buffer[i++] = 0x00;
|
||||
return i;
|
||||
|
||||
case Date:
|
||||
buffer[i++] = 9; /// = BSON.BSON_DATA_DATE;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
var millis = value.getTime();
|
||||
var lowBits = ( millis % 4294967296 ) | 0, highBits = ( millis / 4294967296 ) | 0;
|
||||
|
||||
buffer[i++] = lowBits & 0xff; buffer[i++] = (lowBits >> 8) & 0xff;
|
||||
buffer[i++] = (lowBits >> 16) & 0xff; buffer[i++] = (lowBits >> 24) & 0xff;
|
||||
buffer[i++] = highBits & 0xff; buffer[i++] = (highBits >> 8) & 0xff;
|
||||
buffer[i++] = (highBits >> 16) & 0xff; buffer[i++] = (highBits >> 24) & 0xff;
|
||||
return i;
|
||||
|
||||
case Long:
|
||||
case Timestamp:
|
||||
buffer[i++] = value.constructor === Long ? 18 : 17; /// = BSON.BSON_DATA_LONG / _TIMESTAMP
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
var lowBits = value.getLowBits(), highBits = value.getHighBits();
|
||||
|
||||
buffer[i++] = lowBits & 0xff; buffer[i++] = (lowBits >> 8) & 0xff;
|
||||
buffer[i++] = (lowBits >> 16) & 0xff; buffer[i++] = (lowBits >> 24) & 0xff;
|
||||
buffer[i++] = highBits & 0xff; buffer[i++] = (highBits >> 8) & 0xff;
|
||||
buffer[i++] = (highBits >> 16) & 0xff; buffer[i++] = (highBits >> 24) & 0xff;
|
||||
return i;
|
||||
|
||||
case Double:
|
||||
buffer[i++] = 1; /// = BSON.BSON_DATA_NUMBER;
|
||||
i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
/// OLD: writeIEEE754(buffer, value, i, 'little', 52, 8); i += 8;
|
||||
buffer.writeDoubleLE(value, i); i += 8;
|
||||
return i
|
||||
|
||||
case MinKey: /// = BSON.BSON_DATA_MINKEY;
|
||||
buffer[i++] = 127; i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
return i;
|
||||
case MaxKey: /// = BSON.BSON_DATA_MAXKEY;
|
||||
buffer[i++] = 255; i += buffer.write(name, i, 'utf8'); buffer[i++] = 0;
|
||||
return i;
|
||||
|
||||
} /// end of switch
|
||||
return i; /// ?! If no value to serialize
|
||||
}
|
||||
|
||||
|
||||
module.exports.deserializeFast = deserializeFast;
|
||||
|
||||
function deserializeFast(buffer, i, isArray){ //// , options, isArray) { //// no more options!
|
||||
if (buffer.length < 5) return new Error('Corrupt bson message < 5 bytes long'); /// from 'throw'
|
||||
var elementType, tempindex = 0, name;
|
||||
var string, low, high; /// = lowBits / highBits
|
||||
/// using 'i' as the index to keep the lines shorter:
|
||||
i || ( i = 0 ); /// for parseResponse it's 0; set to running index in deserialize(object/array) recursion
|
||||
var object = isArray ? [] : {}; /// needed for type ARRAY recursion later!
|
||||
var size = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
if(size < 5 || size > buffer.length) return new Error('Corrupt BSON message');
|
||||
/// 'size' var was not used by anything after this, so we can reuse it
|
||||
|
||||
while(true) { // While we have more left data left keep parsing
|
||||
elementType = buffer[i++]; // Read the type
|
||||
if (elementType === 0) break; // If we get a zero it's the last byte, exit
|
||||
|
||||
tempindex = i; /// inlined readCStyleString & removed extra i<buffer.length check slowing EACH loop!
|
||||
while( buffer[tempindex] !== 0x00 ) tempindex++; /// read ahead w/out changing main 'i' index
|
||||
if (tempindex >= buffer.length) return new Error('Corrupt BSON document: illegal CString')
|
||||
name = buffer.toString('utf8', i, tempindex);
|
||||
i = tempindex + 1; /// Update index position to after the string + '0' termination
|
||||
|
||||
switch(elementType) {
|
||||
|
||||
case 7: /// = BSON.BSON_DATA_OID:
|
||||
var buf = new Buffer(12);
|
||||
buffer.copy(buf, 0, i, i += 12 ); /// copy 12 bytes from the current 'i' offset into fresh Buffer
|
||||
object[name] = new ObjectID(buf); ///... & attach to the new ObjectID instance
|
||||
break;
|
||||
|
||||
case 2: /// = BSON.BSON_DATA_STRING:
|
||||
size = buffer[i++] | buffer[i++] <<8 | buffer[i++] <<16 | buffer[i++] <<24;
|
||||
object[name] = buffer.toString('utf8', i, i += size -1 );
|
||||
i++; break; /// need to get the '0' index "tick-forward" back!
|
||||
|
||||
case 16: /// = BSON.BSON_DATA_INT: // Decode the 32bit value
|
||||
object[name] = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24; break;
|
||||
|
||||
case 1: /// = BSON.BSON_DATA_NUMBER: // Decode the double value
|
||||
object[name] = buffer.readDoubleLE(i); /// slightly faster depending on dec.points; a LOT cleaner
|
||||
/// OLD: object[name] = readIEEE754(buffer, i, 'little', 52, 8);
|
||||
i += 8; break;
|
||||
|
||||
case 8: /// = BSON.BSON_DATA_BOOLEAN:
|
||||
object[name] = buffer[i++] == 1; break;
|
||||
|
||||
case 6: /// = BSON.BSON_DATA_UNDEFINED: /// deprecated
|
||||
case 10: /// = BSON.BSON_DATA_NULL:
|
||||
object[name] = null; break;
|
||||
|
||||
case 4: /// = BSON.BSON_DATA_ARRAY
|
||||
size = buffer[i] | buffer[i+1] <<8 | buffer[i+2] <<16 | buffer[i+3] <<24; /// NO 'i' increment since the size bytes are reread during the recursion!
|
||||
object[name] = deserializeFast(buffer, i, true ); /// pass current index & set isArray = true
|
||||
i += size; break;
|
||||
case 3: /// = BSON.BSON_DATA_OBJECT:
|
||||
size = buffer[i] | buffer[i+1] <<8 | buffer[i+2] <<16 | buffer[i+3] <<24;
|
||||
object[name] = deserializeFast(buffer, i, false ); /// isArray = false => Object
|
||||
i += size; break;
|
||||
|
||||
case 5: /// = BSON.BSON_DATA_BINARY: // Decode the size of the binary blob
|
||||
size = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
buffer[i++]; /// Skip, as we assume always default subtype, i.e. 0!
|
||||
object[name] = buffer.slice(i, i += size); /// creates a new Buffer "slice" view of the same memory!
|
||||
break;
|
||||
|
||||
case 9: /// = BSON.BSON_DATA_DATE: /// SEE notes below on the Date type vs. other options...
|
||||
low = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
high = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
object[name] = new Date( high * 4294967296 + (low < 0 ? low + 4294967296 : low) ); break;
|
||||
|
||||
case 18: /// = BSON.BSON_DATA_LONG: /// usage should be somewhat rare beyond parseResponse() -> cursorId, where it is handled inline, NOT as part of deserializeFast(returnedObjects); get lowBits, highBits:
|
||||
low = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
high = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
|
||||
size = high * 4294967296 + (low < 0 ? low + 4294967296 : low); /// from long.toNumber()
|
||||
if (size < JS_INT_MAX && size > JS_INT_MIN) object[name] = size; /// positive # more likely!
|
||||
else object[name] = new Long(low, high); break;
|
||||
|
||||
case 127: /// = BSON.BSON_DATA_MIN_KEY: /// do we EVER actually get these BACK from MongoDB server?!
|
||||
object[name] = new MinKey(); break;
|
||||
case 255: /// = BSON.BSON_DATA_MAX_KEY:
|
||||
object[name] = new MaxKey(); break;
|
||||
|
||||
case 17: /// = BSON.BSON_DATA_TIMESTAMP: /// somewhat obscure internal BSON type; MongoDB uses it for (pseudo) high-res time timestamp (past millisecs precision is just a counter!) in the Oplog ts: field, etc.
|
||||
low = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
high = buffer[i++] | buffer[i++] << 8 | buffer[i++] << 16 | buffer[i++] << 24;
|
||||
object[name] = new Timestamp(low, high); break;
|
||||
|
||||
/// case 11: /// = RegExp is skipped; we should NEVER be getting any from the MongoDB server!?
|
||||
} /// end of switch(elementType)
|
||||
} /// end of while(1)
|
||||
return object; // Return the finalized object
|
||||
}
|
||||
|
||||
|
||||
function MinKey() { this._bsontype = 'MinKey'; } /// these are merely placeholders/stubs to signify the type!?
|
||||
|
||||
function MaxKey() { this._bsontype = 'MaxKey'; }
|
||||
|
||||
function Long(low, high) {
|
||||
this._bsontype = 'Long';
|
||||
this.low_ = low | 0; this.high_ = high | 0; /// force into 32 signed bits.
|
||||
}
|
||||
Long.prototype.getLowBits = function(){ return this.low_; }
|
||||
Long.prototype.getHighBits = function(){ return this.high_; }
|
||||
|
||||
Long.prototype.toNumber = function(){
|
||||
return this.high_ * 4294967296 + (this.low_ < 0 ? this.low_ + 4294967296 : this.low_);
|
||||
}
|
||||
Long.fromNumber = function(num){
|
||||
return new Long(num % 4294967296, num / 4294967296); /// |0 is forced in the constructor!
|
||||
}
|
||||
function Double(value) {
|
||||
this._bsontype = 'Double';
|
||||
this.value = value;
|
||||
}
|
||||
function Timestamp(low, high) {
|
||||
this._bsontype = 'Timestamp';
|
||||
this.low_ = low | 0; this.high_ = high | 0; /// force into 32 signed bits.
|
||||
}
|
||||
Timestamp.prototype.getLowBits = function(){ return this.low_; }
|
||||
Timestamp.prototype.getHighBits = function(){ return this.high_; }
|
||||
|
||||
/////////////////////////////// ObjectID /////////////////////////////////
|
||||
/// machine & proc IDs stored as 1 string, b/c Buffer shouldn't be held for long periods (could use SlowBuffer?!)
|
||||
|
||||
var MACHINE = parseInt(Math.random() * 0xFFFFFF, 10);
|
||||
var PROCESS = process.pid % 0xFFFF;
|
||||
var MACHINE_AND_PROC = encodeIntBE(MACHINE, 3) + encodeIntBE(PROCESS, 2); /// keep as ONE string, ready to go.
|
||||
|
||||
function encodeIntBE(data, bytes){ /// encode the bytes to a string
|
||||
var result = '';
|
||||
if (bytes >= 4){ result += String.fromCharCode(Math.floor(data / 0x1000000)); data %= 0x1000000; }
|
||||
if (bytes >= 3){ result += String.fromCharCode(Math.floor(data / 0x10000)); data %= 0x10000; }
|
||||
if (bytes >= 2){ result += String.fromCharCode(Math.floor(data / 0x100)); data %= 0x100; }
|
||||
result += String.fromCharCode(Math.floor(data));
|
||||
return result;
|
||||
}
|
||||
var _counter = ~~(Math.random() * 0xFFFFFF); /// double-tilde is equivalent to Math.floor()
|
||||
var checkForHex = new RegExp('^[0-9a-fA-F]{24}$');
|
||||
|
||||
function ObjectID(id) {
|
||||
this._bsontype = 'ObjectID';
|
||||
if (!id){ this.id = createFromScratch(); /// base case, DONE.
|
||||
} else {
|
||||
if (id.constructor === Buffer){
|
||||
this.id = id; /// case of
|
||||
} else if (id.constructor === String) {
|
||||
if ( id.length === 24 && checkForHex.test(id) ) {
|
||||
this.id = new Buffer(id, 'hex');
|
||||
} else {
|
||||
this.id = new Error('Illegal/faulty Hexadecimal string supplied!'); /// changed from 'throw'
|
||||
}
|
||||
} else if (id.constructor === Number) {
|
||||
this.id = createFromTime(id); /// this is what should be the only interface for this!?
|
||||
}
|
||||
}
|
||||
}
|
||||
function createFromScratch() {
|
||||
var buf = new Buffer(12), i = 0;
|
||||
var ts = ~~(Date.now()/1000); /// 4 bytes timestamp in seconds, BigEndian notation!
|
||||
buf[i++] = (ts >> 24) & 0xFF; buf[i++] = (ts >> 16) & 0xFF;
|
||||
buf[i++] = (ts >> 8) & 0xFF; buf[i++] = (ts) & 0xFF;
|
||||
|
||||
buf.write(MACHINE_AND_PROC, i, 5, 'utf8'); i += 5; /// write 3 bytes + 2 bytes MACHINE_ID and PROCESS_ID
|
||||
_counter = ++_counter % 0xFFFFFF; /// 3 bytes internal _counter for subsecond resolution; BigEndian
|
||||
buf[i++] = (_counter >> 16) & 0xFF;
|
||||
buf[i++] = (_counter >> 8) & 0xFF;
|
||||
buf[i++] = (_counter) & 0xFF;
|
||||
return buf;
|
||||
}
|
||||
function createFromTime(ts) {
|
||||
ts || ( ts = ~~(Date.now()/1000) ); /// 4 bytes timestamp in seconds only
|
||||
var buf = new Buffer(12), i = 0;
|
||||
buf[i++] = (ts >> 24) & 0xFF; buf[i++] = (ts >> 16) & 0xFF;
|
||||
buf[i++] = (ts >> 8) & 0xFF; buf[i++] = (ts) & 0xFF;
|
||||
|
||||
for (;i < 12; ++i) buf[i] = 0x00; /// indeces 4 through 11 (8 bytes) get filled up with nulls
|
||||
return buf;
|
||||
}
|
||||
ObjectID.prototype.toHexString = function toHexString() {
|
||||
return this.id.toString('hex');
|
||||
}
|
||||
ObjectID.prototype.getTimestamp = function getTimestamp() {
|
||||
return this.id.readUIntBE(0, 4);
|
||||
}
|
||||
ObjectID.prototype.getTimestampDate = function getTimestampDate() {
|
||||
var ts = new Date();
|
||||
ts.setTime(this.id.readUIntBE(0, 4) * 1000);
|
||||
return ts;
|
||||
}
|
||||
ObjectID.createPk = function createPk () { ///?override if a PrivateKey factory w/ unique factors is warranted?!
|
||||
return new ObjectID();
|
||||
}
|
||||
ObjectID.prototype.toJSON = function toJSON() {
|
||||
return "ObjectID('" +this.id.toString('hex')+ "')";
|
||||
}
|
||||
|
||||
/// module.exports.BSON = BSON; /// not needed anymore!? exports.Binary = Binary;
|
||||
module.exports.ObjectID = ObjectID;
|
||||
module.exports.MinKey = MinKey;
|
||||
module.exports.MaxKey = MaxKey;
|
||||
module.exports.Long = Long; /// ?! we really don't want to do the complicated Long math anywhere for now!?
|
||||
|
||||
//module.exports.Double = Double;
|
||||
//module.exports.Timestamp = Timestamp;
|
||||
25
node_modules/bson/bower.json
generated
vendored
Normal file
25
node_modules/bson/bower.json
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"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"
|
||||
]
|
||||
}
|
||||
4843
node_modules/bson/browser_build/bson.js
generated
vendored
Normal file
4843
node_modules/bson/browser_build/bson.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
8
node_modules/bson/browser_build/package.json
generated
vendored
Normal file
8
node_modules/bson/browser_build/package.json
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{ "name" : "bson"
|
||||
, "description" : "A bson parser for node.js and the browser"
|
||||
, "main": "../lib/bson/bson"
|
||||
, "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" } ]
|
||||
}
|
||||
344
node_modules/bson/lib/bson/binary.js
generated
vendored
Normal file
344
node_modules/bson/lib/bson/binary.js
generated
vendored
Normal file
@@ -0,0 +1,344 @@
|
||||
/**
|
||||
* Module dependencies.
|
||||
* @ignore
|
||||
*/
|
||||
if(typeof window === '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);
|
||||
|
||||
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 {
|
||||
var 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(var 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(var 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
node_modules/bson/lib/bson/binary_parser.js
generated
vendored
Normal file
385
node_modules/bson/lib/bson/binary_parser.js
generated
vendored
Normal file
@@ -0,0 +1,385 @@
|
||||
/**
|
||||
* Binary Parser.
|
||||
* Jonas Raoni Soares Silva
|
||||
* http://jsfromhell.com/classes/binary-parser [v1.0]
|
||||
*/
|
||||
var chr = String.fromCharCode;
|
||||
|
||||
var maxBits = [];
|
||||
for (var i = 0; i < 64; i++) {
|
||||
maxBits[i] = Math.pow(2, i);
|
||||
}
|
||||
|
||||
function BinaryParser (bigEndian, allowExceptions) {
|
||||
if(!(this instanceof BinaryParser)) return new BinaryParser(bigEndian, allowExceptions);
|
||||
|
||||
this.bigEndian = bigEndian;
|
||||
this.allowExceptions = allowExceptions;
|
||||
};
|
||||
|
||||
BinaryParser.warn = function warn (msg) {
|
||||
if (this.allowExceptions) {
|
||||
throw new Error(msg);
|
||||
}
|
||||
|
||||
return 1;
|
||||
};
|
||||
|
||||
BinaryParser.decodeFloat = function decodeFloat (data, precisionBits, exponentBits) {
|
||||
var b = new this.Buffer(this.bigEndian, data);
|
||||
|
||||
b.checkBuffer(precisionBits + exponentBits + 1);
|
||||
|
||||
var bias = maxBits[exponentBits - 1] - 1
|
||||
, signal = b.readBits(precisionBits + exponentBits, 1)
|
||||
, exponent = b.readBits(precisionBits, exponentBits)
|
||||
, significand = 0
|
||||
, divisor = 2
|
||||
, curByte = b.buffer.length + (-precisionBits >> 3) - 1;
|
||||
|
||||
do {
|
||||
for (var byteValue = b.buffer[ ++curByte ], startBit = precisionBits % 8 || 8, mask = 1 << startBit; mask >>= 1; ( byteValue & mask ) && ( significand += 1 / divisor ), divisor *= 2 );
|
||||
} while (precisionBits -= startBit);
|
||||
|
||||
return exponent == ( bias << 1 ) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity : ( 1 + signal * -2 ) * ( exponent || significand ? !exponent ? Math.pow( 2, -bias + 1 ) * significand : Math.pow( 2, exponent - bias ) * ( 1 + significand ) : 0 );
|
||||
};
|
||||
|
||||
BinaryParser.decodeInt = function decodeInt (data, bits, signed, forceBigEndian) {
|
||||
var b = new this.Buffer(this.bigEndian || forceBigEndian, data)
|
||||
, x = b.readBits(0, bits)
|
||||
, max = maxBits[bits]; //max = Math.pow( 2, bits );
|
||||
|
||||
return signed && x >= max / 2
|
||||
? x - max
|
||||
: x;
|
||||
};
|
||||
|
||||
BinaryParser.encodeFloat = function encodeFloat (data, precisionBits, exponentBits) {
|
||||
var bias = maxBits[exponentBits - 1] - 1
|
||||
, minExp = -bias + 1
|
||||
, maxExp = bias
|
||||
, minUnnormExp = minExp - precisionBits
|
||||
, n = parseFloat(data)
|
||||
, status = isNaN(n) || n == -Infinity || n == +Infinity ? n : 0
|
||||
, exp = 0
|
||||
, len = 2 * bias + 1 + precisionBits + 3
|
||||
, bin = new Array(len)
|
||||
, signal = (n = status !== 0 ? 0 : n) < 0
|
||||
, intPart = Math.floor(n = Math.abs(n))
|
||||
, floatPart = n - intPart
|
||||
, lastBit
|
||||
, rounded
|
||||
, result
|
||||
, i
|
||||
, j;
|
||||
|
||||
for (i = len; i; bin[--i] = 0);
|
||||
|
||||
for (i = bias + 2; intPart && i; bin[--i] = intPart % 2, intPart = Math.floor(intPart / 2));
|
||||
|
||||
for (i = bias + 1; floatPart > 0 && i; (bin[++i] = ((floatPart *= 2) >= 1) - 0 ) && --floatPart);
|
||||
|
||||
for (i = -1; ++i < len && !bin[i];);
|
||||
|
||||
if (bin[(lastBit = precisionBits - 1 + (i = (exp = bias + 1 - i) >= minExp && exp <= maxExp ? i + 1 : bias + 1 - (exp = minExp - 1))) + 1]) {
|
||||
if (!(rounded = bin[lastBit])) {
|
||||
for (j = lastBit + 2; !rounded && j < len; rounded = bin[j++]);
|
||||
}
|
||||
|
||||
for (j = lastBit + 1; rounded && --j >= 0; (bin[j] = !bin[j] - 0) && (rounded = 0));
|
||||
}
|
||||
|
||||
for (i = i - 2 < 0 ? -1 : i - 3; ++i < len && !bin[i];);
|
||||
|
||||
if ((exp = bias + 1 - i) >= minExp && exp <= maxExp) {
|
||||
++i;
|
||||
} else if (exp < minExp) {
|
||||
exp != bias + 1 - len && exp < minUnnormExp && this.warn("encodeFloat::float underflow");
|
||||
i = bias + 1 - (exp = minExp - 1);
|
||||
}
|
||||
|
||||
if (intPart || status !== 0) {
|
||||
this.warn(intPart ? "encodeFloat::float overflow" : "encodeFloat::" + status);
|
||||
exp = maxExp + 1;
|
||||
i = bias + 2;
|
||||
|
||||
if (status == -Infinity) {
|
||||
signal = 1;
|
||||
} else if (isNaN(status)) {
|
||||
bin[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (n = Math.abs(exp + bias), j = exponentBits + 1, result = ""; --j; result = (n % 2) + result, n = n >>= 1);
|
||||
|
||||
for (n = 0, j = 0, i = (result = (signal ? "1" : "0") + result + bin.slice(i, i + precisionBits).join("")).length, r = []; i; j = (j + 1) % 8) {
|
||||
n += (1 << j) * result.charAt(--i);
|
||||
if (j == 7) {
|
||||
r[r.length] = String.fromCharCode(n);
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
r[r.length] = n
|
||||
? String.fromCharCode(n)
|
||||
: "";
|
||||
|
||||
return (this.bigEndian ? r.reverse() : r).join("");
|
||||
};
|
||||
|
||||
BinaryParser.encodeInt = function encodeInt (data, bits, signed, forceBigEndian) {
|
||||
var max = maxBits[bits];
|
||||
|
||||
if (data >= max || data < -(max / 2)) {
|
||||
this.warn("encodeInt::overflow");
|
||||
data = 0;
|
||||
}
|
||||
|
||||
if (data < 0) {
|
||||
data += max;
|
||||
}
|
||||
|
||||
for (var r = []; data; r[r.length] = String.fromCharCode(data % 256), data = Math.floor(data / 256));
|
||||
|
||||
for (bits = -(-bits >> 3) - r.length; bits--; r[r.length] = "\0");
|
||||
|
||||
return ((this.bigEndian || forceBigEndian) ? r.reverse() : r).join("");
|
||||
};
|
||||
|
||||
BinaryParser.toSmall = function( data ){ return this.decodeInt( data, 8, true ); };
|
||||
BinaryParser.fromSmall = function( data ){ return this.encodeInt( data, 8, true ); };
|
||||
BinaryParser.toByte = function( data ){ return this.decodeInt( data, 8, false ); };
|
||||
BinaryParser.fromByte = function( data ){ return this.encodeInt( data, 8, false ); };
|
||||
BinaryParser.toShort = function( data ){ return this.decodeInt( data, 16, true ); };
|
||||
BinaryParser.fromShort = function( data ){ return this.encodeInt( data, 16, true ); };
|
||||
BinaryParser.toWord = function( data ){ return this.decodeInt( data, 16, false ); };
|
||||
BinaryParser.fromWord = function( data ){ return this.encodeInt( data, 16, false ); };
|
||||
BinaryParser.toInt = function( data ){ return this.decodeInt( data, 32, true ); };
|
||||
BinaryParser.fromInt = function( data ){ return this.encodeInt( data, 32, true ); };
|
||||
BinaryParser.toLong = function( data ){ return this.decodeInt( data, 64, true ); };
|
||||
BinaryParser.fromLong = function( data ){ return this.encodeInt( data, 64, true ); };
|
||||
BinaryParser.toDWord = function( data ){ return this.decodeInt( data, 32, false ); };
|
||||
BinaryParser.fromDWord = function( data ){ return this.encodeInt( data, 32, false ); };
|
||||
BinaryParser.toQWord = function( data ){ return this.decodeInt( data, 64, true ); };
|
||||
BinaryParser.fromQWord = function( data ){ return this.encodeInt( data, 64, true ); };
|
||||
BinaryParser.toFloat = function( data ){ return this.decodeFloat( data, 23, 8 ); };
|
||||
BinaryParser.fromFloat = function( data ){ return this.encodeFloat( data, 23, 8 ); };
|
||||
BinaryParser.toDouble = function( data ){ return this.decodeFloat( data, 52, 11 ); };
|
||||
BinaryParser.fromDouble = function( data ){ return this.encodeFloat( data, 52, 11 ); };
|
||||
|
||||
// Factor out the encode so it can be shared by add_header and push_int32
|
||||
BinaryParser.encode_int32 = function encode_int32 (number, asArray) {
|
||||
var a, b, c, d, unsigned;
|
||||
unsigned = (number < 0) ? (number + 0x100000000) : number;
|
||||
a = Math.floor(unsigned / 0xffffff);
|
||||
unsigned &= 0xffffff;
|
||||
b = Math.floor(unsigned / 0xffff);
|
||||
unsigned &= 0xffff;
|
||||
c = Math.floor(unsigned / 0xff);
|
||||
unsigned &= 0xff;
|
||||
d = Math.floor(unsigned);
|
||||
return asArray ? [chr(a), chr(b), chr(c), chr(d)] : chr(a) + chr(b) + chr(c) + chr(d);
|
||||
};
|
||||
|
||||
BinaryParser.encode_int64 = function encode_int64 (number) {
|
||||
var a, b, c, d, e, f, g, h, unsigned;
|
||||
unsigned = (number < 0) ? (number + 0x10000000000000000) : number;
|
||||
a = Math.floor(unsigned / 0xffffffffffffff);
|
||||
unsigned &= 0xffffffffffffff;
|
||||
b = Math.floor(unsigned / 0xffffffffffff);
|
||||
unsigned &= 0xffffffffffff;
|
||||
c = Math.floor(unsigned / 0xffffffffff);
|
||||
unsigned &= 0xffffffffff;
|
||||
d = Math.floor(unsigned / 0xffffffff);
|
||||
unsigned &= 0xffffffff;
|
||||
e = Math.floor(unsigned / 0xffffff);
|
||||
unsigned &= 0xffffff;
|
||||
f = Math.floor(unsigned / 0xffff);
|
||||
unsigned &= 0xffff;
|
||||
g = Math.floor(unsigned / 0xff);
|
||||
unsigned &= 0xff;
|
||||
h = Math.floor(unsigned);
|
||||
return chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h);
|
||||
};
|
||||
|
||||
/**
|
||||
* UTF8 methods
|
||||
*/
|
||||
|
||||
// Take a raw binary string and return a utf8 string
|
||||
BinaryParser.decode_utf8 = function decode_utf8 (binaryStr) {
|
||||
var len = binaryStr.length
|
||||
, decoded = ''
|
||||
, i = 0
|
||||
, c = 0
|
||||
, c1 = 0
|
||||
, c2 = 0
|
||||
, c3;
|
||||
|
||||
while (i < len) {
|
||||
c = binaryStr.charCodeAt(i);
|
||||
if (c < 128) {
|
||||
decoded += String.fromCharCode(c);
|
||||
i++;
|
||||
} else if ((c > 191) && (c < 224)) {
|
||||
c2 = binaryStr.charCodeAt(i+1);
|
||||
decoded += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
||||
i += 2;
|
||||
} else {
|
||||
c2 = binaryStr.charCodeAt(i+1);
|
||||
c3 = binaryStr.charCodeAt(i+2);
|
||||
decoded += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
||||
i += 3;
|
||||
}
|
||||
}
|
||||
|
||||
return decoded;
|
||||
};
|
||||
|
||||
// Encode a cstring
|
||||
BinaryParser.encode_cstring = function encode_cstring (s) {
|
||||
return unescape(encodeURIComponent(s)) + BinaryParser.fromByte(0);
|
||||
};
|
||||
|
||||
// Take a utf8 string and return a binary string
|
||||
BinaryParser.encode_utf8 = function encode_utf8 (s) {
|
||||
var a = ""
|
||||
, c;
|
||||
|
||||
for (var n = 0, len = s.length; n < len; n++) {
|
||||
c = s.charCodeAt(n);
|
||||
|
||||
if (c < 128) {
|
||||
a += String.fromCharCode(c);
|
||||
} else if ((c > 127) && (c < 2048)) {
|
||||
a += String.fromCharCode((c>>6) | 192) ;
|
||||
a += String.fromCharCode((c&63) | 128);
|
||||
} else {
|
||||
a += String.fromCharCode((c>>12) | 224);
|
||||
a += String.fromCharCode(((c>>6) & 63) | 128);
|
||||
a += String.fromCharCode((c&63) | 128);
|
||||
}
|
||||
}
|
||||
|
||||
return a;
|
||||
};
|
||||
|
||||
BinaryParser.hprint = function hprint (s) {
|
||||
var number;
|
||||
|
||||
for (var i = 0, len = s.length; i < len; i++) {
|
||||
if (s.charCodeAt(i) < 32) {
|
||||
number = s.charCodeAt(i) <= 15
|
||||
? "0" + s.charCodeAt(i).toString(16)
|
||||
: s.charCodeAt(i).toString(16);
|
||||
process.stdout.write(number + " ")
|
||||
} else {
|
||||
number = s.charCodeAt(i) <= 15
|
||||
? "0" + s.charCodeAt(i).toString(16)
|
||||
: s.charCodeAt(i).toString(16);
|
||||
process.stdout.write(number + " ")
|
||||
}
|
||||
}
|
||||
|
||||
process.stdout.write("\n\n");
|
||||
};
|
||||
|
||||
BinaryParser.ilprint = function hprint (s) {
|
||||
var number;
|
||||
|
||||
for (var i = 0, len = s.length; i < len; i++) {
|
||||
if (s.charCodeAt(i) < 32) {
|
||||
number = s.charCodeAt(i) <= 15
|
||||
? "0" + s.charCodeAt(i).toString(10)
|
||||
: s.charCodeAt(i).toString(10);
|
||||
|
||||
require('util').debug(number+' : ');
|
||||
} else {
|
||||
number = s.charCodeAt(i) <= 15
|
||||
? "0" + s.charCodeAt(i).toString(10)
|
||||
: s.charCodeAt(i).toString(10);
|
||||
require('util').debug(number+' : '+ s.charAt(i));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BinaryParser.hlprint = function hprint (s) {
|
||||
var number;
|
||||
|
||||
for (var i = 0, len = s.length; i < len; i++) {
|
||||
if (s.charCodeAt(i) < 32) {
|
||||
number = s.charCodeAt(i) <= 15
|
||||
? "0" + s.charCodeAt(i).toString(16)
|
||||
: s.charCodeAt(i).toString(16);
|
||||
require('util').debug(number+' : ');
|
||||
} else {
|
||||
number = s.charCodeAt(i) <= 15
|
||||
? "0" + s.charCodeAt(i).toString(16)
|
||||
: s.charCodeAt(i).toString(16);
|
||||
require('util').debug(number+' : '+ s.charAt(i));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* BinaryParser buffer constructor.
|
||||
*/
|
||||
function BinaryParserBuffer (bigEndian, buffer) {
|
||||
this.bigEndian = bigEndian || 0;
|
||||
this.buffer = [];
|
||||
this.setBuffer(buffer);
|
||||
};
|
||||
|
||||
BinaryParserBuffer.prototype.setBuffer = function setBuffer (data) {
|
||||
var l, i, b;
|
||||
|
||||
if (data) {
|
||||
i = l = data.length;
|
||||
b = this.buffer = new Array(l);
|
||||
for (; i; b[l - i] = data.charCodeAt(--i));
|
||||
this.bigEndian && b.reverse();
|
||||
}
|
||||
};
|
||||
|
||||
BinaryParserBuffer.prototype.hasNeededBits = function hasNeededBits (neededBits) {
|
||||
return this.buffer.length >= -(-neededBits >> 3);
|
||||
};
|
||||
|
||||
BinaryParserBuffer.prototype.checkBuffer = function checkBuffer (neededBits) {
|
||||
if (!this.hasNeededBits(neededBits)) {
|
||||
throw new Error("checkBuffer::missing bytes");
|
||||
}
|
||||
};
|
||||
|
||||
BinaryParserBuffer.prototype.readBits = function readBits (start, length) {
|
||||
//shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
|
||||
|
||||
function shl (a, b) {
|
||||
for (; b--; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
|
||||
return a;
|
||||
}
|
||||
|
||||
if (start < 0 || length <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
this.checkBuffer(start + length);
|
||||
|
||||
var offsetLeft
|
||||
, offsetRight = start % 8
|
||||
, curByte = this.buffer.length - ( start >> 3 ) - 1
|
||||
, lastByte = this.buffer.length + ( -( start + length ) >> 3 )
|
||||
, diff = curByte - lastByte
|
||||
, sum = ((this.buffer[ curByte ] >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1)) + (diff && (offsetLeft = (start + length) % 8) ? (this.buffer[lastByte++] & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight : 0);
|
||||
|
||||
for(; diff; sum += shl(this.buffer[lastByte++], (diff-- << 3) - offsetRight));
|
||||
|
||||
return sum;
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose.
|
||||
*/
|
||||
BinaryParser.Buffer = BinaryParserBuffer;
|
||||
|
||||
exports.BinaryParser = BinaryParser;
|
||||
323
node_modules/bson/lib/bson/bson.js
generated
vendored
Normal file
323
node_modules/bson/lib/bson/bson.js
generated
vendored
Normal file
@@ -0,0 +1,323 @@
|
||||
// "use strict"
|
||||
|
||||
var writeIEEE754 = require('./float_parser').writeIEEE754,
|
||||
readIEEE754 = require('./float_parser').readIEEE754,
|
||||
Map = require('./map'),
|
||||
Long = require('./long').Long,
|
||||
Double = require('./double').Double,
|
||||
Timestamp = require('./timestamp').Timestamp,
|
||||
ObjectID = require('./objectid').ObjectID,
|
||||
BSONRegExp = require('./regexp').BSONRegExp,
|
||||
Symbol = require('./symbol').Symbol,
|
||||
Code = require('./code').Code,
|
||||
MinKey = require('./min_key').MinKey,
|
||||
MaxKey = require('./max_key').MaxKey,
|
||||
DBRef = require('./db_ref').DBRef,
|
||||
Binary = require('./binary').Binary;
|
||||
|
||||
// Parts of the parser
|
||||
var deserialize = require('./parser/deserializer'),
|
||||
serializer = require('./parser/serializer'),
|
||||
calculateObjectSize = require('./parser/calculate_size');
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
// Max Size
|
||||
var MAXSIZE = (1024*1024*17);
|
||||
// Max Document Buffer size
|
||||
var buffer = new Buffer(MAXSIZE);
|
||||
|
||||
var BSON = function() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a Javascript object.
|
||||
*
|
||||
* @param {Object} object the Javascript object to serialize.
|
||||
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
||||
* @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
|
||||
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
|
||||
* @return {Buffer} returns the Buffer object containing the serialized object.
|
||||
* @api public
|
||||
*/
|
||||
BSON.prototype.serialize = function serialize(object, checkKeys, asBuffer, serializeFunctions, index, ignoreUndefined) {
|
||||
// Attempt to serialize
|
||||
var serializationIndex = serializer(buffer, object, checkKeys, index || 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 {Boolean} checkKeys the serializer will check if keys are valid.
|
||||
* @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
|
||||
* @param {Number} index the index in the buffer where we wish to start serializing into.
|
||||
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
|
||||
* @return {Number} returns the new write index in the Buffer.
|
||||
* @api public
|
||||
*/
|
||||
BSON.prototype.serializeWithBufferAndIndex = function(object, checkKeys, finalBuffer, startIndex, serializeFunctions, ignoreUndefined) {
|
||||
// Attempt to serialize
|
||||
var serializationIndex = serializer(buffer, object, checkKeys, startIndex || 0, 0, serializeFunctions, ignoreUndefined);
|
||||
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
|
||||
// Return the index
|
||||
return startIndex + serializationIndex - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize data as BSON.
|
||||
*
|
||||
* Options
|
||||
* - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
||||
* - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
||||
* - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
||||
* - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
|
||||
*
|
||||
* @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
|
||||
* @param {Object} [options] additional options used for the deserialization.
|
||||
* @param {Boolean} [isArray] ignore used for recursive parsing.
|
||||
* @return {Object} returns the deserialized Javascript Object.
|
||||
* @api public
|
||||
*/
|
||||
BSON.prototype.deserialize = function(data, options) {
|
||||
return deserialize(data, 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} [serializeFunctions] serialize all functions in the object **(default:false)**.
|
||||
* @return {Number} returns the number of bytes the BSON object will take up.
|
||||
* @api public
|
||||
*/
|
||||
BSON.prototype.calculateObjectSize = function(object, serializeFunctions, ignoreUndefined) {
|
||||
return calculateObjectSize(object, serializeFunctions, ignoreUndefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize stream data as BSON documents.
|
||||
*
|
||||
* Options
|
||||
* - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
||||
* - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
||||
* - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
||||
* - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
|
||||
*
|
||||
* @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.
|
||||
* @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) {
|
||||
// if(numberOfDocuments !== documents.length) throw new Error("Number of expected results back is less than the number of documents");
|
||||
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.MinKey = MinKey;
|
||||
module.exports.MaxKey = MaxKey;
|
||||
module.exports.BSONRegExp = BSONRegExp;
|
||||
24
node_modules/bson/lib/bson/code.js
generated
vendored
Normal file
24
node_modules/bson/lib/bson/code.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 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 == null ? {} : scope;
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
Code.prototype.toJSON = function() {
|
||||
return {scope:this.scope, code:this.code};
|
||||
}
|
||||
|
||||
module.exports = Code;
|
||||
module.exports.Code = Code;
|
||||
32
node_modules/bson/lib/bson/db_ref.js
generated
vendored
Normal file
32
node_modules/bson/lib/bson/db_ref.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 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;
|
||||
33
node_modules/bson/lib/bson/double.js
generated
vendored
Normal file
33
node_modules/bson/lib/bson/double.js
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 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;
|
||||
121
node_modules/bson/lib/bson/float_parser.js
generated
vendored
Normal file
121
node_modules/bson/lib/bson/float_parser.js
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
// 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;
|
||||
86
node_modules/bson/lib/bson/index.js
generated
vendored
Normal file
86
node_modules/bson/lib/bson/index.js
generated
vendored
Normal file
@@ -0,0 +1,86 @@
|
||||
try {
|
||||
exports.BSONPure = require('./bson');
|
||||
exports.BSONNative = require('./bson');
|
||||
} catch(err) {
|
||||
}
|
||||
|
||||
[ './binary_parser'
|
||||
, './binary'
|
||||
, './code'
|
||||
, './map'
|
||||
, './db_ref'
|
||||
, './double'
|
||||
, './max_key'
|
||||
, './min_key'
|
||||
, './objectid'
|
||||
, './regexp'
|
||||
, './symbol'
|
||||
, './timestamp'
|
||||
, './long'].forEach(function (path) {
|
||||
var module = require('./' + path);
|
||||
for (var i in module) {
|
||||
exports[i] = module[i];
|
||||
}
|
||||
});
|
||||
|
||||
// Exports all the classes for the PURE JS BSON Parser
|
||||
exports.pure = function() {
|
||||
var classes = {};
|
||||
// Map all the classes
|
||||
[ './binary_parser'
|
||||
, './binary'
|
||||
, './code'
|
||||
, './map'
|
||||
, './db_ref'
|
||||
, './double'
|
||||
, './max_key'
|
||||
, './min_key'
|
||||
, './objectid'
|
||||
, './regexp'
|
||||
, './symbol'
|
||||
, './timestamp'
|
||||
, './long'
|
||||
, '././bson'].forEach(function (path) {
|
||||
var module = require('./' + path);
|
||||
for (var i in module) {
|
||||
classes[i] = module[i];
|
||||
}
|
||||
});
|
||||
// Return classes list
|
||||
return classes;
|
||||
}
|
||||
|
||||
// Exports all the classes for the NATIVE JS BSON Parser
|
||||
exports.native = function() {
|
||||
var classes = {};
|
||||
// Map all the classes
|
||||
[ './binary_parser'
|
||||
, './binary'
|
||||
, './code'
|
||||
, './map'
|
||||
, './db_ref'
|
||||
, './double'
|
||||
, './max_key'
|
||||
, './min_key'
|
||||
, './objectid'
|
||||
, './regexp'
|
||||
, './symbol'
|
||||
, './timestamp'
|
||||
, './long'
|
||||
].forEach(function (path) {
|
||||
var module = require('./' + path);
|
||||
for (var i in module) {
|
||||
classes[i] = module[i];
|
||||
}
|
||||
});
|
||||
|
||||
// Catch error and return no classes found
|
||||
try {
|
||||
classes['BSON'] = require('./bson');
|
||||
} catch(err) {
|
||||
return exports.pure();
|
||||
}
|
||||
|
||||
// Return classes list
|
||||
return classes;
|
||||
}
|
||||
856
node_modules/bson/lib/bson/long.js
generated
vendored
Normal file
856
node_modules/bson/lib/bson/long.js
generated
vendored
Normal file
@@ -0,0 +1,856 @@
|
||||
// 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));
|
||||
|
||||
var rem = this;
|
||||
var result = '';
|
||||
while (true) {
|
||||
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;
|
||||
var rem = this;
|
||||
while (rem.greaterThanOrEqual(other)) {
|
||||
// Approximate the result of division. This may be a little greater or
|
||||
// smaller than the actual value.
|
||||
var 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;
|
||||
126
node_modules/bson/lib/bson/map.js
generated
vendored
Normal file
126
node_modules/bson/lib/bson/map.js
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
"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(key) {
|
||||
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(key, value) {
|
||||
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
node_modules/bson/lib/bson/max_key.js
generated
vendored
Normal file
14
node_modules/bson/lib/bson/max_key.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* 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
node_modules/bson/lib/bson/min_key.js
generated
vendored
Normal file
14
node_modules/bson/lib/bson/min_key.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* 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;
|
||||
278
node_modules/bson/lib/bson/objectid.js
generated
vendored
Normal file
278
node_modules/bson/lib/bson/objectid.js
generated
vendored
Normal file
@@ -0,0 +1,278 @@
|
||||
/**
|
||||
* Module dependencies.
|
||||
* @ignore
|
||||
*/
|
||||
var BinaryParser = require('./binary_parser').BinaryParser;
|
||||
|
||||
/**
|
||||
* 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}$");
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if(!(this instanceof ObjectID)) return new ObjectID(id);
|
||||
if((id instanceof ObjectID)) return id;
|
||||
|
||||
this._bsontype = 'ObjectID';
|
||||
var __id = null;
|
||||
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) {
|
||||
return ObjectID.createFromHexString(id);
|
||||
} else if(id == null || typeof id == 'number') {
|
||||
// convert to 12 byte binary string
|
||||
this.id = this.generate(id);
|
||||
} else if(id != null && id.length === 12) {
|
||||
// assume 12 byte string
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
if(ObjectID.cacheHexString) this.__id = this.toHexString();
|
||||
};
|
||||
|
||||
// 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 = '';
|
||||
|
||||
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 string used in ObjectID's
|
||||
*
|
||||
* @method
|
||||
* @param {number} [time] optional parameter allowing to pass in a second based timestamp.
|
||||
* @return {string} return the 12 byte id binary string.
|
||||
*/
|
||||
ObjectID.prototype.generate = function(time) {
|
||||
if ('number' != typeof time) {
|
||||
time = parseInt(Date.now()/1000,10);
|
||||
}
|
||||
|
||||
var time4Bytes = BinaryParser.encodeInt(time, 32, true, true);
|
||||
/* for time-based ObjectID the bytes following the time will be zeroed */
|
||||
var machine3Bytes = BinaryParser.encodeInt(MACHINE_ID, 24, false);
|
||||
var pid2Bytes = BinaryParser.fromShort((typeof process === 'undefined' ? Math.floor(Math.random() * 100000) : process.pid) % 0xFFFF);
|
||||
var index3Bytes = BinaryParser.encodeInt(this.get_inc(), 24, false, true);
|
||||
|
||||
return time4Bytes + machine3Bytes + pid2Bytes + index3Bytes;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts the id into a 24 byte hex string for printing
|
||||
*
|
||||
* @return {String} return the 24 byte hex string representation.
|
||||
* @ignore
|
||||
*/
|
||||
ObjectID.prototype.toString = function() {
|
||||
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 != null && (otherID instanceof ObjectID || otherID.toHexString)) {
|
||||
id = otherID.id;
|
||||
} else if(typeof otherID == 'string' && ObjectID.isValid(otherID)) {
|
||||
id = ObjectID.createFromHexString(otherID).id;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.id === id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
timestamp.setTime(Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true)) * 1000);
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
ObjectID.index = parseInt(Math.random() * 0xFFFFFF, 10);
|
||||
|
||||
/**
|
||||
* @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 id = BinaryParser.encodeInt(time, 32, true, true) +
|
||||
BinaryParser.encodeInt(0, 64, true, true);
|
||||
return new ObjectID(id);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 (hexString) {
|
||||
// Throw an error if it's not a valid setup
|
||||
if(typeof hexString === 'undefined' || hexString != null && hexString.length != 24)
|
||||
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
|
||||
|
||||
var len = hexString.length;
|
||||
|
||||
if(len > 12*2) {
|
||||
throw new Error('Id cannot be longer than 12 bytes');
|
||||
}
|
||||
|
||||
var result = ''
|
||||
, string
|
||||
, number;
|
||||
|
||||
for (var index = 0; index < len; index += 2) {
|
||||
string = hexString.substr(index, 2);
|
||||
number = parseInt(string, 16);
|
||||
result += BinaryParser.fromByte(number);
|
||||
}
|
||||
|
||||
return new ObjectID(result, hexString);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
Object.defineProperty(ObjectID.prototype, "generationTime", {
|
||||
enumerable: true
|
||||
, get: function () {
|
||||
return Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true));
|
||||
}
|
||||
, set: function (value) {
|
||||
var value = BinaryParser.encodeInt(value, 32, true, true);
|
||||
this.id = value + this.id.substr(4);
|
||||
// delete this.__id;
|
||||
this.toHexString();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Expose.
|
||||
*/
|
||||
module.exports = ObjectID;
|
||||
module.exports.ObjectID = ObjectID;
|
||||
module.exports.ObjectId = ObjectID;
|
||||
310
node_modules/bson/lib/bson/parser/calculate_size.js
generated
vendored
Normal file
310
node_modules/bson/lib/bson/parser/calculate_size.js
generated
vendored
Normal file
@@ -0,0 +1,310 @@
|
||||
"use strict"
|
||||
|
||||
var writeIEEE754 = require('../float_parser').writeIEEE754
|
||||
, readIEEE754 = require('../float_parser').readIEEE754
|
||||
, 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
|
||||
, MinKey = require('../min_key').MinKey
|
||||
, MaxKey = require('../max_key').MaxKey
|
||||
, DBRef = require('../db_ref').DBRef
|
||||
, Binary = require('../binary').Binary;
|
||||
|
||||
// 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 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(value.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined);
|
||||
} else if(serializeFunctions) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
// 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 = calculateObjectSize;
|
||||
555
node_modules/bson/lib/bson/parser/deserializer.js
generated
vendored
Normal file
555
node_modules/bson/lib/bson/parser/deserializer.js
generated
vendored
Normal file
@@ -0,0 +1,555 @@
|
||||
"use strict"
|
||||
|
||||
var writeIEEE754 = require('../float_parser').writeIEEE754,
|
||||
readIEEE754 = require('../float_parser').readIEEE754,
|
||||
f = require('util').format,
|
||||
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,
|
||||
DBRef = require('../db_ref').DBRef,
|
||||
BSONRegExp = require('../regexp').BSONRegExp,
|
||||
Binary = require('../binary').Binary;
|
||||
|
||||
var deserialize = function(buffer, options, isArray) {
|
||||
var index = 0;
|
||||
// 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 || buffer.length < size) {
|
||||
throw new Error("corrupt bson message");
|
||||
}
|
||||
|
||||
// Illegal end value
|
||||
if(buffer[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, options, isArray);
|
||||
}
|
||||
|
||||
// Reads in a C style string
|
||||
var readCStyleStringSpecial = function(buffer, index) {
|
||||
// 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")
|
||||
// Grab utf8 encoded string
|
||||
var string = buffer.toString('utf8', index, i);
|
||||
// Update index position
|
||||
index = i + 1;
|
||||
// Return string
|
||||
return {s: string, i: index};
|
||||
}
|
||||
|
||||
var deserializeObject = function(buffer, options, isArray) {
|
||||
// Options
|
||||
options = options == null ? {} : options;
|
||||
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
|
||||
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
|
||||
var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
|
||||
var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
|
||||
var fieldsAsRaw = options['fieldsAsRaw'] == null ? {} : options['fieldsAsRaw'];
|
||||
// Return BSONRegExp objects instead of native regular expressions
|
||||
var bsonRegExp = typeof options['bsonRegExp'] == 'boolean' ? options['bsonRegExp'] : false;
|
||||
var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
|
||||
|
||||
// Validate that we have at least 4 bytes of buffer
|
||||
if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long");
|
||||
|
||||
// Set up index
|
||||
var index = typeof options['index'] == 'number' ? options['index'] : 0;
|
||||
|
||||
// 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 ? [] : {};
|
||||
|
||||
// While we have more left data left keep parsing
|
||||
while(true) {
|
||||
// Read the type
|
||||
var elementType = buffer[index++];
|
||||
// If we get a zero it's the last byte, exit
|
||||
if(elementType == 0) break;
|
||||
// Read the name of the field
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var name = r.s;
|
||||
index = r.i;
|
||||
|
||||
// Switch on the type
|
||||
if(elementType == BSON.BSON_DATA_OID) {
|
||||
var string = buffer.toString('binary', index, index + 12);
|
||||
// Decode the oid
|
||||
object[name] = new ObjectID(string);
|
||||
// Update index
|
||||
index = index + 12;
|
||||
} else if(elementType == BSON.BSON_DATA_STRING) {
|
||||
// Read the content of the field
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Add string to object
|
||||
object[name] = buffer.toString('utf8', index, index + stringSize - 1);
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
} else if(elementType == BSON.BSON_DATA_INT) {
|
||||
// Decode the 32bit value
|
||||
object[name] = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
} else if(elementType == BSON.BSON_DATA_NUMBER) {
|
||||
// Decode the double value
|
||||
object[name] = readIEEE754(buffer, index, 'little', 52, 8);
|
||||
// Update the index
|
||||
index = index + 8;
|
||||
} else if(elementType == BSON.BSON_DATA_DATE) {
|
||||
// Unpack the low and high bits
|
||||
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;
|
||||
// Set date object
|
||||
object[name] = new Date(new Long(lowBits, highBits).toNumber());
|
||||
} else if(elementType == BSON.BSON_DATA_BOOLEAN) {
|
||||
// Parse the boolean value
|
||||
object[name] = buffer[index++] == 1;
|
||||
} else if(elementType == BSON.BSON_DATA_UNDEFINED || elementType == BSON.BSON_DATA_NULL) {
|
||||
// Parse the boolean value
|
||||
object[name] = null;
|
||||
} else if(elementType == BSON.BSON_DATA_BINARY) {
|
||||
// Decode the size of the binary blob
|
||||
var binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Decode the subtype
|
||||
var subType = buffer[index++];
|
||||
// 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(promoteBuffers) {
|
||||
// assign reference to sliced Buffer object
|
||||
object[name] = buffer.slice(index, index + binarySize);
|
||||
} else {
|
||||
// Slice the data
|
||||
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;
|
||||
}
|
||||
// Copy the data
|
||||
for(var i = 0; i < binarySize; i++) {
|
||||
_buffer[i] = buffer[index + i];
|
||||
}
|
||||
if(promoteBuffers) {
|
||||
// assign reference to Buffer object
|
||||
object[name] = _buffer;
|
||||
} else {
|
||||
// Create the binary object
|
||||
object[name] = new Binary(_buffer, subType);
|
||||
}
|
||||
}
|
||||
// Update the index
|
||||
index = index + binarySize;
|
||||
} else if(elementType == BSON.BSON_DATA_ARRAY) {
|
||||
options['index'] = index;
|
||||
// Decode the size of the array document
|
||||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
var arrayOptions = options;
|
||||
|
||||
// All elements of array to be returned as raw bson
|
||||
if(fieldsAsRaw[name]) {
|
||||
arrayOptions = {};
|
||||
for(var n in options) arrayOptions[n] = options[n];
|
||||
arrayOptions['raw'] = true;
|
||||
}
|
||||
|
||||
// Set the array to the object
|
||||
object[name] = deserializeObject(buffer, arrayOptions, true);
|
||||
// Adjust the index
|
||||
index = index + objectSize;
|
||||
} else if(elementType == BSON.BSON_DATA_OBJECT) {
|
||||
options['index'] = index;
|
||||
// Decode the size of the object document
|
||||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(objectSize <= 0 || objectSize > (buffer.length - index)) throw new Error("bad embedded document length in bson");
|
||||
|
||||
// We have a raw value
|
||||
if(options['raw']) {
|
||||
// Set the array to the object
|
||||
object[name] = buffer.slice(index, index + objectSize);
|
||||
} else {
|
||||
// Set the array to the object
|
||||
object[name] = deserializeObject(buffer, options, false);
|
||||
}
|
||||
|
||||
// Adjust the index
|
||||
index = index + objectSize;
|
||||
} else if(elementType == BSON.BSON_DATA_REGEXP && bsonRegExp == false) {
|
||||
// Create the regexp
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var source = r.s;
|
||||
index = r.i;
|
||||
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var regExpOptions = r.s;
|
||||
index = r.i;
|
||||
|
||||
// For each option add the corresponding one for javascript
|
||||
var optionsArray = new Array(regExpOptions.length);
|
||||
|
||||
// Parse options
|
||||
for(var 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) {
|
||||
// Create the regexp
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var source = r.s;
|
||||
index = r.i;
|
||||
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var regExpOptions = r.s;
|
||||
index = r.i;
|
||||
|
||||
// Set the object
|
||||
object[name] = new BSONRegExp(source, regExpOptions);
|
||||
} else if(elementType == BSON.BSON_DATA_LONG) {
|
||||
// Unpack the low and high bits
|
||||
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;
|
||||
// Create long object
|
||||
var long = new Long(lowBits, highBits);
|
||||
// Promote the long if possible
|
||||
if(promoteLongs) {
|
||||
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_SYMBOL) {
|
||||
// Read the content of the field
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Add string to object
|
||||
object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1));
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
} else if(elementType == BSON.BSON_DATA_TIMESTAMP) {
|
||||
// Unpack the low and high bits
|
||||
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;
|
||||
// Set the object
|
||||
object[name] = new Timestamp(lowBits, highBits);
|
||||
} else if(elementType == BSON.BSON_DATA_MIN_KEY) {
|
||||
// Parse the object
|
||||
object[name] = new MinKey();
|
||||
} else if(elementType == BSON.BSON_DATA_MAX_KEY) {
|
||||
// Parse the object
|
||||
object[name] = new MaxKey();
|
||||
} else if(elementType == BSON.BSON_DATA_CODE) {
|
||||
// Read the content of the field
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Function string
|
||||
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
|
||||
|
||||
// If we are evaluating the functions
|
||||
if(evalFunctions) {
|
||||
// Contains the value we are going to set
|
||||
var value = null;
|
||||
// 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 {
|
||||
// Set directly
|
||||
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) {
|
||||
// Read the content of the field
|
||||
var totalSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Javascript function
|
||||
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
// Parse the element
|
||||
options['index'] = index;
|
||||
// Decode the size of the object document
|
||||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
// Decode the scope object
|
||||
var scopeObject = deserializeObject(buffer, options, false);
|
||||
// Adjust the index
|
||||
index = index + objectSize;
|
||||
|
||||
// If we are evaluating the functions
|
||||
if(evalFunctions) {
|
||||
// Contains the value we are going to set
|
||||
var value = null;
|
||||
// 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 {
|
||||
// Set directly
|
||||
object[name] = isolateEval(functionString);
|
||||
}
|
||||
|
||||
// Set the scope on the object
|
||||
object[name].scope = scopeObject;
|
||||
} else {
|
||||
object[name] = new Code(functionString, scopeObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we have a db ref object
|
||||
if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
|
||||
|
||||
// Return the final objects
|
||||
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;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
// 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
|
||||
912
node_modules/bson/lib/bson/parser/serializer.js
generated
vendored
Normal file
912
node_modules/bson/lib/bson/parser/serializer.js
generated
vendored
Normal file
@@ -0,0 +1,912 @@
|
||||
"use strict"
|
||||
|
||||
var writeIEEE754 = require('../float_parser').writeIEEE754,
|
||||
readIEEE754 = require('../float_parser').readIEEE754,
|
||||
Long = require('../long').Long,
|
||||
Map = require('../map'),
|
||||
Double = require('../double').Double,
|
||||
Timestamp = require('../timestamp').Timestamp,
|
||||
ObjectID = require('../objectid').ObjectID,
|
||||
Symbol = require('../symbol').Symbol,
|
||||
Code = require('../code').Code,
|
||||
BSONRegExp = require('../regexp').BSONRegExp,
|
||||
MinKey = require('../min_key').MinKey,
|
||||
MaxKey = require('../max_key').MaxKey,
|
||||
DBRef = require('../db_ref').DBRef,
|
||||
Binary = require('../binary').Binary;
|
||||
|
||||
var regexp = /\x00/
|
||||
|
||||
// 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 isRegExp = function isRegExp(d) {
|
||||
return Object.prototype.toString.call(d) === '[object RegExp]';
|
||||
}
|
||||
|
||||
var serializeString = function(buffer, key, value, index) {
|
||||
// Encode String type
|
||||
buffer[index++] = BSON.BSON_DATA_STRING;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes + 1;
|
||||
buffer[index - 1] = 0;
|
||||
// Write the string
|
||||
var size = buffer.write(value, index + 4, 'utf8');
|
||||
// Write the size of the string to buffer
|
||||
buffer[index + 3] = (size + 1 >> 24) & 0xff;
|
||||
buffer[index + 2] = (size + 1 >> 16) & 0xff;
|
||||
buffer[index + 1] = (size + 1 >> 8) & 0xff;
|
||||
buffer[index] = size + 1 & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size;
|
||||
// Write zero
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeNumber = function(buffer, key, value, index) {
|
||||
// We have an integer value
|
||||
if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
||||
// If the value fits in 32 bits encode as int, if it fits in a double
|
||||
// encode it as a double, otherwise long
|
||||
if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
|
||||
// Set int type 32 bits or less
|
||||
buffer[index++] = BSON.BSON_DATA_INT;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write the int value
|
||||
buffer[index++] = value & 0xff;
|
||||
buffer[index++] = (value >> 8) & 0xff;
|
||||
buffer[index++] = (value >> 16) & 0xff;
|
||||
buffer[index++] = (value >> 24) & 0xff;
|
||||
} else if(value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
||||
// Encode as double
|
||||
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write float
|
||||
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
||||
// Ajust index
|
||||
index = index + 8;
|
||||
} else {
|
||||
// Set long type
|
||||
buffer[index++] = BSON.BSON_DATA_LONG;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
var longVal = Long.fromNumber(value);
|
||||
var lowBits = longVal.getLowBits();
|
||||
var highBits = longVal.getHighBits();
|
||||
// Encode low bits
|
||||
buffer[index++] = lowBits & 0xff;
|
||||
buffer[index++] = (lowBits >> 8) & 0xff;
|
||||
buffer[index++] = (lowBits >> 16) & 0xff;
|
||||
buffer[index++] = (lowBits >> 24) & 0xff;
|
||||
// Encode high bits
|
||||
buffer[index++] = highBits & 0xff;
|
||||
buffer[index++] = (highBits >> 8) & 0xff;
|
||||
buffer[index++] = (highBits >> 16) & 0xff;
|
||||
buffer[index++] = (highBits >> 24) & 0xff;
|
||||
}
|
||||
} else {
|
||||
// Encode as double
|
||||
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write float
|
||||
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
||||
// Ajust index
|
||||
index = index + 8;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeUndefined = function(buffer, key, value, index) {
|
||||
// Set long type
|
||||
buffer[index++] = BSON.BSON_DATA_NULL;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeBoolean = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_BOOLEAN;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Encode the boolean value
|
||||
buffer[index++] = value ? 1 : 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeDate = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_DATE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
// Write the date
|
||||
var dateInMilis = Long.fromNumber(value.getTime());
|
||||
var lowBits = dateInMilis.getLowBits();
|
||||
var highBits = dateInMilis.getHighBits();
|
||||
// Encode low bits
|
||||
buffer[index++] = lowBits & 0xff;
|
||||
buffer[index++] = (lowBits >> 8) & 0xff;
|
||||
buffer[index++] = (lowBits >> 16) & 0xff;
|
||||
buffer[index++] = (lowBits >> 24) & 0xff;
|
||||
// Encode high bits
|
||||
buffer[index++] = highBits & 0xff;
|
||||
buffer[index++] = (highBits >> 8) & 0xff;
|
||||
buffer[index++] = (highBits >> 16) & 0xff;
|
||||
buffer[index++] = (highBits >> 24) & 0xff;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeRegExp = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
if (value.source && value.source.match(regexp) != null) {
|
||||
throw Error("value " + value.source + " must not contain null bytes");
|
||||
}
|
||||
// Adjust the index
|
||||
index = index + buffer.write(value.source, index, 'utf8');
|
||||
// Write zero
|
||||
buffer[index++] = 0x00;
|
||||
// Write the parameters
|
||||
if(value.global) buffer[index++] = 0x73; // s
|
||||
if(value.ignoreCase) buffer[index++] = 0x69; // i
|
||||
if(value.multiline) buffer[index++] = 0x6d; // m
|
||||
// Add ending zero
|
||||
buffer[index++] = 0x00;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeBSONRegExp = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Adjust the index
|
||||
index = index + buffer.write(value.pattern, index, 'utf8');
|
||||
// Write zero
|
||||
buffer[index++] = 0x00;
|
||||
// Write the options
|
||||
index = index + buffer.write(value.options, index, 'utf8');
|
||||
// Add ending zero
|
||||
buffer[index++] = 0x00;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeMinMax = function(buffer, key, value, index) {
|
||||
// Write the type of either min or max key
|
||||
if(value === null) {
|
||||
buffer[index++] = BSON.BSON_DATA_NULL;
|
||||
} else if(value instanceof MinKey) {
|
||||
buffer[index++] = BSON.BSON_DATA_MIN_KEY;
|
||||
} else {
|
||||
buffer[index++] = BSON.BSON_DATA_MAX_KEY;
|
||||
}
|
||||
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeObjectId = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_OID;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
// Write the objectId into the shared buffer
|
||||
buffer.write(value.id, index, 'binary')
|
||||
|
||||
// Ajust index
|
||||
return index + 12;
|
||||
}
|
||||
|
||||
var serializeBuffer = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_BINARY;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Get size of the buffer (current write point)
|
||||
var size = value.length;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index++] = size & 0xff;
|
||||
buffer[index++] = (size >> 8) & 0xff;
|
||||
buffer[index++] = (size >> 16) & 0xff;
|
||||
buffer[index++] = (size >> 24) & 0xff;
|
||||
// Write the default subtype
|
||||
buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT;
|
||||
// Copy the content form the binary field to the buffer
|
||||
value.copy(buffer, index, 0, size);
|
||||
// Adjust the index
|
||||
index = index + size;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeObject = function(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined) {
|
||||
// Write the type
|
||||
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
var endIndex = serializeInto(buffer, value, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined);
|
||||
// Write size
|
||||
var size = endIndex - index;
|
||||
return endIndex;
|
||||
}
|
||||
|
||||
var serializeLong = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = value instanceof Long ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write the date
|
||||
var lowBits = value.getLowBits();
|
||||
var highBits = value.getHighBits();
|
||||
// Encode low bits
|
||||
buffer[index++] = lowBits & 0xff;
|
||||
buffer[index++] = (lowBits >> 8) & 0xff;
|
||||
buffer[index++] = (lowBits >> 16) & 0xff;
|
||||
buffer[index++] = (lowBits >> 24) & 0xff;
|
||||
// Encode high bits
|
||||
buffer[index++] = highBits & 0xff;
|
||||
buffer[index++] = (highBits >> 8) & 0xff;
|
||||
buffer[index++] = (highBits >> 16) & 0xff;
|
||||
buffer[index++] = (highBits >> 24) & 0xff;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeDouble = function(buffer, key, value, index) {
|
||||
// Encode as double
|
||||
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write float
|
||||
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
||||
// Ajust index
|
||||
index = index + 8;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeFunction = function(buffer, key, value, index, checkKeys, depth) {
|
||||
buffer[index++] = BSON.BSON_DATA_CODE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Function string
|
||||
var functionString = value.toString();
|
||||
// Write the string
|
||||
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = size & 0xff;
|
||||
buffer[index + 1] = (size >> 8) & 0xff;
|
||||
buffer[index + 2] = (size >> 16) & 0xff;
|
||||
buffer[index + 3] = (size >> 24) & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size - 1;
|
||||
// Write zero
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeCode = function(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined) {
|
||||
if(value.scope != null && Object.keys(value.scope).length > 0) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
// Starting index
|
||||
var startIndex = index;
|
||||
|
||||
// Serialize the function
|
||||
// Get the function string
|
||||
var functionString = typeof value.code == 'string' ? value.code : value.code.toString();
|
||||
// Index adjustment
|
||||
index = index + 4;
|
||||
// Write string into buffer
|
||||
var codeSize = buffer.write(functionString, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = codeSize & 0xff;
|
||||
buffer[index + 1] = (codeSize >> 8) & 0xff;
|
||||
buffer[index + 2] = (codeSize >> 16) & 0xff;
|
||||
buffer[index + 3] = (codeSize >> 24) & 0xff;
|
||||
// Write end 0
|
||||
buffer[index + 4 + codeSize - 1] = 0;
|
||||
// Write the
|
||||
index = index + codeSize + 4;
|
||||
|
||||
//
|
||||
// Serialize the scope value
|
||||
var endIndex = serializeInto(buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined)
|
||||
index = endIndex - 1;
|
||||
|
||||
// Writ the total
|
||||
var totalSize = endIndex - startIndex;
|
||||
|
||||
// Write the total size of the object
|
||||
buffer[startIndex++] = totalSize & 0xff;
|
||||
buffer[startIndex++] = (totalSize >> 8) & 0xff;
|
||||
buffer[startIndex++] = (totalSize >> 16) & 0xff;
|
||||
buffer[startIndex++] = (totalSize >> 24) & 0xff;
|
||||
// Write trailing zero
|
||||
buffer[index++] = 0;
|
||||
} else {
|
||||
buffer[index++] = BSON.BSON_DATA_CODE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Function string
|
||||
var functionString = value.code.toString();
|
||||
// Write the string
|
||||
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = size & 0xff;
|
||||
buffer[index + 1] = (size >> 8) & 0xff;
|
||||
buffer[index + 2] = (size >> 16) & 0xff;
|
||||
buffer[index + 3] = (size >> 24) & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size - 1;
|
||||
// Write zero
|
||||
buffer[index++] = 0;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeBinary = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_BINARY;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Extract the buffer
|
||||
var data = value.value(true);
|
||||
// Calculate size
|
||||
var size = value.position;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index++] = size & 0xff;
|
||||
buffer[index++] = (size >> 8) & 0xff;
|
||||
buffer[index++] = (size >> 16) & 0xff;
|
||||
buffer[index++] = (size >> 24) & 0xff;
|
||||
// Write the subtype to the buffer
|
||||
buffer[index++] = value.sub_type;
|
||||
|
||||
// If we have binary type 2 the 4 first bytes are the size
|
||||
if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
|
||||
buffer[index++] = size & 0xff;
|
||||
buffer[index++] = (size >> 8) & 0xff;
|
||||
buffer[index++] = (size >> 16) & 0xff;
|
||||
buffer[index++] = (size >> 24) & 0xff;
|
||||
}
|
||||
|
||||
// Write the data to the object
|
||||
data.copy(buffer, index, 0, value.position);
|
||||
// Adjust the index
|
||||
index = index + value.position;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeSymbol = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_SYMBOL;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write the string
|
||||
var size = buffer.write(value.value, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = size & 0xff;
|
||||
buffer[index + 1] = (size >> 8) & 0xff;
|
||||
buffer[index + 2] = (size >> 16) & 0xff;
|
||||
buffer[index + 3] = (size >> 24) & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size - 1;
|
||||
// Write zero
|
||||
buffer[index++] = 0x00;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeDBRef = function(buffer, key, value, index, depth, serializeFunctions) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_OBJECT;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
var startIndex = index;
|
||||
var endIndex;
|
||||
|
||||
// Serialize object
|
||||
if(null != value.db) {
|
||||
endIndex = serializeInto(buffer, {
|
||||
'$ref': value.namespace
|
||||
, '$id' : value.oid
|
||||
, '$db' : value.db
|
||||
}, false, index, depth + 1, serializeFunctions);
|
||||
} else {
|
||||
endIndex = serializeInto(buffer, {
|
||||
'$ref': value.namespace
|
||||
, '$id' : value.oid
|
||||
}, false, index, depth + 1, serializeFunctions);
|
||||
}
|
||||
|
||||
// Calculate object size
|
||||
var size = endIndex - startIndex;
|
||||
// Write the size
|
||||
buffer[startIndex++] = size & 0xff;
|
||||
buffer[startIndex++] = (size >> 8) & 0xff;
|
||||
buffer[startIndex++] = (size >> 16) & 0xff;
|
||||
buffer[startIndex++] = (size >> 24) & 0xff;
|
||||
// Set index
|
||||
return endIndex;
|
||||
}
|
||||
|
||||
var serializeInto = function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializeFunctions, ignoreUndefined) {
|
||||
startingIndex = startingIndex || 0;
|
||||
|
||||
// Start place to serialize into
|
||||
var index = startingIndex + 4;
|
||||
var self = this;
|
||||
|
||||
// Special case isArray
|
||||
if(Array.isArray(object)) {
|
||||
// Get object keys
|
||||
for(var i = 0; i < object.length; i++) {
|
||||
var key = "" + i;
|
||||
var value = object[i];
|
||||
|
||||
// Is there an override value
|
||||
if(value && value.toBSON) {
|
||||
if(typeof value.toBSON != 'function') throw new Error("toBSON is not a function");
|
||||
value = value.toBSON();
|
||||
}
|
||||
|
||||
var type = typeof value;
|
||||
if(type == 'string') {
|
||||
index = serializeString(buffer, key, value, index);
|
||||
} else if(type == 'number') {
|
||||
index = serializeNumber(buffer, key, value, index);
|
||||
} else if(type == 'boolean') {
|
||||
index = serializeBoolean(buffer, key, value, index);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
index = serializeDate(buffer, key, value, index);
|
||||
} else if(type == 'undefined' || value == null) {
|
||||
index = serializeUndefined(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'ObjectID') {
|
||||
index = serializeObjectId(buffer, key, value, index);
|
||||
} else if(Buffer.isBuffer(value)) {
|
||||
index = serializeBuffer(buffer, key, value, index);
|
||||
} else if(value instanceof RegExp || isRegExp(value)) {
|
||||
index = serializeRegExp(buffer, key, value, index);
|
||||
} else if(type == 'object' && value['_bsontype'] == null) {
|
||||
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
||||
index = serializeLong(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Double') {
|
||||
index = serializeDouble(buffer, key, value, index);
|
||||
} else if(typeof value == 'function' && serializeFunctions) {
|
||||
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'Code') {
|
||||
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Binary') {
|
||||
index = serializeBinary(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Symbol') {
|
||||
index = serializeSymbol(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'DBRef') {
|
||||
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'BSONRegExp') {
|
||||
index = serializeBSONRegExp(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
index = serializeMinMax(buffer, key, value, index);
|
||||
}
|
||||
}
|
||||
} else if(object instanceof Map) {
|
||||
var iterator = object.entries();
|
||||
var done = false;
|
||||
|
||||
while(!done) {
|
||||
// Unpack the next entry
|
||||
var entry = iterator.next();
|
||||
done = entry.done;
|
||||
// Are we done, then skip and terminate
|
||||
if(done) continue;
|
||||
|
||||
// Get the entry values
|
||||
var key = entry.value[0];
|
||||
var value = entry.value[1];
|
||||
|
||||
// Check the type of the value
|
||||
var type = typeof value;
|
||||
|
||||
// Check the key and throw error if it's illegal
|
||||
if(key != '$db' && key != '$ref' && key != '$id') {
|
||||
if (key.match(regexp) != null) {
|
||||
// The BSON spec doesn't allow keys with null bytes because keys are
|
||||
// null-terminated.
|
||||
throw Error("key " + key + " must not contain null bytes");
|
||||
}
|
||||
|
||||
if (checkKeys) {
|
||||
if('$' == key[0]) {
|
||||
throw Error("key " + key + " must not start with '$'");
|
||||
} else if (!!~key.indexOf('.')) {
|
||||
throw Error("key " + key + " must not contain '.'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// console.log("---------------------------------------------------")
|
||||
// console.dir("key = " + key)
|
||||
// console.dir("value = " + value)
|
||||
|
||||
if(type == 'string') {
|
||||
index = serializeString(buffer, key, value, index);
|
||||
} else if(type == 'number') {
|
||||
index = serializeNumber(buffer, key, value, index);
|
||||
} else if(type == 'boolean') {
|
||||
index = serializeBoolean(buffer, key, value, index);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
index = serializeDate(buffer, key, value, index);
|
||||
} else if(value === undefined && ignoreUndefined == true) {
|
||||
} else if(value === null || value === undefined) {
|
||||
index = serializeUndefined(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'ObjectID') {
|
||||
index = serializeObjectId(buffer, key, value, index);
|
||||
} else if(Buffer.isBuffer(value)) {
|
||||
index = serializeBuffer(buffer, key, value, index);
|
||||
} else if(value instanceof RegExp || isRegExp(value)) {
|
||||
index = serializeRegExp(buffer, key, value, index);
|
||||
} else if(type == 'object' && value['_bsontype'] == null) {
|
||||
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
||||
index = serializeLong(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Double') {
|
||||
index = serializeDouble(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Code') {
|
||||
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(typeof value == 'function' && serializeFunctions) {
|
||||
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'Binary') {
|
||||
index = serializeBinary(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Symbol') {
|
||||
index = serializeSymbol(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'DBRef') {
|
||||
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'BSONRegExp') {
|
||||
index = serializeBSONRegExp(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
index = serializeMinMax(buffer, key, value, index);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Did we provide a custom serialization method
|
||||
if(object.toBSON) {
|
||||
if(typeof object.toBSON != 'function') throw new Error("toBSON is not a function");
|
||||
object = object.toBSON();
|
||||
if(object != null && typeof object != 'object') throw new Error("toBSON function did not return an object");
|
||||
}
|
||||
|
||||
// Iterate over all the keys
|
||||
for(var key in object) {
|
||||
var value = object[key];
|
||||
// Is there an override value
|
||||
if(value && value.toBSON) {
|
||||
if(typeof value.toBSON != 'function') throw new Error("toBSON is not a function");
|
||||
value = value.toBSON();
|
||||
}
|
||||
|
||||
// Check the type of the value
|
||||
var type = typeof value;
|
||||
|
||||
// Check the key and throw error if it's illegal
|
||||
if(key != '$db' && key != '$ref' && key != '$id') {
|
||||
if (key.match(regexp) != null) {
|
||||
// The BSON spec doesn't allow keys with null bytes because keys are
|
||||
// null-terminated.
|
||||
throw Error("key " + key + " must not contain null bytes");
|
||||
}
|
||||
|
||||
if (checkKeys) {
|
||||
if('$' == key[0]) {
|
||||
throw Error("key " + key + " must not start with '$'");
|
||||
} else if (!!~key.indexOf('.')) {
|
||||
throw Error("key " + key + " must not contain '.'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(type == 'string') {
|
||||
index = serializeString(buffer, key, value, index);
|
||||
} else if(type == 'number') {
|
||||
index = serializeNumber(buffer, key, value, index);
|
||||
} else if(type == 'boolean') {
|
||||
index = serializeBoolean(buffer, key, value, index);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
index = serializeDate(buffer, key, value, index);
|
||||
} else if(value === undefined && ignoreUndefined == true) {
|
||||
} else if(value === null || value === undefined) {
|
||||
index = serializeUndefined(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'ObjectID') {
|
||||
index = serializeObjectId(buffer, key, value, index);
|
||||
} else if(Buffer.isBuffer(value)) {
|
||||
index = serializeBuffer(buffer, key, value, index);
|
||||
} else if(value instanceof RegExp || isRegExp(value)) {
|
||||
index = serializeRegExp(buffer, key, value, index);
|
||||
} else if(type == 'object' && value['_bsontype'] == null) {
|
||||
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
||||
index = serializeLong(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Double') {
|
||||
index = serializeDouble(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Code') {
|
||||
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(typeof value == 'function' && serializeFunctions) {
|
||||
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'Binary') {
|
||||
index = serializeBinary(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Symbol') {
|
||||
index = serializeSymbol(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'DBRef') {
|
||||
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'BSONRegExp') {
|
||||
index = serializeBSONRegExp(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
index = serializeMinMax(buffer, key, value, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final padding byte for object
|
||||
buffer[index++] = 0x00;
|
||||
|
||||
// Final size
|
||||
var size = index - startingIndex;
|
||||
// Write the size of the object
|
||||
buffer[startingIndex++] = size & 0xff;
|
||||
buffer[startingIndex++] = (size >> 8) & 0xff;
|
||||
buffer[startingIndex++] = (size >> 16) & 0xff;
|
||||
buffer[startingIndex++] = (size >> 24) & 0xff;
|
||||
return index;
|
||||
}
|
||||
|
||||
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;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
// 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 = serializeInto;
|
||||
30
node_modules/bson/lib/bson/regexp.js
generated
vendored
Normal file
30
node_modules/bson/lib/bson/regexp.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 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 < 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;
|
||||
47
node_modules/bson/lib/bson/symbol.js
generated
vendored
Normal file
47
node_modules/bson/lib/bson/symbol.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 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;
|
||||
856
node_modules/bson/lib/bson/timestamp.js
generated
vendored
Normal file
856
node_modules/bson/lib/bson/timestamp.js
generated
vendored
Normal file
@@ -0,0 +1,856 @@
|
||||
// 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));
|
||||
|
||||
var rem = this;
|
||||
var result = '';
|
||||
while (true) {
|
||||
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;
|
||||
var rem = this;
|
||||
while (rem.greaterThanOrEqual(other)) {
|
||||
// Approximate the result of division. This may be a little greater or
|
||||
// smaller than the actual value.
|
||||
var 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;
|
||||
97
node_modules/bson/package.json
generated
vendored
Normal file
97
node_modules/bson/package.json
generated
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"bson@~0.4.20",
|
||||
"/home/mitchell/Desktop/test-mywebsite/mywebsite/node_modules/mongodb-core"
|
||||
]
|
||||
],
|
||||
"_from": "bson@>=0.4.20 <0.5.0",
|
||||
"_id": "bson@0.4.20",
|
||||
"_inCache": true,
|
||||
"_installable": true,
|
||||
"_location": "/bson",
|
||||
"_nodeVersion": "4.2.2",
|
||||
"_npmUser": {
|
||||
"email": "christkv@gmail.com",
|
||||
"name": "christkv"
|
||||
},
|
||||
"_npmVersion": "2.14.7",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"name": "bson",
|
||||
"raw": "bson@~0.4.20",
|
||||
"rawSpec": "~0.4.20",
|
||||
"scope": null,
|
||||
"spec": ">=0.4.20 <0.5.0",
|
||||
"type": "range"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/mongodb-core"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/bson/-/bson-0.4.20.tgz",
|
||||
"_shasum": "39b7fa42ef49378f71f40cbc165e46b41c2ff79e",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "bson@~0.4.20",
|
||||
"_where": "/home/mitchell/Desktop/test-mywebsite/mywebsite/node_modules/mongodb-core",
|
||||
"author": {
|
||||
"email": "christkv@gmail.com",
|
||||
"name": "Christian Amor Kvalheim"
|
||||
},
|
||||
"browser": "lib/bson/bson.js",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mongodb/js-bson/issues"
|
||||
},
|
||||
"config": {
|
||||
"native": false
|
||||
},
|
||||
"contributors": [],
|
||||
"dependencies": {},
|
||||
"description": "A bson parser for node.js and the browser",
|
||||
"devDependencies": {
|
||||
"benchmark": "1.0.0",
|
||||
"colors": "1.1.0",
|
||||
"gleak": "0.2.3",
|
||||
"nodeunit": "0.9.0",
|
||||
"one": "2.X.X"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "./lib/bson"
|
||||
},
|
||||
"dist": {
|
||||
"shasum": "39b7fa42ef49378f71f40cbc165e46b41c2ff79e",
|
||||
"tarball": "http://registry.npmjs.org/bson/-/bson-0.4.20.tgz"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.6.19"
|
||||
},
|
||||
"gitHead": "7a57dc553c82ff1754d04e85fbbbc45b1dbc24fe",
|
||||
"homepage": "https://github.com/mongodb/js-bson",
|
||||
"keywords": [
|
||||
"bson",
|
||||
"mongodb",
|
||||
"parser"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"main": "./lib/bson/index",
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "octave",
|
||||
"email": "chinsay@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "christkv",
|
||||
"email": "christkv@gmail.com"
|
||||
}
|
||||
],
|
||||
"name": "bson",
|
||||
"optionalDependencies": {},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/mongodb/js-bson.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "nodeunit ./test/node"
|
||||
},
|
||||
"version": "0.4.20"
|
||||
}
|
||||
21
node_modules/bson/tools/gleak.js
generated
vendored
Normal file
21
node_modules/bson/tools/gleak.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
var gleak = require('gleak')();
|
||||
gleak.ignore('AssertionError');
|
||||
gleak.ignore('testFullSpec_param_found');
|
||||
gleak.ignore('events');
|
||||
gleak.ignore('Uint8Array');
|
||||
gleak.ignore('Uint8ClampedArray');
|
||||
gleak.ignore('TAP_Global_Harness');
|
||||
gleak.ignore('setImmediate');
|
||||
gleak.ignore('clearImmediate');
|
||||
|
||||
gleak.ignore('DTRACE_NET_SERVER_CONNECTION');
|
||||
gleak.ignore('DTRACE_NET_STREAM_END');
|
||||
gleak.ignore('DTRACE_NET_SOCKET_READ');
|
||||
gleak.ignore('DTRACE_NET_SOCKET_WRITE');
|
||||
gleak.ignore('DTRACE_HTTP_SERVER_REQUEST');
|
||||
gleak.ignore('DTRACE_HTTP_SERVER_RESPONSE');
|
||||
gleak.ignore('DTRACE_HTTP_CLIENT_REQUEST');
|
||||
gleak.ignore('DTRACE_HTTP_CLIENT_RESPONSE');
|
||||
|
||||
module.exports = gleak;
|
||||
Reference in New Issue
Block a user