@adobe-commerce/aio-toolkit 1.2.1 → 1.2.2
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/CHANGELOG.md +93 -0
- package/README.md +29 -20
- package/dist/index.d.mts +12 -7
- package/dist/index.d.ts +12 -7
- package/dist/index.js +3315 -95
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3309 -89
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2190,8 +2190,8 @@ var _FileRepository = class _FileRepository {
|
|
|
2190
2190
|
const existingFiles = await this.metadata();
|
|
2191
2191
|
if (existingFiles.length) {
|
|
2192
2192
|
for (const file of existingFiles) {
|
|
2193
|
-
const
|
|
2194
|
-
const data = JSON.parse(
|
|
2193
|
+
const buffer2 = await filesLib.read(`${file.name}`);
|
|
2194
|
+
const data = JSON.parse(buffer2.toString());
|
|
2195
2195
|
results.push({
|
|
2196
2196
|
...data,
|
|
2197
2197
|
createdAt: file.creationTime.toISOString(),
|
|
@@ -2225,8 +2225,8 @@ var _FileRepository = class _FileRepository {
|
|
|
2225
2225
|
const filesLib = await this.getFiles();
|
|
2226
2226
|
const existingFile = await filesLib.list(filepath);
|
|
2227
2227
|
if (existingFile.length) {
|
|
2228
|
-
const
|
|
2229
|
-
const data = JSON.parse(
|
|
2228
|
+
const buffer2 = await filesLib.read(filepath);
|
|
2229
|
+
const data = JSON.parse(buffer2.toString());
|
|
2230
2230
|
const fileProps = await filesLib.getProperties(filepath);
|
|
2231
2231
|
return {
|
|
2232
2232
|
...data,
|
|
@@ -2266,8 +2266,8 @@ var _FileRepository = class _FileRepository {
|
|
|
2266
2266
|
...payload
|
|
2267
2267
|
};
|
|
2268
2268
|
} else {
|
|
2269
|
-
const
|
|
2270
|
-
const existingData = JSON.parse(
|
|
2269
|
+
const buffer2 = await filesLib.read(filepath);
|
|
2270
|
+
const existingData = JSON.parse(buffer2.toString());
|
|
2271
2271
|
payload = { ...existingData, ...payload };
|
|
2272
2272
|
}
|
|
2273
2273
|
} else {
|
|
@@ -2326,6 +2326,3177 @@ __name(_FileRepository, "FileRepository");
|
|
|
2326
2326
|
var FileRepository = _FileRepository;
|
|
2327
2327
|
var file_repository_default = FileRepository;
|
|
2328
2328
|
|
|
2329
|
+
// node_modules/bson/lib/bson.node.mjs
|
|
2330
|
+
import { randomBytes } from "crypto";
|
|
2331
|
+
var TypedArrayPrototypeGetSymbolToStringTag = (() => {
|
|
2332
|
+
const g = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
|
|
2333
|
+
return (value) => g.call(value);
|
|
2334
|
+
})();
|
|
2335
|
+
function isUint8Array(value) {
|
|
2336
|
+
return TypedArrayPrototypeGetSymbolToStringTag(value) === "Uint8Array";
|
|
2337
|
+
}
|
|
2338
|
+
__name(isUint8Array, "isUint8Array");
|
|
2339
|
+
function isAnyArrayBuffer(value) {
|
|
2340
|
+
return typeof value === "object" && value != null && Symbol.toStringTag in value && (value[Symbol.toStringTag] === "ArrayBuffer" || value[Symbol.toStringTag] === "SharedArrayBuffer");
|
|
2341
|
+
}
|
|
2342
|
+
__name(isAnyArrayBuffer, "isAnyArrayBuffer");
|
|
2343
|
+
function isRegExp(regexp) {
|
|
2344
|
+
return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === "[object RegExp]";
|
|
2345
|
+
}
|
|
2346
|
+
__name(isRegExp, "isRegExp");
|
|
2347
|
+
function isMap(value) {
|
|
2348
|
+
return typeof value === "object" && value != null && Symbol.toStringTag in value && value[Symbol.toStringTag] === "Map";
|
|
2349
|
+
}
|
|
2350
|
+
__name(isMap, "isMap");
|
|
2351
|
+
function isDate(date) {
|
|
2352
|
+
return date instanceof Date || Object.prototype.toString.call(date) === "[object Date]";
|
|
2353
|
+
}
|
|
2354
|
+
__name(isDate, "isDate");
|
|
2355
|
+
function defaultInspect(x, _options) {
|
|
2356
|
+
return JSON.stringify(x, (k, v) => {
|
|
2357
|
+
if (typeof v === "bigint") {
|
|
2358
|
+
return { $numberLong: `${v}` };
|
|
2359
|
+
} else if (isMap(v)) {
|
|
2360
|
+
return Object.fromEntries(v);
|
|
2361
|
+
}
|
|
2362
|
+
return v;
|
|
2363
|
+
});
|
|
2364
|
+
}
|
|
2365
|
+
__name(defaultInspect, "defaultInspect");
|
|
2366
|
+
function getStylizeFunction(options) {
|
|
2367
|
+
const stylizeExists = options != null && typeof options === "object" && "stylize" in options && typeof options.stylize === "function";
|
|
2368
|
+
if (stylizeExists) {
|
|
2369
|
+
return options.stylize;
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
__name(getStylizeFunction, "getStylizeFunction");
|
|
2373
|
+
var BSON_MAJOR_VERSION = 6;
|
|
2374
|
+
var BSON_VERSION_SYMBOL = /* @__PURE__ */ Symbol.for("@@mdb.bson.version");
|
|
2375
|
+
var BSON_INT32_MAX = 2147483647;
|
|
2376
|
+
var BSON_INT32_MIN = -2147483648;
|
|
2377
|
+
var BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
|
2378
|
+
var BSON_INT64_MIN = -Math.pow(2, 63);
|
|
2379
|
+
var JS_INT_MAX = Math.pow(2, 53);
|
|
2380
|
+
var JS_INT_MIN = -Math.pow(2, 53);
|
|
2381
|
+
var BSON_BINARY_SUBTYPE_UUID_NEW = 4;
|
|
2382
|
+
var BSONType = Object.freeze({
|
|
2383
|
+
double: 1,
|
|
2384
|
+
string: 2,
|
|
2385
|
+
object: 3,
|
|
2386
|
+
array: 4,
|
|
2387
|
+
binData: 5,
|
|
2388
|
+
undefined: 6,
|
|
2389
|
+
objectId: 7,
|
|
2390
|
+
bool: 8,
|
|
2391
|
+
date: 9,
|
|
2392
|
+
null: 10,
|
|
2393
|
+
regex: 11,
|
|
2394
|
+
dbPointer: 12,
|
|
2395
|
+
javascript: 13,
|
|
2396
|
+
symbol: 14,
|
|
2397
|
+
javascriptWithScope: 15,
|
|
2398
|
+
int: 16,
|
|
2399
|
+
timestamp: 17,
|
|
2400
|
+
long: 18,
|
|
2401
|
+
decimal: 19,
|
|
2402
|
+
minKey: -1,
|
|
2403
|
+
maxKey: 127
|
|
2404
|
+
});
|
|
2405
|
+
var _BSONError = class _BSONError extends Error {
|
|
2406
|
+
get bsonError() {
|
|
2407
|
+
return true;
|
|
2408
|
+
}
|
|
2409
|
+
get name() {
|
|
2410
|
+
return "BSONError";
|
|
2411
|
+
}
|
|
2412
|
+
constructor(message, options) {
|
|
2413
|
+
super(message, options);
|
|
2414
|
+
}
|
|
2415
|
+
static isBSONError(value) {
|
|
2416
|
+
return value != null && typeof value === "object" && "bsonError" in value && value.bsonError === true && "name" in value && "message" in value && "stack" in value;
|
|
2417
|
+
}
|
|
2418
|
+
};
|
|
2419
|
+
__name(_BSONError, "BSONError");
|
|
2420
|
+
var BSONError = _BSONError;
|
|
2421
|
+
var _BSONVersionError = class _BSONVersionError extends BSONError {
|
|
2422
|
+
get name() {
|
|
2423
|
+
return "BSONVersionError";
|
|
2424
|
+
}
|
|
2425
|
+
constructor() {
|
|
2426
|
+
super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
|
|
2427
|
+
}
|
|
2428
|
+
};
|
|
2429
|
+
__name(_BSONVersionError, "BSONVersionError");
|
|
2430
|
+
var BSONVersionError = _BSONVersionError;
|
|
2431
|
+
var _BSONRuntimeError = class _BSONRuntimeError extends BSONError {
|
|
2432
|
+
get name() {
|
|
2433
|
+
return "BSONRuntimeError";
|
|
2434
|
+
}
|
|
2435
|
+
constructor(message) {
|
|
2436
|
+
super(message);
|
|
2437
|
+
}
|
|
2438
|
+
};
|
|
2439
|
+
__name(_BSONRuntimeError, "BSONRuntimeError");
|
|
2440
|
+
var BSONRuntimeError = _BSONRuntimeError;
|
|
2441
|
+
var _BSONOffsetError = class _BSONOffsetError extends BSONError {
|
|
2442
|
+
get name() {
|
|
2443
|
+
return "BSONOffsetError";
|
|
2444
|
+
}
|
|
2445
|
+
constructor(message, offset, options) {
|
|
2446
|
+
super(`${message}. offset: ${offset}`, options);
|
|
2447
|
+
this.offset = offset;
|
|
2448
|
+
}
|
|
2449
|
+
};
|
|
2450
|
+
__name(_BSONOffsetError, "BSONOffsetError");
|
|
2451
|
+
var BSONOffsetError = _BSONOffsetError;
|
|
2452
|
+
var TextDecoderFatal;
|
|
2453
|
+
var TextDecoderNonFatal;
|
|
2454
|
+
function parseUtf8(buffer2, start, end, fatal) {
|
|
2455
|
+
if (fatal) {
|
|
2456
|
+
TextDecoderFatal ?? (TextDecoderFatal = new TextDecoder("utf8", { fatal: true }));
|
|
2457
|
+
try {
|
|
2458
|
+
return TextDecoderFatal.decode(buffer2.subarray(start, end));
|
|
2459
|
+
} catch (cause) {
|
|
2460
|
+
throw new BSONError("Invalid UTF-8 string in BSON document", { cause });
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
TextDecoderNonFatal ?? (TextDecoderNonFatal = new TextDecoder("utf8", { fatal: false }));
|
|
2464
|
+
return TextDecoderNonFatal.decode(buffer2.subarray(start, end));
|
|
2465
|
+
}
|
|
2466
|
+
__name(parseUtf8, "parseUtf8");
|
|
2467
|
+
function tryReadBasicLatin(uint8array, start, end) {
|
|
2468
|
+
if (uint8array.length === 0) {
|
|
2469
|
+
return "";
|
|
2470
|
+
}
|
|
2471
|
+
const stringByteLength = end - start;
|
|
2472
|
+
if (stringByteLength === 0) {
|
|
2473
|
+
return "";
|
|
2474
|
+
}
|
|
2475
|
+
if (stringByteLength > 20) {
|
|
2476
|
+
return null;
|
|
2477
|
+
}
|
|
2478
|
+
if (stringByteLength === 1 && uint8array[start] < 128) {
|
|
2479
|
+
return String.fromCharCode(uint8array[start]);
|
|
2480
|
+
}
|
|
2481
|
+
if (stringByteLength === 2 && uint8array[start] < 128 && uint8array[start + 1] < 128) {
|
|
2482
|
+
return String.fromCharCode(uint8array[start]) + String.fromCharCode(uint8array[start + 1]);
|
|
2483
|
+
}
|
|
2484
|
+
if (stringByteLength === 3 && uint8array[start] < 128 && uint8array[start + 1] < 128 && uint8array[start + 2] < 128) {
|
|
2485
|
+
return String.fromCharCode(uint8array[start]) + String.fromCharCode(uint8array[start + 1]) + String.fromCharCode(uint8array[start + 2]);
|
|
2486
|
+
}
|
|
2487
|
+
const latinBytes = [];
|
|
2488
|
+
for (let i = start; i < end; i++) {
|
|
2489
|
+
const byte = uint8array[i];
|
|
2490
|
+
if (byte > 127) {
|
|
2491
|
+
return null;
|
|
2492
|
+
}
|
|
2493
|
+
latinBytes.push(byte);
|
|
2494
|
+
}
|
|
2495
|
+
return String.fromCharCode(...latinBytes);
|
|
2496
|
+
}
|
|
2497
|
+
__name(tryReadBasicLatin, "tryReadBasicLatin");
|
|
2498
|
+
function tryWriteBasicLatin(destination, source, offset) {
|
|
2499
|
+
if (source.length === 0)
|
|
2500
|
+
return 0;
|
|
2501
|
+
if (source.length > 25)
|
|
2502
|
+
return null;
|
|
2503
|
+
if (destination.length - offset < source.length)
|
|
2504
|
+
return null;
|
|
2505
|
+
for (let charOffset = 0, destinationOffset = offset; charOffset < source.length; charOffset++, destinationOffset++) {
|
|
2506
|
+
const char = source.charCodeAt(charOffset);
|
|
2507
|
+
if (char > 127)
|
|
2508
|
+
return null;
|
|
2509
|
+
destination[destinationOffset] = char;
|
|
2510
|
+
}
|
|
2511
|
+
return source.length;
|
|
2512
|
+
}
|
|
2513
|
+
__name(tryWriteBasicLatin, "tryWriteBasicLatin");
|
|
2514
|
+
var nodeJsByteUtils = {
|
|
2515
|
+
toLocalBufferType(potentialBuffer) {
|
|
2516
|
+
if (Buffer.isBuffer(potentialBuffer)) {
|
|
2517
|
+
return potentialBuffer;
|
|
2518
|
+
}
|
|
2519
|
+
if (ArrayBuffer.isView(potentialBuffer)) {
|
|
2520
|
+
return Buffer.from(potentialBuffer.buffer, potentialBuffer.byteOffset, potentialBuffer.byteLength);
|
|
2521
|
+
}
|
|
2522
|
+
const stringTag = potentialBuffer?.[Symbol.toStringTag] ?? Object.prototype.toString.call(potentialBuffer);
|
|
2523
|
+
if (stringTag === "ArrayBuffer" || stringTag === "SharedArrayBuffer" || stringTag === "[object ArrayBuffer]" || stringTag === "[object SharedArrayBuffer]") {
|
|
2524
|
+
return Buffer.from(potentialBuffer);
|
|
2525
|
+
}
|
|
2526
|
+
throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`);
|
|
2527
|
+
},
|
|
2528
|
+
allocate(size) {
|
|
2529
|
+
return Buffer.alloc(size);
|
|
2530
|
+
},
|
|
2531
|
+
allocateUnsafe(size) {
|
|
2532
|
+
return Buffer.allocUnsafe(size);
|
|
2533
|
+
},
|
|
2534
|
+
equals(a, b) {
|
|
2535
|
+
return nodeJsByteUtils.toLocalBufferType(a).equals(b);
|
|
2536
|
+
},
|
|
2537
|
+
fromNumberArray(array) {
|
|
2538
|
+
return Buffer.from(array);
|
|
2539
|
+
},
|
|
2540
|
+
fromBase64(base64) {
|
|
2541
|
+
return Buffer.from(base64, "base64");
|
|
2542
|
+
},
|
|
2543
|
+
toBase64(buffer2) {
|
|
2544
|
+
return nodeJsByteUtils.toLocalBufferType(buffer2).toString("base64");
|
|
2545
|
+
},
|
|
2546
|
+
fromISO88591(codePoints) {
|
|
2547
|
+
return Buffer.from(codePoints, "binary");
|
|
2548
|
+
},
|
|
2549
|
+
toISO88591(buffer2) {
|
|
2550
|
+
return nodeJsByteUtils.toLocalBufferType(buffer2).toString("binary");
|
|
2551
|
+
},
|
|
2552
|
+
fromHex(hex) {
|
|
2553
|
+
return Buffer.from(hex, "hex");
|
|
2554
|
+
},
|
|
2555
|
+
toHex(buffer2) {
|
|
2556
|
+
return nodeJsByteUtils.toLocalBufferType(buffer2).toString("hex");
|
|
2557
|
+
},
|
|
2558
|
+
toUTF8(buffer2, start, end, fatal) {
|
|
2559
|
+
const basicLatin = end - start <= 20 ? tryReadBasicLatin(buffer2, start, end) : null;
|
|
2560
|
+
if (basicLatin != null) {
|
|
2561
|
+
return basicLatin;
|
|
2562
|
+
}
|
|
2563
|
+
const string = nodeJsByteUtils.toLocalBufferType(buffer2).toString("utf8", start, end);
|
|
2564
|
+
if (fatal) {
|
|
2565
|
+
for (let i = 0; i < string.length; i++) {
|
|
2566
|
+
if (string.charCodeAt(i) === 65533) {
|
|
2567
|
+
parseUtf8(buffer2, start, end, true);
|
|
2568
|
+
break;
|
|
2569
|
+
}
|
|
2570
|
+
}
|
|
2571
|
+
}
|
|
2572
|
+
return string;
|
|
2573
|
+
},
|
|
2574
|
+
utf8ByteLength(input) {
|
|
2575
|
+
return Buffer.byteLength(input, "utf8");
|
|
2576
|
+
},
|
|
2577
|
+
encodeUTF8Into(buffer2, source, byteOffset) {
|
|
2578
|
+
const latinBytesWritten = tryWriteBasicLatin(buffer2, source, byteOffset);
|
|
2579
|
+
if (latinBytesWritten != null) {
|
|
2580
|
+
return latinBytesWritten;
|
|
2581
|
+
}
|
|
2582
|
+
return nodeJsByteUtils.toLocalBufferType(buffer2).write(source, byteOffset, void 0, "utf8");
|
|
2583
|
+
},
|
|
2584
|
+
randomBytes,
|
|
2585
|
+
swap32(buffer2) {
|
|
2586
|
+
return nodeJsByteUtils.toLocalBufferType(buffer2).swap32();
|
|
2587
|
+
}
|
|
2588
|
+
};
|
|
2589
|
+
function isReactNative() {
|
|
2590
|
+
const { navigator } = globalThis;
|
|
2591
|
+
return typeof navigator === "object" && navigator.product === "ReactNative";
|
|
2592
|
+
}
|
|
2593
|
+
__name(isReactNative, "isReactNative");
|
|
2594
|
+
function webMathRandomBytes(byteLength) {
|
|
2595
|
+
if (byteLength < 0) {
|
|
2596
|
+
throw new RangeError(`The argument 'byteLength' is invalid. Received ${byteLength}`);
|
|
2597
|
+
}
|
|
2598
|
+
return webByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
|
|
2599
|
+
}
|
|
2600
|
+
__name(webMathRandomBytes, "webMathRandomBytes");
|
|
2601
|
+
var webRandomBytes = (() => {
|
|
2602
|
+
const { crypto: crypto4 } = globalThis;
|
|
2603
|
+
if (crypto4 != null && typeof crypto4.getRandomValues === "function") {
|
|
2604
|
+
return (byteLength) => {
|
|
2605
|
+
return crypto4.getRandomValues(webByteUtils.allocate(byteLength));
|
|
2606
|
+
};
|
|
2607
|
+
} else {
|
|
2608
|
+
if (isReactNative()) {
|
|
2609
|
+
const { console: console2 } = globalThis;
|
|
2610
|
+
console2?.warn?.("BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.");
|
|
2611
|
+
}
|
|
2612
|
+
return webMathRandomBytes;
|
|
2613
|
+
}
|
|
2614
|
+
})();
|
|
2615
|
+
var HEX_DIGIT = /(\d|[a-f])/i;
|
|
2616
|
+
var webByteUtils = {
|
|
2617
|
+
toLocalBufferType(potentialUint8array) {
|
|
2618
|
+
const stringTag = potentialUint8array?.[Symbol.toStringTag] ?? Object.prototype.toString.call(potentialUint8array);
|
|
2619
|
+
if (stringTag === "Uint8Array") {
|
|
2620
|
+
return potentialUint8array;
|
|
2621
|
+
}
|
|
2622
|
+
if (ArrayBuffer.isView(potentialUint8array)) {
|
|
2623
|
+
return new Uint8Array(potentialUint8array.buffer.slice(potentialUint8array.byteOffset, potentialUint8array.byteOffset + potentialUint8array.byteLength));
|
|
2624
|
+
}
|
|
2625
|
+
if (stringTag === "ArrayBuffer" || stringTag === "SharedArrayBuffer" || stringTag === "[object ArrayBuffer]" || stringTag === "[object SharedArrayBuffer]") {
|
|
2626
|
+
return new Uint8Array(potentialUint8array);
|
|
2627
|
+
}
|
|
2628
|
+
throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`);
|
|
2629
|
+
},
|
|
2630
|
+
allocate(size) {
|
|
2631
|
+
if (typeof size !== "number") {
|
|
2632
|
+
throw new TypeError(`The "size" argument must be of type number. Received ${String(size)}`);
|
|
2633
|
+
}
|
|
2634
|
+
return new Uint8Array(size);
|
|
2635
|
+
},
|
|
2636
|
+
allocateUnsafe(size) {
|
|
2637
|
+
return webByteUtils.allocate(size);
|
|
2638
|
+
},
|
|
2639
|
+
equals(a, b) {
|
|
2640
|
+
if (a.byteLength !== b.byteLength) {
|
|
2641
|
+
return false;
|
|
2642
|
+
}
|
|
2643
|
+
for (let i = 0; i < a.byteLength; i++) {
|
|
2644
|
+
if (a[i] !== b[i]) {
|
|
2645
|
+
return false;
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
return true;
|
|
2649
|
+
},
|
|
2650
|
+
fromNumberArray(array) {
|
|
2651
|
+
return Uint8Array.from(array);
|
|
2652
|
+
},
|
|
2653
|
+
fromBase64(base64) {
|
|
2654
|
+
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
2655
|
+
},
|
|
2656
|
+
toBase64(uint8array) {
|
|
2657
|
+
return btoa(webByteUtils.toISO88591(uint8array));
|
|
2658
|
+
},
|
|
2659
|
+
fromISO88591(codePoints) {
|
|
2660
|
+
return Uint8Array.from(codePoints, (c) => c.charCodeAt(0) & 255);
|
|
2661
|
+
},
|
|
2662
|
+
toISO88591(uint8array) {
|
|
2663
|
+
return Array.from(Uint16Array.from(uint8array), (b) => String.fromCharCode(b)).join("");
|
|
2664
|
+
},
|
|
2665
|
+
fromHex(hex) {
|
|
2666
|
+
const evenLengthHex = hex.length % 2 === 0 ? hex : hex.slice(0, hex.length - 1);
|
|
2667
|
+
const buffer2 = [];
|
|
2668
|
+
for (let i = 0; i < evenLengthHex.length; i += 2) {
|
|
2669
|
+
const firstDigit = evenLengthHex[i];
|
|
2670
|
+
const secondDigit = evenLengthHex[i + 1];
|
|
2671
|
+
if (!HEX_DIGIT.test(firstDigit)) {
|
|
2672
|
+
break;
|
|
2673
|
+
}
|
|
2674
|
+
if (!HEX_DIGIT.test(secondDigit)) {
|
|
2675
|
+
break;
|
|
2676
|
+
}
|
|
2677
|
+
const hexDigit = Number.parseInt(`${firstDigit}${secondDigit}`, 16);
|
|
2678
|
+
buffer2.push(hexDigit);
|
|
2679
|
+
}
|
|
2680
|
+
return Uint8Array.from(buffer2);
|
|
2681
|
+
},
|
|
2682
|
+
toHex(uint8array) {
|
|
2683
|
+
return Array.from(uint8array, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
2684
|
+
},
|
|
2685
|
+
toUTF8(uint8array, start, end, fatal) {
|
|
2686
|
+
const basicLatin = end - start <= 20 ? tryReadBasicLatin(uint8array, start, end) : null;
|
|
2687
|
+
if (basicLatin != null) {
|
|
2688
|
+
return basicLatin;
|
|
2689
|
+
}
|
|
2690
|
+
return parseUtf8(uint8array, start, end, fatal);
|
|
2691
|
+
},
|
|
2692
|
+
utf8ByteLength(input) {
|
|
2693
|
+
return new TextEncoder().encode(input).byteLength;
|
|
2694
|
+
},
|
|
2695
|
+
encodeUTF8Into(uint8array, source, byteOffset) {
|
|
2696
|
+
const bytes = new TextEncoder().encode(source);
|
|
2697
|
+
uint8array.set(bytes, byteOffset);
|
|
2698
|
+
return bytes.byteLength;
|
|
2699
|
+
},
|
|
2700
|
+
randomBytes: webRandomBytes,
|
|
2701
|
+
swap32(buffer2) {
|
|
2702
|
+
if (buffer2.length % 4 !== 0) {
|
|
2703
|
+
throw new RangeError("Buffer size must be a multiple of 32-bits");
|
|
2704
|
+
}
|
|
2705
|
+
for (let i = 0; i < buffer2.length; i += 4) {
|
|
2706
|
+
const byte0 = buffer2[i];
|
|
2707
|
+
const byte1 = buffer2[i + 1];
|
|
2708
|
+
const byte2 = buffer2[i + 2];
|
|
2709
|
+
const byte3 = buffer2[i + 3];
|
|
2710
|
+
buffer2[i] = byte3;
|
|
2711
|
+
buffer2[i + 1] = byte2;
|
|
2712
|
+
buffer2[i + 2] = byte1;
|
|
2713
|
+
buffer2[i + 3] = byte0;
|
|
2714
|
+
}
|
|
2715
|
+
return buffer2;
|
|
2716
|
+
}
|
|
2717
|
+
};
|
|
2718
|
+
var hasGlobalBuffer = typeof Buffer === "function" && Buffer.prototype?._isBuffer !== true;
|
|
2719
|
+
var ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
|
|
2720
|
+
var _BSONValue = class _BSONValue {
|
|
2721
|
+
get [BSON_VERSION_SYMBOL]() {
|
|
2722
|
+
return BSON_MAJOR_VERSION;
|
|
2723
|
+
}
|
|
2724
|
+
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")](depth, options, inspect) {
|
|
2725
|
+
return this.inspect(depth, options, inspect);
|
|
2726
|
+
}
|
|
2727
|
+
};
|
|
2728
|
+
__name(_BSONValue, "BSONValue");
|
|
2729
|
+
var BSONValue = _BSONValue;
|
|
2730
|
+
var FLOAT = new Float64Array(1);
|
|
2731
|
+
var FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
|
|
2732
|
+
FLOAT[0] = -1;
|
|
2733
|
+
var isBigEndian = FLOAT_BYTES[7] === 0;
|
|
2734
|
+
var NumberUtils = {
|
|
2735
|
+
isBigEndian,
|
|
2736
|
+
getNonnegativeInt32LE(source, offset) {
|
|
2737
|
+
if (source[offset + 3] > 127) {
|
|
2738
|
+
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
|
|
2739
|
+
}
|
|
2740
|
+
return source[offset] | source[offset + 1] << 8 | source[offset + 2] << 16 | source[offset + 3] << 24;
|
|
2741
|
+
},
|
|
2742
|
+
getInt32LE(source, offset) {
|
|
2743
|
+
return source[offset] | source[offset + 1] << 8 | source[offset + 2] << 16 | source[offset + 3] << 24;
|
|
2744
|
+
},
|
|
2745
|
+
getUint32LE(source, offset) {
|
|
2746
|
+
return source[offset] + source[offset + 1] * 256 + source[offset + 2] * 65536 + source[offset + 3] * 16777216;
|
|
2747
|
+
},
|
|
2748
|
+
getUint32BE(source, offset) {
|
|
2749
|
+
return source[offset + 3] + source[offset + 2] * 256 + source[offset + 1] * 65536 + source[offset] * 16777216;
|
|
2750
|
+
},
|
|
2751
|
+
getBigInt64LE(source, offset) {
|
|
2752
|
+
const hi = BigInt(source[offset + 4] + source[offset + 5] * 256 + source[offset + 6] * 65536 + (source[offset + 7] << 24));
|
|
2753
|
+
const lo = BigInt(source[offset] + source[offset + 1] * 256 + source[offset + 2] * 65536 + source[offset + 3] * 16777216);
|
|
2754
|
+
return (hi << BigInt(32)) + lo;
|
|
2755
|
+
},
|
|
2756
|
+
getFloat64LE: isBigEndian ? (source, offset) => {
|
|
2757
|
+
FLOAT_BYTES[7] = source[offset];
|
|
2758
|
+
FLOAT_BYTES[6] = source[offset + 1];
|
|
2759
|
+
FLOAT_BYTES[5] = source[offset + 2];
|
|
2760
|
+
FLOAT_BYTES[4] = source[offset + 3];
|
|
2761
|
+
FLOAT_BYTES[3] = source[offset + 4];
|
|
2762
|
+
FLOAT_BYTES[2] = source[offset + 5];
|
|
2763
|
+
FLOAT_BYTES[1] = source[offset + 6];
|
|
2764
|
+
FLOAT_BYTES[0] = source[offset + 7];
|
|
2765
|
+
return FLOAT[0];
|
|
2766
|
+
} : (source, offset) => {
|
|
2767
|
+
FLOAT_BYTES[0] = source[offset];
|
|
2768
|
+
FLOAT_BYTES[1] = source[offset + 1];
|
|
2769
|
+
FLOAT_BYTES[2] = source[offset + 2];
|
|
2770
|
+
FLOAT_BYTES[3] = source[offset + 3];
|
|
2771
|
+
FLOAT_BYTES[4] = source[offset + 4];
|
|
2772
|
+
FLOAT_BYTES[5] = source[offset + 5];
|
|
2773
|
+
FLOAT_BYTES[6] = source[offset + 6];
|
|
2774
|
+
FLOAT_BYTES[7] = source[offset + 7];
|
|
2775
|
+
return FLOAT[0];
|
|
2776
|
+
},
|
|
2777
|
+
setInt32BE(destination, offset, value) {
|
|
2778
|
+
destination[offset + 3] = value;
|
|
2779
|
+
value >>>= 8;
|
|
2780
|
+
destination[offset + 2] = value;
|
|
2781
|
+
value >>>= 8;
|
|
2782
|
+
destination[offset + 1] = value;
|
|
2783
|
+
value >>>= 8;
|
|
2784
|
+
destination[offset] = value;
|
|
2785
|
+
return 4;
|
|
2786
|
+
},
|
|
2787
|
+
setInt32LE(destination, offset, value) {
|
|
2788
|
+
destination[offset] = value;
|
|
2789
|
+
value >>>= 8;
|
|
2790
|
+
destination[offset + 1] = value;
|
|
2791
|
+
value >>>= 8;
|
|
2792
|
+
destination[offset + 2] = value;
|
|
2793
|
+
value >>>= 8;
|
|
2794
|
+
destination[offset + 3] = value;
|
|
2795
|
+
return 4;
|
|
2796
|
+
},
|
|
2797
|
+
setBigInt64LE(destination, offset, value) {
|
|
2798
|
+
const mask32bits = BigInt(4294967295);
|
|
2799
|
+
let lo = Number(value & mask32bits);
|
|
2800
|
+
destination[offset] = lo;
|
|
2801
|
+
lo >>= 8;
|
|
2802
|
+
destination[offset + 1] = lo;
|
|
2803
|
+
lo >>= 8;
|
|
2804
|
+
destination[offset + 2] = lo;
|
|
2805
|
+
lo >>= 8;
|
|
2806
|
+
destination[offset + 3] = lo;
|
|
2807
|
+
let hi = Number(value >> BigInt(32) & mask32bits);
|
|
2808
|
+
destination[offset + 4] = hi;
|
|
2809
|
+
hi >>= 8;
|
|
2810
|
+
destination[offset + 5] = hi;
|
|
2811
|
+
hi >>= 8;
|
|
2812
|
+
destination[offset + 6] = hi;
|
|
2813
|
+
hi >>= 8;
|
|
2814
|
+
destination[offset + 7] = hi;
|
|
2815
|
+
return 8;
|
|
2816
|
+
},
|
|
2817
|
+
setFloat64LE: isBigEndian ? (destination, offset, value) => {
|
|
2818
|
+
FLOAT[0] = value;
|
|
2819
|
+
destination[offset] = FLOAT_BYTES[7];
|
|
2820
|
+
destination[offset + 1] = FLOAT_BYTES[6];
|
|
2821
|
+
destination[offset + 2] = FLOAT_BYTES[5];
|
|
2822
|
+
destination[offset + 3] = FLOAT_BYTES[4];
|
|
2823
|
+
destination[offset + 4] = FLOAT_BYTES[3];
|
|
2824
|
+
destination[offset + 5] = FLOAT_BYTES[2];
|
|
2825
|
+
destination[offset + 6] = FLOAT_BYTES[1];
|
|
2826
|
+
destination[offset + 7] = FLOAT_BYTES[0];
|
|
2827
|
+
return 8;
|
|
2828
|
+
} : (destination, offset, value) => {
|
|
2829
|
+
FLOAT[0] = value;
|
|
2830
|
+
destination[offset] = FLOAT_BYTES[0];
|
|
2831
|
+
destination[offset + 1] = FLOAT_BYTES[1];
|
|
2832
|
+
destination[offset + 2] = FLOAT_BYTES[2];
|
|
2833
|
+
destination[offset + 3] = FLOAT_BYTES[3];
|
|
2834
|
+
destination[offset + 4] = FLOAT_BYTES[4];
|
|
2835
|
+
destination[offset + 5] = FLOAT_BYTES[5];
|
|
2836
|
+
destination[offset + 6] = FLOAT_BYTES[6];
|
|
2837
|
+
destination[offset + 7] = FLOAT_BYTES[7];
|
|
2838
|
+
return 8;
|
|
2839
|
+
}
|
|
2840
|
+
};
|
|
2841
|
+
var _Binary = class _Binary extends BSONValue {
|
|
2842
|
+
get _bsontype() {
|
|
2843
|
+
return "Binary";
|
|
2844
|
+
}
|
|
2845
|
+
constructor(buffer2, subType) {
|
|
2846
|
+
super();
|
|
2847
|
+
if (!(buffer2 == null) && typeof buffer2 === "string" && !ArrayBuffer.isView(buffer2) && !isAnyArrayBuffer(buffer2) && !Array.isArray(buffer2)) {
|
|
2848
|
+
throw new BSONError("Binary can only be constructed from Uint8Array or number[]");
|
|
2849
|
+
}
|
|
2850
|
+
this.sub_type = subType ?? _Binary.BSON_BINARY_SUBTYPE_DEFAULT;
|
|
2851
|
+
if (buffer2 == null) {
|
|
2852
|
+
this.buffer = ByteUtils.allocate(_Binary.BUFFER_SIZE);
|
|
2853
|
+
this.position = 0;
|
|
2854
|
+
} else {
|
|
2855
|
+
this.buffer = Array.isArray(buffer2) ? ByteUtils.fromNumberArray(buffer2) : ByteUtils.toLocalBufferType(buffer2);
|
|
2856
|
+
this.position = this.buffer.byteLength;
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
put(byteValue) {
|
|
2860
|
+
if (typeof byteValue === "string" && byteValue.length !== 1) {
|
|
2861
|
+
throw new BSONError("only accepts single character String");
|
|
2862
|
+
} else if (typeof byteValue !== "number" && byteValue.length !== 1)
|
|
2863
|
+
throw new BSONError("only accepts single character Uint8Array or Array");
|
|
2864
|
+
let decodedByte;
|
|
2865
|
+
if (typeof byteValue === "string") {
|
|
2866
|
+
decodedByte = byteValue.charCodeAt(0);
|
|
2867
|
+
} else if (typeof byteValue === "number") {
|
|
2868
|
+
decodedByte = byteValue;
|
|
2869
|
+
} else {
|
|
2870
|
+
decodedByte = byteValue[0];
|
|
2871
|
+
}
|
|
2872
|
+
if (decodedByte < 0 || decodedByte > 255) {
|
|
2873
|
+
throw new BSONError("only accepts number in a valid unsigned byte range 0-255");
|
|
2874
|
+
}
|
|
2875
|
+
if (this.buffer.byteLength > this.position) {
|
|
2876
|
+
this.buffer[this.position++] = decodedByte;
|
|
2877
|
+
} else {
|
|
2878
|
+
const newSpace = ByteUtils.allocate(_Binary.BUFFER_SIZE + this.buffer.length);
|
|
2879
|
+
newSpace.set(this.buffer, 0);
|
|
2880
|
+
this.buffer = newSpace;
|
|
2881
|
+
this.buffer[this.position++] = decodedByte;
|
|
2882
|
+
}
|
|
2883
|
+
}
|
|
2884
|
+
write(sequence, offset) {
|
|
2885
|
+
offset = typeof offset === "number" ? offset : this.position;
|
|
2886
|
+
if (this.buffer.byteLength < offset + sequence.length) {
|
|
2887
|
+
const newSpace = ByteUtils.allocate(this.buffer.byteLength + sequence.length);
|
|
2888
|
+
newSpace.set(this.buffer, 0);
|
|
2889
|
+
this.buffer = newSpace;
|
|
2890
|
+
}
|
|
2891
|
+
if (ArrayBuffer.isView(sequence)) {
|
|
2892
|
+
this.buffer.set(ByteUtils.toLocalBufferType(sequence), offset);
|
|
2893
|
+
this.position = offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
|
|
2894
|
+
} else if (typeof sequence === "string") {
|
|
2895
|
+
throw new BSONError("input cannot be string");
|
|
2896
|
+
}
|
|
2897
|
+
}
|
|
2898
|
+
read(position, length) {
|
|
2899
|
+
length = length && length > 0 ? length : this.position;
|
|
2900
|
+
const end = position + length;
|
|
2901
|
+
return this.buffer.subarray(position, end > this.position ? this.position : end);
|
|
2902
|
+
}
|
|
2903
|
+
value() {
|
|
2904
|
+
return this.buffer.length === this.position ? this.buffer : this.buffer.subarray(0, this.position);
|
|
2905
|
+
}
|
|
2906
|
+
length() {
|
|
2907
|
+
return this.position;
|
|
2908
|
+
}
|
|
2909
|
+
toJSON() {
|
|
2910
|
+
return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
2911
|
+
}
|
|
2912
|
+
toString(encoding) {
|
|
2913
|
+
if (encoding === "hex")
|
|
2914
|
+
return ByteUtils.toHex(this.buffer.subarray(0, this.position));
|
|
2915
|
+
if (encoding === "base64")
|
|
2916
|
+
return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
2917
|
+
if (encoding === "utf8" || encoding === "utf-8")
|
|
2918
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
|
|
2919
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
|
|
2920
|
+
}
|
|
2921
|
+
toExtendedJSON(options) {
|
|
2922
|
+
options = options || {};
|
|
2923
|
+
if (this.sub_type === _Binary.SUBTYPE_VECTOR) {
|
|
2924
|
+
validateBinaryVector(this);
|
|
2925
|
+
}
|
|
2926
|
+
const base64String = ByteUtils.toBase64(this.buffer);
|
|
2927
|
+
const subType = Number(this.sub_type).toString(16);
|
|
2928
|
+
if (options.legacy) {
|
|
2929
|
+
return {
|
|
2930
|
+
$binary: base64String,
|
|
2931
|
+
$type: subType.length === 1 ? "0" + subType : subType
|
|
2932
|
+
};
|
|
2933
|
+
}
|
|
2934
|
+
return {
|
|
2935
|
+
$binary: {
|
|
2936
|
+
base64: base64String,
|
|
2937
|
+
subType: subType.length === 1 ? "0" + subType : subType
|
|
2938
|
+
}
|
|
2939
|
+
};
|
|
2940
|
+
}
|
|
2941
|
+
toUUID() {
|
|
2942
|
+
if (this.sub_type === _Binary.SUBTYPE_UUID) {
|
|
2943
|
+
return new UUID(this.buffer.subarray(0, this.position));
|
|
2944
|
+
}
|
|
2945
|
+
throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${_Binary.SUBTYPE_UUID}" is currently supported.`);
|
|
2946
|
+
}
|
|
2947
|
+
static createFromHexString(hex, subType) {
|
|
2948
|
+
return new _Binary(ByteUtils.fromHex(hex), subType);
|
|
2949
|
+
}
|
|
2950
|
+
static createFromBase64(base64, subType) {
|
|
2951
|
+
return new _Binary(ByteUtils.fromBase64(base64), subType);
|
|
2952
|
+
}
|
|
2953
|
+
static fromExtendedJSON(doc, options) {
|
|
2954
|
+
options = options || {};
|
|
2955
|
+
let data;
|
|
2956
|
+
let type;
|
|
2957
|
+
if ("$binary" in doc) {
|
|
2958
|
+
if (options.legacy && typeof doc.$binary === "string" && "$type" in doc) {
|
|
2959
|
+
type = doc.$type ? parseInt(doc.$type, 16) : 0;
|
|
2960
|
+
data = ByteUtils.fromBase64(doc.$binary);
|
|
2961
|
+
} else {
|
|
2962
|
+
if (typeof doc.$binary !== "string") {
|
|
2963
|
+
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
|
|
2964
|
+
data = ByteUtils.fromBase64(doc.$binary.base64);
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
} else if ("$uuid" in doc) {
|
|
2968
|
+
type = 4;
|
|
2969
|
+
data = UUID.bytesFromString(doc.$uuid);
|
|
2970
|
+
}
|
|
2971
|
+
if (!data) {
|
|
2972
|
+
throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
|
|
2973
|
+
}
|
|
2974
|
+
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new _Binary(data, type);
|
|
2975
|
+
}
|
|
2976
|
+
inspect(depth, options, inspect) {
|
|
2977
|
+
inspect ?? (inspect = defaultInspect);
|
|
2978
|
+
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
2979
|
+
const base64Arg = inspect(base64, options);
|
|
2980
|
+
const subTypeArg = inspect(this.sub_type, options);
|
|
2981
|
+
return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
|
|
2982
|
+
}
|
|
2983
|
+
toInt8Array() {
|
|
2984
|
+
if (this.sub_type !== _Binary.SUBTYPE_VECTOR) {
|
|
2985
|
+
throw new BSONError("Binary sub_type is not Vector");
|
|
2986
|
+
}
|
|
2987
|
+
if (this.buffer[0] !== _Binary.VECTOR_TYPE.Int8) {
|
|
2988
|
+
throw new BSONError("Binary datatype field is not Int8");
|
|
2989
|
+
}
|
|
2990
|
+
validateBinaryVector(this);
|
|
2991
|
+
return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
2992
|
+
}
|
|
2993
|
+
toFloat32Array() {
|
|
2994
|
+
if (this.sub_type !== _Binary.SUBTYPE_VECTOR) {
|
|
2995
|
+
throw new BSONError("Binary sub_type is not Vector");
|
|
2996
|
+
}
|
|
2997
|
+
if (this.buffer[0] !== _Binary.VECTOR_TYPE.Float32) {
|
|
2998
|
+
throw new BSONError("Binary datatype field is not Float32");
|
|
2999
|
+
}
|
|
3000
|
+
validateBinaryVector(this);
|
|
3001
|
+
const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
3002
|
+
if (NumberUtils.isBigEndian)
|
|
3003
|
+
ByteUtils.swap32(floatBytes);
|
|
3004
|
+
return new Float32Array(floatBytes.buffer);
|
|
3005
|
+
}
|
|
3006
|
+
toPackedBits() {
|
|
3007
|
+
if (this.sub_type !== _Binary.SUBTYPE_VECTOR) {
|
|
3008
|
+
throw new BSONError("Binary sub_type is not Vector");
|
|
3009
|
+
}
|
|
3010
|
+
if (this.buffer[0] !== _Binary.VECTOR_TYPE.PackedBit) {
|
|
3011
|
+
throw new BSONError("Binary datatype field is not packed bit");
|
|
3012
|
+
}
|
|
3013
|
+
validateBinaryVector(this);
|
|
3014
|
+
return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
3015
|
+
}
|
|
3016
|
+
toBits() {
|
|
3017
|
+
if (this.sub_type !== _Binary.SUBTYPE_VECTOR) {
|
|
3018
|
+
throw new BSONError("Binary sub_type is not Vector");
|
|
3019
|
+
}
|
|
3020
|
+
if (this.buffer[0] !== _Binary.VECTOR_TYPE.PackedBit) {
|
|
3021
|
+
throw new BSONError("Binary datatype field is not packed bit");
|
|
3022
|
+
}
|
|
3023
|
+
validateBinaryVector(this);
|
|
3024
|
+
const byteCount = this.length() - 2;
|
|
3025
|
+
const bitCount = byteCount * 8 - this.buffer[1];
|
|
3026
|
+
const bits = new Int8Array(bitCount);
|
|
3027
|
+
for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
|
|
3028
|
+
const byteOffset = bitOffset / 8 | 0;
|
|
3029
|
+
const byte = this.buffer[byteOffset + 2];
|
|
3030
|
+
const shift = 7 - bitOffset % 8;
|
|
3031
|
+
const bit = byte >> shift & 1;
|
|
3032
|
+
bits[bitOffset] = bit;
|
|
3033
|
+
}
|
|
3034
|
+
return bits;
|
|
3035
|
+
}
|
|
3036
|
+
static fromInt8Array(array) {
|
|
3037
|
+
const buffer2 = ByteUtils.allocate(array.byteLength + 2);
|
|
3038
|
+
buffer2[0] = _Binary.VECTOR_TYPE.Int8;
|
|
3039
|
+
buffer2[1] = 0;
|
|
3040
|
+
const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
3041
|
+
buffer2.set(intBytes, 2);
|
|
3042
|
+
const bin = new this(buffer2, this.SUBTYPE_VECTOR);
|
|
3043
|
+
validateBinaryVector(bin);
|
|
3044
|
+
return bin;
|
|
3045
|
+
}
|
|
3046
|
+
static fromFloat32Array(array) {
|
|
3047
|
+
const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
|
|
3048
|
+
binaryBytes[0] = _Binary.VECTOR_TYPE.Float32;
|
|
3049
|
+
binaryBytes[1] = 0;
|
|
3050
|
+
const floatBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
3051
|
+
binaryBytes.set(floatBytes, 2);
|
|
3052
|
+
if (NumberUtils.isBigEndian)
|
|
3053
|
+
ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
|
|
3054
|
+
const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
|
|
3055
|
+
validateBinaryVector(bin);
|
|
3056
|
+
return bin;
|
|
3057
|
+
}
|
|
3058
|
+
static fromPackedBits(array, padding = 0) {
|
|
3059
|
+
const buffer2 = ByteUtils.allocate(array.byteLength + 2);
|
|
3060
|
+
buffer2[0] = _Binary.VECTOR_TYPE.PackedBit;
|
|
3061
|
+
buffer2[1] = padding;
|
|
3062
|
+
buffer2.set(array, 2);
|
|
3063
|
+
const bin = new this(buffer2, this.SUBTYPE_VECTOR);
|
|
3064
|
+
validateBinaryVector(bin);
|
|
3065
|
+
return bin;
|
|
3066
|
+
}
|
|
3067
|
+
static fromBits(bits) {
|
|
3068
|
+
const byteLength = bits.length + 7 >>> 3;
|
|
3069
|
+
const bytes = new Uint8Array(byteLength + 2);
|
|
3070
|
+
bytes[0] = _Binary.VECTOR_TYPE.PackedBit;
|
|
3071
|
+
const remainder = bits.length % 8;
|
|
3072
|
+
bytes[1] = remainder === 0 ? 0 : 8 - remainder;
|
|
3073
|
+
for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
|
|
3074
|
+
const byteOffset = bitOffset >>> 3;
|
|
3075
|
+
const bit = bits[bitOffset];
|
|
3076
|
+
if (bit !== 0 && bit !== 1) {
|
|
3077
|
+
throw new BSONError(`Invalid bit value at ${bitOffset}: must be 0 or 1, found ${bits[bitOffset]}`);
|
|
3078
|
+
}
|
|
3079
|
+
if (bit === 0)
|
|
3080
|
+
continue;
|
|
3081
|
+
const shift = 7 - bitOffset % 8;
|
|
3082
|
+
bytes[byteOffset + 2] |= bit << shift;
|
|
3083
|
+
}
|
|
3084
|
+
return new this(bytes, _Binary.SUBTYPE_VECTOR);
|
|
3085
|
+
}
|
|
3086
|
+
};
|
|
3087
|
+
__name(_Binary, "Binary");
|
|
3088
|
+
var Binary = _Binary;
|
|
3089
|
+
Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
3090
|
+
Binary.BUFFER_SIZE = 256;
|
|
3091
|
+
Binary.SUBTYPE_DEFAULT = 0;
|
|
3092
|
+
Binary.SUBTYPE_FUNCTION = 1;
|
|
3093
|
+
Binary.SUBTYPE_BYTE_ARRAY = 2;
|
|
3094
|
+
Binary.SUBTYPE_UUID_OLD = 3;
|
|
3095
|
+
Binary.SUBTYPE_UUID = 4;
|
|
3096
|
+
Binary.SUBTYPE_MD5 = 5;
|
|
3097
|
+
Binary.SUBTYPE_ENCRYPTED = 6;
|
|
3098
|
+
Binary.SUBTYPE_COLUMN = 7;
|
|
3099
|
+
Binary.SUBTYPE_SENSITIVE = 8;
|
|
3100
|
+
Binary.SUBTYPE_VECTOR = 9;
|
|
3101
|
+
Binary.SUBTYPE_USER_DEFINED = 128;
|
|
3102
|
+
Binary.VECTOR_TYPE = Object.freeze({
|
|
3103
|
+
Int8: 3,
|
|
3104
|
+
Float32: 39,
|
|
3105
|
+
PackedBit: 16
|
|
3106
|
+
});
|
|
3107
|
+
function validateBinaryVector(vector) {
|
|
3108
|
+
if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
|
|
3109
|
+
return;
|
|
3110
|
+
const size = vector.position;
|
|
3111
|
+
const datatype = vector.buffer[0];
|
|
3112
|
+
const padding = vector.buffer[1];
|
|
3113
|
+
if ((datatype === Binary.VECTOR_TYPE.Float32 || datatype === Binary.VECTOR_TYPE.Int8) && padding !== 0) {
|
|
3114
|
+
throw new BSONError("Invalid Vector: padding must be zero for int8 and float32 vectors");
|
|
3115
|
+
}
|
|
3116
|
+
if (datatype === Binary.VECTOR_TYPE.Float32) {
|
|
3117
|
+
if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
|
|
3118
|
+
throw new BSONError("Invalid Vector: Float32 vector must contain a multiple of 4 bytes");
|
|
3119
|
+
}
|
|
3120
|
+
}
|
|
3121
|
+
if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
|
|
3122
|
+
throw new BSONError("Invalid Vector: padding must be zero for packed bit vectors that are empty");
|
|
3123
|
+
}
|
|
3124
|
+
if (datatype === Binary.VECTOR_TYPE.PackedBit && padding > 7) {
|
|
3125
|
+
throw new BSONError(`Invalid Vector: padding must be a value between 0 and 7. found: ${padding}`);
|
|
3126
|
+
}
|
|
3127
|
+
}
|
|
3128
|
+
__name(validateBinaryVector, "validateBinaryVector");
|
|
3129
|
+
var UUID_BYTE_LENGTH = 16;
|
|
3130
|
+
var UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
|
|
3131
|
+
var UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
|
|
3132
|
+
var _UUID = class _UUID extends Binary {
|
|
3133
|
+
constructor(input) {
|
|
3134
|
+
let bytes;
|
|
3135
|
+
if (input == null) {
|
|
3136
|
+
bytes = _UUID.generate();
|
|
3137
|
+
} else if (input instanceof _UUID) {
|
|
3138
|
+
bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
|
|
3139
|
+
} else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
|
|
3140
|
+
bytes = ByteUtils.toLocalBufferType(input);
|
|
3141
|
+
} else if (typeof input === "string") {
|
|
3142
|
+
bytes = _UUID.bytesFromString(input);
|
|
3143
|
+
} else {
|
|
3144
|
+
throw new BSONError("Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).");
|
|
3145
|
+
}
|
|
3146
|
+
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
|
|
3147
|
+
}
|
|
3148
|
+
get id() {
|
|
3149
|
+
return this.buffer;
|
|
3150
|
+
}
|
|
3151
|
+
set id(value) {
|
|
3152
|
+
this.buffer = value;
|
|
3153
|
+
}
|
|
3154
|
+
toHexString(includeDashes = true) {
|
|
3155
|
+
if (includeDashes) {
|
|
3156
|
+
return [
|
|
3157
|
+
ByteUtils.toHex(this.buffer.subarray(0, 4)),
|
|
3158
|
+
ByteUtils.toHex(this.buffer.subarray(4, 6)),
|
|
3159
|
+
ByteUtils.toHex(this.buffer.subarray(6, 8)),
|
|
3160
|
+
ByteUtils.toHex(this.buffer.subarray(8, 10)),
|
|
3161
|
+
ByteUtils.toHex(this.buffer.subarray(10, 16))
|
|
3162
|
+
].join("-");
|
|
3163
|
+
}
|
|
3164
|
+
return ByteUtils.toHex(this.buffer);
|
|
3165
|
+
}
|
|
3166
|
+
toString(encoding) {
|
|
3167
|
+
if (encoding === "hex")
|
|
3168
|
+
return ByteUtils.toHex(this.id);
|
|
3169
|
+
if (encoding === "base64")
|
|
3170
|
+
return ByteUtils.toBase64(this.id);
|
|
3171
|
+
return this.toHexString();
|
|
3172
|
+
}
|
|
3173
|
+
toJSON() {
|
|
3174
|
+
return this.toHexString();
|
|
3175
|
+
}
|
|
3176
|
+
equals(otherId) {
|
|
3177
|
+
if (!otherId) {
|
|
3178
|
+
return false;
|
|
3179
|
+
}
|
|
3180
|
+
if (otherId instanceof _UUID) {
|
|
3181
|
+
return ByteUtils.equals(otherId.id, this.id);
|
|
3182
|
+
}
|
|
3183
|
+
try {
|
|
3184
|
+
return ByteUtils.equals(new _UUID(otherId).id, this.id);
|
|
3185
|
+
} catch {
|
|
3186
|
+
return false;
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
toBinary() {
|
|
3190
|
+
return new Binary(this.id, Binary.SUBTYPE_UUID);
|
|
3191
|
+
}
|
|
3192
|
+
static generate() {
|
|
3193
|
+
const bytes = ByteUtils.randomBytes(UUID_BYTE_LENGTH);
|
|
3194
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
3195
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
3196
|
+
return bytes;
|
|
3197
|
+
}
|
|
3198
|
+
static isValid(input) {
|
|
3199
|
+
if (!input) {
|
|
3200
|
+
return false;
|
|
3201
|
+
}
|
|
3202
|
+
if (typeof input === "string") {
|
|
3203
|
+
return _UUID.isValidUUIDString(input);
|
|
3204
|
+
}
|
|
3205
|
+
if (isUint8Array(input)) {
|
|
3206
|
+
return input.byteLength === UUID_BYTE_LENGTH;
|
|
3207
|
+
}
|
|
3208
|
+
return input._bsontype === "Binary" && input.sub_type === this.SUBTYPE_UUID && input.buffer.byteLength === 16;
|
|
3209
|
+
}
|
|
3210
|
+
static createFromHexString(hexString) {
|
|
3211
|
+
const buffer2 = _UUID.bytesFromString(hexString);
|
|
3212
|
+
return new _UUID(buffer2);
|
|
3213
|
+
}
|
|
3214
|
+
static createFromBase64(base64) {
|
|
3215
|
+
return new _UUID(ByteUtils.fromBase64(base64));
|
|
3216
|
+
}
|
|
3217
|
+
static bytesFromString(representation) {
|
|
3218
|
+
if (!_UUID.isValidUUIDString(representation)) {
|
|
3219
|
+
throw new BSONError("UUID string representation must be 32 hex digits or canonical hyphenated representation");
|
|
3220
|
+
}
|
|
3221
|
+
return ByteUtils.fromHex(representation.replace(/-/g, ""));
|
|
3222
|
+
}
|
|
3223
|
+
static isValidUUIDString(representation) {
|
|
3224
|
+
return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
|
|
3225
|
+
}
|
|
3226
|
+
inspect(depth, options, inspect) {
|
|
3227
|
+
inspect ?? (inspect = defaultInspect);
|
|
3228
|
+
return `new UUID(${inspect(this.toHexString(), options)})`;
|
|
3229
|
+
}
|
|
3230
|
+
};
|
|
3231
|
+
__name(_UUID, "UUID");
|
|
3232
|
+
var UUID = _UUID;
|
|
3233
|
+
var _Code = class _Code extends BSONValue {
|
|
3234
|
+
get _bsontype() {
|
|
3235
|
+
return "Code";
|
|
3236
|
+
}
|
|
3237
|
+
constructor(code, scope) {
|
|
3238
|
+
super();
|
|
3239
|
+
this.code = code.toString();
|
|
3240
|
+
this.scope = scope ?? null;
|
|
3241
|
+
}
|
|
3242
|
+
toJSON() {
|
|
3243
|
+
if (this.scope != null) {
|
|
3244
|
+
return { code: this.code, scope: this.scope };
|
|
3245
|
+
}
|
|
3246
|
+
return { code: this.code };
|
|
3247
|
+
}
|
|
3248
|
+
toExtendedJSON() {
|
|
3249
|
+
if (this.scope) {
|
|
3250
|
+
return { $code: this.code, $scope: this.scope };
|
|
3251
|
+
}
|
|
3252
|
+
return { $code: this.code };
|
|
3253
|
+
}
|
|
3254
|
+
static fromExtendedJSON(doc) {
|
|
3255
|
+
return new _Code(doc.$code, doc.$scope);
|
|
3256
|
+
}
|
|
3257
|
+
inspect(depth, options, inspect) {
|
|
3258
|
+
inspect ?? (inspect = defaultInspect);
|
|
3259
|
+
let parametersString = inspect(this.code, options);
|
|
3260
|
+
const multiLineFn = parametersString.includes("\n");
|
|
3261
|
+
if (this.scope != null) {
|
|
3262
|
+
parametersString += `,${multiLineFn ? "\n" : " "}${inspect(this.scope, options)}`;
|
|
3263
|
+
}
|
|
3264
|
+
const endingNewline = multiLineFn && this.scope === null;
|
|
3265
|
+
return `new Code(${multiLineFn ? "\n" : ""}${parametersString}${endingNewline ? "\n" : ""})`;
|
|
3266
|
+
}
|
|
3267
|
+
};
|
|
3268
|
+
__name(_Code, "Code");
|
|
3269
|
+
var Code = _Code;
|
|
3270
|
+
function isDBRefLike(value) {
|
|
3271
|
+
return value != null && typeof value === "object" && "$id" in value && value.$id != null && "$ref" in value && typeof value.$ref === "string" && (!("$db" in value) || "$db" in value && typeof value.$db === "string");
|
|
3272
|
+
}
|
|
3273
|
+
__name(isDBRefLike, "isDBRefLike");
|
|
3274
|
+
var _DBRef = class _DBRef extends BSONValue {
|
|
3275
|
+
get _bsontype() {
|
|
3276
|
+
return "DBRef";
|
|
3277
|
+
}
|
|
3278
|
+
constructor(collection, oid, db, fields) {
|
|
3279
|
+
super();
|
|
3280
|
+
const parts = collection.split(".");
|
|
3281
|
+
if (parts.length === 2) {
|
|
3282
|
+
db = parts.shift();
|
|
3283
|
+
collection = parts.shift();
|
|
3284
|
+
}
|
|
3285
|
+
this.collection = collection;
|
|
3286
|
+
this.oid = oid;
|
|
3287
|
+
this.db = db;
|
|
3288
|
+
this.fields = fields || {};
|
|
3289
|
+
}
|
|
3290
|
+
get namespace() {
|
|
3291
|
+
return this.collection;
|
|
3292
|
+
}
|
|
3293
|
+
set namespace(value) {
|
|
3294
|
+
this.collection = value;
|
|
3295
|
+
}
|
|
3296
|
+
toJSON() {
|
|
3297
|
+
const o = Object.assign({
|
|
3298
|
+
$ref: this.collection,
|
|
3299
|
+
$id: this.oid
|
|
3300
|
+
}, this.fields);
|
|
3301
|
+
if (this.db != null)
|
|
3302
|
+
o.$db = this.db;
|
|
3303
|
+
return o;
|
|
3304
|
+
}
|
|
3305
|
+
toExtendedJSON(options) {
|
|
3306
|
+
options = options || {};
|
|
3307
|
+
let o = {
|
|
3308
|
+
$ref: this.collection,
|
|
3309
|
+
$id: this.oid
|
|
3310
|
+
};
|
|
3311
|
+
if (options.legacy) {
|
|
3312
|
+
return o;
|
|
3313
|
+
}
|
|
3314
|
+
if (this.db)
|
|
3315
|
+
o.$db = this.db;
|
|
3316
|
+
o = Object.assign(o, this.fields);
|
|
3317
|
+
return o;
|
|
3318
|
+
}
|
|
3319
|
+
static fromExtendedJSON(doc) {
|
|
3320
|
+
const copy = Object.assign({}, doc);
|
|
3321
|
+
delete copy.$ref;
|
|
3322
|
+
delete copy.$id;
|
|
3323
|
+
delete copy.$db;
|
|
3324
|
+
return new _DBRef(doc.$ref, doc.$id, doc.$db, copy);
|
|
3325
|
+
}
|
|
3326
|
+
inspect(depth, options, inspect) {
|
|
3327
|
+
inspect ?? (inspect = defaultInspect);
|
|
3328
|
+
const args = [
|
|
3329
|
+
inspect(this.namespace, options),
|
|
3330
|
+
inspect(this.oid, options),
|
|
3331
|
+
...this.db ? [inspect(this.db, options)] : [],
|
|
3332
|
+
...Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : []
|
|
3333
|
+
];
|
|
3334
|
+
args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
|
|
3335
|
+
return `new DBRef(${args.join(", ")})`;
|
|
3336
|
+
}
|
|
3337
|
+
};
|
|
3338
|
+
__name(_DBRef, "DBRef");
|
|
3339
|
+
var DBRef = _DBRef;
|
|
3340
|
+
function removeLeadingZerosAndExplicitPlus(str) {
|
|
3341
|
+
if (str === "") {
|
|
3342
|
+
return str;
|
|
3343
|
+
}
|
|
3344
|
+
let startIndex = 0;
|
|
3345
|
+
const isNegative = str[startIndex] === "-";
|
|
3346
|
+
const isExplicitlyPositive = str[startIndex] === "+";
|
|
3347
|
+
if (isExplicitlyPositive || isNegative) {
|
|
3348
|
+
startIndex += 1;
|
|
3349
|
+
}
|
|
3350
|
+
let foundInsignificantZero = false;
|
|
3351
|
+
for (; startIndex < str.length && str[startIndex] === "0"; ++startIndex) {
|
|
3352
|
+
foundInsignificantZero = true;
|
|
3353
|
+
}
|
|
3354
|
+
if (!foundInsignificantZero) {
|
|
3355
|
+
return isExplicitlyPositive ? str.slice(1) : str;
|
|
3356
|
+
}
|
|
3357
|
+
return `${isNegative ? "-" : ""}${str.length === startIndex ? "0" : str.slice(startIndex)}`;
|
|
3358
|
+
}
|
|
3359
|
+
__name(removeLeadingZerosAndExplicitPlus, "removeLeadingZerosAndExplicitPlus");
|
|
3360
|
+
function validateStringCharacters(str, radix) {
|
|
3361
|
+
radix = radix ?? 10;
|
|
3362
|
+
const validCharacters = "0123456789abcdefghijklmnopqrstuvwxyz".slice(0, radix);
|
|
3363
|
+
const regex = new RegExp(`[^-+${validCharacters}]`, "i");
|
|
3364
|
+
return regex.test(str) ? false : str;
|
|
3365
|
+
}
|
|
3366
|
+
__name(validateStringCharacters, "validateStringCharacters");
|
|
3367
|
+
var wasm = void 0;
|
|
3368
|
+
try {
|
|
3369
|
+
wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11])), {}).exports;
|
|
3370
|
+
} catch {
|
|
3371
|
+
}
|
|
3372
|
+
var TWO_PWR_16_DBL = 1 << 16;
|
|
3373
|
+
var TWO_PWR_24_DBL = 1 << 24;
|
|
3374
|
+
var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
|
|
3375
|
+
var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
|
|
3376
|
+
var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
|
|
3377
|
+
var INT_CACHE = {};
|
|
3378
|
+
var UINT_CACHE = {};
|
|
3379
|
+
var MAX_INT64_STRING_LENGTH = 20;
|
|
3380
|
+
var DECIMAL_REG_EX = /^(\+?0|(\+|-)?[1-9][0-9]*)$/;
|
|
3381
|
+
var _Long = class _Long extends BSONValue {
|
|
3382
|
+
get _bsontype() {
|
|
3383
|
+
return "Long";
|
|
3384
|
+
}
|
|
3385
|
+
get __isLong__() {
|
|
3386
|
+
return true;
|
|
3387
|
+
}
|
|
3388
|
+
constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
|
|
3389
|
+
super();
|
|
3390
|
+
const unsignedBool = typeof highOrUnsigned === "boolean" ? highOrUnsigned : Boolean(unsigned);
|
|
3391
|
+
const high = typeof highOrUnsigned === "number" ? highOrUnsigned : 0;
|
|
3392
|
+
const res = typeof lowOrValue === "string" ? _Long.fromString(lowOrValue, unsignedBool) : typeof lowOrValue === "bigint" ? _Long.fromBigInt(lowOrValue, unsignedBool) : { low: lowOrValue | 0, high: high | 0, unsigned: unsignedBool };
|
|
3393
|
+
this.low = res.low;
|
|
3394
|
+
this.high = res.high;
|
|
3395
|
+
this.unsigned = res.unsigned;
|
|
3396
|
+
}
|
|
3397
|
+
static fromBits(lowBits, highBits, unsigned) {
|
|
3398
|
+
return new _Long(lowBits, highBits, unsigned);
|
|
3399
|
+
}
|
|
3400
|
+
static fromInt(value, unsigned) {
|
|
3401
|
+
let obj, cachedObj, cache;
|
|
3402
|
+
if (unsigned) {
|
|
3403
|
+
value >>>= 0;
|
|
3404
|
+
if (cache = 0 <= value && value < 256) {
|
|
3405
|
+
cachedObj = UINT_CACHE[value];
|
|
3406
|
+
if (cachedObj)
|
|
3407
|
+
return cachedObj;
|
|
3408
|
+
}
|
|
3409
|
+
obj = _Long.fromBits(value, (value | 0) < 0 ? -1 : 0, true);
|
|
3410
|
+
if (cache)
|
|
3411
|
+
UINT_CACHE[value] = obj;
|
|
3412
|
+
return obj;
|
|
3413
|
+
} else {
|
|
3414
|
+
value |= 0;
|
|
3415
|
+
if (cache = -128 <= value && value < 128) {
|
|
3416
|
+
cachedObj = INT_CACHE[value];
|
|
3417
|
+
if (cachedObj)
|
|
3418
|
+
return cachedObj;
|
|
3419
|
+
}
|
|
3420
|
+
obj = _Long.fromBits(value, value < 0 ? -1 : 0, false);
|
|
3421
|
+
if (cache)
|
|
3422
|
+
INT_CACHE[value] = obj;
|
|
3423
|
+
return obj;
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
static fromNumber(value, unsigned) {
|
|
3427
|
+
if (isNaN(value))
|
|
3428
|
+
return unsigned ? _Long.UZERO : _Long.ZERO;
|
|
3429
|
+
if (unsigned) {
|
|
3430
|
+
if (value < 0)
|
|
3431
|
+
return _Long.UZERO;
|
|
3432
|
+
if (value >= TWO_PWR_64_DBL)
|
|
3433
|
+
return _Long.MAX_UNSIGNED_VALUE;
|
|
3434
|
+
} else {
|
|
3435
|
+
if (value <= -9223372036854776e3)
|
|
3436
|
+
return _Long.MIN_VALUE;
|
|
3437
|
+
if (value + 1 >= TWO_PWR_63_DBL)
|
|
3438
|
+
return _Long.MAX_VALUE;
|
|
3439
|
+
}
|
|
3440
|
+
if (value < 0)
|
|
3441
|
+
return _Long.fromNumber(-value, unsigned).neg();
|
|
3442
|
+
return _Long.fromBits(value % TWO_PWR_32_DBL | 0, value / TWO_PWR_32_DBL | 0, unsigned);
|
|
3443
|
+
}
|
|
3444
|
+
static fromBigInt(value, unsigned) {
|
|
3445
|
+
const FROM_BIGINT_BIT_MASK = BigInt(4294967295);
|
|
3446
|
+
const FROM_BIGINT_BIT_SHIFT = BigInt(32);
|
|
3447
|
+
return new _Long(Number(value & FROM_BIGINT_BIT_MASK), Number(value >> FROM_BIGINT_BIT_SHIFT & FROM_BIGINT_BIT_MASK), unsigned);
|
|
3448
|
+
}
|
|
3449
|
+
static _fromString(str, unsigned, radix) {
|
|
3450
|
+
if (str.length === 0)
|
|
3451
|
+
throw new BSONError("empty string");
|
|
3452
|
+
if (radix < 2 || 36 < radix)
|
|
3453
|
+
throw new BSONError("radix");
|
|
3454
|
+
let p;
|
|
3455
|
+
if ((p = str.indexOf("-")) > 0)
|
|
3456
|
+
throw new BSONError("interior hyphen");
|
|
3457
|
+
else if (p === 0) {
|
|
3458
|
+
return _Long._fromString(str.substring(1), unsigned, radix).neg();
|
|
3459
|
+
}
|
|
3460
|
+
const radixToPower = _Long.fromNumber(Math.pow(radix, 8));
|
|
3461
|
+
let result = _Long.ZERO;
|
|
3462
|
+
for (let i = 0; i < str.length; i += 8) {
|
|
3463
|
+
const size = Math.min(8, str.length - i), value = parseInt(str.substring(i, i + size), radix);
|
|
3464
|
+
if (size < 8) {
|
|
3465
|
+
const power = _Long.fromNumber(Math.pow(radix, size));
|
|
3466
|
+
result = result.mul(power).add(_Long.fromNumber(value));
|
|
3467
|
+
} else {
|
|
3468
|
+
result = result.mul(radixToPower);
|
|
3469
|
+
result = result.add(_Long.fromNumber(value));
|
|
3470
|
+
}
|
|
3471
|
+
}
|
|
3472
|
+
result.unsigned = unsigned;
|
|
3473
|
+
return result;
|
|
3474
|
+
}
|
|
3475
|
+
static fromStringStrict(str, unsignedOrRadix, radix) {
|
|
3476
|
+
let unsigned = false;
|
|
3477
|
+
if (typeof unsignedOrRadix === "number") {
|
|
3478
|
+
radix = unsignedOrRadix, unsignedOrRadix = false;
|
|
3479
|
+
} else {
|
|
3480
|
+
unsigned = !!unsignedOrRadix;
|
|
3481
|
+
}
|
|
3482
|
+
radix ?? (radix = 10);
|
|
3483
|
+
if (str.trim() !== str) {
|
|
3484
|
+
throw new BSONError(`Input: '${str}' contains leading and/or trailing whitespace`);
|
|
3485
|
+
}
|
|
3486
|
+
if (!validateStringCharacters(str, radix)) {
|
|
3487
|
+
throw new BSONError(`Input: '${str}' contains invalid characters for radix: ${radix}`);
|
|
3488
|
+
}
|
|
3489
|
+
const cleanedStr = removeLeadingZerosAndExplicitPlus(str);
|
|
3490
|
+
const result = _Long._fromString(cleanedStr, unsigned, radix);
|
|
3491
|
+
if (result.toString(radix).toLowerCase() !== cleanedStr.toLowerCase()) {
|
|
3492
|
+
throw new BSONError(`Input: ${str} is not representable as ${result.unsigned ? "an unsigned" : "a signed"} 64-bit Long ${radix != null ? `with radix: ${radix}` : ""}`);
|
|
3493
|
+
}
|
|
3494
|
+
return result;
|
|
3495
|
+
}
|
|
3496
|
+
static fromString(str, unsignedOrRadix, radix) {
|
|
3497
|
+
let unsigned = false;
|
|
3498
|
+
if (typeof unsignedOrRadix === "number") {
|
|
3499
|
+
radix = unsignedOrRadix, unsignedOrRadix = false;
|
|
3500
|
+
} else {
|
|
3501
|
+
unsigned = !!unsignedOrRadix;
|
|
3502
|
+
}
|
|
3503
|
+
radix ?? (radix = 10);
|
|
3504
|
+
if (str === "NaN" && radix < 24) {
|
|
3505
|
+
return _Long.ZERO;
|
|
3506
|
+
} else if ((str === "Infinity" || str === "+Infinity" || str === "-Infinity") && radix < 35) {
|
|
3507
|
+
return _Long.ZERO;
|
|
3508
|
+
}
|
|
3509
|
+
return _Long._fromString(str, unsigned, radix);
|
|
3510
|
+
}
|
|
3511
|
+
static fromBytes(bytes, unsigned, le) {
|
|
3512
|
+
return le ? _Long.fromBytesLE(bytes, unsigned) : _Long.fromBytesBE(bytes, unsigned);
|
|
3513
|
+
}
|
|
3514
|
+
static fromBytesLE(bytes, unsigned) {
|
|
3515
|
+
return new _Long(bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24, bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24, unsigned);
|
|
3516
|
+
}
|
|
3517
|
+
static fromBytesBE(bytes, unsigned) {
|
|
3518
|
+
return new _Long(bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7], bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], unsigned);
|
|
3519
|
+
}
|
|
3520
|
+
static isLong(value) {
|
|
3521
|
+
return value != null && typeof value === "object" && "__isLong__" in value && value.__isLong__ === true;
|
|
3522
|
+
}
|
|
3523
|
+
static fromValue(val, unsigned) {
|
|
3524
|
+
if (typeof val === "number")
|
|
3525
|
+
return _Long.fromNumber(val, unsigned);
|
|
3526
|
+
if (typeof val === "string")
|
|
3527
|
+
return _Long.fromString(val, unsigned);
|
|
3528
|
+
return _Long.fromBits(val.low, val.high, typeof unsigned === "boolean" ? unsigned : val.unsigned);
|
|
3529
|
+
}
|
|
3530
|
+
add(addend) {
|
|
3531
|
+
if (!_Long.isLong(addend))
|
|
3532
|
+
addend = _Long.fromValue(addend);
|
|
3533
|
+
const a48 = this.high >>> 16;
|
|
3534
|
+
const a32 = this.high & 65535;
|
|
3535
|
+
const a16 = this.low >>> 16;
|
|
3536
|
+
const a00 = this.low & 65535;
|
|
3537
|
+
const b48 = addend.high >>> 16;
|
|
3538
|
+
const b32 = addend.high & 65535;
|
|
3539
|
+
const b16 = addend.low >>> 16;
|
|
3540
|
+
const b00 = addend.low & 65535;
|
|
3541
|
+
let c48 = 0, c32 = 0, c16 = 0, c00 = 0;
|
|
3542
|
+
c00 += a00 + b00;
|
|
3543
|
+
c16 += c00 >>> 16;
|
|
3544
|
+
c00 &= 65535;
|
|
3545
|
+
c16 += a16 + b16;
|
|
3546
|
+
c32 += c16 >>> 16;
|
|
3547
|
+
c16 &= 65535;
|
|
3548
|
+
c32 += a32 + b32;
|
|
3549
|
+
c48 += c32 >>> 16;
|
|
3550
|
+
c32 &= 65535;
|
|
3551
|
+
c48 += a48 + b48;
|
|
3552
|
+
c48 &= 65535;
|
|
3553
|
+
return _Long.fromBits(c16 << 16 | c00, c48 << 16 | c32, this.unsigned);
|
|
3554
|
+
}
|
|
3555
|
+
and(other) {
|
|
3556
|
+
if (!_Long.isLong(other))
|
|
3557
|
+
other = _Long.fromValue(other);
|
|
3558
|
+
return _Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned);
|
|
3559
|
+
}
|
|
3560
|
+
compare(other) {
|
|
3561
|
+
if (!_Long.isLong(other))
|
|
3562
|
+
other = _Long.fromValue(other);
|
|
3563
|
+
if (this.eq(other))
|
|
3564
|
+
return 0;
|
|
3565
|
+
const thisNeg = this.isNegative(), otherNeg = other.isNegative();
|
|
3566
|
+
if (thisNeg && !otherNeg)
|
|
3567
|
+
return -1;
|
|
3568
|
+
if (!thisNeg && otherNeg)
|
|
3569
|
+
return 1;
|
|
3570
|
+
if (!this.unsigned)
|
|
3571
|
+
return this.sub(other).isNegative() ? -1 : 1;
|
|
3572
|
+
return other.high >>> 0 > this.high >>> 0 || other.high === this.high && other.low >>> 0 > this.low >>> 0 ? -1 : 1;
|
|
3573
|
+
}
|
|
3574
|
+
comp(other) {
|
|
3575
|
+
return this.compare(other);
|
|
3576
|
+
}
|
|
3577
|
+
divide(divisor) {
|
|
3578
|
+
if (!_Long.isLong(divisor))
|
|
3579
|
+
divisor = _Long.fromValue(divisor);
|
|
3580
|
+
if (divisor.isZero())
|
|
3581
|
+
throw new BSONError("division by zero");
|
|
3582
|
+
if (wasm) {
|
|
3583
|
+
if (!this.unsigned && this.high === -2147483648 && divisor.low === -1 && divisor.high === -1) {
|
|
3584
|
+
return this;
|
|
3585
|
+
}
|
|
3586
|
+
const low = (this.unsigned ? wasm.div_u : wasm.div_s)(this.low, this.high, divisor.low, divisor.high);
|
|
3587
|
+
return _Long.fromBits(low, wasm.get_high(), this.unsigned);
|
|
3588
|
+
}
|
|
3589
|
+
if (this.isZero())
|
|
3590
|
+
return this.unsigned ? _Long.UZERO : _Long.ZERO;
|
|
3591
|
+
let approx, rem, res;
|
|
3592
|
+
if (!this.unsigned) {
|
|
3593
|
+
if (this.eq(_Long.MIN_VALUE)) {
|
|
3594
|
+
if (divisor.eq(_Long.ONE) || divisor.eq(_Long.NEG_ONE))
|
|
3595
|
+
return _Long.MIN_VALUE;
|
|
3596
|
+
else if (divisor.eq(_Long.MIN_VALUE))
|
|
3597
|
+
return _Long.ONE;
|
|
3598
|
+
else {
|
|
3599
|
+
const halfThis = this.shr(1);
|
|
3600
|
+
approx = halfThis.div(divisor).shl(1);
|
|
3601
|
+
if (approx.eq(_Long.ZERO)) {
|
|
3602
|
+
return divisor.isNegative() ? _Long.ONE : _Long.NEG_ONE;
|
|
3603
|
+
} else {
|
|
3604
|
+
rem = this.sub(divisor.mul(approx));
|
|
3605
|
+
res = approx.add(rem.div(divisor));
|
|
3606
|
+
return res;
|
|
3607
|
+
}
|
|
3608
|
+
}
|
|
3609
|
+
} else if (divisor.eq(_Long.MIN_VALUE))
|
|
3610
|
+
return this.unsigned ? _Long.UZERO : _Long.ZERO;
|
|
3611
|
+
if (this.isNegative()) {
|
|
3612
|
+
if (divisor.isNegative())
|
|
3613
|
+
return this.neg().div(divisor.neg());
|
|
3614
|
+
return this.neg().div(divisor).neg();
|
|
3615
|
+
} else if (divisor.isNegative())
|
|
3616
|
+
return this.div(divisor.neg()).neg();
|
|
3617
|
+
res = _Long.ZERO;
|
|
3618
|
+
} else {
|
|
3619
|
+
if (!divisor.unsigned)
|
|
3620
|
+
divisor = divisor.toUnsigned();
|
|
3621
|
+
if (divisor.gt(this))
|
|
3622
|
+
return _Long.UZERO;
|
|
3623
|
+
if (divisor.gt(this.shru(1)))
|
|
3624
|
+
return _Long.UONE;
|
|
3625
|
+
res = _Long.UZERO;
|
|
3626
|
+
}
|
|
3627
|
+
rem = this;
|
|
3628
|
+
while (rem.gte(divisor)) {
|
|
3629
|
+
approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
|
|
3630
|
+
const log2 = Math.ceil(Math.log(approx) / Math.LN2);
|
|
3631
|
+
const delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
|
|
3632
|
+
let approxRes = _Long.fromNumber(approx);
|
|
3633
|
+
let approxRem = approxRes.mul(divisor);
|
|
3634
|
+
while (approxRem.isNegative() || approxRem.gt(rem)) {
|
|
3635
|
+
approx -= delta;
|
|
3636
|
+
approxRes = _Long.fromNumber(approx, this.unsigned);
|
|
3637
|
+
approxRem = approxRes.mul(divisor);
|
|
3638
|
+
}
|
|
3639
|
+
if (approxRes.isZero())
|
|
3640
|
+
approxRes = _Long.ONE;
|
|
3641
|
+
res = res.add(approxRes);
|
|
3642
|
+
rem = rem.sub(approxRem);
|
|
3643
|
+
}
|
|
3644
|
+
return res;
|
|
3645
|
+
}
|
|
3646
|
+
div(divisor) {
|
|
3647
|
+
return this.divide(divisor);
|
|
3648
|
+
}
|
|
3649
|
+
equals(other) {
|
|
3650
|
+
if (!_Long.isLong(other))
|
|
3651
|
+
other = _Long.fromValue(other);
|
|
3652
|
+
if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1)
|
|
3653
|
+
return false;
|
|
3654
|
+
return this.high === other.high && this.low === other.low;
|
|
3655
|
+
}
|
|
3656
|
+
eq(other) {
|
|
3657
|
+
return this.equals(other);
|
|
3658
|
+
}
|
|
3659
|
+
getHighBits() {
|
|
3660
|
+
return this.high;
|
|
3661
|
+
}
|
|
3662
|
+
getHighBitsUnsigned() {
|
|
3663
|
+
return this.high >>> 0;
|
|
3664
|
+
}
|
|
3665
|
+
getLowBits() {
|
|
3666
|
+
return this.low;
|
|
3667
|
+
}
|
|
3668
|
+
getLowBitsUnsigned() {
|
|
3669
|
+
return this.low >>> 0;
|
|
3670
|
+
}
|
|
3671
|
+
getNumBitsAbs() {
|
|
3672
|
+
if (this.isNegative()) {
|
|
3673
|
+
return this.eq(_Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
|
|
3674
|
+
}
|
|
3675
|
+
const val = this.high !== 0 ? this.high : this.low;
|
|
3676
|
+
let bit;
|
|
3677
|
+
for (bit = 31; bit > 0; bit--)
|
|
3678
|
+
if ((val & 1 << bit) !== 0)
|
|
3679
|
+
break;
|
|
3680
|
+
return this.high !== 0 ? bit + 33 : bit + 1;
|
|
3681
|
+
}
|
|
3682
|
+
greaterThan(other) {
|
|
3683
|
+
return this.comp(other) > 0;
|
|
3684
|
+
}
|
|
3685
|
+
gt(other) {
|
|
3686
|
+
return this.greaterThan(other);
|
|
3687
|
+
}
|
|
3688
|
+
greaterThanOrEqual(other) {
|
|
3689
|
+
return this.comp(other) >= 0;
|
|
3690
|
+
}
|
|
3691
|
+
gte(other) {
|
|
3692
|
+
return this.greaterThanOrEqual(other);
|
|
3693
|
+
}
|
|
3694
|
+
ge(other) {
|
|
3695
|
+
return this.greaterThanOrEqual(other);
|
|
3696
|
+
}
|
|
3697
|
+
isEven() {
|
|
3698
|
+
return (this.low & 1) === 0;
|
|
3699
|
+
}
|
|
3700
|
+
isNegative() {
|
|
3701
|
+
return !this.unsigned && this.high < 0;
|
|
3702
|
+
}
|
|
3703
|
+
isOdd() {
|
|
3704
|
+
return (this.low & 1) === 1;
|
|
3705
|
+
}
|
|
3706
|
+
isPositive() {
|
|
3707
|
+
return this.unsigned || this.high >= 0;
|
|
3708
|
+
}
|
|
3709
|
+
isZero() {
|
|
3710
|
+
return this.high === 0 && this.low === 0;
|
|
3711
|
+
}
|
|
3712
|
+
lessThan(other) {
|
|
3713
|
+
return this.comp(other) < 0;
|
|
3714
|
+
}
|
|
3715
|
+
lt(other) {
|
|
3716
|
+
return this.lessThan(other);
|
|
3717
|
+
}
|
|
3718
|
+
lessThanOrEqual(other) {
|
|
3719
|
+
return this.comp(other) <= 0;
|
|
3720
|
+
}
|
|
3721
|
+
lte(other) {
|
|
3722
|
+
return this.lessThanOrEqual(other);
|
|
3723
|
+
}
|
|
3724
|
+
modulo(divisor) {
|
|
3725
|
+
if (!_Long.isLong(divisor))
|
|
3726
|
+
divisor = _Long.fromValue(divisor);
|
|
3727
|
+
if (wasm) {
|
|
3728
|
+
const low = (this.unsigned ? wasm.rem_u : wasm.rem_s)(this.low, this.high, divisor.low, divisor.high);
|
|
3729
|
+
return _Long.fromBits(low, wasm.get_high(), this.unsigned);
|
|
3730
|
+
}
|
|
3731
|
+
return this.sub(this.div(divisor).mul(divisor));
|
|
3732
|
+
}
|
|
3733
|
+
mod(divisor) {
|
|
3734
|
+
return this.modulo(divisor);
|
|
3735
|
+
}
|
|
3736
|
+
rem(divisor) {
|
|
3737
|
+
return this.modulo(divisor);
|
|
3738
|
+
}
|
|
3739
|
+
multiply(multiplier) {
|
|
3740
|
+
if (this.isZero())
|
|
3741
|
+
return _Long.ZERO;
|
|
3742
|
+
if (!_Long.isLong(multiplier))
|
|
3743
|
+
multiplier = _Long.fromValue(multiplier);
|
|
3744
|
+
if (wasm) {
|
|
3745
|
+
const low = wasm.mul(this.low, this.high, multiplier.low, multiplier.high);
|
|
3746
|
+
return _Long.fromBits(low, wasm.get_high(), this.unsigned);
|
|
3747
|
+
}
|
|
3748
|
+
if (multiplier.isZero())
|
|
3749
|
+
return _Long.ZERO;
|
|
3750
|
+
if (this.eq(_Long.MIN_VALUE))
|
|
3751
|
+
return multiplier.isOdd() ? _Long.MIN_VALUE : _Long.ZERO;
|
|
3752
|
+
if (multiplier.eq(_Long.MIN_VALUE))
|
|
3753
|
+
return this.isOdd() ? _Long.MIN_VALUE : _Long.ZERO;
|
|
3754
|
+
if (this.isNegative()) {
|
|
3755
|
+
if (multiplier.isNegative())
|
|
3756
|
+
return this.neg().mul(multiplier.neg());
|
|
3757
|
+
else
|
|
3758
|
+
return this.neg().mul(multiplier).neg();
|
|
3759
|
+
} else if (multiplier.isNegative())
|
|
3760
|
+
return this.mul(multiplier.neg()).neg();
|
|
3761
|
+
if (this.lt(_Long.TWO_PWR_24) && multiplier.lt(_Long.TWO_PWR_24))
|
|
3762
|
+
return _Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
|
|
3763
|
+
const a48 = this.high >>> 16;
|
|
3764
|
+
const a32 = this.high & 65535;
|
|
3765
|
+
const a16 = this.low >>> 16;
|
|
3766
|
+
const a00 = this.low & 65535;
|
|
3767
|
+
const b48 = multiplier.high >>> 16;
|
|
3768
|
+
const b32 = multiplier.high & 65535;
|
|
3769
|
+
const b16 = multiplier.low >>> 16;
|
|
3770
|
+
const b00 = multiplier.low & 65535;
|
|
3771
|
+
let c48 = 0, c32 = 0, c16 = 0, c00 = 0;
|
|
3772
|
+
c00 += a00 * b00;
|
|
3773
|
+
c16 += c00 >>> 16;
|
|
3774
|
+
c00 &= 65535;
|
|
3775
|
+
c16 += a16 * b00;
|
|
3776
|
+
c32 += c16 >>> 16;
|
|
3777
|
+
c16 &= 65535;
|
|
3778
|
+
c16 += a00 * b16;
|
|
3779
|
+
c32 += c16 >>> 16;
|
|
3780
|
+
c16 &= 65535;
|
|
3781
|
+
c32 += a32 * b00;
|
|
3782
|
+
c48 += c32 >>> 16;
|
|
3783
|
+
c32 &= 65535;
|
|
3784
|
+
c32 += a16 * b16;
|
|
3785
|
+
c48 += c32 >>> 16;
|
|
3786
|
+
c32 &= 65535;
|
|
3787
|
+
c32 += a00 * b32;
|
|
3788
|
+
c48 += c32 >>> 16;
|
|
3789
|
+
c32 &= 65535;
|
|
3790
|
+
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
|
|
3791
|
+
c48 &= 65535;
|
|
3792
|
+
return _Long.fromBits(c16 << 16 | c00, c48 << 16 | c32, this.unsigned);
|
|
3793
|
+
}
|
|
3794
|
+
mul(multiplier) {
|
|
3795
|
+
return this.multiply(multiplier);
|
|
3796
|
+
}
|
|
3797
|
+
negate() {
|
|
3798
|
+
if (!this.unsigned && this.eq(_Long.MIN_VALUE))
|
|
3799
|
+
return _Long.MIN_VALUE;
|
|
3800
|
+
return this.not().add(_Long.ONE);
|
|
3801
|
+
}
|
|
3802
|
+
neg() {
|
|
3803
|
+
return this.negate();
|
|
3804
|
+
}
|
|
3805
|
+
not() {
|
|
3806
|
+
return _Long.fromBits(~this.low, ~this.high, this.unsigned);
|
|
3807
|
+
}
|
|
3808
|
+
notEquals(other) {
|
|
3809
|
+
return !this.equals(other);
|
|
3810
|
+
}
|
|
3811
|
+
neq(other) {
|
|
3812
|
+
return this.notEquals(other);
|
|
3813
|
+
}
|
|
3814
|
+
ne(other) {
|
|
3815
|
+
return this.notEquals(other);
|
|
3816
|
+
}
|
|
3817
|
+
or(other) {
|
|
3818
|
+
if (!_Long.isLong(other))
|
|
3819
|
+
other = _Long.fromValue(other);
|
|
3820
|
+
return _Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned);
|
|
3821
|
+
}
|
|
3822
|
+
shiftLeft(numBits) {
|
|
3823
|
+
if (_Long.isLong(numBits))
|
|
3824
|
+
numBits = numBits.toInt();
|
|
3825
|
+
if ((numBits &= 63) === 0)
|
|
3826
|
+
return this;
|
|
3827
|
+
else if (numBits < 32)
|
|
3828
|
+
return _Long.fromBits(this.low << numBits, this.high << numBits | this.low >>> 32 - numBits, this.unsigned);
|
|
3829
|
+
else
|
|
3830
|
+
return _Long.fromBits(0, this.low << numBits - 32, this.unsigned);
|
|
3831
|
+
}
|
|
3832
|
+
shl(numBits) {
|
|
3833
|
+
return this.shiftLeft(numBits);
|
|
3834
|
+
}
|
|
3835
|
+
shiftRight(numBits) {
|
|
3836
|
+
if (_Long.isLong(numBits))
|
|
3837
|
+
numBits = numBits.toInt();
|
|
3838
|
+
if ((numBits &= 63) === 0)
|
|
3839
|
+
return this;
|
|
3840
|
+
else if (numBits < 32)
|
|
3841
|
+
return _Long.fromBits(this.low >>> numBits | this.high << 32 - numBits, this.high >> numBits, this.unsigned);
|
|
3842
|
+
else
|
|
3843
|
+
return _Long.fromBits(this.high >> numBits - 32, this.high >= 0 ? 0 : -1, this.unsigned);
|
|
3844
|
+
}
|
|
3845
|
+
shr(numBits) {
|
|
3846
|
+
return this.shiftRight(numBits);
|
|
3847
|
+
}
|
|
3848
|
+
shiftRightUnsigned(numBits) {
|
|
3849
|
+
if (_Long.isLong(numBits))
|
|
3850
|
+
numBits = numBits.toInt();
|
|
3851
|
+
numBits &= 63;
|
|
3852
|
+
if (numBits === 0)
|
|
3853
|
+
return this;
|
|
3854
|
+
else {
|
|
3855
|
+
const high = this.high;
|
|
3856
|
+
if (numBits < 32) {
|
|
3857
|
+
const low = this.low;
|
|
3858
|
+
return _Long.fromBits(low >>> numBits | high << 32 - numBits, high >>> numBits, this.unsigned);
|
|
3859
|
+
} else if (numBits === 32)
|
|
3860
|
+
return _Long.fromBits(high, 0, this.unsigned);
|
|
3861
|
+
else
|
|
3862
|
+
return _Long.fromBits(high >>> numBits - 32, 0, this.unsigned);
|
|
3863
|
+
}
|
|
3864
|
+
}
|
|
3865
|
+
shr_u(numBits) {
|
|
3866
|
+
return this.shiftRightUnsigned(numBits);
|
|
3867
|
+
}
|
|
3868
|
+
shru(numBits) {
|
|
3869
|
+
return this.shiftRightUnsigned(numBits);
|
|
3870
|
+
}
|
|
3871
|
+
subtract(subtrahend) {
|
|
3872
|
+
if (!_Long.isLong(subtrahend))
|
|
3873
|
+
subtrahend = _Long.fromValue(subtrahend);
|
|
3874
|
+
return this.add(subtrahend.neg());
|
|
3875
|
+
}
|
|
3876
|
+
sub(subtrahend) {
|
|
3877
|
+
return this.subtract(subtrahend);
|
|
3878
|
+
}
|
|
3879
|
+
toInt() {
|
|
3880
|
+
return this.unsigned ? this.low >>> 0 : this.low;
|
|
3881
|
+
}
|
|
3882
|
+
toNumber() {
|
|
3883
|
+
if (this.unsigned)
|
|
3884
|
+
return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0);
|
|
3885
|
+
return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
|
|
3886
|
+
}
|
|
3887
|
+
toBigInt() {
|
|
3888
|
+
return BigInt(this.toString());
|
|
3889
|
+
}
|
|
3890
|
+
toBytes(le) {
|
|
3891
|
+
return le ? this.toBytesLE() : this.toBytesBE();
|
|
3892
|
+
}
|
|
3893
|
+
toBytesLE() {
|
|
3894
|
+
const hi = this.high, lo = this.low;
|
|
3895
|
+
return [
|
|
3896
|
+
lo & 255,
|
|
3897
|
+
lo >>> 8 & 255,
|
|
3898
|
+
lo >>> 16 & 255,
|
|
3899
|
+
lo >>> 24,
|
|
3900
|
+
hi & 255,
|
|
3901
|
+
hi >>> 8 & 255,
|
|
3902
|
+
hi >>> 16 & 255,
|
|
3903
|
+
hi >>> 24
|
|
3904
|
+
];
|
|
3905
|
+
}
|
|
3906
|
+
toBytesBE() {
|
|
3907
|
+
const hi = this.high, lo = this.low;
|
|
3908
|
+
return [
|
|
3909
|
+
hi >>> 24,
|
|
3910
|
+
hi >>> 16 & 255,
|
|
3911
|
+
hi >>> 8 & 255,
|
|
3912
|
+
hi & 255,
|
|
3913
|
+
lo >>> 24,
|
|
3914
|
+
lo >>> 16 & 255,
|
|
3915
|
+
lo >>> 8 & 255,
|
|
3916
|
+
lo & 255
|
|
3917
|
+
];
|
|
3918
|
+
}
|
|
3919
|
+
toSigned() {
|
|
3920
|
+
if (!this.unsigned)
|
|
3921
|
+
return this;
|
|
3922
|
+
return _Long.fromBits(this.low, this.high, false);
|
|
3923
|
+
}
|
|
3924
|
+
toString(radix) {
|
|
3925
|
+
radix = radix || 10;
|
|
3926
|
+
if (radix < 2 || 36 < radix)
|
|
3927
|
+
throw new BSONError("radix");
|
|
3928
|
+
if (this.isZero())
|
|
3929
|
+
return "0";
|
|
3930
|
+
if (this.isNegative()) {
|
|
3931
|
+
if (this.eq(_Long.MIN_VALUE)) {
|
|
3932
|
+
const radixLong = _Long.fromNumber(radix), div = this.div(radixLong), rem1 = div.mul(radixLong).sub(this);
|
|
3933
|
+
return div.toString(radix) + rem1.toInt().toString(radix);
|
|
3934
|
+
} else
|
|
3935
|
+
return "-" + this.neg().toString(radix);
|
|
3936
|
+
}
|
|
3937
|
+
const radixToPower = _Long.fromNumber(Math.pow(radix, 6), this.unsigned);
|
|
3938
|
+
let rem = this;
|
|
3939
|
+
let result = "";
|
|
3940
|
+
while (true) {
|
|
3941
|
+
const remDiv = rem.div(radixToPower);
|
|
3942
|
+
const intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0;
|
|
3943
|
+
let digits = intval.toString(radix);
|
|
3944
|
+
rem = remDiv;
|
|
3945
|
+
if (rem.isZero()) {
|
|
3946
|
+
return digits + result;
|
|
3947
|
+
} else {
|
|
3948
|
+
while (digits.length < 6)
|
|
3949
|
+
digits = "0" + digits;
|
|
3950
|
+
result = "" + digits + result;
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3953
|
+
}
|
|
3954
|
+
toUnsigned() {
|
|
3955
|
+
if (this.unsigned)
|
|
3956
|
+
return this;
|
|
3957
|
+
return _Long.fromBits(this.low, this.high, true);
|
|
3958
|
+
}
|
|
3959
|
+
xor(other) {
|
|
3960
|
+
if (!_Long.isLong(other))
|
|
3961
|
+
other = _Long.fromValue(other);
|
|
3962
|
+
return _Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
|
|
3963
|
+
}
|
|
3964
|
+
eqz() {
|
|
3965
|
+
return this.isZero();
|
|
3966
|
+
}
|
|
3967
|
+
le(other) {
|
|
3968
|
+
return this.lessThanOrEqual(other);
|
|
3969
|
+
}
|
|
3970
|
+
toExtendedJSON(options) {
|
|
3971
|
+
if (options && options.relaxed)
|
|
3972
|
+
return this.toNumber();
|
|
3973
|
+
return { $numberLong: this.toString() };
|
|
3974
|
+
}
|
|
3975
|
+
static fromExtendedJSON(doc, options) {
|
|
3976
|
+
const { useBigInt64 = false, relaxed = true } = { ...options };
|
|
3977
|
+
if (doc.$numberLong.length > MAX_INT64_STRING_LENGTH) {
|
|
3978
|
+
throw new BSONError("$numberLong string is too long");
|
|
3979
|
+
}
|
|
3980
|
+
if (!DECIMAL_REG_EX.test(doc.$numberLong)) {
|
|
3981
|
+
throw new BSONError(`$numberLong string "${doc.$numberLong}" is in an invalid format`);
|
|
3982
|
+
}
|
|
3983
|
+
if (useBigInt64) {
|
|
3984
|
+
const bigIntResult = BigInt(doc.$numberLong);
|
|
3985
|
+
return BigInt.asIntN(64, bigIntResult);
|
|
3986
|
+
}
|
|
3987
|
+
const longResult = _Long.fromString(doc.$numberLong);
|
|
3988
|
+
if (relaxed) {
|
|
3989
|
+
return longResult.toNumber();
|
|
3990
|
+
}
|
|
3991
|
+
return longResult;
|
|
3992
|
+
}
|
|
3993
|
+
inspect(depth, options, inspect) {
|
|
3994
|
+
inspect ?? (inspect = defaultInspect);
|
|
3995
|
+
const longVal = inspect(this.toString(), options);
|
|
3996
|
+
const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : "";
|
|
3997
|
+
return `new Long(${longVal}${unsignedVal})`;
|
|
3998
|
+
}
|
|
3999
|
+
};
|
|
4000
|
+
__name(_Long, "Long");
|
|
4001
|
+
var Long = _Long;
|
|
4002
|
+
Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
|
|
4003
|
+
Long.MAX_UNSIGNED_VALUE = Long.fromBits(4294967295 | 0, 4294967295 | 0, true);
|
|
4004
|
+
Long.ZERO = Long.fromInt(0);
|
|
4005
|
+
Long.UZERO = Long.fromInt(0, true);
|
|
4006
|
+
Long.ONE = Long.fromInt(1);
|
|
4007
|
+
Long.UONE = Long.fromInt(1, true);
|
|
4008
|
+
Long.NEG_ONE = Long.fromInt(-1);
|
|
4009
|
+
Long.MAX_VALUE = Long.fromBits(4294967295 | 0, 2147483647 | 0, false);
|
|
4010
|
+
Long.MIN_VALUE = Long.fromBits(0, 2147483648 | 0, false);
|
|
4011
|
+
var PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
|
|
4012
|
+
var PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
|
|
4013
|
+
var PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i;
|
|
4014
|
+
var EXPONENT_MAX = 6111;
|
|
4015
|
+
var EXPONENT_MIN = -6176;
|
|
4016
|
+
var EXPONENT_BIAS = 6176;
|
|
4017
|
+
var MAX_DIGITS = 34;
|
|
4018
|
+
var NAN_BUFFER = ByteUtils.fromNumberArray([
|
|
4019
|
+
124,
|
|
4020
|
+
0,
|
|
4021
|
+
0,
|
|
4022
|
+
0,
|
|
4023
|
+
0,
|
|
4024
|
+
0,
|
|
4025
|
+
0,
|
|
4026
|
+
0,
|
|
4027
|
+
0,
|
|
4028
|
+
0,
|
|
4029
|
+
0,
|
|
4030
|
+
0,
|
|
4031
|
+
0,
|
|
4032
|
+
0,
|
|
4033
|
+
0,
|
|
4034
|
+
0
|
|
4035
|
+
].reverse());
|
|
4036
|
+
var INF_NEGATIVE_BUFFER = ByteUtils.fromNumberArray([
|
|
4037
|
+
248,
|
|
4038
|
+
0,
|
|
4039
|
+
0,
|
|
4040
|
+
0,
|
|
4041
|
+
0,
|
|
4042
|
+
0,
|
|
4043
|
+
0,
|
|
4044
|
+
0,
|
|
4045
|
+
0,
|
|
4046
|
+
0,
|
|
4047
|
+
0,
|
|
4048
|
+
0,
|
|
4049
|
+
0,
|
|
4050
|
+
0,
|
|
4051
|
+
0,
|
|
4052
|
+
0
|
|
4053
|
+
].reverse());
|
|
4054
|
+
var INF_POSITIVE_BUFFER = ByteUtils.fromNumberArray([
|
|
4055
|
+
120,
|
|
4056
|
+
0,
|
|
4057
|
+
0,
|
|
4058
|
+
0,
|
|
4059
|
+
0,
|
|
4060
|
+
0,
|
|
4061
|
+
0,
|
|
4062
|
+
0,
|
|
4063
|
+
0,
|
|
4064
|
+
0,
|
|
4065
|
+
0,
|
|
4066
|
+
0,
|
|
4067
|
+
0,
|
|
4068
|
+
0,
|
|
4069
|
+
0,
|
|
4070
|
+
0
|
|
4071
|
+
].reverse());
|
|
4072
|
+
var EXPONENT_REGEX = /^([-+])?(\d+)?$/;
|
|
4073
|
+
var COMBINATION_MASK = 31;
|
|
4074
|
+
var EXPONENT_MASK = 16383;
|
|
4075
|
+
var COMBINATION_INFINITY = 30;
|
|
4076
|
+
var COMBINATION_NAN = 31;
|
|
4077
|
+
function isDigit(value) {
|
|
4078
|
+
return !isNaN(parseInt(value, 10));
|
|
4079
|
+
}
|
|
4080
|
+
__name(isDigit, "isDigit");
|
|
4081
|
+
function divideu128(value) {
|
|
4082
|
+
const DIVISOR = Long.fromNumber(1e3 * 1e3 * 1e3);
|
|
4083
|
+
let _rem = Long.fromNumber(0);
|
|
4084
|
+
if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
|
|
4085
|
+
return { quotient: value, rem: _rem };
|
|
4086
|
+
}
|
|
4087
|
+
for (let i = 0; i <= 3; i++) {
|
|
4088
|
+
_rem = _rem.shiftLeft(32);
|
|
4089
|
+
_rem = _rem.add(new Long(value.parts[i], 0));
|
|
4090
|
+
value.parts[i] = _rem.div(DIVISOR).low;
|
|
4091
|
+
_rem = _rem.modulo(DIVISOR);
|
|
4092
|
+
}
|
|
4093
|
+
return { quotient: value, rem: _rem };
|
|
4094
|
+
}
|
|
4095
|
+
__name(divideu128, "divideu128");
|
|
4096
|
+
function multiply64x2(left, right) {
|
|
4097
|
+
if (!left && !right) {
|
|
4098
|
+
return { high: Long.fromNumber(0), low: Long.fromNumber(0) };
|
|
4099
|
+
}
|
|
4100
|
+
const leftHigh = left.shiftRightUnsigned(32);
|
|
4101
|
+
const leftLow = new Long(left.getLowBits(), 0);
|
|
4102
|
+
const rightHigh = right.shiftRightUnsigned(32);
|
|
4103
|
+
const rightLow = new Long(right.getLowBits(), 0);
|
|
4104
|
+
let productHigh = leftHigh.multiply(rightHigh);
|
|
4105
|
+
let productMid = leftHigh.multiply(rightLow);
|
|
4106
|
+
const productMid2 = leftLow.multiply(rightHigh);
|
|
4107
|
+
let productLow = leftLow.multiply(rightLow);
|
|
4108
|
+
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
|
|
4109
|
+
productMid = new Long(productMid.getLowBits(), 0).add(productMid2).add(productLow.shiftRightUnsigned(32));
|
|
4110
|
+
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
|
|
4111
|
+
productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0));
|
|
4112
|
+
return { high: productHigh, low: productLow };
|
|
4113
|
+
}
|
|
4114
|
+
__name(multiply64x2, "multiply64x2");
|
|
4115
|
+
function lessThan(left, right) {
|
|
4116
|
+
const uhleft = left.high >>> 0;
|
|
4117
|
+
const uhright = right.high >>> 0;
|
|
4118
|
+
if (uhleft < uhright) {
|
|
4119
|
+
return true;
|
|
4120
|
+
} else if (uhleft === uhright) {
|
|
4121
|
+
const ulleft = left.low >>> 0;
|
|
4122
|
+
const ulright = right.low >>> 0;
|
|
4123
|
+
if (ulleft < ulright)
|
|
4124
|
+
return true;
|
|
4125
|
+
}
|
|
4126
|
+
return false;
|
|
4127
|
+
}
|
|
4128
|
+
__name(lessThan, "lessThan");
|
|
4129
|
+
function invalidErr(string, message) {
|
|
4130
|
+
throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`);
|
|
4131
|
+
}
|
|
4132
|
+
__name(invalidErr, "invalidErr");
|
|
4133
|
+
var _Decimal128 = class _Decimal128 extends BSONValue {
|
|
4134
|
+
get _bsontype() {
|
|
4135
|
+
return "Decimal128";
|
|
4136
|
+
}
|
|
4137
|
+
constructor(bytes) {
|
|
4138
|
+
super();
|
|
4139
|
+
if (typeof bytes === "string") {
|
|
4140
|
+
this.bytes = _Decimal128.fromString(bytes).bytes;
|
|
4141
|
+
} else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
|
|
4142
|
+
if (bytes.byteLength !== 16) {
|
|
4143
|
+
throw new BSONError("Decimal128 must take a Buffer of 16 bytes");
|
|
4144
|
+
}
|
|
4145
|
+
this.bytes = bytes;
|
|
4146
|
+
} else {
|
|
4147
|
+
throw new BSONError("Decimal128 must take a Buffer or string");
|
|
4148
|
+
}
|
|
4149
|
+
}
|
|
4150
|
+
static fromString(representation) {
|
|
4151
|
+
return _Decimal128._fromString(representation, { allowRounding: false });
|
|
4152
|
+
}
|
|
4153
|
+
static fromStringWithRounding(representation) {
|
|
4154
|
+
return _Decimal128._fromString(representation, { allowRounding: true });
|
|
4155
|
+
}
|
|
4156
|
+
static _fromString(representation, options) {
|
|
4157
|
+
let isNegative = false;
|
|
4158
|
+
let sawSign = false;
|
|
4159
|
+
let sawRadix = false;
|
|
4160
|
+
let foundNonZero = false;
|
|
4161
|
+
let significantDigits = 0;
|
|
4162
|
+
let nDigitsRead = 0;
|
|
4163
|
+
let nDigits = 0;
|
|
4164
|
+
let radixPosition = 0;
|
|
4165
|
+
let firstNonZero = 0;
|
|
4166
|
+
const digits = [0];
|
|
4167
|
+
let nDigitsStored = 0;
|
|
4168
|
+
let digitsInsert = 0;
|
|
4169
|
+
let lastDigit = 0;
|
|
4170
|
+
let exponent = 0;
|
|
4171
|
+
let significandHigh = new Long(0, 0);
|
|
4172
|
+
let significandLow = new Long(0, 0);
|
|
4173
|
+
let biasedExponent = 0;
|
|
4174
|
+
let index = 0;
|
|
4175
|
+
if (representation.length >= 7e3) {
|
|
4176
|
+
throw new BSONError("" + representation + " not a valid Decimal128 string");
|
|
4177
|
+
}
|
|
4178
|
+
const stringMatch = representation.match(PARSE_STRING_REGEXP);
|
|
4179
|
+
const infMatch = representation.match(PARSE_INF_REGEXP);
|
|
4180
|
+
const nanMatch = representation.match(PARSE_NAN_REGEXP);
|
|
4181
|
+
if (!stringMatch && !infMatch && !nanMatch || representation.length === 0) {
|
|
4182
|
+
throw new BSONError("" + representation + " not a valid Decimal128 string");
|
|
4183
|
+
}
|
|
4184
|
+
if (stringMatch) {
|
|
4185
|
+
const unsignedNumber = stringMatch[2];
|
|
4186
|
+
const e = stringMatch[4];
|
|
4187
|
+
const expSign = stringMatch[5];
|
|
4188
|
+
const expNumber = stringMatch[6];
|
|
4189
|
+
if (e && expNumber === void 0)
|
|
4190
|
+
invalidErr(representation, "missing exponent power");
|
|
4191
|
+
if (e && unsignedNumber === void 0)
|
|
4192
|
+
invalidErr(representation, "missing exponent base");
|
|
4193
|
+
if (e === void 0 && (expSign || expNumber)) {
|
|
4194
|
+
invalidErr(representation, "missing e before exponent");
|
|
4195
|
+
}
|
|
4196
|
+
}
|
|
4197
|
+
if (representation[index] === "+" || representation[index] === "-") {
|
|
4198
|
+
sawSign = true;
|
|
4199
|
+
isNegative = representation[index++] === "-";
|
|
4200
|
+
}
|
|
4201
|
+
if (!isDigit(representation[index]) && representation[index] !== ".") {
|
|
4202
|
+
if (representation[index] === "i" || representation[index] === "I") {
|
|
4203
|
+
return new _Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
|
|
4204
|
+
} else if (representation[index] === "N") {
|
|
4205
|
+
return new _Decimal128(NAN_BUFFER);
|
|
4206
|
+
}
|
|
4207
|
+
}
|
|
4208
|
+
while (isDigit(representation[index]) || representation[index] === ".") {
|
|
4209
|
+
if (representation[index] === ".") {
|
|
4210
|
+
if (sawRadix)
|
|
4211
|
+
invalidErr(representation, "contains multiple periods");
|
|
4212
|
+
sawRadix = true;
|
|
4213
|
+
index = index + 1;
|
|
4214
|
+
continue;
|
|
4215
|
+
}
|
|
4216
|
+
if (nDigitsStored < MAX_DIGITS) {
|
|
4217
|
+
if (representation[index] !== "0" || foundNonZero) {
|
|
4218
|
+
if (!foundNonZero) {
|
|
4219
|
+
firstNonZero = nDigitsRead;
|
|
4220
|
+
}
|
|
4221
|
+
foundNonZero = true;
|
|
4222
|
+
digits[digitsInsert++] = parseInt(representation[index], 10);
|
|
4223
|
+
nDigitsStored = nDigitsStored + 1;
|
|
4224
|
+
}
|
|
4225
|
+
}
|
|
4226
|
+
if (foundNonZero)
|
|
4227
|
+
nDigits = nDigits + 1;
|
|
4228
|
+
if (sawRadix)
|
|
4229
|
+
radixPosition = radixPosition + 1;
|
|
4230
|
+
nDigitsRead = nDigitsRead + 1;
|
|
4231
|
+
index = index + 1;
|
|
4232
|
+
}
|
|
4233
|
+
if (sawRadix && !nDigitsRead)
|
|
4234
|
+
throw new BSONError("" + representation + " not a valid Decimal128 string");
|
|
4235
|
+
if (representation[index] === "e" || representation[index] === "E") {
|
|
4236
|
+
const match = representation.substr(++index).match(EXPONENT_REGEX);
|
|
4237
|
+
if (!match || !match[2])
|
|
4238
|
+
return new _Decimal128(NAN_BUFFER);
|
|
4239
|
+
exponent = parseInt(match[0], 10);
|
|
4240
|
+
index = index + match[0].length;
|
|
4241
|
+
}
|
|
4242
|
+
if (representation[index])
|
|
4243
|
+
return new _Decimal128(NAN_BUFFER);
|
|
4244
|
+
if (!nDigitsStored) {
|
|
4245
|
+
digits[0] = 0;
|
|
4246
|
+
nDigits = 1;
|
|
4247
|
+
nDigitsStored = 1;
|
|
4248
|
+
significantDigits = 0;
|
|
4249
|
+
} else {
|
|
4250
|
+
lastDigit = nDigitsStored - 1;
|
|
4251
|
+
significantDigits = nDigits;
|
|
4252
|
+
if (significantDigits !== 1) {
|
|
4253
|
+
while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === "0") {
|
|
4254
|
+
significantDigits = significantDigits - 1;
|
|
4255
|
+
}
|
|
4256
|
+
}
|
|
4257
|
+
}
|
|
4258
|
+
if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
|
|
4259
|
+
exponent = EXPONENT_MIN;
|
|
4260
|
+
} else {
|
|
4261
|
+
exponent = exponent - radixPosition;
|
|
4262
|
+
}
|
|
4263
|
+
while (exponent > EXPONENT_MAX) {
|
|
4264
|
+
lastDigit = lastDigit + 1;
|
|
4265
|
+
if (lastDigit >= MAX_DIGITS) {
|
|
4266
|
+
if (significantDigits === 0) {
|
|
4267
|
+
exponent = EXPONENT_MAX;
|
|
4268
|
+
break;
|
|
4269
|
+
}
|
|
4270
|
+
invalidErr(representation, "overflow");
|
|
4271
|
+
}
|
|
4272
|
+
exponent = exponent - 1;
|
|
4273
|
+
}
|
|
4274
|
+
if (options.allowRounding) {
|
|
4275
|
+
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
4276
|
+
if (lastDigit === 0 && significantDigits < nDigitsStored) {
|
|
4277
|
+
exponent = EXPONENT_MIN;
|
|
4278
|
+
significantDigits = 0;
|
|
4279
|
+
break;
|
|
4280
|
+
}
|
|
4281
|
+
if (nDigitsStored < nDigits) {
|
|
4282
|
+
nDigits = nDigits - 1;
|
|
4283
|
+
} else {
|
|
4284
|
+
lastDigit = lastDigit - 1;
|
|
4285
|
+
}
|
|
4286
|
+
if (exponent < EXPONENT_MAX) {
|
|
4287
|
+
exponent = exponent + 1;
|
|
4288
|
+
} else {
|
|
4289
|
+
const digitsString = digits.join("");
|
|
4290
|
+
if (digitsString.match(/^0+$/)) {
|
|
4291
|
+
exponent = EXPONENT_MAX;
|
|
4292
|
+
break;
|
|
4293
|
+
}
|
|
4294
|
+
invalidErr(representation, "overflow");
|
|
4295
|
+
}
|
|
4296
|
+
}
|
|
4297
|
+
if (lastDigit + 1 < significantDigits) {
|
|
4298
|
+
let endOfString = nDigitsRead;
|
|
4299
|
+
if (sawRadix) {
|
|
4300
|
+
firstNonZero = firstNonZero + 1;
|
|
4301
|
+
endOfString = endOfString + 1;
|
|
4302
|
+
}
|
|
4303
|
+
if (sawSign) {
|
|
4304
|
+
firstNonZero = firstNonZero + 1;
|
|
4305
|
+
endOfString = endOfString + 1;
|
|
4306
|
+
}
|
|
4307
|
+
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
4308
|
+
let roundBit = 0;
|
|
4309
|
+
if (roundDigit >= 5) {
|
|
4310
|
+
roundBit = 1;
|
|
4311
|
+
if (roundDigit === 5) {
|
|
4312
|
+
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
|
|
4313
|
+
for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
|
|
4314
|
+
if (parseInt(representation[i], 10)) {
|
|
4315
|
+
roundBit = 1;
|
|
4316
|
+
break;
|
|
4317
|
+
}
|
|
4318
|
+
}
|
|
4319
|
+
}
|
|
4320
|
+
}
|
|
4321
|
+
if (roundBit) {
|
|
4322
|
+
let dIdx = lastDigit;
|
|
4323
|
+
for (; dIdx >= 0; dIdx--) {
|
|
4324
|
+
if (++digits[dIdx] > 9) {
|
|
4325
|
+
digits[dIdx] = 0;
|
|
4326
|
+
if (dIdx === 0) {
|
|
4327
|
+
if (exponent < EXPONENT_MAX) {
|
|
4328
|
+
exponent = exponent + 1;
|
|
4329
|
+
digits[dIdx] = 1;
|
|
4330
|
+
} else {
|
|
4331
|
+
return new _Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
|
|
4332
|
+
}
|
|
4333
|
+
}
|
|
4334
|
+
} else {
|
|
4335
|
+
break;
|
|
4336
|
+
}
|
|
4337
|
+
}
|
|
4338
|
+
}
|
|
4339
|
+
}
|
|
4340
|
+
} else {
|
|
4341
|
+
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
4342
|
+
if (lastDigit === 0) {
|
|
4343
|
+
if (significantDigits === 0) {
|
|
4344
|
+
exponent = EXPONENT_MIN;
|
|
4345
|
+
break;
|
|
4346
|
+
}
|
|
4347
|
+
invalidErr(representation, "exponent underflow");
|
|
4348
|
+
}
|
|
4349
|
+
if (nDigitsStored < nDigits) {
|
|
4350
|
+
if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== "0" && significantDigits !== 0) {
|
|
4351
|
+
invalidErr(representation, "inexact rounding");
|
|
4352
|
+
}
|
|
4353
|
+
nDigits = nDigits - 1;
|
|
4354
|
+
} else {
|
|
4355
|
+
if (digits[lastDigit] !== 0) {
|
|
4356
|
+
invalidErr(representation, "inexact rounding");
|
|
4357
|
+
}
|
|
4358
|
+
lastDigit = lastDigit - 1;
|
|
4359
|
+
}
|
|
4360
|
+
if (exponent < EXPONENT_MAX) {
|
|
4361
|
+
exponent = exponent + 1;
|
|
4362
|
+
} else {
|
|
4363
|
+
invalidErr(representation, "overflow");
|
|
4364
|
+
}
|
|
4365
|
+
}
|
|
4366
|
+
if (lastDigit + 1 < significantDigits) {
|
|
4367
|
+
if (sawRadix) {
|
|
4368
|
+
firstNonZero = firstNonZero + 1;
|
|
4369
|
+
}
|
|
4370
|
+
if (sawSign) {
|
|
4371
|
+
firstNonZero = firstNonZero + 1;
|
|
4372
|
+
}
|
|
4373
|
+
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
4374
|
+
if (roundDigit !== 0) {
|
|
4375
|
+
invalidErr(representation, "inexact rounding");
|
|
4376
|
+
}
|
|
4377
|
+
}
|
|
4378
|
+
}
|
|
4379
|
+
significandHigh = Long.fromNumber(0);
|
|
4380
|
+
significandLow = Long.fromNumber(0);
|
|
4381
|
+
if (significantDigits === 0) {
|
|
4382
|
+
significandHigh = Long.fromNumber(0);
|
|
4383
|
+
significandLow = Long.fromNumber(0);
|
|
4384
|
+
} else if (lastDigit < 17) {
|
|
4385
|
+
let dIdx = 0;
|
|
4386
|
+
significandLow = Long.fromNumber(digits[dIdx++]);
|
|
4387
|
+
significandHigh = new Long(0, 0);
|
|
4388
|
+
for (; dIdx <= lastDigit; dIdx++) {
|
|
4389
|
+
significandLow = significandLow.multiply(Long.fromNumber(10));
|
|
4390
|
+
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
|
|
4391
|
+
}
|
|
4392
|
+
} else {
|
|
4393
|
+
let dIdx = 0;
|
|
4394
|
+
significandHigh = Long.fromNumber(digits[dIdx++]);
|
|
4395
|
+
for (; dIdx <= lastDigit - 17; dIdx++) {
|
|
4396
|
+
significandHigh = significandHigh.multiply(Long.fromNumber(10));
|
|
4397
|
+
significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx]));
|
|
4398
|
+
}
|
|
4399
|
+
significandLow = Long.fromNumber(digits[dIdx++]);
|
|
4400
|
+
for (; dIdx <= lastDigit; dIdx++) {
|
|
4401
|
+
significandLow = significandLow.multiply(Long.fromNumber(10));
|
|
4402
|
+
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
|
|
4403
|
+
}
|
|
4404
|
+
}
|
|
4405
|
+
const significand = multiply64x2(significandHigh, Long.fromString("100000000000000000"));
|
|
4406
|
+
significand.low = significand.low.add(significandLow);
|
|
4407
|
+
if (lessThan(significand.low, significandLow)) {
|
|
4408
|
+
significand.high = significand.high.add(Long.fromNumber(1));
|
|
4409
|
+
}
|
|
4410
|
+
biasedExponent = exponent + EXPONENT_BIAS;
|
|
4411
|
+
const dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) };
|
|
4412
|
+
if (significand.high.shiftRightUnsigned(49).and(Long.fromNumber(1)).equals(Long.fromNumber(1))) {
|
|
4413
|
+
dec.high = dec.high.or(Long.fromNumber(3).shiftLeft(61));
|
|
4414
|
+
dec.high = dec.high.or(Long.fromNumber(biasedExponent).and(Long.fromNumber(16383).shiftLeft(47)));
|
|
4415
|
+
dec.high = dec.high.or(significand.high.and(Long.fromNumber(140737488355327)));
|
|
4416
|
+
} else {
|
|
4417
|
+
dec.high = dec.high.or(Long.fromNumber(biasedExponent & 16383).shiftLeft(49));
|
|
4418
|
+
dec.high = dec.high.or(significand.high.and(Long.fromNumber(562949953421311)));
|
|
4419
|
+
}
|
|
4420
|
+
dec.low = significand.low;
|
|
4421
|
+
if (isNegative) {
|
|
4422
|
+
dec.high = dec.high.or(Long.fromString("9223372036854775808"));
|
|
4423
|
+
}
|
|
4424
|
+
const buffer2 = ByteUtils.allocateUnsafe(16);
|
|
4425
|
+
index = 0;
|
|
4426
|
+
buffer2[index++] = dec.low.low & 255;
|
|
4427
|
+
buffer2[index++] = dec.low.low >> 8 & 255;
|
|
4428
|
+
buffer2[index++] = dec.low.low >> 16 & 255;
|
|
4429
|
+
buffer2[index++] = dec.low.low >> 24 & 255;
|
|
4430
|
+
buffer2[index++] = dec.low.high & 255;
|
|
4431
|
+
buffer2[index++] = dec.low.high >> 8 & 255;
|
|
4432
|
+
buffer2[index++] = dec.low.high >> 16 & 255;
|
|
4433
|
+
buffer2[index++] = dec.low.high >> 24 & 255;
|
|
4434
|
+
buffer2[index++] = dec.high.low & 255;
|
|
4435
|
+
buffer2[index++] = dec.high.low >> 8 & 255;
|
|
4436
|
+
buffer2[index++] = dec.high.low >> 16 & 255;
|
|
4437
|
+
buffer2[index++] = dec.high.low >> 24 & 255;
|
|
4438
|
+
buffer2[index++] = dec.high.high & 255;
|
|
4439
|
+
buffer2[index++] = dec.high.high >> 8 & 255;
|
|
4440
|
+
buffer2[index++] = dec.high.high >> 16 & 255;
|
|
4441
|
+
buffer2[index++] = dec.high.high >> 24 & 255;
|
|
4442
|
+
return new _Decimal128(buffer2);
|
|
4443
|
+
}
|
|
4444
|
+
toString() {
|
|
4445
|
+
let biased_exponent;
|
|
4446
|
+
let significand_digits = 0;
|
|
4447
|
+
const significand = new Array(36);
|
|
4448
|
+
for (let i = 0; i < significand.length; i++)
|
|
4449
|
+
significand[i] = 0;
|
|
4450
|
+
let index = 0;
|
|
4451
|
+
let is_zero = false;
|
|
4452
|
+
let significand_msb;
|
|
4453
|
+
let significand128 = { parts: [0, 0, 0, 0] };
|
|
4454
|
+
let j, k;
|
|
4455
|
+
const string = [];
|
|
4456
|
+
index = 0;
|
|
4457
|
+
const buffer2 = this.bytes;
|
|
4458
|
+
const low = buffer2[index++] | buffer2[index++] << 8 | buffer2[index++] << 16 | buffer2[index++] << 24;
|
|
4459
|
+
const midl = buffer2[index++] | buffer2[index++] << 8 | buffer2[index++] << 16 | buffer2[index++] << 24;
|
|
4460
|
+
const midh = buffer2[index++] | buffer2[index++] << 8 | buffer2[index++] << 16 | buffer2[index++] << 24;
|
|
4461
|
+
const high = buffer2[index++] | buffer2[index++] << 8 | buffer2[index++] << 16 | buffer2[index++] << 24;
|
|
4462
|
+
index = 0;
|
|
4463
|
+
const dec = {
|
|
4464
|
+
low: new Long(low, midl),
|
|
4465
|
+
high: new Long(midh, high)
|
|
4466
|
+
};
|
|
4467
|
+
if (dec.high.lessThan(Long.ZERO)) {
|
|
4468
|
+
string.push("-");
|
|
4469
|
+
}
|
|
4470
|
+
const combination = high >> 26 & COMBINATION_MASK;
|
|
4471
|
+
if (combination >> 3 === 3) {
|
|
4472
|
+
if (combination === COMBINATION_INFINITY) {
|
|
4473
|
+
return string.join("") + "Infinity";
|
|
4474
|
+
} else if (combination === COMBINATION_NAN) {
|
|
4475
|
+
return "NaN";
|
|
4476
|
+
} else {
|
|
4477
|
+
biased_exponent = high >> 15 & EXPONENT_MASK;
|
|
4478
|
+
significand_msb = 8 + (high >> 14 & 1);
|
|
4479
|
+
}
|
|
4480
|
+
} else {
|
|
4481
|
+
significand_msb = high >> 14 & 7;
|
|
4482
|
+
biased_exponent = high >> 17 & EXPONENT_MASK;
|
|
4483
|
+
}
|
|
4484
|
+
const exponent = biased_exponent - EXPONENT_BIAS;
|
|
4485
|
+
significand128.parts[0] = (high & 16383) + ((significand_msb & 15) << 14);
|
|
4486
|
+
significand128.parts[1] = midh;
|
|
4487
|
+
significand128.parts[2] = midl;
|
|
4488
|
+
significand128.parts[3] = low;
|
|
4489
|
+
if (significand128.parts[0] === 0 && significand128.parts[1] === 0 && significand128.parts[2] === 0 && significand128.parts[3] === 0) {
|
|
4490
|
+
is_zero = true;
|
|
4491
|
+
} else {
|
|
4492
|
+
for (k = 3; k >= 0; k--) {
|
|
4493
|
+
let least_digits = 0;
|
|
4494
|
+
const result = divideu128(significand128);
|
|
4495
|
+
significand128 = result.quotient;
|
|
4496
|
+
least_digits = result.rem.low;
|
|
4497
|
+
if (!least_digits)
|
|
4498
|
+
continue;
|
|
4499
|
+
for (j = 8; j >= 0; j--) {
|
|
4500
|
+
significand[k * 9 + j] = least_digits % 10;
|
|
4501
|
+
least_digits = Math.floor(least_digits / 10);
|
|
4502
|
+
}
|
|
4503
|
+
}
|
|
4504
|
+
}
|
|
4505
|
+
if (is_zero) {
|
|
4506
|
+
significand_digits = 1;
|
|
4507
|
+
significand[index] = 0;
|
|
4508
|
+
} else {
|
|
4509
|
+
significand_digits = 36;
|
|
4510
|
+
while (!significand[index]) {
|
|
4511
|
+
significand_digits = significand_digits - 1;
|
|
4512
|
+
index = index + 1;
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
const scientific_exponent = significand_digits - 1 + exponent;
|
|
4516
|
+
if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) {
|
|
4517
|
+
if (significand_digits > 34) {
|
|
4518
|
+
string.push(`${0}`);
|
|
4519
|
+
if (exponent > 0)
|
|
4520
|
+
string.push(`E+${exponent}`);
|
|
4521
|
+
else if (exponent < 0)
|
|
4522
|
+
string.push(`E${exponent}`);
|
|
4523
|
+
return string.join("");
|
|
4524
|
+
}
|
|
4525
|
+
string.push(`${significand[index++]}`);
|
|
4526
|
+
significand_digits = significand_digits - 1;
|
|
4527
|
+
if (significand_digits) {
|
|
4528
|
+
string.push(".");
|
|
4529
|
+
}
|
|
4530
|
+
for (let i = 0; i < significand_digits; i++) {
|
|
4531
|
+
string.push(`${significand[index++]}`);
|
|
4532
|
+
}
|
|
4533
|
+
string.push("E");
|
|
4534
|
+
if (scientific_exponent > 0) {
|
|
4535
|
+
string.push(`+${scientific_exponent}`);
|
|
4536
|
+
} else {
|
|
4537
|
+
string.push(`${scientific_exponent}`);
|
|
4538
|
+
}
|
|
4539
|
+
} else {
|
|
4540
|
+
if (exponent >= 0) {
|
|
4541
|
+
for (let i = 0; i < significand_digits; i++) {
|
|
4542
|
+
string.push(`${significand[index++]}`);
|
|
4543
|
+
}
|
|
4544
|
+
} else {
|
|
4545
|
+
let radix_position = significand_digits + exponent;
|
|
4546
|
+
if (radix_position > 0) {
|
|
4547
|
+
for (let i = 0; i < radix_position; i++) {
|
|
4548
|
+
string.push(`${significand[index++]}`);
|
|
4549
|
+
}
|
|
4550
|
+
} else {
|
|
4551
|
+
string.push("0");
|
|
4552
|
+
}
|
|
4553
|
+
string.push(".");
|
|
4554
|
+
while (radix_position++ < 0) {
|
|
4555
|
+
string.push("0");
|
|
4556
|
+
}
|
|
4557
|
+
for (let i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
|
|
4558
|
+
string.push(`${significand[index++]}`);
|
|
4559
|
+
}
|
|
4560
|
+
}
|
|
4561
|
+
}
|
|
4562
|
+
return string.join("");
|
|
4563
|
+
}
|
|
4564
|
+
toJSON() {
|
|
4565
|
+
return { $numberDecimal: this.toString() };
|
|
4566
|
+
}
|
|
4567
|
+
toExtendedJSON() {
|
|
4568
|
+
return { $numberDecimal: this.toString() };
|
|
4569
|
+
}
|
|
4570
|
+
static fromExtendedJSON(doc) {
|
|
4571
|
+
return _Decimal128.fromString(doc.$numberDecimal);
|
|
4572
|
+
}
|
|
4573
|
+
inspect(depth, options, inspect) {
|
|
4574
|
+
inspect ?? (inspect = defaultInspect);
|
|
4575
|
+
const d128string = inspect(this.toString(), options);
|
|
4576
|
+
return `new Decimal128(${d128string})`;
|
|
4577
|
+
}
|
|
4578
|
+
};
|
|
4579
|
+
__name(_Decimal128, "Decimal128");
|
|
4580
|
+
var Decimal128 = _Decimal128;
|
|
4581
|
+
var _Double = class _Double extends BSONValue {
|
|
4582
|
+
get _bsontype() {
|
|
4583
|
+
return "Double";
|
|
4584
|
+
}
|
|
4585
|
+
constructor(value) {
|
|
4586
|
+
super();
|
|
4587
|
+
if (value instanceof Number) {
|
|
4588
|
+
value = value.valueOf();
|
|
4589
|
+
}
|
|
4590
|
+
this.value = +value;
|
|
4591
|
+
}
|
|
4592
|
+
static fromString(value) {
|
|
4593
|
+
const coercedValue = Number(value);
|
|
4594
|
+
if (value === "NaN")
|
|
4595
|
+
return new _Double(NaN);
|
|
4596
|
+
if (value === "Infinity")
|
|
4597
|
+
return new _Double(Infinity);
|
|
4598
|
+
if (value === "-Infinity")
|
|
4599
|
+
return new _Double(-Infinity);
|
|
4600
|
+
if (!Number.isFinite(coercedValue)) {
|
|
4601
|
+
throw new BSONError(`Input: ${value} is not representable as a Double`);
|
|
4602
|
+
}
|
|
4603
|
+
if (value.trim() !== value) {
|
|
4604
|
+
throw new BSONError(`Input: '${value}' contains whitespace`);
|
|
4605
|
+
}
|
|
4606
|
+
if (value === "") {
|
|
4607
|
+
throw new BSONError(`Input is an empty string`);
|
|
4608
|
+
}
|
|
4609
|
+
if (/[^-0-9.+eE]/.test(value)) {
|
|
4610
|
+
throw new BSONError(`Input: '${value}' is not in decimal or exponential notation`);
|
|
4611
|
+
}
|
|
4612
|
+
return new _Double(coercedValue);
|
|
4613
|
+
}
|
|
4614
|
+
valueOf() {
|
|
4615
|
+
return this.value;
|
|
4616
|
+
}
|
|
4617
|
+
toJSON() {
|
|
4618
|
+
return this.value;
|
|
4619
|
+
}
|
|
4620
|
+
toString(radix) {
|
|
4621
|
+
return this.value.toString(radix);
|
|
4622
|
+
}
|
|
4623
|
+
toExtendedJSON(options) {
|
|
4624
|
+
if (options && (options.legacy || options.relaxed && isFinite(this.value))) {
|
|
4625
|
+
return this.value;
|
|
4626
|
+
}
|
|
4627
|
+
if (Object.is(Math.sign(this.value), -0)) {
|
|
4628
|
+
return { $numberDouble: "-0.0" };
|
|
4629
|
+
}
|
|
4630
|
+
return {
|
|
4631
|
+
$numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
|
|
4632
|
+
};
|
|
4633
|
+
}
|
|
4634
|
+
static fromExtendedJSON(doc, options) {
|
|
4635
|
+
const doubleValue = parseFloat(doc.$numberDouble);
|
|
4636
|
+
return options && options.relaxed ? doubleValue : new _Double(doubleValue);
|
|
4637
|
+
}
|
|
4638
|
+
inspect(depth, options, inspect) {
|
|
4639
|
+
inspect ?? (inspect = defaultInspect);
|
|
4640
|
+
return `new Double(${inspect(this.value, options)})`;
|
|
4641
|
+
}
|
|
4642
|
+
};
|
|
4643
|
+
__name(_Double, "Double");
|
|
4644
|
+
var Double = _Double;
|
|
4645
|
+
var _Int32 = class _Int32 extends BSONValue {
|
|
4646
|
+
get _bsontype() {
|
|
4647
|
+
return "Int32";
|
|
4648
|
+
}
|
|
4649
|
+
constructor(value) {
|
|
4650
|
+
super();
|
|
4651
|
+
if (value instanceof Number) {
|
|
4652
|
+
value = value.valueOf();
|
|
4653
|
+
}
|
|
4654
|
+
this.value = +value | 0;
|
|
4655
|
+
}
|
|
4656
|
+
static fromString(value) {
|
|
4657
|
+
const cleanedValue = removeLeadingZerosAndExplicitPlus(value);
|
|
4658
|
+
const coercedValue = Number(value);
|
|
4659
|
+
if (BSON_INT32_MAX < coercedValue) {
|
|
4660
|
+
throw new BSONError(`Input: '${value}' is larger than the maximum value for Int32`);
|
|
4661
|
+
} else if (BSON_INT32_MIN > coercedValue) {
|
|
4662
|
+
throw new BSONError(`Input: '${value}' is smaller than the minimum value for Int32`);
|
|
4663
|
+
} else if (!Number.isSafeInteger(coercedValue)) {
|
|
4664
|
+
throw new BSONError(`Input: '${value}' is not a safe integer`);
|
|
4665
|
+
} else if (coercedValue.toString() !== cleanedValue) {
|
|
4666
|
+
throw new BSONError(`Input: '${value}' is not a valid Int32 string`);
|
|
4667
|
+
}
|
|
4668
|
+
return new _Int32(coercedValue);
|
|
4669
|
+
}
|
|
4670
|
+
valueOf() {
|
|
4671
|
+
return this.value;
|
|
4672
|
+
}
|
|
4673
|
+
toString(radix) {
|
|
4674
|
+
return this.value.toString(radix);
|
|
4675
|
+
}
|
|
4676
|
+
toJSON() {
|
|
4677
|
+
return this.value;
|
|
4678
|
+
}
|
|
4679
|
+
toExtendedJSON(options) {
|
|
4680
|
+
if (options && (options.relaxed || options.legacy))
|
|
4681
|
+
return this.value;
|
|
4682
|
+
return { $numberInt: this.value.toString() };
|
|
4683
|
+
}
|
|
4684
|
+
static fromExtendedJSON(doc, options) {
|
|
4685
|
+
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new _Int32(doc.$numberInt);
|
|
4686
|
+
}
|
|
4687
|
+
inspect(depth, options, inspect) {
|
|
4688
|
+
inspect ?? (inspect = defaultInspect);
|
|
4689
|
+
return `new Int32(${inspect(this.value, options)})`;
|
|
4690
|
+
}
|
|
4691
|
+
};
|
|
4692
|
+
__name(_Int32, "Int32");
|
|
4693
|
+
var Int32 = _Int32;
|
|
4694
|
+
var _MaxKey = class _MaxKey extends BSONValue {
|
|
4695
|
+
get _bsontype() {
|
|
4696
|
+
return "MaxKey";
|
|
4697
|
+
}
|
|
4698
|
+
toExtendedJSON() {
|
|
4699
|
+
return { $maxKey: 1 };
|
|
4700
|
+
}
|
|
4701
|
+
static fromExtendedJSON() {
|
|
4702
|
+
return new _MaxKey();
|
|
4703
|
+
}
|
|
4704
|
+
inspect() {
|
|
4705
|
+
return "new MaxKey()";
|
|
4706
|
+
}
|
|
4707
|
+
};
|
|
4708
|
+
__name(_MaxKey, "MaxKey");
|
|
4709
|
+
var MaxKey = _MaxKey;
|
|
4710
|
+
var _MinKey = class _MinKey extends BSONValue {
|
|
4711
|
+
get _bsontype() {
|
|
4712
|
+
return "MinKey";
|
|
4713
|
+
}
|
|
4714
|
+
toExtendedJSON() {
|
|
4715
|
+
return { $minKey: 1 };
|
|
4716
|
+
}
|
|
4717
|
+
static fromExtendedJSON() {
|
|
4718
|
+
return new _MinKey();
|
|
4719
|
+
}
|
|
4720
|
+
inspect() {
|
|
4721
|
+
return "new MinKey()";
|
|
4722
|
+
}
|
|
4723
|
+
};
|
|
4724
|
+
__name(_MinKey, "MinKey");
|
|
4725
|
+
var MinKey = _MinKey;
|
|
4726
|
+
var PROCESS_UNIQUE = null;
|
|
4727
|
+
var __idCache = /* @__PURE__ */ new WeakMap();
|
|
4728
|
+
var _ObjectId = class _ObjectId extends BSONValue {
|
|
4729
|
+
get _bsontype() {
|
|
4730
|
+
return "ObjectId";
|
|
4731
|
+
}
|
|
4732
|
+
constructor(inputId) {
|
|
4733
|
+
super();
|
|
4734
|
+
let workingId;
|
|
4735
|
+
if (typeof inputId === "object" && inputId && "id" in inputId) {
|
|
4736
|
+
if (typeof inputId.id !== "string" && !ArrayBuffer.isView(inputId.id)) {
|
|
4737
|
+
throw new BSONError("Argument passed in must have an id that is of type string or Buffer");
|
|
4738
|
+
}
|
|
4739
|
+
if ("toHexString" in inputId && typeof inputId.toHexString === "function") {
|
|
4740
|
+
workingId = ByteUtils.fromHex(inputId.toHexString());
|
|
4741
|
+
} else {
|
|
4742
|
+
workingId = inputId.id;
|
|
4743
|
+
}
|
|
4744
|
+
} else {
|
|
4745
|
+
workingId = inputId;
|
|
4746
|
+
}
|
|
4747
|
+
if (workingId == null || typeof workingId === "number") {
|
|
4748
|
+
this.buffer = _ObjectId.generate(typeof workingId === "number" ? workingId : void 0);
|
|
4749
|
+
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
|
4750
|
+
this.buffer = ByteUtils.toLocalBufferType(workingId);
|
|
4751
|
+
} else if (typeof workingId === "string") {
|
|
4752
|
+
if (_ObjectId.validateHexString(workingId)) {
|
|
4753
|
+
this.buffer = ByteUtils.fromHex(workingId);
|
|
4754
|
+
if (_ObjectId.cacheHexString) {
|
|
4755
|
+
__idCache.set(this, workingId);
|
|
4756
|
+
}
|
|
4757
|
+
} else {
|
|
4758
|
+
throw new BSONError("input must be a 24 character hex string, 12 byte Uint8Array, or an integer");
|
|
4759
|
+
}
|
|
4760
|
+
} else {
|
|
4761
|
+
throw new BSONError("Argument passed in does not match the accepted types");
|
|
4762
|
+
}
|
|
4763
|
+
}
|
|
4764
|
+
get id() {
|
|
4765
|
+
return this.buffer;
|
|
4766
|
+
}
|
|
4767
|
+
set id(value) {
|
|
4768
|
+
this.buffer = value;
|
|
4769
|
+
if (_ObjectId.cacheHexString) {
|
|
4770
|
+
__idCache.set(this, ByteUtils.toHex(value));
|
|
4771
|
+
}
|
|
4772
|
+
}
|
|
4773
|
+
static validateHexString(string) {
|
|
4774
|
+
if (string?.length !== 24)
|
|
4775
|
+
return false;
|
|
4776
|
+
for (let i = 0; i < 24; i++) {
|
|
4777
|
+
const char = string.charCodeAt(i);
|
|
4778
|
+
if (char >= 48 && char <= 57 || char >= 97 && char <= 102 || char >= 65 && char <= 70) {
|
|
4779
|
+
continue;
|
|
4780
|
+
}
|
|
4781
|
+
return false;
|
|
4782
|
+
}
|
|
4783
|
+
return true;
|
|
4784
|
+
}
|
|
4785
|
+
toHexString() {
|
|
4786
|
+
if (_ObjectId.cacheHexString) {
|
|
4787
|
+
const __id = __idCache.get(this);
|
|
4788
|
+
if (__id)
|
|
4789
|
+
return __id;
|
|
4790
|
+
}
|
|
4791
|
+
const hexString = ByteUtils.toHex(this.id);
|
|
4792
|
+
if (_ObjectId.cacheHexString) {
|
|
4793
|
+
__idCache.set(this, hexString);
|
|
4794
|
+
}
|
|
4795
|
+
return hexString;
|
|
4796
|
+
}
|
|
4797
|
+
static getInc() {
|
|
4798
|
+
return _ObjectId.index = (_ObjectId.index + 1) % 16777215;
|
|
4799
|
+
}
|
|
4800
|
+
static generate(time) {
|
|
4801
|
+
if ("number" !== typeof time) {
|
|
4802
|
+
time = Math.floor(Date.now() / 1e3);
|
|
4803
|
+
}
|
|
4804
|
+
const inc = _ObjectId.getInc();
|
|
4805
|
+
const buffer2 = ByteUtils.allocateUnsafe(12);
|
|
4806
|
+
NumberUtils.setInt32BE(buffer2, 0, time);
|
|
4807
|
+
if (PROCESS_UNIQUE === null) {
|
|
4808
|
+
PROCESS_UNIQUE = ByteUtils.randomBytes(5);
|
|
4809
|
+
}
|
|
4810
|
+
buffer2[4] = PROCESS_UNIQUE[0];
|
|
4811
|
+
buffer2[5] = PROCESS_UNIQUE[1];
|
|
4812
|
+
buffer2[6] = PROCESS_UNIQUE[2];
|
|
4813
|
+
buffer2[7] = PROCESS_UNIQUE[3];
|
|
4814
|
+
buffer2[8] = PROCESS_UNIQUE[4];
|
|
4815
|
+
buffer2[11] = inc & 255;
|
|
4816
|
+
buffer2[10] = inc >> 8 & 255;
|
|
4817
|
+
buffer2[9] = inc >> 16 & 255;
|
|
4818
|
+
return buffer2;
|
|
4819
|
+
}
|
|
4820
|
+
toString(encoding) {
|
|
4821
|
+
if (encoding === "base64")
|
|
4822
|
+
return ByteUtils.toBase64(this.id);
|
|
4823
|
+
if (encoding === "hex")
|
|
4824
|
+
return this.toHexString();
|
|
4825
|
+
return this.toHexString();
|
|
4826
|
+
}
|
|
4827
|
+
toJSON() {
|
|
4828
|
+
return this.toHexString();
|
|
4829
|
+
}
|
|
4830
|
+
static is(variable) {
|
|
4831
|
+
return variable != null && typeof variable === "object" && "_bsontype" in variable && variable._bsontype === "ObjectId";
|
|
4832
|
+
}
|
|
4833
|
+
equals(otherId) {
|
|
4834
|
+
if (otherId === void 0 || otherId === null) {
|
|
4835
|
+
return false;
|
|
4836
|
+
}
|
|
4837
|
+
if (_ObjectId.is(otherId)) {
|
|
4838
|
+
return this.buffer[11] === otherId.buffer[11] && ByteUtils.equals(this.buffer, otherId.buffer);
|
|
4839
|
+
}
|
|
4840
|
+
if (typeof otherId === "string") {
|
|
4841
|
+
return otherId.toLowerCase() === this.toHexString();
|
|
4842
|
+
}
|
|
4843
|
+
if (typeof otherId === "object" && typeof otherId.toHexString === "function") {
|
|
4844
|
+
const otherIdString = otherId.toHexString();
|
|
4845
|
+
const thisIdString = this.toHexString();
|
|
4846
|
+
return typeof otherIdString === "string" && otherIdString.toLowerCase() === thisIdString;
|
|
4847
|
+
}
|
|
4848
|
+
return false;
|
|
4849
|
+
}
|
|
4850
|
+
getTimestamp() {
|
|
4851
|
+
const timestamp = /* @__PURE__ */ new Date();
|
|
4852
|
+
const time = NumberUtils.getUint32BE(this.buffer, 0);
|
|
4853
|
+
timestamp.setTime(Math.floor(time) * 1e3);
|
|
4854
|
+
return timestamp;
|
|
4855
|
+
}
|
|
4856
|
+
static createPk() {
|
|
4857
|
+
return new _ObjectId();
|
|
4858
|
+
}
|
|
4859
|
+
serializeInto(uint8array, index) {
|
|
4860
|
+
uint8array[index] = this.buffer[0];
|
|
4861
|
+
uint8array[index + 1] = this.buffer[1];
|
|
4862
|
+
uint8array[index + 2] = this.buffer[2];
|
|
4863
|
+
uint8array[index + 3] = this.buffer[3];
|
|
4864
|
+
uint8array[index + 4] = this.buffer[4];
|
|
4865
|
+
uint8array[index + 5] = this.buffer[5];
|
|
4866
|
+
uint8array[index + 6] = this.buffer[6];
|
|
4867
|
+
uint8array[index + 7] = this.buffer[7];
|
|
4868
|
+
uint8array[index + 8] = this.buffer[8];
|
|
4869
|
+
uint8array[index + 9] = this.buffer[9];
|
|
4870
|
+
uint8array[index + 10] = this.buffer[10];
|
|
4871
|
+
uint8array[index + 11] = this.buffer[11];
|
|
4872
|
+
return 12;
|
|
4873
|
+
}
|
|
4874
|
+
static createFromTime(time) {
|
|
4875
|
+
const buffer2 = ByteUtils.allocate(12);
|
|
4876
|
+
for (let i = 11; i >= 4; i--)
|
|
4877
|
+
buffer2[i] = 0;
|
|
4878
|
+
NumberUtils.setInt32BE(buffer2, 0, time);
|
|
4879
|
+
return new _ObjectId(buffer2);
|
|
4880
|
+
}
|
|
4881
|
+
static createFromHexString(hexString) {
|
|
4882
|
+
if (hexString?.length !== 24) {
|
|
4883
|
+
throw new BSONError("hex string must be 24 characters");
|
|
4884
|
+
}
|
|
4885
|
+
return new _ObjectId(ByteUtils.fromHex(hexString));
|
|
4886
|
+
}
|
|
4887
|
+
static createFromBase64(base64) {
|
|
4888
|
+
if (base64?.length !== 16) {
|
|
4889
|
+
throw new BSONError("base64 string must be 16 characters");
|
|
4890
|
+
}
|
|
4891
|
+
return new _ObjectId(ByteUtils.fromBase64(base64));
|
|
4892
|
+
}
|
|
4893
|
+
static isValid(id) {
|
|
4894
|
+
if (id == null)
|
|
4895
|
+
return false;
|
|
4896
|
+
if (typeof id === "string")
|
|
4897
|
+
return _ObjectId.validateHexString(id);
|
|
4898
|
+
try {
|
|
4899
|
+
new _ObjectId(id);
|
|
4900
|
+
return true;
|
|
4901
|
+
} catch {
|
|
4902
|
+
return false;
|
|
4903
|
+
}
|
|
4904
|
+
}
|
|
4905
|
+
toExtendedJSON() {
|
|
4906
|
+
if (this.toHexString)
|
|
4907
|
+
return { $oid: this.toHexString() };
|
|
4908
|
+
return { $oid: this.toString("hex") };
|
|
4909
|
+
}
|
|
4910
|
+
static fromExtendedJSON(doc) {
|
|
4911
|
+
return new _ObjectId(doc.$oid);
|
|
4912
|
+
}
|
|
4913
|
+
isCached() {
|
|
4914
|
+
return _ObjectId.cacheHexString && __idCache.has(this);
|
|
4915
|
+
}
|
|
4916
|
+
inspect(depth, options, inspect) {
|
|
4917
|
+
inspect ?? (inspect = defaultInspect);
|
|
4918
|
+
return `new ObjectId(${inspect(this.toHexString(), options)})`;
|
|
4919
|
+
}
|
|
4920
|
+
};
|
|
4921
|
+
__name(_ObjectId, "ObjectId");
|
|
4922
|
+
var ObjectId = _ObjectId;
|
|
4923
|
+
ObjectId.index = Math.floor(Math.random() * 16777215);
|
|
4924
|
+
function alphabetize(str) {
|
|
4925
|
+
return str.split("").sort().join("");
|
|
4926
|
+
}
|
|
4927
|
+
__name(alphabetize, "alphabetize");
|
|
4928
|
+
var _BSONRegExp = class _BSONRegExp extends BSONValue {
|
|
4929
|
+
get _bsontype() {
|
|
4930
|
+
return "BSONRegExp";
|
|
4931
|
+
}
|
|
4932
|
+
constructor(pattern, options) {
|
|
4933
|
+
super();
|
|
4934
|
+
this.pattern = pattern;
|
|
4935
|
+
this.options = alphabetize(options ?? "");
|
|
4936
|
+
if (this.pattern.indexOf("\0") !== -1) {
|
|
4937
|
+
throw new BSONError(`BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`);
|
|
4938
|
+
}
|
|
4939
|
+
if (this.options.indexOf("\0") !== -1) {
|
|
4940
|
+
throw new BSONError(`BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`);
|
|
4941
|
+
}
|
|
4942
|
+
for (let i = 0; i < this.options.length; i++) {
|
|
4943
|
+
if (!(this.options[i] === "i" || this.options[i] === "m" || this.options[i] === "x" || this.options[i] === "l" || this.options[i] === "s" || this.options[i] === "u")) {
|
|
4944
|
+
throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`);
|
|
4945
|
+
}
|
|
4946
|
+
}
|
|
4947
|
+
}
|
|
4948
|
+
static parseOptions(options) {
|
|
4949
|
+
return options ? options.split("").sort().join("") : "";
|
|
4950
|
+
}
|
|
4951
|
+
toExtendedJSON(options) {
|
|
4952
|
+
options = options || {};
|
|
4953
|
+
if (options.legacy) {
|
|
4954
|
+
return { $regex: this.pattern, $options: this.options };
|
|
4955
|
+
}
|
|
4956
|
+
return { $regularExpression: { pattern: this.pattern, options: this.options } };
|
|
4957
|
+
}
|
|
4958
|
+
static fromExtendedJSON(doc) {
|
|
4959
|
+
if ("$regex" in doc) {
|
|
4960
|
+
if (typeof doc.$regex !== "string") {
|
|
4961
|
+
if (doc.$regex._bsontype === "BSONRegExp") {
|
|
4962
|
+
return doc;
|
|
4963
|
+
}
|
|
4964
|
+
} else {
|
|
4965
|
+
return new _BSONRegExp(doc.$regex, _BSONRegExp.parseOptions(doc.$options));
|
|
4966
|
+
}
|
|
4967
|
+
}
|
|
4968
|
+
if ("$regularExpression" in doc) {
|
|
4969
|
+
return new _BSONRegExp(doc.$regularExpression.pattern, _BSONRegExp.parseOptions(doc.$regularExpression.options));
|
|
4970
|
+
}
|
|
4971
|
+
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
|
|
4972
|
+
}
|
|
4973
|
+
inspect(depth, options, inspect) {
|
|
4974
|
+
const stylize = getStylizeFunction(options) ?? ((v) => v);
|
|
4975
|
+
inspect ?? (inspect = defaultInspect);
|
|
4976
|
+
const pattern = stylize(inspect(this.pattern), "regexp");
|
|
4977
|
+
const flags = stylize(inspect(this.options), "regexp");
|
|
4978
|
+
return `new BSONRegExp(${pattern}, ${flags})`;
|
|
4979
|
+
}
|
|
4980
|
+
};
|
|
4981
|
+
__name(_BSONRegExp, "BSONRegExp");
|
|
4982
|
+
var BSONRegExp = _BSONRegExp;
|
|
4983
|
+
var _BSONSymbol = class _BSONSymbol extends BSONValue {
|
|
4984
|
+
get _bsontype() {
|
|
4985
|
+
return "BSONSymbol";
|
|
4986
|
+
}
|
|
4987
|
+
constructor(value) {
|
|
4988
|
+
super();
|
|
4989
|
+
this.value = value;
|
|
4990
|
+
}
|
|
4991
|
+
valueOf() {
|
|
4992
|
+
return this.value;
|
|
4993
|
+
}
|
|
4994
|
+
toString() {
|
|
4995
|
+
return this.value;
|
|
4996
|
+
}
|
|
4997
|
+
toJSON() {
|
|
4998
|
+
return this.value;
|
|
4999
|
+
}
|
|
5000
|
+
toExtendedJSON() {
|
|
5001
|
+
return { $symbol: this.value };
|
|
5002
|
+
}
|
|
5003
|
+
static fromExtendedJSON(doc) {
|
|
5004
|
+
return new _BSONSymbol(doc.$symbol);
|
|
5005
|
+
}
|
|
5006
|
+
inspect(depth, options, inspect) {
|
|
5007
|
+
inspect ?? (inspect = defaultInspect);
|
|
5008
|
+
return `new BSONSymbol(${inspect(this.value, options)})`;
|
|
5009
|
+
}
|
|
5010
|
+
};
|
|
5011
|
+
__name(_BSONSymbol, "BSONSymbol");
|
|
5012
|
+
var BSONSymbol = _BSONSymbol;
|
|
5013
|
+
var LongWithoutOverridesClass = Long;
|
|
5014
|
+
var _Timestamp = class _Timestamp extends LongWithoutOverridesClass {
|
|
5015
|
+
get _bsontype() {
|
|
5016
|
+
return "Timestamp";
|
|
5017
|
+
}
|
|
5018
|
+
get i() {
|
|
5019
|
+
return this.low >>> 0;
|
|
5020
|
+
}
|
|
5021
|
+
get t() {
|
|
5022
|
+
return this.high >>> 0;
|
|
5023
|
+
}
|
|
5024
|
+
constructor(low) {
|
|
5025
|
+
if (low == null) {
|
|
5026
|
+
super(0, 0, true);
|
|
5027
|
+
} else if (typeof low === "bigint") {
|
|
5028
|
+
super(low, true);
|
|
5029
|
+
} else if (Long.isLong(low)) {
|
|
5030
|
+
super(low.low, low.high, true);
|
|
5031
|
+
} else if (typeof low === "object" && "t" in low && "i" in low) {
|
|
5032
|
+
if (typeof low.t !== "number" && (typeof low.t !== "object" || low.t._bsontype !== "Int32")) {
|
|
5033
|
+
throw new BSONError("Timestamp constructed from { t, i } must provide t as a number");
|
|
5034
|
+
}
|
|
5035
|
+
if (typeof low.i !== "number" && (typeof low.i !== "object" || low.i._bsontype !== "Int32")) {
|
|
5036
|
+
throw new BSONError("Timestamp constructed from { t, i } must provide i as a number");
|
|
5037
|
+
}
|
|
5038
|
+
const t = Number(low.t);
|
|
5039
|
+
const i = Number(low.i);
|
|
5040
|
+
if (t < 0 || Number.isNaN(t)) {
|
|
5041
|
+
throw new BSONError("Timestamp constructed from { t, i } must provide a positive t");
|
|
5042
|
+
}
|
|
5043
|
+
if (i < 0 || Number.isNaN(i)) {
|
|
5044
|
+
throw new BSONError("Timestamp constructed from { t, i } must provide a positive i");
|
|
5045
|
+
}
|
|
5046
|
+
if (t > 4294967295) {
|
|
5047
|
+
throw new BSONError("Timestamp constructed from { t, i } must provide t equal or less than uint32 max");
|
|
5048
|
+
}
|
|
5049
|
+
if (i > 4294967295) {
|
|
5050
|
+
throw new BSONError("Timestamp constructed from { t, i } must provide i equal or less than uint32 max");
|
|
5051
|
+
}
|
|
5052
|
+
super(i, t, true);
|
|
5053
|
+
} else {
|
|
5054
|
+
throw new BSONError("A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }");
|
|
5055
|
+
}
|
|
5056
|
+
}
|
|
5057
|
+
toJSON() {
|
|
5058
|
+
return {
|
|
5059
|
+
$timestamp: this.toString()
|
|
5060
|
+
};
|
|
5061
|
+
}
|
|
5062
|
+
static fromInt(value) {
|
|
5063
|
+
return new _Timestamp(Long.fromInt(value, true));
|
|
5064
|
+
}
|
|
5065
|
+
static fromNumber(value) {
|
|
5066
|
+
return new _Timestamp(Long.fromNumber(value, true));
|
|
5067
|
+
}
|
|
5068
|
+
static fromBits(lowBits, highBits) {
|
|
5069
|
+
return new _Timestamp({ i: lowBits, t: highBits });
|
|
5070
|
+
}
|
|
5071
|
+
static fromString(str, optRadix) {
|
|
5072
|
+
return new _Timestamp(Long.fromString(str, true, optRadix));
|
|
5073
|
+
}
|
|
5074
|
+
toExtendedJSON() {
|
|
5075
|
+
return { $timestamp: { t: this.t, i: this.i } };
|
|
5076
|
+
}
|
|
5077
|
+
static fromExtendedJSON(doc) {
|
|
5078
|
+
const i = Long.isLong(doc.$timestamp.i) ? doc.$timestamp.i.getLowBitsUnsigned() : doc.$timestamp.i;
|
|
5079
|
+
const t = Long.isLong(doc.$timestamp.t) ? doc.$timestamp.t.getLowBitsUnsigned() : doc.$timestamp.t;
|
|
5080
|
+
return new _Timestamp({ t, i });
|
|
5081
|
+
}
|
|
5082
|
+
inspect(depth, options, inspect) {
|
|
5083
|
+
inspect ?? (inspect = defaultInspect);
|
|
5084
|
+
const t = inspect(this.t, options);
|
|
5085
|
+
const i = inspect(this.i, options);
|
|
5086
|
+
return `new Timestamp({ t: ${t}, i: ${i} })`;
|
|
5087
|
+
}
|
|
5088
|
+
};
|
|
5089
|
+
__name(_Timestamp, "Timestamp");
|
|
5090
|
+
var Timestamp = _Timestamp;
|
|
5091
|
+
Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
|
|
5092
|
+
var JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
|
|
5093
|
+
var JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
|
|
5094
|
+
function isBSONType(value) {
|
|
5095
|
+
return value != null && typeof value === "object" && "_bsontype" in value && typeof value._bsontype === "string";
|
|
5096
|
+
}
|
|
5097
|
+
__name(isBSONType, "isBSONType");
|
|
5098
|
+
var keysToCodecs = {
|
|
5099
|
+
$oid: ObjectId,
|
|
5100
|
+
$binary: Binary,
|
|
5101
|
+
$uuid: Binary,
|
|
5102
|
+
$symbol: BSONSymbol,
|
|
5103
|
+
$numberInt: Int32,
|
|
5104
|
+
$numberDecimal: Decimal128,
|
|
5105
|
+
$numberDouble: Double,
|
|
5106
|
+
$numberLong: Long,
|
|
5107
|
+
$minKey: MinKey,
|
|
5108
|
+
$maxKey: MaxKey,
|
|
5109
|
+
$regex: BSONRegExp,
|
|
5110
|
+
$regularExpression: BSONRegExp,
|
|
5111
|
+
$timestamp: Timestamp
|
|
5112
|
+
};
|
|
5113
|
+
function deserializeValue(value, options = {}) {
|
|
5114
|
+
if (typeof value === "number") {
|
|
5115
|
+
const in32BitRange = value <= BSON_INT32_MAX && value >= BSON_INT32_MIN;
|
|
5116
|
+
const in64BitRange = value <= BSON_INT64_MAX && value >= BSON_INT64_MIN;
|
|
5117
|
+
if (options.relaxed || options.legacy) {
|
|
5118
|
+
return value;
|
|
5119
|
+
}
|
|
5120
|
+
if (Number.isInteger(value) && !Object.is(value, -0)) {
|
|
5121
|
+
if (in32BitRange) {
|
|
5122
|
+
return new Int32(value);
|
|
5123
|
+
}
|
|
5124
|
+
if (in64BitRange) {
|
|
5125
|
+
if (options.useBigInt64) {
|
|
5126
|
+
return BigInt(value);
|
|
5127
|
+
}
|
|
5128
|
+
return Long.fromNumber(value);
|
|
5129
|
+
}
|
|
5130
|
+
}
|
|
5131
|
+
return new Double(value);
|
|
5132
|
+
}
|
|
5133
|
+
if (value == null || typeof value !== "object")
|
|
5134
|
+
return value;
|
|
5135
|
+
if (value.$undefined)
|
|
5136
|
+
return null;
|
|
5137
|
+
const keys = Object.keys(value).filter((k) => k.startsWith("$") && value[k] != null);
|
|
5138
|
+
for (let i = 0; i < keys.length; i++) {
|
|
5139
|
+
const c = keysToCodecs[keys[i]];
|
|
5140
|
+
if (c)
|
|
5141
|
+
return c.fromExtendedJSON(value, options);
|
|
5142
|
+
}
|
|
5143
|
+
if (value.$date != null) {
|
|
5144
|
+
const d = value.$date;
|
|
5145
|
+
const date = /* @__PURE__ */ new Date();
|
|
5146
|
+
if (options.legacy) {
|
|
5147
|
+
if (typeof d === "number")
|
|
5148
|
+
date.setTime(d);
|
|
5149
|
+
else if (typeof d === "string")
|
|
5150
|
+
date.setTime(Date.parse(d));
|
|
5151
|
+
else if (typeof d === "bigint")
|
|
5152
|
+
date.setTime(Number(d));
|
|
5153
|
+
else
|
|
5154
|
+
throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
|
|
5155
|
+
} else {
|
|
5156
|
+
if (typeof d === "string")
|
|
5157
|
+
date.setTime(Date.parse(d));
|
|
5158
|
+
else if (Long.isLong(d))
|
|
5159
|
+
date.setTime(d.toNumber());
|
|
5160
|
+
else if (typeof d === "number" && options.relaxed)
|
|
5161
|
+
date.setTime(d);
|
|
5162
|
+
else if (typeof d === "bigint")
|
|
5163
|
+
date.setTime(Number(d));
|
|
5164
|
+
else
|
|
5165
|
+
throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`);
|
|
5166
|
+
}
|
|
5167
|
+
return date;
|
|
5168
|
+
}
|
|
5169
|
+
if (value.$code != null) {
|
|
5170
|
+
const copy = Object.assign({}, value);
|
|
5171
|
+
if (value.$scope) {
|
|
5172
|
+
copy.$scope = deserializeValue(value.$scope);
|
|
5173
|
+
}
|
|
5174
|
+
return Code.fromExtendedJSON(value);
|
|
5175
|
+
}
|
|
5176
|
+
if (isDBRefLike(value) || value.$dbPointer) {
|
|
5177
|
+
const v = value.$ref ? value : value.$dbPointer;
|
|
5178
|
+
if (v instanceof DBRef)
|
|
5179
|
+
return v;
|
|
5180
|
+
const dollarKeys = Object.keys(v).filter((k) => k.startsWith("$"));
|
|
5181
|
+
let valid = true;
|
|
5182
|
+
dollarKeys.forEach((k) => {
|
|
5183
|
+
if (["$ref", "$id", "$db"].indexOf(k) === -1)
|
|
5184
|
+
valid = false;
|
|
5185
|
+
});
|
|
5186
|
+
if (valid)
|
|
5187
|
+
return DBRef.fromExtendedJSON(v);
|
|
5188
|
+
}
|
|
5189
|
+
return value;
|
|
5190
|
+
}
|
|
5191
|
+
__name(deserializeValue, "deserializeValue");
|
|
5192
|
+
function serializeArray(array, options) {
|
|
5193
|
+
return array.map((v, index) => {
|
|
5194
|
+
options.seenObjects.push({ propertyName: `index ${index}`, obj: null });
|
|
5195
|
+
try {
|
|
5196
|
+
return serializeValue(v, options);
|
|
5197
|
+
} finally {
|
|
5198
|
+
options.seenObjects.pop();
|
|
5199
|
+
}
|
|
5200
|
+
});
|
|
5201
|
+
}
|
|
5202
|
+
__name(serializeArray, "serializeArray");
|
|
5203
|
+
function getISOString(date) {
|
|
5204
|
+
const isoStr = date.toISOString();
|
|
5205
|
+
return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + "Z";
|
|
5206
|
+
}
|
|
5207
|
+
__name(getISOString, "getISOString");
|
|
5208
|
+
function serializeValue(value, options) {
|
|
5209
|
+
if (value instanceof Map || isMap(value)) {
|
|
5210
|
+
const obj = /* @__PURE__ */ Object.create(null);
|
|
5211
|
+
for (const [k, v] of value) {
|
|
5212
|
+
if (typeof k !== "string") {
|
|
5213
|
+
throw new BSONError("Can only serialize maps with string keys");
|
|
5214
|
+
}
|
|
5215
|
+
obj[k] = v;
|
|
5216
|
+
}
|
|
5217
|
+
return serializeValue(obj, options);
|
|
5218
|
+
}
|
|
5219
|
+
if ((typeof value === "object" || typeof value === "function") && value !== null) {
|
|
5220
|
+
const index = options.seenObjects.findIndex((entry) => entry.obj === value);
|
|
5221
|
+
if (index !== -1) {
|
|
5222
|
+
const props = options.seenObjects.map((entry) => entry.propertyName);
|
|
5223
|
+
const leadingPart = props.slice(0, index).map((prop) => `${prop} -> `).join("");
|
|
5224
|
+
const alreadySeen = props[index];
|
|
5225
|
+
const circularPart = " -> " + props.slice(index + 1, props.length - 1).map((prop) => `${prop} -> `).join("");
|
|
5226
|
+
const current = props[props.length - 1];
|
|
5227
|
+
const leadingSpace = " ".repeat(leadingPart.length + alreadySeen.length / 2);
|
|
5228
|
+
const dashes = "-".repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
|
|
5229
|
+
throw new BSONError(`Converting circular structure to EJSON:
|
|
5230
|
+
${leadingPart}${alreadySeen}${circularPart}${current}
|
|
5231
|
+
${leadingSpace}\\${dashes}/`);
|
|
5232
|
+
}
|
|
5233
|
+
options.seenObjects[options.seenObjects.length - 1].obj = value;
|
|
5234
|
+
}
|
|
5235
|
+
if (Array.isArray(value))
|
|
5236
|
+
return serializeArray(value, options);
|
|
5237
|
+
if (value === void 0)
|
|
5238
|
+
return null;
|
|
5239
|
+
if (value instanceof Date || isDate(value)) {
|
|
5240
|
+
const dateNum = value.getTime(), inRange = dateNum > -1 && dateNum < 2534023188e5;
|
|
5241
|
+
if (options.legacy) {
|
|
5242
|
+
return options.relaxed && inRange ? { $date: value.getTime() } : { $date: getISOString(value) };
|
|
5243
|
+
}
|
|
5244
|
+
return options.relaxed && inRange ? { $date: getISOString(value) } : { $date: { $numberLong: value.getTime().toString() } };
|
|
5245
|
+
}
|
|
5246
|
+
if (typeof value === "number" && (!options.relaxed || !isFinite(value))) {
|
|
5247
|
+
if (Number.isInteger(value) && !Object.is(value, -0)) {
|
|
5248
|
+
if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) {
|
|
5249
|
+
return { $numberInt: value.toString() };
|
|
5250
|
+
}
|
|
5251
|
+
if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) {
|
|
5252
|
+
return { $numberLong: value.toString() };
|
|
5253
|
+
}
|
|
5254
|
+
}
|
|
5255
|
+
return { $numberDouble: Object.is(value, -0) ? "-0.0" : value.toString() };
|
|
5256
|
+
}
|
|
5257
|
+
if (typeof value === "bigint") {
|
|
5258
|
+
if (!options.relaxed) {
|
|
5259
|
+
return { $numberLong: BigInt.asIntN(64, value).toString() };
|
|
5260
|
+
}
|
|
5261
|
+
return Number(BigInt.asIntN(64, value));
|
|
5262
|
+
}
|
|
5263
|
+
if (value instanceof RegExp || isRegExp(value)) {
|
|
5264
|
+
let flags = value.flags;
|
|
5265
|
+
if (flags === void 0) {
|
|
5266
|
+
const match = value.toString().match(/[gimuy]*$/);
|
|
5267
|
+
if (match) {
|
|
5268
|
+
flags = match[0];
|
|
5269
|
+
}
|
|
5270
|
+
}
|
|
5271
|
+
const rx = new BSONRegExp(value.source, flags);
|
|
5272
|
+
return rx.toExtendedJSON(options);
|
|
5273
|
+
}
|
|
5274
|
+
if (value != null && typeof value === "object")
|
|
5275
|
+
return serializeDocument(value, options);
|
|
5276
|
+
return value;
|
|
5277
|
+
}
|
|
5278
|
+
__name(serializeValue, "serializeValue");
|
|
5279
|
+
var BSON_TYPE_MAPPINGS = {
|
|
5280
|
+
Binary: /* @__PURE__ */ __name((o) => new Binary(o.value(), o.sub_type), "Binary"),
|
|
5281
|
+
Code: /* @__PURE__ */ __name((o) => new Code(o.code, o.scope), "Code"),
|
|
5282
|
+
DBRef: /* @__PURE__ */ __name((o) => new DBRef(o.collection || o.namespace, o.oid, o.db, o.fields), "DBRef"),
|
|
5283
|
+
Decimal128: /* @__PURE__ */ __name((o) => new Decimal128(o.bytes), "Decimal128"),
|
|
5284
|
+
Double: /* @__PURE__ */ __name((o) => new Double(o.value), "Double"),
|
|
5285
|
+
Int32: /* @__PURE__ */ __name((o) => new Int32(o.value), "Int32"),
|
|
5286
|
+
Long: /* @__PURE__ */ __name((o) => Long.fromBits(o.low != null ? o.low : o.low_, o.low != null ? o.high : o.high_, o.low != null ? o.unsigned : o.unsigned_), "Long"),
|
|
5287
|
+
MaxKey: /* @__PURE__ */ __name(() => new MaxKey(), "MaxKey"),
|
|
5288
|
+
MinKey: /* @__PURE__ */ __name(() => new MinKey(), "MinKey"),
|
|
5289
|
+
ObjectId: /* @__PURE__ */ __name((o) => new ObjectId(o), "ObjectId"),
|
|
5290
|
+
BSONRegExp: /* @__PURE__ */ __name((o) => new BSONRegExp(o.pattern, o.options), "BSONRegExp"),
|
|
5291
|
+
BSONSymbol: /* @__PURE__ */ __name((o) => new BSONSymbol(o.value), "BSONSymbol"),
|
|
5292
|
+
Timestamp: /* @__PURE__ */ __name((o) => Timestamp.fromBits(o.low, o.high), "Timestamp")
|
|
5293
|
+
};
|
|
5294
|
+
function serializeDocument(doc, options) {
|
|
5295
|
+
if (doc == null || typeof doc !== "object")
|
|
5296
|
+
throw new BSONError("not an object instance");
|
|
5297
|
+
const bsontype = doc._bsontype;
|
|
5298
|
+
if (typeof bsontype === "undefined") {
|
|
5299
|
+
const _doc = {};
|
|
5300
|
+
for (const name of Object.keys(doc)) {
|
|
5301
|
+
options.seenObjects.push({ propertyName: name, obj: null });
|
|
5302
|
+
try {
|
|
5303
|
+
const value = serializeValue(doc[name], options);
|
|
5304
|
+
if (name === "__proto__") {
|
|
5305
|
+
Object.defineProperty(_doc, name, {
|
|
5306
|
+
value,
|
|
5307
|
+
writable: true,
|
|
5308
|
+
enumerable: true,
|
|
5309
|
+
configurable: true
|
|
5310
|
+
});
|
|
5311
|
+
} else {
|
|
5312
|
+
_doc[name] = value;
|
|
5313
|
+
}
|
|
5314
|
+
} finally {
|
|
5315
|
+
options.seenObjects.pop();
|
|
5316
|
+
}
|
|
5317
|
+
}
|
|
5318
|
+
return _doc;
|
|
5319
|
+
} else if (doc != null && typeof doc === "object" && typeof doc._bsontype === "string" && doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
|
|
5320
|
+
throw new BSONVersionError();
|
|
5321
|
+
} else if (isBSONType(doc)) {
|
|
5322
|
+
let outDoc = doc;
|
|
5323
|
+
if (typeof outDoc.toExtendedJSON !== "function") {
|
|
5324
|
+
const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
|
|
5325
|
+
if (!mapper) {
|
|
5326
|
+
throw new BSONError("Unrecognized or invalid _bsontype: " + doc._bsontype);
|
|
5327
|
+
}
|
|
5328
|
+
outDoc = mapper(outDoc);
|
|
5329
|
+
}
|
|
5330
|
+
if (bsontype === "Code" && outDoc.scope) {
|
|
5331
|
+
outDoc = new Code(outDoc.code, serializeValue(outDoc.scope, options));
|
|
5332
|
+
} else if (bsontype === "DBRef" && outDoc.oid) {
|
|
5333
|
+
outDoc = new DBRef(serializeValue(outDoc.collection, options), serializeValue(outDoc.oid, options), serializeValue(outDoc.db, options), serializeValue(outDoc.fields, options));
|
|
5334
|
+
}
|
|
5335
|
+
return outDoc.toExtendedJSON(options);
|
|
5336
|
+
} else {
|
|
5337
|
+
throw new BSONError("_bsontype must be a string, but was: " + typeof bsontype);
|
|
5338
|
+
}
|
|
5339
|
+
}
|
|
5340
|
+
__name(serializeDocument, "serializeDocument");
|
|
5341
|
+
function parse2(text, options) {
|
|
5342
|
+
const ejsonOptions = {
|
|
5343
|
+
useBigInt64: options?.useBigInt64 ?? false,
|
|
5344
|
+
relaxed: options?.relaxed ?? true,
|
|
5345
|
+
legacy: options?.legacy ?? false
|
|
5346
|
+
};
|
|
5347
|
+
return JSON.parse(text, (key, value) => {
|
|
5348
|
+
if (key.indexOf("\0") !== -1) {
|
|
5349
|
+
throw new BSONError(`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`);
|
|
5350
|
+
}
|
|
5351
|
+
return deserializeValue(value, ejsonOptions);
|
|
5352
|
+
});
|
|
5353
|
+
}
|
|
5354
|
+
__name(parse2, "parse");
|
|
5355
|
+
function stringify(value, replacer, space, options) {
|
|
5356
|
+
if (space != null && typeof space === "object") {
|
|
5357
|
+
options = space;
|
|
5358
|
+
space = 0;
|
|
5359
|
+
}
|
|
5360
|
+
if (replacer != null && typeof replacer === "object" && !Array.isArray(replacer)) {
|
|
5361
|
+
options = replacer;
|
|
5362
|
+
replacer = void 0;
|
|
5363
|
+
space = 0;
|
|
5364
|
+
}
|
|
5365
|
+
const serializeOptions = Object.assign({ relaxed: true, legacy: false }, options, {
|
|
5366
|
+
seenObjects: [{ propertyName: "(root)", obj: null }]
|
|
5367
|
+
});
|
|
5368
|
+
const doc = serializeValue(value, serializeOptions);
|
|
5369
|
+
return JSON.stringify(doc, replacer, space);
|
|
5370
|
+
}
|
|
5371
|
+
__name(stringify, "stringify");
|
|
5372
|
+
function EJSONserialize(value, options) {
|
|
5373
|
+
options = options || {};
|
|
5374
|
+
return JSON.parse(stringify(value, options));
|
|
5375
|
+
}
|
|
5376
|
+
__name(EJSONserialize, "EJSONserialize");
|
|
5377
|
+
function EJSONdeserialize(ejson, options) {
|
|
5378
|
+
options = options || {};
|
|
5379
|
+
return parse2(JSON.stringify(ejson), options);
|
|
5380
|
+
}
|
|
5381
|
+
__name(EJSONdeserialize, "EJSONdeserialize");
|
|
5382
|
+
var EJSON = /* @__PURE__ */ Object.create(null);
|
|
5383
|
+
EJSON.parse = parse2;
|
|
5384
|
+
EJSON.stringify = stringify;
|
|
5385
|
+
EJSON.serialize = EJSONserialize;
|
|
5386
|
+
EJSON.deserialize = EJSONdeserialize;
|
|
5387
|
+
Object.freeze(EJSON);
|
|
5388
|
+
var BSONElementType = {
|
|
5389
|
+
double: 1,
|
|
5390
|
+
string: 2,
|
|
5391
|
+
object: 3,
|
|
5392
|
+
array: 4,
|
|
5393
|
+
binData: 5,
|
|
5394
|
+
undefined: 6,
|
|
5395
|
+
objectId: 7,
|
|
5396
|
+
bool: 8,
|
|
5397
|
+
date: 9,
|
|
5398
|
+
null: 10,
|
|
5399
|
+
regex: 11,
|
|
5400
|
+
dbPointer: 12,
|
|
5401
|
+
javascript: 13,
|
|
5402
|
+
symbol: 14,
|
|
5403
|
+
javascriptWithScope: 15,
|
|
5404
|
+
int: 16,
|
|
5405
|
+
timestamp: 17,
|
|
5406
|
+
long: 18,
|
|
5407
|
+
decimal: 19,
|
|
5408
|
+
minKey: 255,
|
|
5409
|
+
maxKey: 127
|
|
5410
|
+
};
|
|
5411
|
+
function getSize(source, offset) {
|
|
5412
|
+
try {
|
|
5413
|
+
return NumberUtils.getNonnegativeInt32LE(source, offset);
|
|
5414
|
+
} catch (cause) {
|
|
5415
|
+
throw new BSONOffsetError("BSON size cannot be negative", offset, { cause });
|
|
5416
|
+
}
|
|
5417
|
+
}
|
|
5418
|
+
__name(getSize, "getSize");
|
|
5419
|
+
function findNull(bytes, offset) {
|
|
5420
|
+
let nullTerminatorOffset = offset;
|
|
5421
|
+
for (; bytes[nullTerminatorOffset] !== 0; nullTerminatorOffset++)
|
|
5422
|
+
;
|
|
5423
|
+
if (nullTerminatorOffset === bytes.length - 1) {
|
|
5424
|
+
throw new BSONOffsetError("Null terminator not found", offset);
|
|
5425
|
+
}
|
|
5426
|
+
return nullTerminatorOffset;
|
|
5427
|
+
}
|
|
5428
|
+
__name(findNull, "findNull");
|
|
5429
|
+
function parseToElements(bytes, startOffset = 0) {
|
|
5430
|
+
startOffset ?? (startOffset = 0);
|
|
5431
|
+
if (bytes.length < 5) {
|
|
5432
|
+
throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
|
|
5433
|
+
}
|
|
5434
|
+
const documentSize = getSize(bytes, startOffset);
|
|
5435
|
+
if (documentSize > bytes.length - startOffset) {
|
|
5436
|
+
throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
|
|
5437
|
+
}
|
|
5438
|
+
if (bytes[startOffset + documentSize - 1] !== 0) {
|
|
5439
|
+
throw new BSONOffsetError("BSON documents must end in 0x00", startOffset + documentSize);
|
|
5440
|
+
}
|
|
5441
|
+
const elements = [];
|
|
5442
|
+
let offset = startOffset + 4;
|
|
5443
|
+
while (offset <= documentSize + startOffset) {
|
|
5444
|
+
const type = bytes[offset];
|
|
5445
|
+
offset += 1;
|
|
5446
|
+
if (type === 0) {
|
|
5447
|
+
if (offset - startOffset !== documentSize) {
|
|
5448
|
+
throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
|
|
5449
|
+
}
|
|
5450
|
+
break;
|
|
5451
|
+
}
|
|
5452
|
+
const nameOffset = offset;
|
|
5453
|
+
const nameLength = findNull(bytes, offset) - nameOffset;
|
|
5454
|
+
offset += nameLength + 1;
|
|
5455
|
+
let length;
|
|
5456
|
+
if (type === BSONElementType.double || type === BSONElementType.long || type === BSONElementType.date || type === BSONElementType.timestamp) {
|
|
5457
|
+
length = 8;
|
|
5458
|
+
} else if (type === BSONElementType.int) {
|
|
5459
|
+
length = 4;
|
|
5460
|
+
} else if (type === BSONElementType.objectId) {
|
|
5461
|
+
length = 12;
|
|
5462
|
+
} else if (type === BSONElementType.decimal) {
|
|
5463
|
+
length = 16;
|
|
5464
|
+
} else if (type === BSONElementType.bool) {
|
|
5465
|
+
length = 1;
|
|
5466
|
+
} else if (type === BSONElementType.null || type === BSONElementType.undefined || type === BSONElementType.maxKey || type === BSONElementType.minKey) {
|
|
5467
|
+
length = 0;
|
|
5468
|
+
} else if (type === BSONElementType.regex) {
|
|
5469
|
+
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
|
|
5470
|
+
} else if (type === BSONElementType.object || type === BSONElementType.array || type === BSONElementType.javascriptWithScope) {
|
|
5471
|
+
length = getSize(bytes, offset);
|
|
5472
|
+
} else if (type === BSONElementType.string || type === BSONElementType.binData || type === BSONElementType.dbPointer || type === BSONElementType.javascript || type === BSONElementType.symbol) {
|
|
5473
|
+
length = getSize(bytes, offset) + 4;
|
|
5474
|
+
if (type === BSONElementType.binData) {
|
|
5475
|
+
length += 1;
|
|
5476
|
+
}
|
|
5477
|
+
if (type === BSONElementType.dbPointer) {
|
|
5478
|
+
length += 12;
|
|
5479
|
+
}
|
|
5480
|
+
} else {
|
|
5481
|
+
throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, "0")} type byte`, offset);
|
|
5482
|
+
}
|
|
5483
|
+
if (length > documentSize) {
|
|
5484
|
+
throw new BSONOffsetError("value reports length larger than document", offset);
|
|
5485
|
+
}
|
|
5486
|
+
elements.push([type, nameOffset, nameLength, offset, length]);
|
|
5487
|
+
offset += length;
|
|
5488
|
+
}
|
|
5489
|
+
return elements;
|
|
5490
|
+
}
|
|
5491
|
+
__name(parseToElements, "parseToElements");
|
|
5492
|
+
var onDemand = /* @__PURE__ */ Object.create(null);
|
|
5493
|
+
onDemand.parseToElements = parseToElements;
|
|
5494
|
+
onDemand.ByteUtils = ByteUtils;
|
|
5495
|
+
onDemand.NumberUtils = NumberUtils;
|
|
5496
|
+
Object.freeze(onDemand);
|
|
5497
|
+
var MAXSIZE = 1024 * 1024 * 17;
|
|
5498
|
+
var buffer = ByteUtils.allocate(MAXSIZE);
|
|
5499
|
+
|
|
2329
5500
|
// src/framework/abdb/collection/index.ts
|
|
2330
5501
|
import { init as initDb, DbError } from "@adobe/aio-lib-db";
|
|
2331
5502
|
|
|
@@ -2831,144 +6002,193 @@ var _AbdbRepository = class _AbdbRepository {
|
|
|
2831
6002
|
);
|
|
2832
6003
|
}
|
|
2833
6004
|
/**
|
|
2834
|
-
* Returns
|
|
2835
|
-
* Uses `countDocuments` rather than fetching the full document for efficiency.
|
|
6005
|
+
* Returns the document with the given `_id`, or `null` if none found.
|
|
2836
6006
|
*
|
|
2837
|
-
* @param id - The `_id` to
|
|
2838
|
-
* @returns
|
|
6007
|
+
* @param id - The `_id` of the document to find (string or ObjectId)
|
|
6008
|
+
* @returns The matched document, or `null`
|
|
2839
6009
|
*/
|
|
2840
|
-
async
|
|
2841
|
-
|
|
2842
|
-
|
|
6010
|
+
async findById(id) {
|
|
6011
|
+
return await this.findOne({ _id: new ObjectId(String(id)) });
|
|
6012
|
+
}
|
|
6013
|
+
/**
|
|
6014
|
+
* Deletes all documents matching `filter` using a single `deleteMany` call.
|
|
6015
|
+
* Passing no filter (or an empty object) deletes every document in the collection.
|
|
6016
|
+
*
|
|
6017
|
+
* @param filter - Query filter (e.g. `{ _id: 'abc' }` or `{ email: 'a@b.com' }`)
|
|
6018
|
+
* @returns The raw `deleteMany` result document (e.g. `acknowledged`, `deletedCount`)
|
|
6019
|
+
*/
|
|
6020
|
+
async delete(filter = {}) {
|
|
6021
|
+
return await this._collection.run(
|
|
2843
6022
|
(collection) => {
|
|
2844
|
-
return collection.
|
|
6023
|
+
return collection.deleteMany(filter);
|
|
2845
6024
|
},
|
|
2846
6025
|
this._token,
|
|
2847
6026
|
this._region
|
|
2848
6027
|
);
|
|
2849
|
-
return n > 0;
|
|
2850
6028
|
}
|
|
2851
6029
|
/**
|
|
2852
|
-
*
|
|
2853
|
-
* Passing no filter (or an empty object) counts every document.
|
|
6030
|
+
* Deletes the first document matching `filter` using a single `deleteOne` call.
|
|
2854
6031
|
*
|
|
2855
|
-
* @param filter -
|
|
2856
|
-
* @returns
|
|
6032
|
+
* @param filter - Query filter (e.g. `{ _id: 'abc' }` or `{ email: 'a@b.com' }`)
|
|
6033
|
+
* @returns The raw `deleteOne` result document (e.g. `acknowledged`, `deletedCount`)
|
|
2857
6034
|
*/
|
|
2858
|
-
async
|
|
2859
|
-
return this._collection.run(
|
|
6035
|
+
async deleteOne(filter = {}) {
|
|
6036
|
+
return await this._collection.run(
|
|
2860
6037
|
(collection) => {
|
|
2861
|
-
return collection.
|
|
6038
|
+
return collection.deleteOne(filter);
|
|
2862
6039
|
},
|
|
2863
6040
|
this._token,
|
|
2864
6041
|
this._region
|
|
2865
6042
|
);
|
|
2866
6043
|
}
|
|
2867
6044
|
/**
|
|
2868
|
-
*
|
|
6045
|
+
* Deletes the document with the given `_id` using a single `deleteOne` call.
|
|
2869
6046
|
*
|
|
2870
|
-
* -
|
|
2871
|
-
*
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
6047
|
+
* @param id - The `_id` of the document to delete (string or ObjectId)
|
|
6048
|
+
* @returns The raw `deleteOne` result document (e.g. `acknowledged`, `deletedCount`)
|
|
6049
|
+
*/
|
|
6050
|
+
async deleteById(id) {
|
|
6051
|
+
return await this.deleteOne({ _id: new ObjectId(String(id)) });
|
|
6052
|
+
}
|
|
6053
|
+
/**
|
|
6054
|
+
* Inserts multiple documents in a single `insertMany` call.
|
|
6055
|
+
* Each payload is stamped with `_created_at` / `_updated_at` and validated
|
|
6056
|
+
* against the full collection schema before the bulk write is sent to the DB.
|
|
2876
6057
|
*
|
|
2877
|
-
* @param
|
|
2878
|
-
* @
|
|
2879
|
-
* @returns The `_id` of the inserted or updated document
|
|
6058
|
+
* @param payloads - Array of document payloads to insert
|
|
6059
|
+
* @returns The raw `insertMany` result document (e.g. `acknowledged`, `insertedIds`)
|
|
2880
6060
|
*/
|
|
2881
|
-
async
|
|
6061
|
+
async insert(payloads) {
|
|
2882
6062
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2883
|
-
|
|
2884
|
-
const
|
|
2885
|
-
this.
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
this._region
|
|
2892
|
-
);
|
|
2893
|
-
return id;
|
|
2894
|
-
}
|
|
2895
|
-
const insertPayload = { ...payload, _created_at: now, _updated_at: now };
|
|
2896
|
-
this._collection.validate(insertPayload);
|
|
2897
|
-
return this._collection.run(
|
|
2898
|
-
async (collection) => {
|
|
2899
|
-
return collection.insertOne(insertPayload);
|
|
6063
|
+
const insertPayloads = payloads.map((payload) => {
|
|
6064
|
+
const doc = { ...payload, _created_at: now, _updated_at: now };
|
|
6065
|
+
this._collection.validate(doc);
|
|
6066
|
+
return doc;
|
|
6067
|
+
});
|
|
6068
|
+
return await this._collection.run(
|
|
6069
|
+
(collection) => {
|
|
6070
|
+
return collection.insertMany(insertPayloads);
|
|
2900
6071
|
},
|
|
2901
6072
|
this._token,
|
|
2902
6073
|
this._region
|
|
2903
6074
|
);
|
|
2904
6075
|
}
|
|
2905
6076
|
/**
|
|
2906
|
-
*
|
|
6077
|
+
* Inserts a single document in a single `insertOne` call.
|
|
6078
|
+
* The payload is stamped with `_created_at` / `_updated_at` and validated
|
|
6079
|
+
* against the full collection schema before the write is sent to the DB.
|
|
2907
6080
|
*
|
|
2908
|
-
* @param
|
|
6081
|
+
* @param payload - Document payload to insert
|
|
6082
|
+
* @returns The raw `insertOne` result document (e.g. `acknowledged`, `insertedId`)
|
|
2909
6083
|
*/
|
|
2910
|
-
async
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
6084
|
+
async insertOne(payload) {
|
|
6085
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
6086
|
+
const doc = { ...payload, _created_at: now, _updated_at: now };
|
|
6087
|
+
this._collection.validate(doc);
|
|
6088
|
+
return await this._collection.run(
|
|
6089
|
+
(collection) => {
|
|
6090
|
+
return collection.insertOne(doc);
|
|
2915
6091
|
},
|
|
2916
6092
|
this._token,
|
|
2917
6093
|
this._region
|
|
2918
6094
|
);
|
|
2919
6095
|
}
|
|
2920
6096
|
/**
|
|
2921
|
-
*
|
|
2922
|
-
* Each payload is stamped with `
|
|
6097
|
+
* Updates multiple documents in a single `updateMany` call.
|
|
6098
|
+
* Each payload is stamped with `_updated_at` and validated
|
|
2923
6099
|
* against the full collection schema before the bulk write is sent to the DB.
|
|
2924
6100
|
*
|
|
2925
|
-
* @param
|
|
2926
|
-
* @
|
|
6101
|
+
* @param payload - Document fields to apply to every matched document
|
|
6102
|
+
* @param filter - Query filter (e.g. `{ _id: 'abc' }` or `{ email: 'a@b.com' }`)
|
|
6103
|
+
* @param options - Optional `updateMany` options (e.g. `{ upsert: true }`)
|
|
6104
|
+
* @returns The raw `updateMany` result document (e.g. `acknowledged`, `modifiedCount`)
|
|
2927
6105
|
*/
|
|
2928
|
-
async
|
|
2929
|
-
const
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
return collection.insertMany(insertPayloads);
|
|
6106
|
+
async update(payload, filter = {}, options = {}) {
|
|
6107
|
+
const updatePayload = {
|
|
6108
|
+
...payload,
|
|
6109
|
+
_updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
6110
|
+
};
|
|
6111
|
+
this._validatePartial(updatePayload);
|
|
6112
|
+
return await this._collection.run(
|
|
6113
|
+
(collection) => {
|
|
6114
|
+
return collection.updateMany(filter, { $set: updatePayload }, options);
|
|
2938
6115
|
},
|
|
2939
6116
|
this._token,
|
|
2940
6117
|
this._region
|
|
2941
6118
|
);
|
|
2942
6119
|
}
|
|
2943
6120
|
/**
|
|
2944
|
-
*
|
|
2945
|
-
*
|
|
2946
|
-
*
|
|
6121
|
+
* Updates a single document in a single `updateOne` call.
|
|
6122
|
+
* The payload is stamped with `_updated_at` and validated
|
|
6123
|
+
* against the full collection schema before the write is sent to the DB.
|
|
2947
6124
|
*
|
|
2948
|
-
* @param payload -
|
|
2949
|
-
* @param filter - Query
|
|
6125
|
+
* @param payload - Document payload to update
|
|
6126
|
+
* @param filter - Query filter (e.g. `{ _id: 'abc' }` or `{ email: 'a@b.com' }`)
|
|
6127
|
+
* @param options - Optional `updateOne` options (e.g. `{ upsert: true }`)
|
|
6128
|
+
* @returns The raw `updateOne` result document (e.g. `acknowledged`, `modifiedCount`)
|
|
2950
6129
|
*/
|
|
2951
|
-
async
|
|
2952
|
-
const updatePayload = {
|
|
6130
|
+
async updateOne(payload, filter = {}, options = {}) {
|
|
6131
|
+
const updatePayload = {
|
|
6132
|
+
...payload,
|
|
6133
|
+
_updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
6134
|
+
};
|
|
2953
6135
|
this._validatePartial(updatePayload);
|
|
2954
|
-
await this._collection.run(
|
|
2955
|
-
|
|
2956
|
-
|
|
6136
|
+
return await this._collection.run(
|
|
6137
|
+
(collection) => {
|
|
6138
|
+
return collection.updateOne(filter, { $set: updatePayload }, options);
|
|
2957
6139
|
},
|
|
2958
6140
|
this._token,
|
|
2959
6141
|
this._region
|
|
2960
6142
|
);
|
|
2961
6143
|
}
|
|
2962
6144
|
/**
|
|
2963
|
-
*
|
|
2964
|
-
* Passing no filter (or an empty object) deletes every document in the collection.
|
|
6145
|
+
* Inserts or updates a document.
|
|
2965
6146
|
*
|
|
2966
|
-
* @param
|
|
6147
|
+
* @param payload - Document payload to insert or update
|
|
6148
|
+
* @param id - The `_id` of the document to update (string or ObjectId)
|
|
6149
|
+
* @returns The raw `insertOne` or `updateOne` result document (e.g. `acknowledged`, `insertedId`, `modifiedCount`)
|
|
2967
6150
|
*/
|
|
2968
|
-
async
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
6151
|
+
async save(payload = {}, id = "") {
|
|
6152
|
+
if (id) {
|
|
6153
|
+
return await this.updateOne(payload, { _id: new ObjectId(String(id)) }, { upsert: true });
|
|
6154
|
+
}
|
|
6155
|
+
return await this.insertOne(payload);
|
|
6156
|
+
}
|
|
6157
|
+
/**
|
|
6158
|
+
* Returns `true` when a document with the given `_id` exists in the collection.
|
|
6159
|
+
* Uses `countDocuments` rather than fetching the full document for efficiency.
|
|
6160
|
+
*
|
|
6161
|
+
* @param id - The `_id` to check
|
|
6162
|
+
* @returns `true` if the document exists, `false` otherwise
|
|
6163
|
+
*/
|
|
6164
|
+
async isIdExists(id) {
|
|
6165
|
+
if (!id) return false;
|
|
6166
|
+
return await this.exists({ _id: new ObjectId(String(id)) });
|
|
6167
|
+
}
|
|
6168
|
+
/**
|
|
6169
|
+
* Returns `true` when a document matching `filter` exists in the collection.
|
|
6170
|
+
* Uses `countDocuments` rather than fetching the full document for efficiency.
|
|
6171
|
+
*
|
|
6172
|
+
* @param filter - Query filter (e.g. `{ _id: 'abc' }` or `{ email: 'a@b.com' }`)
|
|
6173
|
+
* @param options - Optional `countDocuments` options (e.g. `{ limit: 10 }`)
|
|
6174
|
+
* @returns `true` if the document exists, `false` otherwise
|
|
6175
|
+
*/
|
|
6176
|
+
async exists(filter = {}, options = {}) {
|
|
6177
|
+
const n = await this.count(filter, options);
|
|
6178
|
+
return n > 0;
|
|
6179
|
+
}
|
|
6180
|
+
/**
|
|
6181
|
+
* Returns the number of documents in the collection matching `filter`.
|
|
6182
|
+
* Passing no filter (or an empty object) counts every document.
|
|
6183
|
+
*
|
|
6184
|
+
* @param filter - Query filter (e.g. `{ _id: 'abc' }` or `{ email: 'a@b.com' }`)
|
|
6185
|
+
* @param options - Optional `countDocuments` options (e.g. `{ limit: 10 }`)
|
|
6186
|
+
* @returns The number of documents matching the filter
|
|
6187
|
+
*/
|
|
6188
|
+
async count(filter = {}, options = {}) {
|
|
6189
|
+
return await this._collection.run(
|
|
6190
|
+
(collection) => {
|
|
6191
|
+
return collection.countDocuments(filter, options);
|
|
2972
6192
|
},
|
|
2973
6193
|
this._token,
|
|
2974
6194
|
this._region
|
|
@@ -2979,7 +6199,7 @@ var _AbdbRepository = class _AbdbRepository {
|
|
|
2979
6199
|
* Required columns that are absent from the payload are intentionally skipped —
|
|
2980
6200
|
* they already exist in the stored document and are not being changed.
|
|
2981
6201
|
*
|
|
2982
|
-
* Used by
|
|
6202
|
+
* Used by {@link update}, {@link updateOne}, and the update path of {@link save}.
|
|
2983
6203
|
*/
|
|
2984
6204
|
_validatePartial(record) {
|
|
2985
6205
|
for (const col of this._collection.getColumns()) {
|