bson 5.2.0 → 5.3.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.
Files changed (57) hide show
  1. package/README.md +3 -126
  2. package/bson.d.ts +71 -22
  3. package/lib/binary.d.ts +182 -0
  4. package/lib/binary.d.ts.map +1 -0
  5. package/lib/bson.bundle.js +68 -86
  6. package/lib/bson.bundle.js.map +1 -1
  7. package/lib/bson.cjs +68 -86
  8. package/lib/bson.cjs.map +1 -1
  9. package/lib/bson.d.ts +97 -0
  10. package/lib/bson.d.ts.map +1 -0
  11. package/lib/bson.mjs +68 -86
  12. package/lib/bson.mjs.map +1 -1
  13. package/lib/bson_value.d.ts +10 -0
  14. package/lib/bson_value.d.ts.map +1 -0
  15. package/lib/code.d.ts +32 -0
  16. package/lib/code.d.ts.map +1 -0
  17. package/lib/constants.d.ts +107 -0
  18. package/lib/constants.d.ts.map +1 -0
  19. package/lib/db_ref.d.ts +40 -0
  20. package/lib/db_ref.d.ts.map +1 -0
  21. package/lib/decimal128.d.ts +34 -0
  22. package/lib/decimal128.d.ts.map +1 -0
  23. package/lib/double.d.ts +35 -0
  24. package/lib/double.d.ts.map +1 -0
  25. package/lib/error.d.ts +50 -0
  26. package/lib/error.d.ts.map +1 -0
  27. package/lib/extended_json.d.ts +82 -0
  28. package/lib/extended_json.d.ts.map +1 -0
  29. package/lib/index.d.ts +4 -0
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/int_32.d.ts +35 -0
  32. package/lib/int_32.d.ts.map +1 -0
  33. package/lib/long.d.ts +323 -0
  34. package/lib/long.d.ts.map +1 -0
  35. package/lib/max_key.d.ts +19 -0
  36. package/lib/max_key.d.ts.map +1 -0
  37. package/lib/min_key.d.ts +19 -0
  38. package/lib/min_key.d.ts.map +1 -0
  39. package/lib/objectid.d.ts +96 -0
  40. package/lib/objectid.d.ts.map +1 -0
  41. package/lib/regexp.d.ts +36 -0
  42. package/lib/regexp.d.ts.map +1 -0
  43. package/lib/symbol.d.ts +28 -0
  44. package/lib/symbol.d.ts.map +1 -0
  45. package/lib/timestamp.d.ts +66 -0
  46. package/lib/timestamp.d.ts.map +1 -0
  47. package/lib/validate_utf8.d.ts +10 -0
  48. package/lib/validate_utf8.d.ts.map +1 -0
  49. package/package.json +20 -20
  50. package/src/binary.ts +49 -41
  51. package/src/bson.ts +1 -1
  52. package/src/constants.ts +1 -1
  53. package/src/extended_json.ts +11 -3
  54. package/src/parser/deserializer.ts +37 -12
  55. package/src/parser/serializer.ts +17 -4
  56. package/src/timestamp.ts +1 -1
  57. package/src/uuid_utils.ts +0 -33
package/lib/bson.cjs CHANGED
@@ -1,5 +1,21 @@
1
1
  'use strict';
2
2
 
3
+ function isAnyArrayBuffer(value) {
4
+ return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
5
+ }
6
+ function isUint8Array(value) {
7
+ return Object.prototype.toString.call(value) === '[object Uint8Array]';
8
+ }
9
+ function isRegExp(d) {
10
+ return Object.prototype.toString.call(d) === '[object RegExp]';
11
+ }
12
+ function isMap(d) {
13
+ return Object.prototype.toString.call(d) === '[object Map]';
14
+ }
15
+ function isDate(d) {
16
+ return Object.prototype.toString.call(d) === '[object Date]';
17
+ }
18
+
3
19
  const BSON_MAJOR_VERSION = 5;
4
20
  const BSON_INT32_MAX = 0x7fffffff;
5
21
  const BSON_INT32_MIN = -0x80000000;
@@ -282,44 +298,6 @@ class BSONDataView extends DataView {
282
298
  }
283
299
  }
284
300
 
285
- 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;
286
- const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
287
- const uuidHexStringToBuffer = (hexString) => {
288
- if (!uuidValidateString(hexString)) {
289
- 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".');
290
- }
291
- const sanitizedHexString = hexString.replace(/-/g, '');
292
- return ByteUtils.fromHex(sanitizedHexString);
293
- };
294
- function bufferToUuidHexString(buffer, includeDashes = true) {
295
- if (includeDashes) {
296
- return [
297
- ByteUtils.toHex(buffer.subarray(0, 4)),
298
- ByteUtils.toHex(buffer.subarray(4, 6)),
299
- ByteUtils.toHex(buffer.subarray(6, 8)),
300
- ByteUtils.toHex(buffer.subarray(8, 10)),
301
- ByteUtils.toHex(buffer.subarray(10, 16))
302
- ].join('-');
303
- }
304
- return ByteUtils.toHex(buffer);
305
- }
306
-
307
- function isAnyArrayBuffer(value) {
308
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
309
- }
310
- function isUint8Array(value) {
311
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
312
- }
313
- function isRegExp(d) {
314
- return Object.prototype.toString.call(d) === '[object RegExp]';
315
- }
316
- function isMap(d) {
317
- return Object.prototype.toString.call(d) === '[object Map]';
318
- }
319
- function isDate(d) {
320
- return Object.prototype.toString.call(d) === '[object Date]';
321
- }
322
-
323
301
  class BSONValue {
324
302
  get [Symbol.for('@@mdb.bson.version')]() {
325
303
  return BSON_MAJOR_VERSION;
@@ -481,7 +459,7 @@ class Binary extends BSONValue {
481
459
  }
482
460
  else if ('$uuid' in doc) {
483
461
  type = 4;
484
- data = uuidHexStringToBuffer(doc.$uuid);
462
+ data = UUID.bytesFromString(doc.$uuid);
485
463
  }
486
464
  if (!data) {
487
465
  throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
@@ -508,47 +486,45 @@ Binary.SUBTYPE_ENCRYPTED = 6;
508
486
  Binary.SUBTYPE_COLUMN = 7;
509
487
  Binary.SUBTYPE_USER_DEFINED = 128;
510
488
  const UUID_BYTE_LENGTH = 16;
489
+ const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
490
+ 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;
511
491
  class UUID extends Binary {
512
492
  constructor(input) {
513
493
  let bytes;
514
- let hexStr;
515
494
  if (input == null) {
516
495
  bytes = UUID.generate();
517
496
  }
518
497
  else if (input instanceof UUID) {
519
498
  bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
520
- hexStr = input.__id;
521
499
  }
522
500
  else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
523
501
  bytes = ByteUtils.toLocalBufferType(input);
524
502
  }
525
503
  else if (typeof input === 'string') {
526
- bytes = uuidHexStringToBuffer(input);
504
+ bytes = UUID.bytesFromString(input);
527
505
  }
528
506
  else {
529
507
  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).');
530
508
  }
531
509
  super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
532
- this.__id = hexStr;
533
510
  }
534
511
  get id() {
535
512
  return this.buffer;
536
513
  }
537
514
  set id(value) {
538
515
  this.buffer = value;
539
- if (UUID.cacheHexString) {
540
- this.__id = bufferToUuidHexString(value);
541
- }
542
516
  }
543
517
  toHexString(includeDashes = true) {
544
- if (UUID.cacheHexString && this.__id) {
545
- return this.__id;
546
- }
547
- const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
548
- if (UUID.cacheHexString) {
549
- this.__id = uuidHexString;
518
+ if (includeDashes) {
519
+ return [
520
+ ByteUtils.toHex(this.buffer.subarray(0, 4)),
521
+ ByteUtils.toHex(this.buffer.subarray(4, 6)),
522
+ ByteUtils.toHex(this.buffer.subarray(6, 8)),
523
+ ByteUtils.toHex(this.buffer.subarray(8, 10)),
524
+ ByteUtils.toHex(this.buffer.subarray(10, 16))
525
+ ].join('-');
550
526
  }
551
- return uuidHexString;
527
+ return ByteUtils.toHex(this.buffer);
552
528
  }
553
529
  toString(encoding) {
554
530
  if (encoding === 'hex')
@@ -587,27 +563,32 @@ class UUID extends Binary {
587
563
  if (!input) {
588
564
  return false;
589
565
  }
590
- if (input instanceof UUID) {
591
- return true;
592
- }
593
566
  if (typeof input === 'string') {
594
- return uuidValidateString(input);
567
+ return UUID.isValidUUIDString(input);
595
568
  }
596
569
  if (isUint8Array(input)) {
597
- if (input.byteLength !== UUID_BYTE_LENGTH) {
598
- return false;
599
- }
600
- return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
570
+ return input.byteLength === UUID_BYTE_LENGTH;
601
571
  }
602
- return false;
572
+ return (input._bsontype === 'Binary' &&
573
+ input.sub_type === this.SUBTYPE_UUID &&
574
+ input.buffer.byteLength === 16);
603
575
  }
604
576
  static createFromHexString(hexString) {
605
- const buffer = uuidHexStringToBuffer(hexString);
577
+ const buffer = UUID.bytesFromString(hexString);
606
578
  return new UUID(buffer);
607
579
  }
608
580
  static createFromBase64(base64) {
609
581
  return new UUID(ByteUtils.fromBase64(base64));
610
582
  }
583
+ static bytesFromString(representation) {
584
+ if (!UUID.isValidUUIDString(representation)) {
585
+ throw new BSONError('UUID string representation must be 32 hex digits or canonical hyphenated representation');
586
+ }
587
+ return ByteUtils.fromHex(representation.replace(/-/g, ''));
588
+ }
589
+ static isValidUUIDString(representation) {
590
+ return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
591
+ }
611
592
  [Symbol.for('nodejs.util.inspect.custom')]() {
612
593
  return this.inspect();
613
594
  }
@@ -615,6 +596,7 @@ class UUID extends Binary {
615
596
  return `new UUID("${this.toHexString()}")`;
616
597
  }
617
598
  }
599
+ UUID.cacheHexString = false;
618
600
 
619
601
  class Code extends BSONValue {
620
602
  get _bsontype() {
@@ -2791,7 +2773,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2791
2773
  }
2792
2774
  else {
2793
2775
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2794
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2776
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2795
2777
  value = value.toUUID();
2796
2778
  }
2797
2779
  }
@@ -2817,11 +2799,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
2817
2799
  if (promoteBuffers && promoteValues) {
2818
2800
  value = _buffer;
2819
2801
  }
2820
- else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2821
- value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
2822
- }
2823
2802
  else {
2824
2803
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2804
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2805
+ value = value.toUUID();
2806
+ }
2825
2807
  }
2826
2808
  }
2827
2809
  index = index + binarySize;
@@ -4053,32 +4035,32 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4053
4035
 
4054
4036
  var bson = /*#__PURE__*/Object.freeze({
4055
4037
  __proto__: null,
4056
- Code: Code,
4038
+ BSONError: BSONError,
4039
+ BSONRegExp: BSONRegExp,
4040
+ BSONRuntimeError: BSONRuntimeError,
4057
4041
  BSONSymbol: BSONSymbol,
4058
- DBRef: DBRef,
4042
+ BSONType: BSONType,
4043
+ BSONValue: BSONValue,
4044
+ BSONVersionError: BSONVersionError,
4059
4045
  Binary: Binary,
4060
- ObjectId: ObjectId,
4061
- UUID: UUID,
4062
- Long: Long,
4063
- Timestamp: Timestamp,
4046
+ Code: Code,
4047
+ DBRef: DBRef,
4048
+ Decimal128: Decimal128,
4064
4049
  Double: Double,
4050
+ EJSON: EJSON,
4065
4051
  Int32: Int32,
4066
- MinKey: MinKey,
4052
+ Long: Long,
4067
4053
  MaxKey: MaxKey,
4068
- BSONRegExp: BSONRegExp,
4069
- Decimal128: Decimal128,
4070
- setInternalBufferSize: setInternalBufferSize,
4071
- serialize: serialize,
4072
- serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4073
- deserialize: deserialize,
4054
+ MinKey: MinKey,
4055
+ ObjectId: ObjectId,
4056
+ Timestamp: Timestamp,
4057
+ UUID: UUID,
4074
4058
  calculateObjectSize: calculateObjectSize,
4059
+ deserialize: deserialize,
4075
4060
  deserializeStream: deserializeStream,
4076
- BSONValue: BSONValue,
4077
- BSONError: BSONError,
4078
- BSONVersionError: BSONVersionError,
4079
- BSONRuntimeError: BSONRuntimeError,
4080
- BSONType: BSONType,
4081
- EJSON: EJSON
4061
+ serialize: serialize,
4062
+ serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4063
+ setInternalBufferSize: setInternalBufferSize
4082
4064
  });
4083
4065
 
4084
4066
  exports.BSON = bson;