Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

miniflare

Package Overview
Dependencies
Maintainers
1
Versions
466
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

miniflare

Fun, full-featured, fully-local simulator for Cloudflare Workers

  • 0.0.0-97110195a
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
371K
decreased by-52.21%
Maintainers
1
Weekly downloads
 
Created

What is miniflare?

Miniflare is a simulator for Cloudflare Workers, allowing you to develop and test your Workers locally. It provides a local environment that mimics the Cloudflare Workers runtime, enabling you to test your code without deploying it to the cloud.

What are miniflare's main functionalities?

Local Development

Miniflare allows you to run and test your Cloudflare Workers locally. This example demonstrates how to create a simple worker that responds with 'Hello from Miniflare!' to any request.

const { Miniflare } = require('miniflare');

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

Miniflare can simulate Cloudflare Workers KV storage, allowing you to test interactions with KV namespaces locally. This example shows how to store and retrieve a value from a KV namespace.

const { Miniflare } = require('miniflare');

const mf = new Miniflare({
  script: `addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
  });

  async function handleRequest(request) {
    const value = await MY_KV_NAMESPACE.get('key');
    return 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

Miniflare supports simulating Durable Objects, which are used for stateful logic in Cloudflare Workers. This example demonstrates a simple counter Durable Object that increments its value with each request.

const { Miniflare } = require('miniflare');

const mf = new Miniflare({
  script: `class Counter {
    constructor(state, env) {
      this.state = state;
      this.env = env;
    }

    async fetch(request) {
      let value = (await this.state.storage.get('value')) || 0;
      value++;
      await this.state.storage.put('value', value);
      return new Response(value);
    }
  }

  addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
  });

  async function handleRequest(request) {
    const id = 'counter';
    const stub = await COUNTER.get(id);
    return stub.fetch(request);
  }`,
  durableObjects: { COUNTER: 'Counter' }
});

(async () => {
  const res1 = await mf.dispatchFetch('http://localhost');
  console.log(await res1.text()); // 1
  const res2 = await mf.dispatchFetch('http://localhost');
  console.log(await res2.text()); // 2
})();

Other packages similar to miniflare

Keywords

FAQs

Package last updated on 15 Oct 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc