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

Commit 9359109

Browse files
committed
Update existing docs for V2
1 parent ceb8c09 commit 9359109

File tree

13 files changed

+176
-223
lines changed

13 files changed

+176
-223
lines changed

docs/.vitepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = {
5858
text: "⚡️ Developing with esbuild",
5959
link: "/recipes/esbuild.html",
6060
},
61-
{ text: " Testing with AVA", link: "/recipes/ava.html" },
61+
{ text: "🚀 Testing with AVA", link: "/recipes/ava.html" },
6262
{ text: "🐛 Attaching a Debugger", link: "/recipes/debugger.html" },
6363
],
6464
},

docs/api.md

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ The [Guide](/fetch.html) goes into more detail on configuring specific features.
4343

4444
<!--prettier-ignore-start-->
4545
::: warning
46-
Like the CLI, the API will automatically load `.env`, `package.json` and `wrangler.toml` files
47-
in the current working directory. This may lead to unexpected behaviour. You can
48-
disable this by setting `envPath`, `packagePath` and `wranglerConfigPath` options to paths of
49-
empty files:
46+
Unlike the CLI, the API won't automatically load configuration from `.env`,
47+
`package.json` and `wrangler.toml` files in the current working directory. You
48+
can enable this by setting the `envPath`, `packagePath` and `wranglerConfigPath`
49+
options to `true`:
5050

5151
```js
5252
const mf = new Miniflare({
53-
envPath: ".env.empty",
54-
packagePath: "package.empty.json", // Containing empty object: {}
55-
wranglerConfigPath: "wrangler.empty.toml"
53+
envPath: true,
54+
packagePath: true,
55+
wranglerConfigPath: true,
5656
});
5757
```
5858
:::
@@ -91,22 +91,11 @@ 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 `reloadOptions`:
94+
(`.env`, `package.json` and `wrangler.toml`) too with `reload`:
9595

9696
```js
9797
const mf = new Miniflare({ ... });
98-
await mf.reloadOptions();
99-
```
100-
101-
### Getting Processed Options
102-
103-
You can get an object containing processed options with `getOptions`. These
104-
contain options resolved from the constructor, `.env`, `package.json` and
105-
`wrangler.toml` files:
106-
107-
```js
108-
const mf = new Miniflare({ ... });
109-
const options = await mf.getOptions();
98+
await mf.reload();
11099
```
111100

112101
### Dispatching Events
@@ -144,7 +133,7 @@ for more details.
144133

145134
### HTTP Server
146135

147-
To start an HTTP server like the CLI's, use the `createServer` method. This
136+
To start an HTTP server like the CLI's, use the `startServer` method. This
148137
returns a
149138
[Node.js `http.Server`](https://nodejs.org/api/http.html#http_class_http_server)
150139
instance:
@@ -158,43 +147,19 @@ const mf = new Miniflare({
158147
event.respondWith(new Response("Hello Miniflare!"));
159148
});
160149
`,
150+
port: 5000,
161151
});
162-
mf.createServer().listen(5000, () => {
163-
console.log("Listening on :5000");
164-
});
165-
```
166-
167-
Note that `port` and `host` options are ignored by default. It's up to you to
168-
get and use them:
169-
170-
```js
171-
const options = await mf.getOptions();
172-
const port = options.port ?? 5000; // Use port 5000 by default
173-
mf.createServer().listen(port, () => { ... });
152+
const server = await mf.startServer();
153+
console.log("Listening on :5000");
174154
```
175155

176156
### HTTPS Server
177157

178-
To start an HTTPS server instead, set the `https` option as described below and
179-
pass `true` as the secure argument of `createServer`. Note that you must now
180-
`await` the call to `createServer`:
181-
182-
```js
183-
import { Miniflare } from "miniflare";
184-
185-
const mf = new Miniflare({
186-
...,
187-
https: ...
188-
});
189-
(await mf.createServer(true)).listen(5000, () => {
190-
console.log("Listening on :5000");
191-
});
192-
```
193-
194-
To use an automatically generated self-signed certificate, set `https` to
195-
`true`. This certificate will be valid for 30 days and be cached in `./.mf/cert`
196-
by default. You can customise this directory by setting `https` to a string path
197-
instead. The certificate will be renewed if it expires in less than 2 days:
158+
To start an HTTPS server instead, set the `https` option. To use an
159+
automatically generated self-signed certificate, set `https` to `true`. This
160+
certificate will be valid for 30 days and be cached in `./.mf/cert` by default.
161+
You can customise this directory by setting `https` to a string path instead.
162+
The certificate will be renewed if it expires in less than 2 days:
198163

199164
```js
200165
const mf = new Miniflare({
@@ -207,54 +172,50 @@ To load an existing certificate from the file system:
207172

208173
```js
209174
const mf = new Miniflare({
210-
https: {
211-
// These are all optional, you don't need to include them all
212-
keyPath: "./key.pem",
213-
certPath: "./cert.pem",
214-
caPath: "./ca.pem",
215-
pfxPath: "./pfx.pfx",
216-
passphrase: "pfx passphrase",
217-
},
175+
// These are all optional, you don't need to include them all
176+
httpsKeyPath: "./key.pem",
177+
httpsCertPath: "./cert.pem",
178+
httpsCaPath: "./ca.pem",
179+
httpsPfxPath: "./pfx.pfx",
180+
httpsPassphrase: "pfx passphrase",
218181
});
219182
```
220183

221184
To load an existing certificate from strings instead:
222185

223186
```js
224187
const mf = new Miniflare({
225-
https: {
226-
// These are all optional, you don't need to include them all
227-
key: "-----BEGIN RSA PRIVATE KEY-----...",
228-
cert: "-----BEGIN CERTIFICATE-----...",
229-
ca: "...",
230-
pfx: "...",
231-
passphrase: "pfx passphrase",
232-
},
188+
// These are all optional, you don't need to include them all
189+
httpsKey: "-----BEGIN RSA PRIVATE KEY-----...",
190+
httpsCert: "-----BEGIN CERTIFICATE-----...",
191+
httpsCa: "...",
192+
httpsPfx: "...",
193+
httpsPassphrase: "pfx passphrase",
233194
});
234195
```
235196

236-
If both a string and path are specified for an option (e.g. `key` and
237-
`keyPath`), the string will be preferred.
197+
If both a string and path are specified for an option (e.g. `httpsKey` and
198+
`httpsKeyPath`), the string will be preferred.
238199

239200
### Logging
240201

241202
By default, `[mf:*]` logs as seen in the CLI are disabled when using the API. To
242-
enable these, set the `log` property to an instance of the `ConsoleLog` class.
243-
Its only parameter is a boolean indicating whether debug messages should be
244-
logged:
203+
enable these, set the `log` property to an instance of the `Log` class. Its only
204+
parameter is the log level indicating which messages should be logged:
245205

246206
```js{5}
247-
import { Miniflare, ConsoleLog } from "miniflare";
207+
import { Log, LogLevel } from "@miniflare/shared";
208+
import { Miniflare } from "miniflare";
248209
249210
const mf = new Miniflare({
250211
scriptPath: "worker.js",
251-
log: new ConsoleLog(true), // Enable --debug messages
212+
log: new Log(LogLevel.DEBUG), // Enable --debug messages
252213
});
253214
```
254215

255-
### Arbitrary Bindings
216+
### Arbitrary Globals
256217

257-
The `bindings` property can be used to inject arbitrary objects into the global
218+
The `globals` property can be used to inject arbitrary objects into the global
258219
scope of the sandbox. This can be very useful for testing:
259220

260221
```js{9-11}
@@ -266,7 +227,7 @@ const mf = new Miniflare({
266227
event.respondWith(new Response(greet("Miniflare")));
267228
});
268229
`,
269-
bindings: {
230+
globals: {
270231
greet: (name) => `Hello ${name}!`,
271232
},
272233
});

docs/cache.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ const mf = new Miniflare({
8686
let res = await mf.dispatchFetch("http://localhost:8787/put");
8787
console.log(await res.text()); // 1
8888
89-
const cache = await mf.getCache(); // Gets the default cache
90-
const namedCache = await mf.getCache("cache_name"); // Gets a namespaced cache
91-
const cachedRes = await cache.match("https://miniflare.dev/");
89+
const caches = await mf.getCaches(); // Gets the global caches object
90+
const cachedRes = await caches.default.match("https://miniflare.dev/");
9291
console.log(await cachedRes.text()); // 1
9392
94-
await cache.put(
93+
await caches.default.put(
9594
"https://miniflare.dev",
9695
new Response("2", {
9796
headers: { "Cache-Control": "max-age=3600" },
@@ -108,17 +107,17 @@ When disabled, the caches will still be available in the sandbox, they just
108107
won't cache anything. This may be useful during development:
109108

110109
```shell
111-
$ miniflare --disable-cache
110+
$ miniflare --no-cache
112111
```
113112

114113
```toml
115114
# wrangler.toml
116115
[miniflare]
117-
disable_cache = true
116+
cache = false
118117
```
119118

120119
```js
121120
const mf = new Miniflare({
122-
disableCache: true,
121+
cache: false,
123122
});
124123
```

0 commit comments

Comments
 (0)