| 7 | ```sh |
| 8 | npm run typecheck |
| 9 | npm run build |
| 10 | npm test |
| 11 | npm run cli -- [args] |
| 12 | ``` |
| 13 |
| 14 | Single-file tests: `node --import ./src/register-hooks.ts --test src/hooks.test.ts`. |
| 15 | Use local HTTP servers and isolated cache/tmp fixtures; never clear the user's shared cache. Report public-CDN failures separately from regressions. |
| 16 |
| 17 | ## Conventions |
| 18 |
| 19 | - Zero runtime dependencies; use Node's standard library. |
| 20 | - Follow `tsconfig.json`: erasable TypeScript, `.ts` relative imports, `import type` for type-only imports. |
| 21 | - Prefer overloads/generics over casts. Casts are acceptable at untyped parseArgs/worker-message boundaries. |
| 22 | - Keep `createResolve`/`createLoad` implementation return types unannotated; overloads provide concrete caller types. |
| 23 | - Preserve useful protocol/format comments. Keep docs concise: user-facing usage in README, internal invariants here; avoid repeating implied behavior. |
| 24 | - Commit only when requested, using single-line Conventional Commits. |
| 25 |
| 26 | ## Architecture |
| 27 |
| 28 | Resolve chooses the final URL before Node assigns module identity; load reuses the verified source through `pendingLoads`. |
| 29 |
| 30 | | Files | Responsibility | |
| 31 | | --- | --- | |
| 32 | | `hooks.ts` | Shared sync/async resolution, format and attribute validation | |
| 33 | | `register-hooks.ts`, `loader.ts` | Sync registration or async loader initialization | |
| 34 | | `fetch.ts` | Locked redirects, HTTP fetch, verification, cache publication | |
| 35 | | `sync-fetch.ts`, `sync-fetch-worker.ts` | Blocking facade over a persistent network worker | |
| 36 | | `cache.ts` | Deno cache paths, text decoding, atomic writes | |
| 37 | | `lock.ts` | v5 verification and canonical-target transactions | |
| 38 | | `tmp.ts` | Readable module files and exact reverse URL mapping | |
| 39 | | `cli.ts` | Leading own options, unchanged Node arguments, child lifecycle | |
| 40 | | `jsr.ts` | esm.sh-only `jsr:` mapping | |
| 41 |
| 42 | ## Invariants |
| 43 |
| 44 | ### Hooks and CLI |
| 45 |
| 46 | - Sync hooks block only through `fetchModuleSync`. Worker startup resolves `.ts`/`.js` from `import.meta.url` with `execArgv: []`. Error callbacks cannot run during `Atomics.wait`; retain the 60-second bound and discard timed-out workers. Post results before notifying. |
| 47 | - Resolve redirects before load, reusing `pendingLoads` even with reload. Preserve the CJS parent reconstruction regex for `file:///cwd/https:/host/...`. |
| 48 | - `npm:` strips versions only for local `require.resolve`, anchored to a local caller or the working directory. Only `MODULE_NOT_FOUND` falls back to the CDN. Keep built-ins and `#imports` native. `--cdn` bare specifiers resolve natively first with the same anchoring; only module-not-found (`MODULE_NOT_FOUND`/`ERR_MODULE_NOT_FOUND`) falls back to the CDN. |
| 49 | - Validate import attributes before publishing tmp files and when loading source. ESM JSON requires type `json`; native require(JSON) omits `context.importAttributes`. User-configurable `conditions` cannot identify require. |
| 50 | - Tmp reverse mapping stores exact URLs, including query/fragment; synthetic filenames cannot recover relative-import bases. |
| 51 | - Consume/delete `URL_IMPORT_MAIN` and `URL_IMPORT_MAIN_CANDIDATES` before creating Workers. Carry `mainUrl` as local hook/loader data; redirect only the exact parentless placeholder once. |
| 52 | - Node captures async hook exports before `initialize()`. Keep exported `resolve` stable and configure its private delegate. |
| 53 | - Own CLI booleans are flag-only; use `parseArgs` without `allowNegative`. Forward the remaining Node arguments unchanged. Use async `spawn`; forward termination signals, await handled exits, and re-raise POSIX signal exits after removing handlers. |
| 54 | - Read configuration from env before registration. Only explicit CLI `--frozen` supplies a default lock path; inherited frozen alone does not enable a lock. |
| 55 |
| 56 | ### Deno compatibility |
| 57 |
| 58 | Target **Deno 2.9.8 only**; no older cache readers or migration. |
| 59 |
| 60 | - Canonicalize URLs before cache/network access. Join `Location` directly: absent fragments are dropped; explicit fragments and trailing `#` survive. Apply locked redirects even with reload; limit the combined chain to 10 hops. |
| 61 | - Cache authority: escape `_` as `%5f`, use `_port_<port>`, retain bracketed IPv6. Above 250 characters, use `#sha256(unescaped host[:port])`. Hash pathname + query, including a bare `?`; omit fragments from disk keys. |
| 62 | - Keep `<response bytes>\n// denoCacheMetadata=<JSON>` and full canonical metadata URLs. Cache original bytes; cached 404/incomplete entries are misses. |
| 63 | - Decode text with the declared WHATWG charset (default UTF-8, no BOM sniffing), strip one decoded leading BOM, then hash. Wasm bytes stay untouched. |
| 64 | - Filesystem operations remain synchronous. Atomic writes use sibling temp files; restore existing modes with `fchmodSync` before fsync/rename. New files respect umask. |
| 65 | - Lock, reload, merge and replace the same canonical target, following symlinks. Sort remote/redirect keys; preserve unrelated sections. Verify source and every hop before publication; never write a thread-local snapshot. |
| 66 | - Lock keys/targets retain queries and fragments. Hash mismatches always fail; frozen rejects missing/changed records only with an enabled lock path. Reload neither repins redirects nor overwrites hashes. |
| 67 |
| 68 | ## Compatibility checks |
| 69 |
| 70 | - Registration changes: smoke-test compiled `dist/` with isolated `DENO_DIR` on Node 18/20/22 (ESM) and 24 (CJS). |
| 71 | - Retain regressions for CLI → local Worker, captured async initialization, JSON attributes/custom conditions/require, URL aliases, charset bytes, file modes and symlink-alias concurrent locks. |
| 72 | - Deno interop: use 2.9.8, compare v5 locks both ways, require zero-network `--cached-only` loads for BOM-free UTF-8. Deno can reject its own frozen locks when decoding/BOM removal changes bytes; preserve integrity errors rather than rewriting hashes. |
| 73 |