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

main
ts 79 lines 2.26 KB
Raw
1 export const MAX_BYTES = 10 * 1024 * 1024;
2 export const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3
4 export interface UploadInput {
5 filename: string;
6 contentType: string;
7 size: number;
8 }
9
10 export interface UploadRow {
11 id: string;
12 filename: string;
13 object_key: string;
14 content_type: string;
15 expected_bytes: number | string;
16 actual_bytes: number | string | null;
17 status: "awaiting_upload" | "ready" | "rejected";
18 created_at: string;
19 }
20
21 // Effect Postgres decodes int8 as bigint and timestamps as epoch milliseconds.
22 export interface UploadRecord extends Omit<
23 UploadRow,
24 "expected_bytes" | "actual_bytes" | "created_at"
25 > {
26 expected_bytes: bigint;
27 actual_bytes: bigint | null;
28 created_at: number;
29 }
30
31 export function serializeUploadRow(row: UploadRecord): UploadRow {
32 return {
33 ...row,
34 expected_bytes: row.expected_bytes.toString(),
35 actual_bytes: row.actual_bytes?.toString() ?? null,
36 created_at: new Date(row.created_at).toISOString(),
37 };
38 }
39
40 export function parseUpload(value: unknown): UploadInput | undefined {
41 if (typeof value !== "object" || value === null) return;
42 const { filename, contentType, size } = value as Record<string, unknown>;
43 if (
44 typeof filename !== "string" ||
45 !filename.trim() ||
46 filename.length > 200 ||
47 /[\u0000-\u001f\u007f]/.test(filename)
48 )
49 return;
50 if (typeof contentType !== "string" || !/^[\w.+-]+\/[\w.+-]+$/.test(contentType)) return;
51 if (typeof size !== "number" || !Number.isSafeInteger(size) || size < 1 || size > MAX_BYTES)
52 return;
53 return { filename, contentType, size };
54 }
55
56 export function objectKey(owner: string, id: string) {
57 return `incoming/${encodeURIComponent(owner)}/${id}`;
58 }
59
60 export function corsHeaders(
61 origin: string | null,
62 allowed: string | undefined,
63 ): Record<string, string> | undefined {
64 if (origin && allowed !== "*") {
65 if (!allowed) return;
66 try {
67 if (origin !== new URL(allowed).origin) return;
68 } catch {
69 return;
70 }
71 }
72 return {
73 ...(origin ? { "access-control-allow-origin": allowed === "*" ? "*" : origin } : {}),
74 "access-control-allow-methods": "GET, POST, OPTIONS",
75 "access-control-allow-headers": "authorization, content-type",
76 "cache-control": "no-store",
77 vary: "Origin",
78 };
79 }