Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix background worker crash by decoupling rules catalog from storage#130

Merged
twschiller merged 1 commit into
mainfrom
fix/decouple-rules-from-background
Jun 4, 2026
Merged

Fix background worker crash by decoupling rules catalog from storage#130
twschiller merged 1 commit into
mainfrom
fix/decouple-rules-from-background

Conversation

@twschiller

Copy link
Copy Markdown
Contributor

Summary

  • The background service worker crashed at load with ReferenceError: HTMLInputElement is not defined because lib/storage.ts imported RuleId/RULE_IDS from rules/index.ts, which transitively pulls every rule file — including checkout-checkbox-sanitize.ts, which captures the native HTMLInputElement.prototype setter at module top level.
  • Moved the canonical RuleId + RULE_IDS to rules/rule-defaults.generated.ts (derived from its own keys) and switched storage.ts to use the runtime values from there with a import type for RuleId. The SW bundle no longer has any rule file in its import graph.
  • Made checkout-checkbox-sanitize.ts lazy-init the prototype lookup so it's also safe to import in DOM-less contexts.
  • Dropped the JSDOM shim in scripts/build-rule-defaults.ts that previously bootstrapped DOM globals so codegen could import("../src/rules"); codegen is now pure data.
  • Added scripts/check-background-purity.ts, wired into build.ts: scans every rule file for its top-level label: "...", asserts none appear in dist/background.js, fails the build on a leak. Labels survive Bun's minification and are unique to rule files, so they make a sound canary.
  • Removed the now-unused jsdom and @types/jsdom top-level deps.

Test plan

  • bun run preflight — typecheck + lint + knip + 999 tests pass
  • bun run build.ts — purity check reports 35 canaries, no leaks
  • Synthetic leak test (injected a rule label literal into background.ts) — purity check fails with non-zero exit and a labeled rule-file pointer
  • Load the unpacked extension and confirm the background service worker starts cleanly (no ReferenceError)

🤖 Generated with Claude Code

`lib/storage.ts` imported `RuleId` and `RULE_IDS` from `rules/index.ts`,
which pulls in every rule implementation file. A few of those touch DOM
constructors (`HTMLInputElement.prototype` in
`checkout-checkbox-sanitize.ts`) at module top level — crashing the MV3
service worker with `ReferenceError: HTMLInputElement is not defined`.

- `rules/rule-defaults.generated.ts` now owns `RuleId` and `RULE_IDS`
  (derived from its own keys). Storage imports the runtime values from
  there and the type with `import type` from `../rules`, so the SW bundle
  has no dependency on the rule catalog.
- `checkout-checkbox-sanitize.ts` lazy-memoizes the prototype lookup so
  the module is safe to import in DOM-less contexts.
- `scripts/build-rule-defaults.ts` drops the JSDOM shim it used to need
  for `import("../src/rules")`; codegen is now pure data and async-free.
- `scripts/check-background-purity.ts` (wired into `build.ts`) extracts
  every rule's top-level label and asserts none appear in
  `dist/background.js`, failing the build if anything pulls a rule into
  the worker again.
- Drops the now-unused `jsdom` + `@types/jsdom` top-level deps.

Verified via a synthetic leak test that the purity check fails with a
non-zero exit when a rule string is reintroduced into the background
bundle.
@vercel

vercel Bot commented Jun 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser-shield-demo-site Ready Ready Preview, Comment Jun 4, 2026 6:16pm

Request Review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@twschiller twschiller added the bug Something isn't working label Jun 4, 2026
@twschiller twschiller merged commit a976434 into main Jun 4, 2026
7 of 8 checks passed
@twschiller twschiller deleted the fix/decouple-rules-from-background branch June 4, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants