|
| 1 | +# 🔥 Miniflare |
| 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 | +``` |
| 49 | + |
| 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 | +``` |
| 62 | + |
| 63 | +## Using the API |
| 64 | + |
| 65 | +```js |
| 66 | +import { Miniflare } from "miniflare"; |
| 67 | + |
| 68 | +const mf = new Miniflare({ |
| 69 | + script: ` |
| 70 | + addEventListener("fetch", (event) => { |
| 71 | + event.respondWith(new Response("Hello Miniflare!")); |
| 72 | + }); |
| 73 | + `, |
| 74 | +}); |
| 75 | +const res = await mf.dispatchFetch("http://localhost:8787/"); |
| 76 | +console.log(await res.text()); // Hello Miniflare! |
| 77 | +``` |
| 78 | + |
| 79 | +## CLI Reference |
| 80 | + |
| 81 | +``` |
| 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 | +
|
| 110 | +HTTP Options: |
| 111 | + -H, --host Host for HTTP(S) server to listen on [string] |
| 112 | + -p, --port Port for HTTP(S) server to listen on [number] |
| 113 | + -O, --open Automatically open browser to URL [boolean/string] |
| 114 | + --https Enable self-signed HTTPS (with optional cert path) [boolean/string] |
| 115 | + --https-key Path to PEM SSL key [string] |
| 116 | + --https-cert Path to PEM SSL cert chain [string] |
| 117 | + --https-ca Path to SSL trusted CA certs [string] |
| 118 | + --https-pfx Path to PFX/PKCS12 SSL key/cert chain [string] |
| 119 | + --https-passphrase Passphrase to decrypt SSL files [string] |
| 120 | + --(no-)cf-fetch Path for cached Request cf object from Cloudflare [boolean/string] |
| 121 | + --live-reload Reload HTML pages whenever worker is reloaded [boolean] |
| 122 | +
|
| 123 | +Scheduler Options: |
| 124 | + -t, --cron CRON expression for triggering scheduled events [array] |
| 125 | +
|
| 126 | +Build Options: |
| 127 | + -B, --build-command Command to build project [string] |
| 128 | + --build-base-path Working directory for build command [string] |
| 129 | + --build-watch-path Directory to watch for rebuilding on changes [array] |
| 130 | +
|
| 131 | +KV Options: |
| 132 | + -k, --kv KV namespace to bind [array] |
| 133 | + --kv-persist Persist KV data (to optional path) [boolean/string] |
| 134 | +
|
| 135 | +R2 Options: |
| 136 | + -r, --r2 R2 bucket to bind [array] |
| 137 | + --r2-persist Persist R2 data (to optional path) [boolean/string] |
| 138 | +
|
| 139 | +Durable Objects Options: |
| 140 | + -o, --do Durable Object to bind [array:NAME=CLASS[@MOUNT]] |
| 141 | + --do-persist Persist Durable Object data (to optional path) [boolean/string] |
| 142 | + --(no-)do-alarms Enable Durable Object alarms (enabled by default) [boolean] |
| 143 | +
|
| 144 | +Cache Options: |
| 145 | + --(no-)cache Enable default/named caches (enabled by default) [boolean] |
| 146 | + --cache-persist Persist cached data (to optional path) [boolean/string] |
| 147 | +
|
| 148 | +Sites Options: |
| 149 | + -s, --site Path to serve Workers Site files from [string] |
| 150 | + --site-include Glob pattern of site files to serve [array] |
| 151 | + --site-exclude Glob pattern of site files not to serve [array] |
| 152 | +
|
| 153 | +Bindings Options: |
| 154 | + -e, --env Path to .env file [string] |
| 155 | + -b, --binding Binds variable/secret to environment [array:KEY=VALUE] |
| 156 | + --global Binds variable/secret to global scope [array:KEY=VALUE] |
| 157 | + --wasm WASM module to bind [array:NAME=PATH] |
| 158 | + --text-blob Text blob to bind [array:NAME=PATH] |
| 159 | + --data-blob Data blob to bind [array:NAME=PATH] |
| 160 | + -S, --service Mounted service to bind [array:NAME=MOUNT[@ENV]] |
| 161 | +``` |
| 162 | + |
| 163 | +## Acknowledgements |
| 164 | + |
| 165 | +Miniflare was created by [Brendan Coll](https://github.com/mrbbot). |
| 166 | + |
| 167 | +Many thanks to |
| 168 | +[dollarshaveclub/cloudworker](https://github.com/dollarshaveclub/cloudworker) |
| 169 | +and |
| 170 | +[gja/cloudflare-worker-local](https://github.com/gja/cloudflare-worker-local) |
| 171 | +for inspiration. |
| 172 | + |
| 173 | +Durable Object's transactions are implemented using Optimistic Concurrency |
| 174 | +Control (OCC) as described in |
| 175 | +["On optimistic methods for concurrency control." ACM Transactions on Database Systems](https://dl.acm.org/doi/10.1145/319566.319567). |
| 176 | +Thanks to [Alistair O'Brien](https://github.com/johnyob) for helping the |
| 177 | +Miniflare creator understand this. |
0 commit comments