opticore-cache 1.0.0 → 1.0.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/README.md +446 -155
- package/dist/index.cjs +214 -59
- package/dist/index.d.cts +54 -14
- package/dist/index.d.ts +54 -14
- package/dist/index.js +205 -60
- package/package.json +7 -9
package/dist/index.cjs
CHANGED
|
@@ -30,7 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
33
|
+
CacheFactoryRepository: () => CacheFactoryRepository,
|
|
34
|
+
CacheMiddleware: () => CacheMiddleware,
|
|
35
|
+
CacheService: () => CacheService,
|
|
36
|
+
EnvironmentLoader: () => EnvironmentLoader,
|
|
37
|
+
HttpCacheFactory: () => HttpCacheFactory,
|
|
38
|
+
RouteValidator: () => RouteValidator,
|
|
39
|
+
SCacheMiddleware: () => SCacheMiddleware
|
|
34
40
|
});
|
|
35
41
|
module.exports = __toCommonJS(index_exports);
|
|
36
42
|
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
@@ -431,6 +437,7 @@ var MemoryCache = class {
|
|
|
431
437
|
var import_fs = __toESM(require("fs"), 1);
|
|
432
438
|
var import_path = __toESM(require("path"), 1);
|
|
433
439
|
var import_util = require("util");
|
|
440
|
+
var import_opticore_logger = require("opticore-logger");
|
|
434
441
|
var fsExists = (0, import_util.promisify)(import_fs.default.exists);
|
|
435
442
|
var fsMkdir = (0, import_util.promisify)(import_fs.default.mkdir);
|
|
436
443
|
var fsReadFile = (0, import_util.promisify)(import_fs.default.readFile);
|
|
@@ -476,7 +483,7 @@ var DiskCache = class {
|
|
|
476
483
|
* @private
|
|
477
484
|
*/
|
|
478
485
|
getCacheDirectory() {
|
|
479
|
-
return process.env.CACHE_DISK_DIR || import_path.default.join(process.cwd(), "
|
|
486
|
+
return process.env.CACHE_DISK_DIR || import_path.default.join(process.cwd(), "src", "core", "cache", this.config.namespace);
|
|
480
487
|
}
|
|
481
488
|
/**
|
|
482
489
|
* Initialise le répertoire de cache
|
|
@@ -853,8 +860,8 @@ NE PAS SUPPRIMER MANUELLEMENT ces fichiers pendant l'ex\xE9cution du serveur.
|
|
|
853
860
|
if (this.cleanupInterval) {
|
|
854
861
|
clearInterval(this.cleanupInterval);
|
|
855
862
|
}
|
|
856
|
-
this.cleanupInterval = setInterval(() => {
|
|
857
|
-
this.cleanupExpiredEntries();
|
|
863
|
+
this.cleanupInterval = setInterval(async () => {
|
|
864
|
+
await this.cleanupExpiredEntries();
|
|
858
865
|
}, this.config.cleanupInterval);
|
|
859
866
|
if (this.config.debug) {
|
|
860
867
|
console.log(`[DiskCache] Cycle de nettoyage: ${this.config.cleanupInterval}ms`);
|
|
@@ -879,6 +886,9 @@ NE PAS SUPPRIMER MANUELLEMENT ces fichiers pendant l'ex\xE9cution du serveur.
|
|
|
879
886
|
console.log(`[DiskCache] Nettoyage: ${expiredKeys.length} entr\xE9es expir\xE9es`);
|
|
880
887
|
}
|
|
881
888
|
}
|
|
889
|
+
logger() {
|
|
890
|
+
return new import_opticore_logger.LoggerCore();
|
|
891
|
+
}
|
|
882
892
|
/**
|
|
883
893
|
* Récupère le chemin du dossier de cache
|
|
884
894
|
* @returns string
|
|
@@ -1240,7 +1250,7 @@ var CacheManagementUseCase = class {
|
|
|
1240
1250
|
|
|
1241
1251
|
// src/application/services/cacheService.service.ts
|
|
1242
1252
|
var import_opticore_loader_translation = require("opticore-loader-translation");
|
|
1243
|
-
var
|
|
1253
|
+
var import_opticore_logger2 = require("opticore-logger");
|
|
1244
1254
|
var CacheService = class _CacheService {
|
|
1245
1255
|
cacheUseCase;
|
|
1246
1256
|
cacheRepository;
|
|
@@ -1414,7 +1424,7 @@ var CacheService = class _CacheService {
|
|
|
1414
1424
|
});
|
|
1415
1425
|
}
|
|
1416
1426
|
logger() {
|
|
1417
|
-
return new
|
|
1427
|
+
return new import_opticore_logger2.LoggerCore(this.configLogger);
|
|
1418
1428
|
}
|
|
1419
1429
|
};
|
|
1420
1430
|
|
|
@@ -1480,11 +1490,11 @@ var RouteValidator = class {
|
|
|
1480
1490
|
* @param enableWildcards - Activer le support des wildcards
|
|
1481
1491
|
* @returns RegExp
|
|
1482
1492
|
*/
|
|
1483
|
-
compileToRegex(
|
|
1484
|
-
if (!enableWildcards || !
|
|
1485
|
-
return new RegExp(`^${this.escapeRegex(
|
|
1493
|
+
compileToRegex(path3, enableWildcards) {
|
|
1494
|
+
if (!enableWildcards || !path3.includes("*")) {
|
|
1495
|
+
return new RegExp(`^${this.escapeRegex(path3)}$`, "i");
|
|
1486
1496
|
}
|
|
1487
|
-
let regexString = this.escapeRegex(
|
|
1497
|
+
let regexString = this.escapeRegex(path3).replace(/\\\*/g, ".*");
|
|
1488
1498
|
if (regexString.endsWith("/.*")) {
|
|
1489
1499
|
regexString = regexString.replace(/\/\.\*$/, "(\\/.*)?");
|
|
1490
1500
|
}
|
|
@@ -1528,11 +1538,11 @@ var RouteValidator = class {
|
|
|
1528
1538
|
* @param path - Chemin à normaliser
|
|
1529
1539
|
* @returns string - Chemin normalisé
|
|
1530
1540
|
*/
|
|
1531
|
-
normalizePath(
|
|
1532
|
-
if (
|
|
1533
|
-
return
|
|
1541
|
+
normalizePath(path3) {
|
|
1542
|
+
if (path3.length > 1 && path3.endsWith("/")) {
|
|
1543
|
+
return path3.slice(0, -1);
|
|
1534
1544
|
}
|
|
1535
|
-
return
|
|
1545
|
+
return path3;
|
|
1536
1546
|
}
|
|
1537
1547
|
/**
|
|
1538
1548
|
* Récupère la liste des patterns compilés (pour debug)
|
|
@@ -1676,11 +1686,11 @@ var CacheMiddleware = class {
|
|
|
1676
1686
|
* @returns string
|
|
1677
1687
|
* @private
|
|
1678
1688
|
*/
|
|
1679
|
-
normalizePath(
|
|
1680
|
-
if (
|
|
1681
|
-
return
|
|
1689
|
+
normalizePath(path3) {
|
|
1690
|
+
if (path3.length > 1 && path3.endsWith("/")) {
|
|
1691
|
+
return path3.slice(0, -1);
|
|
1682
1692
|
}
|
|
1683
|
-
return
|
|
1693
|
+
return path3;
|
|
1684
1694
|
}
|
|
1685
1695
|
/**
|
|
1686
1696
|
* Adds HTTP headers related to the cache
|
|
@@ -1835,17 +1845,34 @@ var SCacheMiddleware = class {
|
|
|
1835
1845
|
};
|
|
1836
1846
|
|
|
1837
1847
|
// src/application/services/adaptedHttpCache.service.ts
|
|
1848
|
+
var import_opticore_logger3 = require("opticore-logger");
|
|
1849
|
+
var import_opticore_webapp_core = require("opticore-webapp-core");
|
|
1850
|
+
var import_opticore_webapp = require("opticore-webapp");
|
|
1851
|
+
var import_opticore_http_response = require("opticore-http-response");
|
|
1838
1852
|
var AdaptedHttpCacheService = class {
|
|
1853
|
+
/**
|
|
1854
|
+
* Creates an instance of AdaptedHttpCacheService.
|
|
1855
|
+
*
|
|
1856
|
+
* @param cacheService - The underlying cache service instance (must implement getOrSet, delete, etc.)
|
|
1857
|
+
* @param namespace - Namespace prefix for cache keys (default: "http-cache")
|
|
1858
|
+
*/
|
|
1839
1859
|
constructor(cacheService, namespace = "http-cache") {
|
|
1840
1860
|
this.cacheService = cacheService;
|
|
1841
1861
|
this.namespace = namespace;
|
|
1842
1862
|
}
|
|
1843
|
-
stats = {
|
|
1863
|
+
stats = {
|
|
1864
|
+
totalRequests: 0,
|
|
1865
|
+
cacheHits: 0,
|
|
1866
|
+
cacheMisses: 0
|
|
1867
|
+
};
|
|
1844
1868
|
/**
|
|
1869
|
+
* Performs a GET request and caches the response.
|
|
1845
1870
|
*
|
|
1846
|
-
* @param url
|
|
1847
|
-
* @param options
|
|
1848
|
-
* @param cacheOptions
|
|
1871
|
+
* @param url - The URL to fetch
|
|
1872
|
+
* @param options - Optional fetch options (headers, etc.)
|
|
1873
|
+
* @param cacheOptions - Cache configuration (TTL, bypass, custom key)
|
|
1874
|
+
*
|
|
1875
|
+
* @returns A promise resolving to the response data, merged with metadata
|
|
1849
1876
|
*/
|
|
1850
1877
|
async getWithCache(url, options, cacheOptions) {
|
|
1851
1878
|
this.stats.totalRequests++;
|
|
@@ -1873,12 +1900,15 @@ var AdaptedHttpCacheService = class {
|
|
|
1873
1900
|
}
|
|
1874
1901
|
}
|
|
1875
1902
|
/**
|
|
1903
|
+
* Performs a POST request and optionally caches the response.
|
|
1904
|
+
*
|
|
1905
|
+
* @param url - The URL to post to
|
|
1906
|
+
* @param data - The payload to send
|
|
1907
|
+
* @param options - Additional fetch options
|
|
1908
|
+
* @param cacheResponse - Whether to cache the response (default: false)
|
|
1909
|
+
* @param cacheOptions - Cache configuration (TTL, bypass, namespace)
|
|
1876
1910
|
*
|
|
1877
|
-
* @
|
|
1878
|
-
* @param data
|
|
1879
|
-
* @param options
|
|
1880
|
-
* @param cacheResponse
|
|
1881
|
-
* @param cacheOptions
|
|
1911
|
+
* @returns A promise resolving to the response data, merged with metadata
|
|
1882
1912
|
*/
|
|
1883
1913
|
async postWithCache(url, data, options, cacheResponse = false, cacheOptions) {
|
|
1884
1914
|
this.stats.totalRequests++;
|
|
@@ -1908,18 +1938,26 @@ var AdaptedHttpCacheService = class {
|
|
|
1908
1938
|
);
|
|
1909
1939
|
return this.mergeResponse(cachedResponse);
|
|
1910
1940
|
} catch (error) {
|
|
1911
|
-
|
|
1941
|
+
this.SLogger().error({
|
|
1942
|
+
errorType: error.type,
|
|
1943
|
+
httpCodeValue: import_opticore_http_response.HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
1944
|
+
message: `Cache error for POST ${url}:\`, ${error.message}`,
|
|
1945
|
+
stackTrace: error.stack,
|
|
1946
|
+
title: "ERROR_POST_CACHING"
|
|
1947
|
+
});
|
|
1912
1948
|
this.stats.cacheMisses++;
|
|
1913
1949
|
const response = await this.executeRequest("POST", url, requestOptions);
|
|
1914
1950
|
return this.mergeResponse(response);
|
|
1915
1951
|
}
|
|
1916
1952
|
}
|
|
1917
1953
|
/**
|
|
1954
|
+
* Executes an HTTP request and returns a normalized response.
|
|
1918
1955
|
*
|
|
1919
|
-
* @param method
|
|
1920
|
-
* @param url
|
|
1921
|
-
* @param options
|
|
1922
|
-
*
|
|
1956
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
1957
|
+
* @param url - Request URL
|
|
1958
|
+
* @param options - Fetch options
|
|
1959
|
+
*
|
|
1960
|
+
* @returns A promise resolving to an IHttpClientResponse containing data and metadata
|
|
1923
1961
|
*/
|
|
1924
1962
|
async executeRequest(method, url, options) {
|
|
1925
1963
|
const response = await fetch(url, { method, ...options });
|
|
@@ -1936,21 +1974,25 @@ var AdaptedHttpCacheService = class {
|
|
|
1936
1974
|
};
|
|
1937
1975
|
}
|
|
1938
1976
|
/**
|
|
1977
|
+
* Merges response data with metadata, adding a _metadata property.
|
|
1939
1978
|
*
|
|
1940
|
-
* @param response
|
|
1941
|
-
*
|
|
1979
|
+
* @param response - The HTTP client response containing data and metadata
|
|
1980
|
+
*
|
|
1981
|
+
* @returns The data enriched with a _metadata field
|
|
1942
1982
|
*/
|
|
1943
1983
|
mergeResponse(response) {
|
|
1944
1984
|
const result = { ...response.data, _metadata: response.metadata };
|
|
1945
1985
|
return result;
|
|
1946
1986
|
}
|
|
1947
1987
|
/**
|
|
1988
|
+
* Generates a cache key based on request parameters.
|
|
1948
1989
|
*
|
|
1949
|
-
* @param method
|
|
1950
|
-
* @param url
|
|
1951
|
-
* @param options
|
|
1952
|
-
* @param customKey
|
|
1953
|
-
*
|
|
1990
|
+
* @param method - HTTP method
|
|
1991
|
+
* @param url - Request URL
|
|
1992
|
+
* @param options - Request options (headers, body)
|
|
1993
|
+
* @param customKey - Optional override for the key
|
|
1994
|
+
*
|
|
1995
|
+
* @returns A base64 encoded cache key string
|
|
1954
1996
|
*/
|
|
1955
1997
|
generateKey(method, url, options, customKey) {
|
|
1956
1998
|
if (customKey) return customKey;
|
|
@@ -1963,35 +2005,48 @@ var AdaptedHttpCacheService = class {
|
|
|
1963
2005
|
return Buffer.from(keyParts.join("|")).toString("base64");
|
|
1964
2006
|
}
|
|
1965
2007
|
/**
|
|
2008
|
+
* Invalidates cache entries matching a URL or pattern.
|
|
1966
2009
|
*
|
|
1967
|
-
* @param urlOrPattern
|
|
2010
|
+
* @param urlOrPattern - The URL or pattern to invalidate
|
|
2011
|
+
*
|
|
2012
|
+
* @returns A promise resolving to the number of invalidated entries
|
|
1968
2013
|
*/
|
|
1969
2014
|
async invalidateCache(urlOrPattern) {
|
|
1970
2015
|
try {
|
|
1971
|
-
console.log(`Invalidating cache for pattern: ${urlOrPattern}`);
|
|
1972
2016
|
const encodedPattern = Buffer.from(urlOrPattern).toString("base64").slice(0, 20);
|
|
1973
2017
|
const searchPattern = `*${encodedPattern}*`;
|
|
1974
|
-
|
|
1975
|
-
console.log(`Invalidated ${invalidatedCount} entries for pattern: ${urlOrPattern}`);
|
|
1976
|
-
return invalidatedCount;
|
|
2018
|
+
return await this.cacheService.invalidate(searchPattern);
|
|
1977
2019
|
} catch (error) {
|
|
1978
|
-
|
|
2020
|
+
this.SLogger().error({
|
|
2021
|
+
errorType: error.type,
|
|
2022
|
+
httpCodeValue: import_opticore_http_response.HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
2023
|
+
message: `Error invalidating cache for ${urlOrPattern}: error.message`,
|
|
2024
|
+
stackTrace: error.stackTrace,
|
|
2025
|
+
title: "INVALIDATE CACHE"
|
|
2026
|
+
});
|
|
1979
2027
|
try {
|
|
1980
2028
|
if (urlOrPattern.startsWith(this.namespace + ":")) {
|
|
1981
2029
|
await this.cacheService.delete(urlOrPattern);
|
|
1982
2030
|
return 1;
|
|
1983
2031
|
}
|
|
1984
2032
|
} catch (fallbackError) {
|
|
2033
|
+
this.SLogger().error({
|
|
2034
|
+
errorType: fallbackError.name,
|
|
2035
|
+
httpCodeValue: import_opticore_http_response.HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
2036
|
+
message: fallbackError.message,
|
|
2037
|
+
stackTrace: fallbackError.stackTrace,
|
|
2038
|
+
title: "INVALIDATE_PATTERN"
|
|
2039
|
+
});
|
|
1985
2040
|
}
|
|
1986
2041
|
return 0;
|
|
1987
2042
|
}
|
|
1988
2043
|
}
|
|
1989
2044
|
/**
|
|
1990
|
-
*
|
|
2045
|
+
* Clears all HTTP cache entries and resets statistics.
|
|
2046
|
+
* @returns A promise that resolves when the cache is cleared
|
|
1991
2047
|
*/
|
|
1992
2048
|
async clearHttpCache() {
|
|
1993
2049
|
try {
|
|
1994
|
-
console.log("Clearing HTTP cache...");
|
|
1995
2050
|
if (typeof this.cacheService.clear === "function") {
|
|
1996
2051
|
await this.cacheService.clear();
|
|
1997
2052
|
} else if (typeof this.cacheService.keys === "function") {
|
|
@@ -2000,15 +2055,31 @@ var AdaptedHttpCacheService = class {
|
|
|
2000
2055
|
for (const key of httpKeys) {
|
|
2001
2056
|
await this.cacheService.delete(key);
|
|
2002
2057
|
}
|
|
2003
|
-
|
|
2058
|
+
this.SLogger().success({
|
|
2059
|
+
title: "CLEAR_HTTP_CACHE_ENTRIES",
|
|
2060
|
+
message: `Cleared ${httpKeys.length} HTTP cache entries`
|
|
2061
|
+
});
|
|
2004
2062
|
}
|
|
2005
2063
|
this.stats = { totalRequests: 0, cacheHits: 0, cacheMisses: 0 };
|
|
2006
|
-
|
|
2064
|
+
this.SLogger().success({
|
|
2065
|
+
title: "CLEAR_CACHE",
|
|
2066
|
+
message: "HTTP cache cleared successfully"
|
|
2067
|
+
});
|
|
2007
2068
|
} catch (error) {
|
|
2008
|
-
|
|
2069
|
+
this.SLogger().error({
|
|
2070
|
+
errorType: error.name,
|
|
2071
|
+
httpCodeValue: import_opticore_http_response.HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
2072
|
+
message: error.message,
|
|
2073
|
+
stackTrace: error.stackTrace,
|
|
2074
|
+
title: "ERROR_CLEARING_HTTP_CACHE"
|
|
2075
|
+
});
|
|
2009
2076
|
throw new Error(`Failed to clear HTTP cache: ${error.message}`);
|
|
2010
2077
|
}
|
|
2011
2078
|
}
|
|
2079
|
+
/**
|
|
2080
|
+
* Retrieves current cache statistics.
|
|
2081
|
+
* @returns A promise resolving to an HttpCacheStats object
|
|
2082
|
+
*/
|
|
2012
2083
|
async getStats() {
|
|
2013
2084
|
try {
|
|
2014
2085
|
const cacheStats = await this.cacheService.getStatistics();
|
|
@@ -2027,10 +2098,20 @@ var AdaptedHttpCacheService = class {
|
|
|
2027
2098
|
enabled: this.isEnabled()
|
|
2028
2099
|
};
|
|
2029
2100
|
} catch (error) {
|
|
2030
|
-
|
|
2101
|
+
this.SLogger().error({
|
|
2102
|
+
errorType: error.name,
|
|
2103
|
+
httpCodeValue: import_opticore_http_response.HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
2104
|
+
message: error.message,
|
|
2105
|
+
stackTrace: error.stackTrace,
|
|
2106
|
+
title: "ERROR_GETTING_CACHE_STATS"
|
|
2107
|
+
});
|
|
2031
2108
|
return this.getDefaultStats();
|
|
2032
2109
|
}
|
|
2033
2110
|
}
|
|
2111
|
+
/**
|
|
2112
|
+
* Returns default statistics when an error occurs.
|
|
2113
|
+
* @returns A default HttpCacheStats object
|
|
2114
|
+
*/
|
|
2034
2115
|
getDefaultStats() {
|
|
2035
2116
|
return {
|
|
2036
2117
|
totalRequests: this.stats.totalRequests,
|
|
@@ -2043,6 +2124,10 @@ var AdaptedHttpCacheService = class {
|
|
|
2043
2124
|
enabled: false
|
|
2044
2125
|
};
|
|
2045
2126
|
}
|
|
2127
|
+
/**
|
|
2128
|
+
* Checks whether the cache service is enabled.
|
|
2129
|
+
* @returns True if the cache is enabled, false otherwise
|
|
2130
|
+
*/
|
|
2046
2131
|
isEnabled() {
|
|
2047
2132
|
try {
|
|
2048
2133
|
if (this.cacheService.isEnabled && typeof this.cacheService.isEnabled === "function") {
|
|
@@ -2053,6 +2138,13 @@ var AdaptedHttpCacheService = class {
|
|
|
2053
2138
|
return false;
|
|
2054
2139
|
}
|
|
2055
2140
|
}
|
|
2141
|
+
/**
|
|
2142
|
+
* Creates a logger instance for internal error reporting.
|
|
2143
|
+
* @returns A configured LoggerCore instance
|
|
2144
|
+
*/
|
|
2145
|
+
SLogger() {
|
|
2146
|
+
return new import_opticore_logger3.LoggerCore((0, import_opticore_webapp_core.loggerConfig)(import_opticore_webapp.envPath));
|
|
2147
|
+
}
|
|
2056
2148
|
};
|
|
2057
2149
|
|
|
2058
2150
|
// src/application/services/fetch.client.service.ts
|
|
@@ -2505,8 +2597,33 @@ var CurlHttpClient = class {
|
|
|
2505
2597
|
};
|
|
2506
2598
|
|
|
2507
2599
|
// src/application/services/httpCacheFactory.service.ts
|
|
2600
|
+
var import_fs2 = __toESM(require("fs"), 1);
|
|
2601
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
2602
|
+
var import_opticore_logger4 = require("opticore-logger");
|
|
2508
2603
|
var HttpCacheFactory = class {
|
|
2604
|
+
/**
|
|
2605
|
+
* Map of created instances, keyed by `${appName}:${clientType}`.
|
|
2606
|
+
* @private
|
|
2607
|
+
*/
|
|
2509
2608
|
static instances = /* @__PURE__ */ new Map();
|
|
2609
|
+
/**
|
|
2610
|
+
* Creates or retrieves an HTTP cache service instance.
|
|
2611
|
+
* If an instance for the given appName and clientType already exists, it is returned.
|
|
2612
|
+
* Otherwise, a new instance is created with the provided configuration.
|
|
2613
|
+
*
|
|
2614
|
+
* @param appName - Name of the application/context (default: "default"). Used for instance caching and default namespace.
|
|
2615
|
+
* @param config - Configuration options for the cache and HTTP client.
|
|
2616
|
+
* @param config.namespace - Namespace prefix for cache keys (default: `http-cache-${appName}`).
|
|
2617
|
+
* @param config.localLang - Localization language (optional).
|
|
2618
|
+
* @param config.storageType - Storage backend type: "memory", "disk", or "hybrid" (default: "disk").
|
|
2619
|
+
* @param config.diskDir - Directory path for disk storage (default: "src/core/cache").
|
|
2620
|
+
* @param config.maxSize - Maximum number of items to store (default: 10000).
|
|
2621
|
+
* @param config.defaultTTL - Default time-to-live in milliseconds (default: 300000).
|
|
2622
|
+
* @param config.clientType - HTTP client to use: "fetch", "node-http", "axios", or "curl" (default: "fetch").
|
|
2623
|
+
* @param config.clientOptions - Additional options for the HTTP client (e.g., timeout, headers, baseURL).
|
|
2624
|
+
*
|
|
2625
|
+
* @returns An HTTP cache service instance implementing IHttpCacheService.
|
|
2626
|
+
*/
|
|
2510
2627
|
static create(appName = "default", config) {
|
|
2511
2628
|
const instanceKey = `${appName}:${config?.clientType || "fetch"}`;
|
|
2512
2629
|
if (!this.instances.has(instanceKey)) {
|
|
@@ -2531,8 +2648,18 @@ var HttpCacheFactory = class {
|
|
|
2531
2648
|
maxSize: config?.maxSize || 1e4,
|
|
2532
2649
|
defaultTTL: config?.defaultTTL || 3e5,
|
|
2533
2650
|
storageType: config?.storageType || "disk",
|
|
2534
|
-
diskDir: config?.diskDir ||
|
|
2651
|
+
diskDir: config?.diskDir || "src/core/cache"
|
|
2535
2652
|
};
|
|
2653
|
+
if (cacheConfig.storageType === "disk" || cacheConfig.storageType === "hybrid") {
|
|
2654
|
+
const resolvedDiskDir = import_path2.default.resolve(process.cwd(), cacheConfig.diskDir);
|
|
2655
|
+
if (!import_fs2.default.existsSync(resolvedDiskDir)) {
|
|
2656
|
+
this.SLogger().warn({
|
|
2657
|
+
title: "UNRESOLVED_CACHE_DIR",
|
|
2658
|
+
message: `[HttpCacheFactory] Cache directory does not exist: ${resolvedDiskDir}
|
|
2659
|
+
Please create it manually or ensure the path is correct.`
|
|
2660
|
+
});
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2536
2663
|
const instance = new HttpCacheClient(httpClient, void 0, {
|
|
2537
2664
|
cacheConfig,
|
|
2538
2665
|
timeout: config?.clientOptions?.timeout,
|
|
@@ -2543,23 +2670,51 @@ var HttpCacheFactory = class {
|
|
|
2543
2670
|
}
|
|
2544
2671
|
return this.instances.get(instanceKey);
|
|
2545
2672
|
}
|
|
2673
|
+
/**
|
|
2674
|
+
* Creates an HTTP cache service instance by adapting an existing generic cache service.
|
|
2675
|
+
* This is useful when you already have a cache service from another source and want to
|
|
2676
|
+
* use it with the HTTP caching interface.
|
|
2677
|
+
*
|
|
2678
|
+
* @param cacheService - The underlying cache service instance (must support getOrSet, delete, etc.).
|
|
2679
|
+
* @param namespace - Namespace for cache keys (default: "http-cache").
|
|
2680
|
+
*
|
|
2681
|
+
* @returns An adapted HTTP cache service implementing IHttpCacheService.
|
|
2682
|
+
*/
|
|
2546
2683
|
static createFromExistingCache(cacheService, namespace = "http-cache") {
|
|
2547
2684
|
return new AdaptedHttpCacheService(cacheService, namespace);
|
|
2548
2685
|
}
|
|
2686
|
+
/**
|
|
2687
|
+
* Destroys a cached HTTP cache service instance, removing it from the internal map.
|
|
2688
|
+
* After destruction, a new call to `create` will generate a fresh instance.
|
|
2689
|
+
*
|
|
2690
|
+
* @param appName - Name of the application/context (default: "default").
|
|
2691
|
+
* @param clientType - HTTP client type used for the instance (default: "fetch").
|
|
2692
|
+
*
|
|
2693
|
+
* @return void
|
|
2694
|
+
*/
|
|
2549
2695
|
static destroy(appName = "default", clientType) {
|
|
2550
2696
|
const key = `${appName}:${clientType || "fetch"}`;
|
|
2551
2697
|
this.instances.delete(key);
|
|
2552
2698
|
}
|
|
2699
|
+
/**
|
|
2700
|
+
* Internal logger for warnings and errors.
|
|
2701
|
+
* @returns A LoggerCore instance configured for the factory.
|
|
2702
|
+
* @private
|
|
2703
|
+
*/
|
|
2704
|
+
static SLogger() {
|
|
2705
|
+
return new import_opticore_logger4.LoggerCore();
|
|
2706
|
+
}
|
|
2553
2707
|
};
|
|
2554
2708
|
|
|
2555
2709
|
// src/index.ts
|
|
2556
2710
|
import_dotenv.default.config();
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
CacheService,
|
|
2560
|
-
CacheMiddleware,
|
|
2711
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2712
|
+
0 && (module.exports = {
|
|
2561
2713
|
CacheFactoryRepository,
|
|
2562
|
-
|
|
2714
|
+
CacheMiddleware,
|
|
2715
|
+
CacheService,
|
|
2563
2716
|
EnvironmentLoader,
|
|
2564
|
-
HttpCacheFactory
|
|
2565
|
-
|
|
2717
|
+
HttpCacheFactory,
|
|
2718
|
+
RouteValidator,
|
|
2719
|
+
SCacheMiddleware
|
|
2720
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -544,8 +544,36 @@ interface IHttpCacheService {
|
|
|
544
544
|
|
|
545
545
|
type HttpClientType = "fetch" | "node-http" | "axios" | "curl";
|
|
546
546
|
|
|
547
|
+
/**
|
|
548
|
+
* Factory class for creating and managing HTTP cache service instances.
|
|
549
|
+
* It provides a centralized way to obtain configured HTTP cache implementations,
|
|
550
|
+
* supporting different HTTP clients and storage backends.
|
|
551
|
+
* Instances are cached and reused based on application name and client type.
|
|
552
|
+
*/
|
|
547
553
|
declare class HttpCacheFactory {
|
|
554
|
+
/**
|
|
555
|
+
* Map of created instances, keyed by `${appName}:${clientType}`.
|
|
556
|
+
* @private
|
|
557
|
+
*/
|
|
548
558
|
private static instances;
|
|
559
|
+
/**
|
|
560
|
+
* Creates or retrieves an HTTP cache service instance.
|
|
561
|
+
* If an instance for the given appName and clientType already exists, it is returned.
|
|
562
|
+
* Otherwise, a new instance is created with the provided configuration.
|
|
563
|
+
*
|
|
564
|
+
* @param appName - Name of the application/context (default: "default"). Used for instance caching and default namespace.
|
|
565
|
+
* @param config - Configuration options for the cache and HTTP client.
|
|
566
|
+
* @param config.namespace - Namespace prefix for cache keys (default: `http-cache-${appName}`).
|
|
567
|
+
* @param config.localLang - Localization language (optional).
|
|
568
|
+
* @param config.storageType - Storage backend type: "memory", "disk", or "hybrid" (default: "disk").
|
|
569
|
+
* @param config.diskDir - Directory path for disk storage (default: "src/core/cache").
|
|
570
|
+
* @param config.maxSize - Maximum number of items to store (default: 10000).
|
|
571
|
+
* @param config.defaultTTL - Default time-to-live in milliseconds (default: 300000).
|
|
572
|
+
* @param config.clientType - HTTP client to use: "fetch", "node-http", "axios", or "curl" (default: "fetch").
|
|
573
|
+
* @param config.clientOptions - Additional options for the HTTP client (e.g., timeout, headers, baseURL).
|
|
574
|
+
*
|
|
575
|
+
* @returns An HTTP cache service instance implementing IHttpCacheService.
|
|
576
|
+
*/
|
|
549
577
|
static create(appName?: string, config?: {
|
|
550
578
|
namespace?: string;
|
|
551
579
|
localLang?: string;
|
|
@@ -556,8 +584,33 @@ declare class HttpCacheFactory {
|
|
|
556
584
|
clientType?: HttpClientType;
|
|
557
585
|
clientOptions?: any;
|
|
558
586
|
}): IHttpCacheService;
|
|
587
|
+
/**
|
|
588
|
+
* Creates an HTTP cache service instance by adapting an existing generic cache service.
|
|
589
|
+
* This is useful when you already have a cache service from another source and want to
|
|
590
|
+
* use it with the HTTP caching interface.
|
|
591
|
+
*
|
|
592
|
+
* @param cacheService - The underlying cache service instance (must support getOrSet, delete, etc.).
|
|
593
|
+
* @param namespace - Namespace for cache keys (default: "http-cache").
|
|
594
|
+
*
|
|
595
|
+
* @returns An adapted HTTP cache service implementing IHttpCacheService.
|
|
596
|
+
*/
|
|
559
597
|
static createFromExistingCache(cacheService: any, namespace?: string): IHttpCacheService;
|
|
598
|
+
/**
|
|
599
|
+
* Destroys a cached HTTP cache service instance, removing it from the internal map.
|
|
600
|
+
* After destruction, a new call to `create` will generate a fresh instance.
|
|
601
|
+
*
|
|
602
|
+
* @param appName - Name of the application/context (default: "default").
|
|
603
|
+
* @param clientType - HTTP client type used for the instance (default: "fetch").
|
|
604
|
+
*
|
|
605
|
+
* @return void
|
|
606
|
+
*/
|
|
560
607
|
static destroy(appName?: string, clientType?: HttpClientType): void;
|
|
608
|
+
/**
|
|
609
|
+
* Internal logger for warnings and errors.
|
|
610
|
+
* @returns A LoggerCore instance configured for the factory.
|
|
611
|
+
* @private
|
|
612
|
+
*/
|
|
613
|
+
private static SLogger;
|
|
561
614
|
}
|
|
562
615
|
|
|
563
616
|
/**
|
|
@@ -581,17 +634,4 @@ interface CacheEntry<T = any> {
|
|
|
581
634
|
lastAccessedAt: number;
|
|
582
635
|
}
|
|
583
636
|
|
|
584
|
-
|
|
585
|
-
* Default export object with all features
|
|
586
|
-
*/
|
|
587
|
-
declare const _default: {
|
|
588
|
-
SCacheMiddleware: typeof SCacheMiddleware;
|
|
589
|
-
CacheService: typeof CacheService;
|
|
590
|
-
CacheMiddleware: typeof CacheMiddleware;
|
|
591
|
-
CacheFactoryRepository: typeof CacheFactoryRepository;
|
|
592
|
-
RouteValidator: typeof RouteValidator;
|
|
593
|
-
EnvironmentLoader: typeof EnvironmentLoader;
|
|
594
|
-
HttpCacheFactory: typeof HttpCacheFactory;
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
export { type CacheEntry, type ICacheRepository, type IHttpCacheService, _default as default };
|
|
637
|
+
export { type CacheEntry, CacheFactoryRepository, CacheMiddleware, CacheService, EnvironmentLoader, HttpCacheFactory, type ICacheRepository, type IHttpCacheService, RouteValidator, SCacheMiddleware };
|
package/dist/index.d.ts
CHANGED
|
@@ -544,8 +544,36 @@ interface IHttpCacheService {
|
|
|
544
544
|
|
|
545
545
|
type HttpClientType = "fetch" | "node-http" | "axios" | "curl";
|
|
546
546
|
|
|
547
|
+
/**
|
|
548
|
+
* Factory class for creating and managing HTTP cache service instances.
|
|
549
|
+
* It provides a centralized way to obtain configured HTTP cache implementations,
|
|
550
|
+
* supporting different HTTP clients and storage backends.
|
|
551
|
+
* Instances are cached and reused based on application name and client type.
|
|
552
|
+
*/
|
|
547
553
|
declare class HttpCacheFactory {
|
|
554
|
+
/**
|
|
555
|
+
* Map of created instances, keyed by `${appName}:${clientType}`.
|
|
556
|
+
* @private
|
|
557
|
+
*/
|
|
548
558
|
private static instances;
|
|
559
|
+
/**
|
|
560
|
+
* Creates or retrieves an HTTP cache service instance.
|
|
561
|
+
* If an instance for the given appName and clientType already exists, it is returned.
|
|
562
|
+
* Otherwise, a new instance is created with the provided configuration.
|
|
563
|
+
*
|
|
564
|
+
* @param appName - Name of the application/context (default: "default"). Used for instance caching and default namespace.
|
|
565
|
+
* @param config - Configuration options for the cache and HTTP client.
|
|
566
|
+
* @param config.namespace - Namespace prefix for cache keys (default: `http-cache-${appName}`).
|
|
567
|
+
* @param config.localLang - Localization language (optional).
|
|
568
|
+
* @param config.storageType - Storage backend type: "memory", "disk", or "hybrid" (default: "disk").
|
|
569
|
+
* @param config.diskDir - Directory path for disk storage (default: "src/core/cache").
|
|
570
|
+
* @param config.maxSize - Maximum number of items to store (default: 10000).
|
|
571
|
+
* @param config.defaultTTL - Default time-to-live in milliseconds (default: 300000).
|
|
572
|
+
* @param config.clientType - HTTP client to use: "fetch", "node-http", "axios", or "curl" (default: "fetch").
|
|
573
|
+
* @param config.clientOptions - Additional options for the HTTP client (e.g., timeout, headers, baseURL).
|
|
574
|
+
*
|
|
575
|
+
* @returns An HTTP cache service instance implementing IHttpCacheService.
|
|
576
|
+
*/
|
|
549
577
|
static create(appName?: string, config?: {
|
|
550
578
|
namespace?: string;
|
|
551
579
|
localLang?: string;
|
|
@@ -556,8 +584,33 @@ declare class HttpCacheFactory {
|
|
|
556
584
|
clientType?: HttpClientType;
|
|
557
585
|
clientOptions?: any;
|
|
558
586
|
}): IHttpCacheService;
|
|
587
|
+
/**
|
|
588
|
+
* Creates an HTTP cache service instance by adapting an existing generic cache service.
|
|
589
|
+
* This is useful when you already have a cache service from another source and want to
|
|
590
|
+
* use it with the HTTP caching interface.
|
|
591
|
+
*
|
|
592
|
+
* @param cacheService - The underlying cache service instance (must support getOrSet, delete, etc.).
|
|
593
|
+
* @param namespace - Namespace for cache keys (default: "http-cache").
|
|
594
|
+
*
|
|
595
|
+
* @returns An adapted HTTP cache service implementing IHttpCacheService.
|
|
596
|
+
*/
|
|
559
597
|
static createFromExistingCache(cacheService: any, namespace?: string): IHttpCacheService;
|
|
598
|
+
/**
|
|
599
|
+
* Destroys a cached HTTP cache service instance, removing it from the internal map.
|
|
600
|
+
* After destruction, a new call to `create` will generate a fresh instance.
|
|
601
|
+
*
|
|
602
|
+
* @param appName - Name of the application/context (default: "default").
|
|
603
|
+
* @param clientType - HTTP client type used for the instance (default: "fetch").
|
|
604
|
+
*
|
|
605
|
+
* @return void
|
|
606
|
+
*/
|
|
560
607
|
static destroy(appName?: string, clientType?: HttpClientType): void;
|
|
608
|
+
/**
|
|
609
|
+
* Internal logger for warnings and errors.
|
|
610
|
+
* @returns A LoggerCore instance configured for the factory.
|
|
611
|
+
* @private
|
|
612
|
+
*/
|
|
613
|
+
private static SLogger;
|
|
561
614
|
}
|
|
562
615
|
|
|
563
616
|
/**
|
|
@@ -581,17 +634,4 @@ interface CacheEntry<T = any> {
|
|
|
581
634
|
lastAccessedAt: number;
|
|
582
635
|
}
|
|
583
636
|
|
|
584
|
-
|
|
585
|
-
* Default export object with all features
|
|
586
|
-
*/
|
|
587
|
-
declare const _default: {
|
|
588
|
-
SCacheMiddleware: typeof SCacheMiddleware;
|
|
589
|
-
CacheService: typeof CacheService;
|
|
590
|
-
CacheMiddleware: typeof CacheMiddleware;
|
|
591
|
-
CacheFactoryRepository: typeof CacheFactoryRepository;
|
|
592
|
-
RouteValidator: typeof RouteValidator;
|
|
593
|
-
EnvironmentLoader: typeof EnvironmentLoader;
|
|
594
|
-
HttpCacheFactory: typeof HttpCacheFactory;
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
export { type CacheEntry, type ICacheRepository, type IHttpCacheService, _default as default };
|
|
637
|
+
export { type CacheEntry, CacheFactoryRepository, CacheMiddleware, CacheService, EnvironmentLoader, HttpCacheFactory, type ICacheRepository, type IHttpCacheService, RouteValidator, SCacheMiddleware };
|