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.
package/lib/bson.mjs CHANGED
@@ -1,17 +1,21 @@
1
1
  class BSONError extends Error {
2
- constructor(message) {
3
- super(message);
2
+ get bsonError() {
3
+ return true;
4
4
  }
5
5
  get name() {
6
6
  return 'BSONError';
7
7
  }
8
- }
9
- class BSONTypeError extends TypeError {
10
8
  constructor(message) {
11
9
  super(message);
12
10
  }
13
- get name() {
14
- return 'BSONTypeError';
11
+ static isBSONError(value) {
12
+ return (value != null &&
13
+ typeof value === 'object' &&
14
+ 'bsonError' in value &&
15
+ value.bsonError === true &&
16
+ 'name' in value &&
17
+ 'message' in value &&
18
+ 'stack' in value);
15
19
  }
16
20
  }
17
21
 
@@ -210,7 +214,7 @@ const VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f
210
214
  const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
211
215
  const uuidHexStringToBuffer = (hexString) => {
212
216
  if (!uuidValidateString(hexString)) {
213
- 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".');
217
+ 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".');
214
218
  }
215
219
  const sanitizedHexString = hexString.replace(/-/g, '');
216
220
  return ByteUtils.fromHex(sanitizedHexString);
@@ -234,12 +238,6 @@ function isAnyArrayBuffer(value) {
234
238
  function isUint8Array(value) {
235
239
  return Object.prototype.toString.call(value) === '[object Uint8Array]';
236
240
  }
237
- function isBigInt64Array(value) {
238
- return Object.prototype.toString.call(value) === '[object BigInt64Array]';
239
- }
240
- function isBigUInt64Array(value) {
241
- return Object.prototype.toString.call(value) === '[object BigUint64Array]';
242
- }
243
241
  function isRegExp(d) {
244
242
  return Object.prototype.toString.call(d) === '[object RegExp]';
245
243
  }
@@ -317,7 +315,7 @@ class Binary {
317
315
  !ArrayBuffer.isView(buffer) &&
318
316
  !(buffer instanceof ArrayBuffer) &&
319
317
  !Array.isArray(buffer)) {
320
- throw new BSONTypeError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
318
+ throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
321
319
  }
322
320
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
323
321
  if (buffer == null) {
@@ -339,10 +337,10 @@ class Binary {
339
337
  }
340
338
  put(byteValue) {
341
339
  if (typeof byteValue === 'string' && byteValue.length !== 1) {
342
- throw new BSONTypeError('only accepts single character String');
340
+ throw new BSONError('only accepts single character String');
343
341
  }
344
342
  else if (typeof byteValue !== 'number' && byteValue.length !== 1)
345
- throw new BSONTypeError('only accepts single character Uint8Array or Array');
343
+ throw new BSONError('only accepts single character Uint8Array or Array');
346
344
  let decodedByte;
347
345
  if (typeof byteValue === 'string') {
348
346
  decodedByte = byteValue.charCodeAt(0);
@@ -354,7 +352,7 @@ class Binary {
354
352
  decodedByte = byteValue[0];
355
353
  }
356
354
  if (decodedByte < 0 || decodedByte > 255) {
357
- throw new BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
355
+ throw new BSONError('only accepts number in a valid unsigned byte range 0-255');
358
356
  }
359
357
  if (this.buffer.byteLength > this.position) {
360
358
  this.buffer[this.position++] = decodedByte;
@@ -458,7 +456,7 @@ class Binary {
458
456
  data = uuidHexStringToBuffer(doc.$uuid);
459
457
  }
460
458
  if (!data) {
461
- throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
459
+ throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
462
460
  }
463
461
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
464
462
  }
@@ -502,7 +500,7 @@ class UUID extends Binary {
502
500
  bytes = uuidHexStringToBuffer(input);
503
501
  }
504
502
  else {
505
- 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).');
503
+ 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).');
506
504
  }
507
505
  super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
508
506
  this.__id = hexStr;
@@ -786,7 +784,7 @@ class Long {
786
784
  }
787
785
  static fromString(str, unsigned, radix) {
788
786
  if (str.length === 0)
789
- throw Error('empty string');
787
+ throw new BSONError('empty string');
790
788
  if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
791
789
  return Long.ZERO;
792
790
  if (typeof unsigned === 'number') {
@@ -797,10 +795,10 @@ class Long {
797
795
  }
798
796
  radix = radix || 10;
799
797
  if (radix < 2 || 36 < radix)
800
- throw RangeError('radix');
798
+ throw new BSONError('radix');
801
799
  let p;
802
800
  if ((p = str.indexOf('-')) > 0)
803
- throw Error('interior hyphen');
801
+ throw new BSONError('interior hyphen');
804
802
  else if (p === 0) {
805
803
  return Long.fromString(str.substring(1), unsigned, radix).neg();
806
804
  }
@@ -896,7 +894,7 @@ class Long {
896
894
  if (!Long.isLong(divisor))
897
895
  divisor = Long.fromValue(divisor);
898
896
  if (divisor.isZero())
899
- throw Error('division by zero');
897
+ throw new BSONError('division by zero');
900
898
  if (wasm) {
901
899
  if (!this.unsigned &&
902
900
  this.high === -0x80000000 &&
@@ -1251,7 +1249,7 @@ class Long {
1251
1249
  toString(radix) {
1252
1250
  radix = radix || 10;
1253
1251
  if (radix < 2 || 36 < radix)
1254
- throw RangeError('radix');
1252
+ throw new BSONError('radix');
1255
1253
  if (this.isZero())
1256
1254
  return '0';
1257
1255
  if (this.isNegative()) {
@@ -1395,7 +1393,7 @@ function lessThan(left, right) {
1395
1393
  return false;
1396
1394
  }
1397
1395
  function invalidErr(string, message) {
1398
- throw new BSONTypeError(`"${string}" is not a valid Decimal128 string - ${message}`);
1396
+ throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`);
1399
1397
  }
1400
1398
  class Decimal128 {
1401
1399
  get _bsontype() {
@@ -1410,12 +1408,12 @@ class Decimal128 {
1410
1408
  }
1411
1409
  else if (isUint8Array(bytes)) {
1412
1410
  if (bytes.byteLength !== 16) {
1413
- throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
1411
+ throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1414
1412
  }
1415
1413
  this.bytes = bytes;
1416
1414
  }
1417
1415
  else {
1418
- throw new BSONTypeError('Decimal128 must take a Buffer or string');
1416
+ throw new BSONError('Decimal128 must take a Buffer or string');
1419
1417
  }
1420
1418
  }
1421
1419
  static fromString(representation) {
@@ -1439,13 +1437,13 @@ class Decimal128 {
1439
1437
  let biasedExponent = 0;
1440
1438
  let index = 0;
1441
1439
  if (representation.length >= 7000) {
1442
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1440
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1443
1441
  }
1444
1442
  const stringMatch = representation.match(PARSE_STRING_REGEXP);
1445
1443
  const infMatch = representation.match(PARSE_INF_REGEXP);
1446
1444
  const nanMatch = representation.match(PARSE_NAN_REGEXP);
1447
1445
  if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
1448
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1446
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1449
1447
  }
1450
1448
  if (stringMatch) {
1451
1449
  const unsignedNumber = stringMatch[2];
@@ -1497,7 +1495,7 @@ class Decimal128 {
1497
1495
  index = index + 1;
1498
1496
  }
1499
1497
  if (sawRadix && !nDigitsRead)
1500
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1498
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1501
1499
  if (representation[index] === 'e' || representation[index] === 'E') {
1502
1500
  const match = representation.substr(++index).match(EXPONENT_REGEX);
1503
1501
  if (!match || !match[2])
@@ -1853,19 +1851,11 @@ class Double {
1853
1851
  return this.value;
1854
1852
  }
1855
1853
  if (Object.is(Math.sign(this.value), -0)) {
1856
- return { $numberDouble: `-${this.value.toFixed(1)}` };
1857
- }
1858
- let $numberDouble;
1859
- if (Number.isInteger(this.value)) {
1860
- $numberDouble = this.value.toFixed(1);
1861
- if ($numberDouble.length >= 13) {
1862
- $numberDouble = this.value.toExponential(13).toUpperCase();
1863
- }
1854
+ return { $numberDouble: '-0.0' };
1864
1855
  }
1865
- else {
1866
- $numberDouble = this.value.toString();
1867
- }
1868
- return { $numberDouble };
1856
+ return {
1857
+ $numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
1858
+ };
1869
1859
  }
1870
1860
  static fromExtendedJSON(doc, options) {
1871
1861
  const doubleValue = parseFloat(doc.$numberDouble);
@@ -1974,7 +1964,7 @@ class ObjectId {
1974
1964
  let workingId;
1975
1965
  if (typeof inputId === 'object' && inputId && 'id' in inputId) {
1976
1966
  if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
1977
- throw new BSONTypeError('Argument passed in must have an id that is of type string or Buffer');
1967
+ throw new BSONError('Argument passed in must have an id that is of type string or Buffer');
1978
1968
  }
1979
1969
  if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
1980
1970
  workingId = ByteUtils.fromHex(inputId.toHexString());
@@ -1999,18 +1989,18 @@ class ObjectId {
1999
1989
  this[kId] = bytes;
2000
1990
  }
2001
1991
  else {
2002
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes');
1992
+ throw new BSONError('Argument passed in must be a string of 12 bytes');
2003
1993
  }
2004
1994
  }
2005
1995
  else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2006
1996
  this[kId] = ByteUtils.fromHex(workingId);
2007
1997
  }
2008
1998
  else {
2009
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1999
+ throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
2010
2000
  }
2011
2001
  }
2012
2002
  else {
2013
- throw new BSONTypeError('Argument passed in does not match the accepted types');
2003
+ throw new BSONError('Argument passed in does not match the accepted types');
2014
2004
  }
2015
2005
  if (ObjectId.cacheHexString) {
2016
2006
  this.__id = ByteUtils.toHex(this.id);
@@ -2112,7 +2102,7 @@ class ObjectId {
2112
2102
  }
2113
2103
  static createFromHexString(hexString) {
2114
2104
  if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2115
- throw new BSONTypeError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2105
+ throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2116
2106
  }
2117
2107
  return new ObjectId(ByteUtils.fromHex(hexString));
2118
2108
  }
@@ -2189,7 +2179,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2189
2179
  case 'boolean':
2190
2180
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 1);
2191
2181
  case 'object':
2192
- if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
2182
+ if (value != null &&
2183
+ typeof value._bsontype === 'string' &&
2184
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2185
+ throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
2186
+ }
2187
+ else if (value == null ||
2188
+ value['_bsontype'] === 'MinKey' ||
2189
+ value['_bsontype'] === 'MaxKey') {
2193
2190
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
2194
2191
  }
2195
2192
  else if (value['_bsontype'] === 'ObjectId') {
@@ -2347,7 +2344,7 @@ class BSONRegExp {
2347
2344
  if ('$regularExpression' in doc) {
2348
2345
  return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
2349
2346
  }
2350
- throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2347
+ throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2351
2348
  }
2352
2349
  [Symbol.for('nodejs.util.inspect.custom')]() {
2353
2350
  return this.inspect();
@@ -2539,9 +2536,16 @@ function deserializeObject(buffer, index, options, isArray = false) {
2539
2536
  const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
2540
2537
  const raw = options['raw'] == null ? false : options['raw'];
2541
2538
  const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
2542
- const promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
2543
- const promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
2544
- const promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
2539
+ const promoteBuffers = options.promoteBuffers ?? false;
2540
+ const promoteLongs = options.promoteLongs ?? true;
2541
+ const promoteValues = options.promoteValues ?? true;
2542
+ const useBigInt64 = options.useBigInt64 ?? false;
2543
+ if (useBigInt64 && !promoteValues) {
2544
+ throw new BSONError('Must either request bigint or Long for int64 deserialization');
2545
+ }
2546
+ if (useBigInt64 && !promoteLongs) {
2547
+ throw new BSONError('Must either request bigint or Long for int64 deserialization');
2548
+ }
2545
2549
  const validation = options.validation == null ? { utf8: true } : options.validation;
2546
2550
  let globalUTFValidation = true;
2547
2551
  let validationSetting;
@@ -2706,6 +2710,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2706
2710
  value = null;
2707
2711
  }
2708
2712
  else if (elementType === BSON_DATA_LONG) {
2713
+ const dataview = BSONDataView.fromUint8Array(buffer.subarray(index, index + 8));
2709
2714
  const lowBits = buffer[index++] |
2710
2715
  (buffer[index++] << 8) |
2711
2716
  (buffer[index++] << 16) |
@@ -2715,7 +2720,10 @@ function deserializeObject(buffer, index, options, isArray = false) {
2715
2720
  (buffer[index++] << 16) |
2716
2721
  (buffer[index++] << 24);
2717
2722
  const long = new Long(lowBits, highBits);
2718
- if (promoteLongs && promoteValues === true) {
2723
+ if (useBigInt64) {
2724
+ value = dataview.getBigInt64(0, true);
2725
+ }
2726
+ else if (promoteLongs && promoteValues === true) {
2719
2727
  value =
2720
2728
  long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
2721
2729
  ? long.toNumber()
@@ -3037,6 +3045,16 @@ function serializeNumber(buffer, key, value, index) {
3037
3045
  index += bytes.byteLength;
3038
3046
  return index;
3039
3047
  }
3048
+ function serializeBigInt(buffer, key, value, index) {
3049
+ buffer[index++] = BSON_DATA_LONG;
3050
+ const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3051
+ index += numberOfWrittenBytes;
3052
+ buffer[index++] = 0;
3053
+ NUMBER_SPACE.setBigInt64(0, value, true);
3054
+ buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
3055
+ index += EIGHT_BYTE_VIEW_ON_NUMBER.byteLength;
3056
+ return index;
3057
+ }
3040
3058
  function serializeNull(buffer, key, _, index) {
3041
3059
  buffer[index++] = BSON_DATA_NULL;
3042
3060
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
@@ -3076,7 +3094,7 @@ function serializeRegExp(buffer, key, value, index) {
3076
3094
  index = index + numberOfWrittenBytes;
3077
3095
  buffer[index++] = 0;
3078
3096
  if (value.source && value.source.match(regexp) != null) {
3079
- throw Error('value ' + value.source + ' must not contain null bytes');
3097
+ throw new BSONError('value ' + value.source + ' must not contain null bytes');
3080
3098
  }
3081
3099
  index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index);
3082
3100
  buffer[index++] = 0x00;
@@ -3095,7 +3113,7 @@ function serializeBSONRegExp(buffer, key, value, index) {
3095
3113
  index = index + numberOfWrittenBytes;
3096
3114
  buffer[index++] = 0;
3097
3115
  if (value.pattern.match(regexp) != null) {
3098
- throw Error('pattern ' + value.pattern + ' must not contain null bytes');
3116
+ throw new BSONError('pattern ' + value.pattern + ' must not contain null bytes');
3099
3117
  }
3100
3118
  index = index + ByteUtils.encodeUTF8Into(buffer, value.pattern, index);
3101
3119
  buffer[index++] = 0x00;
@@ -3128,7 +3146,7 @@ function serializeObjectId(buffer, key, value, index) {
3128
3146
  buffer.set(value.id.subarray(0, 12), index);
3129
3147
  }
3130
3148
  else {
3131
- throw new BSONTypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3149
+ throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3132
3150
  }
3133
3151
  return index + 12;
3134
3152
  }
@@ -3368,7 +3386,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3368
3386
  index = serializeNumber(buffer, key, value, index);
3369
3387
  }
3370
3388
  else if (typeof value === 'bigint') {
3371
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3389
+ index = serializeBigInt(buffer, key, value, index);
3372
3390
  }
3373
3391
  else if (typeof value === 'boolean') {
3374
3392
  index = serializeBoolean(buffer, key, value, index);
@@ -3391,7 +3409,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3391
3409
  else if (typeof value === 'object' && value._bsontype == null) {
3392
3410
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3393
3411
  }
3394
- else if (typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] == null) {
3412
+ else if (typeof value === 'object' &&
3413
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3395
3414
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3396
3415
  }
3397
3416
  else if (value._bsontype === 'ObjectId') {
@@ -3431,7 +3450,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3431
3450
  index = serializeMinMax(buffer, key, value, index);
3432
3451
  }
3433
3452
  else if (typeof value._bsontype !== 'undefined') {
3434
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3453
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3435
3454
  }
3436
3455
  }
3437
3456
  }
@@ -3444,18 +3463,21 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3444
3463
  if (done)
3445
3464
  continue;
3446
3465
  const key = entry.value[0];
3447
- const value = entry.value[1];
3466
+ let value = entry.value[1];
3467
+ if (typeof value?.toBSON === 'function') {
3468
+ value = value.toBSON();
3469
+ }
3448
3470
  const type = typeof value;
3449
3471
  if (typeof key === 'string' && !ignoreKeys.has(key)) {
3450
3472
  if (key.match(regexp) != null) {
3451
- throw Error('key ' + key + ' must not contain null bytes');
3473
+ throw new BSONError('key ' + key + ' must not contain null bytes');
3452
3474
  }
3453
3475
  if (checkKeys) {
3454
3476
  if ('$' === key[0]) {
3455
- throw Error('key ' + key + " must not start with '$'");
3477
+ throw new BSONError('key ' + key + " must not start with '$'");
3456
3478
  }
3457
3479
  else if (~key.indexOf('.')) {
3458
- throw Error('key ' + key + " must not contain '.'");
3480
+ throw new BSONError('key ' + key + " must not contain '.'");
3459
3481
  }
3460
3482
  }
3461
3483
  }
@@ -3465,8 +3487,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3465
3487
  else if (type === 'number') {
3466
3488
  index = serializeNumber(buffer, key, value, index);
3467
3489
  }
3468
- else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
3469
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3490
+ else if (type === 'bigint') {
3491
+ index = serializeBigInt(buffer, key, value, index);
3470
3492
  }
3471
3493
  else if (type === 'boolean') {
3472
3494
  index = serializeBoolean(buffer, key, value, index);
@@ -3486,7 +3508,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3486
3508
  else if (type === 'object' && value._bsontype == null) {
3487
3509
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3488
3510
  }
3489
- else if (typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] == null) {
3511
+ else if (typeof value === 'object' &&
3512
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3490
3513
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3491
3514
  }
3492
3515
  else if (value._bsontype === 'ObjectId') {
@@ -3526,7 +3549,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3526
3549
  index = serializeMinMax(buffer, key, value, index);
3527
3550
  }
3528
3551
  else if (typeof value._bsontype !== 'undefined') {
3529
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3552
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3530
3553
  }
3531
3554
  }
3532
3555
  }
@@ -3534,7 +3557,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3534
3557
  if (typeof object?.toBSON === 'function') {
3535
3558
  object = object.toBSON();
3536
3559
  if (object != null && typeof object !== 'object') {
3537
- throw new BSONTypeError('toBSON function did not return an object');
3560
+ throw new BSONError('toBSON function did not return an object');
3538
3561
  }
3539
3562
  }
3540
3563
  for (const key of Object.keys(object)) {
@@ -3545,14 +3568,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3545
3568
  const type = typeof value;
3546
3569
  if (typeof key === 'string' && !ignoreKeys.has(key)) {
3547
3570
  if (key.match(regexp) != null) {
3548
- throw Error('key ' + key + ' must not contain null bytes');
3571
+ throw new BSONError('key ' + key + ' must not contain null bytes');
3549
3572
  }
3550
3573
  if (checkKeys) {
3551
3574
  if ('$' === key[0]) {
3552
- throw Error('key ' + key + " must not start with '$'");
3575
+ throw new BSONError('key ' + key + " must not start with '$'");
3553
3576
  }
3554
3577
  else if (~key.indexOf('.')) {
3555
- throw Error('key ' + key + " must not contain '.'");
3578
+ throw new BSONError('key ' + key + " must not contain '.'");
3556
3579
  }
3557
3580
  }
3558
3581
  }
@@ -3563,7 +3586,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3563
3586
  index = serializeNumber(buffer, key, value, index);
3564
3587
  }
3565
3588
  else if (type === 'bigint') {
3566
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3589
+ index = serializeBigInt(buffer, key, value, index);
3567
3590
  }
3568
3591
  else if (type === 'boolean') {
3569
3592
  index = serializeBoolean(buffer, key, value, index);
@@ -3587,7 +3610,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3587
3610
  else if (type === 'object' && value._bsontype == null) {
3588
3611
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3589
3612
  }
3590
- else if (typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] == null) {
3613
+ else if (typeof value === 'object' &&
3614
+ value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3591
3615
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3592
3616
  }
3593
3617
  else if (value._bsontype === 'ObjectId') {
@@ -3627,7 +3651,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3627
3651
  index = serializeMinMax(buffer, key, value, index);
3628
3652
  }
3629
3653
  else if (typeof value._bsontype !== 'undefined') {
3630
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3654
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3631
3655
  }
3632
3656
  }
3633
3657
  }
@@ -3761,7 +3785,7 @@ function serializeValue(value, options) {
3761
3785
  const current = props[props.length - 1];
3762
3786
  const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
3763
3787
  const dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
3764
- throw new BSONTypeError('Converting circular structure to EJSON:\n' +
3788
+ throw new BSONError('Converting circular structure to EJSON:\n' +
3765
3789
  ` ${leadingPart}${alreadySeen}${circularPart}${current}\n` +
3766
3790
  ` ${leadingSpace}\\${dashes}/`);
3767
3791
  }
@@ -3854,7 +3878,7 @@ function serializeDocument(doc, options) {
3854
3878
  else if (doc != null &&
3855
3879
  typeof doc === 'object' &&
3856
3880
  typeof doc._bsontype === 'string' &&
3857
- doc[Symbol.for('@@mdb.bson.version')] == null) {
3881
+ doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3858
3882
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3859
3883
  }
3860
3884
  else if (isBSONType(doc)) {
@@ -3862,7 +3886,7 @@ function serializeDocument(doc, options) {
3862
3886
  if (typeof outDoc.toExtendedJSON !== 'function') {
3863
3887
  const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
3864
3888
  if (!mapper) {
3865
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
3889
+ throw new BSONError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
3866
3890
  }
3867
3891
  outDoc = mapper(outDoc);
3868
3892
  }
@@ -3994,10 +4018,9 @@ var bson = /*#__PURE__*/Object.freeze({
3994
4018
  calculateObjectSize: calculateObjectSize,
3995
4019
  deserializeStream: deserializeStream,
3996
4020
  BSONError: BSONError,
3997
- BSONTypeError: BSONTypeError,
3998
4021
  BSONType: BSONType,
3999
4022
  EJSON: EJSON
4000
4023
  });
4001
4024
 
4002
- export { bson as BSON, BSONError, BSONRegExp, BSONSymbol, BSONType, BSONTypeError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4025
+ export { bson as BSON, BSONError, BSONRegExp, BSONSymbol, BSONType, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4003
4026
  //# sourceMappingURL=bson.mjs.map