From a02f16a86fd1446f83f1f04e7164a6a17b1c38a5 Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Sat, 7 Sep 2024 17:15:12 -0500 Subject: [PATCH] dont wait for download --- .env | 2 +- .env.example | 2 +- public/index.html | 25 +++++++++++++++++++++++++ public/main.js | 37 +++++++++++++++++++++---------------- src/cache.ts | 4 ++-- src/downloader.ts | 2 -- src/index.ts | 5 ++++- 7 files changed, 54 insertions(+), 23 deletions(-) diff --git a/.env b/.env index b36ba0c..98e4615 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -NOSTR_RELAYS=wss://nostrue.com,wss://nos.lol,wss://relay.damus.io +NOSTR_RELAYS=wss://nos.lol,wss://relay.damus.io BLOSSOM_SERVERS=https://cdn.hzrd149.com MAX_FILE_SIZE='2 MB' diff --git a/.env.example b/.env.example index 9d75a90..12008f3 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,5 @@ # A list of nostr relays to search -NOSTR_RELAYS=wss://nostrue.com,wss://nos.lol,wss://relay.damus.io +NOSTR_RELAYS=wss://nos.lol,wss://relay.damus.io # A list of fallback blossom servers BLOSSOM_SERVERS=https://cdn.satellite.earth diff --git a/public/index.html b/public/index.html index 49cbc38..24ab39d 100644 --- a/public/index.html +++ b/public/index.html @@ -14,8 +14,33 @@ + +
+ +
+
+ +
+ +
+
+
diff --git a/public/main.js b/public/main.js index 571c103..a0dda2b 100644 --- a/public/main.js +++ b/public/main.js @@ -1,6 +1,13 @@ import { multiServerUpload, BlossomClient } from "blossom-client-sdk"; import { SimplePool } from "nostr-tools"; +const logContainer = document.getElementById("log"); +function log(...args) { + const el = document.createElement("div"); + el.innerText = args.join(" "); + logContainer.appendChild(el); +} + const uploadButton = document.getElementById("upload-button"); /** @type {HTMLInputElement} */ @@ -47,8 +54,8 @@ async function readFileSystemEntry(entry) { files.push({ file, path, sha256 }); } catch (e) { - console.log("Failed to add" + entry.fullPath); - console.log(e); + log("Failed to add" + entry.fullPath); + log(e.message); } } else if (entry instanceof FileSystemDirectoryEntry && entry.isDirectory) { const entries = await readFileSystemDirectory(entry); @@ -79,11 +86,14 @@ const pool = new SimplePool(); * uploads a file system entry to blossom servers * @param {{file:File, path:string}} files * @param {import("blossom-client-sdk").Signer} signer + * @param {*} auth + * @param {string[]} servers + * @param {string[]} relays */ -async function uploadFiles(files, signer, auth) { +async function uploadFiles(files, signer, auth, servers, relays) { for (const { file, path, sha256 } of files) { try { - const upload = multiServerUpload(["https://cdn.hzrd149.com", "https://cdn.satellite.earth"], file, signer, auth); + const upload = multiServerUpload(servers, file, signer, auth); let published = false; for await (let { blob } of upload) { @@ -97,13 +107,13 @@ async function uploadFiles(files, signer, auth) { ["x", sha256], ], }); - await pool.publish(["wss://nostrue.com"], signed); + await pool.publish(relays, signed); - console.log("Published", path, sha256, signed); + log("Published", path, sha256, signed.id); } } } catch (error) { - console.warn(`Failed to upload ${path}`, error); + log(`Failed to upload ${path}`, error); } } } @@ -112,6 +122,8 @@ uploadButton.addEventListener("click", async () => { if (!window.nostr) return alert("Missing NIP-07 signer"); const signer = (draft) => window.nostr.signEvent(draft); + const relays = document.getElementById("relays").value.split(/\n|,/); + const servers = document.getElementById("servers").value.split(/\n|,/); try { if (filesInput.files) { @@ -120,16 +132,9 @@ uploadButton.addEventListener("click", async () => { // strip leading dir for (const file of files) file.path = file.path.replace(/^[^\/]+\//, "/"); - console.log(`Found files`, files); + log(`Found ${files.length} files`); - // const auth = await BlossomClient.createUploadAuth( - // files.map((f) => f.sha256), - // signer, - // ); - - // console.log("Created upload auth", auth); - - await uploadFiles(files, signer); + await uploadFiles(files, signer, undefined, servers, relays); } } catch (error) { alert(`Failed to upload files: ${error.message}`); diff --git a/src/cache.ts b/src/cache.ts index bef456a..35b6852 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -12,5 +12,5 @@ keyvSqlite.on("error", (err) => { process.exit(1); }); -export const files = new Keyv({ store: keyvSqlite, ttl: 1000 * 60 * 60 * 24, namespace: "files" }); -export const downloaded = new Keyv({ store: keyvSqlite, ttl: 1000 * 30, namespace: "downloaded" }); +export const files = new Keyv({ store: keyvSqlite, namespace: "files" }); +export const downloaded = new Keyv({ store: keyvSqlite, ttl: 1000 * 60 * 5, namespace: "downloaded" }); diff --git a/src/downloader.ts b/src/downloader.ts index e86fe1c..cc857ec 100644 --- a/src/downloader.ts +++ b/src/downloader.ts @@ -36,8 +36,6 @@ async function downloadFile(sha256: string, servers = BLOSSOM_SERVERS) { } export async function downloadSite(pubkey: string) { - if (await downloaded.get(pubkey)) return; - const user = await ndk.getUser({ pubkey }); const blossomServers = await ndk.fetchEvent([{ kinds: [USER_BLOSSOM_SERVER_LIST_KIND], authors: [pubkey] }]); diff --git a/src/index.ts b/src/index.ts index f387764..61f1776 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,10 @@ app.use(async (ctx, next) => { if (pubkey) { if (!(await downloaded.get(pubkey))) { - await downloadSite(pubkey); + // don't wait for download + downloadSite(pubkey); + + await downloaded.set(pubkey, true); } await send(ctx, join(pubkey, ctx.path), { root: "data/sites", index: "index.html" });