Skip to content

Search docs

Jump between documentation pages.

Browse docs

Fastly Compute

Fastly Compute (JavaScript) still uses the fetch event listener model rather than the modules-style default export you see on Cloudflare Workers. The DaloyJS adapter wraps that registration so you only call one function.

Fastly Compute request path
  1. platformfetch eventaddEventListener('fetch', ...)
  2. adapterinstallFastlyListener(app)event.respondWith(app.fetch(...))
  3. coreapp.fetch(Request)routing · hooks · handler
  4. platformResponse
Fastly Compute still uses the fetch-event listener model. installFastlyListener registers it for you and forwards event.request into app.fetch. Outbound fetch() calls must be declared as backends in fastly.toml.

When to choose Fastly Compute

  • You already use Fastly's edge network and want REST API logic on the same plane.
  • You want WebAssembly-compiled JS (js-compute-runtime) for tight cold starts.
  • You're comfortable without node:* modules.

Install

bash
pnpm add @daloyjs/core @fastly/js-compute
pnpm add -D @fastly/cli

Entrypoint

ts
// src/index.ts
/// <reference types="@fastly/js-compute" />
import { installFastlyListener } from "@daloyjs/core/fastly";
import { app } from "./server.js";

installFastlyListener(app);

That helper is equivalent to:

ts
import { toFastlyHandler } from "@daloyjs/core/fastly";

const handler = toFastlyHandler(app);
addEventListener("fetch", (event) =>
  event.respondWith(handler(event.request, event.waitUntil.bind(event)))
);

Passing event.waitUntil keeps the instance alive until OTLP telemetry is exported after the response. installFastlyListener does that for you. Without it the flush still starts, but Fastly may stop the instance before it finishes.

fastly.toml

Fastly Compute requires manifest_version = 3. The [scripts] build command has to bundle TypeScript to a single JS file first, then run js-compute-runtime against that bundle, the runtime only accepts JavaScript input.

toml
manifest_version = 3
name = "my-api"
language = "javascript"
description = "DaloyJS on Fastly Compute"

[scripts]
# Bundle src/index.ts -> bin/index.js -> bin/main.wasm
build = "esbuild src/index.ts --bundle --format=esm --platform=neutral --outfile=bin/index.js && js-compute-runtime bin/index.js bin/main.wasm"

# Declare every outbound HTTP service your REST API calls as a backend.
# Fastly Compute blocks arbitrary fetch() calls without a declared backend.
[local_server]
  [local_server.backends.auth-service]
    url = "https://auth.internal.example.com"

Declared backends, KV stores, config stores, and secrets live under the same [local_server.*] /[setup.*] tables, see the fastly.toml reference for the full schema.

Deploy

bash
pnpm fastly compute serve     # local dev
pnpm fastly compute publish    # deploy

Client IP

installFastlyListener(app) passes Fastly's event.client.address to the App as the trusted peer address, so IP-keyed guards (rateLimit(), autoBan(), ipRestriction()) identify the real client. Declare that nothing sits in front of Compute:

src/app.tsts
import { App } from "@daloyjs/core";

export const app = new App({ behindProxy: "none" });

Do not use behindProxy: { hops: 1 } here: Compute hands your code the client's own X-Forwarded-For, so that header is client-controlled. If you call toFastlyHandler(app) yourself, pass the address as the third argument:

ts
const handler = toFastlyHandler(app);
addEventListener("fetch", (event) =>
  event.respondWith(handler(event.request, event.waitUntil.bind(event), event.client.address)),
);

A value that is not an IP address is ignored.

Gotchas

  • No node:* modules. Avoid the Node session store, the Redis rate-limit store, and multipart helpers that depend on node:stream: use the fetch-based alternatives.
  • Every outbound HTTP call your REST API makes (to a database API, auth service, or third-party endpoint) must be declared as a backend in fastly.toml. Arbitrary fetch("https://...") calls fail at runtime without one.
  • KV stores, config stores, and secrets are also declared in fastly.toml under [setup].

See also

Edit this page on GitHub

Last updated