Minor Changes
-
#4795
027f9719
Thanks @mrbbot! - feat: passMiniflare
instance as argument to custom service binding handlersThis change adds a new
Miniflare
-typed parameter to function-valued service binding handlers. This provides easy access to the correct bindings when re-using service functions across instances.import assert from "node:assert"; import { Miniflare, Response } from "miniflare"; const mf = new Miniflare({ serviceBindings: { SERVICE(request, instance) { assert(instance === mf); return new Response(); }, }, });
-
#4795
027f9719
Thanks @mrbbot! - feat: allowURL
s to be passed inhyperdrives
Previously, the
hyperdrives
option only acceptedstring
s as connection strings. This change allowsURL
objects to be passed too. -
#4795
027f9719
Thanks @mrbbot! - feat: add support for custom root pathsMiniflare has lots of file-path-valued options (e.g.
scriptPath
,kvPersist
,textBlobBindings
). Previously, these were always resolved relative to the current working directory before being used. This change adds a newrootPath
shared, and per-worker option for customising this behaviour. Instead of resolving relative to the current working directory, Miniflare will now resolve path-valued options relative to the closestrootPath
option. Paths are still resolved relative to the current working directory if norootPath
s are defined. Worker-levelrootPath
s are themselves resolved relative to the sharedrootPath
if defined.import { Miniflare } from "miniflare"; const mf1 = new Miniflare({ scriptPath: "index.mjs", }); const mf2 = new Miniflare({ rootPath: "a/b", scriptPath: "c/index.mjs", }); const mf3 = new Miniflare({ rootPath: "/a/b", workers: [ { name: "1", rootPath: "c", scriptPath: "index.mjs", }, { name: "2", scriptPath: "index.mjs", }, ], });
-
#4795
027f9719
Thanks @mrbbot! - feat: allow easy binding to current workerPreviously, if you wanted to create a service binding to the current Worker, you'd need to know the Worker's name. This is usually possible, but can get tricky when dealing with many Workers. This change adds a new
kCurrentWorker
symbol that can be used instead of a Worker name inserviceBindings
.kCurrentWorker
always points to the Worker with the binding.import { kCurrentWorker, Miniflare } from "miniflare"; const mf = new Miniflare({ serviceBindings: { SELF: kCurrentWorker, }, modules: true, script: `export default { fetch(request, env, ctx) { const { pathname } = new URL(request.url); if (pathname === "/recurse") { return env.SELF.fetch("http://placeholder"); } return new Response("body"); } }`, }); const response = await mf.dispatchFetch("http://placeholder/recurse"); console.log(await response.text()); // body
Patch Changes
-
#4954
7723ac17
Thanks @mrbbot! - fix: allow relativescriptPath
/modulesRoot
s to break out of current working directoryPreviously, Miniflare would resolve relative
scriptPath
s againstmoduleRoot
multiple times resulting in incorrect paths and module names. This would lead tocan't use ".." to break out of starting directory
workerd
errors. This change ensures Miniflare usesscriptPath
as is, and only resolves it relative tomodulesRoot
when computing module names. Note this bug didn't affect service workers. This allows you to reference a modulesscriptPath
outside the working directory with something like:const mf = new Miniflare({ modules: true, modulesRoot: "..", scriptPath: "../worker.mjs", });
Fixes #4721
-
#4795
027f9719
Thanks @mrbbot! - fix: return non-WebSocket responses for failed WebSocket upgradingfetch()
esPreviously, Miniflare's
fetch()
would throw an error if theUpgrade: websocket
header was set, and a non-WebSocket response was returned from the origin. This change ensures the non-WebSocket response is returned fromfetch()
instead, withwebSocket
set tonull
. This allows the caller to handle the response as they see fit. -
#4795
027f9719
Thanks @mrbbot! - fix: ensureMiniflareOptions
,WorkerOptions
, andSharedOptions
types are correctMiniflare uses Zod for validating options. Previously, Miniflare inferred
*Options
from the output types of its Zod schemas, rather than the input types. In most cases, these were the same. However, thehyperdrives
option has different input/output types, preventing these from being type checked correctly.