Skip to content

fix: replace Bun.serve with Node.js http module in auth-server#19

Closed
NikolayS wants to merge 4 commits into
mainfrom
claude/fix-bun-auth-error-bugz0
Closed

fix: replace Bun.serve with Node.js http module in auth-server#19
NikolayS wants to merge 4 commits into
mainfrom
claude/fix-bun-auth-error-bugz0

Conversation

@NikolayS
Copy link
Copy Markdown
Contributor

@NikolayS NikolayS commented Dec 26, 2025

Summary

  • Fixed postgresai auth command failing with "Bun is not defined" error after the Node.js to Bun migration
  • Replaced Bun-specific Bun.serve() API with Node.js native http.createServer() in cli/lib/auth-server.ts

Problem

After the CLI migration to Bun (commit 1b1425a), the auth command broke completely:

❯ postgresai auth
Starting authentication flow...
Starting local callback server...
Authentication error: Bun is not defined

Root cause: The auth-server.ts file used Bun.serve() which is a Bun-specific API. Since the CLI is compiled with bun build --target node, the Bun global doesn't exist at runtime.

Solution

Replaced the Bun.serve() implementation with Node.js native http.createServer():

  • Updated server creation to use http.createServer() callback pattern
  • Changed serverInstance.stop() to serverInstance.close()
  • Updated type annotations from ReturnType<typeof Bun.serve> to http.Server
  • Removed unused getServerPort() helper function

Testing

All CLI command groups verified working:

Command Group Status
Main (--version, help, prepare-db)
mon (17 subcommands)
auth (login, show-key, remove-key)
issues (list, view, post_comment)
mcp (start, install)

OAuth flow now works correctly:

Starting authentication flow...
Starting local callback server...
Callback server listening on port 36679 ✅
Initializing authentication session...

https://gitlab.com/postgres-ai/postgres_ai/-/issues/69


Note

Migrates the OAuth callback server off Bun to ensure Node runtime compatibility and more reliable startup/teardown.

  • Rewrites cli/lib/auth-server.ts using Node http.createServer() with explicit server.close(), structured HTML responses, and CSRF/state validation
  • Adds ready: Promise<number> to expose the actual bound port; caller now awaits readiness before constructing redirect_uri
  • Improves timeout and error handling (e.g., EADDRINUSE) and centralizes shutdown via stopServer()
  • Updates cli/bin/postgres-ai.ts to await callbackServer.ready and use http://127.0.0.1:<port>/callback (avoid IPv6 issues)
  • Removes Bun-specific APIs/usages and related helper

Written by Cursor Bugbot for commit 15eccaf. Configure here.

The auth command was failing with "Bun is not defined" because
auth-server.ts used Bun.serve() which is a Bun-specific API that
doesn't exist when the code is compiled to run on Node.js.

Replaced with Node.js native http.createServer() which works in
both Bun and Node.js environments.
@NikolayS
Copy link
Copy Markdown
Contributor Author

@cursor review

@cursor
Copy link
Copy Markdown

cursor Bot commented Dec 26, 2025

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on January 16.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@NikolayS
Copy link
Copy Markdown
Contributor Author

@cursor review

@NikolayS
Copy link
Copy Markdown
Contributor Author

@claude reivew

Comment thread cli/lib/auth-server.ts
Comment thread cli/lib/auth-server.ts
- Add `ready` promise to CallbackServer interface that resolves when
  server is actually listening, fixing race condition where getPort()
  could return 0 before server started
- Add error handler for server 'error' event to properly handle
  EADDRINUSE and other binding errors instead of silent failures
- Update postgres-ai.ts to await ready promise instead of fragile
  100ms setTimeout workaround
- Make stopServer() fully idempotent by clearing timeout on manual stop
- Use 127.0.0.1 consistently in redirect URI to match server bind address
  (fixes potential IPv6 issues on dual-stack hosts where localhost may
  resolve to ::1 while server only binds to 127.0.0.1)
@NikolayS
Copy link
Copy Markdown
Contributor Author

@cursor review again

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!

@NikolayS NikolayS closed this Dec 26, 2025
@NikolayS NikolayS deleted the claude/fix-bun-auth-error-bugz0 branch December 26, 2025 20:32
@NikolayS NikolayS restored the claude/fix-bun-auth-error-bugz0 branch December 26, 2025 20:33
@NikolayS NikolayS reopened this Dec 26, 2025
The test was timing out at 5s in CI. Increased to 15s to account for
slower CI environments spinning up temporary PostgreSQL instances.
@NikolayS NikolayS closed this Dec 26, 2025
@NikolayS NikolayS deleted the claude/fix-bun-auth-error-bugz0 branch December 26, 2025 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants