@blokjs/runner 0.2.0

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 (307) hide show
  1. package/dist/Blok.d.ts +19 -0
  2. package/dist/Blok.js +184 -0
  3. package/dist/Blok.js.map +1 -0
  4. package/dist/BlokResponse.d.ts +16 -0
  5. package/dist/BlokResponse.js +28 -0
  6. package/dist/BlokResponse.js.map +1 -0
  7. package/dist/Configuration.d.ts +37 -0
  8. package/dist/Configuration.js +248 -0
  9. package/dist/Configuration.js.map +1 -0
  10. package/dist/ConfigurationResolver.d.ts +7 -0
  11. package/dist/ConfigurationResolver.js +15 -0
  12. package/dist/ConfigurationResolver.js.map +1 -0
  13. package/dist/DefaultLogger.d.ts +65 -0
  14. package/dist/DefaultLogger.js +101 -0
  15. package/dist/DefaultLogger.js.map +1 -0
  16. package/dist/LocalStorage.d.ts +7 -0
  17. package/dist/LocalStorage.js +56 -0
  18. package/dist/LocalStorage.js.map +1 -0
  19. package/dist/MemoryUsage.d.ts +22 -0
  20. package/dist/MemoryUsage.js +83 -0
  21. package/dist/MemoryUsage.js.map +1 -0
  22. package/dist/NodeMap.d.ts +7 -0
  23. package/dist/NodeMap.js +13 -0
  24. package/dist/NodeMap.js.map +1 -0
  25. package/dist/ResolverBase.d.ts +8 -0
  26. package/dist/ResolverBase.js +18 -0
  27. package/dist/ResolverBase.js.map +1 -0
  28. package/dist/Runner.d.ts +25 -0
  29. package/dist/Runner.js +32 -0
  30. package/dist/Runner.js.map +1 -0
  31. package/dist/RunnerNode.d.ts +9 -0
  32. package/dist/RunnerNode.js +8 -0
  33. package/dist/RunnerNode.js.map +1 -0
  34. package/dist/RunnerNodeBase.d.ts +4 -0
  35. package/dist/RunnerNodeBase.js +3 -0
  36. package/dist/RunnerNodeBase.js.map +1 -0
  37. package/dist/RunnerSteps.d.ts +14 -0
  38. package/dist/RunnerSteps.js +110 -0
  39. package/dist/RunnerSteps.js.map +1 -0
  40. package/dist/RuntimeAdapterNode.d.ts +19 -0
  41. package/dist/RuntimeAdapterNode.js +87 -0
  42. package/dist/RuntimeAdapterNode.js.map +1 -0
  43. package/dist/RuntimeRegistry.d.ts +61 -0
  44. package/dist/RuntimeRegistry.js +87 -0
  45. package/dist/RuntimeRegistry.js.map +1 -0
  46. package/dist/TriggerBase.d.ts +119 -0
  47. package/dist/TriggerBase.js +413 -0
  48. package/dist/TriggerBase.js.map +1 -0
  49. package/dist/adapters/BunRuntimeAdapter.d.ts +38 -0
  50. package/dist/adapters/BunRuntimeAdapter.js +169 -0
  51. package/dist/adapters/BunRuntimeAdapter.js.map +1 -0
  52. package/dist/adapters/DockerRuntimeAdapter.d.ts +85 -0
  53. package/dist/adapters/DockerRuntimeAdapter.js +298 -0
  54. package/dist/adapters/DockerRuntimeAdapter.js.map +1 -0
  55. package/dist/adapters/HttpRuntimeAdapter.d.ts +58 -0
  56. package/dist/adapters/HttpRuntimeAdapter.js +152 -0
  57. package/dist/adapters/HttpRuntimeAdapter.js.map +1 -0
  58. package/dist/adapters/NodeJsRuntimeAdapter.d.ts +23 -0
  59. package/dist/adapters/NodeJsRuntimeAdapter.js +67 -0
  60. package/dist/adapters/NodeJsRuntimeAdapter.js.map +1 -0
  61. package/dist/adapters/RuntimeAdapter.d.ts +42 -0
  62. package/dist/adapters/RuntimeAdapter.js +2 -0
  63. package/dist/adapters/RuntimeAdapter.js.map +1 -0
  64. package/dist/adapters/WasmRuntimeAdapter.d.ts +69 -0
  65. package/dist/adapters/WasmRuntimeAdapter.js +279 -0
  66. package/dist/adapters/WasmRuntimeAdapter.js.map +1 -0
  67. package/dist/cache/NodeResultCache.d.ts +286 -0
  68. package/dist/cache/NodeResultCache.js +499 -0
  69. package/dist/cache/NodeResultCache.js.map +1 -0
  70. package/dist/cache/index.d.ts +1 -0
  71. package/dist/cache/index.js +2 -0
  72. package/dist/cache/index.js.map +1 -0
  73. package/dist/cost/CostEstimator.d.ts +57 -0
  74. package/dist/cost/CostEstimator.js +171 -0
  75. package/dist/cost/CostEstimator.js.map +1 -0
  76. package/dist/cost/index.d.ts +4 -0
  77. package/dist/cost/index.js +3 -0
  78. package/dist/cost/index.js.map +1 -0
  79. package/dist/cost/pricing.d.ts +24 -0
  80. package/dist/cost/pricing.js +169 -0
  81. package/dist/cost/pricing.js.map +1 -0
  82. package/dist/defineNode.d.ts +155 -0
  83. package/dist/defineNode.js +191 -0
  84. package/dist/defineNode.js.map +1 -0
  85. package/dist/graphql/GraphQLSchemaGenerator.d.ts +129 -0
  86. package/dist/graphql/GraphQLSchemaGenerator.js +425 -0
  87. package/dist/graphql/GraphQLSchemaGenerator.js.map +1 -0
  88. package/dist/hmr/FileWatcher.d.ts +62 -0
  89. package/dist/hmr/FileWatcher.js +185 -0
  90. package/dist/hmr/FileWatcher.js.map +1 -0
  91. package/dist/hmr/HmrDevConsole.d.ts +13 -0
  92. package/dist/hmr/HmrDevConsole.js +46 -0
  93. package/dist/hmr/HmrDevConsole.js.map +1 -0
  94. package/dist/hmr/HotReloadManager.d.ts +84 -0
  95. package/dist/hmr/HotReloadManager.js +195 -0
  96. package/dist/hmr/HotReloadManager.js.map +1 -0
  97. package/dist/hmr/index.d.ts +39 -0
  98. package/dist/hmr/index.js +38 -0
  99. package/dist/hmr/index.js.map +1 -0
  100. package/dist/index.d.ts +107 -0
  101. package/dist/index.js +107 -0
  102. package/dist/index.js.map +1 -0
  103. package/dist/integrations/APMIntegration.d.ts +141 -0
  104. package/dist/integrations/APMIntegration.js +212 -0
  105. package/dist/integrations/APMIntegration.js.map +1 -0
  106. package/dist/integrations/AzureMonitorIntegration.d.ts +118 -0
  107. package/dist/integrations/AzureMonitorIntegration.js +254 -0
  108. package/dist/integrations/AzureMonitorIntegration.js.map +1 -0
  109. package/dist/integrations/CloudWatchIntegration.d.ts +135 -0
  110. package/dist/integrations/CloudWatchIntegration.js +293 -0
  111. package/dist/integrations/CloudWatchIntegration.js.map +1 -0
  112. package/dist/integrations/SentryIntegration.d.ts +153 -0
  113. package/dist/integrations/SentryIntegration.js +200 -0
  114. package/dist/integrations/SentryIntegration.js.map +1 -0
  115. package/dist/integrations/index.d.ts +19 -0
  116. package/dist/integrations/index.js +16 -0
  117. package/dist/integrations/index.js.map +1 -0
  118. package/dist/marketplace/RuntimeAutoScaler.d.ts +148 -0
  119. package/dist/marketplace/RuntimeAutoScaler.js +366 -0
  120. package/dist/marketplace/RuntimeAutoScaler.js.map +1 -0
  121. package/dist/marketplace/RuntimeCatalog.d.ts +174 -0
  122. package/dist/marketplace/RuntimeCatalog.js +339 -0
  123. package/dist/marketplace/RuntimeCatalog.js.map +1 -0
  124. package/dist/marketplace/RuntimeDiscovery.d.ts +86 -0
  125. package/dist/marketplace/RuntimeDiscovery.js +219 -0
  126. package/dist/marketplace/RuntimeDiscovery.js.map +1 -0
  127. package/dist/marketplace/RuntimeHealthMonitor.d.ts +100 -0
  128. package/dist/marketplace/RuntimeHealthMonitor.js +241 -0
  129. package/dist/marketplace/RuntimeHealthMonitor.js.map +1 -0
  130. package/dist/marketplace/RuntimeMetricsDashboard.d.ts +113 -0
  131. package/dist/marketplace/RuntimeMetricsDashboard.js +293 -0
  132. package/dist/marketplace/RuntimeMetricsDashboard.js.map +1 -0
  133. package/dist/monitoring/CircuitBreaker.d.ts +107 -0
  134. package/dist/monitoring/CircuitBreaker.js +238 -0
  135. package/dist/monitoring/CircuitBreaker.js.map +1 -0
  136. package/dist/monitoring/DistributedTracer.d.ts +125 -0
  137. package/dist/monitoring/DistributedTracer.js +230 -0
  138. package/dist/monitoring/DistributedTracer.js.map +1 -0
  139. package/dist/monitoring/HealthCheck.d.ts +54 -0
  140. package/dist/monitoring/HealthCheck.js +102 -0
  141. package/dist/monitoring/HealthCheck.js.map +1 -0
  142. package/dist/monitoring/PerformanceProfiler.d.ts +63 -0
  143. package/dist/monitoring/PerformanceProfiler.js +229 -0
  144. package/dist/monitoring/PerformanceProfiler.js.map +1 -0
  145. package/dist/monitoring/PrometheusBootstrap.d.ts +30 -0
  146. package/dist/monitoring/PrometheusBootstrap.js +71 -0
  147. package/dist/monitoring/PrometheusBootstrap.js.map +1 -0
  148. package/dist/monitoring/PrometheusMetricsBridge.d.ts +60 -0
  149. package/dist/monitoring/PrometheusMetricsBridge.js +216 -0
  150. package/dist/monitoring/PrometheusMetricsBridge.js.map +1 -0
  151. package/dist/monitoring/RateLimiter.d.ts +58 -0
  152. package/dist/monitoring/RateLimiter.js +128 -0
  153. package/dist/monitoring/RateLimiter.js.map +1 -0
  154. package/dist/monitoring/StructuredLogger.d.ts +131 -0
  155. package/dist/monitoring/StructuredLogger.js +207 -0
  156. package/dist/monitoring/StructuredLogger.js.map +1 -0
  157. package/dist/monitoring/TracingBootstrap.d.ts +69 -0
  158. package/dist/monitoring/TracingBootstrap.js +129 -0
  159. package/dist/monitoring/TracingBootstrap.js.map +1 -0
  160. package/dist/monitoring/TriggerMetricsCollector.d.ts +94 -0
  161. package/dist/monitoring/TriggerMetricsCollector.js +174 -0
  162. package/dist/monitoring/TriggerMetricsCollector.js.map +1 -0
  163. package/dist/monitoring/index.d.ts +9 -0
  164. package/dist/monitoring/index.js +10 -0
  165. package/dist/monitoring/index.js.map +1 -0
  166. package/dist/openapi/OpenAPIGenerator.d.ts +192 -0
  167. package/dist/openapi/OpenAPIGenerator.js +373 -0
  168. package/dist/openapi/OpenAPIGenerator.js.map +1 -0
  169. package/dist/openapi/index.d.ts +20 -0
  170. package/dist/openapi/index.js +20 -0
  171. package/dist/openapi/index.js.map +1 -0
  172. package/dist/security/ABAC.d.ts +224 -0
  173. package/dist/security/ABAC.js +380 -0
  174. package/dist/security/ABAC.js.map +1 -0
  175. package/dist/security/AuditLogger.d.ts +242 -0
  176. package/dist/security/AuditLogger.js +317 -0
  177. package/dist/security/AuditLogger.js.map +1 -0
  178. package/dist/security/AuthMiddleware.d.ts +163 -0
  179. package/dist/security/AuthMiddleware.js +274 -0
  180. package/dist/security/AuthMiddleware.js.map +1 -0
  181. package/dist/security/EncryptionAtRest.d.ts +206 -0
  182. package/dist/security/EncryptionAtRest.js +236 -0
  183. package/dist/security/EncryptionAtRest.js.map +1 -0
  184. package/dist/security/OAuthProvider.d.ts +334 -0
  185. package/dist/security/OAuthProvider.js +719 -0
  186. package/dist/security/OAuthProvider.js.map +1 -0
  187. package/dist/security/PIIDetector.d.ts +233 -0
  188. package/dist/security/PIIDetector.js +354 -0
  189. package/dist/security/PIIDetector.js.map +1 -0
  190. package/dist/security/RBAC.d.ts +143 -0
  191. package/dist/security/RBAC.js +285 -0
  192. package/dist/security/RBAC.js.map +1 -0
  193. package/dist/security/SecretManager.d.ts +652 -0
  194. package/dist/security/SecretManager.js +1146 -0
  195. package/dist/security/SecretManager.js.map +1 -0
  196. package/dist/security/TLSConfig.d.ts +305 -0
  197. package/dist/security/TLSConfig.js +550 -0
  198. package/dist/security/TLSConfig.js.map +1 -0
  199. package/dist/security/index.d.ts +79 -0
  200. package/dist/security/index.js +80 -0
  201. package/dist/security/index.js.map +1 -0
  202. package/dist/testing/TestHarness.d.ts +189 -0
  203. package/dist/testing/TestHarness.js +272 -0
  204. package/dist/testing/TestHarness.js.map +1 -0
  205. package/dist/testing/TestLogger.d.ts +103 -0
  206. package/dist/testing/TestLogger.js +153 -0
  207. package/dist/testing/TestLogger.js.map +1 -0
  208. package/dist/testing/WorkflowTestRunner.d.ts +172 -0
  209. package/dist/testing/WorkflowTestRunner.js +355 -0
  210. package/dist/testing/WorkflowTestRunner.js.map +1 -0
  211. package/dist/testing/index.d.ts +21 -0
  212. package/dist/testing/index.js +22 -0
  213. package/dist/testing/index.js.map +1 -0
  214. package/dist/tracing/InMemoryRunStore.d.ts +44 -0
  215. package/dist/tracing/InMemoryRunStore.js +341 -0
  216. package/dist/tracing/InMemoryRunStore.js.map +1 -0
  217. package/dist/tracing/PostgresRunStore.d.ts +82 -0
  218. package/dist/tracing/PostgresRunStore.js +640 -0
  219. package/dist/tracing/PostgresRunStore.js.map +1 -0
  220. package/dist/tracing/RunStore.d.ts +38 -0
  221. package/dist/tracing/RunStore.js +2 -0
  222. package/dist/tracing/RunStore.js.map +1 -0
  223. package/dist/tracing/RunTracker.d.ts +75 -0
  224. package/dist/tracing/RunTracker.js +374 -0
  225. package/dist/tracing/RunTracker.js.map +1 -0
  226. package/dist/tracing/SqliteRunStore.d.ts +53 -0
  227. package/dist/tracing/SqliteRunStore.js +703 -0
  228. package/dist/tracing/SqliteRunStore.js.map +1 -0
  229. package/dist/tracing/TraceRouter.d.ts +47 -0
  230. package/dist/tracing/TraceRouter.js +904 -0
  231. package/dist/tracing/TraceRouter.js.map +1 -0
  232. package/dist/tracing/TracingLogger.d.ts +21 -0
  233. package/dist/tracing/TracingLogger.js +62 -0
  234. package/dist/tracing/TracingLogger.js.map +1 -0
  235. package/dist/tracing/createStore.d.ts +30 -0
  236. package/dist/tracing/createStore.js +75 -0
  237. package/dist/tracing/createStore.js.map +1 -0
  238. package/dist/tracing/index.d.ts +13 -0
  239. package/dist/tracing/index.js +9 -0
  240. package/dist/tracing/index.js.map +1 -0
  241. package/dist/tracing/sanitize.d.ts +7 -0
  242. package/dist/tracing/sanitize.js +95 -0
  243. package/dist/tracing/sanitize.js.map +1 -0
  244. package/dist/tracing/types.d.ts +178 -0
  245. package/dist/tracing/types.js +3 -0
  246. package/dist/tracing/types.js.map +1 -0
  247. package/dist/types/Average.d.ts +11 -0
  248. package/dist/types/Average.js +2 -0
  249. package/dist/types/Average.js.map +1 -0
  250. package/dist/types/Condition.d.ts +8 -0
  251. package/dist/types/Condition.js +2 -0
  252. package/dist/types/Condition.js.map +1 -0
  253. package/dist/types/Conditions.d.ts +5 -0
  254. package/dist/types/Conditions.js +2 -0
  255. package/dist/types/Conditions.js.map +1 -0
  256. package/dist/types/Config.d.ts +12 -0
  257. package/dist/types/Config.js +2 -0
  258. package/dist/types/Config.js.map +1 -0
  259. package/dist/types/Flow.d.ts +5 -0
  260. package/dist/types/Flow.js +2 -0
  261. package/dist/types/Flow.js.map +1 -0
  262. package/dist/types/GlobalOptions.d.ts +11 -0
  263. package/dist/types/GlobalOptions.js +2 -0
  264. package/dist/types/GlobalOptions.js.map +1 -0
  265. package/dist/types/Inputs.d.ts +5 -0
  266. package/dist/types/Inputs.js +2 -0
  267. package/dist/types/Inputs.js.map +1 -0
  268. package/dist/types/JsonLikeObject.d.ts +3 -0
  269. package/dist/types/JsonLikeObject.js +2 -0
  270. package/dist/types/JsonLikeObject.js.map +1 -0
  271. package/dist/types/Mapper.d.ts +5 -0
  272. package/dist/types/Mapper.js +2 -0
  273. package/dist/types/Mapper.js.map +1 -0
  274. package/dist/types/Node.d.ts +10 -0
  275. package/dist/types/Node.js +2 -0
  276. package/dist/types/Node.js.map +1 -0
  277. package/dist/types/ParamsDictionary.d.ts +3 -0
  278. package/dist/types/ParamsDictionary.js +2 -0
  279. package/dist/types/ParamsDictionary.js.map +1 -0
  280. package/dist/types/Properties.d.ts +5 -0
  281. package/dist/types/Properties.js +2 -0
  282. package/dist/types/Properties.js.map +1 -0
  283. package/dist/types/Targets.d.ts +5 -0
  284. package/dist/types/Targets.js +2 -0
  285. package/dist/types/Targets.js.map +1 -0
  286. package/dist/types/Trigger.d.ts +5 -0
  287. package/dist/types/Trigger.js +2 -0
  288. package/dist/types/Trigger.js.map +1 -0
  289. package/dist/types/TriggerHttp.d.ts +7 -0
  290. package/dist/types/TriggerHttp.js +2 -0
  291. package/dist/types/TriggerHttp.js.map +1 -0
  292. package/dist/types/TriggerResponse.d.ts +6 -0
  293. package/dist/types/TriggerResponse.js +2 -0
  294. package/dist/types/TriggerResponse.js.map +1 -0
  295. package/dist/types/Triggers.d.ts +5 -0
  296. package/dist/types/Triggers.js +2 -0
  297. package/dist/types/Triggers.js.map +1 -0
  298. package/dist/types/TryCatch.d.ts +6 -0
  299. package/dist/types/TryCatch.js +2 -0
  300. package/dist/types/TryCatch.js.map +1 -0
  301. package/dist/visualization/NodeDependencyGraph.d.ts +76 -0
  302. package/dist/visualization/NodeDependencyGraph.js +418 -0
  303. package/dist/visualization/NodeDependencyGraph.js.map +1 -0
  304. package/dist/visualization/WorkflowVisualizer.d.ts +144 -0
  305. package/dist/visualization/WorkflowVisualizer.js +446 -0
  306. package/dist/visualization/WorkflowVisualizer.js.map +1 -0
  307. package/package.json +95 -0
@@ -0,0 +1,499 @@
1
+ /**
2
+ * Node Result Caching for Blok Runner
3
+ *
4
+ * Provides intelligent caching of node execution results to avoid redundant
5
+ * computation. Supports LRU eviction, TTL-based expiry, tag-based invalidation,
6
+ * and pluggable cache providers.
7
+ *
8
+ * Uses only Node.js built-ins (node:crypto for hashing).
9
+ */
10
+ import { createHash } from "node:crypto";
11
+ /**
12
+ * Estimate the byte-size of an arbitrary value by serialising it to JSON.
13
+ * Falls back to 0 for values that cannot be serialised.
14
+ */
15
+ function estimateSize(value) {
16
+ try {
17
+ return Buffer.byteLength(JSON.stringify(value), "utf-8");
18
+ }
19
+ catch {
20
+ return 0;
21
+ }
22
+ }
23
+ /**
24
+ * Produce a deterministic SHA-256 hex digest for a cache key string.
25
+ */
26
+ function sha256(input) {
27
+ return createHash("sha256").update(input).digest("hex");
28
+ }
29
+ const DEFAULT_MAX_SIZE = 500;
30
+ const DEFAULT_TTL_MS = 60_000;
31
+ const DEFAULT_MAX_MEMORY_BYTES = 50 * 1024 * 1024; // 50 MB
32
+ const SWEEP_INTERVAL_MS = 60_000; // 60 s
33
+ /**
34
+ * LRU cache with TTL-based expiry, tag-based invalidation, and memory limits.
35
+ *
36
+ * Internally backed by a `Map` whose insertion order is used for LRU tracking.
37
+ * Every `get` promotes the accessed key to the most-recently-used position.
38
+ *
39
+ * Expired entries are cleaned up lazily on access and periodically via a
40
+ * background sweep timer (every 60 s). The timer is `unref`-ed so it does
41
+ * not prevent the Node.js process from exiting.
42
+ */
43
+ export class InMemoryCache {
44
+ name = "in-memory";
45
+ store = new Map();
46
+ maxSize;
47
+ defaultTTLMs;
48
+ maxMemoryBytes;
49
+ onEvict;
50
+ _hits = 0;
51
+ _misses = 0;
52
+ _evictions = 0;
53
+ _totalSets = 0;
54
+ _memoryUsageBytes = 0;
55
+ sweepTimer = null;
56
+ constructor(config = {}) {
57
+ this.maxSize = config.maxSize ?? DEFAULT_MAX_SIZE;
58
+ this.defaultTTLMs = config.defaultTTLMs ?? DEFAULT_TTL_MS;
59
+ this.maxMemoryBytes = config.maxMemoryBytes ?? DEFAULT_MAX_MEMORY_BYTES;
60
+ this.onEvict = config.onEvict;
61
+ this.startPeriodicSweep();
62
+ }
63
+ // -- CacheProvider implementation ----------------------------------------
64
+ /** @inheritdoc */
65
+ async get(key) {
66
+ const internal = this.store.get(key);
67
+ if (!internal) {
68
+ this._misses++;
69
+ return null;
70
+ }
71
+ // Lazy TTL check
72
+ if (Date.now() >= internal.entry.expiresAt) {
73
+ this.evict(key, "ttl");
74
+ this._misses++;
75
+ return null;
76
+ }
77
+ // Promote to most-recently-used (re-insert at the end of the Map)
78
+ this.store.delete(key);
79
+ internal.entry.hits++;
80
+ this.store.set(key, internal);
81
+ this._hits++;
82
+ return { ...internal.entry };
83
+ }
84
+ /** @inheritdoc */
85
+ async set(key, value, options) {
86
+ this._totalSets++;
87
+ const ttlMs = options?.ttlMs ?? this.defaultTTLMs;
88
+ const tags = options?.tags ?? [];
89
+ const priority = options?.priority ?? 0;
90
+ const size = estimateSize(value);
91
+ const now = Date.now();
92
+ // If the key already exists, remove it first so the new entry goes to
93
+ // the end of the Map (most-recently-used position).
94
+ const existing = this.store.get(key);
95
+ if (existing) {
96
+ this._memoryUsageBytes -= existing.entry.size;
97
+ this.store.delete(key);
98
+ }
99
+ const entry = {
100
+ value,
101
+ key,
102
+ createdAt: now,
103
+ expiresAt: now + ttlMs,
104
+ hits: 0,
105
+ size,
106
+ };
107
+ const internal = { entry, tags, priority };
108
+ this.store.set(key, internal);
109
+ this._memoryUsageBytes += size;
110
+ // Enforce memory limit
111
+ this.enforceMemoryLimit();
112
+ // Enforce max-size limit via LRU eviction
113
+ this.enforceSizeLimit();
114
+ }
115
+ /** @inheritdoc */
116
+ async delete(key) {
117
+ return this.evict(key, "manual");
118
+ }
119
+ /** @inheritdoc */
120
+ async clear() {
121
+ this.store.clear();
122
+ this._memoryUsageBytes = 0;
123
+ }
124
+ /** @inheritdoc */
125
+ async has(key) {
126
+ const internal = this.store.get(key);
127
+ if (!internal)
128
+ return false;
129
+ if (Date.now() >= internal.entry.expiresAt) {
130
+ this.evict(key, "ttl");
131
+ return false;
132
+ }
133
+ return true;
134
+ }
135
+ /** @inheritdoc */
136
+ getStats() {
137
+ const total = this._hits + this._misses;
138
+ return {
139
+ hits: this._hits,
140
+ misses: this._misses,
141
+ size: this.store.size,
142
+ maxSize: this.maxSize,
143
+ hitRate: total === 0 ? 0 : this._hits / total,
144
+ evictions: this._evictions,
145
+ totalSets: this._totalSets,
146
+ memoryUsageBytes: this._memoryUsageBytes,
147
+ };
148
+ }
149
+ // -- Extended API --------------------------------------------------------
150
+ /**
151
+ * Remove all entries that carry the given tag.
152
+ *
153
+ * @param tag - Tag string to match against.
154
+ * @returns Number of entries removed.
155
+ */
156
+ invalidateByTag(tag) {
157
+ let count = 0;
158
+ const entries = Array.from(this.store.entries());
159
+ for (const [key, internal] of entries) {
160
+ if (internal.tags.includes(tag)) {
161
+ this.evict(key, "manual");
162
+ count++;
163
+ }
164
+ }
165
+ return count;
166
+ }
167
+ /**
168
+ * Stop the periodic sweep timer and release resources.
169
+ * Call this when the cache is no longer needed.
170
+ */
171
+ destroy() {
172
+ this.stopPeriodicSweep();
173
+ this.store.clear();
174
+ this._memoryUsageBytes = 0;
175
+ }
176
+ // -- Private helpers -----------------------------------------------------
177
+ /**
178
+ * Evict a single key from the cache.
179
+ *
180
+ * @returns `true` if the key existed and was removed.
181
+ */
182
+ evict(key, reason) {
183
+ const internal = this.store.get(key);
184
+ if (!internal)
185
+ return false;
186
+ this.store.delete(key);
187
+ this._memoryUsageBytes -= internal.entry.size;
188
+ this._evictions++;
189
+ if (this.onEvict) {
190
+ try {
191
+ this.onEvict(key, reason);
192
+ }
193
+ catch {
194
+ // Eviction callbacks must not throw into the hot path.
195
+ }
196
+ }
197
+ return true;
198
+ }
199
+ /**
200
+ * Evict the least-recently-used entries until `store.size <= maxSize`.
201
+ * Entries with higher priority are skipped in favour of lower-priority ones.
202
+ */
203
+ enforceSizeLimit() {
204
+ while (this.store.size > this.maxSize) {
205
+ // Find the entry with the lowest priority among the oldest entries
206
+ let lowestKey = null;
207
+ let lowestPriority = Number.POSITIVE_INFINITY;
208
+ const entries = Array.from(this.store.entries());
209
+ for (const [key, internal] of entries) {
210
+ if (internal.priority < lowestPriority) {
211
+ lowestPriority = internal.priority;
212
+ lowestKey = key;
213
+ }
214
+ // Only inspect the oldest quarter (those at the front of the Map)
215
+ // to keep eviction fast while still respecting priority.
216
+ if (lowestKey !== null && lowestPriority === 0)
217
+ break;
218
+ }
219
+ if (lowestKey !== null) {
220
+ this.evict(lowestKey, "lru");
221
+ }
222
+ else {
223
+ // Safety valve: evict the first (oldest) key.
224
+ const keys = Array.from(this.store.keys());
225
+ if (keys.length > 0) {
226
+ this.evict(keys[0], "lru");
227
+ }
228
+ }
229
+ }
230
+ }
231
+ /**
232
+ * Evict entries until `_memoryUsageBytes <= maxMemoryBytes`.
233
+ */
234
+ enforceMemoryLimit() {
235
+ while (this._memoryUsageBytes > this.maxMemoryBytes && this.store.size > 0) {
236
+ const keys = Array.from(this.store.keys());
237
+ if (keys.length === 0)
238
+ break;
239
+ this.evict(keys[0], "memory");
240
+ }
241
+ }
242
+ /**
243
+ * Sweep all expired entries. Called periodically by the background timer.
244
+ */
245
+ sweep() {
246
+ const now = Date.now();
247
+ const entries = Array.from(this.store.entries());
248
+ for (const [key, internal] of entries) {
249
+ if (now >= internal.entry.expiresAt) {
250
+ this.evict(key, "ttl");
251
+ }
252
+ }
253
+ }
254
+ startPeriodicSweep() {
255
+ this.sweepTimer = setInterval(() => this.sweep(), SWEEP_INTERVAL_MS);
256
+ if (this.sweepTimer.unref) {
257
+ this.sweepTimer.unref();
258
+ }
259
+ }
260
+ stopPeriodicSweep() {
261
+ if (this.sweepTimer) {
262
+ clearInterval(this.sweepTimer);
263
+ this.sweepTimer = null;
264
+ }
265
+ }
266
+ }
267
+ /**
268
+ * Singleton orchestrator that wraps node executions with transparent caching.
269
+ *
270
+ * Typical usage:
271
+ *
272
+ * ```ts
273
+ * const cache = NodeResultCache.getInstance();
274
+ *
275
+ * const result = await cache.wrapExecution("myNode", input, async () => {
276
+ * return expensiveComputation(input);
277
+ * });
278
+ *
279
+ * console.log(result.cached); // true on subsequent identical calls
280
+ * ```
281
+ *
282
+ * The singleton is configured once via {@link NodeResultCache.configure}.
283
+ * Subsequent calls to `getInstance()` return the already-configured instance.
284
+ */
285
+ export class NodeResultCache {
286
+ static instance = null;
287
+ provider;
288
+ enabled;
289
+ keyStrategy;
290
+ customKeyFn;
291
+ /**
292
+ * In-flight promise map for deduplicating concurrent executions of the
293
+ * same cache key. Prevents the "thundering herd" problem where N
294
+ * concurrent callers all compute the same expensive result.
295
+ */
296
+ inflight = new Map();
297
+ constructor(config = {}) {
298
+ this.provider = config.provider ?? new InMemoryCache();
299
+ this.enabled = config.enabled ?? true;
300
+ this.keyStrategy = config.keyStrategy ?? "node-input";
301
+ this.customKeyFn = config.customKeyFn;
302
+ if (this.keyStrategy === "custom" && typeof this.customKeyFn !== "function") {
303
+ throw new Error('NodeResultCache: "custom" key strategy requires a customKeyFn to be provided.');
304
+ }
305
+ }
306
+ // -- Singleton API -------------------------------------------------------
307
+ /**
308
+ * Return the singleton instance, creating one with defaults if necessary.
309
+ */
310
+ static getInstance() {
311
+ if (!NodeResultCache.instance) {
312
+ NodeResultCache.instance = new NodeResultCache();
313
+ }
314
+ return NodeResultCache.instance;
315
+ }
316
+ /**
317
+ * Create (or replace) the singleton with the given configuration.
318
+ *
319
+ * @returns The newly configured singleton instance.
320
+ */
321
+ static configure(config) {
322
+ // If an existing instance has a destroyable provider, clean it up.
323
+ if (NodeResultCache.instance) {
324
+ const oldProvider = NodeResultCache.instance.provider;
325
+ if (oldProvider instanceof InMemoryCache) {
326
+ oldProvider.destroy();
327
+ }
328
+ }
329
+ NodeResultCache.instance = new NodeResultCache(config);
330
+ return NodeResultCache.instance;
331
+ }
332
+ /**
333
+ * Tear down the singleton, releasing any resources held by the provider.
334
+ */
335
+ static resetInstance() {
336
+ if (NodeResultCache.instance) {
337
+ const provider = NodeResultCache.instance.provider;
338
+ if (provider instanceof InMemoryCache) {
339
+ provider.destroy();
340
+ }
341
+ NodeResultCache.instance = null;
342
+ }
343
+ }
344
+ // -- Public API ----------------------------------------------------------
345
+ /**
346
+ * Wrap an async execution with caching.
347
+ *
348
+ * If a cached result exists for the derived key it is returned immediately.
349
+ * Otherwise `execute` is invoked and its result is stored before being
350
+ * returned.
351
+ *
352
+ * Concurrent calls with the same key will share a single in-flight
353
+ * execution (request coalescing).
354
+ *
355
+ * @param nodeName - Logical name of the node being executed.
356
+ * @param input - The input payload used to derive the cache key.
357
+ * @param execute - Factory that produces the result when no cache hit.
358
+ * @param options - Optional TTL, tags, and priority for the cache entry.
359
+ * @returns A {@link CacheResult} indicating whether the result was cached.
360
+ */
361
+ async wrapExecution(nodeName, input, execute, options) {
362
+ const key = this.buildKey(nodeName, input);
363
+ // Bypass cache entirely when disabled.
364
+ if (!this.enabled) {
365
+ const data = await execute();
366
+ return { data, cached: false, key };
367
+ }
368
+ // 1. Check for a cached entry.
369
+ const cached = await this.provider.get(key);
370
+ if (cached) {
371
+ return {
372
+ data: cached.value,
373
+ cached: true,
374
+ key,
375
+ ttlRemainingMs: Math.max(0, cached.expiresAt - Date.now()),
376
+ };
377
+ }
378
+ // 2. Deduplicate concurrent executions for the same key.
379
+ const existing = this.inflight.get(key);
380
+ if (existing) {
381
+ const data = (await existing);
382
+ // By this point the first caller will have stored the result.
383
+ const entry = await this.provider.get(key);
384
+ return {
385
+ data,
386
+ cached: entry !== null,
387
+ key,
388
+ ttlRemainingMs: entry ? Math.max(0, entry.expiresAt - Date.now()) : undefined,
389
+ };
390
+ }
391
+ // 3. Execute and cache.
392
+ const promise = execute();
393
+ this.inflight.set(key, promise);
394
+ try {
395
+ const data = await promise;
396
+ await this.provider.set(key, data, options);
397
+ return { data, cached: false, key };
398
+ }
399
+ finally {
400
+ this.inflight.delete(key);
401
+ }
402
+ }
403
+ /**
404
+ * Invalidate all cache entries whose key starts with the node-specific
405
+ * prefix. This is only fully effective with the default `"node-input"`
406
+ * strategy and the built-in {@link InMemoryCache} provider.
407
+ *
408
+ * For external providers, consider using tag-based invalidation instead.
409
+ *
410
+ * @param nodeName - Name of the node whose results should be purged.
411
+ */
412
+ async invalidateNode(nodeName) {
413
+ if (this.provider instanceof InMemoryCache) {
414
+ const prefix = `node:${nodeName}:`;
415
+ const store = this.provider.store;
416
+ const keysToDelete = [];
417
+ const allKeys = Array.from(store.keys());
418
+ for (const key of allKeys) {
419
+ if (key.startsWith(prefix)) {
420
+ keysToDelete.push(key);
421
+ }
422
+ }
423
+ for (const key of keysToDelete) {
424
+ await this.provider.delete(key);
425
+ }
426
+ }
427
+ else {
428
+ // For external providers: best-effort tag invalidation.
429
+ await this.invalidateByTags([`node:${nodeName}`]);
430
+ }
431
+ }
432
+ /**
433
+ * Invalidate all entries matching *any* of the supplied tags.
434
+ *
435
+ * This is a convenience wrapper around {@link InMemoryCache.invalidateByTag}
436
+ * when the built-in provider is used.
437
+ *
438
+ * @param tags - Array of tag strings.
439
+ */
440
+ async invalidateByTags(tags) {
441
+ if (this.provider instanceof InMemoryCache) {
442
+ for (const tag of tags) {
443
+ this.provider.invalidateByTag(tag);
444
+ }
445
+ }
446
+ // External providers would need their own tag invalidation mechanism.
447
+ }
448
+ /**
449
+ * Pre-populate the cache for a set of known inputs.
450
+ *
451
+ * This is useful during application startup to "warm" the cache and
452
+ * ensure that the first real request for each input is served from cache.
453
+ *
454
+ * @param nodeName - Name of the node.
455
+ * @param inputs - Array of inputs to pre-compute.
456
+ * @param execute - Factory that computes the result for a given input.
457
+ * @param options - Optional cache-set options applied to every entry.
458
+ */
459
+ async warmup(nodeName, inputs, execute, options) {
460
+ await Promise.all(inputs.map(async (input) => {
461
+ const key = this.buildKey(nodeName, input);
462
+ const exists = await this.provider.has(key);
463
+ if (!exists) {
464
+ const result = await execute(input);
465
+ await this.provider.set(key, result, options);
466
+ }
467
+ }));
468
+ }
469
+ /**
470
+ * Return the underlying provider's statistics snapshot.
471
+ */
472
+ getStats() {
473
+ return this.provider.getStats();
474
+ }
475
+ /**
476
+ * Return the underlying cache provider instance.
477
+ */
478
+ getProvider() {
479
+ return this.provider;
480
+ }
481
+ // -- Private helpers -----------------------------------------------------
482
+ /**
483
+ * Derive a cache key from the node name and input based on the configured
484
+ * {@link CacheKeyStrategy}.
485
+ */
486
+ buildKey(nodeName, input) {
487
+ switch (this.keyStrategy) {
488
+ case "input-hash":
489
+ return sha256(JSON.stringify(input));
490
+ case "node-input":
491
+ return `node:${nodeName}:${sha256(nodeName + JSON.stringify(input))}`;
492
+ case "custom":
493
+ return this.customKeyFn(nodeName, input);
494
+ default:
495
+ return `node:${nodeName}:${sha256(nodeName + JSON.stringify(input))}`;
496
+ }
497
+ }
498
+ }
499
+ //# sourceMappingURL=NodeResultCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NodeResultCache.js","sourceRoot":"","sources":["../../src/cache/NodeResultCache.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA0FzC;;;GAGG;AACH,SAAS,YAAY,CAAC,KAAc;IACnC,IAAI,CAAC;QACJ,OAAO,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,CAAC,CAAC;IACV,CAAC;AACF,CAAC;AAED;;GAEG;AACH,SAAS,MAAM,CAAC,KAAa;IAC5B,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAA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,EAAE,KAAK,CAAC,CAAC;YAC3C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC5C,IAAI,CAAC,MAAM,EAAE,CAAC;gBACb,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC;gBACpC,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAI,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;YAClD,CAAC;QACF,CAAC,CAAC,CACF,CAAC;IACH,CAAC;IAED;;OAEG;IACH,QAAQ;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED;;OAEG;IACH,WAAW;QACV,OAAO,IAAI,CAAC,QAAQ,CAAC;IACtB,CAAC;IAED,2EAA2E;IAE3E;;;OAGG;IACK,QAAQ,CAAC,QAAgB,EAAE,KAAc;QAChD,QAAQ,IAAI,CAAC,WAAW,EAAE,CAAC;YAC1B,KAAK,YAAY;gBAChB,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;YACtC,KAAK,YAAY;gBAChB,OAAO,QAAQ,QAAQ,IAAI,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACvE,KAAK,QAAQ;gBACZ,OAAO,IAAI,CAAC,WAAY,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;YAC3C;gBACC,OAAO,QAAQ,QAAQ,IAAI,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QACxE,CAAC;IACF,CAAC"}
@@ -0,0 +1 @@
1
+ export { InMemoryCache, NodeResultCache, type CacheProvider, type CacheEntry, type CacheSetOptions, type CacheStats, type InMemoryCacheConfig, type CacheKeyStrategy, type CustomKeyFn, type CacheResult, type NodeResultCacheConfig, } from "./NodeResultCache";
@@ -0,0 +1,2 @@
1
+ export { InMemoryCache, NodeResultCache, } from "./NodeResultCache";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cache/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,eAAe,GAUf,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Cost Estimator for Blok Workflows
3
+ *
4
+ * Estimates execution costs per node and workflow based on
5
+ * runtime type, cloud provider pricing, and optional profiling data.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const estimator = new CostEstimator({ provider: "aws", executionsPerMonth: 100000 });
10
+ * const estimate = estimator.estimateWorkflow(workflowDef);
11
+ * console.log(estimator.toTable());
12
+ * ```
13
+ */
14
+ import type { NodeProfile } from "../monitoring/PerformanceProfiler";
15
+ import type { WorkflowDef } from "../visualization/WorkflowVisualizer";
16
+ import { type CloudProvider, type RuntimeCostCategory, type RuntimeCostModel } from "./pricing";
17
+ export interface NodeCostEstimate {
18
+ nodeName: string;
19
+ stepName: string;
20
+ runtime: string;
21
+ category: RuntimeCostCategory;
22
+ estimatedDurationMs: number;
23
+ estimatedMemoryMb: number;
24
+ costPerExecution: number;
25
+ monthlyCost: number;
26
+ }
27
+ export interface WorkflowCostEstimate {
28
+ workflowName: string;
29
+ nodes: NodeCostEstimate[];
30
+ costPerExecution: number;
31
+ monthlyCost: number;
32
+ executionsPerMonth: number;
33
+ provider: CloudProvider;
34
+ }
35
+ export interface CostEstimatorConfig {
36
+ provider?: CloudProvider;
37
+ executionsPerMonth?: number;
38
+ customPricing?: Partial<Record<RuntimeCostCategory, Partial<RuntimeCostModel>>>;
39
+ }
40
+ export declare class CostEstimator {
41
+ private config;
42
+ private estimates;
43
+ constructor(config?: CostEstimatorConfig);
44
+ estimateWorkflow(workflow: WorkflowDef, profiles?: NodeProfile[]): WorkflowCostEstimate;
45
+ estimateNode(nodeName: string, stepName: string, stepType: string, runtime: string, avgDurationMs?: number, avgMemoryMb?: number): NodeCostEstimate;
46
+ getEstimates(): WorkflowCostEstimate[];
47
+ toTable(): string;
48
+ toJson(): string;
49
+ reset(): void;
50
+ private walkSteps;
51
+ private getCostModel;
52
+ private inferRuntime;
53
+ private fmtUsd;
54
+ private fmtNum;
55
+ private truncate;
56
+ private padColumns;
57
+ }