Repository navigation
Conversation
Hyperdrive speaks the Postgres wire protocol, so this crate writes no driver: it opens the Worker's socket to a Hyperdrive binding, settles TLS through the Workers runtime's startTls(), and hands the socket to sqlx-postgres. `connect`, `connect_with` and `options` return sqlx's own `PgConnection` and `PgConnectOptions`, so consumers get `sqlx::Postgres` with all of its types, transactions and `sqlx::migrate!`. It needs two sqlx changes that are not released yet, proposed in transact-rs/sqlx#4426: `PgConnection::connect_with_socket`, and an `Instant` that does not panic on wasm32-unknown-unknown. Until they ship, the workspace patches every sqlx crate to that branch, pinned to a commit, and the crate is `publish = false` and out of the release-please config. The test Worker runs ten scenarios under `wrangler dev --local` against a scratch Postgres that run.sh starts with SCRAM auth and no TLS, so the dev shell gains postgresql and `make test-worker` a third runner.
Hyperdrive also fronts MySQL, so the crate is laid out as sqlx is: one feature per database, none by default, each adding a module that returns that sqlx driver's connection. Postgres moves to `postgres::connect`, `postgres::connect_with` and `postgres::options`; the socket adapter and the binding lookup stay shared, and `HdSocket::connect` opens the socket for any driver. The sqlx dev-dependency gains `macros`, as D1's has: it is what turns on `sqlx-postgres/offline`, and without it a build with no features of this crate's own gets sqlx-postgres without `describe`.
Themezv
added a commit
to MemeBattle/monorepo
that referenced
this pull request
Oct 18, 2026
…e-hd (#794) * build: pin sqlx to the MemeBattle fork (connect_with_socket, wasm Instant) sqlx 0.9.0 cannot open a Postgres connection on wasm32-unknown-unknown: PgStream::connect only knows TCP and unix sockets, and std::time::Instant::now() traps there. MemeBattle/sqlx tag v0.9.0-memebattle.1 is v0.9.0 plus the two commits upstream refused in transact-rs/sqlx#4444 (PgConnection::connect_with_socket and a wasm-safe Instant). The only lockfile changes are the sqlx crates' source and web-time, which the fork's sqlx-core uses on wasm32. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * build: vendor sqlx-cloudflare-hd from sqlx-contrib/sqlx-cloudflare#16 Copied from branch feat/sqlx-cloudflare-hd at 9499455d (unpublished, MIT). It opens a Worker socket to a Hyperdrive binding and hands it to sqlx's own Postgres driver through PgConnection::connect_with_socket. Sources, README and LICENSE are verbatim; only Cargo.toml is rewritten to stand alone outside the upstream workspace. VENDOR.md records the source and every change. worker 0.8.6+ needs wasm-bindgen 0.2.128+, so the lockfile moves the wasm-bindgen family (wasm-bindgen 0.2.129, js-sys/web-sys 0.3.106, wasm-bindgen-futures 0.4.79); those only build on wasm targets. oxfmt now skips vendor/ so the copied Markdown stays byte for byte. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * ci(cas): run the CAS workflow on vendor changes Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * ci(cas): compile the vendored Hyperdrive adapter for wasm32 Adding vendor/** to the path filter only decided when the workflow runs; every job still built apps/cas alone, and CAS does not depend on sqlx-cloudflare-hd, so a regression in the adapter or in the wasm-only code of the sqlx fork could merge with every check green. A dedicated job now checks and lints the crate with its postgres feature for wasm32-unknown-unknown, the target it exists for. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Rust Workers can talk to D1 and Durable Object storage through sqlx, but not to Postgres through Hyperdrive. Hyperdrive speaks the Postgres wire protocol, so sqlx-postgres should just work, except stock sqlx 0.9 panics twice on
wasm32-unknown-unknown: connecting ends inrt::missing_rt, since sqlx can only open sockets through tokio or async-io, and every query callsstd::time::Instant::now(), which isn't implemented on that target.Change
Instant, for hosts like Cloudflare Workers transact-rs/sqlx#4426 proposes the two changes:PgConnection::connect_with_socket(&options, socket), andsqlx_core::rt::Instant(web-timeon that target). The branch isiamralch/sqlxwasm-custom-socket.[patch.crates-io]takessqlx,sqlx-coreandsqlx-postgresfrom that branch, pinned to4313dae. The patch applies to the whole graph, so D1 and DO build against it too; their test Workers keep their own locks on crates.io sqlx.sqlx-cloudflare-hd, with no driver of its own. Laid out like sqlx: one feature per database, none by default, so MySQL (which Hyperdrive also fronts) can come later.postgres::connect(&env, binding),postgres::connect_with(&hyperdrive, &options)andpostgres::options(&hyperdrive)return sqlx's ownPgConnection/PgConnectOptions, so consumers getsqlx::Postgres: its types,query_as,FromRow, transactions andsqlx::migrate!. The socket adapter and binding lookup are shared.StartTlsmode. The crate sends theSSLRequestitself, upgrades withstartTls()when the server accepts, and then hands the socket to sqlx withsslmode=disableso sqlx doesn't negotiate again.prefer/requirebehave as in libpq. It refuses bytes that arrive afterSbut before the upgrade (CVE-2021-23222).HdSocketturnsworker::Socket'sAsyncRead/AsyncWriteinto sqlx's readiness-basedSocket.getrandom'swasm_jsfeature is enabled by the crate, so consumers don't have to.FromRow, migrations,sslmodedisableandrequire, and custom options.run.shstarts a scratch Postgres with SCRAM auth and no TLS and points the binding at it throughCLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HD. The dev shell gainspostgresql, andmake test-workergets a third runner.Not mergeable yet
publish = falseand not in the release-please config. When sqlx releases #4426: drop the patch, bump sqlx, and add the crate to release-please.wrangler dev --localconnects straight to Postgres, so the accepted-TLS path (S, thenstartTls()) and Hyperdrive's own behaviour are unexercised. That needs a real Hyperdrive config on a Cloudflare account.PoolorPgListener. Both need a runtime to spawn tasks. One connection per request is the intended model; Hyperdrive does the pooling.Verification
make check lint test doc-checkpass, host and wasm32.make test-workerpasses for all three: D1 29 scenarios, DO 36, Hyperdrive 10.tests/postgres/postgres.rs(58 passed, 2 ignored, plus the newit_connects_with_socket) andtests/postgres/migrate.rs(4) pass. With only theInstantchange reverted, the Worker panics withtime not implemented on this platform.