Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
// The RustCord user registry: who uses RustCord, since when, and when they
// were last seen. This file is the whole protocol; `worker.mjs` runs it on
// Cloudflare (rows in a private D1 database), `dev.mjs` on your own machine.
//
// POST /v1/hello {"key": "<64 hex>"} register (first time) + mark as seen
// POST /v1/bye {"key": "<64 hex>"} hide that row (the badge setting, off)
// POST /v1/lookup {"keys": ["<64 hex>", …]} are these RustCord users? (50 at most)
//
// Requests are sent with content-type: application/json (anything else gets
// 415). Every answer is {"now": <unix s>, "users": {"<key>": [joined, seen]}} with
// only the keys that were asked about (hello: your own). There is no way to
// list the rows: you can only ask about a user whose ID you already have.
//
// A key is the SHA-256 of "rustcord-user:<Discord user ID>", made by the
// client: the service never sees an ID. The join date is this service's own
// clock at the first hello, so a client can't claim an older one.
const KEY = /^[0-9a-f]{64}$/;
export const MAX_LOOKUP = 50;
function json(body, status = 200) {
return new Response(JSON.stringify(body), {
status,
headers: { "content-type": "application/json", "cache-control": "no-store" },
});
}
/**
* @param {Request} request
* @param {{ hello(key: string, now: number): Promise<void>, hide(key: string): Promise<void>, lookup(keys: string[]): Promise<Record<string, [number, number]>> }} store
* @param {number} now Unix seconds.
*/
export async function handle(request, store, now = Math.floor(Date.now() / 1000)) {
const { pathname } = new URL(request.url);
if (request.method !== "POST" || !["/v1/hello", "/v1/bye", "/v1/lookup"].includes(pathname)) {
return json({ error: "not found" }, 404);
}
// A web page can make its visitors' browsers send text/plain POSTs to any
// site without asking first; application/json needs a preflight, which this
// service never answers, so browsers can't be used to write rows.
const type = (request.headers.get("content-type") ?? "").split(";")[0].trim().toLowerCase();
if (type !== "application/json") return json({ error: "send application/json" }, 415);
// 50 keys and their JSON wrapping: anything much longer isn't one of ours.
const text = await request.text();
if (text.length > 4096) return json({ error: "too long" }, 413);
let body;
try {
body = JSON.parse(text);
} catch {
return json({ error: "not JSON" }, 400);
}
if (pathname === "/v1/lookup") {
const keys = body?.keys;
if (!Array.isArray(keys) || keys.length > MAX_LOOKUP || !keys.every((k) => typeof k === "string" && KEY.test(k))) {
return json({ error: "bad keys" }, 400);
}
return json({ now, users: keys.length ? await store.lookup([...new Set(keys)]) : {} });
}
const key = body?.key;
if (typeof key !== "string" || !KEY.test(key)) return json({ error: "bad key" }, 400);
if (pathname === "/v1/hello") await store.hello(key, now);
else await store.hide(key);
return json({ now, users: await store.lookup([key]) });
}
/** Rows in memory (the local server and the tests). */
export function memoryStore(rows = new Map()) {
return {
rows,
async hello(key, now) {
const row = rows.get(key);
if (row) Object.assign(row, { seen: now, hidden: false });
else rows.set(key, { joined: now, seen: now, hidden: false });
},
async hide(key) {
const row = rows.get(key);
if (row) row.hidden = true;
},
async lookup(keys) {
const users = {};
for (const key of keys) {
const row = rows.get(key);
if (row && !row.hidden) users[key] = [row.joined, row.seen];
}
return users;
},
};
}