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