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.mjs CHANGED
@@ -140,6 +140,7 @@ class BSONOffsetError extends BSONError {
140
140
  get name() {
141
141
  return 'BSONOffsetError';
142
142
  }
143
+ offset;
143
144
  constructor(message, offset, options) {
144
145
  super(`${message}. offset: ${offset}`, options);
145
146
  this.offset = offset;
@@ -216,7 +217,18 @@ function tryWriteBasicLatin(destination, source, offset) {
216
217
  function nodejsMathRandomBytes(byteLength) {
217
218
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
218
219
  }
219
- const nodejsRandomBytes = nodejsMathRandomBytes;
220
+ function nodejsSecureRandomBytes(byteLength) {
221
+ return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
222
+ }
223
+ const nodejsRandomBytes = (() => {
224
+ const { crypto } = globalThis;
225
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
226
+ return nodejsSecureRandomBytes;
227
+ }
228
+ else {
229
+ return nodejsMathRandomBytes;
230
+ }
231
+ })();
220
232
  const nodeJsByteUtils = {
221
233
  toLocalBufferType(potentialBuffer) {
222
234
  if (Buffer.isBuffer(potentialBuffer)) {
@@ -432,7 +444,11 @@ const webByteUtils = {
432
444
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
433
445
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
434
446
 
447
+ const bsonType = Symbol.for('@@mdb.bson.type');
435
448
  class BSONValue {
449
+ get [bsonType]() {
450
+ return this._bsontype;
451
+ }
436
452
  get [BSON_VERSION_SYMBOL]() {
437
453
  return BSON_MAJOR_VERSION;
438
454
  }
@@ -483,7 +499,7 @@ const NumberUtils = {
483
499
  source[offset + 1] * 256 +
484
500
  source[offset + 2] * 65536 +
485
501
  source[offset + 3] * 16777216);
486
- return (hi << BigInt(32)) + lo;
502
+ return (hi << 32n) + lo;
487
503
  },
488
504
  getFloat64LE: isBigEndian
489
505
  ? (source, offset) => {
@@ -529,7 +545,7 @@ const NumberUtils = {
529
545
  return 4;
530
546
  },
531
547
  setBigInt64LE(destination, offset, value) {
532
- const mask32bits = BigInt(0xffff_ffff);
548
+ const mask32bits = 0xffffffffn;
533
549
  let lo = Number(value & mask32bits);
534
550
  destination[offset] = lo;
535
551
  lo >>= 8;
@@ -538,7 +554,7 @@ const NumberUtils = {
538
554
  destination[offset + 2] = lo;
539
555
  lo >>= 8;
540
556
  destination[offset + 3] = lo;
541
- let hi = Number((value >> BigInt(32)) & mask32bits);
557
+ let hi = Number((value >> 32n) & mask32bits);
542
558
  destination[offset + 4] = hi;
543
559
  hi >>= 8;
544
560
  destination[offset + 5] = hi;
@@ -579,6 +595,27 @@ class Binary extends BSONValue {
579
595
  get _bsontype() {
580
596
  return 'Binary';
581
597
  }
598
+ static BSON_BINARY_SUBTYPE_DEFAULT = 0;
599
+ static BUFFER_SIZE = 256;
600
+ static SUBTYPE_DEFAULT = 0;
601
+ static SUBTYPE_FUNCTION = 1;
602
+ static SUBTYPE_BYTE_ARRAY = 2;
603
+ static SUBTYPE_UUID_OLD = 3;
604
+ static SUBTYPE_UUID = 4;
605
+ static SUBTYPE_MD5 = 5;
606
+ static SUBTYPE_ENCRYPTED = 6;
607
+ static SUBTYPE_COLUMN = 7;
608
+ static SUBTYPE_SENSITIVE = 8;
609
+ static SUBTYPE_VECTOR = 9;
610
+ static SUBTYPE_USER_DEFINED = 128;
611
+ static VECTOR_TYPE = Object.freeze({
612
+ Int8: 0x03,
613
+ Float32: 0x27,
614
+ PackedBit: 0x10
615
+ });
616
+ buffer;
617
+ sub_type;
618
+ position;
582
619
  constructor(buffer, subType) {
583
620
  super();
584
621
  if (!(buffer == null) &&
@@ -838,24 +875,6 @@ class Binary extends BSONValue {
838
875
  return new this(bytes, Binary.SUBTYPE_VECTOR);
839
876
  }
840
877
  }
841
- Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
842
- Binary.BUFFER_SIZE = 256;
843
- Binary.SUBTYPE_DEFAULT = 0;
844
- Binary.SUBTYPE_FUNCTION = 1;
845
- Binary.SUBTYPE_BYTE_ARRAY = 2;
846
- Binary.SUBTYPE_UUID_OLD = 3;
847
- Binary.SUBTYPE_UUID = 4;
848
- Binary.SUBTYPE_MD5 = 5;
849
- Binary.SUBTYPE_ENCRYPTED = 6;
850
- Binary.SUBTYPE_COLUMN = 7;
851
- Binary.SUBTYPE_SENSITIVE = 8;
852
- Binary.SUBTYPE_VECTOR = 9;
853
- Binary.SUBTYPE_USER_DEFINED = 128;
854
- Binary.VECTOR_TYPE = Object.freeze({
855
- Int8: 0x03,
856
- Float32: 0x27,
857
- PackedBit: 0x10
858
- });
859
878
  function validateBinaryVector(vector) {
860
879
  if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
861
880
  return;
@@ -992,6 +1011,8 @@ class Code extends BSONValue {
992
1011
  get _bsontype() {
993
1012
  return 'Code';
994
1013
  }
1014
+ code;
1015
+ scope;
995
1016
  constructor(code, scope) {
996
1017
  super();
997
1018
  this.code = code.toString();
@@ -1037,6 +1058,10 @@ class DBRef extends BSONValue {
1037
1058
  get _bsontype() {
1038
1059
  return 'DBRef';
1039
1060
  }
1061
+ collection;
1062
+ oid;
1063
+ db;
1064
+ fields;
1040
1065
  constructor(collection, oid, db, fields) {
1041
1066
  super();
1042
1067
  const parts = collection.split('.');
@@ -1146,6 +1171,9 @@ class Long extends BSONValue {
1146
1171
  get __isLong__() {
1147
1172
  return true;
1148
1173
  }
1174
+ high;
1175
+ low;
1176
+ unsigned;
1149
1177
  constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
1150
1178
  super();
1151
1179
  const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
@@ -1159,6 +1187,15 @@ class Long extends BSONValue {
1159
1187
  this.high = res.high;
1160
1188
  this.unsigned = res.unsigned;
1161
1189
  }
1190
+ static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1191
+ static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1192
+ static ZERO = Long.fromInt(0);
1193
+ static UZERO = Long.fromInt(0, true);
1194
+ static ONE = Long.fromInt(1);
1195
+ static UONE = Long.fromInt(1, true);
1196
+ static NEG_ONE = Long.fromInt(-1);
1197
+ static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1198
+ static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1162
1199
  static fromBits(lowBits, highBits, unsigned) {
1163
1200
  return new Long(lowBits, highBits, unsigned);
1164
1201
  }
@@ -1199,7 +1236,7 @@ class Long extends BSONValue {
1199
1236
  return Long.MAX_UNSIGNED_VALUE;
1200
1237
  }
1201
1238
  else {
1202
- if (value <= -9223372036854776e3)
1239
+ if (value <= -TWO_PWR_63_DBL)
1203
1240
  return Long.MIN_VALUE;
1204
1241
  if (value + 1 >= TWO_PWR_63_DBL)
1205
1242
  return Long.MAX_VALUE;
@@ -1209,8 +1246,8 @@ class Long extends BSONValue {
1209
1246
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1210
1247
  }
1211
1248
  static fromBigInt(value, unsigned) {
1212
- const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
1213
- const FROM_BIGINT_BIT_SHIFT = BigInt(32);
1249
+ const FROM_BIGINT_BIT_MASK = 0xffffffffn;
1250
+ const FROM_BIGINT_BIT_SHIFT = 32n;
1214
1251
  return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
1215
1252
  }
1216
1253
  static _fromString(str, unsigned, radix) {
@@ -1243,7 +1280,7 @@ class Long extends BSONValue {
1243
1280
  static fromStringStrict(str, unsignedOrRadix, radix) {
1244
1281
  let unsigned = false;
1245
1282
  if (typeof unsignedOrRadix === 'number') {
1246
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1283
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1247
1284
  }
1248
1285
  else {
1249
1286
  unsigned = !!unsignedOrRadix;
@@ -1265,7 +1302,7 @@ class Long extends BSONValue {
1265
1302
  static fromString(str, unsignedOrRadix, radix) {
1266
1303
  let unsigned = false;
1267
1304
  if (typeof unsignedOrRadix === 'number') {
1268
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1305
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1269
1306
  }
1270
1307
  else {
1271
1308
  unsigned = !!unsignedOrRadix;
@@ -1785,15 +1822,6 @@ class Long extends BSONValue {
1785
1822
  return `new Long(${longVal}${unsignedVal})`;
1786
1823
  }
1787
1824
  }
1788
- Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1789
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1790
- Long.ZERO = Long.fromInt(0);
1791
- Long.UZERO = Long.fromInt(0, true);
1792
- Long.ONE = Long.fromInt(1);
1793
- Long.UONE = Long.fromInt(1, true);
1794
- Long.NEG_ONE = Long.fromInt(-1);
1795
- Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1796
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1797
1825
 
1798
1826
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
1799
1827
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -1874,6 +1902,7 @@ class Decimal128 extends BSONValue {
1874
1902
  get _bsontype() {
1875
1903
  return 'Decimal128';
1876
1904
  }
1905
+ bytes;
1877
1906
  constructor(bytes) {
1878
1907
  super();
1879
1908
  if (typeof bytes === 'string') {
@@ -2349,6 +2378,7 @@ class Double extends BSONValue {
2349
2378
  get _bsontype() {
2350
2379
  return 'Double';
2351
2380
  }
2381
+ value;
2352
2382
  constructor(value) {
2353
2383
  super();
2354
2384
  if (value instanceof Number) {
@@ -2412,6 +2442,7 @@ class Int32 extends BSONValue {
2412
2442
  get _bsontype() {
2413
2443
  return 'Int32';
2414
2444
  }
2445
+ value;
2415
2446
  constructor(value) {
2416
2447
  super();
2417
2448
  if (value instanceof Number) {
@@ -2490,11 +2521,14 @@ class MinKey extends BSONValue {
2490
2521
  }
2491
2522
 
2492
2523
  let PROCESS_UNIQUE = null;
2493
- const __idCache = new WeakMap();
2494
2524
  class ObjectId extends BSONValue {
2495
2525
  get _bsontype() {
2496
2526
  return 'ObjectId';
2497
2527
  }
2528
+ static index = Math.floor(Math.random() * 0xffffff);
2529
+ static cacheHexString = false;
2530
+ buffer;
2531
+ #cachedHexString = null;
2498
2532
  constructor(inputId) {
2499
2533
  super();
2500
2534
  let workingId;
@@ -2512,8 +2546,8 @@ class ObjectId extends BSONValue {
2512
2546
  else {
2513
2547
  workingId = inputId;
2514
2548
  }
2515
- if (workingId == null || typeof workingId === 'number') {
2516
- this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2549
+ if (workingId == null) {
2550
+ this.buffer = ObjectId.generate();
2517
2551
  }
2518
2552
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2519
2553
  this.buffer = ByteUtils.toLocalBufferType(workingId);
@@ -2522,7 +2556,7 @@ class ObjectId extends BSONValue {
2522
2556
  if (ObjectId.validateHexString(workingId)) {
2523
2557
  this.buffer = ByteUtils.fromHex(workingId);
2524
2558
  if (ObjectId.cacheHexString) {
2525
- __idCache.set(this, workingId);
2559
+ this.#cachedHexString = workingId;
2526
2560
  }
2527
2561
  }
2528
2562
  else {
@@ -2539,7 +2573,7 @@ class ObjectId extends BSONValue {
2539
2573
  set id(value) {
2540
2574
  this.buffer = value;
2541
2575
  if (ObjectId.cacheHexString) {
2542
- __idCache.set(this, ByteUtils.toHex(value));
2576
+ this.#cachedHexString = ByteUtils.toHex(value);
2543
2577
  }
2544
2578
  }
2545
2579
  static validateHexString(string) {
@@ -2557,14 +2591,11 @@ class ObjectId extends BSONValue {
2557
2591
  return true;
2558
2592
  }
2559
2593
  toHexString() {
2560
- if (ObjectId.cacheHexString) {
2561
- const __id = __idCache.get(this);
2562
- if (__id)
2563
- return __id;
2564
- }
2594
+ if (this.#cachedHexString)
2595
+ return this.#cachedHexString.toLowerCase();
2565
2596
  const hexString = ByteUtils.toHex(this.id);
2566
2597
  if (ObjectId.cacheHexString) {
2567
- __idCache.set(this, hexString);
2598
+ this.#cachedHexString = hexString;
2568
2599
  }
2569
2600
  return hexString;
2570
2601
  }
@@ -2689,14 +2720,13 @@ class ObjectId extends BSONValue {
2689
2720
  return new ObjectId(doc.$oid);
2690
2721
  }
2691
2722
  isCached() {
2692
- return ObjectId.cacheHexString && __idCache.has(this);
2723
+ return ObjectId.cacheHexString && this.#cachedHexString != null;
2693
2724
  }
2694
2725
  inspect(depth, options, inspect) {
2695
2726
  inspect ??= defaultInspect;
2696
2727
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
2697
2728
  }
2698
2729
  }
2699
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
2700
2730
 
2701
2731
  function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
2702
2732
  let totalLength = 4 + 1;
@@ -2865,6 +2895,8 @@ class BSONRegExp extends BSONValue {
2865
2895
  get _bsontype() {
2866
2896
  return 'BSONRegExp';
2867
2897
  }
2898
+ pattern;
2899
+ options;
2868
2900
  constructor(pattern, options) {
2869
2901
  super();
2870
2902
  this.pattern = pattern;
@@ -2925,6 +2957,7 @@ class BSONSymbol extends BSONValue {
2925
2957
  get _bsontype() {
2926
2958
  return 'BSONSymbol';
2927
2959
  }
2960
+ value;
2928
2961
  constructor(value) {
2929
2962
  super();
2930
2963
  this.value = value;
@@ -2955,6 +2988,10 @@ class Timestamp extends LongWithoutOverridesClass {
2955
2988
  get _bsontype() {
2956
2989
  return 'Timestamp';
2957
2990
  }
2991
+ get [bsonType]() {
2992
+ return 'Timestamp';
2993
+ }
2994
+ static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2958
2995
  get i() {
2959
2996
  return this.low >>> 0;
2960
2997
  }
@@ -3034,7 +3071,6 @@ class Timestamp extends LongWithoutOverridesClass {
3034
3071
  return `new Timestamp({ t: ${t}, i: ${i} })`;
3035
3072
  }
3036
3073
  }
3037
- Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
3038
3074
 
3039
3075
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
3040
3076
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
@@ -4586,6 +4622,7 @@ var bson = /*#__PURE__*/Object.freeze({
4586
4622
  ObjectId: ObjectId,
4587
4623
  Timestamp: Timestamp,
4588
4624
  UUID: UUID,
4625
+ bsonType: bsonType,
4589
4626
  calculateObjectSize: calculateObjectSize,
4590
4627
  deserialize: deserialize,
4591
4628
  deserializeStream: deserializeStream,
@@ -4595,5 +4632,5 @@ var bson = /*#__PURE__*/Object.freeze({
4595
4632
  setInternalBufferSize: setInternalBufferSize
4596
4633
  });
4597
4634
 
4598
- export { bson as BSON, BSONError, BSONOffsetError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, onDemand, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4635
+ export { bson as BSON, BSONError, BSONOffsetError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, bsonType, calculateObjectSize, deserialize, deserializeStream, onDemand, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4599
4636
  //# sourceMappingURL=bson.mjs.map