Skip to content

feat(cli): vercel target reaches plain Postgres without a WebSocket proxy - #698

Merged
blove merged 2 commits into
mainfrom
blove/vercel-local-postgres-690
Sep 17, 2026
Merged

blove merged 2 commits into
mainfrom
blove/vercel-local-postgres-690

Conversation

@blove

@blove blove commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Closes #690.

What

  • Driver selection on the vercel target. The generated stores.mjs now decides per request which driver opens the pool: B4_PG_DRIVER when set; otherwise @neondatabase/serverless when B4_PG_WS_PROXY is set or the DATABASE_URL host ends in .neon.tech; otherwise a pooled pg connection. The built function therefore runs against a local Postgres with no proxy. The hono variant is unchanged (neon only; B4_PG_DRIVER=pg is refused there because workerd has no TCP).
  • B4_PG_WS_PROXY documented and normalised on both targets: host:port, ws://host:port, or wss://host:port (the last keeps TLS on). Any other scheme, a path, or a query is rejected up front with a message naming the variable and the accepted forms, instead of failing inside the driver.
  • selectPostgresDriver / normalizeWsProxy are a pure module exported from @b4run/cli/fetch, so the generated file runs the same code the unit tests do.
  • createPostgresPool is a new export of @b4run/postgres-storage/node (the existing pool builder made public, 'error' listener included). The emitted file reaches pg through that package's declared dependency, so apps do not have to declare pg and pnpm's strict layout resolves it.
  • Docs: a "Database drivers and B4_PG_WS_PROXY" section plus a "Run the Vercel bundle locally" recipe on the edge deployment page, and rows for the new exports on the two API pages.

Bundle size

pg is bundled into index.func/index.mjs rather than made an optional external: validateVercelOutput rejects any unresolved bare specifier because the function directory must be self-contained, and an external pg would fail exactly the way the issue describes, one hop later. Measured cost of @b4run/postgres-storage/node over the main entry is ~177 KiB unminified (21 → 198 KiB), which is well inside Vercel's function limits.

Tests

  • test/postgres-driver.test.ts: 19 unit tests for normalisation and selection (both targets, overrides, conflicts, malformed values).
  • test/vercel-stores.test.ts: drives the emitted Vercel stores.mjs against driver doubles — pg for a local host, neon for a Neon host, proxy keeps neon and strips ws://, B4_PG_DRIVER overrides both ways, malformed values fail before any pool opens, migration pass still once per isolate, and the hono variant never imports the pg builder.
  • test/hono-target.test.ts: new case for ws:// / wss:// / rejected http:// through the emitted file; the stub-driving helpers moved to test/helpers/emitted-stores.ts so both suites share them.
  • test/vercel-target.test.ts: a Docker-gated sibling of the existing round trip that streams Agent Protocol requests through the copied function against plain Postgres with no proxy (passes locally in ~29s; B4_REQUIRE_DOCKER=1 makes it mandatory in CI).
  • packages/postgres-storage/test/node-pool.test.ts: createPostgresPool returns a real pg.Pool with the error listener and passes options through.

Verified locally on Node 24: full @b4run/cli suite with B4_REQUIRE_DOCKER=1 (162 files, 1845 tests) and @b4run/postgres-storage suite green; pnpm lint, tsc for both packages, and scripts/check-docs.mjs pass. Not released.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
b4-run Ready Ready Preview Sep 17, 2026 3:48pm UTC

Request Review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated approval: this PR received an intelligent (AI) code review. See the review comments on this PR.

blove and others added 2 commits September 17, 2026 08:45
…cket proxy

The vercel target's generated stores.mjs used @neondatabase/serverless
unconditionally, so running the built function against a local Postgres
required an undocumented wsproxy configured through B4_PG_WS_PROXY, whose
value had to be a bare host:port. This makes the Vercel variant select a
driver per request — B4_PG_DRIVER when set, the neon driver when a proxy
is configured or the host is *.neon.tech, and otherwise a pooled pg
connection opened through @b4run/postgres-storage/node's new
createPostgresPool export — and normalises B4_PG_WS_PROXY on both targets
so ws:// and wss:// prefixes are accepted and anything else is rejected
by name.

The decision lives in a pure module exported from @b4run/cli/fetch and is
unit-tested directly; the emitted file is driven against driver doubles
for both targets, and a Docker-gated round trip proves the pg path against
a real Postgres with no proxy. pg is bundled into the function (~177 KiB
unminified) rather than made an optional external, because the Vercel
output validator requires a self-contained index.func.

Closes #690

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The three MDX pages this branch edits carry recorded source digests in
`apps/web/app/seo/lastmod.generated.json`, so editing them makes the
manifest stale and `generate-seo-lastmod --check` fails in source-validate.
Regenerated with `--as-of 2026-09-17`; only the three edited routes move.

Co-Authored-By: Claude Opus 5 <[email protected]>
@blove
blove force-pushed the blove/vercel-local-postgres-690 branch from 1b7bb02 to 07c8db0 Compare September 17, 2026 15:47
@blove
blove merged commit 0429cec into main Sep 17, 2026
29 of 30 checks passed
@blove
blove deleted the blove/vercel-local-postgres-690 branch September 17, 2026 16:06
blove added a commit that referenced this pull request Sep 17, 2026
#698 restructured the generated stores.mjs around selectPostgresDriver and
moved the emitted-stores harness into test/helpers/emitted-stores.ts, both in
the code this branch rewrites.

Resolved by taking main's structure and re-applying this branch's work on top:
the naming bindings resolve before the driver is selected (still before any
pool is opened), NAMING_STORAGE_STUB joins the shared helper beside the other
store stubs, and the new tests use the helper's (appRoot, storesDir, envs)
signature. The inline cliStub gained describeConnectionTarget and
formatErrorChain, which the emitted file now imports.

The edge docs line about per-isolate migration tracking now says per database,
schema and prefix, matching the memo key.

Co-Authored-By: Claude Opus 5 <[email protected]>
blove added a commit that referenced this pull request Sep 17, 2026
Resolves four conflicts against #693 (typed build.targets) and #698
(vercel Postgres driver):

- core types.ts and the configuration reference keep main's
  BuildTargetName union alongside the new build.vercel block.
- The targets registry keeps main's Record<BuildTargetName, BuildTarget>
  alongside the config field on BuildEmitContext.
- The SEO lastmod manifest is regenerated rather than hand-merged.

Also renames the function path in #698's new "Run the Vercel bundle
locally" snippet, which still imported index.func.

Co-Authored-By: Claude Opus 5 <[email protected]>
blove added a commit that referenced this pull request Sep 17, 2026
#698 landed while this branch was open and closed #690: the vercel
target's generated stores now select a driver per request, so the built
bundle reaches a plain local Postgres over pooled `pg` with no WebSocket
proxy at all. The guide documented the proxy as a hard requirement, so
that section was rewritten:

- the local recipe is now DATABASE_URL plus a bare `node --input-type=module`
  import, no Docker network and no wsproxy containers;
- `B4_PG_WS_PROXY` is described with the forms it actually accepts
  (host:port, ws://, wss://), as a way to rehearse the Neon path rather
  than as a precondition, alongside `B4_PG_DRIVER` and the
  proxy-with-pg configuration error;
- the driver detail links to the edge page's new "Database drivers"
  section instead of restating it;
- the fit check no longer claims WebSocket Postgres is required, and the
  Neon section notes non-Neon hosts get a pooled `pg` connection.

Conflicts resolved toward main: configuration.mdx takes main's
`BuildTargetName` union wording from #693, which already names both
edge targets, and lastmod.generated.json is regenerated.

Co-Authored-By: Claude Opus 5 <[email protected]>
blove added a commit that referenced this pull request Sep 17, 2026
#695 landed two things this guide needed:

- `build.vercel.outDir` / `b4 build --out-dir <dir>`, with the flag
  winning over the config value, app-root-relative resolution, the
  rejection of a directory containing the app root, and the error when
  vercel is not a configured target;
- a relaxed output validator. `config.json` now needs version 3 and a
  route reaching `/index` rather than the exact catch-all, and extra
  routes and top-level keys are accepted.

Together those turn the frontend-composition section from a list of
workarounds into a supported flow: stage the runtime tree elsewhere,
assemble into `.vercel/output`, and a composed config still validates.
The section now says so, and says plainly that the validator runs on
what the target stages rather than on what an assembler produces, and
that renaming the function off `dest: "/index"` makes the final config
the author's responsibility.

Also records that the generated stores import
`@b4run/postgres-storage/node`, which reaches `pg` through that
package's own dependency so an app never declares `pg` itself — the one
real gap raised by the session that owns #698.

cli.mdx conflicted and takes main's bullet, which covers outDir and the
relaxed validator, with a link to this page appended.

Co-Authored-By: Claude Opus 5 <[email protected]>
blove added a commit that referenced this pull request Sep 17, 2026
…708)

The "Run the Vercel bundle locally" recipe added in #698 verified nothing, because
#694 landed after it. #694 made GET /healthz pure process liveness and added
GET /readyz, and the recipe still called /healthz, which never builds the
request's stores — so it answered 200 whether or not DATABASE_URL resolved. That
is the opposite of the section's purpose: proving the pg fallback reached a plain
local Postgres with no WebSocket proxy.

Measured on a Vercel fixture built from main, against a real Postgres container:

  /healthz, reachable database:   200 {"status":"ready"}
  /healthz, unreachable database: 200 {"status":"ready"}
  /readyz,  reachable database:   200, checkpointer/permissionsStore/threadsStore ok
  /readyz,  unreachable database: 503 not_ready, naming connect ECONNREFUSED

The two sample bodies in the page are that run's output. The password in the
failing connection string does not appear in the response, which is what the
redaction note rests on.

Also adds /readyz to the rooted surfaces the runtime owns, a list written before
#694 that omitted it.

Co-Authored-By: Claude Opus 5 <[email protected]>

This branch was successfully deployed

1 active and 1 inactive deployments
Preview — 07c8db09 Deployed Sep 17, 2026 by vercel[bot]
vercel-preview — 07c8db09 Deployed Sep 17, 2026 by blove via vercel-native #1947
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.

vercel target: local execution of the bundle requires an undocumented WebSocket proxy

1 participant