@nodeloc/apps-cli 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +45 -3
  2. package/package.json +1 -1
  3. package/src/bundle.js +13 -1
package/README.md CHANGED
@@ -10,11 +10,11 @@ app 是一个导出若干 handler 的 JS 模块,handler 跑在论坛服务端
10
10
  需要 **Node.js 22 或更高**。
11
11
 
12
12
  ```bash
13
- git clone https://github.com/nodeloc/nodeloc-apps-cli.git
14
- cd nodeloc-apps-cli
15
- npm install -g .
13
+ npm install -g @nodeloc/apps-cli
16
14
  ```
17
15
 
16
+ 更新到最新版再跑一次同一条命令。想从源码装:克隆本仓库后在目录里 `npm install -g .`。
17
+
18
18
  ## 登录
19
19
 
20
20
  ```bash
@@ -58,6 +58,48 @@ nodeloc-apps logs --limit 50
58
58
 
59
59
  `dev` 会把相对 import 内联成单个模块,并提前拦掉服务端一定会拒的写法:缺 `render` 导出(service app 则要求至少有一个 `onTrigger`/`onSchedule`/`onFetch`)、`eval`、`node:` 内建、浏览器全局、`fetch`。
60
60
 
61
+ 额度:打包后的代码最多 2 MiB(`dev` 和 `upload` 在本地就会拦,0.3.1 起;更早的版本按 512 KB 拦)。键值存储按区算,一个用户的私有区和一个安装的共享区各算各的:每个区最多 500 个键,单个值(JSON)最多 64 KiB,整个区的键和值合计最多 1 MiB。超出时这次调用的改动整批作废,错误码是 `E_INVALID_EFFECT`(单个值太大)或 `E_KV_QUOTA`(键太多,或者这个区满了)。每次调用都会把整个区读进沙箱,代码越大启动也越慢,所以够用就好。
62
+
63
+ ### 给成员发节点的用户标识(0.3.2 起)
64
+
65
+ 节点里每个成员名字旁边可以戴一个「用户标识」。以前标识只有两种发法:成员自选,或者版主手动分配。现在节点主可以把一个标识交给某个 app 发放:成员不能自选,app 按自己的规则给成员戴上(版主照样能手动分配、摘掉)。
66
+
67
+ 节点主这边:节点页 › 管理 › 用户标识,新建或编辑标识,「谁能使用」选「由应用发放」,填上 app 的标识(应用页地址 `/apps/` 后面那一段,要已经发布)。
68
+
69
+ app 这边:`app.json` 的 `scopes` 加上 `"flair"`(普通权限,不用管理员授予),然后:
70
+
71
+ | | |
72
+ |---|---|
73
+ | `await api.flair.list()` | 所在节点里节点主交给这个 app 发的标识,按节点主排的顺序:`[{id, text, color, style, icon}]`。没有就是 `[]` |
74
+ | `await api.flair.current()` | 当前成员在这个节点里戴着的标识 `{id, text}`;没登录、不是成员、没戴都是 `null` |
75
+ | `await api.flair.member()` | 当前成员是不是这个节点的成员(`true` / `false`) |
76
+ | `{ type: "flair.set", flair_id }` | 给当前成员戴上。只能是 `api.flair.list()` 里的标识 |
77
+ | `{ type: "flair.clear" }` | 摘掉。只摘这个 app 自己发的那种;成员戴的是别的(版主分配的、自选的)时什么都不做,也不算错 |
78
+
79
+ 「所在节点」由安装的位置决定:放进帖子的 app 是帖子所在的节点,装到节点上的机器人是那个节点。装在全站的机器人没有所在节点,声明了会被拒(`E_FLAIR_NO_NODE`)。对象只能是当前成员;机器人在后台(`onTrigger`)不带 `user_id` 时是触发事件的那个人,带的话只能是这次事件涉及的人。
80
+
81
+ 一次调用最多一条 `flair.set` / `flair.clear`。重复发成员已经戴着的那个是成功的,不占额度;一个 app 一天最多给同一个人换 10 次。平台不发通知,要提示成员就自己加一条 `ui.toast`。
82
+
83
+ ```js
84
+ const DAYS = [3, 7, 14, 30, 100, 200, 365]; // 节点主按这个顺序建 7 个标识
85
+ export async function onAction(ctx, api) {
86
+ const days = await checkIn(ctx, api); // 你自己的签到逻辑,天数存在 kv 里
87
+ const effects = [{ type: "kv.set", key: "days", value: days }];
88
+ const flairs = await api.flair.list();
89
+ const earned = flairs[Math.min(DAYS.filter((d) => days >= d).length, flairs.length) - 1];
90
+ const worn = await api.flair.current();
91
+ const member = await api.flair.member();
92
+ if (earned && earned.id !== worn?.id && member) effects.push({ type: "flair.set", flair_id: earned.id });
93
+ return { blocks: screen(days, earned, member), state: null, effects };
94
+ }
95
+ ```
96
+
97
+ 先看 `api.flair.member()` 再声明 `flair.set`:发给还没加入节点的人会被拒(`E_FLAIR_NOT_MEMBER`),而一条被拒整批作废,同一批里的签到也就没存上。不是成员时在界面上提示他先加入节点。
98
+
99
+ 错误码:`E_FLAIR_DENIED`(这个标识不在所在节点里、节点主没有交给这个 app,或者 `user_id` 不是这次调用涉及的人)、`E_FLAIR_NOT_MEMBER`(对方不是节点成员,平台不会替他加入)、`E_FLAIR_NO_NODE`(这个安装不在任何节点里)、`E_FLAIR_QUOTA`(今天给这个人换的次数到上限了)。
100
+
101
+ `playtest` 的私有安装不在任何节点里:`api.flair.list()` 是 `[]`,声明的 `flair.set` / `flair.clear` 只校验参数、不执行,应答里多一条带 `dry_run: true` 的提示。要看真实效果,发布后放进节点里的帖子。
102
+
61
103
  沙箱没有网络,这一点不会变。要访问外部服务,handler 返回 `http.fetch` effect,由站点代为请求,结果之后作为一次单独的 `onFetch` 调用送回来——只允许 https,只允许管理人员批准过的精确主机名。代码不审核,但 `domains` 里的每个主机都要管理人员逐个批准,批准之前(试玩里也一样)请求会得到 `E_HTTP_DOMAIN_DENIED`;`upload` 和 `playtest` 会打印每个主机的状态(已批准、等批准、没有获批、已撤回)。被拒、撤回的主机再次申请也不会重新排队,请联系管理人员。
62
104
 
63
105
  ## 项目结构
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeloc/apps-cli",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Build and publish sandboxed NodeLoc apps",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/bundle.js CHANGED
@@ -46,12 +46,15 @@ function defaultImportIn(clause) {
46
46
 
47
47
  export class BundleError extends Error {}
48
48
 
49
+ /** The site's limit on one version's code, checked here so an upload is not wasted on it. */
50
+ export const MAX_BUNDLE_BYTES = 2 * 1024 * 1024;
51
+
49
52
  /**
50
53
  * Inlines relative imports depth-first. This is deliberately not a general
51
54
  * bundler: apps are small, and a dependency graph an author cannot read in one
52
55
  * sitting is a dependency graph a reviewer cannot check.
53
56
  */
54
- export async function bundle(entryPath, { maxBytes = 512 * 1024, seen = new Set() } = {}) {
57
+ export async function bundle(entryPath, { maxBytes = MAX_BUNDLE_BYTES, seen = new Set() } = {}) {
55
58
  const resolved = path.resolve(entryPath);
56
59
 
57
60
  if (seen.has(resolved)) {
@@ -156,6 +159,15 @@ export function inspect(code, manifest = {}) {
156
159
  "Declares points.spend but exports no onSpend. A member's payment lands there; without it, deliver by reconciling api.points.spends() on render."
157
160
  );
158
161
  }
162
+ // Handing out a node flair is two halves: the app declares the "flair" scope,
163
+ // and the node's owner names this app on the flair. The second half cannot be
164
+ // checked from here; the first can, and without it every call that declares
165
+ // the effect is thrown away whole (E_SCOPE_DENIED).
166
+ if (/["']flair\.(set|clear)["']|\bapi\.flair\./.test(code) && !(manifest.scopes ?? []).includes("flair")) {
167
+ warnings.push(
168
+ 'Uses node flairs (flair.set / flair.clear / api.flair) but app.json does not request the "flair" scope.'
169
+ );
170
+ }
159
171
  if (/\b(eval|new\s+Function)\s*\(/.test(code)) {
160
172
  warnings.push("Uses eval or new Function. Staff can suspend an app that loads code at runtime; avoid it.");
161
173
  }