@rin-chat/rinext 0.0.0-stage → 1.0.0

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.
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "@rin-chat/rinext",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Make, sign, check and test Rin Chat extensions (.rinext).",
5
+ "keywords": ["rin-chat", "extension", "rinext", "quickjs"],
6
+ "type": "module",
7
+ "bin": { "rinext": "./bin/rinext.mjs" },
8
+ "files": [
9
+ "bin/rinext.mjs",
10
+ "src/commands.mjs",
11
+ "src/package.mjs",
12
+ "rules.json",
13
+ "dist/test-runner.mjs",
14
+ "dist/quickjs.wasm",
15
+ "dist/THIRD-PARTY-LICENSES.txt",
16
+ "template",
17
+ "LICENSE"
18
+ ],
19
+ "engines": { "node": ">=22.2" },
20
+ "homepage": "https://rin.chat/developers/extensions/",
21
+ "license": "MIT",
22
+ "author": "Rin Chat LLC",
23
+ "publishConfig": { "access": "public" },
24
+ "scripts": {
25
+ "prepublishOnly": "node build.mjs"
26
+ }
27
+ }
package/rules.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "_comment": "The .rinext and storage rules shared by the rinext CLI (src/package.mjs, src/test-runner.ts) and the app (src-tauri/src/extensions.rs, ext_storage.rs). The Rust test extensions::rinext_cli asserts every value here against the Rust constants it can see; the ones it cannot (private to ext_storage.rs or inline in a match) are pinned by behavior in test/vectors.json, which both sides run.",
3
+ "signatureDomain": "rinext-signature-v1\n",
4
+ "signatureFile": "SIGNATURE",
5
+ "maxPackage": 2097152,
6
+ "maxFiles": 64,
7
+ "maxManifest": 65536,
8
+ "maxNetHosts": 32,
9
+ "maxSettings": 32,
10
+ "maxText": 4000,
11
+ "textMaxDefault": 500,
12
+ "maxOptions": 32,
13
+ "maxSliderSteps": 10000,
14
+ "storageQuota": 1048576,
15
+ "storageValueMax": 262144,
16
+ "storageKeyMax": 128,
17
+ "storageKeysMax": 1024,
18
+ "permissions": [
19
+ "chat.read",
20
+ "prompt.inject",
21
+ "prompt.rewrite",
22
+ "reply.transform",
23
+ "chat.send",
24
+ "chat.vars",
25
+ "storage",
26
+ "ui.command",
27
+ "ui.settings",
28
+ "ui.widget",
29
+ "model.run",
30
+ "open.url",
31
+ "ui.panel",
32
+ "ui.hud",
33
+ "ui.annotate",
34
+ "ui.choices",
35
+ "composer.draft",
36
+ "ui.notify",
37
+ "audio.play",
38
+ "voice.speak",
39
+ "ui.style",
40
+ "ui.theme",
41
+ "prompt.macro",
42
+ "render.transform",
43
+ "chat.generate",
44
+ "gen.params"
45
+ ],
46
+ "iconExts": ["png", "webp", "jpg", "jpeg"],
47
+ "assetExts": ["png", "webp", "jpg", "jpeg", "json"],
48
+ "audioExts": ["wav", "ogg", "mp3"],
49
+ "audioMax": 1048576,
50
+ "docFiles": ["README.md", "LICENSE", "LICENSE.md", "LICENSE.txt"],
51
+ "reservedHostSuffixes": ["localhost", "local", "internal", "lan", "home", "arpa"],
52
+ "net": {
53
+ "_comment": "extnet.rs, for rinext test's fake network (src/fake-net.ts); asserted by ext_net::tests::rinext_rules_match_the_network_constants.",
54
+ "methods": ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"],
55
+ "bodyMax": 65536,
56
+ "urlMax": 8192,
57
+ "headersMax": 32,
58
+ "forbiddenHeaders": ["accept-charset", "accept-encoding", "access-control-request-headers", "access-control-request-method", "connection", "content-length", "cookie", "cookie2", "date", "dnt", "expect", "forwarded", "host", "keep-alive", "origin", "referer", "set-cookie", "te", "trailer", "transfer-encoding", "upgrade", "user-agent", "via", "x-http-method", "x-http-method-override", "x-method-override"],
59
+ "forbiddenHeaderPrefixes": ["proxy-", "sec-", "x-forwarded-"],
60
+ "rateBurst": 5,
61
+ "ratePerMinute": 15,
62
+ "denialsToBlock": 3
63
+ }
64
+ }
@@ -0,0 +1,180 @@
1
+ // The rinext commands. Each returns { ok, out: string[] } (lines for stdout, or the refusals) and never exits,
2
+ // so the tests drive them directly; bin/rinext.mjs prints and sets the exit code.
3
+
4
+ import { generateKeyPairSync } from "node:crypto";
5
+ import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
6
+ import { dirname, join, relative, resolve, sep } from "node:path";
7
+ import {
8
+ RULES, Refusal, checkFiles, decodeKey, loadPrivateKey, netSentence, parsePackage, PERMISSION_SENTENCES, rawPublicKey, refusalText, signFiles,
9
+ validFileName, writeZip,
10
+ } from "./package.mjs";
11
+
12
+ export const KEY_FILE = "rinext-author.key";
13
+ const KEY_WARNING = [
14
+ "# rinext author key: PRIVATE. Anyone holding this file can sign packages as you.",
15
+ "# Keep it outside every extension folder, never commit it, never send it to anyone.",
16
+ "# Rin Chat pins the matching public key to your extension id on first install, so losing",
17
+ "# this file means users cannot update your extension; back it up somewhere safe.",
18
+ "",
19
+ ].join("\n");
20
+
21
+ const said = (r) => `${refusalText(r.code, r.detail)} [${r.message}]`;
22
+ const inside = (child, parent) => {
23
+ const rel = relative(resolve(parent), resolve(child));
24
+ return rel === "" || (!rel.startsWith("..") && !rel.startsWith(sep) && !/^[A-Za-z]:/.test(rel));
25
+ };
26
+ const b64 = (b) => Buffer.from(b).toString("base64url");
27
+
28
+ /** A folder that is, or sits inside, an extension folder (one holding a manifest.json). */
29
+ function extensionFolderAround(dir) {
30
+ for (let d = resolve(dir); ; d = dirname(d)) {
31
+ if (existsSync(join(d, "manifest.json"))) return d;
32
+ if (dirname(d) === d) return null;
33
+ }
34
+ }
35
+
36
+ /** A copy of the kit's template (an extension and its tests) in a new or empty folder. */
37
+ export function init(outDir) {
38
+ const dir = resolve(outDir);
39
+ if (existsSync(dir) && (!statSync(dir).isDirectory() || readdirSync(dir).length)) return { ok: false, out: [`${dir} already exists and is not an empty folder.`] };
40
+ cpSync(new URL("../template", import.meta.url), dir, { recursive: true });
41
+ return {
42
+ ok: true,
43
+ out: [
44
+ `Copied the template to ${dir}`,
45
+ "Next, from that folder: rinext test extension test/mood.test.mjs; then follow its README.md to add your key.",
46
+ ],
47
+ };
48
+ }
49
+
50
+ export function keygen(outDir) {
51
+ const dir = resolve(outDir);
52
+ const ext = extensionFolderAround(dir);
53
+ if (ext) return { ok: false, out: [`${ext} holds a manifest.json, so it looks like an extension folder. Write the key somewhere outside every extension folder.`] };
54
+ const file = join(dir, KEY_FILE);
55
+ if (existsSync(file)) return { ok: false, out: [`${file} already exists. rinext never overwrites a key; move it first if you really mean to replace it.`] };
56
+ mkdirSync(dir, { recursive: true });
57
+ const { privateKey } = generateKeyPairSync("ed25519");
58
+ writeFileSync(file, KEY_WARNING + privateKey.export({ type: "pkcs8", format: "pem" }), { flag: "wx", mode: 0o600 });
59
+ return {
60
+ ok: true,
61
+ out: [
62
+ `Wrote the PRIVATE key to ${file}`,
63
+ "Keep it outside your extension folder and out of version control. Losing it means you cannot ship updates.",
64
+ "",
65
+ "Put this in your manifest.json:",
66
+ ` "author": { "name": "<your name>", "key": "${b64(rawPublicKey(privateKey))}" }`,
67
+ ],
68
+ };
69
+ }
70
+
71
+ /** Walk an extension folder: every regular file under a valid name; symlinks and odd entries are problems. */
72
+ function readFolder(root) {
73
+ const files = new Map();
74
+ const problems = [];
75
+ let total = 0;
76
+ const walk = (dir, rel) => {
77
+ for (const ent of readdirSync(dir, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) {
78
+ const name = rel ? `${rel}/${ent.name}` : ent.name;
79
+ const full = join(dir, ent.name);
80
+ const st = lstatSync(full);
81
+ if (st.isSymbolicLink()) problems.push(`${name} is a symbolic link; a package carries plain files only.`);
82
+ else if (st.isDirectory()) {
83
+ if (!validFileName(name)) problems.push(said(new Refusal("bad_name", `${name}/`)));
84
+ else walk(full, name);
85
+ } else if (!st.isFile()) problems.push(`${name} is not a plain file.`);
86
+ else if (!validFileName(name)) problems.push(said(new Refusal("bad_name", name)));
87
+ else {
88
+ total += st.size;
89
+ if (total <= RULES.maxPackage) files.set(name, readFileSync(full));
90
+ }
91
+ }
92
+ };
93
+ walk(root, "");
94
+ if (total > RULES.maxPackage) problems.push(said(new Refusal("too_large")));
95
+ // SIGNATURE is added by pack and counts toward the cap.
96
+ if (files.size + 1 > RULES.maxFiles) problems.push(`${said(new Refusal("too_many_files"))} At most ${RULES.maxFiles - 1} files, plus the SIGNATURE pack adds.`);
97
+ const folded = new Map();
98
+ for (const n of files.keys()) {
99
+ const k = n.toLowerCase();
100
+ if (folded.has(k)) problems.push(`${said(new Refusal("duplicate_file", n))} ${folded.get(k)} and ${n} differ only in case.`);
101
+ folded.set(k, n);
102
+ }
103
+ if (files.has(RULES.signatureFile)) problems.push(`The folder holds a ${RULES.signatureFile} file; pack writes that itself, so remove it.`);
104
+ return { files, problems };
105
+ }
106
+
107
+ export function pack(folder, keyPath, outPath) {
108
+ const root = resolve(folder);
109
+ if (!existsSync(root) || !statSync(root).isDirectory()) return { ok: false, out: [`${root} is not a folder.`] };
110
+ if (!keyPath) return { ok: false, out: ["Name the private key with --key (make one with: rinext keygen <dir>)."] };
111
+ if (inside(keyPath, root)) return { ok: false, out: [`The key ${resolve(keyPath)} is inside the extension folder. Move it out first: everything in the folder ships.`] };
112
+ let key;
113
+ try {
114
+ key = loadPrivateKey(readFileSync(keyPath));
115
+ } catch (e) {
116
+ return { ok: false, out: [`${resolve(keyPath)} is not an Ed25519 private key (${e.code === "ENOENT" ? "no such file" : "unreadable"}).`] };
117
+ }
118
+ const { files, problems } = readFolder(root);
119
+ const checked = checkFiles(files);
120
+ for (const r of checked.problems) problems.push(said(r));
121
+ if (checked.manifest) {
122
+ const want = b64(rawPublicKey(key));
123
+ if (!decodeKey(checked.manifest.author.key)?.equals(rawPublicKey(key))) problems.push(`manifest.json's author.key does not match ${resolve(keyPath)}. For this key it is "${want}".`);
124
+ }
125
+ const out = resolve(outPath ?? (checked.manifest ? `${checked.manifest.id}-${checked.manifest.version}.rinext` : "extension.rinext"));
126
+ if (inside(out, root)) problems.push(`The output ${out} is inside the extension folder, where the next pack would refuse it. Write it elsewhere.`);
127
+ if (problems.length) return { ok: false, out: problems };
128
+
129
+ const sig = signFiles(files, key);
130
+ const names = [...files.keys()].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
131
+ const bytes = writeZip([...names.map((n) => [n, files.get(n)]), [RULES.signatureFile, sig]]);
132
+ // The app's own reading of what was just written, so a package pack writes is one the app installs.
133
+ try {
134
+ parsePackage(bytes);
135
+ } catch (e) {
136
+ if (e instanceof Refusal) return { ok: false, out: [said(e)] };
137
+ throw e;
138
+ }
139
+ writeFileSync(out, bytes);
140
+ return { ok: true, out: [`Packed ${checked.manifest.name} ${checked.manifest.version} (${files.size + 1} files, ${bytes.length} bytes) to ${out}`] };
141
+ }
142
+
143
+ /** What the install dialog would show for a package, or the one sentence it would refuse it with. */
144
+ export function describe(p) {
145
+ const m = p.manifest;
146
+ const lines = [
147
+ `${m.name} ${m.version}`,
148
+ ` id ${m.id}`,
149
+ ...(m.author.name ? [` author ${m.author.name}`] : []),
150
+ ` fingerprint ${p.fingerprint}`,
151
+ ` sha256 ${p.sha256}`,
152
+ " Unreviewed: installs from a file only with Trust extensions from any source on.",
153
+ ];
154
+ if (m.description) lines.push("", ...m.description.split("\n").map((l) => ` ${l}`));
155
+ const sentences = m.permissions.map((x) => PERMISSION_SENTENCES[x]).filter(Boolean);
156
+ if (m.net.length) sentences.push(netSentence(m.net));
157
+ lines.push("", "The install dialog says:");
158
+ lines.push(...(sentences.length ? sentences.map((s) => ` - ${s}`) : [" (no permission sentences)"]));
159
+ lines.push("", `Permissions: ${m.permissions.length ? m.permissions.join(", ") : "none"}`);
160
+ if (m.net.length) lines.push(`Requested hosts (asked for, never granted on install): ${m.net.join(", ")}`);
161
+ if (m.settings.length) lines.push(`Settings form: ${m.settings.map((f) => `${f.key} (${f.type})`).join(", ")}`);
162
+ return lines;
163
+ }
164
+
165
+ export function check(file) {
166
+ let bytes;
167
+ try {
168
+ const st = statSync(file);
169
+ if (st.size > RULES.maxPackage) return { ok: false, out: [said(new Refusal("too_large"))] };
170
+ bytes = readFileSync(file);
171
+ } catch {
172
+ return { ok: false, out: [`${resolve(file)} could not be read.`] };
173
+ }
174
+ try {
175
+ return { ok: true, out: describe(parsePackage(bytes)) };
176
+ } catch (e) {
177
+ if (e instanceof Refusal) return { ok: false, out: [`Rin Chat would refuse this package: ${said(e)}`] };
178
+ throw e;
179
+ }
180
+ }