@@ -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
5252const 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
9191in Redis to close opened connections.
9292
9393You 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
9797const 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
148137returns a
149138[ Node.js ` http.Server ` ] ( https://nodejs.org/api/http.html#http_class_http_server )
150139instance:
@@ -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
200165const mf = new Miniflare ({
@@ -207,54 +172,50 @@ To load an existing certificate from the file system:
207172
208173``` js
209174const 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
221184To load an existing certificate from strings instead:
222185
223186``` js
224187const 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
241202By 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
249210const 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
258219scope 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});
0 commit comments