gongxiangp2/apps/detail-agent-pi/scripts/client-package-integrity.mjs

85 lines
3.9 KiB
JavaScript

import fs from 'node:fs/promises';
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';
export const UI_FONT_LICENSE = 'assets/ui-fonts/LICENSE-HarmonyOS-Sans.txt';
export const NATIVE_NOTICES = ['LICENSE-MIT', 'LICENSE-APACHE', 'NOTICE', 'ATTRIBUTION.md', UI_FONT_LICENSE];
export const UI_FONT_FILES = [
'assets/ui-fonts/HarmonyOS_SansSC_Regular.ttf',
'assets/ui-fonts/HarmonyOS_SansSC_Medium.ttf',
'assets/ui-fonts/HarmonyOS_SansSC_Semibold.ttf',
];
export const NATIVE_FILES = [...NATIVE_NOTICES, ...UI_FONT_FILES];
export const LAUNCHER = '启动客户端.cmd';
export const BUILD_README = '本地构建说明.txt';
const MANIFEST = 'build-manifest.json';
const agentFiles = ['node.exe', 'LICENSE-Node.txt', 'package.json', 'package-lock.json'];
export const AGENT_DIRECTORIES = ['src', 'vendor', 'node_modules'];
export async function regularFile(file) {
const stat = await fs.lstat(file).catch(() => null);
if (!stat?.isFile() || stat.size === 0) throw Error('运行包缺少有效文件:' + file);
}
export async function inventoryFiles(destination) {
const files = [];
async function visit(dir, prefix) {
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
const file = path.join(dir, entry.name);
const relative = prefix + entry.name;
if (entry.isDirectory()) {
await visit(file, relative + '/');
} else if (!entry.isFile()) {
throw Error('运行包不接受非普通文件:' + relative);
} else if (relative !== MANIFEST) {
let bytes = 0;
const hash = createHash('sha256');
for await (const chunk of createReadStream(file)) {
bytes += chunk.length;
hash.update(chunk);
}
files.push({ path: relative, bytes, sha256: hash.digest('hex') });
}
}
}
await visit(destination, '');
return files.sort((a, b) => a.path.localeCompare(b.path, 'en'));
}
async function directoryExists(directory) {
if (!(await fs.lstat(directory)).isDirectory()) throw Error('运行包目录无效:' + directory);
}
async function onlyEntries(directory, allowed) {
await directoryExists(directory);
for (const name of await fs.readdir(directory)) {
if (!allowed.includes(name)) throw Error('运行包含未批准文件:' + path.join(directory, name));
}
}
export async function checkClientPackage({ destination }) {
const rootFiles = ['photocraft.exe', LAUNCHER, BUILD_README, MANIFEST, ...NATIVE_NOTICES.filter(file => !file.includes('/'))];
await onlyEntries(destination, [...rootFiles, 'assets', 'agent']);
await onlyEntries(path.join(destination, 'assets'), ['ui-fonts']);
const uiAssets = [UI_FONT_LICENSE, ...UI_FONT_FILES];
await onlyEntries(path.join(destination, 'assets/ui-fonts'), uiAssets.map(file => path.posix.basename(file)));
const agent = path.join(destination, 'agent');
await onlyEntries(agent, [...agentFiles, ...AGENT_DIRECTORIES]);
for (const file of [...rootFiles, ...uiAssets]) await regularFile(path.join(destination, file));
for (const file of agentFiles) await regularFile(path.join(agent, file));
for (const dir of AGENT_DIRECTORIES) await directoryExists(path.join(agent, dir));
const manifest = JSON.parse(await fs.readFile(path.join(destination, MANIFEST), 'utf8'));
const files = await inventoryFiles(destination);
const expected = new Map(manifest.files.map(file => [file.path, file]));
if (manifest.files.length !== files.length || expected.size !== files.length) throw Error('运行包文件清单不匹配');
for (const file of files) {
const entry = expected.get(file.path);
if (entry?.bytes !== file.bytes || entry.sha256 !== file.sha256) throw Error('运行包文件校验失败:' + file.path);
}
if (manifest.bytes !== files.reduce((sum, file) => sum + file.bytes, 0)) throw Error('运行包文件清单总大小不匹配');
return { packageReady: true, files: files.length, bytes: manifest.bytes };
}