1
- # 🔥 Miniflare (WIP)
1
+ # 🔥 Miniflare
2
2
3
- Fun, full-featured, fully-local simulator for developing and testing Cloudflare Workers
3
+ Fun, full-featured, fully-local simulator for developing and testing Cloudflare
4
+ Workers
5
+
6
+ ** See < https://miniflare.dev > for many more details.**
4
7
5
8
## Features
6
9
10
+ - 📨 Fetch Events (with HTTP server and manual triggering)
11
+ - ⏰ Scheduled Events (with manual and cron triggering)
12
+ - 🔑 Variables and Secrets with ` .env ` Files
13
+ - 📚 Modules Support
7
14
- 📦 KV (with optional persistence)
8
15
- ✨ Cache (with optional persistence)
9
16
- 📌 Durable Objects (with optional persistence)
10
17
- 🌐 Workers Sites
11
- - 📨 Fetch Events (with HTTP server and manual triggering)
12
- - ⏰ Scheduled Events (with manual and cron triggering)
13
18
- ✉️ WebSockets
14
- - 📄 HTMLRewriter
15
- - 🔑 ` .env ` File Support (for secrets)
16
- - 🕸 Web Standards: Base64, Timers, Fetch, Encoding, URL, Streams, Crypto
17
- - 📚 Modules Support
18
- - 🛠 Custom Builds Support
19
+ - 🛠 Custom & Wrangler Builds Support
19
20
- ⚙️ WebAssembly Support
20
21
- 🗺 Source Map Support
22
+ - 🕸 Web Standards: Base64, Timers, Fetch, Encoding, URL, Streams, Crypto
23
+ - 📄 HTMLRewriter
21
24
- 👀 Automatic Reload on File Changes
22
25
- 💪 Written in TypeScript
23
26
24
- ## CLI Usage
27
+ ## Install
28
+
29
+ Miniflare is installed using npm:
30
+
31
+ ``` shell
32
+ $ npm install -g miniflare # either globally..
33
+ $ npm install -D miniflare # ...or as a dev dependency
34
+ ```
35
+
36
+ ## Using the CLI
37
+
38
+ ``` shell
39
+ $ miniflare worker.js --watch --debug
40
+ [mf:dbg] Options:
41
+ [mf:dbg] - Scripts: worker.js
42
+ [mf:dbg] Reloading worker.js...
43
+ [mf:inf] Worker reloaded!
44
+ [mf:dbg] Watching .env, worker.js, wrangler.toml...
45
+ [mf:inf] Listening on :8787
46
+ [mf:inf] - http://127.0.0.1:8787
47
+ ```
48
+
49
+ ## Using the API
50
+
51
+ ``` js
52
+ import { Miniflare } from " miniflare" ;
53
+
54
+ const mf = new Miniflare ({
55
+ script: `
56
+ addEventListener("fetch", (event) => {
57
+ event.respondWith(new Response("Hello Miniflare!"));
58
+ });
59
+ ` ,
60
+ });
61
+ const res = await mf .dispatchFetch (" http://localhost:8787/" );
62
+ console .log (await res .text ()); // Hello Miniflare!
63
+ ```
64
+
65
+ ## CLI Reference
25
66
26
67
```
27
68
Usage: miniflare [script] [options]
@@ -34,8 +75,8 @@ Options:
34
75
-d, --debug Log debug messages [boolean]
35
76
-c, --wrangler-config Path to wrangler.toml [string]
36
77
--wrangler-env Environment in wrangler.toml to use [string]
37
- -m, --modules Enable ES modules [boolean]
38
- --modules-rule ES modules import rule (TYPE=GLOB) [array]
78
+ -m, --modules Enable modules [boolean]
79
+ --modules-rule Modules import rule (TYPE=GLOB) [array]
39
80
--build-command Command to build project [string]
40
81
--build-base-path Working directory for build command [string]
41
82
--build-watch-path Directory to watch for rebuilding on changes [string]
@@ -56,115 +97,10 @@ Options:
56
97
--wasm WASM module to bind (NAME=PATH) [array]
57
98
```
58
99
59
- ` [script] ` should be a path to a pre-bundled Worker.
60
- If you're using Webpack for instance, set this to your output file.
61
-
62
- ** (Recommended)** Use ` --debug ` /` -d ` to see additional log messages including processed options and watched files.
63
-
64
- ** (Recommended)** Use ` --wrangler-config <toml_path> ` /` -c <toml_path> ` to load options for KV, cache, etc from a ` wrangler.toml ` file.
65
- If ` [script] ` is omitted, Miniflare tries to automatically infer it from the ` wrangler.toml ` file.
66
- You can also include an additional ` [miniflare] ` section for Miniflare specific configuration:
67
-
68
- ``` toml
69
- [miniflare ]
70
- host = " 127.0.0.1" # --host
71
- port = 8787 # --port
72
- upstream = " https://mrbbot.dev" # --upstream
73
- kv_persist = true # --kv-persist
74
- cache_persist = true # --cache-persist
75
- durable_objects_persist = true # --do-persist
76
- env_path = " .env" # --env
77
- wasm_bindings = [ # --wasm
78
- { name = " MODULE" , path =" module.wasm" }
79
- ]
80
- ```
81
-
82
- KV and cache persistence can be enabled with the ` --kv-persist ` and ` --cache-persist ` flags respectively.
83
- Including these on their own will store KV and Cache data in the ` .mf ` directory.
84
- Optionally, you can specify a path (e.g. ` --kv-persist ./data ` ) to use a different location.
85
-
86
- ## Programmatic Usage
87
-
88
- ``` javascript
89
- import { ConsoleLog , Miniflare , Request } from " miniflare" ;
90
-
91
- // Loading script from file
92
- const mf = new Miniflare ({
93
- // Some options omitted, see src/options/index.ts for the full list
94
- scriptPath: " ./path/to/script.js" ,
95
- sourceMap: true ,
96
- log: new ConsoleLog (), // Defaults to no-op logger
97
- wranglerConfigPath: " wrangler.toml" ,
98
- watch: true ,
99
- port: 8787 ,
100
- upstream: " https://mrbbot.dev" ,
101
- crons: [" 0 * * * *" ],
102
- kvNamespaces: [" TEST_NAMESPACE" ],
103
- kvPersist: true ,
104
- cachePersist: " ./data/" ,
105
- durableObjects: {
106
- OBJECT : " ObjectClass" ,
107
- },
108
- sitePath: " ./public/" ,
109
- envPath: " .env" ,
110
- });
111
-
112
- // Loading script from string
113
- const mf = new Miniflare ({
114
- script: `
115
- addEventListener("fetch", (event) => {
116
- event.respondWith(handleRequest(event.request));
117
- event.waitUntil(Promise.resolve("Something"));
118
- });
119
-
120
- async function handleRequest(request) {
121
- const value = await TEST_NAMESPACE.get("key");
122
- return new Response(\` Hello from Miniflare! key="\$ {value}"\` , {
123
- headers: { "content-type": "text/plain" },
124
- })
125
- }
126
-
127
- addEventListener("scheduled", (event) => {
128
- event.waitUntil(Promise.resolve("Something else"));
129
- });
130
- ` ,
131
- kvNamespaces: [" TEST_NAMESPACE" ],
132
- log: new ConsoleLog (),
133
- });
134
-
135
- // Manipulate KV outside of worker (useful for testing)
136
- const TEST_NAMESPACE = await mf .getKVNamespace (" TEST_NAMESPACE" );
137
- await TEST_NAMESPACE .put (" key" , " testing" );
138
-
139
- // Manipulate cache outside of worker
140
- const cache = await mf .getCache ();
141
- const cachedRes = await cache .match (new Request (" http://localhost" ));
142
-
143
- // Manipulate Durable Objects outside of worker
144
- const OBJECT = await mf .getDurableObjectNamespace (" OBJECT" );
145
- const stub = OBJECT .get (OBJECT .idFromName (" name" ));
146
- const doRes = await stub .fetch (" http://localhost" );
147
- const storage = await stub .storage (); // Extra Miniflare-only API
148
- await storage .put (" key" , new Set ([" testing" ]));
149
-
150
- // Dispatch fetch events and get body
151
- const res = await mf .dispatchFetch (" http://localhost" , { method: " POST" });
152
- const res = await mf .dispatchFetch (new Request (" http://localhost" ));
153
-
154
- const body = await res .text ();
155
- console .log (body); // Hello from Miniflare! key="testing"
156
-
157
- const waitUntil = await res .waitUntil ();
158
- console .log (waitUntil[0 ]); // Something
159
-
160
- // Start HTTP server
161
- mf .createServer ().listen (3000 );
162
-
163
- // Dispatch scheduled event at specific time
164
- const waitUntil2 = await mf .dispatchScheduled (Date .now ());
165
- console .log (waitUntil2[0 ]); // Something else
166
- ```
167
-
168
100
## Acknowledgements
169
101
170
- Many thanks to [ dollarshaveclub/cloudworker] ( https://github.com/dollarshaveclub/cloudworker ) and [ gja/cloudflare-worker-local] ( https://github.com/gja/cloudflare-worker-local ) for inspiration.
102
+ Many thanks to
103
+ [ dollarshaveclub/cloudworker] ( https://github.com/dollarshaveclub/cloudworker )
104
+ and
105
+ [ gja/cloudflare-worker-local] ( https://github.com/gja/cloudflare-worker-local )
106
+ for inspiration.
0 commit comments