-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.ts
154 lines (137 loc) · 3.39 KB
/
messages.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
/**
* @file Message type definitions
* @module types/messages
*/
import { MessagePriority } from './priority.js';
/**
* Message types that can be sent through the WebSocket connection
* Following NIP-01 and other NIPs message types
*/
export const MESSAGE_TYPES = {
EVENT: 'EVENT', // NIP-01: Basic protocol flow events
REQ: 'REQ', // NIP-01: Request events
CLOSE: 'CLOSE', // NIP-01: Close subscription
NOTICE: 'NOTICE', // NIP-01: Human-readable messages
EOSE: 'EOSE', // NIP-15: End of stored events notice
OK: 'OK', // NIP-20: Command result
AUTH: 'AUTH', // NIP-42: Authentication
COUNT: 'COUNT', // NIP-45: Event counts
PING: 'PING', // Internal heartbeat ping
PONG: 'PONG', // Internal heartbeat pong
ERROR: 'error' // Internal error type (lowercase to differentiate)
} as const;
export type MessageType = keyof typeof MESSAGE_TYPES;
/**
* Base Nostr WebSocket message interface
*/
export interface NostrWSMessageBase {
type: MessageType;
data?: unknown;
priority?: MessagePriority;
queuedAt?: number;
retryCount?: number;
}
/**
* Nostr WebSocket message type
* Array format: [<message_type>, ...data]
*/
export type NostrWSMessage = [MessageType, ...unknown[]];
/**
* Queue item interface for message queue
*/
export interface QueueItem extends NostrWSMessageBase {
priority: MessagePriority;
queuedAt: number;
retryCount: number;
}
/**
* Nostr event message
*/
export interface NostrEvent {
id: string;
kind: number;
content: string;
tags: string[][];
created_at?: number;
pubkey?: string;
sig?: string;
}
/**
* Server-side Nostr WebSocket message
* Extends the base NostrWSMessage tuple type with additional server-specific metadata
*/
export type NostrWSServerMessage = NostrWSMessage & {
clientId?: string;
timestamp?: number;
};
/**
* Helper function to create server messages
*/
export function createServerMessage(type: MessageType, data: unknown[], clientId?: string): NostrWSServerMessage {
const message = [type, ...data] as NostrWSServerMessage;
message.clientId = clientId;
message.timestamp = Date.now();
return message;
}
/**
* Structure of a Nostr WebSocket filter
*/
export interface NostrWSFilter {
/** Array of event IDs */
ids?: string[];
/** Array of pubkeys */
authors?: string[];
/** Array of event kinds */
kinds?: number[];
/** Event tags */
tags?: Record<string, string[]>;
/** Unix timestamp range */
since?: number;
until?: number;
/** Maximum number of events to return */
limit?: number;
}
/**
* Represents a subscription to a Nostr relay
*/
export interface NostrWSSubscription {
/**
* Unique identifier for the subscription
*/
subscription_id: string;
/**
* Filters for the subscription
*/
filters: Array<Record<string, unknown>>;
/**
* Active state of the subscription
*/
active?: boolean;
/**
* Timestamp when the subscription was created
*/
createdAt?: number;
}
/**
* Result of validating a NostrWSMessage
*/
export interface NostrWSValidationResult {
/**
* Whether the message is valid
*/
isValid: boolean;
/**
* Error message if validation failed
*/
error?: string;
}
/**
* Connection states for WebSocket client
*/
export enum ConnectionState {
CONNECTING = 'CONNECTING',
CONNECTED = 'CONNECTED',
DISCONNECTED = 'DISCONNECTED',
RECONNECTING = 'RECONNECTING',
FAILED = 'FAILED'
}