Skip to content

fix: detect Node via process.type to keep Electron main process working#1033

Open
claygeo wants to merge 2 commits into
electric-sql:mainfrom
claygeo:fix/electron-in-node-main-process
Open

fix: detect Node via process.type to keep Electron main process working#1033
claygeo wants to merge 2 commits into
electric-sql:mainfrom
claygeo:fix/electron-in-node-main-process

Conversation

@claygeo

@claygeo claygeo commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #951 (thanks for merging that one!) and #813.

#951 fixed the Electron renderer crash by adding !process.versions.electron to IN_NODE. While verifying it I noticed that guard also flips IN_NODE to false in the Electron main and utility processes, because process.versions.electron is set in every Electron process, not just the renderer. Those are real Node environments, so PGlite then takes the browser path and loses the Node.js fs code path it needs. Running PGlite in the main process for filesystem-backed storage (keeping DB access out of the renderer) is a common pattern, so this is a regression.

This keys the check off process.type instead, which cleanly separates Electron's web contexts from its Node ones:

Environment process.type !process.versions.electron (#951) process.type exclusion (this PR)
Plain Node undefined Node ✅ Node ✅
Node worker_threads undefined Node ✅ Node ✅
Electron main browser browser ❌ Node ✅
Electron utility utility browser ❌ Node ✅
Electron renderer renderer browser ✅ browser ✅
Electron renderer web worker worker browser ✅ browser ✅
Electron renderer service worker service-worker browser ✅ browser ✅
Browser (no process) n/a browser ✅ browser ✅

So it still fixes the original renderer crash from #813, and additionally keeps the Node path working in the Electron main and utility processes.

process.type is Electron specific and not in @types/node, hence the small structural cast.

Checks run locally: typecheck, lint, and build pass for @electric-sql/pglite-utils. Changeset included (patch to @electric-sql/pglite).

The Electron guard added in electric-sql#951 (!process.versions.electron) also
disabled the Node code path in the Electron main and utility processes,
which are real Node environments that need it. process.versions.electron
is set in every Electron process, not just the renderer.

Key off process.type instead: exclude only Electron's web contexts
('renderer', 'worker', 'service-worker'), keeping the Node path for the
main ('browser') and utility processes, and for plain Node (no
process.type). Fixes the renderer crash from electric-sql#813 without regressing
PGlite in the Electron main process.

Refs electric-sql#951, electric-sql#813
Comment thread packages/pglite-utils/src/utils.ts Outdated
Comment on lines +14 to +16
!['renderer', 'worker', 'service-worker'].includes(
(process as { type?: string }).type ?? '',
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting hairy and hard to comprehend. Please extract this logic to a separate function, move the comments there and please trim them down. Code is the best doc, no need for a 9 line comment if the logic is clear.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 33b5b09. Extracted the detection into an isElectronWebContext() helper, moved the comment onto it, and trimmed it from 9 lines down to 3. Behavior is identical: IN_NODE is still the process.versions.node check plus !isElectronWebContext(), which keeps the Node fs path on in the Electron main and utility processes. Thanks for the steer.

Address review feedback on electric-sql#1033: move the Electron/Node detection out of the
IN_NODE expression into a small named helper and trim the explanatory comment
from 9 lines to 3. Behavior is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants