@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 313 lines 10.7 KB
Raw
1 import { hashDockerBuildInputs, selectDockerBuildContext } from "@alchemy.run/docker/BuildHash";
2 import { initialCwd } from "alchemy/Util/Node";
3 import { sha256Object } from "alchemy/Util/sha256";
4 import { tarGzipDirectory } from "alchemy/Util/tarGzip";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Path from "effect/Path";
9 import * as Result from "effect/Result";
10
11 const DEFAULT_DOCKERFILE = "Dockerfile";
12 const RAILWAY_BUILD_PLATFORM = "linux/amd64";
13 const MAX_CONTEXT_BYTES = 32 * 1024 * 1024;
14 const MAX_CONTEXT_ENTRIES = 10_000;
15 const TAR_BLOCK_BYTES = 512;
16
17 export interface RailwayLocalContextSource {
18 readonly context: string;
19 readonly dockerfilePath?: string;
20 }
21
22 export class ServiceImageOrMainRequired extends Data.TaggedError(
23 "Railway.ServiceImageOrMainRequired",
24 )<{ message: string }> {}
25
26 export class ServiceSourceInvalid extends Data.TaggedError("Railway.ServiceSourceInvalid")<{
27 message: string;
28 }> {}
29
30 export class ServiceDockerfilePathInvalid extends Data.TaggedError(
31 "Railway.ServiceDockerfilePathInvalid",
32 )<{ dockerfilePath: string; message: string }> {}
33
34 export class ServiceContextPathInvalid extends Data.TaggedError(
35 "Railway.ServiceContextPathInvalid",
36 )<{ path: string; expected: "directory" | "file" }> {
37 override get message() {
38 return `Railway.Service expected ${this.path} to be an existing ${this.expected}`;
39 }
40 }
41
42 export class ServiceDockerfileOutsideContext extends Data.TaggedError(
43 "Railway.ServiceDockerfileOutsideContext",
44 )<{ context: string; dockerfile: string }> {}
45
46 export class ServiceContextSymlinkUnsupported extends Data.TaggedError(
47 "Railway.ServiceContextSymlinkUnsupported",
48 )<{ path: string }> {}
49
50 export class ServiceContextPathUnsupported extends Data.TaggedError(
51 "Railway.ServiceContextPathUnsupported",
52 )<{ path: string }> {}
53
54 export class ServiceContextTooLarge extends Data.TaggedError("Railway.ServiceContextTooLarge")<{
55 limit: number;
56 size: number;
57 }> {}
58
59 export type RailwayServiceSource =
60 | { readonly mode: "main"; readonly main: string }
61 | {
62 readonly mode: "context";
63 readonly context: string;
64 readonly dockerfilePath: string | undefined;
65 }
66 | { readonly mode: "image"; readonly image: string }
67 | { readonly mode: "repo"; readonly repo: string };
68
69 const nonEmpty = (value: string | undefined): value is string =>
70 value !== undefined && value.length > 0;
71
72 export const resolveRailwayServiceSource = Effect.fn(function* (props: {
73 readonly main?: string;
74 readonly context?: string;
75 readonly image?: string;
76 readonly repo?: string;
77 readonly dockerfilePath?: string;
78 }) {
79 const main = nonEmpty(props.main) ? props.main : undefined;
80 const context = nonEmpty(props.context) ? props.context : undefined;
81 const image = nonEmpty(props.image) ? props.image : undefined;
82 const repo = nonEmpty(props.repo) ? props.repo : undefined;
83 const modes = [
84 main === undefined ? undefined : "main",
85 context === undefined ? undefined : "context",
86 repo === undefined ? undefined : "repo",
87 main === undefined && image !== undefined ? "image" : undefined,
88 ].filter((mode): mode is RailwayServiceSource["mode"] => mode !== undefined);
89
90 if (modes.length === 0) {
91 return yield* new ServiceImageOrMainRequired({
92 message: "Railway.Service requires `image`, `main`, `context`, or `repo`.",
93 });
94 }
95 if (modes.length !== 1) {
96 return yield* new ServiceSourceInvalid({
97 message:
98 "Railway.Service requires exactly one source: `main`, `context`, `image`, or `repo` (`image` may configure the base image for `main`).",
99 });
100 }
101 switch (modes[0]) {
102 case "main":
103 return { mode: "main", main: main! } as const;
104 case "context":
105 return {
106 mode: "context",
107 context: context!,
108 dockerfilePath: props.dockerfilePath,
109 } as const;
110 case "image":
111 return { mode: "image", image: image! } as const;
112 case "repo":
113 return { mode: "repo", repo: repo! } as const;
114 default:
115 return yield* new ServiceSourceInvalid({
116 message: "Railway.Service source could not be resolved",
117 });
118 }
119 });
120
121 const resolveBuild = Effect.fn(function* (source: RailwayLocalContextSource) {
122 const fs = yield* FileSystem.FileSystem;
123 const path = yield* Path.Path;
124 const context = path.resolve(initialCwd, source.context);
125 const dockerfilePath = source.dockerfilePath ?? DEFAULT_DOCKERFILE;
126 if (dockerfilePath.length === 0 || path.isAbsolute(dockerfilePath)) {
127 return yield* new ServiceDockerfilePathInvalid({
128 dockerfilePath,
129 message: "Railway.Service `dockerfilePath` must be a non-empty path relative to `context`",
130 });
131 }
132 if (!(yield* fs.exists(context))) {
133 return yield* new ServiceContextPathInvalid({
134 path: context,
135 expected: "directory",
136 });
137 }
138 if (Result.isSuccess(yield* Effect.result(fs.readLink(context)))) {
139 return yield* new ServiceContextSymlinkUnsupported({ path: context });
140 }
141 const contextInfo = yield* fs.stat(context);
142 if (contextInfo.type !== "Directory") {
143 return yield* new ServiceContextPathInvalid({
144 path: context,
145 expected: "directory",
146 });
147 }
148 const dockerfile = path.resolve(context, dockerfilePath);
149 if (!(yield* fs.exists(dockerfile))) {
150 return yield* new ServiceContextPathInvalid({
151 path: dockerfile,
152 expected: "file",
153 });
154 }
155 const selected = yield* selectDockerBuildContext({ context, dockerfile });
156 if (selected.dockerfilePath === undefined) {
157 return yield* new ServiceDockerfileOutsideContext({
158 context,
159 dockerfile,
160 });
161 }
162 const parts = selected.dockerfilePath.split("/");
163 let current = context;
164 for (const part of parts) {
165 current = path.join(current, part);
166 if (Result.isSuccess(yield* Effect.result(fs.readLink(current)))) {
167 return yield* new ServiceContextSymlinkUnsupported({ path: current });
168 }
169 }
170 const dockerfileInfo = yield* fs.stat(dockerfile);
171 if (dockerfileInfo.type !== "File") {
172 return yield* new ServiceContextPathInvalid({
173 path: dockerfile,
174 expected: "file",
175 });
176 }
177 return {
178 build: {
179 context,
180 dockerfile,
181 platform: RAILWAY_BUILD_PLATFORM,
182 },
183 selected,
184 };
185 });
186
187 const paddedTarBytes = (size: number) => Math.ceil(size / TAR_BLOCK_BYTES) * TAR_BLOCK_BYTES;
188
189 const fitsUstarPath = (relativePath: string) => {
190 if (relativePath.length <= 100) return true;
191 for (let index = relativePath.length - 101; index >= 1; index--) {
192 if (relativePath[index] !== "/") continue;
193 const nameLength = relativePath.length - index - 1;
194 if (index <= 155 && nameLength > 0 && nameLength <= 100) return true;
195 }
196 return false;
197 };
198
199 const tarEntryBytes = (relativePath: string, type: "Directory" | "File", size: number) => {
200 const tarPath = type === "Directory" ? `${relativePath}/` : relativePath;
201 const longNameBytes = fitsUstarPath(tarPath)
202 ? 0
203 : TAR_BLOCK_BYTES + paddedTarBytes(tarPath.length + 1);
204 return longNameBytes + TAR_BLOCK_BYTES + (type === "File" ? paddedTarBytes(size) : 0);
205 };
206
207 const checkContextLimit = (size: number, entries: number) =>
208 size > MAX_CONTEXT_BYTES || entries > MAX_CONTEXT_ENTRIES
209 ? Effect.fail(
210 new ServiceContextTooLarge({
211 limit: MAX_CONTEXT_BYTES,
212 size,
213 }),
214 )
215 : Effect.void;
216
217 const selectedEntries = Effect.fn(function* (selected: {
218 context: string;
219 includes: (relativePath: string) => boolean;
220 }) {
221 const fs = yield* FileSystem.FileSystem;
222 const path = yield* Path.Path;
223 const entries: Array<{
224 entry: string;
225 sourcePath: string;
226 type: "Directory" | "File";
227 mode: number;
228 }> = [];
229 let size = 2 * TAR_BLOCK_BYTES;
230 let entryCount = 0;
231 for (const entry of (yield* fs.readDirectory(selected.context, {
232 recursive: true,
233 })).sort()) {
234 const relative = entry.replaceAll("\\", "/");
235 if (!selected.includes(relative)) continue;
236 const sourcePath = path.join(selected.context, entry);
237 if (/[^\x20-\x7e]/.test(relative)) {
238 return yield* new ServiceContextPathUnsupported({ path: sourcePath });
239 }
240 if (Result.isSuccess(yield* Effect.result(fs.readLink(sourcePath)))) {
241 return yield* new ServiceContextSymlinkUnsupported({ path: sourcePath });
242 }
243 const info = yield* fs.stat(sourcePath);
244 if (info.type !== "Directory" && info.type !== "File") continue;
245 entryCount += 1;
246 size += tarEntryBytes(relative, info.type, Number(info.size));
247 yield* checkContextLimit(size, entryCount);
248 entries.push({
249 entry,
250 sourcePath,
251 type: info.type,
252 mode: info.mode & 0o7777,
253 });
254 }
255 return entries;
256 });
257
258 const codeHash = Effect.fn(function* (input: { context: string; dockerfilePath: string }) {
259 const contextHash = yield* hashDockerBuildInputs(
260 {
261 context: input.context,
262 dockerfile: input.dockerfilePath,
263 platform: RAILWAY_BUILD_PLATFORM,
264 },
265 "effective",
266 );
267 return (yield* sha256Object({
268 contextHash,
269 dockerfilePath: input.dockerfilePath,
270 })).slice(0, 16);
271 });
272
273 export const hashRailwayLocalContext = Effect.fn(function* (source: RailwayLocalContextSource) {
274 const { build, selected } = yield* resolveBuild(source);
275 yield* selectedEntries(selected);
276 return yield* codeHash({
277 context: build.context,
278 dockerfilePath: selected.dockerfilePath!,
279 });
280 });
281
282 export const prepareRailwayLocalContext = Effect.fn(function* (source: RailwayLocalContextSource) {
283 const fs = yield* FileSystem.FileSystem;
284 const path = yield* Path.Path;
285 const { selected } = yield* resolveBuild(source);
286 const snapshot = yield* fs.makeTempDirectoryScoped({
287 prefix: "alchemy-railway-context-",
288 });
289 const entries = yield* selectedEntries(selected);
290 let copiedSize = 2 * TAR_BLOCK_BYTES;
291 let copiedEntries = 0;
292 for (const entry of entries) {
293 const destination = path.join(snapshot, entry.entry);
294 copiedEntries += 1;
295 if (entry.type === "Directory") {
296 copiedSize += tarEntryBytes(entry.entry, entry.type, 0);
297 yield* checkContextLimit(copiedSize, copiedEntries);
298 yield* fs.makeDirectory(destination, { recursive: true });
299 yield* fs.chmod(destination, entry.mode);
300 } else {
301 yield* fs.makeDirectory(path.dirname(destination), { recursive: true });
302 const contents = yield* fs.readFile(entry.sourcePath);
303 copiedSize += tarEntryBytes(entry.entry, entry.type, contents.byteLength);
304 yield* checkContextLimit(copiedSize, copiedEntries);
305 yield* fs.writeFile(destination, contents);
306 yield* fs.chmod(destination, entry.mode);
307 }
308 }
309 const dockerfilePath = selected.dockerfilePath!;
310 const hash = yield* codeHash({ context: snapshot, dockerfilePath });
311 const tarball = yield* tarGzipDirectory(snapshot, { preserveMode: true });
312 return { codeHash: hash, dockerfilePath, tarball };
313 });