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