Browse Source

implement npub cache singleton

master
Silberengel 10 months ago
parent
commit
2e8d30dd36
  1. 4
      src/lib/utils/nostrUtils.ts
  2. 23
      src/lib/utils/npubCache.ts

4
src/lib/utils/nostrUtils.ts

@ -1,14 +1,12 @@ @@ -1,14 +1,12 @@
import { get } from 'svelte/store';
import { nip19 } from 'nostr-tools';
import { ndkInstance } from '$lib/ndk';
import { npubCache } from './npubCache';
// Regular expressions for Nostr identifiers - match the entire identifier including any prefix
export const NOSTR_PROFILE_REGEX = /(?<![\w/])((nostr:)?(npub|nprofile)[a-zA-Z0-9]{20,})(?![\w/])/g;
export const NOSTR_NOTE_REGEX = /(?<![\w/])((nostr:)?(note|nevent|naddr)[a-zA-Z0-9]{20,})(?![\w/])/g;
// Cache for npub metadata
const npubCache = new Map<string, {name?: string, displayName?: string}>();
/**
* HTML escape a string
*/

23
src/lib/utils/npubCache.ts

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
export type NpubMetadata = { name?: string; displayName?: string };
class NpubCache {
private cache: Record<string, NpubMetadata> = {};
get(key: string): NpubMetadata | undefined {
return this.cache[key];
}
set(key: string, value: NpubMetadata): void {
this.cache[key] = value;
}
has(key: string): boolean {
return key in this.cache;
}
getAll(): Record<string, NpubMetadata> {
return { ...this.cache };
}
}
export const npubCache = new NpubCache();
Loading…
Cancel
Save