#!/usr/bin/env node // `bun run check:store`: what a contributor runs before a pull request that adds or changes a // Store item, and what a maintainer runs before merging. No key is needed and nothing is written // to the repository. // // node scripts/check-store.mjs check every item under store/x/ and the published catalog // node scripts/check-store.mjs --verbose also print the notes (not failures) // // It checks, for every folder under store/x/ (layout: store/README.md): // - ids, file names, item.json fields, categories and tags, duplicates // - plugins: the rules the app applies at install (waybetter-host `validate_plugin`): UTF-8 text, // a header with @name and @version, valid @permissions; plus the Store's own: a description, // a @permissions line that names network or network:none, gated API calls declared, and no // code loaded or evaluated at run time (eval, new Function, import()) // - themes: a JSON object the app's sanitizer accepts, no outside files in its CSS // - previews: .svg or .png, 16:10, at least 640×400, at most 300 KB, nothing scriptable // - media for the item's page (item.json "cover", "screenshots", "video"; files in media/): // PNG, WebP or JPEG up to 400 KB and 1600 px wide with alt text, a 16:10 cover, at most six // screenshots; one MP4 (H.264) or WebM up to 6 MB and 20 s, without sound, with a poster // - search and sharing: the page title (≤ 60) and description (50–160) the site generates // then builds the catalog in a temporary folder (the framework's rules, the ones the apps enforce) // and compares it with the published one in apps/web/public/store/: the signature, every file's // size and SHA-256, and which items a maintainer still has to build and sign. import { cpSync, existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { buildStore, catalogProblems, sha256, verifyFile } from '../waybetter/packages/waybetter/cli/store.mjs'; import config, { APP, source } from './store.config.mjs'; import { CATEGORIES } from './store/source.mjs'; import { readAreaSource } from './store/areas.mjs'; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const PUBLISHED = join(ROOT, 'apps/web/public/store'); const PUBLIC_KEY = join(ROOT, 'apps/desktop/src-tauri/keys/store.pub'); const MAX_ITEM_BYTES = 2 * 1024 * 1024; const MAX_PREVIEW_BYTES = 300 * 1024; const verbose = process.argv.includes('--verbose'); const errors = []; const notes = []; const fail = (where, message) => errors.push(`${where}: ${message}`); const note = (where, message) => notes.push(`${where}: ${message}`); /** What waybetter-host `source_findings` looks for; `network:none` with any of the first five is refused. */ const NETWORK = [['fetch(', 'fetch'], ['XMLHttpRequest', 'XMLHttpRequest'], ['WebSocket', 'WebSocket'], ['EventSource', 'EventSource'], ['sendBeacon', 'sendBeacon']]; const RUNTIME_CODE = [[/\bimport\s*\(/, 'import()'], [/\beval\b/, 'eval'], [/\bnew\s+Function\b/, 'new Function']]; /** The gated part of the plugin API (docs/PLUGINS.md → Permissions). */ const GATED = [ ['storage', /\bapi\.storage\s*\(/], ['ui', /\bapi\.(ui\b|toast\s*\()|\bscope\.ui\b/], ['clipboard', /\bapi\.copyText\s*\(/], ['account', /\bapi\.account\s*\(/], ['users', /\bapi\.users\b/], ['commands', /\bapi\.commands\b|\bscope\.commands\b/], ]; function checkPlugin(entry) { const where = `${entry.dir}/${entry.file}`; const bytes = Buffer.isBuffer(entry.bytes) ? entry.bytes : Buffer.from(entry.bytes); if (!bytes.length) return fail(where, 'the file is empty'); if (bytes.length > MAX_ITEM_BYTES) fail(where, 'plugins are limited to 2 MiB'); const text = bytes.toString('utf8'); if (!Buffer.from(text, 'utf8').equals(bytes)) return fail(where, 'a plugin must be UTF-8 text'); // eslint-disable-next-line no-control-regex if (/[\u0000-\u0008\u000b\u000e-\u001f\u007f-\u009f�]/.test(text)) return fail(where, 'the file contains binary data, not JavaScript text'); const start = text.replace(/^/, '').trimStart(); if (!start.startsWith('/**') || !start.includes('*/')) return fail(where, 'a plugin starts with a /** … */ header (@name, @version, @description, @author, @permissions)'); const header = start.slice(0, start.indexOf('*/')); if (!header.includes('@name')) fail(where, 'the header needs an @name'); if (!header.includes('@description') || !entry.description) fail(where, 'the header needs an @description'); if (!entry.firstParty && !entry.author) fail(where, 'the header needs an @author'); if ([...header.matchAll(/@description\s*(.*)/g)].some(match => [...match[1].trim()].length > 300)) note(where, 'the @description is longer than 300 characters; the app and the Store keep the first 300'); const permissions = entry.permissions; if (permissions == null) fail(where, 'the header needs a @permissions line (for example "@permissions storage, ui, network:none"); a plugin without one is "legacy" and is not accepted in the Store'); else { if (permissions.includes('network') && permissions.includes('network:none')) fail(where, '@permissions: "network" and "network:none" contradict each other'); if (!permissions.includes('network') && !permissions.includes('network:none')) fail(where, '@permissions must say whether the plugin contacts a server: add "network" or "network:none"'); const used = NETWORK.filter(([needle]) => text.includes(needle)).map(([, name]) => name); if (permissions.includes('network:none') && used.length) fail(where, `the header says network:none but the code uses ${used.join(', ')}`); for (const [permission, pattern] of GATED) { if (pattern.test(text) && !permissions.includes(permission)) fail(where, `the code uses the "${permission}" part of the API but @permissions does not declare it`); } } const loaded = RUNTIME_CODE.filter(([pattern]) => pattern.test(text)).map(([, name]) => name); if (loaded.length) fail(where, `a Store plugin may not load or evaluate code at run time (${loaded.join(', ')}): the code that is reviewed and signed must be the code that runs`); if (/]|createElement\(\s*['"`]script['"`]\s*\)/i.test(text)) fail(where, 'a Store plugin may not add