@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, 04:45 UTC

main
ts 92 lines 3.32 KB
Raw
1 import { createPhysicalName } from "alchemy/PhysicalName";
2 import * as Effect from "effect/Effect";
3
4 /**
5 * Railway has no labels. Ownership is stamped into the physical name via
6 * `createPhysicalName` (lowercase, max 32, leading letter). Railway's
7 * `projectCreate` rejects longer generated names (`Invalid project name`).
8 * `list()` filters with {@link matchesAlchemyPhysicalName} so nuke does
9 * not enumerate the whole workspace.
10 */
11 export const RAILWAY_NAME_MAX_LENGTH = 32;
12
13 /**
14 * Railway generates `{serviceName}-{environmentName}.up.railway.app` on
15 * `serviceDomainCreate`. That first DNS label must be ≤ 63 characters
16 * (`32 + 1 + 32 = 65` fails with "please try again"). Extra environments
17 * stay shorter so a 32-char service name still fits.
18 */
19 export const RAILWAY_ENVIRONMENT_NAME_MAX_LENGTH = 24;
20
21 const clipName = (raw: string, maxLength: number) => {
22 const clipped = raw.length > maxLength ? raw.slice(0, maxLength) : raw;
23 const forced = /^[a-z]/.test(clipped) ? clipped : `r${clipped}`.slice(0, maxLength);
24 return forced.replace(/-+$/g, "") || "r";
25 };
26
27 /**
28 * Railway Project / Service / Volume / Variable physical names:
29 * `createPhysicalName({ lowercase: true, maxLength: 32 })`, then force a
30 * leading letter (`r` prefix if needed). Unique per workspace.
31 */
32 export const createRailwayName = Effect.fn(function* (id: string) {
33 const raw = yield* createPhysicalName({
34 id,
35 lowercase: true,
36 maxLength: RAILWAY_NAME_MAX_LENGTH,
37 });
38 return clipName(raw, RAILWAY_NAME_MAX_LENGTH);
39 });
40
41 /** Extra-environment names. See {@link RAILWAY_ENVIRONMENT_NAME_MAX_LENGTH}. */
42 export const createRailwayEnvironmentName = Effect.fn(function* (id: string) {
43 const raw = yield* createPhysicalName({
44 id,
45 lowercase: true,
46 maxLength: RAILWAY_ENVIRONMENT_NAME_MAX_LENGTH,
47 });
48 return clipName(raw, RAILWAY_ENVIRONMENT_NAME_MAX_LENGTH);
49 });
50
51 /**
52 * Sanitize a user-supplied Railway name: lowercase, DNS-compatible
53 * (`[a-z0-9-]`), force a leading letter, max 32 chars.
54 */
55 const sanitizeTo = (name: string, maxLength: number): string => {
56 const lowered = name
57 .toLowerCase()
58 .replace(/[^a-z0-9-]+/g, "-")
59 .replace(/-+/g, "-")
60 .replace(/^-+|-+$/g, "");
61 return clipName(lowered.length === 0 ? "r" : lowered, maxLength);
62 };
63
64 export const sanitizeRailwayName = (name: string): string =>
65 sanitizeTo(name, RAILWAY_NAME_MAX_LENGTH);
66
67 export const sanitizeRailwayEnvironmentName = (name: string): string =>
68 sanitizeTo(name, RAILWAY_ENVIRONMENT_NAME_MAX_LENGTH);
69
70 export const sanitize = sanitizeRailwayName;
71
72 /**
73 * True when `name` matches the `createPhysicalName` + leading-letter shape
74 * used for alchemy-owned Railway resources.
75 *
76 * Untruncated names end with a hyphen plus an 8–16 char RFC4648 base32
77 * instance suffix. Truncated 32-char names keep that suffix (the human
78 * prefix is what gets cut).
79 */
80 export const matchesAlchemyPhysicalName = (name: string | undefined): boolean => {
81 if (name === undefined || name.length === 0 || name.length > RAILWAY_NAME_MAX_LENGTH) {
82 return false;
83 }
84 if (!/^[a-z][a-z0-9-]*$/.test(name)) return false;
85 const parts = name.split("-");
86 const last = parts.at(-1) ?? "";
87 if (parts.length >= 2 && last.length >= 8 && last.length <= 16 && /^[a-z2-7]+$/.test(last)) {
88 return true;
89 }
90 const compact = name.replaceAll("-", "");
91 return /[a-z2-7]{16}$/.test(compact);
92 };