#!/usr/bin/env node 'use strict' // 生成插件市场索引(M3):plugins/index.json + plugins/meta.json(+ 目录插件)→ market/ // // 产物(纯静态,供 market.maqiart.eu.org 与桌面端安装器共用): // market/index.json 市场条目:bundle(zip) + files[逐文件 sha256] + 兼容字段 file // market/plugins/-.zip 目录插件包:manifest.json + main.js [+ styles.css / assets] // market/plugins/plugin-..js 单文件产物(旧客户端仍可安装,保持向后兼容) // // 包里统一用 STORE(不压缩):客户端 src/storage-zip.js 对 DEFLATE 依赖 DecompressionStream, // 旧 macOS 的 WKWebView 没有该 API;插件包只有几十 KB,不压缩没有实际损失。 const fs = require('fs') const path = require('path') const crypto = require('crypto') const { zipStore } = require('./plugin-zip') const root = path.resolve(__dirname, '..') const man = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'index.json'), 'utf8')) const meta = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'meta.json'), 'utf8')) const out = path.join(root, 'market') fs.mkdirSync(path.join(out, 'plugins'), { recursive: true }) const sha = (buf) => crypto.createHash('sha256').update(buf) const shaHex = (buf) => sha(buf).digest('hex') function readFolderFiles (id) { // 目录插件:manifest.json + main.js + 可选 styles.css + assets/**(限两级) const dir = path.join(root, 'plugins', id) const files = [] const manifestPath = path.join(dir, 'manifest.json') const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')) files.push({ name: 'manifest.json', data: fs.readFileSync(manifestPath) }) const main = String(manifest.main || 'main.js') files.push({ name: main, data: fs.readFileSync(path.join(dir, main)) }) if (manifest.style && fs.existsSync(path.join(dir, manifest.style))) files.push({ name: String(manifest.style), data: fs.readFileSync(path.join(dir, String(manifest.style))) }) const assetsDir = path.join(dir, 'assets') if (fs.existsSync(assetsDir)) { for (const name of fs.readdirSync(assetsDir)) { const abs = path.join(assetsDir, name) if (fs.statSync(abs).isFile()) files.push({ name: 'assets/' + name, data: fs.readFileSync(abs) }) } } // v3.26.109:manifest.files 是客户端装载时校验入口文件的依据;写成旧值会让市场安装直接 // 「完整性校验失败(main.js 的 sha256 不符)」(promptlib 1.14.0 就这样发出去过一次)。 // 生成阶段就对照真实字节,不符直接中断,别等用户装不上才发现。 const bad = [] for (const [name, expect] of Object.entries(manifest.files || {})) { const f = files.find((x) => x.name === name) if (!f) { bad.push(name + ' 缺失'); continue } const actual = shaHex(f.data) if (actual !== String(expect).toLowerCase()) bad.push(name + ' 期望 ' + String(expect).slice(0, 8) + ' 实际 ' + actual.slice(0, 8)) } if (bad.length) { console.error('[gen-market] ' + String(manifest.id) + ' 的 manifest.files 与实际字节不符:' + bad.join(';')) console.error('[gen-market] 修好 manifest.files 再发市场(否则客户端会拒绝装载)') process.exit(1) } return { files, manifest } } const plugins = [] let zipped = 0 for (const e of man) { const m = meta[e.id] || {} const base = { id: e.id, name: m.name || e.id, desc: m.desc || '', author: m.author || 'official', icon: m.icon || '', color: m.color || '', tags: Array.isArray(m.tags) ? m.tags : [], version: e.version, apiVersion: 1, } // ---- 目录形态:打包 manifest + main(+style/assets) ---- let files = null if (e.dir) { const got = readFolderFiles(e.dir) files = got.files } else { const src = path.join(root, 'plugins', 'runtime', e.file) if (!fs.existsSync(src)) { console.warn('缺少产物:' + e.file); continue } const js = fs.readFileSync(src) // 单文件插件也打成目录包:manifest.json 由市场索引生成,main.js 就是原产物 // manifest 里带 files(main.js 的 sha256):手工把 zip 解到 // 时, // 加载器扫描目录也能校验入口文件(manifest 自身不参与,避免自引用)。 const manifestJson = { id: e.id, name: base.name, version: e.version, apiVersion: 1, main: 'main.js', description: base.desc, author: base.author, icon: base.icon, tags: base.tags, files: { 'main.js': shaHex(js) }, } files = [ { name: 'manifest.json', data: Buffer.from(JSON.stringify(manifestJson, null, 2) + '\n', 'utf8') }, { name: 'main.js', data: js }, ] } const fileList = files.map((f) => ({ path: f.name, sha256: shaHex(f.data), size: f.data.length })) const zip = zipStore(files) const zipName = e.id + '-' + e.version + '.zip' fs.writeFileSync(path.join(out, 'plugins', zipName), zip) zipped++ const item = Object.assign({}, base, { bundle: 'plugins/' + zipName, bundleSize: zip.length, bundleSha256: shaHex(zip), files: fileList, }) // 兼容:单文件插件继续提供裸 .js(旧客户端 install 只认 file/integrity) if (!e.dir) { const src = path.join(root, 'plugins', 'runtime', e.file) const buf = fs.readFileSync(src) fs.writeFileSync(path.join(out, 'plugins', e.file), buf) item.file = 'plugins/' + e.file item.size = buf.length item.sha256 = shaHex(buf) item.integrity = 'sha256-' + sha(buf).digest('base64') } plugins.push(item) } const index = { version: 2, updatedAt: new Date().toISOString(), plugins: plugins } fs.writeFileSync(path.join(out, 'index.json'), JSON.stringify(index, null, 2) + '\n') console.log('market/index.json:' + plugins.length + ' 个插件 / ' + zipped + ' 个 zip 包') for (const p of plugins) console.log(' ' + p.id.padEnd(16) + String(p.version).padEnd(8) + (p.bundleSize + ' B zip'))