bson 5.0.0 → 5.1.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.
package/lib/bson.cjs CHANGED
@@ -82,13 +82,21 @@ class BSONVersionError extends BSONError {
82
82
  super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
83
83
  }
84
84
  }
85
+ class BSONRuntimeError extends BSONError {
86
+ get name() {
87
+ return 'BSONRuntimeError';
88
+ }
89
+ constructor(message) {
90
+ super(message);
91
+ }
92
+ }
85
93
 
86
94
  function nodejsMathRandomBytes(byteLength) {
87
95
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
88
96
  }
89
97
  const nodejsRandomBytes = (() => {
90
98
  try {
91
- return require('node:crypto').randomBytes;
99
+ return require('crypto').randomBytes;
92
100
  }
93
101
  catch {
94
102
  return nodejsMathRandomBytes;
@@ -3722,6 +3730,10 @@ function deserializeValue(value, options = {}) {
3722
3730
  date.setTime(d);
3723
3731
  else if (typeof d === 'string')
3724
3732
  date.setTime(Date.parse(d));
3733
+ else if (typeof d === 'bigint')
3734
+ date.setTime(Number(d));
3735
+ else
3736
+ throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
3725
3737
  }
3726
3738
  else {
3727
3739
  if (typeof d === 'string')
@@ -3730,6 +3742,10 @@ function deserializeValue(value, options = {}) {
3730
3742
  date.setTime(d.toNumber());
3731
3743
  else if (typeof d === 'number' && options.relaxed)
3732
3744
  date.setTime(d);
3745
+ else if (typeof d === 'bigint')
3746
+ date.setTime(Number(d));
3747
+ else
3748
+ throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
3733
3749
  }
3734
3750
  return date;
3735
3751
  }
@@ -3771,6 +3787,16 @@ function getISOString(date) {
3771
3787
  return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
3772
3788
  }
3773
3789
  function serializeValue(value, options) {
3790
+ if (value instanceof Map || isMap(value)) {
3791
+ const obj = Object.create(null);
3792
+ for (const [k, v] of value) {
3793
+ if (typeof k !== 'string') {
3794
+ throw new BSONError('Can only serialize maps with string keys');
3795
+ }
3796
+ obj[k] = v;
3797
+ }
3798
+ return serializeValue(obj, options);
3799
+ }
3774
3800
  if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
3775
3801
  const index = options.seenObjects.findIndex(entry => entry.obj === value);
3776
3802
  if (index !== -1) {
@@ -4034,6 +4060,7 @@ var bson = /*#__PURE__*/Object.freeze({
4034
4060
  BSONValue: BSONValue,
4035
4061
  BSONError: BSONError,
4036
4062
  BSONVersionError: BSONVersionError,
4063
+ BSONRuntimeError: BSONRuntimeError,
4037
4064
  BSONType: BSONType,
4038
4065
  EJSON: EJSON
4039
4066
  });
@@ -4041,6 +4068,7 @@ var bson = /*#__PURE__*/Object.freeze({
4041
4068
  exports.BSON = bson;
4042
4069
  exports.BSONError = BSONError;
4043
4070
  exports.BSONRegExp = BSONRegExp;
4071
+ exports.BSONRuntimeError = BSONRuntimeError;
4044
4072
  exports.BSONSymbol = BSONSymbol;
4045
4073
  exports.BSONType = BSONType;
4046
4074
  exports.BSONValue = BSONValue;