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