From e3ade42f58b662d2e5c922682be362c5b0c2dd95 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 14 Aug 2023 18:46:35 -0500 Subject: [PATCH] Added script to populate relays from external source --- deno.json | 7 ++++--- scripts/relays.ts | 23 +++++++++++++++++++++++ src/utils.ts | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 scripts/relays.ts diff --git a/deno.json b/deno.json index 5233fa9..66b1213 100644 --- a/deno.json +++ b/deno.json @@ -6,21 +6,22 @@ "dev": "deno run --allow-read --allow-write=data --allow-env --allow-net --unstable --watch src/server.ts", "debug": "deno run --allow-read --allow-write=data --allow-env --allow-net --unstable --inspect src/server.ts", "test": "DB_PATH=\":memory:\" deno test --allow-read --allow-write=data --allow-env --unstable src", - "check": "deno check --unstable src/server.ts" + "check": "deno check --unstable src/server.ts", + "relays:sync": "deno run -A --unstable scripts/relays.ts sync", }, "imports": { "@/": "./src/", "~/": "./" }, "lint": { - "include": ["src/"], + "include": ["src/", "scripts/"], "rules": { "tags": ["recommended"], "exclude": ["no-explicit-any"] } }, "fmt": { - "include": ["src/"], + "include": ["src/", "scripts/"], "useTabs": false, "lineWidth": 120, "indentWidth": 2, diff --git a/scripts/relays.ts b/scripts/relays.ts new file mode 100644 index 0000000..ab4e7ab --- /dev/null +++ b/scripts/relays.ts @@ -0,0 +1,23 @@ +import { addRelays } from '@/db/relays.ts'; +import { filteredArray } from '@/schema.ts'; +import { relaySchema } from '~/src/utils.ts'; + +switch (Deno.args[0]) { + case 'sync': + await sync(Deno.args.slice(1)); + break; + default: + console.log('Usage: deno run -A scripts/relays.ts sync '); +} + +async function sync([url]: string[]) { + if (!url) { + console.error('Error: please provide a URL'); + Deno.exit(1); + } + const response = await fetch(url); + const data = await response.json(); + const values = filteredArray(relaySchema).parse(data) as `wss://${string}`[]; + await addRelays(values); + console.log(`Done: added ${values.length} relays.`); +} diff --git a/src/utils.ts b/src/utils.ts index a2c2504..8716d63 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -164,6 +164,7 @@ export { paginationSchema, parseBody, parseNip05, + relaySchema, sha256, };