fix(entity-decode): single-pass decoder shared across seeders (#5436)
Closes #5436
Problem
Every hand-rolled entity decoder in the repo chained sequential .replace() calls that decoded & in the same pass as the other named entities. That decodes two levels in one pass: &lt; becomes &, and the next replace turns the resulting < into <. So escaped-once text like &lt;script&gt; surfaced as live <script> (notably in seed-security-advisories, which strips tags before decoding). Separately, several numeric decoders called String.fromCodePoint unguarded — a malformed � throws RangeError and, depending on catch scope, can fail the whole seeder parse (seed-energy-intelligence, seed-regulatory-actions).
Fix
New scripts/shared/entity-decode.mjs — a single-pass alternation decoder (decodes exactly one level for every input) with a range-guarded decodeNumericReference. It is config-driven so each call site keeps its exact behaviour:
named— the site's own entity→char map (so e.g. hormuz keeps curly“, energy keeps straight")numericOverrides/numericDefault: decode | literal | space— reproduces per-seeder numeric handling (e.g. security-advisories normalising’→', sovereign-wealth's catch-all→space)caseInsensitive,unknownToSpace— for the/giseeders and the sovereign-wealth catch-all
All 13 decoders from the issue now delegate to it (output unchanged, bugs fixed):
- 11 seeders migrated: energy-intelligence, regulatory-actions, security-advisories, global-tenders, hormuz, climate-news, disease-outbreaks,
_trade-parse-utils, china-macro/calendar, fatf-listing, sovereign-wealth - 2 client components (
CountryDeepDivePanel.ts,CountryDeepDivePanel-news-utils.ts) apply the same single-pass logic inline, to avoid introducing ascripts/ → src/build dependency Dockerfile.relaycopies the new shared module (the relay image buildsseed-climate-news, the one containerised affected seeder — the COPY gotcha noted in the issue)
Testing
tests/entity-decode-shared.test.mjs (node:test) covers: single-level round-trip, the double-decode case from this issue (&lt;script&gt; stays literal), the RangeError guard, numeric overrides, curly-quote preservation, case-insensitive matching, and the sovereign-wealth catch-all. All pass locally.
Note: I verified the shared decoder unit tests, syntax/import loading on every touched file, and faithful per-seeder output on spot-checks, and matched the 2-space style. I could not run the full lint/typecheck/full-variant e2e locally (dependency install), so please let CI confirm those — happy to adjust for any nits.
@rajanpanth is attempting to deploy a commit to the World Monitor Team on Vercel.
A member of the Team first needs to authorize it.
Greptile Summary
This PR introduces scripts/shared/entity-decode.mjs, a config-driven single-pass HTML/XML entity decoder, and migrates all 11 seeder scripts that had hand-rolled multi-pass decoders. It fixes two latent bugs: double-decoding of &lt;-style inputs (which surfaced as live <script> in seed-security-advisories) and unguarded String.fromCodePoint calls that threw RangeError on malformed numeric refs in seed-energy-intelligence and seed-regulatory-actions.
scripts/shared/entity-decode.mjs— new single-pass alternation regex with a range-guardeddecodeNumericReference; config-driven so every seeder's existing entity map is preserved exactly.- 11 seeders +
_trade-parse-utils— imports the shared decoder with per-seeder configs that reproduce original output while closing the double-decode and RangeError paths. - 2 TS client components — inline equivalent decoder avoids a
scripts/→src/import boundary violation;Dockerfile.relayupdated to copy the new shared module.
Confidence Score: 4/5
Safe to merge — the single-pass implementation is correct, all 11 seeder configs are faithful to their originals, and the RangeError guard works as tested.
The core decoder logic is sound and the test suite exercises the double-decode fix, numeric range guard, per-seeder config variants, and the sovereign-wealth catch-all. The two open items are: the regex is rebuilt on every call (a performance concern for hot seeder loops), and the identical inline decoder is copy-pasted into both CountryDeepDivePanel.ts and CountryDeepDivePanel-news-utils.ts, creating future drift risk. Neither blocks correctness today.
The two src/components/CountryDeepDivePanel*.ts files carry identical inline decoders that could diverge silently; worth considering a shared src/utils/ home for them.
Important Files Changed
| Filename | Overview |
|---|---|
| scripts/shared/entity-decode.mjs | New shared single-pass decoder; correct alternation, guarded numeric refs, and config-driven behaviour — no logic bugs found. Regex is rebuilt on every call (no memoization). |
| src/components/CountryDeepDivePanel-news-utils.ts | Inline single-pass decoder correctly reproduces the original entity set and handles '/' and /// via numeric check; identical logic duplicated in CountryDeepDivePanel.ts. |
| src/components/CountryDeepDivePanel.ts | Same inline single-pass decoder as CountryDeepDivePanel-news-utils.ts; logic is correct but the two implementations are exact copies that could drift independently. |
| scripts/seed-security-advisories.mjs | Tag strip before decode order preserved; single-pass fix correctly prevents <script> from surfacing as live markup; config faithful to original named/numeric set. |
| scripts/seed-energy-intelligence.mjs | Replaced unguarded String.fromCodePoint calls with numericDefault:'decode' routed through decodeNumericReference; named entity map faithful to original. |
| scripts/seed-regulatory-actions.mjs | Replaced unguarded String.fromCodePoint with guarded shared decoder; caseInsensitive:true preserves original /gi behaviour. |
| scripts/seed-sovereign-wealth.mjs | Catch-all &[#\w]+; → space faithfully reproduced via unknownToSpace:true + numericDefault:'space'; double-decode intentionally fixed. |
| scripts/_trade-parse-utils.mjs | Faithful migration; import placed before the file's own module-level JSDoc block comment, inconsistent with every other touched file. |
| tests/entity-decode-shared.test.mjs | Covers single-level round-trip, double-decode guard, RangeError guard, numericOverrides, curly-quote preservation, case-insensitive matching, and sovereign-wealth catch-all. |
| Dockerfile.relay | Adds the required COPY for scripts/shared/entity-decode.mjs in the correct position before seed-climate-news.mjs. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["HTML/XML input text"] --> B{{"decodeHtmlEntities(text, config)"}}
B --> C["Build regex from config\n(named + hex + dec + unknown parts)"]
C --> D["Single left-to-right pass\nString.replace(re, callback)"]
D --> E{Match type?}
E -->|"name group\n(e.g. & <)"| F["Lookup in named map\n→ replacement char"]
E -->|"hex or dec group\n(e.g. ' ')"| G{numericOverrides?}
G -->|"override found"| H["Return override value"]
G -->|"no override"| I{numericDefault}
I -->|"'decode'"| J["decodeNumericReference(cp)\nguards RangeError\n→ char or ''"]
I -->|"'literal'"| K["Return original match\n(leave as-is)"]
I -->|"'space'"| L["Return ' '"]
E -->|"unknown group\n(unknownToSpace=true)"| M["Return ' '"]
F --> N["Output string\n(exactly one level decoded)"]
H --> N
J --> N
K --> N
L --> N
M --> N
Comments Outside Diff (1)
-
scripts/_trade-parse-utils.mjs, line 1-5 (link)The
importstatement is placed before the module-level JSDoc block comment. Every other file touched in this PR places imports after any leading file-level comment. Swapping the order keeps the convention consistent.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Reviews (1): Last reviewed commit: "fix(entity-decode): single-pass decoder ..." | Re-trigger Greptile
Superseded by #5754, which the maintainer merged with a repo-wide fix for the same issue (#5436). Closing to avoid a stale rebase.