Edge Runtime Simulator

4.0.1 · active · verified Wed Apr 22

edge-runtime is a JavaScript/TypeScript library, currently at version 4.0.1, developed by Vercel. It provides a robust, spec-compliant environment for simulating Vercel Edge Functions or any Web-standard compliant JavaScript code within Node.js or a CLI. This enables local development, testing, and debugging of Edge Functions without requiring actual deployment, significantly streamlining the development workflow. The library aims to faithfully replicate the Edge Function environment, offering Web APIs like `fetch`, `Request`, `Response`, and `URL` as globals within its isolated context. It maintains an active release cadence, with major versions typically aligning with Node.js LTS updates or substantial API enhancements, as demonstrated by the v4.0.0 release which raised the minimum Node.js version requirement. Its core differentiation lies in providing a high-fidelity simulation of the Vercel Edge context.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing an EdgeRuntime instance, injecting global variables, evaluating Edge Function code, and simulating a fetch event to receive a response.

import { EdgeRuntime } from 'edge-runtime';

async function runEdgeFunctionSimulation() {
  // Instantiate the Edge Runtime, optionally providing initial globals
  const runtime = new EdgeRuntime({
    initialGlobals: {
      // Simulate environment variables or other global objects
      MY_SECRET_KEY: process.env.MY_SECRET_KEY ?? 'default-secret',
      SOME_CONFIG: { foo: 'bar' }
    }
  });

  // Define your Edge Function code as a string
  const edgeFunctionCode = `
    addEventListener('fetch', event => {
      const url = new URL(event.request.url);

      if (url.pathname === '/api/greet') {
        event.respondWith(new Response('Hello from the Edge!', {
          status: 200,
          headers: { 'content-type': 'text/plain' }
        }));
      } else if (url.pathname === '/api/config') {
        event.respondWith(new Response(JSON.stringify(SOME_CONFIG), {
          status: 200,
          headers: { 'content-type': 'application/json' }
        }));
      } else if (url.pathname === '/api/secret') {
        event.respondWith(new Response(MY_SECRET_KEY, {
          status: 200,
          headers: { 'content-type': 'text/plain' }
        }));
      } else {
        event.respondWith(new Response('Not Found', { status: 404 }));
      }
    });
  `;

  // Evaluate the Edge Function code within the simulated runtime
  runtime.evaluate(edgeFunctionCode);

  // Simulate an incoming fetch request
  const request1 = new runtime.Request('https://example.com/api/greet');
  const response1 = await runtime.dispatchFetch(request1);
  console.log('Response 1 Status:', response1.status);
  console.log('Response 1 Body:', await response1.text());

  const request2 = new runtime.Request('https://example.com/api/config');
  const response2 = await runtime.dispatchFetch(request2);
  console.log('Response 2 Status:', response2.status);
  console.log('Response 2 Body:', await response2.json());

  const request3 = new runtime.Request('https://example.com/api/secret');
  const response3 = await runtime.dispatchFetch(request3);
  console.log('Response 3 Status:', response3.status);
  console.log('Response 3 Body:', await response3.text());
}

runEdgeFunctionSimulation().catch(console.error);

view raw JSON →