Skip to content

Fix: use Node's built-in fetch to avoid Electron/Node 24.17.0 'Premature close' (#99) - #101

Open
randomBrainstormer wants to merge 1 commit into
mainfrom
fix/electron-native-fetch
Open

Fix: use Node's built-in fetch to avoid Electron/Node 24.17.0 'Premature close' (#99)#101
randomBrainstormer wants to merge 1 commit into
mainfrom
fix/electron-native-fetch

Conversation

@randomBrainstormer

Copy link
Copy Markdown
Owner

Summary

Fixes the Premature close failure reported in #99, which breaks OAuth token refresh and calendar fetches on current Electron builds.

The root cause (thoroughly diagnosed by @daotsc in #99) is an upstream Node.js 24.17.0 regression (nodejs/node#63989), already fixed in a later 24.x. gaxios (google-auth-library's HTTP transport) falls back to the node-fetch@2 package when there's no browser window global — i.e. node_helper.js running inside MagicMirror's Electron main process — and that combination trips a false-positive ERR_STREAM_PREMATURE_CLOSE on Node 24.17.0's keep-alive path.

Fix

Point gaxios at Node's built-in fetch (undici) via its documented fetchImplementation option. undici has its own connection pool and never touches http.Agent, so it sidesteps the regression. Both the token refresh and the events.list call go through the OAuth2 client's transporter, so a single injection point covers both.

Why this approach (vs #100)

#100 fixes the same issue by replacing googleapis with hand-rolled https calls. This PR is a smaller alternative that:

  • keeps the entire googleapis stack (auto token-refresh persistence, error typing, future API compatibility) instead of reimplementing it;
  • requires no re-authentication — it doesn't change the token.json format, so existing users keep working after updating;
  • is ~28 lines and adds no dependencies.

Testing

  • npm test — 30/30 passing
  • npx eslint node_helper.js — clean
  • Verified at runtime that the native fetch is attached to the transporter used for both the token refresh and the events.list dispatch.

⚠️ Needs confirmation on a machine that reproduces the bug (Electron 42.5.2 / Node 24.17.0). Asked the original reporter to verify.

…ure close'

gaxios (google-auth-library's transport) falls back to node-fetch@2 when
there is no browser `window` global — i.e. node_helper.js running inside
MagicMirror's Electron main process. On Node 24.17.0 that path throws a
false-positive ERR_STREAM_PREMATURE_CLOSE from an http.Agent keep-alive
regression (nodejs/node#63989), breaking OAuth token refresh and calendar
fetches.

Point gaxios at Node's built-in fetch (undici) via its documented
fetchImplementation option. undici has its own connection pool and never
touches http.Agent, so it sidesteps the bug while keeping the googleapis
stack intact and requiring no re-authentication. Both the token refresh
and the events.list call go through the OAuth2 client's transporter, so
setting it there covers both.

Fixes #99
@jhaywood-sys-pi

Copy link
Copy Markdown

Updating my GoogleCalendar module to this branch has resolved the Premature Close error for me and allowed the calendar to be displayed again.

@irsx02

irsx02 commented Aug 17, 2026

Copy link
Copy Markdown

@randomBrainstormer - can you merge your new changes to the main branch?

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.

3 participants