bson 6.10.4 → 7.0.0-alpha.1

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.node.mjs CHANGED
@@ -1,5 +1,3 @@
1
- import { randomBytes } from 'crypto';
2
-
3
1
  const TypedArrayPrototypeGetSymbolToStringTag = (() => {
4
2
  const g = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
5
3
  return (value) => g.call(value);
@@ -47,7 +45,7 @@ function getStylizeFunction(options) {
47
45
  }
48
46
  }
49
47
 
50
- const BSON_MAJOR_VERSION = 6;
48
+ const BSON_MAJOR_VERSION = 7;
51
49
  const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
52
50
  const BSON_INT32_MAX = 0x7fffffff;
53
51
  const BSON_INT32_MIN = -2147483648;
@@ -142,6 +140,7 @@ class BSONOffsetError extends BSONError {
142
140
  get name() {
143
141
  return 'BSONOffsetError';
144
142
  }
143
+ offset;
145
144
  constructor(message, offset, options) {
146
145
  super(`${message}. offset: ${offset}`, options);
147
146
  this.offset = offset;
@@ -215,6 +214,21 @@ function tryWriteBasicLatin(destination, source, offset) {
215
214
  return source.length;
216
215
  }
217
216
 
217
+ function nodejsMathRandomBytes(byteLength) {
218
+ return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
219
+ }
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
+ })();
218
232
  const nodeJsByteUtils = {
219
233
  toLocalBufferType(potentialBuffer) {
220
234
  if (Buffer.isBuffer(potentialBuffer)) {
@@ -288,7 +302,7 @@ const nodeJsByteUtils = {
288
302
  }
289
303
  return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
290
304
  },
291
- randomBytes: randomBytes,
305
+ randomBytes: nodejsRandomBytes,
292
306
  swap32(buffer) {
293
307
  return nodeJsByteUtils.toLocalBufferType(buffer).swap32();
294
308
  }
@@ -430,7 +444,11 @@ const webByteUtils = {
430
444
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
431
445
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
432
446
 
447
+ const bsonType = Symbol.for('@@mdb.bson.type');
433
448
  class BSONValue {
449
+ get [bsonType]() {
450
+ return this._bsontype;
451
+ }
434
452
  get [BSON_VERSION_SYMBOL]() {
435
453
  return BSON_MAJOR_VERSION;
436
454
  }
@@ -481,7 +499,7 @@ const NumberUtils = {
481
499
  source[offset + 1] * 256 +
482
500
  source[offset + 2] * 65536 +
483
501
  source[offset + 3] * 16777216);
484
- return (hi << BigInt(32)) + lo;
502
+ return (hi << 32n) + lo;
485
503
  },
486
504
  getFloat64LE: isBigEndian
487
505
  ? (source, offset) => {
@@ -527,7 +545,7 @@ const NumberUtils = {
527
545
  return 4;
528
546
  },
529
547
  setBigInt64LE(destination, offset, value) {
530
- const mask32bits = BigInt(0xffff_ffff);
548
+ const mask32bits = 0xffffffffn;
531
549
  let lo = Number(value & mask32bits);
532
550
  destination[offset] = lo;
533
551
  lo >>= 8;
@@ -536,7 +554,7 @@ const NumberUtils = {
536
554
  destination[offset + 2] = lo;
537
555
  lo >>= 8;
538
556
  destination[offset + 3] = lo;
539
- let hi = Number((value >> BigInt(32)) & mask32bits);
557
+ let hi = Number((value >> 32n) & mask32bits);
540
558
  destination[offset + 4] = hi;
541
559
  hi >>= 8;
542
560
  destination[offset + 5] = hi;
@@ -577,6 +595,27 @@ class Binary extends BSONValue {
577
595
  get _bsontype() {
578
596
  return 'Binary';
579
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;
580
619
  constructor(buffer, subType) {
581
620
  super();
582
621
  if (!(buffer == null) &&
@@ -836,24 +875,6 @@ class Binary extends BSONValue {
836
875
  return new this(bytes, Binary.SUBTYPE_VECTOR);
837
876
  }
838
877
  }
839
- Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
840
- Binary.BUFFER_SIZE = 256;
841
- Binary.SUBTYPE_DEFAULT = 0;
842
- Binary.SUBTYPE_FUNCTION = 1;
843
- Binary.SUBTYPE_BYTE_ARRAY = 2;
844
- Binary.SUBTYPE_UUID_OLD = 3;
845
- Binary.SUBTYPE_UUID = 4;
846
- Binary.SUBTYPE_MD5 = 5;
847
- Binary.SUBTYPE_ENCRYPTED = 6;
848
- Binary.SUBTYPE_COLUMN = 7;
849
- Binary.SUBTYPE_SENSITIVE = 8;
850
- Binary.SUBTYPE_VECTOR = 9;
851
- Binary.SUBTYPE_USER_DEFINED = 128;
852
- Binary.VECTOR_TYPE = Object.freeze({
853
- Int8: 0x03,
854
- Float32: 0x27,
855
- PackedBit: 0x10
856
- });
857
878
  function validateBinaryVector(vector) {
858
879
  if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
859
880
  return;
@@ -990,6 +1011,8 @@ class Code extends BSONValue {
990
1011
  get _bsontype() {
991
1012
  return 'Code';
992
1013
  }
1014
+ code;
1015
+ scope;
993
1016
  constructor(code, scope) {
994
1017
  super();
995
1018
  this.code = code.toString();
@@ -1035,6 +1058,10 @@ class DBRef extends BSONValue {
1035
1058
  get _bsontype() {
1036
1059
  return 'DBRef';
1037
1060
  }
1061
+ collection;
1062
+ oid;
1063
+ db;
1064
+ fields;
1038
1065
  constructor(collection, oid, db, fields) {
1039
1066
  super();
1040
1067
  const parts = collection.split('.');
@@ -1144,6 +1171,9 @@ class Long extends BSONValue {
1144
1171
  get __isLong__() {
1145
1172
  return true;
1146
1173
  }
1174
+ high;
1175
+ low;
1176
+ unsigned;
1147
1177
  constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
1148
1178
  super();
1149
1179
  const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
@@ -1157,6 +1187,15 @@ class Long extends BSONValue {
1157
1187
  this.high = res.high;
1158
1188
  this.unsigned = res.unsigned;
1159
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);
1160
1199
  static fromBits(lowBits, highBits, unsigned) {
1161
1200
  return new Long(lowBits, highBits, unsigned);
1162
1201
  }
@@ -1197,7 +1236,7 @@ class Long extends BSONValue {
1197
1236
  return Long.MAX_UNSIGNED_VALUE;
1198
1237
  }
1199
1238
  else {
1200
- if (value <= -9223372036854776e3)
1239
+ if (value <= -TWO_PWR_63_DBL)
1201
1240
  return Long.MIN_VALUE;
1202
1241
  if (value + 1 >= TWO_PWR_63_DBL)
1203
1242
  return Long.MAX_VALUE;
@@ -1207,8 +1246,8 @@ class Long extends BSONValue {
1207
1246
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1208
1247
  }
1209
1248
  static fromBigInt(value, unsigned) {
1210
- const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
1211
- const FROM_BIGINT_BIT_SHIFT = BigInt(32);
1249
+ const FROM_BIGINT_BIT_MASK = 0xffffffffn;
1250
+ const FROM_BIGINT_BIT_SHIFT = 32n;
1212
1251
  return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
1213
1252
  }
1214
1253
  static _fromString(str, unsigned, radix) {
@@ -1241,7 +1280,7 @@ class Long extends BSONValue {
1241
1280
  static fromStringStrict(str, unsignedOrRadix, radix) {
1242
1281
  let unsigned = false;
1243
1282
  if (typeof unsignedOrRadix === 'number') {
1244
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1283
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1245
1284
  }
1246
1285
  else {
1247
1286
  unsigned = !!unsignedOrRadix;
@@ -1263,7 +1302,7 @@ class Long extends BSONValue {
1263
1302
  static fromString(str, unsignedOrRadix, radix) {
1264
1303
  let unsigned = false;
1265
1304
  if (typeof unsignedOrRadix === 'number') {
1266
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1305
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1267
1306
  }
1268
1307
  else {
1269
1308
  unsigned = !!unsignedOrRadix;
@@ -1783,15 +1822,6 @@ class Long extends BSONValue {
1783
1822
  return `new Long(${longVal}${unsignedVal})`;
1784
1823
  }
1785
1824
  }
1786
- Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1787
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1788
- Long.ZERO = Long.fromInt(0);
1789
- Long.UZERO = Long.fromInt(0, true);
1790
- Long.ONE = Long.fromInt(1);
1791
- Long.UONE = Long.fromInt(1, true);
1792
- Long.NEG_ONE = Long.fromInt(-1);
1793
- Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1794
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1795
1825
 
1796
1826
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
1797
1827
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -1872,6 +1902,7 @@ class Decimal128 extends BSONValue {
1872
1902
  get _bsontype() {
1873
1903
  return 'Decimal128';
1874
1904
  }
1905
+ bytes;
1875
1906
  constructor(bytes) {
1876
1907
  super();
1877
1908
  if (typeof bytes === 'string') {
@@ -2347,6 +2378,7 @@ class Double extends BSONValue {
2347
2378
  get _bsontype() {
2348
2379
  return 'Double';
2349
2380
  }
2381
+ value;
2350
2382
  constructor(value) {
2351
2383
  super();
2352
2384
  if (value instanceof Number) {
@@ -2410,6 +2442,7 @@ class Int32 extends BSONValue {
2410
2442
  get _bsontype() {
2411
2443
  return 'Int32';
2412
2444
  }
2445
+ value;
2413
2446
  constructor(value) {
2414
2447
  super();
2415
2448
  if (value instanceof Number) {
@@ -2488,11 +2521,14 @@ class MinKey extends BSONValue {
2488
2521
  }
2489
2522
 
2490
2523
  let PROCESS_UNIQUE = null;
2491
- const __idCache = new WeakMap();
2492
2524
  class ObjectId extends BSONValue {
2493
2525
  get _bsontype() {
2494
2526
  return 'ObjectId';
2495
2527
  }
2528
+ static index = Math.floor(Math.random() * 0xffffff);
2529
+ static cacheHexString = false;
2530
+ buffer;
2531
+ #cachedHexString = null;
2496
2532
  constructor(inputId) {
2497
2533
  super();
2498
2534
  let workingId;
@@ -2510,8 +2546,8 @@ class ObjectId extends BSONValue {
2510
2546
  else {
2511
2547
  workingId = inputId;
2512
2548
  }
2513
- if (workingId == null || typeof workingId === 'number') {
2514
- this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2549
+ if (workingId == null) {
2550
+ this.buffer = ObjectId.generate();
2515
2551
  }
2516
2552
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2517
2553
  this.buffer = ByteUtils.toLocalBufferType(workingId);
@@ -2520,7 +2556,7 @@ class ObjectId extends BSONValue {
2520
2556
  if (ObjectId.validateHexString(workingId)) {
2521
2557
  this.buffer = ByteUtils.fromHex(workingId);
2522
2558
  if (ObjectId.cacheHexString) {
2523
- __idCache.set(this, workingId);
2559
+ this.#cachedHexString = workingId;
2524
2560
  }
2525
2561
  }
2526
2562
  else {
@@ -2537,7 +2573,7 @@ class ObjectId extends BSONValue {
2537
2573
  set id(value) {
2538
2574
  this.buffer = value;
2539
2575
  if (ObjectId.cacheHexString) {
2540
- __idCache.set(this, ByteUtils.toHex(value));
2576
+ this.#cachedHexString = ByteUtils.toHex(value);
2541
2577
  }
2542
2578
  }
2543
2579
  static validateHexString(string) {
@@ -2555,14 +2591,11 @@ class ObjectId extends BSONValue {
2555
2591
  return true;
2556
2592
  }
2557
2593
  toHexString() {
2558
- if (ObjectId.cacheHexString) {
2559
- const __id = __idCache.get(this);
2560
- if (__id)
2561
- return __id;
2562
- }
2594
+ if (this.#cachedHexString)
2595
+ return this.#cachedHexString.toLowerCase();
2563
2596
  const hexString = ByteUtils.toHex(this.id);
2564
2597
  if (ObjectId.cacheHexString) {
2565
- __idCache.set(this, hexString);
2598
+ this.#cachedHexString = hexString;
2566
2599
  }
2567
2600
  return hexString;
2568
2601
  }
@@ -2687,14 +2720,13 @@ class ObjectId extends BSONValue {
2687
2720
  return new ObjectId(doc.$oid);
2688
2721
  }
2689
2722
  isCached() {
2690
- return ObjectId.cacheHexString && __idCache.has(this);
2723
+ return ObjectId.cacheHexString && this.#cachedHexString != null;
2691
2724
  }
2692
2725
  inspect(depth, options, inspect) {
2693
2726
  inspect ??= defaultInspect;
2694
2727
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
2695
2728
  }
2696
2729
  }
2697
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
2698
2730
 
2699
2731
  function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
2700
2732
  let totalLength = 4 + 1;
@@ -2863,6 +2895,8 @@ class BSONRegExp extends BSONValue {
2863
2895
  get _bsontype() {
2864
2896
  return 'BSONRegExp';
2865
2897
  }
2898
+ pattern;
2899
+ options;
2866
2900
  constructor(pattern, options) {
2867
2901
  super();
2868
2902
  this.pattern = pattern;
@@ -2923,6 +2957,7 @@ class BSONSymbol extends BSONValue {
2923
2957
  get _bsontype() {
2924
2958
  return 'BSONSymbol';
2925
2959
  }
2960
+ value;
2926
2961
  constructor(value) {
2927
2962
  super();
2928
2963
  this.value = value;
@@ -2953,6 +2988,10 @@ class Timestamp extends LongWithoutOverridesClass {
2953
2988
  get _bsontype() {
2954
2989
  return 'Timestamp';
2955
2990
  }
2991
+ get [bsonType]() {
2992
+ return 'Timestamp';
2993
+ }
2994
+ static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2956
2995
  get i() {
2957
2996
  return this.low >>> 0;
2958
2997
  }
@@ -3032,7 +3071,6 @@ class Timestamp extends LongWithoutOverridesClass {
3032
3071
  return `new Timestamp({ t: ${t}, i: ${i} })`;
3033
3072
  }
3034
3073
  }
3035
- Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
3036
3074
 
3037
3075
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
3038
3076
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
@@ -4584,6 +4622,7 @@ var bson = /*#__PURE__*/Object.freeze({
4584
4622
  ObjectId: ObjectId,
4585
4623
  Timestamp: Timestamp,
4586
4624
  UUID: UUID,
4625
+ bsonType: bsonType,
4587
4626
  calculateObjectSize: calculateObjectSize,
4588
4627
  deserialize: deserialize,
4589
4628
  deserializeStream: deserializeStream,
@@ -4593,5 +4632,5 @@ var bson = /*#__PURE__*/Object.freeze({
4593
4632
  setInternalBufferSize: setInternalBufferSize
4594
4633
  });
4595
4634
 
4596
- 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 };
4597
4636
  //# sourceMappingURL=bson.node.mjs.map