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 { pathToFileURL } from "node:url"; |
| 2 | import type { LoaderHooks } from "./hooks.ts"; |
| 3 | import { isFileLikeSpecifier } from "./resolve.ts"; |
| 4 | |
| 5 | export interface NamespaceOptions { |
| 6 | /** |
| 7 | * Isolates one import graph in Node's module cache: every file URL the |
| 8 | * graph resolves carries this namespace as a query parameter, so the same |
| 9 | * files import again as fresh modules under a new namespace. This is how |
| 10 | * `alchemy dev` reloads the user's stack. |
| 11 | */ |
| 12 | readonly namespace: string; |
| 13 | /** Controls which file URLs belong to the graph. */ |
| 14 | readonly shouldInvalidate?: ((url: string, parentURL: string | undefined) => boolean) | undefined; |
| 15 | /** Called once the runtime loads a file in the graph. */ |
| 16 | readonly onImport?: ((url: string) => void) | undefined; |
| 17 | } |
| 18 | |
| 19 | const parameter = "alchemy-import-namespace"; |
| 20 | |
| 21 | const namespaceOf = (url: string | undefined) => { |
| 22 | if (url === undefined || !url.startsWith("file:")) return undefined; |
| 23 | return new URL(url).searchParams.get(parameter) ?? undefined; |
| 24 | }; |
| 25 | |
| 26 | const withoutNamespace = (url: string) => { |
| 27 | if (!url.startsWith("file:")) return url; |
| 28 | const parsed = new URL(url); |
| 29 | parsed.searchParams.delete(parameter); |
| 30 | return parsed.href; |
| 31 | }; |
| 32 | |
| 33 | const withNamespace = (url: string, namespace: string) => { |
| 34 | const parsed = new URL(url); |
| 35 | parsed.searchParams.set(parameter, namespace); |
| 36 | return parsed.href; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * `hooks` scoped to one namespaced graph. A graph's entry carries the |
| 41 | * namespace itself (see `importNamespaced`); everything it imports inherits |
| 42 | * it from the importing module's URL. Specifiers from any other graph pass |
| 43 | * straight through, and the underlying hooks never see a namespace: it is |
| 44 | * stripped before they run and added back to what they resolve. |
| 45 | * |
| 46 | * CommonJS members are the exception to inheritance by URL: Node's CommonJS |
| 47 | * loader addresses modules by path, so a member's `require()` calls arrive |
| 48 | * with a plain file URL as parent and what they load has none either. The |
| 49 | * graph therefore also remembers its members by clean URL, so those files |
| 50 | * are resolved by the same hooks and reported through `onImport` too. They |
| 51 | * do stay in Node's path-keyed CommonJS cache across generations. |
| 52 | */ |
| 53 | export const namespaced = (hooks: LoaderHooks, options: NamespaceOptions): LoaderHooks => { |
| 54 | const { namespace, shouldInvalidate = () => true, onImport } = options; |
| 55 | const members = new Set<string>(); |
| 56 | return { |
| 57 | resolve(specifier, context, nextResolve) { |
| 58 | const parentURL = |
| 59 | context.parentURL === undefined ? undefined : withoutNamespace(context.parentURL); |
| 60 | const inGraph = |
| 61 | namespaceOf(specifier) === namespace || |
| 62 | namespaceOf(context.parentURL) === namespace || |
| 63 | (parentURL !== undefined && members.has(parentURL)); |
| 64 | if (!inGraph) return nextResolve(specifier, context); |
| 65 | |
| 66 | const resolved = hooks.resolve(withoutNamespace(specifier), context, nextResolve); |
| 67 | if (!resolved.url.startsWith("file:")) return resolved; |
| 68 | const clean = withoutNamespace(resolved.url); |
| 69 | if (!shouldInvalidate(clean, parentURL)) return resolved; |
| 70 | members.add(clean); |
| 71 | return { ...resolved, url: withNamespace(resolved.url, namespace) }; |
| 72 | }, |
| 73 | load(url, context, nextLoad) { |
| 74 | const clean = withoutNamespace(url); |
| 75 | if (namespaceOf(url) !== namespace && !members.has(clean)) return nextLoad(url, context); |
| 76 | onImport?.(clean); |
| 77 | return hooks.load(clean, context, nextLoad); |
| 78 | }, |
| 79 | }; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Imports a file as the entry of `namespace`'s graph. `specifier` is a file |
| 84 | * URL, an absolute path, or a path relative to `parentURL`. |
| 85 | */ |
| 86 | export const importNamespaced = <T>( |
| 87 | specifier: string, |
| 88 | parentURL: string, |
| 89 | namespace: string, |
| 90 | ): Promise<T> => { |
| 91 | if (!isFileLikeSpecifier(specifier)) { |
| 92 | throw new Error(`Cannot import '${specifier}': expected a file URL or path.`); |
| 93 | } |
| 94 | const base = parentURL.startsWith("file:") ? parentURL : pathToFileURL(parentURL).href; |
| 95 | const url = specifier.startsWith("file:") |
| 96 | ? specifier |
| 97 | : new URL(specifier.startsWith(".") ? specifier : pathToFileURL(specifier).href, base).href; |
| 98 | return import(withNamespace(url, namespace)) as Promise<T>; |
| 99 | }; |