You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/dynamic-workers/api-reference.mdx
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,65 +10,65 @@ products:
10
10
11
11
import { Type, MetaInfo } from"~/components";
12
12
13
-
This page describes the Worker Loader binding API, assuming you have [configured such a binding](/dynamic-workers/getting-started/#configure-worker-loader) as `env.LOADER`.
13
+
This page describes the Worker Loader binding API, assuming you have [configured a binding](/dynamic-workers/getting-started/#configure-worker-loader) as `env.LOADER`.
Loads a Worker with the given ID, returning a `WorkerStub` which may be used to invoke the Worker.
30
30
31
-
As a convenience, the loader implements caching of isolates. When a new ID is seen the first time, a new isolate is loaded. But, the isolate may be kept warm in memory for a while. If later invocations of the loader request the same ID, the existing isolate may be returned again, rather than create a new one. But there is no guarantee: a later call with the same ID may instead start a new isolate from scratch.
31
+
The loader caches isolates by ID. When the loader sees an ID for the first time, it creates a new isolate. The isolate may stay warm in memory for subsequent requests. If a later call uses the same ID, the runtime may return the existing isolate instead of creating a new one. However, there is no guarantee—a later call with the same ID may start a new isolate from scratch.
32
32
33
-
Whenever the system determines it needs to start a new isolate, and it does not already have a copy of the code cached, it will invoke `codeCallback` to get the Worker's code. This is an async callback, so the application can load the code from remote storage if desired. The callback returns a `WorkerCode` object (described below).
33
+
When the runtime needs to start a new isolate and does not have a cached copy of the code, it invokes `codeCallback` to get the Worker code. This is an async callback, so the application can load the code from remote storage if needed. The callback returns a `WorkerCode` object (described below).
34
34
35
-
Because of the caching, you should ensure that the callback always returns exactly the same content, when called for the same ID. If anything about the content changes, you must use a new ID. But if the content hasn't changed, it's best to reuse the same ID in order to take advantage of caching. If the `WorkerCode` is different every time, you can pass a random ID.
35
+
Because of caching, ensure that the callback always returns exactly the same contentfor the same ID. If anything about the content changes, you must use a new ID. If the content has not changed, reuse the same ID to take advantage of caching. If the `WorkerCode` is different every time, pass a random ID.
36
36
37
-
You could, for example, use IDs of the form `<worker-name>:<version-number>`, where the version number increments every time the code changes. Or, you could compute IDs based on a hash of the code and config, so that any change results in a new ID.
37
+
For example, use IDs of the form `<worker-name>:<version-number>`, where the version number increments every time the code changes. Alternatively, compute IDs based on a hash of the code and config so that any change produces a new ID.
38
38
39
-
`get()` returns a `WorkerStub`, which can be used to send requests to the loaded Worker. Note that the stub is returned synchronously—you do not have to await it. If the Worker is not loaded yet, requests made to the stub will wait for the Worker to load before being delivered. If loading fails, the request will throw an exception.
39
+
`get()` returns a `WorkerStub`, which you can use to send requests to the loaded Worker. The stub is returned synchronously—you do not have to await it. If the Worker is not loaded yet, requests made to the stub wait for the Worker to load before being delivered. If loading fails, the request throws an exception.
40
40
41
-
It is never guaranteed that two requests will go to the same isolate. Even if you use the same `WorkerStub` to make multiple requests, they could execute in different isolates. The callback passed to `loader.get()` could be called any number of times (although it is unusual for it to be called more than once).
41
+
Two requests are never guaranteed to reach the same isolate. Even if you use the same `WorkerStub` to make multiple requests, they could execute in different isolates. The callback passed to `loader.get()` could run any number of times, although it is unusual for it to run more than once.
42
42
43
-
###`WorkerCode`
43
+
## `WorkerCode`
44
44
45
45
This is the structure returned by `getCodeCallback` to represent a worker.
The [compatibility date](/workers/configuration/compatibility-dates/) for the Worker. This has the same meaning as the `compatibility_date` setting in a Wrangler config file.
An optional list of [compatibility flags](/workers/configuration/compatibility-flags) augmenting the compatibility date. This has the same meaning as the `compatibility_flags` setting in a Wrangler config file.
If true, then experimental compatibility flags will be permitted in `compatibilityFlags`. In order to set this, the worker calling the loader must itself have the compatibility flag `"experimental"` set. Experimental flags cannot be enabled in production.
57
+
If true, experimental compatibility flags are permitted in `compatibilityFlags`. To set this, the Worker calling the loader must itself have the `"experimental"`compatibility flag set. Experimental flags cannot be used in production.
58
58
59
-
####<code>mainModule <Typetext="string" /></code>
59
+
### <code>mainModule <Typetext="string" /></code>
60
60
61
61
The name of the Worker's main module. This must be one of the modules listed in `modules`.
A dictionary object mapping module names to their string contents. If the module content is a plain string, then the module name must have a file extension indicating its type: either `.js` or `.py`.
65
+
A dictionary object mapping module names to their string contents. If the module content is a plain string, the module name must have a file extension indicating its type: either `.js` or `.py`.
66
66
67
-
A module's content can also be specified as an object, in order to specify its type independent from the name. The allowed objects are:
67
+
You can also specify a module's content as an objectto set its type independent of the name. The allowed objects are:
68
68
69
69
-`{js: string}`: A JavaScript module, using ES modules syntax for imports and exports.
70
70
-`{cjs: string}`: A CommonJS module, using `require()` syntax for imports.
71
-
-`{py: string}`: A [Python module](/workers/languages/python/). See warning below.
71
+
-`{py: string}`: A [Python module](/workers/languages/python/). Refer to the warning below.
72
72
-`{text: string}`: An importable string value.
73
73
-`{data: ArrayBuffer}`: An importable `ArrayBuffer` value.
74
74
-`{json: object}`: An importable object. The value must be JSON-serializable. However, note that value is provided as a parsed object, and is delivered as a parsed object; neither side actually sees the JSON serialization.
@@ -79,17 +79,17 @@ While Dynamic Workers support Python, Python Workers are currently much slower t
Controls whether the dynamic Worker has access to the network. The global `fetch()` and `connect()` functions (for making HTTP requests and TCP connections, respectively) can be blocked or redirected to isolate the Worker.
85
85
86
-
If `globalOutbound` is not specified, the default is to inherit the parent's network access, which usually means the dynamic Worker will have full access to the public Internet.
86
+
If `globalOutbound` is not specified, the default is to inherit the parent's network access, which usually means the dynamic Worker has full access to the public internet.
87
87
88
-
If `globalOutbound` is `null`, then the dynamic Worker will be totally cut off from the network. Both `fetch()` and `connect()` will throw exceptions.
88
+
If `globalOutbound` is `null`, the dynamic Worker is fully cut off from the network. Both `fetch()` and `connect()` throw exceptions.
89
89
90
-
`globalOutbound` can also be set to any service binding, including service bindings in the parent worker's `env`as well as[loopback bindings from `ctx.exports`](/workers/runtime-apis/context/#exports).
90
+
You can also set `globalOutbound`to any service binding, including service bindings in the parent Worker's `env`and[loopback bindings from `ctx.exports`](/workers/runtime-apis/context/#exports).
91
91
92
-
Using `ctx.exports` is particularly useful as it allows you to customize the binding further for the specific sandbox, by setting the value of `ctx.props` that should be passed back to it. The `props` can contain information to identify the specific dynamic Worker that made the request.
92
+
Using `ctx.exports` is particularly useful because it lets you customize the binding for a specific sandbox by setting the value of `ctx.props` that is passed back to it. The `props` can contain information to identify the specific dynamic Worker that made the request.
93
93
94
94
For example:
95
95
@@ -120,13 +120,13 @@ export default {
120
120
};
121
121
```
122
122
123
-
####<code>env <Typetext="object" /></code>
123
+
### <code>env <Typetext="object" /></code>
124
124
125
125
The environment object to provide to the dynamic Worker.
126
126
127
127
Using this, you can provide custom bindings to the Worker.
128
128
129
-
`env` is serialized and transferred into the dynamic Worker, where it is used directly as the value of `env` there. It may contain:
129
+
`env` is serialized and transferred into the dynamic Worker, where it becomes the value of `env`. It may contain:
-[Service Bindings](/workers/runtime-apis/bindings/service-bindings), including [loopback bindings from `ctx.exports`](/workers/runtime-apis/context/#exports).
You may specify one or more [Tail Workers](/workers/observability/logs/tail-workers/)which will observe console logs, errors, and other details about the dynamically-loaded worker's execution. A tail event will be delivered to the Tail Worker upon completion of a request to the dynamically-loaded Worker. As always, you can implement the Tail Worker as an alternative entrypoint in your parent Worker, referring to it using`ctx.exports`:
169
+
Specify one or more [Tail Workers](/workers/observability/logs/tail-workers/)to observe console logs, errors, and other details about the dynamically-loaded Worker's execution. A tail event is delivered to the Tail Worker when a request to the dynamically-loaded Worker completes. You can implement the Tail Worker as an alternative entrypoint in your parent Worker, referencing it with`ctx.exports`:
Copy file name to clipboardExpand all lines: src/content/docs/dynamic-workers/getting-started.mdx
+11-16Lines changed: 11 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,36 +19,31 @@ Dynamic Workers support two loading modes:
19
19
20
20
`load()` is best for one-time code execution, for example when using [Codemode](/agents/api-reference/codemode/). `get(id, callback)` is better when the same code will receive subsequent requests, for example when you are building applications.
21
21
22
-
###Try it out
22
+
## Try it out
23
23
24
-
####Dynamic Workers Starter
24
+
### Dynamic Workers Starter
25
25
26
26
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers)
27
27
28
28
Use this "hello world" [starter](https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers) to get a Worker deployed that can load and execute Dynamic Workers.
29
29
30
-
####Dynamic Workers Playground
30
+
### Dynamic Workers Playground
31
31
32
32
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers-playground)
33
33
34
34
You can also deploy the [Dynamic Workers Playground](https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers-playground), where you can write or import code, bundle it at runtime with `@cloudflare/worker-bundler`, execute it through a Dynamic Worker, and see real-time responses and execution logs.
35
35
36
36
## Configure Worker Loader
37
37
38
-
In order for a Worker to be able to create Dynamic Workers, it needs a Worker Loader binding. Unlike most Workers bindings, this binding doesn't point at any external resource in particular; it simply provides access to the Worker Loader API.
38
+
For a Worker to create Dynamic Workers, it needs a Worker Loader binding. Unlike most Workers bindings, this binding does not point at any external resource. It provides access to the Worker Loader API.
39
39
40
-
Configure it like so, in your Worker's `wrangler.jsonc`:
40
+
Add the following to your Worker's Wrangler configuration:
41
41
42
42
<WranglerConfig>
43
43
44
-
```jsonc
45
-
{
46
-
"worker_loaders": [
47
-
{
48
-
"binding":"LOADER",
49
-
},
50
-
],
51
-
}
44
+
```toml
45
+
[[worker_loaders]]
46
+
binding = "LOADER"
52
47
```
53
48
54
49
</WranglerConfig>
@@ -101,9 +96,9 @@ In this example, `env.LOADER.load()` creates a Dynamic Worker from the code defi
101
96
102
97
### Reusing a Dynamic Worker across requests
103
98
104
-
If you expect to load the exact same Worker more than once, use [`get(id, callback)`](/dynamic-workers/api-reference/#get) instead of `load()`. The `id` should be a unique string identifying the particular code you intend to load. When the runtime sees the same `id` again, it can reuse the existing Worker instead of creating a new one, if it hasn't been evicted yet.
99
+
If you expect to load the exact same Worker more than once, use [`get(id, callback)`](/dynamic-workers/api-reference/#get) instead of `load()`. The `id` should be a unique string identifying the particular code you intend to load. When the runtime sees the same `id` again, it can reuse the existing Worker instead of creating a new one, if it has not been evicted yet.
105
100
106
-
The callback you provide will only be called if the Worker is not already loaded. This lets you skip loading the code from storage when the Worker is already running.
101
+
The callback you provide only runs if the Worker is not already loaded. This lets you skip loading the code from storage when the Worker is already running.
Copy file name to clipboardExpand all lines: src/content/docs/dynamic-workers/index.mdx
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,19 +16,19 @@ Spin up Workers at runtime to execute code on-demand in a secure, sandboxed envi
16
16
17
17
</Description>
18
18
19
-
Dynamic Workers let you spin up an unlimited number of Workers to execute arbitrary code specified at runtime. Dynamic Workers can be used as a lightweight alternative to containers for securely sandboxing code you don't trust.
19
+
Dynamic Workers let you spin up an unlimited number of Workers to execute arbitrary code specified at runtime. Use Dynamic Workers as a lightweight alternative to containers for securely sandboxing code you do not trust.
20
20
21
21
Dynamic Workers are the lowest-level primitive for spinning up a Worker, giving you full control over defining how the Worker is composed, which bindings it receives, whether it can reach the network, and more.
22
22
23
-
###Get started
23
+
## Get started
24
24
25
25
Deploy the [Dynamic Workers Playground](https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers-playground) to create and run Workers dynamically from code you write or import from GitHub, with real-time logs and observability.
26
26
27
27
[](https://deploy.workers.cloudflare.com/?url=https://github.com/dinasaur404/dynamic-workers-playground)
28
28
29
-
## Use Dynamic Workers for
29
+
## Use cases
30
30
31
-
Use this pattern when code needs to run quickly in a secure, isolated environment.
31
+
Use Dynamic Workers when code needs to run quickly in a secure, isolated environment.
32
32
33
33
-**AI Agent "Code Mode"**: LLMs are trained to write code. Instead of supplying an agent with tool calls to perform tasks, give it an API and let it write and execute code. Save up to 80% in inference tokens and cost by allowing the agent to programmatically process data instead of sending it all through the LLM.
34
34
-**AI-generated applications / "Vibe Code"**: Run generated code for prototypes, projects, and automations in a secure, isolated sandboxed environment.
0 commit comments