|
| 1 | +import { existsSync, readFileSync } from 'node:fs'; |
| 2 | +import { dirname, resolve } from 'node:path'; |
| 3 | +import * as jsoncParser from 'jsonc-parser'; |
| 4 | +import TOML from 'smol-toml'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The slice of the wrangler configuration the auto-instrument plugin cares |
| 8 | + * about, normalized into a single shape regardless of the source format. |
| 9 | + */ |
| 10 | +export interface WranglerConfig { |
| 11 | + main?: string; |
| 12 | + durableObjects: Array<{ name: string; className: string }>; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * The raw wrangler config as parsed from disk. Both TOML and JSONC decode to |
| 17 | + * this shape (snake_case keys, `durable_objects.bindings`), matching wrangler's |
| 18 | + * own schema; {@link normalizeWranglerConfig} maps it to {@link WranglerConfig}. |
| 19 | + * Named environments (`[env.<name>]` / `"env"`) repeat the same shape. |
| 20 | + */ |
| 21 | +interface RawWranglerEnvironment { |
| 22 | + main?: string; |
| 23 | + durable_objects?: { bindings?: Array<{ name: string; class_name: string; script_name?: string }> }; |
| 24 | +} |
| 25 | + |
| 26 | +interface RawWranglerConfig extends RawWranglerEnvironment { |
| 27 | + env?: Record<string, RawWranglerEnvironment>; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Locate and parse a wrangler configuration file. |
| 32 | + * |
| 33 | + * When `explicitPath` is provided it is resolved against `root` and used |
| 34 | + * directly. Otherwise the function probes for `wrangler.json`, |
| 35 | + * `wrangler.jsonc` and `wrangler.toml` inside `root` — the same precedence |
| 36 | + * wrangler itself applies, so we read the file wrangler would actually use |
| 37 | + * when more than one exists. |
| 38 | + * |
| 39 | + * Returns `undefined` when no config file exists or the file cannot be parsed |
| 40 | + * (the caller warns and disables auto-instrumentation rather than failing the |
| 41 | + * whole build on a malformed config). |
| 42 | + */ |
| 43 | +export function resolveWranglerConfig( |
| 44 | + root: string, |
| 45 | + explicitPath?: string, |
| 46 | +): { config: WranglerConfig; configDir: string } | undefined { |
| 47 | + if (explicitPath) { |
| 48 | + const filePath = resolve(root, explicitPath); |
| 49 | + if (!existsSync(filePath)) return undefined; |
| 50 | + const config = parseWranglerFile(filePath); |
| 51 | + return config && { config, configDir: dirname(filePath) }; |
| 52 | + } |
| 53 | + |
| 54 | + for (const filename of ['wrangler.json', 'wrangler.jsonc', 'wrangler.toml']) { |
| 55 | + const filePath = resolve(root, filename); |
| 56 | + if (existsSync(filePath)) { |
| 57 | + const config = parseWranglerFile(filePath); |
| 58 | + return config && { config, configDir: root }; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return undefined; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Parse a wrangler config file into the normalized {@link WranglerConfig}. |
| 67 | + * |
| 68 | + * TOML is parsed with `smol-toml` and JSON/JSONC with `jsonc-parser` — the same |
| 69 | + * libraries wrangler itself uses — so comments, trailing commas and the full |
| 70 | + * TOML grammar are handled correctly rather than approximated. Returns |
| 71 | + * `undefined` for empty or unparseable files. |
| 72 | + */ |
| 73 | +function parseWranglerFile(filePath: string): WranglerConfig | undefined { |
| 74 | + let raw: unknown; |
| 75 | + try { |
| 76 | + const content = readFileSync(filePath, 'utf-8'); |
| 77 | + raw = filePath.endsWith('.toml') ? TOML.parse(content) : jsoncParser.parse(content); |
| 78 | + } catch { |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + if (typeof raw !== 'object' || raw === null) return undefined; |
| 82 | + return normalizeWranglerConfig(raw as RawWranglerConfig); |
| 83 | +} |
| 84 | + |
| 85 | +function normalizeWranglerConfig(raw: RawWranglerConfig): WranglerConfig { |
| 86 | + // `main` follows the active wrangler environment (selected via CLOUDFLARE_ENV, |
| 87 | + // as `@cloudflare/vite-plugin` does). Durable Object class names are unioned |
| 88 | + // across ALL environments instead: wrapping a class that is only bound in |
| 89 | + // another environment is harmless, while missing one loses instrumentation. |
| 90 | + const activeEnvName = process.env.CLOUDFLARE_ENV; |
| 91 | + const activeEnv = activeEnvName ? raw.env?.[activeEnvName] : undefined; |
| 92 | + |
| 93 | + const durableObjects: WranglerConfig['durableObjects'] = []; |
| 94 | + const seenClassNames = new Set<string>(); |
| 95 | + for (const environment of [raw, ...Object.values(raw.env ?? {})]) { |
| 96 | + for (const binding of environment.durable_objects?.bindings ?? []) { |
| 97 | + // `script_name` bindings reference a class exported by a *different* |
| 98 | + // worker — there is nothing to wrap in this worker's entry file. |
| 99 | + if (typeof binding?.class_name !== 'string' || binding.script_name || seenClassNames.has(binding.class_name)) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + seenClassNames.add(binding.class_name); |
| 103 | + durableObjects.push({ name: binding.name, className: binding.class_name }); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return { main: activeEnv?.main ?? raw.main, durableObjects }; |
| 108 | +} |
0 commit comments