@featureflip/edge

中文文档
2.19.1发布于 2026/11/10Apache-2.0TypeScriptES Module零依赖近 30 天下载 2133

Cloudflare Workers SDK for Featureflip: local flag evaluation at the edge

原始使用文档
文档目录10 节
  1. Install
  2. Quickstart
  3. Options
  4. Freshness
  5. Pass ctx
  6. Cold start
  7. Bootstrapping the browser SDK
  8. Events
  9. Logging
  10. License

@featureflip/edge

Featureflip SDK for Cloudflare Workers. It evaluates flags locally inside your Worker from a cached copy of your environment's flag config, so a request that finds a warm copy makes no network call to evaluate a flag.

Cloudflare Workers is the only supported runtime.

Install

npm install @featureflip/edge

Quickstart

import { createEdgeClient } from '@featureflip/edge';

type Env = { FEATUREFLIP_SERVER_KEY: string };

// Config only at module scope: Workers forbid I/O there, and the key lives in `env`.
let flags: ReturnType<typeof createEdgeClient> | undefined;

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    flags ??= createEdgeClient({ sdkKey: env.FEATUREFLIP_SERVER_KEY });

    const snapshot = await flags.load(ctx);
    const context = { user_id: 'user-123' };

    const enabled = snapshot.boolVariation('new-checkout', context, false);
    const detail = snapshot.variationDetail('checkout-copy', context, 'control');
    const bootstrap = snapshot.bootstrapValues(context);

    return new Response(JSON.stringify({ enabled, detail, bootstrap }));
  },
};

Call load(ctx) once per request. After it resolves, every variation call is synchronous.

Options

sdkKey is the only required option. Use a server SDK key, not a client key.

OptionDefaultDescription
sdkKeynone (required)Server SDK key. Sent as Authorization and never logged.
baseUrlhttps://eval.featureflip.ioEvaluation API origin.
ttlSeconds60Age after which a copy is refreshed in the background.
maxStaleSeconds86400Age after which a copy is no longer served at all.
loadTimeoutMs1000How long a load with no cached copy waits for the origin.
storeWorkers Cache APIWhere fetched config is shared between isolates.
wrappers[]name/version tokens for libraries that wrap this SDK.
flushBatchSize100Events sent per POST /v1/sdk/events request.
fetchglobal fetchfetch implementation, mainly for tests.

Freshness

Flag changes reach edge traffic within ttlSeconds (60 seconds by default). This is not the near-instant push that the server SDKs get over a streaming connection.

A copy older than ttlSeconds is still served while a background refresh runs. So after an idle spell, the first request in gets the older copy once, because the refresh it started hasn't finished. A copy older than maxStaleSeconds is not served. If a config fetch fails, the SDK backs off for 15 seconds before trying the origin again, and keeps serving any copy it has.

Pass ctx

Pass the Workers ExecutionContext to load(ctx). It registers the background refresh and the event flush with waitUntil, so both finish after the response is sent.

Without ctx, await flush() yourself before responding, and the background refresh may be cut off when the request ends. The SDK logs a one-time warning when it runs without ctx.

Cold start

With no cached copy, load waits up to loadTimeoutMs for the origin. If the origin is slower, load resolves a snapshot with ready === false that serves your default values. The fetch keeps going in the background and fills the cache for the next request.

Bootstrapping the browser SDK

snapshot.bootstrapValues(context) returns the values of client-visible flags only, for the bootstrap option of @featureflip/browser. If the server does not send the client-visibility field, it returns nothing, so no flag is exposed by mistake.

Events

Evaluations are queued as events and sent with waitUntil when you pass ctx. A request's flush sends only if a batch is full or 10 seconds have passed since the last send, so a busy isolate stops posting once per request. After a 429, a 5xx or a network error nothing is sent for 30 seconds, and outside that window flush() sends straight away, capped at 5 requests a call.

Delivery is best effort. The queue lives in the isolate's memory, so events still queued when Cloudflare evicts the isolate are lost.

Logging

Messages go to console.warn. The SDK's own start with [featureflip-edge], and the few from the shared evaluation core (rejected or dropped events, an ignored wrappers token) start with [featureflip].

License

Apache-2.0

版本与发布记录

@featureflip/edge 共发布 11 个版本,首次发布于 2026/10/21;过去 12 个月发布了 11 个版本。

版本发布日期距今
2.19.1latest2026/11/104 天前
2.19.02026/11/086 天前
2.18.02026/10/3015 天前
2.17.22026/10/2817 天前
2.17.12026/10/2717 天前
2.17.02026/10/2718 天前

关于 @featureflip/edge 的常见问题

@featureflip/edge 是什么?

Cloudflare Workers SDK for Featureflip: local flag evaluation at the edge

如何安装 @featureflip/edge?

在项目目录运行 npm install @featureflip/edge,或使用 pnpm add / yarn add / bun add。

@featureflip/edge 支持 TypeScript 吗?

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

@featureflip/edge 支持 ESM 还是 CommonJS?

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

@featureflip/edge 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@featureflip/edge 可以商用吗?

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

@featureflip/edge 的下载量有多少?

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

@featureflip/edge 还在维护吗?

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