Skip to content

Commit

Permalink
Adapt to v3 & current state
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Dec 11, 2024
1 parent b4b138a commit 8399376
Show file tree
Hide file tree
Showing 26 changed files with 122 additions and 1,206 deletions.
4 changes: 1 addition & 3 deletions src/content/docs/workers/miniflare/core/compatibility.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ order: 8
title: "📅 Compatibility Dates"
---

- [Compatibility Dates Reference](https://developers.cloudflare.com/workers/platform/compatibility-dates)
- [Compatibility Dates Reference](/workers/platform/compatibility-dates)

## Compatibility Dates

Miniflare uses compatibility dates to opt-into backwards-incompatible changes
from a specific date. If one isn't set, it will default to some time far in the
past.

import ConfigTabs from "../components/mdx/config-tabs";

```js
const mf = new Miniflare({
compatibilityDate: "2021-11-12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ order: 0
title: "📨 Fetch Events"
---

- [`FetchEvent` Reference](https://developers.cloudflare.com/workers/runtime-apis/fetch-event)
- [`FetchEvent` Lifecycle](https://developers.cloudflare.com/workers/learning/fetch-event-lifecycle)
- [`addEventListener` Reference](https://developers.cloudflare.com/workers/runtime-apis/add-event-listener)
- [`FetchEvent` Reference](/workers/runtime-apis/fetch-event)
- [`FetchEvent` Lifecycle](/workers/learning/fetch-event-lifecycle)
- [`addEventListener` Reference](/workers/runtime-apis/add-event-listener)

## HTTP Requests

Whenever an HTTP request is made, a `Request` object is dispatched to your worker, then the generated `Response` is returned. The
`Request` object will include a
[`cf` object](https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties).
[`cf` object](/workers/runtime-apis/request#incomingrequestcfproperties).
Miniflare will log the method, path, status, and the time it took to respond.

If the worker throws an error whilst generating a response, an error page
Expand Down Expand Up @@ -65,7 +65,7 @@ console.log(await res.json()); // { url: "http://localhost:8787/2", header: "2"
When dispatching events, you are responsible for adding
[`CF-*` headers](https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers-)
and the
[`cf` object](https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties).
[`cf` object](/workers/runtime-apis/request#incomingrequestcfproperties).
This lets you control their values for testing:

```js
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/workers/miniflare/core/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ title: Core
order: 2
---

import { DirectoryListing } from "~/components";

<DirectoryListing path="/core" />
19 changes: 8 additions & 11 deletions src/content/docs/workers/miniflare/core/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ order: 3
title: "📚 Modules"
---

- [Modules Reference](https://developers.cloudflare.com/workers/cli-wrangler/configuration#modules)
- [Modules Reference](/workers/cli-wrangler/configuration#modules)

## Enabling Modules

Miniflare supports both the traditional `service-worker` and newer `modules`
formats for writing workers. To use the `modules` format, enable it with:
Miniflare supports both the traditional `service-worker` and the newer `modules` formats for writing workers. To use the `modules` format, enable it with:

```js
const mf = new Miniflare({
modules: true,
});
```

You can now use `modules` worker scripts like the following:
You can then use `modules` worker scripts like the following:

```js
export default {
Expand Down Expand Up @@ -62,11 +61,9 @@ const mf = new Miniflare({
The following rules are automatically added to the end of your modules rules
list. You can override them by specifying rules matching the same `globs`:

```toml
[[build.upload.rules]]
type = "ESModule"
globs = ["**/*.mjs"]
[[build.upload.rules]]
type = "CommonJS"
globs = ["**/*.js", "**/*.cjs"]
```js
[
{ type: "ESModule", include: ["**/*.mjs"] },
{ type: "CommonJS", include: ["**/*.js", "**/*.cjs"] },
];
```
4 changes: 2 additions & 2 deletions src/content/docs/workers/miniflare/core/multiple-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ await mf.dispose();

You can enable routing by specifying `routes` via the API,
using the
[standard route syntax](https://developers.cloudflare.com/workers/platform/routes#matching-behavior).
[standard route syntax](/workers/configuration/routing/routes/#matching-behavior).
Note port numbers are ignored:

```js
Expand Down Expand Up @@ -118,5 +118,5 @@ const res = await mf.dispatchFetch("http://api.mf/todos/update/1", { ... });

Miniflare supports the `script_name` option for accessing Durable Objects
exported by other scripts. See
[📌 Durable Objects](/storage/durable-objects#using-a-class-exported-by-another-script)
[📌 Durable Objects](/workers/miniflare/storage/durable-objects#using-a-class-exported-by-another-script)
for more details.
4 changes: 2 additions & 2 deletions src/content/docs/workers/miniflare/core/scheduled.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ order: 1
title: "⏰ Scheduled Events"
---

- [`ScheduledEvent` Reference](https://developers.cloudflare.com/workers/runtime-apis/scheduled-event)
- [`addEventListener` Reference](https://developers.cloudflare.com/workers/runtime-apis/add-event-listener)
- [`ScheduledEvent` Reference](/workers/runtime-apis/scheduled-event)
- [`addEventListener` Reference](/workers/runtime-apis/add-event-listener)

## Cron Triggers

Expand Down
122 changes: 10 additions & 112 deletions src/content/docs/workers/miniflare/core/standards.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ order: 6
title: "🕸 Web Standards"
---

- [Web Standards Reference](https://developers.cloudflare.com/workers/runtime-apis/web-standards)
- [Encoding Reference](https://developers.cloudflare.com/workers/runtime-apis/encoding)
- [Fetch Reference](https://developers.cloudflare.com/workers/runtime-apis/fetch)
- [Request Reference](https://developers.cloudflare.com/workers/runtime-apis/request)
- [Response Reference](https://developers.cloudflare.com/workers/runtime-apis/response)
- [Streams Reference](https://developers.cloudflare.com/workers/runtime-apis/streams)
- [Using Streams](https://developers.cloudflare.com/workers/learning/using-streams)
- [Web Crypto Reference](https://developers.cloudflare.com/workers/runtime-apis/web-crypto)
- [Web Standards Reference](/workers/runtime-apis/web-standards)
- [Encoding Reference](/workers/runtime-apis/encoding)
- [Fetch Reference](/workers/runtime-apis/fetch)
- [Request Reference](/workers/runtime-apis/request)
- [Response Reference](/workers/runtime-apis/response)
- [Streams Reference](/workers/runtime-apis/streams)
- [Using Streams](/workers/learning/using-streams)
- [Web Crypto Reference](/workers/runtime-apis/web-crypto)

## Mocking Outbound `fetch` Requests

Expand All @@ -21,10 +21,7 @@ This is useful for testing workers that make HTTP requests to other services. To
enable `fetch` mocking, create a
[`MockAgent`](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin)
using the `createFetchMock()` function, then set this using the `fetchMock`
option. If you're using the
[🤹 Jest Environment](/testing/jest#mocking-outbound-fetch-requests), use the
global `getMiniflareFetchMock()` function to obtain a correctly set-up
[`MockAgent`](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin).
option.

```js
import { Miniflare, createFetchMock } from "miniflare";
Expand Down Expand Up @@ -64,105 +61,6 @@ console.log(await res.text()); // "response:Mocked response!"
## Subrequests

Miniflare does not support limiting the amount of
[subrequests](https://developers.cloudflare.com/workers/platform/limits#account-plan-limits).
[subrequests](/workers/platform/limits#account-plan-limits).
Please keep this in mind if you make a large amount of subrequests from your
Worker.

## Global Functionality Limits

To match the
[behaviour of the Workers runtime](https://developers.cloudflare.com/workers/runtime-apis/request/#the-request-context),
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 only be
performed while handling a request, not in the global scope.

KV namespaces and caches returned from `Miniflare#getKVNamespace()` and
`Miniflare#getCaches()` are unaffected by this limit, so they can still be used
in tests without setting any additional options.

## `instanceof`, `constructor` and `prototype` Checks

Miniflare overrides `instanceof` checks for primitive classes like `Object` so
they succeed for values created both inside and outside the Miniflare sandbox
(in a different JavaScript realm). This ensures dynamic type checking often
performed by WebAssembly glue code (e.g. `wasm-bindgen`) always succeeds. Note
that values returned by Workers runtime APIs are created outside the Miniflare
sandbox. See
[this file](https://github.com/cloudflare/miniflare/blob/master/packages/runner-vm/src/instanceof.ts)
for more details.

Primitive classes in this case are defined as JavaScript built-ins that can be
instantiated by something other than their constructor (e.g. literals,
`function`s, runtime errors):

- `Object`
- `Function`
- `Array`
- `Promise`
- `RegExp`
- `Error`, `EvalError`, `RangeError`, `ReferenceError`, `SyntaxError`,
`TypeError`, `URIError`

Primitive `constructor` and `prototype` checks cannot be trapped easily and so
will fail for values created outside the Miniflare sandbox.

```js
import { Miniflare } from "miniflare";

const mf = new Miniflare({
bindings: {
OBJECT: { a: 1 },
ARRAY: new Uint8Array([1, 2, 3]),
},
modules: true,
script: `
export default {
async fetch(request, env, ctx) {
console.log({ a: 1 } instanceof Object); // ✅ true
console.log(new Uint8Array([1, 2, 3]) instanceof Object); // ✅ true
console.log({ a: 1 }.constructor === Object); // ✅ true
console.log(Object.getPrototypeOf({ a: 1 }) === Object.prototype); // ✅ true
console.log(env.OBJECT instanceof Object); // ✅ true
console.log(env.ARRAY instanceof Object); // ✅ true
console.log(env.OBJECT.constructor === Object); // ❌ false
console.log(Object.getPrototypeOf(env.OBJECT) === Object.prototype); // ❌ false
throw new Error("oops!");
}
}
`,
});

try {
await mf.dispatchFetch("http://localhost");
} catch (e) {
console.log(e instanceof Error); // ❌ false
}
```

By default, primitive `instanceof` checks outside the Miniflare sandbox will
fail for values created inside the sandbox (e.g. checking types of thrown
exceptions in tests). To fix this, pass the primitive class in from Node.js as a
custom global. Note this will cause primitive `instanceof` checks to fail for
values created without the constructor inside the sandbox.

```js
const mf = new Miniflare({
modules: true,
script: `
export default {
async fetch(request, env, ctx) {
throw new Error("oops!");
}
}
`,
});

try {
await mf.dispatchFetch("http://localhost");
} catch (e) {
console.log(e instanceof Error); // ✅ true
}
```
48 changes: 0 additions & 48 deletions src/content/docs/workers/miniflare/core/variables-secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@ const mf = new Miniflare({
});
```

## `.env` Files

Variables and secrets are automatically loaded from a `.env` file in the current
directory. This is especially useful for secrets if your `.env` file is
`.gitignore`d. `.env` files look something like this:

```toml
KEY1=value1
# Woah, comments!
KEY2=value2
```

You can also specify the path to a custom `.env` file:

```js
const mf = new Miniflare({
envPath: ".env.test",
});
```

## Text and Data Blobs

Text and data blobs can be loaded from files. File contents will be read and
Expand All @@ -48,34 +28,6 @@ const mf = new Miniflare({
});
```

## Bindings Priority

Higher priority bindings override lower priority bindings with the same name.
The 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. Text blob bindings (`--text-blob`, `[text_blobs]`)
5. Data blob bindings (`--data-blob`, `[data_blobs]`)
6. Custom bindings (`--binding`, `bindings`)

## Globals

Injecting arbitrary globals is not supported by [workerd](https://github.com/cloudflare/workerd). If you're using a service worker, bindings will be injected as globals, but these must be JSON-serialisable.

<Aside header="Tip">

Miniflare will always set the global variable `MINIFLARE` to `true` in its
sandbox. You can use this as an escape hatch to customise behaviour during local
development:

```js
if (globalThis.MINIFLARE) {
// Do something when running in Miniflare
} else {
// Do something else when running in the real Workers environment
}
```

</Aside>
70 changes: 0 additions & 70 deletions src/content/docs/workers/miniflare/core/web-assembly.md

This file was deleted.

Loading

0 comments on commit 8399376

Please sign in to comment.