@asterql/view-protocol

中文文档
0.5.0发布于 2026/07/09MITTypeScriptES Module近 30 天下载 2512

Registered AsterQL view ids, manifests, params URLs, and handlers.

原始使用文档

@asterql/view-protocol

npm install @asterql/view-protocol

Registered view definitions, deterministic view ids, canonical params, and a small handler shape for AsterQL.

This package is the bridge between "run this query locally" and "ship a cacheable UI data endpoint." Production requests use manifest-backed ids, while local tools can opt into raw query evaluation explicitly.

import {
  createViewCacheHeaders,
  createManifest,
  createViewHandler,
  createViewUrl,
  defineView,
} from "@asterql/view-protocol";

const recentOrders = defineView({
  service: "dashboard",
  query: `*.orders[status == $status] | sort(-createdAt)[:10] {id, total}`,
});

const manifest = createManifest([recentOrders]);
const url = createViewUrl(recentOrders, { status: "paid" });
const cacheHeaders = createViewCacheHeaders(recentOrders, { status: "paid" });

const handle = createViewHandler({
  views: [recentOrders],
  async load({ params }) {
    return loadDashboardData(params);
  },
});

API

  • defineView(input): normalize a query and compute a deterministic view id.
  • getViewId(view): return the content hash id for a view.
  • createManifest(views): emit a stable manifest keyed by view id.
  • createViewETag(view, params?, options?): compute a weak ETag from the view id, canonical params hash, and optional service data version.
  • createViewCacheHeaders(view, params?, options?): create Cache-Control and ETag headers from the view cache policy.
  • encodeParams(params) / decodeParams(value): base64url canonical JSON.
  • createViewUrl(view, params?, options?): build /views/:viewId URLs.
  • createViewHandler(options): execute registered views against host data.
  • fetchView(view, params?, options?): tiny fetch helper for clients.

State sync primitives:

  • createEntityKey(kind, id) / parseEntityKey(key): stable normalized entity ids in kind:id form.
  • createScopeKey(kind, id) / parseScopeKey(scope): stable scope ids such as agent:agent_123.
  • hashRecordVersion(input) and hashServerEventId(input): deterministic record/event ids over canonical JSON.
  • ServerEvent, ServerEventEnvelope, EntityRecord, EntityPatch, and InvalidationClass: browser-safe wire and store contracts for normalized state sync.
  • invalidationIntersects(a, b): match entity, entity-kind, scope, service, and view invalidation classes.
  • parseEntityPath(path) / formatEntityPath(path): JSON Pointer helpers for patch paths.
  • getEntityPathValue, setEntityPathValue, unsetEntityPathValue, applyEntityPatch, and applyEntityPatches: immutable JSON patch helpers for set, unset, merge, appendText, keyed insert, and removeKey.
  • canonicalJsonBytes(value), sha256Base64Url(bytes), hashCanonicalJson(prefix, value), hashParams, hashSchema, and hashViewDefinition: canonical hashing helpers matching the package's existing view/schema/params ids.
  • createVersionedViewETag(input) / parseVersionedViewETag(etag): weak ETags that embed the view id, canonical params hash, auth-scope hash, body hash, and a sorted invalidation-class version map, so revalidation can answer "did any class this view depends on advance?" from the ETag alone.
  • formatInvalidationClass(class), classVersionFromDate(value), mergeClassVersions(...maps), advancedClassVersions(current, baseline, keys?), classVersionsFromETagPayload(payload), hashAuthScope(value), and ifNoneMatchIncludes(header, etag): the class-version vocabulary used by versioned ETags and invalidation-driven cache revalidation.

The handler only executes registered views by default. Registered view responses include explicit cache headers: views with a cache policy use that policy, and views without one are no-store. Raw dev _eval responses are always no-store and exist only when allowEval: true is passed.

版本与发布记录

@asterql/view-protocol 共发布 4 个版本,首次发布于 2026/06/11;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.5.0latest2026/07/093 个月前
0.4.02026/06/304 个月前
0.3.02026/06/114 个月前
0.2.02026/06/114 个月前

关于 @asterql/view-protocol 的常见问题

@asterql/view-protocol 是什么?

Registered AsterQL view ids, manifests, params URLs, and handlers.

如何安装 @asterql/view-protocol?

在项目目录运行 npm install @asterql/view-protocol,或使用 pnpm add / yarn add / bun add。

@asterql/view-protocol 支持 TypeScript 吗?

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

@asterql/view-protocol 支持 ESM 还是 CommonJS?

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

@asterql/view-protocol 需要什么版本的 Node.js?

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

@asterql/view-protocol 可以商用吗?

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

@asterql/view-protocol 的下载量有多少?

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

@asterql/view-protocol 还在维护吗?

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