import { AwsClient } from "aws4fetch"; const client = (env) => new AwsClient({ accessKeyId: env.S3_ACCESS_KEY_ID, secretAccessKey: env.S3_SECRET_ACCESS_KEY, service: "s3", region: "ru-central1", retries: 1, }); const endpoint = (env, key) => "https://storage.yandexcloud.net/" + env.S3_BUCKET + "/" + key.split("/").map(encodeURIComponent).join("/"); export async function put( env, key, body, contentType = "application/json; charset=utf-8", condition = {}, ) { const immutable = key.startsWith("editions/") || key.startsWith("media/"); const res = await client(env).fetch(endpoint(env, key), { method: "PUT", headers: { "Content-Type": contentType, "Cache-Control": immutable ? "public, max-age=31536000, immutable" : "no-cache, max-age=0, must-revalidate", ...condition, }, body, }); if ([409, 412].includes(res.status)) return false; if (!res.ok) throw Error( "Object Storage PUT " + key + " " + res.status + " " + (await res.text()).match(/(.*?)<\/Code>/)?.[1], ); return true; } export async function object(env, key) { const res = await client(env).fetch(endpoint(env, key), { headers: { "Cache-Control": "no-cache" }, }); if (res.status === 404) return null; if (!res.ok) throw Error("Object Storage GET " + key + " " + res.status); return { body: await res.json(), etag: res.headers.get("etag") }; } export async function updateManifest(env, change) { for (let i = 0; i < 5; i++) { const previous = await object(env, "manifest.json"); const next = change(previous?.body || { latest: null, dates: [] }); if (!next) return; if ( await put( env, "manifest.json", JSON.stringify(next), undefined, previous ? { "If-Match": previous.etag } : { "If-None-Match": "*" }, ) ) return; } throw Error("Параллельная запись оглавления. Требуется повтор."); } export function manifestFor(old, date) { const dates = [...new Set([...(old.dates || []), date])].sort().reverse(); const next = { ...old, latest: dates[0], dates }; if (!old.warningDate || old.warningDate <= date) { delete next.warning; delete next.warningDate; } return next; } export async function writeJournal(env, sourceNames) { const data = await env.DB.prepare( "SELECT * FROM runs ORDER BY started_at DESC LIMIT 18", ).all(); const recent = data.results; const ids = recent.map((r) => r.id); if (!ids.length) return; const src = await env.DB.prepare( "SELECT * FROM source_runs WHERE run_id IN (" + ids.map(() => "?").join(",") + ") ORDER BY source_id", ) .bind(...ids) .all(); const runs = recent.map((r) => ({ id: r.id, date: r.issue_date, stage: r.stage, startedAt: r.started_at, finishedAt: r.finished_at, outcome: r.outcome, details: r.details, sources: src.results .filter((s) => s.run_id === r.id) .map((s) => ({ id: s.source_id, name: sourceNames[s.source_id] || s.source_id, status: s.outcome, checkedAt: s.checked_at, discovered: s.discovered, details: s.details, })), })); await put( env, "status.json", JSON.stringify({ updatedAt: new Date().toISOString(), runs }), ); }