d3ro-voice/scripts/ci/lib/release-artifacts.mjs

64 lines
3 KiB
JavaScript

import { createHash } from 'node:crypto'
import { createReadStream } from 'node:fs'
import { readFile, readdir, rename, stat, writeFile } from 'node:fs/promises'
import { isAbsolute, join, relative, resolve, sep } from 'node:path'
const CHECKPOINT = 'release-artifacts.json'
function artifactPath(directory, name) {
const path = resolve(directory, name)
const rel = relative(resolve(directory), path)
if (!rel || isAbsolute(rel) || rel === '..' || rel.startsWith(`..${sep}`)) {
throw new Error(`Invalid release artifact path: ${name}`)
}
return path
}
async function fingerprint(directory, name) {
const path = artifactPath(directory, name)
const info = await stat(path)
if (!info.isFile()) throw new Error(`Release artifact is not a file: ${name}`)
const hash = createHash('sha256')
for await (const chunk of createReadStream(path)) hash.update(chunk)
return { name, size: info.size, sha256: hash.digest('hex') }
}
export async function saveReleaseArtifacts(directory, { version, tagCommit, withRuntime }) {
const names = await readdir(directory)
const installers = names.filter((name) => /-Setup-.*\.exe$/.test(name))
if (installers.length !== 1) throw new Error('Expected exactly one release installer')
const files = [installers[0], `${installers[0]}.blockmap`, 'latest.yml']
if (withRuntime) {
const portable = JSON.parse(await readFile(join(directory, 'portable.json'), 'utf8'))
const runtime = JSON.parse(await readFile(join(directory, 'runtime', 'runtime.json'), 'utf8'))
if (portable.version !== version || runtime.version !== version) throw new Error('Release index version mismatch')
files.push('portable.json', 'd3ro-voice.scoop.json', 'runtime/runtime.json')
for (const entry of [...portable.volumes, ...portable.zipParts]) files.push(entry.name)
for (const component of Object.values(runtime.components)) {
for (const part of component.parts) files.push(`runtime/${part.name}`)
}
}
const artifacts = []
for (const name of new Set(files)) artifacts.push(await fingerprint(directory, name))
const checkpoint = { version, tagCommit, withRuntime, artifacts }
const path = join(directory, CHECKPOINT)
await writeFile(`${path}.tmp`, `${JSON.stringify(checkpoint, null, 2)}\n`)
await rename(`${path}.tmp`, path)
}
export async function verifyReleaseArtifacts(directory, expected) {
const checkpoint = JSON.parse(await readFile(join(directory, CHECKPOINT), 'utf8'))
for (const field of ['version', 'tagCommit', 'withRuntime']) {
if (checkpoint[field] !== expected[field]) throw new Error(`Release checkpoint ${field} mismatch`)
}
if (!Array.isArray(checkpoint.artifacts) || checkpoint.artifacts.length < 3) {
throw new Error('Release checkpoint has no complete artifact set')
}
for (const entry of checkpoint.artifacts) {
const actual = await fingerprint(directory, entry.name)
if (actual.size !== entry.size || actual.sha256 !== entry.sha256) {
throw new Error(`Release artifact changed: ${entry.name}`)
}
}
return checkpoint
}