Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ed97c96

Browse files
committed
Bump versions to 2.0.0-rc.1, update CHANGELOG.md and README.md
1 parent c2395cb commit ed97c96

File tree

35 files changed

+562
-331
lines changed

35 files changed

+562
-331
lines changed

CHANGELOG.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
# 🚧 Changelog
22

3+
## 2.0.0-rc.1
4+
5+
### Breaking Changes
6+
7+
- Changed the priority of bindings, so it matches Miniflare 1. The new order
8+
(from lowest to highest priority) is:
9+
1. Variables from `wrangler.toml` `[vars]`
10+
2. Variables from `.env` files
11+
3. WASM module bindings (`--wasm`, `[wasm_modules]`)
12+
4. Custom bindings
13+
- The result of `dispatchScheduled` will no longer include `undefined` if a
14+
module scheduled handler doesn't return a value
15+
16+
### Features
17+
18+
- Added a **custom Jest test environment for Miniflare**. This allows you to
19+
**run unit tests in the Miniflare sandbox**, with **isolated storage** for
20+
each test. Install the `jest-environment-miniflare` to get started and see
21+
[🤹 Jest Environment](https://v2.miniflare.dev/jest.html) for more details.
22+
- Added support for **running multiple workers** in the same Miniflare instance.
23+
See [🔌 Multiple Workers](https://v2.miniflare.dev/mount.html) for more
24+
details.
25+
- Added support for the Durable Object `script_name` option. See
26+
[📌 Durable Objects](https://v2.miniflare.dev/durable-objects.html#using-a-class-exported-by-another-script)
27+
for more details.
28+
- Added support for the new `__STATIC_CONTENT_MANIFEST` text module for using
29+
Workers Sites in modules mode
30+
- Throw an error when a Durable Object `fetch` handler doesn't return a
31+
`Response`
32+
- Added `queueMicrotask` to the sandbox
33+
- Added the `Miniflare#getCaches` method for accessing the global `caches`
34+
outside workers
35+
- Added back the `sourceMap` option to `Miniflare`
36+
- Changed the default location for the `update-check` and `cf.json` files to
37+
inside `node_modules`
38+
- Switched the CRON validation and scheduling package from
39+
[`node-cron`](https://www.npmjs.com/package/node-cron) to
40+
[`cron-schedule`](https://www.npmjs.com/package/cron-schedule). This improves
41+
error messages for invalid CRON expressions, and removes a transitive
42+
dependency on `moment-timezone`, reducing the installation size by a further
43+
5MB.
44+
45+
### Fixes
46+
47+
- Allow any close code when a client closes a WebSocket connection. Closes
48+
[issue #86](https://github.com/cloudflare/miniflare/issues/86), thanks
49+
[@TimTinkers](https://github.com/TimTinkers).
50+
- Wait for worker response before opening WebSocket in client, closes
51+
[issue #88](https://github.com/cloudflare/miniflare/issues/88), thanks
52+
[@TimTinkers](https://github.com/TimTinkers).
53+
- Pass the `--env` flag to `wrangler build` when `--wrangler-env` is set for
54+
`type = "webpack"`/`"rust"` builds
55+
- Set correct `Host` header with `--upstream` flag set
56+
- Fixed memory leak in `HTMLRewriter` when passing non-`ArrayBuffer(View)`
57+
chunks
58+
- Marked `@miniflare/core` and `@miniflare/storage-memory` as `dependencies` of
59+
`@miniflare/durable-objects`
60+
- Removed `ServiceWorkerGlobalScope#dispose()` from global scope
61+
362
## 2.0.0-next.3
463

564
### Fixes
@@ -121,8 +180,7 @@ The docs will be updated over the next few weeks.
121180
much information is logged to the console:
122181

123182
```js
124-
import { Miniflare } from "miniflare";
125-
import { Log, LogLevel } from "@miniflare/shared";
183+
import { Miniflare, Log, LogLevel } from "miniflare";
126184

127185
const mf = new Miniflare({
128186
log: new Log(LogLevel.DEBUG),
@@ -150,6 +208,8 @@ The docs will be updated over the next few weeks.
150208
MODULE2 = "module2.wasm"
151209
```
152210

211+
- Renamed the `buildWatchPath` option to `buildWatchPaths`. This is now an array
212+
of string paths to watch as opposed to a single string.
153213
- Replaced the `Miniflare#reloadOptions()` method with the `Miniflare#reload()`
154214
and `Miniflare#setOptions({ ... })` methods. `reload()` will reload options
155215
from `wrangler.toml` (useful if not watching), and `setOptions()` accepts the

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ goals:
2929
testing.
3030
2.**Lightweight:** Miniflare 1 included
3131
[122 third-party packages](http://npm.anvaka.com/#/view/2d/miniflare) with a
32-
total install size of `88.3MB`. Miniflare 2 reduces this to **24 packages and
33-
`11.5MB`** 🤯. This can probably be reduced further too.
32+
total install size of `88.3MB`. Miniflare 2 reduces this to **23 packages and
33+
`6.2MB`** 🤯.
3434
3.**Correct:** Miniflare 2 more accurately replicates the quirks and thrown
3535
errors of the real Workers runtime, so you'll know before you deploy if
3636
things are going to break.
@@ -114,6 +114,8 @@ Core Options:
114114
-d, --debug Enable debug logging [boolean]
115115
-V, --verbose Enable verbose logging [boolean]
116116
--(no-)update-check Enable update checker (enabled by default) [boolean]
117+
--root Path to resolve default config files relative to [string]
118+
--mount Mount additional named workers [array:NAME=PATH]
117119
118120
HTTP Options:
119121
-H, --host Host for HTTP(S) server to listen on [string]

docs/.vitepress/changelog.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ const changelog = await fs.readFile(changelogPath, "utf8");
1010

1111
const docsChangelogPath = path.resolve(__dirname, "..", "changelog.md");
1212
// Rewrite https://miniflare.dev paths to relative paths
13-
const docsChangelog = changelog.replace(/]\(https:\/\/miniflare.dev/g, "](");
13+
const docsChangelog = changelog.replace(
14+
/]\(https:\/\/(v\d+\.)?miniflare.dev/g,
15+
"]("
16+
);
1417
await fs.writeFile(docsChangelogPath, docsChangelog, "utf8");

docs/.vitepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = {
5050
{ text: "📄 HTMLRewriter", link: "/html-rewriter.html" },
5151
{ text: "⚡️ Live Reload", link: "/live-reload.html" },
5252
{ text: "📅 Compatibility Dates", link: "/compatibility.html" },
53+
{ text: "🔌 Multiple Workers", link: "/mount.html" },
5354
{ text: "🤹 Jest Environment", link: "/jest.html" },
5455
],
5556
},

docs/api.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ const mf = new Miniflare({
5555
wranglerConfigPath: true,
5656
});
5757
```
58-
:::
59-
<!--prettier-ignore-end-->
6058

6159
Note that options specified in the constructor have higher priority than those
6260
in `wrangler.toml`.
61+
:::
62+
<!--prettier-ignore-end-->
6363

6464
### String and File Scripts
6565

@@ -91,7 +91,7 @@ You must also `dispose` if you're persisting KV, cache, or Durable Object data
9191
in Redis to close opened connections.
9292

9393
You can also manually reload scripts (main and Durable Objects') and options
94-
(`.env`, `package.json` and `wrangler.toml`) too with `reload`:
94+
(`.env`, `package.json` and `wrangler.toml`) with `reload`:
9595

9696
```js
9797
const mf = new Miniflare({ ... });
@@ -128,7 +128,8 @@ await mf.setOptions({
128128

129129
You can also access the global scope of the sandbox directly using the
130130
`getGlobalScope` method. Ideally, use should use the `setOptions` method when
131-
updating the environment dynamically though:
131+
updating the environment dynamically, as Miniflare creates a new global scope
132+
each reload, so your changes will be lost:
132133

133134
```js
134135
const mf = new Miniflare({
@@ -302,11 +303,10 @@ await scheduler.dispose();
302303

303304
By default, `[mf:*]` logs as seen in the CLI are disabled when using the API. To
304305
enable these, set the `log` property to an instance of the `Log` class. Its only
305-
parameter is the log level indicating which messages should be logged:
306+
parameter is a log level indicating which messages should be logged:
306307

307308
```js{5}
308-
import { Log, LogLevel } from "@miniflare/shared";
309-
import { Miniflare } from "miniflare";
309+
import { Miniflare, Log, LogLevel } from "miniflare";
310310
311311
const mf = new Miniflare({
312312
scriptPath: "worker.js",

docs/cli.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ load a different `wrangler.toml` file, use the `--wrangler-config`/`-c` flag:
110110
$ miniflare worker.js --wrangler-config wrangler.other.toml
111111
```
112112

113+
To change the directory these default files are resolved relative to, use the
114+
`--root` flag:
115+
116+
```shell
117+
$ miniflare api/worker.js --root api
118+
# Miniflare will look for api/.env, api/package.json and api/wrangler.toml
119+
```
120+
113121
### Script Requirement
114122

115123
The only required option is the script to run. This can either be passed as a
@@ -159,8 +167,8 @@ $ miniflare worker.js --https ./cert_cache # Cache in ./cert_cache instead
159167
```
160168

161169
To use an existing certificate instead, use the `--https-key`, `--https-cert`,
162-
`--https-ca` and `--https-pfx` to set the paths to it. If these are encrypted,
163-
use the `--https-passphrase` flag to set the passphrase:
170+
`--https-ca` and `--https-pfx` flags to set the paths to it. If these are
171+
encrypted, use the `--https-passphrase` flag to set the passphrase:
164172

165173
```shell
166174
$ miniflare worker.js --https-key ./key.pem --https-cert ./cert.pem
@@ -196,6 +204,9 @@ Core Options:
196204
-d, --debug Enable debug logging [boolean]
197205
-V, --verbose Enable verbose logging [boolean]
198206
--(no-)update-check Enable update checker (enabled by default) [boolean]
207+
--root Path to resolve default config files relative [string]
208+
to
209+
--mount Mount additional named workers [array:NAME=PATH]
199210
200211
HTTP Options:
201212
-H, --host Host for HTTP(S) server to listen on [string]
@@ -316,4 +327,6 @@ pfx = "./pfx.pfx" ## --https-pfx
316327
passphrase = "pfx passphrase" ## --https-passphrase
317328
[miniflare.globals] ## --global
318329
KEY = "value"
330+
[miniflare.mounts] ## --mount
331+
api = "./api"
319332
```

docs/durable-objects.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,67 @@ res = await mf.dispatchFetch("http://localhost:8787/");
158158
console.log(await res.text()); // "2"
159159
```
160160

161+
## Using a Class Exported by Another Script
162+
163+
Miniflare supports the `script_name` option for accessing Durable Objects
164+
exported by other scripts. This requires mounting the other worker as described
165+
in [🔌 Multiple Workers](/mount.html). With the following setup:
166+
167+
```js
168+
// api/src/worker.mjs
169+
export class TestObject {
170+
fetch() {
171+
return new Response("API response");
172+
}
173+
}
174+
```
175+
176+
```toml
177+
# api/wrangler.toml
178+
name = "api"
179+
[build.upload]
180+
format = "modules"
181+
dir = "src"
182+
main = "./worker.mjs"
183+
```
184+
185+
```js
186+
// worker.mjs
187+
export default {
188+
fetch(request, env, ctx) {
189+
const { TEST_OBJECT } = env.TEST_OBJECT;
190+
const id = TEST_OBJECT.newUniqueId();
191+
const stub = TEST_OBJECT.get(id);
192+
return stub.fetch(request);
193+
},
194+
};
195+
```
196+
197+
Miniflare can be configured to load `TestObject` from the `api` worker with:
198+
199+
```toml
200+
# wrangler.toml
201+
[durable_objects]
202+
bindings = [
203+
# script_name must be the same as in [miniflare.mounts]
204+
{ name = "TEST_OBJECT", class_name = "TestObject", script_name = "api" },
205+
]
206+
[miniflare.mounts]
207+
api = "./api"
208+
```
209+
210+
```js
211+
const mf = new Miniflare({
212+
durableObjects: {
213+
// scriptName must be the same as in mounts
214+
TEST_OBJECT: { className: "TestObject", scriptName: "api" },
215+
},
216+
mounts: { api: "./api" },
217+
});
218+
```
219+
220+
Note it's not possible to set `script_name` via the CLI.
221+
161222
## Internal Details
162223

163224
Transactional semantics only hold within the same Miniflare instance. Therefore,

docs/jest.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# 🤹 Jest Environment
22

33
Miniflare includes a custom Jest environment that allows you to run your unit
4-
tests within the Miniflare sandbox. Note that Jest &ge; 27 is required.
4+
tests within the Miniflare sandbox. Note that at least Jest 27 is required.
55

66
## Setup
77

88
The Miniflare environment isn't installed by default, install it and Jest with:
99

1010
```shell
11-
$ npm install -D jest-environment-miniflare jest
11+
$ npm install -D jest-environment-miniflare@next jest
1212
```
1313

1414
In the following examples, we'll assume your `package.json` contains
@@ -45,7 +45,7 @@ addEventListener("fetch", (event) => {
4545
});
4646

4747
// Assuming you've got a build tool that removes `export`s when you actually
48-
// deploy your worker
48+
// deploy your worker (e.g. https://esbuild.github.io/api/#format-iife)
4949
export async function handleRequest(request) {
5050
return new Response(`URL: ${request.url} KEY: ${KEY}`);
5151
}

docs/mount.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 🔌 Multiple Workers
2+
3+
## Mounting Workers
4+
5+
Miniflare allows you to run multiple workers in the same instance. Assuming the
6+
following directory structure:
7+
8+
```
9+
├── api
10+
│   ├── api-worker.js // addEventListener("fetch", ...)
11+
│   ├── package.json // { "main": "./api-worker.js" }
12+
│   └── wrangler.toml // name = "api"
13+
├── site
14+
│   ├── package.json // { "module": "./site-worker.mjs" }
15+
│   ├── site-worker.mjs // export default { ... }
16+
│   └── wrangler.toml // name = "site" [build.upload] format = "modules"
17+
├── package.json
18+
├── worker.js
19+
└── wrangler.toml
20+
```
21+
22+
...you can mount the `api` and `site` workers with:
23+
24+
```shell
25+
$ miniflare --mount api=./api --mount site=./site
26+
```
27+
28+
```toml
29+
# wrangler.toml
30+
[miniflare.mounts]
31+
api = "./api"
32+
site = "./site"
33+
```
34+
35+
```js
36+
const mf = new Miniflare({
37+
mounts: {
38+
api: "./api",
39+
site: "./site",
40+
},
41+
});
42+
```
43+
44+
Note the **mounted paths, `./api` and `./site`, are paths to directories not
45+
worker scripts**. All worker configuration must be derivable from
46+
`package.json`, `.env` and `wrangler.toml` files in these directories when
47+
mounting like this. None of the configuration from the parent worker (aside from
48+
the `watch` option) is copied to mounted workers.
49+
50+
When using the API, you can instead configure the mounted workers using the same
51+
options as the `new Miniflare` constructor. Note that nested `mounts` are not
52+
supported: 🙃
53+
54+
```js
55+
const mf = new Miniflare({
56+
mounts: {
57+
api: {
58+
scriptPath: "./api/api-worker.js",
59+
kvNamespaces: ["TEST_NAMESPACE"],
60+
},
61+
},
62+
});
63+
```
64+
65+
## Fetching
66+
67+
When dispatching events (either via `dispatchFetch` or the HTTP(S) server), if
68+
the path starts with the mounted name (e.g. `/api` or `/site/`), this prefix
69+
will be stripped and the event will be dispatched to the mounted worker instead
70+
of the parent:
71+
72+
```js
73+
// api/api-worker.js
74+
addEventListener("fetch", (event) => {
75+
event.respondWith(new Response(`res:${event.request.url}`));
76+
});
77+
```
78+
79+
```shell
80+
$ curl "http://localhost:8787/api/todos/update/1"
81+
res:http://localhost:8787/todos/update/1 # /api removed
82+
```
83+
84+
## Durable Objects
85+
86+
Miniflare supports the `script_name` option for accessing Durable Objects
87+
exported by other scripts. See
88+
[📌 Durable Objects](/durable-objects.html#using-a-class-exported-by-another-script)
89+
for more details.

0 commit comments

Comments
 (0)