feat(market): M3 zip 整包安装 + 逐文件 sha256 + 原子换位 v3.25.46
- 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 两组测试(含覆盖升级与拒绝路径)
This commit is contained in:
1 parent
56e64decd3
commit
e08fdb6271
8 files changed
+532
-45
No files matched your search
+95
-23
@@ -1,36 +1,108 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict'
|
||||
// 生成插件市场索引:plugins/index.json + plugins/meta.json → market/index.json + market/plugins/*
|
||||
// 产物用于 market.maqiart.eu.org(纯静态)。
|
||||
// 生成插件市场索引(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 plugins = []
|
||||
let skipped = 0
|
||||
for (const e of man) {
|
||||
// 目录插件(v3.25.41):市场打包(zip)在 M3 落地,这一版先跳过,避免生成缺 bundle 的条目
|
||||
if (e.dir) { skipped++; console.warn('跳过目录插件(待 M3 打包):' + e.id); continue }
|
||||
const src = path.join(root, 'plugins', 'runtime', e.file)
|
||||
if (!fs.existsSync(src)) { console.warn('缺少产物:' + e.file); continue }
|
||||
const buf = fs.readFileSync(src)
|
||||
fs.writeFileSync(path.join(out, 'plugins', e.file), buf)
|
||||
const m = meta[e.id] || {}
|
||||
plugins.push({
|
||||
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,
|
||||
file: 'plugins/' + e.file,
|
||||
size: buf.length,
|
||||
sha256: sha(buf).digest('hex'),
|
||||
integrity: 'sha256-' + sha(buf).digest('base64'),
|
||||
})
|
||||
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 index = { version: 1, updatedAt: new Date().toISOString(), plugins: plugins }
|
||||
|
||||
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 + ' 个插件' + (skipped ? '(跳过 ' + skipped + ' 个目录插件)' : ''))
|
||||
for (const p of plugins) console.log(' ' + p.id.padEnd(16) + p.version.padEnd(8) + (p.size + ' B'))
|
||||
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'))
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict'
|
||||
// 插件包 ZIP 写入器(M3,2026-11-20)
|
||||
//
|
||||
// 为什么自己写:插件包很小(几十 KB),只需要 STORE(不压缩);
|
||||
// 而客户端解包走 src/storage-zip.js —— 它对 DEFLATE 依赖 DecompressionStream,
|
||||
// 旧 macOS(<13)的 WKWebView 没有该 API。用 STORE 可以彻底绕开这个兼容坑。
|
||||
//
|
||||
// 输出:标准 ZIP(local header + central directory + EOCD),UTF-8 文件名(flag 0x0800)。
|
||||
const CRC_TABLE = (() => {
|
||||
const t = new Uint32Array(256)
|
||||
for (let i = 0; i < 256; i++) {
|
||||
let c = i
|
||||
for (let k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)
|
||||
t[i] = c >>> 0
|
||||
}
|
||||
return t
|
||||
})()
|
||||
|
||||
function crc32 (buf) {
|
||||
let c = 0xFFFFFFFF
|
||||
for (let i = 0; i < buf.length; i++) c = (CRC_TABLE[(c ^ buf[i]) & 0xFF] ^ (c >>> 8)) >>> 0
|
||||
return (c ^ 0xFFFFFFFF) >>> 0
|
||||
}
|
||||
|
||||
// files: [{ name: string, data: Buffer|string }]
|
||||
function zipStore (files) {
|
||||
const locals = []
|
||||
const centrals = []
|
||||
let offset = 0
|
||||
const DOS_TIME = 0
|
||||
const DOS_DATE = 0x21 // 1980-01-01
|
||||
for (const f of files || []) {
|
||||
const name = String(f.name || '')
|
||||
if (!name || name.indexOf('..') >= 0 || name.charAt(0) === '/') throw new Error('非法文件名:' + name)
|
||||
const nameBuf = Buffer.from(name, 'utf8')
|
||||
const data = Buffer.isBuffer(f.data) ? f.data : Buffer.from(String(f.data == null ? '' : f.data), 'utf8')
|
||||
const crc = crc32(data)
|
||||
|
||||
const local = Buffer.alloc(30)
|
||||
local.writeUInt32LE(0x04034b50, 0)
|
||||
local.writeUInt16LE(20, 4) // version needed
|
||||
local.writeUInt16LE(0x0800, 6) // UTF-8 文件名
|
||||
local.writeUInt16LE(0, 8) // method = STORE
|
||||
local.writeUInt16LE(DOS_TIME, 10)
|
||||
local.writeUInt16LE(DOS_DATE, 12)
|
||||
local.writeUInt32LE(crc, 14)
|
||||
local.writeUInt32LE(data.length, 18)
|
||||
local.writeUInt32LE(data.length, 22)
|
||||
local.writeUInt16LE(nameBuf.length, 26)
|
||||
local.writeUInt16LE(0, 28)
|
||||
locals.push(local, nameBuf, data)
|
||||
|
||||
const cd = Buffer.alloc(46)
|
||||
cd.writeUInt32LE(0x02014b50, 0)
|
||||
cd.writeUInt16LE(20, 4) // made by
|
||||
cd.writeUInt16LE(20, 6) // version needed
|
||||
cd.writeUInt16LE(0x0800, 8)
|
||||
cd.writeUInt16LE(0, 10) // method
|
||||
cd.writeUInt16LE(DOS_TIME, 12)
|
||||
cd.writeUInt16LE(DOS_DATE, 14)
|
||||
cd.writeUInt32LE(crc, 16)
|
||||
cd.writeUInt32LE(data.length, 20)
|
||||
cd.writeUInt32LE(data.length, 24)
|
||||
cd.writeUInt16LE(nameBuf.length, 28)
|
||||
cd.writeUInt16LE(0, 30) // extra len
|
||||
cd.writeUInt16LE(0, 32) // comment len
|
||||
cd.writeUInt16LE(0, 34) // disk start
|
||||
cd.writeUInt16LE(0, 36) // internal attrs
|
||||
cd.writeUInt32LE(0, 38) // external attrs
|
||||
cd.writeUInt32LE(offset, 42) // local header offset
|
||||
centrals.push(cd, nameBuf)
|
||||
|
||||
offset += local.length + nameBuf.length + data.length
|
||||
}
|
||||
const cdBuf = Buffer.concat(centrals)
|
||||
const end = Buffer.alloc(22)
|
||||
end.writeUInt32LE(0x06054b50, 0)
|
||||
end.writeUInt16LE(0, 4)
|
||||
end.writeUInt16LE(0, 6)
|
||||
end.writeUInt16LE(files.length, 8)
|
||||
end.writeUInt16LE(files.length, 10)
|
||||
end.writeUInt32LE(cdBuf.length, 12)
|
||||
end.writeUInt32LE(offset, 16)
|
||||
end.writeUInt16LE(0, 20)
|
||||
return Buffer.concat(locals.concat([cdBuf, end]))
|
||||
}
|
||||
|
||||
module.exports = { zipStore, crc32 }
|
||||
Reference in new issue
Block a user