[pull] main from jsr-io:main#93
Merged
Merged
Conversation
…1431) ## Step 3 — Hyperdrive Postgres connectivity spike Part of the API service split ([`docs/design/api-service-split.md`](https://github.com/jsr-io/jsr/blob/main/docs/design/api-service-split.md), RFC #1414). Follows #1417 (`jsr_types` extraction) and #1429 (workers-rs scaffold). This PR proves the **wasm database story end-to-end**: the `api.jsr.io` Worker opens a Postgres connection through the Cloudflare **Hyperdrive** binding and runs a trivial `SELECT 1`. No real endpoints move yet — that starts in step 4. ### What's here - **`src/db.rs`** — `connect()` opens the connection via `env.hyperdrive("HYPERDRIVE").connect()`, which returns a `worker::Socket` (TCP to the Hyperdrive endpoint; Hyperdrive terminates TLS to the DB origin, so we use `NoTls` — no rustls in wasm). The socket is handed to `tokio_postgres::Config::connect_raw`, and the connection future is driven on the Worker event loop with `wasm_bindgen_futures::spawn_local`. `ping()` runs `SELECT 1`. - **`Cargo.toml`** — `tokio-postgres` with **`default-features = false`** so its native runtime (tokio `net`/`time`, `socket2`) is excluded from the wasm build; only the wire-protocol codec we drive over our own socket remains. `getrandom` `wasm_js` feature enabled (the SCRAM client nonce needs an RNG, and getrandom has no default backend on `wasm32-unknown-unknown`). - **`src/lib.rs`** — new `GET /api/db_health` route runs the ping and returns the echoed value; every other path still returns `501`. - **`wrangler.toml`** — local-dev Hyperdrive binding with `localConnectionString` pointing at the `docker-compose` Postgres, so the DB path runs under `wrangler dev` without a Cloudflare account. ### Verification `api-worker` CI (fmt `--check`, `clippy --target wasm32-unknown-unknown -- -D warnings`, `cargo build --target wasm32-unknown-unknown --release`) — all green locally on the repo-pinned toolchain (1.89.0). The key result: **`tokio-postgres` compiles to `wasm32-unknown-unknown`** with the feature set above. ### Deliberately deferred The production Hyperdrive **Terraform** (`cloudflare_hyperdrive_config` + the Cloud SQL public-access / authorized-networks wiring it needs) lands with the **deploy/infra PR**, where a deployed Worker actually consumes the binding — rather than creating an orphaned Hyperdrive config now. Flagging since the RFC sequence listed terraform under step 3; happy to fold it in here instead if preferred.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )