aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-08-06 14:29:50 -0700
committerAdam Soltys <[email protected]>2020-08-06 14:29:50 -0700
commit40fc89ab9b00da0d5928e663c7fde0717b2a96af (patch)
treecfc14b0b38915145dde788ec1e7339670dd88228 /lib
parente849e4c626afb290ac43d543c63580daa09b777c (diff)
downloadcoinos-server-40fc89ab9b00da0d5928e663c7fde0717b2a96af.tar.gz
coinos-server-40fc89ab9b00da0d5928e663c7fde0717b2a96af.zip
socket management
Diffstat (limited to 'lib')
-rw-r--r--lib/sockets.js48
1 files changed, 38 insertions, 10 deletions
diff --git a/lib/sockets.js b/lib/sockets.js
index 242963cf..69fbab07 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -6,6 +6,7 @@ wss = new WebSocket.Server({ clientTracking: false, noServer: true });
/* eslint-disable-next-line */
const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {});
+const all = {};
const subscriptions = [];
emit = (username, type, data) => {
@@ -14,7 +15,11 @@ emit = (username, type, data) => {
if (type === "payment" && data.amount > 0) {
for (let i = subscriptions.length - 1; i >= 0; i--) {
let s = subscriptions[i];
- if (s.invoice.amount === data.amount || s.invoice.address === data.address || s.invoice.unconfidential === data.address) {
+ if (
+ s.invoice.amount === data.amount ||
+ s.invoice.address === data.address ||
+ s.invoice.unconfidential === data.address
+ ) {
s.ws.send(JSON.stringify({ type, data }));
subscriptions.splice(i, 1);
}
@@ -35,6 +40,10 @@ broadcast = (type, data) => {
ws.send(JSON.stringify({ type, data }));
}
}
+
+ Object.values(all).map((ws) => {
+ ws.send(JSON.stringify({ type, data }));
+ });
};
server.on("upgrade", function (req, socket, head) {
@@ -45,9 +54,21 @@ server.on("upgrade", function (req, socket, head) {
const names = {};
+setInterval(() => {
+ for (const username in sockets) {
+ for (const id in sockets[username]) {
+ let ws = sockets[username][id];
+ ws.beats++;
+ if (ws.beats > 10) ws.close();
+ }
+ }
+}, 2000);
+
wss.on("connection", function (ws, req) {
const id = uuidv4();
- ws.send(JSON.stringify({ type: "connected" }));
+ ws.beats = 0;
+ ws.send(JSON.stringify({ type: "connected", data: id }));
+ all[id] = ws;
ws.on("message", async (message) => {
let type, data;
@@ -58,20 +79,24 @@ wss.on("connection", function (ws, req) {
}
switch (type) {
+ case "heartbeat":
+ ws.beats = 0;
+ break;
+
case "login":
let token = data;
try {
- if (!token || token === "null") throw new Error("Unauthorized socket login");
+ if (!token || token === "null")
+ throw new Error("Unauthorized socket login");
const username = jwt.decode(token).username;
if (!username) throw new Error("Invalid JWT token");
if (!sockets[username]) sockets[username] = {};
const existing = Object.keys(sockets[username]);
- if (existing.length > 5) {
- l.warn("too many sockets, logging out", existing[3]);
- sockets[username][existing[3]].send(
- JSON.stringify({ type: "login", data: false })
- );
+ if (existing.length > 4) {
+ const p = existing.find((sid) => sid !== id);
+ l.warn("sending logout to", p);
+ sockets[username][p].send(JSON.stringify({ type: "logout" }));
}
sockets[username][id] = ws;
names[id] = username;
@@ -101,6 +126,7 @@ wss.on("connection", function (ws, req) {
JSON.stringify({ type: "version", data: config.clientVersion })
);
} catch (e) {
+ l.error("caught socket error", e.message);
ws.close();
}
break;
@@ -129,7 +155,6 @@ wss.on("connection", function (ws, req) {
}
break;
case "subscribe":
- l.info("subscribe", data.amount);
subscriptions.push({ invoice: data, ws });
break;
default:
@@ -140,8 +165,11 @@ wss.on("connection", function (ws, req) {
ws.on("close", function () {
try {
const username = names[id];
- if (sockets[username] && sockets[username][id])
+ if (sockets[username] && sockets[username][id]) {
delete sockets[username][id];
+ }
+
+ if (all[id]) delete all[id];
} catch (e) {
l.error("problem closing socket", e.message);
}
class="nx">slice(app.indexOf('function h(tag,'), app.indexOf('// ---------------------------------------------------------------- morphing')); const entry = ` import { messagesFeature } from './src/features/messages.js'; ${domHelper} const plans = new Map(), trace = []; let feature, recipient; const wallet = { nostr: { pk: 'f'.repeat(64) }, loadFeatureState: () => ({}), saveFeatureState() {}, registerCacheExtension() {} }; window.avatarLookup = async (filter) => { if (!filter.kinds.includes(0)) return []; return (await Promise.all((filter.authors || []).map(pk => plans.get(pk)?.answer || []))).flat(); }; function render() { if (!recipient) return; const portrait = feature.profileChip(recipient, 'lg'); document.querySelector('#recipient').replaceChildren(portrait); trace.push({ punk: !!portrait.querySelector('.punk'), loading: !!portrait.querySelector('.loading') }); } feature = messagesFeature({ h, wallet, ui: { screen: 'wallet' }, render, hook: () => false }); window.test = { start(pk, loadingName = false, expiredWait = false) { let finish; plans.set(pk, { answer: new Promise(resolve => finish = resolve), finish: result => finish(result) }); if (loadingName) feature.testSeed(pk, { name: 'Recipient', t: 0, loading: true }); if (expiredWait) feature.testExpiredWait(pk); recipient = pk; trace.length = 0; render(); }, complete(pk, profile) { plans.get(pk).finish(profile === null ? [] : [{ pubkey: pk, created_at: Math.floor(Date.now()/1000), content: JSON.stringify(profile) }]); }, cached(pk, profile) { feature.testSeed(pk, { ...profile, t: Date.now() }); recipient = pk; render(); }, get trace() { return trace; }, render, small(pk) { document.querySelector('#small').replaceChildren(feature.testAvatar(pk, 'chat-avatar mini', false)); }, }; `; const bundle = await Bun.build({ entrypoints: ['avatar-test-entry'], target: 'browser', plugins: [{ name: 'avatar-test', setup(build) { build.onResolve({ filter: /^avatar-test-entry$/ }, () => ({ path: 'entry', namespace: 'avatar-test' })); build.onLoad({ filter: /.*/, namespace: 'avatar-test' }, () => ({ contents: entry, loader: 'js', resolveDir: process.cwd() })); build.onLoad({ filter: /src\/features\/messages\.js$/ }, async ({ path }) => ({ loader: 'js', contents: (await Bun.file(path).text()).replace("id: 'messages',", `id: 'messages', testSeed: (pk, p) => profiles.set(pk, p), testExpiredWait: (pk) => faceAsked.set(pk, Date.now() - 10000), testAvatar: avatar,`), })); build.onLoad({ filter: /src\/nostr\.js$/ }, async ({ path }) => ({ loader: 'js', contents: (await Bun.file(path).text()).replace('export async function queryOn(relays, filter, maxWait = 1500) {', 'export async function queryOn(relays, filter, maxWait = 1500) { return window.avatarLookup(filter);'), })); } }] }); assert(bundle.success, bundle.logs.join('\n')); const html = `<!doctype html><meta charset="utf-8"><style>${await Bun.file('src/style.css').text()}</style><div id="recipient"></div><div id="small"></div><script type="module">${await bundle.outputs[0].text()}</script>`; const server = Bun.serve({ port: 0, fetch(req) { const path = new URL(req.url).pathname; if (/^\/punks\/\d+\.webp$/.test(path)) return new Response(Bun.file('static' + path)); return path === '/' ? new Response(html, { headers: { 'content-type': 'text/html' } }) : new Response(null, { status: 404 }); } }); const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: true, args: ['--no-sandbox'] }); try { const page = await browser.newPage(), errors = []; page.on('pageerror', e => errors.push(e.message)); await page.setRequestInterception(true); page.on('request', r => r.url().startsWith(server.url.origin) ? r.continue() : r.abort()); await page.goto(server.url.href); await page.waitForFunction(() => window.test); const state = () => page.$eval('.send-avatar', e => ({ loading: e.classList.contains('loading'), punk: !!e.querySelector('.punk'), picture: e.style.backgroundImage, hidden: getComputedStyle(e).visibility === 'hidden' })); const pause = ms => new Promise(resolve => setTimeout(resolve, ms)); for (const [i, loadingName, expiredWait] of [[1, false, false], [2, true, false], [3, true, true]]) { const pk = String(i).repeat(64); await page.evaluate(([pk, name, expired]) => test.start(pk, name, expired), [pk, loadingName, expiredWait]); assert.deepEqual(await state(), { loading: true, punk: false, picture: '', hidden: true }, 'cold lookup paints no visible placeholder'); await pause(1350); await page.evaluate(() => test.render()); assert.deepEqual(await state(), { loading: true, punk: false, picture: '', hidden: true }, 'slow metadata lookup never times out into punk art'); await page.evaluate(pk => test.complete(pk, { name: 'Recipient', picture: '/punks/18.webp' }), pk); await page.waitForSelector('.send-avatar.ava-img'); assert((await state()).picture.includes('/punks/18.webp')); assert(await page.evaluate(() => test.trace.every(frame => !frame.punk)), 'no fallback punk appeared before the real picture'); } console.log('✓ Cold, name-only and previously timed-out recipients show no placeholder before their picture arrives'); await page.evaluate(() => test.cached('4'.repeat(64), { name: 'Cached', picture: '/punks/54.webp' })); assert((await state()).picture.includes('/punks/54.webp')); assert.equal((await state()).loading, false); console.log('✓ Cached recipient pictures appear immediately'); for (const [i, loadingName, result] of [[5, false, { name: 'No picture' }], [6, false, null], [7, true, null]]) { const pk = String(i).repeat(64); await page.evaluate(([pk, name]) => test.start(pk, name), [pk, loadingName]); await page.evaluate(([pk, result]) => test.complete(pk, result), [pk, result]); await page.waitForSelector('.send-avatar .punk'); assert.equal((await state()).loading, false, 'finished or missing profile uses its actual fallback'); } console.log('✓ Profiles without pictures and failed lookups settle into their fallback avatar'); await page.evaluate(() => test.small('8'.repeat(64))); assert(await page.$('#small .punk'), 'compact avatars retain their immediate fallback'); assert.deepEqual(errors, []); console.log('✓ Compact avatars keep existing behavior; no browser errors'); } finally { await browser.close(); server.stop(true); }