bson 6.10.3 → 7.0.0-alpha
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/README.md +11 -1
- package/bson.d.ts +21 -14
- package/lib/bson.bundle.js +147 -78
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +147 -78
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +148 -80
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.node.mjs +4636 -0
- package/lib/bson.node.mjs.map +1 -0
- package/lib/bson.rn.cjs +146 -80
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +32 -32
- package/src/binary.ts +27 -4
- package/src/bson.ts +1 -1
- package/src/bson_value.ts +24 -1
- package/src/extended_json.ts +0 -3
- package/src/long.ts +4 -9
- package/src/objectid.ts +25 -25
- package/src/parser/on_demand/parse_to_elements.ts +25 -23
- package/src/timestamp.ts +10 -1
- package/src/utils/node_byte_utils.ts +15 -22
- package/src/utils/number_utils.ts +4 -13
- package/vendor/base64/LICENSE-MIT.txt +0 -20
- package/vendor/base64/README.md +0 -112
- package/vendor/base64/base64.js +0 -157
- package/vendor/base64/package.json +0 -43
- package/vendor/text-encoding/LICENSE.md +0 -237
- package/vendor/text-encoding/README.md +0 -111
- package/vendor/text-encoding/index.js +0 -9
- package/vendor/text-encoding/lib/encoding-indexes.js +0 -47
- package/vendor/text-encoding/lib/encoding.js +0 -3301
- package/vendor/text-encoding/package.json +0 -37
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"vendor"
|
|
15
15
|
],
|
|
16
16
|
"types": "bson.d.ts",
|
|
17
|
-
"version": "
|
|
17
|
+
"version": "7.0.0-alpha",
|
|
18
18
|
"author": {
|
|
19
19
|
"name": "The MongoDB NodeJS Team",
|
|
20
20
|
"email": "dbx-node@mongodb.com"
|
|
@@ -27,40 +27,40 @@
|
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@istanbuljs/nyc-config-typescript": "^1.0.2",
|
|
30
|
-
"@microsoft/api-extractor": "^7.
|
|
31
|
-
"@rollup/plugin-node-resolve": "^
|
|
32
|
-
"@rollup/plugin-typescript": "^
|
|
30
|
+
"@microsoft/api-extractor": "^7.52.5",
|
|
31
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
32
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
33
33
|
"@types/chai": "^4.3.17",
|
|
34
34
|
"@types/mocha": "^10.0.7",
|
|
35
|
-
"@types/node": "^
|
|
36
|
-
"@types/sinon": "^17.0.
|
|
35
|
+
"@types/node": "^24.2.1",
|
|
36
|
+
"@types/sinon": "^17.0.4",
|
|
37
37
|
"@types/sinon-chai": "^3.2.12",
|
|
38
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
39
|
-
"@typescript-eslint/parser": "^8.
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^8.31.1",
|
|
39
|
+
"@typescript-eslint/parser": "^8.31.1",
|
|
40
40
|
"benchmark": "^2.1.4",
|
|
41
41
|
"chai": "^4.4.1",
|
|
42
42
|
"chalk": "^5.3.0",
|
|
43
|
-
"dbx-js-tools": "github:mongodb-js/dbx-js-tools",
|
|
44
|
-
"eslint": "^9.
|
|
45
|
-
"eslint-config-prettier": "^
|
|
46
|
-
"eslint-plugin-
|
|
47
|
-
"eslint-plugin-prettier": "^5.2.1",
|
|
43
|
+
"dbx-js-tools": "github:mongodb-js/dbx-js-tools#main",
|
|
44
|
+
"eslint": "^9.33.0",
|
|
45
|
+
"eslint-config-prettier": "^10.1.2",
|
|
46
|
+
"eslint-plugin-prettier": "^5.2.6",
|
|
48
47
|
"eslint-plugin-tsdoc": "^0.4.0",
|
|
49
48
|
"magic-string": "^0.30.11",
|
|
50
|
-
"mocha": "^
|
|
49
|
+
"mocha": "^11.7.1",
|
|
51
50
|
"node-fetch": "^3.3.2",
|
|
52
51
|
"nyc": "^15.1.0",
|
|
53
|
-
"prettier": "^3.
|
|
54
|
-
"rollup": "^4.
|
|
52
|
+
"prettier": "^3.5.3",
|
|
53
|
+
"rollup": "^4.40.1",
|
|
55
54
|
"sinon": "^18.0.0",
|
|
56
55
|
"sinon-chai": "^3.7.0",
|
|
57
56
|
"source-map-support": "^0.5.21",
|
|
58
57
|
"tar": "^7.4.3",
|
|
59
58
|
"ts-node": "^10.9.2",
|
|
60
|
-
"tsd": "^0.
|
|
61
|
-
"
|
|
59
|
+
"tsd": "^0.33.0",
|
|
60
|
+
"tslib": "^2.8.1",
|
|
61
|
+
"typescript": "^5.8.3",
|
|
62
62
|
"typescript-cached-transpile": "0.0.6",
|
|
63
|
-
"uuid": "^
|
|
63
|
+
"uuid": "^11.1.0"
|
|
64
64
|
},
|
|
65
65
|
"tsd": {
|
|
66
66
|
"directory": "test/types",
|
|
@@ -75,36 +75,36 @@
|
|
|
75
75
|
"native": false
|
|
76
76
|
},
|
|
77
77
|
"main": "./lib/bson.cjs",
|
|
78
|
-
"module": "./lib/bson.mjs",
|
|
78
|
+
"module": "./lib/bson.node.mjs",
|
|
79
79
|
"exports": {
|
|
80
|
-
"
|
|
80
|
+
"browser": {
|
|
81
81
|
"types": "./bson.d.ts",
|
|
82
82
|
"default": "./lib/bson.mjs"
|
|
83
83
|
},
|
|
84
|
-
"require": {
|
|
85
|
-
"types": "./bson.d.ts",
|
|
86
|
-
"default": "./lib/bson.cjs"
|
|
87
|
-
},
|
|
88
84
|
"react-native": "./lib/bson.rn.cjs",
|
|
89
|
-
"
|
|
85
|
+
"default": {
|
|
86
|
+
"types": "./bson.d.ts",
|
|
87
|
+
"import": "./lib/bson.node.mjs",
|
|
88
|
+
"require": "./lib/bson.cjs"
|
|
89
|
+
}
|
|
90
90
|
},
|
|
91
91
|
"compass:exports": {
|
|
92
92
|
"import": "./lib/bson.cjs",
|
|
93
93
|
"require": "./lib/bson.cjs"
|
|
94
94
|
},
|
|
95
95
|
"engines": {
|
|
96
|
-
"node": ">=
|
|
96
|
+
"node": ">=20.19.0"
|
|
97
97
|
},
|
|
98
98
|
"scripts": {
|
|
99
99
|
"pretest": "npm run build",
|
|
100
|
-
"test": "npm run check:node && npm run check:web
|
|
100
|
+
"test": "npm run check:node && npm run check:web",
|
|
101
101
|
"check:node": "WEB=false mocha test/node",
|
|
102
102
|
"check:tsd": "npm run build:dts && tsd",
|
|
103
103
|
"check:web": "WEB=true mocha test/node",
|
|
104
|
-
"check:
|
|
105
|
-
"check:
|
|
106
|
-
"check:
|
|
107
|
-
"check:
|
|
104
|
+
"check:granular-bench": "npm run build:bench && npm run check:baseline-bench && node ./test/bench/etc/run_granular_benchmarks.js",
|
|
105
|
+
"check:spec-bench": "npm run build:bench && npm run check:baseline-bench && node ./test/bench/lib/spec/bsonBench.js",
|
|
106
|
+
"check:custom-bench": "npm run build && npm run check:baseline-bench && node ./test/bench/custom/main.mjs",
|
|
107
|
+
"check:baseline-bench": "node ./test/bench/etc/cpuBaseline.js",
|
|
108
108
|
"build:bench": "cd test/bench && npx tsc",
|
|
109
109
|
"build:ts": "node ./node_modules/typescript/bin/tsc",
|
|
110
110
|
"build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && node etc/clean_definition_files.cjs",
|
package/src/binary.ts
CHANGED
|
@@ -45,7 +45,10 @@ export class Binary extends BSONValue {
|
|
|
45
45
|
static readonly SUBTYPE_DEFAULT = 0;
|
|
46
46
|
/** Function BSON type */
|
|
47
47
|
static readonly SUBTYPE_FUNCTION = 1;
|
|
48
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Legacy default BSON Binary type
|
|
50
|
+
* @deprecated BSON Binary subtype 2 is deprecated in the BSON specification
|
|
51
|
+
*/
|
|
49
52
|
static readonly SUBTYPE_BYTE_ARRAY = 2;
|
|
50
53
|
/** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
|
|
51
54
|
static readonly SUBTYPE_UUID_OLD = 3;
|
|
@@ -341,6 +344,8 @@ export class Binary extends BSONValue {
|
|
|
341
344
|
throw new BSONError('Binary datatype field is not Int8');
|
|
342
345
|
}
|
|
343
346
|
|
|
347
|
+
validateBinaryVector(this);
|
|
348
|
+
|
|
344
349
|
return new Int8Array(
|
|
345
350
|
this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
|
|
346
351
|
);
|
|
@@ -361,6 +366,8 @@ export class Binary extends BSONValue {
|
|
|
361
366
|
throw new BSONError('Binary datatype field is not Float32');
|
|
362
367
|
}
|
|
363
368
|
|
|
369
|
+
validateBinaryVector(this);
|
|
370
|
+
|
|
364
371
|
const floatBytes = new Uint8Array(
|
|
365
372
|
this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
|
|
366
373
|
);
|
|
@@ -387,6 +394,8 @@ export class Binary extends BSONValue {
|
|
|
387
394
|
throw new BSONError('Binary datatype field is not packed bit');
|
|
388
395
|
}
|
|
389
396
|
|
|
397
|
+
validateBinaryVector(this);
|
|
398
|
+
|
|
390
399
|
return new Uint8Array(
|
|
391
400
|
this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
|
|
392
401
|
);
|
|
@@ -409,6 +418,8 @@ export class Binary extends BSONValue {
|
|
|
409
418
|
throw new BSONError('Binary datatype field is not packed bit');
|
|
410
419
|
}
|
|
411
420
|
|
|
421
|
+
validateBinaryVector(this);
|
|
422
|
+
|
|
412
423
|
const byteCount = this.length() - 2;
|
|
413
424
|
const bitCount = byteCount * 8 - this.buffer[1];
|
|
414
425
|
const bits = new Int8Array(bitCount);
|
|
@@ -434,7 +445,9 @@ export class Binary extends BSONValue {
|
|
|
434
445
|
buffer[1] = 0;
|
|
435
446
|
const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
436
447
|
buffer.set(intBytes, 2);
|
|
437
|
-
|
|
448
|
+
const bin = new this(buffer, this.SUBTYPE_VECTOR);
|
|
449
|
+
validateBinaryVector(bin);
|
|
450
|
+
return bin;
|
|
438
451
|
}
|
|
439
452
|
|
|
440
453
|
/** Constructs a Binary representing an Float32 Vector. */
|
|
@@ -448,7 +461,9 @@ export class Binary extends BSONValue {
|
|
|
448
461
|
|
|
449
462
|
if (NumberUtils.isBigEndian) ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
|
|
450
463
|
|
|
451
|
-
|
|
464
|
+
const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
|
|
465
|
+
validateBinaryVector(bin);
|
|
466
|
+
return bin;
|
|
452
467
|
}
|
|
453
468
|
|
|
454
469
|
/**
|
|
@@ -461,7 +476,9 @@ export class Binary extends BSONValue {
|
|
|
461
476
|
buffer[0] = Binary.VECTOR_TYPE.PackedBit;
|
|
462
477
|
buffer[1] = padding;
|
|
463
478
|
buffer.set(array, 2);
|
|
464
|
-
|
|
479
|
+
const bin = new this(buffer, this.SUBTYPE_VECTOR);
|
|
480
|
+
validateBinaryVector(bin);
|
|
481
|
+
return bin;
|
|
465
482
|
}
|
|
466
483
|
|
|
467
484
|
/**
|
|
@@ -517,6 +534,12 @@ export function validateBinaryVector(vector: Binary): void {
|
|
|
517
534
|
throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
|
|
518
535
|
}
|
|
519
536
|
|
|
537
|
+
if (datatype === Binary.VECTOR_TYPE.Float32) {
|
|
538
|
+
if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
|
|
539
|
+
throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
520
543
|
if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
|
|
521
544
|
throw new BSONError(
|
|
522
545
|
'Invalid Vector: padding must be zero for packed bit vectors that are empty'
|
package/src/bson.ts
CHANGED
|
@@ -50,7 +50,7 @@ export {
|
|
|
50
50
|
BSONRegExp,
|
|
51
51
|
Decimal128
|
|
52
52
|
};
|
|
53
|
-
export { BSONValue } from './bson_value';
|
|
53
|
+
export { BSONValue, bsonType, type BSONTypeTag } from './bson_value';
|
|
54
54
|
export { BSONError, BSONVersionError, BSONRuntimeError, BSONOffsetError } from './error';
|
|
55
55
|
export { BSONType } from './constants';
|
|
56
56
|
export { EJSON } from './extended_json';
|
package/src/bson_value.ts
CHANGED
|
@@ -2,10 +2,33 @@ import { BSON_MAJOR_VERSION } from './constants';
|
|
|
2
2
|
import { type InspectFn } from './parser/utils';
|
|
3
3
|
import { BSON_VERSION_SYMBOL } from './constants';
|
|
4
4
|
|
|
5
|
+
/** @public */
|
|
6
|
+
export type BSONTypeTag =
|
|
7
|
+
| 'BSONRegExp'
|
|
8
|
+
| 'BSONSymbol'
|
|
9
|
+
| 'ObjectId'
|
|
10
|
+
| 'Binary'
|
|
11
|
+
| 'Decimal128'
|
|
12
|
+
| 'Double'
|
|
13
|
+
| 'Int32'
|
|
14
|
+
| 'Long'
|
|
15
|
+
| 'MaxKey'
|
|
16
|
+
| 'MinKey'
|
|
17
|
+
| 'Timestamp'
|
|
18
|
+
| 'Code'
|
|
19
|
+
| 'DBRef';
|
|
20
|
+
|
|
21
|
+
/** @public */
|
|
22
|
+
export const bsonType = Symbol.for('@@mdb.bson.type');
|
|
23
|
+
|
|
5
24
|
/** @public */
|
|
6
25
|
export abstract class BSONValue {
|
|
7
26
|
/** @public */
|
|
8
|
-
public abstract get _bsontype():
|
|
27
|
+
public abstract get _bsontype(): BSONTypeTag;
|
|
28
|
+
|
|
29
|
+
public get [bsonType](): this['_bsontype'] {
|
|
30
|
+
return this._bsontype;
|
|
31
|
+
}
|
|
9
32
|
|
|
10
33
|
/** @internal */
|
|
11
34
|
get [BSON_VERSION_SYMBOL](): typeof BSON_MAJOR_VERSION {
|
package/src/extended_json.ts
CHANGED
|
@@ -102,7 +102,6 @@ function deserializeValue(value: any, options: EJSONOptions = {}) {
|
|
|
102
102
|
}
|
|
103
103
|
if (in64BitRange) {
|
|
104
104
|
if (options.useBigInt64) {
|
|
105
|
-
// eslint-disable-next-line no-restricted-globals -- This is allowed here as useBigInt64=true
|
|
106
105
|
return BigInt(value);
|
|
107
106
|
}
|
|
108
107
|
return Long.fromNumber(value);
|
|
@@ -275,12 +274,10 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {
|
|
|
275
274
|
}
|
|
276
275
|
|
|
277
276
|
if (typeof value === 'bigint') {
|
|
278
|
-
/* eslint-disable no-restricted-globals -- This is allowed as we are accepting a bigint as input */
|
|
279
277
|
if (!options.relaxed) {
|
|
280
278
|
return { $numberLong: BigInt.asIntN(64, value).toString() };
|
|
281
279
|
}
|
|
282
280
|
return Number(BigInt.asIntN(64, value));
|
|
283
|
-
/* eslint-enable */
|
|
284
281
|
}
|
|
285
282
|
|
|
286
283
|
if (value instanceof RegExp || isRegExp(value)) {
|
package/src/long.ts
CHANGED
|
@@ -258,10 +258,8 @@ export class Long extends BSONValue {
|
|
|
258
258
|
* @returns The corresponding Long value
|
|
259
259
|
*/
|
|
260
260
|
static fromBigInt(value: bigint, unsigned?: boolean): Long {
|
|
261
|
-
|
|
262
|
-
const
|
|
263
|
-
// eslint-disable-next-line no-restricted-globals
|
|
264
|
-
const FROM_BIGINT_BIT_SHIFT = BigInt(32);
|
|
261
|
+
const FROM_BIGINT_BIT_MASK = 0xffffffffn;
|
|
262
|
+
const FROM_BIGINT_BIT_SHIFT = 32n;
|
|
265
263
|
return new Long(
|
|
266
264
|
Number(value & FROM_BIGINT_BIT_MASK),
|
|
267
265
|
Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK),
|
|
@@ -366,7 +364,7 @@ export class Long extends BSONValue {
|
|
|
366
364
|
let unsigned = false;
|
|
367
365
|
if (typeof unsignedOrRadix === 'number') {
|
|
368
366
|
// For goog.math.long compatibility
|
|
369
|
-
(radix = unsignedOrRadix), (unsignedOrRadix = false);
|
|
367
|
+
((radix = unsignedOrRadix), (unsignedOrRadix = false));
|
|
370
368
|
} else {
|
|
371
369
|
unsigned = !!unsignedOrRadix;
|
|
372
370
|
}
|
|
@@ -456,7 +454,7 @@ export class Long extends BSONValue {
|
|
|
456
454
|
let unsigned = false;
|
|
457
455
|
if (typeof unsignedOrRadix === 'number') {
|
|
458
456
|
// For goog.math.long compatibility
|
|
459
|
-
(radix = unsignedOrRadix), (unsignedOrRadix = false);
|
|
457
|
+
((radix = unsignedOrRadix), (unsignedOrRadix = false));
|
|
460
458
|
} else {
|
|
461
459
|
unsigned = !!unsignedOrRadix;
|
|
462
460
|
}
|
|
@@ -1077,7 +1075,6 @@ export class Long extends BSONValue {
|
|
|
1077
1075
|
|
|
1078
1076
|
/** Converts the Long to a BigInt (arbitrary precision). */
|
|
1079
1077
|
toBigInt(): bigint {
|
|
1080
|
-
// eslint-disable-next-line no-restricted-globals -- This is allowed here as it is explicitly requesting a bigint
|
|
1081
1078
|
return BigInt(this.toString());
|
|
1082
1079
|
}
|
|
1083
1080
|
|
|
@@ -1223,10 +1220,8 @@ export class Long extends BSONValue {
|
|
|
1223
1220
|
}
|
|
1224
1221
|
|
|
1225
1222
|
if (useBigInt64) {
|
|
1226
|
-
/* eslint-disable no-restricted-globals -- Can use BigInt here as useBigInt64=true */
|
|
1227
1223
|
const bigIntResult = BigInt(doc.$numberLong);
|
|
1228
1224
|
return BigInt.asIntN(64, bigIntResult);
|
|
1229
|
-
/* eslint-enable */
|
|
1230
1225
|
}
|
|
1231
1226
|
|
|
1232
1227
|
const longResult = Long.fromString(doc.$numberLong);
|
package/src/objectid.ts
CHANGED
|
@@ -7,9 +7,6 @@ import { NumberUtils } from './utils/number_utils';
|
|
|
7
7
|
// Unique sequence for the current process (initialized on first use)
|
|
8
8
|
let PROCESS_UNIQUE: Uint8Array | null = null;
|
|
9
9
|
|
|
10
|
-
/** ObjectId hexString cache @internal */
|
|
11
|
-
const __idCache = new WeakMap(); // TODO(NODE-6549): convert this to #__id private field when target updated to ES2022
|
|
12
|
-
|
|
13
10
|
/** @public */
|
|
14
11
|
export interface ObjectIdLike {
|
|
15
12
|
id: string | Uint8Array;
|
|
@@ -35,18 +32,22 @@ export class ObjectId extends BSONValue {
|
|
|
35
32
|
/** @internal */
|
|
36
33
|
private static index = Math.floor(Math.random() * 0xffffff);
|
|
37
34
|
|
|
38
|
-
static cacheHexString: boolean;
|
|
35
|
+
static cacheHexString: boolean = false;
|
|
39
36
|
|
|
40
37
|
/** ObjectId Bytes @internal */
|
|
41
38
|
private buffer!: Uint8Array;
|
|
42
39
|
|
|
43
40
|
/**
|
|
44
|
-
*
|
|
41
|
+
* If hex string caching is enabled, contains the cached hex string. Otherwise, is null.
|
|
45
42
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
43
|
+
* Note that #hexString is populated lazily, and as a result simply checking `this.#hexString != null` is
|
|
44
|
+
* not sufficient to determine if caching is enabled. `ObjectId.prototype.isCached()` can be used to
|
|
45
|
+
* determine if the hex string has been cached yet for an ObjectId.
|
|
48
46
|
*/
|
|
49
|
-
|
|
47
|
+
#cachedHexString: string | null = null;
|
|
48
|
+
|
|
49
|
+
/** To generate a new ObjectId, use ObjectId() with no argument. */
|
|
50
|
+
constructor();
|
|
50
51
|
/**
|
|
51
52
|
* Create ObjectId from a 24 character hex string.
|
|
52
53
|
*
|
|
@@ -71,20 +72,18 @@ export class ObjectId extends BSONValue {
|
|
|
71
72
|
* @param inputId - A 12 byte binary Buffer.
|
|
72
73
|
*/
|
|
73
74
|
constructor(inputId: Uint8Array);
|
|
74
|
-
/** To generate a new ObjectId, use ObjectId() with no argument. */
|
|
75
|
-
constructor();
|
|
76
75
|
/**
|
|
77
76
|
* Implementation overload.
|
|
78
77
|
*
|
|
79
78
|
* @param inputId - All input types that are used in the constructor implementation.
|
|
80
79
|
*/
|
|
81
|
-
constructor(inputId?: string |
|
|
80
|
+
constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array);
|
|
82
81
|
/**
|
|
83
82
|
* Create a new ObjectId.
|
|
84
83
|
*
|
|
85
84
|
* @param inputId - An input value to create a new ObjectId from.
|
|
86
85
|
*/
|
|
87
|
-
constructor(inputId?: string |
|
|
86
|
+
constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array) {
|
|
88
87
|
super();
|
|
89
88
|
// workingId is set based on type of input and whether valid id exists for the input
|
|
90
89
|
let workingId;
|
|
@@ -102,19 +101,19 @@ export class ObjectId extends BSONValue {
|
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
// The following cases use workingId to construct an ObjectId
|
|
105
|
-
if (workingId == null
|
|
104
|
+
if (workingId == null) {
|
|
106
105
|
// The most common use case (blank id, new objectId instance)
|
|
107
106
|
// Generate a new id
|
|
108
|
-
this.buffer = ObjectId.generate(
|
|
107
|
+
this.buffer = ObjectId.generate();
|
|
109
108
|
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
|
110
|
-
// If
|
|
109
|
+
// If instanceof matches we can escape calling ensure buffer in Node.js environments
|
|
111
110
|
this.buffer = ByteUtils.toLocalBufferType(workingId);
|
|
112
111
|
} else if (typeof workingId === 'string') {
|
|
113
112
|
if (ObjectId.validateHexString(workingId)) {
|
|
114
113
|
this.buffer = ByteUtils.fromHex(workingId);
|
|
115
114
|
// If we are caching the hex string
|
|
116
115
|
if (ObjectId.cacheHexString) {
|
|
117
|
-
|
|
116
|
+
this.#cachedHexString = workingId;
|
|
118
117
|
}
|
|
119
118
|
} else {
|
|
120
119
|
throw new BSONError(
|
|
@@ -137,7 +136,7 @@ export class ObjectId extends BSONValue {
|
|
|
137
136
|
set id(value: Uint8Array) {
|
|
138
137
|
this.buffer = value;
|
|
139
138
|
if (ObjectId.cacheHexString) {
|
|
140
|
-
|
|
139
|
+
this.#cachedHexString = ByteUtils.toHex(value);
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
142
|
|
|
@@ -166,15 +165,12 @@ export class ObjectId extends BSONValue {
|
|
|
166
165
|
|
|
167
166
|
/** Returns the ObjectId id as a 24 lowercase character hex string representation */
|
|
168
167
|
toHexString(): string {
|
|
169
|
-
if (
|
|
170
|
-
const __id = __idCache.get(this);
|
|
171
|
-
if (__id) return __id;
|
|
172
|
-
}
|
|
168
|
+
if (this.#cachedHexString) return this.#cachedHexString.toLowerCase();
|
|
173
169
|
|
|
174
170
|
const hexString = ByteUtils.toHex(this.id);
|
|
175
171
|
|
|
176
172
|
if (ObjectId.cacheHexString) {
|
|
177
|
-
|
|
173
|
+
this.#cachedHexString = hexString;
|
|
178
174
|
}
|
|
179
175
|
|
|
180
176
|
return hexString;
|
|
@@ -349,7 +345,7 @@ export class ObjectId extends BSONValue {
|
|
|
349
345
|
* Checks if a value can be used to create a valid bson ObjectId
|
|
350
346
|
* @param id - any JS value
|
|
351
347
|
*/
|
|
352
|
-
static isValid(id: string |
|
|
348
|
+
static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean {
|
|
353
349
|
if (id == null) return false;
|
|
354
350
|
if (typeof id === 'string') return ObjectId.validateHexString(id);
|
|
355
351
|
|
|
@@ -372,9 +368,13 @@ export class ObjectId extends BSONValue {
|
|
|
372
368
|
return new ObjectId(doc.$oid);
|
|
373
369
|
}
|
|
374
370
|
|
|
375
|
-
/**
|
|
371
|
+
/**
|
|
372
|
+
* @internal
|
|
373
|
+
*
|
|
374
|
+
* used for testing
|
|
375
|
+
*/
|
|
376
376
|
private isCached(): boolean {
|
|
377
|
-
return ObjectId.cacheHexString &&
|
|
377
|
+
return ObjectId.cacheHexString && this.#cachedHexString != null;
|
|
378
378
|
}
|
|
379
379
|
|
|
380
380
|
/**
|
|
@@ -9,29 +9,31 @@ import { NumberUtils } from '../../utils/number_utils';
|
|
|
9
9
|
* - `minKey` is set to 255 so unsigned comparisons succeed
|
|
10
10
|
* - Modify with caution, double check the bundle contains literals
|
|
11
11
|
*/
|
|
12
|
-
const
|
|
13
|
-
double
|
|
14
|
-
string
|
|
15
|
-
object
|
|
16
|
-
array
|
|
17
|
-
binData
|
|
18
|
-
undefined
|
|
19
|
-
objectId
|
|
20
|
-
bool
|
|
21
|
-
date
|
|
22
|
-
null
|
|
23
|
-
regex
|
|
24
|
-
dbPointer
|
|
25
|
-
javascript
|
|
26
|
-
symbol
|
|
27
|
-
javascriptWithScope
|
|
28
|
-
int
|
|
29
|
-
timestamp
|
|
30
|
-
long
|
|
31
|
-
decimal
|
|
32
|
-
minKey
|
|
33
|
-
maxKey
|
|
34
|
-
}
|
|
12
|
+
const BSONElementType = {
|
|
13
|
+
double: 1,
|
|
14
|
+
string: 2,
|
|
15
|
+
object: 3,
|
|
16
|
+
array: 4,
|
|
17
|
+
binData: 5,
|
|
18
|
+
undefined: 6,
|
|
19
|
+
objectId: 7,
|
|
20
|
+
bool: 8,
|
|
21
|
+
date: 9,
|
|
22
|
+
null: 10,
|
|
23
|
+
regex: 11,
|
|
24
|
+
dbPointer: 12,
|
|
25
|
+
javascript: 13,
|
|
26
|
+
symbol: 14,
|
|
27
|
+
javascriptWithScope: 15,
|
|
28
|
+
int: 16,
|
|
29
|
+
timestamp: 17,
|
|
30
|
+
long: 18,
|
|
31
|
+
decimal: 19,
|
|
32
|
+
minKey: 255,
|
|
33
|
+
maxKey: 127
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
type BSONElementType = (typeof BSONElementType)[keyof typeof BSONElementType];
|
|
35
37
|
|
|
36
38
|
/**
|
|
37
39
|
* @public
|
package/src/timestamp.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
import { bsonType } from './bson_value';
|
|
1
2
|
import { BSONError } from './error';
|
|
2
3
|
import type { Int32 } from './int_32';
|
|
3
4
|
import { Long } from './long';
|
|
4
5
|
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
5
6
|
|
|
6
7
|
/** @public */
|
|
7
|
-
export type TimestampOverrides =
|
|
8
|
+
export type TimestampOverrides =
|
|
9
|
+
| '_bsontype'
|
|
10
|
+
| 'toExtendedJSON'
|
|
11
|
+
| 'fromExtendedJSON'
|
|
12
|
+
| 'inspect'
|
|
13
|
+
| typeof bsonType;
|
|
8
14
|
/** @public */
|
|
9
15
|
export type LongWithoutOverrides = new (
|
|
10
16
|
low: unknown,
|
|
@@ -35,6 +41,9 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
35
41
|
get _bsontype(): 'Timestamp' {
|
|
36
42
|
return 'Timestamp';
|
|
37
43
|
}
|
|
44
|
+
get [bsonType](): 'Timestamp' {
|
|
45
|
+
return 'Timestamp';
|
|
46
|
+
}
|
|
38
47
|
|
|
39
48
|
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
|
|
40
49
|
|
|
@@ -17,7 +17,7 @@ type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
|
|
|
17
17
|
from(array: number[]): NodeJsBuffer;
|
|
18
18
|
from(array: Uint8Array): NodeJsBuffer;
|
|
19
19
|
from(array: ArrayBuffer): NodeJsBuffer;
|
|
20
|
-
from(array:
|
|
20
|
+
from(array: ArrayBufferLike, byteOffset: number, byteLength: number): NodeJsBuffer;
|
|
21
21
|
from(base64: string, encoding: NodeJsEncoding): NodeJsBuffer;
|
|
22
22
|
byteLength(input: string, encoding: 'utf8'): number;
|
|
23
23
|
isBuffer(value: unknown): value is NodeJsBuffer;
|
|
@@ -26,34 +26,27 @@ type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
|
|
|
26
26
|
// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists
|
|
27
27
|
// Node.js global
|
|
28
28
|
declare const Buffer: NodeJsBufferConstructor;
|
|
29
|
-
declare const require: (mod: 'crypto') => { randomBytes: (byteLength: number) => Uint8Array };
|
|
30
29
|
|
|
31
30
|
/** @internal */
|
|
32
|
-
|
|
31
|
+
function nodejsMathRandomBytes(byteLength: number): NodeJsBuffer {
|
|
33
32
|
return nodeJsByteUtils.fromNumberArray(
|
|
34
33
|
Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
|
|
35
34
|
);
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
* when generating an es module bundle.
|
|
52
|
-
*/
|
|
53
|
-
const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => {
|
|
54
|
-
try {
|
|
55
|
-
return require('crypto').randomBytes;
|
|
56
|
-
} catch {
|
|
37
|
+
/** @internal */
|
|
38
|
+
function nodejsSecureRandomBytes(byteLength: number): NodeJsBuffer {
|
|
39
|
+
// @ts-expect-error: crypto.getRandomValues cannot actually be null here
|
|
40
|
+
return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const nodejsRandomBytes = (() => {
|
|
44
|
+
const { crypto } = globalThis as {
|
|
45
|
+
crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array };
|
|
46
|
+
};
|
|
47
|
+
if (crypto != null && typeof crypto.getRandomValues === 'function') {
|
|
48
|
+
return nodejsSecureRandomBytes;
|
|
49
|
+
} else {
|
|
57
50
|
return nodejsMathRandomBytes;
|
|
58
51
|
}
|
|
59
52
|
})();
|
|
@@ -83,7 +83,6 @@ export const NumberUtils: NumberUtils = {
|
|
|
83
83
|
|
|
84
84
|
/** Reads a little-endian 64-bit integer from source */
|
|
85
85
|
getBigInt64LE(source: Uint8Array, offset: number): bigint {
|
|
86
|
-
// eslint-disable-next-line no-restricted-globals
|
|
87
86
|
const hi = BigInt(
|
|
88
87
|
source[offset + 4] +
|
|
89
88
|
source[offset + 5] * 256 +
|
|
@@ -91,15 +90,14 @@ export const NumberUtils: NumberUtils = {
|
|
|
91
90
|
(source[offset + 7] << 24)
|
|
92
91
|
); // Overflow
|
|
93
92
|
|
|
94
|
-
// eslint-disable-next-line no-restricted-globals
|
|
95
93
|
const lo = BigInt(
|
|
96
94
|
source[offset] +
|
|
97
95
|
source[offset + 1] * 256 +
|
|
98
96
|
source[offset + 2] * 65536 +
|
|
99
97
|
source[offset + 3] * 16777216
|
|
100
98
|
);
|
|
101
|
-
|
|
102
|
-
return (hi <<
|
|
99
|
+
|
|
100
|
+
return (hi << 32n) + lo;
|
|
103
101
|
},
|
|
104
102
|
|
|
105
103
|
/** Reads a little-endian 64-bit float from source */
|
|
@@ -153,8 +151,7 @@ export const NumberUtils: NumberUtils = {
|
|
|
153
151
|
|
|
154
152
|
/** Write a little-endian 64-bit integer to source */
|
|
155
153
|
setBigInt64LE(destination: Uint8Array, offset: number, value: bigint): 8 {
|
|
156
|
-
|
|
157
|
-
const mask32bits = BigInt(0xffff_ffff);
|
|
154
|
+
const mask32bits = 0xffff_ffffn;
|
|
158
155
|
|
|
159
156
|
/** lower 32 bits */
|
|
160
157
|
let lo = Number(value & mask32bits);
|
|
@@ -166,13 +163,7 @@ export const NumberUtils: NumberUtils = {
|
|
|
166
163
|
lo >>= 8;
|
|
167
164
|
destination[offset + 3] = lo;
|
|
168
165
|
|
|
169
|
-
|
|
170
|
-
eslint-disable-next-line no-restricted-globals
|
|
171
|
-
-- This is allowed here as useBigInt64=true
|
|
172
|
-
|
|
173
|
-
upper 32 bits
|
|
174
|
-
*/
|
|
175
|
-
let hi = Number((value >> BigInt(32)) & mask32bits);
|
|
166
|
+
let hi = Number((value >> 32n) & mask32bits);
|
|
176
167
|
destination[offset + 4] = hi;
|
|
177
168
|
hi >>= 8;
|
|
178
169
|
destination[offset + 5] = hi;
|