11// Nostr plugin module implements nostr state store behavior.
22import { getNostrRuntime } from "./runtime.js" ;
3+ import { normalizeNostrStateAccountId } from "./state-account-id.js" ;
34
45const STORE_VERSION = 2 ;
56const PROFILE_STATE_VERSION = 1 ;
@@ -25,14 +26,6 @@ type NostrProfileState = {
2526 lastPublishResults : Record < string , "ok" | "failed" | "timeout" > | null ;
2627} ;
2728
28- function normalizeAccountId ( accountId ?: string ) : string {
29- const trimmed = accountId ?. trim ( ) ;
30- if ( ! trimmed ) {
31- return "default" ;
32- }
33- return trimmed . replace ( / [ ^ a - z 0 - 9 . _ - ] + / gi, "_" ) ;
34- }
35-
3629function openNostrBusStateStore ( env ?: NodeJS . ProcessEnv ) {
3730 return getNostrRuntime ( ) . state . openKeyedStore < NostrBusState > ( {
3831 namespace : "bus-state" ,
@@ -54,7 +47,9 @@ export async function readNostrBusState(params: {
5447 env ?: NodeJS . ProcessEnv ;
5548} ) : Promise < NostrBusState | null > {
5649 return (
57- ( await openNostrBusStateStore ( params . env ) . lookup ( normalizeAccountId ( params . accountId ) ) ) ?? null
50+ ( await openNostrBusStateStore ( params . env ) . lookup (
51+ normalizeNostrStateAccountId ( params . accountId ) ,
52+ ) ) ?? null
5853 ) ;
5954}
6055
@@ -71,7 +66,10 @@ export async function writeNostrBusState(params: {
7166 gatewayStartedAt : params . gatewayStartedAt ,
7267 recentEventIds : ( params . recentEventIds ?? [ ] ) . filter ( ( x ) : x is string => typeof x === "string" ) ,
7368 } ;
74- await openNostrBusStateStore ( params . env ) . register ( normalizeAccountId ( params . accountId ) , payload ) ;
69+ await openNostrBusStateStore ( params . env ) . register (
70+ normalizeNostrStateAccountId ( params . accountId ) ,
71+ payload ,
72+ ) ;
7573}
7674
7775/**
@@ -107,8 +105,9 @@ export async function readNostrProfileState(params: {
107105 env ?: NodeJS . ProcessEnv ;
108106} ) : Promise < NostrProfileState | null > {
109107 return (
110- ( await openNostrProfileStateStore ( params . env ) . lookup ( normalizeAccountId ( params . accountId ) ) ) ??
111- null
108+ ( await openNostrProfileStateStore ( params . env ) . lookup (
109+ normalizeNostrStateAccountId ( params . accountId ) ,
110+ ) ) ?? null
112111 ) ;
113112}
114113
@@ -126,7 +125,7 @@ export async function writeNostrProfileState(params: {
126125 lastPublishResults : params . lastPublishResults ,
127126 } ;
128127 await openNostrProfileStateStore ( params . env ) . register (
129- normalizeAccountId ( params . accountId ) ,
128+ normalizeNostrStateAccountId ( params . accountId ) ,
130129 payload ,
131130 ) ;
132131}
0 commit comments