bson 6.1.0 → 6.2.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 +31 -14
- package/lib/bson.bundle.js +83 -70
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +83 -70
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +83 -70
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +83 -70
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +1 -1
- package/src/binary.ts +10 -15
- package/src/bson_value.ts +15 -2
- package/src/code.ts +10 -10
- package/src/db_ref.ts +13 -11
- package/src/decimal128.ts +5 -8
- package/src/double.ts +4 -8
- package/src/error.ts +1 -3
- package/src/int_32.ts +4 -7
- package/src/long.ts +6 -7
- package/src/max_key.ts +0 -5
- package/src/min_key.ts +0 -5
- package/src/objectid.ts +4 -7
- package/src/parser/utils.ts +27 -0
- package/src/regexp.ts +7 -7
- package/src/symbol.ts +4 -7
- package/src/timestamp.ts +6 -7
package/package.json
CHANGED
package/src/binary.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isAnyArrayBuffer, isUint8Array } from './parser/utils';
|
|
1
|
+
import { type InspectFn, defaultInspect, isAnyArrayBuffer, isUint8Array } from './parser/utils';
|
|
2
2
|
import type { EJSONOptions } from './extended_json';
|
|
3
3
|
import { BSONError } from './error';
|
|
4
4
|
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
|
|
@@ -263,14 +263,12 @@ export class Binary extends BSONValue {
|
|
|
263
263
|
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
|
|
264
264
|
}
|
|
265
265
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return this.inspect();
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
inspect(): string {
|
|
266
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
267
|
+
inspect ??= defaultInspect;
|
|
272
268
|
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
273
|
-
|
|
269
|
+
const base64Arg = inspect(base64, options);
|
|
270
|
+
const subTypeArg = inspect(this.sub_type, options);
|
|
271
|
+
return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
|
|
274
272
|
}
|
|
275
273
|
}
|
|
276
274
|
|
|
@@ -463,13 +461,10 @@ export class UUID extends Binary {
|
|
|
463
461
|
* Converts to a string representation of this Id.
|
|
464
462
|
*
|
|
465
463
|
* @returns return the 36 character hex string representation.
|
|
466
|
-
*
|
|
464
|
+
*
|
|
467
465
|
*/
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
inspect(): string {
|
|
473
|
-
return `new UUID("${this.toHexString()}")`;
|
|
466
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
467
|
+
inspect ??= defaultInspect;
|
|
468
|
+
return `new UUID(${inspect(this.toHexString(), options)})`;
|
|
474
469
|
}
|
|
475
470
|
}
|
package/src/bson_value.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BSON_MAJOR_VERSION } from './constants';
|
|
2
|
+
import { type InspectFn } from './parser/utils';
|
|
2
3
|
|
|
3
4
|
/** @public */
|
|
4
5
|
export abstract class BSONValue {
|
|
@@ -10,8 +11,20 @@ export abstract class BSONValue {
|
|
|
10
11
|
return BSON_MAJOR_VERSION;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
[Symbol.for('nodejs.util.inspect.custom')](
|
|
15
|
+
depth?: number,
|
|
16
|
+
options?: unknown,
|
|
17
|
+
inspect?: InspectFn
|
|
18
|
+
): string {
|
|
19
|
+
return this.inspect(depth, options, inspect);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
* Prints a human-readable string of BSON value information
|
|
25
|
+
* If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify
|
|
26
|
+
*/
|
|
27
|
+
public abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
|
|
15
28
|
|
|
16
29
|
/** @internal */
|
|
17
30
|
abstract toExtendedJSON(): unknown;
|
package/src/code.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Document } from './bson';
|
|
2
2
|
import { BSONValue } from './bson_value';
|
|
3
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
3
4
|
|
|
4
5
|
/** @public */
|
|
5
6
|
export interface CodeExtended {
|
|
@@ -55,15 +56,14 @@ export class Code extends BSONValue {
|
|
|
55
56
|
return new Code(doc.$code, doc.$scope);
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
})`;
|
|
59
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
60
|
+
inspect ??= defaultInspect;
|
|
61
|
+
let parametersString = inspect(this.code, options);
|
|
62
|
+
const multiLineFn = parametersString.includes('\n');
|
|
63
|
+
if (this.scope != null) {
|
|
64
|
+
parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
|
|
65
|
+
}
|
|
66
|
+
const endingNewline = multiLineFn && this.scope === null;
|
|
67
|
+
return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
|
|
68
68
|
}
|
|
69
69
|
}
|
package/src/db_ref.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Document } from './bson';
|
|
|
2
2
|
import { BSONValue } from './bson_value';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
4
|
import type { ObjectId } from './objectid';
|
|
5
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
5
6
|
|
|
6
7
|
/** @public */
|
|
7
8
|
export interface DBRefLike {
|
|
@@ -111,17 +112,18 @@ export class DBRef extends BSONValue {
|
|
|
111
112
|
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
116
|
+
inspect ??= defaultInspect;
|
|
117
|
+
|
|
118
|
+
const args = [
|
|
119
|
+
inspect(this.namespace, options),
|
|
120
|
+
inspect(this.oid, options),
|
|
121
|
+
...(this.db ? [inspect(this.db, options)] : []),
|
|
122
|
+
...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
|
|
118
126
|
|
|
119
|
-
|
|
120
|
-
// NOTE: if OID is an ObjectId class it will just print the oid string.
|
|
121
|
-
const oid =
|
|
122
|
-
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
|
|
123
|
-
return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${
|
|
124
|
-
this.db ? `, "${this.db}"` : ''
|
|
125
|
-
})`;
|
|
127
|
+
return `new DBRef(${args.join(', ')})`;
|
|
126
128
|
}
|
|
127
129
|
}
|
package/src/decimal128.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import { Long } from './long';
|
|
4
|
-
import { isUint8Array } from './parser/utils';
|
|
4
|
+
import { type InspectFn, defaultInspect, isUint8Array } from './parser/utils';
|
|
5
5
|
import { ByteUtils } from './utils/byte_utils';
|
|
6
6
|
|
|
7
7
|
const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
|
|
@@ -847,12 +847,9 @@ export class Decimal128 extends BSONValue {
|
|
|
847
847
|
return Decimal128.fromString(doc.$numberDecimal);
|
|
848
848
|
}
|
|
849
849
|
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
inspect(): string {
|
|
856
|
-
return `new Decimal128("${this.toString()}")`;
|
|
850
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
851
|
+
inspect ??= defaultInspect;
|
|
852
|
+
const d128string = inspect(this.toString(), options);
|
|
853
|
+
return `new Decimal128(${d128string})`;
|
|
857
854
|
}
|
|
858
855
|
}
|
package/src/double.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import type { EJSONOptions } from './extended_json';
|
|
3
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
3
4
|
|
|
4
5
|
/** @public */
|
|
5
6
|
export interface DoubleExtended {
|
|
@@ -71,13 +72,8 @@ export class Double extends BSONValue {
|
|
|
71
72
|
return options && options.relaxed ? doubleValue : new Double(doubleValue);
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return this.
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
inspect(): string {
|
|
80
|
-
const eJSON = this.toExtendedJSON() as DoubleExtended;
|
|
81
|
-
return `new Double(${eJSON.$numberDouble})`;
|
|
75
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
76
|
+
inspect ??= defaultInspect;
|
|
77
|
+
return `new Double(${inspect(this.value, options)})`;
|
|
82
78
|
}
|
|
83
79
|
}
|
package/src/error.ts
CHANGED
|
@@ -60,9 +60,7 @@ export class BSONVersionError extends BSONError {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
constructor() {
|
|
63
|
-
super(
|
|
64
|
-
`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`
|
|
65
|
-
);
|
|
63
|
+
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
|
|
66
64
|
}
|
|
67
65
|
}
|
|
68
66
|
|
package/src/int_32.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import type { EJSONOptions } from './extended_json';
|
|
3
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
3
4
|
|
|
4
5
|
/** @public */
|
|
5
6
|
export interface Int32Extended {
|
|
@@ -59,12 +60,8 @@ export class Int32 extends BSONValue {
|
|
|
59
60
|
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return this.
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
inspect(): string {
|
|
68
|
-
return `new Int32(${this.valueOf()})`;
|
|
63
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
64
|
+
inspect ??= defaultInspect;
|
|
65
|
+
return `new Int32(${inspect(this.value, options)})`;
|
|
69
66
|
}
|
|
70
67
|
}
|
package/src/long.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
4
5
|
import type { Timestamp } from './timestamp';
|
|
5
6
|
|
|
6
7
|
interface LongWASMHelpers {
|
|
@@ -1056,12 +1057,10 @@ export class Long extends BSONValue {
|
|
|
1056
1057
|
return longResult;
|
|
1057
1058
|
}
|
|
1058
1059
|
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
inspect(): string {
|
|
1065
|
-
return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
|
|
1060
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
1061
|
+
inspect ??= defaultInspect;
|
|
1062
|
+
const longVal = inspect(this.toString(), options);
|
|
1063
|
+
const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
|
|
1064
|
+
return `new Long(${longVal}${unsignedVal})`;
|
|
1066
1065
|
}
|
|
1067
1066
|
}
|
package/src/max_key.ts
CHANGED
package/src/min_key.ts
CHANGED
package/src/objectid.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
3
4
|
import { BSONDataView, ByteUtils } from './utils/byte_utils';
|
|
4
5
|
|
|
5
6
|
// Regular expression that checks for hex value
|
|
@@ -294,13 +295,9 @@ export class ObjectId extends BSONValue {
|
|
|
294
295
|
* Converts to a string representation of this Id.
|
|
295
296
|
*
|
|
296
297
|
* @returns return the 24 character hex string representation.
|
|
297
|
-
* @internal
|
|
298
298
|
*/
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
inspect(): string {
|
|
304
|
-
return `new ObjectId("${this.toHexString()}")`;
|
|
299
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
300
|
+
inspect ??= defaultInspect;
|
|
301
|
+
return `new ObjectId(${inspect(this.toHexString(), options)})`;
|
|
305
302
|
}
|
|
306
303
|
}
|
package/src/parser/utils.ts
CHANGED
|
@@ -27,3 +27,30 @@ export function isMap(d: unknown): d is Map<unknown, unknown> {
|
|
|
27
27
|
export function isDate(d: unknown): d is Date {
|
|
28
28
|
return Object.prototype.toString.call(d) === '[object Date]';
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
export type InspectFn = (x: unknown, options?: unknown) => string;
|
|
32
|
+
export function defaultInspect(x: unknown, _options?: unknown): string {
|
|
33
|
+
return JSON.stringify(x, (k: string, v: unknown) => {
|
|
34
|
+
if (typeof v === 'bigint') {
|
|
35
|
+
return { $numberLong: `${v}` };
|
|
36
|
+
} else if (isMap(v)) {
|
|
37
|
+
return Object.fromEntries(v);
|
|
38
|
+
}
|
|
39
|
+
return v;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @internal */
|
|
44
|
+
type StylizeFunction = (x: string, style: string) => string;
|
|
45
|
+
/** @internal */
|
|
46
|
+
export function getStylizeFunction(options?: unknown): StylizeFunction | undefined {
|
|
47
|
+
const stylizeExists =
|
|
48
|
+
options != null &&
|
|
49
|
+
typeof options === 'object' &&
|
|
50
|
+
'stylize' in options &&
|
|
51
|
+
typeof options.stylize === 'function';
|
|
52
|
+
|
|
53
|
+
if (stylizeExists) {
|
|
54
|
+
return options.stylize as StylizeFunction;
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/regexp.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
|
+
import { type InspectFn, defaultInspect, getStylizeFunction } from './parser/utils';
|
|
4
5
|
|
|
5
6
|
function alphabetize(str: string): string {
|
|
6
7
|
return str.split('').sort().join('');
|
|
@@ -103,12 +104,11 @@ export class BSONRegExp extends BSONValue {
|
|
|
103
104
|
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
|
|
107
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
108
|
+
const stylize = getStylizeFunction(options) ?? (v => v);
|
|
109
|
+
inspect ??= defaultInspect;
|
|
110
|
+
const pattern = stylize(inspect(this.pattern), 'regexp');
|
|
111
|
+
const flags = stylize(inspect(this.options), 'regexp');
|
|
112
|
+
return `new BSONRegExp(${pattern}, ${flags})`;
|
|
113
113
|
}
|
|
114
114
|
}
|
package/src/symbol.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
2
3
|
|
|
3
4
|
/** @public */
|
|
4
5
|
export interface BSONSymbolExtended {
|
|
@@ -33,10 +34,6 @@ export class BSONSymbol extends BSONValue {
|
|
|
33
34
|
return this.value;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
inspect(): string {
|
|
37
|
-
return `new BSONSymbol(${JSON.stringify(this.value)})`;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
37
|
toJSON(): string {
|
|
41
38
|
return this.value;
|
|
42
39
|
}
|
|
@@ -51,8 +48,8 @@ export class BSONSymbol extends BSONValue {
|
|
|
51
48
|
return new BSONSymbol(doc.$symbol);
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return this.
|
|
51
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
52
|
+
inspect ??= defaultInspect;
|
|
53
|
+
return `new BSONSymbol(${inspect(this.value, options)})`;
|
|
57
54
|
}
|
|
58
55
|
}
|
package/src/timestamp.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BSONError } from './error';
|
|
2
2
|
import type { Int32 } from './int_32';
|
|
3
3
|
import { Long } from './long';
|
|
4
|
+
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
4
5
|
|
|
5
6
|
/** @public */
|
|
6
7
|
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
|
|
@@ -141,12 +142,10 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
141
142
|
return new Timestamp({ t, i });
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
inspect(): string {
|
|
150
|
-
return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
|
|
145
|
+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
146
|
+
inspect ??= defaultInspect;
|
|
147
|
+
const t = inspect(this.high >>> 0, options);
|
|
148
|
+
const i = inspect(this.low >>> 0, options);
|
|
149
|
+
return `new Timestamp({ t: ${t}, i: ${i} })`;
|
|
151
150
|
}
|
|
152
151
|
}
|