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/bson.d.ts +23 -2
- package/lib/bson.bundle.js +29 -1
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +29 -1
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +29 -2
- package/lib/bson.mjs.map +1 -1
- package/package.json +16 -4
- package/src/bson.ts +1 -1
- package/src/error.ts +27 -2
- package/src/extended_json.ts +21 -2
- package/src/long.ts +3 -0
- package/src/utils/node_byte_utils.ts +2 -2
package/bson.d.ts
CHANGED
|
@@ -144,6 +144,7 @@ declare namespace BSON {
|
|
|
144
144
|
BSONValue,
|
|
145
145
|
BSONError,
|
|
146
146
|
BSONVersionError,
|
|
147
|
+
BSONRuntimeError,
|
|
147
148
|
BSONType,
|
|
148
149
|
EJSON,
|
|
149
150
|
Document,
|
|
@@ -154,7 +155,11 @@ export { BSON }
|
|
|
154
155
|
|
|
155
156
|
/**
|
|
156
157
|
* @public
|
|
157
|
-
*
|
|
158
|
+
* @category Error
|
|
159
|
+
*
|
|
160
|
+
* `BSONError` objects are thrown when BSON ecounters an error.
|
|
161
|
+
*
|
|
162
|
+
* This is the parent class for all the other errors thrown by this library.
|
|
158
163
|
*/
|
|
159
164
|
export declare class BSONError extends Error {
|
|
160
165
|
/* Excluded from this release type: bsonError */
|
|
@@ -206,6 +211,19 @@ export declare interface BSONRegExpExtendedLegacy {
|
|
|
206
211
|
$options: string;
|
|
207
212
|
}
|
|
208
213
|
|
|
214
|
+
/**
|
|
215
|
+
* @public
|
|
216
|
+
* @category Error
|
|
217
|
+
*
|
|
218
|
+
* An error generated when BSON functions encounter an unexpected input
|
|
219
|
+
* or reaches an unexpected/invalid internal state
|
|
220
|
+
*
|
|
221
|
+
*/
|
|
222
|
+
export declare class BSONRuntimeError extends BSONError {
|
|
223
|
+
get name(): 'BSONRuntimeError';
|
|
224
|
+
constructor(message: string);
|
|
225
|
+
}
|
|
226
|
+
|
|
209
227
|
/**
|
|
210
228
|
* A class representation of the BSON Symbol type.
|
|
211
229
|
* @public
|
|
@@ -269,7 +287,10 @@ export declare abstract class BSONValue {
|
|
|
269
287
|
/* Excluded from this release type: toExtendedJSON */
|
|
270
288
|
}
|
|
271
289
|
|
|
272
|
-
/**
|
|
290
|
+
/**
|
|
291
|
+
* @public
|
|
292
|
+
* @category Error
|
|
293
|
+
*/
|
|
273
294
|
export declare class BSONVersionError extends BSONError {
|
|
274
295
|
get name(): 'BSONVersionError';
|
|
275
296
|
constructor();
|
package/lib/bson.bundle.js
CHANGED
|
@@ -83,13 +83,21 @@ class BSONVersionError extends BSONError {
|
|
|
83
83
|
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
+
class BSONRuntimeError extends BSONError {
|
|
87
|
+
get name() {
|
|
88
|
+
return 'BSONRuntimeError';
|
|
89
|
+
}
|
|
90
|
+
constructor(message) {
|
|
91
|
+
super(message);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
86
94
|
|
|
87
95
|
function nodejsMathRandomBytes(byteLength) {
|
|
88
96
|
return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
|
|
89
97
|
}
|
|
90
98
|
const nodejsRandomBytes = (() => {
|
|
91
99
|
try {
|
|
92
|
-
return require('
|
|
100
|
+
return require('crypto').randomBytes;
|
|
93
101
|
}
|
|
94
102
|
catch {
|
|
95
103
|
return nodejsMathRandomBytes;
|
|
@@ -3723,6 +3731,10 @@ function deserializeValue(value, options = {}) {
|
|
|
3723
3731
|
date.setTime(d);
|
|
3724
3732
|
else if (typeof d === 'string')
|
|
3725
3733
|
date.setTime(Date.parse(d));
|
|
3734
|
+
else if (typeof d === 'bigint')
|
|
3735
|
+
date.setTime(Number(d));
|
|
3736
|
+
else
|
|
3737
|
+
throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
|
|
3726
3738
|
}
|
|
3727
3739
|
else {
|
|
3728
3740
|
if (typeof d === 'string')
|
|
@@ -3731,6 +3743,10 @@ function deserializeValue(value, options = {}) {
|
|
|
3731
3743
|
date.setTime(d.toNumber());
|
|
3732
3744
|
else if (typeof d === 'number' && options.relaxed)
|
|
3733
3745
|
date.setTime(d);
|
|
3746
|
+
else if (typeof d === 'bigint')
|
|
3747
|
+
date.setTime(Number(d));
|
|
3748
|
+
else
|
|
3749
|
+
throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
|
|
3734
3750
|
}
|
|
3735
3751
|
return date;
|
|
3736
3752
|
}
|
|
@@ -3772,6 +3788,16 @@ function getISOString(date) {
|
|
|
3772
3788
|
return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
|
|
3773
3789
|
}
|
|
3774
3790
|
function serializeValue(value, options) {
|
|
3791
|
+
if (value instanceof Map || isMap(value)) {
|
|
3792
|
+
const obj = Object.create(null);
|
|
3793
|
+
for (const [k, v] of value) {
|
|
3794
|
+
if (typeof k !== 'string') {
|
|
3795
|
+
throw new BSONError('Can only serialize maps with string keys');
|
|
3796
|
+
}
|
|
3797
|
+
obj[k] = v;
|
|
3798
|
+
}
|
|
3799
|
+
return serializeValue(obj, options);
|
|
3800
|
+
}
|
|
3775
3801
|
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
|
|
3776
3802
|
const index = options.seenObjects.findIndex(entry => entry.obj === value);
|
|
3777
3803
|
if (index !== -1) {
|
|
@@ -4035,6 +4061,7 @@ deserializeStream: deserializeStream,
|
|
|
4035
4061
|
BSONValue: BSONValue,
|
|
4036
4062
|
BSONError: BSONError,
|
|
4037
4063
|
BSONVersionError: BSONVersionError,
|
|
4064
|
+
BSONRuntimeError: BSONRuntimeError,
|
|
4038
4065
|
BSONType: BSONType,
|
|
4039
4066
|
EJSON: EJSON
|
|
4040
4067
|
});
|
|
@@ -4042,6 +4069,7 @@ EJSON: EJSON
|
|
|
4042
4069
|
exports.BSON = bson;
|
|
4043
4070
|
exports.BSONError = BSONError;
|
|
4044
4071
|
exports.BSONRegExp = BSONRegExp;
|
|
4072
|
+
exports.BSONRuntimeError = BSONRuntimeError;
|
|
4045
4073
|
exports.BSONSymbol = BSONSymbol;
|
|
4046
4074
|
exports.BSONType = BSONType;
|
|
4047
4075
|
exports.BSONValue = BSONValue;
|