Socket
Socket
Sign inDemoInstall

miniflare

Package Overview
Dependencies
Maintainers
0
Versions
199
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


Version published
Weekly downloads
735K
increased by2.17%
Maintainers
0
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 25 Jun 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