@cloudflare/actors

中文文档
0.0.1-beta.6发布于 2025/09/15MITTypeScriptES Module零依赖近 30 天下载 3.1万

An easier way to build with Cloudflare Durable Objects

原始使用文档
文档目录12 节
  1. Features
  2. Getting Started
  3. Step 1: Install the package
  4. Step 2: Update your Wrangler Configuration
  5. Step 3: Create your class implementation:
  6. Examples
  7. FAQ
  8. Alarms
  9. General
  10. Location Placement
  11. Contributing
  12. License

Cloudflare Actors

This project is in active development.

We are building a full-featured framework that makes developing Cloudflare Durable Objects easier by introducing new patterns and out of the box functionality that help developers.

Features

  • Request Handler to easily define entrypoints to your Actor, Worker, or Request
  • Lifecycle Methods enable you to tap into important lifecycle events
  • Persistent Properties that store property values between requests and evictions
  • RPC Interface into other Actors with a simple MyActor.get('id') interface
  • Managing Instances track, delete, and access all instances that have been created
  • Location Placement allow you to control the location of your Actor
  • SQL Migrations to apply migrations to the SQLite storage
  • Multiple Alarms set any number of alarms by timestamp, delay, or cron
  • Retry utilities to retry operations using exponential backoff with jitter strategy, and to detect if Durable Object errors are retryable.

And many more features, check out the Examples for more information.

Getting Started

Step 1: Install the package

npm i @cloudflare/actors

Step 2: Update your Wrangler Configuration

Notice the code class name in your Typescript implementation must match the binding name, class_name and new_sqlite_classes value in the configuration. Verify all of the values match.

{
  "migrations": [
    {
      "new_sqlite_classes": ["MyActor"],
      "tag": "v1"
    }
  ],
  "durable_objects": {
    "bindings": [
      {
        "class_name": "MyActor",
        "name": "MyActor"
      }
    ]
  }
}

Step 3: Create your class implementation:

import { Actor, handler } from "@cloudflare/actors";

export class MyActor extends Actor<Env> {
  async fetch(request: Request): Promise<Response> {
    return new Response("Hello, World!");
  }
}

export default handler(MyActor);

Examples

FAQ

Alarms

What is the lifecycle of an Alarm trigger? When an Actor is awoken by an Alarm trigger, it currently behaves in a slightly different order than if the Actor was already in memory. Below is the sequence of events.

Cold Start:

  • [Callback]
  • onInit
  • onAlarm

Warm Start:

  • onAlarm
  • [Callback]

For the warm start instance, onInit would have already been called earlier before the alarm trigger.

General

What is an Actor? An Actor is a Durable Object that is stateful and has access to both compute and storage. You can think of it as a small server instance that is active when being accessed and asleep when not.
How long does a single request keep my Actor alive for? A single request will keep the Actor alive for ~10 seconds.
Can I keep my Actor alive longer? Using `setTimeout` in your code can keep it alive for up to ~60 seconds.
Are there other ways to keep my code alive longer? Yes, you can use alarms to keep the Actor alive longer.
Does every new request reset the time until the Actor is no longer in memory? Yes.

Location Placement

How do I control the location of my Actor? You can use location hints to control the location of your Actor.
Where does my Actor live if I do not specify a location hint? If you do not specify a location hint, your Actor will be placed in the region closest to the user.
Can you change the location or region of your Actor? No, you cannot change the location or region of your Actor. Once it has been instantiated it will always live in that region. If you want to move your Actor to a different region, you will need to deploy a new version of your code.
With a location hint where will my Actor be placed? With a location hint, your Actor will be placed in the region you specified. The instance will be spawned somewhere randomly within the location region you provide. For example if you provide the `enam` location hint, the instance will be spawned somewhere randomly within the Eastern North America region.
What happens if the data center where my Actor is located goes down? If the data center where your Actor is located goes down, your Actor will be moved to another data center.

See FAQ for more answers to common questions.


Contributing

We welcome contributions! Please refer to our Contributing Guidelines for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

版本与发布记录

@cloudflare/actors 共发布 9 个版本,首次发布于 2025/06/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1-beta.6latest2025/09/151.2 年前
0.0.1-beta.52025/08/271.2 年前
0.0.1-beta.42025/08/211.2 年前
0.0.1-beta.32025/07/231.3 年前
0.0.1-beta.22025/07/221.3 年前
0.0.1-beta.12025/07/181.3 年前

关于 @cloudflare/actors 的常见问题

@cloudflare/actors 是什么?

An easier way to build with Cloudflare Durable Objects

如何安装 @cloudflare/actors?

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

@cloudflare/actors 支持 TypeScript 吗?

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

@cloudflare/actors 支持 ESM 还是 CommonJS?

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

@cloudflare/actors 需要什么版本的 Node.js?

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

@cloudflare/actors 可以商用吗?

@cloudflare/actors 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudflare/actors 的下载量有多少?

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

@cloudflare/actors 还在维护吗?

最新版本 0.0.1-beta.6 发布于 2025/09/15(1.2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/06/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。