@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 381 lines 15.4 KB
Raw
1 import { createRequire } from "node:module";
2 import { dotAlchemyDirectory } from "alchemy/AlchemyContext";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { isPathWithin } from "alchemy/Util/isPathWithin";
7 import { initialCwd } from "alchemy/Util/Node";
8 import { loadFrontendCore } from "alchemy/Website/FrontendCore";
9 import { packSiteExtraFiles } from "alchemy/Website/packExtraFiles";
10 import * as Data from "effect/Data";
11 import * as Effect from "effect/Effect";
12 import * as FileSystem from "effect/FileSystem";
13 import * as Path from "effect/Path";
14 import type { PlatformError } from "effect/PlatformError";
15 import { createComputeArchive } from "../ComputeArchive.ts";
16
17 export class ArtifactOperationFailed extends Data.TaggedError("ArtifactOperationFailed")<{
18 readonly message: string;
19 readonly cause?: unknown;
20 }> {}
21
22 /** Resolved framework output to stage after Website.Server has built it. */
23 export interface WebsiteArtifactProps {
24 /** Application root, relative to the initial working directory. */
25 root: string;
26 /** Build output directory, relative to the initial working directory. */
27 distDir: string;
28 /** Node serve entry, relative to the initial working directory. */
29 serverEntry?: string;
30 /** Generate the shared static-file server instead of tracing a framework entry. */
31 static?: {
32 /** Unmatched-path behavior. @default "none" */
33 notFoundHandling?: "none" | "spa" | "404-page";
34 /** Extensionless HTML handling. @default "none" */
35 htmlHandling?: "none" | "drop-trailing-slash";
36 /** HTML file served for misses, relative to the static output. @default "404.html" */
37 errorPage?: string;
38 };
39 /** Next.js keeps its output and configuration beside the serve entry. */
40 layout?: "output" | "next";
41 /** Build output fingerprint; keeps the build dependency explicit. */
42 buildHash?: string;
43 }
44
45 /** Internal, filesystem-only artifact lifecycle; never creates cloud resources. */
46 export interface WebsiteArtifact extends Resource<
47 "Prisma.WebsiteArtifact",
48 WebsiteArtifactProps,
49 {
50 /** Private directory owned by this artifact resource. */
51 directory: string;
52 /** Verified Compute tarball path. */
53 artifactPath: string;
54 /** SHA-256 of the tarball uploaded by Compute. */
55 hash: string;
56 }
57 > {}
58
59 export const WebsiteArtifact = Resource<WebsiteArtifact>("Prisma.WebsiteArtifact");
60
61 const MAX_ENTRIES = 50_000;
62 const MAX_BYTES = 768 * 1024 * 1024;
63 const MAX_FILE_BYTES = 128 * 1024 * 1024;
64 const excluded = (relative: string) =>
65 relative
66 .split(/[\\/]/)
67 .some((part) =>
68 /^(?:\.git|\.alchemy|\.env(?:\..*)?|\.envrc|\.npmrc|\.yarnrc.*|\.netrc|\.pypirc|\.aws|\.ssh)$/.test(
69 part,
70 ),
71 );
72 const javascript = (file: string) => /\.[cm]?[jt]sx?$/.test(file);
73 const nextExcluded = (relative: string) =>
74 /^\.next\/(?:cache|standalone|types|diagnostics|dev)(?:\/|$)/.test(relative) ||
75 /^\.next\/trace(?:-|$)/.test(relative);
76
77 export class WebsiteTraceError extends Data.TaggedError("WebsiteTraceError")<{
78 readonly message: string;
79 readonly cause?: unknown;
80 }> {}
81
82 const fail = (message: string) => Effect.fail(new ArtifactOperationFailed({ message }));
83
84 /**
85 * Stage real build files and NFT's reachable runtime graph without relocating
86 * individual modules. Keeping their relative layout preserves import.meta.url,
87 * package scopes, pnpm links, and dynamically loaded framework chunks.
88 * The caller owns the surrounding Scope and must archive before it closes.
89 */
90 export const stageWebsiteArtifact = Effect.fn(function* (props: WebsiteArtifactProps) {
91 const fs = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const root = yield* fs.realPath(path.resolve(initialCwd, props.root));
94 const runtimeBase = process.cwd();
95 const dotAlchemy = yield* dotAlchemyDirectory;
96 const dist = yield* fs.realPath(path.resolve(initialCwd, props.distDir));
97 const entry =
98 props.serverEntry === undefined
99 ? undefined
100 : yield* fs.realPath(path.resolve(initialCwd, props.serverEntry));
101 if (entry === undefined && props.static === undefined) {
102 return yield* fail(
103 "A website artifact requires a Node server entry or static serving options.",
104 );
105 }
106 if (entry !== undefined && (yield* fs.stat(entry)).type !== "File") {
107 return yield* fail(`Website server entry is not a file: ${entry}`);
108 }
109 if (props.layout !== "next" && dist === root) {
110 return yield* fail(
111 "Website output must be a dedicated build directory, not the application root.",
112 );
113 }
114 const files = new Set<string>(entry === undefined ? [] : [entry]);
115 const seeds = new Set<string>(entry === undefined ? [] : [entry]);
116 const manifests: string[] = [];
117 let visited = 0;
118 const collect: (
119 source: string,
120 ancestors?: ReadonlySet<string>,
121 runtimePackage?: boolean,
122 boundary?: string,
123 ) => Effect.Effect<void, Error | PlatformError> = Effect.fn(function* (
124 source,
125 ancestors = new Set(),
126 runtimePackage = false,
127 boundary,
128 ) {
129 if (++visited > MAX_ENTRIES)
130 return yield* fail("Website artifact exceeds the 50,000-entry safety limit.");
131 const relative = path.relative(root, source).replaceAll("\\", "/");
132 if (
133 excluded(relative) ||
134 isPathWithin(dotAlchemy, source, runtimeBase) ||
135 (runtimePackage && /(?:\.map|\.d\.ts)$/.test(source)) ||
136 (props.layout === "next" && nextExcluded(relative))
137 )
138 return;
139 const real = yield* fs.realPath(source);
140 const relativeToBoundary = path.relative(boundary ?? real, real);
141 if (
142 relativeToBoundary === ".." ||
143 relativeToBoundary.startsWith(`..${path.sep}`) ||
144 path.isAbsolute(relativeToBoundary)
145 ) {
146 return yield* fail(`Website output symlink escapes its selected directory: ${source}`);
147 }
148 const stat = yield* fs.stat(real);
149 if (stat.type === "Directory") {
150 if (ancestors.has(real)) return yield* fail(`Cyclic website output symlink: ${source}`);
151 const next = new Set([...ancestors, real]);
152 for (const name of yield* fs.readDirectory(source)) {
153 // Runtime dependencies are traced, never copied wholesale.
154 if (name === "node_modules") continue;
155 yield* collect(path.join(source, name), next, runtimePackage, boundary ?? real);
156 }
157 } else if (stat.type === "File") {
158 files.add(source);
159 if (!runtimePackage && props.static === undefined && javascript(source)) seeds.add(source);
160 if (props.layout === "next" && source.endsWith(".nft.json")) manifests.push(source);
161 } else {
162 return yield* fail(`Unsupported website output file: ${source}`);
163 }
164 });
165
166 const extras = yield* packSiteExtraFiles(
167 props.layout === "next" ? root : dist,
168 props.layout === "next" ? "next" : "client",
169 );
170 if (props.layout === "next" && !(yield* fs.exists(path.join(root, ".next", "BUILD_ID")))) {
171 return yield* fail(
172 "Next.js output is missing .next/BUILD_ID; run the production build before packaging.",
173 );
174 }
175 for (const extra of extras ?? []) yield* collect(extra.source);
176 // Also include config extensions not currently listed by packSiteExtraFiles.
177 if (props.layout === "next") {
178 for (const name of ["next.config.mts", "next.config.cts"]) {
179 const config = path.join(root, name);
180 if (yield* fs.exists(config)) yield* collect(config);
181 }
182 }
183
184 if (props.layout === "next") {
185 const nextPackage = yield* Effect.try(() =>
186 createRequire(path.join(root, "package.json")).resolve("next/package.json"),
187 );
188 const nextRoot = path.dirname(nextPackage);
189 // Next's custom server resolves internal modules through runtime alias
190 // tables that NFT cannot evaluate. Keep that package, not node_modules.
191 yield* collect(nextRoot, undefined, true);
192 yield* collect(path.join(nextRoot, "dist", "compiled", "webpack"));
193 }
194
195 for (const manifest of manifests) {
196 const text = yield* fs.readFileString(manifest);
197 const parsed = yield* Effect.try(() => JSON.parse(text) as { files?: unknown });
198 if (
199 !Array.isArray(parsed.files) ||
200 !parsed.files.every((file): file is string => typeof file === "string")
201 ) {
202 return yield* fail(`Invalid Next.js trace manifest: ${manifest}`);
203 }
204 for (const file of parsed.files) {
205 const source = path.resolve(path.dirname(manifest), file);
206 if (excluded(path.relative(root, source)) || isPathWithin(dotAlchemy, source, runtimeBase))
207 return yield* fail(`A Next.js runtime dependency is a sensitive file: ${source}`);
208 files.add(source);
209 if (javascript(source)) seeds.add(source);
210 }
211 }
212
213 const traceBase = path.parse(root).root;
214 const trace =
215 seeds.size === 0
216 ? undefined
217 : yield* Effect.tryPromise({
218 try: () =>
219 import("@vercel/nft").then(({ nodeFileTrace }) =>
220 nodeFileTrace([...seeds], {
221 base: traceBase,
222 processCwd: root,
223 conditions: ["node", "production"],
224 analysis: {
225 emitGlobs: true,
226 computeFileReferences: true,
227 evaluatePureExpressions: true,
228 },
229 }),
230 ),
231 catch: (cause) =>
232 new WebsiteTraceError({
233 message:
234 "Failed to trace website runtime dependencies. Install @vercel/nft in the deployment workspace.",
235 cause,
236 }),
237 }).pipe(Effect.timeout("90 seconds"));
238 if (trace !== undefined) {
239 for (const file of trace.fileList) {
240 const source = path.resolve(traceBase, file);
241 // NFT records symlinks encountered while resolving optional imports,
242 // even when their targets are absent. They are lookup evidence, not
243 // runtime files. Explicit build/manifest files remain required.
244 if (
245 !files.has(source) &&
246 trace.reasons.get(file)?.type.every((type) => type === "resolve") &&
247 !(yield* fs.exists(source))
248 )
249 continue;
250 files.add(source);
251 }
252 }
253 if (files.size > MAX_ENTRIES)
254 return yield* fail("Website dependency trace exceeds the 50,000-entry safety limit.");
255
256 const links = new Map<string, string>();
257 const selected = new Set<string>();
258 for (const file of files) {
259 if (excluded(path.relative(root, file)) || isPathWithin(dotAlchemy, file, runtimeBase))
260 return yield* fail(`A website runtime dependency is a sensitive file: ${file}`);
261 const link = yield* fs.readLink(file).pipe(Effect.orElseSucceed(() => undefined));
262 if (link !== undefined) {
263 const target = yield* fs.realPath(file);
264 links.set(file, target);
265 if ((yield* fs.stat(target)).type === "File") selected.add(yield* fs.realPath(target));
266 } else {
267 const stat = yield* fs.stat(file);
268 if (stat.type !== "File")
269 return yield* fail(`A traced runtime dependency is not a file: ${file}`);
270 selected.add(file);
271 }
272 }
273 let base = root;
274 const within = (directory: string, file: string) => {
275 const relative = path.relative(directory, file);
276 return relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
277 };
278 for (const file of [...selected, ...links.keys(), ...links.values()]) {
279 while (!within(base, file)) base = path.dirname(base);
280 }
281 const directory = yield* fs.makeTempDirectoryScoped({
282 prefix: "alchemy-prisma-website-",
283 });
284 const staged = (file: string) => path.join(directory, "files", path.relative(base, file));
285 let bytes = 0;
286 for (const file of [...selected].sort()) {
287 const stat = yield* fs.stat(file);
288 const size = Number(stat.size);
289 bytes += size;
290 if (size > MAX_FILE_BYTES || bytes > MAX_BYTES)
291 return yield* fail(
292 "Website artifact exceeds Prisma Compute's file or total byte safety limit.",
293 );
294 const dest = staged(file);
295 yield* fs.makeDirectory(path.dirname(dest), { recursive: true });
296 yield* fs.copyFile(file, dest);
297 yield* fs.chmod(dest, stat.mode & 0o777);
298 }
299 for (const [file, target] of links) {
300 // NFT also reports resolution-only links with no retained runtime files.
301 if (!(yield* fs.exists(staged(target)))) continue;
302 const dest = staged(file);
303 if (yield* fs.exists(dest)) continue;
304 yield* fs.makeDirectory(path.dirname(dest), { recursive: true });
305 yield* fs.symlink(path.relative(path.dirname(dest), staged(target)), dest);
306 }
307 if (props.static !== undefined) {
308 const { makeNodeServeEntrySource } = yield* loadFrontendCore;
309 const client = path.relative(directory, staged(dist)).replaceAll("\\", "/");
310 yield* fs.writeFileString(
311 path.join(directory, "server.mjs"),
312 makeNodeServeEntrySource({
313 ...props.static,
314 clientDirExpression: `fileURLToPath(new URL(${JSON.stringify(`./${client}/`)}, import.meta.url))`,
315 }),
316 );
317 return { directory, entrypoint: "server.mjs", requiredFiles: [] };
318 }
319 if (entry === undefined) return yield* fail("Missing website server entry.");
320 const entryRelative = path.relative(directory, staged(entry)).replaceAll("\\", "/");
321 const cwdRelative = path.relative(directory, staged(root)).replaceAll("\\", "/");
322 yield* fs.makeDirectory(staged(root), { recursive: true });
323 yield* fs.writeFileString(
324 path.join(directory, "server.mjs"),
325 [
326 'import { fileURLToPath } from "node:url";',
327 `process.chdir(fileURLToPath(new URL(${JSON.stringify(`./${cwdRelative}/`)}, import.meta.url)));`,
328 `await import(new URL(${JSON.stringify(`./${entryRelative}`)}, import.meta.url).href);`,
329 "",
330 ].join("\n"),
331 );
332 return {
333 directory,
334 entrypoint: "server.mjs",
335 requiredFiles: [entryRelative],
336 };
337 });
338
339 export const WebsiteArtifactProvider = () =>
340 Provider.effect(
341 WebsiteArtifact,
342 Effect.gen(function* () {
343 const fs = yield* FileSystem.FileSystem;
344 const path = yield* Path.Path;
345 const dotAlchemy = yield* dotAlchemyDirectory;
346 return {
347 list: () => Effect.succeed([]),
348 // Traced dependencies can change independently of the framework's hash.
349 diff: () => Effect.succeed({ action: "update" as const }),
350 reconcile: Effect.fn(function* ({ id, news }) {
351 const name = yield* createPhysicalName({ id, maxLength: 80 });
352 const directory = path.join(dotAlchemy, "prisma-websites", name);
353 return yield* Effect.scoped(
354 Effect.gen(function* () {
355 const staged = yield* stageWebsiteArtifact(news);
356 const archive = yield* createComputeArchive({
357 ...staged,
358 output: "file",
359 });
360 return yield* Effect.gen(function* () {
361 yield* fs.makeDirectory(directory, { recursive: true });
362 const artifactPath = path.join(directory, `${archive.sha256}.tar.gz`);
363 yield* fs.copyFile(archive.path, artifactPath);
364 for (const old of yield* fs.readDirectory(directory)) {
365 if (old !== path.basename(artifactPath) && /^[a-f0-9]{64}\.tar\.gz$/.test(old)) {
366 yield* fs.remove(path.join(directory, old), {
367 force: true,
368 });
369 }
370 }
371 return { directory, artifactPath, hash: archive.sha256 };
372 }).pipe(Effect.ensuring(archive.cleanup));
373 }),
374 );
375 }),
376 delete: Effect.fn(function* ({ output }) {
377 yield* fs.remove(output.directory, { recursive: true, force: true });
378 }),
379 };
380 }),
381 );