bson 4.2.2 → 4.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +44 -0
- package/bower.json +1 -1
- package/bson.d.ts +77 -6
- package/dist/bson.browser.esm.js +2116 -1292
- package/dist/bson.browser.esm.js.map +1 -1
- package/dist/bson.browser.umd.js +2172 -1347
- package/dist/bson.browser.umd.js.map +1 -1
- package/dist/bson.bundle.js +2174 -1347
- package/dist/bson.bundle.js.map +1 -1
- package/dist/bson.esm.js +2061 -1241
- package/dist/bson.esm.js.map +1 -1
- package/lib/binary.js +71 -60
- package/lib/binary.js.map +1 -1
- package/lib/bson.js +60 -52
- package/lib/bson.js.map +1 -1
- package/lib/code.js +18 -15
- package/lib/code.js.map +1 -1
- package/lib/db_ref.js +40 -30
- package/lib/db_ref.js.map +1 -1
- package/lib/decimal128.js +135 -124
- package/lib/decimal128.js.map +1 -1
- package/lib/double.js +24 -21
- package/lib/double.js.map +1 -1
- package/lib/ensure_buffer.js +4 -7
- package/lib/ensure_buffer.js.map +1 -1
- package/lib/extended_json.js +113 -71
- package/lib/extended_json.js.map +1 -1
- package/lib/float_parser.js +21 -21
- package/lib/float_parser.js.map +1 -1
- package/lib/int_32.js +19 -16
- package/lib/int_32.js.map +1 -1
- package/lib/long.js +248 -230
- package/lib/long.js.map +1 -1
- package/lib/map.js +54 -45
- package/lib/map.js.map +1 -1
- package/lib/max_key.js +16 -11
- package/lib/max_key.js.map +1 -1
- package/lib/min_key.js +16 -11
- package/lib/min_key.js.map +1 -1
- package/lib/objectid.js +96 -93
- package/lib/objectid.js.map +1 -1
- package/lib/parser/calculate_size.js +17 -15
- package/lib/parser/calculate_size.js.map +1 -1
- package/lib/parser/deserializer.js +135 -123
- package/lib/parser/deserializer.js.map +1 -1
- package/lib/parser/serializer.js +108 -88
- package/lib/parser/serializer.js.map +1 -1
- package/lib/parser/utils.js +47 -25
- package/lib/parser/utils.js.map +1 -1
- package/lib/regexp.js +16 -15
- package/lib/regexp.js.map +1 -1
- package/lib/symbol.js +21 -18
- package/lib/symbol.js.map +1 -1
- package/lib/timestamp.js +50 -27
- package/lib/timestamp.js.map +1 -1
- package/lib/uuid.js +173 -42
- package/lib/uuid.js.map +1 -1
- package/lib/uuid_utils.js +34 -0
- package/lib/uuid_utils.js.map +1 -0
- package/lib/validate_utf8.js +12 -12
- package/lib/validate_utf8.js.map +1 -1
- package/package.json +5 -4
- package/src/binary.ts +20 -6
- package/src/bson.ts +3 -0
- package/src/code.ts +6 -2
- package/src/db_ref.ts +14 -5
- package/src/decimal128.ts +14 -5
- package/src/double.ts +4 -2
- package/src/ensure_buffer.ts +7 -7
- package/src/extended_json.ts +64 -16
- package/src/int_32.ts +4 -2
- package/src/long.ts +22 -8
- package/src/max_key.ts +5 -1
- package/src/min_key.ts +5 -1
- package/src/objectid.ts +15 -20
- package/src/parser/calculate_size.ts +8 -11
- package/src/parser/deserializer.ts +46 -36
- package/src/parser/serializer.ts +13 -12
- package/src/parser/utils.ts +49 -17
- package/src/regexp.ts +5 -5
- package/src/symbol.ts +4 -2
- package/src/timestamp.ts +6 -1
- package/src/uuid.ts +192 -40
- package/src/uuid_utils.ts +31 -0
|
@@ -1,45 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.deserialize = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
var buffer_1 = require("buffer");
|
|
5
|
+
var binary_1 = require("../binary");
|
|
6
|
+
var code_1 = require("../code");
|
|
7
|
+
var constants = require("../constants");
|
|
8
|
+
var db_ref_1 = require("../db_ref");
|
|
9
|
+
var decimal128_1 = require("../decimal128");
|
|
10
|
+
var double_1 = require("../double");
|
|
11
|
+
var int_32_1 = require("../int_32");
|
|
12
|
+
var long_1 = require("../long");
|
|
13
|
+
var max_key_1 = require("../max_key");
|
|
14
|
+
var min_key_1 = require("../min_key");
|
|
15
|
+
var objectid_1 = require("../objectid");
|
|
16
|
+
var regexp_1 = require("../regexp");
|
|
17
|
+
var symbol_1 = require("../symbol");
|
|
18
|
+
var timestamp_1 = require("../timestamp");
|
|
19
|
+
var validate_utf8_1 = require("../validate_utf8");
|
|
20
20
|
// Internal long versions
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
var JS_INT_MAX_LONG = long_1.Long.fromNumber(constants.JS_INT_MAX);
|
|
22
|
+
var JS_INT_MIN_LONG = long_1.Long.fromNumber(constants.JS_INT_MIN);
|
|
23
|
+
var functionCache = {};
|
|
24
24
|
function deserialize(buffer, options, isArray) {
|
|
25
25
|
options = options == null ? {} : options;
|
|
26
|
-
|
|
26
|
+
var index = options && options.index ? options.index : 0;
|
|
27
27
|
// Read the document size
|
|
28
|
-
|
|
28
|
+
var size = buffer[index] |
|
|
29
29
|
(buffer[index + 1] << 8) |
|
|
30
30
|
(buffer[index + 2] << 16) |
|
|
31
31
|
(buffer[index + 3] << 24);
|
|
32
32
|
if (size < 5) {
|
|
33
|
-
throw new Error(
|
|
33
|
+
throw new Error("bson size must be >= 5, is " + size);
|
|
34
34
|
}
|
|
35
35
|
if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
|
|
36
|
-
throw new Error(
|
|
36
|
+
throw new Error("buffer length " + buffer.length + " must be >= bson size " + size);
|
|
37
37
|
}
|
|
38
38
|
if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
|
|
39
|
-
throw new Error(
|
|
39
|
+
throw new Error("buffer length " + buffer.length + " must === bson size " + size);
|
|
40
40
|
}
|
|
41
41
|
if (size + index > buffer.byteLength) {
|
|
42
|
-
throw new Error(
|
|
42
|
+
throw new Error("(bson size " + size + " + options.index " + index + " must be <= buffer length " + buffer.byteLength + ")");
|
|
43
43
|
}
|
|
44
44
|
// Illegal end value
|
|
45
45
|
if (buffer[index + size - 1] !== 0) {
|
|
@@ -49,42 +49,43 @@ function deserialize(buffer, options, isArray) {
|
|
|
49
49
|
return deserializeObject(buffer, index, options, isArray);
|
|
50
50
|
}
|
|
51
51
|
exports.deserialize = deserialize;
|
|
52
|
-
function deserializeObject(buffer, index, options, isArray
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
function deserializeObject(buffer, index, options, isArray) {
|
|
53
|
+
if (isArray === void 0) { isArray = false; }
|
|
54
|
+
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
|
|
55
|
+
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
|
|
56
|
+
var fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
|
|
56
57
|
// Return raw bson buffer instead of parsing it
|
|
57
|
-
|
|
58
|
+
var raw = options['raw'] == null ? false : options['raw'];
|
|
58
59
|
// Return BSONRegExp objects instead of native regular expressions
|
|
59
|
-
|
|
60
|
+
var bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
|
|
60
61
|
// Controls the promotion of values vs wrapper classes
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
|
|
63
|
+
var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
|
|
64
|
+
var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
|
|
64
65
|
// Set the start index
|
|
65
|
-
|
|
66
|
+
var startIndex = index;
|
|
66
67
|
// Validate that we have at least 4 bytes of buffer
|
|
67
68
|
if (buffer.length < 5)
|
|
68
69
|
throw new Error('corrupt bson message < 5 bytes long');
|
|
69
70
|
// Read the document size
|
|
70
|
-
|
|
71
|
+
var size = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
|
|
71
72
|
// Ensure buffer is valid size
|
|
72
73
|
if (size < 5 || size > buffer.length)
|
|
73
74
|
throw new Error('corrupt bson message');
|
|
74
75
|
// Create holding object
|
|
75
|
-
|
|
76
|
+
var object = isArray ? [] : {};
|
|
76
77
|
// Used for arrays to skip having to perform utf8 decoding
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
var arrayIndex = 0;
|
|
79
|
+
var done = false;
|
|
79
80
|
// While we have more left data left keep parsing
|
|
80
81
|
while (!done) {
|
|
81
82
|
// Read the type
|
|
82
|
-
|
|
83
|
+
var elementType = buffer[index++];
|
|
83
84
|
// If we get a zero it's the last byte, exit
|
|
84
85
|
if (elementType === 0)
|
|
85
86
|
break;
|
|
86
87
|
// Get the start search index
|
|
87
|
-
|
|
88
|
+
var i = index;
|
|
88
89
|
// Locate the end of the c string
|
|
89
90
|
while (buffer[i] !== 0x00 && i < buffer.length) {
|
|
90
91
|
i++;
|
|
@@ -92,10 +93,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
92
93
|
// If are at the end of the buffer there is a problem with the document
|
|
93
94
|
if (i >= buffer.byteLength)
|
|
94
95
|
throw new Error('Bad BSON Document: illegal CString');
|
|
95
|
-
|
|
96
|
+
var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
|
|
97
|
+
var value = void 0;
|
|
96
98
|
index = i + 1;
|
|
97
99
|
if (elementType === constants.BSON_DATA_STRING) {
|
|
98
|
-
|
|
100
|
+
var stringSize = buffer[index++] |
|
|
99
101
|
(buffer[index++] << 8) |
|
|
100
102
|
(buffer[index++] << 16) |
|
|
101
103
|
(buffer[index++] << 24);
|
|
@@ -106,53 +108,52 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
106
108
|
if (!validate_utf8_1.validateUtf8(buffer, index, index + stringSize - 1)) {
|
|
107
109
|
throw new Error('Invalid UTF-8 string in BSON document');
|
|
108
110
|
}
|
|
109
|
-
|
|
110
|
-
object[name] = s;
|
|
111
|
+
value = buffer.toString('utf8', index, index + stringSize - 1);
|
|
111
112
|
index = index + stringSize;
|
|
112
113
|
}
|
|
113
114
|
else if (elementType === constants.BSON_DATA_OID) {
|
|
114
|
-
|
|
115
|
+
var oid = buffer_1.Buffer.alloc(12);
|
|
115
116
|
buffer.copy(oid, 0, index, index + 12);
|
|
116
|
-
|
|
117
|
+
value = new objectid_1.ObjectId(oid);
|
|
117
118
|
index = index + 12;
|
|
118
119
|
}
|
|
119
120
|
else if (elementType === constants.BSON_DATA_INT && promoteValues === false) {
|
|
120
|
-
|
|
121
|
+
value = new int_32_1.Int32(buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24));
|
|
121
122
|
}
|
|
122
123
|
else if (elementType === constants.BSON_DATA_INT) {
|
|
123
|
-
|
|
124
|
+
value =
|
|
124
125
|
buffer[index++] |
|
|
125
126
|
(buffer[index++] << 8) |
|
|
126
127
|
(buffer[index++] << 16) |
|
|
127
128
|
(buffer[index++] << 24);
|
|
128
129
|
}
|
|
129
130
|
else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
|
|
130
|
-
|
|
131
|
+
value = new double_1.Double(buffer.readDoubleLE(index));
|
|
131
132
|
index = index + 8;
|
|
132
133
|
}
|
|
133
134
|
else if (elementType === constants.BSON_DATA_NUMBER) {
|
|
134
|
-
|
|
135
|
+
value = buffer.readDoubleLE(index);
|
|
135
136
|
index = index + 8;
|
|
136
137
|
}
|
|
137
138
|
else if (elementType === constants.BSON_DATA_DATE) {
|
|
138
|
-
|
|
139
|
+
var lowBits = buffer[index++] |
|
|
139
140
|
(buffer[index++] << 8) |
|
|
140
141
|
(buffer[index++] << 16) |
|
|
141
142
|
(buffer[index++] << 24);
|
|
142
|
-
|
|
143
|
+
var highBits = buffer[index++] |
|
|
143
144
|
(buffer[index++] << 8) |
|
|
144
145
|
(buffer[index++] << 16) |
|
|
145
146
|
(buffer[index++] << 24);
|
|
146
|
-
|
|
147
|
+
value = new Date(new long_1.Long(lowBits, highBits).toNumber());
|
|
147
148
|
}
|
|
148
149
|
else if (elementType === constants.BSON_DATA_BOOLEAN) {
|
|
149
150
|
if (buffer[index] !== 0 && buffer[index] !== 1)
|
|
150
151
|
throw new Error('illegal boolean type value');
|
|
151
|
-
|
|
152
|
+
value = buffer[index++] === 1;
|
|
152
153
|
}
|
|
153
154
|
else if (elementType === constants.BSON_DATA_OBJECT) {
|
|
154
|
-
|
|
155
|
-
|
|
155
|
+
var _index = index;
|
|
156
|
+
var objectSize = buffer[index] |
|
|
156
157
|
(buffer[index + 1] << 8) |
|
|
157
158
|
(buffer[index + 2] << 16) |
|
|
158
159
|
(buffer[index + 3] << 24);
|
|
@@ -160,31 +161,31 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
160
161
|
throw new Error('bad embedded document length in bson');
|
|
161
162
|
// We have a raw value
|
|
162
163
|
if (raw) {
|
|
163
|
-
|
|
164
|
+
value = buffer.slice(index, index + objectSize);
|
|
164
165
|
}
|
|
165
166
|
else {
|
|
166
|
-
|
|
167
|
+
value = deserializeObject(buffer, _index, options, false);
|
|
167
168
|
}
|
|
168
169
|
index = index + objectSize;
|
|
169
170
|
}
|
|
170
171
|
else if (elementType === constants.BSON_DATA_ARRAY) {
|
|
171
|
-
|
|
172
|
-
|
|
172
|
+
var _index = index;
|
|
173
|
+
var objectSize = buffer[index] |
|
|
173
174
|
(buffer[index + 1] << 8) |
|
|
174
175
|
(buffer[index + 2] << 16) |
|
|
175
176
|
(buffer[index + 3] << 24);
|
|
176
|
-
|
|
177
|
+
var arrayOptions = options;
|
|
177
178
|
// Stop index
|
|
178
|
-
|
|
179
|
+
var stopIndex = index + objectSize;
|
|
179
180
|
// All elements of array to be returned as raw bson
|
|
180
181
|
if (fieldsAsRaw && fieldsAsRaw[name]) {
|
|
181
182
|
arrayOptions = {};
|
|
182
|
-
for (
|
|
183
|
+
for (var n in options) {
|
|
183
184
|
arrayOptions[n] = options[n];
|
|
184
185
|
}
|
|
185
186
|
arrayOptions['raw'] = true;
|
|
186
187
|
}
|
|
187
|
-
|
|
188
|
+
value = deserializeObject(buffer, _index, arrayOptions, true);
|
|
188
189
|
index = index + objectSize;
|
|
189
190
|
if (buffer[index - 1] !== 0)
|
|
190
191
|
throw new Error('invalid array terminator byte');
|
|
@@ -192,57 +193,57 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
192
193
|
throw new Error('corrupted array bson');
|
|
193
194
|
}
|
|
194
195
|
else if (elementType === constants.BSON_DATA_UNDEFINED) {
|
|
195
|
-
|
|
196
|
+
value = undefined;
|
|
196
197
|
}
|
|
197
198
|
else if (elementType === constants.BSON_DATA_NULL) {
|
|
198
|
-
|
|
199
|
+
value = null;
|
|
199
200
|
}
|
|
200
201
|
else if (elementType === constants.BSON_DATA_LONG) {
|
|
201
202
|
// Unpack the low and high bits
|
|
202
|
-
|
|
203
|
+
var lowBits = buffer[index++] |
|
|
203
204
|
(buffer[index++] << 8) |
|
|
204
205
|
(buffer[index++] << 16) |
|
|
205
206
|
(buffer[index++] << 24);
|
|
206
|
-
|
|
207
|
+
var highBits = buffer[index++] |
|
|
207
208
|
(buffer[index++] << 8) |
|
|
208
209
|
(buffer[index++] << 16) |
|
|
209
210
|
(buffer[index++] << 24);
|
|
210
|
-
|
|
211
|
+
var long = new long_1.Long(lowBits, highBits);
|
|
211
212
|
// Promote the long if possible
|
|
212
213
|
if (promoteLongs && promoteValues === true) {
|
|
213
|
-
|
|
214
|
+
value =
|
|
214
215
|
long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
|
|
215
216
|
? long.toNumber()
|
|
216
217
|
: long;
|
|
217
218
|
}
|
|
218
219
|
else {
|
|
219
|
-
|
|
220
|
+
value = long;
|
|
220
221
|
}
|
|
221
222
|
}
|
|
222
223
|
else if (elementType === constants.BSON_DATA_DECIMAL128) {
|
|
223
224
|
// Buffer to contain the decimal bytes
|
|
224
|
-
|
|
225
|
+
var bytes = buffer_1.Buffer.alloc(16);
|
|
225
226
|
// Copy the next 16 bytes into the bytes buffer
|
|
226
227
|
buffer.copy(bytes, 0, index, index + 16);
|
|
227
228
|
// Update index
|
|
228
229
|
index = index + 16;
|
|
229
230
|
// Assign the new Decimal128 value
|
|
230
|
-
|
|
231
|
+
var decimal128 = new decimal128_1.Decimal128(bytes);
|
|
231
232
|
// If we have an alternative mapper use that
|
|
232
233
|
if ('toObject' in decimal128 && typeof decimal128.toObject === 'function') {
|
|
233
|
-
|
|
234
|
+
value = decimal128.toObject();
|
|
234
235
|
}
|
|
235
236
|
else {
|
|
236
|
-
|
|
237
|
+
value = decimal128;
|
|
237
238
|
}
|
|
238
239
|
}
|
|
239
240
|
else if (elementType === constants.BSON_DATA_BINARY) {
|
|
240
|
-
|
|
241
|
+
var binarySize = buffer[index++] |
|
|
241
242
|
(buffer[index++] << 8) |
|
|
242
243
|
(buffer[index++] << 16) |
|
|
243
244
|
(buffer[index++] << 24);
|
|
244
|
-
|
|
245
|
-
|
|
245
|
+
var totalBinarySize = binarySize;
|
|
246
|
+
var subType = buffer[index++];
|
|
246
247
|
// Did we have a negative binary size, throw
|
|
247
248
|
if (binarySize < 0)
|
|
248
249
|
throw new Error('Negative binary type element size found');
|
|
@@ -266,14 +267,14 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
266
267
|
throw new Error('Binary type with subtype 0x02 contains too short binary size');
|
|
267
268
|
}
|
|
268
269
|
if (promoteBuffers && promoteValues) {
|
|
269
|
-
|
|
270
|
+
value = buffer.slice(index, index + binarySize);
|
|
270
271
|
}
|
|
271
272
|
else {
|
|
272
|
-
|
|
273
|
+
value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType);
|
|
273
274
|
}
|
|
274
275
|
}
|
|
275
276
|
else {
|
|
276
|
-
|
|
277
|
+
var _buffer = buffer_1.Buffer.alloc(binarySize);
|
|
277
278
|
// If we have subtype 2 skip the 4 bytes for the size
|
|
278
279
|
if (subType === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
|
|
279
280
|
binarySize =
|
|
@@ -293,10 +294,10 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
293
294
|
_buffer[i] = buffer[index + i];
|
|
294
295
|
}
|
|
295
296
|
if (promoteBuffers && promoteValues) {
|
|
296
|
-
|
|
297
|
+
value = _buffer;
|
|
297
298
|
}
|
|
298
299
|
else {
|
|
299
|
-
|
|
300
|
+
value = new binary_1.Binary(_buffer, subType);
|
|
300
301
|
}
|
|
301
302
|
}
|
|
302
303
|
// Update the index
|
|
@@ -313,7 +314,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
313
314
|
if (i >= buffer.length)
|
|
314
315
|
throw new Error('Bad BSON Document: illegal CString');
|
|
315
316
|
// Return the C string
|
|
316
|
-
|
|
317
|
+
var source = buffer.toString('utf8', index, i);
|
|
317
318
|
// Create the regexp
|
|
318
319
|
index = i + 1;
|
|
319
320
|
// Get the start search index
|
|
@@ -326,10 +327,10 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
326
327
|
if (i >= buffer.length)
|
|
327
328
|
throw new Error('Bad BSON Document: illegal CString');
|
|
328
329
|
// Return the C string
|
|
329
|
-
|
|
330
|
+
var regExpOptions = buffer.toString('utf8', index, i);
|
|
330
331
|
index = i + 1;
|
|
331
332
|
// For each option add the corresponding one for javascript
|
|
332
|
-
|
|
333
|
+
var optionsArray = new Array(regExpOptions.length);
|
|
333
334
|
// Parse options
|
|
334
335
|
for (i = 0; i < regExpOptions.length; i++) {
|
|
335
336
|
switch (regExpOptions[i]) {
|
|
@@ -344,7 +345,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
344
345
|
break;
|
|
345
346
|
}
|
|
346
347
|
}
|
|
347
|
-
|
|
348
|
+
value = new RegExp(source, optionsArray.join(''));
|
|
348
349
|
}
|
|
349
350
|
else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === true) {
|
|
350
351
|
// Get the start search index
|
|
@@ -357,7 +358,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
357
358
|
if (i >= buffer.length)
|
|
358
359
|
throw new Error('Bad BSON Document: illegal CString');
|
|
359
360
|
// Return the C string
|
|
360
|
-
|
|
361
|
+
var source = buffer.toString('utf8', index, i);
|
|
361
362
|
index = i + 1;
|
|
362
363
|
// Get the start search index
|
|
363
364
|
i = index;
|
|
@@ -369,13 +370,13 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
369
370
|
if (i >= buffer.length)
|
|
370
371
|
throw new Error('Bad BSON Document: illegal CString');
|
|
371
372
|
// Return the C string
|
|
372
|
-
|
|
373
|
+
var regExpOptions = buffer.toString('utf8', index, i);
|
|
373
374
|
index = i + 1;
|
|
374
375
|
// Set the object
|
|
375
|
-
|
|
376
|
+
value = new regexp_1.BSONRegExp(source, regExpOptions);
|
|
376
377
|
}
|
|
377
378
|
else if (elementType === constants.BSON_DATA_SYMBOL) {
|
|
378
|
-
|
|
379
|
+
var stringSize = buffer[index++] |
|
|
379
380
|
(buffer[index++] << 8) |
|
|
380
381
|
(buffer[index++] << 16) |
|
|
381
382
|
(buffer[index++] << 24);
|
|
@@ -383,29 +384,29 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
383
384
|
stringSize > buffer.length - index ||
|
|
384
385
|
buffer[index + stringSize - 1] !== 0)
|
|
385
386
|
throw new Error('bad string length in bson');
|
|
386
|
-
|
|
387
|
-
|
|
387
|
+
var symbol = buffer.toString('utf8', index, index + stringSize - 1);
|
|
388
|
+
value = promoteValues ? symbol : new symbol_1.BSONSymbol(symbol);
|
|
388
389
|
index = index + stringSize;
|
|
389
390
|
}
|
|
390
391
|
else if (elementType === constants.BSON_DATA_TIMESTAMP) {
|
|
391
|
-
|
|
392
|
+
var lowBits = buffer[index++] |
|
|
392
393
|
(buffer[index++] << 8) |
|
|
393
394
|
(buffer[index++] << 16) |
|
|
394
395
|
(buffer[index++] << 24);
|
|
395
|
-
|
|
396
|
+
var highBits = buffer[index++] |
|
|
396
397
|
(buffer[index++] << 8) |
|
|
397
398
|
(buffer[index++] << 16) |
|
|
398
399
|
(buffer[index++] << 24);
|
|
399
|
-
|
|
400
|
+
value = new timestamp_1.Timestamp(lowBits, highBits);
|
|
400
401
|
}
|
|
401
402
|
else if (elementType === constants.BSON_DATA_MIN_KEY) {
|
|
402
|
-
|
|
403
|
+
value = new min_key_1.MinKey();
|
|
403
404
|
}
|
|
404
405
|
else if (elementType === constants.BSON_DATA_MAX_KEY) {
|
|
405
|
-
|
|
406
|
+
value = new max_key_1.MaxKey();
|
|
406
407
|
}
|
|
407
408
|
else if (elementType === constants.BSON_DATA_CODE) {
|
|
408
|
-
|
|
409
|
+
var stringSize = buffer[index++] |
|
|
409
410
|
(buffer[index++] << 8) |
|
|
410
411
|
(buffer[index++] << 16) |
|
|
411
412
|
(buffer[index++] << 24);
|
|
@@ -413,26 +414,26 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
413
414
|
stringSize > buffer.length - index ||
|
|
414
415
|
buffer[index + stringSize - 1] !== 0)
|
|
415
416
|
throw new Error('bad string length in bson');
|
|
416
|
-
|
|
417
|
+
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
|
|
417
418
|
// If we are evaluating the functions
|
|
418
419
|
if (evalFunctions) {
|
|
419
420
|
// If we have cache enabled let's look for the md5 of the function in the cache
|
|
420
421
|
if (cacheFunctions) {
|
|
421
422
|
// Got to do this to avoid V8 deoptimizing the call due to finding eval
|
|
422
|
-
|
|
423
|
+
value = isolateEval(functionString, functionCache, object);
|
|
423
424
|
}
|
|
424
425
|
else {
|
|
425
|
-
|
|
426
|
+
value = isolateEval(functionString);
|
|
426
427
|
}
|
|
427
428
|
}
|
|
428
429
|
else {
|
|
429
|
-
|
|
430
|
+
value = new code_1.Code(functionString);
|
|
430
431
|
}
|
|
431
432
|
// Update parse index position
|
|
432
433
|
index = index + stringSize;
|
|
433
434
|
}
|
|
434
435
|
else if (elementType === constants.BSON_DATA_CODE_W_SCOPE) {
|
|
435
|
-
|
|
436
|
+
var totalSize = buffer[index++] |
|
|
436
437
|
(buffer[index++] << 8) |
|
|
437
438
|
(buffer[index++] << 16) |
|
|
438
439
|
(buffer[index++] << 24);
|
|
@@ -441,7 +442,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
441
442
|
throw new Error('code_w_scope total size shorter minimum expected length');
|
|
442
443
|
}
|
|
443
444
|
// Get the code string size
|
|
444
|
-
|
|
445
|
+
var stringSize = buffer[index++] |
|
|
445
446
|
(buffer[index++] << 8) |
|
|
446
447
|
(buffer[index++] << 16) |
|
|
447
448
|
(buffer[index++] << 24);
|
|
@@ -451,18 +452,18 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
451
452
|
buffer[index + stringSize - 1] !== 0)
|
|
452
453
|
throw new Error('bad string length in bson');
|
|
453
454
|
// Javascript function
|
|
454
|
-
|
|
455
|
+
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
|
|
455
456
|
// Update parse index position
|
|
456
457
|
index = index + stringSize;
|
|
457
458
|
// Parse the element
|
|
458
|
-
|
|
459
|
+
var _index = index;
|
|
459
460
|
// Decode the size of the object document
|
|
460
|
-
|
|
461
|
+
var objectSize = buffer[index] |
|
|
461
462
|
(buffer[index + 1] << 8) |
|
|
462
463
|
(buffer[index + 2] << 16) |
|
|
463
464
|
(buffer[index + 3] << 24);
|
|
464
465
|
// Decode the scope object
|
|
465
|
-
|
|
466
|
+
var scopeObject = deserializeObject(buffer, _index, options, false);
|
|
466
467
|
// Adjust the index
|
|
467
468
|
index = index + objectSize;
|
|
468
469
|
// Check if field length is too short
|
|
@@ -478,20 +479,20 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
478
479
|
// If we have cache enabled let's look for the md5 of the function in the cache
|
|
479
480
|
if (cacheFunctions) {
|
|
480
481
|
// Got to do this to avoid V8 deoptimizing the call due to finding eval
|
|
481
|
-
|
|
482
|
+
value = isolateEval(functionString, functionCache, object);
|
|
482
483
|
}
|
|
483
484
|
else {
|
|
484
|
-
|
|
485
|
+
value = isolateEval(functionString);
|
|
485
486
|
}
|
|
486
|
-
|
|
487
|
+
value.scope = scopeObject;
|
|
487
488
|
}
|
|
488
489
|
else {
|
|
489
|
-
|
|
490
|
+
value = new code_1.Code(functionString, scopeObject);
|
|
490
491
|
}
|
|
491
492
|
}
|
|
492
493
|
else if (elementType === constants.BSON_DATA_DBPOINTER) {
|
|
493
494
|
// Get the code string size
|
|
494
|
-
|
|
495
|
+
var stringSize = buffer[index++] |
|
|
495
496
|
(buffer[index++] << 8) |
|
|
496
497
|
(buffer[index++] << 16) |
|
|
497
498
|
(buffer[index++] << 24);
|
|
@@ -504,21 +505,32 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
504
505
|
if (!validate_utf8_1.validateUtf8(buffer, index, index + stringSize - 1)) {
|
|
505
506
|
throw new Error('Invalid UTF-8 string in BSON document');
|
|
506
507
|
}
|
|
507
|
-
|
|
508
|
+
var namespace = buffer.toString('utf8', index, index + stringSize - 1);
|
|
508
509
|
// Update parse index position
|
|
509
510
|
index = index + stringSize;
|
|
510
511
|
// Read the oid
|
|
511
|
-
|
|
512
|
+
var oidBuffer = buffer_1.Buffer.alloc(12);
|
|
512
513
|
buffer.copy(oidBuffer, 0, index, index + 12);
|
|
513
|
-
|
|
514
|
+
var oid = new objectid_1.ObjectId(oidBuffer);
|
|
514
515
|
// Update the index
|
|
515
516
|
index = index + 12;
|
|
516
517
|
// Upgrade to DBRef type
|
|
517
|
-
|
|
518
|
+
value = new db_ref_1.DBRef(namespace, oid);
|
|
518
519
|
}
|
|
519
520
|
else {
|
|
520
521
|
throw new Error('Detected unknown BSON type ' + elementType.toString(16) + ' for fieldname "' + name + '"');
|
|
521
522
|
}
|
|
523
|
+
if (name === '__proto__') {
|
|
524
|
+
Object.defineProperty(object, name, {
|
|
525
|
+
value: value,
|
|
526
|
+
writable: true,
|
|
527
|
+
enumerable: true,
|
|
528
|
+
configurable: true
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
object[name] = value;
|
|
533
|
+
}
|
|
522
534
|
}
|
|
523
535
|
// Check if the deserialization was against a valid array/object
|
|
524
536
|
if (size !== index - startIndex) {
|
|
@@ -527,9 +539,9 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
527
539
|
throw new Error('corrupt object bson');
|
|
528
540
|
}
|
|
529
541
|
// check if object's $ keys are those of a DBRef
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
dollarKeys.forEach(k
|
|
542
|
+
var dollarKeys = Object.keys(object).filter(function (k) { return k.startsWith('$'); });
|
|
543
|
+
var valid = true;
|
|
544
|
+
dollarKeys.forEach(function (k) {
|
|
533
545
|
if (['$ref', '$id', '$db'].indexOf(k) === -1)
|
|
534
546
|
valid = false;
|
|
535
547
|
});
|
|
@@ -537,7 +549,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
537
549
|
if (!valid)
|
|
538
550
|
return object;
|
|
539
551
|
if (db_ref_1.isDBRefLike(object)) {
|
|
540
|
-
|
|
552
|
+
var copy = Object.assign({}, object);
|
|
541
553
|
delete copy.$ref;
|
|
542
554
|
delete copy.$id;
|
|
543
555
|
delete copy.$db;
|