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.
Files changed (90) hide show
  1. package/HISTORY.md +48 -1
  2. package/README.md +7 -9
  3. package/bower.json +1 -1
  4. package/bson.d.ts +986 -0
  5. package/dist/bson.browser.esm.js +7811 -5011
  6. package/dist/bson.browser.esm.js.map +1 -0
  7. package/dist/bson.browser.umd.js +7862 -5099
  8. package/dist/bson.browser.umd.js.map +1 -0
  9. package/dist/bson.bundle.js +8723 -9228
  10. package/dist/bson.bundle.js.map +1 -0
  11. package/dist/bson.esm.js +5728 -4951
  12. package/dist/bson.esm.js.map +1 -0
  13. package/etc/prepare.js +19 -0
  14. package/lib/binary.js +218 -398
  15. package/lib/binary.js.map +1 -0
  16. package/lib/bson.js +201 -240
  17. package/lib/bson.js.map +1 -0
  18. package/lib/code.js +41 -41
  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 +88 -81
  23. package/lib/db_ref.js.map +1 -0
  24. package/lib/decimal128.js +667 -777
  25. package/lib/decimal128.js.map +1 -0
  26. package/lib/double.js +68 -70
  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 +310 -321
  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 +50 -49
  35. package/lib/int_32.js.map +1 -0
  36. package/lib/long.js +881 -16
  37. package/lib/long.js.map +1 -0
  38. package/lib/map.js +130 -122
  39. package/lib/map.js.map +1 -0
  40. package/lib/max_key.js +28 -25
  41. package/lib/max_key.js.map +1 -0
  42. package/lib/min_key.js +28 -25
  43. package/lib/min_key.js.map +1 -0
  44. package/lib/objectid.js +300 -410
  45. package/lib/objectid.js.map +1 -0
  46. package/lib/parser/calculate_size.js +188 -224
  47. package/lib/parser/calculate_size.js.map +1 -0
  48. package/lib/parser/deserializer.js +545 -621
  49. package/lib/parser/deserializer.js.map +1 -0
  50. package/lib/parser/serializer.js +798 -923
  51. package/lib/parser/serializer.js.map +1 -0
  52. package/lib/parser/utils.js +92 -30
  53. package/lib/parser/utils.js.map +1 -0
  54. package/lib/regexp.js +61 -74
  55. package/lib/regexp.js.map +1 -0
  56. package/lib/symbol.js +45 -58
  57. package/lib/symbol.js.map +1 -0
  58. package/lib/timestamp.js +91 -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 +41 -42
  63. package/lib/validate_utf8.js.map +1 -0
  64. package/package.json +53 -31
  65. package/src/binary.ts +272 -0
  66. package/src/bson.ts +326 -0
  67. package/src/code.ts +61 -0
  68. package/src/constants.ts +104 -0
  69. package/src/db_ref.ts +119 -0
  70. package/src/decimal128.ts +803 -0
  71. package/src/double.ts +87 -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 +66 -0
  76. package/src/long.ts +1002 -0
  77. package/src/map.ts +139 -0
  78. package/src/max_key.ts +37 -0
  79. package/src/min_key.ts +37 -0
  80. package/src/objectid.ts +379 -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 +94 -0
  86. package/src/symbol.ts +59 -0
  87. package/src/timestamp.ts +108 -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,375 @@
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
-
11
- const regexp = /\x00/; // eslint-disable-line no-control-regex
12
- 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
-
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
- // 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
+ 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
- // 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
+ 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
- } 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
+ 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
- const numberOfWrittenBytes = !isArray
114
- ? buffer.write(key, index, 'utf8')
115
- : buffer.write(key, index, 'ascii');
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
- // 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
+ 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
- // 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
+ 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
- // 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
+ 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
- // 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
+ 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
- // 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
+ 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
- // 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
+ 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
- // 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
+ 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
- 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, 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
- 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;
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
- // 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;
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
- // 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;
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
- // 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;
343
+ // Encode as double
344
+ buffer[index++] = constants.BSON_DATA_NUMBER;
485
345
  // Number of written bytes
486
- const numberOfWrittenBytes = !isArray
487
- ? buffer.write(key, index, 'utf8')
488
- : buffer.write(key, index, 'ascii');
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
- // 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 {
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
- const numberOfWrittenBytes = !isArray
540
- ? buffer.write(key, index, 'utf8')
541
- : buffer.write(key, index, 'ascii');
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
- const functionString = value.code.toString();
370
+ var functionString = utils_1.normalizedFunctionString(value);
547
371
  // Write the string
548
- const size = buffer.write(functionString, index + 4, 'utf8') + 1;
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
- return index;
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
- // 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;
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
- // 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;
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
- // 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;
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
- // 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
- }
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
- } 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
- }
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
- } 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');
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
- // 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');
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
- 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
- }
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
- // 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;
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
- module.exports = serializeInto;
866
+ exports.serializeInto = serializeInto;
867
+ //# sourceMappingURL=serializer.js.map