@fastly/js-compute

中文文档
4.3.0发布于 2026/11/14Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 120.9万

JavaScript SDK and CLI for building JavaScript applications on Fastly Compute.

原始使用文档
文档目录8 节
  1. Getting Started
  2. Usage
  3. JavaScript Examples
  4. CLI Flags and configuration
  5. API documentation
  6. Security
  7. Changelog
  8. License

@fastly/js-compute

JavaScript SDK and CLI for building JavaScript applications on Fastly Compute.

npm version npm downloads per month

Getting Started

We recommend using the Fastly CLI to create, build, and deploy JavaScript Fastly Compute services, as described on the Fastly Developer Hub.

Detailed documentation for JavaScript Fastly Compute services is also available on Fastly Developer Hub.

Usage

JavaScript Examples

The Fastly Developer Hub has a collection of example JavaScript applications.

Here is a small example application:

/// <reference types="@fastly/js-compute" />

async function app(event) {
    const request = event.request;
    return new Response(`You made a request to ${request.url}`)
}

addEventListener("fetch", event => {
  event.respondWith(app(event));
});

CLI Flags and configuration

The CLI is typically invoked by the build script defined in your project's package.json scripts.

{
  "scripts": {
    "build": "js-compute-runtime src/index.js bin/main.wasm"
  }
}

The CLI is invoked as follows:

js-compute-runtime [OPTIONS] <input-js-file> <output-wasm-file>

Options can be specified on the command line or added to a persistent configuration file for the project. The CLI will search for a configuration starting from the current directory in the following order:

  • A fastlycompute property in package.json
  • .fastlycomputerc.json
  • fastlycompute.config.js
Click here to see full list

The CLI will search for a configuration starting from the current directory in the following order:

  • A fastlycompute property in package.json
  • .fastlycomputerc (JSON or YAML)
  • .fastlycomputerc.json, .fastlycomputerc.yaml, .fastlycomputerc.yml
  • .fastlycomputerc.js, .fastlycomputerc.mjs
  • fastlycompute.config.js, fastlycompute.config.mjs

If an option is defined in both the command line and the configuration file, the command line option takes precedence.

Supported Options

Config KeyCLI FlagTypeDescription
disableAOT--disable-aotbooleanDisable AOT compilation
aotCache--aot-cachestring (path)Specify a path to the AOT cache file
enableHttpCache--enable-http-cachebooleanEnable the HTTP cache hook API
enableExperimentalHighResolutionTimeMethods--enable-experimental-high-resolution-time-methodsbooleanEnable experimental fastly.now() method
disableTopLevelAwait--disable-top-level-awaitbooleanCompile the application as a classic script rather than a module. As well as top level await, this disables strict-mode-by-default and import.meta
enableStackTraces--enable-stack-tracesbooleanEnable stack traces
excludeSources--exclude-sourcesbooleanDon't include sources in stack traces
debugIntermediateFiles--debug-intermediate-filesstring (path)Output intermediate files in directory
debugBuild--debug-buildbooleanUse debug build of the SDK runtime
engineWasm--engine-wasmstring (path)Specify a custom Wasm engine (advanced)
wevalBin--weval-binstring (path)Specify a custom weval binary (advanced)
env--envstring | object | arraySet environment variables, possibly inheriting from the current environment. Multiple variables can be comma-separated (e.g., --env ENV_VAR,OVERRIDE=val)

NOTE: The env field is additive. Values defined on the command-line values append to, rather than replace, the values defined in any configuration file.

Example command-line options

js-compute-runtime --enable-aot --enable-stack-traces --env=LOG_LEVEL=debug ./src/index.js ./bin/main.wasm

Example .fastlycomputerc.json

{
  "enableAOT": true,
  "enableStackTraces": true,
  "env": {
    "LOG_LEVEL": "debug"
  }
}

Example package.json

{
  "name": "my-fastly-service",
  "type": "module",
  "dependencies": {
    "@fastly/cli": "^13.0.0"
  },
  "scripts": {
    "build": "js-compute-runtime ./src/index.js ./bin/main.wasm",
    "dev": "fastly compute serve",
    "deploy": "fastly compute publish"
  },
  "fastlycompute": {
    "enableAOT": true,
    "enableStackTraces": true,
    "env": {
      "LOG_LEVEL": "debug"
    }
  }
}

API documentation

The API documentation for the JavaScript SDK is located at https://js-compute-reference-docs.edgecompute.app.

Security

If you find any security issues, see the Fastly Security Reporting Page or send an email to: [email protected]

We plan to disclose any found security vulnerabilities per the GitHub security vulnerability guidance. Note that communications related to security issues in Fastly-maintained OSS as described here are distinct from Fastly security advisories.

Changelog

The changelog can be found here.

License

Apache-2.0 WITH LLVM-exception

版本与发布记录

@fastly/js-compute 共发布 177 个版本,首次发布于 2021/07/28;过去 12 个月发布了 33 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.3.0latest2026/11/14今天
4.2.02026/11/103 天前
4.1.02026/11/0410 天前
4.0.12026/10/1530 天前
4.0.02026/10/071 个月前
4.0.0-pre.1next2026/09/191 个月前

关于 @fastly/js-compute 的常见问题

@fastly/js-compute 是什么?

JavaScript SDK and CLI for building JavaScript applications on Fastly Compute.

如何安装 @fastly/js-compute?

在项目目录运行 npm install @fastly/js-compute,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 js-compute、js-compute-runtime,可以用 npx js-compute 直接运行。

@fastly/js-compute 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastly/js-compute 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fastly/js-compute 可以商用吗?

@fastly/js-compute 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@fastly/js-compute 的下载量有多少?

根据 npm 官方下载统计,@fastly/js-compute 在 2026/10/15 – 2026/11/13 这 30 天内被下载 1,209,354 次,日均约 40,312 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastly/js-compute 还在维护吗?

最新版本 4.3.0 发布于 2026/11/14(今天);至今共发布 177 个版本,其中过去 12 个月发布了 33 个;首次发布于 2021/07/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。