bson 4.0.4 → 4.2.2

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