bson 5.0.0-alpha.1 → 5.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,19 +2,23 @@ var BSON = (function (exports) {
2
2
  'use strict';
3
3
 
4
4
  class BSONError extends Error {
5
- constructor(message) {
6
- super(message);
5
+ get bsonError() {
6
+ return true;
7
7
  }
8
8
  get name() {
9
9
  return 'BSONError';
10
10
  }
11
- }
12
- class BSONTypeError extends TypeError {
13
11
  constructor(message) {
14
12
  super(message);
15
13
  }
16
- get name() {
17
- return 'BSONTypeError';
14
+ static isBSONError(value) {
15
+ return (value != null &&
16
+ typeof value === 'object' &&
17
+ 'bsonError' in value &&
18
+ value.bsonError === true &&
19
+ 'name' in value &&
20
+ 'message' in value &&
21
+ 'stack' in value);
18
22
  }
19
23
  }
20
24
 
@@ -213,7 +217,7 @@ const VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f
213
217
  const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
214
218
  const uuidHexStringToBuffer = (hexString) => {
215
219
  if (!uuidValidateString(hexString)) {
216
- throw new BSONTypeError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
220
+ throw new BSONError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
217
221
  }
218
222
  const sanitizedHexString = hexString.replace(/-/g, '');
219
223
  return ByteUtils.fromHex(sanitizedHexString);
@@ -237,12 +241,6 @@ function isAnyArrayBuffer(value) {
237
241
  function isUint8Array(value) {
238
242
  return Object.prototype.toString.call(value) === '[object Uint8Array]';
239
243
  }
240
- function isBigInt64Array(value) {
241
- return Object.prototype.toString.call(value) === '[object BigInt64Array]';
242
- }
243
- function isBigUInt64Array(value) {
244
- return Object.prototype.toString.call(value) === '[object BigUint64Array]';
245
- }
246
244
  function isRegExp(d) {
247
245
  return Object.prototype.toString.call(d) === '[object RegExp]';
248
246
  }
@@ -320,7 +318,7 @@ class Binary {
320
318
  !ArrayBuffer.isView(buffer) &&
321
319
  !(buffer instanceof ArrayBuffer) &&
322
320
  !Array.isArray(buffer)) {
323
- throw new BSONTypeError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
321
+ throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
324
322
  }
325
323
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
326
324
  if (buffer == null) {
@@ -342,10 +340,10 @@ class Binary {
342
340
  }
343
341
  put(byteValue) {
344
342
  if (typeof byteValue === 'string' && byteValue.length !== 1) {
345
- throw new BSONTypeError('only accepts single character String');
343
+ throw new BSONError('only accepts single character String');
346
344
  }
347
345
  else if (typeof byteValue !== 'number' && byteValue.length !== 1)
348
- throw new BSONTypeError('only accepts single character Uint8Array or Array');
346
+ throw new BSONError('only accepts single character Uint8Array or Array');
349
347
  let decodedByte;
350
348
  if (typeof byteValue === 'string') {
351
349
  decodedByte = byteValue.charCodeAt(0);
@@ -357,7 +355,7 @@ class Binary {
357
355
  decodedByte = byteValue[0];
358
356
  }
359
357
  if (decodedByte < 0 || decodedByte > 255) {
360
- throw new BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
358
+ throw new BSONError('only accepts number in a valid unsigned byte range 0-255');
361
359
  }
362
360
  if (this.buffer.byteLength > this.position) {
363
361
  this.buffer[this.position++] = decodedByte;
@@ -461,7 +459,7 @@ class Binary {
461
459
  data = uuidHexStringToBuffer(doc.$uuid);
462
460
  }
463
461
  if (!data) {
464
- throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
462
+ throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
465
463
  }
466
464
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
467
465
  }
@@ -505,7 +503,7 @@ class UUID extends Binary {
505
503
  bytes = uuidHexStringToBuffer(input);
506
504
  }
507
505
  else {
508
- throw new BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
506
+ throw new BSONError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
509
507
  }
510
508
  super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
511
509
  this.__id = hexStr;
@@ -789,7 +787,7 @@ class Long {
789
787
  }
790
788
  static fromString(str, unsigned, radix) {
791
789
  if (str.length === 0)
792
- throw Error('empty string');
790
+ throw new BSONError('empty string');
793
791
  if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
794
792
  return Long.ZERO;
795
793
  if (typeof unsigned === 'number') {
@@ -800,10 +798,10 @@ class Long {
800
798
  }
801
799
  radix = radix || 10;
802
800
  if (radix < 2 || 36 < radix)
803
- throw RangeError('radix');
801
+ throw new BSONError('radix');
804
802
  let p;
805
803
  if ((p = str.indexOf('-')) > 0)
806
- throw Error('interior hyphen');
804
+ throw new BSONError('interior hyphen');
807
805
  else if (p === 0) {
808
806
  return Long.fromString(str.substring(1), unsigned, radix).neg();
809
807
  }
@@ -899,7 +897,7 @@ class Long {
899
897
  if (!Long.isLong(divisor))
900
898
  divisor = Long.fromValue(divisor);
901
899
  if (divisor.isZero())
902
- throw Error('division by zero');
900
+ throw new BSONError('division by zero');
903
901
  if (wasm) {
904
902
  if (!this.unsigned &&
905
903
  this.high === -0x80000000 &&
@@ -1254,7 +1252,7 @@ class Long {
1254
1252
  toString(radix) {
1255
1253
  radix = radix || 10;
1256
1254
  if (radix < 2 || 36 < radix)
1257
- throw RangeError('radix');
1255
+ throw new BSONError('radix');
1258
1256
  if (this.isZero())
1259
1257
  return '0';
1260
1258
  if (this.isNegative()) {
@@ -1398,7 +1396,7 @@ function lessThan(left, right) {
1398
1396
  return false;
1399
1397
  }
1400
1398
  function invalidErr(string, message) {
1401
- throw new BSONTypeError(`"${string}" is not a valid Decimal128 string - ${message}`);
1399
+ throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`);
1402
1400
  }
1403
1401
  class Decimal128 {
1404
1402
  get _bsontype() {
@@ -1413,12 +1411,12 @@ class Decimal128 {
1413
1411
  }
1414
1412
  else if (isUint8Array(bytes)) {
1415
1413
  if (bytes.byteLength !== 16) {
1416
- throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
1414
+ throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1417
1415
  }
1418
1416
  this.bytes = bytes;
1419
1417
  }
1420
1418
  else {
1421
- throw new BSONTypeError('Decimal128 must take a Buffer or string');
1419
+ throw new BSONError('Decimal128 must take a Buffer or string');
1422
1420
  }
1423
1421
  }
1424
1422
  static fromString(representation) {
@@ -1442,13 +1440,13 @@ class Decimal128 {
1442
1440
  let biasedExponent = 0;
1443
1441
  let index = 0;
1444
1442
  if (representation.length >= 7000) {
1445
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1443
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1446
1444
  }
1447
1445
  const stringMatch = representation.match(PARSE_STRING_REGEXP);
1448
1446
  const infMatch = representation.match(PARSE_INF_REGEXP);
1449
1447
  const nanMatch = representation.match(PARSE_NAN_REGEXP);
1450
1448
  if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
1451
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1449
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1452
1450
  }
1453
1451
  if (stringMatch) {
1454
1452
  const unsignedNumber = stringMatch[2];
@@ -1500,7 +1498,7 @@ class Decimal128 {
1500
1498
  index = index + 1;
1501
1499
  }
1502
1500
  if (sawRadix && !nDigitsRead)
1503
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1501
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1504
1502
  if (representation[index] === 'e' || representation[index] === 'E') {
1505
1503
  const match = representation.substr(++index).match(EXPONENT_REGEX);
1506
1504
  if (!match || !match[2])
@@ -1856,19 +1854,11 @@ class Double {
1856
1854
  return this.value;
1857
1855
  }
1858
1856
  if (Object.is(Math.sign(this.value), -0)) {
1859
- return { $numberDouble: `-${this.value.toFixed(1)}` };
1860
- }
1861
- let $numberDouble;
1862
- if (Number.isInteger(this.value)) {
1863
- $numberDouble = this.value.toFixed(1);
1864
- if ($numberDouble.length >= 13) {
1865
- $numberDouble = this.value.toExponential(13).toUpperCase();
1866
- }
1857
+ return { $numberDouble: '-0.0' };
1867
1858
  }
1868
- else {
1869
- $numberDouble = this.value.toString();
1870
- }
1871
- return { $numberDouble };
1859
+ return {
1860
+ $numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
1861
+ };
1872
1862
  }
1873
1863
  static fromExtendedJSON(doc, options) {
1874
1864
  const doubleValue = parseFloat(doc.$numberDouble);
@@ -1977,7 +1967,7 @@ class ObjectId {
1977
1967
  let workingId;
1978
1968
  if (typeof inputId === 'object' && inputId && 'id' in inputId) {
1979
1969
  if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
1980
- throw new BSONTypeError('Argument passed in must have an id that is of type string or Buffer');
1970
+ throw new BSONError('Argument passed in must have an id that is of type string or Buffer');
1981
1971
  }
1982
1972
  if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
1983
1973
  workingId = ByteUtils.fromHex(inputId.toHexString());
@@ -2002,18 +1992,18 @@ class ObjectId {
2002
1992
  this[kId] = bytes;
2003
1993
  }
2004
1994
  else {
2005
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes');
1995
+ throw new BSONError('Argument passed in must be a string of 12 bytes');
2006
1996
  }
2007
1997
  }
2008
1998
  else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2009
1999
  this[kId] = ByteUtils.fromHex(workingId);
2010
2000
  }
2011
2001
  else {
2012
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
2002
+ throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
2013
2003
  }
2014
2004
  }
2015
2005
  else {
2016
- throw new BSONTypeError('Argument passed in does not match the accepted types');
2006
+ throw new BSONError('Argument passed in does not match the accepted types');
2017
2007
  }
2018
2008
  if (ObjectId.cacheHexString) {
2019
2009
  this.__id = ByteUtils.toHex(this.id);
@@ -2115,7 +2105,7 @@ class ObjectId {
2115
2105
  }
2116
2106
  static createFromHexString(hexString) {
2117
2107
  if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2118
- throw new BSONTypeError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2108
+ throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2119
2109
  }
2120
2110
  return new ObjectId(ByteUtils.fromHex(hexString));
2121
2111
  }
@@ -2192,7 +2182,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2192
2182
  case 'boolean':
2193
2183
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 1);
2194
2184
  case 'object':
2195
- if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
2185
+ if (value != null &&
2186
+ typeof value._bsontype === 'string' &&
2187
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2188
+ throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
2189
+ }
2190
+ else if (value == null ||
2191
+ value['_bsontype'] === 'MinKey' ||
2192
+ value['_bsontype'] === 'MaxKey') {
2196
2193
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
2197
2194
  }
2198
2195
  else if (value['_bsontype'] === 'ObjectId') {
@@ -2350,7 +2347,7 @@ class BSONRegExp {
2350
2347
  if ('$regularExpression' in doc) {
2351
2348
  return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
2352
2349
  }
2353
- throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2350
+ throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2354
2351
  }
2355
2352
  [Symbol.for('nodejs.util.inspect.custom')]() {
2356
2353
  return this.inspect();
@@ -2542,9 +2539,16 @@ function deserializeObject(buffer, index, options, isArray = false) {
2542
2539
  const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
2543
2540
  const raw = options['raw'] == null ? false : options['raw'];
2544
2541
  const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
2545
- const promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
2546
- const promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
2547
- const promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
2542
+ const promoteBuffers = options.promoteBuffers ?? false;
2543
+ const promoteLongs = options.promoteLongs ?? true;
2544
+ const promoteValues = options.promoteValues ?? true;
2545
+ const useBigInt64 = options.useBigInt64 ?? false;
2546
+ if (useBigInt64 && !promoteValues) {
2547
+ throw new BSONError('Must either request bigint or Long for int64 deserialization');
2548
+ }
2549
+ if (useBigInt64 && !promoteLongs) {
2550
+ throw new BSONError('Must either request bigint or Long for int64 deserialization');
2551
+ }
2548
2552
  const validation = options.validation == null ? { utf8: true } : options.validation;
2549
2553
  let globalUTFValidation = true;
2550
2554
  let validationSetting;
@@ -2709,6 +2713,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2709
2713
  value = null;
2710
2714
  }
2711
2715
  else if (elementType === BSON_DATA_LONG) {
2716
+ const dataview = BSONDataView.fromUint8Array(buffer.subarray(index, index + 8));
2712
2717
  const lowBits = buffer[index++] |
2713
2718
  (buffer[index++] << 8) |
2714
2719
  (buffer[index++] << 16) |
@@ -2718,7 +2723,10 @@ function deserializeObject(buffer, index, options, isArray = false) {
2718
2723
  (buffer[index++] << 16) |
2719
2724
  (buffer[index++] << 24);
2720
2725
  const long = new Long(lowBits, highBits);
2721
- if (promoteLongs && promoteValues === true) {
2726
+ if (useBigInt64) {
2727
+ value = dataview.getBigInt64(0, true);
2728
+ }
2729
+ else if (promoteLongs && promoteValues === true) {
2722
2730
  value =
2723
2731
  long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
2724
2732
  ? long.toNumber()
@@ -3040,6 +3048,16 @@ function serializeNumber(buffer, key, value, index) {
3040
3048
  index += bytes.byteLength;
3041
3049
  return index;
3042
3050
  }
3051
+ function serializeBigInt(buffer, key, value, index) {
3052
+ buffer[index++] = BSON_DATA_LONG;
3053
+ const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3054
+ index += numberOfWrittenBytes;
3055
+ buffer[index++] = 0;
3056
+ NUMBER_SPACE.setBigInt64(0, value, true);
3057
+ buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
3058
+ index += EIGHT_BYTE_VIEW_ON_NUMBER.byteLength;
3059
+ return index;
3060
+ }
3043
3061
  function serializeNull(buffer, key, _, index) {
3044
3062
  buffer[index++] = BSON_DATA_NULL;
3045
3063
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
@@ -3079,7 +3097,7 @@ function serializeRegExp(buffer, key, value, index) {
3079
3097
  index = index + numberOfWrittenBytes;
3080
3098
  buffer[index++] = 0;
3081
3099
  if (value.source && value.source.match(regexp) != null) {
3082
- throw Error('value ' + value.source + ' must not contain null bytes');
3100
+ throw new BSONError('value ' + value.source + ' must not contain null bytes');
3083
3101
  }
3084
3102
  index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index);
3085
3103
  buffer[index++] = 0x00;
@@ -3098,7 +3116,7 @@ function serializeBSONRegExp(buffer, key, value, index) {
3098
3116
  index = index + numberOfWrittenBytes;
3099
3117
  buffer[index++] = 0;
3100
3118
  if (value.pattern.match(regexp) != null) {
3101
- throw Error('pattern ' + value.pattern + ' must not contain null bytes');
3119
+ throw new BSONError('pattern ' + value.pattern + ' must not contain null bytes');
3102
3120
  }
3103
3121
  index = index + ByteUtils.encodeUTF8Into(buffer, value.pattern, index);
3104
3122
  buffer[index++] = 0x00;
@@ -3131,7 +3149,7 @@ function serializeObjectId(buffer, key, value, index) {
3131
3149
  buffer.set(value.id.subarray(0, 12), index);
3132
3150
  }
3133
3151
  else {
3134
- throw new BSONTypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3152
+ throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3135
3153
  }
3136
3154
  return index + 12;
3137
3155
  }
@@ -3371,7 +3389,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3371
3389
  index = serializeNumber(buffer, key, value, index);
3372
3390
  }
3373
3391
  else if (typeof value === 'bigint') {
3374
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3392
+ index = serializeBigInt(buffer, key, value, index);
3375
3393
  }
3376
3394
  else if (typeof value === 'boolean') {
3377
3395
  index = serializeBoolean(buffer, key, value, index);
@@ -3394,7 +3412,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3394
3412
  else if (typeof value === 'object' && value._bsontype == null) {
3395
3413
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3396
3414
  }
3397
- else if (typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] == null) {
3415
+ else if (typeof value === 'object' &&
3416
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3398
3417
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3399
3418
  }
3400
3419
  else if (value._bsontype === 'ObjectId') {
@@ -3434,7 +3453,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3434
3453
  index = serializeMinMax(buffer, key, value, index);
3435
3454
  }
3436
3455
  else if (typeof value._bsontype !== 'undefined') {
3437
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3456
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3438
3457
  }
3439
3458
  }
3440
3459
  }
@@ -3447,18 +3466,21 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3447
3466
  if (done)
3448
3467
  continue;
3449
3468
  const key = entry.value[0];
3450
- const value = entry.value[1];
3469
+ let value = entry.value[1];
3470
+ if (typeof value?.toBSON === 'function') {
3471
+ value = value.toBSON();
3472
+ }
3451
3473
  const type = typeof value;
3452
3474
  if (typeof key === 'string' && !ignoreKeys.has(key)) {
3453
3475
  if (key.match(regexp) != null) {
3454
- throw Error('key ' + key + ' must not contain null bytes');
3476
+ throw new BSONError('key ' + key + ' must not contain null bytes');
3455
3477
  }
3456
3478
  if (checkKeys) {
3457
3479
  if ('$' === key[0]) {
3458
- throw Error('key ' + key + " must not start with '$'");
3480
+ throw new BSONError('key ' + key + " must not start with '$'");
3459
3481
  }
3460
3482
  else if (~key.indexOf('.')) {
3461
- throw Error('key ' + key + " must not contain '.'");
3483
+ throw new BSONError('key ' + key + " must not contain '.'");
3462
3484
  }
3463
3485
  }
3464
3486
  }
@@ -3468,8 +3490,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3468
3490
  else if (type === 'number') {
3469
3491
  index = serializeNumber(buffer, key, value, index);
3470
3492
  }
3471
- else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
3472
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3493
+ else if (type === 'bigint') {
3494
+ index = serializeBigInt(buffer, key, value, index);
3473
3495
  }
3474
3496
  else if (type === 'boolean') {
3475
3497
  index = serializeBoolean(buffer, key, value, index);
@@ -3489,7 +3511,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3489
3511
  else if (type === 'object' && value._bsontype == null) {
3490
3512
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3491
3513
  }
3492
- else if (typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] == null) {
3514
+ else if (typeof value === 'object' &&
3515
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3493
3516
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3494
3517
  }
3495
3518
  else if (value._bsontype === 'ObjectId') {
@@ -3529,7 +3552,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3529
3552
  index = serializeMinMax(buffer, key, value, index);
3530
3553
  }
3531
3554
  else if (typeof value._bsontype !== 'undefined') {
3532
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3555
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3533
3556
  }
3534
3557
  }
3535
3558
  }
@@ -3537,7 +3560,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3537
3560
  if (typeof object?.toBSON === 'function') {
3538
3561
  object = object.toBSON();
3539
3562
  if (object != null && typeof object !== 'object') {
3540
- throw new BSONTypeError('toBSON function did not return an object');
3563
+ throw new BSONError('toBSON function did not return an object');
3541
3564
  }
3542
3565
  }
3543
3566
  for (const key of Object.keys(object)) {
@@ -3548,14 +3571,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3548
3571
  const type = typeof value;
3549
3572
  if (typeof key === 'string' && !ignoreKeys.has(key)) {
3550
3573
  if (key.match(regexp) != null) {
3551
- throw Error('key ' + key + ' must not contain null bytes');
3574
+ throw new BSONError('key ' + key + ' must not contain null bytes');
3552
3575
  }
3553
3576
  if (checkKeys) {
3554
3577
  if ('$' === key[0]) {
3555
- throw Error('key ' + key + " must not start with '$'");
3578
+ throw new BSONError('key ' + key + " must not start with '$'");
3556
3579
  }
3557
3580
  else if (~key.indexOf('.')) {
3558
- throw Error('key ' + key + " must not contain '.'");
3581
+ throw new BSONError('key ' + key + " must not contain '.'");
3559
3582
  }
3560
3583
  }
3561
3584
  }
@@ -3566,7 +3589,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3566
3589
  index = serializeNumber(buffer, key, value, index);
3567
3590
  }
3568
3591
  else if (type === 'bigint') {
3569
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3592
+ index = serializeBigInt(buffer, key, value, index);
3570
3593
  }
3571
3594
  else if (type === 'boolean') {
3572
3595
  index = serializeBoolean(buffer, key, value, index);
@@ -3590,7 +3613,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3590
3613
  else if (type === 'object' && value._bsontype == null) {
3591
3614
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3592
3615
  }
3593
- else if (typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] == null) {
3616
+ else if (typeof value === 'object' &&
3617
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3594
3618
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3595
3619
  }
3596
3620
  else if (value._bsontype === 'ObjectId') {
@@ -3630,7 +3654,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3630
3654
  index = serializeMinMax(buffer, key, value, index);
3631
3655
  }
3632
3656
  else if (typeof value._bsontype !== 'undefined') {
3633
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3657
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3634
3658
  }
3635
3659
  }
3636
3660
  }
@@ -3764,7 +3788,7 @@ function serializeValue(value, options) {
3764
3788
  const current = props[props.length - 1];
3765
3789
  const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
3766
3790
  const dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
3767
- throw new BSONTypeError('Converting circular structure to EJSON:\n' +
3791
+ throw new BSONError('Converting circular structure to EJSON:\n' +
3768
3792
  ` ${leadingPart}${alreadySeen}${circularPart}${current}\n` +
3769
3793
  ` ${leadingSpace}\\${dashes}/`);
3770
3794
  }
@@ -3857,7 +3881,7 @@ function serializeDocument(doc, options) {
3857
3881
  else if (doc != null &&
3858
3882
  typeof doc === 'object' &&
3859
3883
  typeof doc._bsontype === 'string' &&
3860
- doc[Symbol.for('@@mdb.bson.version')] == null) {
3884
+ doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3861
3885
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3862
3886
  }
3863
3887
  else if (isBSONType(doc)) {
@@ -3865,7 +3889,7 @@ function serializeDocument(doc, options) {
3865
3889
  if (typeof outDoc.toExtendedJSON !== 'function') {
3866
3890
  const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
3867
3891
  if (!mapper) {
3868
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
3892
+ throw new BSONError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
3869
3893
  }
3870
3894
  outDoc = mapper(outDoc);
3871
3895
  }
@@ -3997,7 +4021,6 @@ deserialize: deserialize,
3997
4021
  calculateObjectSize: calculateObjectSize,
3998
4022
  deserializeStream: deserializeStream,
3999
4023
  BSONError: BSONError,
4000
- BSONTypeError: BSONTypeError,
4001
4024
  BSONType: BSONType,
4002
4025
  EJSON: EJSON
4003
4026
  });
@@ -4007,7 +4030,6 @@ exports.BSONError = BSONError;
4007
4030
  exports.BSONRegExp = BSONRegExp;
4008
4031
  exports.BSONSymbol = BSONSymbol;
4009
4032
  exports.BSONType = BSONType;
4010
- exports.BSONTypeError = BSONTypeError;
4011
4033
  exports.Binary = Binary;
4012
4034
  exports.Code = Code;
4013
4035
  exports.DBRef = DBRef;