Skip to content

fix: stop source view renders from running API workers out of memory - #1605

Merged
crowlKats merged 5 commits into
mainfrom
cap-source-view-size
Nov 29, 2026
Merged

crowlKats merged 5 commits into
mainfrom
cap-source-view-size

Conversation

@crowlKats

@crowlKats crowlKats commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Prod jsr-api logged ~4,700 exceededMemory invocations in 24h. Most were cheap requests (package GETs, /downloads: ~5ms CPU, long wall times) on isolates that died while they were in flight. The requests actually doing heavy work were /source renders of very large generated files: @fedify/vocab's vocab.ts (3.2 MB), @skmtc/core's _schemas/cloudflare.json (10.3 MB) and stripe.json (6.8 MB), plus a few large /docs renders. The handler highlighted the whole file with tree-sitter, buffered the highlighted HTML and a second full copy of the file for the copy button, then copied all of it into JS.

Three commits:

  1. fix: cap the source file view at 512 KiB: files over the cap are trimmed at the last full line before it and flagged truncated. The file view shows a notice when that happens.
  2. feat: highlight the source file view in the frontend: /source returns the capped text as content plus the specifier links as UTF-16 ranges. The frontend highlights it with Prism into the same markup and pl-* classes, so line numbers, #L anchors and specifier links keep working. At the cap, the worst case (vocab.ts) takes ~66ms and ~33 MB of transient heap.
  3. refactor: remove tree-sitter: docs code blocks are emitted unhighlighted with data-lang, and the frontend highlights them while loading docs (docs pages, markdown preview, diff view). The API no longer does any syntax highlighting, and tree-sitter plus its 14 grammar crates are removed.

Highlighting multi-megabyte files (generated vocabularies, bundled JSON
schemas) in the file view pushed API worker isolates past their memory
limit, taking every concurrent request on them down with exceededMemory.

Files over the cap are now trimmed at the last full line before it and
flagged with `truncated`, and the file view shows a notice when a file
was cut short.
The API no longer highlights files for the file view. `/source` now
returns the (capped) file text as `content`, along with the import and
export specifier links as UTF-16 ranges into it, and the frontend
highlights it with Prism into the same markup and classes as before, so
line numbers, `#L` anchors and specifier links keep working.

This also drops the second full copy of every file the response used to
carry in the copy button's `data-copy` attribute.

BREAKING: `view` is replaced by `content` and `links` in the source
endpoint's file response.
Docs code blocks are now highlighted in the frontend too. The API emits
them unhighlighted, tagged with their language as `data-lang` on the
`<code>` element, and the frontend highlights them with the same Prism
highlighter as the file view while loading the docs payload. This covers
docs pages, the markdown preview and the diff view.

That leaves the API with no syntax highlighting at all, so tree-sitter
and its 14 grammar crates are gone.
@crowlKats
crowlKats added this pull request to the merge queue Nov 29, 2026
Merged via the queue into main with commit 87b4b0c Nov 29, 2026
10 checks passed
@crowlKats
crowlKats deleted the cap-source-view-size branch November 29, 2026 11:21

This branch was previously deployed

1 inactive deployment
staging — 18481bd1 Deployed Nov 29, 2026 by crowlKats via staging #3939
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants