@@ -98,6 +98,46 @@ const mf = new Miniflare({ ... });
9898await mf .reload ();
9999```
100100
101+ Miniflare will emit a ` reload ` event whenever it reloads too:
102+
103+ ``` js
104+ const mf = new Miniflare ({ ... });
105+ mf .addEventListener (" reload" , (event ) => {
106+ console .log (" Worker reloaded!" );
107+ });
108+ ```
109+
110+ ### Updating Options and the Global Scope
111+
112+ You can use the ` setOptions ` method to update the options of an existing
113+ ` Miniflare ` instance. This accepts the same options object as the
114+ ` new Miniflare ` constructor, applies those options, then reloads the worker.
115+
116+ ``` js
117+ const mf = new Miniflare ({
118+ script: " ..." ,
119+ kvNamespaces: [" TEST_NAMESPACE" ],
120+ bindings: { KEY : " value1" },
121+ });
122+
123+ // Only updates `bindings`, leaves `script` and `kvNamespaces` alone
124+ await mf .setOptions ({
125+ bindings: { KEY : " value2" },
126+ });
127+ ```
128+
129+ You can also access the global scope of the sandbox directly using the
130+ ` getGlobalScope ` method. Ideally, use should use the ` setOptions ` method when
131+ updating the environment dynamically though:
132+
133+ ``` js
134+ const mf = new Miniflare ({
135+ globals: { KEY : " value1" },
136+ });
137+ const globalScope = await mf .getGlobalScope ();
138+ globalScope .KEY = " value2" ;
139+ ```
140+
101141### Dispatching Events
102142
103143` dispatchFetch ` and ` dispatchScheduled ` dispatch ` fetch ` and ` scheduled ` events
@@ -138,7 +178,7 @@ returns a
138178[ Node.js ` http.Server ` ] ( https://nodejs.org/api/http.html#http_class_http_server )
139179instance:
140180
141- ``` js{10 }
181+ ``` js{11 }
142182import { Miniflare } from "miniflare";
143183
144184const mf = new Miniflare({
@@ -153,6 +193,52 @@ const server = await mf.startServer();
153193console.log("Listening on :5000");
154194```
155195
196+ You can also just create the server with ` createServer ` and start it yourself.
197+ Note that you're then responsible for setting the correct host and port:
198+
199+ ``` js
200+ const mf = new Miniflare ({
201+ script: " ..." ,
202+ port: 5000 ,
203+ });
204+ const server = await mf .createServer ();
205+ const { HTTPPlugin } = await mf .getPlugins ();
206+ server .listen (HTTPPlugin .port , () => {
207+ console .log (` Listening on :${ HTTPPlugin .port } ` );
208+ });
209+ ```
210+
211+ #### ` Request#cf ` Object
212+
213+ By default, Miniflare will fetch the ` Request#cf ` object from a trusted
214+ Cloudflare endpoint. You can disable this behaviour, using the ` cfFetch ` option:
215+
216+ ``` js
217+ const mf = new Miniflare ({
218+ cfFetch: false ,
219+ });
220+ ```
221+
222+ You can also provide a custom request metadata provider, which takes the
223+ incoming Node request and may look-up information in a geo-IP database:
224+
225+ ``` js
226+ const mf = new Miniflare ({
227+ async metaProvider (req ) {
228+ return {
229+ forwardedProto: req .headers [" X-Forwarded-Proto" ],
230+ realIp: req .headers [" X-Forwarded-For" ],
231+ cf: {
232+ // Could get these from a geo-IP database
233+ colo: " SFO" ,
234+ country: " US" ,
235+ // ...
236+ },
237+ };
238+ },
239+ });
240+ ```
241+
156242### HTTPS Server
157243
158244To start an HTTPS server instead, set the ` https ` option. To use an
@@ -197,6 +283,21 @@ const mf = new Miniflare({
197283If both a string and path are specified for an option (e.g. ` httpsKey ` and
198284` httpsKeyPath ` ), the string will be preferred.
199285
286+ ### CRON Scheduler
287+
288+ To start a CRON scheduler like the CLI's, use the ` startScheduler ` method. This
289+ will dispatch ` scheduled ` events according to the specified CRON expressions:
290+
291+ ``` js
292+ const mf = new Miniflare ({
293+ crons: [" 30 * * * *" ],
294+ });
295+ const scheduler = await mf .startScheduler ();
296+ // ...
297+ // Stop dispatching events
298+ await scheduler .dispose ();
299+ ```
300+
200301### Logging
201302
202303By default, ` [mf:*] ` logs as seen in the CLI are disabled when using the API. To
0 commit comments