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.
- package/README.md +15 -1
- package/esm/deno.js +1 -1
- package/esm/proxy/cache/index.d.ts +41 -0
- package/esm/proxy/cache/index.d.ts.map +1 -0
- package/esm/proxy/cache/index.js +75 -0
- package/esm/proxy/cache/memory-cache.d.ts +18 -0
- package/esm/proxy/cache/memory-cache.d.ts.map +1 -0
- package/esm/proxy/cache/memory-cache.js +100 -0
- package/esm/proxy/cache/redis-cache.d.ts +27 -0
- package/esm/proxy/cache/redis-cache.d.ts.map +1 -0
- package/esm/proxy/cache/redis-cache.js +183 -0
- package/esm/proxy/cache/resilient-cache.d.ts +44 -0
- package/esm/proxy/cache/resilient-cache.d.ts.map +1 -0
- package/esm/proxy/cache/resilient-cache.js +178 -0
- package/esm/proxy/cache/types.d.ts +65 -0
- package/esm/proxy/cache/types.d.ts.map +1 -0
- package/esm/proxy/cache/types.js +7 -0
- package/esm/proxy/handler.d.ts +81 -0
- package/esm/proxy/handler.d.ts.map +1 -0
- package/esm/proxy/handler.js +417 -0
- package/esm/proxy/logger.d.ts +29 -0
- package/esm/proxy/logger.d.ts.map +1 -0
- package/esm/proxy/logger.js +258 -0
- package/esm/proxy/oauth-client.d.ts +15 -0
- package/esm/proxy/oauth-client.d.ts.map +1 -0
- package/esm/proxy/oauth-client.js +52 -0
- package/esm/proxy/token-manager.d.ts +59 -0
- package/esm/proxy/token-manager.d.ts.map +1 -0
- package/esm/proxy/token-manager.js +125 -0
- package/esm/proxy/tracing.d.ts +39 -0
- package/esm/proxy/tracing.d.ts.map +1 -0
- package/esm/proxy/tracing.js +194 -0
- package/esm/src/cache/backend.d.ts +22 -0
- package/esm/src/cache/backend.d.ts.map +1 -1
- package/esm/src/cache/backend.js +59 -0
- package/esm/src/cache/cache-key-builder.d.ts +0 -4
- package/esm/src/cache/cache-key-builder.d.ts.map +1 -1
- package/esm/src/cache/cache-key-builder.js +0 -6
- package/esm/src/cache/hash.d.ts +107 -0
- package/esm/src/cache/hash.d.ts.map +1 -0
- package/esm/src/cache/hash.js +166 -0
- package/esm/src/cache/index.d.ts +3 -0
- package/esm/src/cache/index.d.ts.map +1 -1
- package/esm/src/cache/index.js +3 -0
- package/esm/src/cache/module-cache.d.ts +82 -0
- package/esm/src/cache/module-cache.d.ts.map +1 -0
- package/esm/src/cache/module-cache.js +214 -0
- package/esm/src/cache/multi-tier.d.ts +148 -0
- package/esm/src/cache/multi-tier.d.ts.map +1 -0
- package/esm/src/cache/multi-tier.js +326 -0
- package/esm/src/cli/app/actions.d.ts +26 -0
- package/esm/src/cli/app/actions.d.ts.map +1 -0
- package/esm/src/cli/app/actions.js +152 -0
- package/esm/src/cli/app/components/inline-input.d.ts +35 -0
- package/esm/src/cli/app/components/inline-input.d.ts.map +1 -0
- package/esm/src/cli/app/components/inline-input.js +220 -0
- package/esm/src/cli/app/components/list-select.d.ts +69 -0
- package/esm/src/cli/app/components/list-select.d.ts.map +1 -0
- package/esm/src/cli/app/components/list-select.js +137 -0
- package/esm/src/cli/app/index.d.ts +45 -0
- package/esm/src/cli/app/index.d.ts.map +1 -0
- package/esm/src/cli/app/index.js +1252 -0
- package/esm/src/cli/app/state.d.ts +122 -0
- package/esm/src/cli/app/state.d.ts.map +1 -0
- package/esm/src/cli/app/state.js +232 -0
- package/esm/src/cli/app/views/dashboard.d.ts +19 -0
- package/esm/src/cli/app/views/dashboard.d.ts.map +1 -0
- package/esm/src/cli/app/views/dashboard.js +178 -0
- package/esm/src/cli/index/command-router.d.ts.map +1 -1
- package/esm/src/cli/index/command-router.js +9 -39
- package/esm/src/cli/index/start-handler.d.ts +3 -0
- package/esm/src/cli/index/start-handler.d.ts.map +1 -0
- package/esm/src/cli/index/start-handler.js +145 -0
- package/esm/src/cli/mcp/index.d.ts +11 -0
- package/esm/src/cli/mcp/index.d.ts.map +1 -0
- package/esm/src/cli/mcp/index.js +10 -0
- package/esm/src/cli/templates/integration-loader.d.ts.map +1 -1
- package/esm/src/cli/templates/integration-loader.js +2 -4
- package/esm/src/middleware/builtin/security/redis-rate-limit.d.ts +2 -0
- package/esm/src/middleware/builtin/security/redis-rate-limit.d.ts.map +1 -1
- package/esm/src/middleware/builtin/security/redis-rate-limit.js +23 -9
- package/esm/src/modules/react-loader/ssr-module-loader/cache/redis.d.ts +10 -0
- package/esm/src/modules/react-loader/ssr-module-loader/cache/redis.d.ts.map +1 -1
- package/esm/src/modules/react-loader/ssr-module-loader/cache/redis.js +30 -42
- package/esm/src/modules/react-loader/ssr-module-loader/loader.d.ts.map +1 -1
- package/esm/src/modules/react-loader/ssr-module-loader/loader.js +148 -20
- package/esm/src/observability/tracing/span-names.d.ts +2 -0
- package/esm/src/observability/tracing/span-names.d.ts.map +1 -1
- package/esm/src/observability/tracing/span-names.js +2 -0
- package/esm/src/platform/adapters/fs/cache/file-cache.d.ts.map +1 -1
- package/esm/src/platform/adapters/fs/cache/file-cache.js +9 -3
- package/esm/src/rendering/orchestrator/module-loader/cache.d.ts +10 -2
- package/esm/src/rendering/orchestrator/module-loader/cache.d.ts.map +1 -1
- package/esm/src/rendering/orchestrator/module-loader/cache.js +11 -6
- package/esm/src/rendering/orchestrator/module-loader/index.d.ts.map +1 -1
- package/esm/src/rendering/orchestrator/module-loader/index.js +72 -77
- package/esm/src/server/context/cache-invalidation.d.ts.map +1 -1
- package/esm/src/server/context/cache-invalidation.js +4 -0
- package/esm/src/server/handlers/dev/dashboard/api.js +4 -0
- package/esm/src/server/handlers/dev/projects/ui-handler.d.ts.map +1 -1
- package/esm/src/server/handlers/dev/projects/ui-handler.js +6 -0
- package/esm/src/transforms/esm/http-cache.d.ts.map +1 -1
- package/esm/src/transforms/esm/http-cache.js +145 -93
- package/esm/src/transforms/esm/transform-cache.d.ts +25 -0
- package/esm/src/transforms/esm/transform-cache.d.ts.map +1 -1
- package/esm/src/transforms/esm/transform-cache.js +45 -0
- package/esm/src/transforms/mdx/esm-module-loader/module-fetcher/index.d.ts.map +1 -1
- package/esm/src/transforms/mdx/esm-module-loader/module-fetcher/index.js +2 -36
- package/esm/src/utils/constants/cache.d.ts +4 -0
- package/esm/src/utils/constants/cache.d.ts.map +1 -1
- package/esm/src/utils/constants/cache.js +14 -1
- package/esm/src/utils/index.d.ts +1 -1
- package/esm/src/utils/index.d.ts.map +1 -1
- package/esm/src/utils/index.js +1 -1
- package/package.json +2 -1
- package/src/deno.js +1 -1
- package/src/proxy/cache/index.ts +93 -0
- package/src/proxy/cache/memory-cache.ts +120 -0
- package/src/proxy/cache/redis-cache.ts +203 -0
- package/src/proxy/cache/resilient-cache.ts +205 -0
- package/src/proxy/cache/types.ts +72 -0
- package/src/proxy/handler.ts +593 -0
- package/src/proxy/logger.ts +329 -0
- package/src/proxy/oauth-client.ts +91 -0
- package/src/proxy/token-manager.ts +174 -0
- package/src/proxy/tracing.ts +237 -0
- package/src/src/cache/backend.ts +65 -0
- package/src/src/cache/cache-key-builder.ts +0 -9
- package/src/src/cache/hash.ts +205 -0
- package/src/src/cache/index.ts +3 -0
- package/src/src/cache/module-cache.ts +252 -0
- package/src/src/cache/multi-tier.ts +462 -0
- package/src/src/cli/app/actions.ts +190 -0
- package/src/src/cli/app/components/inline-input.ts +255 -0
- package/src/src/cli/app/components/list-select.ts +215 -0
- package/src/src/cli/app/index.ts +1471 -0
- package/src/src/cli/app/state.ts +385 -0
- package/src/src/cli/app/views/dashboard.ts +212 -0
- package/src/src/cli/index/command-router.ts +9 -40
- package/src/src/cli/index/start-handler.ts +195 -0
- package/src/src/cli/mcp/index.ts +11 -0
- package/src/src/cli/templates/integration-loader.ts +2 -8
- package/src/src/middleware/builtin/security/redis-rate-limit.ts +24 -11
- package/src/src/modules/react-loader/ssr-module-loader/cache/redis.ts +36 -50
- package/src/src/modules/react-loader/ssr-module-loader/loader.ts +168 -25
- package/src/src/observability/tracing/span-names.ts +2 -0
- package/src/src/platform/adapters/fs/cache/file-cache.ts +9 -3
- package/src/src/rendering/orchestrator/module-loader/cache.ts +14 -8
- package/src/src/rendering/orchestrator/module-loader/index.ts +94 -89
- package/src/src/server/context/cache-invalidation.ts +4 -0
- package/src/src/server/handlers/dev/dashboard/api.ts +2 -0
- package/src/src/server/handlers/dev/projects/ui-handler.ts +6 -0
- package/src/src/transforms/esm/http-cache.ts +160 -105
- package/src/src/transforms/esm/transform-cache.ts +53 -0
- package/src/src/transforms/mdx/esm-module-loader/module-fetcher/index.ts +2 -40
- package/src/src/utils/constants/cache.ts +21 -1
- 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(
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
54
|
+
return null;
|
|
71
55
|
}
|
|
72
56
|
|
|
73
57
|
export async function getFromRedis(cacheKey: string): Promise<string | null> {
|
|
74
|
-
|
|
58
|
+
const backend = await getDistributedCache();
|
|
59
|
+
if (!backend) return null;
|
|
75
60
|
|
|
76
61
|
try {
|
|
77
|
-
return await
|
|
62
|
+
return await backend.get(cacheKey);
|
|
78
63
|
} catch (error) {
|
|
79
|
-
logger.debug("[SSR-MODULE-LOADER]
|
|
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
|
-
|
|
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
|
|
81
|
+
await backend.set(cacheKey, code, ttl);
|
|
96
82
|
} catch (error) {
|
|
97
|
-
logger.debug("[SSR-MODULE-LOADER]
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
-
|
|
381
|
-
const redisClient = getRedisClientInstance();
|
|
382
|
-
if (redisEnabled && redisClient) {
|
|
478
|
+
if (isSSRDistributedCacheEnabled()) {
|
|
383
479
|
const redisCode = await getFromRedis(contentCacheKey);
|
|
384
480
|
if (redisCode) {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
|
|
509
|
+
const entry: ModuleCacheEntry = { tempPath, contentHash };
|
|
510
|
+
globalModuleCache.set(contentCacheKey, entry);
|
|
511
|
+
globalModuleCache.set(filePathCacheKey, entry);
|
|
394
512
|
|
|
395
|
-
|
|
396
|
-
|
|
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 (
|
|
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",
|