bson 5.2.0 → 5.4.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.
@@ -1,6 +1,22 @@
1
1
  var BSON = (function (exports) {
2
2
  'use strict';
3
3
 
4
+ function isAnyArrayBuffer(value) {
5
+ return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
6
+ }
7
+ function isUint8Array(value) {
8
+ return Object.prototype.toString.call(value) === '[object Uint8Array]';
9
+ }
10
+ function isRegExp(d) {
11
+ return Object.prototype.toString.call(d) === '[object RegExp]';
12
+ }
13
+ function isMap(d) {
14
+ return Object.prototype.toString.call(d) === '[object Map]';
15
+ }
16
+ function isDate(d) {
17
+ return Object.prototype.toString.call(d) === '[object Date]';
18
+ }
19
+
4
20
  const BSON_MAJOR_VERSION = 5;
5
21
  const BSON_INT32_MAX = 0x7fffffff;
6
22
  const BSON_INT32_MIN = -0x80000000;
@@ -150,8 +166,8 @@ const nodeJsByteUtils = {
150
166
  fromUTF8(text) {
151
167
  return Buffer.from(text, 'utf8');
152
168
  },
153
- toUTF8(buffer) {
154
- return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8');
169
+ toUTF8(buffer, start, end) {
170
+ return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end);
155
171
  },
156
172
  utf8ByteLength(input) {
157
173
  return Buffer.byteLength(input, 'utf8');
@@ -261,8 +277,8 @@ const webByteUtils = {
261
277
  fromUTF8(text) {
262
278
  return new TextEncoder().encode(text);
263
279
  },
264
- toUTF8(uint8array) {
265
- return new TextDecoder('utf8', { fatal: false }).decode(uint8array);
280
+ toUTF8(uint8array, start, end) {
281
+ return new TextDecoder('utf8', { fatal: false }).decode(uint8array.slice(start, end));
266
282
  },
267
283
  utf8ByteLength(input) {
268
284
  return webByteUtils.fromUTF8(input).byteLength;
@@ -283,44 +299,6 @@ class BSONDataView extends DataView {
283
299
  }
284
300
  }
285
301
 
286
- const VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
287
- const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
288
- const uuidHexStringToBuffer = (hexString) => {
289
- if (!uuidValidateString(hexString)) {
290
- throw new BSONError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
291
- }
292
- const sanitizedHexString = hexString.replace(/-/g, '');
293
- return ByteUtils.fromHex(sanitizedHexString);
294
- };
295
- function bufferToUuidHexString(buffer, includeDashes = true) {
296
- if (includeDashes) {
297
- return [
298
- ByteUtils.toHex(buffer.subarray(0, 4)),
299
- ByteUtils.toHex(buffer.subarray(4, 6)),
300
- ByteUtils.toHex(buffer.subarray(6, 8)),
301
- ByteUtils.toHex(buffer.subarray(8, 10)),
302
- ByteUtils.toHex(buffer.subarray(10, 16))
303
- ].join('-');
304
- }
305
- return ByteUtils.toHex(buffer);
306
- }
307
-
308
- function isAnyArrayBuffer(value) {
309
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
310
- }
311
- function isUint8Array(value) {
312
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
313
- }
314
- function isRegExp(d) {
315
- return Object.prototype.toString.call(d) === '[object RegExp]';
316
- }
317
- function isMap(d) {
318
- return Object.prototype.toString.call(d) === '[object Map]';
319
- }
320
- function isDate(d) {
321
- return Object.prototype.toString.call(d) === '[object Date]';
322
- }
323
-
324
302
  class BSONValue {
325
303
  get [Symbol.for('@@mdb.bson.version')]() {
326
304
  return BSON_MAJOR_VERSION;
@@ -432,8 +410,8 @@ class Binary extends BSONValue {
432
410
  if (encoding === 'base64')
433
411
  return ByteUtils.toBase64(this.buffer);
434
412
  if (encoding === 'utf8' || encoding === 'utf-8')
435
- return ByteUtils.toUTF8(this.buffer);
436
- return ByteUtils.toUTF8(this.buffer);
413
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
414
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
437
415
  }
438
416
  toExtendedJSON(options) {
439
417
  options = options || {};
@@ -482,7 +460,7 @@ class Binary extends BSONValue {
482
460
  }
483
461
  else if ('$uuid' in doc) {
484
462
  type = 4;
485
- data = uuidHexStringToBuffer(doc.$uuid);
463
+ data = UUID.bytesFromString(doc.$uuid);
486
464
  }
487
465
  if (!data) {
488
466
  throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
@@ -509,47 +487,45 @@ Binary.SUBTYPE_ENCRYPTED = 6;
509
487
  Binary.SUBTYPE_COLUMN = 7;
510
488
  Binary.SUBTYPE_USER_DEFINED = 128;
511
489
  const UUID_BYTE_LENGTH = 16;
490
+ const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
491
+ const UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
512
492
  class UUID extends Binary {
513
493
  constructor(input) {
514
494
  let bytes;
515
- let hexStr;
516
495
  if (input == null) {
517
496
  bytes = UUID.generate();
518
497
  }
519
498
  else if (input instanceof UUID) {
520
499
  bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
521
- hexStr = input.__id;
522
500
  }
523
501
  else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
524
502
  bytes = ByteUtils.toLocalBufferType(input);
525
503
  }
526
504
  else if (typeof input === 'string') {
527
- bytes = uuidHexStringToBuffer(input);
505
+ bytes = UUID.bytesFromString(input);
528
506
  }
529
507
  else {
530
508
  throw new BSONError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
531
509
  }
532
510
  super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
533
- this.__id = hexStr;
534
511
  }
535
512
  get id() {
536
513
  return this.buffer;
537
514
  }
538
515
  set id(value) {
539
516
  this.buffer = value;
540
- if (UUID.cacheHexString) {
541
- this.__id = bufferToUuidHexString(value);
542
- }
543
517
  }
544
518
  toHexString(includeDashes = true) {
545
- if (UUID.cacheHexString && this.__id) {
546
- return this.__id;
547
- }
548
- const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
549
- if (UUID.cacheHexString) {
550
- this.__id = uuidHexString;
519
+ if (includeDashes) {
520
+ return [
521
+ ByteUtils.toHex(this.buffer.subarray(0, 4)),
522
+ ByteUtils.toHex(this.buffer.subarray(4, 6)),
523
+ ByteUtils.toHex(this.buffer.subarray(6, 8)),
524
+ ByteUtils.toHex(this.buffer.subarray(8, 10)),
525
+ ByteUtils.toHex(this.buffer.subarray(10, 16))
526
+ ].join('-');
551
527
  }
552
- return uuidHexString;
528
+ return ByteUtils.toHex(this.buffer);
553
529
  }
554
530
  toString(encoding) {
555
531
  if (encoding === 'hex')
@@ -588,27 +564,32 @@ class UUID extends Binary {
588
564
  if (!input) {
589
565
  return false;
590
566
  }
591
- if (input instanceof UUID) {
592
- return true;
593
- }
594
567
  if (typeof input === 'string') {
595
- return uuidValidateString(input);
568
+ return UUID.isValidUUIDString(input);
596
569
  }
597
570
  if (isUint8Array(input)) {
598
- if (input.byteLength !== UUID_BYTE_LENGTH) {
599
- return false;
600
- }
601
- return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
571
+ return input.byteLength === UUID_BYTE_LENGTH;
602
572
  }
603
- return false;
573
+ return (input._bsontype === 'Binary' &&
574
+ input.sub_type === this.SUBTYPE_UUID &&
575
+ input.buffer.byteLength === 16);
604
576
  }
605
577
  static createFromHexString(hexString) {
606
- const buffer = uuidHexStringToBuffer(hexString);
578
+ const buffer = UUID.bytesFromString(hexString);
607
579
  return new UUID(buffer);
608
580
  }
609
581
  static createFromBase64(base64) {
610
582
  return new UUID(ByteUtils.fromBase64(base64));
611
583
  }
584
+ static bytesFromString(representation) {
585
+ if (!UUID.isValidUUIDString(representation)) {
586
+ throw new BSONError('UUID string representation must be 32 hex digits or canonical hyphenated representation');
587
+ }
588
+ return ByteUtils.fromHex(representation.replace(/-/g, ''));
589
+ }
590
+ static isValidUUIDString(representation) {
591
+ return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
592
+ }
612
593
  [Symbol.for('nodejs.util.inspect.custom')]() {
613
594
  return this.inspect();
614
595
  }
@@ -616,6 +597,7 @@ class UUID extends Binary {
616
597
  return `new UUID("${this.toHexString()}")`;
617
598
  }
618
599
  }
600
+ UUID.cacheHexString = false;
619
601
 
620
602
  class Code extends BSONValue {
621
603
  get _bsontype() {
@@ -2435,19 +2417,21 @@ class Timestamp extends LongWithoutOverridesClass {
2435
2417
  if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
2436
2418
  throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
2437
2419
  }
2438
- if (low.t < 0) {
2420
+ const t = Number(low.t);
2421
+ const i = Number(low.i);
2422
+ if (t < 0 || Number.isNaN(t)) {
2439
2423
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
2440
2424
  }
2441
- if (low.i < 0) {
2425
+ if (i < 0 || Number.isNaN(i)) {
2442
2426
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2443
2427
  }
2444
- if (low.t > 4294967295) {
2428
+ if (t > 4294967295) {
2445
2429
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2446
2430
  }
2447
- if (low.i > 4294967295) {
2431
+ if (i > 4294967295) {
2448
2432
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2449
2433
  }
2450
- super(low.i.valueOf(), low.t.valueOf(), true);
2434
+ super(i, t, true);
2451
2435
  }
2452
2436
  else {
2453
2437
  throw new BSONError('A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }');
@@ -2619,7 +2603,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2619
2603
  }
2620
2604
  if (i >= buffer.byteLength)
2621
2605
  throw new BSONError('Bad BSON Document: illegal CString');
2622
- const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer.subarray(index, i));
2606
+ const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i);
2623
2607
  let shouldValidateKey = true;
2624
2608
  if (globalUTFValidation || utf8KeysSet.has(name)) {
2625
2609
  shouldValidateKey = validationSetting;
@@ -2792,7 +2776,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2792
2776
  }
2793
2777
  else {
2794
2778
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2795
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2779
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2796
2780
  value = value.toUUID();
2797
2781
  }
2798
2782
  }
@@ -2818,11 +2802,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
2818
2802
  if (promoteBuffers && promoteValues) {
2819
2803
  value = _buffer;
2820
2804
  }
2821
- else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2822
- value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
2823
- }
2824
2805
  else {
2825
2806
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2807
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2808
+ value = value.toUUID();
2809
+ }
2826
2810
  }
2827
2811
  }
2828
2812
  index = index + binarySize;
@@ -2834,7 +2818,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2834
2818
  }
2835
2819
  if (i >= buffer.length)
2836
2820
  throw new BSONError('Bad BSON Document: illegal CString');
2837
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2821
+ const source = ByteUtils.toUTF8(buffer, index, i);
2838
2822
  index = i + 1;
2839
2823
  i = index;
2840
2824
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2842,7 +2826,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2842
2826
  }
2843
2827
  if (i >= buffer.length)
2844
2828
  throw new BSONError('Bad BSON Document: illegal CString');
2845
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2829
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2846
2830
  index = i + 1;
2847
2831
  const optionsArray = new Array(regExpOptions.length);
2848
2832
  for (i = 0; i < regExpOptions.length; i++) {
@@ -2867,7 +2851,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2867
2851
  }
2868
2852
  if (i >= buffer.length)
2869
2853
  throw new BSONError('Bad BSON Document: illegal CString');
2870
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2854
+ const source = ByteUtils.toUTF8(buffer, index, i);
2871
2855
  index = i + 1;
2872
2856
  i = index;
2873
2857
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2875,7 +2859,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2875
2859
  }
2876
2860
  if (i >= buffer.length)
2877
2861
  throw new BSONError('Bad BSON Document: illegal CString');
2878
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2862
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2879
2863
  index = i + 1;
2880
2864
  value = new BSONRegExp(source, regExpOptions);
2881
2865
  }
@@ -2972,7 +2956,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2972
2956
  throw new BSONError('Invalid UTF-8 string in BSON document');
2973
2957
  }
2974
2958
  }
2975
- const namespace = ByteUtils.toUTF8(buffer.subarray(index, index + stringSize - 1));
2959
+ const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1);
2976
2960
  index = index + stringSize;
2977
2961
  const oidBuffer = ByteUtils.allocate(12);
2978
2962
  oidBuffer.set(buffer.subarray(index, index + 12), 0);
@@ -3012,7 +2996,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
3012
2996
  return object;
3013
2997
  }
3014
2998
  function getValidatedString(buffer, start, end, shouldValidateUtf8) {
3015
- const value = ByteUtils.toUTF8(buffer.subarray(start, end));
2999
+ const value = ByteUtils.toUTF8(buffer, start, end);
3016
3000
  if (shouldValidateUtf8) {
3017
3001
  for (let i = 0; i < value.length; i++) {
3018
3002
  if (value.charCodeAt(i) === 0xfffd) {
@@ -4054,32 +4038,32 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4054
4038
 
4055
4039
  var bson = /*#__PURE__*/Object.freeze({
4056
4040
  __proto__: null,
4057
- Code: Code,
4041
+ BSONError: BSONError,
4042
+ BSONRegExp: BSONRegExp,
4043
+ BSONRuntimeError: BSONRuntimeError,
4058
4044
  BSONSymbol: BSONSymbol,
4059
- DBRef: DBRef,
4045
+ BSONType: BSONType,
4046
+ BSONValue: BSONValue,
4047
+ BSONVersionError: BSONVersionError,
4060
4048
  Binary: Binary,
4061
- ObjectId: ObjectId,
4062
- UUID: UUID,
4063
- Long: Long,
4064
- Timestamp: Timestamp,
4049
+ Code: Code,
4050
+ DBRef: DBRef,
4051
+ Decimal128: Decimal128,
4065
4052
  Double: Double,
4053
+ EJSON: EJSON,
4066
4054
  Int32: Int32,
4067
- MinKey: MinKey,
4055
+ Long: Long,
4068
4056
  MaxKey: MaxKey,
4069
- BSONRegExp: BSONRegExp,
4070
- Decimal128: Decimal128,
4071
- setInternalBufferSize: setInternalBufferSize,
4072
- serialize: serialize,
4073
- serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4074
- deserialize: deserialize,
4057
+ MinKey: MinKey,
4058
+ ObjectId: ObjectId,
4059
+ Timestamp: Timestamp,
4060
+ UUID: UUID,
4075
4061
  calculateObjectSize: calculateObjectSize,
4062
+ deserialize: deserialize,
4076
4063
  deserializeStream: deserializeStream,
4077
- BSONValue: BSONValue,
4078
- BSONError: BSONError,
4079
- BSONVersionError: BSONVersionError,
4080
- BSONRuntimeError: BSONRuntimeError,
4081
- BSONType: BSONType,
4082
- EJSON: EJSON
4064
+ serialize: serialize,
4065
+ serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4066
+ setInternalBufferSize: setInternalBufferSize
4083
4067
  });
4084
4068
 
4085
4069
  exports.BSON = bson;