bson 5.0.0-alpha.2 → 5.0.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 +33 -14
- package/lib/bson.bundle.js +146 -130
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +146 -130
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +145 -131
- package/lib/bson.mjs.map +1 -1
- package/package.json +1 -1
- package/src/binary.ts +4 -11
- package/src/bson.ts +2 -1
- package/src/bson_value.ts +18 -0
- package/src/code.ts +3 -6
- package/src/constants.ts +1 -3
- package/src/db_ref.ts +3 -6
- package/src/decimal128.ts +3 -6
- package/src/double.ts +3 -6
- package/src/error.ts +15 -0
- package/src/extended_json.ts +26 -6
- package/src/int_32.ts +3 -6
- package/src/long.ts +31 -9
- package/src/max_key.ts +2 -6
- package/src/min_key.ts +2 -6
- package/src/objectid.ts +3 -6
- package/src/parser/calculate_size.ts +13 -17
- package/src/parser/serializer.ts +4 -4
- package/src/regexp.ts +3 -6
- package/src/symbol.ts +3 -7
- package/src/timestamp.ts +0 -5
package/package.json
CHANGED
package/src/binary.ts
CHANGED
|
@@ -2,8 +2,9 @@ import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from
|
|
|
2
2
|
import { isUint8Array } from './parser/utils';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
4
|
import { BSONError } from './error';
|
|
5
|
-
import { BSON_BINARY_SUBTYPE_UUID_NEW
|
|
5
|
+
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
|
|
6
6
|
import { ByteUtils } from './utils/byte_utils';
|
|
7
|
+
import { BSONValue } from './bson_value';
|
|
7
8
|
|
|
8
9
|
/** @public */
|
|
9
10
|
export type BinarySequence = Uint8Array | number[];
|
|
@@ -27,14 +28,10 @@ export interface BinaryExtended {
|
|
|
27
28
|
* @public
|
|
28
29
|
* @category BSONType
|
|
29
30
|
*/
|
|
30
|
-
export class Binary {
|
|
31
|
+
export class Binary extends BSONValue {
|
|
31
32
|
get _bsontype(): 'Binary' {
|
|
32
33
|
return 'Binary';
|
|
33
34
|
}
|
|
34
|
-
/** @internal */
|
|
35
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
36
|
-
return BSON_MAJOR_VERSION;
|
|
37
|
-
}
|
|
38
35
|
|
|
39
36
|
/**
|
|
40
37
|
* Binary default subtype
|
|
@@ -79,6 +76,7 @@ export class Binary {
|
|
|
79
76
|
* @param subType - the option binary type.
|
|
80
77
|
*/
|
|
81
78
|
constructor(buffer?: string | BinarySequence, subType?: number) {
|
|
79
|
+
super();
|
|
82
80
|
if (
|
|
83
81
|
!(buffer == null) &&
|
|
84
82
|
!(typeof buffer === 'string') &&
|
|
@@ -309,11 +307,6 @@ const UUID_BYTE_LENGTH = 16;
|
|
|
309
307
|
* @public
|
|
310
308
|
*/
|
|
311
309
|
export class UUID extends Binary {
|
|
312
|
-
/** @internal */
|
|
313
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
314
|
-
return BSON_MAJOR_VERSION;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
310
|
static cacheHexString: boolean;
|
|
318
311
|
|
|
319
312
|
/** UUID hexString cache @internal */
|
package/src/bson.ts
CHANGED
|
@@ -49,7 +49,8 @@ export {
|
|
|
49
49
|
BSONRegExp,
|
|
50
50
|
Decimal128
|
|
51
51
|
};
|
|
52
|
-
export {
|
|
52
|
+
export { BSONValue } from './bson_value';
|
|
53
|
+
export { BSONError, BSONVersionError } from './error';
|
|
53
54
|
export { BSONType } from './constants';
|
|
54
55
|
export { EJSON } from './extended_json';
|
|
55
56
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BSON_MAJOR_VERSION } from './constants';
|
|
2
|
+
|
|
3
|
+
/** @public */
|
|
4
|
+
export abstract class BSONValue {
|
|
5
|
+
/** @public */
|
|
6
|
+
public abstract get _bsontype(): string;
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
get [Symbol.for('@@mdb.bson.version')](): typeof BSON_MAJOR_VERSION {
|
|
10
|
+
return BSON_MAJOR_VERSION;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @public */
|
|
14
|
+
public abstract inspect(): string;
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
abstract toExtendedJSON(): unknown;
|
|
18
|
+
}
|
package/src/code.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Document } from './bson';
|
|
2
|
-
import {
|
|
2
|
+
import { BSONValue } from './bson_value';
|
|
3
3
|
|
|
4
4
|
/** @public */
|
|
5
5
|
export interface CodeExtended {
|
|
@@ -12,14 +12,10 @@ export interface CodeExtended {
|
|
|
12
12
|
* @public
|
|
13
13
|
* @category BSONType
|
|
14
14
|
*/
|
|
15
|
-
export class Code {
|
|
15
|
+
export class Code extends BSONValue {
|
|
16
16
|
get _bsontype(): 'Code' {
|
|
17
17
|
return 'Code';
|
|
18
18
|
}
|
|
19
|
-
/** @internal */
|
|
20
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
21
|
-
return BSON_MAJOR_VERSION;
|
|
22
|
-
}
|
|
23
19
|
|
|
24
20
|
code: string;
|
|
25
21
|
|
|
@@ -32,6 +28,7 @@ export class Code {
|
|
|
32
28
|
* @param scope - an optional scope for the function.
|
|
33
29
|
*/
|
|
34
30
|
constructor(code: string | Function, scope?: Document | null) {
|
|
31
|
+
super();
|
|
35
32
|
this.code = code.toString();
|
|
36
33
|
this.scope = scope ?? null;
|
|
37
34
|
}
|
package/src/constants.ts
CHANGED
package/src/db_ref.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Document } from './bson';
|
|
2
|
-
import {
|
|
2
|
+
import { BSONValue } from './bson_value';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
4
|
import type { ObjectId } from './objectid';
|
|
5
5
|
|
|
@@ -29,14 +29,10 @@ export function isDBRefLike(value: unknown): value is DBRefLike {
|
|
|
29
29
|
* @public
|
|
30
30
|
* @category BSONType
|
|
31
31
|
*/
|
|
32
|
-
export class DBRef {
|
|
32
|
+
export class DBRef extends BSONValue {
|
|
33
33
|
get _bsontype(): 'DBRef' {
|
|
34
34
|
return 'DBRef';
|
|
35
35
|
}
|
|
36
|
-
/** @internal */
|
|
37
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
38
|
-
return BSON_MAJOR_VERSION;
|
|
39
|
-
}
|
|
40
36
|
|
|
41
37
|
collection!: string;
|
|
42
38
|
oid!: ObjectId;
|
|
@@ -49,6 +45,7 @@ export class DBRef {
|
|
|
49
45
|
* @param db - optional db name, if omitted the reference is local to the current db.
|
|
50
46
|
*/
|
|
51
47
|
constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
|
|
48
|
+
super();
|
|
52
49
|
// check if namespace has been provided
|
|
53
50
|
const parts = collection.split('.');
|
|
54
51
|
if (parts.length === 2) {
|
package/src/decimal128.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import { Long } from './long';
|
|
4
4
|
import { isUint8Array } from './parser/utils';
|
|
@@ -127,14 +127,10 @@ export interface Decimal128Extended {
|
|
|
127
127
|
* @public
|
|
128
128
|
* @category BSONType
|
|
129
129
|
*/
|
|
130
|
-
export class Decimal128 {
|
|
130
|
+
export class Decimal128 extends BSONValue {
|
|
131
131
|
get _bsontype(): 'Decimal128' {
|
|
132
132
|
return 'Decimal128';
|
|
133
133
|
}
|
|
134
|
-
/** @internal */
|
|
135
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
136
|
-
return BSON_MAJOR_VERSION;
|
|
137
|
-
}
|
|
138
134
|
|
|
139
135
|
readonly bytes!: Uint8Array;
|
|
140
136
|
|
|
@@ -143,6 +139,7 @@ export class Decimal128 {
|
|
|
143
139
|
* or a string representation as returned by .toString()
|
|
144
140
|
*/
|
|
145
141
|
constructor(bytes: Uint8Array | string) {
|
|
142
|
+
super();
|
|
146
143
|
if (typeof bytes === 'string') {
|
|
147
144
|
this.bytes = Decimal128.fromString(bytes).bytes;
|
|
148
145
|
} else if (isUint8Array(bytes)) {
|
package/src/double.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
import type { EJSONOptions } from './extended_json';
|
|
3
3
|
|
|
4
4
|
/** @public */
|
|
@@ -11,14 +11,10 @@ export interface DoubleExtended {
|
|
|
11
11
|
* @public
|
|
12
12
|
* @category BSONType
|
|
13
13
|
*/
|
|
14
|
-
export class Double {
|
|
14
|
+
export class Double extends BSONValue {
|
|
15
15
|
get _bsontype(): 'Double' {
|
|
16
16
|
return 'Double';
|
|
17
17
|
}
|
|
18
|
-
/** @internal */
|
|
19
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
20
|
-
return BSON_MAJOR_VERSION;
|
|
21
|
-
}
|
|
22
18
|
|
|
23
19
|
value!: number;
|
|
24
20
|
/**
|
|
@@ -27,6 +23,7 @@ export class Double {
|
|
|
27
23
|
* @param value - the number we want to represent as a double.
|
|
28
24
|
*/
|
|
29
25
|
constructor(value: number) {
|
|
26
|
+
super();
|
|
30
27
|
if ((value as unknown) instanceof Number) {
|
|
31
28
|
value = value.valueOf();
|
|
32
29
|
}
|
package/src/error.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BSON_MAJOR_VERSION } from './constants';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @public
|
|
3
5
|
* `BSONError` objects are thrown when runtime errors occur.
|
|
@@ -43,3 +45,16 @@ export class BSONError extends Error {
|
|
|
43
45
|
);
|
|
44
46
|
}
|
|
45
47
|
}
|
|
48
|
+
|
|
49
|
+
/** @public */
|
|
50
|
+
export class BSONVersionError extends BSONError {
|
|
51
|
+
get name(): 'BSONVersionError' {
|
|
52
|
+
return 'BSONVersionError';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
constructor() {
|
|
56
|
+
super(
|
|
57
|
+
`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/extended_json.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
import { DBRef, isDBRefLike } from './db_ref';
|
|
12
12
|
import { Decimal128 } from './decimal128';
|
|
13
13
|
import { Double } from './double';
|
|
14
|
-
import { BSONError } from './error';
|
|
14
|
+
import { BSONError, BSONVersionError } from './error';
|
|
15
15
|
import { Int32 } from './int_32';
|
|
16
16
|
import { Long } from './long';
|
|
17
17
|
import { MaxKey } from './max_key';
|
|
@@ -28,6 +28,8 @@ export type EJSONOptions = {
|
|
|
28
28
|
legacy?: boolean;
|
|
29
29
|
/** Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types */
|
|
30
30
|
relaxed?: boolean;
|
|
31
|
+
/** Enable native bigint support */
|
|
32
|
+
useBigInt64?: boolean;
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
/** @internal */
|
|
@@ -76,17 +78,23 @@ const keysToCodecs = {
|
|
|
76
78
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
79
|
function deserializeValue(value: any, options: EJSONOptions = {}) {
|
|
78
80
|
if (typeof value === 'number') {
|
|
81
|
+
// TODO(NODE-4377): EJSON js number handling diverges from BSON
|
|
82
|
+
const in32BitRange = value <= BSON_INT32_MAX && value >= BSON_INT32_MIN;
|
|
83
|
+
const in64BitRange = value <= BSON_INT64_MAX && value >= BSON_INT64_MIN;
|
|
84
|
+
|
|
79
85
|
if (options.relaxed || options.legacy) {
|
|
80
86
|
return value;
|
|
81
87
|
}
|
|
82
88
|
|
|
83
89
|
if (Number.isInteger(value) && !Object.is(value, -0)) {
|
|
84
90
|
// interpret as being of the smallest BSON integer type that can represent the number exactly
|
|
85
|
-
if (
|
|
91
|
+
if (in32BitRange) {
|
|
86
92
|
return new Int32(value);
|
|
87
93
|
}
|
|
88
|
-
if (
|
|
89
|
-
|
|
94
|
+
if (in64BitRange) {
|
|
95
|
+
if (options.useBigInt64) {
|
|
96
|
+
return BigInt(value);
|
|
97
|
+
}
|
|
90
98
|
return Long.fromNumber(value);
|
|
91
99
|
}
|
|
92
100
|
}
|
|
@@ -240,6 +248,13 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {
|
|
|
240
248
|
return { $numberDouble: Object.is(value, -0) ? '-0.0' : value.toString() };
|
|
241
249
|
}
|
|
242
250
|
|
|
251
|
+
if (typeof value === 'bigint') {
|
|
252
|
+
if (!options.relaxed) {
|
|
253
|
+
return { $numberLong: BigInt.asIntN(64, value).toString() };
|
|
254
|
+
}
|
|
255
|
+
return Number(BigInt.asIntN(64, value));
|
|
256
|
+
}
|
|
257
|
+
|
|
243
258
|
if (value instanceof RegExp || isRegExp(value)) {
|
|
244
259
|
let flags = value.flags;
|
|
245
260
|
if (flags === undefined) {
|
|
@@ -318,7 +333,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
|
|
|
318
333
|
typeof doc._bsontype === 'string' &&
|
|
319
334
|
doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION
|
|
320
335
|
) {
|
|
321
|
-
throw new
|
|
336
|
+
throw new BSONVersionError();
|
|
322
337
|
} else if (isBSONType(doc)) {
|
|
323
338
|
// the "document" is really just a BSON type object
|
|
324
339
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -371,13 +386,18 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
|
|
|
371
386
|
*/
|
|
372
387
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
373
388
|
function parse(text: string, options?: EJSONOptions): any {
|
|
389
|
+
const ejsonOptions = {
|
|
390
|
+
useBigInt64: options?.useBigInt64 ?? false,
|
|
391
|
+
relaxed: options?.relaxed ?? true,
|
|
392
|
+
legacy: options?.legacy ?? false
|
|
393
|
+
};
|
|
374
394
|
return JSON.parse(text, (key, value) => {
|
|
375
395
|
if (key.indexOf('\x00') !== -1) {
|
|
376
396
|
throw new BSONError(
|
|
377
397
|
`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`
|
|
378
398
|
);
|
|
379
399
|
}
|
|
380
|
-
return deserializeValue(value,
|
|
400
|
+
return deserializeValue(value, ejsonOptions);
|
|
381
401
|
});
|
|
382
402
|
}
|
|
383
403
|
|
package/src/int_32.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
import type { EJSONOptions } from './extended_json';
|
|
3
3
|
|
|
4
4
|
/** @public */
|
|
@@ -11,14 +11,10 @@ export interface Int32Extended {
|
|
|
11
11
|
* @public
|
|
12
12
|
* @category BSONType
|
|
13
13
|
*/
|
|
14
|
-
export class Int32 {
|
|
14
|
+
export class Int32 extends BSONValue {
|
|
15
15
|
get _bsontype(): 'Int32' {
|
|
16
16
|
return 'Int32';
|
|
17
17
|
}
|
|
18
|
-
/** @internal */
|
|
19
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
20
|
-
return BSON_MAJOR_VERSION;
|
|
21
|
-
}
|
|
22
18
|
|
|
23
19
|
value!: number;
|
|
24
20
|
/**
|
|
@@ -27,6 +23,7 @@ export class Int32 {
|
|
|
27
23
|
* @param value - the number we want to represent as an int32.
|
|
28
24
|
*/
|
|
29
25
|
constructor(value: number | string) {
|
|
26
|
+
super();
|
|
30
27
|
if ((value as unknown) instanceof Number) {
|
|
31
28
|
value = value.valueOf();
|
|
32
29
|
}
|
package/src/long.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
4
|
import type { Timestamp } from './timestamp';
|
|
@@ -76,6 +76,10 @@ const INT_CACHE: { [key: number]: Long } = {};
|
|
|
76
76
|
/** A cache of the Long representations of small unsigned integer values. */
|
|
77
77
|
const UINT_CACHE: { [key: number]: Long } = {};
|
|
78
78
|
|
|
79
|
+
const MAX_INT64_STRING_LENGTH = 20;
|
|
80
|
+
|
|
81
|
+
const DECIMAL_REG_EX = /^(\+?0|(\+|-)?[1-9][0-9]*)$/;
|
|
82
|
+
|
|
79
83
|
/** @public */
|
|
80
84
|
export interface LongExtended {
|
|
81
85
|
$numberLong: string;
|
|
@@ -100,14 +104,10 @@ export interface LongExtended {
|
|
|
100
104
|
* case would often result in infinite recursion.
|
|
101
105
|
* Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
|
|
102
106
|
*/
|
|
103
|
-
export class Long {
|
|
107
|
+
export class Long extends BSONValue {
|
|
104
108
|
get _bsontype(): 'Long' {
|
|
105
109
|
return 'Long';
|
|
106
110
|
}
|
|
107
|
-
/** @internal */
|
|
108
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
109
|
-
return BSON_MAJOR_VERSION;
|
|
110
|
-
}
|
|
111
111
|
|
|
112
112
|
/** An indicator used to reliably determine if an object is a Long or not. */
|
|
113
113
|
get __isLong__(): boolean {
|
|
@@ -143,6 +143,7 @@ export class Long {
|
|
|
143
143
|
* @param unsigned - Whether unsigned or not, defaults to signed
|
|
144
144
|
*/
|
|
145
145
|
constructor(low: number | bigint | string = 0, high?: number | boolean, unsigned?: boolean) {
|
|
146
|
+
super();
|
|
146
147
|
if (typeof low === 'bigint') {
|
|
147
148
|
Object.assign(this, Long.fromBigInt(low, !!high));
|
|
148
149
|
} else if (typeof low === 'string') {
|
|
@@ -1026,9 +1027,30 @@ export class Long {
|
|
|
1026
1027
|
if (options && options.relaxed) return this.toNumber();
|
|
1027
1028
|
return { $numberLong: this.toString() };
|
|
1028
1029
|
}
|
|
1029
|
-
static fromExtendedJSON(
|
|
1030
|
-
|
|
1031
|
-
|
|
1030
|
+
static fromExtendedJSON(
|
|
1031
|
+
doc: { $numberLong: string },
|
|
1032
|
+
options?: EJSONOptions
|
|
1033
|
+
): number | Long | bigint {
|
|
1034
|
+
const { useBigInt64 = false, relaxed = true } = { ...options };
|
|
1035
|
+
|
|
1036
|
+
if (doc.$numberLong.length > MAX_INT64_STRING_LENGTH) {
|
|
1037
|
+
throw new BSONError('$numberLong string is too long');
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
if (!DECIMAL_REG_EX.test(doc.$numberLong)) {
|
|
1041
|
+
throw new BSONError(`$numberLong string "${doc.$numberLong}" is in an invalid format`);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
if (useBigInt64) {
|
|
1045
|
+
const bigIntResult = BigInt(doc.$numberLong);
|
|
1046
|
+
return BigInt.asIntN(64, bigIntResult);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
const longResult = Long.fromString(doc.$numberLong);
|
|
1050
|
+
if (relaxed) {
|
|
1051
|
+
return longResult.toNumber();
|
|
1052
|
+
}
|
|
1053
|
+
return longResult;
|
|
1032
1054
|
}
|
|
1033
1055
|
|
|
1034
1056
|
/** @internal */
|
package/src/max_key.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
|
|
3
3
|
/** @public */
|
|
4
4
|
export interface MaxKeyExtended {
|
|
@@ -10,14 +10,10 @@ export interface MaxKeyExtended {
|
|
|
10
10
|
* @public
|
|
11
11
|
* @category BSONType
|
|
12
12
|
*/
|
|
13
|
-
export class MaxKey {
|
|
13
|
+
export class MaxKey extends BSONValue {
|
|
14
14
|
get _bsontype(): 'MaxKey' {
|
|
15
15
|
return 'MaxKey';
|
|
16
16
|
}
|
|
17
|
-
/** @internal */
|
|
18
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
19
|
-
return BSON_MAJOR_VERSION;
|
|
20
|
-
}
|
|
21
17
|
|
|
22
18
|
/** @internal */
|
|
23
19
|
toExtendedJSON(): MaxKeyExtended {
|
package/src/min_key.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
|
|
3
3
|
/** @public */
|
|
4
4
|
export interface MinKeyExtended {
|
|
@@ -10,14 +10,10 @@ export interface MinKeyExtended {
|
|
|
10
10
|
* @public
|
|
11
11
|
* @category BSONType
|
|
12
12
|
*/
|
|
13
|
-
export class MinKey {
|
|
13
|
+
export class MinKey extends BSONValue {
|
|
14
14
|
get _bsontype(): 'MinKey' {
|
|
15
15
|
return 'MinKey';
|
|
16
16
|
}
|
|
17
|
-
/** @internal */
|
|
18
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
19
|
-
return BSON_MAJOR_VERSION;
|
|
20
|
-
}
|
|
21
17
|
|
|
22
18
|
/** @internal */
|
|
23
19
|
toExtendedJSON(): MinKeyExtended {
|
package/src/objectid.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import { isUint8Array } from './parser/utils';
|
|
4
4
|
import { BSONDataView, ByteUtils } from './utils/byte_utils';
|
|
@@ -28,14 +28,10 @@ const kId = Symbol('id');
|
|
|
28
28
|
* @public
|
|
29
29
|
* @category BSONType
|
|
30
30
|
*/
|
|
31
|
-
export class ObjectId {
|
|
31
|
+
export class ObjectId extends BSONValue {
|
|
32
32
|
get _bsontype(): 'ObjectId' {
|
|
33
33
|
return 'ObjectId';
|
|
34
34
|
}
|
|
35
|
-
/** @internal */
|
|
36
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
37
|
-
return BSON_MAJOR_VERSION;
|
|
38
|
-
}
|
|
39
35
|
|
|
40
36
|
/** @internal */
|
|
41
37
|
private static index = Math.floor(Math.random() * 0xffffff);
|
|
@@ -53,6 +49,7 @@ export class ObjectId {
|
|
|
53
49
|
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
|
|
54
50
|
*/
|
|
55
51
|
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
|
|
52
|
+
super();
|
|
56
53
|
// workingId is set based on type of input and whether valid id exists for the input
|
|
57
54
|
let workingId;
|
|
58
55
|
if (typeof inputId === 'object' && inputId && 'id' in inputId) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Binary } from '../binary';
|
|
2
2
|
import type { Document } from '../bson';
|
|
3
|
-
import {
|
|
3
|
+
import { BSONVersionError } from '../error';
|
|
4
4
|
import * as constants from '../constants';
|
|
5
5
|
import { ByteUtils } from '../utils/byte_utils';
|
|
6
6
|
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
|
|
@@ -83,14 +83,10 @@ function calculateElement(
|
|
|
83
83
|
typeof value._bsontype === 'string' &&
|
|
84
84
|
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
85
85
|
) {
|
|
86
|
-
throw new
|
|
87
|
-
} else if (
|
|
88
|
-
value == null ||
|
|
89
|
-
value['_bsontype'] === 'MinKey' ||
|
|
90
|
-
value['_bsontype'] === 'MaxKey'
|
|
91
|
-
) {
|
|
86
|
+
throw new BSONVersionError();
|
|
87
|
+
} else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
92
88
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
|
|
93
|
-
} else if (value
|
|
89
|
+
} else if (value._bsontype === 'ObjectId') {
|
|
94
90
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (12 + 1);
|
|
95
91
|
} else if (value instanceof Date || isDate(value)) {
|
|
96
92
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
|
|
@@ -103,14 +99,14 @@ function calculateElement(
|
|
|
103
99
|
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 4 + 1) + value.byteLength
|
|
104
100
|
);
|
|
105
101
|
} else if (
|
|
106
|
-
value
|
|
107
|
-
value
|
|
108
|
-
value
|
|
102
|
+
value._bsontype === 'Long' ||
|
|
103
|
+
value._bsontype === 'Double' ||
|
|
104
|
+
value._bsontype === 'Timestamp'
|
|
109
105
|
) {
|
|
110
106
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
|
|
111
|
-
} else if (value
|
|
107
|
+
} else if (value._bsontype === 'Decimal128') {
|
|
112
108
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (16 + 1);
|
|
113
|
-
} else if (value
|
|
109
|
+
} else if (value._bsontype === 'Code') {
|
|
114
110
|
// Calculate size depending on the availability of a scope
|
|
115
111
|
if (value.scope != null && Object.keys(value.scope).length > 0) {
|
|
116
112
|
return (
|
|
@@ -131,7 +127,7 @@ function calculateElement(
|
|
|
131
127
|
1
|
|
132
128
|
);
|
|
133
129
|
}
|
|
134
|
-
} else if (value
|
|
130
|
+
} else if (value._bsontype === 'Binary') {
|
|
135
131
|
const binary: Binary = value;
|
|
136
132
|
// Check what kind of subtype we have
|
|
137
133
|
if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
|
|
@@ -144,7 +140,7 @@ function calculateElement(
|
|
|
144
140
|
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (binary.position + 1 + 4 + 1)
|
|
145
141
|
);
|
|
146
142
|
}
|
|
147
|
-
} else if (value
|
|
143
|
+
} else if (value._bsontype === 'Symbol') {
|
|
148
144
|
return (
|
|
149
145
|
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
|
|
150
146
|
ByteUtils.utf8ByteLength(value.value) +
|
|
@@ -152,7 +148,7 @@ function calculateElement(
|
|
|
152
148
|
1 +
|
|
153
149
|
1
|
|
154
150
|
);
|
|
155
|
-
} else if (value
|
|
151
|
+
} else if (value._bsontype === 'DBRef') {
|
|
156
152
|
// Set up correct object for serialization
|
|
157
153
|
const ordered_values = Object.assign(
|
|
158
154
|
{
|
|
@@ -183,7 +179,7 @@ function calculateElement(
|
|
|
183
179
|
(value.multiline ? 1 : 0) +
|
|
184
180
|
1
|
|
185
181
|
);
|
|
186
|
-
} else if (value
|
|
182
|
+
} else if (value._bsontype === 'BSONRegExp') {
|
|
187
183
|
return (
|
|
188
184
|
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
|
|
189
185
|
1 +
|
package/src/parser/serializer.ts
CHANGED
|
@@ -5,7 +5,7 @@ import * as constants from '../constants';
|
|
|
5
5
|
import type { DBRefLike } from '../db_ref';
|
|
6
6
|
import type { Decimal128 } from '../decimal128';
|
|
7
7
|
import type { Double } from '../double';
|
|
8
|
-
import { BSONError } from '../error';
|
|
8
|
+
import { BSONError, BSONVersionError } from '../error';
|
|
9
9
|
import type { Int32 } from '../int_32';
|
|
10
10
|
import { Long } from '../long';
|
|
11
11
|
import type { MinKey } from '../min_key';
|
|
@@ -710,7 +710,7 @@ export function serializeInto(
|
|
|
710
710
|
typeof value === 'object' &&
|
|
711
711
|
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
712
712
|
) {
|
|
713
|
-
throw new
|
|
713
|
+
throw new BSONVersionError();
|
|
714
714
|
} else if (value._bsontype === 'ObjectId') {
|
|
715
715
|
index = serializeObjectId(buffer, key, value, index);
|
|
716
716
|
} else if (value._bsontype === 'Decimal128') {
|
|
@@ -820,7 +820,7 @@ export function serializeInto(
|
|
|
820
820
|
typeof value === 'object' &&
|
|
821
821
|
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
822
822
|
) {
|
|
823
|
-
throw new
|
|
823
|
+
throw new BSONVersionError();
|
|
824
824
|
} else if (value._bsontype === 'ObjectId') {
|
|
825
825
|
index = serializeObjectId(buffer, key, value, index);
|
|
826
826
|
} else if (type === 'object' && value._bsontype === 'Decimal128') {
|
|
@@ -930,7 +930,7 @@ export function serializeInto(
|
|
|
930
930
|
typeof value === 'object' &&
|
|
931
931
|
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
932
932
|
) {
|
|
933
|
-
throw new
|
|
933
|
+
throw new BSONVersionError();
|
|
934
934
|
} else if (value._bsontype === 'ObjectId') {
|
|
935
935
|
index = serializeObjectId(buffer, key, value, index);
|
|
936
936
|
} else if (type === 'object' && value._bsontype === 'Decimal128') {
|
package/src/regexp.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import type { EJSONOptions } from './extended_json';
|
|
4
4
|
|
|
@@ -25,14 +25,10 @@ export interface BSONRegExpExtended {
|
|
|
25
25
|
* @public
|
|
26
26
|
* @category BSONType
|
|
27
27
|
*/
|
|
28
|
-
export class BSONRegExp {
|
|
28
|
+
export class BSONRegExp extends BSONValue {
|
|
29
29
|
get _bsontype(): 'BSONRegExp' {
|
|
30
30
|
return 'BSONRegExp';
|
|
31
31
|
}
|
|
32
|
-
/** @internal */
|
|
33
|
-
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
34
|
-
return BSON_MAJOR_VERSION;
|
|
35
|
-
}
|
|
36
32
|
|
|
37
33
|
pattern!: string;
|
|
38
34
|
options!: string;
|
|
@@ -41,6 +37,7 @@ export class BSONRegExp {
|
|
|
41
37
|
* @param options - The regular expression options
|
|
42
38
|
*/
|
|
43
39
|
constructor(pattern: string, options?: string) {
|
|
40
|
+
super();
|
|
44
41
|
this.pattern = pattern;
|
|
45
42
|
this.options = alphabetize(options ?? '');
|
|
46
43
|
|