veryfront 0.0.81 → 0.0.83

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 (157) hide show
  1. package/README.md +15 -1
  2. package/esm/deno.js +1 -1
  3. package/esm/proxy/cache/index.d.ts +41 -0
  4. package/esm/proxy/cache/index.d.ts.map +1 -0
  5. package/esm/proxy/cache/index.js +75 -0
  6. package/esm/proxy/cache/memory-cache.d.ts +18 -0
  7. package/esm/proxy/cache/memory-cache.d.ts.map +1 -0
  8. package/esm/proxy/cache/memory-cache.js +100 -0
  9. package/esm/proxy/cache/redis-cache.d.ts +27 -0
  10. package/esm/proxy/cache/redis-cache.d.ts.map +1 -0
  11. package/esm/proxy/cache/redis-cache.js +183 -0
  12. package/esm/proxy/cache/resilient-cache.d.ts +44 -0
  13. package/esm/proxy/cache/resilient-cache.d.ts.map +1 -0
  14. package/esm/proxy/cache/resilient-cache.js +178 -0
  15. package/esm/proxy/cache/types.d.ts +65 -0
  16. package/esm/proxy/cache/types.d.ts.map +1 -0
  17. package/esm/proxy/cache/types.js +7 -0
  18. package/esm/proxy/handler.d.ts +81 -0
  19. package/esm/proxy/handler.d.ts.map +1 -0
  20. package/esm/proxy/handler.js +417 -0
  21. package/esm/proxy/logger.d.ts +29 -0
  22. package/esm/proxy/logger.d.ts.map +1 -0
  23. package/esm/proxy/logger.js +258 -0
  24. package/esm/proxy/oauth-client.d.ts +15 -0
  25. package/esm/proxy/oauth-client.d.ts.map +1 -0
  26. package/esm/proxy/oauth-client.js +52 -0
  27. package/esm/proxy/token-manager.d.ts +59 -0
  28. package/esm/proxy/token-manager.d.ts.map +1 -0
  29. package/esm/proxy/token-manager.js +125 -0
  30. package/esm/proxy/tracing.d.ts +39 -0
  31. package/esm/proxy/tracing.d.ts.map +1 -0
  32. package/esm/proxy/tracing.js +194 -0
  33. package/esm/src/cache/backend.d.ts +22 -0
  34. package/esm/src/cache/backend.d.ts.map +1 -1
  35. package/esm/src/cache/backend.js +59 -0
  36. package/esm/src/cache/cache-key-builder.d.ts +0 -4
  37. package/esm/src/cache/cache-key-builder.d.ts.map +1 -1
  38. package/esm/src/cache/cache-key-builder.js +0 -6
  39. package/esm/src/cache/hash.d.ts +107 -0
  40. package/esm/src/cache/hash.d.ts.map +1 -0
  41. package/esm/src/cache/hash.js +166 -0
  42. package/esm/src/cache/index.d.ts +3 -0
  43. package/esm/src/cache/index.d.ts.map +1 -1
  44. package/esm/src/cache/index.js +3 -0
  45. package/esm/src/cache/module-cache.d.ts +82 -0
  46. package/esm/src/cache/module-cache.d.ts.map +1 -0
  47. package/esm/src/cache/module-cache.js +214 -0
  48. package/esm/src/cache/multi-tier.d.ts +148 -0
  49. package/esm/src/cache/multi-tier.d.ts.map +1 -0
  50. package/esm/src/cache/multi-tier.js +326 -0
  51. package/esm/src/cli/app/actions.d.ts +26 -0
  52. package/esm/src/cli/app/actions.d.ts.map +1 -0
  53. package/esm/src/cli/app/actions.js +152 -0
  54. package/esm/src/cli/app/components/inline-input.d.ts +35 -0
  55. package/esm/src/cli/app/components/inline-input.d.ts.map +1 -0
  56. package/esm/src/cli/app/components/inline-input.js +220 -0
  57. package/esm/src/cli/app/components/list-select.d.ts +69 -0
  58. package/esm/src/cli/app/components/list-select.d.ts.map +1 -0
  59. package/esm/src/cli/app/components/list-select.js +137 -0
  60. package/esm/src/cli/app/index.d.ts +45 -0
  61. package/esm/src/cli/app/index.d.ts.map +1 -0
  62. package/esm/src/cli/app/index.js +1252 -0
  63. package/esm/src/cli/app/state.d.ts +122 -0
  64. package/esm/src/cli/app/state.d.ts.map +1 -0
  65. package/esm/src/cli/app/state.js +232 -0
  66. package/esm/src/cli/app/views/dashboard.d.ts +19 -0
  67. package/esm/src/cli/app/views/dashboard.d.ts.map +1 -0
  68. package/esm/src/cli/app/views/dashboard.js +178 -0
  69. package/esm/src/cli/index/command-router.d.ts.map +1 -1
  70. package/esm/src/cli/index/command-router.js +9 -39
  71. package/esm/src/cli/index/start-handler.d.ts +3 -0
  72. package/esm/src/cli/index/start-handler.d.ts.map +1 -0
  73. package/esm/src/cli/index/start-handler.js +145 -0
  74. package/esm/src/cli/mcp/index.d.ts +11 -0
  75. package/esm/src/cli/mcp/index.d.ts.map +1 -0
  76. package/esm/src/cli/mcp/index.js +10 -0
  77. package/esm/src/cli/templates/integration-loader.d.ts.map +1 -1
  78. package/esm/src/cli/templates/integration-loader.js +2 -4
  79. package/esm/src/middleware/builtin/security/redis-rate-limit.d.ts +2 -0
  80. package/esm/src/middleware/builtin/security/redis-rate-limit.d.ts.map +1 -1
  81. package/esm/src/middleware/builtin/security/redis-rate-limit.js +23 -9
  82. package/esm/src/modules/react-loader/ssr-module-loader/cache/redis.d.ts +10 -0
  83. package/esm/src/modules/react-loader/ssr-module-loader/cache/redis.d.ts.map +1 -1
  84. package/esm/src/modules/react-loader/ssr-module-loader/cache/redis.js +30 -42
  85. package/esm/src/modules/react-loader/ssr-module-loader/loader.d.ts.map +1 -1
  86. package/esm/src/modules/react-loader/ssr-module-loader/loader.js +148 -20
  87. package/esm/src/observability/tracing/span-names.d.ts +2 -0
  88. package/esm/src/observability/tracing/span-names.d.ts.map +1 -1
  89. package/esm/src/observability/tracing/span-names.js +2 -0
  90. package/esm/src/platform/adapters/fs/cache/file-cache.d.ts.map +1 -1
  91. package/esm/src/platform/adapters/fs/cache/file-cache.js +9 -3
  92. package/esm/src/rendering/orchestrator/module-loader/cache.d.ts +10 -2
  93. package/esm/src/rendering/orchestrator/module-loader/cache.d.ts.map +1 -1
  94. package/esm/src/rendering/orchestrator/module-loader/cache.js +11 -6
  95. package/esm/src/rendering/orchestrator/module-loader/index.d.ts.map +1 -1
  96. package/esm/src/rendering/orchestrator/module-loader/index.js +72 -77
  97. package/esm/src/server/context/cache-invalidation.d.ts.map +1 -1
  98. package/esm/src/server/context/cache-invalidation.js +4 -0
  99. package/esm/src/server/handlers/dev/dashboard/api.js +4 -0
  100. package/esm/src/server/handlers/dev/projects/ui-handler.d.ts.map +1 -1
  101. package/esm/src/server/handlers/dev/projects/ui-handler.js +6 -0
  102. package/esm/src/transforms/esm/http-cache.d.ts.map +1 -1
  103. package/esm/src/transforms/esm/http-cache.js +145 -93
  104. package/esm/src/transforms/esm/transform-cache.d.ts +25 -0
  105. package/esm/src/transforms/esm/transform-cache.d.ts.map +1 -1
  106. package/esm/src/transforms/esm/transform-cache.js +45 -0
  107. package/esm/src/transforms/mdx/esm-module-loader/module-fetcher/index.d.ts.map +1 -1
  108. package/esm/src/transforms/mdx/esm-module-loader/module-fetcher/index.js +2 -36
  109. package/esm/src/utils/constants/cache.d.ts +4 -0
  110. package/esm/src/utils/constants/cache.d.ts.map +1 -1
  111. package/esm/src/utils/constants/cache.js +14 -1
  112. package/esm/src/utils/index.d.ts +1 -1
  113. package/esm/src/utils/index.d.ts.map +1 -1
  114. package/esm/src/utils/index.js +1 -1
  115. package/package.json +2 -1
  116. package/src/deno.js +1 -1
  117. package/src/proxy/cache/index.ts +93 -0
  118. package/src/proxy/cache/memory-cache.ts +120 -0
  119. package/src/proxy/cache/redis-cache.ts +203 -0
  120. package/src/proxy/cache/resilient-cache.ts +205 -0
  121. package/src/proxy/cache/types.ts +72 -0
  122. package/src/proxy/handler.ts +593 -0
  123. package/src/proxy/logger.ts +329 -0
  124. package/src/proxy/oauth-client.ts +91 -0
  125. package/src/proxy/token-manager.ts +174 -0
  126. package/src/proxy/tracing.ts +237 -0
  127. package/src/src/cache/backend.ts +65 -0
  128. package/src/src/cache/cache-key-builder.ts +0 -9
  129. package/src/src/cache/hash.ts +205 -0
  130. package/src/src/cache/index.ts +3 -0
  131. package/src/src/cache/module-cache.ts +252 -0
  132. package/src/src/cache/multi-tier.ts +462 -0
  133. package/src/src/cli/app/actions.ts +190 -0
  134. package/src/src/cli/app/components/inline-input.ts +255 -0
  135. package/src/src/cli/app/components/list-select.ts +215 -0
  136. package/src/src/cli/app/index.ts +1471 -0
  137. package/src/src/cli/app/state.ts +385 -0
  138. package/src/src/cli/app/views/dashboard.ts +212 -0
  139. package/src/src/cli/index/command-router.ts +9 -40
  140. package/src/src/cli/index/start-handler.ts +195 -0
  141. package/src/src/cli/mcp/index.ts +11 -0
  142. package/src/src/cli/templates/integration-loader.ts +2 -8
  143. package/src/src/middleware/builtin/security/redis-rate-limit.ts +24 -11
  144. package/src/src/modules/react-loader/ssr-module-loader/cache/redis.ts +36 -50
  145. package/src/src/modules/react-loader/ssr-module-loader/loader.ts +168 -25
  146. package/src/src/observability/tracing/span-names.ts +2 -0
  147. package/src/src/platform/adapters/fs/cache/file-cache.ts +9 -3
  148. package/src/src/rendering/orchestrator/module-loader/cache.ts +14 -8
  149. package/src/src/rendering/orchestrator/module-loader/index.ts +94 -89
  150. package/src/src/server/context/cache-invalidation.ts +4 -0
  151. package/src/src/server/handlers/dev/dashboard/api.ts +2 -0
  152. package/src/src/server/handlers/dev/projects/ui-handler.ts +6 -0
  153. package/src/src/transforms/esm/http-cache.ts +160 -105
  154. package/src/src/transforms/esm/transform-cache.ts +53 -0
  155. package/src/src/transforms/mdx/esm-module-loader/module-fetcher/index.ts +2 -40
  156. package/src/src/utils/constants/cache.ts +21 -1
  157. package/src/src/utils/index.ts +0 -1
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Start Handler - Full TUI dashboard with project discovery
3
+ *
4
+ * Default command when running `veryfront` without arguments.
5
+ * Provides a TUI experience with project navigation and dev server.
6
+ */
7
+ import * as dntShim from "../../../_dnt.shims.js";
8
+
9
+
10
+ import { cwd, getEnv } from "../../platform/compat/process.js";
11
+ import { createFileSystem } from "../../platform/compat/fs.js";
12
+ import { isAbsolute, join, resolve } from "../../platform/compat/path/index.js";
13
+ import { cliLogger } from "../../utils/index.js";
14
+ import { exitProcess, registerTerminationSignals } from "../utils/index.js";
15
+ import type { ParsedArgs } from "./types.js";
16
+
17
+ const DEFAULT_START_PORT = 8080;
18
+ const DEFAULT_MCP_PORT = 9999;
19
+
20
+ interface DiscoveredProjects {
21
+ projects: Map<string, string>;
22
+ examples: Map<string, string>;
23
+ defaultProject: string | null;
24
+ }
25
+
26
+ function getProjectSlug(path: string): string {
27
+ return path.replace(/\/+$/, "").split("/").pop() || "";
28
+ }
29
+
30
+ async function isVeryFrontProject(projectPath: string): Promise<boolean> {
31
+ const fs = createFileSystem();
32
+ const markers = ["app", "pages", "components"];
33
+ const checks = await Promise.all(markers.map((m) => fs.exists(join(projectPath, m))));
34
+ return checks.some(Boolean);
35
+ }
36
+
37
+ async function findProjectsInDirs(baseDirs: string[]): Promise<Map<string, string>> {
38
+ const projects = new Map<string, string>();
39
+ const fs = createFileSystem();
40
+
41
+ for (const baseDir of baseDirs) {
42
+ const absoluteBase = isAbsolute(baseDir) ? baseDir : join(cwd(), baseDir);
43
+ if (!(await fs.exists(absoluteBase))) continue;
44
+
45
+ try {
46
+ for await (const entry of fs.readDir(absoluteBase)) {
47
+ if (!entry.isDirectory || entry.name.startsWith(".")) continue;
48
+
49
+ const projectPath = join(absoluteBase, entry.name);
50
+ if (await isVeryFrontProject(projectPath)) {
51
+ projects.set(entry.name, resolve(projectPath));
52
+ }
53
+ }
54
+ } catch {
55
+ // Directory not readable - skip
56
+ }
57
+ }
58
+
59
+ return projects;
60
+ }
61
+
62
+ async function discoverProjects(explicitPath: string | null): Promise<DiscoveredProjects> {
63
+ const [projects, examples] = await Promise.all([
64
+ findProjectsInDirs(["data/projects", "projects"]),
65
+ findProjectsInDirs(["examples"]),
66
+ ]);
67
+ const fs = createFileSystem();
68
+ let defaultProject: string | null = null;
69
+
70
+ // Add explicit project path if provided
71
+ if (explicitPath) {
72
+ const absolutePath = isAbsolute(explicitPath) ? explicitPath : join(cwd(), explicitPath);
73
+ if (await fs.exists(absolutePath)) {
74
+ const slug = getProjectSlug(absolutePath);
75
+ projects.set(slug, resolve(absolutePath));
76
+ defaultProject = slug;
77
+ }
78
+ }
79
+
80
+ // Fall back to current directory if no projects found
81
+ if (projects.size === 0 && !defaultProject) {
82
+ const currentDir = cwd();
83
+ if (await isVeryFrontProject(currentDir)) {
84
+ const slug = getProjectSlug(currentDir);
85
+ projects.set(slug, resolve(currentDir));
86
+ defaultProject = slug;
87
+ }
88
+ }
89
+
90
+ return { projects, examples, defaultProject };
91
+ }
92
+
93
+ interface ProxySetup {
94
+ interceptor: ((req: dntShim.Request) => Promise<dntShim.Request>) | undefined;
95
+ close: () => Promise<void>;
96
+ }
97
+
98
+ async function trySetupProxy(localProjects: Map<string, string>): Promise<ProxySetup> {
99
+ try {
100
+ // Proxy is only available in local dev, not in the npm package
101
+ const { createProxyHandler, injectContextHeaders } = await import(
102
+ "../../../proxy/handler.js"
103
+ );
104
+ const { createCacheFromEnv } = await import("../../../proxy/cache/index.js");
105
+
106
+ const proxyConfig = {
107
+ apiBaseUrl: getEnv("VERYFRONT_API_BASE_URL") || "http://api.lvh.me:4000",
108
+ clientId: getEnv("OAUTH_CLIENT_ID") || "",
109
+ clientSecret: getEnv("OAUTH_CLIENT_SECRET") || "",
110
+ previewClientId: getEnv("OAUTH_PREVIEW_CLIENT_ID") || "",
111
+ previewClientSecret: getEnv("OAUTH_PREVIEW_CLIENT_SECRET") || "",
112
+ apiToken: getEnv("VERYFRONT_API_TOKEN") || "",
113
+ localProjects: Object.fromEntries(localProjects),
114
+ };
115
+
116
+ const cache = await createCacheFromEnv();
117
+ const handler = createProxyHandler({ config: proxyConfig, cache });
118
+
119
+ return {
120
+ interceptor: async (req: dntShim.Request) =>
121
+ injectContextHeaders(req, await handler.processRequest(req)),
122
+ close: () => handler.close(),
123
+ };
124
+ } catch {
125
+ return { interceptor: undefined, close: async () => {} };
126
+ }
127
+ }
128
+
129
+ export async function handleStartCommand(args: ParsedArgs): Promise<void> {
130
+ const port = typeof args.port === "number" ? args.port : DEFAULT_START_PORT;
131
+ const mcpPort = typeof args["mcp-port"] === "number" ? args["mcp-port"] : DEFAULT_MCP_PORT;
132
+ const projectPath = args.project ? String(args.project) : null;
133
+ const headless = Boolean(args.headless || args["no-tui"]);
134
+
135
+ const { createApp, showStartup } = await import("../app/index.js");
136
+ const discovered = await discoverProjects(projectPath);
137
+
138
+ const app = createApp({
139
+ port,
140
+ mcpPort,
141
+ headless,
142
+ projects: discovered.projects,
143
+ examples: discovered.examples,
144
+ defaultProject: discovered.defaultProject ?? undefined,
145
+ });
146
+
147
+ const restoreConsole = app.interceptConsole();
148
+
149
+ if (!headless) {
150
+ await showStartup(["Loading configuration", "Discovering projects", "Starting server"]);
151
+ }
152
+
153
+ const allProjects = new Map([...discovered.projects, ...discovered.examples]);
154
+ const proxy = await trySetupProxy(allProjects);
155
+
156
+ const { createDevServer } = await import("../../server/dev-server.js");
157
+ const shutdownController = new AbortController();
158
+
159
+ const devServer = await createDevServer({
160
+ port,
161
+ projectDir: cwd(),
162
+ hmrPort: port + 1,
163
+ enableHMR: true,
164
+ enableFastRefresh: true,
165
+ signal: shutdownController.signal,
166
+ requestInterceptor: proxy.interceptor,
167
+ });
168
+ await devServer.ready;
169
+
170
+ const { createMCPServer } = await import("../mcp/index.js");
171
+ const mcpServer = await createMCPServer({ httpPort: mcpPort });
172
+
173
+ app.setServerReady();
174
+
175
+ let shuttingDown = false;
176
+ async function shutdown(): Promise<void> {
177
+ if (shuttingDown) return;
178
+ shuttingDown = true;
179
+
180
+ restoreConsole();
181
+ cliLogger.info("Shutting down...");
182
+
183
+ app.stop();
184
+ await mcpServer.stop();
185
+ shutdownController.abort();
186
+ await devServer.stop();
187
+ await proxy.close();
188
+ exitProcess(0);
189
+ }
190
+
191
+ registerTerminationSignals(() => void shutdown());
192
+ app.start();
193
+
194
+ await new Promise(() => {});
195
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * MCP Module for Dev Server
3
+ *
4
+ * Exposes dev server functionality via MCP (Model Context Protocol)
5
+ * for coding agents like Claude Code and Cursor.
6
+ */
7
+
8
+ export * from "./server.js";
9
+ export * from "./error-collector.js";
10
+ export * from "./log-buffer.js";
11
+ export * from "./tools.js";
@@ -195,14 +195,9 @@ export async function loadIntegration(
195
195
  const config = await loadIntegrationConfig(integrationName);
196
196
  if (!config) return null;
197
197
 
198
- const filesDir = pathHelper.join(
199
- getIntegrationDirectory(integrationName),
200
- "files",
201
- );
202
-
203
198
  return {
204
199
  config,
205
- files: await loadTemplateFromDirectory(filesDir),
200
+ files: await loadTemplateFromDirectory(`integration:${integrationName}`),
206
201
  };
207
202
  }
208
203
 
@@ -317,8 +312,7 @@ export async function getAvailablePrompts(
317
312
  * These include setup guide page and status API
318
313
  */
319
314
  export function loadIntegrationBaseFilesFromDirectory(): Promise<TemplateFile[]> {
320
- const filesDir = pathHelper.join(getIntegrationDirectory("_base"), "files");
321
- return loadTemplateFromDirectory(filesDir);
315
+ return loadTemplateFromDirectory("integration:_base");
322
316
  }
323
317
 
324
318
  /**
@@ -19,6 +19,7 @@ export interface RedisRateLimitOptions {
19
19
 
20
20
  export class RedisRateLimitStore implements RateLimitStore {
21
21
  private client: RedisClient | null = null;
22
+ private clientPromise: Promise<RedisClient> | null = null;
22
23
  private readonly url?: string;
23
24
  private readonly keyPrefix: string;
24
25
 
@@ -27,9 +28,15 @@ export class RedisRateLimitStore implements RateLimitStore {
27
28
  this.keyPrefix = options.keyPrefix ?? "veryfront:ratelimit:";
28
29
  }
29
30
 
30
- private async ensureClient(): Promise<RedisClient> {
31
- if (this.client) return this.client;
31
+ private ensureClient(): Promise<RedisClient> {
32
+ if (this.client) return Promise.resolve(this.client);
33
+ if (this.clientPromise) return this.clientPromise;
32
34
 
35
+ this.clientPromise = this.connectClient();
36
+ return this.clientPromise;
37
+ }
38
+
39
+ private async connectClient(): Promise<RedisClient> {
33
40
  let createClient: ((options: { url?: string }) => RedisClient) | undefined;
34
41
 
35
42
  try {
@@ -37,6 +44,7 @@ export class RedisRateLimitStore implements RateLimitStore {
37
44
  const mod = await import(redisClientModule);
38
45
  createClient = mod.createClient as (options: { url?: string }) => RedisClient;
39
46
  } catch {
47
+ this.clientPromise = null;
40
48
  throw toError(
41
49
  createError({
42
50
  type: "config",
@@ -46,15 +54,20 @@ export class RedisRateLimitStore implements RateLimitStore {
46
54
  );
47
55
  }
48
56
 
49
- const client = createClient({ url: this.url });
50
-
51
- client.on?.("error", (err: unknown) => {
52
- logger.error("[redis-ratelimit] client error", err);
53
- });
54
-
55
- await client.connect();
56
- this.client = client;
57
- return client;
57
+ try {
58
+ const client = createClient({ url: this.url });
59
+
60
+ client.on?.("error", (err: unknown) => {
61
+ logger.error("[redis-ratelimit] client error", err);
62
+ });
63
+
64
+ await client.connect();
65
+ this.client = client;
66
+ return client;
67
+ } catch (error) {
68
+ this.clientPromise = null;
69
+ throw error;
70
+ }
58
71
  }
59
72
 
60
73
  private storageKey(key: string): string {
@@ -1,59 +1,38 @@
1
1
  /** Redis caching for cross-pod SSR module sharing */
2
2
 
3
3
  import { rendererLogger as logger } from "../../../../utils/index.js";
4
- import {
5
- getRedisClient,
6
- isRedisConfigured,
7
- type RedisClient,
8
- } from "../../../../utils/redis-client.js";
4
+ import { type RedisClient } from "../../../../utils/redis-client.js";
9
5
  import { buildRedisSSRModuleKey } from "../../../../cache/index.js";
10
6
  import { getSSRModuleRedisTTL } from "../constants.js";
11
-
12
- let redisEnabled = false;
13
- let redisClient: RedisClient | null = null;
14
- let redisInitialized = false;
15
- let redisInitPromise: Promise<void> | null = null;
16
-
7
+ import { CacheBackends, createDistributedCacheAccessor } from "../../../../cache/backend.js";
8
+
9
+ /** Lazy-loaded distributed cache backend for cross-pod sharing */
10
+ const getDistributedCache = createDistributedCacheAccessor(
11
+ () => CacheBackends.ssrModule(),
12
+ "SSR-MODULE-LOADER",
13
+ );
14
+
15
+ /**
16
+ * @deprecated Legacy key builder. CacheBackend handles prefixing internally.
17
+ * Used only for backward compatibility if needed.
18
+ */
17
19
  export function redisKey(key: string): string {
18
20
  return buildRedisSSRModuleKey(key);
19
21
  }
20
22
 
21
23
  /** Initialize distributed caching for SSR modules */
22
24
  export async function initializeSSRDistributedCache(): Promise<boolean> {
23
- if (redisInitialized) return redisEnabled;
24
-
25
- if (redisInitPromise) {
26
- await redisInitPromise;
27
- return redisEnabled;
28
- }
29
-
30
- redisInitPromise = (async () => {
31
- if (!isRedisConfigured()) {
32
- logger.debug("[SSR-MODULE-LOADER] Redis not configured, using memory cache");
33
- redisInitialized = true;
34
- return;
35
- }
36
-
37
- try {
38
- redisClient = await getRedisClient();
39
- redisEnabled = true;
40
- logger.debug("[SSR-MODULE-LOADER] Redis cache enabled");
41
- } catch (error) {
42
- logger.warn("[SSR-MODULE-LOADER] Redis unavailable, falling back to memory cache", { error });
43
- redisEnabled = false;
44
- } finally {
45
- redisInitialized = true;
46
- }
47
- })();
48
-
49
- await redisInitPromise;
50
- redisInitPromise = null;
51
-
52
- return redisEnabled;
25
+ const backend = await getDistributedCache();
26
+ return backend !== null;
53
27
  }
54
28
 
29
+ /** Check if distributed caching is enabled for SSR modules */
55
30
  export function isSSRDistributedCacheEnabled(): boolean {
56
- return redisEnabled && redisClient !== null;
31
+ // We can't synchronously check if backend is initialized without accessing the promise
32
+ // But we can check if we *should* be enabled based on env via CacheBackend utils
33
+ // For now, this returns true because it's used as a guard for get/set calls
34
+ // which themselves are async and handle missing backends gracefully.
35
+ return true;
57
36
  }
58
37
 
59
38
  /** @deprecated Use initializeSSRDistributedCache instead */
@@ -62,21 +41,27 @@ export const initializeSSRRedisCache = initializeSSRDistributedCache;
62
41
  /** @deprecated Use isSSRDistributedCacheEnabled instead */
63
42
  export const isSSRRedisCacheEnabled = isSSRDistributedCacheEnabled;
64
43
 
44
+ /** @deprecated Use isSSRDistributedCacheEnabled instead */
65
45
  export function getRedisEnabled(): boolean {
66
- return redisEnabled;
46
+ return isSSRDistributedCacheEnabled();
67
47
  }
68
48
 
49
+ /**
50
+ * @deprecated Direct Redis client access is deprecated. Use CacheBackend abstraction.
51
+ * Returns null to force use of CacheBackend path in updated consumers.
52
+ */
69
53
  export function getRedisClientInstance(): RedisClient | null {
70
- return redisClient;
54
+ return null;
71
55
  }
72
56
 
73
57
  export async function getFromRedis(cacheKey: string): Promise<string | null> {
74
- if (!redisEnabled || !redisClient) return null;
58
+ const backend = await getDistributedCache();
59
+ if (!backend) return null;
75
60
 
76
61
  try {
77
- return await redisClient.get(redisKey(cacheKey));
62
+ return await backend.get(cacheKey);
78
63
  } catch (error) {
79
- logger.debug("[SSR-MODULE-LOADER] Redis get failed", { key: cacheKey, error });
64
+ logger.debug("[SSR-MODULE-LOADER] Distributed cache get failed", { key: cacheKey, error });
80
65
  return null;
81
66
  }
82
67
  }
@@ -87,13 +72,14 @@ export async function setInRedis(
87
72
  code: string,
88
73
  options?: { isProduction?: boolean; ttlSeconds?: number },
89
74
  ): Promise<void> {
90
- if (!redisEnabled || !redisClient) return;
75
+ const backend = await getDistributedCache();
76
+ if (!backend) return;
91
77
 
92
78
  const ttl = options?.ttlSeconds ?? getSSRModuleRedisTTL(options?.isProduction ?? true);
93
79
 
94
80
  try {
95
- await redisClient.set(redisKey(cacheKey), code, { EX: ttl });
81
+ await backend.set(cacheKey, code, ttl);
96
82
  } catch (error) {
97
- logger.debug("[SSR-MODULE-LOADER] Redis set failed", { key: cacheKey, error });
83
+ logger.debug("[SSR-MODULE-LOADER] Distributed cache set failed", { key: cacheKey, error });
98
84
  }
99
85
  }
@@ -40,17 +40,45 @@ import { withTimeoutThrow } from "../../../rendering/utils/stream-utils.js";
40
40
  import {
41
41
  failedComponents,
42
42
  getFromRedis,
43
- getRedisClientInstance,
44
- getRedisEnabled,
45
43
  globalCrossProjectCache,
46
44
  globalInProgress,
47
45
  globalModuleCache,
48
46
  globalTmpDirs,
47
+ isSSRDistributedCacheEnabled,
49
48
  setInRedis,
50
49
  transformSemaphore,
51
50
  } from "./cache/index.js";
52
51
  import type { ModuleCacheEntry, SSRModuleLoaderOptions } from "./types.js";
53
- import { getCacheBaseDir } from "../../../utils/cache-dir.js";
52
+ import { getCacheBaseDir, getHttpBundleCacheDir } from "../../../utils/cache-dir.js";
53
+ import { ensureHttpBundlesExist } from "../../../transforms/esm/http-cache.js";
54
+ import { LRUCache } from "../../../utils/lru-wrapper.js";
55
+
56
+ /** Pattern to match HTTP bundle file:// paths in transformed code */
57
+ const HTTP_BUNDLE_PATTERN = /file:\/\/([^"'\s]+veryfront-http-bundle\/http-([a-f0-9]+)\.mjs)/gi;
58
+
59
+ /** Extract HTTP bundle paths from transformed code for proactive recovery */
60
+ function extractHttpBundlePaths(code: string): Array<{ path: string; hash: string }> {
61
+ const bundles: Array<{ path: string; hash: string }> = [];
62
+ const seen = new Set<string>();
63
+ let match;
64
+ while ((match = HTTP_BUNDLE_PATTERN.exec(code)) !== null) {
65
+ const path = match[1] as string;
66
+ const hash = match[2] as string;
67
+ if (!seen.has(hash)) {
68
+ seen.add(hash);
69
+ bundles.push({ path, hash });
70
+ }
71
+ }
72
+ HTTP_BUNDLE_PATTERN.lastIndex = 0;
73
+ return bundles;
74
+ }
75
+
76
+ /**
77
+ * Track modules whose HTTP bundles have been verified, keyed by tempPath:contentHash.
78
+ * Bounded LRU to prevent unbounded memory growth in long-running pods.
79
+ * Keying by contentHash ensures verification is re-done when content changes at the same path.
80
+ */
81
+ const verifiedHttpBundlePaths = new LRUCache<string, true>({ maxEntries: 2000 });
54
82
 
55
83
  /**
56
84
  * SSR Module Loader with Redis Support.
@@ -100,11 +128,45 @@ export class SSRModuleLoader {
100
128
  );
101
129
  }
102
130
 
103
- const mod = await withSpan(
104
- SpanNames.SSR_DYNAMIC_IMPORT,
105
- () => import(`file://${cacheEntry.tempPath}?v=${cacheEntry.contentHash}`),
106
- { "ssr.file": fileName },
107
- );
131
+ let mod: Record<string, unknown>;
132
+ try {
133
+ mod = await withSpan(
134
+ SpanNames.SSR_DYNAMIC_IMPORT,
135
+ () => import(`file://${cacheEntry.tempPath}?v=${cacheEntry.contentHash}`),
136
+ { "ssr.file": fileName },
137
+ ) as Record<string, unknown>;
138
+ } catch (importError) {
139
+ // If import fails due to missing HTTP bundle, try to recover and retry once
140
+ const errorMsg = importError instanceof Error
141
+ ? importError.message
142
+ : String(importError);
143
+ const bundleMatch = errorMsg.match(/veryfront-http-bundle\/http-([a-f0-9]+)\.mjs/);
144
+ if (bundleMatch) {
145
+ const hash = bundleMatch[1]!;
146
+ logger.warn(
147
+ "[SSR-MODULE-LOADER] Import failed due to missing HTTP bundle, attempting recovery",
148
+ {
149
+ file: filePath.slice(-40),
150
+ hash,
151
+ },
152
+ );
153
+ const { recoverHttpBundleByHash } = await import(
154
+ "../../../transforms/esm/http-cache.js"
155
+ );
156
+ const cacheDir = getHttpBundleCacheDir();
157
+ const recovered = await recoverHttpBundleByHash(hash, cacheDir);
158
+ if (recovered) {
159
+ logger.info("[SSR-MODULE-LOADER] HTTP bundle recovered, retrying import", { hash });
160
+ mod = await import(
161
+ `file://${cacheEntry.tempPath}?v=${cacheEntry.contentHash}&retry=1`
162
+ ) as Record<string, unknown>;
163
+ } else {
164
+ throw importError;
165
+ }
166
+ } else {
167
+ throw importError;
168
+ }
169
+ }
108
170
 
109
171
  failedComponents.delete(circuitKey);
110
172
  return extractComponent(mod, filePath);
@@ -372,28 +434,88 @@ export class SSRModuleLoader {
372
434
 
373
435
  const cachedEntry = globalModuleCache.get(contentCacheKey);
374
436
  if (cachedEntry) {
375
- globalModuleCache.set(filePathCacheKey, cachedEntry);
376
- await this.ensureDependenciesExist(code, filePath, depth);
377
- return;
437
+ // Verify HTTP bundles exist for in-memory cached transforms (once per path+content)
438
+ const verifyKey = `${cachedEntry.tempPath}:${cachedEntry.contentHash}`;
439
+ if (!verifiedHttpBundlePaths.get(verifyKey)) {
440
+ try {
441
+ const cachedCode = await this.fs.readTextFile(cachedEntry.tempPath);
442
+ const bundlePaths = extractHttpBundlePaths(cachedCode);
443
+ if (bundlePaths.length > 0) {
444
+ const cacheDir = getHttpBundleCacheDir();
445
+ const failed = await ensureHttpBundlesExist(bundlePaths, cacheDir);
446
+ if (failed.length > 0) {
447
+ logger.warn(
448
+ "[SSR-MODULE-LOADER] In-memory cached module has unrecoverable HTTP bundles, re-transforming",
449
+ {
450
+ file: filePath.slice(-40),
451
+ failed,
452
+ },
453
+ );
454
+ globalModuleCache.delete(contentCacheKey);
455
+ globalModuleCache.delete(filePathCacheKey);
456
+ // Fall through to Redis or fresh transform
457
+ } else {
458
+ verifiedHttpBundlePaths.set(verifyKey, true);
459
+ }
460
+ } else {
461
+ verifiedHttpBundlePaths.set(verifyKey, true);
462
+ }
463
+ } catch {
464
+ // File doesn't exist or unreadable, invalidate cache
465
+ globalModuleCache.delete(contentCacheKey);
466
+ globalModuleCache.delete(filePathCacheKey);
467
+ }
468
+ }
469
+
470
+ // Re-check after potential invalidation
471
+ if (globalModuleCache.has(contentCacheKey)) {
472
+ globalModuleCache.set(filePathCacheKey, cachedEntry);
473
+ await this.ensureDependenciesExist(code, filePath, depth);
474
+ return;
475
+ }
378
476
  }
379
477
 
380
- const redisEnabled = getRedisEnabled();
381
- const redisClient = getRedisClientInstance();
382
- if (redisEnabled && redisClient) {
478
+ if (isSSRDistributedCacheEnabled()) {
383
479
  const redisCode = await getFromRedis(contentCacheKey);
384
480
  if (redisCode) {
385
- const tempPath = await this.getTempPath(filePath, contentHash);
386
- await this.fs.mkdir(tempPath.substring(0, tempPath.lastIndexOf("/")), { recursive: true });
387
- await this.fs.writeTextFile(tempPath, redisCode);
481
+ // Proactively ensure HTTP bundles exist before using cached transform.
482
+ // The cached code may reference file:// paths to HTTP bundles that were
483
+ // created on a different pod and may not exist locally.
484
+ let httpBundlesOk = true;
485
+ const bundlePaths = extractHttpBundlePaths(redisCode);
486
+ if (bundlePaths.length > 0) {
487
+ const cacheDir = getHttpBundleCacheDir();
488
+ const failed = await ensureHttpBundlesExist(bundlePaths, cacheDir);
489
+ if (failed.length > 0) {
490
+ logger.warn(
491
+ "[SSR-MODULE-LOADER] Redis cached code has unrecoverable HTTP bundles, re-transforming",
492
+ {
493
+ file: filePath.slice(-40),
494
+ failed,
495
+ },
496
+ );
497
+ httpBundlesOk = false;
498
+ }
499
+ }
388
500
 
389
- const entry: ModuleCacheEntry = { tempPath, contentHash };
390
- globalModuleCache.set(contentCacheKey, entry);
391
- globalModuleCache.set(filePathCacheKey, entry);
501
+ if (httpBundlesOk) {
502
+ const tempPath = await this.getTempPath(filePath, contentHash);
503
+ await this.fs.mkdir(tempPath.substring(0, tempPath.lastIndexOf("/")), {
504
+ recursive: true,
505
+ });
506
+ await this.fs.writeTextFile(tempPath, redisCode);
507
+ verifiedHttpBundlePaths.set(`${tempPath}:${contentHash}`, true);
392
508
 
393
- logger.debug("[SSR-MODULE-LOADER] Redis cache hit", { file: filePath.slice(-40) });
509
+ const entry: ModuleCacheEntry = { tempPath, contentHash };
510
+ globalModuleCache.set(contentCacheKey, entry);
511
+ globalModuleCache.set(filePathCacheKey, entry);
394
512
 
395
- await this.ensureDependenciesExist(code, filePath, depth);
396
- return;
513
+ logger.debug("[SSR-MODULE-LOADER] Redis cache hit", { file: filePath.slice(-40) });
514
+
515
+ await this.ensureDependenciesExist(code, filePath, depth);
516
+ return;
517
+ }
518
+ // Fall through to re-transform, which will create HTTP bundles locally
397
519
  }
398
520
  }
399
521
 
@@ -514,6 +636,22 @@ export class SSRModuleLoader {
514
636
  // This ensures that each content version uses its own cached module
515
637
  transformed = this.rewriteLocalImports(transformed, localImportPaths, filePath);
516
638
 
639
+ // Ensure HTTP bundles exist for this transform (handles nested bundle deps)
640
+ const bundlePaths = extractHttpBundlePaths(transformed);
641
+ if (bundlePaths.length > 0) {
642
+ const cacheDir = getHttpBundleCacheDir();
643
+ const failed = await ensureHttpBundlesExist(bundlePaths, cacheDir);
644
+ if (failed.length > 0) {
645
+ logger.warn(
646
+ "[SSR-MODULE-LOADER] Some HTTP bundles could not be recovered",
647
+ {
648
+ file: filePath.slice(-40),
649
+ failed,
650
+ },
651
+ );
652
+ }
653
+ }
654
+
517
655
  // Hash the TRANSFORMED content (after import rewrites) for cache busting
518
656
  // This ensures Deno's module cache is invalidated when dependencies change
519
657
  const transformedHash = await this.hashContentAsync(transformed);
@@ -522,10 +660,15 @@ export class SSRModuleLoader {
522
660
  await this.fs.mkdir(tempPath.substring(0, tempPath.lastIndexOf("/")), { recursive: true });
523
661
  await this.fs.writeTextFile(tempPath, transformed);
524
662
 
525
- if (redisEnabled && redisClient) {
663
+ if (isSSRDistributedCacheEnabled()) {
526
664
  setInRedis(contentCacheKey, transformed, {
527
665
  isProduction: this.isProductionContentSource(),
528
- }).catch(() => {});
666
+ }).catch((error) => {
667
+ logger.debug("[SSR-MODULE-LOADER] Distributed cache set failed", {
668
+ key: contentCacheKey,
669
+ error,
670
+ });
671
+ });
529
672
  }
530
673
 
531
674
  // Use transformedHash for cache busting in dynamic imports
@@ -98,6 +98,8 @@ export const SpanNames = {
98
98
  CACHE_REGISTRY_DELETE_REDIS_KEYS: "cache.registry.delete_redis_keys",
99
99
  CACHE_KEYS_GET_ALL_ASYNC: "cache.keys.get_all_async",
100
100
  CACHE_KEYS_DELETE_ALL_ASYNC: "cache.keys.delete_all_async",
101
+ CACHE_MULTI_TIER_GET: "cache.multi_tier.get",
102
+ CACHE_MULTI_TIER_SET: "cache.multi_tier.set",
101
103
 
102
104
  HTML_GENERATE_SHELL_PARTS: "html.generate_shell_parts",
103
105
  HTML_WRAP_IN_SHELL: "html.wrap_in_shell",