1// Fills a relay with test files and reports, signed by the dev seed key. Usage: npm run dev:junk https://<name>.bind.ws2import { finalizeEvent, getPublicKey } from "nostr-tools/pure";3import { createHash } from "node:crypto";4import { readFileSync } from "node:fs";5const sk = new Uint8Array(32).fill(7);6const base = process.argv[2].replace(/\/$/, "");7const now = () => Math.floor(Date.now() / 1000);8const sign = (kind, tags, content = "") => finalizeEvent({ kind, created_at: now(), tags, content }, sk);9const auth = (e) => "Nostr " + Buffer.from(JSON.stringify(e)).toString("base64");1011const files = [12 ["favicon.svg", "image/svg+xml", readFileSync(new URL("../../src/ui.ts", import.meta.url), "utf8").match(/<svg[^`]*<\/svg>/)[0]],13 ["notes.txt", "text/plain", "junk file for testing the files list\n".repeat(200)],14 ["blob.bin", "application/octet-stream", Buffer.from(Array.from({ length: 3 * 1024 * 1024 }, (_, i) => (i * 7919) & 255))],15 ["hello.json", "application/json", JSON.stringify({ hello: "world", when: now() })],16];17for (const [name, type, body] of files) {18 const data = typeof body === "string" ? Buffer.from(body) : body;19 const x = createHash("sha256").update(data).digest("hex");20 const e = sign(24242, [["t", "upload"], ["x", x], ["expiration", String(now() + 300)]], "upload " + name);21 const r = await fetch(base + "/upload", { method: "PUT", headers: { authorization: auth(e), "content-type": type }, body: data });22 console.log("upload", name, r.status, (await r.text()).slice(0, 120));23}2425// Reports against a couple of the newest notes on the relay, plus one against a random pubkey.26const { Relay } = await import("nostr-tools/relay");27const relay = await Relay.connect(base.replace(/^http/, "ws"));28const notes = await new Promise((res) => { const got = []; relay.subscribe([{ kinds: [1], limit: 3 }], { onevent: (e) => got.push(e), oneose: () => res(got) }); });29const reports = notes.map((n, i) => sign(1984, [["e", n.id, ["spam", "nudity", "impersonation"][i % 3]], ["p", n.pubkey]], ["obvious spam", "not for this relay", "pretending to be someone"][i % 3]));30reports.push(sign(1984, [["p", getPublicKey(new Uint8Array(32).fill(9)), "other"]], "just a test report on a pubkey"));31for (const r of reports) { try { await relay.publish(r); console.log("report", r.id.slice(0, 8), "ok"); } catch (err) { console.log("report", r.id.slice(0, 8), String(err)); } }32relay.close();