Use reference-backed hyperzine manifest
This commit is contained in:
@@ -5,6 +5,7 @@ import { spawn } from "node:child_process";
|
||||
import Arweave from "arweave";
|
||||
import mime from "mime-types";
|
||||
import arbundles from "warp-arbundles";
|
||||
import { ReferenceClient } from "@permaweb/references";
|
||||
|
||||
const { createData, ArweaveSigner } = arbundles;
|
||||
|
||||
@@ -228,6 +229,25 @@ async function resolveBlogManifestTxId() {
|
||||
return match?.[1] || "";
|
||||
}
|
||||
|
||||
async function resolveBlogManifestReferenceName() {
|
||||
const source = await fs.readFile(sourceConfigPath, "utf8");
|
||||
const match = source.match(/MANIFEST_REFERENCE_NAME\s*=\s*"([^"]+)"/);
|
||||
return match?.[1] || "";
|
||||
}
|
||||
|
||||
async function getLatestManifestFromReference(gateway) {
|
||||
const referenceName = await resolveBlogManifestReferenceName();
|
||||
if (!referenceName) return "";
|
||||
|
||||
try {
|
||||
const names = new ReferenceClient({ gateway });
|
||||
const value = await names.resolveName(referenceName);
|
||||
return typeof value === "string" ? value : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveAoConfig() {
|
||||
const source = await fs.readFile(sourceConfigPath, "utf8");
|
||||
const aoUrlMatch = source.match(/AO_URL\s*=\s*"([^"]+)"/);
|
||||
@@ -308,21 +328,51 @@ async function getLatestManifestFromAo() {
|
||||
}
|
||||
}
|
||||
|
||||
async function resolvePostSlugs(gateway) {
|
||||
const blogManifestTxId = (await getLatestManifestFromAo()) || (await resolveBlogManifestTxId());
|
||||
if (!blogManifestTxId) return [];
|
||||
function timestampMs(value) {
|
||||
if (typeof value !== "string" || !value) return 0;
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? 0 : parsed;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${gateway.replace(/\/+$/, "")}/${blogManifestTxId}`);
|
||||
if (!response.ok) return [];
|
||||
const payload = await response.json();
|
||||
if (!Array.isArray(payload?.posts)) return [];
|
||||
return payload.posts
|
||||
.map((post) => (typeof post?.slug === "string" ? post.slug.trim() : ""))
|
||||
.filter((slug) => slug.length > 0);
|
||||
} catch {
|
||||
return [];
|
||||
function manifestFreshness(payload) {
|
||||
const posts = Array.isArray(payload?.posts) ? payload.posts : [];
|
||||
return Math.max(
|
||||
timestampMs(payload?.updated),
|
||||
timestampMs(payload?.date),
|
||||
timestampMs(payload?.generatedAt),
|
||||
...posts.map((post) => Math.max(timestampMs(post?.updated), timestampMs(post?.publishedAt)))
|
||||
);
|
||||
}
|
||||
|
||||
async function resolvePostSlugs(gateway) {
|
||||
const candidates = [
|
||||
await getLatestManifestFromReference(gateway),
|
||||
await getLatestManifestFromAo(),
|
||||
await resolveBlogManifestTxId()
|
||||
].filter(Boolean);
|
||||
|
||||
const manifests = [];
|
||||
for (const blogManifestTxId of candidates) {
|
||||
try {
|
||||
const response = await fetch(`${gateway.replace(/\/+$/, "")}/${blogManifestTxId}`);
|
||||
if (!response.ok) continue;
|
||||
const payload = await response.json();
|
||||
if (!Array.isArray(payload?.posts)) continue;
|
||||
manifests.push({
|
||||
txId: blogManifestTxId,
|
||||
freshness: manifestFreshness(payload),
|
||||
posts: payload.posts
|
||||
});
|
||||
} catch {
|
||||
// Try the next manifest source.
|
||||
}
|
||||
}
|
||||
|
||||
if (!manifests.length) return [];
|
||||
manifests.sort((a, b) => b.freshness - a.freshness);
|
||||
return manifests[0].posts
|
||||
.map((post) => (typeof post?.slug === "string" ? post.slug.trim() : ""))
|
||||
.filter((slug) => slug.length > 0);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@@ -396,8 +446,9 @@ async function main() {
|
||||
|
||||
const slugs = await resolvePostSlugs(gateway);
|
||||
for (const slug of slugs) {
|
||||
pathMap[slug] = { id: indexTxId };
|
||||
pathMap[`${slug}/`] = { id: indexTxId };
|
||||
const routeIndexId = pathMap[`${slug}/index.html`]?.id || indexTxId;
|
||||
pathMap[slug] = { id: routeIndexId };
|
||||
pathMap[`${slug}/`] = { id: routeIndexId };
|
||||
}
|
||||
|
||||
const manifest = {
|
||||
|
||||
Reference in New Issue
Block a user