48 lines
2.1 KiB
JavaScript
48 lines
2.1 KiB
JavaScript
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { packageRelease } from './package-release.mjs';
|
|
import { parsePointer, parseSnapshot } from '../src/lib/releases.mts';
|
|
|
|
const origin = 'https://marqdo.com';
|
|
|
|
async function get(path, limit) {
|
|
const response = await fetch(new URL(path, origin), { redirect: 'error', signal: AbortSignal.timeout(30_000) });
|
|
if (!response.ok || !response.body) throw new Error(`${path}: HTTP ${response.status}`);
|
|
const chunks = [];
|
|
let length = 0;
|
|
for await (const chunk of response.body) {
|
|
length += chunk.byteLength;
|
|
if (length > limit) throw new Error(`Public data exceeds limit at ${path}`);
|
|
chunks.push(chunk);
|
|
}
|
|
return Buffer.concat(chunks);
|
|
}
|
|
|
|
const rootSha = process.env.MARQDO_DATA_ROOT_SHA;
|
|
if (!/^[a-f0-9]{40}$/.test(rootSha ?? '')) throw new Error('Set MARQDO_DATA_ROOT_SHA to the root identity expected by the trusted build');
|
|
const websiteSha = process.env.CI_COMMIT_SHA;
|
|
if (!/^[a-f0-9]{40}$/.test(websiteSha ?? '')) throw new Error('Release build requires the website source SHA in CI_COMMIT_SHA');
|
|
const pointerBytes = await get('/data/current.json', 4096);
|
|
const pointer = parsePointer(JSON.parse(pointerBytes.toString('utf8')));
|
|
const snapshotBytes = await get(pointer.snapshot, 16 * 1024 * 1024);
|
|
parseSnapshot(pointer, snapshotBytes, rootSha);
|
|
|
|
const dir = await mkdtemp(join(tmpdir(), 'marqdo-site-data-'));
|
|
try {
|
|
const pointerFile = join(dir, 'pointer.json');
|
|
const snapshotFile = join(dir, 'snapshot.json');
|
|
await writeFile(pointerFile, pointerBytes);
|
|
await writeFile(snapshotFile, snapshotBytes);
|
|
const result = spawnSync('npm', ['run', 'build'], {
|
|
stdio: 'inherit',
|
|
env: { ...process.env, MARQDO_RELEASE_BUILD: '1', MARQDO_POINTER_FILE: pointerFile, MARQDO_SNAPSHOT_FILE: snapshotFile },
|
|
});
|
|
if (result.error) throw result.error;
|
|
if (result.status !== 0) process.exitCode = result.status ?? 1;
|
|
else packageRelease(websiteSha, rootSha, pointer);
|
|
} finally {
|
|
await rm(dir, { recursive: true, force: true });
|
|
}
|