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
| 1 | import * as Artifacts from "alchemy/Artifacts"; |
| 2 | import * as Bundle from "alchemy/Bundle/Bundle"; |
| 3 | import { sha256 } from "alchemy/Util/sha256"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import path from "pathe"; |
| 7 | import picomatch from "picomatch"; |
| 8 | import { glob } from "tinyglobby"; |
| 9 | import type { SourceProvider } from "../Source.ts"; |
| 10 | import { bundleSource, resolveMainPath, watchBundleDirectory } from "./shared.ts"; |
| 11 | |
| 12 | /** |
| 13 | * A rule selecting files uploaded as their own Worker modules, mirroring |
| 14 | * Wrangler's `rules` configuration. Globs match POSIX-style paths relative |
| 15 | * to the directory containing the Worker's entry module; `**\/` matches any |
| 16 | * depth, and `!` globs exclude across all rules. |
| 17 | */ |
| 18 | export interface ModuleRule { |
| 19 | readonly globs: readonly string[]; |
| 20 | /** |
| 21 | * How matching files are imported: `Text` as a `string`, `Data` as an |
| 22 | * `ArrayBuffer`, `CompiledWasm` as a `WebAssembly.Module`. Defaults to the |
| 23 | * extension's type (`.wasm` CompiledWasm, `.txt`/`.html`/`.sql` Text), |
| 24 | * otherwise Data. JavaScript files are always ES modules. |
| 25 | */ |
| 26 | readonly type?: "Text" | "Data" | "CompiledWasm" | undefined; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * The default {@link ModuleRule | module rules} applied when |
| 31 | * `bundle: false` is set without explicit rules — the same set Wrangler |
| 32 | * applies to `no_bundle` Workers: ESModule (`**\/*.js`, `**\/*.mjs`), |
| 33 | * CompiledWasm (`**\/*.wasm`), Text (`**\/*.txt`, `**\/*.html`, |
| 34 | * `**\/*.sql`), and Data (`**\/*.bin`). Source maps (`.js.map`) are |
| 35 | * deliberately not uploaded. |
| 36 | */ |
| 37 | export const defaultModuleRules: ModuleRule[] = [ |
| 38 | { globs: ["**/*.js", "**/*.mjs"] }, |
| 39 | { globs: ["**/*.wasm"] }, |
| 40 | { globs: ["**/*.txt", "**/*.html", "**/*.sql"] }, |
| 41 | { globs: ["**/*.bin"] }, |
| 42 | ]; |
| 43 | |
| 44 | export interface PrebuiltWorkerBundleOptions { |
| 45 | /** |
| 46 | * Path (or `file://` URL) to the prebuilt, runtime-ready entry module. |
| 47 | */ |
| 48 | main: string; |
| 49 | /** |
| 50 | * Module rules selecting additional files to upload alongside the |
| 51 | * entry. Defaults to {@link defaultModuleRules}. |
| 52 | */ |
| 53 | rules?: readonly ModuleRule[] | undefined; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Read a prebuilt Worker bundle from disk without bundling. |
| 58 | * |
| 59 | * The entry's directory is walked recursively and every file matching the |
| 60 | * rule globs is uploaded byte-for-byte as an additional module, named by |
| 61 | * its POSIX path relative to that directory — the same contract as |
| 62 | * Wrangler's `find_additional_modules` and Alchemy v1's `noBundle`. The |
| 63 | * entry file is always first and never duplicated as an additional |
| 64 | * module. |
| 65 | */ |
| 66 | export const readPrebuiltWorkerBundle = Effect.fn(function* (options: PrebuiltWorkerBundleOptions) { |
| 67 | const fs = yield* FileSystem.FileSystem; |
| 68 | |
| 69 | // Resolve without following symlinks (Alchemy v1 parity): the module |
| 70 | // walk happens in the directory the user pointed at, not the entry's |
| 71 | // canonical location. |
| 72 | const main = yield* resolveMainPath(options.main); |
| 73 | const root = path.dirname(main); |
| 74 | const entryName = path.basename(main); |
| 75 | |
| 76 | const rules = options.rules ?? defaultModuleRules; |
| 77 | // The first matching rule decides the type, as when bundling. |
| 78 | const matchers = rules.map((rule) => { |
| 79 | const include = rule.globs.filter((glob) => !glob.startsWith("!")); |
| 80 | return { |
| 81 | type: rule.type, |
| 82 | test: include.length ? picomatch(include, { dot: true }) : () => false, |
| 83 | }; |
| 84 | }); |
| 85 | const typeOf = (name: string) => |
| 86 | /\.[cm]?js$/.test(name) ? undefined : matchers.find((rule) => rule.test(name))?.type; |
| 87 | |
| 88 | const readModuleFile = Effect.fn(function* (name: string) { |
| 89 | const file = path.join(root, name); |
| 90 | const content = yield* fs.readFile(file).pipe( |
| 91 | Effect.mapError((cause) => |
| 92 | Bundle.BundleError.make({ |
| 93 | message: `Failed to read prebuilt worker bundle module "${file}"`, |
| 94 | cause, |
| 95 | }), |
| 96 | ), |
| 97 | ); |
| 98 | const hash = yield* sha256(content); |
| 99 | const type = typeOf(name); |
| 100 | return ( |
| 101 | type ? { path: name, content, hash, type } : { path: name, content, hash } |
| 102 | ) satisfies Bundle.BundleFile; |
| 103 | }); |
| 104 | |
| 105 | return yield* readModuleFile(entryName).pipe( |
| 106 | Effect.zipWith( |
| 107 | Effect.tryPromise({ |
| 108 | try: () => |
| 109 | glob( |
| 110 | rules.flatMap((rule) => rule.globs), |
| 111 | { |
| 112 | cwd: root, |
| 113 | onlyFiles: true, |
| 114 | expandDirectories: false, |
| 115 | dot: true, |
| 116 | }, |
| 117 | ), |
| 118 | catch: (error) => |
| 119 | Bundle.BundleError.make({ |
| 120 | message: `Failed to read additional modules in directory "${root}"`, |
| 121 | cause: error, |
| 122 | }), |
| 123 | }).pipe( |
| 124 | Effect.map((names) => |
| 125 | names |
| 126 | .map((name) => name.replaceAll("\\", "/")) |
| 127 | .filter((name) => name !== entryName) |
| 128 | .sort(), |
| 129 | ), |
| 130 | Effect.flatMap(Effect.forEach(readModuleFile, { concurrency: "unbounded" })), |
| 131 | ), |
| 132 | (entryModule, additionalModules) => |
| 133 | [entryModule, ...additionalModules] as [Bundle.BundleFile, ...Bundle.BundleFile[]], |
| 134 | { concurrent: true }, |
| 135 | ), |
| 136 | Effect.flatMap(Bundle.bundleOutputFromFiles), |
| 137 | ); |
| 138 | }); |
| 139 | |
| 140 | /** |
| 141 | * Watch a prebuilt Worker (`bundle: false`) for changes: emits the |
| 142 | * initial byte-for-byte read, then re-reads whenever a file under the |
| 143 | * entry's directory changes (debounced). Mirrors the |
| 144 | * {@link Bundle.BundleWatchEvent} protocol of the rolldown watcher so |
| 145 | * local dev can consume either stream interchangeably — crucially |
| 146 | * WITHOUT re-bundling the prebuilt artifact, preserving the same |
| 147 | * byte-for-byte contract as the deploy path. |
| 148 | */ |
| 149 | export const watchPrebuiltWorkerBundle = (options: PrebuiltWorkerBundleOptions) => |
| 150 | watchBundleDirectory({ |
| 151 | main: options.main, |
| 152 | read: readPrebuiltWorkerBundle(options), |
| 153 | }); |
| 154 | |
| 155 | /** |
| 156 | * Source provider for prebuilt workers (`bundle: false`): the entry and |
| 157 | * every file matching the module rules are uploaded byte-for-byte — no |
| 158 | * bundling, no minification. `hash()` is a cheap re-read (IO + sha256, |
| 159 | * no bundling), cached per run under the shared `"build"` artifact key. |
| 160 | * |
| 161 | * Local dev honors the same byte-for-byte contract: the entry directory |
| 162 | * is fs-watched and re-read on change — never re-bundled with rolldown, |
| 163 | * which would violate the prebuilt contract ("re-bundling such artifacts |
| 164 | * is unsafe"). |
| 165 | */ |
| 166 | export const makePrebuiltSource = (options: { |
| 167 | main: string; |
| 168 | rules: ModuleRule[] | undefined; |
| 169 | }): SourceProvider => { |
| 170 | const build = readPrebuiltWorkerBundle({ |
| 171 | main: options.main, |
| 172 | rules: options.rules, |
| 173 | }).pipe(Artifacts.cached("build")); |
| 174 | return bundleSource({ |
| 175 | build: () => build, |
| 176 | watch: () => |
| 177 | Effect.succeed( |
| 178 | watchPrebuiltWorkerBundle({ |
| 179 | main: options.main, |
| 180 | rules: options.rules, |
| 181 | }), |
| 182 | ), |
| 183 | }); |
| 184 | }; |