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
@@ -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;
@@ -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;
@@ -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() {
@@ -2792,7 +2774,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2792
2774
  }
2793
2775
  else {
2794
2776
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2795
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2777
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2796
2778
  value = value.toUUID();
2797
2779
  }
2798
2780
  }
@@ -2818,11 +2800,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
2818
2800
  if (promoteBuffers && promoteValues) {
2819
2801
  value = _buffer;
2820
2802
  }
2821
- else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2822
- value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
2823
- }
2824
2803
  else {
2825
2804
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2805
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2806
+ value = value.toUUID();
2807
+ }
2826
2808
  }
2827
2809
  }
2828
2810
  index = index + binarySize;
@@ -4054,32 +4036,32 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4054
4036
 
4055
4037
  var bson = /*#__PURE__*/Object.freeze({
4056
4038
  __proto__: null,
4057
- Code: Code,
4039
+ BSONError: BSONError,
4040
+ BSONRegExp: BSONRegExp,
4041
+ BSONRuntimeError: BSONRuntimeError,
4058
4042
  BSONSymbol: BSONSymbol,
4059
- DBRef: DBRef,
4043
+ BSONType: BSONType,
4044
+ BSONValue: BSONValue,
4045
+ BSONVersionError: BSONVersionError,
4060
4046
  Binary: Binary,
4061
- ObjectId: ObjectId,
4062
- UUID: UUID,
4063
- Long: Long,
4064
- Timestamp: Timestamp,
4047
+ Code: Code,
4048
+ DBRef: DBRef,
4049
+ Decimal128: Decimal128,
4065
4050
  Double: Double,
4051
+ EJSON: EJSON,
4066
4052
  Int32: Int32,
4067
- MinKey: MinKey,
4053
+ Long: Long,
4068
4054
  MaxKey: MaxKey,
4069
- BSONRegExp: BSONRegExp,
4070
- Decimal128: Decimal128,
4071
- setInternalBufferSize: setInternalBufferSize,
4072
- serialize: serialize,
4073
- serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4074
- deserialize: deserialize,
4055
+ MinKey: MinKey,
4056
+ ObjectId: ObjectId,
4057
+ Timestamp: Timestamp,
4058
+ UUID: UUID,
4075
4059
  calculateObjectSize: calculateObjectSize,
4060
+ deserialize: deserialize,
4076
4061
  deserializeStream: deserializeStream,
4077
- BSONValue: BSONValue,
4078
- BSONError: BSONError,
4079
- BSONVersionError: BSONVersionError,
4080
- BSONRuntimeError: BSONRuntimeError,
4081
- BSONType: BSONType,
4082
- EJSON: EJSON
4062
+ serialize: serialize,
4063
+ serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4064
+ setInternalBufferSize: setInternalBufferSize
4083
4065
  });
4084
4066
 
4085
4067
  exports.BSON = bson;