Skip to content

Fix: bypass gaxios/fetch for OAuth calls to work around Electron "Premature close" failure - #100

Open
daotsc wants to merge 1 commit into
randomBrainstormer:mainfrom
daotsc:fix/electron-oauth-premature-close
Open

Fix: bypass gaxios/fetch for OAuth calls to work around Electron "Premature close" failure#100
daotsc wants to merge 1 commit into
randomBrainstormer:mainfrom
daotsc:fix/electron-oauth-premature-close

Conversation

@daotsc

@daotsc daotsc commented Jul 16, 2026

Copy link
Copy Markdown

Summary

  • gaxios (google-auth-library's transport) only uses native fetch when a browser window global exists. With no window — i.e. node_helper.js running inside Electron's main process — it falls back to the node-fetch npm package, which reliably fails the OAuth token refresh call with Invalid response body ... Premature close (100% reproducible; identical requests succeed instantly via plain Node outside Electron).
  • Root cause is a Node.js v24.17.0 regression (nodejs/node#63989), not this module.
  • Replaces the googleapis-backed OAuth token exchange/refresh and calendar fetch with raw https requests, which are unaffected by the bug.

Fixes #99

Test plan

  • Reproduce the "Premature close" failure on Node 24.17.x under Electron with the fix reverted
  • Confirm token refresh and calendar fetch succeed with this patch applied, same Node/Electron versions
  • Confirm the one-time interactive auth-code exchange (authenticate/authenticateWeb) still completes when token.json is missing

…mature close" failure

gaxios (google-auth-library's transport) only uses native fetch when a
browser `window` global exists; without one - i.e. node_helper.js
running inside Electron's main process - it falls back to node-fetch,
which reliably fails the OAuth token refresh with "Invalid response
body ... Premature close". Root cause is nodejs/node#63989, not this
module.

Replaces the googleapis-backed OAuth token exchange/refresh and
calendar fetch with raw https requests, which are unaffected.

Fixes randomBrainstormer#99
@randomBrainstormer

randomBrainstormer commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Thanks for digging into this, your detective work was spot on. You correctly found that the real problem is a bug in Node.js 24.17.0 itself (nodejs/node#63989), not in this module. Great catch.

I'd like to fix it in a smaller way than this PR, though. Two concerns:

  1. It would log everyone out. This version changes how the saved token.json file is stored, so anyone who already set up the module would have to reconnect their Google account after updating.
  2. It rewrites a big chunk of the Google connection code by hand, which is a lot to maintain, especially for a bug that Node has already fixed in a newer version.

The good news is your findings point to a much smaller fix. The Google library ships with an option (fetchImplementation) that lets us tell it to use Node's own built-in networking instead of the old piece that's hitting the bug. That one switch avoids the problem, keeps all the existing Google code, and doesn't log anyone out, about 5 lines. I've opened it as #101.

Since you can reproduce this reliably (Pi 5 / Electron 42.5.2 / Node 24.17.0), would you mind trying #101 and confirming the "Premature close" error is gone? Your test is the one that really counts here. Thanks again — this was easy to fix because your report was so good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants