Files
qiqi e08fdb6271 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 两组测试(含覆盖升级与拒绝路径)
2026-11-20 10:20:09 +08:00

89 lines
3.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 }