bson 6.3.0 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bson.rn.cjs CHANGED
@@ -177,7 +177,7 @@ function validateUtf8(bytes, start, end) {
177
177
  return !continuation;
178
178
  }
179
179
 
180
- function tryLatin(uint8array, start, end) {
180
+ function tryReadBasicLatin(uint8array, start, end) {
181
181
  if (uint8array.length === 0) {
182
182
  return '';
183
183
  }
@@ -212,6 +212,21 @@ function tryLatin(uint8array, start, end) {
212
212
  }
213
213
  return String.fromCharCode(...latinBytes);
214
214
  }
215
+ function tryWriteBasicLatin(destination, source, offset) {
216
+ if (source.length === 0)
217
+ return 0;
218
+ if (source.length > 25)
219
+ return null;
220
+ if (destination.length - offset < source.length)
221
+ return null;
222
+ for (let charOffset = 0, destinationOffset = offset; charOffset < source.length; charOffset++, destinationOffset++) {
223
+ const char = source.charCodeAt(charOffset);
224
+ if (char > 127)
225
+ return null;
226
+ destination[destinationOffset] = char;
227
+ }
228
+ return source.length;
229
+ }
215
230
 
216
231
  function nodejsMathRandomBytes(byteLength) {
217
232
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
@@ -244,6 +259,9 @@ const nodeJsByteUtils = {
244
259
  allocate(size) {
245
260
  return Buffer.alloc(size);
246
261
  },
262
+ allocateUnsafe(size) {
263
+ return Buffer.allocUnsafe(size);
264
+ },
247
265
  equals(a, b) {
248
266
  return nodeJsByteUtils.toLocalBufferType(a).equals(b);
249
267
  },
@@ -268,11 +286,8 @@ const nodeJsByteUtils = {
268
286
  toHex(buffer) {
269
287
  return nodeJsByteUtils.toLocalBufferType(buffer).toString('hex');
270
288
  },
271
- fromUTF8(text) {
272
- return Buffer.from(text, 'utf8');
273
- },
274
289
  toUTF8(buffer, start, end, fatal) {
275
- const basicLatin = end - start <= 20 ? tryLatin(buffer, start, end) : null;
290
+ const basicLatin = end - start <= 20 ? tryReadBasicLatin(buffer, start, end) : null;
276
291
  if (basicLatin != null) {
277
292
  return basicLatin;
278
293
  }
@@ -293,6 +308,10 @@ const nodeJsByteUtils = {
293
308
  return Buffer.byteLength(input, 'utf8');
294
309
  },
295
310
  encodeUTF8Into(buffer, source, byteOffset) {
311
+ const latinBytesWritten = tryWriteBasicLatin(buffer, source, byteOffset);
312
+ if (latinBytesWritten != null) {
313
+ return latinBytesWritten;
314
+ }
296
315
  return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
297
316
  },
298
317
  randomBytes: nodejsRandomBytes
@@ -350,6 +369,9 @@ const webByteUtils = {
350
369
  }
351
370
  return new Uint8Array(size);
352
371
  },
372
+ allocateUnsafe(size) {
373
+ return webByteUtils.allocate(size);
374
+ },
353
375
  equals(a, b) {
354
376
  if (a.byteLength !== b.byteLength) {
355
377
  return false;
@@ -396,11 +418,8 @@ const webByteUtils = {
396
418
  toHex(uint8array) {
397
419
  return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join('');
398
420
  },
399
- fromUTF8(text) {
400
- return new TextEncoder().encode(text);
401
- },
402
421
  toUTF8(uint8array, start, end, fatal) {
403
- const basicLatin = end - start <= 20 ? tryLatin(uint8array, start, end) : null;
422
+ const basicLatin = end - start <= 20 ? tryReadBasicLatin(uint8array, start, end) : null;
404
423
  if (basicLatin != null) {
405
424
  return basicLatin;
406
425
  }
@@ -415,11 +434,11 @@ const webByteUtils = {
415
434
  return new TextDecoder('utf8', { fatal }).decode(uint8array.slice(start, end));
416
435
  },
417
436
  utf8ByteLength(input) {
418
- return webByteUtils.fromUTF8(input).byteLength;
437
+ return new TextEncoder().encode(input).byteLength;
419
438
  },
420
- encodeUTF8Into(buffer, source, byteOffset) {
421
- const bytes = webByteUtils.fromUTF8(source);
422
- buffer.set(bytes, byteOffset);
439
+ encodeUTF8Into(uint8array, source, byteOffset) {
440
+ const bytes = new TextEncoder().encode(source);
441
+ uint8array.set(bytes, byteOffset);
423
442
  return bytes.byteLength;
424
443
  },
425
444
  randomBytes: webRandomBytes
@@ -427,11 +446,6 @@ const webByteUtils = {
427
446
 
428
447
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
429
448
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
430
- class BSONDataView extends DataView {
431
- static fromUint8Array(input) {
432
- return new DataView(input.buffer, input.byteOffset, input.byteLength);
433
- }
434
- }
435
449
 
436
450
  class BSONValue {
437
451
  get [Symbol.for('@@mdb.bson.version')]() {
@@ -1851,7 +1865,7 @@ class Decimal128 extends BSONValue {
1851
1865
  if (isNegative) {
1852
1866
  dec.high = dec.high.or(Long.fromString('9223372036854775808'));
1853
1867
  }
1854
- const buffer = ByteUtils.allocate(16);
1868
+ const buffer = ByteUtils.allocateUnsafe(16);
1855
1869
  index = 0;
1856
1870
  buffer[index++] = dec.low.low & 0xff;
1857
1871
  buffer[index++] = (dec.low.low >> 8) & 0xff;
@@ -2124,9 +2138,99 @@ class MinKey extends BSONValue {
2124
2138
  }
2125
2139
  }
2126
2140
 
2141
+ const FLOAT = new Float64Array(1);
2142
+ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2143
+ const NumberUtils = {
2144
+ getInt32LE(source, offset) {
2145
+ return (source[offset] |
2146
+ (source[offset + 1] << 8) |
2147
+ (source[offset + 2] << 16) |
2148
+ (source[offset + 3] << 24));
2149
+ },
2150
+ getUint32LE(source, offset) {
2151
+ return (source[offset] +
2152
+ source[offset + 1] * 256 +
2153
+ source[offset + 2] * 65536 +
2154
+ source[offset + 3] * 16777216);
2155
+ },
2156
+ getUint32BE(source, offset) {
2157
+ return (source[offset + 3] +
2158
+ source[offset + 2] * 256 +
2159
+ source[offset + 1] * 65536 +
2160
+ source[offset] * 16777216);
2161
+ },
2162
+ getBigInt64LE(source, offset) {
2163
+ const lo = NumberUtils.getUint32LE(source, offset);
2164
+ const hi = NumberUtils.getUint32LE(source, offset + 4);
2165
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo);
2166
+ },
2167
+ getFloat64LE(source, offset) {
2168
+ FLOAT_BYTES[0] = source[offset];
2169
+ FLOAT_BYTES[1] = source[offset + 1];
2170
+ FLOAT_BYTES[2] = source[offset + 2];
2171
+ FLOAT_BYTES[3] = source[offset + 3];
2172
+ FLOAT_BYTES[4] = source[offset + 4];
2173
+ FLOAT_BYTES[5] = source[offset + 5];
2174
+ FLOAT_BYTES[6] = source[offset + 6];
2175
+ FLOAT_BYTES[7] = source[offset + 7];
2176
+ return FLOAT[0];
2177
+ },
2178
+ setInt32BE(destination, offset, value) {
2179
+ destination[offset + 3] = value;
2180
+ value >>>= 8;
2181
+ destination[offset + 2] = value;
2182
+ value >>>= 8;
2183
+ destination[offset + 1] = value;
2184
+ value >>>= 8;
2185
+ destination[offset] = value;
2186
+ return 4;
2187
+ },
2188
+ setInt32LE(destination, offset, value) {
2189
+ destination[offset] = value;
2190
+ value >>>= 8;
2191
+ destination[offset + 1] = value;
2192
+ value >>>= 8;
2193
+ destination[offset + 2] = value;
2194
+ value >>>= 8;
2195
+ destination[offset + 3] = value;
2196
+ return 4;
2197
+ },
2198
+ setBigInt64LE(destination, offset, value) {
2199
+ const mask32bits = BigInt(4294967295);
2200
+ let lo = Number(value & mask32bits);
2201
+ destination[offset] = lo;
2202
+ lo >>= 8;
2203
+ destination[offset + 1] = lo;
2204
+ lo >>= 8;
2205
+ destination[offset + 2] = lo;
2206
+ lo >>= 8;
2207
+ destination[offset + 3] = lo;
2208
+ let hi = Number((value >> BigInt(32)) & mask32bits);
2209
+ destination[offset + 4] = hi;
2210
+ hi >>= 8;
2211
+ destination[offset + 5] = hi;
2212
+ hi >>= 8;
2213
+ destination[offset + 6] = hi;
2214
+ hi >>= 8;
2215
+ destination[offset + 7] = hi;
2216
+ return 8;
2217
+ },
2218
+ setFloat64LE(destination, offset, value) {
2219
+ FLOAT[0] = value;
2220
+ destination[offset] = FLOAT_BYTES[0];
2221
+ destination[offset + 1] = FLOAT_BYTES[1];
2222
+ destination[offset + 2] = FLOAT_BYTES[2];
2223
+ destination[offset + 3] = FLOAT_BYTES[3];
2224
+ destination[offset + 4] = FLOAT_BYTES[4];
2225
+ destination[offset + 5] = FLOAT_BYTES[5];
2226
+ destination[offset + 6] = FLOAT_BYTES[6];
2227
+ destination[offset + 7] = FLOAT_BYTES[7];
2228
+ return 8;
2229
+ }
2230
+ };
2231
+
2127
2232
  const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2128
2233
  let PROCESS_UNIQUE = null;
2129
- const kId = Symbol('id');
2130
2234
  class ObjectId extends BSONValue {
2131
2235
  get _bsontype() {
2132
2236
  return 'ObjectId';
@@ -2149,14 +2253,14 @@ class ObjectId extends BSONValue {
2149
2253
  workingId = inputId;
2150
2254
  }
2151
2255
  if (workingId == null || typeof workingId === 'number') {
2152
- this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2256
+ this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2153
2257
  }
2154
2258
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2155
- this[kId] = ByteUtils.toLocalBufferType(workingId);
2259
+ this.buffer = ByteUtils.toLocalBufferType(workingId);
2156
2260
  }
2157
2261
  else if (typeof workingId === 'string') {
2158
2262
  if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2159
- this[kId] = ByteUtils.fromHex(workingId);
2263
+ this.buffer = ByteUtils.fromHex(workingId);
2160
2264
  }
2161
2265
  else {
2162
2266
  throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
@@ -2170,10 +2274,10 @@ class ObjectId extends BSONValue {
2170
2274
  }
2171
2275
  }
2172
2276
  get id() {
2173
- return this[kId];
2277
+ return this.buffer;
2174
2278
  }
2175
2279
  set id(value) {
2176
- this[kId] = value;
2280
+ this.buffer = value;
2177
2281
  if (ObjectId.cacheHexString) {
2178
2282
  this.__id = ByteUtils.toHex(value);
2179
2283
  }
@@ -2196,8 +2300,8 @@ class ObjectId extends BSONValue {
2196
2300
  time = Math.floor(Date.now() / 1000);
2197
2301
  }
2198
2302
  const inc = ObjectId.getInc();
2199
- const buffer = ByteUtils.allocate(12);
2200
- BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
2303
+ const buffer = ByteUtils.allocateUnsafe(12);
2304
+ NumberUtils.setInt32BE(buffer, 0, time);
2201
2305
  if (PROCESS_UNIQUE === null) {
2202
2306
  PROCESS_UNIQUE = ByteUtils.randomBytes(5);
2203
2307
  }
@@ -2232,7 +2336,7 @@ class ObjectId extends BSONValue {
2232
2336
  return false;
2233
2337
  }
2234
2338
  if (ObjectId.is(otherId)) {
2235
- return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2339
+ return (this.buffer[11] === otherId.buffer[11] && ByteUtils.equals(this.buffer, otherId.buffer));
2236
2340
  }
2237
2341
  if (typeof otherId === 'string') {
2238
2342
  return otherId.toLowerCase() === this.toHexString();
@@ -2246,16 +2350,33 @@ class ObjectId extends BSONValue {
2246
2350
  }
2247
2351
  getTimestamp() {
2248
2352
  const timestamp = new Date();
2249
- const time = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
2353
+ const time = NumberUtils.getUint32BE(this.buffer, 0);
2250
2354
  timestamp.setTime(Math.floor(time) * 1000);
2251
2355
  return timestamp;
2252
2356
  }
2253
2357
  static createPk() {
2254
2358
  return new ObjectId();
2255
2359
  }
2360
+ serializeInto(uint8array, index) {
2361
+ uint8array[index] = this.buffer[0];
2362
+ uint8array[index + 1] = this.buffer[1];
2363
+ uint8array[index + 2] = this.buffer[2];
2364
+ uint8array[index + 3] = this.buffer[3];
2365
+ uint8array[index + 4] = this.buffer[4];
2366
+ uint8array[index + 5] = this.buffer[5];
2367
+ uint8array[index + 6] = this.buffer[6];
2368
+ uint8array[index + 7] = this.buffer[7];
2369
+ uint8array[index + 8] = this.buffer[8];
2370
+ uint8array[index + 9] = this.buffer[9];
2371
+ uint8array[index + 10] = this.buffer[10];
2372
+ uint8array[index + 11] = this.buffer[11];
2373
+ return 12;
2374
+ }
2256
2375
  static createFromTime(time) {
2257
- const buffer = ByteUtils.fromNumberArray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
2258
- BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
2376
+ const buffer = ByteUtils.allocate(12);
2377
+ for (let i = 11; i >= 4; i--)
2378
+ buffer[i] = 0;
2379
+ NumberUtils.setInt32BE(buffer, 0, time);
2259
2380
  return new ObjectId(buffer);
2260
2381
  }
2261
2382
  static createFromHexString(hexString) {
@@ -2627,10 +2748,7 @@ const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
2627
2748
  function internalDeserialize(buffer, options, isArray) {
2628
2749
  options = options == null ? {} : options;
2629
2750
  const index = options && options.index ? options.index : 0;
2630
- const size = buffer[index] |
2631
- (buffer[index + 1] << 8) |
2632
- (buffer[index + 2] << 16) |
2633
- (buffer[index + 3] << 24);
2751
+ const size = NumberUtils.getInt32LE(buffer, index);
2634
2752
  if (size < 5) {
2635
2753
  throw new BSONError(`bson size must be >= 5, is ${size}`);
2636
2754
  }
@@ -2666,7 +2784,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2666
2784
  const validation = options.validation == null ? { utf8: true } : options.validation;
2667
2785
  let globalUTFValidation = true;
2668
2786
  let validationSetting;
2669
- const utf8KeysSet = new Set();
2787
+ let utf8KeysSet;
2670
2788
  const utf8ValidatedKeys = validation.utf8;
2671
2789
  if (typeof utf8ValidatedKeys === 'boolean') {
2672
2790
  validationSetting = utf8ValidatedKeys;
@@ -2688,6 +2806,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2688
2806
  }
2689
2807
  }
2690
2808
  if (!globalUTFValidation) {
2809
+ utf8KeysSet = new Set();
2691
2810
  for (const key of Object.keys(utf8ValidatedKeys)) {
2692
2811
  utf8KeysSet.add(key);
2693
2812
  }
@@ -2695,14 +2814,14 @@ function deserializeObject(buffer, index, options, isArray = false) {
2695
2814
  const startIndex = index;
2696
2815
  if (buffer.length < 5)
2697
2816
  throw new BSONError('corrupt bson message < 5 bytes long');
2698
- const size = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
2817
+ const size = NumberUtils.getInt32LE(buffer, index);
2818
+ index += 4;
2699
2819
  if (size < 5 || size > buffer.length)
2700
2820
  throw new BSONError('corrupt bson message');
2701
2821
  const object = isArray ? [] : {};
2702
2822
  let arrayIndex = 0;
2703
2823
  const done = false;
2704
2824
  let isPossibleDBRef = isArray ? false : null;
2705
- const dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
2706
2825
  while (!done) {
2707
2826
  const elementType = buffer[index++];
2708
2827
  if (elementType === 0)
@@ -2715,7 +2834,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2715
2834
  throw new BSONError('Bad BSON Document: illegal CString');
2716
2835
  const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i, false);
2717
2836
  let shouldValidateKey = true;
2718
- if (globalUTFValidation || utf8KeysSet.has(name)) {
2837
+ if (globalUTFValidation || utf8KeysSet?.has(name)) {
2719
2838
  shouldValidateKey = validationSetting;
2720
2839
  }
2721
2840
  else {
@@ -2727,10 +2846,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
2727
2846
  let value;
2728
2847
  index = i + 1;
2729
2848
  if (elementType === BSON_DATA_STRING) {
2730
- const stringSize = buffer[index++] |
2731
- (buffer[index++] << 8) |
2732
- (buffer[index++] << 16) |
2733
- (buffer[index++] << 24);
2849
+ const stringSize = NumberUtils.getInt32LE(buffer, index);
2850
+ index += 4;
2734
2851
  if (stringSize <= 0 ||
2735
2852
  stringSize > buffer.length - index ||
2736
2853
  buffer[index + stringSize - 1] !== 0) {
@@ -2740,38 +2857,30 @@ function deserializeObject(buffer, index, options, isArray = false) {
2740
2857
  index = index + stringSize;
2741
2858
  }
2742
2859
  else if (elementType === BSON_DATA_OID) {
2743
- const oid = ByteUtils.allocate(12);
2744
- oid.set(buffer.subarray(index, index + 12));
2860
+ const oid = ByteUtils.allocateUnsafe(12);
2861
+ for (let i = 0; i < 12; i++)
2862
+ oid[i] = buffer[index + i];
2745
2863
  value = new ObjectId(oid);
2746
2864
  index = index + 12;
2747
2865
  }
2748
2866
  else if (elementType === BSON_DATA_INT && promoteValues === false) {
2749
- value = new Int32(buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24));
2867
+ value = new Int32(NumberUtils.getInt32LE(buffer, index));
2868
+ index += 4;
2750
2869
  }
2751
2870
  else if (elementType === BSON_DATA_INT) {
2752
- value =
2753
- buffer[index++] |
2754
- (buffer[index++] << 8) |
2755
- (buffer[index++] << 16) |
2756
- (buffer[index++] << 24);
2757
- }
2758
- else if (elementType === BSON_DATA_NUMBER && promoteValues === false) {
2759
- value = new Double(dataview.getFloat64(index, true));
2760
- index = index + 8;
2871
+ value = NumberUtils.getInt32LE(buffer, index);
2872
+ index += 4;
2761
2873
  }
2762
2874
  else if (elementType === BSON_DATA_NUMBER) {
2763
- value = dataview.getFloat64(index, true);
2764
- index = index + 8;
2875
+ value = NumberUtils.getFloat64LE(buffer, index);
2876
+ index += 8;
2877
+ if (promoteValues === false)
2878
+ value = new Double(value);
2765
2879
  }
2766
2880
  else if (elementType === BSON_DATA_DATE) {
2767
- const lowBits = buffer[index++] |
2768
- (buffer[index++] << 8) |
2769
- (buffer[index++] << 16) |
2770
- (buffer[index++] << 24);
2771
- const highBits = buffer[index++] |
2772
- (buffer[index++] << 8) |
2773
- (buffer[index++] << 16) |
2774
- (buffer[index++] << 24);
2881
+ const lowBits = NumberUtils.getInt32LE(buffer, index);
2882
+ const highBits = NumberUtils.getInt32LE(buffer, index + 4);
2883
+ index += 8;
2775
2884
  value = new Date(new Long(lowBits, highBits).toNumber());
2776
2885
  }
2777
2886
  else if (elementType === BSON_DATA_BOOLEAN) {
@@ -2781,10 +2890,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2781
2890
  }
2782
2891
  else if (elementType === BSON_DATA_OBJECT) {
2783
2892
  const _index = index;
2784
- const objectSize = buffer[index] |
2785
- (buffer[index + 1] << 8) |
2786
- (buffer[index + 2] << 16) |
2787
- (buffer[index + 3] << 24);
2893
+ const objectSize = NumberUtils.getInt32LE(buffer, index);
2788
2894
  if (objectSize <= 0 || objectSize > buffer.length - index)
2789
2895
  throw new BSONError('bad embedded document length in bson');
2790
2896
  if (raw) {
@@ -2801,10 +2907,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2801
2907
  }
2802
2908
  else if (elementType === BSON_DATA_ARRAY) {
2803
2909
  const _index = index;
2804
- const objectSize = buffer[index] |
2805
- (buffer[index + 1] << 8) |
2806
- (buffer[index + 2] << 16) |
2807
- (buffer[index + 3] << 24);
2910
+ const objectSize = NumberUtils.getInt32LE(buffer, index);
2808
2911
  let arrayOptions = options;
2809
2912
  const stopIndex = index + objectSize;
2810
2913
  if (fieldsAsRaw && fieldsAsRaw[name]) {
@@ -2827,40 +2930,36 @@ function deserializeObject(buffer, index, options, isArray = false) {
2827
2930
  value = null;
2828
2931
  }
2829
2932
  else if (elementType === BSON_DATA_LONG) {
2830
- const dataview = BSONDataView.fromUint8Array(buffer.subarray(index, index + 8));
2831
- const lowBits = buffer[index++] |
2832
- (buffer[index++] << 8) |
2833
- (buffer[index++] << 16) |
2834
- (buffer[index++] << 24);
2835
- const highBits = buffer[index++] |
2836
- (buffer[index++] << 8) |
2837
- (buffer[index++] << 16) |
2838
- (buffer[index++] << 24);
2839
- const long = new Long(lowBits, highBits);
2840
2933
  if (useBigInt64) {
2841
- value = dataview.getBigInt64(0, true);
2842
- }
2843
- else if (promoteLongs && promoteValues === true) {
2844
- value =
2845
- long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
2846
- ? long.toNumber()
2847
- : long;
2934
+ value = NumberUtils.getBigInt64LE(buffer, index);
2935
+ index += 8;
2848
2936
  }
2849
2937
  else {
2850
- value = long;
2938
+ const lowBits = NumberUtils.getInt32LE(buffer, index);
2939
+ const highBits = NumberUtils.getInt32LE(buffer, index + 4);
2940
+ index += 8;
2941
+ const long = new Long(lowBits, highBits);
2942
+ if (promoteLongs && promoteValues === true) {
2943
+ value =
2944
+ long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
2945
+ ? long.toNumber()
2946
+ : long;
2947
+ }
2948
+ else {
2949
+ value = long;
2950
+ }
2851
2951
  }
2852
2952
  }
2853
2953
  else if (elementType === BSON_DATA_DECIMAL128) {
2854
- const bytes = ByteUtils.allocate(16);
2855
- bytes.set(buffer.subarray(index, index + 16), 0);
2954
+ const bytes = ByteUtils.allocateUnsafe(16);
2955
+ for (let i = 0; i < 16; i++)
2956
+ bytes[i] = buffer[index + i];
2856
2957
  index = index + 16;
2857
2958
  value = new Decimal128(bytes);
2858
2959
  }
2859
2960
  else if (elementType === BSON_DATA_BINARY) {
2860
- let binarySize = buffer[index++] |
2861
- (buffer[index++] << 8) |
2862
- (buffer[index++] << 16) |
2863
- (buffer[index++] << 24);
2961
+ let binarySize = NumberUtils.getInt32LE(buffer, index);
2962
+ index += 4;
2864
2963
  const totalBinarySize = binarySize;
2865
2964
  const subType = buffer[index++];
2866
2965
  if (binarySize < 0)
@@ -2869,11 +2968,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
2869
2968
  throw new BSONError('Binary type size larger than document size');
2870
2969
  if (buffer['slice'] != null) {
2871
2970
  if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
2872
- binarySize =
2873
- buffer[index++] |
2874
- (buffer[index++] << 8) |
2875
- (buffer[index++] << 16) |
2876
- (buffer[index++] << 24);
2971
+ binarySize = NumberUtils.getInt32LE(buffer, index);
2972
+ index += 4;
2877
2973
  if (binarySize < 0)
2878
2974
  throw new BSONError('Negative binary type element size found for subtype 0x02');
2879
2975
  if (binarySize > totalBinarySize - 4)
@@ -2892,13 +2988,9 @@ function deserializeObject(buffer, index, options, isArray = false) {
2892
2988
  }
2893
2989
  }
2894
2990
  else {
2895
- const _buffer = ByteUtils.allocate(binarySize);
2896
2991
  if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
2897
- binarySize =
2898
- buffer[index++] |
2899
- (buffer[index++] << 8) |
2900
- (buffer[index++] << 16) |
2901
- (buffer[index++] << 24);
2992
+ binarySize = NumberUtils.getInt32LE(buffer, index);
2993
+ index += 4;
2902
2994
  if (binarySize < 0)
2903
2995
  throw new BSONError('Negative binary type element size found for subtype 0x02');
2904
2996
  if (binarySize > totalBinarySize - 4)
@@ -2906,11 +2998,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
2906
2998
  if (binarySize < totalBinarySize - 4)
2907
2999
  throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
2908
3000
  }
2909
- for (i = 0; i < binarySize; i++) {
2910
- _buffer[i] = buffer[index + i];
2911
- }
2912
3001
  if (promoteBuffers && promoteValues) {
2913
- value = _buffer;
3002
+ value = ByteUtils.allocateUnsafe(binarySize);
3003
+ for (i = 0; i < binarySize; i++) {
3004
+ value[i] = buffer[index + i];
3005
+ }
2914
3006
  }
2915
3007
  else {
2916
3008
  value = new Binary(buffer.slice(index, index + binarySize), subType);
@@ -2974,10 +3066,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
2974
3066
  value = new BSONRegExp(source, regExpOptions);
2975
3067
  }
2976
3068
  else if (elementType === BSON_DATA_SYMBOL) {
2977
- const stringSize = buffer[index++] |
2978
- (buffer[index++] << 8) |
2979
- (buffer[index++] << 16) |
2980
- (buffer[index++] << 24);
3069
+ const stringSize = NumberUtils.getInt32LE(buffer, index);
3070
+ index += 4;
2981
3071
  if (stringSize <= 0 ||
2982
3072
  stringSize > buffer.length - index ||
2983
3073
  buffer[index + stringSize - 1] !== 0) {
@@ -2988,15 +3078,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
2988
3078
  index = index + stringSize;
2989
3079
  }
2990
3080
  else if (elementType === BSON_DATA_TIMESTAMP) {
2991
- const i = buffer[index++] +
2992
- buffer[index++] * (1 << 8) +
2993
- buffer[index++] * (1 << 16) +
2994
- buffer[index++] * (1 << 24);
2995
- const t = buffer[index++] +
2996
- buffer[index++] * (1 << 8) +
2997
- buffer[index++] * (1 << 16) +
2998
- buffer[index++] * (1 << 24);
2999
- value = new Timestamp({ i, t });
3081
+ value = new Timestamp({
3082
+ i: NumberUtils.getUint32LE(buffer, index),
3083
+ t: NumberUtils.getUint32LE(buffer, index + 4)
3084
+ });
3085
+ index += 8;
3000
3086
  }
3001
3087
  else if (elementType === BSON_DATA_MIN_KEY) {
3002
3088
  value = new MinKey();
@@ -3005,10 +3091,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
3005
3091
  value = new MaxKey();
3006
3092
  }
3007
3093
  else if (elementType === BSON_DATA_CODE) {
3008
- const stringSize = buffer[index++] |
3009
- (buffer[index++] << 8) |
3010
- (buffer[index++] << 16) |
3011
- (buffer[index++] << 24);
3094
+ const stringSize = NumberUtils.getInt32LE(buffer, index);
3095
+ index += 4;
3012
3096
  if (stringSize <= 0 ||
3013
3097
  stringSize > buffer.length - index ||
3014
3098
  buffer[index + stringSize - 1] !== 0) {
@@ -3019,17 +3103,13 @@ function deserializeObject(buffer, index, options, isArray = false) {
3019
3103
  index = index + stringSize;
3020
3104
  }
3021
3105
  else if (elementType === BSON_DATA_CODE_W_SCOPE) {
3022
- const totalSize = buffer[index++] |
3023
- (buffer[index++] << 8) |
3024
- (buffer[index++] << 16) |
3025
- (buffer[index++] << 24);
3106
+ const totalSize = NumberUtils.getInt32LE(buffer, index);
3107
+ index += 4;
3026
3108
  if (totalSize < 4 + 4 + 4 + 1) {
3027
3109
  throw new BSONError('code_w_scope total size shorter minimum expected length');
3028
3110
  }
3029
- const stringSize = buffer[index++] |
3030
- (buffer[index++] << 8) |
3031
- (buffer[index++] << 16) |
3032
- (buffer[index++] << 24);
3111
+ const stringSize = NumberUtils.getInt32LE(buffer, index);
3112
+ index += 4;
3033
3113
  if (stringSize <= 0 ||
3034
3114
  stringSize > buffer.length - index ||
3035
3115
  buffer[index + stringSize - 1] !== 0) {
@@ -3038,10 +3118,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
3038
3118
  const functionString = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey);
3039
3119
  index = index + stringSize;
3040
3120
  const _index = index;
3041
- const objectSize = buffer[index] |
3042
- (buffer[index + 1] << 8) |
3043
- (buffer[index + 2] << 16) |
3044
- (buffer[index + 3] << 24);
3121
+ const objectSize = NumberUtils.getInt32LE(buffer, index);
3045
3122
  const scopeObject = deserializeObject(buffer, _index, options, false);
3046
3123
  index = index + objectSize;
3047
3124
  if (totalSize < 4 + 4 + objectSize + stringSize) {
@@ -3053,10 +3130,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
3053
3130
  value = new Code(functionString, scopeObject);
3054
3131
  }
3055
3132
  else if (elementType === BSON_DATA_DBPOINTER) {
3056
- const stringSize = buffer[index++] |
3057
- (buffer[index++] << 8) |
3058
- (buffer[index++] << 16) |
3059
- (buffer[index++] << 24);
3133
+ const stringSize = NumberUtils.getInt32LE(buffer, index);
3134
+ index += 4;
3060
3135
  if (stringSize <= 0 ||
3061
3136
  stringSize > buffer.length - index ||
3062
3137
  buffer[index + stringSize - 1] !== 0)
@@ -3068,8 +3143,9 @@ function deserializeObject(buffer, index, options, isArray = false) {
3068
3143
  }
3069
3144
  const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, false);
3070
3145
  index = index + stringSize;
3071
- const oidBuffer = ByteUtils.allocate(12);
3072
- oidBuffer.set(buffer.subarray(index, index + 12), 0);
3146
+ const oidBuffer = ByteUtils.allocateUnsafe(12);
3147
+ for (let i = 0; i < 12; i++)
3148
+ oidBuffer[i] = buffer[index + i];
3073
3149
  const oid = new ObjectId(oidBuffer);
3074
3150
  index = index + 12;
3075
3151
  value = new DBRef(namespace, oid);
@@ -3114,17 +3190,11 @@ function serializeString(buffer, key, value, index) {
3114
3190
  index = index + numberOfWrittenBytes + 1;
3115
3191
  buffer[index - 1] = 0;
3116
3192
  const size = ByteUtils.encodeUTF8Into(buffer, value, index + 4);
3117
- buffer[index + 3] = ((size + 1) >> 24) & 0xff;
3118
- buffer[index + 2] = ((size + 1) >> 16) & 0xff;
3119
- buffer[index + 1] = ((size + 1) >> 8) & 0xff;
3120
- buffer[index] = (size + 1) & 0xff;
3193
+ NumberUtils.setInt32LE(buffer, index, size + 1);
3121
3194
  index = index + 4 + size;
3122
3195
  buffer[index++] = 0;
3123
3196
  return index;
3124
3197
  }
3125
- const NUMBER_SPACE = new DataView(new ArrayBuffer(8), 0, 8);
3126
- const FOUR_BYTE_VIEW_ON_NUMBER = new Uint8Array(NUMBER_SPACE.buffer, 0, 4);
3127
- const EIGHT_BYTE_VIEW_ON_NUMBER = new Uint8Array(NUMBER_SPACE.buffer, 0, 8);
3128
3198
  function serializeNumber(buffer, key, value, index) {
3129
3199
  const isNegativeZero = Object.is(value, -0);
3130
3200
  const type = !isNegativeZero &&
@@ -3133,19 +3203,16 @@ function serializeNumber(buffer, key, value, index) {
3133
3203
  value >= BSON_INT32_MIN
3134
3204
  ? BSON_DATA_INT
3135
3205
  : BSON_DATA_NUMBER;
3136
- if (type === BSON_DATA_INT) {
3137
- NUMBER_SPACE.setInt32(0, value, true);
3138
- }
3139
- else {
3140
- NUMBER_SPACE.setFloat64(0, value, true);
3141
- }
3142
- const bytes = type === BSON_DATA_INT ? FOUR_BYTE_VIEW_ON_NUMBER : EIGHT_BYTE_VIEW_ON_NUMBER;
3143
3206
  buffer[index++] = type;
3144
3207
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3145
3208
  index = index + numberOfWrittenBytes;
3146
3209
  buffer[index++] = 0x00;
3147
- buffer.set(bytes, index);
3148
- index += bytes.byteLength;
3210
+ if (type === BSON_DATA_INT) {
3211
+ index += NumberUtils.setInt32LE(buffer, index, value);
3212
+ }
3213
+ else {
3214
+ index += NumberUtils.setFloat64LE(buffer, index, value);
3215
+ }
3149
3216
  return index;
3150
3217
  }
3151
3218
  function serializeBigInt(buffer, key, value, index) {
@@ -3153,9 +3220,7 @@ function serializeBigInt(buffer, key, value, index) {
3153
3220
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3154
3221
  index += numberOfWrittenBytes;
3155
3222
  buffer[index++] = 0;
3156
- NUMBER_SPACE.setBigInt64(0, value, true);
3157
- buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
3158
- index += EIGHT_BYTE_VIEW_ON_NUMBER.byteLength;
3223
+ index += NumberUtils.setBigInt64LE(buffer, index, value);
3159
3224
  return index;
3160
3225
  }
3161
3226
  function serializeNull(buffer, key, _, index) {
@@ -3181,14 +3246,8 @@ function serializeDate(buffer, key, value, index) {
3181
3246
  const dateInMilis = Long.fromNumber(value.getTime());
3182
3247
  const lowBits = dateInMilis.getLowBits();
3183
3248
  const highBits = dateInMilis.getHighBits();
3184
- buffer[index++] = lowBits & 0xff;
3185
- buffer[index++] = (lowBits >> 8) & 0xff;
3186
- buffer[index++] = (lowBits >> 16) & 0xff;
3187
- buffer[index++] = (lowBits >> 24) & 0xff;
3188
- buffer[index++] = highBits & 0xff;
3189
- buffer[index++] = (highBits >> 8) & 0xff;
3190
- buffer[index++] = (highBits >> 16) & 0xff;
3191
- buffer[index++] = (highBits >> 24) & 0xff;
3249
+ index += NumberUtils.setInt32LE(buffer, index, lowBits);
3250
+ index += NumberUtils.setInt32LE(buffer, index, highBits);
3192
3251
  return index;
3193
3252
  }
3194
3253
  function serializeRegExp(buffer, key, value, index) {
@@ -3245,15 +3304,7 @@ function serializeObjectId(buffer, key, value, index) {
3245
3304
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3246
3305
  index = index + numberOfWrittenBytes;
3247
3306
  buffer[index++] = 0;
3248
- const idValue = value.id;
3249
- if (isUint8Array(idValue)) {
3250
- for (let i = 0; i < 12; i++) {
3251
- buffer[index++] = idValue[i];
3252
- }
3253
- }
3254
- else {
3255
- throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3256
- }
3307
+ index += value.serializeInto(buffer, index);
3257
3308
  return index;
3258
3309
  }
3259
3310
  function serializeBuffer(buffer, key, value, index) {
@@ -3262,12 +3313,15 @@ function serializeBuffer(buffer, key, value, index) {
3262
3313
  index = index + numberOfWrittenBytes;
3263
3314
  buffer[index++] = 0;
3264
3315
  const size = value.length;
3265
- buffer[index++] = size & 0xff;
3266
- buffer[index++] = (size >> 8) & 0xff;
3267
- buffer[index++] = (size >> 16) & 0xff;
3268
- buffer[index++] = (size >> 24) & 0xff;
3316
+ index += NumberUtils.setInt32LE(buffer, index, size);
3269
3317
  buffer[index++] = BSON_BINARY_SUBTYPE_DEFAULT;
3270
- buffer.set(value, index);
3318
+ if (size <= 16) {
3319
+ for (let i = 0; i < size; i++)
3320
+ buffer[index + i] = value[i];
3321
+ }
3322
+ else {
3323
+ buffer.set(value, index);
3324
+ }
3271
3325
  index = index + size;
3272
3326
  return index;
3273
3327
  }
@@ -3289,7 +3343,8 @@ function serializeDecimal128(buffer, key, value, index) {
3289
3343
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3290
3344
  index = index + numberOfWrittenBytes;
3291
3345
  buffer[index++] = 0;
3292
- buffer.set(value.bytes.subarray(0, 16), index);
3346
+ for (let i = 0; i < 16; i++)
3347
+ buffer[index + i] = value.bytes[i];
3293
3348
  return index + 16;
3294
3349
  }
3295
3350
  function serializeLong(buffer, key, value, index) {
@@ -3300,14 +3355,8 @@ function serializeLong(buffer, key, value, index) {
3300
3355
  buffer[index++] = 0;
3301
3356
  const lowBits = value.getLowBits();
3302
3357
  const highBits = value.getHighBits();
3303
- buffer[index++] = lowBits & 0xff;
3304
- buffer[index++] = (lowBits >> 8) & 0xff;
3305
- buffer[index++] = (lowBits >> 16) & 0xff;
3306
- buffer[index++] = (lowBits >> 24) & 0xff;
3307
- buffer[index++] = highBits & 0xff;
3308
- buffer[index++] = (highBits >> 8) & 0xff;
3309
- buffer[index++] = (highBits >> 16) & 0xff;
3310
- buffer[index++] = (highBits >> 24) & 0xff;
3358
+ index += NumberUtils.setInt32LE(buffer, index, lowBits);
3359
+ index += NumberUtils.setInt32LE(buffer, index, highBits);
3311
3360
  return index;
3312
3361
  }
3313
3362
  function serializeInt32(buffer, key, value, index) {
@@ -3316,10 +3365,7 @@ function serializeInt32(buffer, key, value, index) {
3316
3365
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3317
3366
  index = index + numberOfWrittenBytes;
3318
3367
  buffer[index++] = 0;
3319
- buffer[index++] = value & 0xff;
3320
- buffer[index++] = (value >> 8) & 0xff;
3321
- buffer[index++] = (value >> 16) & 0xff;
3322
- buffer[index++] = (value >> 24) & 0xff;
3368
+ index += NumberUtils.setInt32LE(buffer, index, value);
3323
3369
  return index;
3324
3370
  }
3325
3371
  function serializeDouble(buffer, key, value, index) {
@@ -3327,9 +3373,7 @@ function serializeDouble(buffer, key, value, index) {
3327
3373
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3328
3374
  index = index + numberOfWrittenBytes;
3329
3375
  buffer[index++] = 0;
3330
- NUMBER_SPACE.setFloat64(0, value.value, true);
3331
- buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
3332
- index = index + 8;
3376
+ index += NumberUtils.setFloat64LE(buffer, index, value.value);
3333
3377
  return index;
3334
3378
  }
3335
3379
  function serializeFunction(buffer, key, value, index) {
@@ -3339,10 +3383,7 @@ function serializeFunction(buffer, key, value, index) {
3339
3383
  buffer[index++] = 0;
3340
3384
  const functionString = value.toString();
3341
3385
  const size = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 1;
3342
- buffer[index] = size & 0xff;
3343
- buffer[index + 1] = (size >> 8) & 0xff;
3344
- buffer[index + 2] = (size >> 16) & 0xff;
3345
- buffer[index + 3] = (size >> 24) & 0xff;
3386
+ NumberUtils.setInt32LE(buffer, index, size);
3346
3387
  index = index + 4 + size - 1;
3347
3388
  buffer[index++] = 0;
3348
3389
  return index;
@@ -3357,19 +3398,13 @@ function serializeCode(buffer, key, value, index, checkKeys = false, depth = 0,
3357
3398
  const functionString = value.code;
3358
3399
  index = index + 4;
3359
3400
  const codeSize = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 1;
3360
- buffer[index] = codeSize & 0xff;
3361
- buffer[index + 1] = (codeSize >> 8) & 0xff;
3362
- buffer[index + 2] = (codeSize >> 16) & 0xff;
3363
- buffer[index + 3] = (codeSize >> 24) & 0xff;
3401
+ NumberUtils.setInt32LE(buffer, index, codeSize);
3364
3402
  buffer[index + 4 + codeSize - 1] = 0;
3365
3403
  index = index + codeSize + 4;
3366
3404
  const endIndex = serializeInto(buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined, path);
3367
3405
  index = endIndex - 1;
3368
3406
  const totalSize = endIndex - startIndex;
3369
- buffer[startIndex++] = totalSize & 0xff;
3370
- buffer[startIndex++] = (totalSize >> 8) & 0xff;
3371
- buffer[startIndex++] = (totalSize >> 16) & 0xff;
3372
- buffer[startIndex++] = (totalSize >> 24) & 0xff;
3407
+ startIndex += NumberUtils.setInt32LE(buffer, startIndex, totalSize);
3373
3408
  buffer[index++] = 0;
3374
3409
  }
3375
3410
  else {
@@ -3379,10 +3414,7 @@ function serializeCode(buffer, key, value, index, checkKeys = false, depth = 0,
3379
3414
  buffer[index++] = 0;
3380
3415
  const functionString = value.code.toString();
3381
3416
  const size = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 1;
3382
- buffer[index] = size & 0xff;
3383
- buffer[index + 1] = (size >> 8) & 0xff;
3384
- buffer[index + 2] = (size >> 16) & 0xff;
3385
- buffer[index + 3] = (size >> 24) & 0xff;
3417
+ NumberUtils.setInt32LE(buffer, index, size);
3386
3418
  index = index + 4 + size - 1;
3387
3419
  buffer[index++] = 0;
3388
3420
  }
@@ -3397,19 +3429,19 @@ function serializeBinary(buffer, key, value, index) {
3397
3429
  let size = value.position;
3398
3430
  if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY)
3399
3431
  size = size + 4;
3400
- buffer[index++] = size & 0xff;
3401
- buffer[index++] = (size >> 8) & 0xff;
3402
- buffer[index++] = (size >> 16) & 0xff;
3403
- buffer[index++] = (size >> 24) & 0xff;
3432
+ index += NumberUtils.setInt32LE(buffer, index, size);
3404
3433
  buffer[index++] = value.sub_type;
3405
3434
  if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
3406
3435
  size = size - 4;
3407
- buffer[index++] = size & 0xff;
3408
- buffer[index++] = (size >> 8) & 0xff;
3409
- buffer[index++] = (size >> 16) & 0xff;
3410
- buffer[index++] = (size >> 24) & 0xff;
3436
+ index += NumberUtils.setInt32LE(buffer, index, size);
3437
+ }
3438
+ if (size <= 16) {
3439
+ for (let i = 0; i < size; i++)
3440
+ buffer[index + i] = data[i];
3441
+ }
3442
+ else {
3443
+ buffer.set(data, index);
3411
3444
  }
3412
- buffer.set(data, index);
3413
3445
  index = index + value.position;
3414
3446
  return index;
3415
3447
  }
@@ -3419,12 +3451,9 @@ function serializeSymbol(buffer, key, value, index) {
3419
3451
  index = index + numberOfWrittenBytes;
3420
3452
  buffer[index++] = 0;
3421
3453
  const size = ByteUtils.encodeUTF8Into(buffer, value.value, index + 4) + 1;
3422
- buffer[index] = size & 0xff;
3423
- buffer[index + 1] = (size >> 8) & 0xff;
3424
- buffer[index + 2] = (size >> 16) & 0xff;
3425
- buffer[index + 3] = (size >> 24) & 0xff;
3454
+ NumberUtils.setInt32LE(buffer, index, size);
3426
3455
  index = index + 4 + size - 1;
3427
- buffer[index++] = 0x00;
3456
+ buffer[index++] = 0;
3428
3457
  return index;
3429
3458
  }
3430
3459
  function serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path) {
@@ -3443,10 +3472,7 @@ function serializeDBRef(buffer, key, value, index, depth, serializeFunctions, pa
3443
3472
  output = Object.assign(output, value.fields);
3444
3473
  const endIndex = serializeInto(buffer, output, false, index, depth + 1, serializeFunctions, true, path);
3445
3474
  const size = endIndex - startIndex;
3446
- buffer[startIndex++] = size & 0xff;
3447
- buffer[startIndex++] = (size >> 8) & 0xff;
3448
- buffer[startIndex++] = (size >> 16) & 0xff;
3449
- buffer[startIndex++] = (size >> 24) & 0xff;
3475
+ startIndex += NumberUtils.setInt32LE(buffer, index, size);
3450
3476
  return endIndex;
3451
3477
  }
3452
3478
  function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializeFunctions, ignoreUndefined, path) {
@@ -3582,7 +3608,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3582
3608
  if ('$' === key[0]) {
3583
3609
  throw new BSONError('key ' + key + " must not start with '$'");
3584
3610
  }
3585
- else if (~key.indexOf('.')) {
3611
+ else if (key.includes('.')) {
3586
3612
  throw new BSONError('key ' + key + " must not contain '.'");
3587
3613
  }
3588
3614
  }
@@ -3680,7 +3706,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3680
3706
  if ('$' === key[0]) {
3681
3707
  throw new BSONError('key ' + key + " must not start with '$'");
3682
3708
  }
3683
- else if (~key.indexOf('.')) {
3709
+ else if (key.includes('.')) {
3684
3710
  throw new BSONError('key ' + key + " must not contain '.'");
3685
3711
  }
3686
3712
  }
@@ -3764,10 +3790,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3764
3790
  path.delete(object);
3765
3791
  buffer[index++] = 0x00;
3766
3792
  const size = index - startingIndex;
3767
- buffer[startingIndex++] = size & 0xff;
3768
- buffer[startingIndex++] = (size >> 8) & 0xff;
3769
- buffer[startingIndex++] = (size >> 16) & 0xff;
3770
- buffer[startingIndex++] = (size >> 24) & 0xff;
3793
+ startingIndex += NumberUtils.setInt32LE(buffer, startingIndex, size);
3771
3794
  return index;
3772
3795
  }
3773
3796
 
@@ -4097,7 +4120,7 @@ function serialize(object, options = {}) {
4097
4120
  buffer = ByteUtils.allocate(minInternalBufferSize);
4098
4121
  }
4099
4122
  const serializationIndex = serializeInto(buffer, object, checkKeys, 0, 0, serializeFunctions, ignoreUndefined, null);
4100
- const finishedBuffer = ByteUtils.allocate(serializationIndex);
4123
+ const finishedBuffer = ByteUtils.allocateUnsafe(serializationIndex);
4101
4124
  finishedBuffer.set(buffer.subarray(0, serializationIndex), 0);
4102
4125
  return finishedBuffer;
4103
4126
  }
@@ -4124,10 +4147,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4124
4147
  const bufferData = ByteUtils.toLocalBufferType(data);
4125
4148
  let index = startIndex;
4126
4149
  for (let i = 0; i < numberOfDocuments; i++) {
4127
- const size = bufferData[index] |
4128
- (bufferData[index + 1] << 8) |
4129
- (bufferData[index + 2] << 16) |
4130
- (bufferData[index + 3] << 24);
4150
+ const size = NumberUtils.getInt32LE(bufferData, index);
4131
4151
  internalOptions.index = index;
4132
4152
  documents[docStartIndex + i] = internalDeserialize(bufferData, internalOptions);
4133
4153
  index = index + size;