@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 155 lines 4.56 KB
Raw
1 import type { ConfigError } from "@distilled.cloud/core/errors";
2 import * as Effect from "effect/Effect";
3 import * as Redacted from "effect/Redacted";
4 import { PrismaApiError } from "../Client.ts";
5 import type {
6 BuildLogsQuery,
7 BuildLogsRequest,
8 DeploymentLogsQuery,
9 DeploymentLogsRequest,
10 } from "../Types.ts";
11 import { Credentials } from "./Credentials.ts";
12
13 /**
14 * Hand-written request builders for the Management API's log streams. The
15 * deployment stream is a WebSocket and the build stream is NDJSON-over-HTTP;
16 * neither fits distilled's request/response operations, so these stay in
17 * alchemy.
18 *
19 * They read the token and base URL from the distilled {@link Credentials}
20 * service — the same service every migrated operation authenticates with —
21 * rather than from `PrismaEnvironment`. `Credentials` holds an *effect*, so a
22 * stack layer can carry it without resolving credentials at build time;
23 * `PrismaEnvironment` is a resolved value and therefore cannot appear in
24 * `providers()`' output (see the lazy-auth note on `stackManagementApiLayer`).
25 */
26
27 const INVALID_PATH_SEGMENT = "__alchemy_invalid_prisma_path_segment__";
28
29 const pathSegment = (value: string): string => {
30 if (
31 value.length === 0 ||
32 value === "." ||
33 value === ".." ||
34 value === INVALID_PATH_SEGMENT ||
35 /[\\/?#%]/.test(value) ||
36 value.includes("\0")
37 ) {
38 return INVALID_PATH_SEGMENT;
39 }
40 try {
41 return encodeURIComponent(value);
42 } catch {
43 return INVALID_PATH_SEGMENT;
44 }
45 };
46
47 const isValidApiPath = (baseUrl: string, path: string): boolean => {
48 if (
49 (path !== "/v1" && !path.startsWith("/v1/")) ||
50 path.startsWith("//") ||
51 path.includes("\\") ||
52 path.includes("?") ||
53 path.includes("#") ||
54 path.includes(INVALID_PATH_SEGMENT)
55 ) {
56 return false;
57 }
58 try {
59 const base = new URL(baseUrl);
60 const resolved = new URL(path, base);
61 return (
62 resolved.origin === base.origin &&
63 (resolved.pathname === "/v1" || resolved.pathname.startsWith("/v1/"))
64 );
65 } catch {
66 return false;
67 }
68 };
69
70 const buildUrl = (
71 baseUrl: string,
72 path: string,
73 query?: object,
74 ): Effect.Effect<string, PrismaApiError> => {
75 if (!isValidApiPath(baseUrl, path)) {
76 return Effect.fail(
77 new PrismaApiError({
78 method: "GET",
79 path,
80 status: 0,
81 message: "Refused an invalid Prisma Management API path parameter",
82 }),
83 );
84 }
85 return Effect.sync(() => {
86 const url = new URL(path, baseUrl);
87 for (const [key, value] of Object.entries(query ?? {})) {
88 if (value === undefined || value === null) continue;
89 if (Array.isArray(value)) {
90 for (const item of value) {
91 url.searchParams.append(key, String(item));
92 }
93 } else {
94 url.searchParams.set(key, String(value));
95 }
96 }
97 return url.toString();
98 });
99 };
100
101 const buildWebSocketUrl = (baseUrl: string, path: string, query?: object) =>
102 buildUrl(baseUrl, path, query).pipe(
103 Effect.map((value) => {
104 const url = new URL(value);
105 if (url.protocol === "https:") url.protocol = "wss:";
106 if (url.protocol === "http:") url.protocol = "ws:";
107 return url.toString();
108 }),
109 );
110
111 const logsQuery = (query: DeploymentLogsQuery | undefined): Record<string, unknown> | undefined => {
112 if (!query) return undefined;
113 const { fromStart, ...rest } = query;
114 return {
115 ...rest,
116 from_start: fromStart === undefined ? undefined : String(fromStart),
117 };
118 };
119
120 export const getDeploymentLogsRequest = (
121 deploymentId: string,
122 query?: DeploymentLogsQuery,
123 ): Effect.Effect<DeploymentLogsRequest, PrismaApiError | ConfigError, Credentials> =>
124 Effect.gen(function* () {
125 const credentials = yield* Credentials;
126 const { apiToken, apiBaseUrl } = yield* credentials;
127 const url = yield* buildWebSocketUrl(
128 apiBaseUrl,
129 `/v1/deployments/${pathSegment(deploymentId)}/logs`,
130 logsQuery(query),
131 );
132 return {
133 url,
134 headers: {
135 Authorization: Redacted.make(`Bearer ${Redacted.value(apiToken)}`),
136 },
137 };
138 });
139
140 export const getBuildLogsRequest = (
141 buildId: string,
142 query?: BuildLogsQuery,
143 ): Effect.Effect<BuildLogsRequest, PrismaApiError | ConfigError, Credentials> =>
144 Effect.gen(function* () {
145 const credentials = yield* Credentials;
146 const { apiToken, apiBaseUrl } = yield* credentials;
147 const url = yield* buildUrl(apiBaseUrl, `/v1/builds/${pathSegment(buildId)}/logs`, query);
148 return {
149 url,
150 headers: {
151 Authorization: Redacted.make(`Bearer ${Redacted.value(apiToken)}`),
152 Accept: "application/x-ndjson",
153 },
154 };
155 });