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