bson 5.0.0-alpha.0 → 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
  }
@@ -252,6 +250,7 @@ function isDate(d) {
252
250
  return Object.prototype.toString.call(d) === '[object Date]';
253
251
  }
254
252
 
253
+ const BSON_MAJOR_VERSION = 5;
255
254
  const BSON_INT32_MAX = 0x7fffffff;
256
255
  const BSON_INT32_MIN = -0x80000000;
257
256
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -310,7 +309,7 @@ class Binary {
310
309
  return 'Binary';
311
310
  }
312
311
  get [Symbol.for('@@mdb.bson.version')]() {
313
- return 5;
312
+ return BSON_MAJOR_VERSION;
314
313
  }
315
314
  constructor(buffer, subType) {
316
315
  if (!(buffer == null) &&
@@ -318,7 +317,7 @@ class Binary {
318
317
  !ArrayBuffer.isView(buffer) &&
319
318
  !(buffer instanceof ArrayBuffer) &&
320
319
  !Array.isArray(buffer)) {
321
- 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>');
322
321
  }
323
322
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
324
323
  if (buffer == null) {
@@ -340,10 +339,10 @@ class Binary {
340
339
  }
341
340
  put(byteValue) {
342
341
  if (typeof byteValue === 'string' && byteValue.length !== 1) {
343
- throw new BSONTypeError('only accepts single character String');
342
+ throw new BSONError('only accepts single character String');
344
343
  }
345
344
  else if (typeof byteValue !== 'number' && byteValue.length !== 1)
346
- throw new BSONTypeError('only accepts single character Uint8Array or Array');
345
+ throw new BSONError('only accepts single character Uint8Array or Array');
347
346
  let decodedByte;
348
347
  if (typeof byteValue === 'string') {
349
348
  decodedByte = byteValue.charCodeAt(0);
@@ -355,7 +354,7 @@ class Binary {
355
354
  decodedByte = byteValue[0];
356
355
  }
357
356
  if (decodedByte < 0 || decodedByte > 255) {
358
- 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');
359
358
  }
360
359
  if (this.buffer.byteLength > this.position) {
361
360
  this.buffer[this.position++] = decodedByte;
@@ -459,7 +458,7 @@ class Binary {
459
458
  data = uuidHexStringToBuffer(doc.$uuid);
460
459
  }
461
460
  if (!data) {
462
- throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
461
+ throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
463
462
  }
464
463
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
465
464
  }
@@ -484,7 +483,7 @@ Binary.SUBTYPE_USER_DEFINED = 128;
484
483
  const UUID_BYTE_LENGTH = 16;
485
484
  class UUID extends Binary {
486
485
  get [Symbol.for('@@mdb.bson.version')]() {
487
- return 5;
486
+ return BSON_MAJOR_VERSION;
488
487
  }
489
488
  constructor(input) {
490
489
  let bytes;
@@ -503,7 +502,7 @@ class UUID extends Binary {
503
502
  bytes = uuidHexStringToBuffer(input);
504
503
  }
505
504
  else {
506
- 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).');
507
506
  }
508
507
  super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
509
508
  this.__id = hexStr;
@@ -595,7 +594,7 @@ class Code {
595
594
  return 'Code';
596
595
  }
597
596
  get [Symbol.for('@@mdb.bson.version')]() {
598
- return 5;
597
+ return BSON_MAJOR_VERSION;
599
598
  }
600
599
  constructor(code, scope) {
601
600
  this.code = code.toString();
@@ -639,7 +638,7 @@ class DBRef {
639
638
  return 'DBRef';
640
639
  }
641
640
  get [Symbol.for('@@mdb.bson.version')]() {
642
- return 5;
641
+ return BSON_MAJOR_VERSION;
643
642
  }
644
643
  constructor(collection, oid, db, fields) {
645
644
  const parts = collection.split('.');
@@ -715,7 +714,7 @@ class Long {
715
714
  return 'Long';
716
715
  }
717
716
  get [Symbol.for('@@mdb.bson.version')]() {
718
- return 5;
717
+ return BSON_MAJOR_VERSION;
719
718
  }
720
719
  get __isLong__() {
721
720
  return true;
@@ -787,7 +786,7 @@ class Long {
787
786
  }
788
787
  static fromString(str, unsigned, radix) {
789
788
  if (str.length === 0)
790
- throw Error('empty string');
789
+ throw new BSONError('empty string');
791
790
  if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
792
791
  return Long.ZERO;
793
792
  if (typeof unsigned === 'number') {
@@ -798,10 +797,10 @@ class Long {
798
797
  }
799
798
  radix = radix || 10;
800
799
  if (radix < 2 || 36 < radix)
801
- throw RangeError('radix');
800
+ throw new BSONError('radix');
802
801
  let p;
803
802
  if ((p = str.indexOf('-')) > 0)
804
- throw Error('interior hyphen');
803
+ throw new BSONError('interior hyphen');
805
804
  else if (p === 0) {
806
805
  return Long.fromString(str.substring(1), unsigned, radix).neg();
807
806
  }
@@ -897,7 +896,7 @@ class Long {
897
896
  if (!Long.isLong(divisor))
898
897
  divisor = Long.fromValue(divisor);
899
898
  if (divisor.isZero())
900
- throw Error('division by zero');
899
+ throw new BSONError('division by zero');
901
900
  if (wasm) {
902
901
  if (!this.unsigned &&
903
902
  this.high === -0x80000000 &&
@@ -1252,7 +1251,7 @@ class Long {
1252
1251
  toString(radix) {
1253
1252
  radix = radix || 10;
1254
1253
  if (radix < 2 || 36 < radix)
1255
- throw RangeError('radix');
1254
+ throw new BSONError('radix');
1256
1255
  if (this.isZero())
1257
1256
  return '0';
1258
1257
  if (this.isNegative()) {
@@ -1396,14 +1395,14 @@ function lessThan(left, right) {
1396
1395
  return false;
1397
1396
  }
1398
1397
  function invalidErr(string, message) {
1399
- throw new BSONTypeError(`"${string}" is not a valid Decimal128 string - ${message}`);
1398
+ throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`);
1400
1399
  }
1401
1400
  class Decimal128 {
1402
1401
  get _bsontype() {
1403
1402
  return 'Decimal128';
1404
1403
  }
1405
1404
  get [Symbol.for('@@mdb.bson.version')]() {
1406
- return 5;
1405
+ return BSON_MAJOR_VERSION;
1407
1406
  }
1408
1407
  constructor(bytes) {
1409
1408
  if (typeof bytes === 'string') {
@@ -1411,12 +1410,12 @@ class Decimal128 {
1411
1410
  }
1412
1411
  else if (isUint8Array(bytes)) {
1413
1412
  if (bytes.byteLength !== 16) {
1414
- throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
1413
+ throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1415
1414
  }
1416
1415
  this.bytes = bytes;
1417
1416
  }
1418
1417
  else {
1419
- throw new BSONTypeError('Decimal128 must take a Buffer or string');
1418
+ throw new BSONError('Decimal128 must take a Buffer or string');
1420
1419
  }
1421
1420
  }
1422
1421
  static fromString(representation) {
@@ -1440,13 +1439,13 @@ class Decimal128 {
1440
1439
  let biasedExponent = 0;
1441
1440
  let index = 0;
1442
1441
  if (representation.length >= 7000) {
1443
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1442
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1444
1443
  }
1445
1444
  const stringMatch = representation.match(PARSE_STRING_REGEXP);
1446
1445
  const infMatch = representation.match(PARSE_INF_REGEXP);
1447
1446
  const nanMatch = representation.match(PARSE_NAN_REGEXP);
1448
1447
  if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
1449
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1448
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1450
1449
  }
1451
1450
  if (stringMatch) {
1452
1451
  const unsignedNumber = stringMatch[2];
@@ -1498,7 +1497,7 @@ class Decimal128 {
1498
1497
  index = index + 1;
1499
1498
  }
1500
1499
  if (sawRadix && !nDigitsRead)
1501
- throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
1500
+ throw new BSONError('' + representation + ' not a valid Decimal128 string');
1502
1501
  if (representation[index] === 'e' || representation[index] === 'E') {
1503
1502
  const match = representation.substr(++index).match(EXPONENT_REGEX);
1504
1503
  if (!match || !match[2])
@@ -1832,7 +1831,7 @@ class Double {
1832
1831
  return 'Double';
1833
1832
  }
1834
1833
  get [Symbol.for('@@mdb.bson.version')]() {
1835
- return 5;
1834
+ return BSON_MAJOR_VERSION;
1836
1835
  }
1837
1836
  constructor(value) {
1838
1837
  if (value instanceof Number) {
@@ -1854,19 +1853,11 @@ class Double {
1854
1853
  return this.value;
1855
1854
  }
1856
1855
  if (Object.is(Math.sign(this.value), -0)) {
1857
- return { $numberDouble: `-${this.value.toFixed(1)}` };
1858
- }
1859
- let $numberDouble;
1860
- if (Number.isInteger(this.value)) {
1861
- $numberDouble = this.value.toFixed(1);
1862
- if ($numberDouble.length >= 13) {
1863
- $numberDouble = this.value.toExponential(13).toUpperCase();
1864
- }
1856
+ return { $numberDouble: '-0.0' };
1865
1857
  }
1866
- else {
1867
- $numberDouble = this.value.toString();
1868
- }
1869
- return { $numberDouble };
1858
+ return {
1859
+ $numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
1860
+ };
1870
1861
  }
1871
1862
  static fromExtendedJSON(doc, options) {
1872
1863
  const doubleValue = parseFloat(doc.$numberDouble);
@@ -1886,7 +1877,7 @@ class Int32 {
1886
1877
  return 'Int32';
1887
1878
  }
1888
1879
  get [Symbol.for('@@mdb.bson.version')]() {
1889
- return 5;
1880
+ return BSON_MAJOR_VERSION;
1890
1881
  }
1891
1882
  constructor(value) {
1892
1883
  if (value instanceof Number) {
@@ -1924,7 +1915,7 @@ class MaxKey {
1924
1915
  return 'MaxKey';
1925
1916
  }
1926
1917
  get [Symbol.for('@@mdb.bson.version')]() {
1927
- return 5;
1918
+ return BSON_MAJOR_VERSION;
1928
1919
  }
1929
1920
  toExtendedJSON() {
1930
1921
  return { $maxKey: 1 };
@@ -1945,7 +1936,7 @@ class MinKey {
1945
1936
  return 'MinKey';
1946
1937
  }
1947
1938
  get [Symbol.for('@@mdb.bson.version')]() {
1948
- return 5;
1939
+ return BSON_MAJOR_VERSION;
1949
1940
  }
1950
1941
  toExtendedJSON() {
1951
1942
  return { $minKey: 1 };
@@ -1969,13 +1960,13 @@ class ObjectId {
1969
1960
  return 'ObjectId';
1970
1961
  }
1971
1962
  get [Symbol.for('@@mdb.bson.version')]() {
1972
- return 5;
1963
+ return BSON_MAJOR_VERSION;
1973
1964
  }
1974
1965
  constructor(inputId) {
1975
1966
  let workingId;
1976
1967
  if (typeof inputId === 'object' && inputId && 'id' in inputId) {
1977
1968
  if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
1978
- 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');
1979
1970
  }
1980
1971
  if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
1981
1972
  workingId = ByteUtils.fromHex(inputId.toHexString());
@@ -2000,18 +1991,18 @@ class ObjectId {
2000
1991
  this[kId] = bytes;
2001
1992
  }
2002
1993
  else {
2003
- 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');
2004
1995
  }
2005
1996
  }
2006
1997
  else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2007
1998
  this[kId] = ByteUtils.fromHex(workingId);
2008
1999
  }
2009
2000
  else {
2010
- 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');
2011
2002
  }
2012
2003
  }
2013
2004
  else {
2014
- 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');
2015
2006
  }
2016
2007
  if (ObjectId.cacheHexString) {
2017
2008
  this.__id = ByteUtils.toHex(this.id);
@@ -2113,7 +2104,7 @@ class ObjectId {
2113
2104
  }
2114
2105
  static createFromHexString(hexString) {
2115
2106
  if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2116
- 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');
2117
2108
  }
2118
2109
  return new ObjectId(ByteUtils.fromHex(hexString));
2119
2110
  }
@@ -2190,7 +2181,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2190
2181
  case 'boolean':
2191
2182
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 1);
2192
2183
  case 'object':
2193
- 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') {
2194
2192
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
2195
2193
  }
2196
2194
  else if (value['_bsontype'] === 'ObjectId') {
@@ -2302,7 +2300,7 @@ class BSONRegExp {
2302
2300
  return 'BSONRegExp';
2303
2301
  }
2304
2302
  get [Symbol.for('@@mdb.bson.version')]() {
2305
- return 5;
2303
+ return BSON_MAJOR_VERSION;
2306
2304
  }
2307
2305
  constructor(pattern, options) {
2308
2306
  this.pattern = pattern;
@@ -2348,7 +2346,7 @@ class BSONRegExp {
2348
2346
  if ('$regularExpression' in doc) {
2349
2347
  return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
2350
2348
  }
2351
- throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2349
+ throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2352
2350
  }
2353
2351
  [Symbol.for('nodejs.util.inspect.custom')]() {
2354
2352
  return this.inspect();
@@ -2363,7 +2361,7 @@ class BSONSymbol {
2363
2361
  return 'BSONSymbol';
2364
2362
  }
2365
2363
  get [Symbol.for('@@mdb.bson.version')]() {
2366
- return 5;
2364
+ return BSON_MAJOR_VERSION;
2367
2365
  }
2368
2366
  constructor(value) {
2369
2367
  this.value = value;
@@ -2397,7 +2395,7 @@ class Timestamp extends LongWithoutOverridesClass {
2397
2395
  return 'Timestamp';
2398
2396
  }
2399
2397
  get [Symbol.for('@@mdb.bson.version')]() {
2400
- return 5;
2398
+ return BSON_MAJOR_VERSION;
2401
2399
  }
2402
2400
  constructor(low) {
2403
2401
  if (low == null) {
@@ -2540,9 +2538,16 @@ function deserializeObject(buffer, index, options, isArray = false) {
2540
2538
  const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
2541
2539
  const raw = options['raw'] == null ? false : options['raw'];
2542
2540
  const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
2543
- const promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
2544
- const promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
2545
- 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
+ }
2546
2551
  const validation = options.validation == null ? { utf8: true } : options.validation;
2547
2552
  let globalUTFValidation = true;
2548
2553
  let validationSetting;
@@ -2707,6 +2712,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2707
2712
  value = null;
2708
2713
  }
2709
2714
  else if (elementType === BSON_DATA_LONG) {
2715
+ const dataview = BSONDataView.fromUint8Array(buffer.subarray(index, index + 8));
2710
2716
  const lowBits = buffer[index++] |
2711
2717
  (buffer[index++] << 8) |
2712
2718
  (buffer[index++] << 16) |
@@ -2716,7 +2722,10 @@ function deserializeObject(buffer, index, options, isArray = false) {
2716
2722
  (buffer[index++] << 16) |
2717
2723
  (buffer[index++] << 24);
2718
2724
  const long = new Long(lowBits, highBits);
2719
- if (promoteLongs && promoteValues === true) {
2725
+ if (useBigInt64) {
2726
+ value = dataview.getBigInt64(0, true);
2727
+ }
2728
+ else if (promoteLongs && promoteValues === true) {
2720
2729
  value =
2721
2730
  long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
2722
2731
  ? long.toNumber()
@@ -3038,6 +3047,16 @@ function serializeNumber(buffer, key, value, index) {
3038
3047
  index += bytes.byteLength;
3039
3048
  return index;
3040
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
+ }
3041
3060
  function serializeNull(buffer, key, _, index) {
3042
3061
  buffer[index++] = BSON_DATA_NULL;
3043
3062
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
@@ -3077,7 +3096,7 @@ function serializeRegExp(buffer, key, value, index) {
3077
3096
  index = index + numberOfWrittenBytes;
3078
3097
  buffer[index++] = 0;
3079
3098
  if (value.source && value.source.match(regexp) != null) {
3080
- throw Error('value ' + value.source + ' must not contain null bytes');
3099
+ throw new BSONError('value ' + value.source + ' must not contain null bytes');
3081
3100
  }
3082
3101
  index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index);
3083
3102
  buffer[index++] = 0x00;
@@ -3096,7 +3115,7 @@ function serializeBSONRegExp(buffer, key, value, index) {
3096
3115
  index = index + numberOfWrittenBytes;
3097
3116
  buffer[index++] = 0;
3098
3117
  if (value.pattern.match(regexp) != null) {
3099
- throw Error('pattern ' + value.pattern + ' must not contain null bytes');
3118
+ throw new BSONError('pattern ' + value.pattern + ' must not contain null bytes');
3100
3119
  }
3101
3120
  index = index + ByteUtils.encodeUTF8Into(buffer, value.pattern, index);
3102
3121
  buffer[index++] = 0x00;
@@ -3129,7 +3148,7 @@ function serializeObjectId(buffer, key, value, index) {
3129
3148
  buffer.set(value.id.subarray(0, 12), index);
3130
3149
  }
3131
3150
  else {
3132
- 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');
3133
3152
  }
3134
3153
  return index + 12;
3135
3154
  }
@@ -3369,7 +3388,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3369
3388
  index = serializeNumber(buffer, key, value, index);
3370
3389
  }
3371
3390
  else if (typeof value === 'bigint') {
3372
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3391
+ index = serializeBigInt(buffer, key, value, index);
3373
3392
  }
3374
3393
  else if (typeof value === 'boolean') {
3375
3394
  index = serializeBoolean(buffer, key, value, index);
@@ -3392,7 +3411,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3392
3411
  else if (typeof value === 'object' && value._bsontype == null) {
3393
3412
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3394
3413
  }
3395
- 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) {
3396
3416
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3397
3417
  }
3398
3418
  else if (value._bsontype === 'ObjectId') {
@@ -3416,7 +3436,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3416
3436
  else if (value._bsontype === 'Binary') {
3417
3437
  index = serializeBinary(buffer, key, value, index);
3418
3438
  }
3419
- else if (value._bsontype === 'Symbol') {
3439
+ else if (value._bsontype === 'BSONSymbol') {
3420
3440
  index = serializeSymbol(buffer, key, value, index);
3421
3441
  }
3422
3442
  else if (value._bsontype === 'DBRef') {
@@ -3432,7 +3452,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3432
3452
  index = serializeMinMax(buffer, key, value, index);
3433
3453
  }
3434
3454
  else if (typeof value._bsontype !== 'undefined') {
3435
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3455
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3436
3456
  }
3437
3457
  }
3438
3458
  }
@@ -3445,18 +3465,21 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3445
3465
  if (done)
3446
3466
  continue;
3447
3467
  const key = entry.value[0];
3448
- const value = entry.value[1];
3468
+ let value = entry.value[1];
3469
+ if (typeof value?.toBSON === 'function') {
3470
+ value = value.toBSON();
3471
+ }
3449
3472
  const type = typeof value;
3450
3473
  if (typeof key === 'string' && !ignoreKeys.has(key)) {
3451
3474
  if (key.match(regexp) != null) {
3452
- throw Error('key ' + key + ' must not contain null bytes');
3475
+ throw new BSONError('key ' + key + ' must not contain null bytes');
3453
3476
  }
3454
3477
  if (checkKeys) {
3455
3478
  if ('$' === key[0]) {
3456
- throw Error('key ' + key + " must not start with '$'");
3479
+ throw new BSONError('key ' + key + " must not start with '$'");
3457
3480
  }
3458
3481
  else if (~key.indexOf('.')) {
3459
- throw Error('key ' + key + " must not contain '.'");
3482
+ throw new BSONError('key ' + key + " must not contain '.'");
3460
3483
  }
3461
3484
  }
3462
3485
  }
@@ -3466,8 +3489,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3466
3489
  else if (type === 'number') {
3467
3490
  index = serializeNumber(buffer, key, value, index);
3468
3491
  }
3469
- else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
3470
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3492
+ else if (type === 'bigint') {
3493
+ index = serializeBigInt(buffer, key, value, index);
3471
3494
  }
3472
3495
  else if (type === 'boolean') {
3473
3496
  index = serializeBoolean(buffer, key, value, index);
@@ -3487,7 +3510,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3487
3510
  else if (type === 'object' && value._bsontype == null) {
3488
3511
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3489
3512
  }
3490
- 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) {
3491
3515
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3492
3516
  }
3493
3517
  else if (value._bsontype === 'ObjectId') {
@@ -3511,7 +3535,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3511
3535
  else if (value._bsontype === 'Binary') {
3512
3536
  index = serializeBinary(buffer, key, value, index);
3513
3537
  }
3514
- else if (value._bsontype === 'Symbol') {
3538
+ else if (value._bsontype === 'BSONSymbol') {
3515
3539
  index = serializeSymbol(buffer, key, value, index);
3516
3540
  }
3517
3541
  else if (value._bsontype === 'DBRef') {
@@ -3527,7 +3551,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3527
3551
  index = serializeMinMax(buffer, key, value, index);
3528
3552
  }
3529
3553
  else if (typeof value._bsontype !== 'undefined') {
3530
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3554
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3531
3555
  }
3532
3556
  }
3533
3557
  }
@@ -3535,7 +3559,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3535
3559
  if (typeof object?.toBSON === 'function') {
3536
3560
  object = object.toBSON();
3537
3561
  if (object != null && typeof object !== 'object') {
3538
- throw new BSONTypeError('toBSON function did not return an object');
3562
+ throw new BSONError('toBSON function did not return an object');
3539
3563
  }
3540
3564
  }
3541
3565
  for (const key of Object.keys(object)) {
@@ -3546,14 +3570,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3546
3570
  const type = typeof value;
3547
3571
  if (typeof key === 'string' && !ignoreKeys.has(key)) {
3548
3572
  if (key.match(regexp) != null) {
3549
- throw Error('key ' + key + ' must not contain null bytes');
3573
+ throw new BSONError('key ' + key + ' must not contain null bytes');
3550
3574
  }
3551
3575
  if (checkKeys) {
3552
3576
  if ('$' === key[0]) {
3553
- throw Error('key ' + key + " must not start with '$'");
3577
+ throw new BSONError('key ' + key + " must not start with '$'");
3554
3578
  }
3555
3579
  else if (~key.indexOf('.')) {
3556
- throw Error('key ' + key + " must not contain '.'");
3580
+ throw new BSONError('key ' + key + " must not contain '.'");
3557
3581
  }
3558
3582
  }
3559
3583
  }
@@ -3564,7 +3588,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3564
3588
  index = serializeNumber(buffer, key, value, index);
3565
3589
  }
3566
3590
  else if (type === 'bigint') {
3567
- throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
3591
+ index = serializeBigInt(buffer, key, value, index);
3568
3592
  }
3569
3593
  else if (type === 'boolean') {
3570
3594
  index = serializeBoolean(buffer, key, value, index);
@@ -3588,7 +3612,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3588
3612
  else if (type === 'object' && value._bsontype == null) {
3589
3613
  index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3590
3614
  }
3591
- 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) {
3592
3617
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3593
3618
  }
3594
3619
  else if (value._bsontype === 'ObjectId') {
@@ -3612,7 +3637,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3612
3637
  else if (value._bsontype === 'Binary') {
3613
3638
  index = serializeBinary(buffer, key, value, index);
3614
3639
  }
3615
- else if (value._bsontype === 'Symbol') {
3640
+ else if (value._bsontype === 'BSONSymbol') {
3616
3641
  index = serializeSymbol(buffer, key, value, index);
3617
3642
  }
3618
3643
  else if (value._bsontype === 'DBRef') {
@@ -3628,7 +3653,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3628
3653
  index = serializeMinMax(buffer, key, value, index);
3629
3654
  }
3630
3655
  else if (typeof value._bsontype !== 'undefined') {
3631
- throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3656
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3632
3657
  }
3633
3658
  }
3634
3659
  }
@@ -3762,7 +3787,7 @@ function serializeValue(value, options) {
3762
3787
  const current = props[props.length - 1];
3763
3788
  const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
3764
3789
  const dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
3765
- throw new BSONTypeError('Converting circular structure to EJSON:\n' +
3790
+ throw new BSONError('Converting circular structure to EJSON:\n' +
3766
3791
  ` ${leadingPart}${alreadySeen}${circularPart}${current}\n` +
3767
3792
  ` ${leadingSpace}\\${dashes}/`);
3768
3793
  }
@@ -3855,7 +3880,7 @@ function serializeDocument(doc, options) {
3855
3880
  else if (doc != null &&
3856
3881
  typeof doc === 'object' &&
3857
3882
  typeof doc._bsontype === 'string' &&
3858
- doc[Symbol.for('@@mdb.bson.version')] == null) {
3883
+ doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3859
3884
  throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3860
3885
  }
3861
3886
  else if (isBSONType(doc)) {
@@ -3863,7 +3888,7 @@ function serializeDocument(doc, options) {
3863
3888
  if (typeof outDoc.toExtendedJSON !== 'function') {
3864
3889
  const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
3865
3890
  if (!mapper) {
3866
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
3891
+ throw new BSONError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
3867
3892
  }
3868
3893
  outDoc = mapper(outDoc);
3869
3894
  }
@@ -3995,7 +4020,6 @@ var bson = /*#__PURE__*/Object.freeze({
3995
4020
  calculateObjectSize: calculateObjectSize,
3996
4021
  deserializeStream: deserializeStream,
3997
4022
  BSONError: BSONError,
3998
- BSONTypeError: BSONTypeError,
3999
4023
  BSONType: BSONType,
4000
4024
  EJSON: EJSON
4001
4025
  });
@@ -4005,7 +4029,6 @@ exports.BSONError = BSONError;
4005
4029
  exports.BSONRegExp = BSONRegExp;
4006
4030
  exports.BSONSymbol = BSONSymbol;
4007
4031
  exports.BSONType = BSONType;
4008
- exports.BSONTypeError = BSONTypeError;
4009
4032
  exports.Binary = Binary;
4010
4033
  exports.Code = Code;
4011
4034
  exports.DBRef = DBRef;