bson 4.1.0 → 4.2.3
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 +48 -1
- package/README.md +7 -9
- package/bower.json +1 -1
- package/bson.d.ts +986 -0
- package/dist/bson.browser.esm.js +7811 -5011
- package/dist/bson.browser.esm.js.map +1 -0
- package/dist/bson.browser.umd.js +7862 -5099
- package/dist/bson.browser.umd.js.map +1 -0
- package/dist/bson.bundle.js +8723 -9228
- package/dist/bson.bundle.js.map +1 -0
- package/dist/bson.esm.js +5728 -4951
- package/dist/bson.esm.js.map +1 -0
- package/etc/prepare.js +19 -0
- package/lib/binary.js +218 -398
- package/lib/binary.js.map +1 -0
- package/lib/bson.js +201 -240
- package/lib/bson.js.map +1 -0
- package/lib/code.js +41 -41
- package/lib/code.js.map +1 -0
- package/lib/constants.js +78 -203
- package/lib/constants.js.map +1 -0
- package/lib/db_ref.js +88 -81
- package/lib/db_ref.js.map +1 -0
- package/lib/decimal128.js +667 -777
- package/lib/decimal128.js.map +1 -0
- package/lib/double.js +68 -70
- package/lib/double.js.map +1 -0
- package/lib/ensure_buffer.js +22 -18
- package/lib/ensure_buffer.js.map +1 -0
- package/lib/extended_json.js +310 -321
- package/lib/extended_json.js.map +1 -0
- package/lib/float_parser.js +98 -104
- package/lib/float_parser.js.map +1 -0
- package/lib/int_32.js +50 -49
- package/lib/int_32.js.map +1 -0
- package/lib/long.js +881 -16
- package/lib/long.js.map +1 -0
- package/lib/map.js +130 -122
- package/lib/map.js.map +1 -0
- package/lib/max_key.js +28 -25
- package/lib/max_key.js.map +1 -0
- package/lib/min_key.js +28 -25
- package/lib/min_key.js.map +1 -0
- package/lib/objectid.js +300 -410
- package/lib/objectid.js.map +1 -0
- package/lib/parser/calculate_size.js +188 -224
- package/lib/parser/calculate_size.js.map +1 -0
- package/lib/parser/deserializer.js +545 -621
- package/lib/parser/deserializer.js.map +1 -0
- package/lib/parser/serializer.js +798 -923
- package/lib/parser/serializer.js.map +1 -0
- package/lib/parser/utils.js +92 -30
- package/lib/parser/utils.js.map +1 -0
- package/lib/regexp.js +61 -74
- package/lib/regexp.js.map +1 -0
- package/lib/symbol.js +45 -58
- package/lib/symbol.js.map +1 -0
- package/lib/timestamp.js +91 -95
- package/lib/timestamp.js.map +1 -0
- package/lib/uuid.js +48 -0
- package/lib/uuid.js.map +1 -0
- package/lib/validate_utf8.js +41 -42
- package/lib/validate_utf8.js.map +1 -0
- package/package.json +53 -31
- package/src/binary.ts +272 -0
- package/src/bson.ts +326 -0
- package/src/code.ts +61 -0
- package/src/constants.ts +104 -0
- package/src/db_ref.ts +119 -0
- package/src/decimal128.ts +803 -0
- package/src/double.ts +87 -0
- package/src/ensure_buffer.ts +26 -0
- package/src/extended_json.ts +395 -0
- package/src/float_parser.ts +152 -0
- package/src/int_32.ts +66 -0
- package/src/long.ts +1002 -0
- package/src/map.ts +139 -0
- package/src/max_key.ts +37 -0
- package/src/min_key.ts +37 -0
- package/src/objectid.ts +379 -0
- package/src/parser/calculate_size.ts +230 -0
- package/src/parser/deserializer.ts +655 -0
- package/src/parser/serializer.ts +1069 -0
- package/src/parser/utils.ts +93 -0
- package/src/regexp.ts +94 -0
- package/src/symbol.ts +59 -0
- package/src/timestamp.ts +108 -0
- package/src/uuid.ts +57 -0
- package/src/validate_utf8.ts +47 -0
- package/lib/fnv1a.js +0 -48
package/lib/parser/serializer.js
CHANGED
|
@@ -1,551 +1,375 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serializeInto = void 0;
|
|
4
|
+
var binary_1 = require("../binary");
|
|
5
|
+
var constants = require("../constants");
|
|
6
|
+
var ensure_buffer_1 = require("../ensure_buffer");
|
|
7
|
+
var extended_json_1 = require("../extended_json");
|
|
8
|
+
var float_parser_1 = require("../float_parser");
|
|
9
|
+
var long_1 = require("../long");
|
|
10
|
+
var map_1 = require("../map");
|
|
11
|
+
var utils_1 = require("./utils");
|
|
12
|
+
var regexp = /\x00/; // eslint-disable-line no-control-regex
|
|
13
|
+
var ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
|
|
14
|
+
function isRegExp(d) {
|
|
15
|
+
return Object.prototype.toString.call(d) === '[object RegExp]';
|
|
16
|
+
}
|
|
17
|
+
/*
|
|
18
|
+
* isArray indicates if we are writing to a BSON array (type 0x04)
|
|
19
|
+
* which forces the "key" which really an array index as a string to be written as ascii
|
|
20
|
+
* This will catch any errors in index as a string generation
|
|
21
|
+
*/
|
|
23
22
|
function serializeString(buffer, key, value, index, isArray) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
// Encode String type
|
|
24
|
+
buffer[index++] = constants.BSON_DATA_STRING;
|
|
25
|
+
// Number of written bytes
|
|
26
|
+
var numberOfWrittenBytes = !isArray
|
|
27
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
28
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
29
|
+
// Encode the name
|
|
30
|
+
index = index + numberOfWrittenBytes + 1;
|
|
31
|
+
buffer[index - 1] = 0;
|
|
32
|
+
// Write the string
|
|
33
|
+
var size = buffer.write(value, index + 4, undefined, 'utf8');
|
|
34
|
+
// Write the size of the string to buffer
|
|
35
|
+
buffer[index + 3] = ((size + 1) >> 24) & 0xff;
|
|
36
|
+
buffer[index + 2] = ((size + 1) >> 16) & 0xff;
|
|
37
|
+
buffer[index + 1] = ((size + 1) >> 8) & 0xff;
|
|
38
|
+
buffer[index] = (size + 1) & 0xff;
|
|
39
|
+
// Update index
|
|
40
|
+
index = index + 4 + size;
|
|
41
|
+
// Write zero
|
|
42
|
+
buffer[index++] = 0;
|
|
43
|
+
return index;
|
|
45
44
|
}
|
|
46
|
-
|
|
47
45
|
function serializeNumber(buffer, key, value, index, isArray) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
buffer[index++] = (value >> 8) & 0xff;
|
|
69
|
-
buffer[index++] = (value >> 16) & 0xff;
|
|
70
|
-
buffer[index++] = (value >> 24) & 0xff;
|
|
71
|
-
} else if (value >= constants.JS_INT_MIN && value <= constants.JS_INT_MAX) {
|
|
72
|
-
// Encode as double
|
|
73
|
-
buffer[index++] = constants.BSON_DATA_NUMBER;
|
|
74
|
-
// Number of written bytes
|
|
75
|
-
const numberOfWrittenBytes = !isArray
|
|
76
|
-
? buffer.write(key, index, 'utf8')
|
|
77
|
-
: buffer.write(key, index, 'ascii');
|
|
78
|
-
// Encode the name
|
|
79
|
-
index = index + numberOfWrittenBytes;
|
|
80
|
-
buffer[index++] = 0;
|
|
81
|
-
// Write float
|
|
82
|
-
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
|
83
|
-
// Ajust index
|
|
84
|
-
index = index + 8;
|
|
85
|
-
} else {
|
|
86
|
-
// Set long type
|
|
87
|
-
buffer[index++] = constants.BSON_DATA_LONG;
|
|
88
|
-
// Number of written bytes
|
|
89
|
-
const numberOfWrittenBytes = !isArray
|
|
90
|
-
? buffer.write(key, index, 'utf8')
|
|
91
|
-
: buffer.write(key, index, 'ascii');
|
|
92
|
-
// Encode the name
|
|
93
|
-
index = index + numberOfWrittenBytes;
|
|
94
|
-
buffer[index++] = 0;
|
|
95
|
-
const longVal = Long.fromNumber(value);
|
|
96
|
-
const lowBits = longVal.getLowBits();
|
|
97
|
-
const highBits = longVal.getHighBits();
|
|
98
|
-
// Encode low bits
|
|
99
|
-
buffer[index++] = lowBits & 0xff;
|
|
100
|
-
buffer[index++] = (lowBits >> 8) & 0xff;
|
|
101
|
-
buffer[index++] = (lowBits >> 16) & 0xff;
|
|
102
|
-
buffer[index++] = (lowBits >> 24) & 0xff;
|
|
103
|
-
// Encode high bits
|
|
104
|
-
buffer[index++] = highBits & 0xff;
|
|
105
|
-
buffer[index++] = (highBits >> 8) & 0xff;
|
|
106
|
-
buffer[index++] = (highBits >> 16) & 0xff;
|
|
107
|
-
buffer[index++] = (highBits >> 24) & 0xff;
|
|
46
|
+
// We have an integer value
|
|
47
|
+
// TODO(NODE-2529): Add support for big int
|
|
48
|
+
if (Number.isInteger(value) &&
|
|
49
|
+
value >= constants.BSON_INT32_MIN &&
|
|
50
|
+
value <= constants.BSON_INT32_MAX) {
|
|
51
|
+
// If the value fits in 32 bits encode as int32
|
|
52
|
+
// Set int type 32 bits or less
|
|
53
|
+
buffer[index++] = constants.BSON_DATA_INT;
|
|
54
|
+
// Number of written bytes
|
|
55
|
+
var numberOfWrittenBytes = !isArray
|
|
56
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
57
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
58
|
+
// Encode the name
|
|
59
|
+
index = index + numberOfWrittenBytes;
|
|
60
|
+
buffer[index++] = 0;
|
|
61
|
+
// Write the int value
|
|
62
|
+
buffer[index++] = value & 0xff;
|
|
63
|
+
buffer[index++] = (value >> 8) & 0xff;
|
|
64
|
+
buffer[index++] = (value >> 16) & 0xff;
|
|
65
|
+
buffer[index++] = (value >> 24) & 0xff;
|
|
108
66
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
67
|
+
else {
|
|
68
|
+
// Encode as double
|
|
69
|
+
buffer[index++] = constants.BSON_DATA_NUMBER;
|
|
70
|
+
// Number of written bytes
|
|
71
|
+
var numberOfWrittenBytes = !isArray
|
|
72
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
73
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
74
|
+
// Encode the name
|
|
75
|
+
index = index + numberOfWrittenBytes;
|
|
76
|
+
buffer[index++] = 0;
|
|
77
|
+
// Write float
|
|
78
|
+
float_parser_1.writeIEEE754(buffer, value, index, 'little', 52, 8);
|
|
79
|
+
// Adjust index
|
|
80
|
+
index = index + 8;
|
|
81
|
+
}
|
|
82
|
+
return index;
|
|
83
|
+
}
|
|
84
|
+
function serializeNull(buffer, key, _, index, isArray) {
|
|
85
|
+
// Set long type
|
|
86
|
+
buffer[index++] = constants.BSON_DATA_NULL;
|
|
112
87
|
// Number of written bytes
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
88
|
+
var numberOfWrittenBytes = !isArray
|
|
89
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
90
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
116
91
|
// Encode the name
|
|
117
92
|
index = index + numberOfWrittenBytes;
|
|
118
93
|
buffer[index++] = 0;
|
|
119
|
-
|
|
120
|
-
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
|
121
|
-
// Ajust index
|
|
122
|
-
index = index + 8;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return index;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function serializeNull(buffer, key, value, index, isArray) {
|
|
129
|
-
// Set long type
|
|
130
|
-
buffer[index++] = constants.BSON_DATA_NULL;
|
|
131
|
-
|
|
132
|
-
// Number of written bytes
|
|
133
|
-
const numberOfWrittenBytes = !isArray
|
|
134
|
-
? buffer.write(key, index, 'utf8')
|
|
135
|
-
: buffer.write(key, index, 'ascii');
|
|
136
|
-
|
|
137
|
-
// Encode the name
|
|
138
|
-
index = index + numberOfWrittenBytes;
|
|
139
|
-
buffer[index++] = 0;
|
|
140
|
-
return index;
|
|
94
|
+
return index;
|
|
141
95
|
}
|
|
142
|
-
|
|
143
96
|
function serializeBoolean(buffer, key, value, index, isArray) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
97
|
+
// Write the type
|
|
98
|
+
buffer[index++] = constants.BSON_DATA_BOOLEAN;
|
|
99
|
+
// Number of written bytes
|
|
100
|
+
var numberOfWrittenBytes = !isArray
|
|
101
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
102
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
103
|
+
// Encode the name
|
|
104
|
+
index = index + numberOfWrittenBytes;
|
|
105
|
+
buffer[index++] = 0;
|
|
106
|
+
// Encode the boolean value
|
|
107
|
+
buffer[index++] = value ? 1 : 0;
|
|
108
|
+
return index;
|
|
156
109
|
}
|
|
157
|
-
|
|
158
110
|
function serializeDate(buffer, key, value, index, isArray) {
|
|
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
|
-
return index;
|
|
111
|
+
// Write the type
|
|
112
|
+
buffer[index++] = constants.BSON_DATA_DATE;
|
|
113
|
+
// Number of written bytes
|
|
114
|
+
var numberOfWrittenBytes = !isArray
|
|
115
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
116
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
117
|
+
// Encode the name
|
|
118
|
+
index = index + numberOfWrittenBytes;
|
|
119
|
+
buffer[index++] = 0;
|
|
120
|
+
// Write the date
|
|
121
|
+
var dateInMilis = long_1.Long.fromNumber(value.getTime());
|
|
122
|
+
var lowBits = dateInMilis.getLowBits();
|
|
123
|
+
var highBits = dateInMilis.getHighBits();
|
|
124
|
+
// Encode low bits
|
|
125
|
+
buffer[index++] = lowBits & 0xff;
|
|
126
|
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
|
127
|
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
|
128
|
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
|
129
|
+
// Encode high bits
|
|
130
|
+
buffer[index++] = highBits & 0xff;
|
|
131
|
+
buffer[index++] = (highBits >> 8) & 0xff;
|
|
132
|
+
buffer[index++] = (highBits >> 16) & 0xff;
|
|
133
|
+
buffer[index++] = (highBits >> 24) & 0xff;
|
|
134
|
+
return index;
|
|
184
135
|
}
|
|
185
|
-
|
|
186
136
|
function serializeRegExp(buffer, key, value, index, isArray) {
|
|
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
|
-
|
|
137
|
+
// Write the type
|
|
138
|
+
buffer[index++] = constants.BSON_DATA_REGEXP;
|
|
139
|
+
// Number of written bytes
|
|
140
|
+
var numberOfWrittenBytes = !isArray
|
|
141
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
142
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
143
|
+
// Encode the name
|
|
144
|
+
index = index + numberOfWrittenBytes;
|
|
145
|
+
buffer[index++] = 0;
|
|
146
|
+
if (value.source && value.source.match(regexp) != null) {
|
|
147
|
+
throw Error('value ' + value.source + ' must not contain null bytes');
|
|
148
|
+
}
|
|
149
|
+
// Adjust the index
|
|
150
|
+
index = index + buffer.write(value.source, index, undefined, 'utf8');
|
|
151
|
+
// Write zero
|
|
152
|
+
buffer[index++] = 0x00;
|
|
153
|
+
// Write the parameters
|
|
154
|
+
if (value.ignoreCase)
|
|
155
|
+
buffer[index++] = 0x69; // i
|
|
156
|
+
if (value.global)
|
|
157
|
+
buffer[index++] = 0x73; // s
|
|
158
|
+
if (value.multiline)
|
|
159
|
+
buffer[index++] = 0x6d; // m
|
|
160
|
+
// Add ending zero
|
|
161
|
+
buffer[index++] = 0x00;
|
|
162
|
+
return index;
|
|
212
163
|
}
|
|
213
|
-
|
|
214
164
|
function serializeBSONRegExp(buffer, key, value, index, isArray) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
index
|
|
239
|
-
buffer.write(
|
|
240
|
-
value.options
|
|
241
|
-
.split('')
|
|
242
|
-
.sort()
|
|
243
|
-
.join(''),
|
|
244
|
-
index,
|
|
245
|
-
'utf8'
|
|
246
|
-
);
|
|
247
|
-
// Add ending zero
|
|
248
|
-
buffer[index++] = 0x00;
|
|
249
|
-
return index;
|
|
165
|
+
// Write the type
|
|
166
|
+
buffer[index++] = constants.BSON_DATA_REGEXP;
|
|
167
|
+
// Number of written bytes
|
|
168
|
+
var numberOfWrittenBytes = !isArray
|
|
169
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
170
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
171
|
+
// Encode the name
|
|
172
|
+
index = index + numberOfWrittenBytes;
|
|
173
|
+
buffer[index++] = 0;
|
|
174
|
+
// Check the pattern for 0 bytes
|
|
175
|
+
if (value.pattern.match(regexp) != null) {
|
|
176
|
+
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
177
|
+
// null-terminated.
|
|
178
|
+
throw Error('pattern ' + value.pattern + ' must not contain null bytes');
|
|
179
|
+
}
|
|
180
|
+
// Adjust the index
|
|
181
|
+
index = index + buffer.write(value.pattern, index, undefined, 'utf8');
|
|
182
|
+
// Write zero
|
|
183
|
+
buffer[index++] = 0x00;
|
|
184
|
+
// Write the options
|
|
185
|
+
index = index + buffer.write(value.options.split('').sort().join(''), index, undefined, 'utf8');
|
|
186
|
+
// Add ending zero
|
|
187
|
+
buffer[index++] = 0x00;
|
|
188
|
+
return index;
|
|
250
189
|
}
|
|
251
|
-
|
|
252
190
|
function serializeMinMax(buffer, key, value, index, isArray) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
191
|
+
// Write the type of either min or max key
|
|
192
|
+
if (value === null) {
|
|
193
|
+
buffer[index++] = constants.BSON_DATA_NULL;
|
|
194
|
+
}
|
|
195
|
+
else if (value._bsontype === 'MinKey') {
|
|
196
|
+
buffer[index++] = constants.BSON_DATA_MIN_KEY;
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
buffer[index++] = constants.BSON_DATA_MAX_KEY;
|
|
200
|
+
}
|
|
201
|
+
// Number of written bytes
|
|
202
|
+
var numberOfWrittenBytes = !isArray
|
|
203
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
204
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
205
|
+
// Encode the name
|
|
206
|
+
index = index + numberOfWrittenBytes;
|
|
207
|
+
buffer[index++] = 0;
|
|
208
|
+
return index;
|
|
270
209
|
}
|
|
271
|
-
|
|
272
210
|
function serializeObjectId(buffer, key, value, index, isArray) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
return index + 12;
|
|
211
|
+
// Write the type
|
|
212
|
+
buffer[index++] = constants.BSON_DATA_OID;
|
|
213
|
+
// Number of written bytes
|
|
214
|
+
var numberOfWrittenBytes = !isArray
|
|
215
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
216
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
217
|
+
// Encode the name
|
|
218
|
+
index = index + numberOfWrittenBytes;
|
|
219
|
+
buffer[index++] = 0;
|
|
220
|
+
// Write the objectId into the shared buffer
|
|
221
|
+
if (typeof value.id === 'string') {
|
|
222
|
+
buffer.write(value.id, index, undefined, 'binary');
|
|
223
|
+
}
|
|
224
|
+
else if (value.id && value.id.copy) {
|
|
225
|
+
value.id.copy(buffer, index, 0, 12);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
|
|
229
|
+
}
|
|
230
|
+
// Adjust index
|
|
231
|
+
return index + 12;
|
|
295
232
|
}
|
|
296
|
-
|
|
297
233
|
function serializeBuffer(buffer, key, value, index, isArray) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
234
|
+
// Write the type
|
|
235
|
+
buffer[index++] = constants.BSON_DATA_BINARY;
|
|
236
|
+
// Number of written bytes
|
|
237
|
+
var numberOfWrittenBytes = !isArray
|
|
238
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
239
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
240
|
+
// Encode the name
|
|
241
|
+
index = index + numberOfWrittenBytes;
|
|
242
|
+
buffer[index++] = 0;
|
|
243
|
+
// Get size of the buffer (current write point)
|
|
244
|
+
var size = value.length;
|
|
245
|
+
// Write the size of the string to buffer
|
|
246
|
+
buffer[index++] = size & 0xff;
|
|
247
|
+
buffer[index++] = (size >> 8) & 0xff;
|
|
248
|
+
buffer[index++] = (size >> 16) & 0xff;
|
|
249
|
+
buffer[index++] = (size >> 24) & 0xff;
|
|
250
|
+
// Write the default subtype
|
|
251
|
+
buffer[index++] = constants.BSON_BINARY_SUBTYPE_DEFAULT;
|
|
252
|
+
// Copy the content form the binary field to the buffer
|
|
253
|
+
buffer.set(ensure_buffer_1.ensureBuffer(value), index);
|
|
254
|
+
// Adjust the index
|
|
255
|
+
index = index + size;
|
|
256
|
+
return index;
|
|
321
257
|
}
|
|
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
|
-
index = index + numberOfWrittenBytes;
|
|
349
|
-
buffer[index++] = 0;
|
|
350
|
-
const endIndex = serializeInto(
|
|
351
|
-
buffer,
|
|
352
|
-
value,
|
|
353
|
-
checkKeys,
|
|
354
|
-
index,
|
|
355
|
-
depth + 1,
|
|
356
|
-
serializeFunctions,
|
|
357
|
-
ignoreUndefined,
|
|
358
|
-
path
|
|
359
|
-
);
|
|
360
|
-
// Pop stack
|
|
361
|
-
path.pop();
|
|
362
|
-
return endIndex;
|
|
258
|
+
function serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, isArray, path) {
|
|
259
|
+
if (checkKeys === void 0) { checkKeys = false; }
|
|
260
|
+
if (depth === void 0) { depth = 0; }
|
|
261
|
+
if (serializeFunctions === void 0) { serializeFunctions = false; }
|
|
262
|
+
if (ignoreUndefined === void 0) { ignoreUndefined = true; }
|
|
263
|
+
if (isArray === void 0) { isArray = false; }
|
|
264
|
+
if (path === void 0) { path = []; }
|
|
265
|
+
for (var i = 0; i < path.length; i++) {
|
|
266
|
+
if (path[i] === value)
|
|
267
|
+
throw new Error('cyclic dependency detected');
|
|
268
|
+
}
|
|
269
|
+
// Push value to stack
|
|
270
|
+
path.push(value);
|
|
271
|
+
// Write the type
|
|
272
|
+
buffer[index++] = Array.isArray(value) ? constants.BSON_DATA_ARRAY : constants.BSON_DATA_OBJECT;
|
|
273
|
+
// Number of written bytes
|
|
274
|
+
var numberOfWrittenBytes = !isArray
|
|
275
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
276
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
277
|
+
// Encode the name
|
|
278
|
+
index = index + numberOfWrittenBytes;
|
|
279
|
+
buffer[index++] = 0;
|
|
280
|
+
var endIndex = serializeInto(buffer, value, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined, path);
|
|
281
|
+
// Pop stack
|
|
282
|
+
path.pop();
|
|
283
|
+
return endIndex;
|
|
363
284
|
}
|
|
364
|
-
|
|
365
285
|
function serializeDecimal128(buffer, key, value, index, isArray) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
286
|
+
buffer[index++] = constants.BSON_DATA_DECIMAL128;
|
|
287
|
+
// Number of written bytes
|
|
288
|
+
var numberOfWrittenBytes = !isArray
|
|
289
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
290
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
291
|
+
// Encode the name
|
|
292
|
+
index = index + numberOfWrittenBytes;
|
|
293
|
+
buffer[index++] = 0;
|
|
294
|
+
// Write the data from the value
|
|
295
|
+
value.bytes.copy(buffer, index, 0, 16);
|
|
296
|
+
return index + 16;
|
|
377
297
|
}
|
|
378
|
-
|
|
379
298
|
function serializeLong(buffer, key, value, index, isArray) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
299
|
+
// Write the type
|
|
300
|
+
buffer[index++] =
|
|
301
|
+
value._bsontype === 'Long' ? constants.BSON_DATA_LONG : constants.BSON_DATA_TIMESTAMP;
|
|
302
|
+
// Number of written bytes
|
|
303
|
+
var numberOfWrittenBytes = !isArray
|
|
304
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
305
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
306
|
+
// Encode the name
|
|
307
|
+
index = index + numberOfWrittenBytes;
|
|
308
|
+
buffer[index++] = 0;
|
|
309
|
+
// Write the date
|
|
310
|
+
var lowBits = value.getLowBits();
|
|
311
|
+
var highBits = value.getHighBits();
|
|
312
|
+
// Encode low bits
|
|
313
|
+
buffer[index++] = lowBits & 0xff;
|
|
314
|
+
buffer[index++] = (lowBits >> 8) & 0xff;
|
|
315
|
+
buffer[index++] = (lowBits >> 16) & 0xff;
|
|
316
|
+
buffer[index++] = (lowBits >> 24) & 0xff;
|
|
317
|
+
// Encode high bits
|
|
318
|
+
buffer[index++] = highBits & 0xff;
|
|
319
|
+
buffer[index++] = (highBits >> 8) & 0xff;
|
|
320
|
+
buffer[index++] = (highBits >> 16) & 0xff;
|
|
321
|
+
buffer[index++] = (highBits >> 24) & 0xff;
|
|
322
|
+
return index;
|
|
404
323
|
}
|
|
405
|
-
|
|
406
324
|
function serializeInt32(buffer, key, value, index, isArray) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
325
|
+
value = value.valueOf();
|
|
326
|
+
// Set int type 32 bits or less
|
|
327
|
+
buffer[index++] = constants.BSON_DATA_INT;
|
|
328
|
+
// Number of written bytes
|
|
329
|
+
var numberOfWrittenBytes = !isArray
|
|
330
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
331
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
332
|
+
// Encode the name
|
|
333
|
+
index = index + numberOfWrittenBytes;
|
|
334
|
+
buffer[index++] = 0;
|
|
335
|
+
// Write the int value
|
|
336
|
+
buffer[index++] = value & 0xff;
|
|
337
|
+
buffer[index++] = (value >> 8) & 0xff;
|
|
338
|
+
buffer[index++] = (value >> 16) & 0xff;
|
|
339
|
+
buffer[index++] = (value >> 24) & 0xff;
|
|
340
|
+
return index;
|
|
422
341
|
}
|
|
423
|
-
|
|
424
342
|
function serializeDouble(buffer, key, value, index, isArray) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
// Number of written bytes
|
|
429
|
-
const numberOfWrittenBytes = !isArray
|
|
430
|
-
? buffer.write(key, index, 'utf8')
|
|
431
|
-
: buffer.write(key, index, 'ascii');
|
|
432
|
-
|
|
433
|
-
// Encode the name
|
|
434
|
-
index = index + numberOfWrittenBytes;
|
|
435
|
-
buffer[index++] = 0;
|
|
436
|
-
|
|
437
|
-
// Write float
|
|
438
|
-
writeIEEE754(buffer, value.value, index, 'little', 52, 8);
|
|
439
|
-
|
|
440
|
-
// Adjust index
|
|
441
|
-
index = index + 8;
|
|
442
|
-
return index;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
function serializeFunction(buffer, key, value, index, checkKeys, depth, isArray) {
|
|
446
|
-
buffer[index++] = constants.BSON_DATA_CODE;
|
|
447
|
-
// Number of written bytes
|
|
448
|
-
const numberOfWrittenBytes = !isArray
|
|
449
|
-
? buffer.write(key, index, 'utf8')
|
|
450
|
-
: buffer.write(key, index, 'ascii');
|
|
451
|
-
// Encode the name
|
|
452
|
-
index = index + numberOfWrittenBytes;
|
|
453
|
-
buffer[index++] = 0;
|
|
454
|
-
// Function string
|
|
455
|
-
const functionString = normalizedFunctionString(value);
|
|
456
|
-
|
|
457
|
-
// Write the string
|
|
458
|
-
const size = buffer.write(functionString, index + 4, 'utf8') + 1;
|
|
459
|
-
// Write the size of the string to buffer
|
|
460
|
-
buffer[index] = size & 0xff;
|
|
461
|
-
buffer[index + 1] = (size >> 8) & 0xff;
|
|
462
|
-
buffer[index + 2] = (size >> 16) & 0xff;
|
|
463
|
-
buffer[index + 3] = (size >> 24) & 0xff;
|
|
464
|
-
// Update index
|
|
465
|
-
index = index + 4 + size - 1;
|
|
466
|
-
// Write zero
|
|
467
|
-
buffer[index++] = 0;
|
|
468
|
-
return index;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
function serializeCode(
|
|
472
|
-
buffer,
|
|
473
|
-
key,
|
|
474
|
-
value,
|
|
475
|
-
index,
|
|
476
|
-
checkKeys,
|
|
477
|
-
depth,
|
|
478
|
-
serializeFunctions,
|
|
479
|
-
ignoreUndefined,
|
|
480
|
-
isArray
|
|
481
|
-
) {
|
|
482
|
-
if (value.scope && typeof value.scope === 'object') {
|
|
483
|
-
// Write the type
|
|
484
|
-
buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE;
|
|
343
|
+
// Encode as double
|
|
344
|
+
buffer[index++] = constants.BSON_DATA_NUMBER;
|
|
485
345
|
// Number of written bytes
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
346
|
+
var numberOfWrittenBytes = !isArray
|
|
347
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
348
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
489
349
|
// Encode the name
|
|
490
350
|
index = index + numberOfWrittenBytes;
|
|
491
351
|
buffer[index++] = 0;
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
// Write string into buffer
|
|
502
|
-
const codeSize = buffer.write(functionString, index + 4, 'utf8') + 1;
|
|
503
|
-
// Write the size of the string to buffer
|
|
504
|
-
buffer[index] = codeSize & 0xff;
|
|
505
|
-
buffer[index + 1] = (codeSize >> 8) & 0xff;
|
|
506
|
-
buffer[index + 2] = (codeSize >> 16) & 0xff;
|
|
507
|
-
buffer[index + 3] = (codeSize >> 24) & 0xff;
|
|
508
|
-
// Write end 0
|
|
509
|
-
buffer[index + 4 + codeSize - 1] = 0;
|
|
510
|
-
// Write the
|
|
511
|
-
index = index + codeSize + 4;
|
|
512
|
-
|
|
513
|
-
//
|
|
514
|
-
// Serialize the scope value
|
|
515
|
-
const endIndex = serializeInto(
|
|
516
|
-
buffer,
|
|
517
|
-
value.scope,
|
|
518
|
-
checkKeys,
|
|
519
|
-
index,
|
|
520
|
-
depth + 1,
|
|
521
|
-
serializeFunctions,
|
|
522
|
-
ignoreUndefined
|
|
523
|
-
);
|
|
524
|
-
index = endIndex - 1;
|
|
525
|
-
|
|
526
|
-
// Writ the total
|
|
527
|
-
const totalSize = endIndex - startIndex;
|
|
528
|
-
|
|
529
|
-
// Write the total size of the object
|
|
530
|
-
buffer[startIndex++] = totalSize & 0xff;
|
|
531
|
-
buffer[startIndex++] = (totalSize >> 8) & 0xff;
|
|
532
|
-
buffer[startIndex++] = (totalSize >> 16) & 0xff;
|
|
533
|
-
buffer[startIndex++] = (totalSize >> 24) & 0xff;
|
|
534
|
-
// Write trailing zero
|
|
535
|
-
buffer[index++] = 0;
|
|
536
|
-
} else {
|
|
352
|
+
// Write float
|
|
353
|
+
float_parser_1.writeIEEE754(buffer, value.value, index, 'little', 52, 8);
|
|
354
|
+
// Adjust index
|
|
355
|
+
index = index + 8;
|
|
356
|
+
return index;
|
|
357
|
+
}
|
|
358
|
+
function serializeFunction(buffer, key, value, index, _checkKeys, _depth, isArray) {
|
|
359
|
+
if (_checkKeys === void 0) { _checkKeys = false; }
|
|
360
|
+
if (_depth === void 0) { _depth = 0; }
|
|
537
361
|
buffer[index++] = constants.BSON_DATA_CODE;
|
|
538
362
|
// Number of written bytes
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
363
|
+
var numberOfWrittenBytes = !isArray
|
|
364
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
365
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
542
366
|
// Encode the name
|
|
543
367
|
index = index + numberOfWrittenBytes;
|
|
544
368
|
buffer[index++] = 0;
|
|
545
369
|
// Function string
|
|
546
|
-
|
|
370
|
+
var functionString = utils_1.normalizedFunctionString(value);
|
|
547
371
|
// Write the string
|
|
548
|
-
|
|
372
|
+
var size = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
|
|
549
373
|
// Write the size of the string to buffer
|
|
550
374
|
buffer[index] = size & 0xff;
|
|
551
375
|
buffer[index + 1] = (size >> 8) & 0xff;
|
|
@@ -555,438 +379,489 @@ function serializeCode(
|
|
|
555
379
|
index = index + 4 + size - 1;
|
|
556
380
|
// Write zero
|
|
557
381
|
buffer[index++] = 0;
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
382
|
+
return index;
|
|
383
|
+
}
|
|
384
|
+
function serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, isArray) {
|
|
385
|
+
if (checkKeys === void 0) { checkKeys = false; }
|
|
386
|
+
if (depth === void 0) { depth = 0; }
|
|
387
|
+
if (serializeFunctions === void 0) { serializeFunctions = false; }
|
|
388
|
+
if (ignoreUndefined === void 0) { ignoreUndefined = true; }
|
|
389
|
+
if (isArray === void 0) { isArray = false; }
|
|
390
|
+
if (value.scope && typeof value.scope === 'object') {
|
|
391
|
+
// Write the type
|
|
392
|
+
buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE;
|
|
393
|
+
// Number of written bytes
|
|
394
|
+
var numberOfWrittenBytes = !isArray
|
|
395
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
396
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
397
|
+
// Encode the name
|
|
398
|
+
index = index + numberOfWrittenBytes;
|
|
399
|
+
buffer[index++] = 0;
|
|
400
|
+
// Starting index
|
|
401
|
+
var startIndex = index;
|
|
402
|
+
// Serialize the function
|
|
403
|
+
// Get the function string
|
|
404
|
+
var functionString = typeof value.code === 'string' ? value.code : value.code.toString();
|
|
405
|
+
// Index adjustment
|
|
406
|
+
index = index + 4;
|
|
407
|
+
// Write string into buffer
|
|
408
|
+
var codeSize = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
|
|
409
|
+
// Write the size of the string to buffer
|
|
410
|
+
buffer[index] = codeSize & 0xff;
|
|
411
|
+
buffer[index + 1] = (codeSize >> 8) & 0xff;
|
|
412
|
+
buffer[index + 2] = (codeSize >> 16) & 0xff;
|
|
413
|
+
buffer[index + 3] = (codeSize >> 24) & 0xff;
|
|
414
|
+
// Write end 0
|
|
415
|
+
buffer[index + 4 + codeSize - 1] = 0;
|
|
416
|
+
// Write the
|
|
417
|
+
index = index + codeSize + 4;
|
|
418
|
+
//
|
|
419
|
+
// Serialize the scope value
|
|
420
|
+
var endIndex = serializeInto(buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined);
|
|
421
|
+
index = endIndex - 1;
|
|
422
|
+
// Writ the total
|
|
423
|
+
var totalSize = endIndex - startIndex;
|
|
424
|
+
// Write the total size of the object
|
|
425
|
+
buffer[startIndex++] = totalSize & 0xff;
|
|
426
|
+
buffer[startIndex++] = (totalSize >> 8) & 0xff;
|
|
427
|
+
buffer[startIndex++] = (totalSize >> 16) & 0xff;
|
|
428
|
+
buffer[startIndex++] = (totalSize >> 24) & 0xff;
|
|
429
|
+
// Write trailing zero
|
|
430
|
+
buffer[index++] = 0;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
buffer[index++] = constants.BSON_DATA_CODE;
|
|
434
|
+
// Number of written bytes
|
|
435
|
+
var numberOfWrittenBytes = !isArray
|
|
436
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
437
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
438
|
+
// Encode the name
|
|
439
|
+
index = index + numberOfWrittenBytes;
|
|
440
|
+
buffer[index++] = 0;
|
|
441
|
+
// Function string
|
|
442
|
+
var functionString = value.code.toString();
|
|
443
|
+
// Write the string
|
|
444
|
+
var size = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
|
|
445
|
+
// Write the size of the string to buffer
|
|
446
|
+
buffer[index] = size & 0xff;
|
|
447
|
+
buffer[index + 1] = (size >> 8) & 0xff;
|
|
448
|
+
buffer[index + 2] = (size >> 16) & 0xff;
|
|
449
|
+
buffer[index + 3] = (size >> 24) & 0xff;
|
|
450
|
+
// Update index
|
|
451
|
+
index = index + 4 + size - 1;
|
|
452
|
+
// Write zero
|
|
453
|
+
buffer[index++] = 0;
|
|
454
|
+
}
|
|
455
|
+
return index;
|
|
561
456
|
}
|
|
562
|
-
|
|
563
457
|
function serializeBinary(buffer, key, value, index, isArray) {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
buffer[index++] = (size >> 8) & 0xff;
|
|
582
|
-
buffer[index++] = (size >> 16) & 0xff;
|
|
583
|
-
buffer[index++] = (size >> 24) & 0xff;
|
|
584
|
-
// Write the subtype to the buffer
|
|
585
|
-
buffer[index++] = value.sub_type;
|
|
586
|
-
|
|
587
|
-
// If we have binary type 2 the 4 first bytes are the size
|
|
588
|
-
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
|
|
589
|
-
size = size - 4;
|
|
458
|
+
// Write the type
|
|
459
|
+
buffer[index++] = constants.BSON_DATA_BINARY;
|
|
460
|
+
// Number of written bytes
|
|
461
|
+
var numberOfWrittenBytes = !isArray
|
|
462
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
463
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
464
|
+
// Encode the name
|
|
465
|
+
index = index + numberOfWrittenBytes;
|
|
466
|
+
buffer[index++] = 0;
|
|
467
|
+
// Extract the buffer
|
|
468
|
+
var data = value.value(true);
|
|
469
|
+
// Calculate size
|
|
470
|
+
var size = value.position;
|
|
471
|
+
// Add the deprecated 02 type 4 bytes of size to total
|
|
472
|
+
if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY)
|
|
473
|
+
size = size + 4;
|
|
474
|
+
// Write the size of the string to buffer
|
|
590
475
|
buffer[index++] = size & 0xff;
|
|
591
476
|
buffer[index++] = (size >> 8) & 0xff;
|
|
592
477
|
buffer[index++] = (size >> 16) & 0xff;
|
|
593
478
|
buffer[index++] = (size >> 24) & 0xff;
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
479
|
+
// Write the subtype to the buffer
|
|
480
|
+
buffer[index++] = value.sub_type;
|
|
481
|
+
// If we have binary type 2 the 4 first bytes are the size
|
|
482
|
+
if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
|
|
483
|
+
size = size - 4;
|
|
484
|
+
buffer[index++] = size & 0xff;
|
|
485
|
+
buffer[index++] = (size >> 8) & 0xff;
|
|
486
|
+
buffer[index++] = (size >> 16) & 0xff;
|
|
487
|
+
buffer[index++] = (size >> 24) & 0xff;
|
|
488
|
+
}
|
|
489
|
+
// Write the data to the object
|
|
490
|
+
buffer.set(data, index);
|
|
491
|
+
// Adjust the index
|
|
492
|
+
index = index + value.position;
|
|
493
|
+
return index;
|
|
601
494
|
}
|
|
602
|
-
|
|
603
495
|
function serializeSymbol(buffer, key, value, index, isArray) {
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
496
|
+
// Write the type
|
|
497
|
+
buffer[index++] = constants.BSON_DATA_SYMBOL;
|
|
498
|
+
// Number of written bytes
|
|
499
|
+
var numberOfWrittenBytes = !isArray
|
|
500
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
501
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
502
|
+
// Encode the name
|
|
503
|
+
index = index + numberOfWrittenBytes;
|
|
504
|
+
buffer[index++] = 0;
|
|
505
|
+
// Write the string
|
|
506
|
+
var size = buffer.write(value.value, index + 4, undefined, 'utf8') + 1;
|
|
507
|
+
// Write the size of the string to buffer
|
|
508
|
+
buffer[index] = size & 0xff;
|
|
509
|
+
buffer[index + 1] = (size >> 8) & 0xff;
|
|
510
|
+
buffer[index + 2] = (size >> 16) & 0xff;
|
|
511
|
+
buffer[index + 3] = (size >> 24) & 0xff;
|
|
512
|
+
// Update index
|
|
513
|
+
index = index + 4 + size - 1;
|
|
514
|
+
// Write zero
|
|
515
|
+
buffer[index++] = 0x00;
|
|
516
|
+
return index;
|
|
625
517
|
}
|
|
626
|
-
|
|
627
518
|
function serializeDBRef(buffer, key, value, index, depth, serializeFunctions, isArray) {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
};
|
|
645
|
-
|
|
646
|
-
if (value.db != null) output.$db = value.db;
|
|
647
|
-
|
|
648
|
-
output = Object.assign(output, value.fields);
|
|
649
|
-
endIndex = serializeInto(buffer, output, false, index, depth + 1, serializeFunctions);
|
|
650
|
-
|
|
651
|
-
// Calculate object size
|
|
652
|
-
const size = endIndex - startIndex;
|
|
653
|
-
// Write the size
|
|
654
|
-
buffer[startIndex++] = size & 0xff;
|
|
655
|
-
buffer[startIndex++] = (size >> 8) & 0xff;
|
|
656
|
-
buffer[startIndex++] = (size >> 16) & 0xff;
|
|
657
|
-
buffer[startIndex++] = (size >> 24) & 0xff;
|
|
658
|
-
// Set index
|
|
659
|
-
return endIndex;
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
function serializeInto(
|
|
663
|
-
buffer,
|
|
664
|
-
object,
|
|
665
|
-
checkKeys,
|
|
666
|
-
startingIndex,
|
|
667
|
-
depth,
|
|
668
|
-
serializeFunctions,
|
|
669
|
-
ignoreUndefined,
|
|
670
|
-
path
|
|
671
|
-
) {
|
|
672
|
-
startingIndex = startingIndex || 0;
|
|
673
|
-
path = path || [];
|
|
674
|
-
|
|
675
|
-
// Push the object to the path
|
|
676
|
-
path.push(object);
|
|
677
|
-
|
|
678
|
-
// Start place to serialize into
|
|
679
|
-
let index = startingIndex + 4;
|
|
680
|
-
|
|
681
|
-
// Special case isArray
|
|
682
|
-
if (Array.isArray(object)) {
|
|
683
|
-
// Get object keys
|
|
684
|
-
for (let i = 0; i < object.length; i++) {
|
|
685
|
-
let key = '' + i;
|
|
686
|
-
let value = object[i];
|
|
687
|
-
|
|
688
|
-
// Is there an override value
|
|
689
|
-
if (value && value.toBSON) {
|
|
690
|
-
if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
|
|
691
|
-
value = value.toBSON();
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
const type = typeof value;
|
|
695
|
-
if (type === 'string') {
|
|
696
|
-
index = serializeString(buffer, key, value, index, true);
|
|
697
|
-
} else if (type === 'number') {
|
|
698
|
-
index = serializeNumber(buffer, key, value, index, true);
|
|
699
|
-
} else if (type === 'boolean') {
|
|
700
|
-
index = serializeBoolean(buffer, key, value, index, true);
|
|
701
|
-
} else if (value instanceof Date || isDate(value)) {
|
|
702
|
-
index = serializeDate(buffer, key, value, index, true);
|
|
703
|
-
} else if (value === undefined) {
|
|
704
|
-
index = serializeNull(buffer, key, value, index, true);
|
|
705
|
-
} else if (value === null) {
|
|
706
|
-
index = serializeNull(buffer, key, value, index, true);
|
|
707
|
-
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
708
|
-
index = serializeObjectId(buffer, key, value, index, true);
|
|
709
|
-
} else if (Buffer.isBuffer(value)) {
|
|
710
|
-
index = serializeBuffer(buffer, key, value, index, true);
|
|
711
|
-
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
712
|
-
index = serializeRegExp(buffer, key, value, index, true);
|
|
713
|
-
} else if (type === 'object' && value['_bsontype'] == null) {
|
|
714
|
-
index = serializeObject(
|
|
715
|
-
buffer,
|
|
716
|
-
key,
|
|
717
|
-
value,
|
|
718
|
-
index,
|
|
719
|
-
checkKeys,
|
|
720
|
-
depth,
|
|
721
|
-
serializeFunctions,
|
|
722
|
-
ignoreUndefined,
|
|
723
|
-
true,
|
|
724
|
-
path
|
|
725
|
-
);
|
|
726
|
-
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
|
|
727
|
-
index = serializeDecimal128(buffer, key, value, index, true);
|
|
728
|
-
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
|
|
729
|
-
index = serializeLong(buffer, key, value, index, true);
|
|
730
|
-
} else if (value['_bsontype'] === 'Double') {
|
|
731
|
-
index = serializeDouble(buffer, key, value, index, true);
|
|
732
|
-
} else if (typeof value === 'function' && serializeFunctions) {
|
|
733
|
-
index = serializeFunction(
|
|
734
|
-
buffer,
|
|
735
|
-
key,
|
|
736
|
-
value,
|
|
737
|
-
index,
|
|
738
|
-
checkKeys,
|
|
739
|
-
depth,
|
|
740
|
-
serializeFunctions,
|
|
741
|
-
true
|
|
742
|
-
);
|
|
743
|
-
} else if (value['_bsontype'] === 'Code') {
|
|
744
|
-
index = serializeCode(
|
|
745
|
-
buffer,
|
|
746
|
-
key,
|
|
747
|
-
value,
|
|
748
|
-
index,
|
|
749
|
-
checkKeys,
|
|
750
|
-
depth,
|
|
751
|
-
serializeFunctions,
|
|
752
|
-
ignoreUndefined,
|
|
753
|
-
true
|
|
754
|
-
);
|
|
755
|
-
} else if (value['_bsontype'] === 'Binary') {
|
|
756
|
-
index = serializeBinary(buffer, key, value, index, true);
|
|
757
|
-
} else if (value['_bsontype'] === 'Symbol') {
|
|
758
|
-
index = serializeSymbol(buffer, key, value, index, true);
|
|
759
|
-
} else if (value['_bsontype'] === 'DBRef') {
|
|
760
|
-
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
|
|
761
|
-
} else if (value['_bsontype'] === 'BSONRegExp') {
|
|
762
|
-
index = serializeBSONRegExp(buffer, key, value, index, true);
|
|
763
|
-
} else if (value['_bsontype'] === 'Int32') {
|
|
764
|
-
index = serializeInt32(buffer, key, value, index, true);
|
|
765
|
-
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
766
|
-
index = serializeMinMax(buffer, key, value, index, true);
|
|
767
|
-
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
768
|
-
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
769
|
-
}
|
|
519
|
+
// Write the type
|
|
520
|
+
buffer[index++] = constants.BSON_DATA_OBJECT;
|
|
521
|
+
// Number of written bytes
|
|
522
|
+
var numberOfWrittenBytes = !isArray
|
|
523
|
+
? buffer.write(key, index, undefined, 'utf8')
|
|
524
|
+
: buffer.write(key, index, undefined, 'ascii');
|
|
525
|
+
// Encode the name
|
|
526
|
+
index = index + numberOfWrittenBytes;
|
|
527
|
+
buffer[index++] = 0;
|
|
528
|
+
var startIndex = index;
|
|
529
|
+
var output = {
|
|
530
|
+
$ref: value.collection || value.namespace,
|
|
531
|
+
$id: value.oid
|
|
532
|
+
};
|
|
533
|
+
if (value.db != null) {
|
|
534
|
+
output.$db = value.db;
|
|
770
535
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
536
|
+
output = Object.assign(output, value.fields);
|
|
537
|
+
var endIndex = serializeInto(buffer, output, false, index, depth + 1, serializeFunctions);
|
|
538
|
+
// Calculate object size
|
|
539
|
+
var size = endIndex - startIndex;
|
|
540
|
+
// Write the size
|
|
541
|
+
buffer[startIndex++] = size & 0xff;
|
|
542
|
+
buffer[startIndex++] = (size >> 8) & 0xff;
|
|
543
|
+
buffer[startIndex++] = (size >> 16) & 0xff;
|
|
544
|
+
buffer[startIndex++] = (size >> 24) & 0xff;
|
|
545
|
+
// Set index
|
|
546
|
+
return endIndex;
|
|
547
|
+
}
|
|
548
|
+
function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializeFunctions, ignoreUndefined, path) {
|
|
549
|
+
if (checkKeys === void 0) { checkKeys = false; }
|
|
550
|
+
if (startingIndex === void 0) { startingIndex = 0; }
|
|
551
|
+
if (depth === void 0) { depth = 0; }
|
|
552
|
+
if (serializeFunctions === void 0) { serializeFunctions = false; }
|
|
553
|
+
if (ignoreUndefined === void 0) { ignoreUndefined = true; }
|
|
554
|
+
if (path === void 0) { path = []; }
|
|
555
|
+
startingIndex = startingIndex || 0;
|
|
556
|
+
path = path || [];
|
|
557
|
+
// Push the object to the path
|
|
558
|
+
path.push(object);
|
|
559
|
+
// Start place to serialize into
|
|
560
|
+
var index = startingIndex + 4;
|
|
561
|
+
// Special case isArray
|
|
562
|
+
if (Array.isArray(object)) {
|
|
563
|
+
// Get object keys
|
|
564
|
+
for (var i = 0; i < object.length; i++) {
|
|
565
|
+
var key = '' + i;
|
|
566
|
+
var value = object[i];
|
|
567
|
+
// Is there an override value
|
|
568
|
+
if (value && value.toBSON) {
|
|
569
|
+
if (typeof value.toBSON !== 'function')
|
|
570
|
+
throw new TypeError('toBSON is not a function');
|
|
571
|
+
value = value.toBSON();
|
|
572
|
+
}
|
|
573
|
+
if (typeof value === 'string') {
|
|
574
|
+
index = serializeString(buffer, key, value, index, true);
|
|
575
|
+
}
|
|
576
|
+
else if (typeof value === 'number') {
|
|
577
|
+
index = serializeNumber(buffer, key, value, index, true);
|
|
578
|
+
}
|
|
579
|
+
else if (typeof value === 'bigint') {
|
|
580
|
+
throw new TypeError('Unsupported type BigInt, please use Decimal128');
|
|
581
|
+
}
|
|
582
|
+
else if (typeof value === 'boolean') {
|
|
583
|
+
index = serializeBoolean(buffer, key, value, index, true);
|
|
584
|
+
}
|
|
585
|
+
else if (value instanceof Date || utils_1.isDate(value)) {
|
|
586
|
+
index = serializeDate(buffer, key, value, index, true);
|
|
587
|
+
}
|
|
588
|
+
else if (value === undefined) {
|
|
589
|
+
index = serializeNull(buffer, key, value, index, true);
|
|
590
|
+
}
|
|
591
|
+
else if (value === null) {
|
|
592
|
+
index = serializeNull(buffer, key, value, index, true);
|
|
593
|
+
}
|
|
594
|
+
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
595
|
+
index = serializeObjectId(buffer, key, value, index, true);
|
|
596
|
+
}
|
|
597
|
+
else if (utils_1.isBuffer(value) || utils_1.isUint8Array(value)) {
|
|
598
|
+
index = serializeBuffer(buffer, key, value, index, true);
|
|
599
|
+
}
|
|
600
|
+
else if (value instanceof RegExp || isRegExp(value)) {
|
|
601
|
+
index = serializeRegExp(buffer, key, value, index, true);
|
|
602
|
+
}
|
|
603
|
+
else if (typeof value === 'object' && value['_bsontype'] == null) {
|
|
604
|
+
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true, path);
|
|
605
|
+
}
|
|
606
|
+
else if (typeof value === 'object' &&
|
|
607
|
+
extended_json_1.isBSONType(value) &&
|
|
608
|
+
value._bsontype === 'Decimal128') {
|
|
609
|
+
index = serializeDecimal128(buffer, key, value, index, true);
|
|
610
|
+
}
|
|
611
|
+
else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
|
|
612
|
+
index = serializeLong(buffer, key, value, index, true);
|
|
613
|
+
}
|
|
614
|
+
else if (value['_bsontype'] === 'Double') {
|
|
615
|
+
index = serializeDouble(buffer, key, value, index, true);
|
|
616
|
+
}
|
|
617
|
+
else if (typeof value === 'function' && serializeFunctions) {
|
|
618
|
+
index = serializeFunction(buffer, key, value, index, checkKeys, depth, true);
|
|
619
|
+
}
|
|
620
|
+
else if (value['_bsontype'] === 'Code') {
|
|
621
|
+
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true);
|
|
622
|
+
}
|
|
623
|
+
else if (value['_bsontype'] === 'Binary') {
|
|
624
|
+
index = serializeBinary(buffer, key, value, index, true);
|
|
625
|
+
}
|
|
626
|
+
else if (value['_bsontype'] === 'Symbol') {
|
|
627
|
+
index = serializeSymbol(buffer, key, value, index, true);
|
|
628
|
+
}
|
|
629
|
+
else if (value['_bsontype'] === 'DBRef') {
|
|
630
|
+
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
|
|
631
|
+
}
|
|
632
|
+
else if (value['_bsontype'] === 'BSONRegExp') {
|
|
633
|
+
index = serializeBSONRegExp(buffer, key, value, index, true);
|
|
634
|
+
}
|
|
635
|
+
else if (value['_bsontype'] === 'Int32') {
|
|
636
|
+
index = serializeInt32(buffer, key, value, index, true);
|
|
637
|
+
}
|
|
638
|
+
else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
639
|
+
index = serializeMinMax(buffer, key, value, index, true);
|
|
640
|
+
}
|
|
641
|
+
else if (typeof value['_bsontype'] !== 'undefined') {
|
|
642
|
+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
643
|
+
}
|
|
803
644
|
}
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
if (type === 'string') {
|
|
807
|
-
index = serializeString(buffer, key, value, index);
|
|
808
|
-
} else if (type === 'number') {
|
|
809
|
-
index = serializeNumber(buffer, key, value, index);
|
|
810
|
-
} else if (type === 'boolean') {
|
|
811
|
-
index = serializeBoolean(buffer, key, value, index);
|
|
812
|
-
} else if (value instanceof Date || isDate(value)) {
|
|
813
|
-
index = serializeDate(buffer, key, value, index);
|
|
814
|
-
} else if (value === null || (value === undefined && ignoreUndefined === false)) {
|
|
815
|
-
index = serializeNull(buffer, key, value, index);
|
|
816
|
-
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
817
|
-
index = serializeObjectId(buffer, key, value, index);
|
|
818
|
-
} else if (Buffer.isBuffer(value)) {
|
|
819
|
-
index = serializeBuffer(buffer, key, value, index);
|
|
820
|
-
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
821
|
-
index = serializeRegExp(buffer, key, value, index);
|
|
822
|
-
} else if (type === 'object' && value['_bsontype'] == null) {
|
|
823
|
-
index = serializeObject(
|
|
824
|
-
buffer,
|
|
825
|
-
key,
|
|
826
|
-
value,
|
|
827
|
-
index,
|
|
828
|
-
checkKeys,
|
|
829
|
-
depth,
|
|
830
|
-
serializeFunctions,
|
|
831
|
-
ignoreUndefined,
|
|
832
|
-
false,
|
|
833
|
-
path
|
|
834
|
-
);
|
|
835
|
-
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
|
|
836
|
-
index = serializeDecimal128(buffer, key, value, index);
|
|
837
|
-
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
|
|
838
|
-
index = serializeLong(buffer, key, value, index);
|
|
839
|
-
} else if (value['_bsontype'] === 'Double') {
|
|
840
|
-
index = serializeDouble(buffer, key, value, index);
|
|
841
|
-
} else if (value['_bsontype'] === 'Code') {
|
|
842
|
-
index = serializeCode(
|
|
843
|
-
buffer,
|
|
844
|
-
key,
|
|
845
|
-
value,
|
|
846
|
-
index,
|
|
847
|
-
checkKeys,
|
|
848
|
-
depth,
|
|
849
|
-
serializeFunctions,
|
|
850
|
-
ignoreUndefined
|
|
851
|
-
);
|
|
852
|
-
} else if (typeof value === 'function' && serializeFunctions) {
|
|
853
|
-
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
|
854
|
-
} else if (value['_bsontype'] === 'Binary') {
|
|
855
|
-
index = serializeBinary(buffer, key, value, index);
|
|
856
|
-
} else if (value['_bsontype'] === 'Symbol') {
|
|
857
|
-
index = serializeSymbol(buffer, key, value, index);
|
|
858
|
-
} else if (value['_bsontype'] === 'DBRef') {
|
|
859
|
-
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
|
860
|
-
} else if (value['_bsontype'] === 'BSONRegExp') {
|
|
861
|
-
index = serializeBSONRegExp(buffer, key, value, index);
|
|
862
|
-
} else if (value['_bsontype'] === 'Int32') {
|
|
863
|
-
index = serializeInt32(buffer, key, value, index);
|
|
864
|
-
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
865
|
-
index = serializeMinMax(buffer, key, value, index);
|
|
866
|
-
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
867
|
-
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
868
|
-
}
|
|
869
645
|
}
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
646
|
+
else if (object instanceof map_1.Map) {
|
|
647
|
+
var iterator = object.entries();
|
|
648
|
+
var done = false;
|
|
649
|
+
while (!done) {
|
|
650
|
+
// Unpack the next entry
|
|
651
|
+
var entry = iterator.next();
|
|
652
|
+
done = !!entry.done;
|
|
653
|
+
// Are we done, then skip and terminate
|
|
654
|
+
if (done)
|
|
655
|
+
continue;
|
|
656
|
+
// Get the entry values
|
|
657
|
+
var key = entry.value[0];
|
|
658
|
+
var value = entry.value[1];
|
|
659
|
+
// Check the type of the value
|
|
660
|
+
var type = typeof value;
|
|
661
|
+
// Check the key and throw error if it's illegal
|
|
662
|
+
if (typeof key === 'string' && !ignoreKeys.has(key)) {
|
|
663
|
+
if (key.match(regexp) != null) {
|
|
664
|
+
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
665
|
+
// null-terminated.
|
|
666
|
+
throw Error('key ' + key + ' must not contain null bytes');
|
|
667
|
+
}
|
|
668
|
+
if (checkKeys) {
|
|
669
|
+
if ('$' === key[0]) {
|
|
670
|
+
throw Error('key ' + key + " must not start with '$'");
|
|
671
|
+
}
|
|
672
|
+
else if (~key.indexOf('.')) {
|
|
673
|
+
throw Error('key ' + key + " must not contain '.'");
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (type === 'string') {
|
|
678
|
+
index = serializeString(buffer, key, value, index);
|
|
679
|
+
}
|
|
680
|
+
else if (type === 'number') {
|
|
681
|
+
index = serializeNumber(buffer, key, value, index);
|
|
682
|
+
}
|
|
683
|
+
else if (type === 'bigint' || utils_1.isBigInt64Array(value) || utils_1.isBigUInt64Array(value)) {
|
|
684
|
+
throw new TypeError('Unsupported type BigInt, please use Decimal128');
|
|
685
|
+
}
|
|
686
|
+
else if (type === 'boolean') {
|
|
687
|
+
index = serializeBoolean(buffer, key, value, index);
|
|
688
|
+
}
|
|
689
|
+
else if (value instanceof Date || utils_1.isDate(value)) {
|
|
690
|
+
index = serializeDate(buffer, key, value, index);
|
|
691
|
+
}
|
|
692
|
+
else if (value === null || (value === undefined && ignoreUndefined === false)) {
|
|
693
|
+
index = serializeNull(buffer, key, value, index);
|
|
694
|
+
}
|
|
695
|
+
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
696
|
+
index = serializeObjectId(buffer, key, value, index);
|
|
697
|
+
}
|
|
698
|
+
else if (utils_1.isBuffer(value) || utils_1.isUint8Array(value)) {
|
|
699
|
+
index = serializeBuffer(buffer, key, value, index);
|
|
700
|
+
}
|
|
701
|
+
else if (value instanceof RegExp || isRegExp(value)) {
|
|
702
|
+
index = serializeRegExp(buffer, key, value, index);
|
|
703
|
+
}
|
|
704
|
+
else if (type === 'object' && value['_bsontype'] == null) {
|
|
705
|
+
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path);
|
|
706
|
+
}
|
|
707
|
+
else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
|
|
708
|
+
index = serializeDecimal128(buffer, key, value, index);
|
|
709
|
+
}
|
|
710
|
+
else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
|
|
711
|
+
index = serializeLong(buffer, key, value, index);
|
|
712
|
+
}
|
|
713
|
+
else if (value['_bsontype'] === 'Double') {
|
|
714
|
+
index = serializeDouble(buffer, key, value, index);
|
|
715
|
+
}
|
|
716
|
+
else if (value['_bsontype'] === 'Code') {
|
|
717
|
+
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
|
718
|
+
}
|
|
719
|
+
else if (typeof value === 'function' && serializeFunctions) {
|
|
720
|
+
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
|
721
|
+
}
|
|
722
|
+
else if (value['_bsontype'] === 'Binary') {
|
|
723
|
+
index = serializeBinary(buffer, key, value, index);
|
|
724
|
+
}
|
|
725
|
+
else if (value['_bsontype'] === 'Symbol') {
|
|
726
|
+
index = serializeSymbol(buffer, key, value, index);
|
|
727
|
+
}
|
|
728
|
+
else if (value['_bsontype'] === 'DBRef') {
|
|
729
|
+
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
|
730
|
+
}
|
|
731
|
+
else if (value['_bsontype'] === 'BSONRegExp') {
|
|
732
|
+
index = serializeBSONRegExp(buffer, key, value, index);
|
|
733
|
+
}
|
|
734
|
+
else if (value['_bsontype'] === 'Int32') {
|
|
735
|
+
index = serializeInt32(buffer, key, value, index);
|
|
736
|
+
}
|
|
737
|
+
else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
738
|
+
index = serializeMinMax(buffer, key, value, index);
|
|
739
|
+
}
|
|
740
|
+
else if (typeof value['_bsontype'] !== 'undefined') {
|
|
741
|
+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
877
744
|
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
// Check the type of the value
|
|
889
|
-
const type = typeof value;
|
|
890
|
-
|
|
891
|
-
// Check the key and throw error if it's illegal
|
|
892
|
-
if (typeof key === 'string' && !ignoreKeys.has(key)) {
|
|
893
|
-
if (key.match(regexp) != null) {
|
|
894
|
-
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
895
|
-
// null-terminated.
|
|
896
|
-
throw Error('key ' + key + ' must not contain null bytes');
|
|
745
|
+
else {
|
|
746
|
+
// Did we provide a custom serialization method
|
|
747
|
+
if (object.toBSON) {
|
|
748
|
+
if (typeof object.toBSON !== 'function')
|
|
749
|
+
throw new TypeError('toBSON is not a function');
|
|
750
|
+
object = object.toBSON();
|
|
751
|
+
if (object != null && typeof object !== 'object')
|
|
752
|
+
throw new TypeError('toBSON function did not return an object');
|
|
897
753
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
754
|
+
// Iterate over all the keys
|
|
755
|
+
for (var key in object) {
|
|
756
|
+
var value = object[key];
|
|
757
|
+
// Is there an override value
|
|
758
|
+
if (value && value.toBSON) {
|
|
759
|
+
if (typeof value.toBSON !== 'function')
|
|
760
|
+
throw new TypeError('toBSON is not a function');
|
|
761
|
+
value = value.toBSON();
|
|
762
|
+
}
|
|
763
|
+
// Check the type of the value
|
|
764
|
+
var type = typeof value;
|
|
765
|
+
// Check the key and throw error if it's illegal
|
|
766
|
+
if (typeof key === 'string' && !ignoreKeys.has(key)) {
|
|
767
|
+
if (key.match(regexp) != null) {
|
|
768
|
+
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
769
|
+
// null-terminated.
|
|
770
|
+
throw Error('key ' + key + ' must not contain null bytes');
|
|
771
|
+
}
|
|
772
|
+
if (checkKeys) {
|
|
773
|
+
if ('$' === key[0]) {
|
|
774
|
+
throw Error('key ' + key + " must not start with '$'");
|
|
775
|
+
}
|
|
776
|
+
else if (~key.indexOf('.')) {
|
|
777
|
+
throw Error('key ' + key + " must not contain '.'");
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
if (type === 'string') {
|
|
782
|
+
index = serializeString(buffer, key, value, index);
|
|
783
|
+
}
|
|
784
|
+
else if (type === 'number') {
|
|
785
|
+
index = serializeNumber(buffer, key, value, index);
|
|
786
|
+
}
|
|
787
|
+
else if (type === 'bigint') {
|
|
788
|
+
throw new TypeError('Unsupported type BigInt, please use Decimal128');
|
|
789
|
+
}
|
|
790
|
+
else if (type === 'boolean') {
|
|
791
|
+
index = serializeBoolean(buffer, key, value, index);
|
|
792
|
+
}
|
|
793
|
+
else if (value instanceof Date || utils_1.isDate(value)) {
|
|
794
|
+
index = serializeDate(buffer, key, value, index);
|
|
795
|
+
}
|
|
796
|
+
else if (value === undefined) {
|
|
797
|
+
if (ignoreUndefined === false)
|
|
798
|
+
index = serializeNull(buffer, key, value, index);
|
|
799
|
+
}
|
|
800
|
+
else if (value === null) {
|
|
801
|
+
index = serializeNull(buffer, key, value, index);
|
|
802
|
+
}
|
|
803
|
+
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
804
|
+
index = serializeObjectId(buffer, key, value, index);
|
|
805
|
+
}
|
|
806
|
+
else if (utils_1.isBuffer(value) || utils_1.isUint8Array(value)) {
|
|
807
|
+
index = serializeBuffer(buffer, key, value, index);
|
|
808
|
+
}
|
|
809
|
+
else if (value instanceof RegExp || isRegExp(value)) {
|
|
810
|
+
index = serializeRegExp(buffer, key, value, index);
|
|
811
|
+
}
|
|
812
|
+
else if (type === 'object' && value['_bsontype'] == null) {
|
|
813
|
+
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path);
|
|
814
|
+
}
|
|
815
|
+
else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
|
|
816
|
+
index = serializeDecimal128(buffer, key, value, index);
|
|
817
|
+
}
|
|
818
|
+
else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
|
|
819
|
+
index = serializeLong(buffer, key, value, index);
|
|
820
|
+
}
|
|
821
|
+
else if (value['_bsontype'] === 'Double') {
|
|
822
|
+
index = serializeDouble(buffer, key, value, index);
|
|
823
|
+
}
|
|
824
|
+
else if (value['_bsontype'] === 'Code') {
|
|
825
|
+
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
|
826
|
+
}
|
|
827
|
+
else if (typeof value === 'function' && serializeFunctions) {
|
|
828
|
+
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
|
829
|
+
}
|
|
830
|
+
else if (value['_bsontype'] === 'Binary') {
|
|
831
|
+
index = serializeBinary(buffer, key, value, index);
|
|
832
|
+
}
|
|
833
|
+
else if (value['_bsontype'] === 'Symbol') {
|
|
834
|
+
index = serializeSymbol(buffer, key, value, index);
|
|
835
|
+
}
|
|
836
|
+
else if (value['_bsontype'] === 'DBRef') {
|
|
837
|
+
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
|
838
|
+
}
|
|
839
|
+
else if (value['_bsontype'] === 'BSONRegExp') {
|
|
840
|
+
index = serializeBSONRegExp(buffer, key, value, index);
|
|
841
|
+
}
|
|
842
|
+
else if (value['_bsontype'] === 'Int32') {
|
|
843
|
+
index = serializeInt32(buffer, key, value, index);
|
|
844
|
+
}
|
|
845
|
+
else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
846
|
+
index = serializeMinMax(buffer, key, value, index);
|
|
847
|
+
}
|
|
848
|
+
else if (typeof value['_bsontype'] !== 'undefined') {
|
|
849
|
+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
850
|
+
}
|
|
905
851
|
}
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
if (type === 'string') {
|
|
909
|
-
index = serializeString(buffer, key, value, index);
|
|
910
|
-
} else if (type === 'number') {
|
|
911
|
-
index = serializeNumber(buffer, key, value, index);
|
|
912
|
-
} else if (type === 'boolean') {
|
|
913
|
-
index = serializeBoolean(buffer, key, value, index);
|
|
914
|
-
} else if (value instanceof Date || isDate(value)) {
|
|
915
|
-
index = serializeDate(buffer, key, value, index);
|
|
916
|
-
} else if (value === undefined) {
|
|
917
|
-
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
|
|
918
|
-
} else if (value === null) {
|
|
919
|
-
index = serializeNull(buffer, key, value, index);
|
|
920
|
-
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
921
|
-
index = serializeObjectId(buffer, key, value, index);
|
|
922
|
-
} else if (Buffer.isBuffer(value)) {
|
|
923
|
-
index = serializeBuffer(buffer, key, value, index);
|
|
924
|
-
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
925
|
-
index = serializeRegExp(buffer, key, value, index);
|
|
926
|
-
} else if (type === 'object' && value['_bsontype'] == null) {
|
|
927
|
-
index = serializeObject(
|
|
928
|
-
buffer,
|
|
929
|
-
key,
|
|
930
|
-
value,
|
|
931
|
-
index,
|
|
932
|
-
checkKeys,
|
|
933
|
-
depth,
|
|
934
|
-
serializeFunctions,
|
|
935
|
-
ignoreUndefined,
|
|
936
|
-
false,
|
|
937
|
-
path
|
|
938
|
-
);
|
|
939
|
-
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
|
|
940
|
-
index = serializeDecimal128(buffer, key, value, index);
|
|
941
|
-
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
|
|
942
|
-
index = serializeLong(buffer, key, value, index);
|
|
943
|
-
} else if (value['_bsontype'] === 'Double') {
|
|
944
|
-
index = serializeDouble(buffer, key, value, index);
|
|
945
|
-
} else if (value['_bsontype'] === 'Code') {
|
|
946
|
-
index = serializeCode(
|
|
947
|
-
buffer,
|
|
948
|
-
key,
|
|
949
|
-
value,
|
|
950
|
-
index,
|
|
951
|
-
checkKeys,
|
|
952
|
-
depth,
|
|
953
|
-
serializeFunctions,
|
|
954
|
-
ignoreUndefined
|
|
955
|
-
);
|
|
956
|
-
} else if (typeof value === 'function' && serializeFunctions) {
|
|
957
|
-
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
|
958
|
-
} else if (value['_bsontype'] === 'Binary') {
|
|
959
|
-
index = serializeBinary(buffer, key, value, index);
|
|
960
|
-
} else if (value['_bsontype'] === 'Symbol') {
|
|
961
|
-
index = serializeSymbol(buffer, key, value, index);
|
|
962
|
-
} else if (value['_bsontype'] === 'DBRef') {
|
|
963
|
-
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
|
964
|
-
} else if (value['_bsontype'] === 'BSONRegExp') {
|
|
965
|
-
index = serializeBSONRegExp(buffer, key, value, index);
|
|
966
|
-
} else if (value['_bsontype'] === 'Int32') {
|
|
967
|
-
index = serializeInt32(buffer, key, value, index);
|
|
968
|
-
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
969
|
-
index = serializeMinMax(buffer, key, value, index);
|
|
970
|
-
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
971
|
-
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
972
|
-
}
|
|
973
852
|
}
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
buffer[startingIndex++] = (size >> 8) & 0xff;
|
|
987
|
-
buffer[startingIndex++] = (size >> 16) & 0xff;
|
|
988
|
-
buffer[startingIndex++] = (size >> 24) & 0xff;
|
|
989
|
-
return index;
|
|
853
|
+
// Remove the path
|
|
854
|
+
path.pop();
|
|
855
|
+
// Final padding byte for object
|
|
856
|
+
buffer[index++] = 0x00;
|
|
857
|
+
// Final size
|
|
858
|
+
var size = index - startingIndex;
|
|
859
|
+
// Write the size of the object
|
|
860
|
+
buffer[startingIndex++] = size & 0xff;
|
|
861
|
+
buffer[startingIndex++] = (size >> 8) & 0xff;
|
|
862
|
+
buffer[startingIndex++] = (size >> 16) & 0xff;
|
|
863
|
+
buffer[startingIndex++] = (size >> 24) & 0xff;
|
|
864
|
+
return index;
|
|
990
865
|
}
|
|
991
|
-
|
|
992
|
-
|
|
866
|
+
exports.serializeInto = serializeInto;
|
|
867
|
+
//# sourceMappingURL=serializer.js.map
|