- gen-market 产出 <id>-<version>.zip(STORE)+ bundle/bundleSize/bundleSha256/files[];保留单文件兼容 - 安装器:下载→整包校验→解包→逐文件 sha256→路径白名单→体积/数量上限→.new→.old→rename→清理(失败回滚) - storage-file 增 renameDir/existsDir;卸载目录插件保留 plugin-data - 新增 scripts/plugin-zip.js 与 plugin-bundle / plugin-install 两组测试(含覆盖升级与拒绝路径)
108 lines
4.9 KiB
JavaScript
108 lines
4.9 KiB
JavaScript
#!/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/<id>-<version>.zip 目录插件包:manifest.json + main.js [+ styles.css / assets]
|
||
// market/plugins/plugin-<id>.<hash>.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) })
|
||
}
|
||
}
|
||
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 解到 <plugins>/<id>/ 时,
|
||
// 加载器扫描目录也能校验入口文件(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')) |