miniflare 2.14.0 โ†’ 3.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,178 +1,494 @@
1
1
  # ๐Ÿ”ฅ Miniflare
2
2
 
3
- **Miniflare** is a simulator for developing and testing
4
- [**Cloudflare Workers**](https://workers.cloudflare.com/).
5
-
6
- - ๐ŸŽ‰ **Fun:** develop workers easily with detailed logging, file watching and
7
- pretty error pages supporting source maps.
8
- - ๐Ÿ”‹ **Full-featured:** supports most Workers features, including KV, Durable
9
- Objects, WebSockets, modules and more.
10
- - โšก **Fully-local:** test and develop Workers without an internet connection.
11
- Reload code on change quickly.
12
-
13
- It's an alternative to `wrangler dev`, written in TypeScript, that runs your
14
- workers in a sandbox implementing Workers' runtime APIs.
15
-
16
- **See <https://miniflare.dev> for more detailed documentation.**
17
-
18
- ## Features
19
-
20
- - ๐Ÿ“จ Fetch Events (with HTTP(S) server and manual dispatch)
21
- - โฐ Scheduled Events (with cron triggering and manual dispatch)
22
- - ๐Ÿ”‘ Variables and Secrets with `.env` Files
23
- - ๐Ÿ“š Modules Support
24
- - ๐Ÿ“ฆ KV (with optional persistence)
25
- - ๐Ÿชฃ R2 (with optional persistence)
26
- - โœจ Cache (with optional persistence)
27
- - ๐Ÿ“Œ Durable Objects (with optional persistence)
28
- - ๐ŸŒ Workers Sites
29
- - โœ‰๏ธ WebSockets
30
- - ๐Ÿ›  Custom & Wrangler Builds Support
31
- - โš™๏ธ WebAssembly Support
32
- - ๐Ÿ—บ Source Map Support
33
- - ๐Ÿ•ธ Web Standards: Base64, Timers, Fetch, Encoding, URL, Streams, Crypto
34
- - ๐Ÿ“„ HTMLRewriter
35
- - โšก๏ธ Live Reload on File Changes
36
- - ๐Ÿ“… Compatibility Dates/Flags Support
37
- - ๐Ÿ”Œ Multiple Workers Support
38
- - ๐Ÿคน Custom Jest Environment (with isolated per-test storage)
39
- - ๐Ÿ’ช Written in TypeScript
40
-
41
- ## Install
42
-
43
- Miniflare is installed using npm:
44
-
45
- ```sh
46
- $ npm install -g miniflare # either globally..
47
- $ npm install -D miniflare # ...or as a dev dependency
48
- ```
3
+ **Miniflare 3** is a simulator for developing and testing
4
+ [**Cloudflare Workers**](https://workers.cloudflare.com/), powered by
5
+ [`workerd`](https://github.com/cloudflare/workerd).
49
6
 
50
- ## Using the CLI
51
-
52
- ```sh
53
- $ miniflare worker.js --watch --debug
54
- [mf:dbg] Options:
55
- [mf:dbg] - Scripts: worker.js
56
- [mf:dbg] Reloading worker.js...
57
- [mf:inf] Worker reloaded! (97B)
58
- [mf:dbg] Watching .env, package.json, worker.js, wrangler.toml...
59
- [mf:inf] Listening on :8787
60
- [mf:inf] - http://127.0.0.1:8787
61
- ```
7
+ > :warning: Miniflare 3 is API-only, and does not expose a CLI. Use Wrangler
8
+ > with `wrangler dev --experimental-local` to develop your Workers locally with
9
+ > Miniflare 3.
10
+
11
+ ## Quick Start
62
12
 
63
- ## Using the API
13
+ ```shell
14
+ $ npm install miniflare --save-dev
15
+ ```
64
16
 
65
17
  ```js
66
18
  import { Miniflare } from "miniflare";
67
19
 
20
+ // Create a new Miniflare instance, starting a workerd server
68
21
  const mf = new Miniflare({
69
- script: `
70
- addEventListener("fetch", (event) => {
22
+ script: `addEventListener("fetch", (event) => {
71
23
  event.respondWith(new Response("Hello Miniflare!"));
72
- });
73
- `,
24
+ })`,
74
25
  });
75
- const res = await mf.dispatchFetch("http://localhost:8787/");
76
- console.log(await res.text()); // Hello Miniflare!
77
- ```
78
26
 
79
- ## CLI Reference
27
+ // Send a request to the workerd server, the host is ignored
28
+ const response = await mf.dispatchFetch("http://localhost:8787/");
29
+ console.log(await response.text()); // Hello Miniflare!
80
30
 
31
+ // Cleanup Miniflare, shutting down the workerd server
32
+ await mf.dispose();
81
33
  ```
82
- Usage: miniflare [script] [options]
83
-
84
- Core Options:
85
- -h, --help Show help [boolean]
86
- -v, --version Show version number [boolean]
87
- -c, --wrangler-config Path to wrangler.toml [string]
88
- --wrangler-env Environment in wrangler.toml to use [string]
89
- --package Path to package.json [string]
90
- -m, --modules Enable modules [boolean]
91
- --modules-rule Modules import rule [array:TYPE=GLOB]
92
- --compat-date Opt into backwards-incompatible changes from [string]
93
- --compat-flag Control specific backwards-incompatible changes [array]
94
- --usage-model Usage model (bundled by default) [string]
95
- -u, --upstream URL of upstream origin [string]
96
- -w, --watch Watch files for changes [boolean]
97
- -d, --debug Enable debug logging [boolean]
98
- -V, --verbose Enable verbose logging [boolean]
99
- --(no-)update-check Enable update checker (enabled by default) [boolean]
100
- --repl Enable interactive REPL [boolean]
101
- --root Path to resolve files relative to [string]
102
- --mount Mount additional named workers [array:NAME=PATH[@ENV]]
103
- --name Name of service [string]
104
- --route Route to respond with this worker on [array]
105
- --global-async-io Allow async I/O outside handlers [boolean]
106
- --global-timers Allow setting timers outside handlers [boolean]
107
- --global-random Allow secure random generation outside handlers [boolean]
108
- --actual-time Always return correct time from Date methods [boolean]
109
- --inaccurate-cpu Log inaccurate CPU time measurements [boolean]
110
-
111
- HTTP Options:
112
- -H, --host Host for HTTP(S) server to listen on [string]
113
- -p, --port Port for HTTP(S) server to listen on [number]
114
- -O, --open Automatically open browser to URL [boolean/string]
115
- --https Enable self-signed HTTPS (with optional cert path) [boolean/string]
116
- --https-key Path to PEM SSL key [string]
117
- --https-cert Path to PEM SSL cert chain [string]
118
- --https-ca Path to SSL trusted CA certs [string]
119
- --https-pfx Path to PFX/PKCS12 SSL key/cert chain [string]
120
- --https-passphrase Passphrase to decrypt SSL files [string]
121
- --(no-)cf-fetch Path for cached Request cf object from Cloudflare [boolean/string]
122
- --live-reload Reload HTML pages whenever worker is reloaded [boolean]
123
-
124
- Scheduler Options:
125
- -t, --cron CRON expression for triggering scheduled events [array]
126
-
127
- Build Options:
128
- -B, --build-command Command to build project [string]
129
- --build-base-path Working directory for build command [string]
130
- --build-watch-path Directory to watch for rebuilding on changes [array]
131
-
132
- KV Options:
133
- -k, --kv KV namespace to bind [array]
134
- --kv-persist Persist KV data (to optional path) [boolean/string]
135
-
136
- R2 Options:
137
- -r, --r2 R2 bucket to bind [array]
138
- --r2-persist Persist R2 data (to optional path) [boolean/string]
139
-
140
- Durable Objects Options:
141
- -o, --do Durable Object to bind [array:NAME=CLASS[@MOUNT]]
142
- --do-persist Persist Durable Object data (to optional path) [boolean/string]
143
- --(no-)do-alarms Enable Durable Object alarms (enabled by default) [boolean]
144
-
145
- Cache Options:
146
- --(no-)cache Enable default/named caches (enabled by default) [boolean]
147
- --cache-persist Persist cached data (to optional path) [boolean/string]
148
-
149
- Sites Options:
150
- -s, --site Path to serve Workers Site files from [string]
151
- --site-include Glob pattern of site files to serve [array]
152
- --site-exclude Glob pattern of site files not to serve [array]
153
-
154
- Bindings Options:
155
- -e, --env Path to .env file [string]
156
- -b, --binding Binds variable/secret to environment [array:KEY=VALUE]
157
- --global Binds variable/secret to global scope [array:KEY=VALUE]
158
- --wasm WASM module to bind [array:NAME=PATH]
159
- --text-blob Text blob to bind [array:NAME=PATH]
160
- --data-blob Data blob to bind [array:NAME=PATH]
161
- -S, --service Mounted service to bind [array:NAME=MOUNT[@ENV]]
162
- ```
163
34
 
164
- ## Acknowledgements
35
+ ## API
36
+
37
+ ### `type Awaitable<T>`
38
+
39
+ `T | Promise<T>`
40
+
41
+ Represents a value that can be `await`ed. Used in callback types to allow
42
+ `Promise`s to be returned if necessary.
43
+
44
+ ### `type Json`
45
+
46
+ `string | number | boolean | null | Record<string, Json> | Json[]`
47
+
48
+ Represents a JSON-serialisable value.
49
+
50
+ ### `type ModuleRuleType`
51
+
52
+ `"ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm"`
53
+
54
+ Represents how a module's contents should be interpreted.
55
+
56
+ - `"ESModule"`: interpret as
57
+ [ECMAScript module](https://tc39.es/ecma262/#sec-modules)
58
+ - `"CommonJS"`: interpret as
59
+ [CommonJS module](https://nodejs.org/api/modules.html#modules-commonjs-modules)
60
+ - `"Text"`: interpret as UTF8-encoded data, expose in runtime with
61
+ `string`-typed `default` export
62
+ - `"Data"`: interpret as arbitrary binary data, expose in runtime with
63
+ `ArrayBuffer`-typed `default` export
64
+ - `"CompiledWasm"`: interpret as binary WebAssembly module data, expose in
65
+ runtime with `WebAssembly.Module`-typed `default` export
66
+
67
+ ### `interface ModuleDefinition`
68
+
69
+ Represents a manually defined module.
70
+
71
+ - `type: ModuleRuleType`
72
+
73
+ How this module's contents should be interpreted.
74
+
75
+ - `path: string`
76
+
77
+ Path of this module. The module's "name" will be obtained by converting this
78
+ to a relative path. The original path will be used to read `contents` if it's
79
+ omitted.
80
+
81
+ - `contents?: string | Uint8Array`
82
+
83
+ Contents override for this module. Binary data should be passed as
84
+ `Uint8Array`s. If omitted, will be read from `path`.
85
+
86
+ ### `interface ModuleRule`
87
+
88
+ Represents a rule for identifying the `ModuleRuleType` of automatically located
89
+ modules.
90
+
91
+ - `type: ModuleRuleType`
92
+
93
+ How to interpret modules that match the `include` patterns.
94
+
95
+ - `include: string[]`
96
+
97
+ Glob patterns to match located module paths against (e.g. `["**/*.txt"]`).
98
+
99
+ - `fallthrough?: boolean`
100
+
101
+ If `true`, ignore any further rules of this `type`. This is useful for
102
+ disabling the built-in `ESModule` and `CommonJS` rules that match `*.mjs` and
103
+ `*.js`/`*.cjs` files respectively.
104
+
105
+ ### `type Persistence`
106
+
107
+ `boolean | string | undefined`
108
+
109
+ Represents where data should be persisted, if anywhere.
110
+
111
+ - If this is `undefined` or `false`, data will be stored in-memory and only
112
+ persist between `Miniflare#setOptions()` calls, not restarts nor
113
+ `new Miniflare` instances.
114
+ - If this is `true`, data will be stored on the file-system, in the `$PWD/.mf`
115
+ directory.
116
+ - If this looks like a URL, then:
117
+ - If the protocol is `memory:`, data will be stored in-memory as above.
118
+ - If the protocol is `file:`, data will be stored on the file-system, in the
119
+ specified directory (e.g. `file:///path/to/directory`). If the `unsanitise`
120
+ search parameter is `true`, path sanitisation will be disabled.
121
+ - Otherwise, if this is just a regular `string`, data will be stored on the
122
+ file-system, using the value as the directory path.
123
+
124
+ ### `enum LogLevel`
125
+
126
+ `NONE, ERROR, WARN, INFO, DEBUG, VERBOSE`
127
+
128
+ Controls which messages Miniflare logs. All messages at or below the selected
129
+ level will be logged.
130
+
131
+ ### `interface LogOptions`
132
+
133
+ - `prefix?: string`
134
+
135
+ String to add before the level prefix when logging messages. Defaults to `mf`.
136
+
137
+ - `suffix?: string`
138
+
139
+ String to add after the level prefix when logging messages.
140
+
141
+ ### `class Log`
142
+
143
+ - `constructor(level?: LogLevel, opts?: LogOptions)`
144
+
145
+ Creates a new logger that logs all messages at or below the specified level to
146
+ the `console`.
147
+
148
+ - `error(message: Error)`
149
+
150
+ Logs a message at the `ERROR` level. If the constructed log `level` is less
151
+ than `ERROR`, `throw`s the `message` instead.
152
+
153
+ - `warn(message: string)`
154
+
155
+ Logs a message at the `WARN` level.
156
+
157
+ - `info(message: string)`
158
+
159
+ Logs a message at the `INFO` level.
160
+
161
+ - `debug(message: string)`
162
+
163
+ Logs a message at the `DEBUG` level.
164
+
165
+ - `verbose(message: string)`
166
+
167
+ Logs a message at the `VERBOSE` level.
168
+
169
+ ### `class NoOpLog extends Log`
170
+
171
+ - `constructor()`
172
+
173
+ Creates a new logger that logs nothing to the `console`, and always `throw`s
174
+ `message`s logged at the `ERROR` level.
175
+
176
+ ### `interface WorkerOptions`
177
+
178
+ Options for an individual Worker/"nanoservice". All bindings are accessible on
179
+ the global scope in service-worker format Workers, or via the 2nd `env`
180
+ parameter in module format Workers.
181
+
182
+ #### Core
183
+
184
+ - `name?: string`
185
+
186
+ Unique name for this worker. Only required if multiple `workers` are
187
+ specified.
188
+
189
+ - `script?: string`
190
+
191
+ JavaScript code for this worker. If this is a service worker format Worker, it
192
+ must not have any imports. If this is a modules format Worker, it must not
193
+ have any _npm_ imports, and `modules: true` must be set. If it does have
194
+ imports, `scriptPath` must also be set so Miniflare knows where to resolve
195
+ them relative to.
196
+
197
+ - `scriptPath?: string`
198
+
199
+ Path of JavaScript entrypoint. If this is a service worker format Worker, it
200
+ must not have any imports. If this is a modules format Worker, it must not
201
+ have any _npm_ imports, and `modules: true` must be set.
202
+
203
+ - `modules?: boolean | ModuleDefinition[]`
204
+
205
+ - If `true`, Miniflare will treat `script`/`scriptPath` as an ES Module and
206
+ automatically locate transitive module dependencies according to
207
+ `modulesRules`. Note that automatic location is not perfect: if the
208
+ specifier to a dynamic `import()` or `require()` is not a string literal, an
209
+ exception will be thrown.
210
+
211
+ - If set to an array, modules can be defined manually. Transitive dependencies
212
+ must also be defined. Note the first module must be the entrypoint and have
213
+ type `"ESModule"`.
214
+
215
+ - `modulesRoot?: string`
216
+
217
+ If `modules` is set to an array, modules' "name"s will be their `path`s
218
+ relative to this value. This ensures file paths in stack traces are correct.
219
+
220
+ <!-- prettier-ignore-start -->
221
+ <!-- (for disabling `;` insertion in `js` code block) -->
222
+
223
+ - `modulesRules?: ModuleRule[]`
224
+
225
+ Rules for identifying the `ModuleRuleType` of automatically located modules
226
+ when `modules: true` is set. Note the following default rules are always
227
+ included at the end:
228
+
229
+ ```js
230
+ [
231
+ { type: "ESModule", include: ["**/*.mjs"] },
232
+ { type: "CommonJS", include: ["**/*.js", "**/*.cjs"] },
233
+ ]
234
+ ```
235
+
236
+ > If `script` and `scriptPath` are set, and `modules` is set to an array,
237
+ > `modules` takes priority for a Worker's code, followed by `script`, then
238
+ > `scriptPath`.
239
+
240
+ <!-- prettier-ignore-end -->
241
+
242
+ - `compatibilityDate?: string`
243
+
244
+ [Compatibility date](https://developers.cloudflare.com/workers/platform/compatibility-dates/)
245
+ to use for this Worker. Defaults to a date far in the past.
246
+
247
+ - `compatibilityFlags?: string[]`
248
+
249
+ [Compatibility flags](https://developers.cloudflare.com/workers/platform/compatibility-dates/)
250
+ to use for this Worker.
251
+
252
+ - `bindings?: Record<string, Json>`
253
+
254
+ Record mapping binding name to arbitrary JSON-serialisable values to inject as
255
+ bindings into this Worker.
256
+
257
+ - `wasmBindings?: Record<string, string>`
258
+
259
+ Record mapping binding name to paths containing binary WebAssembly module data
260
+ to inject as `WebAssembly.Module` bindings into this Worker.
261
+
262
+ - `textBlobBindings?: Record<string, string>`
263
+
264
+ Record mapping binding name to paths containing UTF8-encoded data to inject as
265
+ `string` bindings into this Worker.
266
+
267
+ - `dataBlobBindings?: Record<string, string>`
268
+
269
+ Record mapping binding name to paths containing arbitrary binary data to
270
+ inject as `ArrayBuffer` bindings into this Worker.
271
+
272
+ - `serviceBindings?: Record<string, string | { network: Network } | { external: ExternalServer } | { disk: DiskDirectory } | (request: Request) => Awaitable<Response>>`
273
+
274
+ Record mapping binding name to service designators to inject as
275
+ `{ fetch: typeof fetch }`
276
+ [service bindings](https://developers.cloudflare.com/workers/platform/bindings/about-service-bindings/)
277
+ into this Worker.
278
+
279
+ - If the designator is a `string`, requests will be dispatched to the Worker
280
+ with that `name`.
281
+ - If the designator is an object of the form `{ network: { ... } }`, where
282
+ `network` is a
283
+ [`workerd` `Network` struct](https://github.com/cloudflare/workerd/blob/bdbd6075c7c53948050c52d22f2dfa37bf376253/src/workerd/server/workerd.capnp#L555-L598),
284
+ requests will be dispatched according to the `fetch`ed URL.
285
+ - If the designator is an object of the form `{ external: { ... } }` where
286
+ `external` is a
287
+ [`workerd` `ExternalServer` struct](https://github.com/cloudflare/workerd/blob/bdbd6075c7c53948050c52d22f2dfa37bf376253/src/workerd/server/workerd.capnp#L504-L553),
288
+ requests will be dispatched to the specified remote server.
289
+ - If the designator is an object of the form `{ disk: { ... } }` where `disk`
290
+ is a
291
+ [`workerd` `DiskDirectory` struct](https://github.com/cloudflare/workerd/blob/bdbd6075c7c53948050c52d22f2dfa37bf376253/src/workerd/server/workerd.capnp#L600-L643),
292
+ requests will be dispatched to an HTTP service backed by an on-disk
293
+ directory.
294
+ - If the designator is a function, requests will be dispatched to your custom
295
+ handler. This allows you to access data and functions defined in Node.js
296
+ from your Worker.
297
+
298
+ #### Cache
299
+
300
+ - `cache?: boolean`
301
+
302
+ If `false`, default and named caches will be disabled. The Cache API will
303
+ still be available, it just won't cache anything.
304
+
305
+ - `cacheWarnUsage?: boolean`
306
+
307
+ If `true`, the first use of the Cache API will log a warning stating that the
308
+ Cache API is unsupported on `workers.dev` subdomains.
309
+
310
+ #### Durable Objects
311
+
312
+ - `durableObjects?: Record<string, string | { className: string, scriptName?: string }>`
313
+
314
+ Record mapping binding name to Durable Object class designators to inject as
315
+ `DurableObjectNamespace` bindings into this Worker.
316
+
317
+ - If the designator is a `string`, it should be the name of a `class` exported
318
+ by this Worker.
319
+ - If the designator is an object, and `scriptName` is `undefined`, `className`
320
+ should be the name of a `class` exported by this Worker.
321
+ - Otherwise, `className` should be the name of a `class` exported by the
322
+ Worker with a `name` of `scriptName`.
323
+
324
+ #### KV
325
+
326
+ - `kvNamespaces?: Record<string, string> | string[]`
327
+
328
+ Record mapping binding name to KV namespace IDs to inject as `KVNamespace`
329
+ bindings into this Worker. Different Workers may bind to the same namespace ID
330
+ with different binding names. If a `string[]` of binding names is specified,
331
+ the binding name and KV namespace ID are assumed to be the same.
332
+
333
+ - `sitePath?: string`
334
+
335
+ Path to serve Workers Sites files from. If set, `__STATIC_CONTENT` and
336
+ `__STATIC_CONTENT_MANIFEST` bindings will be injected into this Worker. In
337
+ modules mode, `__STATIC_CONTENT_MANIFEST` will also be exposed as a module
338
+ with a `string`-typed `default` export, containing the JSON-stringified
339
+ manifest. Note Workers Sites files are never cached in Miniflare.
340
+
341
+ - `siteInclude?: string[]`
342
+
343
+ If set, only files with paths matching these glob patterns will be served.
344
+
345
+ - `siteExclude?: string[]`
346
+
347
+ If set, only files with paths _not_ matching these glob patterns will be
348
+ served.
349
+
350
+ #### R2
351
+
352
+ - `r2Buckets?: Record<string, string> | string[]`
353
+
354
+ Record mapping binding name to R2 bucket names to inject as `R2Bucket`
355
+ bindings into this Worker. Different Workers may bind to the same bucket name
356
+ with different binding names. If a `string[]` of binding names is specified,
357
+ the binding name and bucket name are assumed to be the same.
358
+
359
+ #### D1
360
+
361
+ - `d1Databases?: Record<string, string> | string[]`
362
+
363
+ Record mapping binding name to D1 database IDs to inject as `Fetcher` bindings
364
+ into this Worker. Note these bindings must be wrapped with a facade to provide
365
+ the expected `D1Database` API. Different Workers may bind to the same database
366
+ ID with different binding names. If a `string[]` of binding names is
367
+ specified, the binding name and database ID are assumed to be the same.
368
+
369
+ #### Analytics Engine and Queues
370
+
371
+ _Not yet supported_
372
+
373
+ ### `interface SharedOptions`
374
+
375
+ Options shared between all Workers/"nanoservices".
376
+
377
+ #### Core
378
+
379
+ - `host?: string`
380
+
381
+ Hostname that the `workerd` server should listen on. Defaults to `127.0.0.1`.
382
+
383
+ - `port?: number`
384
+
385
+ Port that the `workerd` server should listen on. Tries to default to `8787`,
386
+ but falls back to a random free port if this is in use. Note if a manually
387
+ specified port is in use, Miniflare throws an error, rather than attempting to
388
+ find a free port.
389
+
390
+ - `inspectorPort?: number`
391
+
392
+ Port that `workerd` should start a DevTools inspector server on. Visit
393
+ `chrome://inspect` in a Chromium-based browser to connect to this. This can be
394
+ used to see detailed `console.log`s, profile CPU usage, and will eventually
395
+ allow step-through debugging.
396
+
397
+ - `verbose?: boolean`
398
+
399
+ Enable `workerd`'s `--verbose` flag for verbose logging. This can be used to
400
+ see simplified `console.log`s.
401
+
402
+ - `log?: Log`
403
+
404
+ Logger implementation for Miniflare's errors, warnings and informative
405
+ messages.
406
+
407
+ - `cf?: boolean | string | Record<string, any>`
408
+
409
+ Controls the object returned from incoming `Request`'s `cf` property.
410
+
411
+ - If set to a falsy value, an object with default placeholder values will be
412
+ used
413
+ - If set to an object, that object will be used
414
+ - If set to `true`, a real `cf` object will be fetched from a trusted
415
+ Cloudflare endpoint and cached in `node_modules/.mf` for 30 days
416
+ - If set to a `string`, a real `cf` object will be fetched and cached at the
417
+ provided path for 30 days
418
+
419
+ - `liveReload?: boolean`
420
+
421
+ If `true`, Miniflare will inject a script into HTML responses that
422
+ automatically reloads the page in-browser whenever the Miniflare instance's
423
+ options are updated.
424
+
425
+ #### Cache, Durable Objects, KV, R2 and D1
426
+
427
+ - `cachePersist?: Persistence`
428
+
429
+ Where to persist data cached in default or named caches. See docs for
430
+ `Persistence`.
431
+
432
+ - `durableObjectsPersist?: Persistence`
433
+
434
+ _Not yet supported_, Miniflare will throw if this is truthy and Durable Object
435
+ bindings are specified.
436
+
437
+ - `kvPersist?: Persistence`
438
+
439
+ Where to persist data stored in KV namespaces. See docs for `Persistence`.
440
+
441
+ - `r2Persist?: Persistence`
442
+
443
+ Where to persist data stored in R2 buckets. See docs for `Persistence`.
444
+
445
+ - `d1Persist?: Persistence`
446
+
447
+ Where to persist data stored in D1 databases. See docs for `Persistence`.
448
+
449
+ #### Analytics Engine and Queues
450
+
451
+ _Not yet supported_
452
+
453
+ ### `type MiniflareOptions`
454
+
455
+ `SharedOptions & (WorkerOptions | { workers: WorkerOptions[] })`
456
+
457
+ Miniflare accepts either a single Worker configuration or multiple Worker
458
+ configurations in the `workers` array. When specifying an array of Workers, the
459
+ first Worker is designated the entrypoint and will receive all incoming HTTP
460
+ requests. Some options are shared between all workers and should always be
461
+ defined at the top-level.
462
+
463
+ ### `class Miniflare`
464
+
465
+ - `constructor(opts: MiniflareOptions)`
466
+
467
+ Creates a Miniflare instance and starts a new `workerd` server. Note unlike
468
+ Miniflare 2, Miniflare 3 _always_ starts a HTTP server listening on the
469
+ configured `host` and `port`: there are no `createServer`/`startServer`
470
+ functions.
471
+
472
+ - `setOptions(opts: MiniflareOptions)`
473
+
474
+ Updates the configuration for this Miniflare instance and restarts the
475
+ `workerd` server. Note unlike Miniflare 2, this does _not_ merge the new
476
+ configuration with the old configuration.
477
+
478
+ - `ready: Promise<URL>`
479
+
480
+ Returns a `Promise` that resolves with a `http` `URL` to the `workerd` server
481
+ once it has started and is able to accept requests.
482
+
483
+ - `dispatchFetch(input: RequestInfo, init?: RequestInit): Promise<Response>`
165
484
 
166
- Miniflare was created by [Brendan Coll](https://github.com/mrbbot).
485
+ Sends a HTTP request to the `workerd` server, dispatching a `fetch` event in
486
+ the entrypoint Worker. Returns a `Promise` that resolves with the response.
487
+ Note that this implicitly waits for the `ready` `Promise` to resolve, there's
488
+ no need to do that yourself first. Additionally, the host of the request's URL
489
+ is always ignored and replaced with the `workerd` server's.
167
490
 
168
- Many thanks to
169
- [dollarshaveclub/cloudworker](https://github.com/dollarshaveclub/cloudworker)
170
- and
171
- [gja/cloudflare-worker-local](https://github.com/gja/cloudflare-worker-local)
172
- for inspiration.
491
+ - `dispose(): Promise<void>`
173
492
 
174
- Durable Object's transactions are implemented using Optimistic Concurrency
175
- Control (OCC) as described in
176
- ["On optimistic methods for concurrency control." ACM Transactions on Database Systems](https://dl.acm.org/doi/10.1145/319566.319567).
177
- Thanks to [Alistair O'Brien](https://github.com/johnyob) for helping the
178
- Miniflare creator understand this.
493
+ Cleans up the Miniflare instance, and shuts down the `workerd` server. Note
494
+ that after this is called, `setOptions` and `dispatchFetch` cannot be called.