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