fix: paths p4

This commit is contained in:
fn
2026-03-27 16:44:49 +00:00
parent 64ad88e650
commit 1072862113
2 changed files with 171 additions and 3 deletions

View File

@@ -228,8 +228,88 @@ async function resolveBlogManifestTxId() {
return match?.[1] || "";
}
async function resolveAoConfig() {
const source = await fs.readFile(sourceConfigPath, "utf8");
const aoUrlMatch = source.match(/AO_URL\s*=\s*"([^"]+)"/);
const aoProcessIdMatch = source.match(/AO_PROCESS_ID\s*=\s*"([^"]+)"/);
return {
aoUrl: aoUrlMatch?.[1] || "",
aoProcessId: aoProcessIdMatch?.[1] || ""
};
}
const asObject = (value) => (typeof value === "object" && value !== null ? value : null);
const asString = (value) => (typeof value === "string" && value.length > 0 ? value : null);
function findManifestIdInTags(tagsValue) {
const tags = Array.isArray(tagsValue) ? tagsValue : [];
for (const tag of tags) {
const tagObject = asObject(tag);
if (!tagObject) continue;
const name = asString(tagObject.name) ?? asString(tagObject.Name);
const value = asString(tagObject.value) ?? asString(tagObject.Value);
if (name === "LatestManifestId" && value) return value;
}
return "";
}
function extractManifestId(payload) {
const root = asObject(payload);
const results = asObject(root?.results);
if (!results) return "";
const outbox = asObject(results.outbox);
if (outbox) {
for (const message of Object.values(outbox)) {
const messageObject = asObject(message);
if (!messageObject) continue;
const direct = asString(messageObject.LatestManifestId);
if (direct) return direct;
const tagged = findManifestIdInTags(messageObject.Tags);
if (tagged) return tagged;
}
}
const raw = asObject(results.raw);
const rawMessages = Array.isArray(raw?.Messages) ? raw.Messages : [];
for (const message of rawMessages) {
const messageObject = asObject(message);
if (!messageObject) continue;
const tagged = findManifestIdInTags(messageObject.Tags);
if (tagged) return tagged;
}
const json = asObject(results.json);
const body = json?.body;
const parsedBody = typeof body === "string" ? asObject(JSON.parse(body)) : asObject(body);
const jsonMessages = Array.isArray(parsedBody?.Messages) ? parsedBody.Messages : [];
for (const message of jsonMessages) {
const messageObject = asObject(message);
if (!messageObject) continue;
const tagged = findManifestIdInTags(messageObject.Tags);
if (tagged) return tagged;
}
return "";
}
async function getLatestManifestFromAo() {
const { aoUrl, aoProcessId } = await resolveAoConfig();
if (!aoUrl || !aoProcessId) return "";
try {
const url = `${aoUrl}/${aoProcessId}~process@1.0/compute?Action=Get&require-codec=application/json&accept-bundle=true`;
const response = await fetch(url, { cache: "no-store" });
if (!response.ok) return "";
const payload = await response.json();
return extractManifestId(payload);
} catch {
return "";
}
}
async function resolvePostSlugs(gateway) {
const blogManifestTxId = await resolveBlogManifestTxId();
const blogManifestTxId = (await getLatestManifestFromAo()) || (await resolveBlogManifestTxId());
if (!blogManifestTxId) return [];
try {