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