@catbee/utils 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.cjs +653 -4
- package/build/index.d.ts +473 -1
- package/build/index.mjs +626 -5
- package/package.json +1 -1
package/build/index.cjs
CHANGED
|
@@ -11,6 +11,7 @@ var expressRateLimit = require('express-rate-limit');
|
|
|
11
11
|
var os = require('os');
|
|
12
12
|
var fsp = require('fs/promises');
|
|
13
13
|
var promises = require('stream/promises');
|
|
14
|
+
var stream = require('stream');
|
|
14
15
|
var url = require('url');
|
|
15
16
|
var express = require('express');
|
|
16
17
|
var https = require('https');
|
|
@@ -2504,6 +2505,234 @@ function verifySignedToken(token, secret) {
|
|
|
2504
2505
|
}
|
|
2505
2506
|
__name(verifySignedToken, "verifySignedToken");
|
|
2506
2507
|
|
|
2508
|
+
// src/utils/date.utils.ts
|
|
2509
|
+
function formatDate(date, options = {}) {
|
|
2510
|
+
const { format = "yyyy-MM-dd", locale, timeZone } = options;
|
|
2511
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
2512
|
+
if (format === "yyyy-MM-dd") {
|
|
2513
|
+
const yyyy = d.getFullYear();
|
|
2514
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
2515
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
2516
|
+
return `${yyyy}-${mm}-${dd}`;
|
|
2517
|
+
}
|
|
2518
|
+
if (format === "yyyy-MM-dd HH:mm:ss") {
|
|
2519
|
+
const yyyy = d.getFullYear();
|
|
2520
|
+
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
2521
|
+
const dd = String(d.getDate()).padStart(2, "0");
|
|
2522
|
+
const HH = String(d.getHours()).padStart(2, "0");
|
|
2523
|
+
const MM = String(d.getMinutes()).padStart(2, "0");
|
|
2524
|
+
const SS = String(d.getSeconds()).padStart(2, "0");
|
|
2525
|
+
return `${yyyy}-${mm}-${dd} ${HH}:${MM}:${SS}`;
|
|
2526
|
+
}
|
|
2527
|
+
if (format === "relative") {
|
|
2528
|
+
return formatRelativeTime(date);
|
|
2529
|
+
}
|
|
2530
|
+
return new Intl.DateTimeFormat(locale, {
|
|
2531
|
+
timeZone,
|
|
2532
|
+
year: format.includes("yyyy") || format.includes("y") ? "numeric" : void 0,
|
|
2533
|
+
month: format.includes("MM") || format.includes("M") ? "numeric" : void 0,
|
|
2534
|
+
day: format.includes("dd") || format.includes("d") ? "numeric" : void 0,
|
|
2535
|
+
hour: format.includes("HH") || format.includes("H") ? "numeric" : void 0,
|
|
2536
|
+
minute: format.includes("mm") || format.includes("m") ? "numeric" : void 0,
|
|
2537
|
+
second: format.includes("ss") || format.includes("s") ? "numeric" : void 0,
|
|
2538
|
+
hour12: !format.includes("HH") && !format.includes("H")
|
|
2539
|
+
}).format(d);
|
|
2540
|
+
}
|
|
2541
|
+
__name(formatDate, "formatDate");
|
|
2542
|
+
function formatRelativeTime(date, now = /* @__PURE__ */ new Date(), locale) {
|
|
2543
|
+
const d1 = date instanceof Date ? date : new Date(date);
|
|
2544
|
+
const d2 = now instanceof Date ? now : new Date(now);
|
|
2545
|
+
const diffMs = d1.getTime() - d2.getTime();
|
|
2546
|
+
const diffSecs = Math.round(diffMs / 1e3);
|
|
2547
|
+
const diffMins = Math.round(diffSecs / 60);
|
|
2548
|
+
const diffHours = Math.round(diffMins / 60);
|
|
2549
|
+
const diffDays = Math.round(diffHours / 24);
|
|
2550
|
+
const diffMonths = Math.round(diffDays / 30);
|
|
2551
|
+
const diffYears = Math.round(diffDays / 365);
|
|
2552
|
+
const formatter = new Intl.RelativeTimeFormat(locale, {
|
|
2553
|
+
numeric: "auto"
|
|
2554
|
+
});
|
|
2555
|
+
if (Math.abs(diffSecs) < 60) return formatter.format(diffSecs, "second");
|
|
2556
|
+
if (Math.abs(diffMins) < 60) return formatter.format(diffMins, "minute");
|
|
2557
|
+
if (Math.abs(diffHours) < 24) return formatter.format(diffHours, "hour");
|
|
2558
|
+
if (Math.abs(diffDays) < 30) return formatter.format(diffDays, "day");
|
|
2559
|
+
if (Math.abs(diffMonths) < 12) return formatter.format(diffMonths, "month");
|
|
2560
|
+
return formatter.format(diffYears, "year");
|
|
2561
|
+
}
|
|
2562
|
+
__name(formatRelativeTime, "formatRelativeTime");
|
|
2563
|
+
function parseDate(input, fallback) {
|
|
2564
|
+
if (typeof input === "number") {
|
|
2565
|
+
return new Date(input);
|
|
2566
|
+
}
|
|
2567
|
+
try {
|
|
2568
|
+
const date = new Date(input);
|
|
2569
|
+
if (isNaN(date.getTime())) {
|
|
2570
|
+
return fallback || null;
|
|
2571
|
+
}
|
|
2572
|
+
return date;
|
|
2573
|
+
} catch {
|
|
2574
|
+
return fallback || null;
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
__name(parseDate, "parseDate");
|
|
2578
|
+
function dateDiff(date1, date2 = /* @__PURE__ */ new Date(), unit = "days") {
|
|
2579
|
+
const d1 = date1 instanceof Date ? date1 : new Date(date1);
|
|
2580
|
+
const d2 = date2 instanceof Date ? date2 : new Date(date2);
|
|
2581
|
+
const diffMs = d1.getTime() - d2.getTime();
|
|
2582
|
+
switch (unit) {
|
|
2583
|
+
case "milliseconds":
|
|
2584
|
+
return diffMs;
|
|
2585
|
+
case "seconds":
|
|
2586
|
+
return diffMs / 1e3;
|
|
2587
|
+
case "minutes":
|
|
2588
|
+
return diffMs / (1e3 * 60);
|
|
2589
|
+
case "hours":
|
|
2590
|
+
return diffMs / (1e3 * 60 * 60);
|
|
2591
|
+
case "days":
|
|
2592
|
+
return diffMs / (1e3 * 60 * 60 * 24);
|
|
2593
|
+
case "months":
|
|
2594
|
+
return (d1.getFullYear() - d2.getFullYear()) * 12 + d1.getMonth() - d2.getMonth();
|
|
2595
|
+
case "years":
|
|
2596
|
+
return d1.getFullYear() - d2.getFullYear();
|
|
2597
|
+
default:
|
|
2598
|
+
throw new Error(`Unsupported unit: ${unit}`);
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
__name(dateDiff, "dateDiff");
|
|
2602
|
+
function addToDate(date, amount, unit) {
|
|
2603
|
+
const d = date instanceof Date ? new Date(date) : new Date(date);
|
|
2604
|
+
switch (unit) {
|
|
2605
|
+
case "milliseconds":
|
|
2606
|
+
d.setMilliseconds(d.getMilliseconds() + amount);
|
|
2607
|
+
break;
|
|
2608
|
+
case "seconds":
|
|
2609
|
+
d.setSeconds(d.getSeconds() + amount);
|
|
2610
|
+
break;
|
|
2611
|
+
case "minutes":
|
|
2612
|
+
d.setMinutes(d.getMinutes() + amount);
|
|
2613
|
+
break;
|
|
2614
|
+
case "hours":
|
|
2615
|
+
d.setHours(d.getHours() + amount);
|
|
2616
|
+
break;
|
|
2617
|
+
case "days":
|
|
2618
|
+
d.setDate(d.getDate() + amount);
|
|
2619
|
+
break;
|
|
2620
|
+
case "months": {
|
|
2621
|
+
const origDate = d.getDate();
|
|
2622
|
+
const origMonth = d.getMonth();
|
|
2623
|
+
d.setDate(1);
|
|
2624
|
+
d.setMonth(origMonth + amount);
|
|
2625
|
+
const lastDay = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
|
|
2626
|
+
d.setDate(Math.min(origDate, lastDay));
|
|
2627
|
+
break;
|
|
2628
|
+
}
|
|
2629
|
+
case "years":
|
|
2630
|
+
d.setFullYear(d.getFullYear() + amount);
|
|
2631
|
+
break;
|
|
2632
|
+
default:
|
|
2633
|
+
throw new Error(`Unsupported unit: ${unit}`);
|
|
2634
|
+
}
|
|
2635
|
+
return d;
|
|
2636
|
+
}
|
|
2637
|
+
__name(addToDate, "addToDate");
|
|
2638
|
+
function startOf(date, unit) {
|
|
2639
|
+
const d = date instanceof Date ? new Date(date) : new Date(date);
|
|
2640
|
+
switch (unit) {
|
|
2641
|
+
case "second":
|
|
2642
|
+
d.setMilliseconds(0);
|
|
2643
|
+
break;
|
|
2644
|
+
case "minute":
|
|
2645
|
+
d.setSeconds(0, 0);
|
|
2646
|
+
break;
|
|
2647
|
+
case "hour":
|
|
2648
|
+
d.setMinutes(0, 0, 0);
|
|
2649
|
+
break;
|
|
2650
|
+
case "day":
|
|
2651
|
+
d.setHours(0, 0, 0, 0);
|
|
2652
|
+
break;
|
|
2653
|
+
case "week":
|
|
2654
|
+
d.setHours(0, 0, 0, 0);
|
|
2655
|
+
d.setDate(d.getDate() - d.getDay());
|
|
2656
|
+
break;
|
|
2657
|
+
case "month":
|
|
2658
|
+
d.setDate(1);
|
|
2659
|
+
d.setHours(0, 0, 0, 0);
|
|
2660
|
+
break;
|
|
2661
|
+
case "quarter":
|
|
2662
|
+
d.setMonth(Math.floor(d.getMonth() / 3) * 3, 1);
|
|
2663
|
+
d.setHours(0, 0, 0, 0);
|
|
2664
|
+
break;
|
|
2665
|
+
case "year":
|
|
2666
|
+
d.setMonth(0, 1);
|
|
2667
|
+
d.setHours(0, 0, 0, 0);
|
|
2668
|
+
break;
|
|
2669
|
+
default:
|
|
2670
|
+
throw new Error(`Unsupported unit: ${unit}`);
|
|
2671
|
+
}
|
|
2672
|
+
return d;
|
|
2673
|
+
}
|
|
2674
|
+
__name(startOf, "startOf");
|
|
2675
|
+
function endOf(date, unit) {
|
|
2676
|
+
const d = date instanceof Date ? new Date(date) : new Date(date);
|
|
2677
|
+
switch (unit) {
|
|
2678
|
+
case "second":
|
|
2679
|
+
d.setMilliseconds(999);
|
|
2680
|
+
break;
|
|
2681
|
+
case "minute":
|
|
2682
|
+
d.setSeconds(59, 999);
|
|
2683
|
+
break;
|
|
2684
|
+
case "hour":
|
|
2685
|
+
d.setMinutes(59, 59, 999);
|
|
2686
|
+
break;
|
|
2687
|
+
case "day":
|
|
2688
|
+
d.setHours(23, 59, 59, 999);
|
|
2689
|
+
break;
|
|
2690
|
+
case "week":
|
|
2691
|
+
d.setDate(d.getDate() - d.getDay() + 6);
|
|
2692
|
+
d.setHours(23, 59, 59, 999);
|
|
2693
|
+
break;
|
|
2694
|
+
case "month":
|
|
2695
|
+
d.setMonth(d.getMonth() + 1, 0);
|
|
2696
|
+
d.setHours(23, 59, 59, 999);
|
|
2697
|
+
break;
|
|
2698
|
+
case "quarter":
|
|
2699
|
+
d.setMonth(Math.floor(d.getMonth() / 3) * 3 + 3, 0);
|
|
2700
|
+
d.setHours(23, 59, 59, 999);
|
|
2701
|
+
break;
|
|
2702
|
+
case "year":
|
|
2703
|
+
d.setMonth(11, 31);
|
|
2704
|
+
d.setHours(23, 59, 59, 999);
|
|
2705
|
+
break;
|
|
2706
|
+
default:
|
|
2707
|
+
throw new Error(`Unsupported unit: ${unit}`);
|
|
2708
|
+
}
|
|
2709
|
+
return d;
|
|
2710
|
+
}
|
|
2711
|
+
__name(endOf, "endOf");
|
|
2712
|
+
function isBetween(date, start, end, inclusive = true) {
|
|
2713
|
+
const d = date instanceof Date ? date.getTime() : date;
|
|
2714
|
+
const s = start instanceof Date ? start.getTime() : start;
|
|
2715
|
+
const e = end instanceof Date ? end.getTime() : end;
|
|
2716
|
+
return inclusive ? d >= s && d <= e : d > s && d < e;
|
|
2717
|
+
}
|
|
2718
|
+
__name(isBetween, "isBetween");
|
|
2719
|
+
function isLeapYear(year) {
|
|
2720
|
+
const y = year instanceof Date ? year.getFullYear() : year;
|
|
2721
|
+
return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0;
|
|
2722
|
+
}
|
|
2723
|
+
__name(isLeapYear, "isLeapYear");
|
|
2724
|
+
function daysInMonth(year, month) {
|
|
2725
|
+
if (year instanceof Date) {
|
|
2726
|
+
month = year.getMonth();
|
|
2727
|
+
year = year.getFullYear();
|
|
2728
|
+
}
|
|
2729
|
+
if (month === void 0) {
|
|
2730
|
+
throw new Error("Month is required when year is a number");
|
|
2731
|
+
}
|
|
2732
|
+
return new Date(year, month + 1, 0).getDate();
|
|
2733
|
+
}
|
|
2734
|
+
__name(daysInMonth, "daysInMonth");
|
|
2735
|
+
|
|
2507
2736
|
// src/utils/response.utils.ts
|
|
2508
2737
|
var SuccessResponse = class {
|
|
2509
2738
|
static {
|
|
@@ -3527,7 +3756,7 @@ function registerControllers(router, controllers) {
|
|
|
3527
3756
|
diContainer.register(ControllerClass);
|
|
3528
3757
|
}
|
|
3529
3758
|
const instance = diContainer.get(ControllerClass);
|
|
3530
|
-
|
|
3759
|
+
let basePath = Reflect.getMetadata("basePath", ControllerClass) || "";
|
|
3531
3760
|
const routes = Reflect.getMetadata(ROUTES_KEY, ControllerClass) || [];
|
|
3532
3761
|
const controllerRateLimit = Reflect.getMetadata(RATE_LIMIT_KEY, ControllerClass);
|
|
3533
3762
|
const controllerCache = Reflect.getMetadata(CACHE_KEY, ControllerClass);
|
|
@@ -3540,6 +3769,9 @@ function registerControllers(router, controllers) {
|
|
|
3540
3769
|
const controllerBeforeHooks = Reflect.getMetadata(BEFORE_KEY, ControllerClass) || [];
|
|
3541
3770
|
const controllerAfterHooks = Reflect.getMetadata(AFTER_KEY, ControllerClass) || [];
|
|
3542
3771
|
const controllerContentType = Reflect.getMetadata(CONTENT_TYPE_KEY, ControllerClass);
|
|
3772
|
+
if (controllerVersion?.options?.addPrefix) {
|
|
3773
|
+
basePath = `/${controllerVersion.version}${basePath || ""}`;
|
|
3774
|
+
}
|
|
3543
3775
|
routes.forEach(({ path: path3, method, handlerName }) => {
|
|
3544
3776
|
const methodMiddlewares = Reflect.getMetadata(MIDDLEWARE_KEY, instance, handlerName) || [];
|
|
3545
3777
|
const params = Reflect.getMetadata(PARAMS_KEY, instance, handlerName) || [];
|
|
@@ -3569,7 +3801,8 @@ function registerControllers(router, controllers) {
|
|
|
3569
3801
|
const roles = Reflect.getMetadata(ROLES_KEY, instance, handlerName) || controllerRoles || [];
|
|
3570
3802
|
const cache = Reflect.getMetadata(CACHE_KEY, instance, handlerName) || controllerCache;
|
|
3571
3803
|
const rateLimitOptions = Reflect.getMetadata(RATE_LIMIT_KEY, instance, handlerName) || controllerRateLimit;
|
|
3572
|
-
const
|
|
3804
|
+
const methodVersion = Reflect.getMetadata(VERSION_KEY, instance, handlerName);
|
|
3805
|
+
const version = methodVersion || controllerVersion;
|
|
3573
3806
|
const timeout2 = Reflect.getMetadata(TIMEOUT_KEY, instance, handlerName) || controllerTimeout;
|
|
3574
3807
|
const logConfig = Reflect.getMetadata(LOG_KEY, instance, handlerName) || controllerLogConfig;
|
|
3575
3808
|
let rateLimiter = null;
|
|
@@ -3583,8 +3816,8 @@ function registerControllers(router, controllers) {
|
|
|
3583
3816
|
}
|
|
3584
3817
|
}
|
|
3585
3818
|
let finalPath = path3;
|
|
3586
|
-
if (
|
|
3587
|
-
finalPath = `/${
|
|
3819
|
+
if (methodVersion?.options?.addPrefix && methodVersion.version !== controllerVersion?.version) {
|
|
3820
|
+
finalPath = `/${methodVersion.version}${path3}`;
|
|
3588
3821
|
}
|
|
3589
3822
|
const handler = /* @__PURE__ */ __name(async (req, res, next) => {
|
|
3590
3823
|
req["startTime"] = Date.now();
|
|
@@ -4573,6 +4806,202 @@ function healthCheck(options) {
|
|
|
4573
4806
|
}
|
|
4574
4807
|
__name(healthCheck, "healthCheck");
|
|
4575
4808
|
|
|
4809
|
+
// src/utils/performance.utils.ts
|
|
4810
|
+
function timeSync(fn, options = {}) {
|
|
4811
|
+
const { label = fn.name || "anonymous function", log = false, logLevel = "debug" } = options;
|
|
4812
|
+
const startTime = performance.now();
|
|
4813
|
+
const result = fn();
|
|
4814
|
+
const endTime = performance.now();
|
|
4815
|
+
const durationMs = endTime - startTime;
|
|
4816
|
+
const durationSec = durationMs / 1e3;
|
|
4817
|
+
const timing = {
|
|
4818
|
+
durationMs,
|
|
4819
|
+
durationSec,
|
|
4820
|
+
startTime,
|
|
4821
|
+
endTime,
|
|
4822
|
+
label
|
|
4823
|
+
};
|
|
4824
|
+
if (log) {
|
|
4825
|
+
const logger2 = getLogger();
|
|
4826
|
+
const message = `${label} completed in ${durationMs.toFixed(2)}ms`;
|
|
4827
|
+
switch (logLevel) {
|
|
4828
|
+
case "trace":
|
|
4829
|
+
logger2.trace({
|
|
4830
|
+
timing
|
|
4831
|
+
}, message);
|
|
4832
|
+
break;
|
|
4833
|
+
case "debug":
|
|
4834
|
+
logger2.debug({
|
|
4835
|
+
timing
|
|
4836
|
+
}, message);
|
|
4837
|
+
break;
|
|
4838
|
+
case "info":
|
|
4839
|
+
logger2.info({
|
|
4840
|
+
timing
|
|
4841
|
+
}, message);
|
|
4842
|
+
break;
|
|
4843
|
+
case "warn":
|
|
4844
|
+
logger2.warn({
|
|
4845
|
+
timing
|
|
4846
|
+
}, message);
|
|
4847
|
+
break;
|
|
4848
|
+
case "error":
|
|
4849
|
+
logger2.error({
|
|
4850
|
+
timing
|
|
4851
|
+
}, message);
|
|
4852
|
+
break;
|
|
4853
|
+
default:
|
|
4854
|
+
logger2.debug({
|
|
4855
|
+
timing
|
|
4856
|
+
}, message);
|
|
4857
|
+
break;
|
|
4858
|
+
}
|
|
4859
|
+
}
|
|
4860
|
+
return {
|
|
4861
|
+
result,
|
|
4862
|
+
timing
|
|
4863
|
+
};
|
|
4864
|
+
}
|
|
4865
|
+
__name(timeSync, "timeSync");
|
|
4866
|
+
async function timeAsync(fn, options = {}) {
|
|
4867
|
+
const { label = fn.name || "anonymous async function", log = false, logLevel = "debug" } = options;
|
|
4868
|
+
const startTime = performance.now();
|
|
4869
|
+
const result = await fn();
|
|
4870
|
+
const endTime = performance.now();
|
|
4871
|
+
const durationMs = endTime - startTime;
|
|
4872
|
+
const durationSec = durationMs / 1e3;
|
|
4873
|
+
const timing = {
|
|
4874
|
+
durationMs,
|
|
4875
|
+
durationSec,
|
|
4876
|
+
startTime,
|
|
4877
|
+
endTime,
|
|
4878
|
+
label
|
|
4879
|
+
};
|
|
4880
|
+
if (log) {
|
|
4881
|
+
const logger2 = getLogger();
|
|
4882
|
+
const message = `${label} completed in ${durationMs.toFixed(2)}ms`;
|
|
4883
|
+
switch (logLevel) {
|
|
4884
|
+
case "trace":
|
|
4885
|
+
logger2.trace({
|
|
4886
|
+
timing
|
|
4887
|
+
}, message);
|
|
4888
|
+
break;
|
|
4889
|
+
case "debug":
|
|
4890
|
+
logger2.debug({
|
|
4891
|
+
timing
|
|
4892
|
+
}, message);
|
|
4893
|
+
break;
|
|
4894
|
+
case "info":
|
|
4895
|
+
logger2.info({
|
|
4896
|
+
timing
|
|
4897
|
+
}, message);
|
|
4898
|
+
break;
|
|
4899
|
+
case "warn":
|
|
4900
|
+
logger2.warn({
|
|
4901
|
+
timing
|
|
4902
|
+
}, message);
|
|
4903
|
+
break;
|
|
4904
|
+
case "error":
|
|
4905
|
+
logger2.error({
|
|
4906
|
+
timing
|
|
4907
|
+
}, message);
|
|
4908
|
+
break;
|
|
4909
|
+
default:
|
|
4910
|
+
logger2.debug({
|
|
4911
|
+
timing
|
|
4912
|
+
}, message);
|
|
4913
|
+
break;
|
|
4914
|
+
}
|
|
4915
|
+
}
|
|
4916
|
+
return {
|
|
4917
|
+
result,
|
|
4918
|
+
timing
|
|
4919
|
+
};
|
|
4920
|
+
}
|
|
4921
|
+
__name(timeAsync, "timeAsync");
|
|
4922
|
+
function timed(options = {}) {
|
|
4923
|
+
return function(target, propertyKeyOrContext, descriptor) {
|
|
4924
|
+
let propertyKey;
|
|
4925
|
+
let actualDescriptor;
|
|
4926
|
+
if (typeof propertyKeyOrContext === "object" && propertyKeyOrContext !== null && "name" in propertyKeyOrContext) {
|
|
4927
|
+
propertyKey = propertyKeyOrContext.name;
|
|
4928
|
+
actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
4929
|
+
} else {
|
|
4930
|
+
propertyKey = propertyKeyOrContext;
|
|
4931
|
+
actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
4932
|
+
}
|
|
4933
|
+
const originalMethod = actualDescriptor.value;
|
|
4934
|
+
actualDescriptor.value = function(...args) {
|
|
4935
|
+
const methodOptions = {
|
|
4936
|
+
...options,
|
|
4937
|
+
label: options.label || `${target.constructor.name}.${String(propertyKey)}`
|
|
4938
|
+
};
|
|
4939
|
+
if (originalMethod.constructor.name === "AsyncFunction") {
|
|
4940
|
+
return timeAsync(() => originalMethod.apply(this, args), methodOptions).then(({ result }) => result);
|
|
4941
|
+
} else {
|
|
4942
|
+
return timeSync(() => originalMethod.apply(this, args), methodOptions).result;
|
|
4943
|
+
}
|
|
4944
|
+
};
|
|
4945
|
+
if (descriptor) {
|
|
4946
|
+
descriptor.value = actualDescriptor.value;
|
|
4947
|
+
} else {
|
|
4948
|
+
Object.defineProperty(target, propertyKey, actualDescriptor);
|
|
4949
|
+
}
|
|
4950
|
+
};
|
|
4951
|
+
}
|
|
4952
|
+
__name(timed, "timed");
|
|
4953
|
+
function memoize(fn, options = {}) {
|
|
4954
|
+
const { ttl, maxSize, autoCleanupMs, cacheKey = /* @__PURE__ */ __name((...args) => JSON.stringify(args), "cacheKey") } = options;
|
|
4955
|
+
const cache = new TTLCache({
|
|
4956
|
+
ttlMs: ttl,
|
|
4957
|
+
maxSize,
|
|
4958
|
+
autoCleanupMs
|
|
4959
|
+
});
|
|
4960
|
+
return function(...args) {
|
|
4961
|
+
const key = cacheKey(...args);
|
|
4962
|
+
const cachedValue = cache.get(key);
|
|
4963
|
+
if (cachedValue !== void 0) {
|
|
4964
|
+
return cachedValue;
|
|
4965
|
+
}
|
|
4966
|
+
const value = fn(...args);
|
|
4967
|
+
cache.set(key, value);
|
|
4968
|
+
return value;
|
|
4969
|
+
};
|
|
4970
|
+
}
|
|
4971
|
+
__name(memoize, "memoize");
|
|
4972
|
+
function trackMemoryUsage(fn, options = {}) {
|
|
4973
|
+
const { log = false, label = fn.name || "anonymous function" } = options;
|
|
4974
|
+
if (global.gc) {
|
|
4975
|
+
global.gc();
|
|
4976
|
+
}
|
|
4977
|
+
const beforeMemory = process.memoryUsage();
|
|
4978
|
+
const result = fn();
|
|
4979
|
+
const afterMemory = process.memoryUsage();
|
|
4980
|
+
const diff = {};
|
|
4981
|
+
for (const key in afterMemory) {
|
|
4982
|
+
if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {
|
|
4983
|
+
diff[key] = afterMemory[key] - beforeMemory[key];
|
|
4984
|
+
}
|
|
4985
|
+
}
|
|
4986
|
+
if (log) {
|
|
4987
|
+
getLogger().info({
|
|
4988
|
+
label,
|
|
4989
|
+
memoryBefore: beforeMemory,
|
|
4990
|
+
memoryAfter: afterMemory,
|
|
4991
|
+
memoryDiff: diff
|
|
4992
|
+
}, `Memory usage for ${label}`);
|
|
4993
|
+
}
|
|
4994
|
+
return {
|
|
4995
|
+
result,
|
|
4996
|
+
memoryUsage: {
|
|
4997
|
+
before: beforeMemory,
|
|
4998
|
+
after: afterMemory,
|
|
4999
|
+
diff
|
|
5000
|
+
}
|
|
5001
|
+
};
|
|
5002
|
+
}
|
|
5003
|
+
__name(trackMemoryUsage, "trackMemoryUsage");
|
|
5004
|
+
|
|
4576
5005
|
// src/utils/request.utils.ts
|
|
4577
5006
|
var getPaginationParams = /* @__PURE__ */ __name((req) => {
|
|
4578
5007
|
const query = req.query;
|
|
@@ -4731,6 +5160,120 @@ function extractFilterParams(query, allowedFilters) {
|
|
|
4731
5160
|
return filters;
|
|
4732
5161
|
}
|
|
4733
5162
|
__name(extractFilterParams, "extractFilterParams");
|
|
5163
|
+
function bufferToStream(data) {
|
|
5164
|
+
const readable = new stream.Readable();
|
|
5165
|
+
readable.push(data);
|
|
5166
|
+
readable.push(null);
|
|
5167
|
+
return readable;
|
|
5168
|
+
}
|
|
5169
|
+
__name(bufferToStream, "bufferToStream");
|
|
5170
|
+
async function streamToBuffer(stream) {
|
|
5171
|
+
return new Promise((resolve2, reject) => {
|
|
5172
|
+
const chunks = [];
|
|
5173
|
+
stream.on("data", (chunk2) => chunks.push(Buffer.isBuffer(chunk2) ? chunk2 : Buffer.from(chunk2)));
|
|
5174
|
+
stream.on("end", () => resolve2(Buffer.concat(chunks)));
|
|
5175
|
+
stream.on("error", reject);
|
|
5176
|
+
});
|
|
5177
|
+
}
|
|
5178
|
+
__name(streamToBuffer, "streamToBuffer");
|
|
5179
|
+
async function streamToString(stream, encoding = "utf8") {
|
|
5180
|
+
const buffer = await streamToBuffer(stream);
|
|
5181
|
+
return buffer.toString(encoding);
|
|
5182
|
+
}
|
|
5183
|
+
__name(streamToString, "streamToString");
|
|
5184
|
+
function createThrottleStream(bytesPerSecond) {
|
|
5185
|
+
let bytesSent = 0;
|
|
5186
|
+
let startTime = Date.now();
|
|
5187
|
+
return new stream.Transform({
|
|
5188
|
+
transform(chunk2, _encoding, callback) {
|
|
5189
|
+
const elapsedSeconds = (Date.now() - startTime) / 1e3;
|
|
5190
|
+
const targetBytes = bytesPerSecond * elapsedSeconds;
|
|
5191
|
+
const chunkSize = chunk2.length;
|
|
5192
|
+
if (bytesSent + chunkSize <= targetBytes) {
|
|
5193
|
+
bytesSent += chunkSize;
|
|
5194
|
+
callback(null, chunk2);
|
|
5195
|
+
return;
|
|
5196
|
+
}
|
|
5197
|
+
const delay = ((bytesSent + chunkSize) / bytesPerSecond - elapsedSeconds) * 1e3;
|
|
5198
|
+
setTimeout(() => {
|
|
5199
|
+
bytesSent += chunkSize;
|
|
5200
|
+
callback(null, chunk2);
|
|
5201
|
+
}, delay);
|
|
5202
|
+
}
|
|
5203
|
+
});
|
|
5204
|
+
}
|
|
5205
|
+
__name(createThrottleStream, "createThrottleStream");
|
|
5206
|
+
function createBatchStream(size, options = {}) {
|
|
5207
|
+
const objectMode = options.objectMode !== false;
|
|
5208
|
+
let batch = [];
|
|
5209
|
+
let buffers = [];
|
|
5210
|
+
let bufferSize = 0;
|
|
5211
|
+
return new stream.Transform({
|
|
5212
|
+
objectMode,
|
|
5213
|
+
transform(chunk2, _encoding, callback) {
|
|
5214
|
+
if (objectMode) {
|
|
5215
|
+
batch.push(chunk2);
|
|
5216
|
+
if (batch.length >= size) {
|
|
5217
|
+
this.push(batch);
|
|
5218
|
+
batch = [];
|
|
5219
|
+
}
|
|
5220
|
+
} else {
|
|
5221
|
+
const buffer = Buffer.isBuffer(chunk2) ? chunk2 : Buffer.from(chunk2);
|
|
5222
|
+
buffers.push(buffer);
|
|
5223
|
+
bufferSize += buffer.length;
|
|
5224
|
+
let combined = Buffer.concat(buffers, bufferSize);
|
|
5225
|
+
while (combined.length >= size) {
|
|
5226
|
+
this.push(combined.slice(0, size));
|
|
5227
|
+
combined = combined.slice(size);
|
|
5228
|
+
}
|
|
5229
|
+
buffers = combined.length > 0 ? [
|
|
5230
|
+
combined
|
|
5231
|
+
] : [];
|
|
5232
|
+
bufferSize = combined.length;
|
|
5233
|
+
}
|
|
5234
|
+
callback();
|
|
5235
|
+
},
|
|
5236
|
+
flush(callback) {
|
|
5237
|
+
if (objectMode && batch.length > 0) {
|
|
5238
|
+
this.push(batch);
|
|
5239
|
+
} else if (!objectMode && buffers.length > 0 && bufferSize > 0) {
|
|
5240
|
+
let combined = Buffer.concat(buffers, bufferSize);
|
|
5241
|
+
while (combined.length >= size) {
|
|
5242
|
+
this.push(combined.slice(0, size));
|
|
5243
|
+
combined = combined.slice(size);
|
|
5244
|
+
}
|
|
5245
|
+
if (combined.length > 0) {
|
|
5246
|
+
this.push(combined);
|
|
5247
|
+
}
|
|
5248
|
+
}
|
|
5249
|
+
callback();
|
|
5250
|
+
}
|
|
5251
|
+
});
|
|
5252
|
+
}
|
|
5253
|
+
__name(createBatchStream, "createBatchStream");
|
|
5254
|
+
function createLineStream(options = {}) {
|
|
5255
|
+
const { encoding = "utf8", includeNewlines = false } = options;
|
|
5256
|
+
let buffer = "";
|
|
5257
|
+
return new stream.Transform({
|
|
5258
|
+
objectMode: true,
|
|
5259
|
+
transform(chunk2, _encoding, callback) {
|
|
5260
|
+
const str = buffer + (Buffer.isBuffer(chunk2) ? chunk2.toString(encoding) : chunk2);
|
|
5261
|
+
const lines = str.split(/\r?\n/);
|
|
5262
|
+
buffer = lines.pop() || "";
|
|
5263
|
+
for (const line of lines) {
|
|
5264
|
+
this.push(includeNewlines ? line + "\n" : line);
|
|
5265
|
+
}
|
|
5266
|
+
callback();
|
|
5267
|
+
},
|
|
5268
|
+
flush(callback) {
|
|
5269
|
+
if (buffer) {
|
|
5270
|
+
this.push(buffer);
|
|
5271
|
+
}
|
|
5272
|
+
callback();
|
|
5273
|
+
}
|
|
5274
|
+
});
|
|
5275
|
+
}
|
|
5276
|
+
__name(createLineStream, "createLineStream");
|
|
4734
5277
|
|
|
4735
5278
|
// src/utils/string.utils.ts
|
|
4736
5279
|
function capitalize(str) {
|
|
@@ -4790,6 +5333,84 @@ function countOccurrences(str, substring, caseSensitive = true) {
|
|
|
4790
5333
|
return str.split(substring).length - 1;
|
|
4791
5334
|
}
|
|
4792
5335
|
__name(countOccurrences, "countOccurrences");
|
|
5336
|
+
|
|
5337
|
+
// src/utils/type.utils.ts
|
|
5338
|
+
function isPrimitiveType(value, type) {
|
|
5339
|
+
if (type === "array") {
|
|
5340
|
+
return Array.isArray(value);
|
|
5341
|
+
}
|
|
5342
|
+
if (type === "null") {
|
|
5343
|
+
return value === null;
|
|
5344
|
+
}
|
|
5345
|
+
if (type === "undefined") {
|
|
5346
|
+
return value === void 0;
|
|
5347
|
+
}
|
|
5348
|
+
if (type === "object") {
|
|
5349
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5350
|
+
}
|
|
5351
|
+
return typeof value === type;
|
|
5352
|
+
}
|
|
5353
|
+
__name(isPrimitiveType, "isPrimitiveType");
|
|
5354
|
+
function getTypeOf(value) {
|
|
5355
|
+
if (value === null) return "null";
|
|
5356
|
+
if (value === void 0) return "undefined";
|
|
5357
|
+
if (Array.isArray(value)) return "array";
|
|
5358
|
+
return typeof value;
|
|
5359
|
+
}
|
|
5360
|
+
__name(getTypeOf, "getTypeOf");
|
|
5361
|
+
function isArrayOf(value, itemTypeGuard) {
|
|
5362
|
+
if (!Array.isArray(value)) return false;
|
|
5363
|
+
return value.every((item) => itemTypeGuard(item));
|
|
5364
|
+
}
|
|
5365
|
+
__name(isArrayOf, "isArrayOf");
|
|
5366
|
+
function toStr(value, defaultValue = "") {
|
|
5367
|
+
if (value === null || value === void 0) return defaultValue;
|
|
5368
|
+
try {
|
|
5369
|
+
if (typeof value === "object") {
|
|
5370
|
+
return JSON.stringify(value);
|
|
5371
|
+
}
|
|
5372
|
+
return String(value);
|
|
5373
|
+
} catch {
|
|
5374
|
+
return defaultValue;
|
|
5375
|
+
}
|
|
5376
|
+
}
|
|
5377
|
+
__name(toStr, "toStr");
|
|
5378
|
+
function toNum(value, defaultValue = 0) {
|
|
5379
|
+
if (value === null || value === void 0) return defaultValue;
|
|
5380
|
+
if (typeof value === "number") return value;
|
|
5381
|
+
try {
|
|
5382
|
+
const num = Number(value);
|
|
5383
|
+
return isNaN(num) ? defaultValue : num;
|
|
5384
|
+
} catch {
|
|
5385
|
+
return defaultValue;
|
|
5386
|
+
}
|
|
5387
|
+
}
|
|
5388
|
+
__name(toNum, "toNum");
|
|
5389
|
+
function toBool(value, defaultValue = false) {
|
|
5390
|
+
if (value === null || value === void 0) return defaultValue;
|
|
5391
|
+
if (typeof value === "boolean") return value;
|
|
5392
|
+
if (typeof value === "string") {
|
|
5393
|
+
const lowercased = value.toLowerCase();
|
|
5394
|
+
if (lowercased === "true" || lowercased === "yes" || lowercased === "y" || lowercased === "1") {
|
|
5395
|
+
return true;
|
|
5396
|
+
}
|
|
5397
|
+
if (lowercased === "false" || lowercased === "no" || lowercased === "n" || lowercased === "0") {
|
|
5398
|
+
return false;
|
|
5399
|
+
}
|
|
5400
|
+
}
|
|
5401
|
+
if (typeof value === "number") {
|
|
5402
|
+
return value !== 0;
|
|
5403
|
+
}
|
|
5404
|
+
return defaultValue;
|
|
5405
|
+
}
|
|
5406
|
+
__name(toBool, "toBool");
|
|
5407
|
+
function ensureType(value, expectedType, defaultValue) {
|
|
5408
|
+
if (getTypeOf(value) === expectedType) {
|
|
5409
|
+
return value;
|
|
5410
|
+
}
|
|
5411
|
+
return defaultValue;
|
|
5412
|
+
}
|
|
5413
|
+
__name(ensureType, "ensureType");
|
|
4793
5414
|
function appendQueryParams(url$1, params) {
|
|
4794
5415
|
const urlObj = new url.URL(url$1);
|
|
4795
5416
|
for (const [key, value] of Object.entries(params)) {
|
|
@@ -6720,8 +7341,10 @@ exports._globalThis = _globalThis;
|
|
|
6720
7341
|
exports.abortable = abortable;
|
|
6721
7342
|
exports.addRedactFields = addRedactFields;
|
|
6722
7343
|
exports.addSensitiveFields = addSensitiveFields;
|
|
7344
|
+
exports.addToDate = addToDate;
|
|
6723
7345
|
exports.appendQueryParams = appendQueryParams;
|
|
6724
7346
|
exports.appendTextFile = appendTextFile;
|
|
7347
|
+
exports.bufferToStream = bufferToStream;
|
|
6725
7348
|
exports.capitalize = capitalize;
|
|
6726
7349
|
exports.chunk = chunk;
|
|
6727
7350
|
exports.circuitBreaker = circuitBreaker;
|
|
@@ -6730,12 +7353,14 @@ exports.copyDir = copyDir;
|
|
|
6730
7353
|
exports.copyFile = copyFile;
|
|
6731
7354
|
exports.countBy = countBy;
|
|
6732
7355
|
exports.countOccurrences = countOccurrences;
|
|
7356
|
+
exports.createBatchStream = createBatchStream;
|
|
6733
7357
|
exports.createChildLogger = createChildLogger;
|
|
6734
7358
|
exports.createDeferred = createDeferred;
|
|
6735
7359
|
exports.createDirectory = createDirectory;
|
|
6736
7360
|
exports.createErrorResponse = createErrorResponse;
|
|
6737
7361
|
exports.createFinalErrorResponse = createFinalErrorResponse;
|
|
6738
7362
|
exports.createHttpError = createHttpError;
|
|
7363
|
+
exports.createLineStream = createLineStream;
|
|
6739
7364
|
exports.createPaginatedResponse = createPaginatedResponse;
|
|
6740
7365
|
exports.createParamDecorator = createParamDecorator;
|
|
6741
7366
|
exports.createParamDecoratorWithoutParam = createParamDecoratorWithoutParam;
|
|
@@ -6745,7 +7370,10 @@ exports.createSuccessResponse = createSuccessResponse;
|
|
|
6745
7370
|
exports.createTaskQueue = createTaskQueue;
|
|
6746
7371
|
exports.createTempDir = createTempDir;
|
|
6747
7372
|
exports.createTempFile = createTempFile;
|
|
7373
|
+
exports.createThrottleStream = createThrottleStream;
|
|
6748
7374
|
exports.createUrlBuilder = createUrlBuilder;
|
|
7375
|
+
exports.dateDiff = dateDiff;
|
|
7376
|
+
exports.daysInMonth = daysInMonth;
|
|
6749
7377
|
exports.debounce = debounce;
|
|
6750
7378
|
exports.decrypt = decrypt;
|
|
6751
7379
|
exports.deepFreeze = deepFreeze;
|
|
@@ -6756,8 +7384,10 @@ exports.deleteFileIfExists = deleteFileIfExists;
|
|
|
6756
7384
|
exports.difference = difference;
|
|
6757
7385
|
exports.emptyDir = emptyDir;
|
|
6758
7386
|
exports.encrypt = encrypt;
|
|
7387
|
+
exports.endOf = endOf;
|
|
6759
7388
|
exports.ensureDir = ensureDir;
|
|
6760
7389
|
exports.ensureEmptyDir = ensureEmptyDir;
|
|
7390
|
+
exports.ensureType = ensureType;
|
|
6761
7391
|
exports.equalsIgnoreCase = equalsIgnoreCase;
|
|
6762
7392
|
exports.errorHandler = errorHandler;
|
|
6763
7393
|
exports.extractFilterParams = extractFilterParams;
|
|
@@ -6772,6 +7402,8 @@ exports.findNewestFile = findNewestFile;
|
|
|
6772
7402
|
exports.findOldestFile = findOldestFile;
|
|
6773
7403
|
exports.flattenDeep = flattenDeep;
|
|
6774
7404
|
exports.flattenObject = flattenObject;
|
|
7405
|
+
exports.formatDate = formatDate;
|
|
7406
|
+
exports.formatRelativeTime = formatRelativeTime;
|
|
6775
7407
|
exports.generateApiKey = generateApiKey;
|
|
6776
7408
|
exports.generateRandomBytes = generateRandomBytes;
|
|
6777
7409
|
exports.generateRandomBytesAsString = generateRandomBytesAsString;
|
|
@@ -6790,6 +7422,7 @@ exports.getPaginationParams = getPaginationParams;
|
|
|
6790
7422
|
exports.getRedactCensor = getRedactCensor;
|
|
6791
7423
|
exports.getRequestId = getRequestId;
|
|
6792
7424
|
exports.getSubdirectories = getSubdirectories;
|
|
7425
|
+
exports.getTypeOf = getTypeOf;
|
|
6793
7426
|
exports.getValueByPath = getValueByPath;
|
|
6794
7427
|
exports.groupBy = groupBy;
|
|
6795
7428
|
exports.hasErrorShape = hasErrorShape;
|
|
@@ -6802,7 +7435,9 @@ exports.intersect = intersect;
|
|
|
6802
7435
|
exports.isAlpha = isAlpha;
|
|
6803
7436
|
exports.isAlphanumeric = isAlphanumeric;
|
|
6804
7437
|
exports.isArray = isArray;
|
|
7438
|
+
exports.isArrayOf = isArrayOf;
|
|
6805
7439
|
exports.isBase64 = isBase64;
|
|
7440
|
+
exports.isBetween = isBetween;
|
|
6806
7441
|
exports.isCreditCard = isCreditCard;
|
|
6807
7442
|
exports.isDateInRange = isDateInRange;
|
|
6808
7443
|
exports.isDirectory = isDirectory;
|
|
@@ -6814,6 +7449,7 @@ exports.isHttpError = isHttpError;
|
|
|
6814
7449
|
exports.isIPv4 = isIPv4;
|
|
6815
7450
|
exports.isIPv6 = isIPv6;
|
|
6816
7451
|
exports.isISODate = isISODate;
|
|
7452
|
+
exports.isLeapYear = isLeapYear;
|
|
6817
7453
|
exports.isLengthBetween = isLengthBetween;
|
|
6818
7454
|
exports.isNumberBetween = isNumberBetween;
|
|
6819
7455
|
exports.isNumeric = isNumeric;
|
|
@@ -6822,6 +7458,7 @@ exports.isObject = isObject;
|
|
|
6822
7458
|
exports.isPhone = isPhone;
|
|
6823
7459
|
exports.isPlainObject = isPlainObject;
|
|
6824
7460
|
exports.isPort = isPort;
|
|
7461
|
+
exports.isPrimitiveType = isPrimitiveType;
|
|
6825
7462
|
exports.isStrongPassword = isStrongPassword;
|
|
6826
7463
|
exports.isURL = isURL;
|
|
6827
7464
|
exports.isUUID = isUUID;
|
|
@@ -6835,6 +7472,7 @@ exports.mapObject = mapObject;
|
|
|
6835
7472
|
exports.mask = mask;
|
|
6836
7473
|
exports.matchesPattern = matchesPattern;
|
|
6837
7474
|
exports.md5 = md5;
|
|
7475
|
+
exports.memoize = memoize;
|
|
6838
7476
|
exports.memoizeAsync = memoizeAsync;
|
|
6839
7477
|
exports.mergeSort = mergeSort;
|
|
6840
7478
|
exports.moveDir = moveDir;
|
|
@@ -6843,6 +7481,7 @@ exports.nanoId = nanoId;
|
|
|
6843
7481
|
exports.normalizeUrl = normalizeUrl;
|
|
6844
7482
|
exports.omit = omit;
|
|
6845
7483
|
exports.parseBooleanParam = parseBooleanParam;
|
|
7484
|
+
exports.parseDate = parseDate;
|
|
6846
7485
|
exports.parseNumberParam = parseNumberParam;
|
|
6847
7486
|
exports.parseQueryString = parseQueryString;
|
|
6848
7487
|
exports.parseTypedQueryParams = parseTypedQueryParams;
|
|
@@ -6886,16 +7525,26 @@ exports.shuffle = shuffle;
|
|
|
6886
7525
|
exports.singletonAsync = singletonAsync;
|
|
6887
7526
|
exports.sleep = sleep;
|
|
6888
7527
|
exports.slugify = slugify;
|
|
7528
|
+
exports.startOf = startOf;
|
|
6889
7529
|
exports.streamFile = streamFile;
|
|
7530
|
+
exports.streamToBuffer = streamToBuffer;
|
|
7531
|
+
exports.streamToString = streamToString;
|
|
6890
7532
|
exports.stripHtml = stripHtml;
|
|
6891
7533
|
exports.take = take;
|
|
6892
7534
|
exports.takeWhile = takeWhile;
|
|
6893
7535
|
exports.throttle = throttle;
|
|
7536
|
+
exports.timeAsync = timeAsync;
|
|
7537
|
+
exports.timeSync = timeSync;
|
|
7538
|
+
exports.timed = timed;
|
|
6894
7539
|
exports.timeout = timeout;
|
|
7540
|
+
exports.toBool = toBool;
|
|
6895
7541
|
exports.toCamelCase = toCamelCase;
|
|
6896
7542
|
exports.toKebabCase = toKebabCase;
|
|
7543
|
+
exports.toNum = toNum;
|
|
6897
7544
|
exports.toPascalCase = toPascalCase;
|
|
6898
7545
|
exports.toSnakeCase = toSnakeCase;
|
|
7546
|
+
exports.toStr = toStr;
|
|
7547
|
+
exports.trackMemoryUsage = trackMemoryUsage;
|
|
6899
7548
|
exports.truncate = truncate;
|
|
6900
7549
|
exports.unique = unique;
|
|
6901
7550
|
exports.uuid = uuid;
|