bd2ee6c7286a20d04011e715e0684b82b7898ff2
[aai/esr-gui.git] /
1 "use strict"
2
3 var writeIEEE754 = require('./float_parser').writeIEEE754,
4         readIEEE754 = require('./float_parser').readIEEE754,
5   Map = require('./map'),
6         Long = require('./long'),
7   Double = require('./double'),
8   Timestamp = require('./timestamp'),
9   ObjectID = require('./objectid'),
10   BSONRegExp = require('./regexp'),
11   Symbol = require('./symbol'),
12         Int32 = require('./int_32'),
13   Code = require('./code'),
14         Decimal128 = require('./decimal128'),
15   MinKey = require('./min_key'),
16   MaxKey = require('./max_key'),
17   DBRef = require('./db_ref'),
18   Binary = require('./binary');
19
20 // Parts of the parser
21 var deserialize = require('./parser/deserializer'),
22         serializer = require('./parser/serializer'),
23         calculateObjectSize = require('./parser/calculate_size');
24
25 /**
26  * @ignore
27  * @api private
28  */
29 // Max Size
30 var MAXSIZE = (1024*1024*17);
31 // Max Document Buffer size
32 var buffer = new Buffer(MAXSIZE);
33
34 var BSON = function() {
35 }
36
37 /**
38  * Serialize a Javascript object.
39  *
40  * @param {Object} object the Javascript object to serialize.
41  * @param {Boolean} checkKeys the serializer will check if keys are valid.
42  * @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
43  * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
44  * @return {Buffer} returns the Buffer object containing the serialized object.
45  * @api public
46  */
47 BSON.prototype.serialize = function serialize(object, checkKeys, asBuffer, serializeFunctions, index, ignoreUndefined) {
48         // Attempt to serialize
49         var serializationIndex = serializer(buffer, object, checkKeys, index || 0, 0, serializeFunctions, ignoreUndefined, []);
50         // Create the final buffer
51         var finishedBuffer = new Buffer(serializationIndex);
52         // Copy into the finished buffer
53         buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
54         // Return the buffer
55         return finishedBuffer;
56 }
57
58 /**
59  * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
60  *
61  * @param {Object} object the Javascript object to serialize.
62  * @param {Boolean} checkKeys the serializer will check if keys are valid.
63  * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
64  * @param {Number} index the index in the buffer where we wish to start serializing into.
65  * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
66  * @return {Number} returns the index pointing to the last written byte in the buffer.
67  * @api public
68  */
69 BSON.prototype.serializeWithBufferAndIndex = function(object, checkKeys, finalBuffer, startIndex, serializeFunctions, ignoreUndefined) {
70         // Attempt to serialize
71         var serializationIndex = serializer(buffer, object, checkKeys, startIndex || 0, 0, serializeFunctions, ignoreUndefined);
72         buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
73         // Return the index
74         return serializationIndex - 1;
75 }
76
77 /**
78  * Deserialize data as BSON.
79  *
80  * Options
81  *  - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
82  *  - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
83  *  - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
84  *  - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
85  *
86  * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
87  * @param {Object} [options] additional options used for the deserialization.
88  * @param {Boolean} [isArray] ignore used for recursive parsing.
89  * @return {Object} returns the deserialized Javascript Object.
90  * @api public
91  */
92 BSON.prototype.deserialize = function(data, options) {
93   return deserialize(data, options);
94 }
95
96 /**
97  * Calculate the bson size for a passed in Javascript object.
98  *
99  * @param {Object} object the Javascript object to calculate the BSON byte size for.
100  * @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**.
101  * @return {Number} returns the number of bytes the BSON object will take up.
102  * @api public
103  */
104 BSON.prototype.calculateObjectSize = function(object, serializeFunctions, ignoreUndefined) {
105   return calculateObjectSize(object, serializeFunctions, ignoreUndefined);
106 }
107
108 /**
109  * Deserialize stream data as BSON documents.
110  *
111  * Options
112  *  - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
113  *  - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
114  *  - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
115  *  - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
116  *
117  * @param {Buffer} data the buffer containing the serialized set of BSON documents.
118  * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
119  * @param {Number} numberOfDocuments number of documents to deserialize.
120  * @param {Array} documents an array where to store the deserialized documents.
121  * @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
122  * @param {Object} [options] additional options used for the deserialization.
123  * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
124  * @api public
125  */
126 BSON.prototype.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
127   // if(numberOfDocuments !== documents.length) throw new Error("Number of expected results back is less than the number of documents");
128   options = options != null ? options : {};
129   var index = startIndex;
130   // Loop over all documents
131   for(var i = 0; i < numberOfDocuments; i++) {
132     // Find size of the document
133     var size = data[index] | data[index + 1] << 8 | data[index + 2] << 16 | data[index + 3] << 24;
134     // Update options with index
135     options['index'] = index;
136     // Parse the document at this point
137     documents[docStartIndex + i] = this.deserialize(data, options);
138     // Adjust index by the document size
139     index = index + size;
140   }
141
142   // Return object containing end index of parsing and list of documents
143   return index;
144 }
145
146 /**
147  * @ignore
148  * @api private
149  */
150 // BSON MAX VALUES
151 BSON.BSON_INT32_MAX = 0x7FFFFFFF;
152 BSON.BSON_INT32_MIN = -0x80000000;
153
154 BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
155 BSON.BSON_INT64_MIN = -Math.pow(2, 63);
156
157 // JS MAX PRECISE VALUES
158 BSON.JS_INT_MAX = 0x20000000000000;  // Any integer up to 2^53 can be precisely represented by a double.
159 BSON.JS_INT_MIN = -0x20000000000000;  // Any integer down to -2^53 can be precisely represented by a double.
160
161 // Internal long versions
162 var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000);  // Any integer up to 2^53 can be precisely represented by a double.
163 var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000);  // Any integer down to -2^53 can be precisely represented by a double.
164
165 /**
166  * Number BSON Type
167  *
168  * @classconstant BSON_DATA_NUMBER
169  **/
170 BSON.BSON_DATA_NUMBER = 1;
171 /**
172  * String BSON Type
173  *
174  * @classconstant BSON_DATA_STRING
175  **/
176 BSON.BSON_DATA_STRING = 2;
177 /**
178  * Object BSON Type
179  *
180  * @classconstant BSON_DATA_OBJECT
181  **/
182 BSON.BSON_DATA_OBJECT = 3;
183 /**
184  * Array BSON Type
185  *
186  * @classconstant BSON_DATA_ARRAY
187  **/
188 BSON.BSON_DATA_ARRAY = 4;
189 /**
190  * Binary BSON Type
191  *
192  * @classconstant BSON_DATA_BINARY
193  **/
194 BSON.BSON_DATA_BINARY = 5;
195 /**
196  * ObjectID BSON Type
197  *
198  * @classconstant BSON_DATA_OID
199  **/
200 BSON.BSON_DATA_OID = 7;
201 /**
202  * Boolean BSON Type
203  *
204  * @classconstant BSON_DATA_BOOLEAN
205  **/
206 BSON.BSON_DATA_BOOLEAN = 8;
207 /**
208  * Date BSON Type
209  *
210  * @classconstant BSON_DATA_DATE
211  **/
212 BSON.BSON_DATA_DATE = 9;
213 /**
214  * null BSON Type
215  *
216  * @classconstant BSON_DATA_NULL
217  **/
218 BSON.BSON_DATA_NULL = 10;
219 /**
220  * RegExp BSON Type
221  *
222  * @classconstant BSON_DATA_REGEXP
223  **/
224 BSON.BSON_DATA_REGEXP = 11;
225 /**
226  * Code BSON Type
227  *
228  * @classconstant BSON_DATA_CODE
229  **/
230 BSON.BSON_DATA_CODE = 13;
231 /**
232  * Symbol BSON Type
233  *
234  * @classconstant BSON_DATA_SYMBOL
235  **/
236 BSON.BSON_DATA_SYMBOL = 14;
237 /**
238  * Code with Scope BSON Type
239  *
240  * @classconstant BSON_DATA_CODE_W_SCOPE
241  **/
242 BSON.BSON_DATA_CODE_W_SCOPE = 15;
243 /**
244  * 32 bit Integer BSON Type
245  *
246  * @classconstant BSON_DATA_INT
247  **/
248 BSON.BSON_DATA_INT = 16;
249 /**
250  * Timestamp BSON Type
251  *
252  * @classconstant BSON_DATA_TIMESTAMP
253  **/
254 BSON.BSON_DATA_TIMESTAMP = 17;
255 /**
256  * Long BSON Type
257  *
258  * @classconstant BSON_DATA_LONG
259  **/
260 BSON.BSON_DATA_LONG = 18;
261 /**
262  * MinKey BSON Type
263  *
264  * @classconstant BSON_DATA_MIN_KEY
265  **/
266 BSON.BSON_DATA_MIN_KEY = 0xff;
267 /**
268  * MaxKey BSON Type
269  *
270  * @classconstant BSON_DATA_MAX_KEY
271  **/
272 BSON.BSON_DATA_MAX_KEY = 0x7f;
273
274 /**
275  * Binary Default Type
276  *
277  * @classconstant BSON_BINARY_SUBTYPE_DEFAULT
278  **/
279 BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
280 /**
281  * Binary Function Type
282  *
283  * @classconstant BSON_BINARY_SUBTYPE_FUNCTION
284  **/
285 BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
286 /**
287  * Binary Byte Array Type
288  *
289  * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
290  **/
291 BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
292 /**
293  * Binary UUID Type
294  *
295  * @classconstant BSON_BINARY_SUBTYPE_UUID
296  **/
297 BSON.BSON_BINARY_SUBTYPE_UUID = 3;
298 /**
299  * Binary MD5 Type
300  *
301  * @classconstant BSON_BINARY_SUBTYPE_MD5
302  **/
303 BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
304 /**
305  * Binary User Defined Type
306  *
307  * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
308  **/
309 BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
310
311 // Return BSON
312 module.exports = BSON;
313 module.exports.Code = Code;
314 module.exports.Map = Map;
315 module.exports.Symbol = Symbol;
316 module.exports.BSON = BSON;
317 module.exports.DBRef = DBRef;
318 module.exports.Binary = Binary;
319 module.exports.ObjectID = ObjectID;
320 module.exports.Long = Long;
321 module.exports.Timestamp = Timestamp;
322 module.exports.Double = Double;
323 module.exports.Int32 = Int32;
324 module.exports.MinKey = MinKey;
325 module.exports.MaxKey = MaxKey;
326 module.exports.BSONRegExp = BSONRegExp;
327 module.exports.Decimal128 = Decimal128;