-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
189 lines (170 loc) · 8.52 KB
/
Copy pathserver.js
File metadata and controls
189 lines (170 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Thin read API over the indexed social graph.
import express from 'express';
import path from 'node:path';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { getProfile, getFollows, getRelays, recentProfiles, connect, stats, searchProfiles, enrichCounts, followerCount, relaysDirectory } from './db.js';
import { buildDidDocument } from './diddoc.js';
import 'dotenv/config';
const PUBLIC = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'public');
const SITE = process.env.SITE_URL || 'https://nostr.social';
// ---- server-rendered OGP (social crawlers don't run JS) --------------------
const escAttr = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
let SHELL;
const shell = () => (SHELL ??= readFileSync(path.join(PUBLIC, 'index.html'), 'utf8'));
// Inject <title> + Open Graph / Twitter meta into the SPA shell at <!--OGP-->.
function renderShell(meta = {}) {
const m = {
title: 'nostr.social · a directory of did:nostr identities',
description: 'Profiles, the follow graph, and a verifiable DID document for every did:nostr key — indexed live from Nostr.',
url: `${SITE}/`, type: 'website', ...meta,
};
const image = m.image || `${SITE}/og.png`; // fall back to the site banner
const tags = [
`<title>${escAttr(m.title)}</title>`,
`<meta name="description" content="${escAttr(m.description)}">`,
`<meta property="og:site_name" content="nostr.social">`,
`<meta property="og:type" content="${escAttr(m.type)}">`,
`<meta property="og:title" content="${escAttr(m.title)}">`,
`<meta property="og:description" content="${escAttr(m.description)}">`,
`<meta property="og:url" content="${escAttr(m.url)}">`,
`<meta property="og:image" content="${escAttr(image)}">`,
`<meta name="twitter:card" content="summary_large_image">`,
`<meta name="twitter:title" content="${escAttr(m.title)}">`,
`<meta name="twitter:description" content="${escAttr(m.description)}">`,
`<meta name="twitter:image" content="${escAttr(image)}">`,
].join('\n ');
return shell().replace('<!--OGP-->', tags);
}
export async function startServer(port = process.env.PORT || 3000) {
await connect();
const app = express();
// Public read-only resolver: allow cross-origin fetches so browser-based
// did:nostr resolvers can read the documents.
app.use((_req, res, next) => { res.setHeader('Access-Control-Allow-Origin', '*'); next(); });
app.use(express.static(PUBLIC, { index: false }));
// home shell (default OGP)
app.get('/', (_req, res) => res.type('html').send(renderShell()));
// "link your pod" onboarding shell
app.get('/link', (_req, res) => res.type('html').send(renderShell({
title: 'Link your pod · nostr.social',
description: 'Link your nostr key to a Solid pod so did:nostr resolves to your WebID — one-click sign-in via jss.live SSO.',
url: `${SITE}/link`,
})));
// relay directory shell
app.get('/relays', (_req, res) => res.type('html').send(renderShell({
title: 'Relay directory · nostr.social',
description: 'A health-checked directory of Nostr relays — uptime, latency, write-acceptance, and paid/auth requirements.',
url: `${SITE}/relays`,
})));
// per-profile shell at /<pubkey> with that identity's OGP (crawlable canonical
// URL). RegExp route constrained to 64-hex, so it never collides with /api,
// /healthz, /.well-known, or static assets.
app.get(/^\/([0-9a-f]{64})$/, async (req, res, next) => {
try {
const id = req.params[0].toLowerCase();
const p = await getProfile(id);
let c = {}; try { c = JSON.parse(p?.content || '{}'); } catch { /* malformed */ }
const name = c.name || c.display_name || `did:nostr:${id.slice(0, 8)}…`;
const about = String(c.about || `A did:nostr identity · ${id.slice(0, 16)}…`).replace(/\s+/g, ' ').slice(0, 180);
res.type('html').send(renderShell({
title: `${name} · nostr.social`, description: about, url: `${SITE}/${id}`,
type: 'profile', image: c.picture || '',
}));
} catch (e) { next(e); }
});
// Liveness/readiness: confirms Mongo is reachable (for haproxy httpchk + monitoring).
app.get('/healthz', async (_req, res) => {
try { await (await connect()).command({ ping: 1 }); res.json({ ok: true }); }
catch (e) { res.status(503).json({ ok: false, error: e.message }); }
});
// attach { followers, following } to each profile under `_counts`
const withCounts = async (profiles) => {
const counts = await enrichCounts(profiles.map((p) => p.pubkey));
return profiles.map((p) => ({ ...p, _counts: counts[p.pubkey] }));
};
// network stats, cached 60s (estimatedDocumentCount is cheap but stable)
let statsCache = { at: 0, val: null };
app.get('/api/stats', async (_req, res, next) => {
try {
if (!statsCache.val || Date.now() - statsCache.at > 60_000) statsCache = { at: Date.now(), val: await stats() };
res.json(statsCache.val);
} catch (e) { next(e); }
});
// search by name / nip05 / npub / hex pubkey; rank matches by followers so
// prominent accounts surface above same-named namesakes.
app.get('/api/search', async (req, res, next) => {
try {
const limit = Math.min(Number(req.query.limit) || 24, 50);
const pool = await withCounts(await searchProfiles(req.query.q, Math.max(limit, 40)));
pool.sort((a, b) => (b._counts?.followers || 0) - (a._counts?.followers || 0));
res.json(pool.slice(0, limit));
} catch (e) { next(e); }
});
app.get('/api/profiles', async (req, res, next) => {
try { res.json(await withCounts(await recentProfiles(Math.min(Number(req.query.limit) || 30, 100)))); } catch (e) { next(e); }
});
app.get('/api/profile/:pubkey', async (req, res, next) => {
try { res.json(await getProfile(req.params.pubkey)); } catch (e) { next(e); }
});
app.get('/api/follows/:pubkey', async (req, res, next) => {
try { res.json(await getFollows(req.params.pubkey)); } catch (e) { next(e); }
});
app.get('/api/relays/:pubkey', async (req, res, next) => {
try { res.json(await getRelays(req.params.pubkey)); } catch (e) { next(e); }
});
// relay-health directory (firehose data): { counts, lastChecked, relays }
app.get('/api/relays-directory', async (req, res, next) => {
try {
res.json(await relaysDirectory({
filter: req.query.filter,
sort: req.query.sort,
limit: req.query.limit,
}));
} catch (e) { next(e); }
});
// did:nostr resolution — build the conformant DID document from the index
const resolve = async (pubkey) => {
const [profile, follows, relays] = await Promise.all([
getProfile(pubkey), getFollows(pubkey), getRelays(pubkey),
]);
return buildDidDocument(pubkey, { profile, follows, relays });
};
app.get('/.well-known/did/nostr/:pubkey.json', async (req, res, next) => {
try {
const doc = await resolve(req.params.pubkey);
if (!doc) return res.status(400).json({ error: 'invalid did:nostr pubkey (expect 64-hex)' });
res.setHeader('Content-Type', 'application/did+json');
res.json(doc);
} catch (e) { next(e); }
});
// UI-facing resolution: the conformant doc + graph counts (followers = in-degree).
// The .well-known endpoint above stays the pure conformant document.
app.get('/api/did/:pubkey', async (req, res, next) => {
try {
const doc = await resolve(req.params.pubkey);
if (!doc) return res.status(400).json({ error: 'invalid did:nostr pubkey (expect 64-hex)' });
const followers = await followerCount(req.params.pubkey.toLowerCase());
res.json({ ...doc, followersCount: followers, followingCount: doc.follows?.length || 0 });
} catch (e) { next(e); }
});
app.use((err, _req, res, _next) => res.status(500).json({ error: err.message }));
const server = await listenWithSkip(app, Number(port) || 3000);
console.log(`[beacon] serving on http://localhost:${server.address().port}`);
return server;
}
// Try `port`, skipping to the next port on EADDRINUSE (so a busy 3000 just moves on).
function listenWithSkip(app, port, attempts = 25) {
return new Promise((resolve, reject) => {
const tryPort = (p, left) => {
const server = app.listen(p);
server.once('listening', () => resolve(server));
server.once('error', (e) => {
if (e.code === 'EADDRINUSE' && left > 0) { console.log(`[beacon] port ${p} busy, skipping…`); tryPort(p + 1, left - 1); }
else reject(e);
});
};
tryPort(port, attempts);
});
}
if (import.meta.url === `file://${process.argv[1]}`) startServer();