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.rn.cjs CHANGED
@@ -47,7 +47,7 @@ function getStylizeFunction(options) {
47
47
  }
48
48
  }
49
49
 
50
- const BSON_MAJOR_VERSION = 6;
50
+ const BSON_MAJOR_VERSION = 7;
51
51
  const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
52
52
  const BSON_INT32_MAX = 0x7fffffff;
53
53
  const BSON_INT32_MIN = -0x80000000;
@@ -150,13 +150,13 @@ class BSONOffsetError extends BSONError {
150
150
  get name() {
151
151
  return 'BSONOffsetError';
152
152
  }
153
+ offset;
153
154
  constructor(message, offset, options) {
154
155
  super(`${message}. offset: ${offset}`, options);
155
156
  this.offset = offset;
156
157
  }
157
158
  }
158
159
 
159
- const { TextDecoder } = require('../vendor/text-encoding');
160
160
  let TextDecoderFatal;
161
161
  let TextDecoderNonFatal;
162
162
  function parseUtf8(buffer, start, end, fatal) {
@@ -227,11 +227,15 @@ function tryWriteBasicLatin(destination, source, offset) {
227
227
  function nodejsMathRandomBytes(byteLength) {
228
228
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
229
229
  }
230
+ function nodejsSecureRandomBytes(byteLength) {
231
+ return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
232
+ }
230
233
  const nodejsRandomBytes = (() => {
231
- try {
232
- return require('crypto').randomBytes;
234
+ const { crypto } = globalThis;
235
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
236
+ return nodejsSecureRandomBytes;
233
237
  }
234
- catch {
238
+ else {
235
239
  return nodejsMathRandomBytes;
236
240
  }
237
241
  })();
@@ -314,8 +318,6 @@ const nodeJsByteUtils = {
314
318
  }
315
319
  };
316
320
 
317
- const { TextEncoder } = require('../vendor/text-encoding');
318
- const { encode: btoa, decode: atob } = require('../vendor/base64');
319
321
  function isReactNative() {
320
322
  const { navigator } = globalThis;
321
323
  return typeof navigator === 'object' && navigator.product === 'ReactNative';
@@ -452,7 +454,11 @@ const webByteUtils = {
452
454
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
453
455
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
454
456
 
457
+ const bsonType = Symbol.for('@@mdb.bson.type');
455
458
  class BSONValue {
459
+ get [bsonType]() {
460
+ return this._bsontype;
461
+ }
456
462
  get [BSON_VERSION_SYMBOL]() {
457
463
  return BSON_MAJOR_VERSION;
458
464
  }
@@ -503,7 +509,7 @@ const NumberUtils = {
503
509
  source[offset + 1] * 256 +
504
510
  source[offset + 2] * 65536 +
505
511
  source[offset + 3] * 16777216);
506
- return (hi << BigInt(32)) + lo;
512
+ return (hi << 32n) + lo;
507
513
  },
508
514
  getFloat64LE: isBigEndian
509
515
  ? (source, offset) => {
@@ -549,7 +555,7 @@ const NumberUtils = {
549
555
  return 4;
550
556
  },
551
557
  setBigInt64LE(destination, offset, value) {
552
- const mask32bits = BigInt(0xffff_ffff);
558
+ const mask32bits = 0xffffffffn;
553
559
  let lo = Number(value & mask32bits);
554
560
  destination[offset] = lo;
555
561
  lo >>= 8;
@@ -558,7 +564,7 @@ const NumberUtils = {
558
564
  destination[offset + 2] = lo;
559
565
  lo >>= 8;
560
566
  destination[offset + 3] = lo;
561
- let hi = Number((value >> BigInt(32)) & mask32bits);
567
+ let hi = Number((value >> 32n) & mask32bits);
562
568
  destination[offset + 4] = hi;
563
569
  hi >>= 8;
564
570
  destination[offset + 5] = hi;
@@ -599,6 +605,27 @@ class Binary extends BSONValue {
599
605
  get _bsontype() {
600
606
  return 'Binary';
601
607
  }
608
+ static BSON_BINARY_SUBTYPE_DEFAULT = 0;
609
+ static BUFFER_SIZE = 256;
610
+ static SUBTYPE_DEFAULT = 0;
611
+ static SUBTYPE_FUNCTION = 1;
612
+ static SUBTYPE_BYTE_ARRAY = 2;
613
+ static SUBTYPE_UUID_OLD = 3;
614
+ static SUBTYPE_UUID = 4;
615
+ static SUBTYPE_MD5 = 5;
616
+ static SUBTYPE_ENCRYPTED = 6;
617
+ static SUBTYPE_COLUMN = 7;
618
+ static SUBTYPE_SENSITIVE = 8;
619
+ static SUBTYPE_VECTOR = 9;
620
+ static SUBTYPE_USER_DEFINED = 128;
621
+ static VECTOR_TYPE = Object.freeze({
622
+ Int8: 0x03,
623
+ Float32: 0x27,
624
+ PackedBit: 0x10
625
+ });
626
+ buffer;
627
+ sub_type;
628
+ position;
602
629
  constructor(buffer, subType) {
603
630
  super();
604
631
  if (!(buffer == null) &&
@@ -858,24 +885,6 @@ class Binary extends BSONValue {
858
885
  return new this(bytes, Binary.SUBTYPE_VECTOR);
859
886
  }
860
887
  }
861
- Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
862
- Binary.BUFFER_SIZE = 256;
863
- Binary.SUBTYPE_DEFAULT = 0;
864
- Binary.SUBTYPE_FUNCTION = 1;
865
- Binary.SUBTYPE_BYTE_ARRAY = 2;
866
- Binary.SUBTYPE_UUID_OLD = 3;
867
- Binary.SUBTYPE_UUID = 4;
868
- Binary.SUBTYPE_MD5 = 5;
869
- Binary.SUBTYPE_ENCRYPTED = 6;
870
- Binary.SUBTYPE_COLUMN = 7;
871
- Binary.SUBTYPE_SENSITIVE = 8;
872
- Binary.SUBTYPE_VECTOR = 9;
873
- Binary.SUBTYPE_USER_DEFINED = 128;
874
- Binary.VECTOR_TYPE = Object.freeze({
875
- Int8: 0x03,
876
- Float32: 0x27,
877
- PackedBit: 0x10
878
- });
879
888
  function validateBinaryVector(vector) {
880
889
  if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
881
890
  return;
@@ -1012,6 +1021,8 @@ class Code extends BSONValue {
1012
1021
  get _bsontype() {
1013
1022
  return 'Code';
1014
1023
  }
1024
+ code;
1025
+ scope;
1015
1026
  constructor(code, scope) {
1016
1027
  super();
1017
1028
  this.code = code.toString();
@@ -1057,6 +1068,10 @@ class DBRef extends BSONValue {
1057
1068
  get _bsontype() {
1058
1069
  return 'DBRef';
1059
1070
  }
1071
+ collection;
1072
+ oid;
1073
+ db;
1074
+ fields;
1060
1075
  constructor(collection, oid, db, fields) {
1061
1076
  super();
1062
1077
  const parts = collection.split('.');
@@ -1166,6 +1181,9 @@ class Long extends BSONValue {
1166
1181
  get __isLong__() {
1167
1182
  return true;
1168
1183
  }
1184
+ high;
1185
+ low;
1186
+ unsigned;
1169
1187
  constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
1170
1188
  super();
1171
1189
  const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
@@ -1179,6 +1197,15 @@ class Long extends BSONValue {
1179
1197
  this.high = res.high;
1180
1198
  this.unsigned = res.unsigned;
1181
1199
  }
1200
+ static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1201
+ static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1202
+ static ZERO = Long.fromInt(0);
1203
+ static UZERO = Long.fromInt(0, true);
1204
+ static ONE = Long.fromInt(1);
1205
+ static UONE = Long.fromInt(1, true);
1206
+ static NEG_ONE = Long.fromInt(-1);
1207
+ static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1208
+ static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1182
1209
  static fromBits(lowBits, highBits, unsigned) {
1183
1210
  return new Long(lowBits, highBits, unsigned);
1184
1211
  }
@@ -1229,8 +1256,8 @@ class Long extends BSONValue {
1229
1256
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1230
1257
  }
1231
1258
  static fromBigInt(value, unsigned) {
1232
- const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
1233
- const FROM_BIGINT_BIT_SHIFT = BigInt(32);
1259
+ const FROM_BIGINT_BIT_MASK = 0xffffffffn;
1260
+ const FROM_BIGINT_BIT_SHIFT = 32n;
1234
1261
  return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
1235
1262
  }
1236
1263
  static _fromString(str, unsigned, radix) {
@@ -1263,7 +1290,7 @@ class Long extends BSONValue {
1263
1290
  static fromStringStrict(str, unsignedOrRadix, radix) {
1264
1291
  let unsigned = false;
1265
1292
  if (typeof unsignedOrRadix === 'number') {
1266
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1293
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1267
1294
  }
1268
1295
  else {
1269
1296
  unsigned = !!unsignedOrRadix;
@@ -1285,7 +1312,7 @@ class Long extends BSONValue {
1285
1312
  static fromString(str, unsignedOrRadix, radix) {
1286
1313
  let unsigned = false;
1287
1314
  if (typeof unsignedOrRadix === 'number') {
1288
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1315
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1289
1316
  }
1290
1317
  else {
1291
1318
  unsigned = !!unsignedOrRadix;
@@ -1805,15 +1832,6 @@ class Long extends BSONValue {
1805
1832
  return `new Long(${longVal}${unsignedVal})`;
1806
1833
  }
1807
1834
  }
1808
- Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1809
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1810
- Long.ZERO = Long.fromInt(0);
1811
- Long.UZERO = Long.fromInt(0, true);
1812
- Long.ONE = Long.fromInt(1);
1813
- Long.UONE = Long.fromInt(1, true);
1814
- Long.NEG_ONE = Long.fromInt(-1);
1815
- Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1816
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1817
1835
 
1818
1836
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
1819
1837
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -1894,6 +1912,7 @@ class Decimal128 extends BSONValue {
1894
1912
  get _bsontype() {
1895
1913
  return 'Decimal128';
1896
1914
  }
1915
+ bytes;
1897
1916
  constructor(bytes) {
1898
1917
  super();
1899
1918
  if (typeof bytes === 'string') {
@@ -2369,6 +2388,7 @@ class Double extends BSONValue {
2369
2388
  get _bsontype() {
2370
2389
  return 'Double';
2371
2390
  }
2391
+ value;
2372
2392
  constructor(value) {
2373
2393
  super();
2374
2394
  if (value instanceof Number) {
@@ -2432,6 +2452,7 @@ class Int32 extends BSONValue {
2432
2452
  get _bsontype() {
2433
2453
  return 'Int32';
2434
2454
  }
2455
+ value;
2435
2456
  constructor(value) {
2436
2457
  super();
2437
2458
  if (value instanceof Number) {
@@ -2510,11 +2531,14 @@ class MinKey extends BSONValue {
2510
2531
  }
2511
2532
 
2512
2533
  let PROCESS_UNIQUE = null;
2513
- const __idCache = new WeakMap();
2514
2534
  class ObjectId extends BSONValue {
2515
2535
  get _bsontype() {
2516
2536
  return 'ObjectId';
2517
2537
  }
2538
+ static index = Math.floor(Math.random() * 0xffffff);
2539
+ static cacheHexString = false;
2540
+ buffer;
2541
+ #cachedHexString = null;
2518
2542
  constructor(inputId) {
2519
2543
  super();
2520
2544
  let workingId;
@@ -2532,8 +2556,8 @@ class ObjectId extends BSONValue {
2532
2556
  else {
2533
2557
  workingId = inputId;
2534
2558
  }
2535
- if (workingId == null || typeof workingId === 'number') {
2536
- this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2559
+ if (workingId == null) {
2560
+ this.buffer = ObjectId.generate();
2537
2561
  }
2538
2562
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2539
2563
  this.buffer = ByteUtils.toLocalBufferType(workingId);
@@ -2542,7 +2566,7 @@ class ObjectId extends BSONValue {
2542
2566
  if (ObjectId.validateHexString(workingId)) {
2543
2567
  this.buffer = ByteUtils.fromHex(workingId);
2544
2568
  if (ObjectId.cacheHexString) {
2545
- __idCache.set(this, workingId);
2569
+ this.#cachedHexString = workingId;
2546
2570
  }
2547
2571
  }
2548
2572
  else {
@@ -2559,7 +2583,7 @@ class ObjectId extends BSONValue {
2559
2583
  set id(value) {
2560
2584
  this.buffer = value;
2561
2585
  if (ObjectId.cacheHexString) {
2562
- __idCache.set(this, ByteUtils.toHex(value));
2586
+ this.#cachedHexString = ByteUtils.toHex(value);
2563
2587
  }
2564
2588
  }
2565
2589
  static validateHexString(string) {
@@ -2577,14 +2601,11 @@ class ObjectId extends BSONValue {
2577
2601
  return true;
2578
2602
  }
2579
2603
  toHexString() {
2580
- if (ObjectId.cacheHexString) {
2581
- const __id = __idCache.get(this);
2582
- if (__id)
2583
- return __id;
2584
- }
2604
+ if (this.#cachedHexString)
2605
+ return this.#cachedHexString.toLowerCase();
2585
2606
  const hexString = ByteUtils.toHex(this.id);
2586
2607
  if (ObjectId.cacheHexString) {
2587
- __idCache.set(this, hexString);
2608
+ this.#cachedHexString = hexString;
2588
2609
  }
2589
2610
  return hexString;
2590
2611
  }
@@ -2709,14 +2730,13 @@ class ObjectId extends BSONValue {
2709
2730
  return new ObjectId(doc.$oid);
2710
2731
  }
2711
2732
  isCached() {
2712
- return ObjectId.cacheHexString && __idCache.has(this);
2733
+ return ObjectId.cacheHexString && this.#cachedHexString != null;
2713
2734
  }
2714
2735
  inspect(depth, options, inspect) {
2715
2736
  inspect ??= defaultInspect;
2716
2737
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
2717
2738
  }
2718
2739
  }
2719
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
2720
2740
 
2721
2741
  function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
2722
2742
  let totalLength = 4 + 1;
@@ -2886,6 +2906,8 @@ class BSONRegExp extends BSONValue {
2886
2906
  get _bsontype() {
2887
2907
  return 'BSONRegExp';
2888
2908
  }
2909
+ pattern;
2910
+ options;
2889
2911
  constructor(pattern, options) {
2890
2912
  super();
2891
2913
  this.pattern = pattern;
@@ -2946,6 +2968,7 @@ class BSONSymbol extends BSONValue {
2946
2968
  get _bsontype() {
2947
2969
  return 'BSONSymbol';
2948
2970
  }
2971
+ value;
2949
2972
  constructor(value) {
2950
2973
  super();
2951
2974
  this.value = value;
@@ -2976,6 +2999,10 @@ class Timestamp extends LongWithoutOverridesClass {
2976
2999
  get _bsontype() {
2977
3000
  return 'Timestamp';
2978
3001
  }
3002
+ get [bsonType]() {
3003
+ return 'Timestamp';
3004
+ }
3005
+ static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2979
3006
  get i() {
2980
3007
  return this.low >>> 0;
2981
3008
  }
@@ -3055,7 +3082,6 @@ class Timestamp extends LongWithoutOverridesClass {
3055
3082
  return `new Timestamp({ t: ${t}, i: ${i} })`;
3056
3083
  }
3057
3084
  }
3058
- Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
3059
3085
 
3060
3086
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
3061
3087
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
@@ -4608,6 +4634,7 @@ var bson = /*#__PURE__*/Object.freeze({
4608
4634
  ObjectId: ObjectId,
4609
4635
  Timestamp: Timestamp,
4610
4636
  UUID: UUID,
4637
+ bsonType: bsonType,
4611
4638
  calculateObjectSize: calculateObjectSize,
4612
4639
  deserialize: deserialize,
4613
4640
  deserializeStream: deserializeStream,
@@ -4639,6 +4666,7 @@ exports.MinKey = MinKey;
4639
4666
  exports.ObjectId = ObjectId;
4640
4667
  exports.Timestamp = Timestamp;
4641
4668
  exports.UUID = UUID;
4669
+ exports.bsonType = bsonType;
4642
4670
  exports.calculateObjectSize = calculateObjectSize;
4643
4671
  exports.deserialize = deserialize;
4644
4672
  exports.deserializeStream = deserializeStream;