40 lines
2.0 KiB
JavaScript
40 lines
2.0 KiB
JavaScript
import { productionTarget } from './production-settings.mjs';
|
|
import { pathToFileURL } from 'node:url';
|
|
import { setTimeout as wait } from 'node:timers/promises';
|
|
|
|
export async function verifyProduction(fetchImpl = fetch, revision = process.env.DEPLOY_COMMIT_SHA) {
|
|
const origin = productionTarget.origin;
|
|
const request = (path) => fetchImpl(origin + path, { redirect: 'manual', signal: AbortSignal.timeout(15000), headers: { 'cache-control': 'no-cache' } });
|
|
const session = await request('/auth/session');
|
|
if (!session.ok) throw new Error('Production session endpoint failed');
|
|
const body = await session.json();
|
|
if (body.authenticated !== false || body.user !== null) throw new Error('Production session endpoint returned an unexpected anonymous session');
|
|
const asset = await request('/assets/styles.css');
|
|
if (!asset.ok || !String(asset.headers.get('content-type')).includes('text/css') || !(await asset.text()).trim()) {
|
|
throw new Error('Production stylesheet is unavailable');
|
|
}
|
|
if (revision && asset.headers.get('x-qa-release') !== 'releases/' + revision) {
|
|
throw new Error('Production stylesheet does not match the deployed commit');
|
|
}
|
|
const protectedPage = await request('/egbim/write.html');
|
|
const login = new URL(protectedPage.headers.get('location') || '/', origin);
|
|
if (protectedPage.status !== 302 || login.origin !== origin || login.pathname !== '/auth/login') {
|
|
throw new Error('Production write page must redirect anonymous visitors to login');
|
|
}
|
|
console.log('Production HTTP checks passed: session, stylesheet, protected write page.');
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
for (let attempt = 1; attempt <= 5; attempt++) {
|
|
try { await verifyProduction(); break; } catch (error) {
|
|
if (attempt === 5) {
|
|
console.error(error.message);
|
|
process.exitCode = 1;
|
|
} else {
|
|
console.log('Waiting for production deployment propagation (' + attempt + '/5).');
|
|
await wait(5000);
|
|
}
|
|
}
|
|
}
|
|
}
|