Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- 5485-master/demos/node_modules/mongoose/lib/schema/objectid.js +203 -0
- 5485-master/demos/node_modules/mongoose/lib/schema/string.js +477 -0
- 5485-master/demos/node_modules/mongoose/lib/types/embedded.js +310 -0
- 5485-master/demos/node_modules/mongoose/lib/types/index.js +13 -0
- 5485-master/demos/node_modules/mongoose/lib/types/objectid.js +20 -0
- 5485-master/demos/node_modules/mongoose/node_modules/async/.travis.yml +3 -0
- 5485-master/demos/node_modules/mongoose/node_modules/async/LICENSE +19 -0
- 5485-master/demos/node_modules/mongoose/node_modules/async/README.md +1646 -0
- 5485-master/demos/node_modules/mongoose/node_modules/async/component.json +11 -0
- 5485-master/demos/node_modules/mongoose/node_modules/async/lib/async.js +1123 -0
- 5485-master/demos/node_modules/mongoose/node_modules/async/package.json +59 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/.travis.yml +4 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/HISTORY +42 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/LICENSE +201 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/README.md +69 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/browser_build/bson.js +0 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/browser_build/package.json +8 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/binary.js +344 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/binary_parser.js +385 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/bson.js +1573 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/bson_new.js +776 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/code.js +24 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/db_ref.js +32 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/double.js +33 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/float_parser.js +121 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/index.js +81 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/long.js +856 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/max_key.js +14 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/min_key.js +14 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/objectid.js +274 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/symbol.js +47 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/timestamp.js +856 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/.npmignore +38 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/HISTORY +31 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/LICENSE +201 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/Makefile +16 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/README.md +37 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/binding.gyp +30 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Makefile +350 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/.deps/Release/bson.node.d +1 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/.deps/Release/obj.target/bson/ext/bson.o.d +38 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/bson.node +0 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/binding.Makefile +6 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/bson.target.mk +154 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/config.gypi +141 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/gyp-mac-tool +512 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/.npmignore +3 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/Makefile +28 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/bson.cc +1072 -0
- 5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/bson.h +278 -0
5485-master/demos/node_modules/mongoose/lib/schema/objectid.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*!
|
| 2 |
+
* Module dependencies.
|
| 3 |
+
*/
|
| 4 |
+
|
| 5 |
+
var SchemaType = require('../schematype')
|
| 6 |
+
, CastError = SchemaType.CastError
|
| 7 |
+
, oid = require('../types/objectid')
|
| 8 |
+
, utils = require('../utils')
|
| 9 |
+
, Document
|
| 10 |
+
|
| 11 |
+
/**
|
| 12 |
+
* ObjectId SchemaType constructor.
|
| 13 |
+
*
|
| 14 |
+
* @param {String} key
|
| 15 |
+
* @param {Object} options
|
| 16 |
+
* @inherits SchemaType
|
| 17 |
+
* @api private
|
| 18 |
+
*/
|
| 19 |
+
|
| 20 |
+
function ObjectId (key, options) {
|
| 21 |
+
SchemaType.call(this, key, options, 'ObjectID');
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/**
|
| 25 |
+
* This schema type's name, to defend against minifiers that mangle
|
| 26 |
+
* function names.
|
| 27 |
+
*
|
| 28 |
+
* @api private
|
| 29 |
+
*/
|
| 30 |
+
ObjectId.schemaName = 'ObjectId';
|
| 31 |
+
|
| 32 |
+
/*!
|
| 33 |
+
* Inherits from SchemaType.
|
| 34 |
+
*/
|
| 35 |
+
ObjectId.prototype = Object.create( SchemaType.prototype );
|
| 36 |
+
ObjectId.prototype.constructor = ObjectId;
|
| 37 |
+
|
| 38 |
+
/**
|
| 39 |
+
* Adds an auto-generated ObjectId default if turnOn is true.
|
| 40 |
+
* @param {Boolean} turnOn auto generated ObjectId defaults
|
| 41 |
+
* @api public
|
| 42 |
+
* @return {SchemaType} this
|
| 43 |
+
*/
|
| 44 |
+
|
| 45 |
+
ObjectId.prototype.auto = function (turnOn) {
|
| 46 |
+
if (turnOn) {
|
| 47 |
+
this.default(defaultId);
|
| 48 |
+
this.set(resetId)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
return this;
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* Check required
|
| 56 |
+
*
|
| 57 |
+
* @api private
|
| 58 |
+
*/
|
| 59 |
+
|
| 60 |
+
ObjectId.prototype.checkRequired = function checkRequired (value, doc) {
|
| 61 |
+
if (SchemaType._isRef(this, value, doc, true)) {
|
| 62 |
+
return null != value;
|
| 63 |
+
} else {
|
| 64 |
+
return value instanceof oid;
|
| 65 |
+
}
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
/**
|
| 69 |
+
* Casts to ObjectId
|
| 70 |
+
*
|
| 71 |
+
* @param {Object} value
|
| 72 |
+
* @param {Object} doc
|
| 73 |
+
* @param {Boolean} init whether this is an initialization cast
|
| 74 |
+
* @api private
|
| 75 |
+
*/
|
| 76 |
+
|
| 77 |
+
ObjectId.prototype.cast = function (value, doc, init) {
|
| 78 |
+
if (SchemaType._isRef(this, value, doc, init)) {
|
| 79 |
+
// wait! we may need to cast this to a document
|
| 80 |
+
|
| 81 |
+
if (null == value) {
|
| 82 |
+
return value;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// lazy load
|
| 86 |
+
Document || (Document = require('./../document'));
|
| 87 |
+
|
| 88 |
+
if (value instanceof Document) {
|
| 89 |
+
value.$__.wasPopulated = true;
|
| 90 |
+
return value;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// setting a populated path
|
| 94 |
+
if (value instanceof oid) {
|
| 95 |
+
return value;
|
| 96 |
+
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
| 97 |
+
throw new CastError('ObjectId', value, this.path);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// Handle the case where user directly sets a populated
|
| 101 |
+
// path to a plain object; cast to the Model used in
|
| 102 |
+
// the population query.
|
| 103 |
+
var path = doc.$__fullPath(this.path);
|
| 104 |
+
var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
| 105 |
+
var pop = owner.populated(path, true);
|
| 106 |
+
var ret = new pop.options.model(value);
|
| 107 |
+
ret.$__.wasPopulated = true;
|
| 108 |
+
return ret;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// If null or undefined
|
| 112 |
+
if (value == null) return value;
|
| 113 |
+
|
| 114 |
+
if (value instanceof oid)
|
| 115 |
+
return value;
|
| 116 |
+
|
| 117 |
+
if (value._id) {
|
| 118 |
+
if (value._id instanceof oid) {
|
| 119 |
+
return value._id;
|
| 120 |
+
}
|
| 121 |
+
if (value._id.toString instanceof Function) {
|
| 122 |
+
try {
|
| 123 |
+
return oid.createFromHexString(value._id.toString());
|
| 124 |
+
} catch(e) {}
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
if (value.toString instanceof Function) {
|
| 129 |
+
try {
|
| 130 |
+
return oid.createFromHexString(value.toString());
|
| 131 |
+
} catch (err) {
|
| 132 |
+
throw new CastError('ObjectId', value, this.path);
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
throw new CastError('ObjectId', value, this.path);
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
/*!
|
| 140 |
+
* ignore
|
| 141 |
+
*/
|
| 142 |
+
|
| 143 |
+
function handleSingle (val) {
|
| 144 |
+
return this.cast(val);
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
function handleArray (val) {
|
| 148 |
+
var self = this;
|
| 149 |
+
return val.map(function (m) {
|
| 150 |
+
return self.cast(m);
|
| 151 |
+
});
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
ObjectId.prototype.$conditionalHandlers =
|
| 155 |
+
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
| 156 |
+
'$all': handleArray,
|
| 157 |
+
'$gt': handleSingle,
|
| 158 |
+
'$gte': handleSingle,
|
| 159 |
+
'$in': handleArray,
|
| 160 |
+
'$lt': handleSingle,
|
| 161 |
+
'$lte': handleSingle,
|
| 162 |
+
'$ne': handleSingle,
|
| 163 |
+
'$nin': handleArray
|
| 164 |
+
});
|
| 165 |
+
|
| 166 |
+
/**
|
| 167 |
+
* Casts contents for queries.
|
| 168 |
+
*
|
| 169 |
+
* @param {String} $conditional
|
| 170 |
+
* @param {any} [val]
|
| 171 |
+
* @api private
|
| 172 |
+
*/
|
| 173 |
+
|
| 174 |
+
ObjectId.prototype.castForQuery = function ($conditional, val) {
|
| 175 |
+
var handler;
|
| 176 |
+
if (arguments.length === 2) {
|
| 177 |
+
handler = this.$conditionalHandlers[$conditional];
|
| 178 |
+
if (!handler)
|
| 179 |
+
throw new Error("Can't use " + $conditional + " with ObjectId.");
|
| 180 |
+
return handler.call(this, val);
|
| 181 |
+
} else {
|
| 182 |
+
return this.cast($conditional);
|
| 183 |
+
}
|
| 184 |
+
};
|
| 185 |
+
|
| 186 |
+
/*!
|
| 187 |
+
* ignore
|
| 188 |
+
*/
|
| 189 |
+
|
| 190 |
+
function defaultId () {
|
| 191 |
+
return new oid();
|
| 192 |
+
};
|
| 193 |
+
|
| 194 |
+
function resetId (v) {
|
| 195 |
+
this.$__._id = null;
|
| 196 |
+
return v;
|
| 197 |
+
}
|
| 198 |
+
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
var SchemaType = require('../schematype')
|
| 7 |
+
, CastError = SchemaType.CastError
|
| 8 |
+
, errorMessages = require('../error').messages
|
| 9 |
+
, utils = require('../utils')
|
| 10 |
+
, Document
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* String SchemaType constructor.
|
| 14 |
+
*
|
| 15 |
+
* @param {String} key
|
| 16 |
+
* @param {Object} options
|
| 17 |
+
* @inherits SchemaType
|
| 18 |
+
* @api private
|
| 19 |
+
*/
|
| 20 |
+
|
| 21 |
+
function SchemaString (key, options) {
|
| 22 |
+
this.enumValues = [];
|
| 23 |
+
this.regExp = null;
|
| 24 |
+
SchemaType.call(this, key, options, 'String');
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
/**
|
| 28 |
+
* This schema type's name, to defend against minifiers that mangle
|
| 29 |
+
* function names.
|
| 30 |
+
*
|
| 31 |
+
* @api private
|
| 32 |
+
*/
|
| 33 |
+
SchemaString.schemaName = 'String';
|
| 34 |
+
|
| 35 |
+
/*!
|
| 36 |
+
* Inherits from SchemaType.
|
| 37 |
+
*/
|
| 38 |
+
SchemaString.prototype = Object.create( SchemaType.prototype );
|
| 39 |
+
SchemaString.prototype.constructor = SchemaString;
|
| 40 |
+
|
| 41 |
+
/**
|
| 42 |
+
* Adds an enum validator
|
| 43 |
+
*
|
| 44 |
+
* ####Example:
|
| 45 |
+
*
|
| 46 |
+
* var states = 'opening open closing closed'.split(' ')
|
| 47 |
+
* var s = new Schema({ state: { type: String, enum: states }})
|
| 48 |
+
* var M = db.model('M', s)
|
| 49 |
+
* var m = new M({ state: 'invalid' })
|
| 50 |
+
* m.save(function (err) {
|
| 51 |
+
* console.error(String(err)) // ValidationError: `invalid` is not a valid enum value for path `state`.
|
| 52 |
+
* m.state = 'open'
|
| 53 |
+
* m.save(callback) // success
|
| 54 |
+
* })
|
| 55 |
+
*
|
| 56 |
+
* // or with custom error messages
|
| 57 |
+
* var enu = {
|
| 58 |
+
* values: 'opening open closing closed'.split(' '),
|
| 59 |
+
* message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
| 60 |
+
* }
|
| 61 |
+
* var s = new Schema({ state: { type: String, enum: enu })
|
| 62 |
+
* var M = db.model('M', s)
|
| 63 |
+
* var m = new M({ state: 'invalid' })
|
| 64 |
+
* m.save(function (err) {
|
| 65 |
+
* console.error(String(err)) // ValidationError: enum validator failed for path `state` with value `invalid`
|
| 66 |
+
* m.state = 'open'
|
| 67 |
+
* m.save(callback) // success
|
| 68 |
+
* })
|
| 69 |
+
*
|
| 70 |
+
* @param {String|Object} [args...] enumeration values
|
| 71 |
+
* @return {SchemaType} this
|
| 72 |
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
| 73 |
+
* @api public
|
| 74 |
+
*/
|
| 75 |
+
|
| 76 |
+
SchemaString.prototype.enum = function () {
|
| 77 |
+
if (this.enumValidator) {
|
| 78 |
+
this.validators = this.validators.filter(function(v) {
|
| 79 |
+
return v.validator != this.enumValidator;
|
| 80 |
+
}, this);
|
| 81 |
+
this.enumValidator = false;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
if (undefined === arguments[0] || false === arguments[0]) {
|
| 85 |
+
return this;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
var values;
|
| 89 |
+
var errorMessage;
|
| 90 |
+
|
| 91 |
+
if (utils.isObject(arguments[0])) {
|
| 92 |
+
values = arguments[0].values;
|
| 93 |
+
errorMessage = arguments[0].message;
|
| 94 |
+
} else {
|
| 95 |
+
values = arguments;
|
| 96 |
+
errorMessage = errorMessages.String.enum;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
for (var i = 0; i < values.length; i++) {
|
| 100 |
+
if (undefined !== values[i]) {
|
| 101 |
+
this.enumValues.push(this.cast(values[i]));
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
var vals = this.enumValues;
|
| 106 |
+
this.enumValidator = function (v) {
|
| 107 |
+
return undefined === v || ~vals.indexOf(v);
|
| 108 |
+
};
|
| 109 |
+
this.validators.push({
|
| 110 |
+
validator: this.enumValidator,
|
| 111 |
+
message: errorMessage,
|
| 112 |
+
kind: 'enum'
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
return this;
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
/**
|
| 119 |
+
* Adds a lowercase setter.
|
| 120 |
+
*
|
| 121 |
+
* ####Example:
|
| 122 |
+
*
|
| 123 |
+
* var s = new Schema({ email: { type: String, lowercase: true }})
|
| 124 |
+
* var M = db.model('M', s);
|
| 125 |
+
* var m = new M({ email: 'SomeEmail@example.COM' });
|
| 126 |
+
* console.log(m.email) // someemail@example.com
|
| 127 |
+
*
|
| 128 |
+
* @api public
|
| 129 |
+
* @return {SchemaType} this
|
| 130 |
+
*/
|
| 131 |
+
|
| 132 |
+
SchemaString.prototype.lowercase = function () {
|
| 133 |
+
return this.set(function (v, self) {
|
| 134 |
+
if ('string' != typeof v) v = self.cast(v)
|
| 135 |
+
if (v) return v.toLowerCase();
|
| 136 |
+
return v;
|
| 137 |
+
});
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
/**
|
| 141 |
+
* Adds an uppercase setter.
|
| 142 |
+
*
|
| 143 |
+
* ####Example:
|
| 144 |
+
*
|
| 145 |
+
* var s = new Schema({ caps: { type: String, uppercase: true }})
|
| 146 |
+
* var M = db.model('M', s);
|
| 147 |
+
* var m = new M({ caps: 'an example' });
|
| 148 |
+
* console.log(m.caps) // AN EXAMPLE
|
| 149 |
+
*
|
| 150 |
+
* @api public
|
| 151 |
+
* @return {SchemaType} this
|
| 152 |
+
*/
|
| 153 |
+
|
| 154 |
+
SchemaString.prototype.uppercase = function () {
|
| 155 |
+
return this.set(function (v, self) {
|
| 156 |
+
if ('string' != typeof v) v = self.cast(v)
|
| 157 |
+
if (v) return v.toUpperCase();
|
| 158 |
+
return v;
|
| 159 |
+
});
|
| 160 |
+
};
|
| 161 |
+
|
| 162 |
+
/**
|
| 163 |
+
* Adds a trim setter.
|
| 164 |
+
*
|
| 165 |
+
* The string value will be trimmed when set.
|
| 166 |
+
*
|
| 167 |
+
* ####Example:
|
| 168 |
+
*
|
| 169 |
+
* var s = new Schema({ name: { type: String, trim: true }})
|
| 170 |
+
* var M = db.model('M', s)
|
| 171 |
+
* var string = ' some name '
|
| 172 |
+
* console.log(string.length) // 11
|
| 173 |
+
* var m = new M({ name: string })
|
| 174 |
+
* console.log(m.name.length) // 9
|
| 175 |
+
*
|
| 176 |
+
* @api public
|
| 177 |
+
* @return {SchemaType} this
|
| 178 |
+
*/
|
| 179 |
+
|
| 180 |
+
SchemaString.prototype.trim = function () {
|
| 181 |
+
return this.set(function (v, self) {
|
| 182 |
+
if ('string' != typeof v) v = self.cast(v)
|
| 183 |
+
if (v) return v.trim();
|
| 184 |
+
return v;
|
| 185 |
+
});
|
| 186 |
+
};
|
| 187 |
+
|
| 188 |
+
/**
|
| 189 |
+
* Sets a minimum length validator.
|
| 190 |
+
*
|
| 191 |
+
* ####Example:
|
| 192 |
+
*
|
| 193 |
+
* var schema = new Schema({ postalCode: { type: String, minlength: 5 })
|
| 194 |
+
* var Address = db.model('Address', schema)
|
| 195 |
+
* var address = new Address({ postalCode: '9512' })
|
| 196 |
+
* address.save(function (err) {
|
| 197 |
+
* console.error(err) // validator error
|
| 198 |
+
* address.postalCode = '95125';
|
| 199 |
+
* address.save() // success
|
| 200 |
+
* })
|
| 201 |
+
*
|
| 202 |
+
* // custom error messages
|
| 203 |
+
* // We can also use the special {MINLENGTH} token which will be replaced with the invalid value
|
| 204 |
+
* var minlength = [10, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum length ({MINLENGTH}).'];
|
| 205 |
+
* var schema = new Schema({ postalCode: { type: String, minlength: minlength })
|
| 206 |
+
* var Address = mongoose.model('Address', schema);
|
| 207 |
+
* var address = new Address({ postalCode: '9512' });
|
| 208 |
+
* s.validate(function (err) {
|
| 209 |
+
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512`) is shorter than the minimum length (5).
|
| 210 |
+
* })
|
| 211 |
+
*
|
| 212 |
+
* @param {Number} value minimum string length
|
| 213 |
+
* @param {String} [message] optional custom error message
|
| 214 |
+
* @return {SchemaType} this
|
| 215 |
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
| 216 |
+
* @api public
|
| 217 |
+
*/
|
| 218 |
+
|
| 219 |
+
SchemaString.prototype.minlength = function (value, message) {
|
| 220 |
+
if (this.minlengthValidator) {
|
| 221 |
+
this.validators = this.validators.filter(function (v) {
|
| 222 |
+
return v.validator != this.minlengthValidator;
|
| 223 |
+
}, this);
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
if (null != value) {
|
| 227 |
+
var msg = message || errorMessages.String.minlength;
|
| 228 |
+
msg = msg.replace(/{MINLENGTH}/, value);
|
| 229 |
+
this.validators.push({
|
| 230 |
+
validator: this.minlengthValidator = function (v) {
|
| 231 |
+
return v === null || v.length >= value;
|
| 232 |
+
},
|
| 233 |
+
message: msg,
|
| 234 |
+
type: 'minlength'
|
| 235 |
+
});
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
return this;
|
| 239 |
+
};
|
| 240 |
+
|
| 241 |
+
/**
|
| 242 |
+
* Sets a maximum length validator.
|
| 243 |
+
*
|
| 244 |
+
* ####Example:
|
| 245 |
+
*
|
| 246 |
+
* var schema = new Schema({ postalCode: { type: String, maxlength: 9 })
|
| 247 |
+
* var Address = db.model('Address', schema)
|
| 248 |
+
* var address = new Address({ postalCode: '9512512345' })
|
| 249 |
+
* address.save(function (err) {
|
| 250 |
+
* console.error(err) // validator error
|
| 251 |
+
* address.postalCode = '95125';
|
| 252 |
+
* address.save() // success
|
| 253 |
+
* })
|
| 254 |
+
*
|
| 255 |
+
* // custom error messages
|
| 256 |
+
* // We can also use the special {MAXLENGTH} token which will be replaced with the invalid value
|
| 257 |
+
* var maxlength = [10, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
|
| 258 |
+
* var schema = new Schema({ postalCode: { type: String, maxlength: maxlength })
|
| 259 |
+
* var Address = mongoose.model('Address', schema);
|
| 260 |
+
* var address = new Address({ postalCode: '9512512345' });
|
| 261 |
+
* address.validate(function (err) {
|
| 262 |
+
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512512345`) exceeds the maximum allowed length (10).
|
| 263 |
+
* })
|
| 264 |
+
*
|
| 265 |
+
* @param {Number} value maximum string length
|
| 266 |
+
* @param {String} [message] optional custom error message
|
| 267 |
+
* @return {SchemaType} this
|
| 268 |
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
| 269 |
+
* @api public
|
| 270 |
+
*/
|
| 271 |
+
|
| 272 |
+
SchemaString.prototype.maxlength = function (value, message) {
|
| 273 |
+
if (this.maxlengthValidator) {
|
| 274 |
+
this.validators = this.validators.filter(function(v){
|
| 275 |
+
return v.validator != this.maxlengthValidator;
|
| 276 |
+
}, this);
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
if (null != value) {
|
| 280 |
+
var msg = message || errorMessages.String.maxlength;
|
| 281 |
+
msg = msg.replace(/{MAXLENGTH}/, value);
|
| 282 |
+
this.validators.push({
|
| 283 |
+
validator: this.maxlengthValidator = function(v) {
|
| 284 |
+
return v === null || v.length <= value;
|
| 285 |
+
},
|
| 286 |
+
message: msg,
|
| 287 |
+
type: 'maxlength'
|
| 288 |
+
});
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
return this;
|
| 292 |
+
};
|
| 293 |
+
|
| 294 |
+
/**
|
| 295 |
+
* Sets a regexp validator.
|
| 296 |
+
*
|
| 297 |
+
* Any value that does not pass `regExp`.test(val) will fail validation.
|
| 298 |
+
*
|
| 299 |
+
* ####Example:
|
| 300 |
+
*
|
| 301 |
+
* var s = new Schema({ name: { type: String, match: /^a/ }})
|
| 302 |
+
* var M = db.model('M', s)
|
| 303 |
+
* var m = new M({ name: 'I am invalid' })
|
| 304 |
+
* m.validate(function (err) {
|
| 305 |
+
* console.error(String(err)) // "ValidationError: Path `name` is invalid (I am invalid)."
|
| 306 |
+
* m.name = 'apples'
|
| 307 |
+
* m.validate(function (err) {
|
| 308 |
+
* assert.ok(err) // success
|
| 309 |
+
* })
|
| 310 |
+
* })
|
| 311 |
+
*
|
| 312 |
+
* // using a custom error message
|
| 313 |
+
* var match = [ /\.html$/, "That file doesn't end in .html ({VALUE})" ];
|
| 314 |
+
* var s = new Schema({ file: { type: String, match: match }})
|
| 315 |
+
* var M = db.model('M', s);
|
| 316 |
+
* var m = new M({ file: 'invalid' });
|
| 317 |
+
* m.validate(function (err) {
|
| 318 |
+
* console.log(String(err)) // "ValidationError: That file doesn't end in .html (invalid)"
|
| 319 |
+
* })
|
| 320 |
+
*
|
| 321 |
+
* Empty strings, `undefined`, and `null` values always pass the match validator. If you require these values, enable the `required` validator also.
|
| 322 |
+
*
|
| 323 |
+
* var s = new Schema({ name: { type: String, match: /^a/, required: true }})
|
| 324 |
+
*
|
| 325 |
+
* @param {RegExp} regExp regular expression to test against
|
| 326 |
+
* @param {String} [message] optional custom error message
|
| 327 |
+
* @return {SchemaType} this
|
| 328 |
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
| 329 |
+
* @api public
|
| 330 |
+
*/
|
| 331 |
+
|
| 332 |
+
SchemaString.prototype.match = function match (regExp, message) {
|
| 333 |
+
// yes, we allow multiple match validators
|
| 334 |
+
|
| 335 |
+
var msg = message || errorMessages.String.match;
|
| 336 |
+
|
| 337 |
+
var matchValidator = function(v) {
|
| 338 |
+
var ret = ((null != v && '' !== v)
|
| 339 |
+
? regExp.test(v)
|
| 340 |
+
: true);
|
| 341 |
+
return ret;
|
| 342 |
+
};
|
| 343 |
+
|
| 344 |
+
this.validators.push({ validator: matchValidator, message: msg, type: 'regexp' });
|
| 345 |
+
return this;
|
| 346 |
+
};
|
| 347 |
+
|
| 348 |
+
/**
|
| 349 |
+
* Check required
|
| 350 |
+
*
|
| 351 |
+
* @param {String|null|undefined} value
|
| 352 |
+
* @api private
|
| 353 |
+
*/
|
| 354 |
+
|
| 355 |
+
SchemaString.prototype.checkRequired = function checkRequired (value, doc) {
|
| 356 |
+
if (SchemaType._isRef(this, value, doc, true)) {
|
| 357 |
+
return null != value;
|
| 358 |
+
} else {
|
| 359 |
+
return (value instanceof String || typeof value == 'string') && value.length;
|
| 360 |
+
}
|
| 361 |
+
};
|
| 362 |
+
|
| 363 |
+
/**
|
| 364 |
+
* Casts to String
|
| 365 |
+
*
|
| 366 |
+
* @api private
|
| 367 |
+
*/
|
| 368 |
+
|
| 369 |
+
SchemaString.prototype.cast = function (value, doc, init) {
|
| 370 |
+
if (SchemaType._isRef(this, value, doc, init)) {
|
| 371 |
+
// wait! we may need to cast this to a document
|
| 372 |
+
|
| 373 |
+
if (null == value) {
|
| 374 |
+
return value;
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
// lazy load
|
| 378 |
+
Document || (Document = require('./../document'));
|
| 379 |
+
|
| 380 |
+
if (value instanceof Document) {
|
| 381 |
+
value.$__.wasPopulated = true;
|
| 382 |
+
return value;
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
// setting a populated path
|
| 386 |
+
if ('string' == typeof value) {
|
| 387 |
+
return value;
|
| 388 |
+
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
| 389 |
+
throw new CastError('string', value, this.path);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
// Handle the case where user directly sets a populated
|
| 393 |
+
// path to a plain object; cast to the Model used in
|
| 394 |
+
// the population query.
|
| 395 |
+
var path = doc.$__fullPath(this.path);
|
| 396 |
+
var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
| 397 |
+
var pop = owner.populated(path, true);
|
| 398 |
+
var ret = new pop.options.model(value);
|
| 399 |
+
ret.$__.wasPopulated = true;
|
| 400 |
+
return ret;
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
// If null or undefined
|
| 404 |
+
if (value == null) {
|
| 405 |
+
return value;
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
if ('undefined' !== typeof value) {
|
| 409 |
+
// handle documents being passed
|
| 410 |
+
if (value._id && 'string' == typeof value._id) {
|
| 411 |
+
return value._id;
|
| 412 |
+
}
|
| 413 |
+
if (value.toString) {
|
| 414 |
+
return value.toString();
|
| 415 |
+
}
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
throw new CastError('string', value, this.path);
|
| 420 |
+
};
|
| 421 |
+
|
| 422 |
+
/*!
|
| 423 |
+
* ignore
|
| 424 |
+
*/
|
| 425 |
+
|
| 426 |
+
function handleSingle (val) {
|
| 427 |
+
return this.castForQuery(val);
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
function handleArray (val) {
|
| 431 |
+
var self = this;
|
| 432 |
+
return val.map(function (m) {
|
| 433 |
+
return self.castForQuery(m);
|
| 434 |
+
});
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
SchemaString.prototype.$conditionalHandlers =
|
| 438 |
+
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
| 439 |
+
'$all': handleArray,
|
| 440 |
+
'$gt' : handleSingle,
|
| 441 |
+
'$gte': handleSingle,
|
| 442 |
+
'$in' : handleArray,
|
| 443 |
+
'$lt' : handleSingle,
|
| 444 |
+
'$lte': handleSingle,
|
| 445 |
+
'$ne' : handleSingle,
|
| 446 |
+
'$nin': handleArray,
|
| 447 |
+
'$options': handleSingle,
|
| 448 |
+
'$regex': handleSingle
|
| 449 |
+
});
|
| 450 |
+
|
| 451 |
+
/**
|
| 452 |
+
* Casts contents for queries.
|
| 453 |
+
*
|
| 454 |
+
* @param {String} $conditional
|
| 455 |
+
* @param {any} [val]
|
| 456 |
+
* @api private
|
| 457 |
+
*/
|
| 458 |
+
|
| 459 |
+
SchemaString.prototype.castForQuery = function ($conditional, val) {
|
| 460 |
+
var handler;
|
| 461 |
+
if (arguments.length === 2) {
|
| 462 |
+
handler = this.$conditionalHandlers[$conditional];
|
| 463 |
+
if (!handler)
|
| 464 |
+
throw new Error("Can't use " + $conditional + " with String.");
|
| 465 |
+
return handler.call(this, val);
|
| 466 |
+
} else {
|
| 467 |
+
val = $conditional;
|
| 468 |
+
if (val instanceof RegExp) return val;
|
| 469 |
+
return this.cast(val);
|
| 470 |
+
}
|
| 471 |
+
};
|
| 472 |
+
|
| 473 |
+
/*!
|
| 474 |
+
* Module exports.
|
| 475 |
+
*/
|
| 476 |
+
|
| 477 |
+
module.exports = SchemaString;
|
5485-master/demos/node_modules/mongoose/lib/types/embedded.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*!
|
| 2 |
+
* Module dependencies.
|
| 3 |
+
*/
|
| 4 |
+
|
| 5 |
+
var Document = require('../document_provider')();
|
| 6 |
+
var inspect = require('util').inspect;
|
| 7 |
+
var Promise = require('../promise');
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* EmbeddedDocument constructor.
|
| 11 |
+
*
|
| 12 |
+
* @param {Object} obj js object returned from the db
|
| 13 |
+
* @param {MongooseDocumentArray} parentArr the parent array of this document
|
| 14 |
+
* @param {Boolean} skipId
|
| 15 |
+
* @inherits Document
|
| 16 |
+
* @api private
|
| 17 |
+
*/
|
| 18 |
+
|
| 19 |
+
function EmbeddedDocument (obj, parentArr, skipId, fields, index) {
|
| 20 |
+
if (parentArr) {
|
| 21 |
+
this.__parentArray = parentArr;
|
| 22 |
+
this.__parent = parentArr._parent;
|
| 23 |
+
} else {
|
| 24 |
+
this.__parentArray = undefined;
|
| 25 |
+
this.__parent = undefined;
|
| 26 |
+
}
|
| 27 |
+
this.__index = index;
|
| 28 |
+
|
| 29 |
+
Document.call(this, obj, fields, skipId);
|
| 30 |
+
|
| 31 |
+
var self = this;
|
| 32 |
+
this.on('isNew', function (val) {
|
| 33 |
+
self.isNew = val;
|
| 34 |
+
});
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
/*!
|
| 38 |
+
* Inherit from Document
|
| 39 |
+
*/
|
| 40 |
+
EmbeddedDocument.prototype = Object.create( Document.prototype );
|
| 41 |
+
EmbeddedDocument.prototype.constructor = EmbeddedDocument;
|
| 42 |
+
|
| 43 |
+
/**
|
| 44 |
+
* Marks the embedded doc modified.
|
| 45 |
+
*
|
| 46 |
+
* ####Example:
|
| 47 |
+
*
|
| 48 |
+
* var doc = blogpost.comments.id(hexstring);
|
| 49 |
+
* doc.mixed.type = 'changed';
|
| 50 |
+
* doc.markModified('mixed.type');
|
| 51 |
+
*
|
| 52 |
+
* @param {String} path the path which changed
|
| 53 |
+
* @api public
|
| 54 |
+
* @receiver EmbeddedDocument
|
| 55 |
+
*/
|
| 56 |
+
|
| 57 |
+
EmbeddedDocument.prototype.markModified = function (path) {
|
| 58 |
+
if (!this.__parentArray) return;
|
| 59 |
+
|
| 60 |
+
this.$__.activePaths.modify(path);
|
| 61 |
+
if (this.isNew) {
|
| 62 |
+
// Mark the WHOLE parent array as modified
|
| 63 |
+
// if this is a new document (i.e., we are initializing
|
| 64 |
+
// a document),
|
| 65 |
+
this.__parentArray._markModified();
|
| 66 |
+
} else {
|
| 67 |
+
this.__parentArray._markModified(this, path);
|
| 68 |
+
}
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
/**
|
| 72 |
+
* Used as a stub for [hooks.js](https://github.com/bnoguchi/hooks-js/tree/31ec571cef0332e21121ee7157e0cf9728572cc3)
|
| 73 |
+
*
|
| 74 |
+
* ####NOTE:
|
| 75 |
+
*
|
| 76 |
+
* _This is a no-op. Does not actually save the doc to the db._
|
| 77 |
+
*
|
| 78 |
+
* @param {Function} [fn]
|
| 79 |
+
* @return {Promise} resolved Promise
|
| 80 |
+
* @api private
|
| 81 |
+
*/
|
| 82 |
+
|
| 83 |
+
EmbeddedDocument.prototype.save = function (fn) {
|
| 84 |
+
var promise = new Promise(fn);
|
| 85 |
+
promise.fulfill();
|
| 86 |
+
return promise;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/**
|
| 90 |
+
* Removes the subdocument from its parent array.
|
| 91 |
+
*
|
| 92 |
+
* @param {Function} [fn]
|
| 93 |
+
* @api public
|
| 94 |
+
*/
|
| 95 |
+
|
| 96 |
+
EmbeddedDocument.prototype.remove = function (fn) {
|
| 97 |
+
if (!this.__parentArray) return this;
|
| 98 |
+
|
| 99 |
+
var _id;
|
| 100 |
+
if (!this.willRemove) {
|
| 101 |
+
_id = this._doc._id;
|
| 102 |
+
if (!_id) {
|
| 103 |
+
throw new Error('For your own good, Mongoose does not know ' +
|
| 104 |
+
'how to remove an EmbeddedDocument that has no _id');
|
| 105 |
+
}
|
| 106 |
+
this.__parentArray.pull({ _id: _id });
|
| 107 |
+
this.willRemove = true;
|
| 108 |
+
registerRemoveListener(this);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
if (fn)
|
| 112 |
+
fn(null);
|
| 113 |
+
|
| 114 |
+
return this;
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
/*!
|
| 118 |
+
* Registers remove event listeners for triggering
|
| 119 |
+
* on subdocuments.
|
| 120 |
+
*
|
| 121 |
+
* @param {EmbeddedDocument} sub
|
| 122 |
+
* @api private
|
| 123 |
+
*/
|
| 124 |
+
|
| 125 |
+
function registerRemoveListener (sub) {
|
| 126 |
+
var owner = sub.ownerDocument();
|
| 127 |
+
|
| 128 |
+
owner.on('save', emitRemove);
|
| 129 |
+
owner.on('remove', emitRemove);
|
| 130 |
+
|
| 131 |
+
function emitRemove () {
|
| 132 |
+
owner.removeListener('save', emitRemove);
|
| 133 |
+
owner.removeListener('remove', emitRemove);
|
| 134 |
+
sub.emit('remove', sub);
|
| 135 |
+
owner = sub = emitRemove = null;
|
| 136 |
+
};
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
/**
|
| 140 |
+
* Override #update method of parent documents.
|
| 141 |
+
* @api private
|
| 142 |
+
*/
|
| 143 |
+
|
| 144 |
+
EmbeddedDocument.prototype.update = function () {
|
| 145 |
+
throw new Error('The #update method is not available on EmbeddedDocuments');
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/**
|
| 149 |
+
* Helper for console.log
|
| 150 |
+
*
|
| 151 |
+
* @api public
|
| 152 |
+
*/
|
| 153 |
+
|
| 154 |
+
EmbeddedDocument.prototype.inspect = function () {
|
| 155 |
+
return inspect(this.toObject());
|
| 156 |
+
};
|
| 157 |
+
|
| 158 |
+
/**
|
| 159 |
+
* Marks a path as invalid, causing validation to fail.
|
| 160 |
+
*
|
| 161 |
+
* @param {String} path the field to invalidate
|
| 162 |
+
* @param {String|Error} err error which states the reason `path` was invalid
|
| 163 |
+
* @return {Boolean}
|
| 164 |
+
* @api public
|
| 165 |
+
*/
|
| 166 |
+
|
| 167 |
+
EmbeddedDocument.prototype.invalidate = function (path, err, val, first) {
|
| 168 |
+
if (!this.__parent) {
|
| 169 |
+
var msg = 'Unable to invalidate a subdocument that has not been added to an array.'
|
| 170 |
+
throw new Error(msg);
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
var index = this.__index;
|
| 174 |
+
if (typeof index !== 'undefined') {
|
| 175 |
+
var parentPath = this.__parentArray._path;
|
| 176 |
+
var fullPath = [parentPath, index, path].join('.');
|
| 177 |
+
this.__parent.invalidate(fullPath, err, val);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
if (first) {
|
| 181 |
+
this.$__.validationError = this.ownerDocument().$__.validationError;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
return true;
|
| 185 |
+
};
|
| 186 |
+
|
| 187 |
+
/**
|
| 188 |
+
* Marks a path as valid, removing existing validation errors.
|
| 189 |
+
*
|
| 190 |
+
* @param {String} path the field to mark as valid
|
| 191 |
+
* @api private
|
| 192 |
+
* @method $markValid
|
| 193 |
+
* @receiver EmbeddedDocument
|
| 194 |
+
*/
|
| 195 |
+
|
| 196 |
+
EmbeddedDocument.prototype.$markValid = function(path) {
|
| 197 |
+
if (!this.__parent) {
|
| 198 |
+
return;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
var index = this.__index;
|
| 202 |
+
if (typeof index !== 'undefined') {
|
| 203 |
+
var parentPath = this.__parentArray._path;
|
| 204 |
+
var fullPath = [parentPath, index, path].join('.');
|
| 205 |
+
this.__parent.$markValid(fullPath);
|
| 206 |
+
}
|
| 207 |
+
};
|
| 208 |
+
|
| 209 |
+
/**
|
| 210 |
+
* Checks if a path is invalid
|
| 211 |
+
*
|
| 212 |
+
* @param {String} path the field to check
|
| 213 |
+
* @api private
|
| 214 |
+
* @method $isValid
|
| 215 |
+
* @receiver EmbeddedDocument
|
| 216 |
+
*/
|
| 217 |
+
|
| 218 |
+
EmbeddedDocument.prototype.$isValid = function(path) {
|
| 219 |
+
var index = this.__index;
|
| 220 |
+
if (typeof index !== 'undefined') {
|
| 221 |
+
var parentPath = this.__parentArray._path;
|
| 222 |
+
var fullPath = [parentPath, index, path].join('.');
|
| 223 |
+
|
| 224 |
+
return !this.__parent.$__.validationError ||
|
| 225 |
+
!this.__parent.$__.validationError.errors[path];
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
return true;
|
| 229 |
+
};
|
| 230 |
+
|
| 231 |
+
/**
|
| 232 |
+
* Returns the top level document of this sub-document.
|
| 233 |
+
*
|
| 234 |
+
* @return {Document}
|
| 235 |
+
*/
|
| 236 |
+
|
| 237 |
+
EmbeddedDocument.prototype.ownerDocument = function () {
|
| 238 |
+
if (this.$__.ownerDocument) {
|
| 239 |
+
return this.$__.ownerDocument;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
var parent = this.__parent;
|
| 243 |
+
if (!parent) return this;
|
| 244 |
+
|
| 245 |
+
while (parent.__parent) {
|
| 246 |
+
parent = parent.__parent;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
return this.$__.ownerDocument = parent;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
/**
|
| 253 |
+
* Returns the full path to this document. If optional `path` is passed, it is appended to the full path.
|
| 254 |
+
*
|
| 255 |
+
* @param {String} [path]
|
| 256 |
+
* @return {String}
|
| 257 |
+
* @api private
|
| 258 |
+
* @method $__fullPath
|
| 259 |
+
* @memberOf EmbeddedDocument
|
| 260 |
+
*/
|
| 261 |
+
|
| 262 |
+
EmbeddedDocument.prototype.$__fullPath = function (path) {
|
| 263 |
+
if (!this.$__.fullPath) {
|
| 264 |
+
var parent = this;
|
| 265 |
+
if (!parent.__parent) return path;
|
| 266 |
+
|
| 267 |
+
var paths = [];
|
| 268 |
+
while (parent.__parent) {
|
| 269 |
+
paths.unshift(parent.__parentArray._path);
|
| 270 |
+
parent = parent.__parent;
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
this.$__.fullPath = paths.join('.');
|
| 274 |
+
|
| 275 |
+
if (!this.$__.ownerDocument) {
|
| 276 |
+
// optimization
|
| 277 |
+
this.$__.ownerDocument = parent;
|
| 278 |
+
}
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
return path
|
| 282 |
+
? this.$__.fullPath + '.' + path
|
| 283 |
+
: this.$__.fullPath;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
/**
|
| 287 |
+
* Returns this sub-documents parent document.
|
| 288 |
+
*
|
| 289 |
+
* @api public
|
| 290 |
+
*/
|
| 291 |
+
|
| 292 |
+
EmbeddedDocument.prototype.parent = function () {
|
| 293 |
+
return this.__parent;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
/**
|
| 297 |
+
* Returns this sub-documents parent array.
|
| 298 |
+
*
|
| 299 |
+
* @api public
|
| 300 |
+
*/
|
| 301 |
+
|
| 302 |
+
EmbeddedDocument.prototype.parentArray = function () {
|
| 303 |
+
return this.__parentArray;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
/*!
|
| 307 |
+
* Module exports.
|
| 308 |
+
*/
|
| 309 |
+
|
| 310 |
+
module.exports = EmbeddedDocument;
|
5485-master/demos/node_modules/mongoose/lib/types/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*!
|
| 3 |
+
* Module exports.
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
exports.Array = require('./array');
|
| 7 |
+
exports.Buffer = require('./buffer');
|
| 8 |
+
|
| 9 |
+
exports.Document = // @deprecate
|
| 10 |
+
exports.Embedded = require('./embedded');
|
| 11 |
+
|
| 12 |
+
exports.DocumentArray = require('./documentarray');
|
| 13 |
+
exports.ObjectId = require('./objectid');
|
5485-master/demos/node_modules/mongoose/lib/types/objectid.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*!
|
| 3 |
+
* Access driver.
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
var driver = global.MONGOOSE_DRIVER_PATH || '../drivers/node-mongodb-native';
|
| 7 |
+
|
| 8 |
+
/**
|
| 9 |
+
* ObjectId type constructor
|
| 10 |
+
*
|
| 11 |
+
* ####Example
|
| 12 |
+
*
|
| 13 |
+
* var id = new mongoose.Types.ObjectId;
|
| 14 |
+
*
|
| 15 |
+
* @constructor ObjectId
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
var ObjectId = require('../drivers/node-mongodb-native/objectid');
|
| 19 |
+
module.exports = ObjectId;
|
| 20 |
+
|
5485-master/demos/node_modules/mongoose/node_modules/async/.travis.yml
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
language: node_js
|
| 2 |
+
node_js:
|
| 3 |
+
- "0.10"
|
5485-master/demos/node_modules/mongoose/node_modules/async/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (c) 2010-2014 Caolan McMahon
|
| 2 |
+
|
| 3 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 4 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 5 |
+
in the Software without restriction, including without limitation the rights
|
| 6 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 7 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 8 |
+
furnished to do so, subject to the following conditions:
|
| 9 |
+
|
| 10 |
+
The above copyright notice and this permission notice shall be included in
|
| 11 |
+
all copies or substantial portions of the Software.
|
| 12 |
+
|
| 13 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 14 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 15 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 16 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 17 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 18 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 19 |
+
THE SOFTWARE.
|
5485-master/demos/node_modules/mongoose/node_modules/async/README.md
ADDED
|
@@ -0,0 +1,1646 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Async.js
|
| 2 |
+
|
| 3 |
+
[](https://travis-ci.org/caolan/async)
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
Async is a utility module which provides straight-forward, powerful functions
|
| 7 |
+
for working with asynchronous JavaScript. Although originally designed for
|
| 8 |
+
use with [Node.js](http://nodejs.org), it can also be used directly in the
|
| 9 |
+
browser. Also supports [component](https://github.com/component/component).
|
| 10 |
+
|
| 11 |
+
Async provides around 20 functions that include the usual 'functional'
|
| 12 |
+
suspects (`map`, `reduce`, `filter`, `each`…) as well as some common patterns
|
| 13 |
+
for asynchronous control flow (`parallel`, `series`, `waterfall`…). All these
|
| 14 |
+
functions assume you follow the Node.js convention of providing a single
|
| 15 |
+
callback as the last argument of your `async` function.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
## Quick Examples
|
| 19 |
+
|
| 20 |
+
```javascript
|
| 21 |
+
async.map(['file1','file2','file3'], fs.stat, function(err, results){
|
| 22 |
+
// results is now an array of stats for each file
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
async.filter(['file1','file2','file3'], fs.exists, function(results){
|
| 26 |
+
// results now equals an array of the existing files
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
async.parallel([
|
| 30 |
+
function(){ ... },
|
| 31 |
+
function(){ ... }
|
| 32 |
+
], callback);
|
| 33 |
+
|
| 34 |
+
async.series([
|
| 35 |
+
function(){ ... },
|
| 36 |
+
function(){ ... }
|
| 37 |
+
]);
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
There are many more functions available so take a look at the docs below for a
|
| 41 |
+
full list. This module aims to be comprehensive, so if you feel anything is
|
| 42 |
+
missing please create a GitHub issue for it.
|
| 43 |
+
|
| 44 |
+
## Common Pitfalls
|
| 45 |
+
|
| 46 |
+
### Binding a context to an iterator
|
| 47 |
+
|
| 48 |
+
This section is really about `bind`, not about `async`. If you are wondering how to
|
| 49 |
+
make `async` execute your iterators in a given context, or are confused as to why
|
| 50 |
+
a method of another library isn't working as an iterator, study this example:
|
| 51 |
+
|
| 52 |
+
```js
|
| 53 |
+
// Here is a simple object with an (unnecessarily roundabout) squaring method
|
| 54 |
+
var AsyncSquaringLibrary = {
|
| 55 |
+
squareExponent: 2,
|
| 56 |
+
square: function(number, callback){
|
| 57 |
+
var result = Math.pow(number, this.squareExponent);
|
| 58 |
+
setTimeout(function(){
|
| 59 |
+
callback(null, result);
|
| 60 |
+
}, 200);
|
| 61 |
+
}
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){
|
| 65 |
+
// result is [NaN, NaN, NaN]
|
| 66 |
+
// This fails because the `this.squareExponent` expression in the square
|
| 67 |
+
// function is not evaluated in the context of AsyncSquaringLibrary, and is
|
| 68 |
+
// therefore undefined.
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), function(err, result){
|
| 72 |
+
// result is [1, 4, 9]
|
| 73 |
+
// With the help of bind we can attach a context to the iterator before
|
| 74 |
+
// passing it to async. Now the square function will be executed in its
|
| 75 |
+
// 'home' AsyncSquaringLibrary context and the value of `this.squareExponent`
|
| 76 |
+
// will be as expected.
|
| 77 |
+
});
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
## Download
|
| 81 |
+
|
| 82 |
+
The source is available for download from
|
| 83 |
+
[GitHub](http://github.com/caolan/async).
|
| 84 |
+
Alternatively, you can install using Node Package Manager (`npm`):
|
| 85 |
+
|
| 86 |
+
npm install async
|
| 87 |
+
|
| 88 |
+
__Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 29.6kb Uncompressed
|
| 89 |
+
|
| 90 |
+
## In the Browser
|
| 91 |
+
|
| 92 |
+
So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5.
|
| 93 |
+
|
| 94 |
+
Usage:
|
| 95 |
+
|
| 96 |
+
```html
|
| 97 |
+
<script type="text/javascript" src="async.js"></script>
|
| 98 |
+
<script type="text/javascript">
|
| 99 |
+
|
| 100 |
+
async.map(data, asyncProcess, function(err, results){
|
| 101 |
+
alert(results);
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
</script>
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
## Documentation
|
| 108 |
+
|
| 109 |
+
### Collections
|
| 110 |
+
|
| 111 |
+
* [`each`](#each)
|
| 112 |
+
* [`eachSeries`](#eachSeries)
|
| 113 |
+
* [`eachLimit`](#eachLimit)
|
| 114 |
+
* [`map`](#map)
|
| 115 |
+
* [`mapSeries`](#mapSeries)
|
| 116 |
+
* [`mapLimit`](#mapLimit)
|
| 117 |
+
* [`filter`](#filter)
|
| 118 |
+
* [`filterSeries`](#filterSeries)
|
| 119 |
+
* [`reject`](#reject)
|
| 120 |
+
* [`rejectSeries`](#rejectSeries)
|
| 121 |
+
* [`reduce`](#reduce)
|
| 122 |
+
* [`reduceRight`](#reduceRight)
|
| 123 |
+
* [`detect`](#detect)
|
| 124 |
+
* [`detectSeries`](#detectSeries)
|
| 125 |
+
* [`sortBy`](#sortBy)
|
| 126 |
+
* [`some`](#some)
|
| 127 |
+
* [`every`](#every)
|
| 128 |
+
* [`concat`](#concat)
|
| 129 |
+
* [`concatSeries`](#concatSeries)
|
| 130 |
+
|
| 131 |
+
### Control Flow
|
| 132 |
+
|
| 133 |
+
* [`series`](#seriestasks-callback)
|
| 134 |
+
* [`parallel`](#parallel)
|
| 135 |
+
* [`parallelLimit`](#parallellimittasks-limit-callback)
|
| 136 |
+
* [`whilst`](#whilst)
|
| 137 |
+
* [`doWhilst`](#doWhilst)
|
| 138 |
+
* [`until`](#until)
|
| 139 |
+
* [`doUntil`](#doUntil)
|
| 140 |
+
* [`forever`](#forever)
|
| 141 |
+
* [`waterfall`](#waterfall)
|
| 142 |
+
* [`compose`](#compose)
|
| 143 |
+
* [`seq`](#seq)
|
| 144 |
+
* [`applyEach`](#applyEach)
|
| 145 |
+
* [`applyEachSeries`](#applyEachSeries)
|
| 146 |
+
* [`queue`](#queue)
|
| 147 |
+
* [`priorityQueue`](#priorityQueue)
|
| 148 |
+
* [`cargo`](#cargo)
|
| 149 |
+
* [`auto`](#auto)
|
| 150 |
+
* [`retry`](#retry)
|
| 151 |
+
* [`iterator`](#iterator)
|
| 152 |
+
* [`apply`](#apply)
|
| 153 |
+
* [`nextTick`](#nextTick)
|
| 154 |
+
* [`times`](#times)
|
| 155 |
+
* [`timesSeries`](#timesSeries)
|
| 156 |
+
|
| 157 |
+
### Utils
|
| 158 |
+
|
| 159 |
+
* [`memoize`](#memoize)
|
| 160 |
+
* [`unmemoize`](#unmemoize)
|
| 161 |
+
* [`log`](#log)
|
| 162 |
+
* [`dir`](#dir)
|
| 163 |
+
* [`noConflict`](#noConflict)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
## Collections
|
| 167 |
+
|
| 168 |
+
<a name="forEach" />
|
| 169 |
+
<a name="each" />
|
| 170 |
+
### each(arr, iterator, callback)
|
| 171 |
+
|
| 172 |
+
Applies the function `iterator` to each item in `arr`, in parallel.
|
| 173 |
+
The `iterator` is called with an item from the list, and a callback for when it
|
| 174 |
+
has finished. If the `iterator` passes an error to its `callback`, the main
|
| 175 |
+
`callback` (for the `each` function) is immediately called with the error.
|
| 176 |
+
|
| 177 |
+
Note, that since this function applies `iterator` to each item in parallel,
|
| 178 |
+
there is no guarantee that the iterator functions will complete in order.
|
| 179 |
+
|
| 180 |
+
__Arguments__
|
| 181 |
+
|
| 182 |
+
* `arr` - An array to iterate over.
|
| 183 |
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
| 184 |
+
The iterator is passed a `callback(err)` which must be called once it has
|
| 185 |
+
completed. If no error has occured, the `callback` should be run without
|
| 186 |
+
arguments or with an explicit `null` argument.
|
| 187 |
+
* `callback(err)` - A callback which is called when all `iterator` functions
|
| 188 |
+
have finished, or an error occurs.
|
| 189 |
+
|
| 190 |
+
__Examples__
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
```js
|
| 194 |
+
// assuming openFiles is an array of file names and saveFile is a function
|
| 195 |
+
// to save the modified contents of that file:
|
| 196 |
+
|
| 197 |
+
async.each(openFiles, saveFile, function(err){
|
| 198 |
+
// if any of the saves produced an error, err would equal that error
|
| 199 |
+
});
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
```js
|
| 203 |
+
// assuming openFiles is an array of file names
|
| 204 |
+
|
| 205 |
+
async.each(openFiles, function( file, callback) {
|
| 206 |
+
|
| 207 |
+
// Perform operation on file here.
|
| 208 |
+
console.log('Processing file ' + file);
|
| 209 |
+
|
| 210 |
+
if( file.length > 32 ) {
|
| 211 |
+
console.log('This file name is too long');
|
| 212 |
+
callback('File name too long');
|
| 213 |
+
} else {
|
| 214 |
+
// Do work to process file here
|
| 215 |
+
console.log('File processed');
|
| 216 |
+
callback();
|
| 217 |
+
}
|
| 218 |
+
}, function(err){
|
| 219 |
+
// if any of the file processing produced an error, err would equal that error
|
| 220 |
+
if( err ) {
|
| 221 |
+
// One of the iterations produced an error.
|
| 222 |
+
// All processing will now stop.
|
| 223 |
+
console.log('A file failed to process');
|
| 224 |
+
} else {
|
| 225 |
+
console.log('All files have been processed successfully');
|
| 226 |
+
}
|
| 227 |
+
});
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
---------------------------------------
|
| 231 |
+
|
| 232 |
+
<a name="forEachSeries" />
|
| 233 |
+
<a name="eachSeries" />
|
| 234 |
+
### eachSeries(arr, iterator, callback)
|
| 235 |
+
|
| 236 |
+
The same as [`each`](#each), only `iterator` is applied to each item in `arr` in
|
| 237 |
+
series. The next `iterator` is only called once the current one has completed.
|
| 238 |
+
This means the `iterator` functions will complete in order.
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
---------------------------------------
|
| 242 |
+
|
| 243 |
+
<a name="forEachLimit" />
|
| 244 |
+
<a name="eachLimit" />
|
| 245 |
+
### eachLimit(arr, limit, iterator, callback)
|
| 246 |
+
|
| 247 |
+
The same as [`each`](#each), only no more than `limit` `iterator`s will be simultaneously
|
| 248 |
+
running at any time.
|
| 249 |
+
|
| 250 |
+
Note that the items in `arr` are not processed in batches, so there is no guarantee that
|
| 251 |
+
the first `limit` `iterator` functions will complete before any others are started.
|
| 252 |
+
|
| 253 |
+
__Arguments__
|
| 254 |
+
|
| 255 |
+
* `arr` - An array to iterate over.
|
| 256 |
+
* `limit` - The maximum number of `iterator`s to run at any time.
|
| 257 |
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
| 258 |
+
The iterator is passed a `callback(err)` which must be called once it has
|
| 259 |
+
completed. If no error has occured, the callback should be run without
|
| 260 |
+
arguments or with an explicit `null` argument.
|
| 261 |
+
* `callback(err)` - A callback which is called when all `iterator` functions
|
| 262 |
+
have finished, or an error occurs.
|
| 263 |
+
|
| 264 |
+
__Example__
|
| 265 |
+
|
| 266 |
+
```js
|
| 267 |
+
// Assume documents is an array of JSON objects and requestApi is a
|
| 268 |
+
// function that interacts with a rate-limited REST api.
|
| 269 |
+
|
| 270 |
+
async.eachLimit(documents, 20, requestApi, function(err){
|
| 271 |
+
// if any of the saves produced an error, err would equal that error
|
| 272 |
+
});
|
| 273 |
+
```
|
| 274 |
+
|
| 275 |
+
---------------------------------------
|
| 276 |
+
|
| 277 |
+
<a name="map" />
|
| 278 |
+
### map(arr, iterator, callback)
|
| 279 |
+
|
| 280 |
+
Produces a new array of values by mapping each value in `arr` through
|
| 281 |
+
the `iterator` function. The `iterator` is called with an item from `arr` and a
|
| 282 |
+
callback for when it has finished processing. Each of these callback takes 2 arguments:
|
| 283 |
+
an `error`, and the transformed item from `arr`. If `iterator` passes an error to this
|
| 284 |
+
callback, the main `callback` (for the `map` function) is immediately called with the error.
|
| 285 |
+
|
| 286 |
+
Note, that since this function applies the `iterator` to each item in parallel,
|
| 287 |
+
there is no guarantee that the `iterator` functions will complete in order.
|
| 288 |
+
However, the results array will be in the same order as the original `arr`.
|
| 289 |
+
|
| 290 |
+
__Arguments__
|
| 291 |
+
|
| 292 |
+
* `arr` - An array to iterate over.
|
| 293 |
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
| 294 |
+
The iterator is passed a `callback(err, transformed)` which must be called once
|
| 295 |
+
it has completed with an error (which can be `null`) and a transformed item.
|
| 296 |
+
* `callback(err, results)` - A callback which is called when all `iterator`
|
| 297 |
+
functions have finished, or an error occurs. Results is an array of the
|
| 298 |
+
transformed items from the `arr`.
|
| 299 |
+
|
| 300 |
+
__Example__
|
| 301 |
+
|
| 302 |
+
```js
|
| 303 |
+
async.map(['file1','file2','file3'], fs.stat, function(err, results){
|
| 304 |
+
// results is now an array of stats for each file
|
| 305 |
+
});
|
| 306 |
+
```
|
| 307 |
+
|
| 308 |
+
---------------------------------------
|
| 309 |
+
|
| 310 |
+
<a name="mapSeries" />
|
| 311 |
+
### mapSeries(arr, iterator, callback)
|
| 312 |
+
|
| 313 |
+
The same as [`map`](#map), only the `iterator` is applied to each item in `arr` in
|
| 314 |
+
series. The next `iterator` is only called once the current one has completed.
|
| 315 |
+
The results array will be in the same order as the original.
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
---------------------------------------
|
| 319 |
+
|
| 320 |
+
<a name="mapLimit" />
|
| 321 |
+
### mapLimit(arr, limit, iterator, callback)
|
| 322 |
+
|
| 323 |
+
The same as [`map`](#map), only no more than `limit` `iterator`s will be simultaneously
|
| 324 |
+
running at any time.
|
| 325 |
+
|
| 326 |
+
Note that the items are not processed in batches, so there is no guarantee that
|
| 327 |
+
the first `limit` `iterator` functions will complete before any others are started.
|
| 328 |
+
|
| 329 |
+
__Arguments__
|
| 330 |
+
|
| 331 |
+
* `arr` - An array to iterate over.
|
| 332 |
+
* `limit` - The maximum number of `iterator`s to run at any time.
|
| 333 |
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
| 334 |
+
The iterator is passed a `callback(err, transformed)` which must be called once
|
| 335 |
+
it has completed with an error (which can be `null`) and a transformed item.
|
| 336 |
+
* `callback(err, results)` - A callback which is called when all `iterator`
|
| 337 |
+
calls have finished, or an error occurs. The result is an array of the
|
| 338 |
+
transformed items from the original `arr`.
|
| 339 |
+
|
| 340 |
+
__Example__
|
| 341 |
+
|
| 342 |
+
```js
|
| 343 |
+
async.mapLimit(['file1','file2','file3'], 1, fs.stat, function(err, results){
|
| 344 |
+
// results is now an array of stats for each file
|
| 345 |
+
});
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
---------------------------------------
|
| 349 |
+
|
| 350 |
+
<a name="select" />
|
| 351 |
+
<a name="filter" />
|
| 352 |
+
### filter(arr, iterator, callback)
|
| 353 |
+
|
| 354 |
+
__Alias:__ `select`
|
| 355 |
+
|
| 356 |
+
Returns a new array of all the values in `arr` which pass an async truth test.
|
| 357 |
+
_The callback for each `iterator` call only accepts a single argument of `true` or
|
| 358 |
+
`false`; it does not accept an error argument first!_ This is in-line with the
|
| 359 |
+
way node libraries work with truth tests like `fs.exists`. This operation is
|
| 360 |
+
performed in parallel, but the results array will be in the same order as the
|
| 361 |
+
original.
|
| 362 |
+
|
| 363 |
+
__Arguments__
|
| 364 |
+
|
| 365 |
+
* `arr` - An array to iterate over.
|
| 366 |
+
* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
|
| 367 |
+
The `iterator` is passed a `callback(truthValue)`, which must be called with a
|
| 368 |
+
boolean argument once it has completed.
|
| 369 |
+
* `callback(results)` - A callback which is called after all the `iterator`
|
| 370 |
+
functions have finished.
|
| 371 |
+
|
| 372 |
+
__Example__
|
| 373 |
+
|
| 374 |
+
```js
|
| 375 |
+
async.filter(['file1','file2','file3'], fs.exists, function(results){
|
| 376 |
+
// results now equals an array of the existing files
|
| 377 |
+
});
|
| 378 |
+
```
|
| 379 |
+
|
| 380 |
+
---------------------------------------
|
| 381 |
+
|
| 382 |
+
<a name="selectSeries" />
|
| 383 |
+
<a name="filterSeries" />
|
| 384 |
+
### filterSeries(arr, iterator, callback)
|
| 385 |
+
|
| 386 |
+
__Alias:__ `selectSeries`
|
| 387 |
+
|
| 388 |
+
The same as [`filter`](#filter) only the `iterator` is applied to each item in `arr` in
|
| 389 |
+
series. The next `iterator` is only called once the current one has completed.
|
| 390 |
+
The results array will be in the same order as the original.
|
| 391 |
+
|
| 392 |
+
---------------------------------------
|
| 393 |
+
|
| 394 |
+
<a name="reject" />
|
| 395 |
+
### reject(arr, iterator, callback)
|
| 396 |
+
|
| 397 |
+
The opposite of [`filter`](#filter). Removes values that pass an `async` truth test.
|
| 398 |
+
|
| 399 |
+
---------------------------------------
|
| 400 |
+
|
| 401 |
+
<a name="rejectSeries" />
|
| 402 |
+
### rejectSeries(arr, iterator, callback)
|
| 403 |
+
|
| 404 |
+
The same as [`reject`](#reject), only the `iterator` is applied to each item in `arr`
|
| 405 |
+
in series.
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
---------------------------------------
|
| 409 |
+
|
| 410 |
+
<a name="reduce" />
|
| 411 |
+
### reduce(arr, memo, iterator, callback)
|
| 412 |
+
|
| 413 |
+
__Aliases:__ `inject`, `foldl`
|
| 414 |
+
|
| 415 |
+
Reduces `arr` into a single value using an async `iterator` to return
|
| 416 |
+
each successive step. `memo` is the initial state of the reduction.
|
| 417 |
+
This function only operates in series.
|
| 418 |
+
|
| 419 |
+
For performance reasons, it may make sense to split a call to this function into
|
| 420 |
+
a parallel map, and then use the normal `Array.prototype.reduce` on the results.
|
| 421 |
+
This function is for situations where each step in the reduction needs to be async;
|
| 422 |
+
if you can get the data before reducing it, then it's probably a good idea to do so.
|
| 423 |
+
|
| 424 |
+
__Arguments__
|
| 425 |
+
|
| 426 |
+
* `arr` - An array to iterate over.
|
| 427 |
+
* `memo` - The initial state of the reduction.
|
| 428 |
+
* `iterator(memo, item, callback)` - A function applied to each item in the
|
| 429 |
+
array to produce the next step in the reduction. The `iterator` is passed a
|
| 430 |
+
`callback(err, reduction)` which accepts an optional error as its first
|
| 431 |
+
argument, and the state of the reduction as the second. If an error is
|
| 432 |
+
passed to the callback, the reduction is stopped and the main `callback` is
|
| 433 |
+
immediately called with the error.
|
| 434 |
+
* `callback(err, result)` - A callback which is called after all the `iterator`
|
| 435 |
+
functions have finished. Result is the reduced value.
|
| 436 |
+
|
| 437 |
+
__Example__
|
| 438 |
+
|
| 439 |
+
```js
|
| 440 |
+
async.reduce([1,2,3], 0, function(memo, item, callback){
|
| 441 |
+
// pointless async:
|
| 442 |
+
process.nextTick(function(){
|
| 443 |
+
callback(null, memo + item)
|
| 444 |
+
});
|
| 445 |
+
}, function(err, result){
|
| 446 |
+
// result is now equal to the last value of memo, which is 6
|
| 447 |
+
});
|
| 448 |
+
```
|
| 449 |
+
|
| 450 |
+
---------------------------------------
|
| 451 |
+
|
| 452 |
+
<a name="reduceRight" />
|
| 453 |
+
### reduceRight(arr, memo, iterator, callback)
|
| 454 |
+
|
| 455 |
+
__Alias:__ `foldr`
|
| 456 |
+
|
| 457 |
+
Same as [`reduce`](#reduce), only operates on `arr` in reverse order.
|
| 458 |
+
|
| 459 |
+
|
| 460 |
+
---------------------------------------
|
| 461 |
+
|
| 462 |
+
<a name="detect" />
|
| 463 |
+
### detect(arr, iterator, callback)
|
| 464 |
+
|
| 465 |
+
Returns the first value in `arr` that passes an async truth test. The
|
| 466 |
+
`iterator` is applied in parallel, meaning the first iterator to return `true` will
|
| 467 |
+
fire the detect `callback` with that result. That means the result might not be
|
| 468 |
+
the first item in the original `arr` (in terms of order) that passes the test.
|
| 469 |
+
|
| 470 |
+
If order within the original `arr` is important, then look at [`detectSeries`](#detectSeries).
|
| 471 |
+
|
| 472 |
+
__Arguments__
|
| 473 |
+
|
| 474 |
+
* `arr` - An array to iterate over.
|
| 475 |
+
* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
|
| 476 |
+
The iterator is passed a `callback(truthValue)` which must be called with a
|
| 477 |
+
boolean argument once it has completed.
|
| 478 |
+
* `callback(result)` - A callback which is called as soon as any iterator returns
|
| 479 |
+
`true`, or after all the `iterator` functions have finished. Result will be
|
| 480 |
+
the first item in the array that passes the truth test (iterator) or the
|
| 481 |
+
value `undefined` if none passed.
|
| 482 |
+
|
| 483 |
+
__Example__
|
| 484 |
+
|
| 485 |
+
```js
|
| 486 |
+
async.detect(['file1','file2','file3'], fs.exists, function(result){
|
| 487 |
+
// result now equals the first file in the list that exists
|
| 488 |
+
});
|
| 489 |
+
```
|
| 490 |
+
|
| 491 |
+
---------------------------------------
|
| 492 |
+
|
| 493 |
+
<a name="detectSeries" />
|
| 494 |
+
### detectSeries(arr, iterator, callback)
|
| 495 |
+
|
| 496 |
+
The same as [`detect`](#detect), only the `iterator` is applied to each item in `arr`
|
| 497 |
+
in series. This means the result is always the first in the original `arr` (in
|
| 498 |
+
terms of array order) that passes the truth test.
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
---------------------------------------
|
| 502 |
+
|
| 503 |
+
<a name="sortBy" />
|
| 504 |
+
### sortBy(arr, iterator, callback)
|
| 505 |
+
|
| 506 |
+
Sorts a list by the results of running each `arr` value through an async `iterator`.
|
| 507 |
+
|
| 508 |
+
__Arguments__
|
| 509 |
+
|
| 510 |
+
* `arr` - An array to iterate over.
|
| 511 |
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
| 512 |
+
The iterator is passed a `callback(err, sortValue)` which must be called once it
|
| 513 |
+
has completed with an error (which can be `null`) and a value to use as the sort
|
| 514 |
+
criteria.
|
| 515 |
+
* `callback(err, results)` - A callback which is called after all the `iterator`
|
| 516 |
+
functions have finished, or an error occurs. Results is the items from
|
| 517 |
+
the original `arr` sorted by the values returned by the `iterator` calls.
|
| 518 |
+
|
| 519 |
+
__Example__
|
| 520 |
+
|
| 521 |
+
```js
|
| 522 |
+
async.sortBy(['file1','file2','file3'], function(file, callback){
|
| 523 |
+
fs.stat(file, function(err, stats){
|
| 524 |
+
callback(err, stats.mtime);
|
| 525 |
+
});
|
| 526 |
+
}, function(err, results){
|
| 527 |
+
// results is now the original array of files sorted by
|
| 528 |
+
// modified date
|
| 529 |
+
});
|
| 530 |
+
```
|
| 531 |
+
|
| 532 |
+
__Sort Order__
|
| 533 |
+
|
| 534 |
+
By modifying the callback parameter the sorting order can be influenced:
|
| 535 |
+
|
| 536 |
+
```js
|
| 537 |
+
//ascending order
|
| 538 |
+
async.sortBy([1,9,3,5], function(x, callback){
|
| 539 |
+
callback(err, x);
|
| 540 |
+
}, function(err,result){
|
| 541 |
+
//result callback
|
| 542 |
+
} );
|
| 543 |
+
|
| 544 |
+
//descending order
|
| 545 |
+
async.sortBy([1,9,3,5], function(x, callback){
|
| 546 |
+
callback(err, x*-1); //<- x*-1 instead of x, turns the order around
|
| 547 |
+
}, function(err,result){
|
| 548 |
+
//result callback
|
| 549 |
+
} );
|
| 550 |
+
```
|
| 551 |
+
|
| 552 |
+
---------------------------------------
|
| 553 |
+
|
| 554 |
+
<a name="some" />
|
| 555 |
+
### some(arr, iterator, callback)
|
| 556 |
+
|
| 557 |
+
__Alias:__ `any`
|
| 558 |
+
|
| 559 |
+
Returns `true` if at least one element in the `arr` satisfies an async test.
|
| 560 |
+
_The callback for each iterator call only accepts a single argument of `true` or
|
| 561 |
+
`false`; it does not accept an error argument first!_ This is in-line with the
|
| 562 |
+
way node libraries work with truth tests like `fs.exists`. Once any iterator
|
| 563 |
+
call returns `true`, the main `callback` is immediately called.
|
| 564 |
+
|
| 565 |
+
__Arguments__
|
| 566 |
+
|
| 567 |
+
* `arr` - An array to iterate over.
|
| 568 |
+
* `iterator(item, callback)` - A truth test to apply to each item in the array
|
| 569 |
+
in parallel. The iterator is passed a callback(truthValue) which must be
|
| 570 |
+
called with a boolean argument once it has completed.
|
| 571 |
+
* `callback(result)` - A callback which is called as soon as any iterator returns
|
| 572 |
+
`true`, or after all the iterator functions have finished. Result will be
|
| 573 |
+
either `true` or `false` depending on the values of the async tests.
|
| 574 |
+
|
| 575 |
+
__Example__
|
| 576 |
+
|
| 577 |
+
```js
|
| 578 |
+
async.some(['file1','file2','file3'], fs.exists, function(result){
|
| 579 |
+
// if result is true then at least one of the files exists
|
| 580 |
+
});
|
| 581 |
+
```
|
| 582 |
+
|
| 583 |
+
---------------------------------------
|
| 584 |
+
|
| 585 |
+
<a name="every" />
|
| 586 |
+
### every(arr, iterator, callback)
|
| 587 |
+
|
| 588 |
+
__Alias:__ `all`
|
| 589 |
+
|
| 590 |
+
Returns `true` if every element in `arr` satisfies an async test.
|
| 591 |
+
_The callback for each `iterator` call only accepts a single argument of `true` or
|
| 592 |
+
`false`; it does not accept an error argument first!_ This is in-line with the
|
| 593 |
+
way node libraries work with truth tests like `fs.exists`.
|
| 594 |
+
|
| 595 |
+
__Arguments__
|
| 596 |
+
|
| 597 |
+
* `arr` - An array to iterate over.
|
| 598 |
+
* `iterator(item, callback)` - A truth test to apply to each item in the array
|
| 599 |
+
in parallel. The iterator is passed a callback(truthValue) which must be
|
| 600 |
+
called with a boolean argument once it has completed.
|
| 601 |
+
* `callback(result)` - A callback which is called after all the `iterator`
|
| 602 |
+
functions have finished. Result will be either `true` or `false` depending on
|
| 603 |
+
the values of the async tests.
|
| 604 |
+
|
| 605 |
+
__Example__
|
| 606 |
+
|
| 607 |
+
```js
|
| 608 |
+
async.every(['file1','file2','file3'], fs.exists, function(result){
|
| 609 |
+
// if result is true then every file exists
|
| 610 |
+
});
|
| 611 |
+
```
|
| 612 |
+
|
| 613 |
+
---------------------------------------
|
| 614 |
+
|
| 615 |
+
<a name="concat" />
|
| 616 |
+
### concat(arr, iterator, callback)
|
| 617 |
+
|
| 618 |
+
Applies `iterator` to each item in `arr`, concatenating the results. Returns the
|
| 619 |
+
concatenated list. The `iterator`s are called in parallel, and the results are
|
| 620 |
+
concatenated as they return. There is no guarantee that the results array will
|
| 621 |
+
be returned in the original order of `arr` passed to the `iterator` function.
|
| 622 |
+
|
| 623 |
+
__Arguments__
|
| 624 |
+
|
| 625 |
+
* `arr` - An array to iterate over.
|
| 626 |
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
| 627 |
+
The iterator is passed a `callback(err, results)` which must be called once it
|
| 628 |
+
has completed with an error (which can be `null`) and an array of results.
|
| 629 |
+
* `callback(err, results)` - A callback which is called after all the `iterator`
|
| 630 |
+
functions have finished, or an error occurs. Results is an array containing
|
| 631 |
+
the concatenated results of the `iterator` function.
|
| 632 |
+
|
| 633 |
+
__Example__
|
| 634 |
+
|
| 635 |
+
```js
|
| 636 |
+
async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files){
|
| 637 |
+
// files is now a list of filenames that exist in the 3 directories
|
| 638 |
+
});
|
| 639 |
+
```
|
| 640 |
+
|
| 641 |
+
---------------------------------------
|
| 642 |
+
|
| 643 |
+
<a name="concatSeries" />
|
| 644 |
+
### concatSeries(arr, iterator, callback)
|
| 645 |
+
|
| 646 |
+
Same as [`concat`](#concat), but executes in series instead of parallel.
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
## Control Flow
|
| 650 |
+
|
| 651 |
+
<a name="series" />
|
| 652 |
+
### series(tasks, [callback])
|
| 653 |
+
|
| 654 |
+
Run the functions in the `tasks` array in series, each one running once the previous
|
| 655 |
+
function has completed. If any functions in the series pass an error to its
|
| 656 |
+
callback, no more functions are run, and `callback` is immediately called with the value of the error.
|
| 657 |
+
Otherwise, `callback` receives an array of results when `tasks` have completed.
|
| 658 |
+
|
| 659 |
+
It is also possible to use an object instead of an array. Each property will be
|
| 660 |
+
run as a function, and the results will be passed to the final `callback` as an object
|
| 661 |
+
instead of an array. This can be a more readable way of handling results from
|
| 662 |
+
[`series`](#series).
|
| 663 |
+
|
| 664 |
+
**Note** that while many implementations preserve the order of object properties, the
|
| 665 |
+
[ECMAScript Language Specifcation](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6)
|
| 666 |
+
explicitly states that
|
| 667 |
+
|
| 668 |
+
> The mechanics and order of enumerating the properties is not specified.
|
| 669 |
+
|
| 670 |
+
So if you rely on the order in which your series of functions are executed, and want
|
| 671 |
+
this to work on all platforms, consider using an array.
|
| 672 |
+
|
| 673 |
+
__Arguments__
|
| 674 |
+
|
| 675 |
+
* `tasks` - An array or object containing functions to run, each function is passed
|
| 676 |
+
a `callback(err, result)` it must call on completion with an error `err` (which can
|
| 677 |
+
be `null`) and an optional `result` value.
|
| 678 |
+
* `callback(err, results)` - An optional callback to run once all the functions
|
| 679 |
+
have completed. This function gets a results array (or object) containing all
|
| 680 |
+
the result arguments passed to the `task` callbacks.
|
| 681 |
+
|
| 682 |
+
__Example__
|
| 683 |
+
|
| 684 |
+
```js
|
| 685 |
+
async.series([
|
| 686 |
+
function(callback){
|
| 687 |
+
// do some stuff ...
|
| 688 |
+
callback(null, 'one');
|
| 689 |
+
},
|
| 690 |
+
function(callback){
|
| 691 |
+
// do some more stuff ...
|
| 692 |
+
callback(null, 'two');
|
| 693 |
+
}
|
| 694 |
+
],
|
| 695 |
+
// optional callback
|
| 696 |
+
function(err, results){
|
| 697 |
+
// results is now equal to ['one', 'two']
|
| 698 |
+
});
|
| 699 |
+
|
| 700 |
+
|
| 701 |
+
// an example using an object instead of an array
|
| 702 |
+
async.series({
|
| 703 |
+
one: function(callback){
|
| 704 |
+
setTimeout(function(){
|
| 705 |
+
callback(null, 1);
|
| 706 |
+
}, 200);
|
| 707 |
+
},
|
| 708 |
+
two: function(callback){
|
| 709 |
+
setTimeout(function(){
|
| 710 |
+
callback(null, 2);
|
| 711 |
+
}, 100);
|
| 712 |
+
}
|
| 713 |
+
},
|
| 714 |
+
function(err, results) {
|
| 715 |
+
// results is now equal to: {one: 1, two: 2}
|
| 716 |
+
});
|
| 717 |
+
```
|
| 718 |
+
|
| 719 |
+
---------------------------------------
|
| 720 |
+
|
| 721 |
+
<a name="parallel" />
|
| 722 |
+
### parallel(tasks, [callback])
|
| 723 |
+
|
| 724 |
+
Run the `tasks` array of functions in parallel, without waiting until the previous
|
| 725 |
+
function has completed. If any of the functions pass an error to its
|
| 726 |
+
callback, the main `callback` is immediately called with the value of the error.
|
| 727 |
+
Once the `tasks` have completed, the results are passed to the final `callback` as an
|
| 728 |
+
array.
|
| 729 |
+
|
| 730 |
+
It is also possible to use an object instead of an array. Each property will be
|
| 731 |
+
run as a function and the results will be passed to the final `callback` as an object
|
| 732 |
+
instead of an array. This can be a more readable way of handling results from
|
| 733 |
+
[`parallel`](#parallel).
|
| 734 |
+
|
| 735 |
+
|
| 736 |
+
__Arguments__
|
| 737 |
+
|
| 738 |
+
* `tasks` - An array or object containing functions to run. Each function is passed
|
| 739 |
+
a `callback(err, result)` which it must call on completion with an error `err`
|
| 740 |
+
(which can be `null`) and an optional `result` value.
|
| 741 |
+
* `callback(err, results)` - An optional callback to run once all the functions
|
| 742 |
+
have completed. This function gets a results array (or object) containing all
|
| 743 |
+
the result arguments passed to the task callbacks.
|
| 744 |
+
|
| 745 |
+
__Example__
|
| 746 |
+
|
| 747 |
+
```js
|
| 748 |
+
async.parallel([
|
| 749 |
+
function(callback){
|
| 750 |
+
setTimeout(function(){
|
| 751 |
+
callback(null, 'one');
|
| 752 |
+
}, 200);
|
| 753 |
+
},
|
| 754 |
+
function(callback){
|
| 755 |
+
setTimeout(function(){
|
| 756 |
+
callback(null, 'two');
|
| 757 |
+
}, 100);
|
| 758 |
+
}
|
| 759 |
+
],
|
| 760 |
+
// optional callback
|
| 761 |
+
function(err, results){
|
| 762 |
+
// the results array will equal ['one','two'] even though
|
| 763 |
+
// the second function had a shorter timeout.
|
| 764 |
+
});
|
| 765 |
+
|
| 766 |
+
|
| 767 |
+
// an example using an object instead of an array
|
| 768 |
+
async.parallel({
|
| 769 |
+
one: function(callback){
|
| 770 |
+
setTimeout(function(){
|
| 771 |
+
callback(null, 1);
|
| 772 |
+
}, 200);
|
| 773 |
+
},
|
| 774 |
+
two: function(callback){
|
| 775 |
+
setTimeout(function(){
|
| 776 |
+
callback(null, 2);
|
| 777 |
+
}, 100);
|
| 778 |
+
}
|
| 779 |
+
},
|
| 780 |
+
function(err, results) {
|
| 781 |
+
// results is now equals to: {one: 1, two: 2}
|
| 782 |
+
});
|
| 783 |
+
```
|
| 784 |
+
|
| 785 |
+
---------------------------------------
|
| 786 |
+
|
| 787 |
+
<a name="parallelLimit" />
|
| 788 |
+
### parallelLimit(tasks, limit, [callback])
|
| 789 |
+
|
| 790 |
+
The same as [`parallel`](#parallel), only `tasks` are executed in parallel
|
| 791 |
+
with a maximum of `limit` tasks executing at any time.
|
| 792 |
+
|
| 793 |
+
Note that the `tasks` are not executed in batches, so there is no guarantee that
|
| 794 |
+
the first `limit` tasks will complete before any others are started.
|
| 795 |
+
|
| 796 |
+
__Arguments__
|
| 797 |
+
|
| 798 |
+
* `tasks` - An array or object containing functions to run, each function is passed
|
| 799 |
+
a `callback(err, result)` it must call on completion with an error `err` (which can
|
| 800 |
+
be `null`) and an optional `result` value.
|
| 801 |
+
* `limit` - The maximum number of `tasks` to run at any time.
|
| 802 |
+
* `callback(err, results)` - An optional callback to run once all the functions
|
| 803 |
+
have completed. This function gets a results array (or object) containing all
|
| 804 |
+
the result arguments passed to the `task` callbacks.
|
| 805 |
+
|
| 806 |
+
---------------------------------------
|
| 807 |
+
|
| 808 |
+
<a name="whilst" />
|
| 809 |
+
### whilst(test, fn, callback)
|
| 810 |
+
|
| 811 |
+
Repeatedly call `fn`, while `test` returns `true`. Calls `callback` when stopped,
|
| 812 |
+
or an error occurs.
|
| 813 |
+
|
| 814 |
+
__Arguments__
|
| 815 |
+
|
| 816 |
+
* `test()` - synchronous truth test to perform before each execution of `fn`.
|
| 817 |
+
* `fn(callback)` - A function which is called each time `test` passes. The function is
|
| 818 |
+
passed a `callback(err)`, which must be called once it has completed with an
|
| 819 |
+
optional `err` argument.
|
| 820 |
+
* `callback(err)` - A callback which is called after the test fails and repeated
|
| 821 |
+
execution of `fn` has stopped.
|
| 822 |
+
|
| 823 |
+
__Example__
|
| 824 |
+
|
| 825 |
+
```js
|
| 826 |
+
var count = 0;
|
| 827 |
+
|
| 828 |
+
async.whilst(
|
| 829 |
+
function () { return count < 5; },
|
| 830 |
+
function (callback) {
|
| 831 |
+
count++;
|
| 832 |
+
setTimeout(callback, 1000);
|
| 833 |
+
},
|
| 834 |
+
function (err) {
|
| 835 |
+
// 5 seconds have passed
|
| 836 |
+
}
|
| 837 |
+
);
|
| 838 |
+
```
|
| 839 |
+
|
| 840 |
+
---------------------------------------
|
| 841 |
+
|
| 842 |
+
<a name="doWhilst" />
|
| 843 |
+
### doWhilst(fn, test, callback)
|
| 844 |
+
|
| 845 |
+
The post-check version of [`whilst`](#whilst). To reflect the difference in
|
| 846 |
+
the order of operations, the arguments `test` and `fn` are switched.
|
| 847 |
+
|
| 848 |
+
`doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
|
| 849 |
+
|
| 850 |
+
---------------------------------------
|
| 851 |
+
|
| 852 |
+
<a name="until" />
|
| 853 |
+
### until(test, fn, callback)
|
| 854 |
+
|
| 855 |
+
Repeatedly call `fn` until `test` returns `true`. Calls `callback` when stopped,
|
| 856 |
+
or an error occurs.
|
| 857 |
+
|
| 858 |
+
The inverse of [`whilst`](#whilst).
|
| 859 |
+
|
| 860 |
+
---------------------------------------
|
| 861 |
+
|
| 862 |
+
<a name="doUntil" />
|
| 863 |
+
### doUntil(fn, test, callback)
|
| 864 |
+
|
| 865 |
+
Like [`doWhilst`](#doWhilst), except the `test` is inverted. Note the argument ordering differs from `until`.
|
| 866 |
+
|
| 867 |
+
---------------------------------------
|
| 868 |
+
|
| 869 |
+
<a name="forever" />
|
| 870 |
+
### forever(fn, errback)
|
| 871 |
+
|
| 872 |
+
Calls the asynchronous function `fn` with a callback parameter that allows it to
|
| 873 |
+
call itself again, in series, indefinitely.
|
| 874 |
+
|
| 875 |
+
If an error is passed to the callback then `errback` is called with the
|
| 876 |
+
error, and execution stops, otherwise it will never be called.
|
| 877 |
+
|
| 878 |
+
```js
|
| 879 |
+
async.forever(
|
| 880 |
+
function(next) {
|
| 881 |
+
// next is suitable for passing to things that need a callback(err [, whatever]);
|
| 882 |
+
// it will result in this function being called again.
|
| 883 |
+
},
|
| 884 |
+
function(err) {
|
| 885 |
+
// if next is called with a value in its first parameter, it will appear
|
| 886 |
+
// in here as 'err', and execution will stop.
|
| 887 |
+
}
|
| 888 |
+
);
|
| 889 |
+
```
|
| 890 |
+
|
| 891 |
+
---------------------------------------
|
| 892 |
+
|
| 893 |
+
<a name="waterfall" />
|
| 894 |
+
### waterfall(tasks, [callback])
|
| 895 |
+
|
| 896 |
+
Runs the `tasks` array of functions in series, each passing their results to the next in
|
| 897 |
+
the array. However, if any of the `tasks` pass an error to their own callback, the
|
| 898 |
+
next function is not executed, and the main `callback` is immediately called with
|
| 899 |
+
the error.
|
| 900 |
+
|
| 901 |
+
__Arguments__
|
| 902 |
+
|
| 903 |
+
* `tasks` - An array of functions to run, each function is passed a
|
| 904 |
+
`callback(err, result1, result2, ...)` it must call on completion. The first
|
| 905 |
+
argument is an error (which can be `null`) and any further arguments will be
|
| 906 |
+
passed as arguments in order to the next task.
|
| 907 |
+
* `callback(err, [results])` - An optional callback to run once all the functions
|
| 908 |
+
have completed. This will be passed the results of the last task's callback.
|
| 909 |
+
|
| 910 |
+
|
| 911 |
+
|
| 912 |
+
__Example__
|
| 913 |
+
|
| 914 |
+
```js
|
| 915 |
+
async.waterfall([
|
| 916 |
+
function(callback){
|
| 917 |
+
callback(null, 'one', 'two');
|
| 918 |
+
},
|
| 919 |
+
function(arg1, arg2, callback){
|
| 920 |
+
// arg1 now equals 'one' and arg2 now equals 'two'
|
| 921 |
+
callback(null, 'three');
|
| 922 |
+
},
|
| 923 |
+
function(arg1, callback){
|
| 924 |
+
// arg1 now equals 'three'
|
| 925 |
+
callback(null, 'done');
|
| 926 |
+
}
|
| 927 |
+
], function (err, result) {
|
| 928 |
+
// result now equals 'done'
|
| 929 |
+
});
|
| 930 |
+
```
|
| 931 |
+
|
| 932 |
+
---------------------------------------
|
| 933 |
+
<a name="compose" />
|
| 934 |
+
### compose(fn1, fn2...)
|
| 935 |
+
|
| 936 |
+
Creates a function which is a composition of the passed asynchronous
|
| 937 |
+
functions. Each function consumes the return value of the function that
|
| 938 |
+
follows. Composing functions `f()`, `g()`, and `h()` would produce the result of
|
| 939 |
+
`f(g(h()))`, only this version uses callbacks to obtain the return values.
|
| 940 |
+
|
| 941 |
+
Each function is executed with the `this` binding of the composed function.
|
| 942 |
+
|
| 943 |
+
__Arguments__
|
| 944 |
+
|
| 945 |
+
* `functions...` - the asynchronous functions to compose
|
| 946 |
+
|
| 947 |
+
|
| 948 |
+
__Example__
|
| 949 |
+
|
| 950 |
+
```js
|
| 951 |
+
function add1(n, callback) {
|
| 952 |
+
setTimeout(function () {
|
| 953 |
+
callback(null, n + 1);
|
| 954 |
+
}, 10);
|
| 955 |
+
}
|
| 956 |
+
|
| 957 |
+
function mul3(n, callback) {
|
| 958 |
+
setTimeout(function () {
|
| 959 |
+
callback(null, n * 3);
|
| 960 |
+
}, 10);
|
| 961 |
+
}
|
| 962 |
+
|
| 963 |
+
var add1mul3 = async.compose(mul3, add1);
|
| 964 |
+
|
| 965 |
+
add1mul3(4, function (err, result) {
|
| 966 |
+
// result now equals 15
|
| 967 |
+
});
|
| 968 |
+
```
|
| 969 |
+
|
| 970 |
+
---------------------------------------
|
| 971 |
+
<a name="seq" />
|
| 972 |
+
### seq(fn1, fn2...)
|
| 973 |
+
|
| 974 |
+
Version of the compose function that is more natural to read.
|
| 975 |
+
Each following function consumes the return value of the latter function.
|
| 976 |
+
|
| 977 |
+
Each function is executed with the `this` binding of the composed function.
|
| 978 |
+
|
| 979 |
+
__Arguments__
|
| 980 |
+
|
| 981 |
+
* functions... - the asynchronous functions to compose
|
| 982 |
+
|
| 983 |
+
|
| 984 |
+
__Example__
|
| 985 |
+
|
| 986 |
+
```js
|
| 987 |
+
// Requires lodash (or underscore), express3 and dresende's orm2.
|
| 988 |
+
// Part of an app, that fetches cats of the logged user.
|
| 989 |
+
// This example uses `seq` function to avoid overnesting and error
|
| 990 |
+
// handling clutter.
|
| 991 |
+
app.get('/cats', function(request, response) {
|
| 992 |
+
function handleError(err, data, callback) {
|
| 993 |
+
if (err) {
|
| 994 |
+
console.error(err);
|
| 995 |
+
response.json({ status: 'error', message: err.message });
|
| 996 |
+
}
|
| 997 |
+
else {
|
| 998 |
+
callback(data);
|
| 999 |
+
}
|
| 1000 |
+
}
|
| 1001 |
+
var User = request.models.User;
|
| 1002 |
+
async.seq(
|
| 1003 |
+
_.bind(User.get, User), // 'User.get' has signature (id, callback(err, data))
|
| 1004 |
+
handleError,
|
| 1005 |
+
function(user, fn) {
|
| 1006 |
+
user.getCats(fn); // 'getCats' has signature (callback(err, data))
|
| 1007 |
+
},
|
| 1008 |
+
handleError,
|
| 1009 |
+
function(cats) {
|
| 1010 |
+
response.json({ status: 'ok', message: 'Cats found', data: cats });
|
| 1011 |
+
}
|
| 1012 |
+
)(req.session.user_id);
|
| 1013 |
+
}
|
| 1014 |
+
});
|
| 1015 |
+
```
|
| 1016 |
+
|
| 1017 |
+
---------------------------------------
|
| 1018 |
+
<a name="applyEach" />
|
| 1019 |
+
### applyEach(fns, args..., callback)
|
| 1020 |
+
|
| 1021 |
+
Applies the provided arguments to each function in the array, calling
|
| 1022 |
+
`callback` after all functions have completed. If you only provide the first
|
| 1023 |
+
argument, then it will return a function which lets you pass in the
|
| 1024 |
+
arguments as if it were a single function call.
|
| 1025 |
+
|
| 1026 |
+
__Arguments__
|
| 1027 |
+
|
| 1028 |
+
* `fns` - the asynchronous functions to all call with the same arguments
|
| 1029 |
+
* `args...` - any number of separate arguments to pass to the function
|
| 1030 |
+
* `callback` - the final argument should be the callback, called when all
|
| 1031 |
+
functions have completed processing
|
| 1032 |
+
|
| 1033 |
+
|
| 1034 |
+
__Example__
|
| 1035 |
+
|
| 1036 |
+
```js
|
| 1037 |
+
async.applyEach([enableSearch, updateSchema], 'bucket', callback);
|
| 1038 |
+
|
| 1039 |
+
// partial application example:
|
| 1040 |
+
async.each(
|
| 1041 |
+
buckets,
|
| 1042 |
+
async.applyEach([enableSearch, updateSchema]),
|
| 1043 |
+
callback
|
| 1044 |
+
);
|
| 1045 |
+
```
|
| 1046 |
+
|
| 1047 |
+
---------------------------------------
|
| 1048 |
+
|
| 1049 |
+
<a name="applyEachSeries" />
|
| 1050 |
+
### applyEachSeries(arr, iterator, callback)
|
| 1051 |
+
|
| 1052 |
+
The same as [`applyEach`](#applyEach) only the functions are applied in series.
|
| 1053 |
+
|
| 1054 |
+
---------------------------------------
|
| 1055 |
+
|
| 1056 |
+
<a name="queue" />
|
| 1057 |
+
### queue(worker, concurrency)
|
| 1058 |
+
|
| 1059 |
+
Creates a `queue` object with the specified `concurrency`. Tasks added to the
|
| 1060 |
+
`queue` are processed in parallel (up to the `concurrency` limit). If all
|
| 1061 |
+
`worker`s are in progress, the task is queued until one becomes available.
|
| 1062 |
+
Once a `worker` completes a `task`, that `task`'s callback is called.
|
| 1063 |
+
|
| 1064 |
+
__Arguments__
|
| 1065 |
+
|
| 1066 |
+
* `worker(task, callback)` - An asynchronous function for processing a queued
|
| 1067 |
+
task, which must call its `callback(err)` argument when finished, with an
|
| 1068 |
+
optional `error` as an argument.
|
| 1069 |
+
* `concurrency` - An `integer` for determining how many `worker` functions should be
|
| 1070 |
+
run in parallel.
|
| 1071 |
+
|
| 1072 |
+
__Queue objects__
|
| 1073 |
+
|
| 1074 |
+
The `queue` object returned by this function has the following properties and
|
| 1075 |
+
methods:
|
| 1076 |
+
|
| 1077 |
+
* `length()` - a function returning the number of items waiting to be processed.
|
| 1078 |
+
* `started` - a function returning whether or not any items have been pushed and processed by the queue
|
| 1079 |
+
* `running()` - a function returning the number of items currently being processed.
|
| 1080 |
+
* `idle()` - a function returning false if there are items waiting or being processed, or true if not.
|
| 1081 |
+
* `concurrency` - an integer for determining how many `worker` functions should be
|
| 1082 |
+
run in parallel. This property can be changed after a `queue` is created to
|
| 1083 |
+
alter the concurrency on-the-fly.
|
| 1084 |
+
* `push(task, [callback])` - add a new task to the `queue`. Calls `callback` once
|
| 1085 |
+
the `worker` has finished processing the task. Instead of a single task, a `tasks` array
|
| 1086 |
+
can be submitted. The respective callback is used for every task in the list.
|
| 1087 |
+
* `unshift(task, [callback])` - add a new task to the front of the `queue`.
|
| 1088 |
+
* `saturated` - a callback that is called when the `queue` length hits the `concurrency` limit,
|
| 1089 |
+
and further tasks will be queued.
|
| 1090 |
+
* `empty` - a callback that is called when the last item from the `queue` is given to a `worker`.
|
| 1091 |
+
* `drain` - a callback that is called when the last item from the `queue` has returned from the `worker`.
|
| 1092 |
+
* `paused` - a boolean for determining whether the queue is in a paused state
|
| 1093 |
+
* `pause()` - a function that pauses the processing of tasks until `resume()` is called.
|
| 1094 |
+
* `resume()` - a function that resumes the processing of queued tasks when the queue is paused.
|
| 1095 |
+
* `kill()` - a function that empties remaining tasks from the queue forcing it to go idle.
|
| 1096 |
+
|
| 1097 |
+
__Example__
|
| 1098 |
+
|
| 1099 |
+
```js
|
| 1100 |
+
// create a queue object with concurrency 2
|
| 1101 |
+
|
| 1102 |
+
var q = async.queue(function (task, callback) {
|
| 1103 |
+
console.log('hello ' + task.name);
|
| 1104 |
+
callback();
|
| 1105 |
+
}, 2);
|
| 1106 |
+
|
| 1107 |
+
|
| 1108 |
+
// assign a callback
|
| 1109 |
+
q.drain = function() {
|
| 1110 |
+
console.log('all items have been processed');
|
| 1111 |
+
}
|
| 1112 |
+
|
| 1113 |
+
// add some items to the queue
|
| 1114 |
+
|
| 1115 |
+
q.push({name: 'foo'}, function (err) {
|
| 1116 |
+
console.log('finished processing foo');
|
| 1117 |
+
});
|
| 1118 |
+
q.push({name: 'bar'}, function (err) {
|
| 1119 |
+
console.log('finished processing bar');
|
| 1120 |
+
});
|
| 1121 |
+
|
| 1122 |
+
// add some items to the queue (batch-wise)
|
| 1123 |
+
|
| 1124 |
+
q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) {
|
| 1125 |
+
console.log('finished processing bar');
|
| 1126 |
+
});
|
| 1127 |
+
|
| 1128 |
+
// add some items to the front of the queue
|
| 1129 |
+
|
| 1130 |
+
q.unshift({name: 'bar'}, function (err) {
|
| 1131 |
+
console.log('finished processing bar');
|
| 1132 |
+
});
|
| 1133 |
+
```
|
| 1134 |
+
|
| 1135 |
+
|
| 1136 |
+
---------------------------------------
|
| 1137 |
+
|
| 1138 |
+
<a name="priorityQueue" />
|
| 1139 |
+
### priorityQueue(worker, concurrency)
|
| 1140 |
+
|
| 1141 |
+
The same as [`queue`](#queue) only tasks are assigned a priority and completed in ascending priority order. There are two differences between `queue` and `priorityQueue` objects:
|
| 1142 |
+
|
| 1143 |
+
* `push(task, priority, [callback])` - `priority` should be a number. If an array of
|
| 1144 |
+
`tasks` is given, all tasks will be assigned the same priority.
|
| 1145 |
+
* The `unshift` method was removed.
|
| 1146 |
+
|
| 1147 |
+
---------------------------------------
|
| 1148 |
+
|
| 1149 |
+
<a name="cargo" />
|
| 1150 |
+
### cargo(worker, [payload])
|
| 1151 |
+
|
| 1152 |
+
Creates a `cargo` object with the specified payload. Tasks added to the
|
| 1153 |
+
cargo will be processed altogether (up to the `payload` limit). If the
|
| 1154 |
+
`worker` is in progress, the task is queued until it becomes available. Once
|
| 1155 |
+
the `worker` has completed some tasks, each callback of those tasks is called.
|
| 1156 |
+
Check out [this animation](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) for how `cargo` and `queue` work.
|
| 1157 |
+
|
| 1158 |
+
While [queue](#queue) passes only one task to one of a group of workers
|
| 1159 |
+
at a time, cargo passes an array of tasks to a single worker, repeating
|
| 1160 |
+
when the worker is finished.
|
| 1161 |
+
|
| 1162 |
+
__Arguments__
|
| 1163 |
+
|
| 1164 |
+
* `worker(tasks, callback)` - An asynchronous function for processing an array of
|
| 1165 |
+
queued tasks, which must call its `callback(err)` argument when finished, with
|
| 1166 |
+
an optional `err` argument.
|
| 1167 |
+
* `payload` - An optional `integer` for determining how many tasks should be
|
| 1168 |
+
processed per round; if omitted, the default is unlimited.
|
| 1169 |
+
|
| 1170 |
+
__Cargo objects__
|
| 1171 |
+
|
| 1172 |
+
The `cargo` object returned by this function has the following properties and
|
| 1173 |
+
methods:
|
| 1174 |
+
|
| 1175 |
+
* `length()` - A function returning the number of items waiting to be processed.
|
| 1176 |
+
* `payload` - An `integer` for determining how many tasks should be
|
| 1177 |
+
process per round. This property can be changed after a `cargo` is created to
|
| 1178 |
+
alter the payload on-the-fly.
|
| 1179 |
+
* `push(task, [callback])` - Adds `task` to the `queue`. The callback is called
|
| 1180 |
+
once the `worker` has finished processing the task. Instead of a single task, an array of `tasks`
|
| 1181 |
+
can be submitted. The respective callback is used for every task in the list.
|
| 1182 |
+
* `saturated` - A callback that is called when the `queue.length()` hits the concurrency and further tasks will be queued.
|
| 1183 |
+
* `empty` - A callback that is called when the last item from the `queue` is given to a `worker`.
|
| 1184 |
+
* `drain` - A callback that is called when the last item from the `queue` has returned from the `worker`.
|
| 1185 |
+
|
| 1186 |
+
__Example__
|
| 1187 |
+
|
| 1188 |
+
```js
|
| 1189 |
+
// create a cargo object with payload 2
|
| 1190 |
+
|
| 1191 |
+
var cargo = async.cargo(function (tasks, callback) {
|
| 1192 |
+
for(var i=0; i<tasks.length; i++){
|
| 1193 |
+
console.log('hello ' + tasks[i].name);
|
| 1194 |
+
}
|
| 1195 |
+
callback();
|
| 1196 |
+
}, 2);
|
| 1197 |
+
|
| 1198 |
+
|
| 1199 |
+
// add some items
|
| 1200 |
+
|
| 1201 |
+
cargo.push({name: 'foo'}, function (err) {
|
| 1202 |
+
console.log('finished processing foo');
|
| 1203 |
+
});
|
| 1204 |
+
cargo.push({name: 'bar'}, function (err) {
|
| 1205 |
+
console.log('finished processing bar');
|
| 1206 |
+
});
|
| 1207 |
+
cargo.push({name: 'baz'}, function (err) {
|
| 1208 |
+
console.log('finished processing baz');
|
| 1209 |
+
});
|
| 1210 |
+
```
|
| 1211 |
+
|
| 1212 |
+
---------------------------------------
|
| 1213 |
+
|
| 1214 |
+
<a name="auto" />
|
| 1215 |
+
### auto(tasks, [callback])
|
| 1216 |
+
|
| 1217 |
+
Determines the best order for running the functions in `tasks`, based on their
|
| 1218 |
+
requirements. Each function can optionally depend on other functions being completed
|
| 1219 |
+
first, and each function is run as soon as its requirements are satisfied.
|
| 1220 |
+
|
| 1221 |
+
If any of the functions pass an error to their callback, it will not
|
| 1222 |
+
complete (so any other functions depending on it will not run), and the main
|
| 1223 |
+
`callback` is immediately called with the error. Functions also receive an
|
| 1224 |
+
object containing the results of functions which have completed so far.
|
| 1225 |
+
|
| 1226 |
+
Note, all functions are called with a `results` object as a second argument,
|
| 1227 |
+
so it is unsafe to pass functions in the `tasks` object which cannot handle the
|
| 1228 |
+
extra argument.
|
| 1229 |
+
|
| 1230 |
+
For example, this snippet of code:
|
| 1231 |
+
|
| 1232 |
+
```js
|
| 1233 |
+
async.auto({
|
| 1234 |
+
readData: async.apply(fs.readFile, 'data.txt', 'utf-8')
|
| 1235 |
+
}, callback);
|
| 1236 |
+
```
|
| 1237 |
+
|
| 1238 |
+
will have the effect of calling `readFile` with the results object as the last
|
| 1239 |
+
argument, which will fail:
|
| 1240 |
+
|
| 1241 |
+
```js
|
| 1242 |
+
fs.readFile('data.txt', 'utf-8', cb, {});
|
| 1243 |
+
```
|
| 1244 |
+
|
| 1245 |
+
Instead, wrap the call to `readFile` in a function which does not forward the
|
| 1246 |
+
`results` object:
|
| 1247 |
+
|
| 1248 |
+
```js
|
| 1249 |
+
async.auto({
|
| 1250 |
+
readData: function(cb, results){
|
| 1251 |
+
fs.readFile('data.txt', 'utf-8', cb);
|
| 1252 |
+
}
|
| 1253 |
+
}, callback);
|
| 1254 |
+
```
|
| 1255 |
+
|
| 1256 |
+
__Arguments__
|
| 1257 |
+
|
| 1258 |
+
* `tasks` - An object. Each of its properties is either a function or an array of
|
| 1259 |
+
requirements, with the function itself the last item in the array. The object's key
|
| 1260 |
+
of a property serves as the name of the task defined by that property,
|
| 1261 |
+
i.e. can be used when specifying requirements for other tasks.
|
| 1262 |
+
The function receives two arguments: (1) a `callback(err, result)` which must be
|
| 1263 |
+
called when finished, passing an `error` (which can be `null`) and the result of
|
| 1264 |
+
the function's execution, and (2) a `results` object, containing the results of
|
| 1265 |
+
the previously executed functions.
|
| 1266 |
+
* `callback(err, results)` - An optional callback which is called when all the
|
| 1267 |
+
tasks have been completed. It receives the `err` argument if any `tasks`
|
| 1268 |
+
pass an error to their callback. Results are always returned; however, if
|
| 1269 |
+
an error occurs, no further `tasks` will be performed, and the results
|
| 1270 |
+
object will only contain partial results.
|
| 1271 |
+
|
| 1272 |
+
|
| 1273 |
+
__Example__
|
| 1274 |
+
|
| 1275 |
+
```js
|
| 1276 |
+
async.auto({
|
| 1277 |
+
get_data: function(callback){
|
| 1278 |
+
console.log('in get_data');
|
| 1279 |
+
// async code to get some data
|
| 1280 |
+
callback(null, 'data', 'converted to array');
|
| 1281 |
+
},
|
| 1282 |
+
make_folder: function(callback){
|
| 1283 |
+
console.log('in make_folder');
|
| 1284 |
+
// async code to create a directory to store a file in
|
| 1285 |
+
// this is run at the same time as getting the data
|
| 1286 |
+
callback(null, 'folder');
|
| 1287 |
+
},
|
| 1288 |
+
write_file: ['get_data', 'make_folder', function(callback, results){
|
| 1289 |
+
console.log('in write_file', JSON.stringify(results));
|
| 1290 |
+
// once there is some data and the directory exists,
|
| 1291 |
+
// write the data to a file in the directory
|
| 1292 |
+
callback(null, 'filename');
|
| 1293 |
+
}],
|
| 1294 |
+
email_link: ['write_file', function(callback, results){
|
| 1295 |
+
console.log('in email_link', JSON.stringify(results));
|
| 1296 |
+
// once the file is written let's email a link to it...
|
| 1297 |
+
// results.write_file contains the filename returned by write_file.
|
| 1298 |
+
callback(null, {'file':results.write_file, 'email':'user@example.com'});
|
| 1299 |
+
}]
|
| 1300 |
+
}, function(err, results) {
|
| 1301 |
+
console.log('err = ', err);
|
| 1302 |
+
console.log('results = ', results);
|
| 1303 |
+
});
|
| 1304 |
+
```
|
| 1305 |
+
|
| 1306 |
+
This is a fairly trivial example, but to do this using the basic parallel and
|
| 1307 |
+
series functions would look like this:
|
| 1308 |
+
|
| 1309 |
+
```js
|
| 1310 |
+
async.parallel([
|
| 1311 |
+
function(callback){
|
| 1312 |
+
console.log('in get_data');
|
| 1313 |
+
// async code to get some data
|
| 1314 |
+
callback(null, 'data', 'converted to array');
|
| 1315 |
+
},
|
| 1316 |
+
function(callback){
|
| 1317 |
+
console.log('in make_folder');
|
| 1318 |
+
// async code to create a directory to store a file in
|
| 1319 |
+
// this is run at the same time as getting the data
|
| 1320 |
+
callback(null, 'folder');
|
| 1321 |
+
}
|
| 1322 |
+
],
|
| 1323 |
+
function(err, results){
|
| 1324 |
+
async.series([
|
| 1325 |
+
function(callback){
|
| 1326 |
+
console.log('in write_file', JSON.stringify(results));
|
| 1327 |
+
// once there is some data and the directory exists,
|
| 1328 |
+
// write the data to a file in the directory
|
| 1329 |
+
results.push('filename');
|
| 1330 |
+
callback(null);
|
| 1331 |
+
},
|
| 1332 |
+
function(callback){
|
| 1333 |
+
console.log('in email_link', JSON.stringify(results));
|
| 1334 |
+
// once the file is written let's email a link to it...
|
| 1335 |
+
callback(null, {'file':results.pop(), 'email':'user@example.com'});
|
| 1336 |
+
}
|
| 1337 |
+
]);
|
| 1338 |
+
});
|
| 1339 |
+
```
|
| 1340 |
+
|
| 1341 |
+
For a complicated series of `async` tasks, using the [`auto`](#auto) function makes adding
|
| 1342 |
+
new tasks much easier (and the code more readable).
|
| 1343 |
+
|
| 1344 |
+
|
| 1345 |
+
---------------------------------------
|
| 1346 |
+
|
| 1347 |
+
<a name="retry" />
|
| 1348 |
+
### retry([times = 5], task, [callback])
|
| 1349 |
+
|
| 1350 |
+
Attempts to get a successful response from `task` no more than `times` times before
|
| 1351 |
+
returning an error. If the task is successful, the `callback` will be passed the result
|
| 1352 |
+
of the successfull task. If all attemps fail, the callback will be passed the error and
|
| 1353 |
+
result (if any) of the final attempt.
|
| 1354 |
+
|
| 1355 |
+
__Arguments__
|
| 1356 |
+
|
| 1357 |
+
* `times` - An integer indicating how many times to attempt the `task` before giving up. Defaults to 5.
|
| 1358 |
+
* `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)`
|
| 1359 |
+
which must be called when finished, passing `err` (which can be `null`) and the `result` of
|
| 1360 |
+
the function's execution, and (2) a `results` object, containing the results of
|
| 1361 |
+
the previously executed functions (if nested inside another control flow).
|
| 1362 |
+
* `callback(err, results)` - An optional callback which is called when the
|
| 1363 |
+
task has succeeded, or after the final failed attempt. It receives the `err` and `result` arguments of the last attempt at completing the `task`.
|
| 1364 |
+
|
| 1365 |
+
The [`retry`](#retry) function can be used as a stand-alone control flow by passing a
|
| 1366 |
+
callback, as shown below:
|
| 1367 |
+
|
| 1368 |
+
```js
|
| 1369 |
+
async.retry(3, apiMethod, function(err, result) {
|
| 1370 |
+
// do something with the result
|
| 1371 |
+
});
|
| 1372 |
+
```
|
| 1373 |
+
|
| 1374 |
+
It can also be embeded within other control flow functions to retry individual methods
|
| 1375 |
+
that are not as reliable, like this:
|
| 1376 |
+
|
| 1377 |
+
```js
|
| 1378 |
+
async.auto({
|
| 1379 |
+
users: api.getUsers.bind(api),
|
| 1380 |
+
payments: async.retry(3, api.getPayments.bind(api))
|
| 1381 |
+
}, function(err, results) {
|
| 1382 |
+
// do something with the results
|
| 1383 |
+
});
|
| 1384 |
+
```
|
| 1385 |
+
|
| 1386 |
+
|
| 1387 |
+
---------------------------------------
|
| 1388 |
+
|
| 1389 |
+
<a name="iterator" />
|
| 1390 |
+
### iterator(tasks)
|
| 1391 |
+
|
| 1392 |
+
Creates an iterator function which calls the next function in the `tasks` array,
|
| 1393 |
+
returning a continuation to call the next one after that. It's also possible to
|
| 1394 |
+
“peek” at the next iterator with `iterator.next()`.
|
| 1395 |
+
|
| 1396 |
+
This function is used internally by the `async` module, but can be useful when
|
| 1397 |
+
you want to manually control the flow of functions in series.
|
| 1398 |
+
|
| 1399 |
+
__Arguments__
|
| 1400 |
+
|
| 1401 |
+
* `tasks` - An array of functions to run.
|
| 1402 |
+
|
| 1403 |
+
__Example__
|
| 1404 |
+
|
| 1405 |
+
```js
|
| 1406 |
+
var iterator = async.iterator([
|
| 1407 |
+
function(){ sys.p('one'); },
|
| 1408 |
+
function(){ sys.p('two'); },
|
| 1409 |
+
function(){ sys.p('three'); }
|
| 1410 |
+
]);
|
| 1411 |
+
|
| 1412 |
+
node> var iterator2 = iterator();
|
| 1413 |
+
'one'
|
| 1414 |
+
node> var iterator3 = iterator2();
|
| 1415 |
+
'two'
|
| 1416 |
+
node> iterator3();
|
| 1417 |
+
'three'
|
| 1418 |
+
node> var nextfn = iterator2.next();
|
| 1419 |
+
node> nextfn();
|
| 1420 |
+
'three'
|
| 1421 |
+
```
|
| 1422 |
+
|
| 1423 |
+
---------------------------------------
|
| 1424 |
+
|
| 1425 |
+
<a name="apply" />
|
| 1426 |
+
### apply(function, arguments..)
|
| 1427 |
+
|
| 1428 |
+
Creates a continuation function with some arguments already applied.
|
| 1429 |
+
|
| 1430 |
+
Useful as a shorthand when combined with other control flow functions. Any arguments
|
| 1431 |
+
passed to the returned function are added to the arguments originally passed
|
| 1432 |
+
to apply.
|
| 1433 |
+
|
| 1434 |
+
__Arguments__
|
| 1435 |
+
|
| 1436 |
+
* `function` - The function you want to eventually apply all arguments to.
|
| 1437 |
+
* `arguments...` - Any number of arguments to automatically apply when the
|
| 1438 |
+
continuation is called.
|
| 1439 |
+
|
| 1440 |
+
__Example__
|
| 1441 |
+
|
| 1442 |
+
```js
|
| 1443 |
+
// using apply
|
| 1444 |
+
|
| 1445 |
+
async.parallel([
|
| 1446 |
+
async.apply(fs.writeFile, 'testfile1', 'test1'),
|
| 1447 |
+
async.apply(fs.writeFile, 'testfile2', 'test2'),
|
| 1448 |
+
]);
|
| 1449 |
+
|
| 1450 |
+
|
| 1451 |
+
// the same process without using apply
|
| 1452 |
+
|
| 1453 |
+
async.parallel([
|
| 1454 |
+
function(callback){
|
| 1455 |
+
fs.writeFile('testfile1', 'test1', callback);
|
| 1456 |
+
},
|
| 1457 |
+
function(callback){
|
| 1458 |
+
fs.writeFile('testfile2', 'test2', callback);
|
| 1459 |
+
}
|
| 1460 |
+
]);
|
| 1461 |
+
```
|
| 1462 |
+
|
| 1463 |
+
It's possible to pass any number of additional arguments when calling the
|
| 1464 |
+
continuation:
|
| 1465 |
+
|
| 1466 |
+
```js
|
| 1467 |
+
node> var fn = async.apply(sys.puts, 'one');
|
| 1468 |
+
node> fn('two', 'three');
|
| 1469 |
+
one
|
| 1470 |
+
two
|
| 1471 |
+
three
|
| 1472 |
+
```
|
| 1473 |
+
|
| 1474 |
+
---------------------------------------
|
| 1475 |
+
|
| 1476 |
+
<a name="nextTick" />
|
| 1477 |
+
### nextTick(callback)
|
| 1478 |
+
|
| 1479 |
+
Calls `callback` on a later loop around the event loop. In Node.js this just
|
| 1480 |
+
calls `process.nextTick`; in the browser it falls back to `setImmediate(callback)`
|
| 1481 |
+
if available, otherwise `setTimeout(callback, 0)`, which means other higher priority
|
| 1482 |
+
events may precede the execution of `callback`.
|
| 1483 |
+
|
| 1484 |
+
This is used internally for browser-compatibility purposes.
|
| 1485 |
+
|
| 1486 |
+
__Arguments__
|
| 1487 |
+
|
| 1488 |
+
* `callback` - The function to call on a later loop around the event loop.
|
| 1489 |
+
|
| 1490 |
+
__Example__
|
| 1491 |
+
|
| 1492 |
+
```js
|
| 1493 |
+
var call_order = [];
|
| 1494 |
+
async.nextTick(function(){
|
| 1495 |
+
call_order.push('two');
|
| 1496 |
+
// call_order now equals ['one','two']
|
| 1497 |
+
});
|
| 1498 |
+
call_order.push('one')
|
| 1499 |
+
```
|
| 1500 |
+
|
| 1501 |
+
<a name="times" />
|
| 1502 |
+
### times(n, callback)
|
| 1503 |
+
|
| 1504 |
+
Calls the `callback` function `n` times, and accumulates results in the same manner
|
| 1505 |
+
you would use with [`map`](#map).
|
| 1506 |
+
|
| 1507 |
+
__Arguments__
|
| 1508 |
+
|
| 1509 |
+
* `n` - The number of times to run the function.
|
| 1510 |
+
* `callback` - The function to call `n` times.
|
| 1511 |
+
|
| 1512 |
+
__Example__
|
| 1513 |
+
|
| 1514 |
+
```js
|
| 1515 |
+
// Pretend this is some complicated async factory
|
| 1516 |
+
var createUser = function(id, callback) {
|
| 1517 |
+
callback(null, {
|
| 1518 |
+
id: 'user' + id
|
| 1519 |
+
})
|
| 1520 |
+
}
|
| 1521 |
+
// generate 5 users
|
| 1522 |
+
async.times(5, function(n, next){
|
| 1523 |
+
createUser(n, function(err, user) {
|
| 1524 |
+
next(err, user)
|
| 1525 |
+
})
|
| 1526 |
+
}, function(err, users) {
|
| 1527 |
+
// we should now have 5 users
|
| 1528 |
+
});
|
| 1529 |
+
```
|
| 1530 |
+
|
| 1531 |
+
<a name="timesSeries" />
|
| 1532 |
+
### timesSeries(n, callback)
|
| 1533 |
+
|
| 1534 |
+
The same as [`times`](#times), only the iterator is applied to each item in `arr` in
|
| 1535 |
+
series. The next `iterator` is only called once the current one has completed.
|
| 1536 |
+
The results array will be in the same order as the original.
|
| 1537 |
+
|
| 1538 |
+
|
| 1539 |
+
## Utils
|
| 1540 |
+
|
| 1541 |
+
<a name="memoize" />
|
| 1542 |
+
### memoize(fn, [hasher])
|
| 1543 |
+
|
| 1544 |
+
Caches the results of an `async` function. When creating a hash to store function
|
| 1545 |
+
results against, the callback is omitted from the hash and an optional hash
|
| 1546 |
+
function can be used.
|
| 1547 |
+
|
| 1548 |
+
The cache of results is exposed as the `memo` property of the function returned
|
| 1549 |
+
by `memoize`.
|
| 1550 |
+
|
| 1551 |
+
__Arguments__
|
| 1552 |
+
|
| 1553 |
+
* `fn` - The function to proxy and cache results from.
|
| 1554 |
+
* `hasher` - Tn optional function for generating a custom hash for storing
|
| 1555 |
+
results. It has all the arguments applied to it apart from the callback, and
|
| 1556 |
+
must be synchronous.
|
| 1557 |
+
|
| 1558 |
+
__Example__
|
| 1559 |
+
|
| 1560 |
+
```js
|
| 1561 |
+
var slow_fn = function (name, callback) {
|
| 1562 |
+
// do something
|
| 1563 |
+
callback(null, result);
|
| 1564 |
+
};
|
| 1565 |
+
var fn = async.memoize(slow_fn);
|
| 1566 |
+
|
| 1567 |
+
// fn can now be used as if it were slow_fn
|
| 1568 |
+
fn('some name', function () {
|
| 1569 |
+
// callback
|
| 1570 |
+
});
|
| 1571 |
+
```
|
| 1572 |
+
|
| 1573 |
+
<a name="unmemoize" />
|
| 1574 |
+
### unmemoize(fn)
|
| 1575 |
+
|
| 1576 |
+
Undoes a [`memoize`](#memoize)d function, reverting it to the original, unmemoized
|
| 1577 |
+
form. Handy for testing.
|
| 1578 |
+
|
| 1579 |
+
__Arguments__
|
| 1580 |
+
|
| 1581 |
+
* `fn` - the memoized function
|
| 1582 |
+
|
| 1583 |
+
<a name="log" />
|
| 1584 |
+
### log(function, arguments)
|
| 1585 |
+
|
| 1586 |
+
Logs the result of an `async` function to the `console`. Only works in Node.js or
|
| 1587 |
+
in browsers that support `console.log` and `console.error` (such as FF and Chrome).
|
| 1588 |
+
If multiple arguments are returned from the async function, `console.log` is
|
| 1589 |
+
called on each argument in order.
|
| 1590 |
+
|
| 1591 |
+
__Arguments__
|
| 1592 |
+
|
| 1593 |
+
* `function` - The function you want to eventually apply all arguments to.
|
| 1594 |
+
* `arguments...` - Any number of arguments to apply to the function.
|
| 1595 |
+
|
| 1596 |
+
__Example__
|
| 1597 |
+
|
| 1598 |
+
```js
|
| 1599 |
+
var hello = function(name, callback){
|
| 1600 |
+
setTimeout(function(){
|
| 1601 |
+
callback(null, 'hello ' + name);
|
| 1602 |
+
}, 1000);
|
| 1603 |
+
};
|
| 1604 |
+
```
|
| 1605 |
+
```js
|
| 1606 |
+
node> async.log(hello, 'world');
|
| 1607 |
+
'hello world'
|
| 1608 |
+
```
|
| 1609 |
+
|
| 1610 |
+
---------------------------------------
|
| 1611 |
+
|
| 1612 |
+
<a name="dir" />
|
| 1613 |
+
### dir(function, arguments)
|
| 1614 |
+
|
| 1615 |
+
Logs the result of an `async` function to the `console` using `console.dir` to
|
| 1616 |
+
display the properties of the resulting object. Only works in Node.js or
|
| 1617 |
+
in browsers that support `console.dir` and `console.error` (such as FF and Chrome).
|
| 1618 |
+
If multiple arguments are returned from the async function, `console.dir` is
|
| 1619 |
+
called on each argument in order.
|
| 1620 |
+
|
| 1621 |
+
__Arguments__
|
| 1622 |
+
|
| 1623 |
+
* `function` - The function you want to eventually apply all arguments to.
|
| 1624 |
+
* `arguments...` - Any number of arguments to apply to the function.
|
| 1625 |
+
|
| 1626 |
+
__Example__
|
| 1627 |
+
|
| 1628 |
+
```js
|
| 1629 |
+
var hello = function(name, callback){
|
| 1630 |
+
setTimeout(function(){
|
| 1631 |
+
callback(null, {hello: name});
|
| 1632 |
+
}, 1000);
|
| 1633 |
+
};
|
| 1634 |
+
```
|
| 1635 |
+
```js
|
| 1636 |
+
node> async.dir(hello, 'world');
|
| 1637 |
+
{hello: 'world'}
|
| 1638 |
+
```
|
| 1639 |
+
|
| 1640 |
+
---------------------------------------
|
| 1641 |
+
|
| 1642 |
+
<a name="noConflict" />
|
| 1643 |
+
### noConflict()
|
| 1644 |
+
|
| 1645 |
+
Changes the value of `async` back to its original value, returning a reference to the
|
| 1646 |
+
`async` object.
|
5485-master/demos/node_modules/mongoose/node_modules/async/component.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "async",
|
| 3 |
+
"repo": "caolan/async",
|
| 4 |
+
"description": "Higher-order functions and common patterns for asynchronous code",
|
| 5 |
+
"version": "0.1.23",
|
| 6 |
+
"keywords": [],
|
| 7 |
+
"dependencies": {},
|
| 8 |
+
"development": {},
|
| 9 |
+
"main": "lib/async.js",
|
| 10 |
+
"scripts": [ "lib/async.js" ]
|
| 11 |
+
}
|
5485-master/demos/node_modules/mongoose/node_modules/async/lib/async.js
ADDED
|
@@ -0,0 +1,1123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 8 |
+
/*jshint onevar: false, indent:4 */
|
| 9 |
+
/*global setImmediate: false, setTimeout: false, console: false */
|
| 10 |
+
(function () {
|
| 11 |
+
|
| 12 |
+
var async = {};
|
| 13 |
+
|
| 14 |
+
// global on the server, window in the browser
|
| 15 |
+
var root, previous_async;
|
| 16 |
+
|
| 17 |
+
root = this;
|
| 18 |
+
if (root != null) {
|
| 19 |
+
previous_async = root.async;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
async.noConflict = function () {
|
| 23 |
+
root.async = previous_async;
|
| 24 |
+
return async;
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
function only_once(fn) {
|
| 28 |
+
var called = false;
|
| 29 |
+
return function() {
|
| 30 |
+
if (called) throw new Error("Callback was already called.");
|
| 31 |
+
called = true;
|
| 32 |
+
fn.apply(root, arguments);
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
//// cross-browser compatiblity functions ////
|
| 37 |
+
|
| 38 |
+
var _toString = Object.prototype.toString;
|
| 39 |
+
|
| 40 |
+
var _isArray = Array.isArray || function (obj) {
|
| 41 |
+
return _toString.call(obj) === '[object Array]';
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
var _each = function (arr, iterator) {
|
| 45 |
+
if (arr.forEach) {
|
| 46 |
+
return arr.forEach(iterator);
|
| 47 |
+
}
|
| 48 |
+
for (var i = 0; i < arr.length; i += 1) {
|
| 49 |
+
iterator(arr[i], i, arr);
|
| 50 |
+
}
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
var _map = function (arr, iterator) {
|
| 54 |
+
if (arr.map) {
|
| 55 |
+
return arr.map(iterator);
|
| 56 |
+
}
|
| 57 |
+
var results = [];
|
| 58 |
+
_each(arr, function (x, i, a) {
|
| 59 |
+
results.push(iterator(x, i, a));
|
| 60 |
+
});
|
| 61 |
+
return results;
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
var _reduce = function (arr, iterator, memo) {
|
| 65 |
+
if (arr.reduce) {
|
| 66 |
+
return arr.reduce(iterator, memo);
|
| 67 |
+
}
|
| 68 |
+
_each(arr, function (x, i, a) {
|
| 69 |
+
memo = iterator(memo, x, i, a);
|
| 70 |
+
});
|
| 71 |
+
return memo;
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
var _keys = function (obj) {
|
| 75 |
+
if (Object.keys) {
|
| 76 |
+
return Object.keys(obj);
|
| 77 |
+
}
|
| 78 |
+
var keys = [];
|
| 79 |
+
for (var k in obj) {
|
| 80 |
+
if (obj.hasOwnProperty(k)) {
|
| 81 |
+
keys.push(k);
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
return keys;
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
//// exported async module functions ////
|
| 88 |
+
|
| 89 |
+
//// nextTick implementation with browser-compatible fallback ////
|
| 90 |
+
if (typeof process === 'undefined' || !(process.nextTick)) {
|
| 91 |
+
if (typeof setImmediate === 'function') {
|
| 92 |
+
async.nextTick = function (fn) {
|
| 93 |
+
// not a direct alias for IE10 compatibility
|
| 94 |
+
setImmediate(fn);
|
| 95 |
+
};
|
| 96 |
+
async.setImmediate = async.nextTick;
|
| 97 |
+
}
|
| 98 |
+
else {
|
| 99 |
+
async.nextTick = function (fn) {
|
| 100 |
+
setTimeout(fn, 0);
|
| 101 |
+
};
|
| 102 |
+
async.setImmediate = async.nextTick;
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
else {
|
| 106 |
+
async.nextTick = process.nextTick;
|
| 107 |
+
if (typeof setImmediate !== 'undefined') {
|
| 108 |
+
async.setImmediate = function (fn) {
|
| 109 |
+
// not a direct alias for IE10 compatibility
|
| 110 |
+
setImmediate(fn);
|
| 111 |
+
};
|
| 112 |
+
}
|
| 113 |
+
else {
|
| 114 |
+
async.setImmediate = async.nextTick;
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
async.each = function (arr, iterator, callback) {
|
| 119 |
+
callback = callback || function () {};
|
| 120 |
+
if (!arr.length) {
|
| 121 |
+
return callback();
|
| 122 |
+
}
|
| 123 |
+
var completed = 0;
|
| 124 |
+
_each(arr, function (x) {
|
| 125 |
+
iterator(x, only_once(done) );
|
| 126 |
+
});
|
| 127 |
+
function done(err) {
|
| 128 |
+
if (err) {
|
| 129 |
+
callback(err);
|
| 130 |
+
callback = function () {};
|
| 131 |
+
}
|
| 132 |
+
else {
|
| 133 |
+
completed += 1;
|
| 134 |
+
if (completed >= arr.length) {
|
| 135 |
+
callback();
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
};
|
| 140 |
+
async.forEach = async.each;
|
| 141 |
+
|
| 142 |
+
async.eachSeries = function (arr, iterator, callback) {
|
| 143 |
+
callback = callback || function () {};
|
| 144 |
+
if (!arr.length) {
|
| 145 |
+
return callback();
|
| 146 |
+
}
|
| 147 |
+
var completed = 0;
|
| 148 |
+
var iterate = function () {
|
| 149 |
+
iterator(arr[completed], function (err) {
|
| 150 |
+
if (err) {
|
| 151 |
+
callback(err);
|
| 152 |
+
callback = function () {};
|
| 153 |
+
}
|
| 154 |
+
else {
|
| 155 |
+
completed += 1;
|
| 156 |
+
if (completed >= arr.length) {
|
| 157 |
+
callback();
|
| 158 |
+
}
|
| 159 |
+
else {
|
| 160 |
+
iterate();
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
});
|
| 164 |
+
};
|
| 165 |
+
iterate();
|
| 166 |
+
};
|
| 167 |
+
async.forEachSeries = async.eachSeries;
|
| 168 |
+
|
| 169 |
+
async.eachLimit = function (arr, limit, iterator, callback) {
|
| 170 |
+
var fn = _eachLimit(limit);
|
| 171 |
+
fn.apply(null, [arr, iterator, callback]);
|
| 172 |
+
};
|
| 173 |
+
async.forEachLimit = async.eachLimit;
|
| 174 |
+
|
| 175 |
+
var _eachLimit = function (limit) {
|
| 176 |
+
|
| 177 |
+
return function (arr, iterator, callback) {
|
| 178 |
+
callback = callback || function () {};
|
| 179 |
+
if (!arr.length || limit <= 0) {
|
| 180 |
+
return callback();
|
| 181 |
+
}
|
| 182 |
+
var completed = 0;
|
| 183 |
+
var started = 0;
|
| 184 |
+
var running = 0;
|
| 185 |
+
|
| 186 |
+
(function replenish () {
|
| 187 |
+
if (completed >= arr.length) {
|
| 188 |
+
return callback();
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
while (running < limit && started < arr.length) {
|
| 192 |
+
started += 1;
|
| 193 |
+
running += 1;
|
| 194 |
+
iterator(arr[started - 1], function (err) {
|
| 195 |
+
if (err) {
|
| 196 |
+
callback(err);
|
| 197 |
+
callback = function () {};
|
| 198 |
+
}
|
| 199 |
+
else {
|
| 200 |
+
completed += 1;
|
| 201 |
+
running -= 1;
|
| 202 |
+
if (completed >= arr.length) {
|
| 203 |
+
callback();
|
| 204 |
+
}
|
| 205 |
+
else {
|
| 206 |
+
replenish();
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
});
|
| 210 |
+
}
|
| 211 |
+
})();
|
| 212 |
+
};
|
| 213 |
+
};
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
var doParallel = function (fn) {
|
| 217 |
+
return function () {
|
| 218 |
+
var args = Array.prototype.slice.call(arguments);
|
| 219 |
+
return fn.apply(null, [async.each].concat(args));
|
| 220 |
+
};
|
| 221 |
+
};
|
| 222 |
+
var doParallelLimit = function(limit, fn) {
|
| 223 |
+
return function () {
|
| 224 |
+
var args = Array.prototype.slice.call(arguments);
|
| 225 |
+
return fn.apply(null, [_eachLimit(limit)].concat(args));
|
| 226 |
+
};
|
| 227 |
+
};
|
| 228 |
+
var doSeries = function (fn) {
|
| 229 |
+
return function () {
|
| 230 |
+
var args = Array.prototype.slice.call(arguments);
|
| 231 |
+
return fn.apply(null, [async.eachSeries].concat(args));
|
| 232 |
+
};
|
| 233 |
+
};
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
var _asyncMap = function (eachfn, arr, iterator, callback) {
|
| 237 |
+
arr = _map(arr, function (x, i) {
|
| 238 |
+
return {index: i, value: x};
|
| 239 |
+
});
|
| 240 |
+
if (!callback) {
|
| 241 |
+
eachfn(arr, function (x, callback) {
|
| 242 |
+
iterator(x.value, function (err) {
|
| 243 |
+
callback(err);
|
| 244 |
+
});
|
| 245 |
+
});
|
| 246 |
+
} else {
|
| 247 |
+
var results = [];
|
| 248 |
+
eachfn(arr, function (x, callback) {
|
| 249 |
+
iterator(x.value, function (err, v) {
|
| 250 |
+
results[x.index] = v;
|
| 251 |
+
callback(err);
|
| 252 |
+
});
|
| 253 |
+
}, function (err) {
|
| 254 |
+
callback(err, results);
|
| 255 |
+
});
|
| 256 |
+
}
|
| 257 |
+
};
|
| 258 |
+
async.map = doParallel(_asyncMap);
|
| 259 |
+
async.mapSeries = doSeries(_asyncMap);
|
| 260 |
+
async.mapLimit = function (arr, limit, iterator, callback) {
|
| 261 |
+
return _mapLimit(limit)(arr, iterator, callback);
|
| 262 |
+
};
|
| 263 |
+
|
| 264 |
+
var _mapLimit = function(limit) {
|
| 265 |
+
return doParallelLimit(limit, _asyncMap);
|
| 266 |
+
};
|
| 267 |
+
|
| 268 |
+
// reduce only has a series version, as doing reduce in parallel won't
|
| 269 |
+
// work in many situations.
|
| 270 |
+
async.reduce = function (arr, memo, iterator, callback) {
|
| 271 |
+
async.eachSeries(arr, function (x, callback) {
|
| 272 |
+
iterator(memo, x, function (err, v) {
|
| 273 |
+
memo = v;
|
| 274 |
+
callback(err);
|
| 275 |
+
});
|
| 276 |
+
}, function (err) {
|
| 277 |
+
callback(err, memo);
|
| 278 |
+
});
|
| 279 |
+
};
|
| 280 |
+
// inject alias
|
| 281 |
+
async.inject = async.reduce;
|
| 282 |
+
// foldl alias
|
| 283 |
+
async.foldl = async.reduce;
|
| 284 |
+
|
| 285 |
+
async.reduceRight = function (arr, memo, iterator, callback) {
|
| 286 |
+
var reversed = _map(arr, function (x) {
|
| 287 |
+
return x;
|
| 288 |
+
}).reverse();
|
| 289 |
+
async.reduce(reversed, memo, iterator, callback);
|
| 290 |
+
};
|
| 291 |
+
// foldr alias
|
| 292 |
+
async.foldr = async.reduceRight;
|
| 293 |
+
|
| 294 |
+
var _filter = function (eachfn, arr, iterator, callback) {
|
| 295 |
+
var results = [];
|
| 296 |
+
arr = _map(arr, function (x, i) {
|
| 297 |
+
return {index: i, value: x};
|
| 298 |
+
});
|
| 299 |
+
eachfn(arr, function (x, callback) {
|
| 300 |
+
iterator(x.value, function (v) {
|
| 301 |
+
if (v) {
|
| 302 |
+
results.push(x);
|
| 303 |
+
}
|
| 304 |
+
callback();
|
| 305 |
+
});
|
| 306 |
+
}, function (err) {
|
| 307 |
+
callback(_map(results.sort(function (a, b) {
|
| 308 |
+
return a.index - b.index;
|
| 309 |
+
}), function (x) {
|
| 310 |
+
return x.value;
|
| 311 |
+
}));
|
| 312 |
+
});
|
| 313 |
+
};
|
| 314 |
+
async.filter = doParallel(_filter);
|
| 315 |
+
async.filterSeries = doSeries(_filter);
|
| 316 |
+
// select alias
|
| 317 |
+
async.select = async.filter;
|
| 318 |
+
async.selectSeries = async.filterSeries;
|
| 319 |
+
|
| 320 |
+
var _reject = function (eachfn, arr, iterator, callback) {
|
| 321 |
+
var results = [];
|
| 322 |
+
arr = _map(arr, function (x, i) {
|
| 323 |
+
return {index: i, value: x};
|
| 324 |
+
});
|
| 325 |
+
eachfn(arr, function (x, callback) {
|
| 326 |
+
iterator(x.value, function (v) {
|
| 327 |
+
if (!v) {
|
| 328 |
+
results.push(x);
|
| 329 |
+
}
|
| 330 |
+
callback();
|
| 331 |
+
});
|
| 332 |
+
}, function (err) {
|
| 333 |
+
callback(_map(results.sort(function (a, b) {
|
| 334 |
+
return a.index - b.index;
|
| 335 |
+
}), function (x) {
|
| 336 |
+
return x.value;
|
| 337 |
+
}));
|
| 338 |
+
});
|
| 339 |
+
};
|
| 340 |
+
async.reject = doParallel(_reject);
|
| 341 |
+
async.rejectSeries = doSeries(_reject);
|
| 342 |
+
|
| 343 |
+
var _detect = function (eachfn, arr, iterator, main_callback) {
|
| 344 |
+
eachfn(arr, function (x, callback) {
|
| 345 |
+
iterator(x, function (result) {
|
| 346 |
+
if (result) {
|
| 347 |
+
main_callback(x);
|
| 348 |
+
main_callback = function () {};
|
| 349 |
+
}
|
| 350 |
+
else {
|
| 351 |
+
callback();
|
| 352 |
+
}
|
| 353 |
+
});
|
| 354 |
+
}, function (err) {
|
| 355 |
+
main_callback();
|
| 356 |
+
});
|
| 357 |
+
};
|
| 358 |
+
async.detect = doParallel(_detect);
|
| 359 |
+
async.detectSeries = doSeries(_detect);
|
| 360 |
+
|
| 361 |
+
async.some = function (arr, iterator, main_callback) {
|
| 362 |
+
async.each(arr, function (x, callback) {
|
| 363 |
+
iterator(x, function (v) {
|
| 364 |
+
if (v) {
|
| 365 |
+
main_callback(true);
|
| 366 |
+
main_callback = function () {};
|
| 367 |
+
}
|
| 368 |
+
callback();
|
| 369 |
+
});
|
| 370 |
+
}, function (err) {
|
| 371 |
+
main_callback(false);
|
| 372 |
+
});
|
| 373 |
+
};
|
| 374 |
+
// any alias
|
| 375 |
+
async.any = async.some;
|
| 376 |
+
|
| 377 |
+
async.every = function (arr, iterator, main_callback) {
|
| 378 |
+
async.each(arr, function (x, callback) {
|
| 379 |
+
iterator(x, function (v) {
|
| 380 |
+
if (!v) {
|
| 381 |
+
main_callback(false);
|
| 382 |
+
main_callback = function () {};
|
| 383 |
+
}
|
| 384 |
+
callback();
|
| 385 |
+
});
|
| 386 |
+
}, function (err) {
|
| 387 |
+
main_callback(true);
|
| 388 |
+
});
|
| 389 |
+
};
|
| 390 |
+
// all alias
|
| 391 |
+
async.all = async.every;
|
| 392 |
+
|
| 393 |
+
async.sortBy = function (arr, iterator, callback) {
|
| 394 |
+
async.map(arr, function (x, callback) {
|
| 395 |
+
iterator(x, function (err, criteria) {
|
| 396 |
+
if (err) {
|
| 397 |
+
callback(err);
|
| 398 |
+
}
|
| 399 |
+
else {
|
| 400 |
+
callback(null, {value: x, criteria: criteria});
|
| 401 |
+
}
|
| 402 |
+
});
|
| 403 |
+
}, function (err, results) {
|
| 404 |
+
if (err) {
|
| 405 |
+
return callback(err);
|
| 406 |
+
}
|
| 407 |
+
else {
|
| 408 |
+
var fn = function (left, right) {
|
| 409 |
+
var a = left.criteria, b = right.criteria;
|
| 410 |
+
return a < b ? -1 : a > b ? 1 : 0;
|
| 411 |
+
};
|
| 412 |
+
callback(null, _map(results.sort(fn), function (x) {
|
| 413 |
+
return x.value;
|
| 414 |
+
}));
|
| 415 |
+
}
|
| 416 |
+
});
|
| 417 |
+
};
|
| 418 |
+
|
| 419 |
+
async.auto = function (tasks, callback) {
|
| 420 |
+
callback = callback || function () {};
|
| 421 |
+
var keys = _keys(tasks);
|
| 422 |
+
var remainingTasks = keys.length
|
| 423 |
+
if (!remainingTasks) {
|
| 424 |
+
return callback();
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
var results = {};
|
| 428 |
+
|
| 429 |
+
var listeners = [];
|
| 430 |
+
var addListener = function (fn) {
|
| 431 |
+
listeners.unshift(fn);
|
| 432 |
+
};
|
| 433 |
+
var removeListener = function (fn) {
|
| 434 |
+
for (var i = 0; i < listeners.length; i += 1) {
|
| 435 |
+
if (listeners[i] === fn) {
|
| 436 |
+
listeners.splice(i, 1);
|
| 437 |
+
return;
|
| 438 |
+
}
|
| 439 |
+
}
|
| 440 |
+
};
|
| 441 |
+
var taskComplete = function () {
|
| 442 |
+
remainingTasks--
|
| 443 |
+
_each(listeners.slice(0), function (fn) {
|
| 444 |
+
fn();
|
| 445 |
+
});
|
| 446 |
+
};
|
| 447 |
+
|
| 448 |
+
addListener(function () {
|
| 449 |
+
if (!remainingTasks) {
|
| 450 |
+
var theCallback = callback;
|
| 451 |
+
// prevent final callback from calling itself if it errors
|
| 452 |
+
callback = function () {};
|
| 453 |
+
|
| 454 |
+
theCallback(null, results);
|
| 455 |
+
}
|
| 456 |
+
});
|
| 457 |
+
|
| 458 |
+
_each(keys, function (k) {
|
| 459 |
+
var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]];
|
| 460 |
+
var taskCallback = function (err) {
|
| 461 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 462 |
+
if (args.length <= 1) {
|
| 463 |
+
args = args[0];
|
| 464 |
+
}
|
| 465 |
+
if (err) {
|
| 466 |
+
var safeResults = {};
|
| 467 |
+
_each(_keys(results), function(rkey) {
|
| 468 |
+
safeResults[rkey] = results[rkey];
|
| 469 |
+
});
|
| 470 |
+
safeResults[k] = args;
|
| 471 |
+
callback(err, safeResults);
|
| 472 |
+
// stop subsequent errors hitting callback multiple times
|
| 473 |
+
callback = function () {};
|
| 474 |
+
}
|
| 475 |
+
else {
|
| 476 |
+
results[k] = args;
|
| 477 |
+
async.setImmediate(taskComplete);
|
| 478 |
+
}
|
| 479 |
+
};
|
| 480 |
+
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
|
| 481 |
+
var ready = function () {
|
| 482 |
+
return _reduce(requires, function (a, x) {
|
| 483 |
+
return (a && results.hasOwnProperty(x));
|
| 484 |
+
}, true) && !results.hasOwnProperty(k);
|
| 485 |
+
};
|
| 486 |
+
if (ready()) {
|
| 487 |
+
task[task.length - 1](taskCallback, results);
|
| 488 |
+
}
|
| 489 |
+
else {
|
| 490 |
+
var listener = function () {
|
| 491 |
+
if (ready()) {
|
| 492 |
+
removeListener(listener);
|
| 493 |
+
task[task.length - 1](taskCallback, results);
|
| 494 |
+
}
|
| 495 |
+
};
|
| 496 |
+
addListener(listener);
|
| 497 |
+
}
|
| 498 |
+
});
|
| 499 |
+
};
|
| 500 |
+
|
| 501 |
+
async.retry = function(times, task, callback) {
|
| 502 |
+
var DEFAULT_TIMES = 5;
|
| 503 |
+
var attempts = [];
|
| 504 |
+
// Use defaults if times not passed
|
| 505 |
+
if (typeof times === 'function') {
|
| 506 |
+
callback = task;
|
| 507 |
+
task = times;
|
| 508 |
+
times = DEFAULT_TIMES;
|
| 509 |
+
}
|
| 510 |
+
// Make sure times is a number
|
| 511 |
+
times = parseInt(times, 10) || DEFAULT_TIMES;
|
| 512 |
+
var wrappedTask = function(wrappedCallback, wrappedResults) {
|
| 513 |
+
var retryAttempt = function(task, finalAttempt) {
|
| 514 |
+
return function(seriesCallback) {
|
| 515 |
+
task(function(err, result){
|
| 516 |
+
seriesCallback(!err || finalAttempt, {err: err, result: result});
|
| 517 |
+
}, wrappedResults);
|
| 518 |
+
};
|
| 519 |
+
};
|
| 520 |
+
while (times) {
|
| 521 |
+
attempts.push(retryAttempt(task, !(times-=1)));
|
| 522 |
+
}
|
| 523 |
+
async.series(attempts, function(done, data){
|
| 524 |
+
data = data[data.length - 1];
|
| 525 |
+
(wrappedCallback || callback)(data.err, data.result);
|
| 526 |
+
});
|
| 527 |
+
}
|
| 528 |
+
// If a callback is passed, run this as a controll flow
|
| 529 |
+
return callback ? wrappedTask() : wrappedTask
|
| 530 |
+
};
|
| 531 |
+
|
| 532 |
+
async.waterfall = function (tasks, callback) {
|
| 533 |
+
callback = callback || function () {};
|
| 534 |
+
if (!_isArray(tasks)) {
|
| 535 |
+
var err = new Error('First argument to waterfall must be an array of functions');
|
| 536 |
+
return callback(err);
|
| 537 |
+
}
|
| 538 |
+
if (!tasks.length) {
|
| 539 |
+
return callback();
|
| 540 |
+
}
|
| 541 |
+
var wrapIterator = function (iterator) {
|
| 542 |
+
return function (err) {
|
| 543 |
+
if (err) {
|
| 544 |
+
callback.apply(null, arguments);
|
| 545 |
+
callback = function () {};
|
| 546 |
+
}
|
| 547 |
+
else {
|
| 548 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 549 |
+
var next = iterator.next();
|
| 550 |
+
if (next) {
|
| 551 |
+
args.push(wrapIterator(next));
|
| 552 |
+
}
|
| 553 |
+
else {
|
| 554 |
+
args.push(callback);
|
| 555 |
+
}
|
| 556 |
+
async.setImmediate(function () {
|
| 557 |
+
iterator.apply(null, args);
|
| 558 |
+
});
|
| 559 |
+
}
|
| 560 |
+
};
|
| 561 |
+
};
|
| 562 |
+
wrapIterator(async.iterator(tasks))();
|
| 563 |
+
};
|
| 564 |
+
|
| 565 |
+
var _parallel = function(eachfn, tasks, callback) {
|
| 566 |
+
callback = callback || function () {};
|
| 567 |
+
if (_isArray(tasks)) {
|
| 568 |
+
eachfn.map(tasks, function (fn, callback) {
|
| 569 |
+
if (fn) {
|
| 570 |
+
fn(function (err) {
|
| 571 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 572 |
+
if (args.length <= 1) {
|
| 573 |
+
args = args[0];
|
| 574 |
+
}
|
| 575 |
+
callback.call(null, err, args);
|
| 576 |
+
});
|
| 577 |
+
}
|
| 578 |
+
}, callback);
|
| 579 |
+
}
|
| 580 |
+
else {
|
| 581 |
+
var results = {};
|
| 582 |
+
eachfn.each(_keys(tasks), function (k, callback) {
|
| 583 |
+
tasks[k](function (err) {
|
| 584 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 585 |
+
if (args.length <= 1) {
|
| 586 |
+
args = args[0];
|
| 587 |
+
}
|
| 588 |
+
results[k] = args;
|
| 589 |
+
callback(err);
|
| 590 |
+
});
|
| 591 |
+
}, function (err) {
|
| 592 |
+
callback(err, results);
|
| 593 |
+
});
|
| 594 |
+
}
|
| 595 |
+
};
|
| 596 |
+
|
| 597 |
+
async.parallel = function (tasks, callback) {
|
| 598 |
+
_parallel({ map: async.map, each: async.each }, tasks, callback);
|
| 599 |
+
};
|
| 600 |
+
|
| 601 |
+
async.parallelLimit = function(tasks, limit, callback) {
|
| 602 |
+
_parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback);
|
| 603 |
+
};
|
| 604 |
+
|
| 605 |
+
async.series = function (tasks, callback) {
|
| 606 |
+
callback = callback || function () {};
|
| 607 |
+
if (_isArray(tasks)) {
|
| 608 |
+
async.mapSeries(tasks, function (fn, callback) {
|
| 609 |
+
if (fn) {
|
| 610 |
+
fn(function (err) {
|
| 611 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 612 |
+
if (args.length <= 1) {
|
| 613 |
+
args = args[0];
|
| 614 |
+
}
|
| 615 |
+
callback.call(null, err, args);
|
| 616 |
+
});
|
| 617 |
+
}
|
| 618 |
+
}, callback);
|
| 619 |
+
}
|
| 620 |
+
else {
|
| 621 |
+
var results = {};
|
| 622 |
+
async.eachSeries(_keys(tasks), function (k, callback) {
|
| 623 |
+
tasks[k](function (err) {
|
| 624 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 625 |
+
if (args.length <= 1) {
|
| 626 |
+
args = args[0];
|
| 627 |
+
}
|
| 628 |
+
results[k] = args;
|
| 629 |
+
callback(err);
|
| 630 |
+
});
|
| 631 |
+
}, function (err) {
|
| 632 |
+
callback(err, results);
|
| 633 |
+
});
|
| 634 |
+
}
|
| 635 |
+
};
|
| 636 |
+
|
| 637 |
+
async.iterator = function (tasks) {
|
| 638 |
+
var makeCallback = function (index) {
|
| 639 |
+
var fn = function () {
|
| 640 |
+
if (tasks.length) {
|
| 641 |
+
tasks[index].apply(null, arguments);
|
| 642 |
+
}
|
| 643 |
+
return fn.next();
|
| 644 |
+
};
|
| 645 |
+
fn.next = function () {
|
| 646 |
+
return (index < tasks.length - 1) ? makeCallback(index + 1): null;
|
| 647 |
+
};
|
| 648 |
+
return fn;
|
| 649 |
+
};
|
| 650 |
+
return makeCallback(0);
|
| 651 |
+
};
|
| 652 |
+
|
| 653 |
+
async.apply = function (fn) {
|
| 654 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 655 |
+
return function () {
|
| 656 |
+
return fn.apply(
|
| 657 |
+
null, args.concat(Array.prototype.slice.call(arguments))
|
| 658 |
+
);
|
| 659 |
+
};
|
| 660 |
+
};
|
| 661 |
+
|
| 662 |
+
var _concat = function (eachfn, arr, fn, callback) {
|
| 663 |
+
var r = [];
|
| 664 |
+
eachfn(arr, function (x, cb) {
|
| 665 |
+
fn(x, function (err, y) {
|
| 666 |
+
r = r.concat(y || []);
|
| 667 |
+
cb(err);
|
| 668 |
+
});
|
| 669 |
+
}, function (err) {
|
| 670 |
+
callback(err, r);
|
| 671 |
+
});
|
| 672 |
+
};
|
| 673 |
+
async.concat = doParallel(_concat);
|
| 674 |
+
async.concatSeries = doSeries(_concat);
|
| 675 |
+
|
| 676 |
+
async.whilst = function (test, iterator, callback) {
|
| 677 |
+
if (test()) {
|
| 678 |
+
iterator(function (err) {
|
| 679 |
+
if (err) {
|
| 680 |
+
return callback(err);
|
| 681 |
+
}
|
| 682 |
+
async.whilst(test, iterator, callback);
|
| 683 |
+
});
|
| 684 |
+
}
|
| 685 |
+
else {
|
| 686 |
+
callback();
|
| 687 |
+
}
|
| 688 |
+
};
|
| 689 |
+
|
| 690 |
+
async.doWhilst = function (iterator, test, callback) {
|
| 691 |
+
iterator(function (err) {
|
| 692 |
+
if (err) {
|
| 693 |
+
return callback(err);
|
| 694 |
+
}
|
| 695 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 696 |
+
if (test.apply(null, args)) {
|
| 697 |
+
async.doWhilst(iterator, test, callback);
|
| 698 |
+
}
|
| 699 |
+
else {
|
| 700 |
+
callback();
|
| 701 |
+
}
|
| 702 |
+
});
|
| 703 |
+
};
|
| 704 |
+
|
| 705 |
+
async.until = function (test, iterator, callback) {
|
| 706 |
+
if (!test()) {
|
| 707 |
+
iterator(function (err) {
|
| 708 |
+
if (err) {
|
| 709 |
+
return callback(err);
|
| 710 |
+
}
|
| 711 |
+
async.until(test, iterator, callback);
|
| 712 |
+
});
|
| 713 |
+
}
|
| 714 |
+
else {
|
| 715 |
+
callback();
|
| 716 |
+
}
|
| 717 |
+
};
|
| 718 |
+
|
| 719 |
+
async.doUntil = function (iterator, test, callback) {
|
| 720 |
+
iterator(function (err) {
|
| 721 |
+
if (err) {
|
| 722 |
+
return callback(err);
|
| 723 |
+
}
|
| 724 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 725 |
+
if (!test.apply(null, args)) {
|
| 726 |
+
async.doUntil(iterator, test, callback);
|
| 727 |
+
}
|
| 728 |
+
else {
|
| 729 |
+
callback();
|
| 730 |
+
}
|
| 731 |
+
});
|
| 732 |
+
};
|
| 733 |
+
|
| 734 |
+
async.queue = function (worker, concurrency) {
|
| 735 |
+
if (concurrency === undefined) {
|
| 736 |
+
concurrency = 1;
|
| 737 |
+
}
|
| 738 |
+
function _insert(q, data, pos, callback) {
|
| 739 |
+
if (!q.started){
|
| 740 |
+
q.started = true;
|
| 741 |
+
}
|
| 742 |
+
if (!_isArray(data)) {
|
| 743 |
+
data = [data];
|
| 744 |
+
}
|
| 745 |
+
if(data.length == 0) {
|
| 746 |
+
// call drain immediately if there are no tasks
|
| 747 |
+
return async.setImmediate(function() {
|
| 748 |
+
if (q.drain) {
|
| 749 |
+
q.drain();
|
| 750 |
+
}
|
| 751 |
+
});
|
| 752 |
+
}
|
| 753 |
+
_each(data, function(task) {
|
| 754 |
+
var item = {
|
| 755 |
+
data: task,
|
| 756 |
+
callback: typeof callback === 'function' ? callback : null
|
| 757 |
+
};
|
| 758 |
+
|
| 759 |
+
if (pos) {
|
| 760 |
+
q.tasks.unshift(item);
|
| 761 |
+
} else {
|
| 762 |
+
q.tasks.push(item);
|
| 763 |
+
}
|
| 764 |
+
|
| 765 |
+
if (q.saturated && q.tasks.length === q.concurrency) {
|
| 766 |
+
q.saturated();
|
| 767 |
+
}
|
| 768 |
+
async.setImmediate(q.process);
|
| 769 |
+
});
|
| 770 |
+
}
|
| 771 |
+
|
| 772 |
+
var workers = 0;
|
| 773 |
+
var q = {
|
| 774 |
+
tasks: [],
|
| 775 |
+
concurrency: concurrency,
|
| 776 |
+
saturated: null,
|
| 777 |
+
empty: null,
|
| 778 |
+
drain: null,
|
| 779 |
+
started: false,
|
| 780 |
+
paused: false,
|
| 781 |
+
push: function (data, callback) {
|
| 782 |
+
_insert(q, data, false, callback);
|
| 783 |
+
},
|
| 784 |
+
kill: function () {
|
| 785 |
+
q.drain = null;
|
| 786 |
+
q.tasks = [];
|
| 787 |
+
},
|
| 788 |
+
unshift: function (data, callback) {
|
| 789 |
+
_insert(q, data, true, callback);
|
| 790 |
+
},
|
| 791 |
+
process: function () {
|
| 792 |
+
if (!q.paused && workers < q.concurrency && q.tasks.length) {
|
| 793 |
+
var task = q.tasks.shift();
|
| 794 |
+
if (q.empty && q.tasks.length === 0) {
|
| 795 |
+
q.empty();
|
| 796 |
+
}
|
| 797 |
+
workers += 1;
|
| 798 |
+
var next = function () {
|
| 799 |
+
workers -= 1;
|
| 800 |
+
if (task.callback) {
|
| 801 |
+
task.callback.apply(task, arguments);
|
| 802 |
+
}
|
| 803 |
+
if (q.drain && q.tasks.length + workers === 0) {
|
| 804 |
+
q.drain();
|
| 805 |
+
}
|
| 806 |
+
q.process();
|
| 807 |
+
};
|
| 808 |
+
var cb = only_once(next);
|
| 809 |
+
worker(task.data, cb);
|
| 810 |
+
}
|
| 811 |
+
},
|
| 812 |
+
length: function () {
|
| 813 |
+
return q.tasks.length;
|
| 814 |
+
},
|
| 815 |
+
running: function () {
|
| 816 |
+
return workers;
|
| 817 |
+
},
|
| 818 |
+
idle: function() {
|
| 819 |
+
return q.tasks.length + workers === 0;
|
| 820 |
+
},
|
| 821 |
+
pause: function () {
|
| 822 |
+
if (q.paused === true) { return; }
|
| 823 |
+
q.paused = true;
|
| 824 |
+
q.process();
|
| 825 |
+
},
|
| 826 |
+
resume: function () {
|
| 827 |
+
if (q.paused === false) { return; }
|
| 828 |
+
q.paused = false;
|
| 829 |
+
q.process();
|
| 830 |
+
}
|
| 831 |
+
};
|
| 832 |
+
return q;
|
| 833 |
+
};
|
| 834 |
+
|
| 835 |
+
async.priorityQueue = function (worker, concurrency) {
|
| 836 |
+
|
| 837 |
+
function _compareTasks(a, b){
|
| 838 |
+
return a.priority - b.priority;
|
| 839 |
+
};
|
| 840 |
+
|
| 841 |
+
function _binarySearch(sequence, item, compare) {
|
| 842 |
+
var beg = -1,
|
| 843 |
+
end = sequence.length - 1;
|
| 844 |
+
while (beg < end) {
|
| 845 |
+
var mid = beg + ((end - beg + 1) >>> 1);
|
| 846 |
+
if (compare(item, sequence[mid]) >= 0) {
|
| 847 |
+
beg = mid;
|
| 848 |
+
} else {
|
| 849 |
+
end = mid - 1;
|
| 850 |
+
}
|
| 851 |
+
}
|
| 852 |
+
return beg;
|
| 853 |
+
}
|
| 854 |
+
|
| 855 |
+
function _insert(q, data, priority, callback) {
|
| 856 |
+
if (!q.started){
|
| 857 |
+
q.started = true;
|
| 858 |
+
}
|
| 859 |
+
if (!_isArray(data)) {
|
| 860 |
+
data = [data];
|
| 861 |
+
}
|
| 862 |
+
if(data.length == 0) {
|
| 863 |
+
// call drain immediately if there are no tasks
|
| 864 |
+
return async.setImmediate(function() {
|
| 865 |
+
if (q.drain) {
|
| 866 |
+
q.drain();
|
| 867 |
+
}
|
| 868 |
+
});
|
| 869 |
+
}
|
| 870 |
+
_each(data, function(task) {
|
| 871 |
+
var item = {
|
| 872 |
+
data: task,
|
| 873 |
+
priority: priority,
|
| 874 |
+
callback: typeof callback === 'function' ? callback : null
|
| 875 |
+
};
|
| 876 |
+
|
| 877 |
+
q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item);
|
| 878 |
+
|
| 879 |
+
if (q.saturated && q.tasks.length === q.concurrency) {
|
| 880 |
+
q.saturated();
|
| 881 |
+
}
|
| 882 |
+
async.setImmediate(q.process);
|
| 883 |
+
});
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
+
// Start with a normal queue
|
| 887 |
+
var q = async.queue(worker, concurrency);
|
| 888 |
+
|
| 889 |
+
// Override push to accept second parameter representing priority
|
| 890 |
+
q.push = function (data, priority, callback) {
|
| 891 |
+
_insert(q, data, priority, callback);
|
| 892 |
+
};
|
| 893 |
+
|
| 894 |
+
// Remove unshift function
|
| 895 |
+
delete q.unshift;
|
| 896 |
+
|
| 897 |
+
return q;
|
| 898 |
+
};
|
| 899 |
+
|
| 900 |
+
async.cargo = function (worker, payload) {
|
| 901 |
+
var working = false,
|
| 902 |
+
tasks = [];
|
| 903 |
+
|
| 904 |
+
var cargo = {
|
| 905 |
+
tasks: tasks,
|
| 906 |
+
payload: payload,
|
| 907 |
+
saturated: null,
|
| 908 |
+
empty: null,
|
| 909 |
+
drain: null,
|
| 910 |
+
drained: true,
|
| 911 |
+
push: function (data, callback) {
|
| 912 |
+
if (!_isArray(data)) {
|
| 913 |
+
data = [data];
|
| 914 |
+
}
|
| 915 |
+
_each(data, function(task) {
|
| 916 |
+
tasks.push({
|
| 917 |
+
data: task,
|
| 918 |
+
callback: typeof callback === 'function' ? callback : null
|
| 919 |
+
});
|
| 920 |
+
cargo.drained = false;
|
| 921 |
+
if (cargo.saturated && tasks.length === payload) {
|
| 922 |
+
cargo.saturated();
|
| 923 |
+
}
|
| 924 |
+
});
|
| 925 |
+
async.setImmediate(cargo.process);
|
| 926 |
+
},
|
| 927 |
+
process: function process() {
|
| 928 |
+
if (working) return;
|
| 929 |
+
if (tasks.length === 0) {
|
| 930 |
+
if(cargo.drain && !cargo.drained) cargo.drain();
|
| 931 |
+
cargo.drained = true;
|
| 932 |
+
return;
|
| 933 |
+
}
|
| 934 |
+
|
| 935 |
+
var ts = typeof payload === 'number'
|
| 936 |
+
? tasks.splice(0, payload)
|
| 937 |
+
: tasks.splice(0, tasks.length);
|
| 938 |
+
|
| 939 |
+
var ds = _map(ts, function (task) {
|
| 940 |
+
return task.data;
|
| 941 |
+
});
|
| 942 |
+
|
| 943 |
+
if(cargo.empty) cargo.empty();
|
| 944 |
+
working = true;
|
| 945 |
+
worker(ds, function () {
|
| 946 |
+
working = false;
|
| 947 |
+
|
| 948 |
+
var args = arguments;
|
| 949 |
+
_each(ts, function (data) {
|
| 950 |
+
if (data.callback) {
|
| 951 |
+
data.callback.apply(null, args);
|
| 952 |
+
}
|
| 953 |
+
});
|
| 954 |
+
|
| 955 |
+
process();
|
| 956 |
+
});
|
| 957 |
+
},
|
| 958 |
+
length: function () {
|
| 959 |
+
return tasks.length;
|
| 960 |
+
},
|
| 961 |
+
running: function () {
|
| 962 |
+
return working;
|
| 963 |
+
}
|
| 964 |
+
};
|
| 965 |
+
return cargo;
|
| 966 |
+
};
|
| 967 |
+
|
| 968 |
+
var _console_fn = function (name) {
|
| 969 |
+
return function (fn) {
|
| 970 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 971 |
+
fn.apply(null, args.concat([function (err) {
|
| 972 |
+
var args = Array.prototype.slice.call(arguments, 1);
|
| 973 |
+
if (typeof console !== 'undefined') {
|
| 974 |
+
if (err) {
|
| 975 |
+
if (console.error) {
|
| 976 |
+
console.error(err);
|
| 977 |
+
}
|
| 978 |
+
}
|
| 979 |
+
else if (console[name]) {
|
| 980 |
+
_each(args, function (x) {
|
| 981 |
+
console[name](x);
|
| 982 |
+
});
|
| 983 |
+
}
|
| 984 |
+
}
|
| 985 |
+
}]));
|
| 986 |
+
};
|
| 987 |
+
};
|
| 988 |
+
async.log = _console_fn('log');
|
| 989 |
+
async.dir = _console_fn('dir');
|
| 990 |
+
/*async.info = _console_fn('info');
|
| 991 |
+
async.warn = _console_fn('warn');
|
| 992 |
+
async.error = _console_fn('error');*/
|
| 993 |
+
|
| 994 |
+
async.memoize = function (fn, hasher) {
|
| 995 |
+
var memo = {};
|
| 996 |
+
var queues = {};
|
| 997 |
+
hasher = hasher || function (x) {
|
| 998 |
+
return x;
|
| 999 |
+
};
|
| 1000 |
+
var memoized = function () {
|
| 1001 |
+
var args = Array.prototype.slice.call(arguments);
|
| 1002 |
+
var callback = args.pop();
|
| 1003 |
+
var key = hasher.apply(null, args);
|
| 1004 |
+
if (key in memo) {
|
| 1005 |
+
async.nextTick(function () {
|
| 1006 |
+
callback.apply(null, memo[key]);
|
| 1007 |
+
});
|
| 1008 |
+
}
|
| 1009 |
+
else if (key in queues) {
|
| 1010 |
+
queues[key].push(callback);
|
| 1011 |
+
}
|
| 1012 |
+
else {
|
| 1013 |
+
queues[key] = [callback];
|
| 1014 |
+
fn.apply(null, args.concat([function () {
|
| 1015 |
+
memo[key] = arguments;
|
| 1016 |
+
var q = queues[key];
|
| 1017 |
+
delete queues[key];
|
| 1018 |
+
for (var i = 0, l = q.length; i < l; i++) {
|
| 1019 |
+
q[i].apply(null, arguments);
|
| 1020 |
+
}
|
| 1021 |
+
}]));
|
| 1022 |
+
}
|
| 1023 |
+
};
|
| 1024 |
+
memoized.memo = memo;
|
| 1025 |
+
memoized.unmemoized = fn;
|
| 1026 |
+
return memoized;
|
| 1027 |
+
};
|
| 1028 |
+
|
| 1029 |
+
async.unmemoize = function (fn) {
|
| 1030 |
+
return function () {
|
| 1031 |
+
return (fn.unmemoized || fn).apply(null, arguments);
|
| 1032 |
+
};
|
| 1033 |
+
};
|
| 1034 |
+
|
| 1035 |
+
async.times = function (count, iterator, callback) {
|
| 1036 |
+
var counter = [];
|
| 1037 |
+
for (var i = 0; i < count; i++) {
|
| 1038 |
+
counter.push(i);
|
| 1039 |
+
}
|
| 1040 |
+
return async.map(counter, iterator, callback);
|
| 1041 |
+
};
|
| 1042 |
+
|
| 1043 |
+
async.timesSeries = function (count, iterator, callback) {
|
| 1044 |
+
var counter = [];
|
| 1045 |
+
for (var i = 0; i < count; i++) {
|
| 1046 |
+
counter.push(i);
|
| 1047 |
+
}
|
| 1048 |
+
return async.mapSeries(counter, iterator, callback);
|
| 1049 |
+
};
|
| 1050 |
+
|
| 1051 |
+
async.seq = function (/* functions... */) {
|
| 1052 |
+
var fns = arguments;
|
| 1053 |
+
return function () {
|
| 1054 |
+
var that = this;
|
| 1055 |
+
var args = Array.prototype.slice.call(arguments);
|
| 1056 |
+
var callback = args.pop();
|
| 1057 |
+
async.reduce(fns, args, function (newargs, fn, cb) {
|
| 1058 |
+
fn.apply(that, newargs.concat([function () {
|
| 1059 |
+
var err = arguments[0];
|
| 1060 |
+
var nextargs = Array.prototype.slice.call(arguments, 1);
|
| 1061 |
+
cb(err, nextargs);
|
| 1062 |
+
}]))
|
| 1063 |
+
},
|
| 1064 |
+
function (err, results) {
|
| 1065 |
+
callback.apply(that, [err].concat(results));
|
| 1066 |
+
});
|
| 1067 |
+
};
|
| 1068 |
+
};
|
| 1069 |
+
|
| 1070 |
+
async.compose = function (/* functions... */) {
|
| 1071 |
+
return async.seq.apply(null, Array.prototype.reverse.call(arguments));
|
| 1072 |
+
};
|
| 1073 |
+
|
| 1074 |
+
var _applyEach = function (eachfn, fns /*args...*/) {
|
| 1075 |
+
var go = function () {
|
| 1076 |
+
var that = this;
|
| 1077 |
+
var args = Array.prototype.slice.call(arguments);
|
| 1078 |
+
var callback = args.pop();
|
| 1079 |
+
return eachfn(fns, function (fn, cb) {
|
| 1080 |
+
fn.apply(that, args.concat([cb]));
|
| 1081 |
+
},
|
| 1082 |
+
callback);
|
| 1083 |
+
};
|
| 1084 |
+
if (arguments.length > 2) {
|
| 1085 |
+
var args = Array.prototype.slice.call(arguments, 2);
|
| 1086 |
+
return go.apply(this, args);
|
| 1087 |
+
}
|
| 1088 |
+
else {
|
| 1089 |
+
return go;
|
| 1090 |
+
}
|
| 1091 |
+
};
|
| 1092 |
+
async.applyEach = doParallel(_applyEach);
|
| 1093 |
+
async.applyEachSeries = doSeries(_applyEach);
|
| 1094 |
+
|
| 1095 |
+
async.forever = function (fn, callback) {
|
| 1096 |
+
function next(err) {
|
| 1097 |
+
if (err) {
|
| 1098 |
+
if (callback) {
|
| 1099 |
+
return callback(err);
|
| 1100 |
+
}
|
| 1101 |
+
throw err;
|
| 1102 |
+
}
|
| 1103 |
+
fn(next);
|
| 1104 |
+
}
|
| 1105 |
+
next();
|
| 1106 |
+
};
|
| 1107 |
+
|
| 1108 |
+
// Node.js
|
| 1109 |
+
if (typeof module !== 'undefined' && module.exports) {
|
| 1110 |
+
module.exports = async;
|
| 1111 |
+
}
|
| 1112 |
+
// AMD / RequireJS
|
| 1113 |
+
else if (typeof define !== 'undefined' && define.amd) {
|
| 1114 |
+
define([], function () {
|
| 1115 |
+
return async;
|
| 1116 |
+
});
|
| 1117 |
+
}
|
| 1118 |
+
// included directly via <script> tag
|
| 1119 |
+
else {
|
| 1120 |
+
root.async = async;
|
| 1121 |
+
}
|
| 1122 |
+
|
| 1123 |
+
}());
|
5485-master/demos/node_modules/mongoose/node_modules/async/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "async",
|
| 3 |
+
"description": "Higher-order functions and common patterns for asynchronous code",
|
| 4 |
+
"main": "./lib/async",
|
| 5 |
+
"author": {
|
| 6 |
+
"name": "Caolan McMahon"
|
| 7 |
+
},
|
| 8 |
+
"version": "0.9.0",
|
| 9 |
+
"repository": {
|
| 10 |
+
"type": "git",
|
| 11 |
+
"url": "https://github.com/caolan/async.git"
|
| 12 |
+
},
|
| 13 |
+
"bugs": {
|
| 14 |
+
"url": "https://github.com/caolan/async/issues"
|
| 15 |
+
},
|
| 16 |
+
"licenses": [
|
| 17 |
+
{
|
| 18 |
+
"type": "MIT",
|
| 19 |
+
"url": "https://github.com/caolan/async/raw/master/LICENSE"
|
| 20 |
+
}
|
| 21 |
+
],
|
| 22 |
+
"devDependencies": {
|
| 23 |
+
"nodeunit": ">0.0.0",
|
| 24 |
+
"uglify-js": "1.2.x",
|
| 25 |
+
"nodelint": ">0.0.0"
|
| 26 |
+
},
|
| 27 |
+
"jam": {
|
| 28 |
+
"main": "lib/async.js",
|
| 29 |
+
"include": [
|
| 30 |
+
"lib/async.js",
|
| 31 |
+
"README.md",
|
| 32 |
+
"LICENSE"
|
| 33 |
+
]
|
| 34 |
+
},
|
| 35 |
+
"scripts": {
|
| 36 |
+
"test": "nodeunit test/test-async.js"
|
| 37 |
+
},
|
| 38 |
+
"homepage": "https://github.com/caolan/async",
|
| 39 |
+
"_id": "async@0.9.0",
|
| 40 |
+
"dist": {
|
| 41 |
+
"shasum": "ac3613b1da9bed1b47510bb4651b8931e47146c7",
|
| 42 |
+
"tarball": "http://registry.npmjs.org/async/-/async-0.9.0.tgz"
|
| 43 |
+
},
|
| 44 |
+
"_from": "async@0.9.0",
|
| 45 |
+
"_npmVersion": "1.4.3",
|
| 46 |
+
"_npmUser": {
|
| 47 |
+
"name": "caolan",
|
| 48 |
+
"email": "caolan.mcmahon@gmail.com"
|
| 49 |
+
},
|
| 50 |
+
"maintainers": [
|
| 51 |
+
{
|
| 52 |
+
"name": "caolan",
|
| 53 |
+
"email": "caolan@caolanmcmahon.com"
|
| 54 |
+
}
|
| 55 |
+
],
|
| 56 |
+
"directories": {},
|
| 57 |
+
"_shasum": "ac3613b1da9bed1b47510bb4651b8931e47146c7",
|
| 58 |
+
"_resolved": "https://registry.npmjs.org/async/-/async-0.9.0.tgz"
|
| 59 |
+
}
|
5485-master/demos/node_modules/mongoose/node_modules/bson/.travis.yml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
language: node_js
|
| 2 |
+
node_js:
|
| 3 |
+
- 0.10 # development version of 0.8, may be unstable
|
| 4 |
+
- 0.12
|
5485-master/demos/node_modules/mongoose/node_modules/bson/HISTORY
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0.3.2 2015-03-27
|
| 2 |
+
----------------
|
| 3 |
+
- Removed node-gyp from install script in package.json.
|
| 4 |
+
|
| 5 |
+
0.3.1 2015-03-27
|
| 6 |
+
----------------
|
| 7 |
+
- Return pure js version on native() call if failed to initialize.
|
| 8 |
+
|
| 9 |
+
0.3.0 2015-03-26
|
| 10 |
+
----------------
|
| 11 |
+
- Pulled out all C++ code into bson-ext and made it an optional dependency.
|
| 12 |
+
|
| 13 |
+
0.2.21 2015-03-21
|
| 14 |
+
-----------------
|
| 15 |
+
- Updated Nan to 1.7.0 to support io.js and node 0.12.0
|
| 16 |
+
|
| 17 |
+
0.2.19 2015-02-16
|
| 18 |
+
-----------------
|
| 19 |
+
- Updated Nan to 1.6.2 to support io.js and node 0.12.0
|
| 20 |
+
|
| 21 |
+
0.2.18 2015-01-20
|
| 22 |
+
-----------------
|
| 23 |
+
- Updated Nan to 1.5.1 to support io.js
|
| 24 |
+
|
| 25 |
+
0.2.16 2014-12-17
|
| 26 |
+
-----------------
|
| 27 |
+
- Made pid cycle on 0xffff to avoid weird overflows on creation of ObjectID's
|
| 28 |
+
|
| 29 |
+
0.2.12 2014-08-24
|
| 30 |
+
-----------------
|
| 31 |
+
- Fixes for fortify review of c++ extension
|
| 32 |
+
- toBSON correctly allows returns of non objects
|
| 33 |
+
|
| 34 |
+
0.2.3 2013-10-01
|
| 35 |
+
----------------
|
| 36 |
+
- Drying of ObjectId code for generation of id (Issue #54, https://github.com/moredip)
|
| 37 |
+
- Fixed issue where corrupt CString's could cause endless loop
|
| 38 |
+
- Support for Node 0.11.X > (Issue #49, https://github.com/kkoopa)
|
| 39 |
+
|
| 40 |
+
0.1.4 2012-09-25
|
| 41 |
+
----------------
|
| 42 |
+
- Added precompiled c++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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [yyyy] [name of copyright owner]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
5485-master/demos/node_modules/mongoose/node_modules/bson/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Javascript + C++ BSON parser
|
| 2 |
+
============================
|
| 3 |
+
|
| 4 |
+
This BSON parser is primarily meant to be used with the `mongodb` node.js driver.
|
| 5 |
+
However, wonderful tools such as `onejs` can package up a BSON parser that will work in the browser.
|
| 6 |
+
The current build is located in the `browser_build/bson.js` file.
|
| 7 |
+
|
| 8 |
+
A simple example of how to use BSON in the browser:
|
| 9 |
+
|
| 10 |
+
```html
|
| 11 |
+
<html>
|
| 12 |
+
<head>
|
| 13 |
+
<script src="https://raw.github.com/mongodb/js-bson/master/browser_build/bson.js">
|
| 14 |
+
</script>
|
| 15 |
+
</head>
|
| 16 |
+
<body onload="start();">
|
| 17 |
+
<script>
|
| 18 |
+
function start() {
|
| 19 |
+
var BSON = bson().BSON;
|
| 20 |
+
var Long = bson().Long;
|
| 21 |
+
|
| 22 |
+
var doc = {long: Long.fromNumber(100)}
|
| 23 |
+
|
| 24 |
+
// Serialize a document
|
| 25 |
+
var data = BSON.serialize(doc, false, true, false);
|
| 26 |
+
// De serialize it again
|
| 27 |
+
var doc_2 = BSON.deserialize(data);
|
| 28 |
+
}
|
| 29 |
+
</script>
|
| 30 |
+
</body>
|
| 31 |
+
</html>
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
A simple example of how to use BSON in `node.js`:
|
| 35 |
+
|
| 36 |
+
```javascript
|
| 37 |
+
var bson = require("bson");
|
| 38 |
+
var BSON = bson.BSONPure.BSON;
|
| 39 |
+
var Long = bson.BSONPure.Long;
|
| 40 |
+
|
| 41 |
+
var doc = {long: Long.fromNumber(100)}
|
| 42 |
+
|
| 43 |
+
// Serialize a document
|
| 44 |
+
var data = BSON.serialize(doc, false, true, false);
|
| 45 |
+
console.log("data:", data);
|
| 46 |
+
|
| 47 |
+
// Deserialize the resulting Buffer
|
| 48 |
+
var doc_2 = BSON.deserialize(data);
|
| 49 |
+
console.log("doc_2:", doc_2);
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
The API consists of two simple methods to serialize/deserialize objects to/from BSON format:
|
| 53 |
+
|
| 54 |
+
* BSON.serialize(object, checkKeys, asBuffer, serializeFunctions)
|
| 55 |
+
* @param {Object} object the Javascript object to serialize.
|
| 56 |
+
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
| 57 |
+
* @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
|
| 58 |
+
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**
|
| 59 |
+
* @return {TypedArray/Array} returns a TypedArray or Array depending on what your browser supports
|
| 60 |
+
|
| 61 |
+
* BSON.deserialize(buffer, options, isArray)
|
| 62 |
+
* Options
|
| 63 |
+
* **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
| 64 |
+
* **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
| 65 |
+
* **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
| 66 |
+
* @param {TypedArray/Array} a TypedArray/Array containing the BSON data
|
| 67 |
+
* @param {Object} [options] additional options used for the deserialization.
|
| 68 |
+
* @param {Boolean} [isArray] ignore used for recursive parsing.
|
| 69 |
+
* @return {Object} returns the deserialized Javascript Object.
|
5485-master/demos/node_modules/mongoose/node_modules/bson/browser_build/bson.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
5485-master/demos/node_modules/mongoose/node_modules/bson/browser_build/package.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{ "name" : "bson"
|
| 2 |
+
, "description" : "A bson parser for node.js and the browser"
|
| 3 |
+
, "main": "../lib/bson/bson"
|
| 4 |
+
, "directories" : { "lib" : "../lib/bson" }
|
| 5 |
+
, "engines" : { "node" : ">=0.6.0" }
|
| 6 |
+
, "licenses" : [ { "type" : "Apache License, Version 2.0"
|
| 7 |
+
, "url" : "http://www.apache.org/licenses/LICENSE-2.0" } ]
|
| 8 |
+
}
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/binary.js
ADDED
|
@@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Module dependencies.
|
| 3 |
+
* @ignore
|
| 4 |
+
*/
|
| 5 |
+
if(typeof window === 'undefined') {
|
| 6 |
+
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* A class representation of the BSON Binary type.
|
| 11 |
+
*
|
| 12 |
+
* Sub types
|
| 13 |
+
* - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type.
|
| 14 |
+
* - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type.
|
| 15 |
+
* - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type.
|
| 16 |
+
* - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type.
|
| 17 |
+
* - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type.
|
| 18 |
+
* - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type.
|
| 19 |
+
*
|
| 20 |
+
* @class
|
| 21 |
+
* @param {Buffer} buffer a buffer object containing the binary data.
|
| 22 |
+
* @param {Number} [subType] the option binary type.
|
| 23 |
+
* @return {Binary}
|
| 24 |
+
*/
|
| 25 |
+
function Binary(buffer, subType) {
|
| 26 |
+
if(!(this instanceof Binary)) return new Binary(buffer, subType);
|
| 27 |
+
|
| 28 |
+
this._bsontype = 'Binary';
|
| 29 |
+
|
| 30 |
+
if(buffer instanceof Number) {
|
| 31 |
+
this.sub_type = buffer;
|
| 32 |
+
this.position = 0;
|
| 33 |
+
} else {
|
| 34 |
+
this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
|
| 35 |
+
this.position = 0;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
if(buffer != null && !(buffer instanceof Number)) {
|
| 39 |
+
// Only accept Buffer, Uint8Array or Arrays
|
| 40 |
+
if(typeof buffer == 'string') {
|
| 41 |
+
// Different ways of writing the length of the string for the different types
|
| 42 |
+
if(typeof Buffer != 'undefined') {
|
| 43 |
+
this.buffer = new Buffer(buffer);
|
| 44 |
+
} else if(typeof Uint8Array != 'undefined' || (Object.prototype.toString.call(buffer) == '[object Array]')) {
|
| 45 |
+
this.buffer = writeStringToArray(buffer);
|
| 46 |
+
} else {
|
| 47 |
+
throw new Error("only String, Buffer, Uint8Array or Array accepted");
|
| 48 |
+
}
|
| 49 |
+
} else {
|
| 50 |
+
this.buffer = buffer;
|
| 51 |
+
}
|
| 52 |
+
this.position = buffer.length;
|
| 53 |
+
} else {
|
| 54 |
+
if(typeof Buffer != 'undefined') {
|
| 55 |
+
this.buffer = new Buffer(Binary.BUFFER_SIZE);
|
| 56 |
+
} else if(typeof Uint8Array != 'undefined'){
|
| 57 |
+
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
|
| 58 |
+
} else {
|
| 59 |
+
this.buffer = new Array(Binary.BUFFER_SIZE);
|
| 60 |
+
}
|
| 61 |
+
// Set position to start of buffer
|
| 62 |
+
this.position = 0;
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
/**
|
| 67 |
+
* Updates this binary with byte_value.
|
| 68 |
+
*
|
| 69 |
+
* @method
|
| 70 |
+
* @param {string} byte_value a single byte we wish to write.
|
| 71 |
+
*/
|
| 72 |
+
Binary.prototype.put = function put(byte_value) {
|
| 73 |
+
// If it's a string and a has more than one character throw an error
|
| 74 |
+
if(byte_value['length'] != null && typeof byte_value != 'number' && byte_value.length != 1) throw new Error("only accepts single character String, Uint8Array or Array");
|
| 75 |
+
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");
|
| 76 |
+
|
| 77 |
+
// Decode the byte value once
|
| 78 |
+
var decoded_byte = null;
|
| 79 |
+
if(typeof byte_value == 'string') {
|
| 80 |
+
decoded_byte = byte_value.charCodeAt(0);
|
| 81 |
+
} else if(byte_value['length'] != null) {
|
| 82 |
+
decoded_byte = byte_value[0];
|
| 83 |
+
} else {
|
| 84 |
+
decoded_byte = byte_value;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
if(this.buffer.length > this.position) {
|
| 88 |
+
this.buffer[this.position++] = decoded_byte;
|
| 89 |
+
} else {
|
| 90 |
+
if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) {
|
| 91 |
+
// Create additional overflow buffer
|
| 92 |
+
var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
|
| 93 |
+
// Combine the two buffers together
|
| 94 |
+
this.buffer.copy(buffer, 0, 0, this.buffer.length);
|
| 95 |
+
this.buffer = buffer;
|
| 96 |
+
this.buffer[this.position++] = decoded_byte;
|
| 97 |
+
} else {
|
| 98 |
+
var buffer = null;
|
| 99 |
+
// Create a new buffer (typed or normal array)
|
| 100 |
+
if(Object.prototype.toString.call(this.buffer) == '[object Uint8Array]') {
|
| 101 |
+
buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length));
|
| 102 |
+
} else {
|
| 103 |
+
buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// We need to copy all the content to the new array
|
| 107 |
+
for(var i = 0; i < this.buffer.length; i++) {
|
| 108 |
+
buffer[i] = this.buffer[i];
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// Reassign the buffer
|
| 112 |
+
this.buffer = buffer;
|
| 113 |
+
// Write the byte
|
| 114 |
+
this.buffer[this.position++] = decoded_byte;
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
+
/**
|
| 120 |
+
* Writes a buffer or string to the binary.
|
| 121 |
+
*
|
| 122 |
+
* @method
|
| 123 |
+
* @param {(Buffer|string)} string a string or buffer to be written to the Binary BSON object.
|
| 124 |
+
* @param {number} offset specify the binary of where to write the content.
|
| 125 |
+
* @return {null}
|
| 126 |
+
*/
|
| 127 |
+
Binary.prototype.write = function write(string, offset) {
|
| 128 |
+
offset = typeof offset == 'number' ? offset : this.position;
|
| 129 |
+
|
| 130 |
+
// If the buffer is to small let's extend the buffer
|
| 131 |
+
if(this.buffer.length < offset + string.length) {
|
| 132 |
+
var buffer = null;
|
| 133 |
+
// If we are in node.js
|
| 134 |
+
if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) {
|
| 135 |
+
buffer = new Buffer(this.buffer.length + string.length);
|
| 136 |
+
this.buffer.copy(buffer, 0, 0, this.buffer.length);
|
| 137 |
+
} else if(Object.prototype.toString.call(this.buffer) == '[object Uint8Array]') {
|
| 138 |
+
// Create a new buffer
|
| 139 |
+
buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length))
|
| 140 |
+
// Copy the content
|
| 141 |
+
for(var i = 0; i < this.position; i++) {
|
| 142 |
+
buffer[i] = this.buffer[i];
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// Assign the new buffer
|
| 147 |
+
this.buffer = buffer;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
if(typeof Buffer != 'undefined' && Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) {
|
| 151 |
+
string.copy(this.buffer, offset, 0, string.length);
|
| 152 |
+
this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position;
|
| 153 |
+
// offset = string.length
|
| 154 |
+
} else if(typeof Buffer != 'undefined' && typeof string == 'string' && Buffer.isBuffer(this.buffer)) {
|
| 155 |
+
this.buffer.write(string, 'binary', offset);
|
| 156 |
+
this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position;
|
| 157 |
+
// offset = string.length;
|
| 158 |
+
} else if(Object.prototype.toString.call(string) == '[object Uint8Array]'
|
| 159 |
+
|| Object.prototype.toString.call(string) == '[object Array]' && typeof string != 'string') {
|
| 160 |
+
for(var i = 0; i < string.length; i++) {
|
| 161 |
+
this.buffer[offset++] = string[i];
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
this.position = offset > this.position ? offset : this.position;
|
| 165 |
+
} else if(typeof string == 'string') {
|
| 166 |
+
for(var i = 0; i < string.length; i++) {
|
| 167 |
+
this.buffer[offset++] = string.charCodeAt(i);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
this.position = offset > this.position ? offset : this.position;
|
| 171 |
+
}
|
| 172 |
+
};
|
| 173 |
+
|
| 174 |
+
/**
|
| 175 |
+
* Reads **length** bytes starting at **position**.
|
| 176 |
+
*
|
| 177 |
+
* @method
|
| 178 |
+
* @param {number} position read from the given position in the Binary.
|
| 179 |
+
* @param {number} length the number of bytes to read.
|
| 180 |
+
* @return {Buffer}
|
| 181 |
+
*/
|
| 182 |
+
Binary.prototype.read = function read(position, length) {
|
| 183 |
+
length = length && length > 0
|
| 184 |
+
? length
|
| 185 |
+
: this.position;
|
| 186 |
+
|
| 187 |
+
// Let's return the data based on the type we have
|
| 188 |
+
if(this.buffer['slice']) {
|
| 189 |
+
return this.buffer.slice(position, position + length);
|
| 190 |
+
} else {
|
| 191 |
+
// Create a buffer to keep the result
|
| 192 |
+
var buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(length)) : new Array(length);
|
| 193 |
+
for(var i = 0; i < length; i++) {
|
| 194 |
+
buffer[i] = this.buffer[position++];
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
// Return the buffer
|
| 198 |
+
return buffer;
|
| 199 |
+
};
|
| 200 |
+
|
| 201 |
+
/**
|
| 202 |
+
* Returns the value of this binary as a string.
|
| 203 |
+
*
|
| 204 |
+
* @method
|
| 205 |
+
* @return {string}
|
| 206 |
+
*/
|
| 207 |
+
Binary.prototype.value = function value(asRaw) {
|
| 208 |
+
asRaw = asRaw == null ? false : asRaw;
|
| 209 |
+
|
| 210 |
+
// Optimize to serialize for the situation where the data == size of buffer
|
| 211 |
+
if(asRaw && typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer) && this.buffer.length == this.position)
|
| 212 |
+
return this.buffer;
|
| 213 |
+
|
| 214 |
+
// If it's a node.js buffer object
|
| 215 |
+
if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) {
|
| 216 |
+
return asRaw ? this.buffer.slice(0, this.position) : this.buffer.toString('binary', 0, this.position);
|
| 217 |
+
} else {
|
| 218 |
+
if(asRaw) {
|
| 219 |
+
// we support the slice command use it
|
| 220 |
+
if(this.buffer['slice'] != null) {
|
| 221 |
+
return this.buffer.slice(0, this.position);
|
| 222 |
+
} else {
|
| 223 |
+
// Create a new buffer to copy content to
|
| 224 |
+
var newBuffer = Object.prototype.toString.call(this.buffer) == '[object Uint8Array]' ? new Uint8Array(new ArrayBuffer(this.position)) : new Array(this.position);
|
| 225 |
+
// Copy content
|
| 226 |
+
for(var i = 0; i < this.position; i++) {
|
| 227 |
+
newBuffer[i] = this.buffer[i];
|
| 228 |
+
}
|
| 229 |
+
// Return the buffer
|
| 230 |
+
return newBuffer;
|
| 231 |
+
}
|
| 232 |
+
} else {
|
| 233 |
+
return convertArraytoUtf8BinaryString(this.buffer, 0, this.position);
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
};
|
| 237 |
+
|
| 238 |
+
/**
|
| 239 |
+
* Length.
|
| 240 |
+
*
|
| 241 |
+
* @method
|
| 242 |
+
* @return {number} the length of the binary.
|
| 243 |
+
*/
|
| 244 |
+
Binary.prototype.length = function length() {
|
| 245 |
+
return this.position;
|
| 246 |
+
};
|
| 247 |
+
|
| 248 |
+
/**
|
| 249 |
+
* @ignore
|
| 250 |
+
*/
|
| 251 |
+
Binary.prototype.toJSON = function() {
|
| 252 |
+
return this.buffer != null ? this.buffer.toString('base64') : '';
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
/**
|
| 256 |
+
* @ignore
|
| 257 |
+
*/
|
| 258 |
+
Binary.prototype.toString = function(format) {
|
| 259 |
+
return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : '';
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
/**
|
| 263 |
+
* Binary default subtype
|
| 264 |
+
* @ignore
|
| 265 |
+
*/
|
| 266 |
+
var BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
| 267 |
+
|
| 268 |
+
/**
|
| 269 |
+
* @ignore
|
| 270 |
+
*/
|
| 271 |
+
var writeStringToArray = function(data) {
|
| 272 |
+
// Create a buffer
|
| 273 |
+
var buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(data.length)) : new Array(data.length);
|
| 274 |
+
// Write the content to the buffer
|
| 275 |
+
for(var i = 0; i < data.length; i++) {
|
| 276 |
+
buffer[i] = data.charCodeAt(i);
|
| 277 |
+
}
|
| 278 |
+
// Write the string to the buffer
|
| 279 |
+
return buffer;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
/**
|
| 283 |
+
* Convert Array ot Uint8Array to Binary String
|
| 284 |
+
*
|
| 285 |
+
* @ignore
|
| 286 |
+
*/
|
| 287 |
+
var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
|
| 288 |
+
var result = "";
|
| 289 |
+
for(var i = startIndex; i < endIndex; i++) {
|
| 290 |
+
result = result + String.fromCharCode(byteArray[i]);
|
| 291 |
+
}
|
| 292 |
+
return result;
|
| 293 |
+
};
|
| 294 |
+
|
| 295 |
+
Binary.BUFFER_SIZE = 256;
|
| 296 |
+
|
| 297 |
+
/**
|
| 298 |
+
* Default BSON type
|
| 299 |
+
*
|
| 300 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 301 |
+
**/
|
| 302 |
+
Binary.SUBTYPE_DEFAULT = 0;
|
| 303 |
+
/**
|
| 304 |
+
* Function BSON type
|
| 305 |
+
*
|
| 306 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 307 |
+
**/
|
| 308 |
+
Binary.SUBTYPE_FUNCTION = 1;
|
| 309 |
+
/**
|
| 310 |
+
* Byte Array BSON type
|
| 311 |
+
*
|
| 312 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 313 |
+
**/
|
| 314 |
+
Binary.SUBTYPE_BYTE_ARRAY = 2;
|
| 315 |
+
/**
|
| 316 |
+
* OLD UUID BSON type
|
| 317 |
+
*
|
| 318 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 319 |
+
**/
|
| 320 |
+
Binary.SUBTYPE_UUID_OLD = 3;
|
| 321 |
+
/**
|
| 322 |
+
* UUID BSON type
|
| 323 |
+
*
|
| 324 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 325 |
+
**/
|
| 326 |
+
Binary.SUBTYPE_UUID = 4;
|
| 327 |
+
/**
|
| 328 |
+
* MD5 BSON type
|
| 329 |
+
*
|
| 330 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 331 |
+
**/
|
| 332 |
+
Binary.SUBTYPE_MD5 = 5;
|
| 333 |
+
/**
|
| 334 |
+
* User BSON type
|
| 335 |
+
*
|
| 336 |
+
* @classconstant SUBTYPE_DEFAULT
|
| 337 |
+
**/
|
| 338 |
+
Binary.SUBTYPE_USER_DEFINED = 128;
|
| 339 |
+
|
| 340 |
+
/**
|
| 341 |
+
* Expose.
|
| 342 |
+
*/
|
| 343 |
+
module.exports = Binary;
|
| 344 |
+
module.exports.Binary = Binary;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/binary_parser.js
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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parser.
|
| 3 |
+
* Jonas Raoni Soares Silva
|
| 4 |
+
* http://jsfromhell.com/classes/binary-parser [v1.0]
|
| 5 |
+
*/
|
| 6 |
+
var chr = String.fromCharCode;
|
| 7 |
+
|
| 8 |
+
var maxBits = [];
|
| 9 |
+
for (var i = 0; i < 64; i++) {
|
| 10 |
+
maxBits[i] = Math.pow(2, i);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
function BinaryParser (bigEndian, allowExceptions) {
|
| 14 |
+
if(!(this instanceof BinaryParser)) return new BinaryParser(bigEndian, allowExceptions);
|
| 15 |
+
|
| 16 |
+
this.bigEndian = bigEndian;
|
| 17 |
+
this.allowExceptions = allowExceptions;
|
| 18 |
+
};
|
| 19 |
+
|
| 20 |
+
BinaryParser.warn = function warn (msg) {
|
| 21 |
+
if (this.allowExceptions) {
|
| 22 |
+
throw new Error(msg);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
return 1;
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
BinaryParser.decodeFloat = function decodeFloat (data, precisionBits, exponentBits) {
|
| 29 |
+
var b = new this.Buffer(this.bigEndian, data);
|
| 30 |
+
|
| 31 |
+
b.checkBuffer(precisionBits + exponentBits + 1);
|
| 32 |
+
|
| 33 |
+
var bias = maxBits[exponentBits - 1] - 1
|
| 34 |
+
, signal = b.readBits(precisionBits + exponentBits, 1)
|
| 35 |
+
, exponent = b.readBits(precisionBits, exponentBits)
|
| 36 |
+
, significand = 0
|
| 37 |
+
, divisor = 2
|
| 38 |
+
, curByte = b.buffer.length + (-precisionBits >> 3) - 1;
|
| 39 |
+
|
| 40 |
+
do {
|
| 41 |
+
for (var byteValue = b.buffer[ ++curByte ], startBit = precisionBits % 8 || 8, mask = 1 << startBit; mask >>= 1; ( byteValue & mask ) && ( significand += 1 / divisor ), divisor *= 2 );
|
| 42 |
+
} while (precisionBits -= startBit);
|
| 43 |
+
|
| 44 |
+
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 );
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
BinaryParser.decodeInt = function decodeInt (data, bits, signed, forceBigEndian) {
|
| 48 |
+
var b = new this.Buffer(this.bigEndian || forceBigEndian, data)
|
| 49 |
+
, x = b.readBits(0, bits)
|
| 50 |
+
, max = maxBits[bits]; //max = Math.pow( 2, bits );
|
| 51 |
+
|
| 52 |
+
return signed && x >= max / 2
|
| 53 |
+
? x - max
|
| 54 |
+
: x;
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
BinaryParser.encodeFloat = function encodeFloat (data, precisionBits, exponentBits) {
|
| 58 |
+
var bias = maxBits[exponentBits - 1] - 1
|
| 59 |
+
, minExp = -bias + 1
|
| 60 |
+
, maxExp = bias
|
| 61 |
+
, minUnnormExp = minExp - precisionBits
|
| 62 |
+
, n = parseFloat(data)
|
| 63 |
+
, status = isNaN(n) || n == -Infinity || n == +Infinity ? n : 0
|
| 64 |
+
, exp = 0
|
| 65 |
+
, len = 2 * bias + 1 + precisionBits + 3
|
| 66 |
+
, bin = new Array(len)
|
| 67 |
+
, signal = (n = status !== 0 ? 0 : n) < 0
|
| 68 |
+
, intPart = Math.floor(n = Math.abs(n))
|
| 69 |
+
, floatPart = n - intPart
|
| 70 |
+
, lastBit
|
| 71 |
+
, rounded
|
| 72 |
+
, result
|
| 73 |
+
, i
|
| 74 |
+
, j;
|
| 75 |
+
|
| 76 |
+
for (i = len; i; bin[--i] = 0);
|
| 77 |
+
|
| 78 |
+
for (i = bias + 2; intPart && i; bin[--i] = intPart % 2, intPart = Math.floor(intPart / 2));
|
| 79 |
+
|
| 80 |
+
for (i = bias + 1; floatPart > 0 && i; (bin[++i] = ((floatPart *= 2) >= 1) - 0 ) && --floatPart);
|
| 81 |
+
|
| 82 |
+
for (i = -1; ++i < len && !bin[i];);
|
| 83 |
+
|
| 84 |
+
if (bin[(lastBit = precisionBits - 1 + (i = (exp = bias + 1 - i) >= minExp && exp <= maxExp ? i + 1 : bias + 1 - (exp = minExp - 1))) + 1]) {
|
| 85 |
+
if (!(rounded = bin[lastBit])) {
|
| 86 |
+
for (j = lastBit + 2; !rounded && j < len; rounded = bin[j++]);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
for (j = lastBit + 1; rounded && --j >= 0; (bin[j] = !bin[j] - 0) && (rounded = 0));
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
for (i = i - 2 < 0 ? -1 : i - 3; ++i < len && !bin[i];);
|
| 93 |
+
|
| 94 |
+
if ((exp = bias + 1 - i) >= minExp && exp <= maxExp) {
|
| 95 |
+
++i;
|
| 96 |
+
} else if (exp < minExp) {
|
| 97 |
+
exp != bias + 1 - len && exp < minUnnormExp && this.warn("encodeFloat::float underflow");
|
| 98 |
+
i = bias + 1 - (exp = minExp - 1);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
if (intPart || status !== 0) {
|
| 102 |
+
this.warn(intPart ? "encodeFloat::float overflow" : "encodeFloat::" + status);
|
| 103 |
+
exp = maxExp + 1;
|
| 104 |
+
i = bias + 2;
|
| 105 |
+
|
| 106 |
+
if (status == -Infinity) {
|
| 107 |
+
signal = 1;
|
| 108 |
+
} else if (isNaN(status)) {
|
| 109 |
+
bin[i] = 1;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
for (n = Math.abs(exp + bias), j = exponentBits + 1, result = ""; --j; result = (n % 2) + result, n = n >>= 1);
|
| 114 |
+
|
| 115 |
+
for (n = 0, j = 0, i = (result = (signal ? "1" : "0") + result + bin.slice(i, i + precisionBits).join("")).length, r = []; i; j = (j + 1) % 8) {
|
| 116 |
+
n += (1 << j) * result.charAt(--i);
|
| 117 |
+
if (j == 7) {
|
| 118 |
+
r[r.length] = String.fromCharCode(n);
|
| 119 |
+
n = 0;
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
r[r.length] = n
|
| 124 |
+
? String.fromCharCode(n)
|
| 125 |
+
: "";
|
| 126 |
+
|
| 127 |
+
return (this.bigEndian ? r.reverse() : r).join("");
|
| 128 |
+
};
|
| 129 |
+
|
| 130 |
+
BinaryParser.encodeInt = function encodeInt (data, bits, signed, forceBigEndian) {
|
| 131 |
+
var max = maxBits[bits];
|
| 132 |
+
|
| 133 |
+
if (data >= max || data < -(max / 2)) {
|
| 134 |
+
this.warn("encodeInt::overflow");
|
| 135 |
+
data = 0;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
if (data < 0) {
|
| 139 |
+
data += max;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
for (var r = []; data; r[r.length] = String.fromCharCode(data % 256), data = Math.floor(data / 256));
|
| 143 |
+
|
| 144 |
+
for (bits = -(-bits >> 3) - r.length; bits--; r[r.length] = "\0");
|
| 145 |
+
|
| 146 |
+
return ((this.bigEndian || forceBigEndian) ? r.reverse() : r).join("");
|
| 147 |
+
};
|
| 148 |
+
|
| 149 |
+
BinaryParser.toSmall = function( data ){ return this.decodeInt( data, 8, true ); };
|
| 150 |
+
BinaryParser.fromSmall = function( data ){ return this.encodeInt( data, 8, true ); };
|
| 151 |
+
BinaryParser.toByte = function( data ){ return this.decodeInt( data, 8, false ); };
|
| 152 |
+
BinaryParser.fromByte = function( data ){ return this.encodeInt( data, 8, false ); };
|
| 153 |
+
BinaryParser.toShort = function( data ){ return this.decodeInt( data, 16, true ); };
|
| 154 |
+
BinaryParser.fromShort = function( data ){ return this.encodeInt( data, 16, true ); };
|
| 155 |
+
BinaryParser.toWord = function( data ){ return this.decodeInt( data, 16, false ); };
|
| 156 |
+
BinaryParser.fromWord = function( data ){ return this.encodeInt( data, 16, false ); };
|
| 157 |
+
BinaryParser.toInt = function( data ){ return this.decodeInt( data, 32, true ); };
|
| 158 |
+
BinaryParser.fromInt = function( data ){ return this.encodeInt( data, 32, true ); };
|
| 159 |
+
BinaryParser.toLong = function( data ){ return this.decodeInt( data, 64, true ); };
|
| 160 |
+
BinaryParser.fromLong = function( data ){ return this.encodeInt( data, 64, true ); };
|
| 161 |
+
BinaryParser.toDWord = function( data ){ return this.decodeInt( data, 32, false ); };
|
| 162 |
+
BinaryParser.fromDWord = function( data ){ return this.encodeInt( data, 32, false ); };
|
| 163 |
+
BinaryParser.toQWord = function( data ){ return this.decodeInt( data, 64, true ); };
|
| 164 |
+
BinaryParser.fromQWord = function( data ){ return this.encodeInt( data, 64, true ); };
|
| 165 |
+
BinaryParser.toFloat = function( data ){ return this.decodeFloat( data, 23, 8 ); };
|
| 166 |
+
BinaryParser.fromFloat = function( data ){ return this.encodeFloat( data, 23, 8 ); };
|
| 167 |
+
BinaryParser.toDouble = function( data ){ return this.decodeFloat( data, 52, 11 ); };
|
| 168 |
+
BinaryParser.fromDouble = function( data ){ return this.encodeFloat( data, 52, 11 ); };
|
| 169 |
+
|
| 170 |
+
// Factor out the encode so it can be shared by add_header and push_int32
|
| 171 |
+
BinaryParser.encode_int32 = function encode_int32 (number, asArray) {
|
| 172 |
+
var a, b, c, d, unsigned;
|
| 173 |
+
unsigned = (number < 0) ? (number + 0x100000000) : number;
|
| 174 |
+
a = Math.floor(unsigned / 0xffffff);
|
| 175 |
+
unsigned &= 0xffffff;
|
| 176 |
+
b = Math.floor(unsigned / 0xffff);
|
| 177 |
+
unsigned &= 0xffff;
|
| 178 |
+
c = Math.floor(unsigned / 0xff);
|
| 179 |
+
unsigned &= 0xff;
|
| 180 |
+
d = Math.floor(unsigned);
|
| 181 |
+
return asArray ? [chr(a), chr(b), chr(c), chr(d)] : chr(a) + chr(b) + chr(c) + chr(d);
|
| 182 |
+
};
|
| 183 |
+
|
| 184 |
+
BinaryParser.encode_int64 = function encode_int64 (number) {
|
| 185 |
+
var a, b, c, d, e, f, g, h, unsigned;
|
| 186 |
+
unsigned = (number < 0) ? (number + 0x10000000000000000) : number;
|
| 187 |
+
a = Math.floor(unsigned / 0xffffffffffffff);
|
| 188 |
+
unsigned &= 0xffffffffffffff;
|
| 189 |
+
b = Math.floor(unsigned / 0xffffffffffff);
|
| 190 |
+
unsigned &= 0xffffffffffff;
|
| 191 |
+
c = Math.floor(unsigned / 0xffffffffff);
|
| 192 |
+
unsigned &= 0xffffffffff;
|
| 193 |
+
d = Math.floor(unsigned / 0xffffffff);
|
| 194 |
+
unsigned &= 0xffffffff;
|
| 195 |
+
e = Math.floor(unsigned / 0xffffff);
|
| 196 |
+
unsigned &= 0xffffff;
|
| 197 |
+
f = Math.floor(unsigned / 0xffff);
|
| 198 |
+
unsigned &= 0xffff;
|
| 199 |
+
g = Math.floor(unsigned / 0xff);
|
| 200 |
+
unsigned &= 0xff;
|
| 201 |
+
h = Math.floor(unsigned);
|
| 202 |
+
return chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h);
|
| 203 |
+
};
|
| 204 |
+
|
| 205 |
+
/**
|
| 206 |
+
* UTF8 methods
|
| 207 |
+
*/
|
| 208 |
+
|
| 209 |
+
// Take a raw binary string and return a utf8 string
|
| 210 |
+
BinaryParser.decode_utf8 = function decode_utf8 (binaryStr) {
|
| 211 |
+
var len = binaryStr.length
|
| 212 |
+
, decoded = ''
|
| 213 |
+
, i = 0
|
| 214 |
+
, c = 0
|
| 215 |
+
, c1 = 0
|
| 216 |
+
, c2 = 0
|
| 217 |
+
, c3;
|
| 218 |
+
|
| 219 |
+
while (i < len) {
|
| 220 |
+
c = binaryStr.charCodeAt(i);
|
| 221 |
+
if (c < 128) {
|
| 222 |
+
decoded += String.fromCharCode(c);
|
| 223 |
+
i++;
|
| 224 |
+
} else if ((c > 191) && (c < 224)) {
|
| 225 |
+
c2 = binaryStr.charCodeAt(i+1);
|
| 226 |
+
decoded += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
| 227 |
+
i += 2;
|
| 228 |
+
} else {
|
| 229 |
+
c2 = binaryStr.charCodeAt(i+1);
|
| 230 |
+
c3 = binaryStr.charCodeAt(i+2);
|
| 231 |
+
decoded += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
| 232 |
+
i += 3;
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
return decoded;
|
| 237 |
+
};
|
| 238 |
+
|
| 239 |
+
// Encode a cstring
|
| 240 |
+
BinaryParser.encode_cstring = function encode_cstring (s) {
|
| 241 |
+
return unescape(encodeURIComponent(s)) + BinaryParser.fromByte(0);
|
| 242 |
+
};
|
| 243 |
+
|
| 244 |
+
// Take a utf8 string and return a binary string
|
| 245 |
+
BinaryParser.encode_utf8 = function encode_utf8 (s) {
|
| 246 |
+
var a = ""
|
| 247 |
+
, c;
|
| 248 |
+
|
| 249 |
+
for (var n = 0, len = s.length; n < len; n++) {
|
| 250 |
+
c = s.charCodeAt(n);
|
| 251 |
+
|
| 252 |
+
if (c < 128) {
|
| 253 |
+
a += String.fromCharCode(c);
|
| 254 |
+
} else if ((c > 127) && (c < 2048)) {
|
| 255 |
+
a += String.fromCharCode((c>>6) | 192) ;
|
| 256 |
+
a += String.fromCharCode((c&63) | 128);
|
| 257 |
+
} else {
|
| 258 |
+
a += String.fromCharCode((c>>12) | 224);
|
| 259 |
+
a += String.fromCharCode(((c>>6) & 63) | 128);
|
| 260 |
+
a += String.fromCharCode((c&63) | 128);
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
return a;
|
| 265 |
+
};
|
| 266 |
+
|
| 267 |
+
BinaryParser.hprint = function hprint (s) {
|
| 268 |
+
var number;
|
| 269 |
+
|
| 270 |
+
for (var i = 0, len = s.length; i < len; i++) {
|
| 271 |
+
if (s.charCodeAt(i) < 32) {
|
| 272 |
+
number = s.charCodeAt(i) <= 15
|
| 273 |
+
? "0" + s.charCodeAt(i).toString(16)
|
| 274 |
+
: s.charCodeAt(i).toString(16);
|
| 275 |
+
process.stdout.write(number + " ")
|
| 276 |
+
} else {
|
| 277 |
+
number = s.charCodeAt(i) <= 15
|
| 278 |
+
? "0" + s.charCodeAt(i).toString(16)
|
| 279 |
+
: s.charCodeAt(i).toString(16);
|
| 280 |
+
process.stdout.write(number + " ")
|
| 281 |
+
}
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
process.stdout.write("\n\n");
|
| 285 |
+
};
|
| 286 |
+
|
| 287 |
+
BinaryParser.ilprint = function hprint (s) {
|
| 288 |
+
var number;
|
| 289 |
+
|
| 290 |
+
for (var i = 0, len = s.length; i < len; i++) {
|
| 291 |
+
if (s.charCodeAt(i) < 32) {
|
| 292 |
+
number = s.charCodeAt(i) <= 15
|
| 293 |
+
? "0" + s.charCodeAt(i).toString(10)
|
| 294 |
+
: s.charCodeAt(i).toString(10);
|
| 295 |
+
|
| 296 |
+
require('util').debug(number+' : ');
|
| 297 |
+
} else {
|
| 298 |
+
number = s.charCodeAt(i) <= 15
|
| 299 |
+
? "0" + s.charCodeAt(i).toString(10)
|
| 300 |
+
: s.charCodeAt(i).toString(10);
|
| 301 |
+
require('util').debug(number+' : '+ s.charAt(i));
|
| 302 |
+
}
|
| 303 |
+
}
|
| 304 |
+
};
|
| 305 |
+
|
| 306 |
+
BinaryParser.hlprint = function hprint (s) {
|
| 307 |
+
var number;
|
| 308 |
+
|
| 309 |
+
for (var i = 0, len = s.length; i < len; i++) {
|
| 310 |
+
if (s.charCodeAt(i) < 32) {
|
| 311 |
+
number = s.charCodeAt(i) <= 15
|
| 312 |
+
? "0" + s.charCodeAt(i).toString(16)
|
| 313 |
+
: s.charCodeAt(i).toString(16);
|
| 314 |
+
require('util').debug(number+' : ');
|
| 315 |
+
} else {
|
| 316 |
+
number = s.charCodeAt(i) <= 15
|
| 317 |
+
? "0" + s.charCodeAt(i).toString(16)
|
| 318 |
+
: s.charCodeAt(i).toString(16);
|
| 319 |
+
require('util').debug(number+' : '+ s.charAt(i));
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
};
|
| 323 |
+
|
| 324 |
+
/**
|
| 325 |
+
* BinaryParser buffer constructor.
|
| 326 |
+
*/
|
| 327 |
+
function BinaryParserBuffer (bigEndian, buffer) {
|
| 328 |
+
this.bigEndian = bigEndian || 0;
|
| 329 |
+
this.buffer = [];
|
| 330 |
+
this.setBuffer(buffer);
|
| 331 |
+
};
|
| 332 |
+
|
| 333 |
+
BinaryParserBuffer.prototype.setBuffer = function setBuffer (data) {
|
| 334 |
+
var l, i, b;
|
| 335 |
+
|
| 336 |
+
if (data) {
|
| 337 |
+
i = l = data.length;
|
| 338 |
+
b = this.buffer = new Array(l);
|
| 339 |
+
for (; i; b[l - i] = data.charCodeAt(--i));
|
| 340 |
+
this.bigEndian && b.reverse();
|
| 341 |
+
}
|
| 342 |
+
};
|
| 343 |
+
|
| 344 |
+
BinaryParserBuffer.prototype.hasNeededBits = function hasNeededBits (neededBits) {
|
| 345 |
+
return this.buffer.length >= -(-neededBits >> 3);
|
| 346 |
+
};
|
| 347 |
+
|
| 348 |
+
BinaryParserBuffer.prototype.checkBuffer = function checkBuffer (neededBits) {
|
| 349 |
+
if (!this.hasNeededBits(neededBits)) {
|
| 350 |
+
throw new Error("checkBuffer::missing bytes");
|
| 351 |
+
}
|
| 352 |
+
};
|
| 353 |
+
|
| 354 |
+
BinaryParserBuffer.prototype.readBits = function readBits (start, length) {
|
| 355 |
+
//shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
|
| 356 |
+
|
| 357 |
+
function shl (a, b) {
|
| 358 |
+
for (; b--; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
|
| 359 |
+
return a;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
if (start < 0 || length <= 0) {
|
| 363 |
+
return 0;
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
this.checkBuffer(start + length);
|
| 367 |
+
|
| 368 |
+
var offsetLeft
|
| 369 |
+
, offsetRight = start % 8
|
| 370 |
+
, curByte = this.buffer.length - ( start >> 3 ) - 1
|
| 371 |
+
, lastByte = this.buffer.length + ( -( start + length ) >> 3 )
|
| 372 |
+
, diff = curByte - lastByte
|
| 373 |
+
, 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);
|
| 374 |
+
|
| 375 |
+
for(; diff; sum += shl(this.buffer[lastByte++], (diff-- << 3) - offsetRight));
|
| 376 |
+
|
| 377 |
+
return sum;
|
| 378 |
+
};
|
| 379 |
+
|
| 380 |
+
/**
|
| 381 |
+
* Expose.
|
| 382 |
+
*/
|
| 383 |
+
BinaryParser.Buffer = BinaryParserBuffer;
|
| 384 |
+
|
| 385 |
+
exports.BinaryParser = BinaryParser;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/bson.js
ADDED
|
@@ -0,0 +1,1573 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
var Long = require('./long').Long
|
| 2 |
+
, Double = require('./double').Double
|
| 3 |
+
, Timestamp = require('./timestamp').Timestamp
|
| 4 |
+
, ObjectID = require('./objectid').ObjectID
|
| 5 |
+
, Symbol = require('./symbol').Symbol
|
| 6 |
+
, Code = require('./code').Code
|
| 7 |
+
, MinKey = require('./min_key').MinKey
|
| 8 |
+
, MaxKey = require('./max_key').MaxKey
|
| 9 |
+
, DBRef = require('./db_ref').DBRef
|
| 10 |
+
, Binary = require('./binary').Binary
|
| 11 |
+
, BinaryParser = require('./binary_parser').BinaryParser
|
| 12 |
+
, writeIEEE754 = require('./float_parser').writeIEEE754
|
| 13 |
+
, readIEEE754 = require('./float_parser').readIEEE754
|
| 14 |
+
|
| 15 |
+
// To ensure that 0.4 of node works correctly
|
| 16 |
+
var isDate = function isDate(d) {
|
| 17 |
+
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
/**
|
| 21 |
+
* Create a new BSON instance
|
| 22 |
+
*
|
| 23 |
+
* @class
|
| 24 |
+
* @return {BSON} instance of BSON Parser.
|
| 25 |
+
*/
|
| 26 |
+
function BSON () {};
|
| 27 |
+
|
| 28 |
+
/**
|
| 29 |
+
* @ignore
|
| 30 |
+
* @api private
|
| 31 |
+
*/
|
| 32 |
+
// BSON MAX VALUES
|
| 33 |
+
BSON.BSON_INT32_MAX = 0x7FFFFFFF;
|
| 34 |
+
BSON.BSON_INT32_MIN = -0x80000000;
|
| 35 |
+
|
| 36 |
+
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
| 37 |
+
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
|
| 38 |
+
|
| 39 |
+
// JS MAX PRECISE VALUES
|
| 40 |
+
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
|
| 41 |
+
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
|
| 42 |
+
|
| 43 |
+
// Internal long versions
|
| 44 |
+
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
|
| 45 |
+
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
|
| 46 |
+
|
| 47 |
+
/**
|
| 48 |
+
* Number BSON Type
|
| 49 |
+
*
|
| 50 |
+
* @classconstant BSON_DATA_NUMBER
|
| 51 |
+
**/
|
| 52 |
+
BSON.BSON_DATA_NUMBER = 1;
|
| 53 |
+
/**
|
| 54 |
+
* String BSON Type
|
| 55 |
+
*
|
| 56 |
+
* @classconstant BSON_DATA_STRING
|
| 57 |
+
**/
|
| 58 |
+
BSON.BSON_DATA_STRING = 2;
|
| 59 |
+
/**
|
| 60 |
+
* Object BSON Type
|
| 61 |
+
*
|
| 62 |
+
* @classconstant BSON_DATA_OBJECT
|
| 63 |
+
**/
|
| 64 |
+
BSON.BSON_DATA_OBJECT = 3;
|
| 65 |
+
/**
|
| 66 |
+
* Array BSON Type
|
| 67 |
+
*
|
| 68 |
+
* @classconstant BSON_DATA_ARRAY
|
| 69 |
+
**/
|
| 70 |
+
BSON.BSON_DATA_ARRAY = 4;
|
| 71 |
+
/**
|
| 72 |
+
* Binary BSON Type
|
| 73 |
+
*
|
| 74 |
+
* @classconstant BSON_DATA_BINARY
|
| 75 |
+
**/
|
| 76 |
+
BSON.BSON_DATA_BINARY = 5;
|
| 77 |
+
/**
|
| 78 |
+
* Binary BSON Type
|
| 79 |
+
*
|
| 80 |
+
* @classconstant BSON_DATA_UNDEFINED
|
| 81 |
+
**/
|
| 82 |
+
BSON.BSON_DATA_UNDEFINED = 6;
|
| 83 |
+
/**
|
| 84 |
+
* ObjectID BSON Type
|
| 85 |
+
*
|
| 86 |
+
* @classconstant BSON_DATA_OID
|
| 87 |
+
**/
|
| 88 |
+
BSON.BSON_DATA_OID = 7;
|
| 89 |
+
/**
|
| 90 |
+
* Boolean BSON Type
|
| 91 |
+
*
|
| 92 |
+
* @classconstant BSON_DATA_BOOLEAN
|
| 93 |
+
**/
|
| 94 |
+
BSON.BSON_DATA_BOOLEAN = 8;
|
| 95 |
+
/**
|
| 96 |
+
* Date BSON Type
|
| 97 |
+
*
|
| 98 |
+
* @classconstant BSON_DATA_DATE
|
| 99 |
+
**/
|
| 100 |
+
BSON.BSON_DATA_DATE = 9;
|
| 101 |
+
/**
|
| 102 |
+
* null BSON Type
|
| 103 |
+
*
|
| 104 |
+
* @classconstant BSON_DATA_NULL
|
| 105 |
+
**/
|
| 106 |
+
BSON.BSON_DATA_NULL = 10;
|
| 107 |
+
/**
|
| 108 |
+
* RegExp BSON Type
|
| 109 |
+
*
|
| 110 |
+
* @classconstant BSON_DATA_REGEXP
|
| 111 |
+
**/
|
| 112 |
+
BSON.BSON_DATA_REGEXP = 11;
|
| 113 |
+
/**
|
| 114 |
+
* Code BSON Type
|
| 115 |
+
*
|
| 116 |
+
* @classconstant BSON_DATA_CODE
|
| 117 |
+
**/
|
| 118 |
+
BSON.BSON_DATA_CODE = 13;
|
| 119 |
+
/**
|
| 120 |
+
* Symbol BSON Type
|
| 121 |
+
*
|
| 122 |
+
* @classconstant BSON_DATA_SYMBOL
|
| 123 |
+
**/
|
| 124 |
+
BSON.BSON_DATA_SYMBOL = 14;
|
| 125 |
+
/**
|
| 126 |
+
* Code with Scope BSON Type
|
| 127 |
+
*
|
| 128 |
+
* @classconstant BSON_DATA_CODE_W_SCOPE
|
| 129 |
+
**/
|
| 130 |
+
BSON.BSON_DATA_CODE_W_SCOPE = 15;
|
| 131 |
+
/**
|
| 132 |
+
* 32 bit Integer BSON Type
|
| 133 |
+
*
|
| 134 |
+
* @classconstant BSON_DATA_INT
|
| 135 |
+
**/
|
| 136 |
+
BSON.BSON_DATA_INT = 16;
|
| 137 |
+
/**
|
| 138 |
+
* Timestamp BSON Type
|
| 139 |
+
*
|
| 140 |
+
* @classconstant BSON_DATA_TIMESTAMP
|
| 141 |
+
**/
|
| 142 |
+
BSON.BSON_DATA_TIMESTAMP = 17;
|
| 143 |
+
/**
|
| 144 |
+
* Long BSON Type
|
| 145 |
+
*
|
| 146 |
+
* @classconstant BSON_DATA_LONG
|
| 147 |
+
**/
|
| 148 |
+
BSON.BSON_DATA_LONG = 18;
|
| 149 |
+
/**
|
| 150 |
+
* MinKey BSON Type
|
| 151 |
+
*
|
| 152 |
+
* @classconstant BSON_DATA_MIN_KEY
|
| 153 |
+
**/
|
| 154 |
+
BSON.BSON_DATA_MIN_KEY = 0xff;
|
| 155 |
+
/**
|
| 156 |
+
* MaxKey BSON Type
|
| 157 |
+
*
|
| 158 |
+
* @classconstant BSON_DATA_MAX_KEY
|
| 159 |
+
**/
|
| 160 |
+
BSON.BSON_DATA_MAX_KEY = 0x7f;
|
| 161 |
+
|
| 162 |
+
/**
|
| 163 |
+
* Binary Default Type
|
| 164 |
+
*
|
| 165 |
+
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
|
| 166 |
+
**/
|
| 167 |
+
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
| 168 |
+
/**
|
| 169 |
+
* Binary Function Type
|
| 170 |
+
*
|
| 171 |
+
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
|
| 172 |
+
**/
|
| 173 |
+
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
| 174 |
+
/**
|
| 175 |
+
* Binary Byte Array Type
|
| 176 |
+
*
|
| 177 |
+
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
|
| 178 |
+
**/
|
| 179 |
+
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
| 180 |
+
/**
|
| 181 |
+
* Binary UUID Type
|
| 182 |
+
*
|
| 183 |
+
* @classconstant BSON_BINARY_SUBTYPE_UUID
|
| 184 |
+
**/
|
| 185 |
+
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
|
| 186 |
+
/**
|
| 187 |
+
* Binary MD5 Type
|
| 188 |
+
*
|
| 189 |
+
* @classconstant BSON_BINARY_SUBTYPE_MD5
|
| 190 |
+
**/
|
| 191 |
+
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
|
| 192 |
+
/**
|
| 193 |
+
* Binary User Defined Type
|
| 194 |
+
*
|
| 195 |
+
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
|
| 196 |
+
**/
|
| 197 |
+
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
| 198 |
+
|
| 199 |
+
/**
|
| 200 |
+
* Calculate the bson size for a passed in Javascript object.
|
| 201 |
+
*
|
| 202 |
+
* @param {Object} object the Javascript object to calculate the BSON byte size for.
|
| 203 |
+
* @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**.
|
| 204 |
+
* @return {Number} returns the number of bytes the BSON object will take up.
|
| 205 |
+
* @api public
|
| 206 |
+
*/
|
| 207 |
+
BSON.calculateObjectSize = function calculateObjectSize(object, serializeFunctions) {
|
| 208 |
+
var totalLength = (4 + 1);
|
| 209 |
+
|
| 210 |
+
if(Array.isArray(object)) {
|
| 211 |
+
for(var i = 0; i < object.length; i++) {
|
| 212 |
+
totalLength += calculateElement(i.toString(), object[i], serializeFunctions)
|
| 213 |
+
}
|
| 214 |
+
} else {
|
| 215 |
+
// If we have toBSON defined, override the current object
|
| 216 |
+
if(object.toBSON) {
|
| 217 |
+
object = object.toBSON();
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
// Calculate size
|
| 221 |
+
for(var key in object) {
|
| 222 |
+
totalLength += calculateElement(key, object[key], serializeFunctions)
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
return totalLength;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
/**
|
| 230 |
+
* @ignore
|
| 231 |
+
* @api private
|
| 232 |
+
*/
|
| 233 |
+
function calculateElement(name, value, serializeFunctions) {
|
| 234 |
+
var isBuffer = typeof Buffer !== 'undefined';
|
| 235 |
+
|
| 236 |
+
// If we have toBSON defined, override the current object
|
| 237 |
+
if(value && value.toBSON){
|
| 238 |
+
value = value.toBSON();
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
switch(typeof value) {
|
| 242 |
+
case 'string':
|
| 243 |
+
return 1 + (!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1 + 4 + (!isBuffer ? numberOfBytes(value) : Buffer.byteLength(value, 'utf8')) + 1;
|
| 244 |
+
case 'number':
|
| 245 |
+
if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
| 246 |
+
if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { // 32 bit
|
| 247 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (4 + 1);
|
| 248 |
+
} else {
|
| 249 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
|
| 250 |
+
}
|
| 251 |
+
} else { // 64 bit
|
| 252 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
|
| 253 |
+
}
|
| 254 |
+
case 'undefined':
|
| 255 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1);
|
| 256 |
+
case 'boolean':
|
| 257 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 1);
|
| 258 |
+
case 'object':
|
| 259 |
+
if(value == null || value instanceof MinKey || value instanceof MaxKey || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
| 260 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1);
|
| 261 |
+
} else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') {
|
| 262 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (12 + 1);
|
| 263 |
+
} else if(value instanceof Date || isDate(value)) {
|
| 264 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
|
| 265 |
+
} else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
|
| 266 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 4 + 1) + value.length;
|
| 267 |
+
} else if(value instanceof Long || value instanceof Double || value instanceof Timestamp
|
| 268 |
+
|| value['_bsontype'] == 'Long' || value['_bsontype'] == 'Double' || value['_bsontype'] == 'Timestamp') {
|
| 269 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
|
| 270 |
+
} else if(value instanceof Code || value['_bsontype'] == 'Code') {
|
| 271 |
+
// Calculate size depending on the availability of a scope
|
| 272 |
+
if(value.scope != null && Object.keys(value.scope).length > 0) {
|
| 273 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + 4 + (!isBuffer ? numberOfBytes(value.code.toString()) : Buffer.byteLength(value.code.toString(), 'utf8')) + 1 + BSON.calculateObjectSize(value.scope, serializeFunctions);
|
| 274 |
+
} else {
|
| 275 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + (!isBuffer ? numberOfBytes(value.code.toString()) : Buffer.byteLength(value.code.toString(), 'utf8')) + 1;
|
| 276 |
+
}
|
| 277 |
+
} else if(value instanceof Binary || value['_bsontype'] == 'Binary') {
|
| 278 |
+
// Check what kind of subtype we have
|
| 279 |
+
if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
|
| 280 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (value.position + 1 + 4 + 1 + 4);
|
| 281 |
+
} else {
|
| 282 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (value.position + 1 + 4 + 1);
|
| 283 |
+
}
|
| 284 |
+
} else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') {
|
| 285 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + ((!isBuffer ? numberOfBytes(value.value) : Buffer.byteLength(value.value, 'utf8')) + 4 + 1 + 1);
|
| 286 |
+
} else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') {
|
| 287 |
+
// Set up correct object for serialization
|
| 288 |
+
var ordered_values = {
|
| 289 |
+
'$ref': value.namespace
|
| 290 |
+
, '$id' : value.oid
|
| 291 |
+
};
|
| 292 |
+
|
| 293 |
+
// Add db reference if it exists
|
| 294 |
+
if(null != value.db) {
|
| 295 |
+
ordered_values['$db'] = value.db;
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + BSON.calculateObjectSize(ordered_values, serializeFunctions);
|
| 299 |
+
} else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') {
|
| 300 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + (!isBuffer ? numberOfBytes(value.source) : Buffer.byteLength(value.source, 'utf8')) + 1
|
| 301 |
+
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
|
| 302 |
+
} else {
|
| 303 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + BSON.calculateObjectSize(value, serializeFunctions) + 1;
|
| 304 |
+
}
|
| 305 |
+
case 'function':
|
| 306 |
+
// WTF for 0.4.X where typeof /someregexp/ === 'function'
|
| 307 |
+
if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') {
|
| 308 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + (!isBuffer ? numberOfBytes(value.source) : Buffer.byteLength(value.source, 'utf8')) + 1
|
| 309 |
+
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
|
| 310 |
+
} else {
|
| 311 |
+
if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
|
| 312 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + 4 + (!isBuffer ? numberOfBytes(value.toString()) : Buffer.byteLength(value.toString(), 'utf8')) + 1 + BSON.calculateObjectSize(value.scope, serializeFunctions);
|
| 313 |
+
} else if(serializeFunctions) {
|
| 314 |
+
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + (!isBuffer ? numberOfBytes(value.toString()) : Buffer.byteLength(value.toString(), 'utf8')) + 1;
|
| 315 |
+
}
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
return 0;
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
/**
|
| 323 |
+
* Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
|
| 324 |
+
*
|
| 325 |
+
* @param {Object} object the Javascript object to serialize.
|
| 326 |
+
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
| 327 |
+
* @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
|
| 328 |
+
* @param {Number} index the index in the buffer where we wish to start serializing into.
|
| 329 |
+
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
|
| 330 |
+
* @return {Number} returns the new write index in the Buffer.
|
| 331 |
+
* @api public
|
| 332 |
+
*/
|
| 333 |
+
BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, index, serializeFunctions) {
|
| 334 |
+
// Default setting false
|
| 335 |
+
serializeFunctions = serializeFunctions == null ? false : serializeFunctions;
|
| 336 |
+
// Write end information (length of the object)
|
| 337 |
+
var size = buffer.length;
|
| 338 |
+
// Write the size of the object
|
| 339 |
+
buffer[index++] = size & 0xff;
|
| 340 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 341 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 342 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 343 |
+
return serializeObject(object, checkKeys, buffer, index, serializeFunctions) - 1;
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
/**
|
| 347 |
+
* @ignore
|
| 348 |
+
* @api private
|
| 349 |
+
*/
|
| 350 |
+
var serializeObject = function(object, checkKeys, buffer, index, serializeFunctions) {
|
| 351 |
+
if(object.toBSON) {
|
| 352 |
+
if(typeof object.toBSON != 'function') throw new Error("toBSON is not a function");
|
| 353 |
+
object = object.toBSON();
|
| 354 |
+
if(object != null && typeof object != 'object') throw new Error("toBSON function did not return an object");
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
// Process the object
|
| 358 |
+
if(Array.isArray(object)) {
|
| 359 |
+
for(var i = 0; i < object.length; i++) {
|
| 360 |
+
index = packElement(i.toString(), object[i], checkKeys, buffer, index, serializeFunctions);
|
| 361 |
+
}
|
| 362 |
+
} else {
|
| 363 |
+
// If we have toBSON defined, override the current object
|
| 364 |
+
if(object.toBSON) {
|
| 365 |
+
object = object.toBSON();
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
// Serialize the object
|
| 369 |
+
for(var key in object) {
|
| 370 |
+
// Check the key and throw error if it's illegal
|
| 371 |
+
if (key != '$db' && key != '$ref' && key != '$id') {
|
| 372 |
+
// dollars and dots ok
|
| 373 |
+
BSON.checkKey(key, !checkKeys);
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
// Pack the element
|
| 377 |
+
index = packElement(key, object[key], checkKeys, buffer, index, serializeFunctions);
|
| 378 |
+
}
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
// Write zero
|
| 382 |
+
buffer[index++] = 0;
|
| 383 |
+
return index;
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
var stringToBytes = function(str) {
|
| 387 |
+
var ch, st, re = [];
|
| 388 |
+
for (var i = 0; i < str.length; i++ ) {
|
| 389 |
+
ch = str.charCodeAt(i); // get char
|
| 390 |
+
st = []; // set up "stack"
|
| 391 |
+
do {
|
| 392 |
+
st.push( ch & 0xFF ); // push byte to stack
|
| 393 |
+
ch = ch >> 8; // shift value down by 1 byte
|
| 394 |
+
}
|
| 395 |
+
while ( ch );
|
| 396 |
+
// add stack contents to result
|
| 397 |
+
// done because chars have "wrong" endianness
|
| 398 |
+
re = re.concat( st.reverse() );
|
| 399 |
+
}
|
| 400 |
+
// return an array of bytes
|
| 401 |
+
return re;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
var numberOfBytes = function(str) {
|
| 405 |
+
var ch, st, re = 0;
|
| 406 |
+
for (var i = 0; i < str.length; i++ ) {
|
| 407 |
+
ch = str.charCodeAt(i); // get char
|
| 408 |
+
st = []; // set up "stack"
|
| 409 |
+
do {
|
| 410 |
+
st.push( ch & 0xFF ); // push byte to stack
|
| 411 |
+
ch = ch >> 8; // shift value down by 1 byte
|
| 412 |
+
}
|
| 413 |
+
while ( ch );
|
| 414 |
+
// add stack contents to result
|
| 415 |
+
// done because chars have "wrong" endianness
|
| 416 |
+
re = re + st.length;
|
| 417 |
+
}
|
| 418 |
+
// return an array of bytes
|
| 419 |
+
return re;
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
/**
|
| 423 |
+
* @ignore
|
| 424 |
+
* @api private
|
| 425 |
+
*/
|
| 426 |
+
var writeToTypedArray = function(buffer, string, index) {
|
| 427 |
+
var bytes = stringToBytes(string);
|
| 428 |
+
for(var i = 0; i < bytes.length; i++) {
|
| 429 |
+
buffer[index + i] = bytes[i];
|
| 430 |
+
}
|
| 431 |
+
return bytes.length;
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
/**
|
| 435 |
+
* @ignore
|
| 436 |
+
* @api private
|
| 437 |
+
*/
|
| 438 |
+
var supportsBuffer = typeof Buffer != 'undefined';
|
| 439 |
+
|
| 440 |
+
/**
|
| 441 |
+
* @ignore
|
| 442 |
+
* @api private
|
| 443 |
+
*/
|
| 444 |
+
var packElement = function(name, value, checkKeys, buffer, index, serializeFunctions) {
|
| 445 |
+
|
| 446 |
+
// If we have toBSON defined, override the current object
|
| 447 |
+
if(value && value.toBSON){
|
| 448 |
+
value = value.toBSON();
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
var startIndex = index;
|
| 452 |
+
|
| 453 |
+
switch(typeof value) {
|
| 454 |
+
case 'string':
|
| 455 |
+
// console.log("+++++++++++ index string:: " + index)
|
| 456 |
+
// Encode String type
|
| 457 |
+
buffer[index++] = BSON.BSON_DATA_STRING;
|
| 458 |
+
// Number of written bytes
|
| 459 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 460 |
+
// Encode the name
|
| 461 |
+
index = index + numberOfWrittenBytes + 1;
|
| 462 |
+
buffer[index - 1] = 0;
|
| 463 |
+
|
| 464 |
+
// Calculate size
|
| 465 |
+
var size = supportsBuffer ? Buffer.byteLength(value) + 1 : numberOfBytes(value) + 1;
|
| 466 |
+
// console.log("====== key :: " + name + " size ::" + size)
|
| 467 |
+
// Write the size of the string to buffer
|
| 468 |
+
buffer[index + 3] = (size >> 24) & 0xff;
|
| 469 |
+
buffer[index + 2] = (size >> 16) & 0xff;
|
| 470 |
+
buffer[index + 1] = (size >> 8) & 0xff;
|
| 471 |
+
buffer[index] = size & 0xff;
|
| 472 |
+
// Ajust the index
|
| 473 |
+
index = index + 4;
|
| 474 |
+
// Write the string
|
| 475 |
+
supportsBuffer ? buffer.write(value, index, 'utf8') : writeToTypedArray(buffer, value, index);
|
| 476 |
+
// Update index
|
| 477 |
+
index = index + size - 1;
|
| 478 |
+
// Write zero
|
| 479 |
+
buffer[index++] = 0;
|
| 480 |
+
// Return index
|
| 481 |
+
return index;
|
| 482 |
+
case 'number':
|
| 483 |
+
// We have an integer value
|
| 484 |
+
if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
| 485 |
+
// If the value fits in 32 bits encode as int, if it fits in a double
|
| 486 |
+
// encode it as a double, otherwise long
|
| 487 |
+
if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
|
| 488 |
+
// Set int type 32 bits or less
|
| 489 |
+
buffer[index++] = BSON.BSON_DATA_INT;
|
| 490 |
+
// Number of written bytes
|
| 491 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 492 |
+
// Encode the name
|
| 493 |
+
index = index + numberOfWrittenBytes + 1;
|
| 494 |
+
buffer[index - 1] = 0;
|
| 495 |
+
// Write the int value
|
| 496 |
+
buffer[index++] = value & 0xff;
|
| 497 |
+
buffer[index++] = (value >> 8) & 0xff;
|
| 498 |
+
buffer[index++] = (value >> 16) & 0xff;
|
| 499 |
+
buffer[index++] = (value >> 24) & 0xff;
|
| 500 |
+
} else if(value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
| 501 |
+
// Encode as double
|
| 502 |
+
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
| 503 |
+
// Number of written bytes
|
| 504 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 505 |
+
// Encode the name
|
| 506 |
+
index = index + numberOfWrittenBytes + 1;
|
| 507 |
+
buffer[index - 1] = 0;
|
| 508 |
+
// Write float
|
| 509 |
+
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
| 510 |
+
// Ajust index
|
| 511 |
+
index = index + 8;
|
| 512 |
+
} else {
|
| 513 |
+
// Set long type
|
| 514 |
+
buffer[index++] = BSON.BSON_DATA_LONG;
|
| 515 |
+
// Number of written bytes
|
| 516 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 517 |
+
// Encode the name
|
| 518 |
+
index = index + numberOfWrittenBytes + 1;
|
| 519 |
+
buffer[index - 1] = 0;
|
| 520 |
+
var longVal = Long.fromNumber(value);
|
| 521 |
+
var lowBits = longVal.getLowBits();
|
| 522 |
+
var highBits = longVal.getHighBits();
|
| 523 |
+
// Encode low bits
|
| 524 |
+
buffer[index++] = lowBits & 0xff;
|
| 525 |
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
| 526 |
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
| 527 |
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
| 528 |
+
// Encode high bits
|
| 529 |
+
buffer[index++] = highBits & 0xff;
|
| 530 |
+
buffer[index++] = (highBits >> 8) & 0xff;
|
| 531 |
+
buffer[index++] = (highBits >> 16) & 0xff;
|
| 532 |
+
buffer[index++] = (highBits >> 24) & 0xff;
|
| 533 |
+
}
|
| 534 |
+
} else {
|
| 535 |
+
// Encode as double
|
| 536 |
+
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
| 537 |
+
// Number of written bytes
|
| 538 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 539 |
+
// Encode the name
|
| 540 |
+
index = index + numberOfWrittenBytes + 1;
|
| 541 |
+
buffer[index - 1] = 0;
|
| 542 |
+
// Write float
|
| 543 |
+
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
| 544 |
+
// Ajust index
|
| 545 |
+
index = index + 8;
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
return index;
|
| 549 |
+
case 'undefined':
|
| 550 |
+
// Set long type
|
| 551 |
+
buffer[index++] = BSON.BSON_DATA_NULL;
|
| 552 |
+
// Number of written bytes
|
| 553 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 554 |
+
// Encode the name
|
| 555 |
+
index = index + numberOfWrittenBytes + 1;
|
| 556 |
+
buffer[index - 1] = 0;
|
| 557 |
+
return index;
|
| 558 |
+
case 'boolean':
|
| 559 |
+
// Write the type
|
| 560 |
+
buffer[index++] = BSON.BSON_DATA_BOOLEAN;
|
| 561 |
+
// Number of written bytes
|
| 562 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 563 |
+
// Encode the name
|
| 564 |
+
index = index + numberOfWrittenBytes + 1;
|
| 565 |
+
buffer[index - 1] = 0;
|
| 566 |
+
// Encode the boolean value
|
| 567 |
+
buffer[index++] = value ? 1 : 0;
|
| 568 |
+
return index;
|
| 569 |
+
case 'object':
|
| 570 |
+
if(value === null || value instanceof MinKey || value instanceof MaxKey
|
| 571 |
+
|| value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
| 572 |
+
// Write the type of either min or max key
|
| 573 |
+
if(value === null) {
|
| 574 |
+
buffer[index++] = BSON.BSON_DATA_NULL;
|
| 575 |
+
} else if(value instanceof MinKey) {
|
| 576 |
+
buffer[index++] = BSON.BSON_DATA_MIN_KEY;
|
| 577 |
+
} else {
|
| 578 |
+
buffer[index++] = BSON.BSON_DATA_MAX_KEY;
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
// Number of written bytes
|
| 582 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 583 |
+
// Encode the name
|
| 584 |
+
index = index + numberOfWrittenBytes + 1;
|
| 585 |
+
buffer[index - 1] = 0;
|
| 586 |
+
return index;
|
| 587 |
+
} else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') {
|
| 588 |
+
// console.log("+++++++++++ index OBJECTID:: " + index)
|
| 589 |
+
// Write the type
|
| 590 |
+
buffer[index++] = BSON.BSON_DATA_OID;
|
| 591 |
+
// Number of written bytes
|
| 592 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 593 |
+
// Encode the name
|
| 594 |
+
index = index + numberOfWrittenBytes + 1;
|
| 595 |
+
buffer[index - 1] = 0;
|
| 596 |
+
|
| 597 |
+
// Write objectid
|
| 598 |
+
supportsBuffer ? buffer.write(value.id, index, 'binary') : writeToTypedArray(buffer, value.id, index);
|
| 599 |
+
// Ajust index
|
| 600 |
+
index = index + 12;
|
| 601 |
+
return index;
|
| 602 |
+
} else if(value instanceof Date || isDate(value)) {
|
| 603 |
+
// Write the type
|
| 604 |
+
buffer[index++] = BSON.BSON_DATA_DATE;
|
| 605 |
+
// Number of written bytes
|
| 606 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 607 |
+
// Encode the name
|
| 608 |
+
index = index + numberOfWrittenBytes + 1;
|
| 609 |
+
buffer[index - 1] = 0;
|
| 610 |
+
|
| 611 |
+
// Write the date
|
| 612 |
+
var dateInMilis = Long.fromNumber(value.getTime());
|
| 613 |
+
var lowBits = dateInMilis.getLowBits();
|
| 614 |
+
var highBits = dateInMilis.getHighBits();
|
| 615 |
+
// Encode low bits
|
| 616 |
+
buffer[index++] = lowBits & 0xff;
|
| 617 |
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
| 618 |
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
| 619 |
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
| 620 |
+
// Encode high bits
|
| 621 |
+
buffer[index++] = highBits & 0xff;
|
| 622 |
+
buffer[index++] = (highBits >> 8) & 0xff;
|
| 623 |
+
buffer[index++] = (highBits >> 16) & 0xff;
|
| 624 |
+
buffer[index++] = (highBits >> 24) & 0xff;
|
| 625 |
+
return index;
|
| 626 |
+
} else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
|
| 627 |
+
// Write the type
|
| 628 |
+
buffer[index++] = BSON.BSON_DATA_BINARY;
|
| 629 |
+
// Number of written bytes
|
| 630 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 631 |
+
// Encode the name
|
| 632 |
+
index = index + numberOfWrittenBytes + 1;
|
| 633 |
+
buffer[index - 1] = 0;
|
| 634 |
+
// Get size of the buffer (current write point)
|
| 635 |
+
var size = value.length;
|
| 636 |
+
// Write the size of the string to buffer
|
| 637 |
+
buffer[index++] = size & 0xff;
|
| 638 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 639 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 640 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 641 |
+
// Write the default subtype
|
| 642 |
+
buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT;
|
| 643 |
+
// Copy the content form the binary field to the buffer
|
| 644 |
+
value.copy(buffer, index, 0, size);
|
| 645 |
+
// Adjust the index
|
| 646 |
+
index = index + size;
|
| 647 |
+
return index;
|
| 648 |
+
} else if(value instanceof Long || value instanceof Timestamp || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
| 649 |
+
// Write the type
|
| 650 |
+
buffer[index++] = value instanceof Long || value['_bsontype'] == 'Long' ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP;
|
| 651 |
+
// Number of written bytes
|
| 652 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 653 |
+
// Encode the name
|
| 654 |
+
index = index + numberOfWrittenBytes + 1;
|
| 655 |
+
buffer[index - 1] = 0;
|
| 656 |
+
// Write the date
|
| 657 |
+
var lowBits = value.getLowBits();
|
| 658 |
+
var highBits = value.getHighBits();
|
| 659 |
+
// Encode low bits
|
| 660 |
+
buffer[index++] = lowBits & 0xff;
|
| 661 |
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
| 662 |
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
| 663 |
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
| 664 |
+
// Encode high bits
|
| 665 |
+
buffer[index++] = highBits & 0xff;
|
| 666 |
+
buffer[index++] = (highBits >> 8) & 0xff;
|
| 667 |
+
buffer[index++] = (highBits >> 16) & 0xff;
|
| 668 |
+
buffer[index++] = (highBits >> 24) & 0xff;
|
| 669 |
+
return index;
|
| 670 |
+
} else if(value instanceof Double || value['_bsontype'] == 'Double') {
|
| 671 |
+
// Encode as double
|
| 672 |
+
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
| 673 |
+
// Number of written bytes
|
| 674 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 675 |
+
// Encode the name
|
| 676 |
+
index = index + numberOfWrittenBytes + 1;
|
| 677 |
+
buffer[index - 1] = 0;
|
| 678 |
+
// Write float
|
| 679 |
+
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
| 680 |
+
// Ajust index
|
| 681 |
+
index = index + 8;
|
| 682 |
+
return index;
|
| 683 |
+
} else if(value instanceof Code || value['_bsontype'] == 'Code') {
|
| 684 |
+
if(value.scope != null && Object.keys(value.scope).length > 0) {
|
| 685 |
+
// Write the type
|
| 686 |
+
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
|
| 687 |
+
// Number of written bytes
|
| 688 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 689 |
+
// Encode the name
|
| 690 |
+
index = index + numberOfWrittenBytes + 1;
|
| 691 |
+
buffer[index - 1] = 0;
|
| 692 |
+
// Calculate the scope size
|
| 693 |
+
var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions);
|
| 694 |
+
// Function string
|
| 695 |
+
var functionString = value.code.toString();
|
| 696 |
+
// Function Size
|
| 697 |
+
var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
|
| 698 |
+
|
| 699 |
+
// Calculate full size of the object
|
| 700 |
+
var totalSize = 4 + codeSize + scopeSize + 4;
|
| 701 |
+
|
| 702 |
+
// Write the total size of the object
|
| 703 |
+
buffer[index++] = totalSize & 0xff;
|
| 704 |
+
buffer[index++] = (totalSize >> 8) & 0xff;
|
| 705 |
+
buffer[index++] = (totalSize >> 16) & 0xff;
|
| 706 |
+
buffer[index++] = (totalSize >> 24) & 0xff;
|
| 707 |
+
|
| 708 |
+
// Write the size of the string to buffer
|
| 709 |
+
buffer[index++] = codeSize & 0xff;
|
| 710 |
+
buffer[index++] = (codeSize >> 8) & 0xff;
|
| 711 |
+
buffer[index++] = (codeSize >> 16) & 0xff;
|
| 712 |
+
buffer[index++] = (codeSize >> 24) & 0xff;
|
| 713 |
+
|
| 714 |
+
// Write the string
|
| 715 |
+
supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
|
| 716 |
+
// Update index
|
| 717 |
+
index = index + codeSize - 1;
|
| 718 |
+
// Write zero
|
| 719 |
+
buffer[index++] = 0;
|
| 720 |
+
// Serialize the scope object
|
| 721 |
+
var scopeObjectBuffer = supportsBuffer ? new Buffer(scopeSize) : new Uint8Array(new ArrayBuffer(scopeSize));
|
| 722 |
+
// Execute the serialization into a seperate buffer
|
| 723 |
+
serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions);
|
| 724 |
+
|
| 725 |
+
// Adjusted scope Size (removing the header)
|
| 726 |
+
var scopeDocSize = scopeSize;
|
| 727 |
+
// Write scope object size
|
| 728 |
+
buffer[index++] = scopeDocSize & 0xff;
|
| 729 |
+
buffer[index++] = (scopeDocSize >> 8) & 0xff;
|
| 730 |
+
buffer[index++] = (scopeDocSize >> 16) & 0xff;
|
| 731 |
+
buffer[index++] = (scopeDocSize >> 24) & 0xff;
|
| 732 |
+
|
| 733 |
+
// Write the scopeObject into the buffer
|
| 734 |
+
supportsBuffer ? scopeObjectBuffer.copy(buffer, index, 0, scopeSize) : buffer.set(scopeObjectBuffer, index);
|
| 735 |
+
// Adjust index, removing the empty size of the doc (5 bytes 0000000005)
|
| 736 |
+
index = index + scopeDocSize - 5;
|
| 737 |
+
// Write trailing zero
|
| 738 |
+
buffer[index++] = 0;
|
| 739 |
+
return index
|
| 740 |
+
} else {
|
| 741 |
+
buffer[index++] = BSON.BSON_DATA_CODE;
|
| 742 |
+
// Number of written bytes
|
| 743 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 744 |
+
// Encode the name
|
| 745 |
+
index = index + numberOfWrittenBytes + 1;
|
| 746 |
+
buffer[index - 1] = 0;
|
| 747 |
+
// Function string
|
| 748 |
+
var functionString = value.code.toString();
|
| 749 |
+
// Function Size
|
| 750 |
+
var size = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
|
| 751 |
+
// Write the size of the string to buffer
|
| 752 |
+
buffer[index++] = size & 0xff;
|
| 753 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 754 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 755 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 756 |
+
// Write the string
|
| 757 |
+
supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
|
| 758 |
+
// Update index
|
| 759 |
+
index = index + size - 1;
|
| 760 |
+
// Write zero
|
| 761 |
+
buffer[index++] = 0;
|
| 762 |
+
return index;
|
| 763 |
+
}
|
| 764 |
+
} else if(value instanceof Binary || value['_bsontype'] == 'Binary') {
|
| 765 |
+
// Write the type
|
| 766 |
+
buffer[index++] = BSON.BSON_DATA_BINARY;
|
| 767 |
+
// Number of written bytes
|
| 768 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 769 |
+
// Encode the name
|
| 770 |
+
index = index + numberOfWrittenBytes + 1;
|
| 771 |
+
buffer[index - 1] = 0;
|
| 772 |
+
// Extract the buffer
|
| 773 |
+
var data = value.value(true);
|
| 774 |
+
// Calculate size
|
| 775 |
+
var size = value.position;
|
| 776 |
+
// Write the size of the string to buffer
|
| 777 |
+
buffer[index++] = size & 0xff;
|
| 778 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 779 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 780 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 781 |
+
// Write the subtype to the buffer
|
| 782 |
+
buffer[index++] = value.sub_type;
|
| 783 |
+
|
| 784 |
+
// If we have binary type 2 the 4 first bytes are the size
|
| 785 |
+
if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
|
| 786 |
+
buffer[index++] = size & 0xff;
|
| 787 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 788 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 789 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 790 |
+
}
|
| 791 |
+
|
| 792 |
+
// Write the data to the object
|
| 793 |
+
supportsBuffer ? data.copy(buffer, index, 0, value.position) : buffer.set(data, index);
|
| 794 |
+
// Ajust index
|
| 795 |
+
index = index + value.position;
|
| 796 |
+
return index;
|
| 797 |
+
} else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') {
|
| 798 |
+
// Write the type
|
| 799 |
+
buffer[index++] = BSON.BSON_DATA_SYMBOL;
|
| 800 |
+
// Number of written bytes
|
| 801 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 802 |
+
// Encode the name
|
| 803 |
+
index = index + numberOfWrittenBytes + 1;
|
| 804 |
+
buffer[index - 1] = 0;
|
| 805 |
+
// Calculate size
|
| 806 |
+
var size = supportsBuffer ? Buffer.byteLength(value.value) + 1 : numberOfBytes(value.value) + 1;
|
| 807 |
+
// Write the size of the string to buffer
|
| 808 |
+
buffer[index++] = size & 0xff;
|
| 809 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 810 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 811 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 812 |
+
// Write the string
|
| 813 |
+
buffer.write(value.value, index, 'utf8');
|
| 814 |
+
// Update index
|
| 815 |
+
index = index + size - 1;
|
| 816 |
+
// Write zero
|
| 817 |
+
buffer[index++] = 0x00;
|
| 818 |
+
return index;
|
| 819 |
+
} else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') {
|
| 820 |
+
// Write the type
|
| 821 |
+
buffer[index++] = BSON.BSON_DATA_OBJECT;
|
| 822 |
+
// Number of written bytes
|
| 823 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 824 |
+
// Encode the name
|
| 825 |
+
index = index + numberOfWrittenBytes + 1;
|
| 826 |
+
buffer[index - 1] = 0;
|
| 827 |
+
// Set up correct object for serialization
|
| 828 |
+
var ordered_values = {
|
| 829 |
+
'$ref': value.namespace
|
| 830 |
+
, '$id' : value.oid
|
| 831 |
+
};
|
| 832 |
+
|
| 833 |
+
// Add db reference if it exists
|
| 834 |
+
if(null != value.db) {
|
| 835 |
+
ordered_values['$db'] = value.db;
|
| 836 |
+
}
|
| 837 |
+
|
| 838 |
+
// Message size
|
| 839 |
+
var size = BSON.calculateObjectSize(ordered_values, serializeFunctions);
|
| 840 |
+
// Serialize the object
|
| 841 |
+
var endIndex = BSON.serializeWithBufferAndIndex(ordered_values, checkKeys, buffer, index, serializeFunctions);
|
| 842 |
+
// Write the size of the string to buffer
|
| 843 |
+
buffer[index++] = size & 0xff;
|
| 844 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 845 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 846 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 847 |
+
// Write zero for object
|
| 848 |
+
buffer[endIndex++] = 0x00;
|
| 849 |
+
// Return the end index
|
| 850 |
+
return endIndex;
|
| 851 |
+
} else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') {
|
| 852 |
+
// Write the type
|
| 853 |
+
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
| 854 |
+
// Number of written bytes
|
| 855 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 856 |
+
// Encode the name
|
| 857 |
+
index = index + numberOfWrittenBytes + 1;
|
| 858 |
+
buffer[index - 1] = 0;
|
| 859 |
+
|
| 860 |
+
// Write the regular expression string
|
| 861 |
+
supportsBuffer ? buffer.write(value.source, index, 'utf8') : writeToTypedArray(buffer, value.source, index);
|
| 862 |
+
// Adjust the index
|
| 863 |
+
index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source));
|
| 864 |
+
// Write zero
|
| 865 |
+
buffer[index++] = 0x00;
|
| 866 |
+
// Write the parameters
|
| 867 |
+
if(value.global) buffer[index++] = 0x73; // s
|
| 868 |
+
if(value.ignoreCase) buffer[index++] = 0x69; // i
|
| 869 |
+
if(value.multiline) buffer[index++] = 0x6d; // m
|
| 870 |
+
// Add ending zero
|
| 871 |
+
buffer[index++] = 0x00;
|
| 872 |
+
return index;
|
| 873 |
+
} else {
|
| 874 |
+
// Write the type
|
| 875 |
+
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT;
|
| 876 |
+
// Number of written bytes
|
| 877 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 878 |
+
// Adjust the index
|
| 879 |
+
index = index + numberOfWrittenBytes + 1;
|
| 880 |
+
buffer[index - 1] = 0;
|
| 881 |
+
var endIndex = serializeObject(value, checkKeys, buffer, index + 4, serializeFunctions);
|
| 882 |
+
// Write size
|
| 883 |
+
var size = endIndex - index;
|
| 884 |
+
// Write the size of the string to buffer
|
| 885 |
+
buffer[index++] = size & 0xff;
|
| 886 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 887 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 888 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 889 |
+
return endIndex;
|
| 890 |
+
}
|
| 891 |
+
case 'function':
|
| 892 |
+
// WTF for 0.4.X where typeof /someregexp/ === 'function'
|
| 893 |
+
if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') {
|
| 894 |
+
// Write the type
|
| 895 |
+
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
| 896 |
+
// Number of written bytes
|
| 897 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 898 |
+
// Encode the name
|
| 899 |
+
index = index + numberOfWrittenBytes + 1;
|
| 900 |
+
buffer[index - 1] = 0;
|
| 901 |
+
|
| 902 |
+
// Write the regular expression string
|
| 903 |
+
buffer.write(value.source, index, 'utf8');
|
| 904 |
+
// Adjust the index
|
| 905 |
+
index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source));
|
| 906 |
+
// Write zero
|
| 907 |
+
buffer[index++] = 0x00;
|
| 908 |
+
// Write the parameters
|
| 909 |
+
if(value.global) buffer[index++] = 0x73; // s
|
| 910 |
+
if(value.ignoreCase) buffer[index++] = 0x69; // i
|
| 911 |
+
if(value.multiline) buffer[index++] = 0x6d; // m
|
| 912 |
+
// Add ending zero
|
| 913 |
+
buffer[index++] = 0x00;
|
| 914 |
+
return index;
|
| 915 |
+
} else {
|
| 916 |
+
if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
|
| 917 |
+
// Write the type
|
| 918 |
+
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
|
| 919 |
+
// Number of written bytes
|
| 920 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 921 |
+
// Encode the name
|
| 922 |
+
index = index + numberOfWrittenBytes + 1;
|
| 923 |
+
buffer[index - 1] = 0;
|
| 924 |
+
// Calculate the scope size
|
| 925 |
+
var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions);
|
| 926 |
+
// Function string
|
| 927 |
+
var functionString = value.toString();
|
| 928 |
+
// Function Size
|
| 929 |
+
var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
|
| 930 |
+
|
| 931 |
+
// Calculate full size of the object
|
| 932 |
+
var totalSize = 4 + codeSize + scopeSize;
|
| 933 |
+
|
| 934 |
+
// Write the total size of the object
|
| 935 |
+
buffer[index++] = totalSize & 0xff;
|
| 936 |
+
buffer[index++] = (totalSize >> 8) & 0xff;
|
| 937 |
+
buffer[index++] = (totalSize >> 16) & 0xff;
|
| 938 |
+
buffer[index++] = (totalSize >> 24) & 0xff;
|
| 939 |
+
|
| 940 |
+
// Write the size of the string to buffer
|
| 941 |
+
buffer[index++] = codeSize & 0xff;
|
| 942 |
+
buffer[index++] = (codeSize >> 8) & 0xff;
|
| 943 |
+
buffer[index++] = (codeSize >> 16) & 0xff;
|
| 944 |
+
buffer[index++] = (codeSize >> 24) & 0xff;
|
| 945 |
+
|
| 946 |
+
// Write the string
|
| 947 |
+
supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
|
| 948 |
+
// Update index
|
| 949 |
+
index = index + codeSize - 1;
|
| 950 |
+
// Write zero
|
| 951 |
+
buffer[index++] = 0;
|
| 952 |
+
// Serialize the scope object
|
| 953 |
+
var scopeObjectBuffer = new Buffer(scopeSize);
|
| 954 |
+
// Execute the serialization into a seperate buffer
|
| 955 |
+
serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions);
|
| 956 |
+
|
| 957 |
+
// Adjusted scope Size (removing the header)
|
| 958 |
+
var scopeDocSize = scopeSize - 4;
|
| 959 |
+
// Write scope object size
|
| 960 |
+
buffer[index++] = scopeDocSize & 0xff;
|
| 961 |
+
buffer[index++] = (scopeDocSize >> 8) & 0xff;
|
| 962 |
+
buffer[index++] = (scopeDocSize >> 16) & 0xff;
|
| 963 |
+
buffer[index++] = (scopeDocSize >> 24) & 0xff;
|
| 964 |
+
|
| 965 |
+
// Write the scopeObject into the buffer
|
| 966 |
+
scopeObjectBuffer.copy(buffer, index, 0, scopeSize);
|
| 967 |
+
|
| 968 |
+
// Adjust index, removing the empty size of the doc (5 bytes 0000000005)
|
| 969 |
+
index = index + scopeDocSize - 5;
|
| 970 |
+
// Write trailing zero
|
| 971 |
+
buffer[index++] = 0;
|
| 972 |
+
return index
|
| 973 |
+
} else if(serializeFunctions) {
|
| 974 |
+
buffer[index++] = BSON.BSON_DATA_CODE;
|
| 975 |
+
// Number of written bytes
|
| 976 |
+
var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
|
| 977 |
+
// Encode the name
|
| 978 |
+
index = index + numberOfWrittenBytes + 1;
|
| 979 |
+
buffer[index - 1] = 0;
|
| 980 |
+
// Function string
|
| 981 |
+
var functionString = value.toString();
|
| 982 |
+
// Function Size
|
| 983 |
+
var size = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
|
| 984 |
+
// Write the size of the string to buffer
|
| 985 |
+
buffer[index++] = size & 0xff;
|
| 986 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 987 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 988 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 989 |
+
// Write the string
|
| 990 |
+
supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
|
| 991 |
+
// Update index
|
| 992 |
+
index = index + size - 1;
|
| 993 |
+
// Write zero
|
| 994 |
+
buffer[index++] = 0;
|
| 995 |
+
return index;
|
| 996 |
+
}
|
| 997 |
+
}
|
| 998 |
+
}
|
| 999 |
+
|
| 1000 |
+
// If no value to serialize
|
| 1001 |
+
return index;
|
| 1002 |
+
}
|
| 1003 |
+
|
| 1004 |
+
/**
|
| 1005 |
+
* Serialize a Javascript object.
|
| 1006 |
+
*
|
| 1007 |
+
* @param {Object} object the Javascript object to serialize.
|
| 1008 |
+
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
| 1009 |
+
* @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
|
| 1010 |
+
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
|
| 1011 |
+
* @return {Buffer} returns the Buffer object containing the serialized object.
|
| 1012 |
+
* @api public
|
| 1013 |
+
*/
|
| 1014 |
+
BSON.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
|
| 1015 |
+
// Throw error if we are trying serialize an illegal type
|
| 1016 |
+
if(object == null || typeof object != 'object' || Array.isArray(object))
|
| 1017 |
+
throw new Error("Only javascript objects supported");
|
| 1018 |
+
|
| 1019 |
+
// Emoty target buffer
|
| 1020 |
+
var buffer = null;
|
| 1021 |
+
// Calculate the size of the object
|
| 1022 |
+
var size = BSON.calculateObjectSize(object, serializeFunctions);
|
| 1023 |
+
// Fetch the best available type for storing the binary data
|
| 1024 |
+
if(buffer = typeof Buffer != 'undefined') {
|
| 1025 |
+
buffer = new Buffer(size);
|
| 1026 |
+
asBuffer = true;
|
| 1027 |
+
} else if(typeof Uint8Array != 'undefined') {
|
| 1028 |
+
buffer = new Uint8Array(new ArrayBuffer(size));
|
| 1029 |
+
} else {
|
| 1030 |
+
buffer = new Array(size);
|
| 1031 |
+
}
|
| 1032 |
+
|
| 1033 |
+
// If asBuffer is false use typed arrays
|
| 1034 |
+
BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, 0, serializeFunctions);
|
| 1035 |
+
// console.log("++++++++++++++++++++++++++++++++++++ OLDJS :: " + buffer.length)
|
| 1036 |
+
// console.log(buffer.toString('hex'))
|
| 1037 |
+
// console.log(buffer.toString('ascii'))
|
| 1038 |
+
return buffer;
|
| 1039 |
+
}
|
| 1040 |
+
|
| 1041 |
+
/**
|
| 1042 |
+
* Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
|
| 1043 |
+
*
|
| 1044 |
+
* @ignore
|
| 1045 |
+
* @api private
|
| 1046 |
+
*/
|
| 1047 |
+
var functionCache = BSON.functionCache = {};
|
| 1048 |
+
|
| 1049 |
+
/**
|
| 1050 |
+
* Crc state variables shared by function
|
| 1051 |
+
*
|
| 1052 |
+
* @ignore
|
| 1053 |
+
* @api private
|
| 1054 |
+
*/
|
| 1055 |
+
var table = [0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D];
|
| 1056 |
+
|
| 1057 |
+
/**
|
| 1058 |
+
* CRC32 hash method, Fast and enough versitility for our usage
|
| 1059 |
+
*
|
| 1060 |
+
* @ignore
|
| 1061 |
+
* @api private
|
| 1062 |
+
*/
|
| 1063 |
+
var crc32 = function(string, start, end) {
|
| 1064 |
+
var crc = 0
|
| 1065 |
+
var x = 0;
|
| 1066 |
+
var y = 0;
|
| 1067 |
+
crc = crc ^ (-1);
|
| 1068 |
+
|
| 1069 |
+
for(var i = start, iTop = end; i < iTop;i++) {
|
| 1070 |
+
y = (crc ^ string[i]) & 0xFF;
|
| 1071 |
+
x = table[y];
|
| 1072 |
+
crc = (crc >>> 8) ^ x;
|
| 1073 |
+
}
|
| 1074 |
+
|
| 1075 |
+
return crc ^ (-1);
|
| 1076 |
+
}
|
| 1077 |
+
|
| 1078 |
+
/**
|
| 1079 |
+
* Deserialize stream data as BSON documents.
|
| 1080 |
+
*
|
| 1081 |
+
* Options
|
| 1082 |
+
* - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
| 1083 |
+
* - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
| 1084 |
+
* - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
| 1085 |
+
* - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
|
| 1086 |
+
*
|
| 1087 |
+
* @param {Buffer} data the buffer containing the serialized set of BSON documents.
|
| 1088 |
+
* @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
|
| 1089 |
+
* @param {Number} numberOfDocuments number of documents to deserialize.
|
| 1090 |
+
* @param {Array} documents an array where to store the deserialized documents.
|
| 1091 |
+
* @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
|
| 1092 |
+
* @param {Object} [options] additional options used for the deserialization.
|
| 1093 |
+
* @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
|
| 1094 |
+
* @api public
|
| 1095 |
+
*/
|
| 1096 |
+
BSON.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
|
| 1097 |
+
// if(numberOfDocuments !== documents.length) throw new Error("Number of expected results back is less than the number of documents");
|
| 1098 |
+
options = options != null ? options : {};
|
| 1099 |
+
var index = startIndex;
|
| 1100 |
+
// Loop over all documents
|
| 1101 |
+
for(var i = 0; i < numberOfDocuments; i++) {
|
| 1102 |
+
// Find size of the document
|
| 1103 |
+
var size = data[index] | data[index + 1] << 8 | data[index + 2] << 16 | data[index + 3] << 24;
|
| 1104 |
+
// Update options with index
|
| 1105 |
+
options['index'] = index;
|
| 1106 |
+
// Parse the document at this point
|
| 1107 |
+
documents[docStartIndex + i] = BSON.deserialize(data, options);
|
| 1108 |
+
// Adjust index by the document size
|
| 1109 |
+
index = index + size;
|
| 1110 |
+
}
|
| 1111 |
+
|
| 1112 |
+
// Return object containing end index of parsing and list of documents
|
| 1113 |
+
return index;
|
| 1114 |
+
}
|
| 1115 |
+
|
| 1116 |
+
/**
|
| 1117 |
+
* Ensure eval is isolated.
|
| 1118 |
+
*
|
| 1119 |
+
* @ignore
|
| 1120 |
+
* @api private
|
| 1121 |
+
*/
|
| 1122 |
+
var isolateEvalWithHash = function(functionCache, hash, functionString, object) {
|
| 1123 |
+
// Contains the value we are going to set
|
| 1124 |
+
var value = null;
|
| 1125 |
+
|
| 1126 |
+
// Check for cache hit, eval if missing and return cached function
|
| 1127 |
+
if(functionCache[hash] == null) {
|
| 1128 |
+
eval("value = " + functionString);
|
| 1129 |
+
functionCache[hash] = value;
|
| 1130 |
+
}
|
| 1131 |
+
// Set the object
|
| 1132 |
+
return functionCache[hash].bind(object);
|
| 1133 |
+
}
|
| 1134 |
+
|
| 1135 |
+
/**
|
| 1136 |
+
* Ensure eval is isolated.
|
| 1137 |
+
*
|
| 1138 |
+
* @ignore
|
| 1139 |
+
* @api private
|
| 1140 |
+
*/
|
| 1141 |
+
var isolateEval = function(functionString) {
|
| 1142 |
+
// Contains the value we are going to set
|
| 1143 |
+
var value = null;
|
| 1144 |
+
// Eval the function
|
| 1145 |
+
eval("value = " + functionString);
|
| 1146 |
+
return value;
|
| 1147 |
+
}
|
| 1148 |
+
|
| 1149 |
+
/**
|
| 1150 |
+
* Convert Uint8Array to String
|
| 1151 |
+
*
|
| 1152 |
+
* @ignore
|
| 1153 |
+
* @api private
|
| 1154 |
+
*/
|
| 1155 |
+
var convertUint8ArrayToUtf8String = function(byteArray, startIndex, endIndex) {
|
| 1156 |
+
return BinaryParser.decode_utf8(convertArraytoUtf8BinaryString(byteArray, startIndex, endIndex));
|
| 1157 |
+
}
|
| 1158 |
+
|
| 1159 |
+
var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
|
| 1160 |
+
var result = "";
|
| 1161 |
+
for(var i = startIndex; i < endIndex; i++) {
|
| 1162 |
+
result = result + String.fromCharCode(byteArray[i]);
|
| 1163 |
+
}
|
| 1164 |
+
|
| 1165 |
+
return result;
|
| 1166 |
+
};
|
| 1167 |
+
|
| 1168 |
+
/**
|
| 1169 |
+
* Deserialize data as BSON.
|
| 1170 |
+
*
|
| 1171 |
+
* Options
|
| 1172 |
+
* - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
| 1173 |
+
* - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
| 1174 |
+
* - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
| 1175 |
+
* - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
|
| 1176 |
+
*
|
| 1177 |
+
* @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
|
| 1178 |
+
* @param {Object} [options] additional options used for the deserialization.
|
| 1179 |
+
* @param {Boolean} [isArray] ignore used for recursive parsing.
|
| 1180 |
+
* @return {Object} returns the deserialized Javascript Object.
|
| 1181 |
+
* @api public
|
| 1182 |
+
*/
|
| 1183 |
+
BSON.deserialize = function(buffer, options, isArray) {
|
| 1184 |
+
// Options
|
| 1185 |
+
options = options == null ? {} : options;
|
| 1186 |
+
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
|
| 1187 |
+
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
|
| 1188 |
+
var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
|
| 1189 |
+
var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
|
| 1190 |
+
|
| 1191 |
+
// Validate that we have at least 4 bytes of buffer
|
| 1192 |
+
if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long");
|
| 1193 |
+
|
| 1194 |
+
// Set up index
|
| 1195 |
+
var index = typeof options['index'] == 'number' ? options['index'] : 0;
|
| 1196 |
+
// Reads in a C style string
|
| 1197 |
+
var readCStyleString = function() {
|
| 1198 |
+
// Get the start search index
|
| 1199 |
+
var i = index;
|
| 1200 |
+
// Locate the end of the c string
|
| 1201 |
+
while(buffer[i] !== 0x00 && i < buffer.length) {
|
| 1202 |
+
i++
|
| 1203 |
+
}
|
| 1204 |
+
// If are at the end of the buffer there is a problem with the document
|
| 1205 |
+
if(i >= buffer.length) throw new Error("Bad BSON Document: illegal CString")
|
| 1206 |
+
// Grab utf8 encoded string
|
| 1207 |
+
var string = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, i) : convertUint8ArrayToUtf8String(buffer, index, i);
|
| 1208 |
+
// Update index position
|
| 1209 |
+
index = i + 1;
|
| 1210 |
+
// Return string
|
| 1211 |
+
return string;
|
| 1212 |
+
}
|
| 1213 |
+
|
| 1214 |
+
// Create holding object
|
| 1215 |
+
var object = isArray ? [] : {};
|
| 1216 |
+
|
| 1217 |
+
// Read the document size
|
| 1218 |
+
var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1219 |
+
|
| 1220 |
+
// Ensure buffer is valid size
|
| 1221 |
+
if(size < 5 || size > buffer.length) throw new Error("corrupt bson message");
|
| 1222 |
+
|
| 1223 |
+
// While we have more left data left keep parsing
|
| 1224 |
+
while(true) {
|
| 1225 |
+
// Read the type
|
| 1226 |
+
var elementType = buffer[index++];
|
| 1227 |
+
// If we get a zero it's the last byte, exit
|
| 1228 |
+
if(elementType == 0) break;
|
| 1229 |
+
// Read the name of the field
|
| 1230 |
+
var name = readCStyleString();
|
| 1231 |
+
// Switch on the type
|
| 1232 |
+
switch(elementType) {
|
| 1233 |
+
case BSON.BSON_DATA_OID:
|
| 1234 |
+
var string = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('binary', index, index + 12) : convertArraytoUtf8BinaryString(buffer, index, index + 12);
|
| 1235 |
+
// Decode the oid
|
| 1236 |
+
object[name] = new ObjectID(string);
|
| 1237 |
+
// Update index
|
| 1238 |
+
index = index + 12;
|
| 1239 |
+
break;
|
| 1240 |
+
case BSON.BSON_DATA_STRING:
|
| 1241 |
+
// Read the content of the field
|
| 1242 |
+
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1243 |
+
// Add string to object
|
| 1244 |
+
object[name] = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
|
| 1245 |
+
// Update parse index position
|
| 1246 |
+
index = index + stringSize;
|
| 1247 |
+
break;
|
| 1248 |
+
case BSON.BSON_DATA_INT:
|
| 1249 |
+
// Decode the 32bit value
|
| 1250 |
+
object[name] = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1251 |
+
break;
|
| 1252 |
+
case BSON.BSON_DATA_NUMBER:
|
| 1253 |
+
// Decode the double value
|
| 1254 |
+
object[name] = readIEEE754(buffer, index, 'little', 52, 8);
|
| 1255 |
+
// Update the index
|
| 1256 |
+
index = index + 8;
|
| 1257 |
+
break;
|
| 1258 |
+
case BSON.BSON_DATA_DATE:
|
| 1259 |
+
// Unpack the low and high bits
|
| 1260 |
+
var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1261 |
+
var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1262 |
+
// Set date object
|
| 1263 |
+
object[name] = new Date(new Long(lowBits, highBits).toNumber());
|
| 1264 |
+
break;
|
| 1265 |
+
case BSON.BSON_DATA_BOOLEAN:
|
| 1266 |
+
// Parse the boolean value
|
| 1267 |
+
object[name] = buffer[index++] == 1;
|
| 1268 |
+
break;
|
| 1269 |
+
case BSON.BSON_DATA_UNDEFINED:
|
| 1270 |
+
case BSON.BSON_DATA_NULL:
|
| 1271 |
+
// Parse the boolean value
|
| 1272 |
+
object[name] = null;
|
| 1273 |
+
break;
|
| 1274 |
+
case BSON.BSON_DATA_BINARY:
|
| 1275 |
+
// Decode the size of the binary blob
|
| 1276 |
+
var binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1277 |
+
// Decode the subtype
|
| 1278 |
+
var subType = buffer[index++];
|
| 1279 |
+
// Decode as raw Buffer object if options specifies it
|
| 1280 |
+
if(buffer['slice'] != null) {
|
| 1281 |
+
// If we have subtype 2 skip the 4 bytes for the size
|
| 1282 |
+
if(subType == Binary.SUBTYPE_BYTE_ARRAY) {
|
| 1283 |
+
binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1284 |
+
}
|
| 1285 |
+
// Slice the data
|
| 1286 |
+
object[name] = new Binary(buffer.slice(index, index + binarySize), subType);
|
| 1287 |
+
} else {
|
| 1288 |
+
var _buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(binarySize)) : new Array(binarySize);
|
| 1289 |
+
// If we have subtype 2 skip the 4 bytes for the size
|
| 1290 |
+
if(subType == Binary.SUBTYPE_BYTE_ARRAY) {
|
| 1291 |
+
binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1292 |
+
}
|
| 1293 |
+
// Copy the data
|
| 1294 |
+
for(var i = 0; i < binarySize; i++) {
|
| 1295 |
+
_buffer[i] = buffer[index + i];
|
| 1296 |
+
}
|
| 1297 |
+
// Create the binary object
|
| 1298 |
+
object[name] = new Binary(_buffer, subType);
|
| 1299 |
+
}
|
| 1300 |
+
// Update the index
|
| 1301 |
+
index = index + binarySize;
|
| 1302 |
+
break;
|
| 1303 |
+
case BSON.BSON_DATA_ARRAY:
|
| 1304 |
+
options['index'] = index;
|
| 1305 |
+
// Decode the size of the array document
|
| 1306 |
+
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
| 1307 |
+
// Set the array to the object
|
| 1308 |
+
object[name] = BSON.deserialize(buffer, options, true);
|
| 1309 |
+
// Adjust the index
|
| 1310 |
+
index = index + objectSize;
|
| 1311 |
+
break;
|
| 1312 |
+
case BSON.BSON_DATA_OBJECT:
|
| 1313 |
+
options['index'] = index;
|
| 1314 |
+
// Decode the size of the object document
|
| 1315 |
+
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
| 1316 |
+
// Set the array to the object
|
| 1317 |
+
object[name] = BSON.deserialize(buffer, options, false);
|
| 1318 |
+
// Adjust the index
|
| 1319 |
+
index = index + objectSize;
|
| 1320 |
+
break;
|
| 1321 |
+
case BSON.BSON_DATA_REGEXP:
|
| 1322 |
+
// Create the regexp
|
| 1323 |
+
var source = readCStyleString();
|
| 1324 |
+
var regExpOptions = readCStyleString();
|
| 1325 |
+
// For each option add the corresponding one for javascript
|
| 1326 |
+
var optionsArray = new Array(regExpOptions.length);
|
| 1327 |
+
|
| 1328 |
+
// Parse options
|
| 1329 |
+
for(var i = 0; i < regExpOptions.length; i++) {
|
| 1330 |
+
switch(regExpOptions[i]) {
|
| 1331 |
+
case 'm':
|
| 1332 |
+
optionsArray[i] = 'm';
|
| 1333 |
+
break;
|
| 1334 |
+
case 's':
|
| 1335 |
+
optionsArray[i] = 'g';
|
| 1336 |
+
break;
|
| 1337 |
+
case 'i':
|
| 1338 |
+
optionsArray[i] = 'i';
|
| 1339 |
+
break;
|
| 1340 |
+
}
|
| 1341 |
+
}
|
| 1342 |
+
|
| 1343 |
+
object[name] = new RegExp(source, optionsArray.join(''));
|
| 1344 |
+
break;
|
| 1345 |
+
case BSON.BSON_DATA_LONG:
|
| 1346 |
+
// Unpack the low and high bits
|
| 1347 |
+
var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1348 |
+
var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1349 |
+
// Create long object
|
| 1350 |
+
var long = new Long(lowBits, highBits);
|
| 1351 |
+
// Promote the long if possible
|
| 1352 |
+
if(promoteLongs) {
|
| 1353 |
+
object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long;
|
| 1354 |
+
} else {
|
| 1355 |
+
object[name] = long;
|
| 1356 |
+
}
|
| 1357 |
+
break;
|
| 1358 |
+
case BSON.BSON_DATA_SYMBOL:
|
| 1359 |
+
// Read the content of the field
|
| 1360 |
+
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1361 |
+
// Add string to object
|
| 1362 |
+
object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1));
|
| 1363 |
+
// Update parse index position
|
| 1364 |
+
index = index + stringSize;
|
| 1365 |
+
break;
|
| 1366 |
+
case BSON.BSON_DATA_TIMESTAMP:
|
| 1367 |
+
// Unpack the low and high bits
|
| 1368 |
+
var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1369 |
+
var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1370 |
+
// Set the object
|
| 1371 |
+
object[name] = new Timestamp(lowBits, highBits);
|
| 1372 |
+
break;
|
| 1373 |
+
case BSON.BSON_DATA_MIN_KEY:
|
| 1374 |
+
// Parse the object
|
| 1375 |
+
object[name] = new MinKey();
|
| 1376 |
+
break;
|
| 1377 |
+
case BSON.BSON_DATA_MAX_KEY:
|
| 1378 |
+
// Parse the object
|
| 1379 |
+
object[name] = new MaxKey();
|
| 1380 |
+
break;
|
| 1381 |
+
case BSON.BSON_DATA_CODE:
|
| 1382 |
+
// Read the content of the field
|
| 1383 |
+
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1384 |
+
// Function string
|
| 1385 |
+
var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
|
| 1386 |
+
|
| 1387 |
+
// If we are evaluating the functions
|
| 1388 |
+
if(evalFunctions) {
|
| 1389 |
+
// Contains the value we are going to set
|
| 1390 |
+
var value = null;
|
| 1391 |
+
// If we have cache enabled let's look for the md5 of the function in the cache
|
| 1392 |
+
if(cacheFunctions) {
|
| 1393 |
+
var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
|
| 1394 |
+
// Got to do this to avoid V8 deoptimizing the call due to finding eval
|
| 1395 |
+
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
|
| 1396 |
+
} else {
|
| 1397 |
+
// Set directly
|
| 1398 |
+
object[name] = isolateEval(functionString);
|
| 1399 |
+
}
|
| 1400 |
+
} else {
|
| 1401 |
+
object[name] = new Code(functionString, {});
|
| 1402 |
+
}
|
| 1403 |
+
|
| 1404 |
+
// Update parse index position
|
| 1405 |
+
index = index + stringSize;
|
| 1406 |
+
break;
|
| 1407 |
+
case BSON.BSON_DATA_CODE_W_SCOPE:
|
| 1408 |
+
// Read the content of the field
|
| 1409 |
+
var totalSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1410 |
+
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
| 1411 |
+
// Javascript function
|
| 1412 |
+
var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
|
| 1413 |
+
// Update parse index position
|
| 1414 |
+
index = index + stringSize;
|
| 1415 |
+
// Parse the element
|
| 1416 |
+
options['index'] = index;
|
| 1417 |
+
// Decode the size of the object document
|
| 1418 |
+
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
| 1419 |
+
// Decode the scope object
|
| 1420 |
+
var scopeObject = BSON.deserialize(buffer, options, false);
|
| 1421 |
+
// Adjust the index
|
| 1422 |
+
index = index + objectSize;
|
| 1423 |
+
|
| 1424 |
+
// If we are evaluating the functions
|
| 1425 |
+
if(evalFunctions) {
|
| 1426 |
+
// Contains the value we are going to set
|
| 1427 |
+
var value = null;
|
| 1428 |
+
// If we have cache enabled let's look for the md5 of the function in the cache
|
| 1429 |
+
if(cacheFunctions) {
|
| 1430 |
+
var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
|
| 1431 |
+
// Got to do this to avoid V8 deoptimizing the call due to finding eval
|
| 1432 |
+
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
|
| 1433 |
+
} else {
|
| 1434 |
+
// Set directly
|
| 1435 |
+
object[name] = isolateEval(functionString);
|
| 1436 |
+
}
|
| 1437 |
+
|
| 1438 |
+
// Set the scope on the object
|
| 1439 |
+
object[name].scope = scopeObject;
|
| 1440 |
+
} else {
|
| 1441 |
+
object[name] = new Code(functionString, scopeObject);
|
| 1442 |
+
}
|
| 1443 |
+
|
| 1444 |
+
// Add string to object
|
| 1445 |
+
break;
|
| 1446 |
+
}
|
| 1447 |
+
}
|
| 1448 |
+
|
| 1449 |
+
// Check if we have a db ref object
|
| 1450 |
+
if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
|
| 1451 |
+
|
| 1452 |
+
// Return the final objects
|
| 1453 |
+
return object;
|
| 1454 |
+
}
|
| 1455 |
+
|
| 1456 |
+
/**
|
| 1457 |
+
* Check if key name is valid.
|
| 1458 |
+
*
|
| 1459 |
+
* @ignore
|
| 1460 |
+
* @api private
|
| 1461 |
+
*/
|
| 1462 |
+
BSON.checkKey = function checkKey (key, dollarsAndDotsOk) {
|
| 1463 |
+
if (!key.length) return;
|
| 1464 |
+
// Check if we have a legal key for the object
|
| 1465 |
+
if (!!~key.indexOf("\x00")) {
|
| 1466 |
+
// The BSON spec doesn't allow keys with null bytes because keys are
|
| 1467 |
+
// null-terminated.
|
| 1468 |
+
throw Error("key " + key + " must not contain null bytes");
|
| 1469 |
+
}
|
| 1470 |
+
if (!dollarsAndDotsOk) {
|
| 1471 |
+
if('$' == key[0]) {
|
| 1472 |
+
throw Error("key " + key + " must not start with '$'");
|
| 1473 |
+
} else if (!!~key.indexOf('.')) {
|
| 1474 |
+
throw Error("key " + key + " must not contain '.'");
|
| 1475 |
+
}
|
| 1476 |
+
}
|
| 1477 |
+
};
|
| 1478 |
+
|
| 1479 |
+
/**
|
| 1480 |
+
* Deserialize data as BSON.
|
| 1481 |
+
*
|
| 1482 |
+
* Options
|
| 1483 |
+
* - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
| 1484 |
+
* - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
| 1485 |
+
* - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
| 1486 |
+
*
|
| 1487 |
+
* @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
|
| 1488 |
+
* @param {Object} [options] additional options used for the deserialization.
|
| 1489 |
+
* @param {Boolean} [isArray] ignore used for recursive parsing.
|
| 1490 |
+
* @return {Object} returns the deserialized Javascript Object.
|
| 1491 |
+
* @api public
|
| 1492 |
+
*/
|
| 1493 |
+
BSON.prototype.deserialize = function(data, options) {
|
| 1494 |
+
return BSON.deserialize(data, options);
|
| 1495 |
+
}
|
| 1496 |
+
|
| 1497 |
+
/**
|
| 1498 |
+
* Deserialize stream data as BSON documents.
|
| 1499 |
+
*
|
| 1500 |
+
* Options
|
| 1501 |
+
* - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
|
| 1502 |
+
* - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
|
| 1503 |
+
* - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
|
| 1504 |
+
*
|
| 1505 |
+
* @param {Buffer} data the buffer containing the serialized set of BSON documents.
|
| 1506 |
+
* @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
|
| 1507 |
+
* @param {Number} numberOfDocuments number of documents to deserialize.
|
| 1508 |
+
* @param {Array} documents an array where to store the deserialized documents.
|
| 1509 |
+
* @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
|
| 1510 |
+
* @param {Object} [options] additional options used for the deserialization.
|
| 1511 |
+
* @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
|
| 1512 |
+
* @api public
|
| 1513 |
+
*/
|
| 1514 |
+
BSON.prototype.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
|
| 1515 |
+
return BSON.deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, options);
|
| 1516 |
+
}
|
| 1517 |
+
|
| 1518 |
+
/**
|
| 1519 |
+
* Serialize a Javascript object.
|
| 1520 |
+
*
|
| 1521 |
+
* @param {Object} object the Javascript object to serialize.
|
| 1522 |
+
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
| 1523 |
+
* @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
|
| 1524 |
+
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
|
| 1525 |
+
* @return {Buffer} returns the Buffer object containing the serialized object.
|
| 1526 |
+
* @api public
|
| 1527 |
+
*/
|
| 1528 |
+
BSON.prototype.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
|
| 1529 |
+
return BSON.serialize(object, checkKeys, asBuffer, serializeFunctions);
|
| 1530 |
+
}
|
| 1531 |
+
|
| 1532 |
+
/**
|
| 1533 |
+
* Calculate the bson size for a passed in Javascript object.
|
| 1534 |
+
*
|
| 1535 |
+
* @param {Object} object the Javascript object to calculate the BSON byte size for.
|
| 1536 |
+
* @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**.
|
| 1537 |
+
* @return {Number} returns the number of bytes the BSON object will take up.
|
| 1538 |
+
* @api public
|
| 1539 |
+
*/
|
| 1540 |
+
BSON.prototype.calculateObjectSize = function(object, serializeFunctions) {
|
| 1541 |
+
return BSON.calculateObjectSize(object, serializeFunctions);
|
| 1542 |
+
}
|
| 1543 |
+
|
| 1544 |
+
/**
|
| 1545 |
+
* Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
|
| 1546 |
+
*
|
| 1547 |
+
* @param {Object} object the Javascript object to serialize.
|
| 1548 |
+
* @param {Boolean} checkKeys the serializer will check if keys are valid.
|
| 1549 |
+
* @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
|
| 1550 |
+
* @param {Number} index the index in the buffer where we wish to start serializing into.
|
| 1551 |
+
* @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
|
| 1552 |
+
* @return {Number} returns the new write index in the Buffer.
|
| 1553 |
+
* @api public
|
| 1554 |
+
*/
|
| 1555 |
+
BSON.prototype.serializeWithBufferAndIndex = function(object, checkKeys, buffer, startIndex, serializeFunctions) {
|
| 1556 |
+
return BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, startIndex, serializeFunctions);
|
| 1557 |
+
}
|
| 1558 |
+
|
| 1559 |
+
/**
|
| 1560 |
+
* @ignore
|
| 1561 |
+
* @api private
|
| 1562 |
+
*/
|
| 1563 |
+
exports.Code = Code;
|
| 1564 |
+
exports.Symbol = Symbol;
|
| 1565 |
+
exports.BSON = BSON;
|
| 1566 |
+
exports.DBRef = DBRef;
|
| 1567 |
+
exports.Binary = Binary;
|
| 1568 |
+
exports.ObjectID = ObjectID;
|
| 1569 |
+
exports.Long = Long;
|
| 1570 |
+
exports.Timestamp = Timestamp;
|
| 1571 |
+
exports.Double = Double;
|
| 1572 |
+
exports.MinKey = MinKey;
|
| 1573 |
+
exports.MaxKey = MaxKey;
|
5485-master/demos/node_modules/mongoose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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
var writeIEEE754 = require('./float_parser').writeIEEE754
|
| 2 |
+
, Long = require('./long').Long
|
| 3 |
+
, Double = require('./double').Double
|
| 4 |
+
, Timestamp = require('./timestamp').Timestamp
|
| 5 |
+
, ObjectID = require('./objectid').ObjectID
|
| 6 |
+
, Symbol = require('./symbol').Symbol
|
| 7 |
+
, Code = require('./code').Code
|
| 8 |
+
, MinKey = require('./min_key').MinKey
|
| 9 |
+
, MaxKey = require('./max_key').MaxKey
|
| 10 |
+
, DBRef = require('./db_ref').DBRef
|
| 11 |
+
, Binary = require('./binary').Binary
|
| 12 |
+
, BinaryParser = require('./binary_parser').BinaryParser;
|
| 13 |
+
|
| 14 |
+
// Max Document Buffer size
|
| 15 |
+
var buffer = new Buffer(1024 * 1024 * 16);
|
| 16 |
+
|
| 17 |
+
var checkKey = function checkKey (key, dollarsAndDotsOk) {
|
| 18 |
+
if (!key.length) return;
|
| 19 |
+
// Check if we have a legal key for the object
|
| 20 |
+
if (!!~key.indexOf("\x00")) {
|
| 21 |
+
// The BSON spec doesn't allow keys with null bytes because keys are
|
| 22 |
+
// null-terminated.
|
| 23 |
+
throw Error("key " + key + " must not contain null bytes");
|
| 24 |
+
}
|
| 25 |
+
if (!dollarsAndDotsOk) {
|
| 26 |
+
if('$' == key[0]) {
|
| 27 |
+
throw Error("key " + key + " must not start with '$'");
|
| 28 |
+
} else if (!!~key.indexOf('.')) {
|
| 29 |
+
throw Error("key " + key + " must not contain '.'");
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
var serializeString = function(key, value, index) {
|
| 35 |
+
// Encode String type
|
| 36 |
+
buffer[index++] = BSON.BSON_DATA_STRING;
|
| 37 |
+
// Number of written bytes
|
| 38 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 39 |
+
// Encode the name
|
| 40 |
+
index = index + numberOfWrittenBytes + 1;
|
| 41 |
+
buffer[index - 1] = 0;
|
| 42 |
+
|
| 43 |
+
// Calculate size
|
| 44 |
+
var size = Buffer.byteLength(value) + 1;
|
| 45 |
+
// Write the size of the string to buffer
|
| 46 |
+
buffer[index + 3] = (size >> 24) & 0xff;
|
| 47 |
+
buffer[index + 2] = (size >> 16) & 0xff;
|
| 48 |
+
buffer[index + 1] = (size >> 8) & 0xff;
|
| 49 |
+
buffer[index] = size & 0xff;
|
| 50 |
+
// Ajust the index
|
| 51 |
+
index = index + 4;
|
| 52 |
+
// Write the string
|
| 53 |
+
buffer.write(value, index, 'utf8');
|
| 54 |
+
// Update index
|
| 55 |
+
index = index + size - 1;
|
| 56 |
+
// Write zero
|
| 57 |
+
buffer[index++] = 0;
|
| 58 |
+
return index;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
var serializeNumber = function(key, value, index) {
|
| 62 |
+
// We have an integer value
|
| 63 |
+
if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
| 64 |
+
// If the value fits in 32 bits encode as int, if it fits in a double
|
| 65 |
+
// encode it as a double, otherwise long
|
| 66 |
+
if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
|
| 67 |
+
// Set int type 32 bits or less
|
| 68 |
+
buffer[index++] = BSON.BSON_DATA_INT;
|
| 69 |
+
// Number of written bytes
|
| 70 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 71 |
+
// Encode the name
|
| 72 |
+
index = index + numberOfWrittenBytes;
|
| 73 |
+
buffer[index++] = 0;
|
| 74 |
+
// Write the int value
|
| 75 |
+
buffer[index++] = value & 0xff;
|
| 76 |
+
buffer[index++] = (value >> 8) & 0xff;
|
| 77 |
+
buffer[index++] = (value >> 16) & 0xff;
|
| 78 |
+
buffer[index++] = (value >> 24) & 0xff;
|
| 79 |
+
} else if(value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
| 80 |
+
// Encode as double
|
| 81 |
+
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
| 82 |
+
// Number of written bytes
|
| 83 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 84 |
+
// Encode the name
|
| 85 |
+
index = index + numberOfWrittenBytes;
|
| 86 |
+
buffer[index++] = 0;
|
| 87 |
+
// Write float
|
| 88 |
+
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
| 89 |
+
// Ajust index
|
| 90 |
+
index = index + 8;
|
| 91 |
+
} else {
|
| 92 |
+
// Set long type
|
| 93 |
+
buffer[index++] = BSON.BSON_DATA_LONG;
|
| 94 |
+
// Number of written bytes
|
| 95 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 96 |
+
// Encode the name
|
| 97 |
+
index = index + numberOfWrittenBytes;
|
| 98 |
+
buffer[index++] = 0;
|
| 99 |
+
var longVal = Long.fromNumber(value);
|
| 100 |
+
var lowBits = longVal.getLowBits();
|
| 101 |
+
var highBits = longVal.getHighBits();
|
| 102 |
+
// Encode low bits
|
| 103 |
+
buffer[index++] = lowBits & 0xff;
|
| 104 |
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
| 105 |
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
| 106 |
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
| 107 |
+
// Encode high bits
|
| 108 |
+
buffer[index++] = highBits & 0xff;
|
| 109 |
+
buffer[index++] = (highBits >> 8) & 0xff;
|
| 110 |
+
buffer[index++] = (highBits >> 16) & 0xff;
|
| 111 |
+
buffer[index++] = (highBits >> 24) & 0xff;
|
| 112 |
+
}
|
| 113 |
+
} else {
|
| 114 |
+
// Encode as double
|
| 115 |
+
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
| 116 |
+
// Number of written bytes
|
| 117 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 118 |
+
// Encode the name
|
| 119 |
+
index = index + numberOfWrittenBytes;
|
| 120 |
+
buffer[index++] = 0;
|
| 121 |
+
// Write float
|
| 122 |
+
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
| 123 |
+
// Ajust index
|
| 124 |
+
index = index + 8;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
return index;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
var serializeUndefined = function(key, value, index) {
|
| 131 |
+
// Set long type
|
| 132 |
+
buffer[index++] = BSON.BSON_DATA_NULL;
|
| 133 |
+
// Number of written bytes
|
| 134 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 135 |
+
// Encode the name
|
| 136 |
+
index = index + numberOfWrittenBytes;
|
| 137 |
+
buffer[index++] = 0;
|
| 138 |
+
return index;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
var serializeBoolean = function(key, value, index) {
|
| 142 |
+
// Write the type
|
| 143 |
+
buffer[index++] = BSON.BSON_DATA_BOOLEAN;
|
| 144 |
+
// Number of written bytes
|
| 145 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 146 |
+
// Encode the name
|
| 147 |
+
index = index + numberOfWrittenBytes;
|
| 148 |
+
buffer[index++] = 0;
|
| 149 |
+
// Encode the boolean value
|
| 150 |
+
buffer[index++] = value ? 1 : 0;
|
| 151 |
+
return index;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
var serializeDate = function(key, value, index) {
|
| 155 |
+
// Write the type
|
| 156 |
+
buffer[index++] = BSON.BSON_DATA_DATE;
|
| 157 |
+
// Number of written bytes
|
| 158 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 159 |
+
// Encode the name
|
| 160 |
+
index = index + numberOfWrittenBytes;
|
| 161 |
+
buffer[index++] = 0;
|
| 162 |
+
|
| 163 |
+
// Write the date
|
| 164 |
+
var dateInMilis = Long.fromNumber(value.getTime());
|
| 165 |
+
var lowBits = dateInMilis.getLowBits();
|
| 166 |
+
var highBits = dateInMilis.getHighBits();
|
| 167 |
+
// Encode low bits
|
| 168 |
+
buffer[index++] = lowBits & 0xff;
|
| 169 |
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
| 170 |
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
| 171 |
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
| 172 |
+
// Encode high bits
|
| 173 |
+
buffer[index++] = highBits & 0xff;
|
| 174 |
+
buffer[index++] = (highBits >> 8) & 0xff;
|
| 175 |
+
buffer[index++] = (highBits >> 16) & 0xff;
|
| 176 |
+
buffer[index++] = (highBits >> 24) & 0xff;
|
| 177 |
+
return index;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
var serializeRegExp = function(key, value, index) {
|
| 181 |
+
// Write the type
|
| 182 |
+
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
| 183 |
+
// Number of written bytes
|
| 184 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 185 |
+
// Encode the name
|
| 186 |
+
index = index + numberOfWrittenBytes;
|
| 187 |
+
buffer[index++] = 0;
|
| 188 |
+
|
| 189 |
+
// Write the regular expression string
|
| 190 |
+
buffer.write(value.source, index, 'utf8');
|
| 191 |
+
// Adjust the index
|
| 192 |
+
index = index + Buffer.byteLength(value.source);
|
| 193 |
+
// Write zero
|
| 194 |
+
buffer[index++] = 0x00;
|
| 195 |
+
// Write the parameters
|
| 196 |
+
if(value.global) buffer[index++] = 0x73; // s
|
| 197 |
+
if(value.ignoreCase) buffer[index++] = 0x69; // i
|
| 198 |
+
if(value.multiline) buffer[index++] = 0x6d; // m
|
| 199 |
+
// Add ending zero
|
| 200 |
+
buffer[index++] = 0x00;
|
| 201 |
+
return index;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
var serializeMinMax = function(key, value, index) {
|
| 205 |
+
// Write the type of either min or max key
|
| 206 |
+
if(value === null) {
|
| 207 |
+
buffer[index++] = BSON.BSON_DATA_NULL;
|
| 208 |
+
} else if(value instanceof MinKey) {
|
| 209 |
+
buffer[index++] = BSON.BSON_DATA_MIN_KEY;
|
| 210 |
+
} else {
|
| 211 |
+
buffer[index++] = BSON.BSON_DATA_MAX_KEY;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
// Number of written bytes
|
| 215 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 216 |
+
// Encode the name
|
| 217 |
+
index = index + numberOfWrittenBytes;
|
| 218 |
+
buffer[index++] = 0;
|
| 219 |
+
return index;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
var serializeObjectId = function(key, value, index) {
|
| 223 |
+
// Write the type
|
| 224 |
+
buffer[index++] = BSON.BSON_DATA_OID;
|
| 225 |
+
// Number of written bytes
|
| 226 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 227 |
+
// Encode the name
|
| 228 |
+
index = index + numberOfWrittenBytes;
|
| 229 |
+
buffer[index++] = 0;
|
| 230 |
+
|
| 231 |
+
for(var j = 0; j < 12; j++) {
|
| 232 |
+
buffer[index + j] = value.binId[j];
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
// Ajust index
|
| 236 |
+
index = index + 12;
|
| 237 |
+
return index;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
var serializeBuffer = function(key, value, index) {
|
| 241 |
+
// Write the type
|
| 242 |
+
buffer[index++] = BSON.BSON_DATA_BINARY;
|
| 243 |
+
// Number of written bytes
|
| 244 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 245 |
+
// Encode the name
|
| 246 |
+
index = index + numberOfWrittenBytes;
|
| 247 |
+
buffer[index++] = 0;
|
| 248 |
+
// Get size of the buffer (current write point)
|
| 249 |
+
var size = value.length;
|
| 250 |
+
// Write the size of the string to buffer
|
| 251 |
+
buffer[index++] = size & 0xff;
|
| 252 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 253 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 254 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 255 |
+
// Write the default subtype
|
| 256 |
+
buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT;
|
| 257 |
+
// Copy the content form the binary field to the buffer
|
| 258 |
+
value.copy(buffer, index, 0, size);
|
| 259 |
+
// Adjust the index
|
| 260 |
+
index = index + size;
|
| 261 |
+
return index;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
var serializeObject = function(key, value, index, checkKeys, depth) {
|
| 265 |
+
// Write the type
|
| 266 |
+
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT;
|
| 267 |
+
// Number of written bytes
|
| 268 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 269 |
+
// Encode the name
|
| 270 |
+
index = index + numberOfWrittenBytes;
|
| 271 |
+
buffer[index++] = 0;
|
| 272 |
+
var endIndex = serializeInto(value, checkKeys, index, depth + 1);
|
| 273 |
+
// Write size
|
| 274 |
+
var size = endIndex - index;
|
| 275 |
+
return endIndex;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
var serializeLong = function(key, value, index) {
|
| 279 |
+
// Write the type
|
| 280 |
+
buffer[index++] = value instanceof Long ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP;
|
| 281 |
+
// Number of written bytes
|
| 282 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 283 |
+
// Encode the name
|
| 284 |
+
index = index + numberOfWrittenBytes;
|
| 285 |
+
buffer[index++] = 0;
|
| 286 |
+
// Write the date
|
| 287 |
+
var lowBits = value.getLowBits();
|
| 288 |
+
var highBits = value.getHighBits();
|
| 289 |
+
// Encode low bits
|
| 290 |
+
buffer[index++] = lowBits & 0xff;
|
| 291 |
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
| 292 |
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
| 293 |
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
| 294 |
+
// Encode high bits
|
| 295 |
+
buffer[index++] = highBits & 0xff;
|
| 296 |
+
buffer[index++] = (highBits >> 8) & 0xff;
|
| 297 |
+
buffer[index++] = (highBits >> 16) & 0xff;
|
| 298 |
+
buffer[index++] = (highBits >> 24) & 0xff;
|
| 299 |
+
return index;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
var serializeDouble = function(key, value, index) {
|
| 303 |
+
// Encode as double
|
| 304 |
+
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
| 305 |
+
// Number of written bytes
|
| 306 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 307 |
+
// Encode the name
|
| 308 |
+
index = index + numberOfWrittenBytes;
|
| 309 |
+
buffer[index++] = 0;
|
| 310 |
+
// Write float
|
| 311 |
+
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
| 312 |
+
// Ajust index
|
| 313 |
+
index = index + 8;
|
| 314 |
+
return index;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
var serializeCode = function(key, value, index, checkKeys, depth) {
|
| 318 |
+
if(value.scope != null && Object.keys(value.scope).length > 0) {
|
| 319 |
+
// Write the type
|
| 320 |
+
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
|
| 321 |
+
// Number of written bytes
|
| 322 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 323 |
+
// Encode the name
|
| 324 |
+
index = index + numberOfWrittenBytes;
|
| 325 |
+
buffer[index++] = 0;
|
| 326 |
+
|
| 327 |
+
// Starting index
|
| 328 |
+
var startIndex = index;
|
| 329 |
+
|
| 330 |
+
// Serialize the function
|
| 331 |
+
// Get the function string
|
| 332 |
+
var functionString = typeof value.code == 'string' ? value.code : value.code.toString();
|
| 333 |
+
var codeSize = Buffer.byteLength(functionString) + 1;
|
| 334 |
+
// Index adjustment
|
| 335 |
+
index = index + 4;
|
| 336 |
+
// Write the size of the string to buffer
|
| 337 |
+
buffer[index] = codeSize & 0xff;
|
| 338 |
+
buffer[index + 1] = (codeSize >> 8) & 0xff;
|
| 339 |
+
buffer[index + 2] = (codeSize >> 16) & 0xff;
|
| 340 |
+
buffer[index + 3] = (codeSize >> 24) & 0xff;
|
| 341 |
+
// Write string into buffer
|
| 342 |
+
buffer.write(functionString, index + 4, 'utf8');
|
| 343 |
+
// Write end 0
|
| 344 |
+
buffer[index + 4 + codeSize - 1] = 0;
|
| 345 |
+
// Write the
|
| 346 |
+
index = index + codeSize + 4;
|
| 347 |
+
|
| 348 |
+
//
|
| 349 |
+
// Serialize the scope value
|
| 350 |
+
var endIndex = serializeInto(value.scope, checkKeys, index, depth + 1)
|
| 351 |
+
index = endIndex - 1;
|
| 352 |
+
|
| 353 |
+
// Writ the total
|
| 354 |
+
var totalSize = endIndex - startIndex;
|
| 355 |
+
|
| 356 |
+
// Write the total size of the object
|
| 357 |
+
buffer[startIndex++] = totalSize & 0xff;
|
| 358 |
+
buffer[startIndex++] = (totalSize >> 8) & 0xff;
|
| 359 |
+
buffer[startIndex++] = (totalSize >> 16) & 0xff;
|
| 360 |
+
buffer[startIndex++] = (totalSize >> 24) & 0xff;
|
| 361 |
+
// Write trailing zero
|
| 362 |
+
buffer[index++] = 0;
|
| 363 |
+
} else {
|
| 364 |
+
buffer[index++] = BSON.BSON_DATA_CODE;
|
| 365 |
+
// Number of written bytes
|
| 366 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 367 |
+
// Encode the name
|
| 368 |
+
index = index + numberOfWrittenBytes;
|
| 369 |
+
buffer[index++] = 0;
|
| 370 |
+
// Function string
|
| 371 |
+
var functionString = value.code.toString();
|
| 372 |
+
// Function Size
|
| 373 |
+
var size = Buffer.byteLength(functionString) + 1;
|
| 374 |
+
// Write the size of the string to buffer
|
| 375 |
+
buffer[index++] = size & 0xff;
|
| 376 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 377 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 378 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 379 |
+
// Write the string
|
| 380 |
+
buffer.write(functionString, index, 'utf8');
|
| 381 |
+
// Update index
|
| 382 |
+
index = index + size - 1;
|
| 383 |
+
// Write zero
|
| 384 |
+
buffer[index++] = 0;
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
return index;
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
var serializeBinary = function(key, value, index) {
|
| 391 |
+
// Write the type
|
| 392 |
+
buffer[index++] = BSON.BSON_DATA_BINARY;
|
| 393 |
+
// Number of written bytes
|
| 394 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 395 |
+
// Encode the name
|
| 396 |
+
index = index + numberOfWrittenBytes;
|
| 397 |
+
buffer[index++] = 0;
|
| 398 |
+
// Extract the buffer
|
| 399 |
+
var data = value.value(true);
|
| 400 |
+
// Calculate size
|
| 401 |
+
var size = value.position;
|
| 402 |
+
// Write the size of the string to buffer
|
| 403 |
+
buffer[index++] = size & 0xff;
|
| 404 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 405 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 406 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 407 |
+
// Write the subtype to the buffer
|
| 408 |
+
buffer[index++] = value.sub_type;
|
| 409 |
+
|
| 410 |
+
// If we have binary type 2 the 4 first bytes are the size
|
| 411 |
+
if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
|
| 412 |
+
buffer[index++] = size & 0xff;
|
| 413 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 414 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 415 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
// Write the data to the object
|
| 419 |
+
data.copy(buffer, index, 0, value.position);
|
| 420 |
+
// Adjust the index
|
| 421 |
+
index = index + value.position;
|
| 422 |
+
return index;
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
var serializeSymbol = function(key, value, index) {
|
| 426 |
+
// Write the type
|
| 427 |
+
buffer[index++] = BSON.BSON_DATA_SYMBOL;
|
| 428 |
+
// Number of written bytes
|
| 429 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 430 |
+
// Encode the name
|
| 431 |
+
index = index + numberOfWrittenBytes;
|
| 432 |
+
buffer[index++] = 0;
|
| 433 |
+
// Calculate size
|
| 434 |
+
var size = Buffer.byteLength(value.value) + 1;
|
| 435 |
+
// Write the size of the string to buffer
|
| 436 |
+
buffer[index++] = size & 0xff;
|
| 437 |
+
buffer[index++] = (size >> 8) & 0xff;
|
| 438 |
+
buffer[index++] = (size >> 16) & 0xff;
|
| 439 |
+
buffer[index++] = (size >> 24) & 0xff;
|
| 440 |
+
// Write the string
|
| 441 |
+
buffer.write(value.value, index, 'utf8');
|
| 442 |
+
// Update index
|
| 443 |
+
index = index + size - 1;
|
| 444 |
+
// Write zero
|
| 445 |
+
buffer[index++] = 0x00;
|
| 446 |
+
return index;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
var serializeDBRef = function(key, value, index, depth) {
|
| 450 |
+
// Write the type
|
| 451 |
+
buffer[index++] = BSON.BSON_DATA_OBJECT;
|
| 452 |
+
// Number of written bytes
|
| 453 |
+
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
| 454 |
+
|
| 455 |
+
// Encode the name
|
| 456 |
+
index = index + numberOfWrittenBytes;
|
| 457 |
+
buffer[index++] = 0;
|
| 458 |
+
|
| 459 |
+
var startIndex = index;
|
| 460 |
+
var endIndex;
|
| 461 |
+
|
| 462 |
+
// Serialize object
|
| 463 |
+
if(null != value.db) {
|
| 464 |
+
endIndex = serializeInto({
|
| 465 |
+
'$ref': value.namespace
|
| 466 |
+
, '$id' : value.oid
|
| 467 |
+
, '$db' : value.db
|
| 468 |
+
}, false, index, depth + 1);
|
| 469 |
+
} else {
|
| 470 |
+
endIndex = serializeInto({
|
| 471 |
+
'$ref': value.namespace
|
| 472 |
+
, '$id' : value.oid
|
| 473 |
+
}, false, index, depth + 1);
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
// Calculate object size
|
| 477 |
+
var size = endIndex - startIndex;
|
| 478 |
+
// Write the size
|
| 479 |
+
buffer[startIndex++] = size & 0xff;
|
| 480 |
+
buffer[startIndex++] = (size >> 8) & 0xff;
|
| 481 |
+
buffer[startIndex++] = (size >> 16) & 0xff;
|
| 482 |
+
buffer[startIndex++] = (size >> 24) & 0xff;
|
| 483 |
+
// Set index
|
| 484 |
+
return endIndex;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
var BSON = function() {
|
| 488 |
+
this.buffer = buffer;
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
BSON.prototype.serialize = function serialize(object, checkKeys, index) {
|
| 492 |
+
var finishedBuffer = new Buffer(serializeInto(object, checkKeys, index || 0, 0));
|
| 493 |
+
this.buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
|
| 494 |
+
return finishedBuffer;
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
var serializeInto = function serializeInto(object, checkKeys, startingIndex, depth) {
|
| 498 |
+
startingIndex = startingIndex || 0;
|
| 499 |
+
|
| 500 |
+
// Start place to serialize into
|
| 501 |
+
var index = startingIndex + 4;
|
| 502 |
+
var self = this;
|
| 503 |
+
|
| 504 |
+
// Special case isArray
|
| 505 |
+
if(Array.isArray(object)) {
|
| 506 |
+
// Get object keys
|
| 507 |
+
for(var i = 0; i < object.length; i++) {
|
| 508 |
+
var key = "" + i;
|
| 509 |
+
var type = typeof object[i];
|
| 510 |
+
|
| 511 |
+
// Check the key and throw error if it's illegal
|
| 512 |
+
if(key != '$db' && key != '$ref' && key != '$id') {
|
| 513 |
+
checkKey(key, !checkKeys);
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
if(type == 'string') {
|
| 517 |
+
index = serializeString(key, object[i], index);
|
| 518 |
+
} else if(type == 'number') {
|
| 519 |
+
index = serializeNumber(key, object[i], index);
|
| 520 |
+
} else if(type == 'undefined') {
|
| 521 |
+
index = serializeUndefined(key, object[i], index);
|
| 522 |
+
} else if(type == 'boolean') {
|
| 523 |
+
index = serializeBoolean(key, object[i], index);
|
| 524 |
+
} else if(object[i] instanceof Date) {
|
| 525 |
+
index = serializeDate(key, object[i], index);
|
| 526 |
+
} else if(object[i] instanceof RegExp || Object.prototype.toString.call(object[i]) === '[object RegExp]') {
|
| 527 |
+
index = serializeRegExp(key, object[i], index);
|
| 528 |
+
} else if(object[i]['_bsontype'] == 'MinKey' || object[i]['_bsontype'] == 'MaxKey') {
|
| 529 |
+
index = serializeMinMax(key, object[i], index);
|
| 530 |
+
} else if(object[i]['_bsontype'] == 'ObjectID') {
|
| 531 |
+
index = serializeObjectId(key, object[i], index);
|
| 532 |
+
} else if(Buffer.isBuffer(object[i])) {
|
| 533 |
+
index = serializeBuffer(key, object[i], index);
|
| 534 |
+
} else if(type == 'object' && object[i]['_bsontype'] == null) {
|
| 535 |
+
index = serializeObject(key, object[i], index, checkKeys, depth);
|
| 536 |
+
} else if(object[i]['_bsontype'] == 'Long' || object[i]['_bsontype'] == 'Timestamp') {
|
| 537 |
+
index = serializeLong(key, object[i], index);
|
| 538 |
+
} else if(object[i]['_bsontype'] == 'Double') {
|
| 539 |
+
index = serializeDouble(key, object[i], index);
|
| 540 |
+
} else if(object[i]['_bsontype'] == 'Code') {
|
| 541 |
+
index = serializeCode(key, object[i], index, checkKeys, depth);
|
| 542 |
+
} else if(object[i]['_bsontype'] == 'Binary') {
|
| 543 |
+
index = serializeBinary(key, object[i], index);
|
| 544 |
+
} else if(object[i]['_bsontype'] == 'Symbol') {
|
| 545 |
+
index = serializeSymbol(key, object[i], index);
|
| 546 |
+
} else if(object[i]['_bsontype'] == 'DBRef') {
|
| 547 |
+
index = serializeDBRef(key, object[i], index, depth);
|
| 548 |
+
}
|
| 549 |
+
}
|
| 550 |
+
} else {
|
| 551 |
+
var keys = Object.keys(object);
|
| 552 |
+
for(var i = 0; i < keys.length; i++) {
|
| 553 |
+
var key = keys[i];
|
| 554 |
+
var type = typeof object[key];
|
| 555 |
+
|
| 556 |
+
// Check the key and throw error if it's illegal
|
| 557 |
+
if(key != '$db' && key != '$ref' && key != '$id') {
|
| 558 |
+
checkKey(key, !checkKeys);
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
if(type == 'string') {
|
| 562 |
+
index = serializeString(key, object[key], index);
|
| 563 |
+
} else if(type == 'number') {
|
| 564 |
+
index = serializeNumber(key, object[key], index);
|
| 565 |
+
} else if(type == 'undefined') {
|
| 566 |
+
index = serializeUndefined(key, object[key], index);
|
| 567 |
+
} else if(type == 'boolean') {
|
| 568 |
+
index = serializeBoolean(key, object[key], index);
|
| 569 |
+
} else if(object[key] instanceof Date) {
|
| 570 |
+
index = serializeDate(key, object[key], index);
|
| 571 |
+
} else if(object[key] instanceof RegExp || Object.prototype.toString.call(object[key]) === '[object RegExp]') {
|
| 572 |
+
index = serializeRegExp(key, object[key], index);
|
| 573 |
+
} else if(object[key]['_bsontype'] == 'MinKey' || object[key]['_bsontype'] == 'MaxKey') {
|
| 574 |
+
index = serializeMinMax(key, object[key], index);
|
| 575 |
+
} else if(object[key]['_bsontype'] == 'ObjectID') {
|
| 576 |
+
index = serializeObjectId(key, object[key], index);
|
| 577 |
+
} else if(Buffer.isBuffer(object[key])) {
|
| 578 |
+
index = serializeBuffer(key, object[key], index);
|
| 579 |
+
} else if(type == 'object' && object[key]['_bsontype'] == null) {
|
| 580 |
+
index = serializeObject(key, object[key], index, checkKeys, depth);
|
| 581 |
+
} else if(object[key]['_bsontype'] == 'Long' || object[key]['_bsontype'] == 'Timestamp') {
|
| 582 |
+
index = serializeLong(key, object[key], index);
|
| 583 |
+
} else if(object[key]['_bsontype'] == 'Double') {
|
| 584 |
+
index = serializeDouble(key, object[key], index);
|
| 585 |
+
} else if(object[key]['_bsontype'] == 'Code') {
|
| 586 |
+
index = serializeCode(key, object[key], index, checkKeys, depth);
|
| 587 |
+
} else if(object[key]['_bsontype'] == 'Binary') {
|
| 588 |
+
index = serializeBinary(key, object[key], index);
|
| 589 |
+
} else if(object[key]['_bsontype'] == 'Symbol') {
|
| 590 |
+
index = serializeSymbol(key, object[key], index);
|
| 591 |
+
} else if(object[key]['_bsontype'] == 'DBRef') {
|
| 592 |
+
index = serializeDBRef(key, object[key], index, depth);
|
| 593 |
+
}
|
| 594 |
+
}
|
| 595 |
+
}
|
| 596 |
+
|
| 597 |
+
// Final padding byte for object
|
| 598 |
+
buffer[index++] = 0x00;
|
| 599 |
+
|
| 600 |
+
// Final size
|
| 601 |
+
var size = index - startingIndex;
|
| 602 |
+
// Write the size of the object
|
| 603 |
+
buffer[startingIndex++] = size & 0xff;
|
| 604 |
+
buffer[startingIndex++] = (size >> 8) & 0xff;
|
| 605 |
+
buffer[startingIndex++] = (size >> 16) & 0xff;
|
| 606 |
+
buffer[startingIndex++] = (size >> 24) & 0xff;
|
| 607 |
+
return index;
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
/**
|
| 611 |
+
* @ignore
|
| 612 |
+
* @api private
|
| 613 |
+
*/
|
| 614 |
+
// BSON MAX VALUES
|
| 615 |
+
BSON.BSON_INT32_MAX = 0x7FFFFFFF;
|
| 616 |
+
BSON.BSON_INT32_MIN = -0x80000000;
|
| 617 |
+
|
| 618 |
+
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
| 619 |
+
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
|
| 620 |
+
|
| 621 |
+
// JS MAX PRECISE VALUES
|
| 622 |
+
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
|
| 623 |
+
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
|
| 624 |
+
|
| 625 |
+
// Internal long versions
|
| 626 |
+
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
|
| 627 |
+
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
|
| 628 |
+
|
| 629 |
+
/**
|
| 630 |
+
* Number BSON Type
|
| 631 |
+
*
|
| 632 |
+
* @classconstant BSON_DATA_NUMBER
|
| 633 |
+
**/
|
| 634 |
+
BSON.BSON_DATA_NUMBER = 1;
|
| 635 |
+
/**
|
| 636 |
+
* String BSON Type
|
| 637 |
+
*
|
| 638 |
+
* @classconstant BSON_DATA_STRING
|
| 639 |
+
**/
|
| 640 |
+
BSON.BSON_DATA_STRING = 2;
|
| 641 |
+
/**
|
| 642 |
+
* Object BSON Type
|
| 643 |
+
*
|
| 644 |
+
* @classconstant BSON_DATA_OBJECT
|
| 645 |
+
**/
|
| 646 |
+
BSON.BSON_DATA_OBJECT = 3;
|
| 647 |
+
/**
|
| 648 |
+
* Array BSON Type
|
| 649 |
+
*
|
| 650 |
+
* @classconstant BSON_DATA_ARRAY
|
| 651 |
+
**/
|
| 652 |
+
BSON.BSON_DATA_ARRAY = 4;
|
| 653 |
+
/**
|
| 654 |
+
* Binary BSON Type
|
| 655 |
+
*
|
| 656 |
+
* @classconstant BSON_DATA_BINARY
|
| 657 |
+
**/
|
| 658 |
+
BSON.BSON_DATA_BINARY = 5;
|
| 659 |
+
/**
|
| 660 |
+
* ObjectID BSON Type
|
| 661 |
+
*
|
| 662 |
+
* @classconstant BSON_DATA_OID
|
| 663 |
+
**/
|
| 664 |
+
BSON.BSON_DATA_OID = 7;
|
| 665 |
+
/**
|
| 666 |
+
* Boolean BSON Type
|
| 667 |
+
*
|
| 668 |
+
* @classconstant BSON_DATA_BOOLEAN
|
| 669 |
+
**/
|
| 670 |
+
BSON.BSON_DATA_BOOLEAN = 8;
|
| 671 |
+
/**
|
| 672 |
+
* Date BSON Type
|
| 673 |
+
*
|
| 674 |
+
* @classconstant BSON_DATA_DATE
|
| 675 |
+
**/
|
| 676 |
+
BSON.BSON_DATA_DATE = 9;
|
| 677 |
+
/**
|
| 678 |
+
* null BSON Type
|
| 679 |
+
*
|
| 680 |
+
* @classconstant BSON_DATA_NULL
|
| 681 |
+
**/
|
| 682 |
+
BSON.BSON_DATA_NULL = 10;
|
| 683 |
+
/**
|
| 684 |
+
* RegExp BSON Type
|
| 685 |
+
*
|
| 686 |
+
* @classconstant BSON_DATA_REGEXP
|
| 687 |
+
**/
|
| 688 |
+
BSON.BSON_DATA_REGEXP = 11;
|
| 689 |
+
/**
|
| 690 |
+
* Code BSON Type
|
| 691 |
+
*
|
| 692 |
+
* @classconstant BSON_DATA_CODE
|
| 693 |
+
**/
|
| 694 |
+
BSON.BSON_DATA_CODE = 13;
|
| 695 |
+
/**
|
| 696 |
+
* Symbol BSON Type
|
| 697 |
+
*
|
| 698 |
+
* @classconstant BSON_DATA_SYMBOL
|
| 699 |
+
**/
|
| 700 |
+
BSON.BSON_DATA_SYMBOL = 14;
|
| 701 |
+
/**
|
| 702 |
+
* Code with Scope BSON Type
|
| 703 |
+
*
|
| 704 |
+
* @classconstant BSON_DATA_CODE_W_SCOPE
|
| 705 |
+
**/
|
| 706 |
+
BSON.BSON_DATA_CODE_W_SCOPE = 15;
|
| 707 |
+
/**
|
| 708 |
+
* 32 bit Integer BSON Type
|
| 709 |
+
*
|
| 710 |
+
* @classconstant BSON_DATA_INT
|
| 711 |
+
**/
|
| 712 |
+
BSON.BSON_DATA_INT = 16;
|
| 713 |
+
/**
|
| 714 |
+
* Timestamp BSON Type
|
| 715 |
+
*
|
| 716 |
+
* @classconstant BSON_DATA_TIMESTAMP
|
| 717 |
+
**/
|
| 718 |
+
BSON.BSON_DATA_TIMESTAMP = 17;
|
| 719 |
+
/**
|
| 720 |
+
* Long BSON Type
|
| 721 |
+
*
|
| 722 |
+
* @classconstant BSON_DATA_LONG
|
| 723 |
+
**/
|
| 724 |
+
BSON.BSON_DATA_LONG = 18;
|
| 725 |
+
/**
|
| 726 |
+
* MinKey BSON Type
|
| 727 |
+
*
|
| 728 |
+
* @classconstant BSON_DATA_MIN_KEY
|
| 729 |
+
**/
|
| 730 |
+
BSON.BSON_DATA_MIN_KEY = 0xff;
|
| 731 |
+
/**
|
| 732 |
+
* MaxKey BSON Type
|
| 733 |
+
*
|
| 734 |
+
* @classconstant BSON_DATA_MAX_KEY
|
| 735 |
+
**/
|
| 736 |
+
BSON.BSON_DATA_MAX_KEY = 0x7f;
|
| 737 |
+
|
| 738 |
+
/**
|
| 739 |
+
* Binary Default Type
|
| 740 |
+
*
|
| 741 |
+
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
|
| 742 |
+
**/
|
| 743 |
+
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
| 744 |
+
/**
|
| 745 |
+
* Binary Function Type
|
| 746 |
+
*
|
| 747 |
+
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
|
| 748 |
+
**/
|
| 749 |
+
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
| 750 |
+
/**
|
| 751 |
+
* Binary Byte Array Type
|
| 752 |
+
*
|
| 753 |
+
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
|
| 754 |
+
**/
|
| 755 |
+
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
| 756 |
+
/**
|
| 757 |
+
* Binary UUID Type
|
| 758 |
+
*
|
| 759 |
+
* @classconstant BSON_BINARY_SUBTYPE_UUID
|
| 760 |
+
**/
|
| 761 |
+
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
|
| 762 |
+
/**
|
| 763 |
+
* Binary MD5 Type
|
| 764 |
+
*
|
| 765 |
+
* @classconstant BSON_BINARY_SUBTYPE_MD5
|
| 766 |
+
**/
|
| 767 |
+
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
|
| 768 |
+
/**
|
| 769 |
+
* Binary User Defined Type
|
| 770 |
+
*
|
| 771 |
+
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
|
| 772 |
+
**/
|
| 773 |
+
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
| 774 |
+
|
| 775 |
+
// Return BSON
|
| 776 |
+
exports.BSON = BSON;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/code.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* A class representation of the BSON Code type.
|
| 3 |
+
*
|
| 4 |
+
* @class
|
| 5 |
+
* @param {(string|function)} code a string or function.
|
| 6 |
+
* @param {Object} [scope] an optional scope for the function.
|
| 7 |
+
* @return {Code}
|
| 8 |
+
*/
|
| 9 |
+
var Code = function Code(code, scope) {
|
| 10 |
+
if(!(this instanceof Code)) return new Code(code, scope);
|
| 11 |
+
this._bsontype = 'Code';
|
| 12 |
+
this.code = code;
|
| 13 |
+
this.scope = scope == null ? {} : scope;
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
/**
|
| 17 |
+
* @ignore
|
| 18 |
+
*/
|
| 19 |
+
Code.prototype.toJSON = function() {
|
| 20 |
+
return {scope:this.scope, code:this.code};
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
module.exports = Code;
|
| 24 |
+
module.exports.Code = Code;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/db_ref.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* A class representation of the BSON DBRef type.
|
| 3 |
+
*
|
| 4 |
+
* @class
|
| 5 |
+
* @param {string} namespace the collection name.
|
| 6 |
+
* @param {ObjectID} oid the reference ObjectID.
|
| 7 |
+
* @param {string} [db] optional db name, if omitted the reference is local to the current db.
|
| 8 |
+
* @return {DBRef}
|
| 9 |
+
*/
|
| 10 |
+
function DBRef(namespace, oid, db) {
|
| 11 |
+
if(!(this instanceof DBRef)) return new DBRef(namespace, oid, db);
|
| 12 |
+
|
| 13 |
+
this._bsontype = 'DBRef';
|
| 14 |
+
this.namespace = namespace;
|
| 15 |
+
this.oid = oid;
|
| 16 |
+
this.db = db;
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
/**
|
| 20 |
+
* @ignore
|
| 21 |
+
* @api private
|
| 22 |
+
*/
|
| 23 |
+
DBRef.prototype.toJSON = function() {
|
| 24 |
+
return {
|
| 25 |
+
'$ref':this.namespace,
|
| 26 |
+
'$id':this.oid,
|
| 27 |
+
'$db':this.db == null ? '' : this.db
|
| 28 |
+
};
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
module.exports = DBRef;
|
| 32 |
+
module.exports.DBRef = DBRef;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/double.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* A class representation of the BSON Double type.
|
| 3 |
+
*
|
| 4 |
+
* @class
|
| 5 |
+
* @param {number} value the number we want to represent as a double.
|
| 6 |
+
* @return {Double}
|
| 7 |
+
*/
|
| 8 |
+
function Double(value) {
|
| 9 |
+
if(!(this instanceof Double)) return new Double(value);
|
| 10 |
+
|
| 11 |
+
this._bsontype = 'Double';
|
| 12 |
+
this.value = value;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* Access the number value.
|
| 17 |
+
*
|
| 18 |
+
* @method
|
| 19 |
+
* @return {number} returns the wrapped double number.
|
| 20 |
+
*/
|
| 21 |
+
Double.prototype.valueOf = function() {
|
| 22 |
+
return this.value;
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
/**
|
| 26 |
+
* @ignore
|
| 27 |
+
*/
|
| 28 |
+
Double.prototype.toJSON = function() {
|
| 29 |
+
return this.value;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
module.exports = Double;
|
| 33 |
+
module.exports.Double = Double;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/float_parser.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright (c) 2008, Fair Oaks Labs, Inc.
|
| 2 |
+
// All rights reserved.
|
| 3 |
+
//
|
| 4 |
+
// Redistribution and use in source and binary forms, with or without
|
| 5 |
+
// modification, are permitted provided that the following conditions are met:
|
| 6 |
+
//
|
| 7 |
+
// * Redistributions of source code must retain the above copyright notice,
|
| 8 |
+
// this list of conditions and the following disclaimer.
|
| 9 |
+
//
|
| 10 |
+
// * Redistributions in binary form must reproduce the above copyright notice,
|
| 11 |
+
// this list of conditions and the following disclaimer in the documentation
|
| 12 |
+
// and/or other materials provided with the distribution.
|
| 13 |
+
//
|
| 14 |
+
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
|
| 15 |
+
// may be used to endorse or promote products derived from this software
|
| 16 |
+
// without specific prior written permission.
|
| 17 |
+
//
|
| 18 |
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 19 |
+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 20 |
+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 21 |
+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
| 22 |
+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 23 |
+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 24 |
+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 25 |
+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 26 |
+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 27 |
+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 28 |
+
// POSSIBILITY OF SUCH DAMAGE.
|
| 29 |
+
//
|
| 30 |
+
//
|
| 31 |
+
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
|
| 32 |
+
|
| 33 |
+
var readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
|
| 34 |
+
var e, m,
|
| 35 |
+
bBE = (endian === 'big'),
|
| 36 |
+
eLen = nBytes * 8 - mLen - 1,
|
| 37 |
+
eMax = (1 << eLen) - 1,
|
| 38 |
+
eBias = eMax >> 1,
|
| 39 |
+
nBits = -7,
|
| 40 |
+
i = bBE ? 0 : (nBytes - 1),
|
| 41 |
+
d = bBE ? 1 : -1,
|
| 42 |
+
s = buffer[offset + i];
|
| 43 |
+
|
| 44 |
+
i += d;
|
| 45 |
+
|
| 46 |
+
e = s & ((1 << (-nBits)) - 1);
|
| 47 |
+
s >>= (-nBits);
|
| 48 |
+
nBits += eLen;
|
| 49 |
+
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
|
| 50 |
+
|
| 51 |
+
m = e & ((1 << (-nBits)) - 1);
|
| 52 |
+
e >>= (-nBits);
|
| 53 |
+
nBits += mLen;
|
| 54 |
+
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
|
| 55 |
+
|
| 56 |
+
if (e === 0) {
|
| 57 |
+
e = 1 - eBias;
|
| 58 |
+
} else if (e === eMax) {
|
| 59 |
+
return m ? NaN : ((s ? -1 : 1) * Infinity);
|
| 60 |
+
} else {
|
| 61 |
+
m = m + Math.pow(2, mLen);
|
| 62 |
+
e = e - eBias;
|
| 63 |
+
}
|
| 64 |
+
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
var writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) {
|
| 68 |
+
var e, m, c,
|
| 69 |
+
bBE = (endian === 'big'),
|
| 70 |
+
eLen = nBytes * 8 - mLen - 1,
|
| 71 |
+
eMax = (1 << eLen) - 1,
|
| 72 |
+
eBias = eMax >> 1,
|
| 73 |
+
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
|
| 74 |
+
i = bBE ? (nBytes-1) : 0,
|
| 75 |
+
d = bBE ? -1 : 1,
|
| 76 |
+
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
| 77 |
+
|
| 78 |
+
value = Math.abs(value);
|
| 79 |
+
|
| 80 |
+
if (isNaN(value) || value === Infinity) {
|
| 81 |
+
m = isNaN(value) ? 1 : 0;
|
| 82 |
+
e = eMax;
|
| 83 |
+
} else {
|
| 84 |
+
e = Math.floor(Math.log(value) / Math.LN2);
|
| 85 |
+
if (value * (c = Math.pow(2, -e)) < 1) {
|
| 86 |
+
e--;
|
| 87 |
+
c *= 2;
|
| 88 |
+
}
|
| 89 |
+
if (e+eBias >= 1) {
|
| 90 |
+
value += rt / c;
|
| 91 |
+
} else {
|
| 92 |
+
value += rt * Math.pow(2, 1 - eBias);
|
| 93 |
+
}
|
| 94 |
+
if (value * c >= 2) {
|
| 95 |
+
e++;
|
| 96 |
+
c /= 2;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
if (e + eBias >= eMax) {
|
| 100 |
+
m = 0;
|
| 101 |
+
e = eMax;
|
| 102 |
+
} else if (e + eBias >= 1) {
|
| 103 |
+
m = (value * c - 1) * Math.pow(2, mLen);
|
| 104 |
+
e = e + eBias;
|
| 105 |
+
} else {
|
| 106 |
+
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
| 107 |
+
e = 0;
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
|
| 112 |
+
|
| 113 |
+
e = (e << mLen) | m;
|
| 114 |
+
eLen += mLen;
|
| 115 |
+
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
|
| 116 |
+
|
| 117 |
+
buffer[offset + i - d] |= s * 128;
|
| 118 |
+
};
|
| 119 |
+
|
| 120 |
+
exports.readIEEE754 = readIEEE754;
|
| 121 |
+
exports.writeIEEE754 = writeIEEE754;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
try {
|
| 2 |
+
exports.BSONPure = require('./bson');
|
| 3 |
+
exports.BSONNative = require('bson-ext');
|
| 4 |
+
} catch(err) {
|
| 5 |
+
// do nothing
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
[ './binary_parser'
|
| 9 |
+
, './binary'
|
| 10 |
+
, './code'
|
| 11 |
+
, './db_ref'
|
| 12 |
+
, './double'
|
| 13 |
+
, './max_key'
|
| 14 |
+
, './min_key'
|
| 15 |
+
, './objectid'
|
| 16 |
+
, './symbol'
|
| 17 |
+
, './timestamp'
|
| 18 |
+
, './long'].forEach(function (path) {
|
| 19 |
+
var module = require('./' + path);
|
| 20 |
+
for (var i in module) {
|
| 21 |
+
exports[i] = module[i];
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
// Exports all the classes for the PURE JS BSON Parser
|
| 26 |
+
exports.pure = function() {
|
| 27 |
+
var classes = {};
|
| 28 |
+
// Map all the classes
|
| 29 |
+
[ './binary_parser'
|
| 30 |
+
, './binary'
|
| 31 |
+
, './code'
|
| 32 |
+
, './db_ref'
|
| 33 |
+
, './double'
|
| 34 |
+
, './max_key'
|
| 35 |
+
, './min_key'
|
| 36 |
+
, './objectid'
|
| 37 |
+
, './symbol'
|
| 38 |
+
, './timestamp'
|
| 39 |
+
, './long'
|
| 40 |
+
, '././bson'].forEach(function (path) {
|
| 41 |
+
var module = require('./' + path);
|
| 42 |
+
for (var i in module) {
|
| 43 |
+
classes[i] = module[i];
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
// Return classes list
|
| 47 |
+
return classes;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Exports all the classes for the NATIVE JS BSON Parser
|
| 51 |
+
exports.native = function() {
|
| 52 |
+
var classes = {};
|
| 53 |
+
// Map all the classes
|
| 54 |
+
[ './binary_parser'
|
| 55 |
+
, './binary'
|
| 56 |
+
, './code'
|
| 57 |
+
, './db_ref'
|
| 58 |
+
, './double'
|
| 59 |
+
, './max_key'
|
| 60 |
+
, './min_key'
|
| 61 |
+
, './objectid'
|
| 62 |
+
, './symbol'
|
| 63 |
+
, './timestamp'
|
| 64 |
+
, './long'
|
| 65 |
+
].forEach(function (path) {
|
| 66 |
+
var module = require('./' + path);
|
| 67 |
+
for (var i in module) {
|
| 68 |
+
classes[i] = module[i];
|
| 69 |
+
}
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
// Catch error and return no classes found
|
| 73 |
+
try {
|
| 74 |
+
classes['BSON'] = require('bson-ext')
|
| 75 |
+
} catch(err) {
|
| 76 |
+
return exports.pure();
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// Return classes list
|
| 80 |
+
return classes;
|
| 81 |
+
}
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/long.js
ADDED
|
@@ -0,0 +1,856 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 2 |
+
// you may not use this file except in compliance with the License.
|
| 3 |
+
// You may obtain a copy of the License at
|
| 4 |
+
//
|
| 5 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
| 6 |
+
//
|
| 7 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 8 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 9 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 10 |
+
// See the License for the specific language governing permissions and
|
| 11 |
+
// limitations under the License.
|
| 12 |
+
//
|
| 13 |
+
// Copyright 2009 Google Inc. All Rights Reserved
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* Defines a Long class for representing a 64-bit two's-complement
|
| 17 |
+
* integer value, which faithfully simulates the behavior of a Java "Long". This
|
| 18 |
+
* implementation is derived from LongLib in GWT.
|
| 19 |
+
*
|
| 20 |
+
* Constructs a 64-bit two's-complement integer, given its low and high 32-bit
|
| 21 |
+
* values as *signed* integers. See the from* functions below for more
|
| 22 |
+
* convenient ways of constructing Longs.
|
| 23 |
+
*
|
| 24 |
+
* The internal representation of a Long is the two given signed, 32-bit values.
|
| 25 |
+
* We use 32-bit pieces because these are the size of integers on which
|
| 26 |
+
* Javascript performs bit-operations. For operations like addition and
|
| 27 |
+
* multiplication, we split each number into 16-bit pieces, which can easily be
|
| 28 |
+
* multiplied within Javascript's floating-point representation without overflow
|
| 29 |
+
* or change in sign.
|
| 30 |
+
*
|
| 31 |
+
* In the algorithms below, we frequently reduce the negative case to the
|
| 32 |
+
* positive case by negating the input(s) and then post-processing the result.
|
| 33 |
+
* Note that we must ALWAYS check specially whether those values are MIN_VALUE
|
| 34 |
+
* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
|
| 35 |
+
* a positive number, it overflows back into a negative). Not handling this
|
| 36 |
+
* case would often result in infinite recursion.
|
| 37 |
+
*
|
| 38 |
+
* @class
|
| 39 |
+
* @param {number} low the low (signed) 32 bits of the Long.
|
| 40 |
+
* @param {number} high the high (signed) 32 bits of the Long.
|
| 41 |
+
* @return {Long}
|
| 42 |
+
*/
|
| 43 |
+
function Long(low, high) {
|
| 44 |
+
if(!(this instanceof Long)) return new Long(low, high);
|
| 45 |
+
|
| 46 |
+
this._bsontype = 'Long';
|
| 47 |
+
/**
|
| 48 |
+
* @type {number}
|
| 49 |
+
* @ignore
|
| 50 |
+
*/
|
| 51 |
+
this.low_ = low | 0; // force into 32 signed bits.
|
| 52 |
+
|
| 53 |
+
/**
|
| 54 |
+
* @type {number}
|
| 55 |
+
* @ignore
|
| 56 |
+
*/
|
| 57 |
+
this.high_ = high | 0; // force into 32 signed bits.
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
/**
|
| 61 |
+
* Return the int value.
|
| 62 |
+
*
|
| 63 |
+
* @method
|
| 64 |
+
* @return {number} the value, assuming it is a 32-bit integer.
|
| 65 |
+
*/
|
| 66 |
+
Long.prototype.toInt = function() {
|
| 67 |
+
return this.low_;
|
| 68 |
+
};
|
| 69 |
+
|
| 70 |
+
/**
|
| 71 |
+
* Return the Number value.
|
| 72 |
+
*
|
| 73 |
+
* @method
|
| 74 |
+
* @return {number} the closest floating-point representation to this value.
|
| 75 |
+
*/
|
| 76 |
+
Long.prototype.toNumber = function() {
|
| 77 |
+
return this.high_ * Long.TWO_PWR_32_DBL_ +
|
| 78 |
+
this.getLowBitsUnsigned();
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
/**
|
| 82 |
+
* Return the JSON value.
|
| 83 |
+
*
|
| 84 |
+
* @method
|
| 85 |
+
* @return {string} the JSON representation.
|
| 86 |
+
*/
|
| 87 |
+
Long.prototype.toJSON = function() {
|
| 88 |
+
return this.toString();
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/**
|
| 92 |
+
* Return the String value.
|
| 93 |
+
*
|
| 94 |
+
* @method
|
| 95 |
+
* @param {number} [opt_radix] the radix in which the text should be written.
|
| 96 |
+
* @return {string} the textual representation of this value.
|
| 97 |
+
*/
|
| 98 |
+
Long.prototype.toString = function(opt_radix) {
|
| 99 |
+
var radix = opt_radix || 10;
|
| 100 |
+
if (radix < 2 || 36 < radix) {
|
| 101 |
+
throw Error('radix out of range: ' + radix);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
if (this.isZero()) {
|
| 105 |
+
return '0';
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
if (this.isNegative()) {
|
| 109 |
+
if (this.equals(Long.MIN_VALUE)) {
|
| 110 |
+
// We need to change the Long value before it can be negated, so we remove
|
| 111 |
+
// the bottom-most digit in this base and then recurse to do the rest.
|
| 112 |
+
var radixLong = Long.fromNumber(radix);
|
| 113 |
+
var div = this.div(radixLong);
|
| 114 |
+
var rem = div.multiply(radixLong).subtract(this);
|
| 115 |
+
return div.toString(radix) + rem.toInt().toString(radix);
|
| 116 |
+
} else {
|
| 117 |
+
return '-' + this.negate().toString(radix);
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// Do several (6) digits each time through the loop, so as to
|
| 122 |
+
// minimize the calls to the very expensive emulated div.
|
| 123 |
+
var radixToPower = Long.fromNumber(Math.pow(radix, 6));
|
| 124 |
+
|
| 125 |
+
var rem = this;
|
| 126 |
+
var result = '';
|
| 127 |
+
while (true) {
|
| 128 |
+
var remDiv = rem.div(radixToPower);
|
| 129 |
+
var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
|
| 130 |
+
var digits = intval.toString(radix);
|
| 131 |
+
|
| 132 |
+
rem = remDiv;
|
| 133 |
+
if (rem.isZero()) {
|
| 134 |
+
return digits + result;
|
| 135 |
+
} else {
|
| 136 |
+
while (digits.length < 6) {
|
| 137 |
+
digits = '0' + digits;
|
| 138 |
+
}
|
| 139 |
+
result = '' + digits + result;
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
/**
|
| 145 |
+
* Return the high 32-bits value.
|
| 146 |
+
*
|
| 147 |
+
* @method
|
| 148 |
+
* @return {number} the high 32-bits as a signed value.
|
| 149 |
+
*/
|
| 150 |
+
Long.prototype.getHighBits = function() {
|
| 151 |
+
return this.high_;
|
| 152 |
+
};
|
| 153 |
+
|
| 154 |
+
/**
|
| 155 |
+
* Return the low 32-bits value.
|
| 156 |
+
*
|
| 157 |
+
* @method
|
| 158 |
+
* @return {number} the low 32-bits as a signed value.
|
| 159 |
+
*/
|
| 160 |
+
Long.prototype.getLowBits = function() {
|
| 161 |
+
return this.low_;
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
/**
|
| 165 |
+
* Return the low unsigned 32-bits value.
|
| 166 |
+
*
|
| 167 |
+
* @method
|
| 168 |
+
* @return {number} the low 32-bits as an unsigned value.
|
| 169 |
+
*/
|
| 170 |
+
Long.prototype.getLowBitsUnsigned = function() {
|
| 171 |
+
return (this.low_ >= 0) ?
|
| 172 |
+
this.low_ : Long.TWO_PWR_32_DBL_ + this.low_;
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
+
/**
|
| 176 |
+
* Returns the number of bits needed to represent the absolute value of this Long.
|
| 177 |
+
*
|
| 178 |
+
* @method
|
| 179 |
+
* @return {number} Returns the number of bits needed to represent the absolute value of this Long.
|
| 180 |
+
*/
|
| 181 |
+
Long.prototype.getNumBitsAbs = function() {
|
| 182 |
+
if (this.isNegative()) {
|
| 183 |
+
if (this.equals(Long.MIN_VALUE)) {
|
| 184 |
+
return 64;
|
| 185 |
+
} else {
|
| 186 |
+
return this.negate().getNumBitsAbs();
|
| 187 |
+
}
|
| 188 |
+
} else {
|
| 189 |
+
var val = this.high_ != 0 ? this.high_ : this.low_;
|
| 190 |
+
for (var bit = 31; bit > 0; bit--) {
|
| 191 |
+
if ((val & (1 << bit)) != 0) {
|
| 192 |
+
break;
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
return this.high_ != 0 ? bit + 33 : bit + 1;
|
| 196 |
+
}
|
| 197 |
+
};
|
| 198 |
+
|
| 199 |
+
/**
|
| 200 |
+
* Return whether this value is zero.
|
| 201 |
+
*
|
| 202 |
+
* @method
|
| 203 |
+
* @return {boolean} whether this value is zero.
|
| 204 |
+
*/
|
| 205 |
+
Long.prototype.isZero = function() {
|
| 206 |
+
return this.high_ == 0 && this.low_ == 0;
|
| 207 |
+
};
|
| 208 |
+
|
| 209 |
+
/**
|
| 210 |
+
* Return whether this value is negative.
|
| 211 |
+
*
|
| 212 |
+
* @method
|
| 213 |
+
* @return {boolean} whether this value is negative.
|
| 214 |
+
*/
|
| 215 |
+
Long.prototype.isNegative = function() {
|
| 216 |
+
return this.high_ < 0;
|
| 217 |
+
};
|
| 218 |
+
|
| 219 |
+
/**
|
| 220 |
+
* Return whether this value is odd.
|
| 221 |
+
*
|
| 222 |
+
* @method
|
| 223 |
+
* @return {boolean} whether this value is odd.
|
| 224 |
+
*/
|
| 225 |
+
Long.prototype.isOdd = function() {
|
| 226 |
+
return (this.low_ & 1) == 1;
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
/**
|
| 230 |
+
* Return whether this Long equals the other
|
| 231 |
+
*
|
| 232 |
+
* @method
|
| 233 |
+
* @param {Long} other Long to compare against.
|
| 234 |
+
* @return {boolean} whether this Long equals the other
|
| 235 |
+
*/
|
| 236 |
+
Long.prototype.equals = function(other) {
|
| 237 |
+
return (this.high_ == other.high_) && (this.low_ == other.low_);
|
| 238 |
+
};
|
| 239 |
+
|
| 240 |
+
/**
|
| 241 |
+
* Return whether this Long does not equal the other.
|
| 242 |
+
*
|
| 243 |
+
* @method
|
| 244 |
+
* @param {Long} other Long to compare against.
|
| 245 |
+
* @return {boolean} whether this Long does not equal the other.
|
| 246 |
+
*/
|
| 247 |
+
Long.prototype.notEquals = function(other) {
|
| 248 |
+
return (this.high_ != other.high_) || (this.low_ != other.low_);
|
| 249 |
+
};
|
| 250 |
+
|
| 251 |
+
/**
|
| 252 |
+
* Return whether this Long is less than the other.
|
| 253 |
+
*
|
| 254 |
+
* @method
|
| 255 |
+
* @param {Long} other Long to compare against.
|
| 256 |
+
* @return {boolean} whether this Long is less than the other.
|
| 257 |
+
*/
|
| 258 |
+
Long.prototype.lessThan = function(other) {
|
| 259 |
+
return this.compare(other) < 0;
|
| 260 |
+
};
|
| 261 |
+
|
| 262 |
+
/**
|
| 263 |
+
* Return whether this Long is less than or equal to the other.
|
| 264 |
+
*
|
| 265 |
+
* @method
|
| 266 |
+
* @param {Long} other Long to compare against.
|
| 267 |
+
* @return {boolean} whether this Long is less than or equal to the other.
|
| 268 |
+
*/
|
| 269 |
+
Long.prototype.lessThanOrEqual = function(other) {
|
| 270 |
+
return this.compare(other) <= 0;
|
| 271 |
+
};
|
| 272 |
+
|
| 273 |
+
/**
|
| 274 |
+
* Return whether this Long is greater than the other.
|
| 275 |
+
*
|
| 276 |
+
* @method
|
| 277 |
+
* @param {Long} other Long to compare against.
|
| 278 |
+
* @return {boolean} whether this Long is greater than the other.
|
| 279 |
+
*/
|
| 280 |
+
Long.prototype.greaterThan = function(other) {
|
| 281 |
+
return this.compare(other) > 0;
|
| 282 |
+
};
|
| 283 |
+
|
| 284 |
+
/**
|
| 285 |
+
* Return whether this Long is greater than or equal to the other.
|
| 286 |
+
*
|
| 287 |
+
* @method
|
| 288 |
+
* @param {Long} other Long to compare against.
|
| 289 |
+
* @return {boolean} whether this Long is greater than or equal to the other.
|
| 290 |
+
*/
|
| 291 |
+
Long.prototype.greaterThanOrEqual = function(other) {
|
| 292 |
+
return this.compare(other) >= 0;
|
| 293 |
+
};
|
| 294 |
+
|
| 295 |
+
/**
|
| 296 |
+
* Compares this Long with the given one.
|
| 297 |
+
*
|
| 298 |
+
* @method
|
| 299 |
+
* @param {Long} other Long to compare against.
|
| 300 |
+
* @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
|
| 301 |
+
*/
|
| 302 |
+
Long.prototype.compare = function(other) {
|
| 303 |
+
if (this.equals(other)) {
|
| 304 |
+
return 0;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
var thisNeg = this.isNegative();
|
| 308 |
+
var otherNeg = other.isNegative();
|
| 309 |
+
if (thisNeg && !otherNeg) {
|
| 310 |
+
return -1;
|
| 311 |
+
}
|
| 312 |
+
if (!thisNeg && otherNeg) {
|
| 313 |
+
return 1;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
// at this point, the signs are the same, so subtraction will not overflow
|
| 317 |
+
if (this.subtract(other).isNegative()) {
|
| 318 |
+
return -1;
|
| 319 |
+
} else {
|
| 320 |
+
return 1;
|
| 321 |
+
}
|
| 322 |
+
};
|
| 323 |
+
|
| 324 |
+
/**
|
| 325 |
+
* The negation of this value.
|
| 326 |
+
*
|
| 327 |
+
* @method
|
| 328 |
+
* @return {Long} the negation of this value.
|
| 329 |
+
*/
|
| 330 |
+
Long.prototype.negate = function() {
|
| 331 |
+
if (this.equals(Long.MIN_VALUE)) {
|
| 332 |
+
return Long.MIN_VALUE;
|
| 333 |
+
} else {
|
| 334 |
+
return this.not().add(Long.ONE);
|
| 335 |
+
}
|
| 336 |
+
};
|
| 337 |
+
|
| 338 |
+
/**
|
| 339 |
+
* Returns the sum of this and the given Long.
|
| 340 |
+
*
|
| 341 |
+
* @method
|
| 342 |
+
* @param {Long} other Long to add to this one.
|
| 343 |
+
* @return {Long} the sum of this and the given Long.
|
| 344 |
+
*/
|
| 345 |
+
Long.prototype.add = function(other) {
|
| 346 |
+
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
|
| 347 |
+
|
| 348 |
+
var a48 = this.high_ >>> 16;
|
| 349 |
+
var a32 = this.high_ & 0xFFFF;
|
| 350 |
+
var a16 = this.low_ >>> 16;
|
| 351 |
+
var a00 = this.low_ & 0xFFFF;
|
| 352 |
+
|
| 353 |
+
var b48 = other.high_ >>> 16;
|
| 354 |
+
var b32 = other.high_ & 0xFFFF;
|
| 355 |
+
var b16 = other.low_ >>> 16;
|
| 356 |
+
var b00 = other.low_ & 0xFFFF;
|
| 357 |
+
|
| 358 |
+
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
|
| 359 |
+
c00 += a00 + b00;
|
| 360 |
+
c16 += c00 >>> 16;
|
| 361 |
+
c00 &= 0xFFFF;
|
| 362 |
+
c16 += a16 + b16;
|
| 363 |
+
c32 += c16 >>> 16;
|
| 364 |
+
c16 &= 0xFFFF;
|
| 365 |
+
c32 += a32 + b32;
|
| 366 |
+
c48 += c32 >>> 16;
|
| 367 |
+
c32 &= 0xFFFF;
|
| 368 |
+
c48 += a48 + b48;
|
| 369 |
+
c48 &= 0xFFFF;
|
| 370 |
+
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
|
| 371 |
+
};
|
| 372 |
+
|
| 373 |
+
/**
|
| 374 |
+
* Returns the difference of this and the given Long.
|
| 375 |
+
*
|
| 376 |
+
* @method
|
| 377 |
+
* @param {Long} other Long to subtract from this.
|
| 378 |
+
* @return {Long} the difference of this and the given Long.
|
| 379 |
+
*/
|
| 380 |
+
Long.prototype.subtract = function(other) {
|
| 381 |
+
return this.add(other.negate());
|
| 382 |
+
};
|
| 383 |
+
|
| 384 |
+
/**
|
| 385 |
+
* Returns the product of this and the given Long.
|
| 386 |
+
*
|
| 387 |
+
* @method
|
| 388 |
+
* @param {Long} other Long to multiply with this.
|
| 389 |
+
* @return {Long} the product of this and the other.
|
| 390 |
+
*/
|
| 391 |
+
Long.prototype.multiply = function(other) {
|
| 392 |
+
if (this.isZero()) {
|
| 393 |
+
return Long.ZERO;
|
| 394 |
+
} else if (other.isZero()) {
|
| 395 |
+
return Long.ZERO;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
if (this.equals(Long.MIN_VALUE)) {
|
| 399 |
+
return other.isOdd() ? Long.MIN_VALUE : Long.ZERO;
|
| 400 |
+
} else if (other.equals(Long.MIN_VALUE)) {
|
| 401 |
+
return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
if (this.isNegative()) {
|
| 405 |
+
if (other.isNegative()) {
|
| 406 |
+
return this.negate().multiply(other.negate());
|
| 407 |
+
} else {
|
| 408 |
+
return this.negate().multiply(other).negate();
|
| 409 |
+
}
|
| 410 |
+
} else if (other.isNegative()) {
|
| 411 |
+
return this.multiply(other.negate()).negate();
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
// If both Longs are small, use float multiplication
|
| 415 |
+
if (this.lessThan(Long.TWO_PWR_24_) &&
|
| 416 |
+
other.lessThan(Long.TWO_PWR_24_)) {
|
| 417 |
+
return Long.fromNumber(this.toNumber() * other.toNumber());
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
// Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products.
|
| 421 |
+
// We can skip products that would overflow.
|
| 422 |
+
|
| 423 |
+
var a48 = this.high_ >>> 16;
|
| 424 |
+
var a32 = this.high_ & 0xFFFF;
|
| 425 |
+
var a16 = this.low_ >>> 16;
|
| 426 |
+
var a00 = this.low_ & 0xFFFF;
|
| 427 |
+
|
| 428 |
+
var b48 = other.high_ >>> 16;
|
| 429 |
+
var b32 = other.high_ & 0xFFFF;
|
| 430 |
+
var b16 = other.low_ >>> 16;
|
| 431 |
+
var b00 = other.low_ & 0xFFFF;
|
| 432 |
+
|
| 433 |
+
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
|
| 434 |
+
c00 += a00 * b00;
|
| 435 |
+
c16 += c00 >>> 16;
|
| 436 |
+
c00 &= 0xFFFF;
|
| 437 |
+
c16 += a16 * b00;
|
| 438 |
+
c32 += c16 >>> 16;
|
| 439 |
+
c16 &= 0xFFFF;
|
| 440 |
+
c16 += a00 * b16;
|
| 441 |
+
c32 += c16 >>> 16;
|
| 442 |
+
c16 &= 0xFFFF;
|
| 443 |
+
c32 += a32 * b00;
|
| 444 |
+
c48 += c32 >>> 16;
|
| 445 |
+
c32 &= 0xFFFF;
|
| 446 |
+
c32 += a16 * b16;
|
| 447 |
+
c48 += c32 >>> 16;
|
| 448 |
+
c32 &= 0xFFFF;
|
| 449 |
+
c32 += a00 * b32;
|
| 450 |
+
c48 += c32 >>> 16;
|
| 451 |
+
c32 &= 0xFFFF;
|
| 452 |
+
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
|
| 453 |
+
c48 &= 0xFFFF;
|
| 454 |
+
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
|
| 455 |
+
};
|
| 456 |
+
|
| 457 |
+
/**
|
| 458 |
+
* Returns this Long divided by the given one.
|
| 459 |
+
*
|
| 460 |
+
* @method
|
| 461 |
+
* @param {Long} other Long by which to divide.
|
| 462 |
+
* @return {Long} this Long divided by the given one.
|
| 463 |
+
*/
|
| 464 |
+
Long.prototype.div = function(other) {
|
| 465 |
+
if (other.isZero()) {
|
| 466 |
+
throw Error('division by zero');
|
| 467 |
+
} else if (this.isZero()) {
|
| 468 |
+
return Long.ZERO;
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
if (this.equals(Long.MIN_VALUE)) {
|
| 472 |
+
if (other.equals(Long.ONE) ||
|
| 473 |
+
other.equals(Long.NEG_ONE)) {
|
| 474 |
+
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
|
| 475 |
+
} else if (other.equals(Long.MIN_VALUE)) {
|
| 476 |
+
return Long.ONE;
|
| 477 |
+
} else {
|
| 478 |
+
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
|
| 479 |
+
var halfThis = this.shiftRight(1);
|
| 480 |
+
var approx = halfThis.div(other).shiftLeft(1);
|
| 481 |
+
if (approx.equals(Long.ZERO)) {
|
| 482 |
+
return other.isNegative() ? Long.ONE : Long.NEG_ONE;
|
| 483 |
+
} else {
|
| 484 |
+
var rem = this.subtract(other.multiply(approx));
|
| 485 |
+
var result = approx.add(rem.div(other));
|
| 486 |
+
return result;
|
| 487 |
+
}
|
| 488 |
+
}
|
| 489 |
+
} else if (other.equals(Long.MIN_VALUE)) {
|
| 490 |
+
return Long.ZERO;
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
if (this.isNegative()) {
|
| 494 |
+
if (other.isNegative()) {
|
| 495 |
+
return this.negate().div(other.negate());
|
| 496 |
+
} else {
|
| 497 |
+
return this.negate().div(other).negate();
|
| 498 |
+
}
|
| 499 |
+
} else if (other.isNegative()) {
|
| 500 |
+
return this.div(other.negate()).negate();
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
// Repeat the following until the remainder is less than other: find a
|
| 504 |
+
// floating-point that approximates remainder / other *from below*, add this
|
| 505 |
+
// into the result, and subtract it from the remainder. It is critical that
|
| 506 |
+
// the approximate value is less than or equal to the real value so that the
|
| 507 |
+
// remainder never becomes negative.
|
| 508 |
+
var res = Long.ZERO;
|
| 509 |
+
var rem = this;
|
| 510 |
+
while (rem.greaterThanOrEqual(other)) {
|
| 511 |
+
// Approximate the result of division. This may be a little greater or
|
| 512 |
+
// smaller than the actual value.
|
| 513 |
+
var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
|
| 514 |
+
|
| 515 |
+
// We will tweak the approximate result by changing it in the 48-th digit or
|
| 516 |
+
// the smallest non-fractional digit, whichever is larger.
|
| 517 |
+
var log2 = Math.ceil(Math.log(approx) / Math.LN2);
|
| 518 |
+
var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);
|
| 519 |
+
|
| 520 |
+
// Decrease the approximation until it is smaller than the remainder. Note
|
| 521 |
+
// that if it is too large, the product overflows and is negative.
|
| 522 |
+
var approxRes = Long.fromNumber(approx);
|
| 523 |
+
var approxRem = approxRes.multiply(other);
|
| 524 |
+
while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
|
| 525 |
+
approx -= delta;
|
| 526 |
+
approxRes = Long.fromNumber(approx);
|
| 527 |
+
approxRem = approxRes.multiply(other);
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
// We know the answer can't be zero... and actually, zero would cause
|
| 531 |
+
// infinite recursion since we would make no progress.
|
| 532 |
+
if (approxRes.isZero()) {
|
| 533 |
+
approxRes = Long.ONE;
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
res = res.add(approxRes);
|
| 537 |
+
rem = rem.subtract(approxRem);
|
| 538 |
+
}
|
| 539 |
+
return res;
|
| 540 |
+
};
|
| 541 |
+
|
| 542 |
+
/**
|
| 543 |
+
* Returns this Long modulo the given one.
|
| 544 |
+
*
|
| 545 |
+
* @method
|
| 546 |
+
* @param {Long} other Long by which to mod.
|
| 547 |
+
* @return {Long} this Long modulo the given one.
|
| 548 |
+
*/
|
| 549 |
+
Long.prototype.modulo = function(other) {
|
| 550 |
+
return this.subtract(this.div(other).multiply(other));
|
| 551 |
+
};
|
| 552 |
+
|
| 553 |
+
/**
|
| 554 |
+
* The bitwise-NOT of this value.
|
| 555 |
+
*
|
| 556 |
+
* @method
|
| 557 |
+
* @return {Long} the bitwise-NOT of this value.
|
| 558 |
+
*/
|
| 559 |
+
Long.prototype.not = function() {
|
| 560 |
+
return Long.fromBits(~this.low_, ~this.high_);
|
| 561 |
+
};
|
| 562 |
+
|
| 563 |
+
/**
|
| 564 |
+
* Returns the bitwise-AND of this Long and the given one.
|
| 565 |
+
*
|
| 566 |
+
* @method
|
| 567 |
+
* @param {Long} other the Long with which to AND.
|
| 568 |
+
* @return {Long} the bitwise-AND of this and the other.
|
| 569 |
+
*/
|
| 570 |
+
Long.prototype.and = function(other) {
|
| 571 |
+
return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_);
|
| 572 |
+
};
|
| 573 |
+
|
| 574 |
+
/**
|
| 575 |
+
* Returns the bitwise-OR of this Long and the given one.
|
| 576 |
+
*
|
| 577 |
+
* @method
|
| 578 |
+
* @param {Long} other the Long with which to OR.
|
| 579 |
+
* @return {Long} the bitwise-OR of this and the other.
|
| 580 |
+
*/
|
| 581 |
+
Long.prototype.or = function(other) {
|
| 582 |
+
return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_);
|
| 583 |
+
};
|
| 584 |
+
|
| 585 |
+
/**
|
| 586 |
+
* Returns the bitwise-XOR of this Long and the given one.
|
| 587 |
+
*
|
| 588 |
+
* @method
|
| 589 |
+
* @param {Long} other the Long with which to XOR.
|
| 590 |
+
* @return {Long} the bitwise-XOR of this and the other.
|
| 591 |
+
*/
|
| 592 |
+
Long.prototype.xor = function(other) {
|
| 593 |
+
return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
|
| 594 |
+
};
|
| 595 |
+
|
| 596 |
+
/**
|
| 597 |
+
* Returns this Long with bits shifted to the left by the given amount.
|
| 598 |
+
*
|
| 599 |
+
* @method
|
| 600 |
+
* @param {number} numBits the number of bits by which to shift.
|
| 601 |
+
* @return {Long} this shifted to the left by the given amount.
|
| 602 |
+
*/
|
| 603 |
+
Long.prototype.shiftLeft = function(numBits) {
|
| 604 |
+
numBits &= 63;
|
| 605 |
+
if (numBits == 0) {
|
| 606 |
+
return this;
|
| 607 |
+
} else {
|
| 608 |
+
var low = this.low_;
|
| 609 |
+
if (numBits < 32) {
|
| 610 |
+
var high = this.high_;
|
| 611 |
+
return Long.fromBits(
|
| 612 |
+
low << numBits,
|
| 613 |
+
(high << numBits) | (low >>> (32 - numBits)));
|
| 614 |
+
} else {
|
| 615 |
+
return Long.fromBits(0, low << (numBits - 32));
|
| 616 |
+
}
|
| 617 |
+
}
|
| 618 |
+
};
|
| 619 |
+
|
| 620 |
+
/**
|
| 621 |
+
* Returns this Long with bits shifted to the right by the given amount.
|
| 622 |
+
*
|
| 623 |
+
* @method
|
| 624 |
+
* @param {number} numBits the number of bits by which to shift.
|
| 625 |
+
* @return {Long} this shifted to the right by the given amount.
|
| 626 |
+
*/
|
| 627 |
+
Long.prototype.shiftRight = function(numBits) {
|
| 628 |
+
numBits &= 63;
|
| 629 |
+
if (numBits == 0) {
|
| 630 |
+
return this;
|
| 631 |
+
} else {
|
| 632 |
+
var high = this.high_;
|
| 633 |
+
if (numBits < 32) {
|
| 634 |
+
var low = this.low_;
|
| 635 |
+
return Long.fromBits(
|
| 636 |
+
(low >>> numBits) | (high << (32 - numBits)),
|
| 637 |
+
high >> numBits);
|
| 638 |
+
} else {
|
| 639 |
+
return Long.fromBits(
|
| 640 |
+
high >> (numBits - 32),
|
| 641 |
+
high >= 0 ? 0 : -1);
|
| 642 |
+
}
|
| 643 |
+
}
|
| 644 |
+
};
|
| 645 |
+
|
| 646 |
+
/**
|
| 647 |
+
* Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
|
| 648 |
+
*
|
| 649 |
+
* @method
|
| 650 |
+
* @param {number} numBits the number of bits by which to shift.
|
| 651 |
+
* @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits.
|
| 652 |
+
*/
|
| 653 |
+
Long.prototype.shiftRightUnsigned = function(numBits) {
|
| 654 |
+
numBits &= 63;
|
| 655 |
+
if (numBits == 0) {
|
| 656 |
+
return this;
|
| 657 |
+
} else {
|
| 658 |
+
var high = this.high_;
|
| 659 |
+
if (numBits < 32) {
|
| 660 |
+
var low = this.low_;
|
| 661 |
+
return Long.fromBits(
|
| 662 |
+
(low >>> numBits) | (high << (32 - numBits)),
|
| 663 |
+
high >>> numBits);
|
| 664 |
+
} else if (numBits == 32) {
|
| 665 |
+
return Long.fromBits(high, 0);
|
| 666 |
+
} else {
|
| 667 |
+
return Long.fromBits(high >>> (numBits - 32), 0);
|
| 668 |
+
}
|
| 669 |
+
}
|
| 670 |
+
};
|
| 671 |
+
|
| 672 |
+
/**
|
| 673 |
+
* Returns a Long representing the given (32-bit) integer value.
|
| 674 |
+
*
|
| 675 |
+
* @method
|
| 676 |
+
* @param {number} value the 32-bit integer in question.
|
| 677 |
+
* @return {Long} the corresponding Long value.
|
| 678 |
+
*/
|
| 679 |
+
Long.fromInt = function(value) {
|
| 680 |
+
if (-128 <= value && value < 128) {
|
| 681 |
+
var cachedObj = Long.INT_CACHE_[value];
|
| 682 |
+
if (cachedObj) {
|
| 683 |
+
return cachedObj;
|
| 684 |
+
}
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
var obj = new Long(value | 0, value < 0 ? -1 : 0);
|
| 688 |
+
if (-128 <= value && value < 128) {
|
| 689 |
+
Long.INT_CACHE_[value] = obj;
|
| 690 |
+
}
|
| 691 |
+
return obj;
|
| 692 |
+
};
|
| 693 |
+
|
| 694 |
+
/**
|
| 695 |
+
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
|
| 696 |
+
*
|
| 697 |
+
* @method
|
| 698 |
+
* @param {number} value the number in question.
|
| 699 |
+
* @return {Long} the corresponding Long value.
|
| 700 |
+
*/
|
| 701 |
+
Long.fromNumber = function(value) {
|
| 702 |
+
if (isNaN(value) || !isFinite(value)) {
|
| 703 |
+
return Long.ZERO;
|
| 704 |
+
} else if (value <= -Long.TWO_PWR_63_DBL_) {
|
| 705 |
+
return Long.MIN_VALUE;
|
| 706 |
+
} else if (value + 1 >= Long.TWO_PWR_63_DBL_) {
|
| 707 |
+
return Long.MAX_VALUE;
|
| 708 |
+
} else if (value < 0) {
|
| 709 |
+
return Long.fromNumber(-value).negate();
|
| 710 |
+
} else {
|
| 711 |
+
return new Long(
|
| 712 |
+
(value % Long.TWO_PWR_32_DBL_) | 0,
|
| 713 |
+
(value / Long.TWO_PWR_32_DBL_) | 0);
|
| 714 |
+
}
|
| 715 |
+
};
|
| 716 |
+
|
| 717 |
+
/**
|
| 718 |
+
* 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.
|
| 719 |
+
*
|
| 720 |
+
* @method
|
| 721 |
+
* @param {number} lowBits the low 32-bits.
|
| 722 |
+
* @param {number} highBits the high 32-bits.
|
| 723 |
+
* @return {Long} the corresponding Long value.
|
| 724 |
+
*/
|
| 725 |
+
Long.fromBits = function(lowBits, highBits) {
|
| 726 |
+
return new Long(lowBits, highBits);
|
| 727 |
+
};
|
| 728 |
+
|
| 729 |
+
/**
|
| 730 |
+
* Returns a Long representation of the given string, written using the given radix.
|
| 731 |
+
*
|
| 732 |
+
* @method
|
| 733 |
+
* @param {string} str the textual representation of the Long.
|
| 734 |
+
* @param {number} opt_radix the radix in which the text is written.
|
| 735 |
+
* @return {Long} the corresponding Long value.
|
| 736 |
+
*/
|
| 737 |
+
Long.fromString = function(str, opt_radix) {
|
| 738 |
+
if (str.length == 0) {
|
| 739 |
+
throw Error('number format error: empty string');
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
var radix = opt_radix || 10;
|
| 743 |
+
if (radix < 2 || 36 < radix) {
|
| 744 |
+
throw Error('radix out of range: ' + radix);
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
if (str.charAt(0) == '-') {
|
| 748 |
+
return Long.fromString(str.substring(1), radix).negate();
|
| 749 |
+
} else if (str.indexOf('-') >= 0) {
|
| 750 |
+
throw Error('number format error: interior "-" character: ' + str);
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
// Do several (8) digits each time through the loop, so as to
|
| 754 |
+
// minimize the calls to the very expensive emulated div.
|
| 755 |
+
var radixToPower = Long.fromNumber(Math.pow(radix, 8));
|
| 756 |
+
|
| 757 |
+
var result = Long.ZERO;
|
| 758 |
+
for (var i = 0; i < str.length; i += 8) {
|
| 759 |
+
var size = Math.min(8, str.length - i);
|
| 760 |
+
var value = parseInt(str.substring(i, i + size), radix);
|
| 761 |
+
if (size < 8) {
|
| 762 |
+
var power = Long.fromNumber(Math.pow(radix, size));
|
| 763 |
+
result = result.multiply(power).add(Long.fromNumber(value));
|
| 764 |
+
} else {
|
| 765 |
+
result = result.multiply(radixToPower);
|
| 766 |
+
result = result.add(Long.fromNumber(value));
|
| 767 |
+
}
|
| 768 |
+
}
|
| 769 |
+
return result;
|
| 770 |
+
};
|
| 771 |
+
|
| 772 |
+
// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
|
| 773 |
+
// from* methods on which they depend.
|
| 774 |
+
|
| 775 |
+
|
| 776 |
+
/**
|
| 777 |
+
* A cache of the Long representations of small integer values.
|
| 778 |
+
* @type {Object}
|
| 779 |
+
* @ignore
|
| 780 |
+
*/
|
| 781 |
+
Long.INT_CACHE_ = {};
|
| 782 |
+
|
| 783 |
+
// NOTE: the compiler should inline these constant values below and then remove
|
| 784 |
+
// these variables, so there should be no runtime penalty for these.
|
| 785 |
+
|
| 786 |
+
/**
|
| 787 |
+
* Number used repeated below in calculations. This must appear before the
|
| 788 |
+
* first call to any from* function below.
|
| 789 |
+
* @type {number}
|
| 790 |
+
* @ignore
|
| 791 |
+
*/
|
| 792 |
+
Long.TWO_PWR_16_DBL_ = 1 << 16;
|
| 793 |
+
|
| 794 |
+
/**
|
| 795 |
+
* @type {number}
|
| 796 |
+
* @ignore
|
| 797 |
+
*/
|
| 798 |
+
Long.TWO_PWR_24_DBL_ = 1 << 24;
|
| 799 |
+
|
| 800 |
+
/**
|
| 801 |
+
* @type {number}
|
| 802 |
+
* @ignore
|
| 803 |
+
*/
|
| 804 |
+
Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_;
|
| 805 |
+
|
| 806 |
+
/**
|
| 807 |
+
* @type {number}
|
| 808 |
+
* @ignore
|
| 809 |
+
*/
|
| 810 |
+
Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2;
|
| 811 |
+
|
| 812 |
+
/**
|
| 813 |
+
* @type {number}
|
| 814 |
+
* @ignore
|
| 815 |
+
*/
|
| 816 |
+
Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_;
|
| 817 |
+
|
| 818 |
+
/**
|
| 819 |
+
* @type {number}
|
| 820 |
+
* @ignore
|
| 821 |
+
*/
|
| 822 |
+
Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_;
|
| 823 |
+
|
| 824 |
+
/**
|
| 825 |
+
* @type {number}
|
| 826 |
+
* @ignore
|
| 827 |
+
*/
|
| 828 |
+
Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2;
|
| 829 |
+
|
| 830 |
+
/** @type {Long} */
|
| 831 |
+
Long.ZERO = Long.fromInt(0);
|
| 832 |
+
|
| 833 |
+
/** @type {Long} */
|
| 834 |
+
Long.ONE = Long.fromInt(1);
|
| 835 |
+
|
| 836 |
+
/** @type {Long} */
|
| 837 |
+
Long.NEG_ONE = Long.fromInt(-1);
|
| 838 |
+
|
| 839 |
+
/** @type {Long} */
|
| 840 |
+
Long.MAX_VALUE =
|
| 841 |
+
Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);
|
| 842 |
+
|
| 843 |
+
/** @type {Long} */
|
| 844 |
+
Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0);
|
| 845 |
+
|
| 846 |
+
/**
|
| 847 |
+
* @type {Long}
|
| 848 |
+
* @ignore
|
| 849 |
+
*/
|
| 850 |
+
Long.TWO_PWR_24_ = Long.fromInt(1 << 24);
|
| 851 |
+
|
| 852 |
+
/**
|
| 853 |
+
* Expose.
|
| 854 |
+
*/
|
| 855 |
+
module.exports = Long;
|
| 856 |
+
module.exports.Long = Long;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/max_key.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* A class representation of the BSON MaxKey type.
|
| 3 |
+
*
|
| 4 |
+
* @class
|
| 5 |
+
* @return {MaxKey} A MaxKey instance
|
| 6 |
+
*/
|
| 7 |
+
function MaxKey() {
|
| 8 |
+
if(!(this instanceof MaxKey)) return new MaxKey();
|
| 9 |
+
|
| 10 |
+
this._bsontype = 'MaxKey';
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
module.exports = MaxKey;
|
| 14 |
+
module.exports.MaxKey = MaxKey;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/min_key.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* A class representation of the BSON MinKey type.
|
| 3 |
+
*
|
| 4 |
+
* @class
|
| 5 |
+
* @return {MinKey} A MinKey instance
|
| 6 |
+
*/
|
| 7 |
+
function MinKey() {
|
| 8 |
+
if(!(this instanceof MinKey)) return new MinKey();
|
| 9 |
+
|
| 10 |
+
this._bsontype = 'MinKey';
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
module.exports = MinKey;
|
| 14 |
+
module.exports.MinKey = MinKey;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/objectid.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 10 |
+
* Create a random 3-byte value (i.e. unique for this
|
| 11 |
+
* process). Other drivers use a md5 of the machine id here, but
|
| 12 |
+
* that would mean an asyc call to gethostname, so we don't bother.
|
| 13 |
+
* @ignore
|
| 14 |
+
*/
|
| 15 |
+
var MACHINE_ID = parseInt(Math.random() * 0xFFFFFF, 10);
|
| 16 |
+
|
| 17 |
+
// Regular expression that checks for hex value
|
| 18 |
+
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
|
| 19 |
+
|
| 20 |
+
/**
|
| 21 |
+
* Create a new ObjectID instance
|
| 22 |
+
*
|
| 23 |
+
* @class
|
| 24 |
+
* @param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number.
|
| 25 |
+
* @property {number} generationTime The generation time of this ObjectId instance
|
| 26 |
+
* @return {ObjectID} instance of ObjectID.
|
| 27 |
+
*/
|
| 28 |
+
var ObjectID = function ObjectID(id) {
|
| 29 |
+
if(!(this instanceof ObjectID)) return new ObjectID(id);
|
| 30 |
+
if((id instanceof ObjectID)) return id;
|
| 31 |
+
|
| 32 |
+
this._bsontype = 'ObjectID';
|
| 33 |
+
var __id = null;
|
| 34 |
+
var valid = ObjectID.isValid(id);
|
| 35 |
+
|
| 36 |
+
// Throw an error if it's not a valid setup
|
| 37 |
+
if(!valid && id != null){
|
| 38 |
+
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
|
| 39 |
+
} else if(valid && typeof id == 'string' && id.length == 24) {
|
| 40 |
+
return ObjectID.createFromHexString(id);
|
| 41 |
+
} else if(id == null || typeof id == 'number') {
|
| 42 |
+
// convert to 12 byte binary string
|
| 43 |
+
this.id = this.generate(id);
|
| 44 |
+
} else if(id != null && id.length === 12) {
|
| 45 |
+
// assume 12 byte string
|
| 46 |
+
this.id = id;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
if(ObjectID.cacheHexString) this.__id = this.toHexString();
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
// Allow usage of ObjectId as well as ObjectID
|
| 53 |
+
var ObjectId = ObjectID;
|
| 54 |
+
|
| 55 |
+
// Precomputed hex table enables speedy hex string conversion
|
| 56 |
+
var hexTable = [];
|
| 57 |
+
for (var i = 0; i < 256; i++) {
|
| 58 |
+
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
/**
|
| 62 |
+
* Return the ObjectID id as a 24 byte hex string representation
|
| 63 |
+
*
|
| 64 |
+
* @method
|
| 65 |
+
* @return {string} return the 24 byte hex string representation.
|
| 66 |
+
*/
|
| 67 |
+
ObjectID.prototype.toHexString = function() {
|
| 68 |
+
if(ObjectID.cacheHexString && this.__id) return this.__id;
|
| 69 |
+
|
| 70 |
+
var hexString = '';
|
| 71 |
+
|
| 72 |
+
for (var i = 0; i < this.id.length; i++) {
|
| 73 |
+
hexString += hexTable[this.id.charCodeAt(i)];
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
if(ObjectID.cacheHexString) this.__id = hexString;
|
| 77 |
+
return hexString;
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
/**
|
| 81 |
+
* Update the ObjectID index used in generating new ObjectID's on the driver
|
| 82 |
+
*
|
| 83 |
+
* @method
|
| 84 |
+
* @return {number} returns next index value.
|
| 85 |
+
* @ignore
|
| 86 |
+
*/
|
| 87 |
+
ObjectID.prototype.get_inc = function() {
|
| 88 |
+
return ObjectID.index = (ObjectID.index + 1) % 0xFFFFFF;
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
/**
|
| 92 |
+
* Update the ObjectID index used in generating new ObjectID's on the driver
|
| 93 |
+
*
|
| 94 |
+
* @method
|
| 95 |
+
* @return {number} returns next index value.
|
| 96 |
+
* @ignore
|
| 97 |
+
*/
|
| 98 |
+
ObjectID.prototype.getInc = function() {
|
| 99 |
+
return this.get_inc();
|
| 100 |
+
};
|
| 101 |
+
|
| 102 |
+
/**
|
| 103 |
+
* Generate a 12 byte id string used in ObjectID's
|
| 104 |
+
*
|
| 105 |
+
* @method
|
| 106 |
+
* @param {number} [time] optional parameter allowing to pass in a second based timestamp.
|
| 107 |
+
* @return {string} return the 12 byte id binary string.
|
| 108 |
+
*/
|
| 109 |
+
ObjectID.prototype.generate = function(time) {
|
| 110 |
+
if ('number' != typeof time) {
|
| 111 |
+
time = parseInt(Date.now()/1000,10);
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
var time4Bytes = BinaryParser.encodeInt(time, 32, true, true);
|
| 115 |
+
/* for time-based ObjectID the bytes following the time will be zeroed */
|
| 116 |
+
var machine3Bytes = BinaryParser.encodeInt(MACHINE_ID, 24, false);
|
| 117 |
+
var pid2Bytes = BinaryParser.fromShort(typeof process === 'undefined' ? Math.floor(Math.random() * 100000) : process.pid % 0xFFFF);
|
| 118 |
+
var index3Bytes = BinaryParser.encodeInt(this.get_inc(), 24, false, true);
|
| 119 |
+
|
| 120 |
+
return time4Bytes + machine3Bytes + pid2Bytes + index3Bytes;
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
+
/**
|
| 124 |
+
* Converts the id into a 24 byte hex string for printing
|
| 125 |
+
*
|
| 126 |
+
* @return {String} return the 24 byte hex string representation.
|
| 127 |
+
* @ignore
|
| 128 |
+
*/
|
| 129 |
+
ObjectID.prototype.toString = function() {
|
| 130 |
+
return this.toHexString();
|
| 131 |
+
};
|
| 132 |
+
|
| 133 |
+
/**
|
| 134 |
+
* Converts to a string representation of this Id.
|
| 135 |
+
*
|
| 136 |
+
* @return {String} return the 24 byte hex string representation.
|
| 137 |
+
* @ignore
|
| 138 |
+
*/
|
| 139 |
+
ObjectID.prototype.inspect = ObjectID.prototype.toString;
|
| 140 |
+
|
| 141 |
+
/**
|
| 142 |
+
* Converts to its JSON representation.
|
| 143 |
+
*
|
| 144 |
+
* @return {String} return the 24 byte hex string representation.
|
| 145 |
+
* @ignore
|
| 146 |
+
*/
|
| 147 |
+
ObjectID.prototype.toJSON = function() {
|
| 148 |
+
return this.toHexString();
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
/**
|
| 152 |
+
* Compares the equality of this ObjectID with `otherID`.
|
| 153 |
+
*
|
| 154 |
+
* @method
|
| 155 |
+
* @param {object} otherID ObjectID instance to compare against.
|
| 156 |
+
* @return {boolean} the result of comparing two ObjectID's
|
| 157 |
+
*/
|
| 158 |
+
ObjectID.prototype.equals = function equals (otherID) {
|
| 159 |
+
if(otherID == null) return false;
|
| 160 |
+
var id = (otherID instanceof ObjectID || otherID.toHexString)
|
| 161 |
+
? otherID.id
|
| 162 |
+
: ObjectID.createFromHexString(otherID).id;
|
| 163 |
+
|
| 164 |
+
return this.id === id;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
/**
|
| 168 |
+
* Returns the generation date (accurate up to the second) that this ID was generated.
|
| 169 |
+
*
|
| 170 |
+
* @method
|
| 171 |
+
* @return {date} the generation date
|
| 172 |
+
*/
|
| 173 |
+
ObjectID.prototype.getTimestamp = function() {
|
| 174 |
+
var timestamp = new Date();
|
| 175 |
+
timestamp.setTime(Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true)) * 1000);
|
| 176 |
+
return timestamp;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
/**
|
| 180 |
+
* @ignore
|
| 181 |
+
*/
|
| 182 |
+
ObjectID.index = parseInt(Math.random() * 0xFFFFFF, 10);
|
| 183 |
+
|
| 184 |
+
/**
|
| 185 |
+
* @ignore
|
| 186 |
+
*/
|
| 187 |
+
ObjectID.createPk = function createPk () {
|
| 188 |
+
return new ObjectID();
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
/**
|
| 192 |
+
* Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
|
| 193 |
+
*
|
| 194 |
+
* @method
|
| 195 |
+
* @param {number} time an integer number representing a number of seconds.
|
| 196 |
+
* @return {ObjectID} return the created ObjectID
|
| 197 |
+
*/
|
| 198 |
+
ObjectID.createFromTime = function createFromTime (time) {
|
| 199 |
+
var id = BinaryParser.encodeInt(time, 32, true, true) +
|
| 200 |
+
BinaryParser.encodeInt(0, 64, true, true);
|
| 201 |
+
return new ObjectID(id);
|
| 202 |
+
};
|
| 203 |
+
|
| 204 |
+
/**
|
| 205 |
+
* Creates an ObjectID from a hex string representation of an ObjectID.
|
| 206 |
+
*
|
| 207 |
+
* @method
|
| 208 |
+
* @param {string} hexString create a ObjectID from a passed in 24 byte hexstring.
|
| 209 |
+
* @return {ObjectID} return the created ObjectID
|
| 210 |
+
*/
|
| 211 |
+
ObjectID.createFromHexString = function createFromHexString (hexString) {
|
| 212 |
+
// Throw an error if it's not a valid setup
|
| 213 |
+
if(typeof hexString === 'undefined' || hexString != null && hexString.length != 24)
|
| 214 |
+
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
|
| 215 |
+
|
| 216 |
+
var len = hexString.length;
|
| 217 |
+
|
| 218 |
+
if(len > 12*2) {
|
| 219 |
+
throw new Error('Id cannot be longer than 12 bytes');
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
var result = ''
|
| 223 |
+
, string
|
| 224 |
+
, number;
|
| 225 |
+
|
| 226 |
+
for (var index = 0; index < len; index += 2) {
|
| 227 |
+
string = hexString.substr(index, 2);
|
| 228 |
+
number = parseInt(string, 16);
|
| 229 |
+
result += BinaryParser.fromByte(number);
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
return new ObjectID(result, hexString);
|
| 233 |
+
};
|
| 234 |
+
|
| 235 |
+
/**
|
| 236 |
+
* Checks if a value is a valid bson ObjectId
|
| 237 |
+
*
|
| 238 |
+
* @method
|
| 239 |
+
* @return {boolean} return true if the value is a valid bson ObjectId, return false otherwise.
|
| 240 |
+
*/
|
| 241 |
+
ObjectID.isValid = function isValid(id) {
|
| 242 |
+
if(id == null) return false;
|
| 243 |
+
|
| 244 |
+
if(id != null && 'number' != typeof id && (id.length != 12 && id.length != 24)) {
|
| 245 |
+
return false;
|
| 246 |
+
} else {
|
| 247 |
+
// Check specifically for hex correctness
|
| 248 |
+
if(typeof id == 'string' && id.length == 24) return checkForHexRegExp.test(id);
|
| 249 |
+
return true;
|
| 250 |
+
}
|
| 251 |
+
};
|
| 252 |
+
|
| 253 |
+
/**
|
| 254 |
+
* @ignore
|
| 255 |
+
*/
|
| 256 |
+
Object.defineProperty(ObjectID.prototype, "generationTime", {
|
| 257 |
+
enumerable: true
|
| 258 |
+
, get: function () {
|
| 259 |
+
return Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true));
|
| 260 |
+
}
|
| 261 |
+
, set: function (value) {
|
| 262 |
+
var value = BinaryParser.encodeInt(value, 32, true, true);
|
| 263 |
+
this.id = value + this.id.substr(4);
|
| 264 |
+
// delete this.__id;
|
| 265 |
+
this.toHexString();
|
| 266 |
+
}
|
| 267 |
+
});
|
| 268 |
+
|
| 269 |
+
/**
|
| 270 |
+
* Expose.
|
| 271 |
+
*/
|
| 272 |
+
module.exports = ObjectID;
|
| 273 |
+
module.exports.ObjectID = ObjectID;
|
| 274 |
+
module.exports.ObjectId = ObjectID;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/symbol.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* A class representation of the BSON Symbol type.
|
| 3 |
+
*
|
| 4 |
+
* @class
|
| 5 |
+
* @deprecated
|
| 6 |
+
* @param {string} value the string representing the symbol.
|
| 7 |
+
* @return {Symbol}
|
| 8 |
+
*/
|
| 9 |
+
function Symbol(value) {
|
| 10 |
+
if(!(this instanceof Symbol)) return new Symbol(value);
|
| 11 |
+
this._bsontype = 'Symbol';
|
| 12 |
+
this.value = value;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* Access the wrapped string value.
|
| 17 |
+
*
|
| 18 |
+
* @method
|
| 19 |
+
* @return {String} returns the wrapped string.
|
| 20 |
+
*/
|
| 21 |
+
Symbol.prototype.valueOf = function() {
|
| 22 |
+
return this.value;
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
/**
|
| 26 |
+
* @ignore
|
| 27 |
+
*/
|
| 28 |
+
Symbol.prototype.toString = function() {
|
| 29 |
+
return this.value;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
/**
|
| 33 |
+
* @ignore
|
| 34 |
+
*/
|
| 35 |
+
Symbol.prototype.inspect = function() {
|
| 36 |
+
return this.value;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* @ignore
|
| 41 |
+
*/
|
| 42 |
+
Symbol.prototype.toJSON = function() {
|
| 43 |
+
return this.value;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
module.exports = Symbol;
|
| 47 |
+
module.exports.Symbol = Symbol;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/lib/bson/timestamp.js
ADDED
|
@@ -0,0 +1,856 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 2 |
+
// you may not use this file except in compliance with the License.
|
| 3 |
+
// You may obtain a copy of the License at
|
| 4 |
+
//
|
| 5 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
| 6 |
+
//
|
| 7 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 8 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 9 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 10 |
+
// See the License for the specific language governing permissions and
|
| 11 |
+
// limitations under the License.
|
| 12 |
+
//
|
| 13 |
+
// Copyright 2009 Google Inc. All Rights Reserved
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* This type is for INTERNAL use in MongoDB only and should not be used in applications.
|
| 17 |
+
* The appropriate corresponding type is the JavaScript Date type.
|
| 18 |
+
*
|
| 19 |
+
* Defines a Timestamp class for representing a 64-bit two's-complement
|
| 20 |
+
* integer value, which faithfully simulates the behavior of a Java "Timestamp". This
|
| 21 |
+
* implementation is derived from TimestampLib in GWT.
|
| 22 |
+
*
|
| 23 |
+
* Constructs a 64-bit two's-complement integer, given its low and high 32-bit
|
| 24 |
+
* values as *signed* integers. See the from* functions below for more
|
| 25 |
+
* convenient ways of constructing Timestamps.
|
| 26 |
+
*
|
| 27 |
+
* The internal representation of a Timestamp is the two given signed, 32-bit values.
|
| 28 |
+
* We use 32-bit pieces because these are the size of integers on which
|
| 29 |
+
* Javascript performs bit-operations. For operations like addition and
|
| 30 |
+
* multiplication, we split each number into 16-bit pieces, which can easily be
|
| 31 |
+
* multiplied within Javascript's floating-point representation without overflow
|
| 32 |
+
* or change in sign.
|
| 33 |
+
*
|
| 34 |
+
* In the algorithms below, we frequently reduce the negative case to the
|
| 35 |
+
* positive case by negating the input(s) and then post-processing the result.
|
| 36 |
+
* Note that we must ALWAYS check specially whether those values are MIN_VALUE
|
| 37 |
+
* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
|
| 38 |
+
* a positive number, it overflows back into a negative). Not handling this
|
| 39 |
+
* case would often result in infinite recursion.
|
| 40 |
+
*
|
| 41 |
+
* @class
|
| 42 |
+
* @param {number} low the low (signed) 32 bits of the Timestamp.
|
| 43 |
+
* @param {number} high the high (signed) 32 bits of the Timestamp.
|
| 44 |
+
*/
|
| 45 |
+
function Timestamp(low, high) {
|
| 46 |
+
if(!(this instanceof Timestamp)) return new Timestamp(low, high);
|
| 47 |
+
this._bsontype = 'Timestamp';
|
| 48 |
+
/**
|
| 49 |
+
* @type {number}
|
| 50 |
+
* @ignore
|
| 51 |
+
*/
|
| 52 |
+
this.low_ = low | 0; // force into 32 signed bits.
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* @type {number}
|
| 56 |
+
* @ignore
|
| 57 |
+
*/
|
| 58 |
+
this.high_ = high | 0; // force into 32 signed bits.
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
/**
|
| 62 |
+
* Return the int value.
|
| 63 |
+
*
|
| 64 |
+
* @return {number} the value, assuming it is a 32-bit integer.
|
| 65 |
+
*/
|
| 66 |
+
Timestamp.prototype.toInt = function() {
|
| 67 |
+
return this.low_;
|
| 68 |
+
};
|
| 69 |
+
|
| 70 |
+
/**
|
| 71 |
+
* Return the Number value.
|
| 72 |
+
*
|
| 73 |
+
* @method
|
| 74 |
+
* @return {number} the closest floating-point representation to this value.
|
| 75 |
+
*/
|
| 76 |
+
Timestamp.prototype.toNumber = function() {
|
| 77 |
+
return this.high_ * Timestamp.TWO_PWR_32_DBL_ +
|
| 78 |
+
this.getLowBitsUnsigned();
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
/**
|
| 82 |
+
* Return the JSON value.
|
| 83 |
+
*
|
| 84 |
+
* @method
|
| 85 |
+
* @return {string} the JSON representation.
|
| 86 |
+
*/
|
| 87 |
+
Timestamp.prototype.toJSON = function() {
|
| 88 |
+
return this.toString();
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/**
|
| 92 |
+
* Return the String value.
|
| 93 |
+
*
|
| 94 |
+
* @method
|
| 95 |
+
* @param {number} [opt_radix] the radix in which the text should be written.
|
| 96 |
+
* @return {string} the textual representation of this value.
|
| 97 |
+
*/
|
| 98 |
+
Timestamp.prototype.toString = function(opt_radix) {
|
| 99 |
+
var radix = opt_radix || 10;
|
| 100 |
+
if (radix < 2 || 36 < radix) {
|
| 101 |
+
throw Error('radix out of range: ' + radix);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
if (this.isZero()) {
|
| 105 |
+
return '0';
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
if (this.isNegative()) {
|
| 109 |
+
if (this.equals(Timestamp.MIN_VALUE)) {
|
| 110 |
+
// We need to change the Timestamp value before it can be negated, so we remove
|
| 111 |
+
// the bottom-most digit in this base and then recurse to do the rest.
|
| 112 |
+
var radixTimestamp = Timestamp.fromNumber(radix);
|
| 113 |
+
var div = this.div(radixTimestamp);
|
| 114 |
+
var rem = div.multiply(radixTimestamp).subtract(this);
|
| 115 |
+
return div.toString(radix) + rem.toInt().toString(radix);
|
| 116 |
+
} else {
|
| 117 |
+
return '-' + this.negate().toString(radix);
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// Do several (6) digits each time through the loop, so as to
|
| 122 |
+
// minimize the calls to the very expensive emulated div.
|
| 123 |
+
var radixToPower = Timestamp.fromNumber(Math.pow(radix, 6));
|
| 124 |
+
|
| 125 |
+
var rem = this;
|
| 126 |
+
var result = '';
|
| 127 |
+
while (true) {
|
| 128 |
+
var remDiv = rem.div(radixToPower);
|
| 129 |
+
var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
|
| 130 |
+
var digits = intval.toString(radix);
|
| 131 |
+
|
| 132 |
+
rem = remDiv;
|
| 133 |
+
if (rem.isZero()) {
|
| 134 |
+
return digits + result;
|
| 135 |
+
} else {
|
| 136 |
+
while (digits.length < 6) {
|
| 137 |
+
digits = '0' + digits;
|
| 138 |
+
}
|
| 139 |
+
result = '' + digits + result;
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
/**
|
| 145 |
+
* Return the high 32-bits value.
|
| 146 |
+
*
|
| 147 |
+
* @method
|
| 148 |
+
* @return {number} the high 32-bits as a signed value.
|
| 149 |
+
*/
|
| 150 |
+
Timestamp.prototype.getHighBits = function() {
|
| 151 |
+
return this.high_;
|
| 152 |
+
};
|
| 153 |
+
|
| 154 |
+
/**
|
| 155 |
+
* Return the low 32-bits value.
|
| 156 |
+
*
|
| 157 |
+
* @method
|
| 158 |
+
* @return {number} the low 32-bits as a signed value.
|
| 159 |
+
*/
|
| 160 |
+
Timestamp.prototype.getLowBits = function() {
|
| 161 |
+
return this.low_;
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
/**
|
| 165 |
+
* Return the low unsigned 32-bits value.
|
| 166 |
+
*
|
| 167 |
+
* @method
|
| 168 |
+
* @return {number} the low 32-bits as an unsigned value.
|
| 169 |
+
*/
|
| 170 |
+
Timestamp.prototype.getLowBitsUnsigned = function() {
|
| 171 |
+
return (this.low_ >= 0) ?
|
| 172 |
+
this.low_ : Timestamp.TWO_PWR_32_DBL_ + this.low_;
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
+
/**
|
| 176 |
+
* Returns the number of bits needed to represent the absolute value of this Timestamp.
|
| 177 |
+
*
|
| 178 |
+
* @method
|
| 179 |
+
* @return {number} Returns the number of bits needed to represent the absolute value of this Timestamp.
|
| 180 |
+
*/
|
| 181 |
+
Timestamp.prototype.getNumBitsAbs = function() {
|
| 182 |
+
if (this.isNegative()) {
|
| 183 |
+
if (this.equals(Timestamp.MIN_VALUE)) {
|
| 184 |
+
return 64;
|
| 185 |
+
} else {
|
| 186 |
+
return this.negate().getNumBitsAbs();
|
| 187 |
+
}
|
| 188 |
+
} else {
|
| 189 |
+
var val = this.high_ != 0 ? this.high_ : this.low_;
|
| 190 |
+
for (var bit = 31; bit > 0; bit--) {
|
| 191 |
+
if ((val & (1 << bit)) != 0) {
|
| 192 |
+
break;
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
return this.high_ != 0 ? bit + 33 : bit + 1;
|
| 196 |
+
}
|
| 197 |
+
};
|
| 198 |
+
|
| 199 |
+
/**
|
| 200 |
+
* Return whether this value is zero.
|
| 201 |
+
*
|
| 202 |
+
* @method
|
| 203 |
+
* @return {boolean} whether this value is zero.
|
| 204 |
+
*/
|
| 205 |
+
Timestamp.prototype.isZero = function() {
|
| 206 |
+
return this.high_ == 0 && this.low_ == 0;
|
| 207 |
+
};
|
| 208 |
+
|
| 209 |
+
/**
|
| 210 |
+
* Return whether this value is negative.
|
| 211 |
+
*
|
| 212 |
+
* @method
|
| 213 |
+
* @return {boolean} whether this value is negative.
|
| 214 |
+
*/
|
| 215 |
+
Timestamp.prototype.isNegative = function() {
|
| 216 |
+
return this.high_ < 0;
|
| 217 |
+
};
|
| 218 |
+
|
| 219 |
+
/**
|
| 220 |
+
* Return whether this value is odd.
|
| 221 |
+
*
|
| 222 |
+
* @method
|
| 223 |
+
* @return {boolean} whether this value is odd.
|
| 224 |
+
*/
|
| 225 |
+
Timestamp.prototype.isOdd = function() {
|
| 226 |
+
return (this.low_ & 1) == 1;
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
/**
|
| 230 |
+
* Return whether this Timestamp equals the other
|
| 231 |
+
*
|
| 232 |
+
* @method
|
| 233 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 234 |
+
* @return {boolean} whether this Timestamp equals the other
|
| 235 |
+
*/
|
| 236 |
+
Timestamp.prototype.equals = function(other) {
|
| 237 |
+
return (this.high_ == other.high_) && (this.low_ == other.low_);
|
| 238 |
+
};
|
| 239 |
+
|
| 240 |
+
/**
|
| 241 |
+
* Return whether this Timestamp does not equal the other.
|
| 242 |
+
*
|
| 243 |
+
* @method
|
| 244 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 245 |
+
* @return {boolean} whether this Timestamp does not equal the other.
|
| 246 |
+
*/
|
| 247 |
+
Timestamp.prototype.notEquals = function(other) {
|
| 248 |
+
return (this.high_ != other.high_) || (this.low_ != other.low_);
|
| 249 |
+
};
|
| 250 |
+
|
| 251 |
+
/**
|
| 252 |
+
* Return whether this Timestamp is less than the other.
|
| 253 |
+
*
|
| 254 |
+
* @method
|
| 255 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 256 |
+
* @return {boolean} whether this Timestamp is less than the other.
|
| 257 |
+
*/
|
| 258 |
+
Timestamp.prototype.lessThan = function(other) {
|
| 259 |
+
return this.compare(other) < 0;
|
| 260 |
+
};
|
| 261 |
+
|
| 262 |
+
/**
|
| 263 |
+
* Return whether this Timestamp is less than or equal to the other.
|
| 264 |
+
*
|
| 265 |
+
* @method
|
| 266 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 267 |
+
* @return {boolean} whether this Timestamp is less than or equal to the other.
|
| 268 |
+
*/
|
| 269 |
+
Timestamp.prototype.lessThanOrEqual = function(other) {
|
| 270 |
+
return this.compare(other) <= 0;
|
| 271 |
+
};
|
| 272 |
+
|
| 273 |
+
/**
|
| 274 |
+
* Return whether this Timestamp is greater than the other.
|
| 275 |
+
*
|
| 276 |
+
* @method
|
| 277 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 278 |
+
* @return {boolean} whether this Timestamp is greater than the other.
|
| 279 |
+
*/
|
| 280 |
+
Timestamp.prototype.greaterThan = function(other) {
|
| 281 |
+
return this.compare(other) > 0;
|
| 282 |
+
};
|
| 283 |
+
|
| 284 |
+
/**
|
| 285 |
+
* Return whether this Timestamp is greater than or equal to the other.
|
| 286 |
+
*
|
| 287 |
+
* @method
|
| 288 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 289 |
+
* @return {boolean} whether this Timestamp is greater than or equal to the other.
|
| 290 |
+
*/
|
| 291 |
+
Timestamp.prototype.greaterThanOrEqual = function(other) {
|
| 292 |
+
return this.compare(other) >= 0;
|
| 293 |
+
};
|
| 294 |
+
|
| 295 |
+
/**
|
| 296 |
+
* Compares this Timestamp with the given one.
|
| 297 |
+
*
|
| 298 |
+
* @method
|
| 299 |
+
* @param {Timestamp} other Timestamp to compare against.
|
| 300 |
+
* @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
|
| 301 |
+
*/
|
| 302 |
+
Timestamp.prototype.compare = function(other) {
|
| 303 |
+
if (this.equals(other)) {
|
| 304 |
+
return 0;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
var thisNeg = this.isNegative();
|
| 308 |
+
var otherNeg = other.isNegative();
|
| 309 |
+
if (thisNeg && !otherNeg) {
|
| 310 |
+
return -1;
|
| 311 |
+
}
|
| 312 |
+
if (!thisNeg && otherNeg) {
|
| 313 |
+
return 1;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
// at this point, the signs are the same, so subtraction will not overflow
|
| 317 |
+
if (this.subtract(other).isNegative()) {
|
| 318 |
+
return -1;
|
| 319 |
+
} else {
|
| 320 |
+
return 1;
|
| 321 |
+
}
|
| 322 |
+
};
|
| 323 |
+
|
| 324 |
+
/**
|
| 325 |
+
* The negation of this value.
|
| 326 |
+
*
|
| 327 |
+
* @method
|
| 328 |
+
* @return {Timestamp} the negation of this value.
|
| 329 |
+
*/
|
| 330 |
+
Timestamp.prototype.negate = function() {
|
| 331 |
+
if (this.equals(Timestamp.MIN_VALUE)) {
|
| 332 |
+
return Timestamp.MIN_VALUE;
|
| 333 |
+
} else {
|
| 334 |
+
return this.not().add(Timestamp.ONE);
|
| 335 |
+
}
|
| 336 |
+
};
|
| 337 |
+
|
| 338 |
+
/**
|
| 339 |
+
* Returns the sum of this and the given Timestamp.
|
| 340 |
+
*
|
| 341 |
+
* @method
|
| 342 |
+
* @param {Timestamp} other Timestamp to add to this one.
|
| 343 |
+
* @return {Timestamp} the sum of this and the given Timestamp.
|
| 344 |
+
*/
|
| 345 |
+
Timestamp.prototype.add = function(other) {
|
| 346 |
+
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
|
| 347 |
+
|
| 348 |
+
var a48 = this.high_ >>> 16;
|
| 349 |
+
var a32 = this.high_ & 0xFFFF;
|
| 350 |
+
var a16 = this.low_ >>> 16;
|
| 351 |
+
var a00 = this.low_ & 0xFFFF;
|
| 352 |
+
|
| 353 |
+
var b48 = other.high_ >>> 16;
|
| 354 |
+
var b32 = other.high_ & 0xFFFF;
|
| 355 |
+
var b16 = other.low_ >>> 16;
|
| 356 |
+
var b00 = other.low_ & 0xFFFF;
|
| 357 |
+
|
| 358 |
+
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
|
| 359 |
+
c00 += a00 + b00;
|
| 360 |
+
c16 += c00 >>> 16;
|
| 361 |
+
c00 &= 0xFFFF;
|
| 362 |
+
c16 += a16 + b16;
|
| 363 |
+
c32 += c16 >>> 16;
|
| 364 |
+
c16 &= 0xFFFF;
|
| 365 |
+
c32 += a32 + b32;
|
| 366 |
+
c48 += c32 >>> 16;
|
| 367 |
+
c32 &= 0xFFFF;
|
| 368 |
+
c48 += a48 + b48;
|
| 369 |
+
c48 &= 0xFFFF;
|
| 370 |
+
return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
|
| 371 |
+
};
|
| 372 |
+
|
| 373 |
+
/**
|
| 374 |
+
* Returns the difference of this and the given Timestamp.
|
| 375 |
+
*
|
| 376 |
+
* @method
|
| 377 |
+
* @param {Timestamp} other Timestamp to subtract from this.
|
| 378 |
+
* @return {Timestamp} the difference of this and the given Timestamp.
|
| 379 |
+
*/
|
| 380 |
+
Timestamp.prototype.subtract = function(other) {
|
| 381 |
+
return this.add(other.negate());
|
| 382 |
+
};
|
| 383 |
+
|
| 384 |
+
/**
|
| 385 |
+
* Returns the product of this and the given Timestamp.
|
| 386 |
+
*
|
| 387 |
+
* @method
|
| 388 |
+
* @param {Timestamp} other Timestamp to multiply with this.
|
| 389 |
+
* @return {Timestamp} the product of this and the other.
|
| 390 |
+
*/
|
| 391 |
+
Timestamp.prototype.multiply = function(other) {
|
| 392 |
+
if (this.isZero()) {
|
| 393 |
+
return Timestamp.ZERO;
|
| 394 |
+
} else if (other.isZero()) {
|
| 395 |
+
return Timestamp.ZERO;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
if (this.equals(Timestamp.MIN_VALUE)) {
|
| 399 |
+
return other.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO;
|
| 400 |
+
} else if (other.equals(Timestamp.MIN_VALUE)) {
|
| 401 |
+
return this.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
if (this.isNegative()) {
|
| 405 |
+
if (other.isNegative()) {
|
| 406 |
+
return this.negate().multiply(other.negate());
|
| 407 |
+
} else {
|
| 408 |
+
return this.negate().multiply(other).negate();
|
| 409 |
+
}
|
| 410 |
+
} else if (other.isNegative()) {
|
| 411 |
+
return this.multiply(other.negate()).negate();
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
// If both Timestamps are small, use float multiplication
|
| 415 |
+
if (this.lessThan(Timestamp.TWO_PWR_24_) &&
|
| 416 |
+
other.lessThan(Timestamp.TWO_PWR_24_)) {
|
| 417 |
+
return Timestamp.fromNumber(this.toNumber() * other.toNumber());
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
// Divide each Timestamp into 4 chunks of 16 bits, and then add up 4x4 products.
|
| 421 |
+
// We can skip products that would overflow.
|
| 422 |
+
|
| 423 |
+
var a48 = this.high_ >>> 16;
|
| 424 |
+
var a32 = this.high_ & 0xFFFF;
|
| 425 |
+
var a16 = this.low_ >>> 16;
|
| 426 |
+
var a00 = this.low_ & 0xFFFF;
|
| 427 |
+
|
| 428 |
+
var b48 = other.high_ >>> 16;
|
| 429 |
+
var b32 = other.high_ & 0xFFFF;
|
| 430 |
+
var b16 = other.low_ >>> 16;
|
| 431 |
+
var b00 = other.low_ & 0xFFFF;
|
| 432 |
+
|
| 433 |
+
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
|
| 434 |
+
c00 += a00 * b00;
|
| 435 |
+
c16 += c00 >>> 16;
|
| 436 |
+
c00 &= 0xFFFF;
|
| 437 |
+
c16 += a16 * b00;
|
| 438 |
+
c32 += c16 >>> 16;
|
| 439 |
+
c16 &= 0xFFFF;
|
| 440 |
+
c16 += a00 * b16;
|
| 441 |
+
c32 += c16 >>> 16;
|
| 442 |
+
c16 &= 0xFFFF;
|
| 443 |
+
c32 += a32 * b00;
|
| 444 |
+
c48 += c32 >>> 16;
|
| 445 |
+
c32 &= 0xFFFF;
|
| 446 |
+
c32 += a16 * b16;
|
| 447 |
+
c48 += c32 >>> 16;
|
| 448 |
+
c32 &= 0xFFFF;
|
| 449 |
+
c32 += a00 * b32;
|
| 450 |
+
c48 += c32 >>> 16;
|
| 451 |
+
c32 &= 0xFFFF;
|
| 452 |
+
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
|
| 453 |
+
c48 &= 0xFFFF;
|
| 454 |
+
return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
|
| 455 |
+
};
|
| 456 |
+
|
| 457 |
+
/**
|
| 458 |
+
* Returns this Timestamp divided by the given one.
|
| 459 |
+
*
|
| 460 |
+
* @method
|
| 461 |
+
* @param {Timestamp} other Timestamp by which to divide.
|
| 462 |
+
* @return {Timestamp} this Timestamp divided by the given one.
|
| 463 |
+
*/
|
| 464 |
+
Timestamp.prototype.div = function(other) {
|
| 465 |
+
if (other.isZero()) {
|
| 466 |
+
throw Error('division by zero');
|
| 467 |
+
} else if (this.isZero()) {
|
| 468 |
+
return Timestamp.ZERO;
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
if (this.equals(Timestamp.MIN_VALUE)) {
|
| 472 |
+
if (other.equals(Timestamp.ONE) ||
|
| 473 |
+
other.equals(Timestamp.NEG_ONE)) {
|
| 474 |
+
return Timestamp.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
|
| 475 |
+
} else if (other.equals(Timestamp.MIN_VALUE)) {
|
| 476 |
+
return Timestamp.ONE;
|
| 477 |
+
} else {
|
| 478 |
+
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
|
| 479 |
+
var halfThis = this.shiftRight(1);
|
| 480 |
+
var approx = halfThis.div(other).shiftLeft(1);
|
| 481 |
+
if (approx.equals(Timestamp.ZERO)) {
|
| 482 |
+
return other.isNegative() ? Timestamp.ONE : Timestamp.NEG_ONE;
|
| 483 |
+
} else {
|
| 484 |
+
var rem = this.subtract(other.multiply(approx));
|
| 485 |
+
var result = approx.add(rem.div(other));
|
| 486 |
+
return result;
|
| 487 |
+
}
|
| 488 |
+
}
|
| 489 |
+
} else if (other.equals(Timestamp.MIN_VALUE)) {
|
| 490 |
+
return Timestamp.ZERO;
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
if (this.isNegative()) {
|
| 494 |
+
if (other.isNegative()) {
|
| 495 |
+
return this.negate().div(other.negate());
|
| 496 |
+
} else {
|
| 497 |
+
return this.negate().div(other).negate();
|
| 498 |
+
}
|
| 499 |
+
} else if (other.isNegative()) {
|
| 500 |
+
return this.div(other.negate()).negate();
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
// Repeat the following until the remainder is less than other: find a
|
| 504 |
+
// floating-point that approximates remainder / other *from below*, add this
|
| 505 |
+
// into the result, and subtract it from the remainder. It is critical that
|
| 506 |
+
// the approximate value is less than or equal to the real value so that the
|
| 507 |
+
// remainder never becomes negative.
|
| 508 |
+
var res = Timestamp.ZERO;
|
| 509 |
+
var rem = this;
|
| 510 |
+
while (rem.greaterThanOrEqual(other)) {
|
| 511 |
+
// Approximate the result of division. This may be a little greater or
|
| 512 |
+
// smaller than the actual value.
|
| 513 |
+
var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
|
| 514 |
+
|
| 515 |
+
// We will tweak the approximate result by changing it in the 48-th digit or
|
| 516 |
+
// the smallest non-fractional digit, whichever is larger.
|
| 517 |
+
var log2 = Math.ceil(Math.log(approx) / Math.LN2);
|
| 518 |
+
var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);
|
| 519 |
+
|
| 520 |
+
// Decrease the approximation until it is smaller than the remainder. Note
|
| 521 |
+
// that if it is too large, the product overflows and is negative.
|
| 522 |
+
var approxRes = Timestamp.fromNumber(approx);
|
| 523 |
+
var approxRem = approxRes.multiply(other);
|
| 524 |
+
while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
|
| 525 |
+
approx -= delta;
|
| 526 |
+
approxRes = Timestamp.fromNumber(approx);
|
| 527 |
+
approxRem = approxRes.multiply(other);
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
// We know the answer can't be zero... and actually, zero would cause
|
| 531 |
+
// infinite recursion since we would make no progress.
|
| 532 |
+
if (approxRes.isZero()) {
|
| 533 |
+
approxRes = Timestamp.ONE;
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
res = res.add(approxRes);
|
| 537 |
+
rem = rem.subtract(approxRem);
|
| 538 |
+
}
|
| 539 |
+
return res;
|
| 540 |
+
};
|
| 541 |
+
|
| 542 |
+
/**
|
| 543 |
+
* Returns this Timestamp modulo the given one.
|
| 544 |
+
*
|
| 545 |
+
* @method
|
| 546 |
+
* @param {Timestamp} other Timestamp by which to mod.
|
| 547 |
+
* @return {Timestamp} this Timestamp modulo the given one.
|
| 548 |
+
*/
|
| 549 |
+
Timestamp.prototype.modulo = function(other) {
|
| 550 |
+
return this.subtract(this.div(other).multiply(other));
|
| 551 |
+
};
|
| 552 |
+
|
| 553 |
+
/**
|
| 554 |
+
* The bitwise-NOT of this value.
|
| 555 |
+
*
|
| 556 |
+
* @method
|
| 557 |
+
* @return {Timestamp} the bitwise-NOT of this value.
|
| 558 |
+
*/
|
| 559 |
+
Timestamp.prototype.not = function() {
|
| 560 |
+
return Timestamp.fromBits(~this.low_, ~this.high_);
|
| 561 |
+
};
|
| 562 |
+
|
| 563 |
+
/**
|
| 564 |
+
* Returns the bitwise-AND of this Timestamp and the given one.
|
| 565 |
+
*
|
| 566 |
+
* @method
|
| 567 |
+
* @param {Timestamp} other the Timestamp with which to AND.
|
| 568 |
+
* @return {Timestamp} the bitwise-AND of this and the other.
|
| 569 |
+
*/
|
| 570 |
+
Timestamp.prototype.and = function(other) {
|
| 571 |
+
return Timestamp.fromBits(this.low_ & other.low_, this.high_ & other.high_);
|
| 572 |
+
};
|
| 573 |
+
|
| 574 |
+
/**
|
| 575 |
+
* Returns the bitwise-OR of this Timestamp and the given one.
|
| 576 |
+
*
|
| 577 |
+
* @method
|
| 578 |
+
* @param {Timestamp} other the Timestamp with which to OR.
|
| 579 |
+
* @return {Timestamp} the bitwise-OR of this and the other.
|
| 580 |
+
*/
|
| 581 |
+
Timestamp.prototype.or = function(other) {
|
| 582 |
+
return Timestamp.fromBits(this.low_ | other.low_, this.high_ | other.high_);
|
| 583 |
+
};
|
| 584 |
+
|
| 585 |
+
/**
|
| 586 |
+
* Returns the bitwise-XOR of this Timestamp and the given one.
|
| 587 |
+
*
|
| 588 |
+
* @method
|
| 589 |
+
* @param {Timestamp} other the Timestamp with which to XOR.
|
| 590 |
+
* @return {Timestamp} the bitwise-XOR of this and the other.
|
| 591 |
+
*/
|
| 592 |
+
Timestamp.prototype.xor = function(other) {
|
| 593 |
+
return Timestamp.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
|
| 594 |
+
};
|
| 595 |
+
|
| 596 |
+
/**
|
| 597 |
+
* Returns this Timestamp with bits shifted to the left by the given amount.
|
| 598 |
+
*
|
| 599 |
+
* @method
|
| 600 |
+
* @param {number} numBits the number of bits by which to shift.
|
| 601 |
+
* @return {Timestamp} this shifted to the left by the given amount.
|
| 602 |
+
*/
|
| 603 |
+
Timestamp.prototype.shiftLeft = function(numBits) {
|
| 604 |
+
numBits &= 63;
|
| 605 |
+
if (numBits == 0) {
|
| 606 |
+
return this;
|
| 607 |
+
} else {
|
| 608 |
+
var low = this.low_;
|
| 609 |
+
if (numBits < 32) {
|
| 610 |
+
var high = this.high_;
|
| 611 |
+
return Timestamp.fromBits(
|
| 612 |
+
low << numBits,
|
| 613 |
+
(high << numBits) | (low >>> (32 - numBits)));
|
| 614 |
+
} else {
|
| 615 |
+
return Timestamp.fromBits(0, low << (numBits - 32));
|
| 616 |
+
}
|
| 617 |
+
}
|
| 618 |
+
};
|
| 619 |
+
|
| 620 |
+
/**
|
| 621 |
+
* Returns this Timestamp with bits shifted to the right by the given amount.
|
| 622 |
+
*
|
| 623 |
+
* @method
|
| 624 |
+
* @param {number} numBits the number of bits by which to shift.
|
| 625 |
+
* @return {Timestamp} this shifted to the right by the given amount.
|
| 626 |
+
*/
|
| 627 |
+
Timestamp.prototype.shiftRight = function(numBits) {
|
| 628 |
+
numBits &= 63;
|
| 629 |
+
if (numBits == 0) {
|
| 630 |
+
return this;
|
| 631 |
+
} else {
|
| 632 |
+
var high = this.high_;
|
| 633 |
+
if (numBits < 32) {
|
| 634 |
+
var low = this.low_;
|
| 635 |
+
return Timestamp.fromBits(
|
| 636 |
+
(low >>> numBits) | (high << (32 - numBits)),
|
| 637 |
+
high >> numBits);
|
| 638 |
+
} else {
|
| 639 |
+
return Timestamp.fromBits(
|
| 640 |
+
high >> (numBits - 32),
|
| 641 |
+
high >= 0 ? 0 : -1);
|
| 642 |
+
}
|
| 643 |
+
}
|
| 644 |
+
};
|
| 645 |
+
|
| 646 |
+
/**
|
| 647 |
+
* Returns this Timestamp with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
|
| 648 |
+
*
|
| 649 |
+
* @method
|
| 650 |
+
* @param {number} numBits the number of bits by which to shift.
|
| 651 |
+
* @return {Timestamp} this shifted to the right by the given amount, with zeros placed into the new leading bits.
|
| 652 |
+
*/
|
| 653 |
+
Timestamp.prototype.shiftRightUnsigned = function(numBits) {
|
| 654 |
+
numBits &= 63;
|
| 655 |
+
if (numBits == 0) {
|
| 656 |
+
return this;
|
| 657 |
+
} else {
|
| 658 |
+
var high = this.high_;
|
| 659 |
+
if (numBits < 32) {
|
| 660 |
+
var low = this.low_;
|
| 661 |
+
return Timestamp.fromBits(
|
| 662 |
+
(low >>> numBits) | (high << (32 - numBits)),
|
| 663 |
+
high >>> numBits);
|
| 664 |
+
} else if (numBits == 32) {
|
| 665 |
+
return Timestamp.fromBits(high, 0);
|
| 666 |
+
} else {
|
| 667 |
+
return Timestamp.fromBits(high >>> (numBits - 32), 0);
|
| 668 |
+
}
|
| 669 |
+
}
|
| 670 |
+
};
|
| 671 |
+
|
| 672 |
+
/**
|
| 673 |
+
* Returns a Timestamp representing the given (32-bit) integer value.
|
| 674 |
+
*
|
| 675 |
+
* @method
|
| 676 |
+
* @param {number} value the 32-bit integer in question.
|
| 677 |
+
* @return {Timestamp} the corresponding Timestamp value.
|
| 678 |
+
*/
|
| 679 |
+
Timestamp.fromInt = function(value) {
|
| 680 |
+
if (-128 <= value && value < 128) {
|
| 681 |
+
var cachedObj = Timestamp.INT_CACHE_[value];
|
| 682 |
+
if (cachedObj) {
|
| 683 |
+
return cachedObj;
|
| 684 |
+
}
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
var obj = new Timestamp(value | 0, value < 0 ? -1 : 0);
|
| 688 |
+
if (-128 <= value && value < 128) {
|
| 689 |
+
Timestamp.INT_CACHE_[value] = obj;
|
| 690 |
+
}
|
| 691 |
+
return obj;
|
| 692 |
+
};
|
| 693 |
+
|
| 694 |
+
/**
|
| 695 |
+
* Returns a Timestamp representing the given value, provided that it is a finite number. Otherwise, zero is returned.
|
| 696 |
+
*
|
| 697 |
+
* @method
|
| 698 |
+
* @param {number} value the number in question.
|
| 699 |
+
* @return {Timestamp} the corresponding Timestamp value.
|
| 700 |
+
*/
|
| 701 |
+
Timestamp.fromNumber = function(value) {
|
| 702 |
+
if (isNaN(value) || !isFinite(value)) {
|
| 703 |
+
return Timestamp.ZERO;
|
| 704 |
+
} else if (value <= -Timestamp.TWO_PWR_63_DBL_) {
|
| 705 |
+
return Timestamp.MIN_VALUE;
|
| 706 |
+
} else if (value + 1 >= Timestamp.TWO_PWR_63_DBL_) {
|
| 707 |
+
return Timestamp.MAX_VALUE;
|
| 708 |
+
} else if (value < 0) {
|
| 709 |
+
return Timestamp.fromNumber(-value).negate();
|
| 710 |
+
} else {
|
| 711 |
+
return new Timestamp(
|
| 712 |
+
(value % Timestamp.TWO_PWR_32_DBL_) | 0,
|
| 713 |
+
(value / Timestamp.TWO_PWR_32_DBL_) | 0);
|
| 714 |
+
}
|
| 715 |
+
};
|
| 716 |
+
|
| 717 |
+
/**
|
| 718 |
+
* 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.
|
| 719 |
+
*
|
| 720 |
+
* @method
|
| 721 |
+
* @param {number} lowBits the low 32-bits.
|
| 722 |
+
* @param {number} highBits the high 32-bits.
|
| 723 |
+
* @return {Timestamp} the corresponding Timestamp value.
|
| 724 |
+
*/
|
| 725 |
+
Timestamp.fromBits = function(lowBits, highBits) {
|
| 726 |
+
return new Timestamp(lowBits, highBits);
|
| 727 |
+
};
|
| 728 |
+
|
| 729 |
+
/**
|
| 730 |
+
* Returns a Timestamp representation of the given string, written using the given radix.
|
| 731 |
+
*
|
| 732 |
+
* @method
|
| 733 |
+
* @param {string} str the textual representation of the Timestamp.
|
| 734 |
+
* @param {number} opt_radix the radix in which the text is written.
|
| 735 |
+
* @return {Timestamp} the corresponding Timestamp value.
|
| 736 |
+
*/
|
| 737 |
+
Timestamp.fromString = function(str, opt_radix) {
|
| 738 |
+
if (str.length == 0) {
|
| 739 |
+
throw Error('number format error: empty string');
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
var radix = opt_radix || 10;
|
| 743 |
+
if (radix < 2 || 36 < radix) {
|
| 744 |
+
throw Error('radix out of range: ' + radix);
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
if (str.charAt(0) == '-') {
|
| 748 |
+
return Timestamp.fromString(str.substring(1), radix).negate();
|
| 749 |
+
} else if (str.indexOf('-') >= 0) {
|
| 750 |
+
throw Error('number format error: interior "-" character: ' + str);
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
// Do several (8) digits each time through the loop, so as to
|
| 754 |
+
// minimize the calls to the very expensive emulated div.
|
| 755 |
+
var radixToPower = Timestamp.fromNumber(Math.pow(radix, 8));
|
| 756 |
+
|
| 757 |
+
var result = Timestamp.ZERO;
|
| 758 |
+
for (var i = 0; i < str.length; i += 8) {
|
| 759 |
+
var size = Math.min(8, str.length - i);
|
| 760 |
+
var value = parseInt(str.substring(i, i + size), radix);
|
| 761 |
+
if (size < 8) {
|
| 762 |
+
var power = Timestamp.fromNumber(Math.pow(radix, size));
|
| 763 |
+
result = result.multiply(power).add(Timestamp.fromNumber(value));
|
| 764 |
+
} else {
|
| 765 |
+
result = result.multiply(radixToPower);
|
| 766 |
+
result = result.add(Timestamp.fromNumber(value));
|
| 767 |
+
}
|
| 768 |
+
}
|
| 769 |
+
return result;
|
| 770 |
+
};
|
| 771 |
+
|
| 772 |
+
// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
|
| 773 |
+
// from* methods on which they depend.
|
| 774 |
+
|
| 775 |
+
|
| 776 |
+
/**
|
| 777 |
+
* A cache of the Timestamp representations of small integer values.
|
| 778 |
+
* @type {Object}
|
| 779 |
+
* @ignore
|
| 780 |
+
*/
|
| 781 |
+
Timestamp.INT_CACHE_ = {};
|
| 782 |
+
|
| 783 |
+
// NOTE: the compiler should inline these constant values below and then remove
|
| 784 |
+
// these variables, so there should be no runtime penalty for these.
|
| 785 |
+
|
| 786 |
+
/**
|
| 787 |
+
* Number used repeated below in calculations. This must appear before the
|
| 788 |
+
* first call to any from* function below.
|
| 789 |
+
* @type {number}
|
| 790 |
+
* @ignore
|
| 791 |
+
*/
|
| 792 |
+
Timestamp.TWO_PWR_16_DBL_ = 1 << 16;
|
| 793 |
+
|
| 794 |
+
/**
|
| 795 |
+
* @type {number}
|
| 796 |
+
* @ignore
|
| 797 |
+
*/
|
| 798 |
+
Timestamp.TWO_PWR_24_DBL_ = 1 << 24;
|
| 799 |
+
|
| 800 |
+
/**
|
| 801 |
+
* @type {number}
|
| 802 |
+
* @ignore
|
| 803 |
+
*/
|
| 804 |
+
Timestamp.TWO_PWR_32_DBL_ = Timestamp.TWO_PWR_16_DBL_ * Timestamp.TWO_PWR_16_DBL_;
|
| 805 |
+
|
| 806 |
+
/**
|
| 807 |
+
* @type {number}
|
| 808 |
+
* @ignore
|
| 809 |
+
*/
|
| 810 |
+
Timestamp.TWO_PWR_31_DBL_ = Timestamp.TWO_PWR_32_DBL_ / 2;
|
| 811 |
+
|
| 812 |
+
/**
|
| 813 |
+
* @type {number}
|
| 814 |
+
* @ignore
|
| 815 |
+
*/
|
| 816 |
+
Timestamp.TWO_PWR_48_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_16_DBL_;
|
| 817 |
+
|
| 818 |
+
/**
|
| 819 |
+
* @type {number}
|
| 820 |
+
* @ignore
|
| 821 |
+
*/
|
| 822 |
+
Timestamp.TWO_PWR_64_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_32_DBL_;
|
| 823 |
+
|
| 824 |
+
/**
|
| 825 |
+
* @type {number}
|
| 826 |
+
* @ignore
|
| 827 |
+
*/
|
| 828 |
+
Timestamp.TWO_PWR_63_DBL_ = Timestamp.TWO_PWR_64_DBL_ / 2;
|
| 829 |
+
|
| 830 |
+
/** @type {Timestamp} */
|
| 831 |
+
Timestamp.ZERO = Timestamp.fromInt(0);
|
| 832 |
+
|
| 833 |
+
/** @type {Timestamp} */
|
| 834 |
+
Timestamp.ONE = Timestamp.fromInt(1);
|
| 835 |
+
|
| 836 |
+
/** @type {Timestamp} */
|
| 837 |
+
Timestamp.NEG_ONE = Timestamp.fromInt(-1);
|
| 838 |
+
|
| 839 |
+
/** @type {Timestamp} */
|
| 840 |
+
Timestamp.MAX_VALUE =
|
| 841 |
+
Timestamp.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);
|
| 842 |
+
|
| 843 |
+
/** @type {Timestamp} */
|
| 844 |
+
Timestamp.MIN_VALUE = Timestamp.fromBits(0, 0x80000000 | 0);
|
| 845 |
+
|
| 846 |
+
/**
|
| 847 |
+
* @type {Timestamp}
|
| 848 |
+
* @ignore
|
| 849 |
+
*/
|
| 850 |
+
Timestamp.TWO_PWR_24_ = Timestamp.fromInt(1 << 24);
|
| 851 |
+
|
| 852 |
+
/**
|
| 853 |
+
* Expose.
|
| 854 |
+
*/
|
| 855 |
+
module.exports = Timestamp;
|
| 856 |
+
module.exports.Timestamp = Timestamp;
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/.npmignore
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.travis.yml
|
| 2 |
+
appveyor.yml
|
| 3 |
+
test.bat
|
| 4 |
+
build/
|
| 5 |
+
test/
|
| 6 |
+
.vagrant/
|
| 7 |
+
|
| 8 |
+
# Users Environment Variables
|
| 9 |
+
.lock-wscript
|
| 10 |
+
|
| 11 |
+
.DS_Store
|
| 12 |
+
*.swp
|
| 13 |
+
*.seed
|
| 14 |
+
.project
|
| 15 |
+
.settings
|
| 16 |
+
./data
|
| 17 |
+
node_modules/
|
| 18 |
+
|
| 19 |
+
# Logs
|
| 20 |
+
logs
|
| 21 |
+
*.log
|
| 22 |
+
|
| 23 |
+
# Runtime data
|
| 24 |
+
pids
|
| 25 |
+
*.pid
|
| 26 |
+
*.seed
|
| 27 |
+
|
| 28 |
+
# Directory for instrumented libs generated by jscoverage/JSCover
|
| 29 |
+
lib-cov
|
| 30 |
+
|
| 31 |
+
# Coverage directory used by tools like istanbul
|
| 32 |
+
coverage
|
| 33 |
+
|
| 34 |
+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
| 35 |
+
.grunt
|
| 36 |
+
|
| 37 |
+
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
| 38 |
+
build/Release
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/HISTORY
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0.1.7 2015-05-15
|
| 2 |
+
----------------
|
| 3 |
+
- Attempt node-pre-gyp otherwise fallback to node-gyp or just fail.
|
| 4 |
+
|
| 5 |
+
0.1.6 2015-05-07
|
| 6 |
+
----------------
|
| 7 |
+
- Updated to use bundled node-pre-gyp as install method.
|
| 8 |
+
|
| 9 |
+
0.1.5 2015-05-07
|
| 10 |
+
----------------
|
| 11 |
+
- Updated npmignore to remove any non-needed artifacts.
|
| 12 |
+
|
| 13 |
+
0.1.4 2015-05-05
|
| 14 |
+
----------------
|
| 15 |
+
- Updated nan.h dependency to 1.8.x.
|
| 16 |
+
|
| 17 |
+
0.1.3 2015-04-23
|
| 18 |
+
----------------
|
| 19 |
+
- Windows only prebuilt support (Issue #6, https://github.com/imlucas)
|
| 20 |
+
|
| 21 |
+
0.1.2 2015-04-20
|
| 22 |
+
----------------
|
| 23 |
+
- Removed pre-packaged binaries from build.
|
| 24 |
+
|
| 25 |
+
0.1.1 2015-04-20
|
| 26 |
+
----------------
|
| 27 |
+
- Merged node-pre-gyp support (Issue #1, https://github.com/imlucas)
|
| 28 |
+
|
| 29 |
+
0.1.0 2015-03-26
|
| 30 |
+
----------------
|
| 31 |
+
- First pusht to npm, cleanup of project and left only c++ and test harnesses.
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [yyyy] [name of copyright owner]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/Makefile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
NODE = node
|
| 2 |
+
NPM = npm
|
| 3 |
+
NODEUNIT = node_modules/nodeunit/bin/nodeunit
|
| 4 |
+
|
| 5 |
+
all: clean node_gyp
|
| 6 |
+
|
| 7 |
+
test: clean node_gyp
|
| 8 |
+
npm test
|
| 9 |
+
|
| 10 |
+
node_gyp: clean
|
| 11 |
+
node-gyp configure build
|
| 12 |
+
|
| 13 |
+
clean:
|
| 14 |
+
node-gyp clean
|
| 15 |
+
|
| 16 |
+
.PHONY: all
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# bson-ext
|
| 2 |
+
|
| 3 |
+
[](http://travis-ci.org/imlucas/bson-ext)
|
| 4 |
+
[](https://ci.appveyor.com/project/imlucas/bson-ext)
|
| 5 |
+
|
| 6 |
+
This module contains the BSON [native addon](https://nodejs.org/api/addons.html)
|
| 7 |
+
only and is not meant to be used in isolation from the [bson](http://npm.im/bson)
|
| 8 |
+
NPM module. It lives in it's own module so it can be an optional
|
| 9 |
+
dependency for the [bson](http://npm.im/bson) module.
|
| 10 |
+
|
| 11 |
+
## Testing
|
| 12 |
+
|
| 13 |
+
```
|
| 14 |
+
npm test
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
## Prebuilt Binaries
|
| 18 |
+
|
| 19 |
+
Have you ever seen this message in your console?
|
| 20 |
+
|
| 21 |
+
```
|
| 22 |
+
js-bson: Failed to load c++ bson extension, using pure JS version
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
We are experimenting with [node-pre-gyp](http://npm.im/node-pre-gyp) to publish
|
| 26 |
+
and install prebuilt binaries. This means you don't need the full toolchain installed
|
| 27 |
+
and configured correctly to use this module and you'll never have to see this
|
| 28 |
+
message again. Currently, prebuilt binaries will only be used for Windows,
|
| 29 |
+
as it is the most problematic platform for this issue. This will also allow us
|
| 30 |
+
more time to evaluate the costs and benefits of prebuilt support on OSX and Linux.
|
| 31 |
+
|
| 32 |
+
If you are interested in prebuilt binary support on OSX or Linux, please
|
| 33 |
+
[join the discussion on this issue](https://github.com/christkv/bson-ext/issues/6)!
|
| 34 |
+
|
| 35 |
+
## License
|
| 36 |
+
|
| 37 |
+
Apache 2
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/binding.gyp
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
'targets': [
|
| 3 |
+
{
|
| 4 |
+
'win_delay_load_hook': 'true',
|
| 5 |
+
'target_name': 'bson',
|
| 6 |
+
'sources': [ 'ext/bson.cc' ],
|
| 7 |
+
'cflags!': [ '-fno-exceptions' ],
|
| 8 |
+
'cflags_cc!': [ '-fno-exceptions' ],
|
| 9 |
+
'include_dirs': [ '<!(node -e "require(\'nan\')")' ],
|
| 10 |
+
'conditions': [
|
| 11 |
+
['OS=="mac"', {
|
| 12 |
+
'xcode_settings': {
|
| 13 |
+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
|
| 14 |
+
}
|
| 15 |
+
}],
|
| 16 |
+
['OS=="win"', {
|
| 17 |
+
'configurations': {
|
| 18 |
+
'Release': {
|
| 19 |
+
'msvs_settings': {
|
| 20 |
+
'VCCLCompilerTool': {
|
| 21 |
+
'ExceptionHandling': 1
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
}]
|
| 27 |
+
]
|
| 28 |
+
}
|
| 29 |
+
]
|
| 30 |
+
}
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Makefile
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# We borrow heavily from the kernel build setup, though we are simpler since
|
| 2 |
+
# we don't have Kconfig tweaking settings on us.
|
| 3 |
+
|
| 4 |
+
# The implicit make rules have it looking for RCS files, among other things.
|
| 5 |
+
# We instead explicitly write all the rules we care about.
|
| 6 |
+
# It's even quicker (saves ~200ms) to pass -r on the command line.
|
| 7 |
+
MAKEFLAGS=-r
|
| 8 |
+
|
| 9 |
+
# The source directory tree.
|
| 10 |
+
srcdir := ..
|
| 11 |
+
abs_srcdir := $(abspath $(srcdir))
|
| 12 |
+
|
| 13 |
+
# The name of the builddir.
|
| 14 |
+
builddir_name ?= .
|
| 15 |
+
|
| 16 |
+
# The V=1 flag on command line makes us verbosely print command lines.
|
| 17 |
+
ifdef V
|
| 18 |
+
quiet=
|
| 19 |
+
else
|
| 20 |
+
quiet=quiet_
|
| 21 |
+
endif
|
| 22 |
+
|
| 23 |
+
# Specify BUILDTYPE=Release on the command line for a release build.
|
| 24 |
+
BUILDTYPE ?= Release
|
| 25 |
+
|
| 26 |
+
# Directory all our build output goes into.
|
| 27 |
+
# Note that this must be two directories beneath src/ for unit tests to pass,
|
| 28 |
+
# as they reach into the src/ directory for data with relative paths.
|
| 29 |
+
builddir ?= $(builddir_name)/$(BUILDTYPE)
|
| 30 |
+
abs_builddir := $(abspath $(builddir))
|
| 31 |
+
depsdir := $(builddir)/.deps
|
| 32 |
+
|
| 33 |
+
# Object output directory.
|
| 34 |
+
obj := $(builddir)/obj
|
| 35 |
+
abs_obj := $(abspath $(obj))
|
| 36 |
+
|
| 37 |
+
# We build up a list of every single one of the targets so we can slurp in the
|
| 38 |
+
# generated dependency rule Makefiles in one pass.
|
| 39 |
+
all_deps :=
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
CC.target ?= $(CC)
|
| 44 |
+
CFLAGS.target ?= $(CFLAGS)
|
| 45 |
+
CXX.target ?= $(CXX)
|
| 46 |
+
CXXFLAGS.target ?= $(CXXFLAGS)
|
| 47 |
+
LINK.target ?= $(LINK)
|
| 48 |
+
LDFLAGS.target ?= $(LDFLAGS)
|
| 49 |
+
AR.target ?= $(AR)
|
| 50 |
+
|
| 51 |
+
# C++ apps need to be linked with g++.
|
| 52 |
+
#
|
| 53 |
+
# Note: flock is used to seralize linking. Linking is a memory-intensive
|
| 54 |
+
# process so running parallel links can often lead to thrashing. To disable
|
| 55 |
+
# the serialization, override LINK via an envrionment variable as follows:
|
| 56 |
+
#
|
| 57 |
+
# export LINK=g++
|
| 58 |
+
#
|
| 59 |
+
# This will allow make to invoke N linker processes as specified in -jN.
|
| 60 |
+
LINK ?= ./gyp-mac-tool flock $(builddir)/linker.lock $(CXX.target)
|
| 61 |
+
|
| 62 |
+
# TODO(evan): move all cross-compilation logic to gyp-time so we don't need
|
| 63 |
+
# to replicate this environment fallback in make as well.
|
| 64 |
+
CC.host ?= gcc
|
| 65 |
+
CFLAGS.host ?=
|
| 66 |
+
CXX.host ?= g++
|
| 67 |
+
CXXFLAGS.host ?=
|
| 68 |
+
LINK.host ?= $(CXX.host)
|
| 69 |
+
LDFLAGS.host ?=
|
| 70 |
+
AR.host ?= ar
|
| 71 |
+
|
| 72 |
+
# Define a dir function that can handle spaces.
|
| 73 |
+
# http://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions
|
| 74 |
+
# "leading spaces cannot appear in the text of the first argument as written.
|
| 75 |
+
# These characters can be put into the argument value by variable substitution."
|
| 76 |
+
empty :=
|
| 77 |
+
space := $(empty) $(empty)
|
| 78 |
+
|
| 79 |
+
# http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces
|
| 80 |
+
replace_spaces = $(subst $(space),?,$1)
|
| 81 |
+
unreplace_spaces = $(subst ?,$(space),$1)
|
| 82 |
+
dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1)))
|
| 83 |
+
|
| 84 |
+
# Flags to make gcc output dependency info. Note that you need to be
|
| 85 |
+
# careful here to use the flags that ccache and distcc can understand.
|
| 86 |
+
# We write to a dep file on the side first and then rename at the end
|
| 87 |
+
# so we can't end up with a broken dep file.
|
| 88 |
+
depfile = $(depsdir)/$(call replace_spaces,$@).d
|
| 89 |
+
DEPFLAGS = -MMD -MF $(depfile).raw
|
| 90 |
+
|
| 91 |
+
# We have to fixup the deps output in a few ways.
|
| 92 |
+
# (1) the file output should mention the proper .o file.
|
| 93 |
+
# ccache or distcc lose the path to the target, so we convert a rule of
|
| 94 |
+
# the form:
|
| 95 |
+
# foobar.o: DEP1 DEP2
|
| 96 |
+
# into
|
| 97 |
+
# path/to/foobar.o: DEP1 DEP2
|
| 98 |
+
# (2) we want missing files not to cause us to fail to build.
|
| 99 |
+
# We want to rewrite
|
| 100 |
+
# foobar.o: DEP1 DEP2 \
|
| 101 |
+
# DEP3
|
| 102 |
+
# to
|
| 103 |
+
# DEP1:
|
| 104 |
+
# DEP2:
|
| 105 |
+
# DEP3:
|
| 106 |
+
# so if the files are missing, they're just considered phony rules.
|
| 107 |
+
# We have to do some pretty insane escaping to get those backslashes
|
| 108 |
+
# and dollar signs past make, the shell, and sed at the same time.
|
| 109 |
+
# Doesn't work with spaces, but that's fine: .d files have spaces in
|
| 110 |
+
# their names replaced with other characters.
|
| 111 |
+
define fixup_dep
|
| 112 |
+
# The depfile may not exist if the input file didn't have any #includes.
|
| 113 |
+
touch $(depfile).raw
|
| 114 |
+
# Fixup path as in (1).
|
| 115 |
+
sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile)
|
| 116 |
+
# Add extra rules as in (2).
|
| 117 |
+
# We remove slashes and replace spaces with new lines;
|
| 118 |
+
# remove blank lines;
|
| 119 |
+
# delete the first line and append a colon to the remaining lines.
|
| 120 |
+
sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\
|
| 121 |
+
grep -v '^$$' |\
|
| 122 |
+
sed -e 1d -e 's|$$|:|' \
|
| 123 |
+
>> $(depfile)
|
| 124 |
+
rm $(depfile).raw
|
| 125 |
+
endef
|
| 126 |
+
|
| 127 |
+
# Command definitions:
|
| 128 |
+
# - cmd_foo is the actual command to run;
|
| 129 |
+
# - quiet_cmd_foo is the brief-output summary of the command.
|
| 130 |
+
|
| 131 |
+
quiet_cmd_cc = CC($(TOOLSET)) $@
|
| 132 |
+
cmd_cc = $(CC.$(TOOLSET)) $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c -o $@ $<
|
| 133 |
+
|
| 134 |
+
quiet_cmd_cxx = CXX($(TOOLSET)) $@
|
| 135 |
+
cmd_cxx = $(CXX.$(TOOLSET)) $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
|
| 136 |
+
|
| 137 |
+
quiet_cmd_objc = CXX($(TOOLSET)) $@
|
| 138 |
+
cmd_objc = $(CC.$(TOOLSET)) $(GYP_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
|
| 139 |
+
|
| 140 |
+
quiet_cmd_objcxx = CXX($(TOOLSET)) $@
|
| 141 |
+
cmd_objcxx = $(CXX.$(TOOLSET)) $(GYP_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
|
| 142 |
+
|
| 143 |
+
# Commands for precompiled header files.
|
| 144 |
+
quiet_cmd_pch_c = CXX($(TOOLSET)) $@
|
| 145 |
+
cmd_pch_c = $(CC.$(TOOLSET)) $(GYP_PCH_CFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
|
| 146 |
+
quiet_cmd_pch_cc = CXX($(TOOLSET)) $@
|
| 147 |
+
cmd_pch_cc = $(CC.$(TOOLSET)) $(GYP_PCH_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
|
| 148 |
+
quiet_cmd_pch_m = CXX($(TOOLSET)) $@
|
| 149 |
+
cmd_pch_m = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
|
| 150 |
+
quiet_cmd_pch_mm = CXX($(TOOLSET)) $@
|
| 151 |
+
cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
|
| 152 |
+
|
| 153 |
+
# gyp-mac-tool is written next to the root Makefile by gyp.
|
| 154 |
+
# Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd
|
| 155 |
+
# already.
|
| 156 |
+
quiet_cmd_mac_tool = MACTOOL $(4) $<
|
| 157 |
+
cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@"
|
| 158 |
+
|
| 159 |
+
quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@
|
| 160 |
+
cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4)
|
| 161 |
+
|
| 162 |
+
quiet_cmd_infoplist = INFOPLIST $@
|
| 163 |
+
cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@"
|
| 164 |
+
|
| 165 |
+
quiet_cmd_touch = TOUCH $@
|
| 166 |
+
cmd_touch = touch $@
|
| 167 |
+
|
| 168 |
+
quiet_cmd_copy = COPY $@
|
| 169 |
+
# send stderr to /dev/null to ignore messages when linking directories.
|
| 170 |
+
cmd_copy = rm -rf "$@" && cp -af "$<" "$@"
|
| 171 |
+
|
| 172 |
+
quiet_cmd_alink = LIBTOOL-STATIC $@
|
| 173 |
+
cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^)
|
| 174 |
+
|
| 175 |
+
quiet_cmd_link = LINK($(TOOLSET)) $@
|
| 176 |
+
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
|
| 177 |
+
|
| 178 |
+
quiet_cmd_solink = SOLINK($(TOOLSET)) $@
|
| 179 |
+
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
|
| 180 |
+
|
| 181 |
+
quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
|
| 182 |
+
cmd_solink_module = $(LINK.$(TOOLSET)) -bundle $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# Define an escape_quotes function to escape single quotes.
|
| 186 |
+
# This allows us to handle quotes properly as long as we always use
|
| 187 |
+
# use single quotes and escape_quotes.
|
| 188 |
+
escape_quotes = $(subst ','\'',$(1))
|
| 189 |
+
# This comment is here just to include a ' to unconfuse syntax highlighting.
|
| 190 |
+
# Define an escape_vars function to escape '$' variable syntax.
|
| 191 |
+
# This allows us to read/write command lines with shell variables (e.g.
|
| 192 |
+
# $LD_LIBRARY_PATH), without triggering make substitution.
|
| 193 |
+
escape_vars = $(subst $$,$$$$,$(1))
|
| 194 |
+
# Helper that expands to a shell command to echo a string exactly as it is in
|
| 195 |
+
# make. This uses printf instead of echo because printf's behaviour with respect
|
| 196 |
+
# to escape sequences is more portable than echo's across different shells
|
| 197 |
+
# (e.g., dash, bash).
|
| 198 |
+
exact_echo = printf '%s\n' '$(call escape_quotes,$(1))'
|
| 199 |
+
|
| 200 |
+
# Helper to compare the command we're about to run against the command
|
| 201 |
+
# we logged the last time we ran the command. Produces an empty
|
| 202 |
+
# string (false) when the commands match.
|
| 203 |
+
# Tricky point: Make has no string-equality test function.
|
| 204 |
+
# The kernel uses the following, but it seems like it would have false
|
| 205 |
+
# positives, where one string reordered its arguments.
|
| 206 |
+
# arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
|
| 207 |
+
# $(filter-out $(cmd_$@), $(cmd_$(1))))
|
| 208 |
+
# We instead substitute each for the empty string into the other, and
|
| 209 |
+
# say they're equal if both substitutions produce the empty string.
|
| 210 |
+
# .d files contain ? instead of spaces, take that into account.
|
| 211 |
+
command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\
|
| 212 |
+
$(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1))))
|
| 213 |
+
|
| 214 |
+
# Helper that is non-empty when a prerequisite changes.
|
| 215 |
+
# Normally make does this implicitly, but we force rules to always run
|
| 216 |
+
# so we can check their command lines.
|
| 217 |
+
# $? -- new prerequisites
|
| 218 |
+
# $| -- order-only dependencies
|
| 219 |
+
prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?))
|
| 220 |
+
|
| 221 |
+
# Helper that executes all postbuilds until one fails.
|
| 222 |
+
define do_postbuilds
|
| 223 |
+
@E=0;\
|
| 224 |
+
for p in $(POSTBUILDS); do\
|
| 225 |
+
eval $$p;\
|
| 226 |
+
E=$$?;\
|
| 227 |
+
if [ $$E -ne 0 ]; then\
|
| 228 |
+
break;\
|
| 229 |
+
fi;\
|
| 230 |
+
done;\
|
| 231 |
+
if [ $$E -ne 0 ]; then\
|
| 232 |
+
rm -rf "$@";\
|
| 233 |
+
exit $$E;\
|
| 234 |
+
fi
|
| 235 |
+
endef
|
| 236 |
+
|
| 237 |
+
# do_cmd: run a command via the above cmd_foo names, if necessary.
|
| 238 |
+
# Should always run for a given target to handle command-line changes.
|
| 239 |
+
# Second argument, if non-zero, makes it do asm/C/C++ dependency munging.
|
| 240 |
+
# Third argument, if non-zero, makes it do POSTBUILDS processing.
|
| 241 |
+
# Note: We intentionally do NOT call dirx for depfile, since it contains ? for
|
| 242 |
+
# spaces already and dirx strips the ? characters.
|
| 243 |
+
define do_cmd
|
| 244 |
+
$(if $(or $(command_changed),$(prereq_changed)),
|
| 245 |
+
@$(call exact_echo, $($(quiet)cmd_$(1)))
|
| 246 |
+
@mkdir -p "$(call dirx,$@)" "$(dir $(depfile))"
|
| 247 |
+
$(if $(findstring flock,$(word 2,$(cmd_$1))),
|
| 248 |
+
@$(cmd_$(1))
|
| 249 |
+
@echo " $(quiet_cmd_$(1)): Finished",
|
| 250 |
+
@$(cmd_$(1))
|
| 251 |
+
)
|
| 252 |
+
@$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile)
|
| 253 |
+
@$(if $(2),$(fixup_dep))
|
| 254 |
+
$(if $(and $(3), $(POSTBUILDS)),
|
| 255 |
+
$(call do_postbuilds)
|
| 256 |
+
)
|
| 257 |
+
)
|
| 258 |
+
endef
|
| 259 |
+
|
| 260 |
+
# Declare the "all" target first so it is the default,
|
| 261 |
+
# even though we don't have the deps yet.
|
| 262 |
+
.PHONY: all
|
| 263 |
+
all:
|
| 264 |
+
|
| 265 |
+
# make looks for ways to re-generate included makefiles, but in our case, we
|
| 266 |
+
# don't have a direct way. Explicitly telling make that it has nothing to do
|
| 267 |
+
# for them makes it go faster.
|
| 268 |
+
%.d: ;
|
| 269 |
+
|
| 270 |
+
# Use FORCE_DO_CMD to force a target to run. Should be coupled with
|
| 271 |
+
# do_cmd.
|
| 272 |
+
.PHONY: FORCE_DO_CMD
|
| 273 |
+
FORCE_DO_CMD:
|
| 274 |
+
|
| 275 |
+
TOOLSET := target
|
| 276 |
+
# Suffix rules, putting all outputs into $(obj).
|
| 277 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.c FORCE_DO_CMD
|
| 278 |
+
@$(call do_cmd,cc,1)
|
| 279 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
|
| 280 |
+
@$(call do_cmd,cxx,1)
|
| 281 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD
|
| 282 |
+
@$(call do_cmd,cxx,1)
|
| 283 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cxx FORCE_DO_CMD
|
| 284 |
+
@$(call do_cmd,cxx,1)
|
| 285 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.m FORCE_DO_CMD
|
| 286 |
+
@$(call do_cmd,objc,1)
|
| 287 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.mm FORCE_DO_CMD
|
| 288 |
+
@$(call do_cmd,objcxx,1)
|
| 289 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.S FORCE_DO_CMD
|
| 290 |
+
@$(call do_cmd,cc,1)
|
| 291 |
+
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.s FORCE_DO_CMD
|
| 292 |
+
@$(call do_cmd,cc,1)
|
| 293 |
+
|
| 294 |
+
# Try building from generated source, too.
|
| 295 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.c FORCE_DO_CMD
|
| 296 |
+
@$(call do_cmd,cc,1)
|
| 297 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
|
| 298 |
+
@$(call do_cmd,cxx,1)
|
| 299 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD
|
| 300 |
+
@$(call do_cmd,cxx,1)
|
| 301 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cxx FORCE_DO_CMD
|
| 302 |
+
@$(call do_cmd,cxx,1)
|
| 303 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.m FORCE_DO_CMD
|
| 304 |
+
@$(call do_cmd,objc,1)
|
| 305 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.mm FORCE_DO_CMD
|
| 306 |
+
@$(call do_cmd,objcxx,1)
|
| 307 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.S FORCE_DO_CMD
|
| 308 |
+
@$(call do_cmd,cc,1)
|
| 309 |
+
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.s FORCE_DO_CMD
|
| 310 |
+
@$(call do_cmd,cc,1)
|
| 311 |
+
|
| 312 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.c FORCE_DO_CMD
|
| 313 |
+
@$(call do_cmd,cc,1)
|
| 314 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.cc FORCE_DO_CMD
|
| 315 |
+
@$(call do_cmd,cxx,1)
|
| 316 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.cpp FORCE_DO_CMD
|
| 317 |
+
@$(call do_cmd,cxx,1)
|
| 318 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.cxx FORCE_DO_CMD
|
| 319 |
+
@$(call do_cmd,cxx,1)
|
| 320 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.m FORCE_DO_CMD
|
| 321 |
+
@$(call do_cmd,objc,1)
|
| 322 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.mm FORCE_DO_CMD
|
| 323 |
+
@$(call do_cmd,objcxx,1)
|
| 324 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.S FORCE_DO_CMD
|
| 325 |
+
@$(call do_cmd,cc,1)
|
| 326 |
+
$(obj).$(TOOLSET)/%.o: $(obj)/%.s FORCE_DO_CMD
|
| 327 |
+
@$(call do_cmd,cc,1)
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
ifeq ($(strip $(foreach prefix,$(NO_LOAD),\
|
| 331 |
+
$(findstring $(join ^,$(prefix)),\
|
| 332 |
+
$(join ^,bson.target.mk)))),)
|
| 333 |
+
include bson.target.mk
|
| 334 |
+
endif
|
| 335 |
+
|
| 336 |
+
quiet_cmd_regen_makefile = ACTION Regenerating $@
|
| 337 |
+
cmd_regen_makefile = cd $(srcdir); /usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "--toplevel-dir=." -I/Users/andrebaltieri/dev/5485/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/config.gypi -I/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi -I/Users/andrebaltieri/.node-gyp/0.12.3/common.gypi "--depth=." "-Goutput_dir=." "--generator-output=build" "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/andrebaltieri/.node-gyp/0.12.3" "-Dmodule_root_dir=/Users/andrebaltieri/dev/5485/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext" binding.gyp
|
| 338 |
+
Makefile: $(srcdir)/../../../../../../../../../../../usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi $(srcdir)/build/config.gypi $(srcdir)/binding.gyp $(srcdir)/../../../../../../../../../.node-gyp/0.12.3/common.gypi
|
| 339 |
+
$(call do_cmd,regen_makefile)
|
| 340 |
+
|
| 341 |
+
# "all" is a concatenation of the "all" targets from all the included
|
| 342 |
+
# sub-makefiles. This is just here to clarify.
|
| 343 |
+
all:
|
| 344 |
+
|
| 345 |
+
# Add in dependency-tracking rules. $(all_deps) is the list of every single
|
| 346 |
+
# target in our tree. Only consider the ones with .d (dependency) info:
|
| 347 |
+
d_files := $(wildcard $(foreach f,$(all_deps),$(depsdir)/$(f).d))
|
| 348 |
+
ifneq ($(d_files),)
|
| 349 |
+
include $(d_files)
|
| 350 |
+
endif
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/.deps/Release/bson.node.d
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
cmd_Release/bson.node := ./gyp-mac-tool flock ./Release/linker.lock c++ -bundle -Wl,-search_paths_first -mmacosx-version-min=10.5 -arch x86_64 -L./Release -o Release/bson.node Release/obj.target/bson/ext/bson.o -undefined dynamic_lookup
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/.deps/Release/obj.target/bson/ext/bson.o.d
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cmd_Release/obj.target/bson/ext/bson.o := c++ '-D_DARWIN_USE_64_BIT_INODE=1' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' -I/Users/andrebaltieri/.node-gyp/0.12.3/src -I/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include -I/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include -I../node_modules/nan -Os -gdwarf-2 -mmacosx-version-min=10.5 -arch x86_64 -Wall -Wendif-labels -W -Wno-unused-parameter -fno-rtti -fno-threadsafe-statics -fno-strict-aliasing -MMD -MF ./Release/.deps/Release/obj.target/bson/ext/bson.o.d.raw -c -o Release/obj.target/bson/ext/bson.o ../ext/bson.cc
|
| 2 |
+
Release/obj.target/bson/ext/bson.o: ../ext/bson.cc \
|
| 3 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include/v8.h \
|
| 4 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include/v8stdint.h \
|
| 5 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include/v8config.h \
|
| 6 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node.h \
|
| 7 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node_version.h \
|
| 8 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node_buffer.h \
|
| 9 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/smalloc.h ../ext/bson.h \
|
| 10 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node_object_wrap.h \
|
| 11 |
+
../node_modules/nan/nan.h \
|
| 12 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv.h \
|
| 13 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-errno.h \
|
| 14 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-version.h \
|
| 15 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-unix.h \
|
| 16 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-threadpool.h \
|
| 17 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-darwin.h \
|
| 18 |
+
../node_modules/nan/nan_new.h \
|
| 19 |
+
../node_modules/nan/nan_implementation_12_inl.h
|
| 20 |
+
../ext/bson.cc:
|
| 21 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include/v8.h:
|
| 22 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include/v8stdint.h:
|
| 23 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include/v8config.h:
|
| 24 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node.h:
|
| 25 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node_version.h:
|
| 26 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node_buffer.h:
|
| 27 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/smalloc.h:
|
| 28 |
+
../ext/bson.h:
|
| 29 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/src/node_object_wrap.h:
|
| 30 |
+
../node_modules/nan/nan.h:
|
| 31 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv.h:
|
| 32 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-errno.h:
|
| 33 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-version.h:
|
| 34 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-unix.h:
|
| 35 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-threadpool.h:
|
| 36 |
+
/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include/uv-darwin.h:
|
| 37 |
+
../node_modules/nan/nan_new.h:
|
| 38 |
+
../node_modules/nan/nan_implementation_12_inl.h:
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/bson.node
ADDED
|
Binary file (63.7 kB). View file
|
|
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/binding.Makefile
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is generated by gyp; do not edit.
|
| 2 |
+
|
| 3 |
+
export builddir_name ?= ./build/.
|
| 4 |
+
.PHONY: all
|
| 5 |
+
all:
|
| 6 |
+
$(MAKE) bson
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/bson.target.mk
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is generated by gyp; do not edit.
|
| 2 |
+
|
| 3 |
+
TOOLSET := target
|
| 4 |
+
TARGET := bson
|
| 5 |
+
DEFS_Debug := \
|
| 6 |
+
'-D_DARWIN_USE_64_BIT_INODE=1' \
|
| 7 |
+
'-D_LARGEFILE_SOURCE' \
|
| 8 |
+
'-D_FILE_OFFSET_BITS=64' \
|
| 9 |
+
'-DBUILDING_NODE_EXTENSION' \
|
| 10 |
+
'-DDEBUG' \
|
| 11 |
+
'-D_DEBUG'
|
| 12 |
+
|
| 13 |
+
# Flags passed to all source files.
|
| 14 |
+
CFLAGS_Debug := \
|
| 15 |
+
-O0 \
|
| 16 |
+
-gdwarf-2 \
|
| 17 |
+
-mmacosx-version-min=10.5 \
|
| 18 |
+
-arch x86_64 \
|
| 19 |
+
-Wall \
|
| 20 |
+
-Wendif-labels \
|
| 21 |
+
-W \
|
| 22 |
+
-Wno-unused-parameter
|
| 23 |
+
|
| 24 |
+
# Flags passed to only C files.
|
| 25 |
+
CFLAGS_C_Debug := \
|
| 26 |
+
-fno-strict-aliasing
|
| 27 |
+
|
| 28 |
+
# Flags passed to only C++ files.
|
| 29 |
+
CFLAGS_CC_Debug := \
|
| 30 |
+
-fno-rtti \
|
| 31 |
+
-fno-threadsafe-statics \
|
| 32 |
+
-fno-strict-aliasing
|
| 33 |
+
|
| 34 |
+
# Flags passed to only ObjC files.
|
| 35 |
+
CFLAGS_OBJC_Debug :=
|
| 36 |
+
|
| 37 |
+
# Flags passed to only ObjC++ files.
|
| 38 |
+
CFLAGS_OBJCC_Debug :=
|
| 39 |
+
|
| 40 |
+
INCS_Debug := \
|
| 41 |
+
-I/Users/andrebaltieri/.node-gyp/0.12.3/src \
|
| 42 |
+
-I/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include \
|
| 43 |
+
-I/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include \
|
| 44 |
+
-I$(srcdir)/node_modules/nan
|
| 45 |
+
|
| 46 |
+
DEFS_Release := \
|
| 47 |
+
'-D_DARWIN_USE_64_BIT_INODE=1' \
|
| 48 |
+
'-D_LARGEFILE_SOURCE' \
|
| 49 |
+
'-D_FILE_OFFSET_BITS=64' \
|
| 50 |
+
'-DBUILDING_NODE_EXTENSION'
|
| 51 |
+
|
| 52 |
+
# Flags passed to all source files.
|
| 53 |
+
CFLAGS_Release := \
|
| 54 |
+
-Os \
|
| 55 |
+
-gdwarf-2 \
|
| 56 |
+
-mmacosx-version-min=10.5 \
|
| 57 |
+
-arch x86_64 \
|
| 58 |
+
-Wall \
|
| 59 |
+
-Wendif-labels \
|
| 60 |
+
-W \
|
| 61 |
+
-Wno-unused-parameter
|
| 62 |
+
|
| 63 |
+
# Flags passed to only C files.
|
| 64 |
+
CFLAGS_C_Release := \
|
| 65 |
+
-fno-strict-aliasing
|
| 66 |
+
|
| 67 |
+
# Flags passed to only C++ files.
|
| 68 |
+
CFLAGS_CC_Release := \
|
| 69 |
+
-fno-rtti \
|
| 70 |
+
-fno-threadsafe-statics \
|
| 71 |
+
-fno-strict-aliasing
|
| 72 |
+
|
| 73 |
+
# Flags passed to only ObjC files.
|
| 74 |
+
CFLAGS_OBJC_Release :=
|
| 75 |
+
|
| 76 |
+
# Flags passed to only ObjC++ files.
|
| 77 |
+
CFLAGS_OBJCC_Release :=
|
| 78 |
+
|
| 79 |
+
INCS_Release := \
|
| 80 |
+
-I/Users/andrebaltieri/.node-gyp/0.12.3/src \
|
| 81 |
+
-I/Users/andrebaltieri/.node-gyp/0.12.3/deps/uv/include \
|
| 82 |
+
-I/Users/andrebaltieri/.node-gyp/0.12.3/deps/v8/include \
|
| 83 |
+
-I$(srcdir)/node_modules/nan
|
| 84 |
+
|
| 85 |
+
OBJS := \
|
| 86 |
+
$(obj).target/$(TARGET)/ext/bson.o
|
| 87 |
+
|
| 88 |
+
# Add to the list of files we specially track dependencies for.
|
| 89 |
+
all_deps += $(OBJS)
|
| 90 |
+
|
| 91 |
+
# CFLAGS et al overrides must be target-local.
|
| 92 |
+
# See "Target-specific Variable Values" in the GNU Make manual.
|
| 93 |
+
$(OBJS): TOOLSET := $(TOOLSET)
|
| 94 |
+
$(OBJS): GYP_CFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE))
|
| 95 |
+
$(OBJS): GYP_CXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE))
|
| 96 |
+
$(OBJS): GYP_OBJCFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))
|
| 97 |
+
$(OBJS): GYP_OBJCXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE))
|
| 98 |
+
|
| 99 |
+
# Suffix rules, putting all outputs into $(obj).
|
| 100 |
+
|
| 101 |
+
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
|
| 102 |
+
@$(call do_cmd,cxx,1)
|
| 103 |
+
|
| 104 |
+
# Try building from generated source, too.
|
| 105 |
+
|
| 106 |
+
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
|
| 107 |
+
@$(call do_cmd,cxx,1)
|
| 108 |
+
|
| 109 |
+
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj)/%.cc FORCE_DO_CMD
|
| 110 |
+
@$(call do_cmd,cxx,1)
|
| 111 |
+
|
| 112 |
+
# End of this set of suffix rules
|
| 113 |
+
### Rules for final target.
|
| 114 |
+
LDFLAGS_Debug := \
|
| 115 |
+
-Wl,-search_paths_first \
|
| 116 |
+
-mmacosx-version-min=10.5 \
|
| 117 |
+
-arch x86_64 \
|
| 118 |
+
-L$(builddir)
|
| 119 |
+
|
| 120 |
+
LIBTOOLFLAGS_Debug := \
|
| 121 |
+
-Wl,-search_paths_first
|
| 122 |
+
|
| 123 |
+
LDFLAGS_Release := \
|
| 124 |
+
-Wl,-search_paths_first \
|
| 125 |
+
-mmacosx-version-min=10.5 \
|
| 126 |
+
-arch x86_64 \
|
| 127 |
+
-L$(builddir)
|
| 128 |
+
|
| 129 |
+
LIBTOOLFLAGS_Release := \
|
| 130 |
+
-Wl,-search_paths_first
|
| 131 |
+
|
| 132 |
+
LIBS := \
|
| 133 |
+
-undefined dynamic_lookup
|
| 134 |
+
|
| 135 |
+
$(builddir)/bson.node: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))
|
| 136 |
+
$(builddir)/bson.node: LIBS := $(LIBS)
|
| 137 |
+
$(builddir)/bson.node: GYP_LIBTOOLFLAGS := $(LIBTOOLFLAGS_$(BUILDTYPE))
|
| 138 |
+
$(builddir)/bson.node: TOOLSET := $(TOOLSET)
|
| 139 |
+
$(builddir)/bson.node: $(OBJS) FORCE_DO_CMD
|
| 140 |
+
$(call do_cmd,solink_module)
|
| 141 |
+
|
| 142 |
+
all_deps += $(builddir)/bson.node
|
| 143 |
+
# Add target alias
|
| 144 |
+
.PHONY: bson
|
| 145 |
+
bson: $(builddir)/bson.node
|
| 146 |
+
|
| 147 |
+
# Short alias for building this executable.
|
| 148 |
+
.PHONY: bson.node
|
| 149 |
+
bson.node: $(builddir)/bson.node
|
| 150 |
+
|
| 151 |
+
# Add executable to "all" target.
|
| 152 |
+
.PHONY: all
|
| 153 |
+
all: $(builddir)/bson.node
|
| 154 |
+
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/config.gypi
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Do not edit. File was generated by node-gyp's "configure" step
|
| 2 |
+
{
|
| 3 |
+
"target_defaults": {
|
| 4 |
+
"cflags": [],
|
| 5 |
+
"default_configuration": "Release",
|
| 6 |
+
"defines": [],
|
| 7 |
+
"include_dirs": [],
|
| 8 |
+
"libraries": []
|
| 9 |
+
},
|
| 10 |
+
"variables": {
|
| 11 |
+
"clang": 1,
|
| 12 |
+
"host_arch": "x64",
|
| 13 |
+
"icu_data_file": "icudt54l.dat",
|
| 14 |
+
"icu_data_in": "../../deps/icu/source/data/in/icudt54l.dat",
|
| 15 |
+
"icu_endianness": "l",
|
| 16 |
+
"icu_gyp_path": "tools/icu/icu-generic.gyp",
|
| 17 |
+
"icu_locales": "en,root",
|
| 18 |
+
"icu_path": "./deps/icu",
|
| 19 |
+
"icu_small": "true",
|
| 20 |
+
"icu_ver_major": "54",
|
| 21 |
+
"node_install_npm": "true",
|
| 22 |
+
"node_prefix": "/",
|
| 23 |
+
"node_shared_cares": "false",
|
| 24 |
+
"node_shared_http_parser": "false",
|
| 25 |
+
"node_shared_libuv": "false",
|
| 26 |
+
"node_shared_openssl": "false",
|
| 27 |
+
"node_shared_v8": "false",
|
| 28 |
+
"node_shared_zlib": "false",
|
| 29 |
+
"node_tag": "",
|
| 30 |
+
"node_use_dtrace": "true",
|
| 31 |
+
"node_use_etw": "false",
|
| 32 |
+
"node_use_mdb": "false",
|
| 33 |
+
"node_use_openssl": "true",
|
| 34 |
+
"node_use_perfctr": "false",
|
| 35 |
+
"openssl_no_asm": 0,
|
| 36 |
+
"python": "/usr/bin/python",
|
| 37 |
+
"target_arch": "x64",
|
| 38 |
+
"uv_library": "static_library",
|
| 39 |
+
"uv_parent_path": "/deps/uv/",
|
| 40 |
+
"uv_use_dtrace": "true",
|
| 41 |
+
"v8_enable_gdbjit": 0,
|
| 42 |
+
"v8_enable_i18n_support": 1,
|
| 43 |
+
"v8_no_strict_aliasing": 1,
|
| 44 |
+
"v8_optimized_debug": 0,
|
| 45 |
+
"v8_random_seed": 0,
|
| 46 |
+
"v8_use_snapshot": "false",
|
| 47 |
+
"want_separate_host_toolset": 0,
|
| 48 |
+
"nodedir": "/Users/andrebaltieri/.node-gyp/0.12.3",
|
| 49 |
+
"copy_dev_lib": "true",
|
| 50 |
+
"standalone_static_library": 1,
|
| 51 |
+
"fallback_to_build": "true",
|
| 52 |
+
"module": "/Users/andrebaltieri/dev/5485/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release/bson.node",
|
| 53 |
+
"module_name": "bson",
|
| 54 |
+
"module_path": "/Users/andrebaltieri/dev/5485/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/build/Release",
|
| 55 |
+
"save_dev": "",
|
| 56 |
+
"viewer": "man",
|
| 57 |
+
"browser": "",
|
| 58 |
+
"rollback": "true",
|
| 59 |
+
"usage": "",
|
| 60 |
+
"globalignorefile": "/usr/local/etc/npmignore",
|
| 61 |
+
"shell": "/bin/zsh",
|
| 62 |
+
"init_author_url": "",
|
| 63 |
+
"shrinkwrap": "true",
|
| 64 |
+
"parseable": "",
|
| 65 |
+
"init_license": "ISC",
|
| 66 |
+
"if_present": "",
|
| 67 |
+
"sign_git_tag": "",
|
| 68 |
+
"init_author_email": "",
|
| 69 |
+
"cache_max": "Infinity",
|
| 70 |
+
"long": "",
|
| 71 |
+
"local_address": "",
|
| 72 |
+
"git_tag_version": "true",
|
| 73 |
+
"cert": "",
|
| 74 |
+
"registry": "https://registry.npmjs.org/",
|
| 75 |
+
"npat": "",
|
| 76 |
+
"fetch_retries": "2",
|
| 77 |
+
"versions": "",
|
| 78 |
+
"message": "%s",
|
| 79 |
+
"key": "",
|
| 80 |
+
"globalconfig": "/usr/local/etc/npmrc",
|
| 81 |
+
"always_auth": "",
|
| 82 |
+
"spin": "true",
|
| 83 |
+
"cache_lock_retries": "10",
|
| 84 |
+
"cafile": "",
|
| 85 |
+
"heading": "npm",
|
| 86 |
+
"proprietary_attribs": "true",
|
| 87 |
+
"fetch_retry_mintimeout": "10000",
|
| 88 |
+
"json": "",
|
| 89 |
+
"access": "",
|
| 90 |
+
"https_proxy": "",
|
| 91 |
+
"engine_strict": "",
|
| 92 |
+
"description": "true",
|
| 93 |
+
"userconfig": "/Users/andrebaltieri/.npmrc",
|
| 94 |
+
"init_module": "/Users/andrebaltieri/.npm-init.js",
|
| 95 |
+
"user": "501",
|
| 96 |
+
"node_version": "0.12.3",
|
| 97 |
+
"editor": "vi",
|
| 98 |
+
"save": "true",
|
| 99 |
+
"tag": "latest",
|
| 100 |
+
"global": "",
|
| 101 |
+
"optional": "true",
|
| 102 |
+
"force": "",
|
| 103 |
+
"bin_links": "true",
|
| 104 |
+
"searchopts": "",
|
| 105 |
+
"depth": "Infinity",
|
| 106 |
+
"searchsort": "name",
|
| 107 |
+
"rebuild_bundle": "true",
|
| 108 |
+
"unicode": "true",
|
| 109 |
+
"fetch_retry_maxtimeout": "60000",
|
| 110 |
+
"tag_version_prefix": "v",
|
| 111 |
+
"strict_ssl": "true",
|
| 112 |
+
"save_prefix": "^",
|
| 113 |
+
"ca": "",
|
| 114 |
+
"save_exact": "",
|
| 115 |
+
"group": "20",
|
| 116 |
+
"fetch_retry_factor": "10",
|
| 117 |
+
"dev": "",
|
| 118 |
+
"version": "",
|
| 119 |
+
"cache_lock_stale": "60000",
|
| 120 |
+
"cache_min": "10",
|
| 121 |
+
"searchexclude": "",
|
| 122 |
+
"cache": "/Users/andrebaltieri/.npm",
|
| 123 |
+
"color": "true",
|
| 124 |
+
"save_optional": "",
|
| 125 |
+
"ignore_scripts": "",
|
| 126 |
+
"user_agent": "npm/2.9.1 node/v0.12.3 darwin x64",
|
| 127 |
+
"cache_lock_wait": "10000",
|
| 128 |
+
"production": "",
|
| 129 |
+
"save_bundle": "",
|
| 130 |
+
"umask": "0022",
|
| 131 |
+
"init_version": "1.0.0",
|
| 132 |
+
"scope": "",
|
| 133 |
+
"init_author_name": "",
|
| 134 |
+
"git": "git",
|
| 135 |
+
"unsafe_perm": "",
|
| 136 |
+
"tmp": "/tmp",
|
| 137 |
+
"onload_script": "",
|
| 138 |
+
"prefix": "/usr/local",
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# Generated by gyp. Do not edit.
|
| 3 |
+
# Copyright (c) 2012 Google Inc. All rights reserved.
|
| 4 |
+
# Use of this source code is governed by a BSD-style license that can be
|
| 5 |
+
# found in the LICENSE file.
|
| 6 |
+
|
| 7 |
+
"""Utility functions to perform Xcode-style build steps.
|
| 8 |
+
|
| 9 |
+
These functions are executed via gyp-mac-tool when using the Makefile generator.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import fcntl
|
| 13 |
+
import fnmatch
|
| 14 |
+
import glob
|
| 15 |
+
import json
|
| 16 |
+
import os
|
| 17 |
+
import plistlib
|
| 18 |
+
import re
|
| 19 |
+
import shutil
|
| 20 |
+
import string
|
| 21 |
+
import subprocess
|
| 22 |
+
import sys
|
| 23 |
+
import tempfile
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main(args):
|
| 27 |
+
executor = MacTool()
|
| 28 |
+
exit_code = executor.Dispatch(args)
|
| 29 |
+
if exit_code is not None:
|
| 30 |
+
sys.exit(exit_code)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class MacTool(object):
|
| 34 |
+
"""This class performs all the Mac tooling steps. The methods can either be
|
| 35 |
+
executed directly, or dispatched from an argument list."""
|
| 36 |
+
|
| 37 |
+
def Dispatch(self, args):
|
| 38 |
+
"""Dispatches a string command to a method."""
|
| 39 |
+
if len(args) < 1:
|
| 40 |
+
raise Exception("Not enough arguments")
|
| 41 |
+
|
| 42 |
+
method = "Exec%s" % self._CommandifyName(args[0])
|
| 43 |
+
return getattr(self, method)(*args[1:])
|
| 44 |
+
|
| 45 |
+
def _CommandifyName(self, name_string):
|
| 46 |
+
"""Transforms a tool name like copy-info-plist to CopyInfoPlist"""
|
| 47 |
+
return name_string.title().replace('-', '')
|
| 48 |
+
|
| 49 |
+
def ExecCopyBundleResource(self, source, dest):
|
| 50 |
+
"""Copies a resource file to the bundle/Resources directory, performing any
|
| 51 |
+
necessary compilation on each resource."""
|
| 52 |
+
extension = os.path.splitext(source)[1].lower()
|
| 53 |
+
if os.path.isdir(source):
|
| 54 |
+
# Copy tree.
|
| 55 |
+
# TODO(thakis): This copies file attributes like mtime, while the
|
| 56 |
+
# single-file branch below doesn't. This should probably be changed to
|
| 57 |
+
# be consistent with the single-file branch.
|
| 58 |
+
if os.path.exists(dest):
|
| 59 |
+
shutil.rmtree(dest)
|
| 60 |
+
shutil.copytree(source, dest)
|
| 61 |
+
elif extension == '.xib':
|
| 62 |
+
return self._CopyXIBFile(source, dest)
|
| 63 |
+
elif extension == '.storyboard':
|
| 64 |
+
return self._CopyXIBFile(source, dest)
|
| 65 |
+
elif extension == '.strings':
|
| 66 |
+
self._CopyStringsFile(source, dest)
|
| 67 |
+
else:
|
| 68 |
+
shutil.copy(source, dest)
|
| 69 |
+
|
| 70 |
+
def _CopyXIBFile(self, source, dest):
|
| 71 |
+
"""Compiles a XIB file with ibtool into a binary plist in the bundle."""
|
| 72 |
+
|
| 73 |
+
# ibtool sometimes crashes with relative paths. See crbug.com/314728.
|
| 74 |
+
base = os.path.dirname(os.path.realpath(__file__))
|
| 75 |
+
if os.path.relpath(source):
|
| 76 |
+
source = os.path.join(base, source)
|
| 77 |
+
if os.path.relpath(dest):
|
| 78 |
+
dest = os.path.join(base, dest)
|
| 79 |
+
|
| 80 |
+
args = ['xcrun', 'ibtool', '--errors', '--warnings', '--notices',
|
| 81 |
+
'--output-format', 'human-readable-text', '--compile', dest, source]
|
| 82 |
+
ibtool_section_re = re.compile(r'/\*.*\*/')
|
| 83 |
+
ibtool_re = re.compile(r'.*note:.*is clipping its content')
|
| 84 |
+
ibtoolout = subprocess.Popen(args, stdout=subprocess.PIPE)
|
| 85 |
+
current_section_header = None
|
| 86 |
+
for line in ibtoolout.stdout:
|
| 87 |
+
if ibtool_section_re.match(line):
|
| 88 |
+
current_section_header = line
|
| 89 |
+
elif not ibtool_re.match(line):
|
| 90 |
+
if current_section_header:
|
| 91 |
+
sys.stdout.write(current_section_header)
|
| 92 |
+
current_section_header = None
|
| 93 |
+
sys.stdout.write(line)
|
| 94 |
+
return ibtoolout.returncode
|
| 95 |
+
|
| 96 |
+
def _CopyStringsFile(self, source, dest):
|
| 97 |
+
"""Copies a .strings file using iconv to reconvert the input into UTF-16."""
|
| 98 |
+
input_code = self._DetectInputEncoding(source) or "UTF-8"
|
| 99 |
+
|
| 100 |
+
# Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call
|
| 101 |
+
# CFPropertyListCreateFromXMLData() behind the scenes; at least it prints
|
| 102 |
+
# CFPropertyListCreateFromXMLData(): Old-style plist parser: missing
|
| 103 |
+
# semicolon in dictionary.
|
| 104 |
+
# on invalid files. Do the same kind of validation.
|
| 105 |
+
import CoreFoundation
|
| 106 |
+
s = open(source, 'rb').read()
|
| 107 |
+
d = CoreFoundation.CFDataCreate(None, s, len(s))
|
| 108 |
+
_, error = CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None)
|
| 109 |
+
if error:
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
fp = open(dest, 'wb')
|
| 113 |
+
fp.write(s.decode(input_code).encode('UTF-16'))
|
| 114 |
+
fp.close()
|
| 115 |
+
|
| 116 |
+
def _DetectInputEncoding(self, file_name):
|
| 117 |
+
"""Reads the first few bytes from file_name and tries to guess the text
|
| 118 |
+
encoding. Returns None as a guess if it can't detect it."""
|
| 119 |
+
fp = open(file_name, 'rb')
|
| 120 |
+
try:
|
| 121 |
+
header = fp.read(3)
|
| 122 |
+
except e:
|
| 123 |
+
fp.close()
|
| 124 |
+
return None
|
| 125 |
+
fp.close()
|
| 126 |
+
if header.startswith("\xFE\xFF"):
|
| 127 |
+
return "UTF-16"
|
| 128 |
+
elif header.startswith("\xFF\xFE"):
|
| 129 |
+
return "UTF-16"
|
| 130 |
+
elif header.startswith("\xEF\xBB\xBF"):
|
| 131 |
+
return "UTF-8"
|
| 132 |
+
else:
|
| 133 |
+
return None
|
| 134 |
+
|
| 135 |
+
def ExecCopyInfoPlist(self, source, dest, *keys):
|
| 136 |
+
"""Copies the |source| Info.plist to the destination directory |dest|."""
|
| 137 |
+
# Read the source Info.plist into memory.
|
| 138 |
+
fd = open(source, 'r')
|
| 139 |
+
lines = fd.read()
|
| 140 |
+
fd.close()
|
| 141 |
+
|
| 142 |
+
# Insert synthesized key/value pairs (e.g. BuildMachineOSBuild).
|
| 143 |
+
plist = plistlib.readPlistFromString(lines)
|
| 144 |
+
if keys:
|
| 145 |
+
plist = dict(plist.items() + json.loads(keys[0]).items())
|
| 146 |
+
lines = plistlib.writePlistToString(plist)
|
| 147 |
+
|
| 148 |
+
# Go through all the environment variables and replace them as variables in
|
| 149 |
+
# the file.
|
| 150 |
+
IDENT_RE = re.compile('[/\s]')
|
| 151 |
+
for key in os.environ:
|
| 152 |
+
if key.startswith('_'):
|
| 153 |
+
continue
|
| 154 |
+
evar = '${%s}' % key
|
| 155 |
+
evalue = os.environ[key]
|
| 156 |
+
lines = string.replace(lines, evar, evalue)
|
| 157 |
+
|
| 158 |
+
# Xcode supports various suffices on environment variables, which are
|
| 159 |
+
# all undocumented. :rfc1034identifier is used in the standard project
|
| 160 |
+
# template these days, and :identifier was used earlier. They are used to
|
| 161 |
+
# convert non-url characters into things that look like valid urls --
|
| 162 |
+
# except that the replacement character for :identifier, '_' isn't valid
|
| 163 |
+
# in a URL either -- oops, hence :rfc1034identifier was born.
|
| 164 |
+
evar = '${%s:identifier}' % key
|
| 165 |
+
evalue = IDENT_RE.sub('_', os.environ[key])
|
| 166 |
+
lines = string.replace(lines, evar, evalue)
|
| 167 |
+
|
| 168 |
+
evar = '${%s:rfc1034identifier}' % key
|
| 169 |
+
evalue = IDENT_RE.sub('-', os.environ[key])
|
| 170 |
+
lines = string.replace(lines, evar, evalue)
|
| 171 |
+
|
| 172 |
+
# Remove any keys with values that haven't been replaced.
|
| 173 |
+
lines = lines.split('\n')
|
| 174 |
+
for i in range(len(lines)):
|
| 175 |
+
if lines[i].strip().startswith("<string>${"):
|
| 176 |
+
lines[i] = None
|
| 177 |
+
lines[i - 1] = None
|
| 178 |
+
lines = '\n'.join(filter(lambda x: x is not None, lines))
|
| 179 |
+
|
| 180 |
+
# Write out the file with variables replaced.
|
| 181 |
+
fd = open(dest, 'w')
|
| 182 |
+
fd.write(lines)
|
| 183 |
+
fd.close()
|
| 184 |
+
|
| 185 |
+
# Now write out PkgInfo file now that the Info.plist file has been
|
| 186 |
+
# "compiled".
|
| 187 |
+
self._WritePkgInfo(dest)
|
| 188 |
+
|
| 189 |
+
def _WritePkgInfo(self, info_plist):
|
| 190 |
+
"""This writes the PkgInfo file from the data stored in Info.plist."""
|
| 191 |
+
plist = plistlib.readPlist(info_plist)
|
| 192 |
+
if not plist:
|
| 193 |
+
return
|
| 194 |
+
|
| 195 |
+
# Only create PkgInfo for executable types.
|
| 196 |
+
package_type = plist['CFBundlePackageType']
|
| 197 |
+
if package_type != 'APPL':
|
| 198 |
+
return
|
| 199 |
+
|
| 200 |
+
# The format of PkgInfo is eight characters, representing the bundle type
|
| 201 |
+
# and bundle signature, each four characters. If that is missing, four
|
| 202 |
+
# '?' characters are used instead.
|
| 203 |
+
signature_code = plist.get('CFBundleSignature', '????')
|
| 204 |
+
if len(signature_code) != 4: # Wrong length resets everything, too.
|
| 205 |
+
signature_code = '?' * 4
|
| 206 |
+
|
| 207 |
+
dest = os.path.join(os.path.dirname(info_plist), 'PkgInfo')
|
| 208 |
+
fp = open(dest, 'w')
|
| 209 |
+
fp.write('%s%s' % (package_type, signature_code))
|
| 210 |
+
fp.close()
|
| 211 |
+
|
| 212 |
+
def ExecFlock(self, lockfile, *cmd_list):
|
| 213 |
+
"""Emulates the most basic behavior of Linux's flock(1)."""
|
| 214 |
+
# Rely on exception handling to report errors.
|
| 215 |
+
fd = os.open(lockfile, os.O_RDONLY|os.O_NOCTTY|os.O_CREAT, 0o666)
|
| 216 |
+
fcntl.flock(fd, fcntl.LOCK_EX)
|
| 217 |
+
return subprocess.call(cmd_list)
|
| 218 |
+
|
| 219 |
+
def ExecFilterLibtool(self, *cmd_list):
|
| 220 |
+
"""Calls libtool and filters out '/path/to/libtool: file: foo.o has no
|
| 221 |
+
symbols'."""
|
| 222 |
+
libtool_re = re.compile(r'^.*libtool: file: .* has no symbols$')
|
| 223 |
+
libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE)
|
| 224 |
+
_, err = libtoolout.communicate()
|
| 225 |
+
for line in err.splitlines():
|
| 226 |
+
if not libtool_re.match(line):
|
| 227 |
+
print >>sys.stderr, line
|
| 228 |
+
return libtoolout.returncode
|
| 229 |
+
|
| 230 |
+
def ExecPackageFramework(self, framework, version):
|
| 231 |
+
"""Takes a path to Something.framework and the Current version of that and
|
| 232 |
+
sets up all the symlinks."""
|
| 233 |
+
# Find the name of the binary based on the part before the ".framework".
|
| 234 |
+
binary = os.path.basename(framework).split('.')[0]
|
| 235 |
+
|
| 236 |
+
CURRENT = 'Current'
|
| 237 |
+
RESOURCES = 'Resources'
|
| 238 |
+
VERSIONS = 'Versions'
|
| 239 |
+
|
| 240 |
+
if not os.path.exists(os.path.join(framework, VERSIONS, version, binary)):
|
| 241 |
+
# Binary-less frameworks don't seem to contain symlinks (see e.g.
|
| 242 |
+
# chromium's out/Debug/org.chromium.Chromium.manifest/ bundle).
|
| 243 |
+
return
|
| 244 |
+
|
| 245 |
+
# Move into the framework directory to set the symlinks correctly.
|
| 246 |
+
pwd = os.getcwd()
|
| 247 |
+
os.chdir(framework)
|
| 248 |
+
|
| 249 |
+
# Set up the Current version.
|
| 250 |
+
self._Relink(version, os.path.join(VERSIONS, CURRENT))
|
| 251 |
+
|
| 252 |
+
# Set up the root symlinks.
|
| 253 |
+
self._Relink(os.path.join(VERSIONS, CURRENT, binary), binary)
|
| 254 |
+
self._Relink(os.path.join(VERSIONS, CURRENT, RESOURCES), RESOURCES)
|
| 255 |
+
|
| 256 |
+
# Back to where we were before!
|
| 257 |
+
os.chdir(pwd)
|
| 258 |
+
|
| 259 |
+
def _Relink(self, dest, link):
|
| 260 |
+
"""Creates a symlink to |dest| named |link|. If |link| already exists,
|
| 261 |
+
it is overwritten."""
|
| 262 |
+
if os.path.lexists(link):
|
| 263 |
+
os.remove(link)
|
| 264 |
+
os.symlink(dest, link)
|
| 265 |
+
|
| 266 |
+
def ExecCodeSignBundle(self, key, resource_rules, entitlements, provisioning):
|
| 267 |
+
"""Code sign a bundle.
|
| 268 |
+
|
| 269 |
+
This function tries to code sign an iOS bundle, following the same
|
| 270 |
+
algorithm as Xcode:
|
| 271 |
+
1. copy ResourceRules.plist from the user or the SDK into the bundle,
|
| 272 |
+
2. pick the provisioning profile that best match the bundle identifier,
|
| 273 |
+
and copy it into the bundle as embedded.mobileprovision,
|
| 274 |
+
3. copy Entitlements.plist from user or SDK next to the bundle,
|
| 275 |
+
4. code sign the bundle.
|
| 276 |
+
"""
|
| 277 |
+
resource_rules_path = self._InstallResourceRules(resource_rules)
|
| 278 |
+
substitutions, overrides = self._InstallProvisioningProfile(
|
| 279 |
+
provisioning, self._GetCFBundleIdentifier())
|
| 280 |
+
entitlements_path = self._InstallEntitlements(
|
| 281 |
+
entitlements, substitutions, overrides)
|
| 282 |
+
subprocess.check_call([
|
| 283 |
+
'codesign', '--force', '--sign', key, '--resource-rules',
|
| 284 |
+
resource_rules_path, '--entitlements', entitlements_path,
|
| 285 |
+
os.path.join(
|
| 286 |
+
os.environ['TARGET_BUILD_DIR'],
|
| 287 |
+
os.environ['FULL_PRODUCT_NAME'])])
|
| 288 |
+
|
| 289 |
+
def _InstallResourceRules(self, resource_rules):
|
| 290 |
+
"""Installs ResourceRules.plist from user or SDK into the bundle.
|
| 291 |
+
|
| 292 |
+
Args:
|
| 293 |
+
resource_rules: string, optional, path to the ResourceRules.plist file
|
| 294 |
+
to use, default to "${SDKROOT}/ResourceRules.plist"
|
| 295 |
+
|
| 296 |
+
Returns:
|
| 297 |
+
Path to the copy of ResourceRules.plist into the bundle.
|
| 298 |
+
"""
|
| 299 |
+
source_path = resource_rules
|
| 300 |
+
target_path = os.path.join(
|
| 301 |
+
os.environ['BUILT_PRODUCTS_DIR'],
|
| 302 |
+
os.environ['CONTENTS_FOLDER_PATH'],
|
| 303 |
+
'ResourceRules.plist')
|
| 304 |
+
if not source_path:
|
| 305 |
+
source_path = os.path.join(
|
| 306 |
+
os.environ['SDKROOT'], 'ResourceRules.plist')
|
| 307 |
+
shutil.copy2(source_path, target_path)
|
| 308 |
+
return target_path
|
| 309 |
+
|
| 310 |
+
def _InstallProvisioningProfile(self, profile, bundle_identifier):
|
| 311 |
+
"""Installs embedded.mobileprovision into the bundle.
|
| 312 |
+
|
| 313 |
+
Args:
|
| 314 |
+
profile: string, optional, short name of the .mobileprovision file
|
| 315 |
+
to use, if empty or the file is missing, the best file installed
|
| 316 |
+
will be used
|
| 317 |
+
bundle_identifier: string, value of CFBundleIdentifier from Info.plist
|
| 318 |
+
|
| 319 |
+
Returns:
|
| 320 |
+
A tuple containing two dictionary: variables substitutions and values
|
| 321 |
+
to overrides when generating the entitlements file.
|
| 322 |
+
"""
|
| 323 |
+
source_path, provisioning_data, team_id = self._FindProvisioningProfile(
|
| 324 |
+
profile, bundle_identifier)
|
| 325 |
+
target_path = os.path.join(
|
| 326 |
+
os.environ['BUILT_PRODUCTS_DIR'],
|
| 327 |
+
os.environ['CONTENTS_FOLDER_PATH'],
|
| 328 |
+
'embedded.mobileprovision')
|
| 329 |
+
shutil.copy2(source_path, target_path)
|
| 330 |
+
substitutions = self._GetSubstitutions(bundle_identifier, team_id + '.')
|
| 331 |
+
return substitutions, provisioning_data['Entitlements']
|
| 332 |
+
|
| 333 |
+
def _FindProvisioningProfile(self, profile, bundle_identifier):
|
| 334 |
+
"""Finds the .mobileprovision file to use for signing the bundle.
|
| 335 |
+
|
| 336 |
+
Checks all the installed provisioning profiles (or if the user specified
|
| 337 |
+
the PROVISIONING_PROFILE variable, only consult it) and select the most
|
| 338 |
+
specific that correspond to the bundle identifier.
|
| 339 |
+
|
| 340 |
+
Args:
|
| 341 |
+
profile: string, optional, short name of the .mobileprovision file
|
| 342 |
+
to use, if empty or the file is missing, the best file installed
|
| 343 |
+
will be used
|
| 344 |
+
bundle_identifier: string, value of CFBundleIdentifier from Info.plist
|
| 345 |
+
|
| 346 |
+
Returns:
|
| 347 |
+
A tuple of the path to the selected provisioning profile, the data of
|
| 348 |
+
the embedded plist in the provisioning profile and the team identifier
|
| 349 |
+
to use for code signing.
|
| 350 |
+
|
| 351 |
+
Raises:
|
| 352 |
+
SystemExit: if no .mobileprovision can be used to sign the bundle.
|
| 353 |
+
"""
|
| 354 |
+
profiles_dir = os.path.join(
|
| 355 |
+
os.environ['HOME'], 'Library', 'MobileDevice', 'Provisioning Profiles')
|
| 356 |
+
if not os.path.isdir(profiles_dir):
|
| 357 |
+
print >>sys.stderr, (
|
| 358 |
+
'cannot find mobile provisioning for %s' % bundle_identifier)
|
| 359 |
+
sys.exit(1)
|
| 360 |
+
provisioning_profiles = None
|
| 361 |
+
if profile:
|
| 362 |
+
profile_path = os.path.join(profiles_dir, profile + '.mobileprovision')
|
| 363 |
+
if os.path.exists(profile_path):
|
| 364 |
+
provisioning_profiles = [profile_path]
|
| 365 |
+
if not provisioning_profiles:
|
| 366 |
+
provisioning_profiles = glob.glob(
|
| 367 |
+
os.path.join(profiles_dir, '*.mobileprovision'))
|
| 368 |
+
valid_provisioning_profiles = {}
|
| 369 |
+
for profile_path in provisioning_profiles:
|
| 370 |
+
profile_data = self._LoadProvisioningProfile(profile_path)
|
| 371 |
+
app_id_pattern = profile_data.get(
|
| 372 |
+
'Entitlements', {}).get('application-identifier', '')
|
| 373 |
+
for team_identifier in profile_data.get('TeamIdentifier', []):
|
| 374 |
+
app_id = '%s.%s' % (team_identifier, bundle_identifier)
|
| 375 |
+
if fnmatch.fnmatch(app_id, app_id_pattern):
|
| 376 |
+
valid_provisioning_profiles[app_id_pattern] = (
|
| 377 |
+
profile_path, profile_data, team_identifier)
|
| 378 |
+
if not valid_provisioning_profiles:
|
| 379 |
+
print >>sys.stderr, (
|
| 380 |
+
'cannot find mobile provisioning for %s' % bundle_identifier)
|
| 381 |
+
sys.exit(1)
|
| 382 |
+
# If the user has multiple provisioning profiles installed that can be
|
| 383 |
+
# used for ${bundle_identifier}, pick the most specific one (ie. the
|
| 384 |
+
# provisioning profile whose pattern is the longest).
|
| 385 |
+
selected_key = max(valid_provisioning_profiles, key=lambda v: len(v))
|
| 386 |
+
return valid_provisioning_profiles[selected_key]
|
| 387 |
+
|
| 388 |
+
def _LoadProvisioningProfile(self, profile_path):
|
| 389 |
+
"""Extracts the plist embedded in a provisioning profile.
|
| 390 |
+
|
| 391 |
+
Args:
|
| 392 |
+
profile_path: string, path to the .mobileprovision file
|
| 393 |
+
|
| 394 |
+
Returns:
|
| 395 |
+
Content of the plist embedded in the provisioning profile as a dictionary.
|
| 396 |
+
"""
|
| 397 |
+
with tempfile.NamedTemporaryFile() as temp:
|
| 398 |
+
subprocess.check_call([
|
| 399 |
+
'security', 'cms', '-D', '-i', profile_path, '-o', temp.name])
|
| 400 |
+
return self._LoadPlistMaybeBinary(temp.name)
|
| 401 |
+
|
| 402 |
+
def _LoadPlistMaybeBinary(self, plist_path):
|
| 403 |
+
"""Loads into a memory a plist possibly encoded in binary format.
|
| 404 |
+
|
| 405 |
+
This is a wrapper around plistlib.readPlist that tries to convert the
|
| 406 |
+
plist to the XML format if it can't be parsed (assuming that it is in
|
| 407 |
+
the binary format).
|
| 408 |
+
|
| 409 |
+
Args:
|
| 410 |
+
plist_path: string, path to a plist file, in XML or binary format
|
| 411 |
+
|
| 412 |
+
Returns:
|
| 413 |
+
Content of the plist as a dictionary.
|
| 414 |
+
"""
|
| 415 |
+
try:
|
| 416 |
+
# First, try to read the file using plistlib that only supports XML,
|
| 417 |
+
# and if an exception is raised, convert a temporary copy to XML and
|
| 418 |
+
# load that copy.
|
| 419 |
+
return plistlib.readPlist(plist_path)
|
| 420 |
+
except:
|
| 421 |
+
pass
|
| 422 |
+
with tempfile.NamedTemporaryFile() as temp:
|
| 423 |
+
shutil.copy2(plist_path, temp.name)
|
| 424 |
+
subprocess.check_call(['plutil', '-convert', 'xml1', temp.name])
|
| 425 |
+
return plistlib.readPlist(temp.name)
|
| 426 |
+
|
| 427 |
+
def _GetSubstitutions(self, bundle_identifier, app_identifier_prefix):
|
| 428 |
+
"""Constructs a dictionary of variable substitutions for Entitlements.plist.
|
| 429 |
+
|
| 430 |
+
Args:
|
| 431 |
+
bundle_identifier: string, value of CFBundleIdentifier from Info.plist
|
| 432 |
+
app_identifier_prefix: string, value for AppIdentifierPrefix
|
| 433 |
+
|
| 434 |
+
Returns:
|
| 435 |
+
Dictionary of substitutions to apply when generating Entitlements.plist.
|
| 436 |
+
"""
|
| 437 |
+
return {
|
| 438 |
+
'CFBundleIdentifier': bundle_identifier,
|
| 439 |
+
'AppIdentifierPrefix': app_identifier_prefix,
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
def _GetCFBundleIdentifier(self):
|
| 443 |
+
"""Extracts CFBundleIdentifier value from Info.plist in the bundle.
|
| 444 |
+
|
| 445 |
+
Returns:
|
| 446 |
+
Value of CFBundleIdentifier in the Info.plist located in the bundle.
|
| 447 |
+
"""
|
| 448 |
+
info_plist_path = os.path.join(
|
| 449 |
+
os.environ['TARGET_BUILD_DIR'],
|
| 450 |
+
os.environ['INFOPLIST_PATH'])
|
| 451 |
+
info_plist_data = self._LoadPlistMaybeBinary(info_plist_path)
|
| 452 |
+
return info_plist_data['CFBundleIdentifier']
|
| 453 |
+
|
| 454 |
+
def _InstallEntitlements(self, entitlements, substitutions, overrides):
|
| 455 |
+
"""Generates and install the ${BundleName}.xcent entitlements file.
|
| 456 |
+
|
| 457 |
+
Expands variables "$(variable)" pattern in the source entitlements file,
|
| 458 |
+
add extra entitlements defined in the .mobileprovision file and the copy
|
| 459 |
+
the generated plist to "${BundlePath}.xcent".
|
| 460 |
+
|
| 461 |
+
Args:
|
| 462 |
+
entitlements: string, optional, path to the Entitlements.plist template
|
| 463 |
+
to use, defaults to "${SDKROOT}/Entitlements.plist"
|
| 464 |
+
substitutions: dictionary, variable substitutions
|
| 465 |
+
overrides: dictionary, values to add to the entitlements
|
| 466 |
+
|
| 467 |
+
Returns:
|
| 468 |
+
Path to the generated entitlements file.
|
| 469 |
+
"""
|
| 470 |
+
source_path = entitlements
|
| 471 |
+
target_path = os.path.join(
|
| 472 |
+
os.environ['BUILT_PRODUCTS_DIR'],
|
| 473 |
+
os.environ['PRODUCT_NAME'] + '.xcent')
|
| 474 |
+
if not source_path:
|
| 475 |
+
source_path = os.path.join(
|
| 476 |
+
os.environ['SDKROOT'],
|
| 477 |
+
'Entitlements.plist')
|
| 478 |
+
shutil.copy2(source_path, target_path)
|
| 479 |
+
data = self._LoadPlistMaybeBinary(target_path)
|
| 480 |
+
data = self._ExpandVariables(data, substitutions)
|
| 481 |
+
if overrides:
|
| 482 |
+
for key in overrides:
|
| 483 |
+
if key not in data:
|
| 484 |
+
data[key] = overrides[key]
|
| 485 |
+
plistlib.writePlist(data, target_path)
|
| 486 |
+
return target_path
|
| 487 |
+
|
| 488 |
+
def _ExpandVariables(self, data, substitutions):
|
| 489 |
+
"""Expands variables "$(variable)" in data.
|
| 490 |
+
|
| 491 |
+
Args:
|
| 492 |
+
data: object, can be either string, list or dictionary
|
| 493 |
+
substitutions: dictionary, variable substitutions to perform
|
| 494 |
+
|
| 495 |
+
Returns:
|
| 496 |
+
Copy of data where each references to "$(variable)" has been replaced
|
| 497 |
+
by the corresponding value found in substitutions, or left intact if
|
| 498 |
+
the key was not found.
|
| 499 |
+
"""
|
| 500 |
+
if isinstance(data, str):
|
| 501 |
+
for key, value in substitutions.iteritems():
|
| 502 |
+
data = data.replace('$(%s)' % key, value)
|
| 503 |
+
return data
|
| 504 |
+
if isinstance(data, list):
|
| 505 |
+
return [self._ExpandVariables(v, substitutions) for v in data]
|
| 506 |
+
if isinstance(data, dict):
|
| 507 |
+
return dict((k, self._ExpandVariables(data[k],
|
| 508 |
+
substitutions)) for k in data)
|
| 509 |
+
return data
|
| 510 |
+
|
| 511 |
+
if __name__ == '__main__':
|
| 512 |
+
sys.exit(main(sys.argv[1:]))
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/.npmignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
build/
|
| 2 |
+
./bson.node
|
| 3 |
+
.lock-wscript
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/Makefile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
NODE = node
|
| 2 |
+
name = all
|
| 3 |
+
JOBS = 1
|
| 4 |
+
|
| 5 |
+
all:
|
| 6 |
+
rm -rf build .lock-wscript bson.node
|
| 7 |
+
node-waf configure build
|
| 8 |
+
cp -R ./build/Release/bson.node . || true
|
| 9 |
+
|
| 10 |
+
all_debug:
|
| 11 |
+
rm -rf build .lock-wscript bson.node
|
| 12 |
+
node-waf --debug configure build
|
| 13 |
+
cp -R ./build/Release/bson.node . || true
|
| 14 |
+
|
| 15 |
+
clang:
|
| 16 |
+
rm -rf build .lock-wscript bson.node
|
| 17 |
+
CXX=clang node-waf configure build
|
| 18 |
+
cp -R ./build/Release/bson.node . || true
|
| 19 |
+
|
| 20 |
+
clang_debug:
|
| 21 |
+
rm -rf build .lock-wscript bson.node
|
| 22 |
+
CXX=clang node-waf --debug configure build
|
| 23 |
+
cp -R ./build/Release/bson.node . || true
|
| 24 |
+
|
| 25 |
+
clean:
|
| 26 |
+
rm -rf build .lock-wscript bson.node
|
| 27 |
+
|
| 28 |
+
.PHONY: all
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/bson.cc
ADDED
|
@@ -0,0 +1,1072 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//===========================================================================
|
| 2 |
+
|
| 3 |
+
#include <stdarg.h>
|
| 4 |
+
#include <cstdlib>
|
| 5 |
+
#include <cstring>
|
| 6 |
+
#include <string.h>
|
| 7 |
+
#include <stdlib.h>
|
| 8 |
+
|
| 9 |
+
#ifdef __clang__
|
| 10 |
+
#pragma clang diagnostic push
|
| 11 |
+
#pragma clang diagnostic ignored "-Wunused-parameter"
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
#include <v8.h>
|
| 15 |
+
|
| 16 |
+
// this and the above block must be around the v8.h header otherwise
|
| 17 |
+
// v8 is not happy
|
| 18 |
+
#ifdef __clang__
|
| 19 |
+
#pragma clang diagnostic pop
|
| 20 |
+
#endif
|
| 21 |
+
|
| 22 |
+
#include <node.h>
|
| 23 |
+
#include <node_version.h>
|
| 24 |
+
#include <node_buffer.h>
|
| 25 |
+
|
| 26 |
+
#include <cmath>
|
| 27 |
+
#include <iostream>
|
| 28 |
+
#include <limits>
|
| 29 |
+
#include <vector>
|
| 30 |
+
#include <errno.h>
|
| 31 |
+
|
| 32 |
+
#if defined(__sun) || defined(_AIX)
|
| 33 |
+
#include <alloca.h>
|
| 34 |
+
#endif
|
| 35 |
+
|
| 36 |
+
#include "bson.h"
|
| 37 |
+
|
| 38 |
+
using namespace v8;
|
| 39 |
+
using namespace node;
|
| 40 |
+
|
| 41 |
+
void die(const char *message) {
|
| 42 |
+
if(errno) {
|
| 43 |
+
perror(message);
|
| 44 |
+
} else {
|
| 45 |
+
printf("ERROR: %s\n", message);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
exit(1);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
//===========================================================================
|
| 52 |
+
|
| 53 |
+
void DataStream::WriteObjectId(const Handle<Object>& object, const Handle<String>& key)
|
| 54 |
+
{
|
| 55 |
+
uint16_t buffer[12];
|
| 56 |
+
object->Get(key)->ToString()->Write(buffer, 0, 12);
|
| 57 |
+
for(uint32_t i = 0; i < 12; ++i)
|
| 58 |
+
{
|
| 59 |
+
*p++ = (char) buffer[i];
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
void ThrowAllocatedStringException(size_t allocationSize, const char* format, ...)
|
| 64 |
+
{
|
| 65 |
+
va_list args;
|
| 66 |
+
va_start(args, format);
|
| 67 |
+
char* string = (char*) malloc(allocationSize);
|
| 68 |
+
if(string == NULL) die("Failed to allocate ThrowAllocatedStringException");
|
| 69 |
+
vsprintf(string, format, args);
|
| 70 |
+
va_end(args);
|
| 71 |
+
throw string;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
void DataStream::CheckKey(const Local<String>& keyName)
|
| 75 |
+
{
|
| 76 |
+
size_t keyLength = keyName->Utf8Length();
|
| 77 |
+
if(keyLength == 0) return;
|
| 78 |
+
|
| 79 |
+
// Allocate space for the key, do not need to zero terminate as WriteUtf8 does it
|
| 80 |
+
char* keyStringBuffer = (char*) alloca(keyLength + 1);
|
| 81 |
+
// Write the key to the allocated buffer
|
| 82 |
+
keyName->WriteUtf8(keyStringBuffer);
|
| 83 |
+
// Check for the zero terminator
|
| 84 |
+
char* terminator = strchr(keyStringBuffer, 0x00);
|
| 85 |
+
|
| 86 |
+
// If the location is not at the end of the string we've got an illegal 0x00 byte somewhere
|
| 87 |
+
if(terminator != &keyStringBuffer[keyLength]) {
|
| 88 |
+
ThrowAllocatedStringException(64+keyLength, "key %s must not contain null bytes", keyStringBuffer);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if(keyStringBuffer[0] == '$')
|
| 92 |
+
{
|
| 93 |
+
ThrowAllocatedStringException(64+keyLength, "key %s must not start with '$'", keyStringBuffer);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
if(strchr(keyStringBuffer, '.') != NULL)
|
| 97 |
+
{
|
| 98 |
+
ThrowAllocatedStringException(64+keyLength, "key %s must not contain '.'", keyStringBuffer);
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
template<typename T> void BSONSerializer<T>::SerializeDocument(const Handle<Value>& value)
|
| 103 |
+
{
|
| 104 |
+
void* documentSize = this->BeginWriteSize();
|
| 105 |
+
Local<Object> object = bson->GetSerializeObject(value);
|
| 106 |
+
|
| 107 |
+
// Get the object property names
|
| 108 |
+
Local<Array> propertyNames = object->GetPropertyNames();
|
| 109 |
+
|
| 110 |
+
// Length of the property
|
| 111 |
+
int propertyLength = propertyNames->Length();
|
| 112 |
+
for(int i = 0; i < propertyLength; ++i)
|
| 113 |
+
{
|
| 114 |
+
const Local<String>& propertyName = propertyNames->Get(i)->ToString();
|
| 115 |
+
if(checkKeys) this->CheckKey(propertyName);
|
| 116 |
+
|
| 117 |
+
const Local<Value>& propertyValue = object->Get(propertyName);
|
| 118 |
+
|
| 119 |
+
if(serializeFunctions || !propertyValue->IsFunction())
|
| 120 |
+
{
|
| 121 |
+
void* typeLocation = this->BeginWriteType();
|
| 122 |
+
this->WriteString(propertyName);
|
| 123 |
+
SerializeValue(typeLocation, propertyValue);
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
this->WriteByte(0);
|
| 128 |
+
this->CommitSize(documentSize);
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
template<typename T> void BSONSerializer<T>::SerializeArray(const Handle<Value>& value)
|
| 132 |
+
{
|
| 133 |
+
void* documentSize = this->BeginWriteSize();
|
| 134 |
+
|
| 135 |
+
Local<Array> array = Local<Array>::Cast(value->ToObject());
|
| 136 |
+
uint32_t arrayLength = array->Length();
|
| 137 |
+
|
| 138 |
+
for(uint32_t i = 0; i < arrayLength; ++i)
|
| 139 |
+
{
|
| 140 |
+
void* typeLocation = this->BeginWriteType();
|
| 141 |
+
this->WriteUInt32String(i);
|
| 142 |
+
SerializeValue(typeLocation, array->Get(i));
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
this->WriteByte(0);
|
| 146 |
+
this->CommitSize(documentSize);
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// This is templated so that we can use this function to both count the number of bytes, and to serialize those bytes.
|
| 150 |
+
// The template approach eliminates almost all of the inspection of values unless they're required (eg. string lengths)
|
| 151 |
+
// and ensures that there is always consistency between bytes counted and bytes written by design.
|
| 152 |
+
template<typename T> void BSONSerializer<T>::SerializeValue(void* typeLocation, const Handle<Value> constValue)
|
| 153 |
+
{
|
| 154 |
+
// Turn into local value
|
| 155 |
+
Local<Value> value = NanNew<Value>(constValue);
|
| 156 |
+
|
| 157 |
+
// Check for toBSON function
|
| 158 |
+
if(value->IsObject()) {
|
| 159 |
+
Local<Object> object = value->ToObject();
|
| 160 |
+
|
| 161 |
+
// NanNew<String>("toBSON")
|
| 162 |
+
// NanNew(BSON::_toBSONString)
|
| 163 |
+
|
| 164 |
+
if(object->Has(NanNew<String>("toBSON"))) {
|
| 165 |
+
const Local<Value>& toBSON = object->Get(NanNew<String>("toBSON"));
|
| 166 |
+
if(!toBSON->IsFunction()) ThrowAllocatedStringException(64, "toBSON is not a function");
|
| 167 |
+
value = Local<Function>::Cast(toBSON)->Call(object, 0, NULL);
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
// Process all the values
|
| 172 |
+
if(value->IsNumber())
|
| 173 |
+
{
|
| 174 |
+
double doubleValue = value->NumberValue();
|
| 175 |
+
int intValue = (int) doubleValue;
|
| 176 |
+
if(intValue == doubleValue)
|
| 177 |
+
{
|
| 178 |
+
this->CommitType(typeLocation, BSON_TYPE_INT);
|
| 179 |
+
this->WriteInt32(intValue);
|
| 180 |
+
}
|
| 181 |
+
else
|
| 182 |
+
{
|
| 183 |
+
this->CommitType(typeLocation, BSON_TYPE_NUMBER);
|
| 184 |
+
this->WriteDouble(doubleValue);
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
else if(value->IsString())
|
| 188 |
+
{
|
| 189 |
+
this->CommitType(typeLocation, BSON_TYPE_STRING);
|
| 190 |
+
this->WriteLengthPrefixedString(value->ToString());
|
| 191 |
+
}
|
| 192 |
+
else if(value->IsBoolean())
|
| 193 |
+
{
|
| 194 |
+
this->CommitType(typeLocation, BSON_TYPE_BOOLEAN);
|
| 195 |
+
this->WriteBool(value);
|
| 196 |
+
}
|
| 197 |
+
else if(value->IsArray())
|
| 198 |
+
{
|
| 199 |
+
this->CommitType(typeLocation, BSON_TYPE_ARRAY);
|
| 200 |
+
SerializeArray(value);
|
| 201 |
+
}
|
| 202 |
+
else if(value->IsDate())
|
| 203 |
+
{
|
| 204 |
+
this->CommitType(typeLocation, BSON_TYPE_DATE);
|
| 205 |
+
this->WriteInt64(value);
|
| 206 |
+
}
|
| 207 |
+
else if(value->IsRegExp())
|
| 208 |
+
{
|
| 209 |
+
this->CommitType(typeLocation, BSON_TYPE_REGEXP);
|
| 210 |
+
const Handle<RegExp>& regExp = Handle<RegExp>::Cast(value);
|
| 211 |
+
|
| 212 |
+
this->WriteString(regExp->GetSource());
|
| 213 |
+
|
| 214 |
+
int flags = regExp->GetFlags();
|
| 215 |
+
if(flags & RegExp::kGlobal) this->WriteByte('s');
|
| 216 |
+
if(flags & RegExp::kIgnoreCase) this->WriteByte('i');
|
| 217 |
+
if(flags & RegExp::kMultiline) this->WriteByte('m');
|
| 218 |
+
this->WriteByte(0);
|
| 219 |
+
}
|
| 220 |
+
else if(value->IsFunction())
|
| 221 |
+
{
|
| 222 |
+
this->CommitType(typeLocation, BSON_TYPE_CODE);
|
| 223 |
+
this->WriteLengthPrefixedString(value->ToString());
|
| 224 |
+
}
|
| 225 |
+
else if(value->IsObject())
|
| 226 |
+
{
|
| 227 |
+
const Local<Object>& object = value->ToObject();
|
| 228 |
+
if(object->Has(NanNew(bson->_bsontypeString)))
|
| 229 |
+
{
|
| 230 |
+
const Local<String>& constructorString = object->Get(NanNew(bson->_bsontypeString))->ToString();
|
| 231 |
+
if(NanNew(bson->longString)->StrictEquals(constructorString))
|
| 232 |
+
{
|
| 233 |
+
this->CommitType(typeLocation, BSON_TYPE_LONG);
|
| 234 |
+
this->WriteInt32(object, NanNew(bson->_longLowString));
|
| 235 |
+
this->WriteInt32(object, NanNew(bson->_longHighString));
|
| 236 |
+
}
|
| 237 |
+
else if(NanNew(bson->timestampString)->StrictEquals(constructorString))
|
| 238 |
+
{
|
| 239 |
+
this->CommitType(typeLocation, BSON_TYPE_TIMESTAMP);
|
| 240 |
+
this->WriteInt32(object, NanNew(bson->_longLowString));
|
| 241 |
+
this->WriteInt32(object, NanNew(bson->_longHighString));
|
| 242 |
+
}
|
| 243 |
+
else if(NanNew(bson->objectIDString)->StrictEquals(constructorString))
|
| 244 |
+
{
|
| 245 |
+
this->CommitType(typeLocation, BSON_TYPE_OID);
|
| 246 |
+
this->WriteObjectId(object, NanNew(bson->_objectIDidString));
|
| 247 |
+
}
|
| 248 |
+
else if(NanNew(bson->binaryString)->StrictEquals(constructorString))
|
| 249 |
+
{
|
| 250 |
+
this->CommitType(typeLocation, BSON_TYPE_BINARY);
|
| 251 |
+
|
| 252 |
+
uint32_t length = object->Get(NanNew(bson->_binaryPositionString))->Uint32Value();
|
| 253 |
+
Local<Object> bufferObj = object->Get(NanNew(bson->_binaryBufferString))->ToObject();
|
| 254 |
+
|
| 255 |
+
this->WriteInt32(length);
|
| 256 |
+
this->WriteByte(object, NanNew(bson->_binarySubTypeString)); // write subtype
|
| 257 |
+
// If type 0x02 write the array length aswell
|
| 258 |
+
if(object->Get(NanNew(bson->_binarySubTypeString))->Int32Value() == 0x02) {
|
| 259 |
+
this->WriteInt32(length);
|
| 260 |
+
}
|
| 261 |
+
// Write the actual data
|
| 262 |
+
this->WriteData(Buffer::Data(bufferObj), length);
|
| 263 |
+
}
|
| 264 |
+
else if(NanNew(bson->doubleString)->StrictEquals(constructorString))
|
| 265 |
+
{
|
| 266 |
+
this->CommitType(typeLocation, BSON_TYPE_NUMBER);
|
| 267 |
+
this->WriteDouble(object, NanNew(bson->_doubleValueString));
|
| 268 |
+
}
|
| 269 |
+
else if(NanNew(bson->symbolString)->StrictEquals(constructorString))
|
| 270 |
+
{
|
| 271 |
+
this->CommitType(typeLocation, BSON_TYPE_SYMBOL);
|
| 272 |
+
this->WriteLengthPrefixedString(object->Get(NanNew(bson->_symbolValueString))->ToString());
|
| 273 |
+
}
|
| 274 |
+
else if(NanNew(bson->codeString)->StrictEquals(constructorString))
|
| 275 |
+
{
|
| 276 |
+
const Local<String>& function = object->Get(NanNew(bson->_codeCodeString))->ToString();
|
| 277 |
+
const Local<Object>& scope = object->Get(NanNew(bson->_codeScopeString))->ToObject();
|
| 278 |
+
|
| 279 |
+
// For Node < 0.6.X use the GetPropertyNames
|
| 280 |
+
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6
|
| 281 |
+
uint32_t propertyNameLength = scope->GetPropertyNames()->Length();
|
| 282 |
+
#else
|
| 283 |
+
uint32_t propertyNameLength = scope->GetOwnPropertyNames()->Length();
|
| 284 |
+
#endif
|
| 285 |
+
|
| 286 |
+
if(propertyNameLength > 0)
|
| 287 |
+
{
|
| 288 |
+
this->CommitType(typeLocation, BSON_TYPE_CODE_W_SCOPE);
|
| 289 |
+
void* codeWidthScopeSize = this->BeginWriteSize();
|
| 290 |
+
this->WriteLengthPrefixedString(function->ToString());
|
| 291 |
+
SerializeDocument(scope);
|
| 292 |
+
this->CommitSize(codeWidthScopeSize);
|
| 293 |
+
}
|
| 294 |
+
else
|
| 295 |
+
{
|
| 296 |
+
this->CommitType(typeLocation, BSON_TYPE_CODE);
|
| 297 |
+
this->WriteLengthPrefixedString(function->ToString());
|
| 298 |
+
}
|
| 299 |
+
}
|
| 300 |
+
else if(NanNew(bson->dbrefString)->StrictEquals(constructorString))
|
| 301 |
+
{
|
| 302 |
+
this->CommitType(typeLocation, BSON_TYPE_OBJECT);
|
| 303 |
+
|
| 304 |
+
void* dbRefSize = this->BeginWriteSize();
|
| 305 |
+
|
| 306 |
+
void* refType = this->BeginWriteType();
|
| 307 |
+
this->WriteData("$ref", 5);
|
| 308 |
+
SerializeValue(refType, object->Get(NanNew(bson->_dbRefNamespaceString)));
|
| 309 |
+
|
| 310 |
+
void* idType = this->BeginWriteType();
|
| 311 |
+
this->WriteData("$id", 4);
|
| 312 |
+
SerializeValue(idType, object->Get(NanNew(bson->_dbRefOidString)));
|
| 313 |
+
|
| 314 |
+
const Local<Value>& refDbValue = object->Get(NanNew(bson->_dbRefDbString));
|
| 315 |
+
if(!refDbValue->IsUndefined())
|
| 316 |
+
{
|
| 317 |
+
void* dbType = this->BeginWriteType();
|
| 318 |
+
this->WriteData("$db", 4);
|
| 319 |
+
SerializeValue(dbType, refDbValue);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
this->WriteByte(0);
|
| 323 |
+
this->CommitSize(dbRefSize);
|
| 324 |
+
}
|
| 325 |
+
else if(NanNew(bson->minKeyString)->StrictEquals(constructorString))
|
| 326 |
+
{
|
| 327 |
+
this->CommitType(typeLocation, BSON_TYPE_MIN_KEY);
|
| 328 |
+
}
|
| 329 |
+
else if(NanNew(bson->maxKeyString)->StrictEquals(constructorString))
|
| 330 |
+
{
|
| 331 |
+
this->CommitType(typeLocation, BSON_TYPE_MAX_KEY);
|
| 332 |
+
}
|
| 333 |
+
}
|
| 334 |
+
else if(Buffer::HasInstance(value))
|
| 335 |
+
{
|
| 336 |
+
this->CommitType(typeLocation, BSON_TYPE_BINARY);
|
| 337 |
+
|
| 338 |
+
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3
|
| 339 |
+
Local<Object> buffer = ObjectWrap::Unwrap<Buffer>(value->ToObject());
|
| 340 |
+
uint32_t length = object->length();
|
| 341 |
+
#else
|
| 342 |
+
uint32_t length = Buffer::Length(value->ToObject());
|
| 343 |
+
#endif
|
| 344 |
+
|
| 345 |
+
this->WriteInt32(length);
|
| 346 |
+
this->WriteByte(0);
|
| 347 |
+
this->WriteData(Buffer::Data(value->ToObject()), length);
|
| 348 |
+
}
|
| 349 |
+
else
|
| 350 |
+
{
|
| 351 |
+
this->CommitType(typeLocation, BSON_TYPE_OBJECT);
|
| 352 |
+
SerializeDocument(value);
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
else if(value->IsNull() || value->IsUndefined())
|
| 356 |
+
{
|
| 357 |
+
this->CommitType(typeLocation, BSON_TYPE_NULL);
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
// Data points to start of element list, length is length of entire document including '\0' but excluding initial size
|
| 362 |
+
BSONDeserializer::BSONDeserializer(BSON* aBson, char* data, size_t length)
|
| 363 |
+
: bson(aBson),
|
| 364 |
+
pStart(data),
|
| 365 |
+
p(data),
|
| 366 |
+
pEnd(data + length - 1)
|
| 367 |
+
{
|
| 368 |
+
if(*pEnd != '\0') ThrowAllocatedStringException(64, "Missing end of document marker '\\0'");
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
BSONDeserializer::BSONDeserializer(BSONDeserializer& parentSerializer, size_t length)
|
| 372 |
+
: bson(parentSerializer.bson),
|
| 373 |
+
pStart(parentSerializer.p),
|
| 374 |
+
p(parentSerializer.p),
|
| 375 |
+
pEnd(parentSerializer.p + length - 1)
|
| 376 |
+
{
|
| 377 |
+
parentSerializer.p += length;
|
| 378 |
+
if(pEnd > parentSerializer.pEnd) ThrowAllocatedStringException(64, "Child document exceeds parent's bounds");
|
| 379 |
+
if(*pEnd != '\0') ThrowAllocatedStringException(64, "Missing end of document marker '\\0'");
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
Handle<Value> BSONDeserializer::ReadCString()
|
| 383 |
+
{
|
| 384 |
+
char* start = p;
|
| 385 |
+
while(*p++ && (p < pEnd)) { }
|
| 386 |
+
if(p > pEnd) {
|
| 387 |
+
return NanNull();
|
| 388 |
+
}
|
| 389 |
+
return NanNew<String>(start, (int32_t) (p-start-1) );
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
int32_t BSONDeserializer::ReadRegexOptions()
|
| 393 |
+
{
|
| 394 |
+
int32_t options = 0;
|
| 395 |
+
for(;;)
|
| 396 |
+
{
|
| 397 |
+
switch(*p++)
|
| 398 |
+
{
|
| 399 |
+
case '\0': return options;
|
| 400 |
+
case 's': options |= RegExp::kGlobal; break;
|
| 401 |
+
case 'i': options |= RegExp::kIgnoreCase; break;
|
| 402 |
+
case 'm': options |= RegExp::kMultiline; break;
|
| 403 |
+
}
|
| 404 |
+
}
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
uint32_t BSONDeserializer::ReadIntegerString()
|
| 408 |
+
{
|
| 409 |
+
uint32_t value = 0;
|
| 410 |
+
while(*p)
|
| 411 |
+
{
|
| 412 |
+
if(*p < '0' || *p > '9') ThrowAllocatedStringException(64, "Invalid key for array");
|
| 413 |
+
value = value * 10 + *p++ - '0';
|
| 414 |
+
}
|
| 415 |
+
++p;
|
| 416 |
+
return value;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
Local<String> BSONDeserializer::ReadString()
|
| 420 |
+
{
|
| 421 |
+
uint32_t length = ReadUInt32();
|
| 422 |
+
char* start = p;
|
| 423 |
+
p += length;
|
| 424 |
+
return NanNew<String>(start, length-1);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
Local<String> BSONDeserializer::ReadObjectId()
|
| 428 |
+
{
|
| 429 |
+
uint16_t objectId[12];
|
| 430 |
+
for(size_t i = 0; i < 12; ++i)
|
| 431 |
+
{
|
| 432 |
+
objectId[i] = *reinterpret_cast<unsigned char*>(p++);
|
| 433 |
+
}
|
| 434 |
+
return NanNew<String>(objectId, 12);
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
Handle<Value> BSONDeserializer::DeserializeDocument(bool promoteLongs)
|
| 438 |
+
{
|
| 439 |
+
uint32_t length = ReadUInt32();
|
| 440 |
+
if(length < 5) ThrowAllocatedStringException(64, "Bad BSON: Document is less than 5 bytes");
|
| 441 |
+
|
| 442 |
+
BSONDeserializer documentDeserializer(*this, length-4);
|
| 443 |
+
return documentDeserializer.DeserializeDocumentInternal(promoteLongs);
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
Handle<Value> BSONDeserializer::DeserializeDocumentInternal(bool promoteLongs)
|
| 447 |
+
{
|
| 448 |
+
Local<Object> returnObject = NanNew<Object>();
|
| 449 |
+
|
| 450 |
+
while(HasMoreData())
|
| 451 |
+
{
|
| 452 |
+
BsonType type = (BsonType) ReadByte();
|
| 453 |
+
const Handle<Value>& name = ReadCString();
|
| 454 |
+
if(name->IsNull()) ThrowAllocatedStringException(64, "Bad BSON Document: illegal CString");
|
| 455 |
+
// name->Is
|
| 456 |
+
const Handle<Value>& value = DeserializeValue(type, promoteLongs);
|
| 457 |
+
returnObject->ForceSet(name, value);
|
| 458 |
+
}
|
| 459 |
+
if(p != pEnd) ThrowAllocatedStringException(64, "Bad BSON Document: Serialize consumed unexpected number of bytes");
|
| 460 |
+
|
| 461 |
+
// From JavaScript:
|
| 462 |
+
// if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
|
| 463 |
+
if(returnObject->Has(NanNew(bson->_dbRefIdRefString)))
|
| 464 |
+
{
|
| 465 |
+
Local<Value> argv[] = { returnObject->Get(NanNew(bson->_dbRefRefString)), returnObject->Get(NanNew(bson->_dbRefIdRefString)), returnObject->Get(NanNew(bson->_dbRefDbRefString)) };
|
| 466 |
+
return NanNew(bson->dbrefConstructor)->NewInstance(3, argv);
|
| 467 |
+
}
|
| 468 |
+
else
|
| 469 |
+
{
|
| 470 |
+
return returnObject;
|
| 471 |
+
}
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
Handle<Value> BSONDeserializer::DeserializeArray(bool promoteLongs)
|
| 475 |
+
{
|
| 476 |
+
uint32_t length = ReadUInt32();
|
| 477 |
+
if(length < 5) ThrowAllocatedStringException(64, "Bad BSON: Array Document is less than 5 bytes");
|
| 478 |
+
|
| 479 |
+
BSONDeserializer documentDeserializer(*this, length-4);
|
| 480 |
+
return documentDeserializer.DeserializeArrayInternal(promoteLongs);
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
Handle<Value> BSONDeserializer::DeserializeArrayInternal(bool promoteLongs)
|
| 484 |
+
{
|
| 485 |
+
Local<Array> returnArray = NanNew<Array>();
|
| 486 |
+
|
| 487 |
+
while(HasMoreData())
|
| 488 |
+
{
|
| 489 |
+
BsonType type = (BsonType) ReadByte();
|
| 490 |
+
uint32_t index = ReadIntegerString();
|
| 491 |
+
const Handle<Value>& value = DeserializeValue(type, promoteLongs);
|
| 492 |
+
returnArray->Set(index, value);
|
| 493 |
+
}
|
| 494 |
+
if(p != pEnd) ThrowAllocatedStringException(64, "Bad BSON Array: Serialize consumed unexpected number of bytes");
|
| 495 |
+
|
| 496 |
+
return returnArray;
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
Handle<Value> BSONDeserializer::DeserializeValue(BsonType type, bool promoteLongs)
|
| 500 |
+
{
|
| 501 |
+
switch(type)
|
| 502 |
+
{
|
| 503 |
+
case BSON_TYPE_STRING:
|
| 504 |
+
return ReadString();
|
| 505 |
+
|
| 506 |
+
case BSON_TYPE_INT:
|
| 507 |
+
return NanNew<Integer>(ReadInt32());
|
| 508 |
+
|
| 509 |
+
case BSON_TYPE_NUMBER:
|
| 510 |
+
return NanNew<Number>(ReadDouble());
|
| 511 |
+
|
| 512 |
+
case BSON_TYPE_NULL:
|
| 513 |
+
return NanNull();
|
| 514 |
+
|
| 515 |
+
case BSON_TYPE_UNDEFINED:
|
| 516 |
+
return NanNull();
|
| 517 |
+
|
| 518 |
+
case BSON_TYPE_TIMESTAMP:
|
| 519 |
+
{
|
| 520 |
+
int32_t lowBits = ReadInt32();
|
| 521 |
+
int32_t highBits = ReadInt32();
|
| 522 |
+
Local<Value> argv[] = { NanNew<Int32>(lowBits), NanNew<Int32>(highBits) };
|
| 523 |
+
return NanNew(bson->timestampConstructor)->NewInstance(2, argv);
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
case BSON_TYPE_BOOLEAN:
|
| 527 |
+
return (ReadByte() != 0) ? NanTrue() : NanFalse();
|
| 528 |
+
|
| 529 |
+
case BSON_TYPE_REGEXP:
|
| 530 |
+
{
|
| 531 |
+
const Handle<Value>& regex = ReadCString();
|
| 532 |
+
if(regex->IsNull()) ThrowAllocatedStringException(64, "Bad BSON Document: illegal CString");
|
| 533 |
+
int32_t options = ReadRegexOptions();
|
| 534 |
+
return NanNew<RegExp>(regex->ToString(), (RegExp::Flags) options);
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
case BSON_TYPE_CODE:
|
| 538 |
+
{
|
| 539 |
+
const Local<Value>& code = ReadString();
|
| 540 |
+
const Local<Value>& scope = NanNew<Object>();
|
| 541 |
+
Local<Value> argv[] = { code, scope };
|
| 542 |
+
return NanNew(bson->codeConstructor)->NewInstance(2, argv);
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
case BSON_TYPE_CODE_W_SCOPE:
|
| 546 |
+
{
|
| 547 |
+
ReadUInt32();
|
| 548 |
+
const Local<Value>& code = ReadString();
|
| 549 |
+
const Handle<Value>& scope = DeserializeDocument(promoteLongs);
|
| 550 |
+
Local<Value> argv[] = { code, scope->ToObject() };
|
| 551 |
+
return NanNew(bson->codeConstructor)->NewInstance(2, argv);
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
case BSON_TYPE_OID:
|
| 555 |
+
{
|
| 556 |
+
Local<Value> argv[] = { ReadObjectId() };
|
| 557 |
+
return NanNew(bson->objectIDConstructor)->NewInstance(1, argv);
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
case BSON_TYPE_BINARY:
|
| 561 |
+
{
|
| 562 |
+
uint32_t length = ReadUInt32();
|
| 563 |
+
uint32_t subType = ReadByte();
|
| 564 |
+
if(subType == 0x02) {
|
| 565 |
+
length = ReadInt32();
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
Local<Object> buffer = NanNewBufferHandle(p, length);
|
| 569 |
+
p += length;
|
| 570 |
+
|
| 571 |
+
Handle<Value> argv[] = { buffer, NanNew<Uint32>(subType) };
|
| 572 |
+
return NanNew(bson->binaryConstructor)->NewInstance(2, argv);
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
case BSON_TYPE_LONG:
|
| 576 |
+
{
|
| 577 |
+
// Read 32 bit integers
|
| 578 |
+
int32_t lowBits = (int32_t) ReadInt32();
|
| 579 |
+
int32_t highBits = (int32_t) ReadInt32();
|
| 580 |
+
|
| 581 |
+
// Promote long is enabled
|
| 582 |
+
if(promoteLongs) {
|
| 583 |
+
// If value is < 2^53 and >-2^53
|
| 584 |
+
if((highBits < 0x200000 || (highBits == 0x200000 && lowBits == 0)) && highBits >= -0x200000) {
|
| 585 |
+
// Adjust the pointer and read as 64 bit value
|
| 586 |
+
p -= 8;
|
| 587 |
+
// Read the 64 bit value
|
| 588 |
+
int64_t finalValue = (int64_t) ReadInt64();
|
| 589 |
+
return NanNew<Number>(finalValue);
|
| 590 |
+
}
|
| 591 |
+
}
|
| 592 |
+
|
| 593 |
+
// Decode the Long value
|
| 594 |
+
Local<Value> argv[] = { NanNew<Int32>(lowBits), NanNew<Int32>(highBits) };
|
| 595 |
+
return NanNew(bson->longConstructor)->NewInstance(2, argv);
|
| 596 |
+
}
|
| 597 |
+
|
| 598 |
+
case BSON_TYPE_DATE:
|
| 599 |
+
return NanNew<Date>((double) ReadInt64());
|
| 600 |
+
|
| 601 |
+
case BSON_TYPE_ARRAY:
|
| 602 |
+
return DeserializeArray(promoteLongs);
|
| 603 |
+
|
| 604 |
+
case BSON_TYPE_OBJECT:
|
| 605 |
+
return DeserializeDocument(promoteLongs);
|
| 606 |
+
|
| 607 |
+
case BSON_TYPE_SYMBOL:
|
| 608 |
+
{
|
| 609 |
+
const Local<String>& string = ReadString();
|
| 610 |
+
Local<Value> argv[] = { string };
|
| 611 |
+
return NanNew(bson->symbolConstructor)->NewInstance(1, argv);
|
| 612 |
+
}
|
| 613 |
+
|
| 614 |
+
case BSON_TYPE_MIN_KEY:
|
| 615 |
+
return NanNew(bson->minKeyConstructor)->NewInstance();
|
| 616 |
+
|
| 617 |
+
case BSON_TYPE_MAX_KEY:
|
| 618 |
+
return NanNew(bson->maxKeyConstructor)->NewInstance();
|
| 619 |
+
|
| 620 |
+
default:
|
| 621 |
+
ThrowAllocatedStringException(64, "Unhandled BSON Type: %d", type);
|
| 622 |
+
}
|
| 623 |
+
|
| 624 |
+
return NanNull();
|
| 625 |
+
}
|
| 626 |
+
|
| 627 |
+
Persistent<FunctionTemplate> BSON::constructor_template;
|
| 628 |
+
|
| 629 |
+
BSON::BSON() : ObjectWrap()
|
| 630 |
+
{
|
| 631 |
+
// Setup pre-allocated comparision objects
|
| 632 |
+
NanAssignPersistent(_bsontypeString, NanNew<String>("_bsontype"));
|
| 633 |
+
NanAssignPersistent(_longLowString, NanNew<String>("low_"));
|
| 634 |
+
NanAssignPersistent(_longHighString, NanNew<String>("high_"));
|
| 635 |
+
NanAssignPersistent(_objectIDidString, NanNew<String>("id"));
|
| 636 |
+
NanAssignPersistent(_binaryPositionString, NanNew<String>("position"));
|
| 637 |
+
NanAssignPersistent(_binarySubTypeString, NanNew<String>("sub_type"));
|
| 638 |
+
NanAssignPersistent(_binaryBufferString, NanNew<String>("buffer"));
|
| 639 |
+
NanAssignPersistent(_doubleValueString, NanNew<String>("value"));
|
| 640 |
+
NanAssignPersistent(_symbolValueString, NanNew<String>("value"));
|
| 641 |
+
NanAssignPersistent(_dbRefRefString, NanNew<String>("$ref"));
|
| 642 |
+
NanAssignPersistent(_dbRefIdRefString, NanNew<String>("$id"));
|
| 643 |
+
NanAssignPersistent(_dbRefDbRefString, NanNew<String>("$db"));
|
| 644 |
+
NanAssignPersistent(_dbRefNamespaceString, NanNew<String>("namespace"));
|
| 645 |
+
NanAssignPersistent(_dbRefDbString, NanNew<String>("db"));
|
| 646 |
+
NanAssignPersistent(_dbRefOidString, NanNew<String>("oid"));
|
| 647 |
+
NanAssignPersistent(_codeCodeString, NanNew<String>("code"));
|
| 648 |
+
NanAssignPersistent(_codeScopeString, NanNew<String>("scope"));
|
| 649 |
+
NanAssignPersistent(_toBSONString, NanNew<String>("toBSON"));
|
| 650 |
+
|
| 651 |
+
NanAssignPersistent(longString, NanNew<String>("Long"));
|
| 652 |
+
NanAssignPersistent(objectIDString, NanNew<String>("ObjectID"));
|
| 653 |
+
NanAssignPersistent(binaryString, NanNew<String>("Binary"));
|
| 654 |
+
NanAssignPersistent(codeString, NanNew<String>("Code"));
|
| 655 |
+
NanAssignPersistent(dbrefString, NanNew<String>("DBRef"));
|
| 656 |
+
NanAssignPersistent(symbolString, NanNew<String>("Symbol"));
|
| 657 |
+
NanAssignPersistent(doubleString, NanNew<String>("Double"));
|
| 658 |
+
NanAssignPersistent(timestampString, NanNew<String>("Timestamp"));
|
| 659 |
+
NanAssignPersistent(minKeyString, NanNew<String>("MinKey"));
|
| 660 |
+
NanAssignPersistent(maxKeyString, NanNew<String>("MaxKey"));
|
| 661 |
+
}
|
| 662 |
+
|
| 663 |
+
void BSON::Initialize(v8::Handle<v8::Object> target)
|
| 664 |
+
{
|
| 665 |
+
// Grab the scope of the call from Node
|
| 666 |
+
NanScope();
|
| 667 |
+
// Define a new function template
|
| 668 |
+
Local<FunctionTemplate> t = NanNew<FunctionTemplate>(New);
|
| 669 |
+
t->InstanceTemplate()->SetInternalFieldCount(1);
|
| 670 |
+
t->SetClassName(NanNew<String>("BSON"));
|
| 671 |
+
|
| 672 |
+
// Instance methods
|
| 673 |
+
NODE_SET_PROTOTYPE_METHOD(t, "calculateObjectSize", CalculateObjectSize);
|
| 674 |
+
NODE_SET_PROTOTYPE_METHOD(t, "serialize", BSONSerialize);
|
| 675 |
+
NODE_SET_PROTOTYPE_METHOD(t, "serializeWithBufferAndIndex", SerializeWithBufferAndIndex);
|
| 676 |
+
NODE_SET_PROTOTYPE_METHOD(t, "deserialize", BSONDeserialize);
|
| 677 |
+
NODE_SET_PROTOTYPE_METHOD(t, "deserializeStream", BSONDeserializeStream);
|
| 678 |
+
|
| 679 |
+
NanAssignPersistent(constructor_template, t);
|
| 680 |
+
|
| 681 |
+
target->ForceSet(NanNew<String>("BSON"), t->GetFunction());
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
+
// Create a new instance of BSON and passing it the existing context
|
| 685 |
+
NAN_METHOD(BSON::New)
|
| 686 |
+
{
|
| 687 |
+
NanScope();
|
| 688 |
+
|
| 689 |
+
// Check that we have an array
|
| 690 |
+
if(args.Length() == 1 && args[0]->IsArray())
|
| 691 |
+
{
|
| 692 |
+
// Cast the array to a local reference
|
| 693 |
+
Local<Array> array = Local<Array>::Cast(args[0]);
|
| 694 |
+
|
| 695 |
+
if(array->Length() > 0)
|
| 696 |
+
{
|
| 697 |
+
// Create a bson object instance and return it
|
| 698 |
+
BSON *bson = new BSON();
|
| 699 |
+
|
| 700 |
+
uint32_t foundClassesMask = 0;
|
| 701 |
+
|
| 702 |
+
// Iterate over all entries to save the instantiate functions
|
| 703 |
+
for(uint32_t i = 0; i < array->Length(); i++) {
|
| 704 |
+
// Let's get a reference to the function
|
| 705 |
+
Local<Function> func = Local<Function>::Cast(array->Get(i));
|
| 706 |
+
Local<String> functionName = func->GetName()->ToString();
|
| 707 |
+
|
| 708 |
+
// Save the functions making them persistant handles (they don't get collected)
|
| 709 |
+
if(functionName->StrictEquals(NanNew(bson->longString))) {
|
| 710 |
+
NanAssignPersistent(bson->longConstructor, func);
|
| 711 |
+
foundClassesMask |= 1;
|
| 712 |
+
} else if(functionName->StrictEquals(NanNew(bson->objectIDString))) {
|
| 713 |
+
NanAssignPersistent(bson->objectIDConstructor, func);
|
| 714 |
+
foundClassesMask |= 2;
|
| 715 |
+
} else if(functionName->StrictEquals(NanNew(bson->binaryString))) {
|
| 716 |
+
NanAssignPersistent(bson->binaryConstructor, func);
|
| 717 |
+
foundClassesMask |= 4;
|
| 718 |
+
} else if(functionName->StrictEquals(NanNew(bson->codeString))) {
|
| 719 |
+
NanAssignPersistent(bson->codeConstructor, func);
|
| 720 |
+
foundClassesMask |= 8;
|
| 721 |
+
} else if(functionName->StrictEquals(NanNew(bson->dbrefString))) {
|
| 722 |
+
NanAssignPersistent(bson->dbrefConstructor, func);
|
| 723 |
+
foundClassesMask |= 0x10;
|
| 724 |
+
} else if(functionName->StrictEquals(NanNew(bson->symbolString))) {
|
| 725 |
+
NanAssignPersistent(bson->symbolConstructor, func);
|
| 726 |
+
foundClassesMask |= 0x20;
|
| 727 |
+
} else if(functionName->StrictEquals(NanNew(bson->doubleString))) {
|
| 728 |
+
NanAssignPersistent(bson->doubleConstructor, func);
|
| 729 |
+
foundClassesMask |= 0x40;
|
| 730 |
+
} else if(functionName->StrictEquals(NanNew(bson->timestampString))) {
|
| 731 |
+
NanAssignPersistent(bson->timestampConstructor, func);
|
| 732 |
+
foundClassesMask |= 0x80;
|
| 733 |
+
} else if(functionName->StrictEquals(NanNew(bson->minKeyString))) {
|
| 734 |
+
NanAssignPersistent(bson->minKeyConstructor, func);
|
| 735 |
+
foundClassesMask |= 0x100;
|
| 736 |
+
} else if(functionName->StrictEquals(NanNew(bson->maxKeyString))) {
|
| 737 |
+
NanAssignPersistent(bson->maxKeyConstructor, func);
|
| 738 |
+
foundClassesMask |= 0x200;
|
| 739 |
+
}
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
// Check if we have the right number of constructors otherwise throw an error
|
| 743 |
+
if(foundClassesMask != 0x3ff) {
|
| 744 |
+
delete bson;
|
| 745 |
+
return NanThrowError("Missing function constructor for either [Long/ObjectID/Binary/Code/DbRef/Symbol/Double/Timestamp/MinKey/MaxKey]");
|
| 746 |
+
} else {
|
| 747 |
+
bson->Wrap(args.This());
|
| 748 |
+
NanReturnValue(args.This());
|
| 749 |
+
}
|
| 750 |
+
}
|
| 751 |
+
else
|
| 752 |
+
{
|
| 753 |
+
return NanThrowError("No types passed in");
|
| 754 |
+
}
|
| 755 |
+
}
|
| 756 |
+
else
|
| 757 |
+
{
|
| 758 |
+
return NanThrowTypeError("Argument passed in must be an array of types");
|
| 759 |
+
}
|
| 760 |
+
}
|
| 761 |
+
|
| 762 |
+
//------------------------------------------------------------------------------------------------
|
| 763 |
+
//------------------------------------------------------------------------------------------------
|
| 764 |
+
//------------------------------------------------------------------------------------------------
|
| 765 |
+
//------------------------------------------------------------------------------------------------
|
| 766 |
+
|
| 767 |
+
NAN_METHOD(BSON::BSONDeserialize)
|
| 768 |
+
{
|
| 769 |
+
NanScope();
|
| 770 |
+
|
| 771 |
+
// Fail if the first argument is not a string or a buffer
|
| 772 |
+
if(args.Length() > 1 && !args[0]->IsString() && !Buffer::HasInstance(args[0]))
|
| 773 |
+
return NanThrowError("First Argument must be a Buffer or String.");
|
| 774 |
+
|
| 775 |
+
// Promote longs
|
| 776 |
+
bool promoteLongs = true;
|
| 777 |
+
|
| 778 |
+
// If we have an options object
|
| 779 |
+
if(args.Length() == 2 && args[1]->IsObject()) {
|
| 780 |
+
Local<Object> options = args[1]->ToObject();
|
| 781 |
+
|
| 782 |
+
if(options->Has(NanNew<String>("promoteLongs"))) {
|
| 783 |
+
promoteLongs = options->Get(NanNew<String>("promoteLongs"))->ToBoolean()->Value();
|
| 784 |
+
}
|
| 785 |
+
}
|
| 786 |
+
|
| 787 |
+
// Define pointer to data
|
| 788 |
+
Local<Object> obj = args[0]->ToObject();
|
| 789 |
+
|
| 790 |
+
// Unpack the BSON parser instance
|
| 791 |
+
BSON *bson = ObjectWrap::Unwrap<BSON>(args.This());
|
| 792 |
+
|
| 793 |
+
// If we passed in a buffer, let's unpack it, otherwise let's unpack the string
|
| 794 |
+
if(Buffer::HasInstance(obj))
|
| 795 |
+
{
|
| 796 |
+
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3
|
| 797 |
+
Local<Object> buffer = ObjectWrap::Unwrap<Buffer>(obj);
|
| 798 |
+
char* data = buffer->data();
|
| 799 |
+
size_t length = buffer->length();
|
| 800 |
+
#else
|
| 801 |
+
char* data = Buffer::Data(obj);
|
| 802 |
+
size_t length = Buffer::Length(obj);
|
| 803 |
+
#endif
|
| 804 |
+
|
| 805 |
+
// Validate that we have at least 5 bytes
|
| 806 |
+
if(length < 5) return NanThrowError("corrupt bson message < 5 bytes long");
|
| 807 |
+
|
| 808 |
+
try
|
| 809 |
+
{
|
| 810 |
+
BSONDeserializer deserializer(bson, data, length);
|
| 811 |
+
// deserializer.promoteLongs = promoteLongs;
|
| 812 |
+
NanReturnValue(deserializer.DeserializeDocument(promoteLongs));
|
| 813 |
+
}
|
| 814 |
+
catch(char* exception)
|
| 815 |
+
{
|
| 816 |
+
Local<String> error = NanNew<String>(exception);
|
| 817 |
+
free(exception);
|
| 818 |
+
return NanThrowError(error);
|
| 819 |
+
}
|
| 820 |
+
|
| 821 |
+
}
|
| 822 |
+
else
|
| 823 |
+
{
|
| 824 |
+
// The length of the data for this encoding
|
| 825 |
+
ssize_t len = DecodeBytes(args[0], BINARY);
|
| 826 |
+
|
| 827 |
+
// Validate that we have at least 5 bytes
|
| 828 |
+
if(len < 5) return NanThrowError("corrupt bson message < 5 bytes long");
|
| 829 |
+
|
| 830 |
+
// Let's define the buffer size
|
| 831 |
+
char* data = (char *)malloc(len);
|
| 832 |
+
if(data == NULL) die("Failed to allocate char buffer for BSON serialization");
|
| 833 |
+
DecodeWrite(data, len, args[0], BINARY);
|
| 834 |
+
|
| 835 |
+
try
|
| 836 |
+
{
|
| 837 |
+
BSONDeserializer deserializer(bson, data, len);
|
| 838 |
+
// deserializer.promoteLongs = promoteLongs;
|
| 839 |
+
Handle<Value> result = deserializer.DeserializeDocument(promoteLongs);
|
| 840 |
+
free(data);
|
| 841 |
+
NanReturnValue(result);
|
| 842 |
+
|
| 843 |
+
}
|
| 844 |
+
catch(char* exception)
|
| 845 |
+
{
|
| 846 |
+
Local<String> error = NanNew<String>(exception);
|
| 847 |
+
free(exception);
|
| 848 |
+
free(data);
|
| 849 |
+
return NanThrowError(error);
|
| 850 |
+
}
|
| 851 |
+
}
|
| 852 |
+
}
|
| 853 |
+
|
| 854 |
+
Local<Object> BSON::GetSerializeObject(const Handle<Value>& argValue)
|
| 855 |
+
{
|
| 856 |
+
Local<Object> object = argValue->ToObject();
|
| 857 |
+
if(object->Has(NanNew(_toBSONString)))
|
| 858 |
+
{
|
| 859 |
+
const Local<Value>& toBSON = object->Get(NanNew(_toBSONString));
|
| 860 |
+
if(!toBSON->IsFunction()) ThrowAllocatedStringException(64, "toBSON is not a function");
|
| 861 |
+
|
| 862 |
+
Local<Value> result = Local<Function>::Cast(toBSON)->Call(object, 0, NULL);
|
| 863 |
+
if(!result->IsObject()) ThrowAllocatedStringException(64, "toBSON function did not return an object");
|
| 864 |
+
return result->ToObject();
|
| 865 |
+
}
|
| 866 |
+
else
|
| 867 |
+
{
|
| 868 |
+
return object;
|
| 869 |
+
}
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
NAN_METHOD(BSON::BSONSerialize)
|
| 873 |
+
{
|
| 874 |
+
NanScope();
|
| 875 |
+
|
| 876 |
+
if(args.Length() == 1 && !args[0]->IsObject()) return NanThrowError("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]");
|
| 877 |
+
if(args.Length() == 2 && !args[0]->IsObject() && !args[1]->IsBoolean()) return NanThrowError("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]");
|
| 878 |
+
if(args.Length() == 3 && !args[0]->IsObject() && !args[1]->IsBoolean() && !args[2]->IsBoolean()) return NanThrowError("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]");
|
| 879 |
+
if(args.Length() == 4 && !args[0]->IsObject() && !args[1]->IsBoolean() && !args[2]->IsBoolean() && !args[3]->IsBoolean()) return NanThrowError("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean] or [object, boolean, boolean, boolean]");
|
| 880 |
+
if(args.Length() > 4) return NanThrowError("One, two, tree or four arguments required - [object] or [object, boolean] or [object, boolean, boolean] or [object, boolean, boolean, boolean]");
|
| 881 |
+
|
| 882 |
+
// Check if we have an array as the object
|
| 883 |
+
if(args[0]->IsArray()) return NanThrowError("Only javascript objects supported");
|
| 884 |
+
|
| 885 |
+
// Unpack the BSON parser instance
|
| 886 |
+
BSON *bson = ObjectWrap::Unwrap<BSON>(args.This());
|
| 887 |
+
|
| 888 |
+
// Calculate the total size of the document in binary form to ensure we only allocate memory once
|
| 889 |
+
// With serialize function
|
| 890 |
+
bool serializeFunctions = (args.Length() >= 4) && args[3]->BooleanValue();
|
| 891 |
+
|
| 892 |
+
char *serialized_object = NULL;
|
| 893 |
+
size_t object_size;
|
| 894 |
+
try
|
| 895 |
+
{
|
| 896 |
+
Local<Object> object = bson->GetSerializeObject(args[0]);
|
| 897 |
+
|
| 898 |
+
BSONSerializer<CountStream> counter(bson, false, serializeFunctions);
|
| 899 |
+
counter.SerializeDocument(object);
|
| 900 |
+
object_size = counter.GetSerializeSize();
|
| 901 |
+
|
| 902 |
+
// Allocate the memory needed for the serialization
|
| 903 |
+
serialized_object = (char *)malloc(object_size);
|
| 904 |
+
if(serialized_object == NULL) die("Failed to allocate memory for object");
|
| 905 |
+
|
| 906 |
+
// Check if we have a boolean value
|
| 907 |
+
bool checkKeys = args.Length() >= 3 && args[1]->IsBoolean() && args[1]->BooleanValue();
|
| 908 |
+
BSONSerializer<DataStream> data(bson, checkKeys, serializeFunctions, serialized_object);
|
| 909 |
+
data.SerializeDocument(object);
|
| 910 |
+
}
|
| 911 |
+
catch(char *err_msg)
|
| 912 |
+
{
|
| 913 |
+
free(serialized_object);
|
| 914 |
+
Local<String> error = NanNew<String>(err_msg);
|
| 915 |
+
free(err_msg);
|
| 916 |
+
return NanThrowError(error);
|
| 917 |
+
}
|
| 918 |
+
|
| 919 |
+
// If we have 3 arguments
|
| 920 |
+
if(args.Length() == 3 || args.Length() == 4)
|
| 921 |
+
{
|
| 922 |
+
Local<Object> buffer = NanNewBufferHandle(serialized_object, object_size);
|
| 923 |
+
free(serialized_object);
|
| 924 |
+
NanReturnValue(buffer);
|
| 925 |
+
}
|
| 926 |
+
else
|
| 927 |
+
{
|
| 928 |
+
Local<Value> bin_value = Encode(serialized_object, object_size, BINARY)->ToString();
|
| 929 |
+
free(serialized_object);
|
| 930 |
+
NanReturnValue(bin_value);
|
| 931 |
+
}
|
| 932 |
+
}
|
| 933 |
+
|
| 934 |
+
NAN_METHOD(BSON::CalculateObjectSize)
|
| 935 |
+
{
|
| 936 |
+
NanScope();
|
| 937 |
+
// Ensure we have a valid object
|
| 938 |
+
if(args.Length() == 1 && !args[0]->IsObject()) return NanThrowError("One argument required - [object]");
|
| 939 |
+
if(args.Length() == 2 && !args[0]->IsObject() && !args[1]->IsBoolean()) return NanThrowError("Two arguments required - [object, boolean]");
|
| 940 |
+
if(args.Length() > 3) return NanThrowError("One or two arguments required - [object] or [object, boolean]");
|
| 941 |
+
|
| 942 |
+
// Unpack the BSON parser instance
|
| 943 |
+
BSON *bson = ObjectWrap::Unwrap<BSON>(args.This());
|
| 944 |
+
bool serializeFunctions = (args.Length() >= 2) && args[1]->BooleanValue();
|
| 945 |
+
BSONSerializer<CountStream> countSerializer(bson, false, serializeFunctions);
|
| 946 |
+
countSerializer.SerializeDocument(args[0]);
|
| 947 |
+
|
| 948 |
+
// Return the object size
|
| 949 |
+
NanReturnValue(NanNew<Uint32>((uint32_t) countSerializer.GetSerializeSize()));
|
| 950 |
+
}
|
| 951 |
+
|
| 952 |
+
NAN_METHOD(BSON::SerializeWithBufferAndIndex)
|
| 953 |
+
{
|
| 954 |
+
NanScope();
|
| 955 |
+
|
| 956 |
+
//BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, ->, buffer, index) {
|
| 957 |
+
// Ensure we have the correct values
|
| 958 |
+
if(args.Length() > 5) return NanThrowError("Four or five parameters required [object, boolean, Buffer, int] or [object, boolean, Buffer, int, boolean]");
|
| 959 |
+
if(args.Length() == 4 && !args[0]->IsObject() && !args[1]->IsBoolean() && !Buffer::HasInstance(args[2]) && !args[3]->IsUint32()) return NanThrowError("Four parameters required [object, boolean, Buffer, int]");
|
| 960 |
+
if(args.Length() == 5 && !args[0]->IsObject() && !args[1]->IsBoolean() && !Buffer::HasInstance(args[2]) && !args[3]->IsUint32() && !args[4]->IsBoolean()) return NanThrowError("Four parameters required [object, boolean, Buffer, int, boolean]");
|
| 961 |
+
|
| 962 |
+
uint32_t index;
|
| 963 |
+
size_t object_size;
|
| 964 |
+
|
| 965 |
+
try
|
| 966 |
+
{
|
| 967 |
+
BSON *bson = ObjectWrap::Unwrap<BSON>(args.This());
|
| 968 |
+
|
| 969 |
+
Local<Object> obj = args[2]->ToObject();
|
| 970 |
+
char* data = Buffer::Data(obj);
|
| 971 |
+
size_t length = Buffer::Length(obj);
|
| 972 |
+
|
| 973 |
+
index = args[3]->Uint32Value();
|
| 974 |
+
bool checkKeys = args.Length() >= 4 && args[1]->IsBoolean() && args[1]->BooleanValue();
|
| 975 |
+
bool serializeFunctions = (args.Length() == 5) && args[4]->BooleanValue();
|
| 976 |
+
|
| 977 |
+
BSONSerializer<DataStream> dataSerializer(bson, checkKeys, serializeFunctions, data+index);
|
| 978 |
+
dataSerializer.SerializeDocument(bson->GetSerializeObject(args[0]));
|
| 979 |
+
object_size = dataSerializer.GetSerializeSize();
|
| 980 |
+
|
| 981 |
+
if(object_size + index > length) return NanThrowError("Serious error - overflowed buffer!!");
|
| 982 |
+
}
|
| 983 |
+
catch(char *exception)
|
| 984 |
+
{
|
| 985 |
+
Local<String> error = NanNew<String>(exception);
|
| 986 |
+
free(exception);
|
| 987 |
+
return NanThrowError(error);
|
| 988 |
+
}
|
| 989 |
+
|
| 990 |
+
NanReturnValue(NanNew<Uint32>((uint32_t) (index + object_size - 1)));
|
| 991 |
+
}
|
| 992 |
+
|
| 993 |
+
NAN_METHOD(BSON::BSONDeserializeStream)
|
| 994 |
+
{
|
| 995 |
+
NanScope();
|
| 996 |
+
|
| 997 |
+
// At least 3 arguments required
|
| 998 |
+
if(args.Length() < 5) return NanThrowError("Arguments required (Buffer(data), Number(index in data), Number(number of documents to deserialize), Array(results), Number(index in the array), Object(optional))");
|
| 999 |
+
|
| 1000 |
+
// If the number of argumets equals 3
|
| 1001 |
+
if(args.Length() >= 5)
|
| 1002 |
+
{
|
| 1003 |
+
if(!Buffer::HasInstance(args[0])) return NanThrowError("First argument must be Buffer instance");
|
| 1004 |
+
if(!args[1]->IsUint32()) return NanThrowError("Second argument must be a positive index number");
|
| 1005 |
+
if(!args[2]->IsUint32()) return NanThrowError("Third argument must be a positive number of documents to deserialize");
|
| 1006 |
+
if(!args[3]->IsArray()) return NanThrowError("Fourth argument must be an array the size of documents to deserialize");
|
| 1007 |
+
if(!args[4]->IsUint32()) return NanThrowError("Sixth argument must be a positive index number");
|
| 1008 |
+
}
|
| 1009 |
+
|
| 1010 |
+
// If we have 4 arguments
|
| 1011 |
+
if(args.Length() == 6 && !args[5]->IsObject()) return NanThrowError("Fifth argument must be an object with options");
|
| 1012 |
+
|
| 1013 |
+
// Define pointer to data
|
| 1014 |
+
Local<Object> obj = args[0]->ToObject();
|
| 1015 |
+
uint32_t numberOfDocuments = args[2]->Uint32Value();
|
| 1016 |
+
uint32_t index = args[1]->Uint32Value();
|
| 1017 |
+
uint32_t resultIndex = args[4]->Uint32Value();
|
| 1018 |
+
bool promoteLongs = true;
|
| 1019 |
+
|
| 1020 |
+
// Check for the value promoteLongs in the options object
|
| 1021 |
+
if(args.Length() == 6) {
|
| 1022 |
+
Local<Object> options = args[5]->ToObject();
|
| 1023 |
+
|
| 1024 |
+
// Check if we have the promoteLong variable
|
| 1025 |
+
if(options->Has(NanNew<String>("promoteLongs"))) {
|
| 1026 |
+
promoteLongs = options->Get(NanNew<String>("promoteLongs"))->ToBoolean()->Value();
|
| 1027 |
+
}
|
| 1028 |
+
}
|
| 1029 |
+
|
| 1030 |
+
// Unpack the BSON parser instance
|
| 1031 |
+
BSON *bson = ObjectWrap::Unwrap<BSON>(args.This());
|
| 1032 |
+
|
| 1033 |
+
// Unpack the buffer variable
|
| 1034 |
+
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3
|
| 1035 |
+
Local<Object> buffer = ObjectWrap::Unwrap<Buffer>(obj);
|
| 1036 |
+
char* data = buffer->data();
|
| 1037 |
+
size_t length = buffer->length();
|
| 1038 |
+
#else
|
| 1039 |
+
char* data = Buffer::Data(obj);
|
| 1040 |
+
size_t length = Buffer::Length(obj);
|
| 1041 |
+
#endif
|
| 1042 |
+
|
| 1043 |
+
// Fetch the documents
|
| 1044 |
+
Local<Object> documents = args[3]->ToObject();
|
| 1045 |
+
|
| 1046 |
+
BSONDeserializer deserializer(bson, data+index, length-index);
|
| 1047 |
+
for(uint32_t i = 0; i < numberOfDocuments; i++)
|
| 1048 |
+
{
|
| 1049 |
+
try
|
| 1050 |
+
{
|
| 1051 |
+
documents->Set(i + resultIndex, deserializer.DeserializeDocument(promoteLongs));
|
| 1052 |
+
}
|
| 1053 |
+
catch (char* exception)
|
| 1054 |
+
{
|
| 1055 |
+
Local<String> error = NanNew<String>(exception);
|
| 1056 |
+
free(exception);
|
| 1057 |
+
return NanThrowError(error);
|
| 1058 |
+
}
|
| 1059 |
+
}
|
| 1060 |
+
|
| 1061 |
+
// Return new index of parsing
|
| 1062 |
+
NanReturnValue(NanNew<Uint32>((uint32_t) (index + deserializer.GetSerializeSize())));
|
| 1063 |
+
}
|
| 1064 |
+
|
| 1065 |
+
// Exporting function
|
| 1066 |
+
extern "C" void init(Handle<Object> target)
|
| 1067 |
+
{
|
| 1068 |
+
NanScope();
|
| 1069 |
+
BSON::Initialize(target);
|
| 1070 |
+
}
|
| 1071 |
+
|
| 1072 |
+
NODE_MODULE(bson, BSON::Initialize);
|
5485-master/demos/node_modules/mongoose/node_modules/bson/node_modules/bson-ext/ext/bson.h
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
| 5 |
+
|
| 6 |
+
//===========================================================================
|
| 7 |
+
|
| 8 |
+
#ifdef __arm__
|
| 9 |
+
#define USE_MISALIGNED_MEMORY_ACCESS 0
|
| 10 |
+
#else
|
| 11 |
+
#define USE_MISALIGNED_MEMORY_ACCESS 1
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
#include <node.h>
|
| 15 |
+
#include <node_object_wrap.h>
|
| 16 |
+
#include <v8.h>
|
| 17 |
+
#include "nan.h"
|
| 18 |
+
|
| 19 |
+
using namespace v8;
|
| 20 |
+
using namespace node;
|
| 21 |
+
|
| 22 |
+
//===========================================================================
|
| 23 |
+
|
| 24 |
+
enum BsonType
|
| 25 |
+
{
|
| 26 |
+
BSON_TYPE_NUMBER = 1,
|
| 27 |
+
BSON_TYPE_STRING = 2,
|
| 28 |
+
BSON_TYPE_OBJECT = 3,
|
| 29 |
+
BSON_TYPE_ARRAY = 4,
|
| 30 |
+
BSON_TYPE_BINARY = 5,
|
| 31 |
+
BSON_TYPE_UNDEFINED = 6,
|
| 32 |
+
BSON_TYPE_OID = 7,
|
| 33 |
+
BSON_TYPE_BOOLEAN = 8,
|
| 34 |
+
BSON_TYPE_DATE = 9,
|
| 35 |
+
BSON_TYPE_NULL = 10,
|
| 36 |
+
BSON_TYPE_REGEXP = 11,
|
| 37 |
+
BSON_TYPE_CODE = 13,
|
| 38 |
+
BSON_TYPE_SYMBOL = 14,
|
| 39 |
+
BSON_TYPE_CODE_W_SCOPE = 15,
|
| 40 |
+
BSON_TYPE_INT = 16,
|
| 41 |
+
BSON_TYPE_TIMESTAMP = 17,
|
| 42 |
+
BSON_TYPE_LONG = 18,
|
| 43 |
+
BSON_TYPE_MAX_KEY = 0x7f,
|
| 44 |
+
BSON_TYPE_MIN_KEY = 0xff
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
//===========================================================================
|
| 48 |
+
|
| 49 |
+
template<typename T> class BSONSerializer;
|
| 50 |
+
|
| 51 |
+
class BSON : public ObjectWrap {
|
| 52 |
+
public:
|
| 53 |
+
BSON();
|
| 54 |
+
~BSON() {}
|
| 55 |
+
|
| 56 |
+
static void Initialize(Handle<Object> target);
|
| 57 |
+
static NAN_METHOD(BSONDeserializeStream);
|
| 58 |
+
|
| 59 |
+
// JS based objects
|
| 60 |
+
static NAN_METHOD(BSONSerialize);
|
| 61 |
+
static NAN_METHOD(BSONDeserialize);
|
| 62 |
+
|
| 63 |
+
// Calculate size of function
|
| 64 |
+
static NAN_METHOD(CalculateObjectSize);
|
| 65 |
+
static NAN_METHOD(SerializeWithBufferAndIndex);
|
| 66 |
+
|
| 67 |
+
// Constructor used for creating new BSON objects from C++
|
| 68 |
+
static Persistent<FunctionTemplate> constructor_template;
|
| 69 |
+
|
| 70 |
+
private:
|
| 71 |
+
static NAN_METHOD(New);
|
| 72 |
+
static Handle<Value> deserialize(BSON *bson, char *data, uint32_t dataLength, uint32_t startIndex, bool is_array_item);
|
| 73 |
+
|
| 74 |
+
// BSON type instantiate functions
|
| 75 |
+
Persistent<Function> longConstructor;
|
| 76 |
+
Persistent<Function> objectIDConstructor;
|
| 77 |
+
Persistent<Function> binaryConstructor;
|
| 78 |
+
Persistent<Function> codeConstructor;
|
| 79 |
+
Persistent<Function> dbrefConstructor;
|
| 80 |
+
Persistent<Function> symbolConstructor;
|
| 81 |
+
Persistent<Function> doubleConstructor;
|
| 82 |
+
Persistent<Function> timestampConstructor;
|
| 83 |
+
Persistent<Function> minKeyConstructor;
|
| 84 |
+
Persistent<Function> maxKeyConstructor;
|
| 85 |
+
|
| 86 |
+
// Equality Objects
|
| 87 |
+
Persistent<String> longString;
|
| 88 |
+
Persistent<String> objectIDString;
|
| 89 |
+
Persistent<String> binaryString;
|
| 90 |
+
Persistent<String> codeString;
|
| 91 |
+
Persistent<String> dbrefString;
|
| 92 |
+
Persistent<String> symbolString;
|
| 93 |
+
Persistent<String> doubleString;
|
| 94 |
+
Persistent<String> timestampString;
|
| 95 |
+
Persistent<String> minKeyString;
|
| 96 |
+
Persistent<String> maxKeyString;
|
| 97 |
+
|
| 98 |
+
// Equality speed up comparison objects
|
| 99 |
+
Persistent<String> _bsontypeString;
|
| 100 |
+
Persistent<String> _longLowString;
|
| 101 |
+
Persistent<String> _longHighString;
|
| 102 |
+
Persistent<String> _objectIDidString;
|
| 103 |
+
Persistent<String> _binaryPositionString;
|
| 104 |
+
Persistent<String> _binarySubTypeString;
|
| 105 |
+
Persistent<String> _binaryBufferString;
|
| 106 |
+
Persistent<String> _doubleValueString;
|
| 107 |
+
Persistent<String> _symbolValueString;
|
| 108 |
+
|
| 109 |
+
Persistent<String> _dbRefRefString;
|
| 110 |
+
Persistent<String> _dbRefIdRefString;
|
| 111 |
+
Persistent<String> _dbRefDbRefString;
|
| 112 |
+
Persistent<String> _dbRefNamespaceString;
|
| 113 |
+
Persistent<String> _dbRefDbString;
|
| 114 |
+
Persistent<String> _dbRefOidString;
|
| 115 |
+
|
| 116 |
+
Persistent<String> _codeCodeString;
|
| 117 |
+
Persistent<String> _codeScopeString;
|
| 118 |
+
Persistent<String> _toBSONString;
|
| 119 |
+
|
| 120 |
+
Local<Object> GetSerializeObject(const Handle<Value>& object);
|
| 121 |
+
|
| 122 |
+
template<typename T> friend class BSONSerializer;
|
| 123 |
+
friend class BSONDeserializer;
|
| 124 |
+
};
|
| 125 |
+
|
| 126 |
+
//===========================================================================
|
| 127 |
+
|
| 128 |
+
class CountStream
|
| 129 |
+
{
|
| 130 |
+
public:
|
| 131 |
+
CountStream() : count(0) { }
|
| 132 |
+
|
| 133 |
+
void WriteByte(int value) { ++count; }
|
| 134 |
+
void WriteByte(const Handle<Object>&, const Handle<String>&) { ++count; }
|
| 135 |
+
void WriteBool(const Handle<Value>& value) { ++count; }
|
| 136 |
+
void WriteInt32(int32_t value) { count += 4; }
|
| 137 |
+
void WriteInt32(const Handle<Value>& value) { count += 4; }
|
| 138 |
+
void WriteInt32(const Handle<Object>& object, const Handle<String>& key) { count += 4; }
|
| 139 |
+
void WriteInt64(int64_t value) { count += 8; }
|
| 140 |
+
void WriteInt64(const Handle<Value>& value) { count += 8; }
|
| 141 |
+
void WriteDouble(double value) { count += 8; }
|
| 142 |
+
void WriteDouble(const Handle<Value>& value) { count += 8; }
|
| 143 |
+
void WriteDouble(const Handle<Object>&, const Handle<String>&) { count += 8; }
|
| 144 |
+
void WriteUInt32String(uint32_t name) { char buffer[32]; count += sprintf(buffer, "%u", name) + 1; }
|
| 145 |
+
void WriteLengthPrefixedString(const Local<String>& value) { count += value->Utf8Length()+5; }
|
| 146 |
+
void WriteObjectId(const Handle<Object>& object, const Handle<String>& key) { count += 12; }
|
| 147 |
+
void WriteString(const Local<String>& value) { count += value->Utf8Length() + 1; } // This returns the number of bytes exclusive of the NULL terminator
|
| 148 |
+
void WriteData(const char* data, size_t length) { count += length; }
|
| 149 |
+
|
| 150 |
+
void* BeginWriteType() { ++count; return NULL; }
|
| 151 |
+
void CommitType(void*, BsonType) { }
|
| 152 |
+
void* BeginWriteSize() { count += 4; return NULL; }
|
| 153 |
+
void CommitSize(void*) { }
|
| 154 |
+
|
| 155 |
+
size_t GetSerializeSize() const { return count; }
|
| 156 |
+
|
| 157 |
+
// Do nothing. CheckKey is implemented for DataStream
|
| 158 |
+
void CheckKey(const Local<String>&) { }
|
| 159 |
+
|
| 160 |
+
private:
|
| 161 |
+
size_t count;
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
class DataStream
|
| 165 |
+
{
|
| 166 |
+
public:
|
| 167 |
+
DataStream(char* aDestinationBuffer) : destinationBuffer(aDestinationBuffer), p(aDestinationBuffer) { }
|
| 168 |
+
|
| 169 |
+
void WriteByte(int value) { *p++ = value; }
|
| 170 |
+
void WriteByte(const Handle<Object>& object, const Handle<String>& key) { *p++ = object->Get(key)->Int32Value(); }
|
| 171 |
+
#if USE_MISALIGNED_MEMORY_ACCESS
|
| 172 |
+
void WriteInt32(int32_t value) { *reinterpret_cast<int32_t*>(p) = value; p += 4; }
|
| 173 |
+
void WriteInt64(int64_t value) { *reinterpret_cast<int64_t*>(p) = value; p += 8; }
|
| 174 |
+
void WriteDouble(double value) { *reinterpret_cast<double*>(p) = value; p += 8; }
|
| 175 |
+
#else
|
| 176 |
+
void WriteInt32(int32_t value) { memcpy(p, &value, 4); p += 4; }
|
| 177 |
+
void WriteInt64(int64_t value) { memcpy(p, &value, 8); p += 8; }
|
| 178 |
+
void WriteDouble(double value) { memcpy(p, &value, 8); p += 8; }
|
| 179 |
+
#endif
|
| 180 |
+
void WriteBool(const Handle<Value>& value) { WriteByte(value->BooleanValue() ? 1 : 0); }
|
| 181 |
+
void WriteInt32(const Handle<Value>& value) { WriteInt32(value->Int32Value()); }
|
| 182 |
+
void WriteInt32(const Handle<Object>& object, const Handle<String>& key) { WriteInt32(object->Get(key)); }
|
| 183 |
+
void WriteInt64(const Handle<Value>& value) { WriteInt64(value->IntegerValue()); }
|
| 184 |
+
void WriteDouble(const Handle<Value>& value) { WriteDouble(value->NumberValue()); }
|
| 185 |
+
void WriteDouble(const Handle<Object>& object, const Handle<String>& key) { WriteDouble(object->Get(key)); }
|
| 186 |
+
void WriteUInt32String(uint32_t name) { p += sprintf(p, "%u", name) + 1; }
|
| 187 |
+
void WriteLengthPrefixedString(const Local<String>& value) { WriteInt32(value->Utf8Length()+1); WriteString(value); }
|
| 188 |
+
void WriteObjectId(const Handle<Object>& object, const Handle<String>& key);
|
| 189 |
+
void WriteString(const Local<String>& value) { p += value->WriteUtf8(p); } // This returns the number of bytes inclusive of the NULL terminator.
|
| 190 |
+
void WriteData(const char* data, size_t length) { memcpy(p, data, length); p += length; }
|
| 191 |
+
|
| 192 |
+
void* BeginWriteType() { void* returnValue = p; p++; return returnValue; }
|
| 193 |
+
void CommitType(void* beginPoint, BsonType value) { *reinterpret_cast<unsigned char*>(beginPoint) = value; }
|
| 194 |
+
void* BeginWriteSize() { void* returnValue = p; p += 4; return returnValue; }
|
| 195 |
+
|
| 196 |
+
#if USE_MISALIGNED_MEMORY_ACCESS
|
| 197 |
+
void CommitSize(void* beginPoint) { *reinterpret_cast<int32_t*>(beginPoint) = (int32_t) (p - (char*) beginPoint); }
|
| 198 |
+
#else
|
| 199 |
+
void CommitSize(void* beginPoint) { int32_t value = (int32_t) (p - (char*) beginPoint); memcpy(beginPoint, &value, 4); }
|
| 200 |
+
#endif
|
| 201 |
+
|
| 202 |
+
size_t GetSerializeSize() const { return p - destinationBuffer; }
|
| 203 |
+
|
| 204 |
+
void CheckKey(const Local<String>& keyName);
|
| 205 |
+
|
| 206 |
+
protected:
|
| 207 |
+
char *const destinationBuffer; // base, never changes
|
| 208 |
+
char* p; // cursor into buffer
|
| 209 |
+
};
|
| 210 |
+
|
| 211 |
+
template<typename T> class BSONSerializer : public T
|
| 212 |
+
{
|
| 213 |
+
private:
|
| 214 |
+
typedef T Inherited;
|
| 215 |
+
|
| 216 |
+
public:
|
| 217 |
+
BSONSerializer(BSON* aBson, bool aCheckKeys, bool aSerializeFunctions) : Inherited(), checkKeys(aCheckKeys), serializeFunctions(aSerializeFunctions), bson(aBson) { }
|
| 218 |
+
BSONSerializer(BSON* aBson, bool aCheckKeys, bool aSerializeFunctions, char* parentParam) : Inherited(parentParam), checkKeys(aCheckKeys), serializeFunctions(aSerializeFunctions), bson(aBson) { }
|
| 219 |
+
|
| 220 |
+
void SerializeDocument(const Handle<Value>& value);
|
| 221 |
+
void SerializeArray(const Handle<Value>& value);
|
| 222 |
+
void SerializeValue(void* typeLocation, const Handle<Value> value);
|
| 223 |
+
|
| 224 |
+
private:
|
| 225 |
+
bool checkKeys;
|
| 226 |
+
bool serializeFunctions;
|
| 227 |
+
BSON* bson;
|
| 228 |
+
};
|
| 229 |
+
|
| 230 |
+
//===========================================================================
|
| 231 |
+
|
| 232 |
+
class BSONDeserializer
|
| 233 |
+
{
|
| 234 |
+
public:
|
| 235 |
+
BSONDeserializer(BSON* aBson, char* data, size_t length);
|
| 236 |
+
BSONDeserializer(BSONDeserializer& parentSerializer, size_t length);
|
| 237 |
+
|
| 238 |
+
Handle<Value> DeserializeDocument(bool promoteLongs);
|
| 239 |
+
|
| 240 |
+
bool HasMoreData() const { return p < pEnd; }
|
| 241 |
+
Handle<Value> ReadCString();
|
| 242 |
+
uint32_t ReadIntegerString();
|
| 243 |
+
int32_t ReadRegexOptions();
|
| 244 |
+
Local<String> ReadString();
|
| 245 |
+
Local<String> ReadObjectId();
|
| 246 |
+
|
| 247 |
+
unsigned char ReadByte() { return *reinterpret_cast<unsigned char*>(p++); }
|
| 248 |
+
#if USE_MISALIGNED_MEMORY_ACCESS
|
| 249 |
+
int32_t ReadInt32() { int32_t returnValue = *reinterpret_cast<int32_t*>(p); p += 4; return returnValue; }
|
| 250 |
+
uint32_t ReadUInt32() { uint32_t returnValue = *reinterpret_cast<uint32_t*>(p); p += 4; return returnValue; }
|
| 251 |
+
int64_t ReadInt64() { int64_t returnValue = *reinterpret_cast<int64_t*>(p); p += 8; return returnValue; }
|
| 252 |
+
double ReadDouble() { double returnValue = *reinterpret_cast<double*>(p); p += 8; return returnValue; }
|
| 253 |
+
#else
|
| 254 |
+
int32_t ReadInt32() { int32_t returnValue; memcpy(&returnValue, p, 4); p += 4; return returnValue; }
|
| 255 |
+
uint32_t ReadUInt32() { uint32_t returnValue; memcpy(&returnValue, p, 4); p += 4; return returnValue; }
|
| 256 |
+
int64_t ReadInt64() { int64_t returnValue; memcpy(&returnValue, p, 8); p += 8; return returnValue; }
|
| 257 |
+
double ReadDouble() { double returnValue; memcpy(&returnValue, p, 8); p += 8; return returnValue; }
|
| 258 |
+
#endif
|
| 259 |
+
|
| 260 |
+
size_t GetSerializeSize() const { return p - pStart; }
|
| 261 |
+
|
| 262 |
+
private:
|
| 263 |
+
Handle<Value> DeserializeArray(bool promoteLongs);
|
| 264 |
+
Handle<Value> DeserializeValue(BsonType type, bool promoteLongs);
|
| 265 |
+
Handle<Value> DeserializeDocumentInternal(bool promoteLongs);
|
| 266 |
+
Handle<Value> DeserializeArrayInternal(bool promoteLongs);
|
| 267 |
+
|
| 268 |
+
BSON* bson;
|
| 269 |
+
char* const pStart;
|
| 270 |
+
char* p;
|
| 271 |
+
char* const pEnd;
|
| 272 |
+
};
|
| 273 |
+
|
| 274 |
+
//===========================================================================
|
| 275 |
+
|
| 276 |
+
#endif // BSON_H_
|
| 277 |
+
|
| 278 |
+
//===========================================================================
|