bson 5.0.0-alpha.2 → 5.0.0-alpha.3

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