Skip to content

Commit 8a8234f

Browse files
committed
Adapt to v3 & current state
1 parent 9764dd3 commit 8a8234f

File tree

26 files changed

+122
-1206
lines changed

26 files changed

+122
-1206
lines changed

src/content/docs/workers/miniflare/core/compatibility.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ order: 8
33
title: "📅 Compatibility Dates"
44
---
55

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

88
## Compatibility Dates
99

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

14-
import ConfigTabs from "../components/mdx/config-tabs";
15-
1614
```js
1715
const mf = new Miniflare({
1816
compatibilityDate: "2021-11-12",

src/content/docs/workers/miniflare/core/fetch.md renamed to src/content/docs/workers/miniflare/core/fetch.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ order: 0
33
title: "📨 Fetch Events"
44
---
55

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

1010
## HTTP Requests
1111

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

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

7171
```js

src/content/docs/workers/miniflare/core/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ title: Core
33
order: 2
44
---
55

6+
import { DirectoryListing } from "~/components";
7+
68
<DirectoryListing path="/core" />

src/content/docs/workers/miniflare/core/modules.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ order: 3
33
title: "📚 Modules"
44
---
55

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

88
## Enabling Modules
99

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

1312
```js
1413
const mf = new Miniflare({
1514
modules: true,
1615
});
1716
```
1817

19-
You can now use `modules` worker scripts like the following:
18+
You can then use `modules` worker scripts like the following:
2019

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

65-
```toml
66-
[[build.upload.rules]]
67-
type = "ESModule"
68-
globs = ["**/*.mjs"]
69-
[[build.upload.rules]]
70-
type = "CommonJS"
71-
globs = ["**/*.js", "**/*.cjs"]
64+
```js
65+
[
66+
{ type: "ESModule", include: ["**/*.mjs"] },
67+
{ type: "CommonJS", include: ["**/*.js", "**/*.cjs"] },
68+
];
7269
```

src/content/docs/workers/miniflare/core/multiple-workers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ await mf.dispose();
7474

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

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

119119
Miniflare supports the `script_name` option for accessing Durable Objects
120120
exported by other scripts. See
121-
[📌 Durable Objects](/storage/durable-objects#using-a-class-exported-by-another-script)
121+
[📌 Durable Objects](/workers/miniflare/storage/durable-objects#using-a-class-exported-by-another-script)
122122
for more details.

src/content/docs/workers/miniflare/core/scheduled.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ order: 1
33
title: "⏰ Scheduled Events"
44
---
55

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

99
## Cron Triggers
1010

src/content/docs/workers/miniflare/core/standards.md

Lines changed: 10 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ order: 6
33
title: "🕸 Web Standards"
44
---
55

6-
- [Web Standards Reference](https://developers.cloudflare.com/workers/runtime-apis/web-standards)
7-
- [Encoding Reference](https://developers.cloudflare.com/workers/runtime-apis/encoding)
8-
- [Fetch Reference](https://developers.cloudflare.com/workers/runtime-apis/fetch)
9-
- [Request Reference](https://developers.cloudflare.com/workers/runtime-apis/request)
10-
- [Response Reference](https://developers.cloudflare.com/workers/runtime-apis/response)
11-
- [Streams Reference](https://developers.cloudflare.com/workers/runtime-apis/streams)
12-
- [Using Streams](https://developers.cloudflare.com/workers/learning/using-streams)
13-
- [Web Crypto Reference](https://developers.cloudflare.com/workers/runtime-apis/web-crypto)
6+
- [Web Standards Reference](/workers/runtime-apis/web-standards)
7+
- [Encoding Reference](/workers/runtime-apis/encoding)
8+
- [Fetch Reference](/workers/runtime-apis/fetch)
9+
- [Request Reference](/workers/runtime-apis/request)
10+
- [Response Reference](/workers/runtime-apis/response)
11+
- [Streams Reference](/workers/runtime-apis/streams)
12+
- [Using Streams](/workers/learning/using-streams)
13+
- [Web Crypto Reference](/workers/runtime-apis/web-crypto)
1414

1515
## Mocking Outbound `fetch` Requests
1616

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

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

6663
Miniflare does not support limiting the amount of
67-
[subrequests](https://developers.cloudflare.com/workers/platform/limits#account-plan-limits).
64+
[subrequests](/workers/platform/limits#account-plan-limits).
6865
Please keep this in mind if you make a large amount of subrequests from your
6966
Worker.
70-
71-
## Global Functionality Limits
72-
73-
To match the
74-
[behaviour of the Workers runtime](https://developers.cloudflare.com/workers/runtime-apis/request/#the-request-context),
75-
some functionality, such as asynchronous I/O (`fetch`, Cache API, KV), timeouts
76-
(`setTimeout`, `setInterval`), and generating cryptographically-secure random
77-
values (`crypto.getRandomValues`, `crypto.subtle.generateKey`), can only be
78-
performed while handling a request, not in the global scope.
79-
80-
KV namespaces and caches returned from `Miniflare#getKVNamespace()` and
81-
`Miniflare#getCaches()` are unaffected by this limit, so they can still be used
82-
in tests without setting any additional options.
83-
84-
## `instanceof`, `constructor` and `prototype` Checks
85-
86-
Miniflare overrides `instanceof` checks for primitive classes like `Object` so
87-
they succeed for values created both inside and outside the Miniflare sandbox
88-
(in a different JavaScript realm). This ensures dynamic type checking often
89-
performed by WebAssembly glue code (e.g. `wasm-bindgen`) always succeeds. Note
90-
that values returned by Workers runtime APIs are created outside the Miniflare
91-
sandbox. See
92-
[this file](https://github.com/cloudflare/miniflare/blob/master/packages/runner-vm/src/instanceof.ts)
93-
for more details.
94-
95-
Primitive classes in this case are defined as JavaScript built-ins that can be
96-
instantiated by something other than their constructor (e.g. literals,
97-
`function`s, runtime errors):
98-
99-
- `Object`
100-
- `Function`
101-
- `Array`
102-
- `Promise`
103-
- `RegExp`
104-
- `Error`, `EvalError`, `RangeError`, `ReferenceError`, `SyntaxError`,
105-
`TypeError`, `URIError`
106-
107-
Primitive `constructor` and `prototype` checks cannot be trapped easily and so
108-
will fail for values created outside the Miniflare sandbox.
109-
110-
```js
111-
import { Miniflare } from "miniflare";
112-
113-
const mf = new Miniflare({
114-
bindings: {
115-
OBJECT: { a: 1 },
116-
ARRAY: new Uint8Array([1, 2, 3]),
117-
},
118-
modules: true,
119-
script: `
120-
export default {
121-
async fetch(request, env, ctx) {
122-
console.log({ a: 1 } instanceof Object); // ✅ true
123-
console.log(new Uint8Array([1, 2, 3]) instanceof Object); // ✅ true
124-
console.log({ a: 1 }.constructor === Object); // ✅ true
125-
console.log(Object.getPrototypeOf({ a: 1 }) === Object.prototype); // ✅ true
126-
127-
console.log(env.OBJECT instanceof Object); // ✅ true
128-
console.log(env.ARRAY instanceof Object); // ✅ true
129-
console.log(env.OBJECT.constructor === Object); // ❌ false
130-
console.log(Object.getPrototypeOf(env.OBJECT) === Object.prototype); // ❌ false
131-
132-
throw new Error("oops!");
133-
}
134-
}
135-
`,
136-
});
137-
138-
try {
139-
await mf.dispatchFetch("http://localhost");
140-
} catch (e) {
141-
console.log(e instanceof Error); // ❌ false
142-
}
143-
```
144-
145-
By default, primitive `instanceof` checks outside the Miniflare sandbox will
146-
fail for values created inside the sandbox (e.g. checking types of thrown
147-
exceptions in tests). To fix this, pass the primitive class in from Node.js as a
148-
custom global. Note this will cause primitive `instanceof` checks to fail for
149-
values created without the constructor inside the sandbox.
150-
151-
```js
152-
const mf = new Miniflare({
153-
modules: true,
154-
script: `
155-
export default {
156-
async fetch(request, env, ctx) {
157-
throw new Error("oops!");
158-
}
159-
}
160-
`,
161-
});
162-
163-
try {
164-
await mf.dispatchFetch("http://localhost");
165-
} catch (e) {
166-
console.log(e instanceof Error); // ✅ true
167-
}
168-
```

src/content/docs/workers/miniflare/core/variables-secrets.md

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,6 @@ const mf = new Miniflare({
1616
});
1717
```
1818

19-
## `.env` Files
20-
21-
Variables and secrets are automatically loaded from a `.env` file in the current
22-
directory. This is especially useful for secrets if your `.env` file is
23-
`.gitignore`d. `.env` files look something like this:
24-
25-
```toml
26-
KEY1=value1
27-
# Woah, comments!
28-
KEY2=value2
29-
```
30-
31-
You can also specify the path to a custom `.env` file:
32-
33-
```js
34-
const mf = new Miniflare({
35-
envPath: ".env.test",
36-
});
37-
```
38-
3919
## Text and Data Blobs
4020

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

51-
## Bindings Priority
52-
53-
Higher priority bindings override lower priority bindings with the same name.
54-
The order (from lowest to highest priority) is:
55-
56-
1. Variables from `wrangler.toml` `[vars]`
57-
2. Variables from `.env` files
58-
3. WASM module bindings (`--wasm`, `[wasm_modules]`)
59-
4. Text blob bindings (`--text-blob`, `[text_blobs]`)
60-
5. Data blob bindings (`--data-blob`, `[data_blobs]`)
61-
6. Custom bindings (`--binding`, `bindings`)
62-
6331
## Globals
6432

6533
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.
66-
67-
<Aside header="Tip">
68-
69-
Miniflare will always set the global variable `MINIFLARE` to `true` in its
70-
sandbox. You can use this as an escape hatch to customise behaviour during local
71-
development:
72-
73-
```js
74-
if (globalThis.MINIFLARE) {
75-
// Do something when running in Miniflare
76-
} else {
77-
// Do something else when running in the real Workers environment
78-
}
79-
```
80-
81-
</Aside>

src/content/docs/workers/miniflare/core/web-assembly.md

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)