Skip to content

Commit

Permalink
Bump versions to 2.0.0-rc.1, update CHANGELOG.md and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Nov 13, 2021
1 parent c2395cb commit ed97c96
Show file tree
Hide file tree
Showing 35 changed files with 562 additions and 331 deletions.
64 changes: 62 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
# 🚧 Changelog

## 2.0.0-rc.1

### Breaking Changes

- Changed the priority of bindings, so it matches Miniflare 1. The new order
(from lowest to highest priority) is:
1. Variables from `wrangler.toml` `[vars]`
2. Variables from `.env` files
3. WASM module bindings (`--wasm`, `[wasm_modules]`)
4. Custom bindings
- The result of `dispatchScheduled` will no longer include `undefined` if a
module scheduled handler doesn't return a value

### Features

- Added a **custom Jest test environment for Miniflare**. This allows you to
**run unit tests in the Miniflare sandbox**, with **isolated storage** for
each test. Install the `jest-environment-miniflare` to get started and see
[🤹 Jest Environment](https://v2.miniflare.dev/jest.html) for more details.
- Added support for **running multiple workers** in the same Miniflare instance.
See [🔌 Multiple Workers](https://v2.miniflare.dev/mount.html) for more
details.
- Added support for the Durable Object `script_name` option. See
[📌 Durable Objects](https://v2.miniflare.dev/durable-objects.html#using-a-class-exported-by-another-script)
for more details.
- Added support for the new `__STATIC_CONTENT_MANIFEST` text module for using
Workers Sites in modules mode
- Throw an error when a Durable Object `fetch` handler doesn't return a
`Response`
- Added `queueMicrotask` to the sandbox
- Added the `Miniflare#getCaches` method for accessing the global `caches`
outside workers
- Added back the `sourceMap` option to `Miniflare`
- Changed the default location for the `update-check` and `cf.json` files to
inside `node_modules`
- Switched the CRON validation and scheduling package from
[`node-cron`](https://www.npmjs.com/package/node-cron) to
[`cron-schedule`](https://www.npmjs.com/package/cron-schedule). This improves
error messages for invalid CRON expressions, and removes a transitive
dependency on `moment-timezone`, reducing the installation size by a further
5MB.

### Fixes

- Allow any close code when a client closes a WebSocket connection. Closes
[issue #86](https://github.com/cloudflare/miniflare/issues/86), thanks
[@TimTinkers](https://github.com/TimTinkers).
- Wait for worker response before opening WebSocket in client, closes
[issue #88](https://github.com/cloudflare/miniflare/issues/88), thanks
[@TimTinkers](https://github.com/TimTinkers).
- Pass the `--env` flag to `wrangler build` when `--wrangler-env` is set for
`type = "webpack"`/`"rust"` builds
- Set correct `Host` header with `--upstream` flag set
- Fixed memory leak in `HTMLRewriter` when passing non-`ArrayBuffer(View)`
chunks
- Marked `@miniflare/core` and `@miniflare/storage-memory` as `dependencies` of
`@miniflare/durable-objects`
- Removed `ServiceWorkerGlobalScope#dispose()` from global scope

## 2.0.0-next.3

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

```js
import { Miniflare } from "miniflare";
import { Log, LogLevel } from "@miniflare/shared";
import { Miniflare, Log, LogLevel } from "miniflare";
const mf = new Miniflare({
log: new Log(LogLevel.DEBUG),
Expand Down Expand Up @@ -150,6 +208,8 @@ The docs will be updated over the next few weeks.
MODULE2 = "module2.wasm"
```

- Renamed the `buildWatchPath` option to `buildWatchPaths`. This is now an array
of string paths to watch as opposed to a single string.
- Replaced the `Miniflare#reloadOptions()` method with the `Miniflare#reload()`
and `Miniflare#setOptions({ ... })` methods. `reload()` will reload options
from `wrangler.toml` (useful if not watching), and `setOptions()` accepts the
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ goals:
testing.
2.**Lightweight:** Miniflare 1 included
[122 third-party packages](http://npm.anvaka.com/#/view/2d/miniflare) with a
total install size of `88.3MB`. Miniflare 2 reduces this to **24 packages and
`11.5MB`** 🤯. This can probably be reduced further too.
total install size of `88.3MB`. Miniflare 2 reduces this to **23 packages and
`6.2MB`** 🤯.
3.**Correct:** Miniflare 2 more accurately replicates the quirks and thrown
errors of the real Workers runtime, so you'll know before you deploy if
things are going to break.
Expand Down Expand Up @@ -114,6 +114,8 @@ Core Options:
-d, --debug Enable debug logging [boolean]
-V, --verbose Enable verbose logging [boolean]
--(no-)update-check Enable update checker (enabled by default) [boolean]
--root Path to resolve default config files relative to [string]
--mount Mount additional named workers [array:NAME=PATH]
HTTP Options:
-H, --host Host for HTTP(S) server to listen on [string]
Expand Down
5 changes: 4 additions & 1 deletion docs/.vitepress/changelog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ const changelog = await fs.readFile(changelogPath, "utf8");

const docsChangelogPath = path.resolve(__dirname, "..", "changelog.md");
// Rewrite https://miniflare.dev paths to relative paths
const docsChangelog = changelog.replace(/]\(https:\/\/miniflare.dev/g, "](");
const docsChangelog = changelog.replace(
/]\(https:\/\/(v\d+\.)?miniflare.dev/g,
"]("
);
await fs.writeFile(docsChangelogPath, docsChangelog, "utf8");
1 change: 1 addition & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
{ text: "📄 HTMLRewriter", link: "/html-rewriter.html" },
{ text: "⚡️ Live Reload", link: "/live-reload.html" },
{ text: "📅 Compatibility Dates", link: "/compatibility.html" },
{ text: "🔌 Multiple Workers", link: "/mount.html" },
{ text: "🤹 Jest Environment", link: "/jest.html" },
],
},
Expand Down
14 changes: 7 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const mf = new Miniflare({
wranglerConfigPath: true,
});
```
:::
<!--prettier-ignore-end-->

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

### String and File Scripts

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

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

```js
const mf = new Miniflare({ ... });
Expand Down Expand Up @@ -128,7 +128,8 @@ await mf.setOptions({

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

```js
const mf = new Miniflare({
Expand Down Expand Up @@ -302,11 +303,10 @@ await scheduler.dispose();

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

```js{5}
import { Log, LogLevel } from "@miniflare/shared";
import { Miniflare } from "miniflare";
import { Miniflare, Log, LogLevel } from "miniflare";
const mf = new Miniflare({
scriptPath: "worker.js",
Expand Down
17 changes: 15 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ load a different `wrangler.toml` file, use the `--wrangler-config`/`-c` flag:
$ miniflare worker.js --wrangler-config wrangler.other.toml
```

To change the directory these default files are resolved relative to, use the
`--root` flag:

```shell
$ miniflare api/worker.js --root api
# Miniflare will look for api/.env, api/package.json and api/wrangler.toml
```

### Script Requirement

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

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

```shell
$ miniflare worker.js --https-key ./key.pem --https-cert ./cert.pem
Expand Down Expand Up @@ -196,6 +204,9 @@ Core Options:
-d, --debug Enable debug logging [boolean]
-V, --verbose Enable verbose logging [boolean]
--(no-)update-check Enable update checker (enabled by default) [boolean]
--root Path to resolve default config files relative [string]
to
--mount Mount additional named workers [array:NAME=PATH]
HTTP Options:
-H, --host Host for HTTP(S) server to listen on [string]
Expand Down Expand Up @@ -316,4 +327,6 @@ pfx = "./pfx.pfx" ## --https-pfx
passphrase = "pfx passphrase" ## --https-passphrase
[miniflare.globals] ## --global
KEY = "value"
[miniflare.mounts] ## --mount
api = "./api"
```
61 changes: 61 additions & 0 deletions docs/durable-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,67 @@ res = await mf.dispatchFetch("http://localhost:8787/");
console.log(await res.text()); // "2"
```

## Using a Class Exported by Another Script

Miniflare supports the `script_name` option for accessing Durable Objects
exported by other scripts. This requires mounting the other worker as described
in [🔌 Multiple Workers](/mount.html). With the following setup:

```js
// api/src/worker.mjs
export class TestObject {
fetch() {
return new Response("API response");
}
}
```

```toml
# api/wrangler.toml
name = "api"
[build.upload]
format = "modules"
dir = "src"
main = "./worker.mjs"
```

```js
// worker.mjs
export default {
fetch(request, env, ctx) {
const { TEST_OBJECT } = env.TEST_OBJECT;
const id = TEST_OBJECT.newUniqueId();
const stub = TEST_OBJECT.get(id);
return stub.fetch(request);
},
};
```

Miniflare can be configured to load `TestObject` from the `api` worker with:

```toml
# wrangler.toml
[durable_objects]
bindings = [
# script_name must be the same as in [miniflare.mounts]
{ name = "TEST_OBJECT", class_name = "TestObject", script_name = "api" },
]
[miniflare.mounts]
api = "./api"
```

```js
const mf = new Miniflare({
durableObjects: {
// scriptName must be the same as in mounts
TEST_OBJECT: { className: "TestObject", scriptName: "api" },
},
mounts: { api: "./api" },
});
```

Note it's not possible to set `script_name` via the CLI.

## Internal Details

Transactional semantics only hold within the same Miniflare instance. Therefore,
Expand Down
6 changes: 3 additions & 3 deletions docs/jest.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 🤹 Jest Environment

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

## Setup

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

```shell
$ npm install -D jest-environment-miniflare jest
$ npm install -D jest-environment-miniflare@next jest
```

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

// Assuming you've got a build tool that removes `export`s when you actually
// deploy your worker
// deploy your worker (e.g. https://esbuild.github.io/api/#format-iife)
export async function handleRequest(request) {
return new Response(`URL: ${request.url} KEY: ${KEY}`);
}
Expand Down
89 changes: 89 additions & 0 deletions docs/mount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# 🔌 Multiple Workers

## Mounting Workers

Miniflare allows you to run multiple workers in the same instance. Assuming the
following directory structure:

```
├── api
│   ├── api-worker.js // addEventListener("fetch", ...)
│   ├── package.json // { "main": "./api-worker.js" }
│   └── wrangler.toml // name = "api"
├── site
│   ├── package.json // { "module": "./site-worker.mjs" }
│   ├── site-worker.mjs // export default { ... }
│   └── wrangler.toml // name = "site" [build.upload] format = "modules"
├── package.json
├── worker.js
└── wrangler.toml
```

...you can mount the `api` and `site` workers with:

```shell
$ miniflare --mount api=./api --mount site=./site
```

```toml
# wrangler.toml
[miniflare.mounts]
api = "./api"
site = "./site"
```

```js
const mf = new Miniflare({
mounts: {
api: "./api",
site: "./site",
},
});
```

Note the **mounted paths, `./api` and `./site`, are paths to directories not
worker scripts**. All worker configuration must be derivable from
`package.json`, `.env` and `wrangler.toml` files in these directories when
mounting like this. None of the configuration from the parent worker (aside from
the `watch` option) is copied to mounted workers.

When using the API, you can instead configure the mounted workers using the same
options as the `new Miniflare` constructor. Note that nested `mounts` are not
supported: 🙃

```js
const mf = new Miniflare({
mounts: {
api: {
scriptPath: "./api/api-worker.js",
kvNamespaces: ["TEST_NAMESPACE"],
},
},
});
```

## Fetching

When dispatching events (either via `dispatchFetch` or the HTTP(S) server), if
the path starts with the mounted name (e.g. `/api` or `/site/`), this prefix
will be stripped and the event will be dispatched to the mounted worker instead
of the parent:

```js
// api/api-worker.js
addEventListener("fetch", (event) => {
event.respondWith(new Response(`res:${event.request.url}`));
});
```

```shell
$ curl "http://localhost:8787/api/todos/update/1"
res:http://localhost:8787/todos/update/1 # /api removed
```

## Durable Objects

Miniflare supports the `script_name` option for accessing Durable Objects
exported by other scripts. See
[📌 Durable Objects](/durable-objects.html#using-a-class-exported-by-another-script)
for more details.
Loading

0 comments on commit ed97c96

Please sign in to comment.