@miniflare/core
TypeScript icon, indicating that this package has built-in type declarations

2.14.2 • Public • Published

@miniflare/core

Core module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers. See 🧰 Using the API for more details.

Example

import { CorePlugin, MiniflareCore } from "@miniflare/core";
import { KVPlugin } from "@miniflare/kv";
import { VMScriptRunner } from "@miniflare/runner-vm";
import { Log, LogLevel } from "@miniflare/shared";
import { MemoryStorage } from "@miniflare/storage-memory";

export class StorageFactory {
  storages = new Map();

  storage(namespace) {
    let storage = this.storages.get(namespace);
    if (storage) return storage;
    this.storages.set(namespace, (storage = new MemoryStorage()));
    return storage;
  }
}

const plugins = { CorePlugin, KVPlugin };
const ctx = {
  log: new Log(LogLevel.INFO),
  storageFactory: new StorageFactory(),
  scriptRunner: new VMScriptRunner(),
};

const mf = new MiniflareCore(plugins, ctx, {
  modules: true,
  script: `export default {
    async fetch(request, env) {
      return new Response(await env.TEST_NAMESPACE.get("key"));
    }
  }`,
  kvNamespaces: ["TEST_NAMESPACE"],
});

const { TEST_NAMESPACE } = await mf.getBindings();
await TEST_NAMESPACE.put("key", "value");

const res = await mf.dispatchFetch("http://localhost");
console.log(await res.text()); // value

Dependencies (10)

Dev Dependencies (7)

Package Sidebar

Install

npm i @miniflare/core

Weekly Downloads

117,644

Version

2.14.2

License

MIT

Unpacked Size

228 kB

Total Files

7

Last publish

Collaborators

  • wrangler-publisher