or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdindex.mdruntime.mdserver.md
tile.json

index.mddocs/

Edge Runtime

Edge Runtime is a comprehensive runtime environment for executing Edge Functions with Web API compatibility and Node.js integration. It provides both CLI tools and Node.js module interfaces for running serverless functions that conform to Web API standards, making it ideal for edge computing scenarios with fast cold starts and minimal resource overhead.

Package Information

  • Package Name: edge-runtime
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install edge-runtime
  • Node.js Requirement: >=18

Core Imports

import { EdgeRuntime, createHandler, runServer } from "edge-runtime";

For CommonJS:

const { EdgeRuntime, createHandler, runServer } = require("edge-runtime");

Stream utilities:

import { consumeUint8ArrayReadableStream, pipeBodyStreamToResponse } from "edge-runtime";

Basic Usage

import { EdgeRuntime } from "edge-runtime";

// Create a runtime instance with edge function code
const runtime = new EdgeRuntime({
  initialCode: `
    addEventListener('fetch', event => {
      const response = new Response('Hello from Edge Runtime!', {
        headers: { 'content-type': 'text/plain' }
      });
      event.respondWith(response);
    });
  `
});

// Dispatch a fetch event to the runtime
const response = await runtime.dispatchFetch('https://example.com');
console.log(await response.text()); // "Hello from Edge Runtime!"

Architecture

Edge Runtime is built around several key components:

  • EdgeRuntime Class: Core VM for executing Edge Functions with Web API globals
  • Server Integration: HTTP server utilities for Node.js integration with request/response handling
  • CLI Interface: Command-line tools for development, testing, and deployment
  • Stream Processing: Utilities for handling request/response body streams with backpressure
  • Web API Compatibility: Full support for standard Web APIs (fetch, Request, Response, etc.)

Capabilities

Edge Function Runtime

Core runtime environment for executing Edge Functions with complete Web API compatibility and isolated execution context.

class EdgeRuntime<T extends EdgeContext = EdgeContext> {
  constructor(options?: EdgeVMOptions<T>);
  evaluate<T>(code: string): T;
  dispatchFetch(input: string, init?: RequestInit): Promise<Response & { waitUntil: () => Promise<any> }>;
  readonly context: EdgeContext & T;
}

interface EdgeVMOptions<T extends EdgeContext> {
  codeGeneration?: VMOptions<T>['codeGeneration'];
  extend?: (context: EdgeContext) => EdgeContext & T;
  initialCode?: string;
}

Edge Runtime VM

HTTP Server Integration

Server utilities for integrating Edge Runtime with Node.js HTTP servers, providing request handling and response streaming.

function createHandler<T extends EdgeContext>(options: {
  logger?: Logger;
  runtime: EdgeRuntime<T>;
}): {
  handler: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
  waitUntil: () => Promise<unknown[]>;
};

function runServer<T extends EdgeContext>(
  options: ListenOptions & { logger?: Logger; runtime: EdgeRuntime<T> }
): Promise<EdgeRuntimeServer>;

interface EdgeRuntimeServer {
  url: string;
  close: () => Promise<void>;
  waitUntil: () => Promise<any[]>;
}

Server Integration

CLI Interface

Command-line interface for executing Edge Functions, starting development servers, and interactive REPL sessions.

edge-runtime [options] [script]

# Execute script file
edge-runtime my-function.js

# Run as HTTP server
edge-runtime --listen --port 3000 my-function.js

# Evaluate inline code
edge-runtime --eval "console.log('Hello World')"

# Start interactive REPL
edge-runtime

CLI Commands

Types

interface EdgeContext {
  // Core globals
  self: EdgeContext;
  globalThis: EdgeContext;
  EdgeRuntime: string;
  
  // Web API globals
  fetch: typeof fetch;
  Request: typeof Request;
  Response: typeof Response;
  Headers: typeof Headers;
  URL: typeof URL;
  URLSearchParams: typeof URLSearchParams;
  URLPattern: typeof URLPattern;
  
  // Crypto APIs
  crypto: typeof crypto;
  Crypto: typeof Crypto;
  CryptoKey: typeof CryptoKey;
  SubtleCrypto: typeof SubtleCrypto;
  
  // Stream APIs
  ReadableStream: typeof ReadableStream;
  ReadableStreamBYOBReader: typeof ReadableStreamBYOBReader;
  ReadableStreamDefaultReader: typeof ReadableStreamDefaultReader;
  WritableStream: typeof WritableStream;
  WritableStreamDefaultWriter: typeof WritableStreamDefaultWriter;
  TransformStream: typeof TransformStream;
  
  // Text encoding
  TextEncoder: typeof TextEncoder;
  TextDecoder: typeof TextDecoder;
  TextEncoderStream: typeof TextEncoderStream;
  TextDecoderStream: typeof TextDecoderStream;
  atob: typeof atob;
  btoa: typeof btoa;
  
  // Events
  Event: typeof Event;
  EventTarget: typeof EventTarget;
  FetchEvent: typeof FetchEvent;
  PromiseRejectionEvent: typeof PromiseRejectionEvent;
  
  // Abort controllers
  AbortController: typeof AbortController;
  AbortSignal: typeof AbortSignal;
  DOMException: typeof DOMException;
  
  // File and form APIs
  Blob: typeof Blob;
  File: typeof File;
  FormData: typeof FormData;
  
  // Timers
  setTimeout: typeof setTimeout;
  setInterval: typeof setInterval;
  
  // Utilities
  console: typeof console;
  structuredClone: typeof structuredClone;
  
  // Standard JavaScript globals (inherited from VMContext)
  Array: typeof Array;
  Object: typeof Object;
  String: typeof String;
  Number: typeof Number;
  Boolean: typeof Boolean;
  Date: typeof Date;
  RegExp: typeof RegExp;
  Promise: typeof Promise;
  Map: typeof Map;
  Set: typeof Set;
  WeakMap: typeof WeakMap;
  WeakSet: typeof WeakSet;
  Symbol: typeof Symbol;
  Proxy: typeof Proxy;
  Reflect: typeof Reflect;
  JSON: typeof JSON;
  Math: typeof Math;
  Intl: typeof Intl;
  // ... and all other standard JavaScript globals
}

interface Logger {
  (message: string, opts?: LoggerOptions): void;
  warn(message: string, opts?: LoggerOptions): void;
  debug(message: string, opts?: LoggerOptions): void;
  error(message: string, opts?: LoggerOptions): void;
  info(message: string, opts?: LoggerOptions): void;
  quotes(str: string): string;
}

interface LoggerOptions {
  color?: keyof Colors;
  withHeader?: boolean;
  withBreakline?: boolean;
}

interface Colors {
  reset: string;
  bold: string;
  dim: string;
  italic: string;
  underline: string;
  inverse: string;
  hidden: string;
  strikethrough: string;
  black: string;
  red: string;
  green: string;
  yellow: string;
  blue: string;
  magenta: string;
  cyan: string;
  white: string;
  gray: string;
  grey: string;
  blackBright: string;
  redBright: string;
  greenBright: string;
  yellowBright: string;
  blueBright: string;
  magentaBright: string;
  cyanBright: string;
  whiteBright: string;
  bgBlack: string;
  bgRed: string;  
  bgGreen: string;
  bgYellow: string;
  bgBlue: string;
  bgMagenta: string;
  bgCyan: string;
  bgWhite: string;
  bgGray: string;
  bgGrey: string;
  bgBlackBright: string;
  bgRedBright: string;
  bgGreenBright: string;
  bgYellowBright: string;
  bgBlueBright: string;
  bgMagentaBright: string;
  bgCyanBright: string;
  bgWhiteBright: string;
}

interface NodeHeaders {
  [header: string]: string | string[] | undefined;
}

interface ListenOptions {
  port?: number;
  host?: string;
  backlog?: number;
  path?: string;
  exclusive?: boolean;
  readableAll?: boolean;
  writableAll?: boolean;
  ipv6Only?: boolean;
}