@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 321 lines 11.8 KB
Raw
1 import * as NodeFs from "node:fs/promises";
2 import * as NodeHttp from "node:http";
3 import * as Credentials from "@distilled.cloud/cloudflare/Credentials";
4 import type * as Context from "effect/Context";
5 import * as Effect from "effect/Effect";
6 import * as Exit from "effect/Exit";
7 import * as FetchHttpClient from "effect/http/FetchHttpClient";
8 import * as Headers from "effect/http/Headers";
9 import * as HttpServerRequest from "effect/http/HttpServerRequest";
10 import * as HttpServerResponse from "effect/http/HttpServerResponse";
11 import * as Layer from "effect/Layer";
12 import * as Option from "effect/Option";
13 import * as Scope from "effect/Scope";
14 import type * as vite from "vite";
15 import * as DurableObjectNamespace from "../core/bindings/DurableObjectNamespace.ts";
16 import * as Loopback from "../core/bindings/Loopback.ts";
17 import * as UnsafeEval from "../core/bindings/UnsafeEval.ts";
18 import type { BindingHooks, Module } from "../core/index.ts";
19 import { DEFAULT_COMPATIBILITY_DATE } from "../core/internal/constants.ts";
20 import { loadInternalWorker } from "../core/internal/internal-worker.ts";
21 import * as Runtime from "../core/Runtime.ts";
22 import * as RuntimeServices from "../core/RuntimeServices.ts";
23 import { PlatformServices } from "../Platform.ts";
24 import type { ExportTypes } from "../rolldown/export-types.ts";
25 import { EXPORT_TYPES_MODULE_ID } from "../rolldown/export-types.ts";
26 import { MODULE_REFERENCE_REGEX } from "../rolldown/plugins/index.ts";
27 const ModuleRunnerWorker = {
28 worker: () =>
29 loadInternalWorker("#cloudflare-runtime-vite-worker/module-runner/module-runner.worker"),
30 };
31 const WrapperWorker = {
32 worker: () => loadInternalWorker("#cloudflare-runtime-vite-worker/module-runner/wrapper.worker"),
33 };
34 import * as ViteAssets from "./assets/ViteAssets.ts";
35 import { renderExportWrappers } from "./export-types.ts";
36 import type { EntryEnvironment } from "./module-runner/constants.shared.ts";
37 import {
38 ENTRY_ENVIRONMENT_KEY,
39 ENVIRONMENT_NAME_HEADER,
40 } from "./module-runner/constants.shared.ts";
41 import type { CloudflareVitePluginOptions } from "./plugin.ts";
42
43 export type ServerHandle = Awaited<ReturnType<typeof startServer>>;
44
45 export const startServer = async <B extends BindingHooks = BindingHooks>(
46 options: CloudflareVitePluginOptions<B>,
47 entryEnvironment: Omit<EntryEnvironment, "exportTypesId">,
48 server: vite.ViteDevServer,
49 context: Context.Context<RuntimeServices.RuntimeServices>,
50 exportTypes: ExportTypes,
51 /** See `RuntimeWorker.onRestart`. */
52 onRestart?: () => void,
53 ) => {
54 const scope = Scope.makeUnsafe();
55 const proxySharedSecret = crypto.randomUUID();
56 const address = await serve(
57 options,
58 entryEnvironment,
59 server,
60 exportTypes,
61 proxySharedSecret,
62 onRestart,
63 ).pipe(
64 // `provideMerge`: the assets layer's construction reads `Loopback` (and
65 // friends) from the runtime context, so the context must feed the layer,
66 // not just sit beside it.
67 Effect.provide(
68 ViteAssets.ViteAssetsLive(server, {
69 hasUserWorker: entryEnvironment.entryName !== undefined,
70 }).pipe(Layer.provideMerge(Layer.succeedContext(context))),
71 ),
72 Scope.provide(scope),
73 Effect.runPromise,
74 );
75 return { address, proxySharedSecret, close: () => closeScope(scope) };
76 };
77
78 export const createDefaultContext = async (): Promise<
79 Context.Context<RuntimeServices.RuntimeServices>
80 > => {
81 const scope = Scope.makeUnsafe();
82
83 return await RuntimeServices.layerRuntime({
84 api: { accountId: process.env.CLOUDFLARE_ACCOUNT_ID! },
85 }).pipe(
86 Layer.provideMerge(PlatformServices),
87 Layer.provide(Layer.merge(Credentials.fromEnv(), FetchHttpClient.layer)),
88 Layer.buildWithScope(scope),
89 Effect.runPromise,
90 );
91 };
92
93 const closeScope = async (scope: Scope.Closeable) => {
94 await Effect.runPromiseExit(Scope.closeUnsafe(scope, Exit.void) ?? Effect.void);
95 };
96
97 const makeModuleFallbackService = Effect.gen(function* () {
98 const server = NodeHttp.createServer(async (req, res) => {
99 try {
100 const request = await parseModuleFallbackRequest(req);
101 if (!request) {
102 res.writeHead(400, { "Content-Type": "text/plain" });
103 res.end("Invalid module fallback request");
104 return;
105 }
106
107 const candidate = request.rawSpecifier ?? request.specifier;
108 const match = MODULE_REFERENCE_REGEX.exec(candidate);
109 if (!match) {
110 res.writeHead(400, { "Content-Type": "text/plain" });
111 res.end(`No match for module: ${candidate}`);
112 return;
113 }
114
115 const [, moduleType, modulePath] = match;
116 if (!moduleType || !modulePath) {
117 res.writeHead(400, { "Content-Type": "text/plain" });
118 res.end(`Invalid module type or path: ${match[0]}`);
119 return;
120 }
121
122 let content: Buffer;
123 try {
124 content = await NodeFs.readFile(modulePath);
125 } catch {
126 res.writeHead(404, { "Content-Type": "text/plain" });
127 res.end(`Module not found: ${modulePath}`);
128 return;
129 }
130
131 switch (moduleType) {
132 case "CompiledWasm": {
133 res.writeHead(200, { "Content-Type": "application/json" });
134 res.end(JSON.stringify({ wasm: Array.from(content) }));
135 return;
136 }
137 case "Data": {
138 res.writeHead(200, { "Content-Type": "application/json" });
139 res.end(JSON.stringify({ data: Array.from(content) }));
140 return;
141 }
142 case "Text": {
143 res.writeHead(200, { "Content-Type": "application/json" });
144 res.end(JSON.stringify({ text: content.toString() }));
145 return;
146 }
147 default: {
148 res.writeHead(400, { "Content-Type": "text/plain" });
149 res.end(`Invalid module type: ${moduleType}`);
150 }
151 }
152 } catch (error) {
153 res.writeHead(500, { "Content-Type": "text/plain" });
154 res.end(error instanceof Error ? (error.stack ?? error.message) : String(error));
155 }
156 });
157
158 // workerd fetches every fallback module over this server. Node's default
159 // 5s `keepAliveTimeout` closes idle sockets between module bursts, forcing
160 // workerd to open a fresh TCP connection per burst — on Windows CI the
161 // resulting TIME_WAIT churn exhausts ephemeral ports (WSAENOBUFS #10055).
162 // Keep idle connections around for a minute so they get reused.
163 server.keepAliveTimeout = 60_000;
164 server.headersTimeout = 65_000;
165 yield* Effect.callback<void>((resume) => {
166 server.listen(0, "127.0.0.1", () => resume(Effect.void));
167 });
168 yield* Effect.addFinalizer(() =>
169 Effect.callback<void>((resume) => {
170 server.close(() => resume(Effect.void));
171 // `close()` waits for in-flight/keep-alive connections; destroy them so
172 // shutdown doesn't hang for up to `keepAliveTimeout`.
173 server.closeAllConnections();
174 }),
175 );
176
177 const address = server.address();
178 if (address === null || typeof address === "string") {
179 return yield* Effect.die(new Error("Module fallback server address unavailable"));
180 }
181 return `127.0.0.1:${address.port}`;
182 });
183
184 const serve = Effect.fn(function* <B extends BindingHooks = BindingHooks>(
185 options: CloudflareVitePluginOptions<B>,
186 entryEnvironment: Omit<EntryEnvironment, "exportTypesId">,
187 server: vite.ViteDevServer,
188 exportTypes: ExportTypes,
189 proxySharedSecret: string,
190 onRestart?: () => void,
191 ) {
192 const runtime = yield* Runtime.Runtime;
193 const moduleFallback = yield* makeModuleFallbackService;
194
195 const name = options.worker?.name ?? `vite-dev-${crypto.randomUUID()}`;
196 return yield* runtime.start({
197 // Everything the caller configured on the Worker (crons, tail consumers,
198 // Cache API, request.cf, ...) reaches workerd; the fields below are the
199 // ones the dev server owns or extends.
200 ...options.worker,
201 name,
202 proxySharedSecret,
203 // The stable proxy in front relays TLS as-is, so a dev server on HTTPS
204 // is public on HTTPS.
205 publicUrl:
206 options.worker?.publicUrl && server.config.server.https
207 ? options.worker.publicUrl.replace(/^http:/, "https:")
208 : options.worker?.publicUrl,
209 onRestart,
210 modules: yield* Effect.promise(() =>
211 makeWorkerModules(exportTypes, {
212 ...entryEnvironment,
213 exportTypesId: EXPORT_TYPES_MODULE_ID,
214 }),
215 ),
216 compatibilityDate: options.compatibilityDate ?? DEFAULT_COMPATIBILITY_DATE,
217 compatibilityFlags: options.compatibilityFlags ?? [],
218 bindings: [
219 UnsafeEval.local("__DISTILLED_UNSAFE_EVAL__"),
220 DurableObjectNamespace.local({
221 binding: "__DISTILLED_MODULE_RUNNER__",
222 className: "ModuleRunnerDO",
223 }),
224 Loopback.local({
225 binding: "__DISTILLED_INVOKE_MODULE__",
226 name: `vite:invoke-module:${name}`,
227 handler: Effect.gen(function* () {
228 const request = yield* HttpServerRequest.HttpServerRequest;
229 const targetEnvironment = Headers.get(request.headers, ENVIRONMENT_NAME_HEADER).pipe(
230 Option.getOrThrow,
231 );
232 const json = (yield* request.json) as unknown as vite.CustomPayload;
233 const devEnvironment = server.environments[targetEnvironment];
234 const result = yield* Effect.promise(
235 async () => await devEnvironment.hot.handleInvoke(json),
236 );
237 return HttpServerResponse.jsonUnsafe(result);
238 }),
239 }),
240 ...(options.worker?.bindings ?? []),
241 ],
242 durableObjectNamespaces: [
243 { className: "ModuleRunnerDO", sql: false, ephemeralLocal: true },
244 ...(options.worker?.durableObjectNamespaces ?? []),
245 ],
246 unsafe: { moduleFallback, ...options.worker?.unsafe },
247 });
248 });
249
250 type ModuleFallbackRequest =
251 | {
252 protocol: "v1";
253 type: "import" | "require";
254 specifier: string;
255 rawSpecifier?: string;
256 referrer?: string;
257 }
258 | {
259 protocol: "v2";
260 type: "import" | "require" | "internal";
261 specifier: string;
262 rawSpecifier?: string;
263 referrer?: string;
264 attributes?: Array<{ name: string; value: string }>;
265 };
266
267 const parseModuleFallbackRequest = async (
268 req: NodeHttp.IncomingMessage,
269 ): Promise<ModuleFallbackRequest | undefined> => {
270 if (req.method === "GET" && req.headers["x-resolve-method"]) {
271 const url = new URL(req.url ?? "", "http://localhost");
272 const specifier = url.searchParams.get("specifier");
273 if (!specifier) {
274 return;
275 }
276 const resolveMethod = req.headers["x-resolve-method"];
277 return {
278 protocol: "v1",
279 type: resolveMethod === "require" ? "require" : "import",
280 specifier,
281 rawSpecifier: url.searchParams.get("rawSpecifier") ?? undefined,
282 referrer: url.searchParams.get("referrer") ?? undefined,
283 };
284 }
285
286 if (req.method === "POST") {
287 const chunks: Array<Buffer> = [];
288 for await (const chunk of req) {
289 chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
290 }
291 const json: unknown = JSON.parse(Buffer.concat(chunks).toString());
292 if (typeof json === "object" && json !== null && "specifier" in json) {
293 return {
294 protocol: "v2",
295 ...(json as Omit<Extract<ModuleFallbackRequest, { protocol: "v2" }>, "protocol">),
296 };
297 }
298 }
299 };
300
301 async function makeWorkerModules(
302 exportTypes: ExportTypes,
303 entryEnvironment: EntryEnvironment,
304 ): Promise<Array<Module>> {
305 const [moduleRunnerWorker, wrapperWorker] = await Promise.all([
306 ModuleRunnerWorker.worker(),
307 WrapperWorker.worker(),
308 ]);
309 const modules = {
310 "index.worker.mjs": [
311 `import { createWorkerEntrypointWrapper, createDurableObjectWrapper, createWorkflowEntrypointWrapper } from "./module-runner/wrapper.worker.mjs";`,
312 'export { ModuleRunnerDO } from "./module-runner/module-runner.worker.mjs";',
313 `globalThis[Symbol.for(${JSON.stringify(ENTRY_ENVIRONMENT_KEY)})] = ${JSON.stringify(entryEnvironment)};`,
314 'export default createWorkerEntrypointWrapper("default");',
315 ...renderExportWrappers(exportTypes),
316 ].join("\n"),
317 ...moduleRunnerWorker.modules,
318 ...wrapperWorker.modules,
319 };
320 return Object.entries(modules).map(([name, content]) => ({ name, type: "ESModule", content }));
321 }