@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 89 lines 3.71 KB
Raw
1 import {
2 DEFAULT_COMPATIBILITY_DATE,
3 NODEJS_COMPAT_DEFAULT_ON,
4 withDefaultFlags,
5 } from "@alchemy.run/cloudflare-runtime/core/internal/constants";
6 import { isPythonMain } from "./Sources/Python.ts";
7 import type { WorkerProps } from "./Worker.ts";
8
9 /**
10 * The Effect worker bridge builds its layer stack once per isolate and shares
11 * the in-flight build promise across concurrent events. Awaiting a promise
12 * created under another event's request context is only sound with workerd's
13 * corrected cross-request promise semantics (default-on since compatibility
14 * date 2024-10-14): continuations are scheduled back into the promise's
15 * origin context instead of running in whichever request happens to resolve
16 * them. A user pinning an older compatibility date must not silently revert
17 * the bridge to the broken semantics, so the flag is forced for
18 * alchemy-bundled workers — and explicitly disabling it is a deploy-time
19 * error.
20 */
21 const CROSS_REQUEST_PROMISE_RESOLUTION = "handle_cross_request_promise_resolution";
22
23 /**
24 * An Effect Worker that binds itself with `bindWorker` calls its own default
25 * entrypoint through workerd's `ctx.exports` loopback (default-on since
26 * compatibility date 2025-11-17), so disabling it is a deploy-time error.
27 */
28 const DISABLE_CTX_EXPORTS = "disable_ctx_exports";
29
30 /**
31 * Compatibility settings passed to build tools and framework adapters.
32 * Cloudflare rejects a redundant `nodejs_compat` flag after its default-on
33 * date, but downstream tools may still detect Node support from the explicit
34 * flag only, so internal build configuration materializes the effective flag.
35 */
36 export const getToolingCompatibility = (
37 compatibility: { date: string; flags: string[] },
38 main: unknown,
39 ) => ({
40 date: compatibility.date,
41 // TODO: Stop materializing `nodejs_compat` once supported downstream tools
42 // consistently derive the default from the compatibility date.
43 flags:
44 isPythonMain(main) ||
45 compatibility.date < NODEJS_COMPAT_DEFAULT_ON ||
46 compatibility.flags.includes("nodejs_compat") ||
47 compatibility.flags.includes("nodejs_compat_v2") ||
48 compatibility.flags.includes("no_nodejs_compat")
49 ? compatibility.flags
50 : [...compatibility.flags, "nodejs_compat"],
51 });
52
53 export const getCompatibility = (props: WorkerProps) => {
54 const userFlags = props.compatibility?.flags ?? [];
55 const python = isPythonMain(props.main);
56 if (python && !props.isExternal) {
57 throw new Error(
58 "Python Workers cannot have an inline Effect implementation: the " +
59 "Effect runtime is a JavaScript bundle and cannot be injected into " +
60 "a Pyodide Worker. Declare the Worker with only its props (the " +
61 "handlers live in the Python entry module).",
62 );
63 }
64 if (!props.isExternal && userFlags.includes(`no_${CROSS_REQUEST_PROMISE_RESOLUTION}`)) {
65 throw new Error(
66 `The "no_${CROSS_REQUEST_PROMISE_RESOLUTION}" compatibility flag is not supported: ` +
67 "the alchemy Worker runtime shares its layer build across concurrent " +
68 "requests, which requires workerd's corrected cross-request promise " +
69 "semantics. Remove the flag from `compatibility.flags`.",
70 );
71 }
72 if (!props.isExternal && userFlags.includes(DISABLE_CTX_EXPORTS)) {
73 throw new Error(
74 `The "${DISABLE_CTX_EXPORTS}" compatibility flag is not supported: ` +
75 "the alchemy Worker runtime calls a Worker's own entrypoints through " +
76 "`ctx.exports`. Remove the flag from `compatibility.flags`.",
77 );
78 }
79 const date = props.compatibility?.date ?? DEFAULT_COMPATIBILITY_DATE;
80 return {
81 date,
82 flags: withDefaultFlags(props.compatibility?.flags, {
83 date,
84 python,
85 isExternal: props.isExternal ?? false,
86 bundle: props.bundle,
87 }),
88 };
89 };