v2.0.0-rc.5
Pre-releaseThis update is mostly focused on improving mounts for service bindings. It also matches some other core behaviour of the Workers runtime, like blocking asynchronous I/O outside request handlers, and limiting request recursion depth.
Breaking Changes
NOTE: this change will be reverted in the next release. I've thought of an alternative solution that doesn't need this flag.instanceofchecks on primitives such asObject,Array,Promise, etc. from outside the Miniflare sandbox no longer succeed by default. This was initially added to make it easier to run Rust workers in Miniflare, aswasm-bindgenfrequently generates this code. However, the implementation causedObjectprototype/constructor checks to fail in JavaScript. The previous behaviour can be restored with the--proxy-primitive/miniflare.proxy_primitive_instanceof/proxyPrimitiveInstanceOfCLI/wrangler.toml/API option. This should be enabled when developing Rust workers. See this comment for more details. Closes issues #109, #137, #141 and cloudflare/wrangler2#91. Thanks @EmilianoSanchez, @lukaszczerpak, @SirJosh3917 and @johnhenry.- Mount paths defined in
wrangler.tomlare now resolved relative to the directory containing thewrangler.tomlfile - Mount names must match the
namefield inwrangler.tomlif its defined
Features
-
To match the behaviour of the Workers runtime, some functionality, such as asynchronous I/O (
fetch, Cache API, KV), timeouts (setTimeout,setInterval), and generating cryptographically-secure random values (crypto.getRandomValues,crypto.subtle.generateKey), can now only be performed while handling a request.-
This behaviour can be disabled by setting the
--global-async-io/globalAsyncIO,--global-timers/globalTimersand--global-random/globalRandomoptions respectively, which may be useful for tests or libraries that need async I/O for setup during local development. Note the Miniflare Jest environment automatically enables these options. -
KV namespaces and caches returned from
Miniflare#getKVNamespace()andgetCaches()are unaffected by this change, so they can still be used in tests without setting any additional options.
-
-
Adds highly experimental support for service bindings. This is primarily meant for internal testing, and users outside the beta can't deploy workers using this feature yet, but feel free to play around with them locally and let us know what you think in the Cloudflare Workers Discord server.
To enable these, mount your service (so Miniflare knows where to find it) then add the binding. Note the bound service name must match the mounted name:
$ miniflare --mount auth=./auth --service AUTH_SERVICE=auth # or -S# wrangler.toml experimental_services = [ # Note environment is currently ignored { name = "AUTH_SERVICE", service = "auth", environment = "production" } ] [miniflare.mounts] auth = "./auth"
const mf = new Miniflare({ mounts: { auth: "./auth" }, serviceBindings: { AUTH_SERVICE: "auth" }, });
...then to use the service binding:
export default { async fetch(request, env, ctx) { const res = await env.AUTH_SERVICE.fetch("..."); // ... }, };
If
./auth/wrangler.tomlcontains its own service bindings, those services must also be mounted in the root worker (i.e. inwrangler.tomlnot./auth/wrangler.toml). Nested mounts are not supported. -
To match the behaviour of the Workers runtime, Miniflare now enforces recursion depth limits. Durable Object
fetches can recurse up to 16 times, and service bindings can recurse up to 32 times. This means if a Durable Object fetch triggers another Durable Object fetch, and so on 16 times, an error will be thrown. -
The root worker is now routable using
route/routesas with mounts if it has anameset. This means if it has more specific routes than other mounts, they'll be used instead. The root worker is always used as a fallback if no mounts' routes match. -
Mounted workers can now access Durable Objects defined in other mounts or the root worker (assuming a
nameis set) using thescript_nameoption. -
Allow the subrequest limit to be customised using the
MINIFLARE_SUBREQUEST_LIMITenvironment variable. Setting this to a negative number disables the limit. Setting this to 0 disables subrequests. The majority of users should never need to touch this, hence it's configured via an environment variable, not a CLI option. This also makes the implementation much simpler. :slight_smile: Closes issue #132, thanks @DanielAGW.
Fixes
- Allow
env_pathto be set inwrangler.toml - WebSocket client
fetches now contribute to the subrequest limit - Errors raised when reloading (e.g. missing Durable Object classes) in response to mount file changes are now logged, and don't crash
- Fixed issue where deleting
[miniflare.mounts]inwrangler.tomlwouldn't unmount everything