Skip to content

Commit d91929a

Browse files
add Hybrid development API section
1 parent 3f9ab86 commit d91929a

File tree

1 file changed

+108
-1
lines changed
  • src/content/docs/workers/development-testing

1 file changed

+108
-1
lines changed

src/content/docs/workers/development-testing/index.mdx

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
InlineBadge,
1818
CardGrid,
1919
Card,
20+
Type,
21+
TypeScriptExample,
2022
} from "~/components";
2123

2224
Cloudflare Workers offers three distinct development modes to build, run, and test your Worker code before deploying to production. Choose from [fully **local development**](/workers/development-testing/#local-development) with simulated resources, [**hybrid development**](/workers/development-testing/#hybrid-development) that combines local execution with remote resource connections, or [legacy **remote development**](/workers/development-testing/#remote-development-legacy) that runs entirely on Cloudflare's infrastructure.
@@ -320,7 +322,112 @@ If you have use-cases for connecting to any of the remote resources above, pleas
320322

321323
### API
322324

323-
TO-DO
325+
Wrangler provides programmatic utilities to help tooling authors support hybrid development sessions when running Workers code with [Miniflare](/workers/testing/miniflare/).
326+
327+
Key APIs include:
328+
329+
- [`experimental_startMixedModeSession`](#experimental_startmixedmodesession) - starts a hybrid development session that allows interaction with remote bindings
330+
- [`unstable_convertConfigBindingsToStartWorkerBindings`](#unstable_convertconfigbindingstostartworkerbindings) - utility for converting binding definitions
331+
- [`experimental_maybeStartOrUpdateMixedModeSession`](#experimental_maybestartorupdatemixedmodesession) - convenience function to easily start or update a hybrid session
332+
333+
#### `experimental_startMixedModeSession`
334+
335+
This function starts a hybrid development session for a given set of bindings. It accepts options to control session behavior, including an `auth` option with your Cloudflare account ID and API token for remote binding access.
336+
337+
It returns an object with:
338+
339+
- `ready` <Type text="Promise<void>" />: Resolves when the session is ready.
340+
- `dispose` <Type text="() => Promise<void>" />: Stops the session.
341+
- `updateBindings` <Type text="(bindings: StartDevWorkerInput['bindings']) => Promise<void>" />: Updates session bindings.
342+
- `mixedModeConnectionString` <Type text="MixedModeConnectionString" />: String to pass to Miniflare for remote binding access.
343+
344+
#### `unstable_convertConfigBindingsToStartWorkerBindings`
345+
346+
The `unstable_readConfig` utility returns an `Unstable_Config` object which includes the definition of the bindings included in the configuration file. These bindings definitions
347+
are however not directly compatible with `experimental_startMixedModeSession`. It can be quite convenient to however read the binding declarations with `unstable_readConfig` and then
348+
pass them to `experimental_startMixedModeSession`, so for this wrangler exposes `unstable_convertConfigBindingsToStartWorkerBindings` which is a simple utility to convert
349+
the bindings in an `Unstable_Config` object into a structure that can be passed to `experimental_startMixedModeSession`.
350+
351+
<Aside type="note">
352+
This type conversion is temporary. In the future, the types will be unified so
353+
you can pass the config object directly to
354+
`experimental_startMixedModeSession`.
355+
</Aside>
356+
357+
#### `experimental_maybeStartOrUpdateMixedModeSession`
358+
359+
This wrapper simplifies hybrid session management. It takes:
360+
361+
- The path to your Wrangler config, or an object with remote bindings.
362+
- The current hybrid session details (or `null` if none).
363+
364+
It returns:
365+
366+
- `null` if no hybrid session is needed.
367+
- An object with the hybrid session details if started or updated.
368+
369+
The function:
370+
371+
- based on the first argument prepares the input arguments for the hybrid session
372+
- if there are no remote bindings to be used (nor a pre-existing hybrid session) it returns null, signaling that no hybrid session is needed
373+
- if the details of an existing hybrid session have been provided it updates the hybrid session accordingly
374+
- otherwise if starts a new hybrid session
375+
- returns the hybrid session details (that can later be passed as the second argument to `experimental_maybeStartOrUpdateMixedModeSession`)
376+
377+
#### Example
378+
379+
Here's a basic example of using Miniflare with `experimental_maybeStartOrUpdateMixedModeSession` to provide a hybrid dev session. This example uses a single hardcoded KV binding.
380+
381+
<TypeScriptExample>
382+
```ts
383+
import { Miniflare, MiniflareOptions } from "miniflare";
384+
import { experimental_maybeStartOrUpdateMixedModeSession } from "wrangler";
385+
386+
let mf: Miniflare | null;
387+
388+
let mixedModeSessionDetails: Awaited<
389+
ReturnType<typeof experimental_maybeStartOrUpdateMixedModeSession>
390+
> | null = null;
391+
392+
async function startOrUpdateDevSession() {
393+
mixedModeSessionDetails =
394+
await experimental_maybeStartOrUpdateMixedModeSession(
395+
{
396+
bindings: {
397+
MY_KV: {
398+
type: 'kv_namespace',
399+
id: 'kv-id',
400+
remote: true,
401+
}
402+
}
403+
},
404+
mixedModeSessionDetails
405+
);
406+
407+
const miniflareOptions: MiniflareOptions = {
408+
scriptPath: "./worker.js",
409+
kvNamespaces: {
410+
MY_KV: {
411+
id: "kv-id",
412+
mixedModeConnectionString:
413+
mixedModeSessionDetails?.session.mixedModeConnectionString,
414+
},
415+
},
416+
};
417+
418+
if (!mf) {
419+
mf = new Miniflare(miniflareOptions);
420+
} else {
421+
mf.setOptions(miniflareOptions);
422+
}
423+
}
424+
425+
// ... tool logic that invokes `startOrUpdateDevSession()` ...
426+
427+
// ... once the dev session is no longer needed run
428+
// `mixedModeSessionDetails?.session.dispose()`
429+
```
430+
</TypeScriptExample>
324431

325432
## Remote development (Legacy)
326433

0 commit comments

Comments
 (0)