Fix background worker crash by decoupling rules catalog from storage#130
Merged
Conversation
`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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ReferenceError: HTMLInputElement is not definedbecauselib/storage.tsimportedRuleId/RULE_IDSfromrules/index.ts, which transitively pulls every rule file — includingcheckout-checkbox-sanitize.ts, which captures the nativeHTMLInputElement.prototypesetter at module top level.RuleId+RULE_IDStorules/rule-defaults.generated.ts(derived from its own keys) and switchedstorage.tsto use the runtime values from there with aimport typeforRuleId. The SW bundle no longer has any rule file in its import graph.checkout-checkbox-sanitize.tslazy-init the prototype lookup so it's also safe to import in DOM-less contexts.JSDOMshim inscripts/build-rule-defaults.tsthat previously bootstrapped DOM globals so codegen couldimport("../src/rules"); codegen is now pure data.scripts/check-background-purity.ts, wired intobuild.ts: scans every rule file for its top-levellabel: "...", asserts none appear indist/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.jsdomand@types/jsdomtop-level deps.Test plan
bun run preflight— typecheck + lint + knip + 999 tests passbun run build.ts— purity check reports35 canaries, no leaksbackground.ts) — purity check fails with non-zero exit and a labeled rule-file pointerReferenceError)🤖 Generated with Claude Code