
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@miniflare/core
Advanced tools
Core module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers
@miniflare/core is a package designed to provide a local development environment for Cloudflare Workers. It allows developers to simulate the Cloudflare Workers runtime locally, enabling easier testing and debugging of serverless functions.
Simulate Cloudflare Workers
This feature allows you to simulate Cloudflare Workers locally. The code sample demonstrates how to create a Miniflare instance with a simple worker script that responds with 'Hello from Miniflare!' to any fetch event.
const { Miniflare } = require('@miniflare/core');
const mf = new Miniflare({
script: `addEventListener('fetch', event => {
event.respondWith(new Response('Hello from Miniflare!'));
})`
});
(async () => {
const res = await mf.dispatchFetch('http://localhost/');
console.log(await res.text()); // Hello from Miniflare!
})();
KV Storage Simulation
This feature allows you to simulate Cloudflare Workers KV storage. The code sample demonstrates how to create a Miniflare instance with a worker script that retrieves a value from a KV namespace and responds with it.
const { Miniflare } = require('@miniflare/core');
const mf = new Miniflare({
script: `addEventListener('fetch', async event => {
const value = await MY_KV_NAMESPACE.get('key');
event.respondWith(new Response(value));
})`,
kvNamespaces: ['MY_KV_NAMESPACE']
});
(async () => {
await mf.getKVNamespace('MY_KV_NAMESPACE').put('key', 'value');
const res = await mf.dispatchFetch('http://localhost/');
console.log(await res.text()); // value
})();
Durable Objects Simulation
This feature allows you to simulate Cloudflare Workers Durable Objects. The code sample demonstrates how to create a Miniflare instance with a worker script that increments a counter stored in a Durable Object and responds with the current count.
const { Miniflare } = require('@miniflare/core');
const mf = new Miniflare({
script: `class Counter {
constructor(state, env) {
this.state = state;
this.state.blockConcurrencyWhile(async () => {
this.value = (await this.state.storage.get('value')) || 0;
});
}
async fetch(request) {
this.value++;
await this.state.storage.put('value', this.value);
return new Response(this.value);
}
}
registerDurableObject('Counter', Counter);
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const id = new URL(request.url).pathname.slice(1);
const counter = Counter.get(id);
return counter.fetch(request);
}`,
durableObjects: { Counter: 'Counter' }
});
(async () => {
const res1 = await mf.dispatchFetch('http://localhost/1');
console.log(await res1.text()); // 1
const res2 = await mf.dispatchFetch('http://localhost/1');
console.log(await res2.text()); // 2
})();
Wrangler is the official CLI tool for managing Cloudflare Workers. It provides functionalities for developing, testing, and deploying Cloudflare Workers. Unlike @miniflare/core, which focuses on local simulation, Wrangler offers a more comprehensive set of tools for the entire development lifecycle, including deployment to Cloudflare's network.
Worktop is a lightweight framework for building Cloudflare Workers. It provides utilities for routing, handling requests, and managing responses. While @miniflare/core focuses on simulating the Cloudflare Workers environment locally, Worktop is more about providing a structured way to build and organize your worker scripts.
@miniflare/core
Core module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers. See 🧰 Using the API for more details.
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
FAQs
Core module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers
The npm package @miniflare/core receives a total of 113,919 weekly downloads. As such, @miniflare/core popularity was classified as popular.
We found that @miniflare/core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.