-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
164 lines (145 loc) · 3.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @file Core type definitions
* @module types
*/
import type { WebSocket } from 'ws';
import type { Logger } from './logger.js';
import type { NostrWSMessage, NostrEvent as MessageNostrEvent } from './messages.js';
// Re-export specific types to avoid ambiguity
export type { NostrWSMessage } from './messages.js';
export type { QueueItem } from './messages.js';
export * from './filters.js';
export * from './relays.js';
export * from './logger.js';
export * from './priority.js';
// Export the NostrEvent from messages.ts as our canonical version
export type NostrEvent = MessageNostrEvent;
/**
* Extended WebSocket interface with client ID
*/
export interface ExtendedWebSocket extends WebSocket {
clientId?: string;
isAlive?: boolean;
subscriptions?: Set<string>;
lastPing?: number;
reconnectAttempts?: number;
messageQueue?: NostrWSMessage[];
}
/**
* WebSocket connection states
*/
export enum ConnectionState {
CONNECTING = 'CONNECTING',
CONNECTED = 'CONNECTED',
DISCONNECTED = 'DISCONNECTED',
RECONNECTING = 'RECONNECTING',
FAILED = 'FAILED'
}
/**
* Retry configuration options
*/
export interface RetryConfig {
maxAttempts: number;
initialDelay: number;
maxDelay: number;
backoffFactor: number;
}
/**
* Queue configuration options
*/
export interface QueueConfig {
maxSize: number;
maxRetries: number;
retryDelay: number;
staleTimeout: number;
}
/**
* Heartbeat configuration options
*/
export interface HeartbeatConfig {
interval: number;
timeout: number;
maxMissed: number;
}
/**
* WebSocket client options
*/
export interface NostrWSOptions {
WebSocketImpl?: typeof WebSocket;
connectionTimeout?: number;
retryAttempts?: number;
retryDelay?: number;
onMessage?: (message: string) => void;
onError?: (error: Error) => void;
retry?: Partial<RetryConfig>;
queue?: Partial<QueueConfig>;
heartbeat?: Partial<HeartbeatConfig>;
autoReconnect?: boolean;
bufferMessages?: boolean;
cleanStaleMessages?: boolean;
logger?: Logger;
}
/**
* Represents a subscription to a Nostr relay
*/
export interface NostrWSSubscription {
/**
* Channel identifier for the subscription
*/
channel: string;
/**
* Filter criteria for the subscription
*/
filter?: Record<string, unknown>;
}
/**
* Events emitted by the NostrWSClient
*/
export interface NostrWSClientEvents {
/**
* Emitted when the client connects to the relay
*/
connect: () => void;
/**
* Emitted when the client disconnects from the relay
*/
disconnect: () => void;
/**
* Emitted when the client reconnects to the relay
*/
reconnect: () => void;
/**
* Emitted when a message is received from the relay
* @param message - The received message
*/
message: (message: NostrWSMessage) => Promise<void>;
/**
* Emitted when an error occurs
* @param error - The error that occurred
*/
error: (error: Error) => void;
close: () => void;
stateChange?: (state: ConnectionState) => void;
heartbeat?: () => void;
}
/**
* Events emitted by the NostrWSServer
*/
export interface NostrWSServerEvents {
/**
* Emitted when a client connects
* @param client - The connected client
*/
connection: (client: ExtendedWebSocket) => void;
/**
* Emitted when a message is received from a client
* @param message - The received message
* @param client - The client that sent the message
*/
message: (message: NostrWSMessage, client: ExtendedWebSocket) => void;
/**
* Emitted when an error occurs
* @param error - The error that occurred
*/
error: (error: Error) => void;
}