2024-09-01 13:26:37 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
import "./polyfill.js";
|
|
|
|
import Koa from "koa";
|
|
|
|
import serve from "koa-static";
|
2024-09-25 13:37:32 -05:00
|
|
|
import path from "node:path";
|
2024-09-01 13:26:37 -05:00
|
|
|
import cors from "@koa/cors";
|
|
|
|
import fs from "node:fs";
|
|
|
|
import { fileURLToPath } from "node:url";
|
2024-09-25 13:37:32 -05:00
|
|
|
import mime from "mime";
|
|
|
|
import morgan from "koa-morgan";
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
import { resolveNpubFromHostname } from "./helpers/dns.js";
|
2024-09-26 12:48:13 -05:00
|
|
|
import { getNsiteBlobs, parseNsiteEvent } from "./events.js";
|
2024-09-25 13:37:32 -05:00
|
|
|
import { downloadFile, getUserBlossomServers } from "./blossom.js";
|
2024-09-26 12:48:13 -05:00
|
|
|
import { BLOSSOM_SERVERS, NGINX_CACHE_DIR, SUBSCRIPTION_RELAYS } from "./env.js";
|
|
|
|
import { userDomains, userRelays, userServers } from "./cache.js";
|
|
|
|
import { NSITE_KIND } from "./const.js";
|
|
|
|
import { invalidatePubkeyPath } from "./nginx.js";
|
|
|
|
import pool, { getUserOutboxes, subscribeForEvents } from "./nostr.js";
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
|
|
const app = new Koa();
|
|
|
|
|
2024-09-25 13:37:32 -05:00
|
|
|
morgan.token("host", (req) => req.headers.host ?? "");
|
|
|
|
|
|
|
|
app.use(morgan(":method :host:url :status :response-time ms - :res[content-length]"));
|
|
|
|
|
2024-09-01 13:26:37 -05:00
|
|
|
// set CORS headers
|
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
origin: "*",
|
|
|
|
allowMethods: "*",
|
|
|
|
allowHeaders: "Authorization,*",
|
|
|
|
exposeHeaders: "*",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// handle errors
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await next();
|
|
|
|
} catch (err) {
|
2024-09-25 13:37:32 -05:00
|
|
|
console.log(err);
|
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = { message: "Something went wrong" };
|
2024-09-01 13:26:37 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-26 12:48:13 -05:00
|
|
|
// handle nsite requests
|
2024-09-01 13:26:37 -05:00
|
|
|
app.use(async (ctx, next) => {
|
2024-09-26 12:48:13 -05:00
|
|
|
let pubkey = await userDomains.get<string | undefined>(ctx.hostname);
|
|
|
|
|
|
|
|
// resolve pubkey if not in cache
|
|
|
|
if (!pubkey) {
|
|
|
|
console.log(`${ctx.hostname}: Resolving`);
|
|
|
|
pubkey = await resolveNpubFromHostname(ctx.hostname);
|
|
|
|
|
|
|
|
if (pubkey) {
|
|
|
|
await userDomains.set(ctx.hostname, pubkey);
|
|
|
|
console.log(`${ctx.hostname}: Found ${pubkey}`);
|
|
|
|
} else {
|
|
|
|
await userDomains.set(ctx.hostname, "");
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
if (pubkey) {
|
2024-09-26 12:48:13 -05:00
|
|
|
ctx.state.pubkey = pubkey;
|
|
|
|
|
2024-09-25 15:28:28 -05:00
|
|
|
let relays = await userRelays.get<string[] | undefined>(pubkey);
|
2024-09-26 12:48:13 -05:00
|
|
|
|
|
|
|
// fetch relays if not in cache
|
2024-09-25 15:28:28 -05:00
|
|
|
if (!relays) {
|
2024-09-26 09:06:08 -05:00
|
|
|
console.log(`${pubkey}: Fetching relays`);
|
|
|
|
|
2024-09-25 15:28:28 -05:00
|
|
|
relays = await getUserOutboxes(pubkey);
|
2024-09-26 09:06:08 -05:00
|
|
|
if (relays) {
|
|
|
|
await userRelays.set(pubkey, relays);
|
|
|
|
console.log(`${pubkey}: Found ${relays.length} relays`);
|
|
|
|
} else {
|
|
|
|
relays = [];
|
|
|
|
await userServers.set(pubkey, [], 30_000);
|
|
|
|
console.log(`${pubkey}: Failed to find relays`);
|
|
|
|
}
|
2024-09-25 15:28:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-25 13:37:32 -05:00
|
|
|
console.log(`${pubkey}: Searching for ${ctx.path}`);
|
2024-09-25 15:28:28 -05:00
|
|
|
const blobs = await getNsiteBlobs(pubkey, ctx.path, relays);
|
2024-09-07 17:15:12 -05:00
|
|
|
|
2024-09-25 13:37:32 -05:00
|
|
|
if (blobs.length === 0) {
|
2024-09-26 12:48:13 -05:00
|
|
|
console.log(`${pubkey}: Found 0 events`);
|
2024-09-25 13:37:32 -05:00
|
|
|
ctx.status = 404;
|
|
|
|
ctx.body = "Not Found";
|
|
|
|
return;
|
2024-09-01 13:26:37 -05:00
|
|
|
}
|
|
|
|
|
2024-09-25 15:28:28 -05:00
|
|
|
let servers = await userServers.get<string[] | undefined>(pubkey);
|
2024-09-26 12:48:13 -05:00
|
|
|
|
|
|
|
// fetch blossom servers if not in cache
|
2024-09-25 13:37:32 -05:00
|
|
|
if (!servers) {
|
|
|
|
console.log(`${pubkey}: Searching for blossom servers`);
|
2024-09-26 08:51:32 -05:00
|
|
|
servers = await getUserBlossomServers(pubkey, relays);
|
|
|
|
|
|
|
|
if (servers) {
|
|
|
|
await userServers.set(pubkey, servers);
|
|
|
|
console.log(`${pubkey}: Found ${servers.length} servers`);
|
|
|
|
} else {
|
|
|
|
servers = [];
|
|
|
|
await userServers.set(pubkey, [], 30_000);
|
|
|
|
console.log(`${pubkey}: Failed to find servers`);
|
|
|
|
}
|
2024-09-25 13:37:32 -05:00
|
|
|
}
|
2024-09-26 12:48:13 -05:00
|
|
|
|
|
|
|
// always fetch from additional servers
|
2024-09-25 13:37:32 -05:00
|
|
|
servers.push(...BLOSSOM_SERVERS);
|
|
|
|
|
|
|
|
for (const blob of blobs) {
|
|
|
|
const res = await downloadFile(blob.sha256, servers);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
const type = mime.getType(blob.path);
|
|
|
|
if (type) ctx.set("Content-Type", type);
|
|
|
|
else if (res.headers["content-type"]) ctx.set("content-type", res.headers["content-type"]);
|
|
|
|
|
|
|
|
// pass headers along
|
|
|
|
if (res.headers["content-length"]) ctx.set("content-length", res.headers["content-length"]);
|
|
|
|
|
|
|
|
ctx.body = res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 500;
|
2024-09-26 08:51:32 -05:00
|
|
|
ctx.body = "Failed to find blob";
|
2024-09-01 13:26:37 -05:00
|
|
|
} else await next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// serve static files from public
|
|
|
|
try {
|
|
|
|
const www = path.resolve(process.cwd(), "public");
|
|
|
|
fs.statSync(www);
|
|
|
|
app.use(serve(www));
|
|
|
|
} catch (error) {
|
|
|
|
const www = path.resolve(__dirname, "../public");
|
|
|
|
app.use(serve(www));
|
|
|
|
}
|
|
|
|
|
2024-09-26 12:48:13 -05:00
|
|
|
app.listen(
|
|
|
|
{
|
|
|
|
port: process.env.NSITE_PORT || 3000,
|
|
|
|
host: process.env.NSITE_HOST || "0.0.0.0",
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
console.log("Started on port", process.env.PORT || 3000);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// invalidate nginx cache on new events
|
|
|
|
if (NGINX_CACHE_DIR && SUBSCRIPTION_RELAYS.length > 0) {
|
|
|
|
console.log(`Listening for new nsite events`);
|
|
|
|
|
|
|
|
subscribeForEvents(SUBSCRIPTION_RELAYS, async (event) => {
|
|
|
|
try {
|
|
|
|
const nsite = parseNsiteEvent(event);
|
|
|
|
if (nsite) {
|
|
|
|
console.log(`${nsite.pubkey}: Invalidating ${nsite.path}`);
|
|
|
|
await invalidatePubkeyPath(nsite.pubkey, nsite.path);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Failed to invalidate ${event.id}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
|
|
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
2024-09-25 13:37:32 -05:00
|
|
|
});
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
async function shutdown() {
|
2024-09-25 13:37:32 -05:00
|
|
|
console.log("Shutting down...");
|
2024-09-26 12:48:13 -05:00
|
|
|
pool.destroy();
|
2024-09-01 13:26:37 -05:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.addListener("SIGTERM", shutdown);
|
|
|
|
process.addListener("SIGINT", shutdown);
|