8000 Load font paths from disk in Bun by hipstersmoothie · Pull Request #1576 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

Load font paths from disk in Bun - #1576

Merged
hipstersmoothie merged 1 commit into
mainfrom
fix/load-ttf-bun-self
Dec 4, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
fix/load-ttf-bun-self

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Load font paths from disk in Bun

In Bun, loadTTF("fonts/Inter-Regular.otf") fails with loadTTF: couldn't fetch "...": fetch() URL is invalid, though the README says @jimp/layout and loadTTF run in Bun. loadFont fails the same way for a .fnt path.

The loaders decide between reading from disk and fetch with isWebWorker = typeof self !== "undefined" && self.document === undefined. Bun defines self on its main thread, without a document, so it looks like a web worker and the path goes to fetch. Deno's main thread has the same self.

isWebWorker now checks for WorkerGlobalScope, which browser workers have and Bun's and Deno's main threads don't. It lives in a new runtime.ts, shared by loadTTF, loadFont and the bitmap font parser (which had two copies of the old check). Node and browser main threads behave as before: they never had a self without a window.

runtime.node.test.ts stubs a global self the way Bun has it, then checks that isWebWorker is false and that loadTTF and loadFont read paths from disk. On main all three tests fail, with the same fetch error.

Validation on macOS, Node 24.15:

  • plugin-print's tests pass in Node (137) and headless Chromium (45).
  • plugin-print's ESLint and build pass.
  • Against the built dist, loadTTF and loadFont load fonts by path in Bun 1.3.9 and Node.

Not tested: Deno end to end (from outside a project it stops earlier, at import("opentype.js")), a real browser web worker, and the full workspace test run.

🤖 Generated with Claude Code

Bun defines `self` on its main thread, without a `document`, so the
loaders' web worker check (`typeof self !== "undefined" &&
self.document === undefined`) took Bun for a worker. loadTTF and loadFont
then passed a file path to fetch, which fails with "fetch() URL is
invalid", though the README says they run in Bun.

Check for WorkerGlobalScope instead, which browser workers have and Bun's
and Deno's main threads don't. The check lives in runtime.ts, shared by
loadTTF, loadFont and the bitmap font parser.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added the patch Increment the patch version when merged label Dec 3, 2026
@hipstersmoothie
hipstersmoothie merged commit 1eb92d5 into main Dec 4, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the fix/load-ttf-bun-self branch December 4, 2026 07:39
@hipstersmoothie hipstersmoothie added released This issue/pull request has been released. and removed released This issue/pull request has been released. labels Dec 4, 2026
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.3.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Increment the patch version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0