bson 5.0.0-alpha.2 → 5.0.0

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.
@@ -1,6 +1,60 @@
1
1
  var BSON = (function (exports) {
2
2
  'use strict';
3
3
 
4
+ const BSON_MAJOR_VERSION = 5;
5
+ const BSON_INT32_MAX = 0x7fffffff;
6
+ const BSON_INT32_MIN = -0x80000000;
7
+ const BSON_INT64_MAX = Math.pow(2, 63) - 1;
8
+ const BSON_INT64_MIN = -Math.pow(2, 63);
9
+ const JS_INT_MAX = Math.pow(2, 53);
10
+ const JS_INT_MIN = -Math.pow(2, 53);
11
+ const BSON_DATA_NUMBER = 1;
12
+ const BSON_DATA_STRING = 2;
13
+ const BSON_DATA_OBJECT = 3;
14
+ const BSON_DATA_ARRAY = 4;
15
+ const BSON_DATA_BINARY = 5;
16
+ const BSON_DATA_UNDEFINED = 6;
17
+ const BSON_DATA_OID = 7;
18
+ const BSON_DATA_BOOLEAN = 8;
19
+ const BSON_DATA_DATE = 9;
20
+ const BSON_DATA_NULL = 10;
21
+ const BSON_DATA_REGEXP = 11;
22
+ const BSON_DATA_DBPOINTER = 12;
23
+ const BSON_DATA_CODE = 13;
24
+ const BSON_DATA_SYMBOL = 14;
25
+ const BSON_DATA_CODE_W_SCOPE = 15;
26
+ const BSON_DATA_INT = 16;
27
+ const BSON_DATA_TIMESTAMP = 17;
28
+ const BSON_DATA_LONG = 18;
29
+ const BSON_DATA_DECIMAL128 = 19;
30
+ const BSON_DATA_MIN_KEY = 0xff;
31
+ const BSON_DATA_MAX_KEY = 0x7f;
32
+ const BSON_BINARY_SUBTYPE_DEFAULT = 0;
33
+ const BSON_BINARY_SUBTYPE_UUID_NEW = 4;
34
+ const BSONType = Object.freeze({
35
+ double: 1,
36
+ string: 2,
37
+ object: 3,
38
+ array: 4,
39
+ binData: 5,
40
+ undefined: 6,
41
+ objectId: 7,
42
+ bool: 8,
43
+ date: 9,
44
+ null: 10,
45
+ regex: 11,
46
+ dbPointer: 12,
47
+ javascript: 13,
48
+ symbol: 14,
49
+ javascriptWithScope: 15,
50
+ int: 16,
51
+ timestamp: 17,
52
+ long: 18,
53
+ decimal: 19,
54
+ minKey: -1,
55
+ maxKey: 127
56
+ });
57
+
4
58
  class BSONError extends Error {
5
59
  get bsonError() {
6
60
  return true;
@@ -21,6 +75,14 @@ class BSONError extends Error {
21
75
  'stack' in value);
22
76
  }
23
77
  }
78
+ class BSONVersionError extends BSONError {
79
+ get name() {
80
+ return 'BSONVersionError';
81
+ }
82
+ constructor() {
83
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
84
+ }
85
+ }
24
86
 
25
87
  function nodejsMathRandomBytes(byteLength) {
26
88
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
@@ -251,68 +313,18 @@ function isDate(d) {
251
313
  return Object.prototype.toString.call(d) === '[object Date]';
252
314
  }
253
315
 
254
- const BSON_MAJOR_VERSION = 5;
255
- const BSON_INT32_MAX = 0x7fffffff;
256
- const BSON_INT32_MIN = -0x80000000;
257
- const BSON_INT64_MAX = Math.pow(2, 63) - 1;
258
- const BSON_INT64_MIN = -Math.pow(2, 63);
259
- const JS_INT_MAX = Math.pow(2, 53);
260
- const JS_INT_MIN = -Math.pow(2, 53);
261
- const BSON_DATA_NUMBER = 1;
262
- const BSON_DATA_STRING = 2;
263
- const BSON_DATA_OBJECT = 3;
264
- const BSON_DATA_ARRAY = 4;
265
- const BSON_DATA_BINARY = 5;
266
- const BSON_DATA_UNDEFINED = 6;
267
- const BSON_DATA_OID = 7;
268
- const BSON_DATA_BOOLEAN = 8;
269
- const BSON_DATA_DATE = 9;
270
- const BSON_DATA_NULL = 10;
271
- const BSON_DATA_REGEXP = 11;
272
- const BSON_DATA_DBPOINTER = 12;
273
- const BSON_DATA_CODE = 13;
274
- const BSON_DATA_SYMBOL = 14;
275
- const BSON_DATA_CODE_W_SCOPE = 15;
276
- const BSON_DATA_INT = 16;
277
- const BSON_DATA_TIMESTAMP = 17;
278
- const BSON_DATA_LONG = 18;
279
- const BSON_DATA_DECIMAL128 = 19;
280
- const BSON_DATA_MIN_KEY = 0xff;
281
- const BSON_DATA_MAX_KEY = 0x7f;
282
- const BSON_BINARY_SUBTYPE_DEFAULT = 0;
283
- const BSON_BINARY_SUBTYPE_UUID_NEW = 4;
284
- const BSONType = Object.freeze({
285
- double: 1,
286
- string: 2,
287
- object: 3,
288
- array: 4,
289
- binData: 5,
290
- undefined: 6,
291
- objectId: 7,
292
- bool: 8,
293
- date: 9,
294
- null: 10,
295
- regex: 11,
296
- dbPointer: 12,
297
- javascript: 13,
298
- symbol: 14,
299
- javascriptWithScope: 15,
300
- int: 16,
301
- timestamp: 17,
302
- long: 18,
303
- decimal: 19,
304
- minKey: -1,
305
- maxKey: 127
306
- });
316
+ class BSONValue {
317
+ get [Symbol.for('@@mdb.bson.version')]() {
318
+ return BSON_MAJOR_VERSION;
319
+ }
320
+ }
307
321
 
308
- class Binary {
322
+ class Binary extends BSONValue {
309
323
  get _bsontype() {
310
324
  return 'Binary';
311
325
  }
312
- get [Symbol.for('@@mdb.bson.version')]() {
313
- return BSON_MAJOR_VERSION;
314
- }
315
326
  constructor(buffer, subType) {
327
+ super();
316
328
  if (!(buffer == null) &&
317
329
  !(typeof buffer === 'string') &&
318
330
  !ArrayBuffer.isView(buffer) &&
@@ -483,9 +495,6 @@ Binary.SUBTYPE_COLUMN = 7;
483
495
  Binary.SUBTYPE_USER_DEFINED = 128;
484
496
  const UUID_BYTE_LENGTH = 16;
485
497
  class UUID extends Binary {
486
- get [Symbol.for('@@mdb.bson.version')]() {
487
- return BSON_MAJOR_VERSION;
488
- }
489
498
  constructor(input) {
490
499
  let bytes;
491
500
  let hexStr;
@@ -590,14 +599,12 @@ class UUID extends Binary {
590
599
  }
591
600
  }
592
601
 
593
- class Code {
602
+ class Code extends BSONValue {
594
603
  get _bsontype() {
595
604
  return 'Code';
596
605
  }
597
- get [Symbol.for('@@mdb.bson.version')]() {
598
- return BSON_MAJOR_VERSION;
599
- }
600
606
  constructor(code, scope) {
607
+ super();
601
608
  this.code = code.toString();
602
609
  this.scope = scope ?? null;
603
610
  }
@@ -634,14 +641,12 @@ function isDBRefLike(value) {
634
641
  typeof value.$ref === 'string' &&
635
642
  (!('$db' in value) || ('$db' in value && typeof value.$db === 'string')));
636
643
  }
637
- class DBRef {
644
+ class DBRef extends BSONValue {
638
645
  get _bsontype() {
639
646
  return 'DBRef';
640
647
  }
641
- get [Symbol.for('@@mdb.bson.version')]() {
642
- return BSON_MAJOR_VERSION;
643
- }
644
648
  constructor(collection, oid, db, fields) {
649
+ super();
645
650
  const parts = collection.split('.');
646
651
  if (parts.length === 2) {
647
652
  db = parts.shift();
@@ -710,17 +715,17 @@ const TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
710
715
  const TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
711
716
  const INT_CACHE = {};
712
717
  const UINT_CACHE = {};
713
- class Long {
718
+ const MAX_INT64_STRING_LENGTH = 20;
719
+ const DECIMAL_REG_EX = /^(\+?0|(\+|-)?[1-9][0-9]*)$/;
720
+ class Long extends BSONValue {
714
721
  get _bsontype() {
715
722
  return 'Long';
716
723
  }
717
- get [Symbol.for('@@mdb.bson.version')]() {
718
- return BSON_MAJOR_VERSION;
719
- }
720
724
  get __isLong__() {
721
725
  return true;
722
726
  }
723
727
  constructor(low = 0, high, unsigned) {
728
+ super();
724
729
  if (typeof low === 'bigint') {
725
730
  Object.assign(this, Long.fromBigInt(low, !!high));
726
731
  }
@@ -1303,8 +1308,22 @@ class Long {
1303
1308
  return { $numberLong: this.toString() };
1304
1309
  }
1305
1310
  static fromExtendedJSON(doc, options) {
1306
- const result = Long.fromString(doc.$numberLong);
1307
- return options && options.relaxed ? result.toNumber() : result;
1311
+ const { useBigInt64 = false, relaxed = true } = { ...options };
1312
+ if (doc.$numberLong.length > MAX_INT64_STRING_LENGTH) {
1313
+ throw new BSONError('$numberLong string is too long');
1314
+ }
1315
+ if (!DECIMAL_REG_EX.test(doc.$numberLong)) {
1316
+ throw new BSONError(`$numberLong string "${doc.$numberLong}" is in an invalid format`);
1317
+ }
1318
+ if (useBigInt64) {
1319
+ const bigIntResult = BigInt(doc.$numberLong);
1320
+ return BigInt.asIntN(64, bigIntResult);
1321
+ }
1322
+ const longResult = Long.fromString(doc.$numberLong);
1323
+ if (relaxed) {
1324
+ return longResult.toNumber();
1325
+ }
1326
+ return longResult;
1308
1327
  }
1309
1328
  [Symbol.for('nodejs.util.inspect.custom')]() {
1310
1329
  return this.inspect();
@@ -1398,14 +1417,12 @@ function lessThan(left, right) {
1398
1417
  function invalidErr(string, message) {
1399
1418
  throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`);
1400
1419
  }
1401
- class Decimal128 {
1420
+ class Decimal128 extends BSONValue {
1402
1421
  get _bsontype() {
1403
1422
  return 'Decimal128';
1404
1423
  }
1405
- get [Symbol.for('@@mdb.bson.version')]() {
1406
- return BSON_MAJOR_VERSION;
1407
- }
1408
1424
  constructor(bytes) {
1425
+ super();
1409
1426
  if (typeof bytes === 'string') {
1410
1427
  this.bytes = Decimal128.fromString(bytes).bytes;
1411
1428
  }
@@ -1827,14 +1844,12 @@ class Decimal128 {
1827
1844
  }
1828
1845
  }
1829
1846
 
1830
- class Double {
1847
+ class Double extends BSONValue {
1831
1848
  get _bsontype() {
1832
1849
  return 'Double';
1833
1850
  }
1834
- get [Symbol.for('@@mdb.bson.version')]() {
1835
- return BSON_MAJOR_VERSION;
1836
- }
1837
1851
  constructor(value) {
1852
+ super();
1838
1853
  if (value instanceof Number) {
1839
1854
  value = value.valueOf();
1840
1855
  }
@@ -1873,14 +1888,12 @@ class Double {
1873
1888
  }
1874
1889
  }
1875
1890
 
1876
- class Int32 {
1891
+ class Int32 extends BSONValue {
1877
1892
  get _bsontype() {
1878
1893
  return 'Int32';
1879
1894
  }
1880
- get [Symbol.for('@@mdb.bson.version')]() {
1881
- return BSON_MAJOR_VERSION;
1882
- }
1883
1895
  constructor(value) {
1896
+ super();
1884
1897
  if (value instanceof Number) {
1885
1898
  value = value.valueOf();
1886
1899
  }
@@ -1911,13 +1924,10 @@ class Int32 {
1911
1924
  }
1912
1925
  }
1913
1926
 
1914
- class MaxKey {
1927
+ class MaxKey extends BSONValue {
1915
1928
  get _bsontype() {
1916
1929
  return 'MaxKey';
1917
1930
  }
1918
- get [Symbol.for('@@mdb.bson.version')]() {
1919
- return BSON_MAJOR_VERSION;
1920
- }
1921
1931
  toExtendedJSON() {
1922
1932
  return { $maxKey: 1 };
1923
1933
  }
@@ -1932,13 +1942,10 @@ class MaxKey {
1932
1942
  }
1933
1943
  }
1934
1944
 
1935
- class MinKey {
1945
+ class MinKey extends BSONValue {
1936
1946
  get _bsontype() {
1937
1947
  return 'MinKey';
1938
1948
  }
1939
- get [Symbol.for('@@mdb.bson.version')]() {
1940
- return BSON_MAJOR_VERSION;
1941
- }
1942
1949
  toExtendedJSON() {
1943
1950
  return { $minKey: 1 };
1944
1951
  }
@@ -1956,14 +1963,12 @@ class MinKey {
1956
1963
  const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
1957
1964
  let PROCESS_UNIQUE = null;
1958
1965
  const kId = Symbol('id');
1959
- class ObjectId {
1966
+ class ObjectId extends BSONValue {
1960
1967
  get _bsontype() {
1961
1968
  return 'ObjectId';
1962
1969
  }
1963
- get [Symbol.for('@@mdb.bson.version')]() {
1964
- return BSON_MAJOR_VERSION;
1965
- }
1966
1970
  constructor(inputId) {
1971
+ super();
1967
1972
  let workingId;
1968
1973
  if (typeof inputId === 'object' && inputId && 'id' in inputId) {
1969
1974
  if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
@@ -2185,14 +2190,12 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2185
2190
  if (value != null &&
2186
2191
  typeof value._bsontype === 'string' &&
2187
2192
  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');
2193
+ throw new BSONVersionError();
2189
2194
  }
2190
- else if (value == null ||
2191
- value['_bsontype'] === 'MinKey' ||
2192
- value['_bsontype'] === 'MaxKey') {
2195
+ else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
2193
2196
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
2194
2197
  }
2195
- else if (value['_bsontype'] === 'ObjectId') {
2198
+ else if (value._bsontype === 'ObjectId') {
2196
2199
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (12 + 1);
2197
2200
  }
2198
2201
  else if (value instanceof Date || isDate(value)) {
@@ -2203,15 +2206,15 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2203
2206
  isAnyArrayBuffer(value)) {
2204
2207
  return ((name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 4 + 1) + value.byteLength);
2205
2208
  }
2206
- else if (value['_bsontype'] === 'Long' ||
2207
- value['_bsontype'] === 'Double' ||
2208
- value['_bsontype'] === 'Timestamp') {
2209
+ else if (value._bsontype === 'Long' ||
2210
+ value._bsontype === 'Double' ||
2211
+ value._bsontype === 'Timestamp') {
2209
2212
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
2210
2213
  }
2211
- else if (value['_bsontype'] === 'Decimal128') {
2214
+ else if (value._bsontype === 'Decimal128') {
2212
2215
  return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (16 + 1);
2213
2216
  }
2214
- else if (value['_bsontype'] === 'Code') {
2217
+ else if (value._bsontype === 'Code') {
2215
2218
  if (value.scope != null && Object.keys(value.scope).length > 0) {
2216
2219
  return ((name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
2217
2220
  1 +
@@ -2229,7 +2232,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2229
2232
  1);
2230
2233
  }
2231
2234
  }
2232
- else if (value['_bsontype'] === 'Binary') {
2235
+ else if (value._bsontype === 'Binary') {
2233
2236
  const binary = value;
2234
2237
  if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
2235
2238
  return ((name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
@@ -2239,14 +2242,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2239
2242
  return ((name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (binary.position + 1 + 4 + 1));
2240
2243
  }
2241
2244
  }
2242
- else if (value['_bsontype'] === 'Symbol') {
2245
+ else if (value._bsontype === 'Symbol') {
2243
2246
  return ((name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
2244
2247
  ByteUtils.utf8ByteLength(value.value) +
2245
2248
  4 +
2246
2249
  1 +
2247
2250
  1);
2248
2251
  }
2249
- else if (value['_bsontype'] === 'DBRef') {
2252
+ else if (value._bsontype === 'DBRef') {
2250
2253
  const ordered_values = Object.assign({
2251
2254
  $ref: value.collection,
2252
2255
  $id: value.oid
@@ -2268,7 +2271,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2268
2271
  (value.multiline ? 1 : 0) +
2269
2272
  1);
2270
2273
  }
2271
- else if (value['_bsontype'] === 'BSONRegExp') {
2274
+ else if (value._bsontype === 'BSONRegExp') {
2272
2275
  return ((name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
2273
2276
  1 +
2274
2277
  ByteUtils.utf8ByteLength(value.pattern) +
@@ -2296,14 +2299,12 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2296
2299
  function alphabetize(str) {
2297
2300
  return str.split('').sort().join('');
2298
2301
  }
2299
- class BSONRegExp {
2302
+ class BSONRegExp extends BSONValue {
2300
2303
  get _bsontype() {
2301
2304
  return 'BSONRegExp';
2302
2305
  }
2303
- get [Symbol.for('@@mdb.bson.version')]() {
2304
- return BSON_MAJOR_VERSION;
2305
- }
2306
2306
  constructor(pattern, options) {
2307
+ super();
2307
2308
  this.pattern = pattern;
2308
2309
  this.options = alphabetize(options ?? '');
2309
2310
  if (this.pattern.indexOf('\x00') !== -1) {
@@ -2357,14 +2358,12 @@ class BSONRegExp {
2357
2358
  }
2358
2359
  }
2359
2360
 
2360
- class BSONSymbol {
2361
+ class BSONSymbol extends BSONValue {
2361
2362
  get _bsontype() {
2362
2363
  return 'BSONSymbol';
2363
2364
  }
2364
- get [Symbol.for('@@mdb.bson.version')]() {
2365
- return BSON_MAJOR_VERSION;
2366
- }
2367
2365
  constructor(value) {
2366
+ super();
2368
2367
  this.value = value;
2369
2368
  }
2370
2369
  valueOf() {
@@ -2395,9 +2394,6 @@ class Timestamp extends LongWithoutOverridesClass {
2395
2394
  get _bsontype() {
2396
2395
  return 'Timestamp';
2397
2396
  }
2398
- get [Symbol.for('@@mdb.bson.version')]() {
2399
- return BSON_MAJOR_VERSION;
2400
- }
2401
2397
  constructor(low) {
2402
2398
  if (low == null) {
2403
2399
  super(0, 0, true);
@@ -3414,7 +3410,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3414
3410
  }
3415
3411
  else if (typeof value === 'object' &&
3416
3412
  value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3417
- throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3413
+ throw new BSONVersionError();
3418
3414
  }
3419
3415
  else if (value._bsontype === 'ObjectId') {
3420
3416
  index = serializeObjectId(buffer, key, value, index);
@@ -3513,7 +3509,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3513
3509
  }
3514
3510
  else if (typeof value === 'object' &&
3515
3511
  value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3516
- throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3512
+ throw new BSONVersionError();
3517
3513
  }
3518
3514
  else if (value._bsontype === 'ObjectId') {
3519
3515
  index = serializeObjectId(buffer, key, value, index);
@@ -3615,7 +3611,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3615
3611
  }
3616
3612
  else if (typeof value === 'object' &&
3617
3613
  value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3618
- throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3614
+ throw new BSONVersionError();
3619
3615
  }
3620
3616
  else if (value._bsontype === 'ObjectId') {
3621
3617
  index = serializeObjectId(buffer, key, value, index);
@@ -3691,14 +3687,19 @@ const keysToCodecs = {
3691
3687
  };
3692
3688
  function deserializeValue(value, options = {}) {
3693
3689
  if (typeof value === 'number') {
3690
+ const in32BitRange = value <= BSON_INT32_MAX && value >= BSON_INT32_MIN;
3691
+ const in64BitRange = value <= BSON_INT64_MAX && value >= BSON_INT64_MIN;
3694
3692
  if (options.relaxed || options.legacy) {
3695
3693
  return value;
3696
3694
  }
3697
3695
  if (Number.isInteger(value) && !Object.is(value, -0)) {
3698
- if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) {
3696
+ if (in32BitRange) {
3699
3697
  return new Int32(value);
3700
3698
  }
3701
- if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) {
3699
+ if (in64BitRange) {
3700
+ if (options.useBigInt64) {
3701
+ return BigInt(value);
3702
+ }
3702
3703
  return Long.fromNumber(value);
3703
3704
  }
3704
3705
  }
@@ -3820,6 +3821,12 @@ function serializeValue(value, options) {
3820
3821
  }
3821
3822
  return { $numberDouble: Object.is(value, -0) ? '-0.0' : value.toString() };
3822
3823
  }
3824
+ if (typeof value === 'bigint') {
3825
+ if (!options.relaxed) {
3826
+ return { $numberLong: BigInt.asIntN(64, value).toString() };
3827
+ }
3828
+ return Number(BigInt.asIntN(64, value));
3829
+ }
3823
3830
  if (value instanceof RegExp || isRegExp(value)) {
3824
3831
  let flags = value.flags;
3825
3832
  if (flags === undefined) {
@@ -3882,7 +3889,7 @@ function serializeDocument(doc, options) {
3882
3889
  typeof doc === 'object' &&
3883
3890
  typeof doc._bsontype === 'string' &&
3884
3891
  doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3885
- throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
3892
+ throw new BSONVersionError();
3886
3893
  }
3887
3894
  else if (isBSONType(doc)) {
3888
3895
  let outDoc = doc;
@@ -3906,11 +3913,16 @@ function serializeDocument(doc, options) {
3906
3913
  }
3907
3914
  }
3908
3915
  function parse(text, options) {
3916
+ const ejsonOptions = {
3917
+ useBigInt64: options?.useBigInt64 ?? false,
3918
+ relaxed: options?.relaxed ?? true,
3919
+ legacy: options?.legacy ?? false
3920
+ };
3909
3921
  return JSON.parse(text, (key, value) => {
3910
3922
  if (key.indexOf('\x00') !== -1) {
3911
3923
  throw new BSONError(`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`);
3912
3924
  }
3913
- return deserializeValue(value, { relaxed: true, legacy: false, ...options });
3925
+ return deserializeValue(value, ejsonOptions);
3914
3926
  });
3915
3927
  }
3916
3928
  function stringify(value, replacer, space, options) {
@@ -4020,7 +4032,9 @@ serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4020
4032
  deserialize: deserialize,
4021
4033
  calculateObjectSize: calculateObjectSize,
4022
4034
  deserializeStream: deserializeStream,
4035
+ BSONValue: BSONValue,
4023
4036
  BSONError: BSONError,
4037
+ BSONVersionError: BSONVersionError,
4024
4038
  BSONType: BSONType,
4025
4039
  EJSON: EJSON
4026
4040
  });
@@ -4030,6 +4044,8 @@ exports.BSONError = BSONError;
4030
4044
  exports.BSONRegExp = BSONRegExp;
4031
4045
  exports.BSONSymbol = BSONSymbol;
4032
4046
  exports.BSONType = BSONType;
4047
+ exports.BSONValue = BSONValue;
4048
+ exports.BSONVersionError = BSONVersionError;
4033
4049
  exports.Binary = Binary;
4034
4050
  exports.Code = Code;
4035
4051
  exports.DBRef = DBRef;