small local relay fixes

This commit is contained in:
hzrd149
2024-01-19 10:55:43 +00:00
parent 30ee065ef7
commit 078073bbed
4 changed files with 12 additions and 25 deletions

View File

@@ -1,8 +1,8 @@
import stringify from "json-stringify-deterministic";
import { NostrQuery, NostrRequestFilter, RelayQueryMap } from "../../types/nostr-query";
import { LOCAL_CACHE_RELAY, LOCAL_CACHE_RELAY_ENABLED } from "../../services/local-relay";
import { NostrRequestFilter, RelayQueryMap } from "../../types/nostr-query";
import { Filter } from "nostr-tools";
export function addQueryToFilter(filter: NostrRequestFilter, query: NostrQuery) {
export function addQueryToFilter(filter: NostrRequestFilter, query: Filter) {
if (Array.isArray(filter)) {
return filter.map((f) => ({ ...f, ...query }));
}
@@ -28,13 +28,6 @@ export function mapQueryMap(queryMap: RelayQueryMap, fn: (filter: NostrRequestFi
export function createSimpleQueryMap(relays: string[], filter: NostrRequestFilter) {
const map: RelayQueryMap = {};
// if the local cache relay is enabled, also ask it
if (LOCAL_CACHE_RELAY_ENABLED) {
map[LOCAL_CACHE_RELAY] = filter;
}
for (const relay of relays) map[relay] = filter;
return map;
}

View File

@@ -8,7 +8,6 @@ import relayScoreboardService from "./relay-scoreboard";
import { logger } from "../helpers/debug";
import { matchFilter, matchFilters } from "nostr-tools";
import { NostrEvent } from "../types/nostr-event";
import { LOCAL_CACHE_RELAY, LOCAL_CACHE_RELAY_ENABLED } from "./local-relay";
function hashFilter(filter: NostrRequestFilter) {
return stringify(filter);
@@ -36,10 +35,7 @@ class EventExistsService {
if (!this.filters.has(key)) this.filters.set(key, filter);
if (sub.value !== true) {
const relayUrls = Array.from(relays);
if (LOCAL_CACHE_RELAY_ENABLED) relayUrls.unshift(LOCAL_CACHE_RELAY);
for (const url of relayUrls) {
for (const url of relays) {
if (!asked.has(url) && !pending.has(url)) {
pending.add(url);
}

View File

@@ -4,8 +4,8 @@ import { logger } from "../helpers/debug";
import _throttle from "lodash.throttle";
const log = logger.extend(`LocalRelay`);
const params = new URLSearchParams(location.search);
const params = new URLSearchParams(location.search);
const paramRelay = params.get("localRelay");
// save the cache relay to localStorage
if (paramRelay) {
@@ -18,7 +18,9 @@ const storedCacheRelayURL = localStorage.getItem("localRelay");
const url = (storedCacheRelayURL && new URL(storedCacheRelayURL)) || new URL("/local-relay", location.href);
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
/** @deprecated */
export const LOCAL_CACHE_RELAY_ENABLED = !!window.CACHE_RELAY_ENABLED || !!localStorage.getItem("localRelay");
/** @deprecated */
export const LOCAL_CACHE_RELAY = url.toString();
export const localDatabase = await openDB();

View File

@@ -1,6 +1,7 @@
import dayjs from "dayjs";
import debug, { Debugger } from "debug";
import _throttle from "lodash/throttle";
import { Filter } from "nostr-tools";
import NostrSubscription from "../classes/nostr-subscription";
import SuperMap from "../classes/super-map";
@@ -8,11 +9,10 @@ import { NostrEvent } from "../types/nostr-event";
import Subject from "../classes/subject";
import { NostrQuery } from "../types/nostr-query";
import { logger } from "../helpers/debug";
import db from "./db";
import { nameOrPubkey } from "./user-metadata";
import { getEventCoordinate, parseCoordinate } from "../helpers/nostr/events";
import { getEventCoordinate } from "../helpers/nostr/events";
import createDefer, { Deferred } from "../classes/deferred";
import { LOCAL_CACHE_RELAY, LOCAL_CACHE_RELAY_ENABLED, localRelay } from "./local-relay";
import { localRelay } from "./local-relay";
import { relayRequest } from "../helpers/relay";
type Pubkey = string;
@@ -183,7 +183,7 @@ class ReplaceableEventLoaderService {
private async readFromCache() {
if (this.readFromCachePromises.size === 0) return;
const kindFilters: Record<number, NostrQuery> = {};
const kindFilters: Record<number, Filter> = {};
for (const [cord] of this.readFromCachePromises) {
const [kindStr, pubkey, d] = cord.split(":") as [string, string] | [string, string, string];
const kind = parseInt(kindStr);
@@ -247,11 +247,7 @@ class ReplaceableEventLoaderService {
const cord = createCoordinate(kind, pubkey, d);
const sub = this.events.get(cord);
const relayUrls = Array.from(relays);
// TODO: use localRelay instead
if (LOCAL_CACHE_RELAY_ENABLED) relayUrls.unshift(LOCAL_CACHE_RELAY);
for (const relay of relayUrls) {
for (const relay of relays) {
const request = this.loaders.get(relay).requestEvent(kind, pubkey, d);
sub.connectWithHandler(request, (event, next, current) => {