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/package.json
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"etc/prepare.js"
|
|
14
14
|
],
|
|
15
15
|
"types": "bson.d.ts",
|
|
16
|
-
"version": "5.
|
|
16
|
+
"version": "5.1.0",
|
|
17
17
|
"author": {
|
|
18
18
|
"name": "The MongoDB NodeJS Team",
|
|
19
19
|
"email": "dbx-node@mongodb.com"
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"chalk": "^5.1.2",
|
|
42
42
|
"eslint": "^8.29.0",
|
|
43
43
|
"eslint-config-prettier": "^8.5.0",
|
|
44
|
+
"eslint-plugin-no-bigint-usage": "file:./etc/eslint/no-bigint-usage",
|
|
44
45
|
"eslint-plugin-prettier": "^4.2.1",
|
|
45
46
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
46
47
|
"magic-string": "^0.27.0",
|
|
@@ -76,20 +77,31 @@
|
|
|
76
77
|
"main": "./lib/bson.cjs",
|
|
77
78
|
"module": "./lib/bson.mjs",
|
|
78
79
|
"exports": {
|
|
79
|
-
"import":
|
|
80
|
-
|
|
80
|
+
"import": {
|
|
81
|
+
"types": "./bson.d.ts",
|
|
82
|
+
"default": "./lib/bson.mjs"
|
|
83
|
+
},
|
|
84
|
+
"require": {
|
|
85
|
+
"types": "./bson.d.ts",
|
|
86
|
+
"default": "./lib/bson.cjs"
|
|
87
|
+
},
|
|
81
88
|
"react-native": "./lib/bson.cjs",
|
|
82
89
|
"browser": "./lib/bson.mjs"
|
|
83
90
|
},
|
|
91
|
+
"compass:exports": {
|
|
92
|
+
"import": "./lib/bson.cjs",
|
|
93
|
+
"require": "./lib/bson.cjs"
|
|
94
|
+
},
|
|
84
95
|
"engines": {
|
|
85
96
|
"node": ">=14.20.1"
|
|
86
97
|
},
|
|
87
98
|
"scripts": {
|
|
88
99
|
"pretest": "npm run build",
|
|
89
|
-
"test": "npm run check:node && npm run check:web",
|
|
100
|
+
"test": "npm run check:node && npm run check:web && npm run check:web-no-bigint",
|
|
90
101
|
"check:node": "WEB=false mocha test/node",
|
|
91
102
|
"check:tsd": "npm run build:dts && tsd",
|
|
92
103
|
"check:web": "WEB=true mocha test/node",
|
|
104
|
+
"check:web-no-bigint": "WEB=true NO_BIGINT=true mocha test/node",
|
|
93
105
|
"build:ts": "node ./node_modules/typescript/bin/tsc",
|
|
94
106
|
"build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && rimraf 'lib/**/*.d.ts*' lib/parser lib/utils",
|
|
95
107
|
"build:bundle": "rollup -c rollup.config.mjs",
|
package/src/bson.ts
CHANGED
|
@@ -50,7 +50,7 @@ export {
|
|
|
50
50
|
Decimal128
|
|
51
51
|
};
|
|
52
52
|
export { BSONValue } from './bson_value';
|
|
53
|
-
export { BSONError, BSONVersionError } from './error';
|
|
53
|
+
export { BSONError, BSONVersionError, BSONRuntimeError } from './error';
|
|
54
54
|
export { BSONType } from './constants';
|
|
55
55
|
export { EJSON } from './extended_json';
|
|
56
56
|
|
package/src/error.ts
CHANGED
|
@@ -2,7 +2,11 @@ import { BSON_MAJOR_VERSION } from './constants';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @public
|
|
5
|
-
*
|
|
5
|
+
* @category Error
|
|
6
|
+
*
|
|
7
|
+
* `BSONError` objects are thrown when BSON ecounters an error.
|
|
8
|
+
*
|
|
9
|
+
* This is the parent class for all the other errors thrown by this library.
|
|
6
10
|
*/
|
|
7
11
|
export class BSONError extends Error {
|
|
8
12
|
/**
|
|
@@ -46,7 +50,10 @@ export class BSONError extends Error {
|
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* @public
|
|
55
|
+
* @category Error
|
|
56
|
+
*/
|
|
50
57
|
export class BSONVersionError extends BSONError {
|
|
51
58
|
get name(): 'BSONVersionError' {
|
|
52
59
|
return 'BSONVersionError';
|
|
@@ -58,3 +65,21 @@ export class BSONVersionError extends BSONError {
|
|
|
58
65
|
);
|
|
59
66
|
}
|
|
60
67
|
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @public
|
|
71
|
+
* @category Error
|
|
72
|
+
*
|
|
73
|
+
* An error generated when BSON functions encounter an unexpected input
|
|
74
|
+
* or reaches an unexpected/invalid internal state
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
export class BSONRuntimeError extends BSONError {
|
|
78
|
+
get name(): 'BSONRuntimeError' {
|
|
79
|
+
return 'BSONRuntimeError';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
constructor(message: string) {
|
|
83
|
+
super(message);
|
|
84
|
+
}
|
|
85
|
+
}
|
package/src/extended_json.ts
CHANGED
|
@@ -11,13 +11,13 @@ import {
|
|
|
11
11
|
import { DBRef, isDBRefLike } from './db_ref';
|
|
12
12
|
import { Decimal128 } from './decimal128';
|
|
13
13
|
import { Double } from './double';
|
|
14
|
-
import { BSONError, BSONVersionError } from './error';
|
|
14
|
+
import { BSONError, BSONRuntimeError, BSONVersionError } from './error';
|
|
15
15
|
import { Int32 } from './int_32';
|
|
16
16
|
import { Long } from './long';
|
|
17
17
|
import { MaxKey } from './max_key';
|
|
18
18
|
import { MinKey } from './min_key';
|
|
19
19
|
import { ObjectId } from './objectid';
|
|
20
|
-
import { isDate, isRegExp } from './parser/utils';
|
|
20
|
+
import { isDate, isRegExp, isMap } from './parser/utils';
|
|
21
21
|
import { BSONRegExp } from './regexp';
|
|
22
22
|
import { BSONSymbol } from './symbol';
|
|
23
23
|
import { Timestamp } from './timestamp';
|
|
@@ -93,6 +93,7 @@ function deserializeValue(value: any, options: EJSONOptions = {}) {
|
|
|
93
93
|
}
|
|
94
94
|
if (in64BitRange) {
|
|
95
95
|
if (options.useBigInt64) {
|
|
96
|
+
// eslint-disable-next-line no-restricted-globals -- This is allowed here as useBigInt64=true
|
|
96
97
|
return BigInt(value);
|
|
97
98
|
}
|
|
98
99
|
return Long.fromNumber(value);
|
|
@@ -124,10 +125,14 @@ function deserializeValue(value: any, options: EJSONOptions = {}) {
|
|
|
124
125
|
if (options.legacy) {
|
|
125
126
|
if (typeof d === 'number') date.setTime(d);
|
|
126
127
|
else if (typeof d === 'string') date.setTime(Date.parse(d));
|
|
128
|
+
else if (typeof d === 'bigint') date.setTime(Number(d));
|
|
129
|
+
else throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
|
|
127
130
|
} else {
|
|
128
131
|
if (typeof d === 'string') date.setTime(Date.parse(d));
|
|
129
132
|
else if (Long.isLong(d)) date.setTime(d.toNumber());
|
|
130
133
|
else if (typeof d === 'number' && options.relaxed) date.setTime(d);
|
|
134
|
+
else if (typeof d === 'bigint') date.setTime(Number(d));
|
|
135
|
+
else throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
|
|
131
136
|
}
|
|
132
137
|
return date;
|
|
133
138
|
}
|
|
@@ -185,6 +190,18 @@ function getISOString(date: Date) {
|
|
|
185
190
|
|
|
186
191
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
187
192
|
function serializeValue(value: any, options: EJSONSerializeOptions): any {
|
|
193
|
+
if (value instanceof Map || isMap(value)) {
|
|
194
|
+
const obj: Record<string, unknown> = Object.create(null);
|
|
195
|
+
for (const [k, v] of value) {
|
|
196
|
+
if (typeof k !== 'string') {
|
|
197
|
+
throw new BSONError('Can only serialize maps with string keys');
|
|
198
|
+
}
|
|
199
|
+
obj[k] = v;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return serializeValue(obj, options);
|
|
203
|
+
}
|
|
204
|
+
|
|
188
205
|
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
|
|
189
206
|
const index = options.seenObjects.findIndex(entry => entry.obj === value);
|
|
190
207
|
if (index !== -1) {
|
|
@@ -249,10 +266,12 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {
|
|
|
249
266
|
}
|
|
250
267
|
|
|
251
268
|
if (typeof value === 'bigint') {
|
|
269
|
+
/* eslint-disable no-restricted-globals -- This is allowed as we are accepting a bigint as input */
|
|
252
270
|
if (!options.relaxed) {
|
|
253
271
|
return { $numberLong: BigInt.asIntN(64, value).toString() };
|
|
254
272
|
}
|
|
255
273
|
return Number(BigInt.asIntN(64, value));
|
|
274
|
+
/* eslint-enable */
|
|
256
275
|
}
|
|
257
276
|
|
|
258
277
|
if (value instanceof RegExp || isRegExp(value)) {
|
package/src/long.ts
CHANGED
|
@@ -896,6 +896,7 @@ export class Long extends BSONValue {
|
|
|
896
896
|
|
|
897
897
|
/** Converts the Long to a BigInt (arbitrary precision). */
|
|
898
898
|
toBigInt(): bigint {
|
|
899
|
+
// eslint-disable-next-line no-restricted-globals -- This is allowed here as it is explicitly requesting a bigint
|
|
899
900
|
return BigInt(this.toString());
|
|
900
901
|
}
|
|
901
902
|
|
|
@@ -1042,8 +1043,10 @@ export class Long extends BSONValue {
|
|
|
1042
1043
|
}
|
|
1043
1044
|
|
|
1044
1045
|
if (useBigInt64) {
|
|
1046
|
+
/* eslint-disable no-restricted-globals -- Can use BigInt here as useBigInt64=true */
|
|
1045
1047
|
const bigIntResult = BigInt(doc.$numberLong);
|
|
1046
1048
|
return BigInt.asIntN(64, bigIntResult);
|
|
1049
|
+
/* eslint-enable */
|
|
1047
1050
|
}
|
|
1048
1051
|
|
|
1049
1052
|
const longResult = Long.fromString(doc.$numberLong);
|
|
@@ -22,7 +22,7 @@ type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
|
|
|
22
22
|
// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists
|
|
23
23
|
// Node.js global
|
|
24
24
|
declare const Buffer: NodeJsBufferConstructor;
|
|
25
|
-
declare const require: (mod: '
|
|
25
|
+
declare const require: (mod: 'crypto') => { randomBytes: (byteLength: number) => Uint8Array };
|
|
26
26
|
|
|
27
27
|
/** @internal */
|
|
28
28
|
export function nodejsMathRandomBytes(byteLength: number) {
|
|
@@ -48,7 +48,7 @@ export function nodejsMathRandomBytes(byteLength: number) {
|
|
|
48
48
|
*/
|
|
49
49
|
const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => {
|
|
50
50
|
try {
|
|
51
|
-
return require('
|
|
51
|
+
return require('crypto').randomBytes;
|
|
52
52
|
} catch {
|
|
53
53
|
return nodejsMathRandomBytes;
|
|
54
54
|
}
|