@@ -2321,6 +2321,91 @@ let store = new DocumentStore('url', 'databaseName', authOptions);
23212321store .initialize ();
23222322```
23232323
2324+ ## Cloudflare Workers
2325+
2326+ The client runs on [ Cloudflare Workers] ( https://developers.cloudflare.com/workers/ ) .
2327+ Three Workers-specific things to know:
2328+
2329+ ** 1. Enable the Node.js compatibility flag.** The client uses a few Node built-ins
2330+ (` node:stream ` , ` node:events ` , …). Add ` nodejs_compat ` to your ` wrangler.toml ` :
2331+
2332+ ``` toml
2333+ compatibility_flags = [" nodejs_compat" ]
2334+ compatibility_date = " 2024-09-23" # or newer
2335+ ```
2336+
2337+ The package ships ` workerd ` and ` worker ` export conditions, so wrangler/esbuild and
2338+ framework bundlers (OpenNext, Vite, …) automatically resolve the Workers-compatible
2339+ ESM build — you do not need any bundler aliases or ` serverExternalPackages ` tweaks.
2340+
2341+ ** 2. On frameworks that use Nitro (TanStack Start, Nuxt, Nitro), make sure Node
2342+ built-ins resolve to the runtime — not to ` unenv ` stubs.** Nitro polyfills Node with
2343+ [ ` unenv ` ] ( https://github.com/unjs/unenv ) , whose ` node:stream ` is incomplete. If Node.js
2344+ compatibility is not enabled, the first request fails and the client raises a descriptive
2345+ error (the underlying ` [unenv] PassThrough is not implemented yet! ` is kept as its cause):
2346+
2347+ ```
2348+ InvalidOperationException: The RavenDB client requires a working node:stream on Cloudflare
2349+ Workers, but this runtime provided an incomplete polyfill. Enable Node.js compatibility by
2350+ adding compatibility_flags = ["nodejs_compat"] (and a recent compatibility_date) to your
2351+ wrangler configuration. ...
2352+ ```
2353+
2354+ The fix is to enable Cloudflare's real Node.js compatibility so Nitro defers ` node: `
2355+ built-ins to ` workerd ` instead of stubbing them. Provide a ` wrangler.toml ` (that Nitro
2356+ reads at build time) with the flag, and set a recent ` compatibilityDate ` in your Nitro
2357+ / TanStack Start config:
2358+
2359+ ``` toml
2360+ # wrangler.toml (read by Nitro at build time)
2361+ compatibility_date = " 2024-09-23" # or newer
2362+ compatibility_flags = [" nodejs_compat" ]
2363+ ```
2364+
2365+ You can confirm it worked: the built bundle no longer inlines ` unenv ` ` node:* ` stubs,
2366+ and requests stream responses successfully.
2367+
2368+ ** 3. mTLS is done with a Cloudflare ` mtls_certificate ` binding, not ` authOptions ` .**
2369+ Workers has no Node ` https ` agent, so X.509 client-certificate authentication cannot
2370+ use the usual ` authOptions ` certificate. Instead, upload the client certificate to
2371+ Cloudflare and hand the client a ` fetch ` bound to that certificate via
2372+ ` conventions.customFetch ` :
2373+
2374+ ``` bash
2375+ # Upload the RavenDB client certificate (PEM: cert + key) once:
2376+ npx wrangler mtls-certificate upload --cert client.crt --key client.key --name ravendb
2377+ ```
2378+
2379+ ``` toml
2380+ # wrangler.toml — bind the uploaded certificate
2381+ mtls_certificates = [
2382+ { binding = " RAVENDB_CERT" , certificate_id = " <id printed by the upload command>" }
2383+ ]
2384+ ```
2385+
2386+ ``` javascript
2387+ import { DocumentStore } from " ravendb" ;
2388+
2389+ export default {
2390+ async fetch (request , env ) {
2391+ // `env` (and therefore the binding) is only available inside the handler.
2392+ const store = new DocumentStore (" https://a.free.example.ravendb.cloud" , " MyDatabase" );
2393+
2394+ // Route every request through the mTLS binding's fetch. Because the binding
2395+ // presents the client certificate at the TLS layer, do NOT also pass authOptions.
2396+ store .conventions .customFetch = env .RAVENDB_CERT .fetch .bind (env .RAVENDB_CERT );
2397+ store .initialize ();
2398+
2399+ const session = store .openSession ();
2400+ const doc = await session .load (" users/1" );
2401+ return Response .json (doc ?? null );
2402+ }
2403+ };
2404+ ` ` `
2405+
2406+ A minimal, runnable load check lives in [` test/ cloudflare- worker` ](./test/cloudflare-worker)
2407+ and runs in CI.
2408+
23242409## Building
23252410
23262411` ` ` bash
0 commit comments