mirror of
https://github.com/hzrd149/nsite-gateway.git
synced 2025-06-23 20:05:03 +00:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { extname, isAbsolute, join } from "path";
|
|
import { NSITE_KIND } from "./const.js";
|
|
import ndk from "./ndk.js";
|
|
import { NDKRelaySet } from "@nostr-dev-kit/ndk";
|
|
|
|
export function getSearchPaths(path: string) {
|
|
const paths = [path];
|
|
|
|
// if the path does not have an extension, also look for index.html
|
|
if (extname(path) === "") paths.push(join(path, "index.html"));
|
|
|
|
// also look for relative paths
|
|
for (const p of Array.from(paths)) {
|
|
if (isAbsolute(p)) paths.push(p.replace(/^\//, ""));
|
|
}
|
|
|
|
return paths.filter((p) => !!p);
|
|
}
|
|
|
|
export function parseNsiteEvent(event: { pubkey: string; tags: string[][] }) {
|
|
const path = event.tags.find((t) => t[0] === "d" && t[1])?.[1];
|
|
const sha256 = event.tags.find((t) => t[0] === "x" && t[1])?.[1];
|
|
|
|
if (path && sha256)
|
|
return {
|
|
pubkey: event.pubkey,
|
|
path: join("/", path),
|
|
sha256,
|
|
};
|
|
}
|
|
|
|
export async function getNsiteBlobs(pubkey: string, path: string, relays?: string[]) {
|
|
const paths = getSearchPaths(path);
|
|
const events = await ndk.fetchEvents(
|
|
{ kinds: [NSITE_KIND], "#d": paths, authors: [pubkey] },
|
|
{},
|
|
relays && NDKRelaySet.fromRelayUrls(relays, ndk, true),
|
|
);
|
|
|
|
return Array.from(events)
|
|
.map(parseNsiteEvent)
|
|
.filter((e) => !!e)
|
|
.sort((a, b) => paths.indexOf(a.path) - paths.indexOf(b.path));
|
|
}
|