From 6b5a4dce2ba028e63a120de508e10fb4fbb3b0c6 Mon Sep 17 00:00:00 2001 From: claygeo Date: Fri, 19 Jun 2026 16:32:42 -0400 Subject: [PATCH 1/2] fix: detect Node via process.type, not process.versions.electron The Electron guard added in #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 #813 without regressing PGlite in the Electron main process. Refs #951, #813 --- .changeset/electron-main-process-in-node.md | 5 +++++ packages/pglite-utils/src/utils.ts | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/electron-main-process-in-node.md diff --git a/.changeset/electron-main-process-in-node.md b/.changeset/electron-main-process-in-node.md new file mode 100644 index 000000000..1bd2b4b55 --- /dev/null +++ b/.changeset/electron-main-process-in-node.md @@ -0,0 +1,5 @@ +--- +'@electric-sql/pglite': patch +--- + +Detect the Node environment via `process.type` instead of `process.versions.electron`. The previous Electron guard (#951) also treated the Electron main and utility processes as non-Node, which broke PGlite's filesystem code path there. `process.type` only excludes Electron's web contexts (renderer, web worker, service worker), so PGlite keeps using the Node.js path in the Electron main and utility processes. Follow-up to #951 / #813. diff --git a/packages/pglite-utils/src/utils.ts b/packages/pglite-utils/src/utils.ts index 306c386bb..4f3ff55d1 100644 --- a/packages/pglite-utils/src/utils.ts +++ b/packages/pglite-utils/src/utils.ts @@ -1,8 +1,19 @@ +// Electron exposes a Node-like `process` (with `process.versions.node`) in its +// renderer/worker/service-worker contexts, which made `IN_NODE` true there and +// crashed PGlite on the Node.js fs path (#813). The earlier `!process.versions +// .electron` guard fixed the renderer but also disabled the Node 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). Key +// off `process.type` instead: it is 'renderer'/'worker'/'service-worker' only in +// Electron's web contexts, and 'browser' (main) / 'utility' / undefined (plain +// Node) wherever a Node fs is actually available. export const IN_NODE = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string' && - !process.versions.electron + !['renderer', 'worker', 'service-worker'].includes( + (process as { type?: string }).type ?? '', + ) export const WASM_PREFIX = '/pglite' From 33b5b09a3c25cebde57bb4157dd59a3a84ab6de4 Mon Sep 17 00:00:00 2001 From: claygeo Date: Tue, 30 Jun 2026 13:45:35 -0400 Subject: [PATCH 2/2] refactor: extract isElectronWebContext helper, trim comment Address review feedback on #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. --- packages/pglite-utils/src/utils.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/pglite-utils/src/utils.ts b/packages/pglite-utils/src/utils.ts index 4f3ff55d1..30c9ad7cd 100644 --- a/packages/pglite-utils/src/utils.ts +++ b/packages/pglite-utils/src/utils.ts @@ -1,19 +1,16 @@ -// Electron exposes a Node-like `process` (with `process.versions.node`) in its -// renderer/worker/service-worker contexts, which made `IN_NODE` true there and -// crashed PGlite on the Node.js fs path (#813). The earlier `!process.versions -// .electron` guard fixed the renderer but also disabled the Node 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). Key -// off `process.type` instead: it is 'renderer'/'worker'/'service-worker' only in -// Electron's web contexts, and 'browser' (main) / 'utility' / undefined (plain -// Node) wherever a Node fs is actually available. +// Electron exposes a Node-like `process` in its renderer/worker/service-worker +// contexts, which must use the browser fs path. Its main and utility processes +// are real Node environments (#813). +function isElectronWebContext(): boolean { + const type = (process as { type?: string }).type + return type === 'renderer' || type === 'worker' || type === 'service-worker' +} + export const IN_NODE = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string' && - !['renderer', 'worker', 'service-worker'].includes( - (process as { type?: string }).type ?? '', - ) + !isElectronWebContext() export const WASM_PREFIX = '/pglite'