New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@miniflare/core

Package Overview
Dependencies
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@miniflare/core

Core module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers

  • 2.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
134K
increased by6.1%
Maintainers
2
Weekly downloads
 
Created

What is @miniflare/core?

@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.

What are @miniflare/core's main functionalities?

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
})();

Other packages similar to @miniflare/core

Keywords

FAQs

Package last updated on 27 May 2022

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