@unispechq/unispec-core 0.3.2 → 0.3.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/dist/cjs/src/cache/cache-factory.js +1 -1
- package/dist/cjs/src/cache/cache-manager.js +2 -2
- package/dist/cjs/src/cache/hash-utils.js +1 -1
- package/dist/cjs/src/cache/hashing.js +1 -1
- package/dist/cjs/src/cache/index.js +5 -5
- package/dist/cjs/src/cache/lru-cache.js +1 -1
- package/dist/cjs/src/diff/core.js +2 -2
- package/dist/cjs/src/diff/enhanced-diff.js +5 -5
- package/dist/cjs/src/diff/index.js +8 -8
- package/dist/cjs/src/errors/config-error.js +1 -1
- package/dist/cjs/src/errors/error-factory.js +7 -7
- package/dist/cjs/src/errors/index.js +8 -8
- package/dist/cjs/src/errors/loader-error.js +1 -1
- package/dist/cjs/src/errors/reference-error.js +1 -1
- package/dist/cjs/src/errors/schema-error.js +1 -1
- package/dist/cjs/src/errors/security-error.js +1 -1
- package/dist/cjs/src/errors/semantic-error.js +1 -1
- package/dist/cjs/src/index.js +13 -13
- package/dist/cjs/src/loader/index.js +4 -4
- package/dist/cjs/src/loader/security-validator.js +1 -1
- package/dist/cjs/src/loader/unispec-loader.js +3 -3
- package/dist/cjs/src/loader/yaml-loader.js +1 -1
- package/dist/cjs/src/normalizer/core.js +4 -4
- package/dist/cjs/src/normalizer/index.js +1 -1
- package/dist/cjs/src/optimizer/core.js +2 -2
- package/dist/cjs/src/optimizer/index.js +3 -3
- package/dist/cjs/src/optimizer/optimization-functions.js +2 -2
- package/dist/cjs/src/schemas/dedupe.js +1 -1
- package/dist/cjs/src/schemas/index.js +3 -3
- package/dist/cjs/src/schemas/resolver.js +1 -1
- package/dist/cjs/src/validator/ajv-validator.js +1 -1
- package/dist/cjs/src/validator/config-validator-main.js +4 -4
- package/dist/cjs/src/validator/config-validator.js +1 -1
- package/dist/cjs/src/validator/index.js +7 -7
- package/dist/cjs/src/validator/reference-validator.js +1 -1
- package/dist/cjs/src/validator/tests-validator.js +2 -2
- package/dist/cjs/src/validator/unispec-validator.js +5 -5
- package/dist/cjs/src/validator/validator-factory.js +1 -1
- package/dist/cjs/tests/cache/cache.test.js +4 -4
- package/dist/cjs/tests/cache/utils.js +1 -1
- package/dist/cjs/tests/diff/diff-annotators.test.js +2 -2
- package/dist/cjs/tests/diff/diff-comprehensive.test.js +2 -2
- package/dist/cjs/tests/diff/diff-extended.test.js +2 -2
- package/dist/cjs/tests/diff/diff.test.js +2 -2
- package/dist/cjs/tests/diff/utils.js +1 -1
- package/dist/cjs/tests/errors/errors-integration.test.js +3 -3
- package/dist/cjs/tests/errors/errors.test.js +2 -2
- package/dist/cjs/tests/errors/utils.js +1 -1
- package/dist/cjs/tests/loader/integration.test.js +2 -2
- package/dist/cjs/tests/loader/loader.test.js +2 -2
- package/dist/cjs/tests/normalizer/normalizer-comprehensive.test.js +2 -2
- package/dist/cjs/tests/normalizer/normalizer-invalid.test.js +2 -2
- package/dist/cjs/tests/normalizer/normalizer-valid.test.js +2 -2
- package/dist/cjs/tests/normalizer/utils.js +1 -1
- package/dist/cjs/tests/optimizer/compress-references.test.js +2 -2
- package/dist/cjs/tests/optimizer/deduplication.test.js +2 -2
- package/dist/cjs/tests/optimizer/integration.test.js +3 -3
- package/dist/cjs/tests/optimizer/optimization-report.test.js +2 -2
- package/dist/cjs/tests/optimizer/optimize-document.test.js +2 -2
- package/dist/cjs/tests/optimizer/orphaned-schemas.test.js +2 -2
- package/dist/cjs/tests/optimizer/sort-schemas.test.js +2 -2
- package/dist/cjs/tests/optimizer/utils.js +1 -1
- package/dist/cjs/tests/schemas/schemas-edge-cases.test.js +3 -3
- package/dist/cjs/tests/schemas/schemas.test.js +3 -3
- package/dist/cjs/tests/schemas/utils.js +1 -1
- package/dist/cjs/tests/validator/config-validator.test.js +2 -2
- package/dist/cjs/tests/validator/unispec-validator.test.js +2 -2
- package/dist/cjs/tests/validator/utils.js +1 -1
- package/package.json +1 -1
- package/dist/cache/cache-factory.d.ts +0 -31
- package/dist/cache/cache-factory.js +0 -65
- package/dist/cache/cache-manager.d.ts +0 -62
- package/dist/cache/cache-manager.js +0 -122
- package/dist/cache/constants.d.ts +0 -21
- package/dist/cache/constants.js +0 -22
- package/dist/cache/hash-utils.d.ts +0 -21
- package/dist/cache/hash-utils.js +0 -35
- package/dist/cache/hashing.d.ts +0 -19
- package/dist/cache/hashing.js +0 -197
- package/dist/cache/index.d.ts +0 -6
- package/dist/cache/index.js +0 -10
- package/dist/cache/lru-cache.d.ts +0 -56
- package/dist/cache/lru-cache.js +0 -161
- package/dist/cache/types.d.ts +0 -24
- package/dist/cache/types.js +0 -4
- package/dist/cjs/cache/cache-factory.js +0 -72
- package/dist/cjs/cache/cache-manager.js +0 -126
- package/dist/cjs/cache/constants.js +0 -25
- package/dist/cjs/cache/hash-utils.js +0 -41
- package/dist/cjs/cache/hashing.js +0 -236
- package/dist/cjs/cache/index.js +0 -26
- package/dist/cjs/cache/lru-cache.js +0 -165
- package/dist/cjs/cache/types.js +0 -5
- package/dist/cjs/diff/annotators.js +0 -159
- package/dist/cjs/diff/change-reports.js +0 -369
- package/dist/cjs/diff/core.js +0 -158
- package/dist/cjs/diff/enhanced-diff.js +0 -79
- package/dist/cjs/diff/impact-strategies-refactored.js +0 -230
- package/dist/cjs/diff/impact-strategies.js +0 -219
- package/dist/cjs/diff/index.js +0 -27
- package/dist/cjs/diff/metrics-calculator.js +0 -69
- package/dist/cjs/diff/risk-calculator.js +0 -58
- package/dist/cjs/diff/suggestion-generator.js +0 -78
- package/dist/cjs/diff/types.js +0 -11
- package/dist/cjs/errors/base-error.js +0 -33
- package/dist/cjs/errors/config-error.js +0 -11
- package/dist/cjs/errors/error-factory.js +0 -48
- package/dist/cjs/errors/index.js +0 -19
- package/dist/cjs/errors/loader-error.js +0 -11
- package/dist/cjs/errors/reference-error.js +0 -11
- package/dist/cjs/errors/schema-error.js +0 -11
- package/dist/cjs/errors/security-error.js +0 -11
- package/dist/cjs/errors/semantic-error.js +0 -11
- package/dist/cjs/generated-schemas.js +0 -2100
- package/dist/cjs/index.js +0 -59
- package/dist/cjs/loader/index.js +0 -13
- package/dist/cjs/loader/security-validator.js +0 -53
- package/dist/cjs/loader/types.js +0 -11
- package/dist/cjs/loader/unispec-loader.js +0 -84
- package/dist/cjs/loader/yaml-loader.js +0 -76
- package/dist/cjs/normalizer/core.js +0 -32
- package/dist/cjs/normalizer/graphql-normalizer.js +0 -67
- package/dist/cjs/normalizer/index.js +0 -7
- package/dist/cjs/normalizer/rest-normalizer.js +0 -51
- package/dist/cjs/normalizer/types.js +0 -2
- package/dist/cjs/normalizer/utils.js +0 -33
- package/dist/cjs/normalizer/websocket-normalizer.js +0 -81
- package/dist/cjs/optimizer/core.js +0 -115
- package/dist/cjs/optimizer/index.js +0 -17
- package/dist/cjs/optimizer/optimization-functions.js +0 -185
- package/dist/cjs/optimizer/types.js +0 -2
- package/dist/cjs/optimizer/utils.js +0 -32
- package/dist/cjs/schemas/dedupe.js +0 -100
- package/dist/cjs/schemas/index.js +0 -14
- package/dist/cjs/schemas/resolver.js +0 -41
- package/dist/cjs/schemas/utils.js +0 -53
- package/dist/cjs/types/index.js +0 -2
- package/dist/cjs/validator/ajv-validator.js +0 -82
- package/dist/cjs/validator/config-validator-main.js +0 -34
- package/dist/cjs/validator/config-validator.js +0 -17
- package/dist/cjs/validator/index.js +0 -23
- package/dist/cjs/validator/object-traversal.js +0 -112
- package/dist/cjs/validator/reference-validator.js +0 -233
- package/dist/cjs/validator/schema-references.js +0 -116
- package/dist/cjs/validator/semantic-validator.js +0 -328
- package/dist/cjs/validator/tests-validator.js +0 -16
- package/dist/cjs/validator/types.js +0 -2
- package/dist/cjs/validator/unispec-validator.js +0 -84
- package/dist/cjs/validator/validator-factory.js +0 -77
- package/dist/cjs/versions.js +0 -147
- package/dist/diff/annotators.d.ts +0 -4
- package/dist/diff/annotators.js +0 -154
- package/dist/diff/change-reports.d.ts +0 -37
- package/dist/diff/change-reports.js +0 -366
- package/dist/diff/core.d.ts +0 -26
- package/dist/diff/core.js +0 -155
- package/dist/diff/enhanced-diff.d.ts +0 -51
- package/dist/diff/enhanced-diff.js +0 -76
- package/dist/diff/impact-strategies-refactored.d.ts +0 -69
- package/dist/diff/impact-strategies-refactored.js +0 -223
- package/dist/diff/impact-strategies.d.ts +0 -41
- package/dist/diff/impact-strategies.js +0 -212
- package/dist/diff/index.d.ts +0 -8
- package/dist/diff/index.js +0 -11
- package/dist/diff/metrics-calculator.d.ts +0 -23
- package/dist/diff/metrics-calculator.js +0 -65
- package/dist/diff/risk-calculator.d.ts +0 -23
- package/dist/diff/risk-calculator.js +0 -55
- package/dist/diff/suggestion-generator.d.ts +0 -18
- package/dist/diff/suggestion-generator.js +0 -74
- package/dist/diff/types.d.ts +0 -24
- package/dist/diff/types.js +0 -8
- package/dist/errors/base-error.d.ts +0 -20
- package/dist/errors/base-error.js +0 -29
- package/dist/errors/config-error.d.ts +0 -4
- package/dist/errors/config-error.js +0 -7
- package/dist/errors/error-factory.d.ts +0 -22
- package/dist/errors/error-factory.js +0 -45
- package/dist/errors/index.d.ts +0 -8
- package/dist/errors/index.js +0 -8
- package/dist/errors/loader-error.d.ts +0 -4
- package/dist/errors/loader-error.js +0 -7
- package/dist/errors/reference-error.d.ts +0 -4
- package/dist/errors/reference-error.js +0 -7
- package/dist/errors/schema-error.d.ts +0 -4
- package/dist/errors/schema-error.js +0 -7
- package/dist/errors/security-error.d.ts +0 -4
- package/dist/errors/security-error.js +0 -7
- package/dist/errors/semantic-error.d.ts +0 -4
- package/dist/errors/semantic-error.js +0 -7
- package/dist/generated-schemas.d.ts +0 -2073
- package/dist/generated-schemas.js +0 -2097
- package/dist/index.cjs +0 -59
- package/dist/index.d.ts +0 -13
- package/dist/index.js +0 -43
- package/dist/loader/index.d.ts +0 -5
- package/dist/loader/index.js +0 -5
- package/dist/loader/security-validator.d.ts +0 -5
- package/dist/loader/security-validator.js +0 -50
- package/dist/loader/types.d.ts +0 -30
- package/dist/loader/types.js +0 -8
- package/dist/loader/unispec-loader.d.ts +0 -10
- package/dist/loader/unispec-loader.js +0 -81
- package/dist/loader/yaml-loader.d.ts +0 -10
- package/dist/loader/yaml-loader.js +0 -39
- package/dist/normalizer/core.d.ts +0 -24
- package/dist/normalizer/core.js +0 -29
- package/dist/normalizer/graphql-normalizer.d.ts +0 -8
- package/dist/normalizer/graphql-normalizer.js +0 -64
- package/dist/normalizer/index.d.ts +0 -2
- package/dist/normalizer/index.js +0 -3
- package/dist/normalizer/rest-normalizer.d.ts +0 -8
- package/dist/normalizer/rest-normalizer.js +0 -48
- package/dist/normalizer/types.d.ts +0 -7
- package/dist/normalizer/types.js +0 -1
- package/dist/normalizer/utils.d.ts +0 -14
- package/dist/normalizer/utils.js +0 -29
- package/dist/normalizer/websocket-normalizer.d.ts +0 -8
- package/dist/normalizer/websocket-normalizer.js +0 -78
- package/dist/optimizer/core.d.ts +0 -17
- package/dist/optimizer/core.js +0 -111
- package/dist/optimizer/index.d.ts +0 -4
- package/dist/optimizer/index.js +0 -7
- package/dist/optimizer/optimization-functions.d.ts +0 -32
- package/dist/optimizer/optimization-functions.js +0 -179
- package/dist/optimizer/types.d.ts +0 -28
- package/dist/optimizer/types.js +0 -1
- package/dist/optimizer/utils.d.ts +0 -7
- package/dist/optimizer/utils.js +0 -29
- package/dist/schemas/dedupe.d.ts +0 -9
- package/dist/schemas/dedupe.js +0 -97
- package/dist/schemas/index.d.ts +0 -3
- package/dist/schemas/index.js +0 -6
- package/dist/schemas/resolver.d.ts +0 -19
- package/dist/schemas/resolver.js +0 -37
- package/dist/schemas/utils.d.ts +0 -20
- package/dist/schemas/utils.js +0 -49
- package/dist/types/index.d.ts +0 -433
- package/dist/types/index.js +0 -1
- package/dist/validator/ajv-validator.d.ts +0 -15
- package/dist/validator/ajv-validator.js +0 -75
- package/dist/validator/config-validator-main.d.ts +0 -10
- package/dist/validator/config-validator-main.js +0 -31
- package/dist/validator/config-validator.d.ts +0 -5
- package/dist/validator/config-validator.js +0 -14
- package/dist/validator/index.d.ts +0 -10
- package/dist/validator/index.js +0 -11
- package/dist/validator/object-traversal.d.ts +0 -52
- package/dist/validator/object-traversal.js +0 -104
- package/dist/validator/reference-validator.d.ts +0 -31
- package/dist/validator/reference-validator.js +0 -230
- package/dist/validator/schema-references.d.ts +0 -23
- package/dist/validator/schema-references.js +0 -111
- package/dist/validator/semantic-validator.d.ts +0 -26
- package/dist/validator/semantic-validator.js +0 -325
- package/dist/validator/tests-validator.d.ts +0 -9
- package/dist/validator/tests-validator.js +0 -13
- package/dist/validator/types.d.ts +0 -29
- package/dist/validator/types.js +0 -1
- package/dist/validator/unispec-validator.d.ts +0 -15
- package/dist/validator/unispec-validator.js +0 -81
- package/dist/validator/validator-factory.d.ts +0 -10
- package/dist/validator/validator-factory.js +0 -73
- package/dist/versions.d.ts +0 -10
- package/dist/versions.js +0 -143
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UniSpecCacheManager = void 0;
|
|
4
|
-
const constants_1 = require("./constants.js");
|
|
5
|
-
const lru_cache_1 = require("./lru-cache.js");
|
|
6
|
-
/**
|
|
7
|
-
* Cache manager for UniSpec operations.
|
|
8
|
-
*/
|
|
9
|
-
class UniSpecCacheManager {
|
|
10
|
-
constructor(options = {}) {
|
|
11
|
-
this.cleanupInterval = null;
|
|
12
|
-
this.validationCache = new lru_cache_1.LRUCache({
|
|
13
|
-
...options,
|
|
14
|
-
maxSize: constants_1.CACHE_CONSTANTS.VALIDATION_MAX_SIZE,
|
|
15
|
-
});
|
|
16
|
-
this.normalizationCache = new lru_cache_1.LRUCache({
|
|
17
|
-
...options,
|
|
18
|
-
maxSize: constants_1.CACHE_CONSTANTS.NORMALIZATION_MAX_SIZE,
|
|
19
|
-
});
|
|
20
|
-
this.diffCache = new lru_cache_1.LRUCache({
|
|
21
|
-
...options,
|
|
22
|
-
maxSize: constants_1.CACHE_CONSTANTS.DIFF_MAX_SIZE,
|
|
23
|
-
});
|
|
24
|
-
// Start cleanup interval
|
|
25
|
-
this.startCleanup();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Get cached validation result asynchronously.
|
|
29
|
-
*/
|
|
30
|
-
async getValidationResult(documentHash) {
|
|
31
|
-
return await this.validationCache.get(documentHash);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Cache validation result asynchronously.
|
|
35
|
-
*/
|
|
36
|
-
async setValidationResult(documentHash, result) {
|
|
37
|
-
await this.validationCache.set(documentHash, result);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Get cached normalization result asynchronously.
|
|
41
|
-
*/
|
|
42
|
-
async getNormalizationResult(documentHash) {
|
|
43
|
-
return await this.normalizationCache.get(documentHash);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Cache normalization result asynchronously.
|
|
47
|
-
*/
|
|
48
|
-
async setNormalizationResult(documentHash, result) {
|
|
49
|
-
await this.normalizationCache.set(documentHash, result);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Get cached diff result asynchronously.
|
|
53
|
-
*/
|
|
54
|
-
async getDiffResult(docsHash) {
|
|
55
|
-
return await this.diffCache.get(docsHash);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Cache diff result asynchronously.
|
|
59
|
-
*/
|
|
60
|
-
async setDiffResult(docsHash, result) {
|
|
61
|
-
await this.diffCache.set(docsHash, result);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Get comprehensive cache statistics.
|
|
65
|
-
*/
|
|
66
|
-
getStats() {
|
|
67
|
-
const validationStats = this.validationCache.getStats();
|
|
68
|
-
const normalizationStats = this.normalizationCache.getStats();
|
|
69
|
-
const diffStats = this.diffCache.getStats();
|
|
70
|
-
const total = {
|
|
71
|
-
size: validationStats.size + normalizationStats.size + diffStats.size,
|
|
72
|
-
hits: validationStats.hits + normalizationStats.hits + diffStats.hits,
|
|
73
|
-
misses: validationStats.misses + normalizationStats.misses + diffStats.misses,
|
|
74
|
-
hitRate: 0,
|
|
75
|
-
};
|
|
76
|
-
const totalRequests = total.hits + total.misses;
|
|
77
|
-
total.hitRate = totalRequests > 0 ? total.hits / totalRequests : 0;
|
|
78
|
-
return {
|
|
79
|
-
validation: validationStats,
|
|
80
|
-
normalization: normalizationStats,
|
|
81
|
-
diff: diffStats,
|
|
82
|
-
total,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Clear all caches.
|
|
87
|
-
*/
|
|
88
|
-
clearAll() {
|
|
89
|
-
this.validationCache.clear();
|
|
90
|
-
this.normalizationCache.clear();
|
|
91
|
-
this.diffCache.clear();
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Start automatic cleanup interval.
|
|
95
|
-
*/
|
|
96
|
-
startCleanup() {
|
|
97
|
-
try {
|
|
98
|
-
this.cleanupInterval = setInterval(() => {
|
|
99
|
-
try {
|
|
100
|
-
this.validationCache.cleanup();
|
|
101
|
-
this.normalizationCache.cleanup();
|
|
102
|
-
this.diffCache.cleanup();
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
console.error(constants_1.CACHE_MESSAGES.CLEANUP_ERROR, error);
|
|
106
|
-
// Continue running cleanup even if one cache fails
|
|
107
|
-
}
|
|
108
|
-
}, constants_1.CACHE_CONSTANTS.CLEANUP_INTERVAL);
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
console.error(constants_1.CACHE_MESSAGES.CLEANUP_START_ERROR, error);
|
|
112
|
-
// Cache will still work without automatic cleanup
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Stop automatic cleanup.
|
|
117
|
-
*/
|
|
118
|
-
destroy() {
|
|
119
|
-
if (this.cleanupInterval) {
|
|
120
|
-
clearInterval(this.cleanupInterval);
|
|
121
|
-
this.cleanupInterval = null;
|
|
122
|
-
}
|
|
123
|
-
this.clearAll();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
exports.UniSpecCacheManager = UniSpecCacheManager;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Cache configuration constants.
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.CACHE_MESSAGES = exports.CACHE_CONSTANTS = void 0;
|
|
7
|
-
exports.CACHE_CONSTANTS = {
|
|
8
|
-
// Default cache sizes
|
|
9
|
-
DEFAULT_MAX_SIZE: 100,
|
|
10
|
-
VALIDATION_MAX_SIZE: 50,
|
|
11
|
-
NORMALIZATION_MAX_SIZE: 30,
|
|
12
|
-
DIFF_MAX_SIZE: 20,
|
|
13
|
-
// TTL values (in milliseconds)
|
|
14
|
-
DEFAULT_TTL: 5 * 60 * 1000, // 5 minutes
|
|
15
|
-
CLEANUP_INTERVAL: 60000, // 1 minute
|
|
16
|
-
// Hash configuration
|
|
17
|
-
HASH_GOLDEN_RATIO: 2654435761,
|
|
18
|
-
HASH_BASE: 31,
|
|
19
|
-
HASH_MASK: 0xffffffff,
|
|
20
|
-
};
|
|
21
|
-
exports.CACHE_MESSAGES = {
|
|
22
|
-
CLEANUP_ERROR: "Cache cleanup error",
|
|
23
|
-
CLEANUP_START_ERROR: "Failed to start cleanup interval",
|
|
24
|
-
CACHE_WILL_CONTINUE: "Cache will still work without automatic cleanup",
|
|
25
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateDocumentHash = generateDocumentHash;
|
|
4
|
-
exports.generateDocumentHashSync = generateDocumentHashSync;
|
|
5
|
-
exports.generateDiffHash = generateDiffHash;
|
|
6
|
-
exports.generateDiffHashSync = generateDiffHashSync;
|
|
7
|
-
const hashing_1 = require("./hashing.js");
|
|
8
|
-
/**
|
|
9
|
-
* Generate a hash for a UniSpec document for caching purposes.
|
|
10
|
-
* Uses optimized hashing for better performance and collision resistance.
|
|
11
|
-
*/
|
|
12
|
-
async function generateDocumentHash(document) {
|
|
13
|
-
return await (0, hashing_1.generateOptimizedDocumentHash)(document);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Synchronous version for backward compatibility (deprecated).
|
|
17
|
-
* @deprecated Use async version for better security
|
|
18
|
-
*/
|
|
19
|
-
function generateDocumentHashSync(document) {
|
|
20
|
-
// Fallback to old method for backward compatibility
|
|
21
|
-
console.warn("generateDocumentHashSync is deprecated. Use async generateDocumentHash for better security.");
|
|
22
|
-
const str = JSON.stringify(document, Object.keys(document).sort());
|
|
23
|
-
return (0, hashing_1.simpleHash)(str);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Generate a hash for two documents (for diff caching).
|
|
27
|
-
* Uses optimized diff hashing with better collision resistance.
|
|
28
|
-
*/
|
|
29
|
-
async function generateDiffHash(oldDoc, newDoc) {
|
|
30
|
-
return await (0, hashing_1.generateOptimizedDiffHash)(oldDoc, newDoc);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Synchronous version for backward compatibility (deprecated).
|
|
34
|
-
* @deprecated Use async version for better security
|
|
35
|
-
*/
|
|
36
|
-
function generateDiffHashSync(oldDoc, newDoc) {
|
|
37
|
-
console.warn("generateDiffHashSync is deprecated. Use async generateDiffHash for better security.");
|
|
38
|
-
const oldStr = JSON.stringify(oldDoc, Object.keys(oldDoc).sort());
|
|
39
|
-
const newStr = JSON.stringify(newDoc, Object.keys(newDoc).sort());
|
|
40
|
-
return (0, hashing_1.simpleHash)(`${oldStr}:${newStr}`);
|
|
41
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.generateOptimizedDocumentHash = generateOptimizedDocumentHash;
|
|
37
|
-
exports.generateOptimizedDiffHash = generateOptimizedDiffHash;
|
|
38
|
-
exports.simpleHash = simpleHash;
|
|
39
|
-
exports.fnv1aHash = fnv1aHash;
|
|
40
|
-
const constants_1 = require("../cache/constants.js");
|
|
41
|
-
/**
|
|
42
|
-
* Secure document hashing utilities using Web Crypto API.
|
|
43
|
-
*/
|
|
44
|
-
/**
|
|
45
|
-
* Secure hash using Web Crypto API (SHA-256) or Node.js crypto.
|
|
46
|
-
* Fallback to FNV-1a only if both crypto APIs are unavailable.
|
|
47
|
-
*/
|
|
48
|
-
async function secureHash(str) {
|
|
49
|
-
// Try Web Crypto API first (browser environments)
|
|
50
|
-
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
51
|
-
try {
|
|
52
|
-
const encoder = new TextEncoder();
|
|
53
|
-
const data = encoder.encode(str);
|
|
54
|
-
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
55
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
56
|
-
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
console.warn("Web Crypto API failed, trying Node.js crypto:", error);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
// Try Node.js crypto API (server environments)
|
|
63
|
-
try {
|
|
64
|
-
// Dynamic import for Node.js crypto to avoid browser compatibility issues
|
|
65
|
-
const crypto = await Promise.resolve().then(() => __importStar(require("node:crypto")));
|
|
66
|
-
const hash = crypto.createHash("sha256");
|
|
67
|
-
hash.update(str, "utf8");
|
|
68
|
-
return hash.digest("hex");
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
console.warn("Node.js crypto API failed, falling back to FNV-1a:", error);
|
|
72
|
-
}
|
|
73
|
-
// Last resort: FNV-1a algorithm (non-cryptographic, for compatibility only)
|
|
74
|
-
console.warn("Using FNV-1a fallback hash - consider installing crypto support");
|
|
75
|
-
return fnv1aHashFallback(str).toString();
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* FNV-1a fallback hash function (non-cryptographic, for compatibility).
|
|
79
|
-
*/
|
|
80
|
-
function fnv1aHashFallback(str) {
|
|
81
|
-
let hash = 2166136261; // FNV offset basis
|
|
82
|
-
for (let i = 0; i < str.length; i++) {
|
|
83
|
-
hash ^= str.charCodeAt(i);
|
|
84
|
-
hash = (hash * 16777619) & constants_1.CACHE_CONSTANTS.HASH_MASK; // FNV prime
|
|
85
|
-
}
|
|
86
|
-
return hash;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Generate a secure hash for a document using optimized sorting and Web Crypto API.
|
|
90
|
-
*/
|
|
91
|
-
async function generateOptimizedDocumentHash(document) {
|
|
92
|
-
// Use optimized stable stringification
|
|
93
|
-
const str = stableStringify(document);
|
|
94
|
-
// Use secure hash for better distribution and collision resistance
|
|
95
|
-
const hash = await secureHash(str);
|
|
96
|
-
// Add length and type prefix for additional collision resistance
|
|
97
|
-
const typePrefix = getDocumentTypePrefix(document);
|
|
98
|
-
const combined = `${hash}_${str.length}_${typePrefix}`;
|
|
99
|
-
// Create final hash with combined string
|
|
100
|
-
const finalHash = await secureHash(combined);
|
|
101
|
-
return `${finalHash.slice(0, 16)}_${str.length}`;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Default configuration for stable stringification.
|
|
105
|
-
*/
|
|
106
|
-
const DEFAULT_STRINGIFY_CONFIG = {
|
|
107
|
-
maxDepth: 100,
|
|
108
|
-
maxKeys: 10000,
|
|
109
|
-
handleCircular: true,
|
|
110
|
-
handleDeepObjects: true,
|
|
111
|
-
};
|
|
112
|
-
/**
|
|
113
|
-
* Optimized stable stringification that avoids recursion depth issues.
|
|
114
|
-
*/
|
|
115
|
-
function stableStringify(obj, config = {}, depth = 0, visited = new WeakSet()) {
|
|
116
|
-
const finalConfig = { ...DEFAULT_STRINGIFY_CONFIG, ...config };
|
|
117
|
-
// Prevent infinite recursion
|
|
118
|
-
if (depth > finalConfig.maxDepth) {
|
|
119
|
-
return `"[MAX_DEPTH_REACHED:${depth}]"`;
|
|
120
|
-
}
|
|
121
|
-
// Handle circular references
|
|
122
|
-
if (finalConfig.handleCircular && obj && typeof obj === "object") {
|
|
123
|
-
if (visited.has(obj)) {
|
|
124
|
-
return `"[CIRCULAR_REFERENCE:${obj.constructor?.name || "Object"}]"`;
|
|
125
|
-
}
|
|
126
|
-
visited.add(obj);
|
|
127
|
-
}
|
|
128
|
-
// Handle primitives
|
|
129
|
-
if (obj === null || typeof obj !== "object") {
|
|
130
|
-
return JSON.stringify(obj);
|
|
131
|
-
}
|
|
132
|
-
// Handle special object types
|
|
133
|
-
if (obj instanceof Date) {
|
|
134
|
-
return obj.toISOString();
|
|
135
|
-
}
|
|
136
|
-
if (obj instanceof RegExp) {
|
|
137
|
-
return `"[REGEX:${obj.toString()}]"`;
|
|
138
|
-
}
|
|
139
|
-
if (obj instanceof Error) {
|
|
140
|
-
return `"[ERROR:${obj.name}:${obj.message}]"`;
|
|
141
|
-
}
|
|
142
|
-
if (typeof Buffer !== "undefined" && Buffer.isBuffer(obj)) {
|
|
143
|
-
return `"[BUFFER:${obj.toString("base64")}]"`;
|
|
144
|
-
}
|
|
145
|
-
// Handle Array
|
|
146
|
-
if (Array.isArray(obj)) {
|
|
147
|
-
if (obj.length > finalConfig.maxKeys) {
|
|
148
|
-
return `"[ARRAY_TOO_LARGE:${obj.length}]"`;
|
|
149
|
-
}
|
|
150
|
-
const elements = obj.map((item, index) => {
|
|
151
|
-
try {
|
|
152
|
-
return stableStringify(item, finalConfig, depth + 1, visited);
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
return `"[ERROR_AT_INDEX_${index}:${error instanceof Error ? error.message : String(error)}]"`;
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
return `[${elements.join(",")}]`;
|
|
159
|
-
}
|
|
160
|
-
// Handle Object
|
|
161
|
-
if (typeof obj === "object") {
|
|
162
|
-
const objRecord = obj;
|
|
163
|
-
const keys = Object.keys(objRecord);
|
|
164
|
-
if (keys.length > finalConfig.maxKeys) {
|
|
165
|
-
return `"[OBJECT_TOO_LARGE:${keys.length}]"`;
|
|
166
|
-
}
|
|
167
|
-
const sortedKeys = keys.sort();
|
|
168
|
-
const pairs = [];
|
|
169
|
-
for (const key of sortedKeys) {
|
|
170
|
-
try {
|
|
171
|
-
const value = stableStringify(objRecord[key], finalConfig, depth + 1, visited);
|
|
172
|
-
pairs.push(`"${key}":${value}`);
|
|
173
|
-
}
|
|
174
|
-
catch (error) {
|
|
175
|
-
pairs.push(`"${key}":"[ERROR:${error instanceof Error ? error.message : String(error)}]"`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return `{${pairs.join(",")}}`;
|
|
179
|
-
}
|
|
180
|
-
return "{}";
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Get a numeric prefix based on document type to reduce collisions.
|
|
184
|
-
*/
|
|
185
|
-
function getDocumentTypePrefix(document) {
|
|
186
|
-
if (!document || typeof document !== "object")
|
|
187
|
-
return 0;
|
|
188
|
-
const doc = document;
|
|
189
|
-
// Check for UniSpec document
|
|
190
|
-
if (doc.unispecVersion)
|
|
191
|
-
return 1;
|
|
192
|
-
if (doc.uniSpecTestsVersion)
|
|
193
|
-
return 2;
|
|
194
|
-
if (doc.version && doc.services)
|
|
195
|
-
return 3;
|
|
196
|
-
// Check for common patterns
|
|
197
|
-
if (Array.isArray(document))
|
|
198
|
-
return 4;
|
|
199
|
-
if (doc.$schema)
|
|
200
|
-
return 5;
|
|
201
|
-
if (doc.type)
|
|
202
|
-
return typeof doc.type === "string" ? doc.type.charCodeAt(0) || 6 : 6;
|
|
203
|
-
return 7;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Generate a secure hash for two documents (for diff caching) with optimized collision resistance.
|
|
207
|
-
*/
|
|
208
|
-
async function generateOptimizedDiffHash(oldDoc, newDoc) {
|
|
209
|
-
const oldHash = await generateOptimizedDocumentHash(oldDoc);
|
|
210
|
-
const newHash = await generateOptimizedDocumentHash(newDoc);
|
|
211
|
-
// Combine hashes using order-sensitive operation
|
|
212
|
-
const combined = `${oldHash}:${newHash}`;
|
|
213
|
-
// Hash the combined string to get final result
|
|
214
|
-
const finalHash = await secureHash(combined);
|
|
215
|
-
return `${finalHash.slice(0, 16)}_${oldHash.length}_${newHash.length}`;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Simple hash for small objects (keys, strings) using built-in hash.
|
|
219
|
-
* Kept for backward compatibility but consider using secureHash for new code.
|
|
220
|
-
*/
|
|
221
|
-
function simpleHash(str) {
|
|
222
|
-
let hash = 0;
|
|
223
|
-
for (let i = 0; i < str.length; i++) {
|
|
224
|
-
const char = str.charCodeAt(i);
|
|
225
|
-
hash = ((hash << 5) - hash + char) & constants_1.CACHE_CONSTANTS.HASH_MASK;
|
|
226
|
-
}
|
|
227
|
-
return hash.toString(36);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Legacy synchronous hash function (deprecated - use secureHash instead).
|
|
231
|
-
* @deprecated Use secureHash for better security
|
|
232
|
-
*/
|
|
233
|
-
function fnv1aHash(str) {
|
|
234
|
-
console.warn("fnv1aHash is deprecated. Use secureHash for better security.");
|
|
235
|
-
return fnv1aHashFallback(str);
|
|
236
|
-
}
|
package/dist/cjs/cache/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Constants and messages
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.LRUCache = exports.generateDocumentHashSync = exports.generateDocumentHash = exports.generateDiffHashSync = exports.generateDiffHash = exports.CACHE_MESSAGES = exports.CACHE_CONSTANTS = exports.UniSpecCacheManager = exports.getManagersStats = exports.destroyManagers = exports.createNamedCacheManager = exports.createCacheManager = exports.clearTestRegistry = void 0;
|
|
5
|
-
// Cache factory
|
|
6
|
-
var cache_factory_1 = require("./cache-factory.js");
|
|
7
|
-
Object.defineProperty(exports, "clearTestRegistry", { enumerable: true, get: function () { return cache_factory_1.clearTestRegistry; } });
|
|
8
|
-
Object.defineProperty(exports, "createCacheManager", { enumerable: true, get: function () { return cache_factory_1.createCacheManager; } });
|
|
9
|
-
Object.defineProperty(exports, "createNamedCacheManager", { enumerable: true, get: function () { return cache_factory_1.createNamedCacheManager; } });
|
|
10
|
-
Object.defineProperty(exports, "destroyManagers", { enumerable: true, get: function () { return cache_factory_1.destroyManagers; } });
|
|
11
|
-
Object.defineProperty(exports, "getManagersStats", { enumerable: true, get: function () { return cache_factory_1.getManagersStats; } });
|
|
12
|
-
// Cache manager
|
|
13
|
-
var cache_manager_1 = require("./cache-manager.js");
|
|
14
|
-
Object.defineProperty(exports, "UniSpecCacheManager", { enumerable: true, get: function () { return cache_manager_1.UniSpecCacheManager; } });
|
|
15
|
-
var constants_1 = require("./constants.js");
|
|
16
|
-
Object.defineProperty(exports, "CACHE_CONSTANTS", { enumerable: true, get: function () { return constants_1.CACHE_CONSTANTS; } });
|
|
17
|
-
Object.defineProperty(exports, "CACHE_MESSAGES", { enumerable: true, get: function () { return constants_1.CACHE_MESSAGES; } });
|
|
18
|
-
// Hash utilities
|
|
19
|
-
var hash_utils_1 = require("./hash-utils.js");
|
|
20
|
-
Object.defineProperty(exports, "generateDiffHash", { enumerable: true, get: function () { return hash_utils_1.generateDiffHash; } });
|
|
21
|
-
Object.defineProperty(exports, "generateDiffHashSync", { enumerable: true, get: function () { return hash_utils_1.generateDiffHashSync; } });
|
|
22
|
-
Object.defineProperty(exports, "generateDocumentHash", { enumerable: true, get: function () { return hash_utils_1.generateDocumentHash; } });
|
|
23
|
-
Object.defineProperty(exports, "generateDocumentHashSync", { enumerable: true, get: function () { return hash_utils_1.generateDocumentHashSync; } });
|
|
24
|
-
// Core cache implementation
|
|
25
|
-
var lru_cache_1 = require("./lru-cache.js");
|
|
26
|
-
Object.defineProperty(exports, "LRUCache", { enumerable: true, get: function () { return lru_cache_1.LRUCache; } });
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LRUCache = void 0;
|
|
4
|
-
const constants_1 = require("./constants.js");
|
|
5
|
-
/**
|
|
6
|
-
* Simple LRU (Least Recently Used) cache implementation.
|
|
7
|
-
*/
|
|
8
|
-
class LRUCache {
|
|
9
|
-
constructor(options = {}) {
|
|
10
|
-
this.cache = new Map();
|
|
11
|
-
this.maxSize = options.maxSize || constants_1.CACHE_CONSTANTS.DEFAULT_MAX_SIZE;
|
|
12
|
-
this.ttl = options.ttl || constants_1.CACHE_CONSTANTS.DEFAULT_TTL;
|
|
13
|
-
this.enableStats = options.enableStats || false;
|
|
14
|
-
this.stats = {
|
|
15
|
-
hits: 0,
|
|
16
|
-
misses: 0,
|
|
17
|
-
size: 0,
|
|
18
|
-
hitRate: 0,
|
|
19
|
-
evictions: 0,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Get a value from the cache asynchronously.
|
|
24
|
-
* For performance, this uses sync operations wrapped in Promise for compatibility.
|
|
25
|
-
*/
|
|
26
|
-
async get(key) {
|
|
27
|
-
// Use synchronous operations for better performance
|
|
28
|
-
// Only wrap in Promise for async interface compatibility
|
|
29
|
-
return Promise.resolve(this.getSync(key));
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Set a value in the cache asynchronously.
|
|
33
|
-
* For performance, this uses sync operations wrapped in Promise for compatibility.
|
|
34
|
-
*/
|
|
35
|
-
async set(key, value) {
|
|
36
|
-
// Use synchronous operations for better performance
|
|
37
|
-
// Only wrap in Promise for async interface compatibility
|
|
38
|
-
this.setSync(key, value);
|
|
39
|
-
return Promise.resolve();
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Get a value from the cache (synchronous version for backward compatibility).
|
|
43
|
-
* @deprecated Use async get() method instead
|
|
44
|
-
*/
|
|
45
|
-
getSync(key) {
|
|
46
|
-
const entry = this.cache.get(key);
|
|
47
|
-
if (!entry) {
|
|
48
|
-
if (this.enableStats) {
|
|
49
|
-
this.stats.misses++;
|
|
50
|
-
this.updateHitRate();
|
|
51
|
-
}
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
// Check TTL
|
|
55
|
-
if (this.isExpired(entry)) {
|
|
56
|
-
this.cache.delete(key);
|
|
57
|
-
if (this.enableStats) {
|
|
58
|
-
this.stats.misses++;
|
|
59
|
-
this.stats.size--;
|
|
60
|
-
this.updateHitRate();
|
|
61
|
-
}
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
// Update access info
|
|
65
|
-
entry.accessCount++;
|
|
66
|
-
entry.lastAccessed = Date.now();
|
|
67
|
-
// Move to end (most recently used)
|
|
68
|
-
this.cache.delete(key);
|
|
69
|
-
this.cache.set(key, entry);
|
|
70
|
-
if (this.enableStats) {
|
|
71
|
-
this.stats.hits++;
|
|
72
|
-
this.updateHitRate();
|
|
73
|
-
}
|
|
74
|
-
return entry.value;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Set a value in the cache (synchronous version for backward compatibility).
|
|
78
|
-
* @deprecated Use async set() method instead
|
|
79
|
-
*/
|
|
80
|
-
setSync(key, value) {
|
|
81
|
-
// Remove existing entry if present
|
|
82
|
-
if (this.cache.has(key)) {
|
|
83
|
-
this.cache.delete(key);
|
|
84
|
-
}
|
|
85
|
-
// Evict oldest entries if cache is full
|
|
86
|
-
while (this.cache.size >= this.maxSize) {
|
|
87
|
-
const oldestKey = this.cache.keys().next().value;
|
|
88
|
-
if (oldestKey) {
|
|
89
|
-
this.cache.delete(oldestKey);
|
|
90
|
-
if (this.enableStats) {
|
|
91
|
-
this.stats.evictions++;
|
|
92
|
-
this.stats.size--;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const entry = {
|
|
100
|
-
value,
|
|
101
|
-
timestamp: Date.now(),
|
|
102
|
-
accessCount: 1,
|
|
103
|
-
lastAccessed: Date.now(),
|
|
104
|
-
};
|
|
105
|
-
this.cache.set(key, entry);
|
|
106
|
-
if (this.enableStats) {
|
|
107
|
-
this.stats.size++;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Delete a value from the cache.
|
|
112
|
-
*/
|
|
113
|
-
delete(key) {
|
|
114
|
-
const deleted = this.cache.delete(key);
|
|
115
|
-
if (deleted && this.enableStats) {
|
|
116
|
-
this.stats.size--;
|
|
117
|
-
}
|
|
118
|
-
return deleted;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Clear all entries from the cache.
|
|
122
|
-
*/
|
|
123
|
-
clear() {
|
|
124
|
-
this.cache.clear();
|
|
125
|
-
if (this.enableStats) {
|
|
126
|
-
this.stats.size = 0;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Get cache statistics.
|
|
131
|
-
*/
|
|
132
|
-
getStats() {
|
|
133
|
-
return { ...this.stats };
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Check if an entry is expired.
|
|
137
|
-
*/
|
|
138
|
-
isExpired(entry) {
|
|
139
|
-
return Date.now() - entry.timestamp > this.ttl;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Update hit rate statistics.
|
|
143
|
-
*/
|
|
144
|
-
updateHitRate() {
|
|
145
|
-
const total = this.stats.hits + this.stats.misses;
|
|
146
|
-
this.stats.hitRate = total > 0 ? this.stats.hits / total : 0;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Clean up expired entries.
|
|
150
|
-
*/
|
|
151
|
-
cleanup() {
|
|
152
|
-
let cleaned = 0;
|
|
153
|
-
for (const [key, entry] of this.cache.entries()) {
|
|
154
|
-
if (this.isExpired(entry)) {
|
|
155
|
-
this.cache.delete(key);
|
|
156
|
-
cleaned++;
|
|
157
|
-
if (this.enableStats) {
|
|
158
|
-
this.stats.size--;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return cleaned;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
exports.LRUCache = LRUCache;
|