bson 6.10.4 → 7.0.0-alpha

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
@@ -142,6 +142,7 @@ class BSONOffsetError extends BSONError {
142
142
  get name() {
143
143
  return 'BSONOffsetError';
144
144
  }
145
+ offset;
145
146
  constructor(message, offset, options) {
146
147
  super(`${message}. offset: ${offset}`, options);
147
148
  this.offset = offset;
@@ -218,11 +219,15 @@ function tryWriteBasicLatin(destination, source, offset) {
218
219
  function nodejsMathRandomBytes(byteLength) {
219
220
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
220
221
  }
222
+ function nodejsSecureRandomBytes(byteLength) {
223
+ return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
224
+ }
221
225
  const nodejsRandomBytes = (() => {
222
- try {
223
- return require('crypto').randomBytes;
226
+ const { crypto } = globalThis;
227
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
228
+ return nodejsSecureRandomBytes;
224
229
  }
225
- catch {
230
+ else {
226
231
  return nodejsMathRandomBytes;
227
232
  }
228
233
  })();
@@ -441,7 +446,11 @@ const webByteUtils = {
441
446
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
442
447
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
443
448
 
449
+ const bsonType = Symbol.for('@@mdb.bson.type');
444
450
  class BSONValue {
451
+ get [bsonType]() {
452
+ return this._bsontype;
453
+ }
445
454
  get [BSON_VERSION_SYMBOL]() {
446
455
  return BSON_MAJOR_VERSION;
447
456
  }
@@ -492,7 +501,7 @@ const NumberUtils = {
492
501
  source[offset + 1] * 256 +
493
502
  source[offset + 2] * 65536 +
494
503
  source[offset + 3] * 16777216);
495
- return (hi << BigInt(32)) + lo;
504
+ return (hi << 32n) + lo;
496
505
  },
497
506
  getFloat64LE: isBigEndian
498
507
  ? (source, offset) => {
@@ -538,7 +547,7 @@ const NumberUtils = {
538
547
  return 4;
539
548
  },
540
549
  setBigInt64LE(destination, offset, value) {
541
- const mask32bits = BigInt(0xffff_ffff);
550
+ const mask32bits = 0xffffffffn;
542
551
  let lo = Number(value & mask32bits);
543
552
  destination[offset] = lo;
544
553
  lo >>= 8;
@@ -547,7 +556,7 @@ const NumberUtils = {
547
556
  destination[offset + 2] = lo;
548
557
  lo >>= 8;
549
558
  destination[offset + 3] = lo;
550
- let hi = Number((value >> BigInt(32)) & mask32bits);
559
+ let hi = Number((value >> 32n) & mask32bits);
551
560
  destination[offset + 4] = hi;
552
561
  hi >>= 8;
553
562
  destination[offset + 5] = hi;
@@ -588,6 +597,27 @@ class Binary extends BSONValue {
588
597
  get _bsontype() {
589
598
  return 'Binary';
590
599
  }
600
+ static BSON_BINARY_SUBTYPE_DEFAULT = 0;
601
+ static BUFFER_SIZE = 256;
602
+ static SUBTYPE_DEFAULT = 0;
603
+ static SUBTYPE_FUNCTION = 1;
604
+ static SUBTYPE_BYTE_ARRAY = 2;
605
+ static SUBTYPE_UUID_OLD = 3;
606
+ static SUBTYPE_UUID = 4;
607
+ static SUBTYPE_MD5 = 5;
608
+ static SUBTYPE_ENCRYPTED = 6;
609
+ static SUBTYPE_COLUMN = 7;
610
+ static SUBTYPE_SENSITIVE = 8;
611
+ static SUBTYPE_VECTOR = 9;
612
+ static SUBTYPE_USER_DEFINED = 128;
613
+ static VECTOR_TYPE = Object.freeze({
614
+ Int8: 0x03,
615
+ Float32: 0x27,
616
+ PackedBit: 0x10
617
+ });
618
+ buffer;
619
+ sub_type;
620
+ position;
591
621
  constructor(buffer, subType) {
592
622
  super();
593
623
  if (!(buffer == null) &&
@@ -847,24 +877,6 @@ class Binary extends BSONValue {
847
877
  return new this(bytes, Binary.SUBTYPE_VECTOR);
848
878
  }
849
879
  }
850
- Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
851
- Binary.BUFFER_SIZE = 256;
852
- Binary.SUBTYPE_DEFAULT = 0;
853
- Binary.SUBTYPE_FUNCTION = 1;
854
- Binary.SUBTYPE_BYTE_ARRAY = 2;
855
- Binary.SUBTYPE_UUID_OLD = 3;
856
- Binary.SUBTYPE_UUID = 4;
857
- Binary.SUBTYPE_MD5 = 5;
858
- Binary.SUBTYPE_ENCRYPTED = 6;
859
- Binary.SUBTYPE_COLUMN = 7;
860
- Binary.SUBTYPE_SENSITIVE = 8;
861
- Binary.SUBTYPE_VECTOR = 9;
862
- Binary.SUBTYPE_USER_DEFINED = 128;
863
- Binary.VECTOR_TYPE = Object.freeze({
864
- Int8: 0x03,
865
- Float32: 0x27,
866
- PackedBit: 0x10
867
- });
868
880
  function validateBinaryVector(vector) {
869
881
  if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
870
882
  return;
@@ -1001,6 +1013,8 @@ class Code extends BSONValue {
1001
1013
  get _bsontype() {
1002
1014
  return 'Code';
1003
1015
  }
1016
+ code;
1017
+ scope;
1004
1018
  constructor(code, scope) {
1005
1019
  super();
1006
1020
  this.code = code.toString();
@@ -1046,6 +1060,10 @@ class DBRef extends BSONValue {
1046
1060
  get _bsontype() {
1047
1061
  return 'DBRef';
1048
1062
  }
1063
+ collection;
1064
+ oid;
1065
+ db;
1066
+ fields;
1049
1067
  constructor(collection, oid, db, fields) {
1050
1068
  super();
1051
1069
  const parts = collection.split('.');
@@ -1155,6 +1173,9 @@ class Long extends BSONValue {
1155
1173
  get __isLong__() {
1156
1174
  return true;
1157
1175
  }
1176
+ high;
1177
+ low;
1178
+ unsigned;
1158
1179
  constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
1159
1180
  super();
1160
1181
  const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
@@ -1168,6 +1189,15 @@ class Long extends BSONValue {
1168
1189
  this.high = res.high;
1169
1190
  this.unsigned = res.unsigned;
1170
1191
  }
1192
+ static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1193
+ static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1194
+ static ZERO = Long.fromInt(0);
1195
+ static UZERO = Long.fromInt(0, true);
1196
+ static ONE = Long.fromInt(1);
1197
+ static UONE = Long.fromInt(1, true);
1198
+ static NEG_ONE = Long.fromInt(-1);
1199
+ static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1200
+ static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1171
1201
  static fromBits(lowBits, highBits, unsigned) {
1172
1202
  return new Long(lowBits, highBits, unsigned);
1173
1203
  }
@@ -1208,7 +1238,7 @@ class Long extends BSONValue {
1208
1238
  return Long.MAX_UNSIGNED_VALUE;
1209
1239
  }
1210
1240
  else {
1211
- if (value <= -9223372036854776e3)
1241
+ if (value <= -TWO_PWR_63_DBL)
1212
1242
  return Long.MIN_VALUE;
1213
1243
  if (value + 1 >= TWO_PWR_63_DBL)
1214
1244
  return Long.MAX_VALUE;
@@ -1218,8 +1248,8 @@ class Long extends BSONValue {
1218
1248
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1219
1249
  }
1220
1250
  static fromBigInt(value, unsigned) {
1221
- const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
1222
- const FROM_BIGINT_BIT_SHIFT = BigInt(32);
1251
+ const FROM_BIGINT_BIT_MASK = 0xffffffffn;
1252
+ const FROM_BIGINT_BIT_SHIFT = 32n;
1223
1253
  return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
1224
1254
  }
1225
1255
  static _fromString(str, unsigned, radix) {
@@ -1252,7 +1282,7 @@ class Long extends BSONValue {
1252
1282
  static fromStringStrict(str, unsignedOrRadix, radix) {
1253
1283
  let unsigned = false;
1254
1284
  if (typeof unsignedOrRadix === 'number') {
1255
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1285
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1256
1286
  }
1257
1287
  else {
1258
1288
  unsigned = !!unsignedOrRadix;
@@ -1274,7 +1304,7 @@ class Long extends BSONValue {
1274
1304
  static fromString(str, unsignedOrRadix, radix) {
1275
1305
  let unsigned = false;
1276
1306
  if (typeof unsignedOrRadix === 'number') {
1277
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1307
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1278
1308
  }
1279
1309
  else {
1280
1310
  unsigned = !!unsignedOrRadix;
@@ -1794,15 +1824,6 @@ class Long extends BSONValue {
1794
1824
  return `new Long(${longVal}${unsignedVal})`;
1795
1825
  }
1796
1826
  }
1797
- Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1798
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1799
- Long.ZERO = Long.fromInt(0);
1800
- Long.UZERO = Long.fromInt(0, true);
1801
- Long.ONE = Long.fromInt(1);
1802
- Long.UONE = Long.fromInt(1, true);
1803
- Long.NEG_ONE = Long.fromInt(-1);
1804
- Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1805
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1806
1827
 
1807
1828
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
1808
1829
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -1883,6 +1904,7 @@ class Decimal128 extends BSONValue {
1883
1904
  get _bsontype() {
1884
1905
  return 'Decimal128';
1885
1906
  }
1907
+ bytes;
1886
1908
  constructor(bytes) {
1887
1909
  super();
1888
1910
  if (typeof bytes === 'string') {
@@ -2358,6 +2380,7 @@ class Double extends BSONValue {
2358
2380
  get _bsontype() {
2359
2381
  return 'Double';
2360
2382
  }
2383
+ value;
2361
2384
  constructor(value) {
2362
2385
  super();
2363
2386
  if (value instanceof Number) {
@@ -2421,6 +2444,7 @@ class Int32 extends BSONValue {
2421
2444
  get _bsontype() {
2422
2445
  return 'Int32';
2423
2446
  }
2447
+ value;
2424
2448
  constructor(value) {
2425
2449
  super();
2426
2450
  if (value instanceof Number) {
@@ -2499,11 +2523,14 @@ class MinKey extends BSONValue {
2499
2523
  }
2500
2524
 
2501
2525
  let PROCESS_UNIQUE = null;
2502
- const __idCache = new WeakMap();
2503
2526
  class ObjectId extends BSONValue {
2504
2527
  get _bsontype() {
2505
2528
  return 'ObjectId';
2506
2529
  }
2530
+ static index = Math.floor(Math.random() * 0xffffff);
2531
+ static cacheHexString = false;
2532
+ buffer;
2533
+ #cachedHexString = null;
2507
2534
  constructor(inputId) {
2508
2535
  super();
2509
2536
  let workingId;
@@ -2521,8 +2548,8 @@ class ObjectId extends BSONValue {
2521
2548
  else {
2522
2549
  workingId = inputId;
2523
2550
  }
2524
- if (workingId == null || typeof workingId === 'number') {
2525
- this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2551
+ if (workingId == null) {
2552
+ this.buffer = ObjectId.generate();
2526
2553
  }
2527
2554
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2528
2555
  this.buffer = ByteUtils.toLocalBufferType(workingId);
@@ -2531,7 +2558,7 @@ class ObjectId extends BSONValue {
2531
2558
  if (ObjectId.validateHexString(workingId)) {
2532
2559
  this.buffer = ByteUtils.fromHex(workingId);
2533
2560
  if (ObjectId.cacheHexString) {
2534
- __idCache.set(this, workingId);
2561
+ this.#cachedHexString = workingId;
2535
2562
  }
2536
2563
  }
2537
2564
  else {
@@ -2548,7 +2575,7 @@ class ObjectId extends BSONValue {
2548
2575
  set id(value) {
2549
2576
  this.buffer = value;
2550
2577
  if (ObjectId.cacheHexString) {
2551
- __idCache.set(this, ByteUtils.toHex(value));
2578
+ this.#cachedHexString = ByteUtils.toHex(value);
2552
2579
  }
2553
2580
  }
2554
2581
  static validateHexString(string) {
@@ -2566,14 +2593,11 @@ class ObjectId extends BSONValue {
2566
2593
  return true;
2567
2594
  }
2568
2595
  toHexString() {
2569
- if (ObjectId.cacheHexString) {
2570
- const __id = __idCache.get(this);
2571
- if (__id)
2572
- return __id;
2573
- }
2596
+ if (this.#cachedHexString)
2597
+ return this.#cachedHexString.toLowerCase();
2574
2598
  const hexString = ByteUtils.toHex(this.id);
2575
2599
  if (ObjectId.cacheHexString) {
2576
- __idCache.set(this, hexString);
2600
+ this.#cachedHexString = hexString;
2577
2601
  }
2578
2602
  return hexString;
2579
2603
  }
@@ -2698,14 +2722,13 @@ class ObjectId extends BSONValue {
2698
2722
  return new ObjectId(doc.$oid);
2699
2723
  }
2700
2724
  isCached() {
2701
- return ObjectId.cacheHexString && __idCache.has(this);
2725
+ return ObjectId.cacheHexString && this.#cachedHexString != null;
2702
2726
  }
2703
2727
  inspect(depth, options, inspect) {
2704
2728
  inspect ??= defaultInspect;
2705
2729
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
2706
2730
  }
2707
2731
  }
2708
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
2709
2732
 
2710
2733
  function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
2711
2734
  let totalLength = 4 + 1;
@@ -2874,6 +2897,8 @@ class BSONRegExp extends BSONValue {
2874
2897
  get _bsontype() {
2875
2898
  return 'BSONRegExp';
2876
2899
  }
2900
+ pattern;
2901
+ options;
2877
2902
  constructor(pattern, options) {
2878
2903
  super();
2879
2904
  this.pattern = pattern;
@@ -2934,6 +2959,7 @@ class BSONSymbol extends BSONValue {
2934
2959
  get _bsontype() {
2935
2960
  return 'BSONSymbol';
2936
2961
  }
2962
+ value;
2937
2963
  constructor(value) {
2938
2964
  super();
2939
2965
  this.value = value;
@@ -2964,6 +2990,10 @@ class Timestamp extends LongWithoutOverridesClass {
2964
2990
  get _bsontype() {
2965
2991
  return 'Timestamp';
2966
2992
  }
2993
+ get [bsonType]() {
2994
+ return 'Timestamp';
2995
+ }
2996
+ static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2967
2997
  get i() {
2968
2998
  return this.low >>> 0;
2969
2999
  }
@@ -3043,7 +3073,6 @@ class Timestamp extends LongWithoutOverridesClass {
3043
3073
  return `new Timestamp({ t: ${t}, i: ${i} })`;
3044
3074
  }
3045
3075
  }
3046
- Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
3047
3076
 
3048
3077
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
3049
3078
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
@@ -4595,6 +4624,7 @@ var bson = /*#__PURE__*/Object.freeze({
4595
4624
  ObjectId: ObjectId,
4596
4625
  Timestamp: Timestamp,
4597
4626
  UUID: UUID,
4627
+ bsonType: bsonType,
4598
4628
  calculateObjectSize: calculateObjectSize,
4599
4629
  deserialize: deserialize,
4600
4630
  deserializeStream: deserializeStream,
@@ -4626,6 +4656,7 @@ exports.MinKey = MinKey;
4626
4656
  exports.ObjectId = ObjectId;
4627
4657
  exports.Timestamp = Timestamp;
4628
4658
  exports.UUID = UUID;
4659
+ exports.bsonType = bsonType;
4629
4660
  exports.calculateObjectSize = calculateObjectSize;
4630
4661
  exports.deserialize = deserialize;
4631
4662
  exports.deserializeStream = deserializeStream;