From f684658183370faa1f72e49b2dbc39d8d607551f Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 25 May 2023 11:05:06 +0100 Subject: [PATCH] fix: Service worker precache delete fix: pubkey in profile metadata breaks cache refresh fix: non-specific queries not sent to any relays sometimes fix: sub-query trace not sending "CLOSE" --- packages/app/src/Cache/index.ts | 2 +- packages/app/src/System/Query.ts | 21 ++++++++++++----- packages/app/src/System/index.ts | 38 ++++++------------------------ packages/app/src/service-worker.ts | 2 -- 4 files changed, 23 insertions(+), 40 deletions(-) diff --git a/packages/app/src/Cache/index.ts b/packages/app/src/Cache/index.ts index 8942fede..24b8e991 100644 --- a/packages/app/src/Cache/index.ts +++ b/packages/app/src/Cache/index.ts @@ -41,10 +41,10 @@ export function mapEventToProfile(ev: RawEvent) { try { const data: UserMetadata = JSON.parse(ev.content); return { + ...data, pubkey: ev.pubkey, npub: hexToBech32("npub", ev.pubkey), created: ev.created_at, - ...data, loaded: unixNowMs(), } as MetadataCache; } catch (e) { diff --git a/packages/app/src/System/Query.ts b/packages/app/src/System/Query.ts index cb06c125..9622e959 100644 --- a/packages/app/src/System/Query.ts +++ b/packages/app/src/System/Query.ts @@ -13,6 +13,7 @@ class QueryTrace { readonly relay: string; readonly connId: string; readonly start: number; + readonly leaveOpen: boolean; sent?: number; eose?: number; close?: number; @@ -21,11 +22,19 @@ class QueryTrace { readonly #fnProgress: () => void; readonly #log = debug("QueryTrace"); - constructor(sub: string, relay: string, connId: string, fnClose: (id: string) => void, fnProgress: () => void) { + constructor( + sub: string, + relay: string, + connId: string, + leaveOpen: boolean, + fnClose: (id: string) => void, + fnProgress: () => void + ) { this.id = uuid(); this.subId = sub; this.relay = relay; this.connId = connId; + this.leaveOpen = leaveOpen; this.start = unixNowMs(); this.#fnClose = fnClose; this.#fnProgress = fnProgress; @@ -39,6 +48,9 @@ class QueryTrace { gotEose() { this.eose = unixNowMs(); this.#fnProgress(); + if (!this.leaveOpen) { + this.sendClose(); + } //this.#log("[EOSE] %s %s", this.subId, this.relay); } @@ -46,6 +58,7 @@ class QueryTrace { this.eose = unixNowMs(); this.#wasForceClosed = true; this.#fnProgress(); + this.sendClose(); //this.#log("[F-EOSE] %s %s", this.subId, this.relay); } @@ -199,11 +212,6 @@ export class Query implements QueryBase { eose(sub: string, conn: Readonly) { const qt = this.#tracing.find(a => a.subId === sub && a.connId === conn.Id); qt?.gotEose(); - if (sub === this.id) { - if (!this.leaveOpen) { - qt?.sendClose(); - } - } } /** @@ -262,6 +270,7 @@ export class Query implements QueryBase { q.id, c.Address, c.Id, + this.leaveOpen, x => c.CloseReq(x), () => this.#onProgress() ); diff --git a/packages/app/src/System/index.ts b/packages/app/src/System/index.ts index 89146f19..9a061a84 100644 --- a/packages/app/src/System/index.ts +++ b/packages/app/src/System/index.ts @@ -202,7 +202,7 @@ export class NostrSystem extends ExternalStore { filters: sf.filters, relays: sf.relay ? [sf.relay] : undefined, } as QueryBase; - this.SendSubQuery(q, subQ); + this.SendQuery(q, subQ, (q, s, c) => q.sendSubQueryToRelay(c, s)); } q.filters = filters; this.notifyChange(); @@ -234,10 +234,10 @@ export class NostrSystem extends ExternalStore { filters: sf.filters, relays: sf.relay ? [sf.relay] : undefined, } as QueryBase; - this.SendSubQuery(q, subQ); + this.SendQuery(q, subQ, (q, s, c) => q.sendSubQueryToRelay(c, s)); } } else { - this.SendQuery(q); + this.SendQuery(q, q, (q, s, c) => q.sendToRelay(c)); } this.notifyChange(); return store; @@ -250,16 +250,16 @@ export class NostrSystem extends ExternalStore { } } - async SendQuery(q: Query) { + async SendQuery(q: Query, qSend: QueryBase, qSender: (q: Query, qSend: QueryBase, c: Connection) => void) { if (q.relays && q.relays.length > 0) { for (const r of q.relays) { const s = this.Sockets.get(r); if (s) { - q.sendToRelay(s); + qSender(q, qSend, s); } else { const nc = await this.ConnectEphemeralRelay(r); if (nc) { - q.sendToRelay(nc); + qSender(q, qSend, nc); } else { console.warn("Failed to connect to new relay for:", r, q); } @@ -268,31 +268,7 @@ export class NostrSystem extends ExternalStore { } else { for (const [, s] of this.Sockets) { if (!s.Ephemeral) { - q.sendToRelay(s); - } - } - } - } - - async SendSubQuery(q: Query, subQ: QueryBase) { - if (subQ.relays && subQ.relays.length > 0) { - for (const r of subQ.relays) { - const s = this.Sockets.get(r); - if (s) { - q.sendSubQueryToRelay(s, subQ); - } else { - const nc = await this.ConnectEphemeralRelay(r); - if (nc) { - q.sendSubQueryToRelay(nc, subQ); - } else { - console.warn("Failed to connect to new relay for:", r, subQ); - } - } - } - } else { - for (const [, s] of this.Sockets) { - if (!s.Ephemeral) { - q.sendSubQueryToRelay(s, subQ); + qSender(q, qSend, s); } } } diff --git a/packages/app/src/service-worker.ts b/packages/app/src/service-worker.ts index 1e03c491..26cf6bf3 100644 --- a/packages/app/src/service-worker.ts +++ b/packages/app/src/service-worker.ts @@ -4,12 +4,10 @@ declare var self: ServiceWorkerGlobalScope; import { clientsClaim } from "workbox-core"; import { ExpirationPlugin } from "workbox-expiration"; -import { precacheAndRoute } from "workbox-precaching"; import { registerRoute } from "workbox-routing"; import { StaleWhileRevalidate, CacheFirst } from "workbox-strategies"; clientsClaim(); -precacheAndRoute(self.__WB_MANIFEST); const staticTypes = ["image", "video", "audio"]; registerRoute(