content: ao-the-asset pt2
This commit is contained in:
@@ -4,7 +4,7 @@ export const BLOG_PERMALINK_PREFIX = "https://386900368387k.arweave.net"
|
||||
export const BLOG_DEFAULT_DESCRIPTION =
|
||||
"A blog about cyberspace decentralization";
|
||||
export const BLOG_TWITTER_HANDLE = "";
|
||||
export const MANIFEST_TX_ID = "EnWM5yrSyOCGV9YSUe1bUYWsq_Yxg4uEaVF9ODdbWWk";
|
||||
export const MANIFEST_TX_ID = "hm2oq70wCqZ89MjQNpnz1n2Ng6vjCsvZ5wMSzJvbR_g";
|
||||
export const ARWEAVE_GATEWAY = "https://arweave.net";
|
||||
|
||||
export const AO_URL = "https://push-1.forward.computer";
|
||||
|
||||
104
src/lib.ts
104
src/lib.ts
@@ -1,7 +1,7 @@
|
||||
import DOMPurify from "dompurify";
|
||||
import { marked, type Tokens } from "marked";
|
||||
import { parse as parseYaml } from "yaml";
|
||||
import { AO_PROCESS_ID, AO_URL, ARWEAVE_GATEWAY, MANIFEST_TX_ID } from "./config";
|
||||
import { ARWEAVE_GATEWAY, MANIFEST_TX_ID } from "./config";
|
||||
import { parseManifest, type Frontmatter, type ManifestPost } from "./types";
|
||||
|
||||
const formatDate = (date: string): string =>
|
||||
@@ -71,114 +71,16 @@ export const getReadableDate = (date?: string | null): string | null => {
|
||||
return formatDate(date);
|
||||
};
|
||||
|
||||
const MANIFEST_ID_CACHE_TTL_MS = 60_000;
|
||||
let manifestIdCache: { expiresAt: number; manifestId: string } | null = null;
|
||||
|
||||
const asObject = (value: unknown): Record<string, unknown> | null =>
|
||||
typeof value === "object" && value !== null ? (value as Record<string, unknown>) : null;
|
||||
|
||||
const asString = (value: unknown): string | null =>
|
||||
typeof value === "string" && value.length > 0 ? value : null;
|
||||
|
||||
const findManifestIdInTags = (tagsValue: unknown): string | null => {
|
||||
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" || name === "ManifestId") && value) return value;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const extractManifestId = (payload: unknown): string | null => {
|
||||
const root = asObject(payload);
|
||||
const results = asObject(root?.results);
|
||||
if (!results) return null;
|
||||
|
||||
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) ?? asString(messageObject.ManifestId);
|
||||
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 null;
|
||||
};
|
||||
|
||||
const getLatestManifestFromAo = async (): Promise<string | null> => {
|
||||
try {
|
||||
const now = Date.now();
|
||||
if (manifestIdCache && manifestIdCache.expiresAt > now) {
|
||||
return manifestIdCache.manifestId;
|
||||
}
|
||||
|
||||
const url = `${AO_URL}/${AO_PROCESS_ID}~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 null;
|
||||
|
||||
const payload: unknown = await response.json();
|
||||
const manifestId = extractManifestId(payload);
|
||||
if (!manifestId) return null;
|
||||
|
||||
manifestIdCache = {
|
||||
expiresAt: now + MANIFEST_ID_CACHE_TTL_MS,
|
||||
manifestId
|
||||
};
|
||||
return manifestId;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const loadManifest = async (): Promise<ManifestPost[]> => {
|
||||
const loadByTxId = async (manifestTxId: string): Promise<ManifestPost[]> => {
|
||||
const response = await fetch(arweaveUrl(manifestTxId));
|
||||
const response = await fetch(arweaveUrl(MANIFEST_TX_ID));
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load manifest (${response.status})`);
|
||||
}
|
||||
|
||||
const payload: unknown = await response.json();
|
||||
return parseManifest(payload);
|
||||
};
|
||||
|
||||
try {
|
||||
return await loadByTxId(MANIFEST_TX_ID);
|
||||
} catch (manifestError) {
|
||||
const latestManifestTxId = await getLatestManifestFromAo();
|
||||
if (!latestManifestTxId || latestManifestTxId === MANIFEST_TX_ID) {
|
||||
throw manifestError;
|
||||
}
|
||||
return loadByTxId(latestManifestTxId);
|
||||
}
|
||||
};
|
||||
|
||||
const isObject = (value: unknown): value is Record<string, unknown> =>
|
||||
typeof value === "object" && value !== null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user