@@ -335,3 +335,154 @@ const mf = new Miniflare({
335335const res = await mf.dispatchFetch("http://localhost:8787/");
336336console.log(await res.text()); // Hello Miniflare!
337337```
338+
339+ ## Reference
340+
341+ ``` js
342+ import { Miniflare , Log , LogLevel } from " miniflare" ;
343+
344+ const mf = new Miniflare ({
345+ // All options are optional, but one of script or scriptPath is required
346+
347+ log: new Log (LogLevel .INFO ), // Logger Miniflare uses for debugging
348+ sourceMap: true , // Enable source map support globally
349+
350+ script: ` export default {
351+ async fetch(request, env, ctx) {
352+ return new Response("Hello Miniflare!");
353+ }
354+ }` ,
355+ scriptPath: " ./index.mjs" ,
356+
357+ wranglerConfigPath: true , // Load configuration from wrangler.toml
358+ wranglerConfigPath: " ./wrangler.custom.toml" , // ...or custom wrangler.toml path
359+
360+ wranglerConfigEnv: " dev" , // Environment in wrangler.toml to use
361+
362+ packagePath: true , // Load script from package.json
363+ packagePath: " ./package.custom.json" , // ...or custom package.json path
364+
365+ modules: true , // Enable modules
366+ modulesRules: [
367+ // Modules import rule
368+ { type: " ESModule" , include: [" **/*.js" ], fallthrough: true },
369+ { type: " Text" , include: [" **/*.text" ] },
370+ ],
371+ compatibilityDate: " 2021-11-23" , // Opt into backwards-incompatible changes from
372+ compatibilityFlags: [" formdata_parser_supports_files" ], // Control specific backwards-incompatible changes
373+ upstream: " https://miniflare.dev" , // URL of upstream origin
374+ watch: true , // Watch files for changes
375+ mounts: {
376+ // Mount additional named workers
377+ api: " ./api" ,
378+ site: {
379+ rootPath: " ./site" , // Path to resolve files relative to
380+ scriptPath: " ./index.js" , // Resolved as ./site/index.js
381+ sitePath: " ./public" , // Resolved as ./site/public
382+ routes: [" site.mf/*" ], // Route requests matching site.mf/* to this worker
383+ },
384+ },
385+
386+ host: " 127.0.0.1" , // Host for HTTP(S) server to listen on
387+ port: 8787 , // Port for HTTP(S) server to listen on
388+ https: true , // Enable self-signed HTTPS (with optional cert path)
389+ httpsKey: " -----BEGIN RSA PRIVATE KEY-----..." ,
390+ httpsKeyPath: " ./key.pem" , // Path to PEM SSL key
391+ httpsCert: " -----BEGIN CERTIFICATE-----..." ,
392+ httpsCertPath: " ./cert.pem" , // Path to PEM SSL cert chain
393+ httpsCa: " ..." ,
394+ httpsCaPath: " ./ca.pem" , // Path to SSL trusted CA certs
395+ httpsPfx: " ..." ,
396+ httpsPfxPath: " ./pfx.pfx" , // Path to PFX/PKCS12 SSL key/cert chain
397+ httpsPassphrase: " pfx passphrase" , // Passphrase to decrypt SSL files
398+ cfFetch: " ./node_modules/.mf/cf.json" , // Path for cached Request cf object from Cloudflare
399+ async metaProvider (req ) {
400+ // Custom request metadata provider taking Node `http.IncomingMessage`
401+ return {
402+ forwardedProto: req .headers [" X-Forwarded-Proto" ],
403+ realIp: req .headers [" X-Forwarded-For" ],
404+ cf: {
405+ colo: " SFO" ,
406+ country: " US" ,
407+ // ...
408+ },
409+ };
410+ },
411+ liveReload: true , // Reload HTML pages whenever worker is reloaded
412+
413+ crons: [" 30 * * * *" ], // CRON expression for triggering scheduled events
414+
415+ buildCommand: " npm run build" , // Command to build project
416+ buildBasePath: " ./build" , // Working directory for build command
417+ buildWatchPaths: [" ./src" ], // Directory to watch for rebuilding on changes
418+
419+ kvNamespaces: [" TEST_NAMESPACE" ], // KV namespace to bind
420+ kvPersist: " ./kv-data" , // Persist KV data (to optional path)
421+
422+ durableObjects: {
423+ // Durable Object to bind
424+ TEST_OBJECT : " TestObject" , // className
425+ API_OBJECT : { className: " ApiObject" , scriptName: " api" },
426+ },
427+ durableObjectsPersist: " ./durable-objects-data" , // Persist Durable Object data (to optional path)
428+
429+ cache: false , // Enable default/named caches (enabled by default)
430+ cachePersist: " ./cache-data" , // Persist cached data (to optional path)
431+ cacheWarnUsage: true , // Warn on cache usage, for workers.dev subdomains
432+
433+ sitePath: " ./site" , // Path to serve Workers Site files from
434+ siteInclude: [" **/*.html" , " **/*.css" , " **/*.js" ], // Glob pattern of site files to serve
435+ siteExclude: [" node_modules" ], // Glob pattern of site files not to serve
436+
437+ envPath: true , // Load environment variables from .env
438+ envPath: " ./env.custom" , // ...or custom .env path
439+
440+ bindings: { SECRET : " sssh" }, // Binds variable/secret to environment
441+ globals: { LOG : () => console .log (" magic" ) }, // Binds variable/secret to global scope
442+ wasmBindings: { ADD_MODULE : " ./add.wasm" }, // WASM module to bind
443+ });
444+
445+ await mf .reload (); // Reload scripts and configuration files
446+
447+ await mf .setOptions ({ kvNamespaces: [" TEST_NAMESPACE2" ] }); // Apply options and reload
448+
449+ const globalScope = await mf .getGlobalScope (); // Get sandbox's global scope
450+ const bindings = await mf .getBindings (); // Get bindings (KV/Durable Object namespaces, variables, etc)
451+
452+ const exports = await mf .getModuleExports (); // Get exports from entry module
453+
454+ const mount = await mf .getMount (" api" ); // Get mounted MiniflareCore instance
455+ await mount .getBindings ();
456+
457+ // Dispatch "fetch" event to worker
458+ const res = await mf .dispatchFetch (" http://localhost:8787/" , {
459+ headers: { Authorization: " Bearer ..." },
460+ });
461+ const text = await res .text ();
462+ const resWaitUntil = await res .waitUntil (); // Get `waitUntil`ed promises
463+
464+ // Dispatch "scheduled" event to worker
465+ const waitUntil = await mf .dispatchScheduled (Date .now (), " 30 * * * *" );
466+
467+ const TEST_NAMESPACE = await mf .getKVNamespace (" TEST_NAMESPACE" );
468+
469+ const caches = await mf .getCaches (); // Get global `CacheStorage` instance
470+ const defaultCache = caches .default ;
471+ const namedCache = await caches .open (" name" );
472+
473+ // Get Durable Object namespace and storage for ID
474+ const TEST_OBJECT = await mf .getDurableObjectNamespace (" TEST_OBJECT" );
475+ const id = TEST_OBJECT .newUniqueId ();
476+ const storage = await mf .getDurableObjectStorage (id);
477+
478+ const server = await mf .createServer (); // Create http.Server instance
479+ server .listen (8787 , () => {});
480+
481+ const server2 = await mf .startServer (); // Create and start http.Server instance
482+ server2 .close ();
483+
484+ const scheduler = await mf .startScheduler (); // Start CRON scheduler
485+ await scheduler .dispose ();
486+
487+ await mf .dispose (); // Cleanup storage database connections and watcher
488+ ```
0 commit comments