@canvas-js/core

中文文档
0.16.1发布于 2025/07/30TypeScriptES Module近 30 天下载 2195

This package exports a Canvas class that can be used to manually instantiate Canvas applications.

原始使用文档
文档目录4 节
  1. Table of Contents
  2. Installation
  3. How it works
  4. API

@canvas-js/core

This package exports a Canvas class that can be used to manually instantiate Canvas applications.

Use this package directly if you want fine-grained control over when an application is started/stopped. Otherwise, you can use useCanvas in @canvas-js/hooks, which has the same API, but handles initialization inside React for you.

Table of Contents

Installation

$ npm i @canvas-js/core

How it works

Under the hood, each Canvas application replicates and executes a log of signed actions, sourced from GossipLog, with read/write access to a ModelDB database.

Each application accepts several arguments:

  • contract takes a class that extends Contract, or a string containing a JS module which exports a default class that extends Contract.
  • topic takes a string
  • snapshot (optional) takes a Snapshot object which provides initial database contents for the application.
  • signers (optional) takes an array of signers, which allows different auth methods to be added to the application.

Use await Canvas.initialize to start the application. (For synchronous initialization, see CanvasLoadable.ts.)

To connect the application to other sync peers, use app.connect() to start a WebSocket connection, or app.listen() to listen for WebSocket connections from the server.

Or, use app.startLibp2p() to start a libp2p node.

After starting the application, you can use app.actions to access each of the actions that you have defined.

Action calls will be signed and proxied to the contract.

import { Canvas, Contract } from "@canvas-js/core"

class Chat extends Contract<typeof Chat.models> {
  static models = {
    posts: {
      id: "primary",
      user: "string",
      content: "string",
      updated_at: "integer",
    },
  }

  async createPost(content: string) {
    const { id, chain, address, timestamp, db } = this
    const user = [chain, address].join(":")
    await db.set("posts", { id, user, content, updated_at: timestamp })
  }

  async deletePost(postId: string) {
    const { chain, address, db } = this
    const post = await db.get("posts", postId)
    if (post === null) {
      return
    }

    const user = [chain, address].join(":")
    if (post.user !== user) {
      throw new Error("not authorized")
    }

    await db.delete("posts", postId)
  }
}

const app = await Canvas.initialize({
  topic: "example.xyz",
  contract: Chat,
})

await app.actions.createPost("hello world!")
const results = await app.db.query("posts", {})
// [
//   {
//     id: '09p5qn7affkhtbflscr663tet8ddeu41',
//     user: 'did:pkh:eip155:1:0x79c5158f81ebb0c2bcF877E9e1813aed2Eb652B7',
//     content: 'hello world!',
//     updated_at: 1698339861041
//   }
// ]

API

<<< @/../packages/core/src/types.ts

版本与发布记录

@canvas-js/core 共发布 263 个版本,首次发布于 2022/05/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.16.1latest2025/07/301.3 年前
0.16.0-next.9next2025/07/141.3 年前
0.16.02025/07/081.3 年前
0.16.0-next.82025/06/271.4 年前
0.16.0-next.72025/06/251.4 年前
0.16.0-next.62025/06/251.4 年前

其他发布通道:alpha → 0.10.0-alpha.1,beta → 0.10.0-beta.3,next → 0.16.0-next.9。

关于 @canvas-js/core 的常见问题

@canvas-js/core 是什么?

This package exports a Canvas class that can be used to manually instantiate Canvas applications.

如何安装 @canvas-js/core?

在项目目录运行 npm install @canvas-js/core,或使用 pnpm add / yarn add / bun add。

@canvas-js/core 支持 TypeScript 吗?

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

@canvas-js/core 支持 ESM 还是 CommonJS?

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

@canvas-js/core 需要什么版本的 Node.js?

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

@canvas-js/core 可以商用吗?

@canvas-js/core 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@canvas-js/core 的下载量有多少?

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

@canvas-js/core 还在维护吗?

最新版本 0.16.1 发布于 2025/07/30(1.3 年前);至今共发布 263 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。