Skip to content

fix: prevent fatal crash when startServer() called with missing hosts#322

Merged
BYK merged 2 commits into
mainfrom
fix/gateway-f-hosts-crash
May 14, 2026
Merged

fix: prevent fatal crash when startServer() called with missing hosts#322
BYK merged 2 commits into
mainfrom
fix/gateway-f-hosts-crash

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 14, 2026

Summary

Fixes LOREAI-GATEWAY-F — a fatal unhandled TypeError: Cannot read properties of undefined (reading 'map') crash when startServer() is called with incomplete config (missing hosts field).

  • Add defensive defaults in startServer(): if config.hosts is missing/empty, default to ["127.0.0.1"] with a warning; also default port if missing
  • Fix hand-written dist/index.d.cts type declarations which declared host?: string (singular, optional) instead of hosts: string[] (plural, required), made startServer config optional, and omitted key exports (startGateway, probeGateway, readPortFile, GatewayHandle, StartOptions, DEFAULT_PORTS, DEFAULT_PORT)

Root Cause

The published .d.cts actively misled npm consumers into calling startServer() directly with incomplete config. The types declared host?: string instead of hosts: string[], made config optional, and omitted the safe entry point startGateway() entirely.

Files Changed

File Change
packages/gateway/src/server.ts Defensive defaults for hosts/port at top of startServer()
packages/gateway/script/bundle.ts Correct .d.cts type declarations matching actual source exports

Verification

  • bun run typecheck — all 4 packages pass
  • bun test — all 1445 tests pass
  • bun run bundle — regenerated dist/index.d.cts verified correct
  • Smoke test: node -e "require('./dist/index.cjs').startServer({})" warns and defaults instead of crashing

BYK added 2 commits May 14, 2026 20:26
startServer() is a public export but had no validation — calling it with
incomplete config (e.g. missing hosts field) caused:
  TypeError: Cannot read properties of undefined (reading 'map')

This was LOREAI-GATEWAY-F, a fatal unhandled crash in production.

Two co-contributing fixes:

1. Add defensive defaults in startServer(): if config.hosts is missing,
   default to ["127.0.0.1"] with a warning. Also default port if missing.

2. Fix hand-written dist/index.d.cts type declarations which declared
   host?: string (singular, optional) instead of hosts: string[] (plural,
   required), made startServer config param optional, and omitted key
   exports (startGateway, probeGateway, readPortFile, GatewayHandle).
   The wrong types actively misled npm consumers into calling startServer
   with incomplete config.
- Remove handleRequest from .d.cts (internal API with wrong signature)
- Export StartOptions type from index.ts (was declared in .d.cts but missing)
- Fix resetPipelineState return type: void -> Promise<void>
- Guard against config itself being undefined/null
- Guard against NaN port values with Number.isFinite()
@BYK BYK merged commit cb8a227 into main May 14, 2026
7 checks passed
@BYK BYK deleted the fix/gateway-f-hosts-crash branch May 14, 2026 20:36
@craft-deployer craft-deployer Bot mentioned this pull request May 14, 2026
6 tasks
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.

1 participant