プラグインを作る
プラグインは manifest.json と JavaScript のファイル 1 つでできています。サンプルはリポジトリの examples/plugin-greeter にあります。
manifest.json
{
"id": "com.example.hello",
"version": "0.1.0",
"displayName": "Hello",
"minTypressVersion": "0.0.0",
"entry": "index.js",
"capabilities": ["posts:read", "posts:write"],
"permissionReasons": {
"posts:write": "初回に挨拶の投稿を 1 つ作るため"
}
}id: 逆ドメイン形式(例:com.example.hello)capabilities: 使いたい権限。インストールした人が 1 つずつ許可します。permissionReasons: 権限ごとの理由(200 文字まで)。許可の画面に「作者による理由」として出ます。httpAllowlist: 通信先の一覧。http:fetch:allowlistを使うときは必須です。
権限
posts:read/posts:write: 投稿の読み書きhooks:register: イベントの登録settings:read:own/settings:write:own: プラグイン専用の設定http:fetch:allowlist:httpAllowlistの相手とだけ通信
index.js
export default async function init(host, { granted }) {
if (!granted.includes("posts:read") || !granted.includes("posts:write")) return;
const existing = await host.posts.read.list({ limit: 1 });
if (existing.length === 0) {
await host.posts.write.create({ title: "Hello", slug: "hello", body: [], status: "draft" });
}
}granted には、実際に許可された権限だけが入っています。許可されなかった権限の API は undefined になるので、確かめてから使ってください。
動く環境
- 本体とは別のプロセスで動き、Linux では Landlock と seccomp、macOS では sandbox-exec で閉じ込められます。
- ネットワークには直接出られません。
fetchは本体が代わりに行い、https のhttpAllowlistの相手とだけ通信できます。内部ネットワーク宛ては拒否され、リクエストは 1 MiB、レスポンスは 5 MiB まで、タイムアウトは 15 秒です。 - 起動に 3 回続けて失敗すると自動で無効になります。
パックと署名
bun tools/tpkg/src/main.ts pack examples/plugin-greeter
bun tools/tpkg/src/main.ts sign examples/plugin-greeter/com.typress.greeter-0.1.2.tpkg --key dev更新版は、同じ鍵で署名して同じ手順でインストールします。鍵が変わると「作者が変わった」として更新を拒否します。