69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
import { verifyIntegrity } from '@/lib/exporter/verify'
|
|
|
|
export type ReadText = (path: string) => Promise<string>
|
|
export type Logger = (line: string) => void
|
|
|
|
// base64 -> Uint8Array
|
|
function fromBase64(b64: string): Uint8Array {
|
|
if (typeof Buffer !== 'undefined') return new Uint8Array(Buffer.from(b64, 'base64'))
|
|
throw new Error('Buffer not available')
|
|
}
|
|
|
|
// parse args like: [--extras, path, --assets, path]
|
|
function parseArgs(argv: string[]): { extras?: string; assets?: string } {
|
|
const out: { extras?: string; assets?: string } = {}
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const a = argv[i]
|
|
if (a === '--extras') out.extras = argv[++i]
|
|
else if (a === '--assets') out.assets = argv[++i]
|
|
}
|
|
return out
|
|
}
|
|
|
|
export async function verifyCli(argv: string[], readText: ReadText, log: Logger): Promise<number> {
|
|
try {
|
|
const { extras: extrasPath, assets: assetsPath } = parseArgs(argv)
|
|
if (!extrasPath || !assetsPath) {
|
|
log('Usage: verify --extras <extras.json> --assets <assets.json>')
|
|
return 2
|
|
}
|
|
const extrasJson = await readText(extrasPath)
|
|
const assetsJson = await readText(assetsPath)
|
|
let extrasMap: Record<string, string>
|
|
let assetsMap: Record<string, string>
|
|
try {
|
|
extrasMap = JSON.parse(extrasJson)
|
|
assetsMap = JSON.parse(assetsJson)
|
|
} catch (e) {
|
|
log('Invalid JSON input files')
|
|
return 1
|
|
}
|
|
// decode base64 into Uint8Array maps
|
|
const extrasBytes: Record<string, Uint8Array> = {}
|
|
for (const [k, v] of Object.entries(extrasMap)) {
|
|
try {
|
|
extrasBytes[k] = fromBase64(v)
|
|
} catch {
|
|
// mark invalid; let verifyIntegrity handle missing/invalid as possible
|
|
log(`Failed to decode base64 for extras entry: ${k}`)
|
|
return 1
|
|
}
|
|
}
|
|
const assetsBytes: Record<string, Uint8Array> = {}
|
|
for (const [k, v] of Object.entries(assetsMap)) {
|
|
try {
|
|
assetsBytes[k] = fromBase64(v)
|
|
} catch {
|
|
log(`Failed to decode base64 for asset entry: ${k}`)
|
|
return 1
|
|
}
|
|
}
|
|
const result = verifyIntegrity(extrasBytes, assetsBytes)
|
|
log(JSON.stringify(result, null, 2))
|
|
return result.ok ? 0 : 1
|
|
} catch (e) {
|
|
log(String(e instanceof Error ? e.message : e))
|
|
return 1
|
|
}
|
|
}
|