npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@zhin.js/permission

v1.1.15

Published

Unified PermissionHost for command permit, tool permit, and platform-level auth in Zhin Plugin Runtime

Readme

@zhin.js/permission

统一鉴权面(PermissionHost),供命令 permit、工具 permissions、平台级权限校验使用。

核心概念

  • PermissionHost — 解析 permit 字符串并校验是否通过。解析顺序:builtin → platform(adapter,perm) → 自定义 register → deny。
  • PermissionSubject — 鸭式鉴权主体(与 CommandSession / Message 同构:adapter / endpoint / scene / sender)。
  • permissionHostToken — Plugin Runtime Scope Token,ImRuntime 在根 Scope provide。

使用

import {
  PermissionHost,
  permissionHostToken,
  toPermissionSubject,
  assertPermitSyntax,
  type PermissionSubject,
} from '@zhin.js/permission';

const host = new PermissionHost();

命令 permit 校验

defineCommand 的 permit 字段支持 builtin DSL 和 platform(adapter,perm) 两种形态。构建期通过 assertPermitSyntax 做语法校验。

只读解释与假设检查

explainPermitRules(permits, subject?, { rolesKnown? }) 只解析声明并复用内置纯判断,返回 match / mismatch / unknown / not_evaluated,不查询或执行 PermissionHost 的任何 checker。 角色未明确作为假设输入提供时,不会把空角色或默认角色当作已核验依据。 platform(...) 与未知规则保留为未执行;结果不是授权结论。

规则数组为 AND,同一项括号内逗号为 OR。group()、private()、channel() 的空值和 * 表示不限该场景的 ID;adapter(*) 没有通配语义。master 身份仅隐含 trusted,不会隐含平台的 admin。内置场景规则不约束 Endpoint、父容器或话题: 例如 group(123) 不能保证只在某个 Bot 账号的群或某个线程生效。

命令应使用 @zhin.js/command 的 explainCommandPermissions,它与实际命令准入复用 同一主体投影,并单独解释命令 adapter 的精确匹配约束,不能用单独的 toPermissionSubject() 替代命令侧角色优先级。

适配器注册平台 checker

// adapter plugin.ts
import { permissionHostToken, createSceneRolePlatformChecker } from '@zhin.js/permission';

export default definePlugin({
  name: 'my-adapter',
  setup(context) {
    if (context.resources.has(permissionHostToken)) {
      const host = context.resources.use(permissionHostToken);
      return host.registerPlatform('my-adapter', createSceneRolePlatformChecker());
    }
  },
});

自定义 permission

import { definePermission, definePlatformPermission } from '@zhin.js/permission';

// 名称匹配
export default definePermission({
  name: 'can_deploy',
  check: (name, subject) => subject.sender?.role?.includes('admin') ?? false,
});

// 正则匹配
export default definePermission({
  name: /^feature\./,
  check: (name, subject) => true,
});

层级

@zhin.js/plugin-runtime
  ↓
@zhin.js/permission  ← 本包
  ↓
@zhin.js/command / @zhin.js/core / @zhin.js/agent(消费者)

不依赖 @zhin.js/core——通过 PermissionSubject 鸭式接口解耦。