mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-05 15:47:54 +01:00
* fix(prices): read proxy statements without a DOM so the daily run cannot stall the site appcron-substrata-prices timed out (curl exit 28, 0 bytes in 300s) although the route stops starting work at 200s, and on 2026-11-24 the whole site stopped answering during the 22:07 UTC run. Cause: the facts half parsed each company's multi-MB DEF 14A with linkedom + Readability. V8 sizes its heap from the box's RAM, not the unit's MemoryMax, so that garbage was not collected: measured over the first 40 SEC filers, RSS climbed 334 MB -> 1107 MB before a major GC, past the app unit's MemoryHigh=768M, where the kernel throttles the whole process. The proxy table is read by pattern, so it needs text, not an article: strip tags in a few linear passes (lib/sources/html-text.ts). Same 40 companies: peak RSS 383 MB, heap 75-145 MB; identical parsed rows on all 30 proxies compared; parse 1.6 s against 30 s. Co-Authored-By: Claude <noreply@anthropic.com> * docs(readme): PRs merge through forge/verify, not the removed auto-merge workflow Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
/**
|
||
* An HTML page as plain text, without building a DOM: scripts, styles and
|
||
* comments dropped, every tag a space (block ends a newline), entities decoded.
|
||
*
|
||
* For filings read by pattern (a proxy statement's ownership table), where a
|
||
* readability pass is not wanted and a DOM is too dear: a multi-MB proxy
|
||
* parsed by linkedom + Readability left 50-130 MB of garbage per company that
|
||
* V8 did not collect before the app passed its unit's MemoryHigh (768M), and
|
||
* the kernel then throttled the whole site (appcron-substrata-prices, exit 28,
|
||
* 2026-11-23). This is a few linear passes over the string.
|
||
*/
|
||
|
||
const NAMED: Record<string, string> = {
|
||
amp: '&',
|
||
lt: '<',
|
||
gt: '>',
|
||
quot: '"',
|
||
apos: "'",
|
||
nbsp: ' ',
|
||
ensp: ' ',
|
||
emsp: ' ',
|
||
thinsp: ' ',
|
||
ndash: '–',
|
||
mdash: '—',
|
||
lsquo: '‘',
|
||
rsquo: '’',
|
||
ldquo: '“',
|
||
rdquo: '”',
|
||
bull: '•',
|
||
middot: '·',
|
||
sect: '§',
|
||
para: '¶',
|
||
copy: '©',
|
||
reg: '®',
|
||
trade: '™',
|
||
deg: '°',
|
||
frac12: '½',
|
||
frac14: '¼',
|
||
frac34: '¾',
|
||
sup1: '¹',
|
||
sup2: '²',
|
||
sup3: '³',
|
||
};
|
||
|
||
function decodeEntities(s: string): string {
|
||
return s.replace(/&(#x[0-9a-f]+|#\d+|[a-z][a-z0-9]*);/gi, (whole, body: string) => {
|
||
if (body[0] === '#') {
|
||
const code =
|
||
body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : Number(body.slice(1));
|
||
return Number.isFinite(code) && code > 0 && code <= 0x10ffff
|
||
? String.fromCodePoint(code)
|
||
: ' ';
|
||
}
|
||
return NAMED[body.toLowerCase()] ?? whole;
|
||
});
|
||
}
|
||
|
||
const BLOCK_END =
|
||
/<\/?(?:p|div|tr|table|tbody|thead|li|ul|ol|h[1-6]|section|article|header|footer|blockquote|pre)\b[^>]*>|<br\s*\/?>/gi;
|
||
|
||
export function htmlToText(html: string): string {
|
||
return decodeEntities(
|
||
html
|
||
.replace(/<!--[\s\S]*?-->/g, ' ')
|
||
.replace(/<(script|style|head|title)\b[^>]*>[\s\S]*?<\/\1\s*>/gi, ' ')
|
||
.replace(BLOCK_END, '\n')
|
||
.replace(/<[^>]*>/g, ' '),
|
||
)
|
||
.replace(/[ \t\f\v ]+/g, ' ')
|
||
.replace(/ ?\n[\s]*/g, '\n')
|
||
.trim();
|
||
}
|