nostr-websocket-utils 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +151 -103
  3. package/dist/__mocks__/extendedWsMock.d.ts +35 -0
  4. package/dist/__mocks__/extendedWsMock.js +156 -0
  5. package/dist/__mocks__/logger.d.ts +9 -0
  6. package/dist/__mocks__/logger.js +6 -0
  7. package/dist/__mocks__/mockLogger.d.ts +41 -0
  8. package/dist/__mocks__/mockLogger.js +47 -0
  9. package/dist/__mocks__/mockserver.d.ts +31 -0
  10. package/dist/__mocks__/mockserver.js +39 -0
  11. package/dist/__mocks__/wsMock.d.ts +26 -0
  12. package/dist/__mocks__/wsMock.js +120 -0
  13. package/dist/client.d.ts +105 -0
  14. package/dist/client.js +105 -0
  15. package/dist/core/client.d.ts +94 -0
  16. package/dist/core/client.js +360 -0
  17. package/dist/core/nostr-server.d.ts +27 -0
  18. package/dist/core/nostr-server.js +95 -0
  19. package/dist/core/queue.d.ts +61 -0
  20. package/dist/core/queue.js +108 -0
  21. package/dist/core/server.d.ts +27 -0
  22. package/dist/core/server.js +114 -0
  23. package/dist/crypto/bech32.d.ts +26 -0
  24. package/dist/crypto/bech32.js +163 -0
  25. package/dist/crypto/handlers.d.ts +11 -0
  26. package/dist/crypto/handlers.js +36 -0
  27. package/dist/crypto/index.d.ts +5 -0
  28. package/dist/crypto/index.js +5 -0
  29. package/dist/crypto/schnorr.d.ts +16 -0
  30. package/dist/crypto/schnorr.js +51 -0
  31. package/dist/endpoints/metrics.d.ts +29 -0
  32. package/dist/endpoints/metrics.js +101 -0
  33. package/dist/index.d.ts +11 -6
  34. package/dist/index.js +16 -4
  35. package/dist/nips/index.d.ts +19 -0
  36. package/dist/nips/index.js +34 -0
  37. package/dist/nips/nip-01.d.ts +34 -0
  38. package/dist/nips/nip-01.js +145 -0
  39. package/dist/nips/nip-02.d.ts +83 -0
  40. package/dist/nips/nip-02.js +123 -0
  41. package/dist/nips/nip-04.d.ts +36 -0
  42. package/dist/nips/nip-04.js +105 -0
  43. package/dist/nips/nip-05.d.ts +86 -0
  44. package/dist/nips/nip-05.js +151 -0
  45. package/dist/nips/nip-09.d.ts +92 -0
  46. package/dist/nips/nip-09.js +190 -0
  47. package/dist/nips/nip-11.d.ts +64 -0
  48. package/dist/nips/nip-11.js +154 -0
  49. package/dist/nips/nip-13.d.ts +73 -0
  50. package/dist/nips/nip-13.js +128 -0
  51. package/dist/nips/nip-15.d.ts +83 -0
  52. package/dist/nips/nip-15.js +101 -0
  53. package/dist/nips/nip-16.d.ts +88 -0
  54. package/dist/nips/nip-16.js +150 -0
  55. package/dist/nips/nip-19.d.ts +28 -0
  56. package/dist/nips/nip-19.js +103 -0
  57. package/dist/nips/nip-20.d.ts +59 -0
  58. package/dist/nips/nip-20.js +95 -0
  59. package/dist/nips/nip-22.d.ts +89 -0
  60. package/dist/nips/nip-22.js +142 -0
  61. package/dist/nips/nip-26.d.ts +52 -0
  62. package/dist/nips/nip-26.js +139 -0
  63. package/dist/nips/nip-28.d.ts +103 -0
  64. package/dist/nips/nip-28.js +170 -0
  65. package/dist/nips/nip-33.d.ts +94 -0
  66. package/dist/nips/nip-33.js +133 -0
  67. package/dist/nostr-server.d.ts +23 -0
  68. package/dist/nostr-server.js +44 -0
  69. package/dist/server.d.ts +13 -3
  70. package/dist/server.js +60 -33
  71. package/dist/transport/base.d.ts +54 -0
  72. package/dist/transport/base.js +104 -0
  73. package/dist/transport/websocket.d.ts +22 -0
  74. package/dist/transport/websocket.js +122 -0
  75. package/dist/types/events.d.ts +63 -0
  76. package/dist/types/events.js +5 -0
  77. package/dist/types/filters.d.ts +19 -0
  78. package/dist/types/filters.js +5 -0
  79. package/dist/types/handlers.d.ts +80 -0
  80. package/dist/types/handlers.js +5 -0
  81. package/dist/types/index.d.ts +118 -39
  82. package/dist/types/index.js +21 -1
  83. package/dist/types/logger.d.ts +40 -0
  84. package/dist/types/logger.js +5 -0
  85. package/dist/types/messages.d.ts +135 -0
  86. package/dist/types/messages.js +40 -0
  87. package/dist/types/nostr.d.ts +120 -39
  88. package/dist/types/nostr.js +5 -10
  89. package/dist/types/options.d.ts +154 -0
  90. package/dist/types/options.js +5 -0
  91. package/dist/types/relays.d.ts +26 -0
  92. package/dist/types/relays.js +5 -0
  93. package/dist/types/scoring.d.ts +47 -0
  94. package/dist/types/scoring.js +29 -0
  95. package/dist/types/socket.d.ts +99 -0
  96. package/dist/types/socket.js +5 -0
  97. package/dist/types/transport.d.ts +97 -0
  98. package/dist/types/transport.js +5 -0
  99. package/dist/types/validation.d.ts +50 -0
  100. package/dist/types/validation.js +5 -0
  101. package/dist/types/websocket.d.ts +172 -0
  102. package/dist/types/websocket.js +5 -0
  103. package/dist/utils/http.d.ts +10 -0
  104. package/dist/utils/http.js +24 -0
  105. package/dist/utils/logger.d.ts +11 -2
  106. package/dist/utils/logger.js +18 -13
  107. package/dist/utils/metrics.d.ts +81 -0
  108. package/dist/utils/metrics.js +206 -0
  109. package/dist/utils/rate-limiter.d.ts +85 -0
  110. package/dist/utils/rate-limiter.js +175 -0
  111. package/package.json +18 -21
@@ -1,58 +1,137 @@
1
+ /**
2
+ * @file Core type definitions
3
+ * @module types
4
+ */
1
5
  import type { WebSocket } from 'ws';
2
- export type MessageType = 'subscribe' | 'unsubscribe' | 'event' | 'request' | 'response' | 'error' | 'status';
6
+ import type { Logger } from './logger';
7
+ import type { NostrWSMessage } from './messages';
8
+ export * from './messages';
9
+ export * from './events';
10
+ export * from './filters';
11
+ export * from './relays';
12
+ export * from './logger';
13
+ /**
14
+ * Extended WebSocket interface with client ID
15
+ */
16
+ export interface ExtendedWebSocket extends WebSocket {
17
+ clientId?: string;
18
+ isAlive?: boolean;
19
+ subscriptions?: Set<string>;
20
+ lastPing?: number;
21
+ reconnectAttempts?: number;
22
+ messageQueue?: NostrWSMessage[];
23
+ }
24
+ /**
25
+ * WebSocket connection states
26
+ */
27
+ export declare enum ConnectionState {
28
+ CONNECTING = "CONNECTING",
29
+ CONNECTED = "CONNECTED",
30
+ DISCONNECTED = "DISCONNECTED",
31
+ RECONNECTING = "RECONNECTING",
32
+ FAILED = "FAILED"
33
+ }
34
+ /**
35
+ * Retry configuration options
36
+ */
37
+ export interface RetryConfig {
38
+ maxAttempts: number;
39
+ initialDelay: number;
40
+ maxDelay: number;
41
+ backoffFactor: number;
42
+ }
43
+ /**
44
+ * Queue configuration options
45
+ */
46
+ export interface QueueConfig {
47
+ maxSize: number;
48
+ maxRetries: number;
49
+ retryDelay: number;
50
+ staleTimeout: number;
51
+ }
52
+ /**
53
+ * Heartbeat configuration options
54
+ */
55
+ export interface HeartbeatConfig {
56
+ interval: number;
57
+ timeout: number;
58
+ maxMissed: number;
59
+ }
60
+ /**
61
+ * WebSocket client options
62
+ */
3
63
  export interface NostrWSOptions {
4
- heartbeatInterval?: number;
5
- reconnectInterval?: number;
6
- maxReconnectAttempts?: number;
7
- logger: Logger;
8
64
  WebSocketImpl: typeof WebSocket;
9
- handlers: {
10
- message: (ws: ExtendedWebSocket, message: NostrWSMessage) => Promise<void> | void;
11
- error?: (ws: WebSocket, error: Error) => void;
12
- close?: (ws: WebSocket) => void;
13
- };
14
- }
15
- export interface NostrWSMessage {
16
- type: string;
17
- id?: string;
18
- data: Record<string, unknown>;
65
+ handlers: NostrWSClientEvents;
66
+ retry?: Partial<RetryConfig>;
67
+ queue?: Partial<QueueConfig>;
68
+ heartbeat?: Partial<HeartbeatConfig>;
69
+ autoReconnect?: boolean;
70
+ bufferMessages?: boolean;
71
+ cleanStaleMessages?: boolean;
72
+ logger: Logger;
19
73
  }
74
+ /**
75
+ * Represents a subscription to a Nostr relay
76
+ */
20
77
  export interface NostrWSSubscription {
78
+ /**
79
+ * Channel identifier for the subscription
80
+ */
21
81
  channel: string;
82
+ /**
83
+ * Filter criteria for the subscription
84
+ */
22
85
  filter?: Record<string, unknown>;
23
86
  }
87
+ /**
88
+ * Events emitted by the NostrWSClient
89
+ */
24
90
  export interface NostrWSClientEvents {
91
+ /**
92
+ * Emitted when the client connects to the relay
93
+ */
25
94
  connect: () => void;
95
+ /**
96
+ * Emitted when the client disconnects from the relay
97
+ */
26
98
  disconnect: () => void;
99
+ /**
100
+ * Emitted when the client reconnects to the relay
101
+ */
27
102
  reconnect: () => void;
28
- message: (message: NostrWSMessage) => void;
103
+ /**
104
+ * Emitted when a message is received from the relay
105
+ * @param message - The received message
106
+ */
107
+ message: (message: NostrWSMessage) => Promise<void>;
108
+ /**
109
+ * Emitted when an error occurs
110
+ * @param error - The error that occurred
111
+ */
29
112
  error: (error: Error) => void;
113
+ close: () => void;
114
+ stateChange?: (state: ConnectionState) => void;
115
+ heartbeat?: () => void;
30
116
  }
117
+ /**
118
+ * Events emitted by the NostrWSServer
119
+ */
31
120
  export interface NostrWSServerEvents {
121
+ /**
122
+ * Emitted when a client connects
123
+ * @param client - The connected client
124
+ */
32
125
  connection: (client: ExtendedWebSocket) => void;
126
+ /**
127
+ * Emitted when a message is received from a client
128
+ * @param message - The received message
129
+ * @param client - The client that sent the message
130
+ */
33
131
  message: (message: NostrWSMessage, client: ExtendedWebSocket) => void;
132
+ /**
133
+ * Emitted when an error occurs
134
+ * @param error - The error that occurred
135
+ */
34
136
  error: (error: Error) => void;
35
137
  }
36
- export interface ExtendedWebSocket extends WebSocket {
37
- isAlive?: boolean;
38
- subscriptions?: Set<string>;
39
- clientId?: string;
40
- messageQueue?: NostrWSMessage[];
41
- lastPing?: number;
42
- reconnectAttempts?: number;
43
- }
44
- export interface NostrWSValidationResult {
45
- isValid: boolean;
46
- error?: string;
47
- }
48
- export interface NostrWSConnectionState {
49
- isConnected: boolean;
50
- reconnectAttempts: number;
51
- lastError?: string;
52
- }
53
- export interface Logger {
54
- debug: (message: string, ...args: unknown[]) => void;
55
- info: (message: string, ...args: unknown[]) => void;
56
- warn: (message: string, ...args: unknown[]) => void;
57
- error: (message: string, ...args: unknown[]) => void;
58
- }
@@ -1 +1,21 @@
1
- export {};
1
+ /**
2
+ * @file Core type definitions
3
+ * @module types
4
+ */
5
+ // Re-export all types
6
+ export * from './messages';
7
+ export * from './events';
8
+ export * from './filters';
9
+ export * from './relays';
10
+ export * from './logger';
11
+ /**
12
+ * WebSocket connection states
13
+ */
14
+ export var ConnectionState;
15
+ (function (ConnectionState) {
16
+ ConnectionState["CONNECTING"] = "CONNECTING";
17
+ ConnectionState["CONNECTED"] = "CONNECTED";
18
+ ConnectionState["DISCONNECTED"] = "DISCONNECTED";
19
+ ConnectionState["RECONNECTING"] = "RECONNECTING";
20
+ ConnectionState["FAILED"] = "FAILED";
21
+ })(ConnectionState || (ConnectionState = {}));
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @file Logger type definitions
3
+ * @module types/logger
4
+ */
5
+ import type { Logger as PinoLogger } from 'pino';
6
+ export type Logger = PinoLogger;
7
+ /**
8
+ * WebSocket context for logging
9
+ */
10
+ export interface WebSocketLogContext {
11
+ clientId?: string;
12
+ url?: string;
13
+ messageId?: string;
14
+ [key: string]: unknown;
15
+ }
16
+ /**
17
+ * Extended logger interface with WebSocket-specific methods
18
+ */
19
+ export interface NostrWSLogger extends Logger {
20
+ /**
21
+ * Log WebSocket connection event
22
+ */
23
+ wsConnect(context: WebSocketLogContext): void;
24
+ /**
25
+ * Log WebSocket disconnection event
26
+ */
27
+ wsDisconnect(context: WebSocketLogContext): void;
28
+ /**
29
+ * Log WebSocket message event
30
+ */
31
+ wsMessage(context: WebSocketLogContext): void;
32
+ /**
33
+ * Log WebSocket error event
34
+ */
35
+ wsError(error: Error, context: WebSocketLogContext): void;
36
+ /**
37
+ * Log WebSocket metrics
38
+ */
39
+ wsMetrics(metrics: Record<string, unknown>, context: WebSocketLogContext): void;
40
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @file Logger type definitions
3
+ * @module types/logger
4
+ */
5
+ export {};
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @file Message type definitions for WebSocket communication
3
+ * @module types/messages
4
+ */
5
+ /**
6
+ * Connection states for WebSocket client
7
+ */
8
+ export declare enum ConnectionState {
9
+ CONNECTING = "CONNECTING",
10
+ CONNECTED = "CONNECTED",
11
+ DISCONNECTED = "DISCONNECTED",
12
+ RECONNECTING = "RECONNECTING",
13
+ FAILED = "FAILED"
14
+ }
15
+ /**
16
+ * Type of message that can be sent through the WebSocket connection
17
+ * Following NIP-01 and other NIPs message types
18
+ */
19
+ export type MessageType = 'EVENT' | 'REQ' | 'CLOSE' | 'NOTICE' | 'EOSE' | 'OK' | 'AUTH' | 'COUNT' | 'PING' | 'PONG' | 'error';
20
+ /**
21
+ * Message priority levels for queue management
22
+ */
23
+ export declare enum MessagePriority {
24
+ HIGH = 0,// Critical messages (AUTH, etc.)
25
+ MEDIUM = 1,// Normal messages (EVENT, REQ)
26
+ LOW = 2
27
+ }
28
+ /**
29
+ * Structure of a Nostr WebSocket event
30
+ */
31
+ export interface NostrWSEvent {
32
+ id: string;
33
+ pubkey: string;
34
+ created_at: number;
35
+ kind: number;
36
+ tags: string[][];
37
+ content: string;
38
+ sig?: string;
39
+ }
40
+ /**
41
+ * Structure of a Nostr WebSocket filter
42
+ */
43
+ export interface NostrWSFilter {
44
+ ids?: string[];
45
+ authors?: string[];
46
+ kinds?: number[];
47
+ since?: number;
48
+ until?: number;
49
+ limit?: number;
50
+ [key: string]: unknown;
51
+ }
52
+ /**
53
+ * Structure of messages sent through the WebSocket connection
54
+ */
55
+ export interface NostrWSMessage {
56
+ /**
57
+ * Type of the message following NIP specifications
58
+ */
59
+ type: MessageType;
60
+ /**
61
+ * Message content - structure depends on type
62
+ */
63
+ content?: NostrWSEvent | NostrWSFilter | string | unknown;
64
+ /**
65
+ * Optional subscription ID for subscription-based messages
66
+ */
67
+ subscription_id?: string;
68
+ /**
69
+ * Message priority for queue management
70
+ */
71
+ priority?: MessagePriority;
72
+ /**
73
+ * Timestamp when the message was queued
74
+ */
75
+ queuedAt?: number;
76
+ /**
77
+ * Number of retry attempts for this message
78
+ */
79
+ retryCount?: number;
80
+ /**
81
+ * Additional data for the message
82
+ */
83
+ data?: unknown;
84
+ }
85
+ /**
86
+ * Represents a subscription to a Nostr relay
87
+ */
88
+ export interface NostrWSSubscription {
89
+ /**
90
+ * Unique identifier for the subscription
91
+ */
92
+ subscription_id: string;
93
+ /**
94
+ * Filters for the subscription
95
+ */
96
+ filters: Array<Record<string, unknown>>;
97
+ /**
98
+ * Active state of the subscription
99
+ */
100
+ active?: boolean;
101
+ /**
102
+ * Timestamp when the subscription was created
103
+ */
104
+ createdAt?: number;
105
+ }
106
+ /**
107
+ * Result of validating a NostrWSMessage
108
+ */
109
+ export interface NostrWSValidationResult {
110
+ /**
111
+ * Whether the message is valid
112
+ */
113
+ isValid: boolean;
114
+ /**
115
+ * Error message if validation failed
116
+ */
117
+ error?: string;
118
+ }
119
+ /**
120
+ * Message types as defined in NIP-01 and other NIPs
121
+ */
122
+ export declare const MESSAGE_TYPES: {
123
+ readonly EVENT: "EVENT";
124
+ readonly REQ: "REQ";
125
+ readonly CLOSE: "CLOSE";
126
+ readonly NOTICE: "NOTICE";
127
+ readonly EOSE: "EOSE";
128
+ readonly OK: "OK";
129
+ readonly AUTH: "AUTH";
130
+ readonly COUNT: "COUNT";
131
+ readonly PING: "PING";
132
+ readonly PONG: "PONG";
133
+ readonly ERROR: "error";
134
+ };
135
+ export type NostrWSMessageType = MessageType;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @file Message type definitions for WebSocket communication
3
+ * @module types/messages
4
+ */
5
+ /**
6
+ * Connection states for WebSocket client
7
+ */
8
+ export var ConnectionState;
9
+ (function (ConnectionState) {
10
+ ConnectionState["CONNECTING"] = "CONNECTING";
11
+ ConnectionState["CONNECTED"] = "CONNECTED";
12
+ ConnectionState["DISCONNECTED"] = "DISCONNECTED";
13
+ ConnectionState["RECONNECTING"] = "RECONNECTING";
14
+ ConnectionState["FAILED"] = "FAILED";
15
+ })(ConnectionState || (ConnectionState = {}));
16
+ /**
17
+ * Message priority levels for queue management
18
+ */
19
+ export var MessagePriority;
20
+ (function (MessagePriority) {
21
+ MessagePriority[MessagePriority["HIGH"] = 0] = "HIGH";
22
+ MessagePriority[MessagePriority["MEDIUM"] = 1] = "MEDIUM";
23
+ MessagePriority[MessagePriority["LOW"] = 2] = "LOW"; // Non-critical messages (PING)
24
+ })(MessagePriority || (MessagePriority = {}));
25
+ /**
26
+ * Message types as defined in NIP-01 and other NIPs
27
+ */
28
+ export const MESSAGE_TYPES = {
29
+ EVENT: 'EVENT',
30
+ REQ: 'REQ',
31
+ CLOSE: 'CLOSE',
32
+ NOTICE: 'NOTICE',
33
+ EOSE: 'EOSE',
34
+ OK: 'OK',
35
+ AUTH: 'AUTH',
36
+ COUNT: 'COUNT',
37
+ PING: 'PING',
38
+ PONG: 'PONG',
39
+ ERROR: 'error'
40
+ };
@@ -1,52 +1,133 @@
1
- import { ExtendedWebSocket } from './index';
2
- export interface NostrWSEvent {
1
+ /**
2
+ * @file Nostr protocol type definitions
3
+ * @module types/nostr
4
+ */
5
+ import type { WebSocket as WSClient } from 'ws';
6
+ import { Logger } from './logger';
7
+ import type { NostrWSMessage } from './messages';
8
+ import type { NostrEvent } from './events';
9
+ /**
10
+ * Signed Nostr event with id and signature
11
+ */
12
+ export interface SignedNostrEvent extends NostrEvent {
13
+ /**
14
+ * Event ID (32-bytes lowercase hex of the serialized event data)
15
+ */
3
16
  id: string;
4
- pubkey: string;
5
- created_at: number;
6
- kind: number;
7
- tags: string[][];
8
- content: string;
17
+ /**
18
+ * Signature of the event ID (64-bytes hex of the schnorr signature)
19
+ */
9
20
  sig: string;
10
21
  }
11
- export interface NostrWSFilter {
12
- ids?: string[];
13
- authors?: string[];
14
- kinds?: number[];
15
- '#e'?: string[];
16
- '#p'?: string[];
17
- since?: number;
18
- until?: number;
19
- limit?: number;
20
- }
21
- export interface NostrWSSocket extends ExtendedWebSocket {
22
+ /**
23
+ * Extended WebSocket interface for Nostr
24
+ */
25
+ export interface NostrWSSocket extends WSClient {
26
+ /**
27
+ * Whether the socket is alive (for ping/pong)
28
+ */
29
+ isAlive?: boolean;
30
+ /**
31
+ * Set of active subscription IDs
32
+ */
22
33
  subscriptions?: Set<string>;
23
- authenticated?: boolean;
24
- pubkey?: string;
25
- }
26
- export declare enum NostrWSMessageType {
27
- EVENT = "EVENT",
28
- REQ = "REQ",
29
- CLOSE = "CLOSE",
30
- NOTICE = "NOTICE",
31
- OK = "OK",
32
- AUTH = "AUTH",
33
- EOSE = "EOSE"
34
+ /**
35
+ * Unique client identifier
36
+ */
37
+ clientId?: string;
34
38
  }
35
- export type NostrWSServerMessage = [NostrWSMessageType, ...unknown[]];
39
+ /**
40
+ * Server configuration options
41
+ */
36
42
  export interface NostrWSServerOptions {
43
+ /**
44
+ * Port to listen on
45
+ */
37
46
  port: number;
47
+ /**
48
+ * Host to bind to
49
+ */
50
+ host?: string;
51
+ /**
52
+ * Path for the WebSocket endpoint
53
+ */
54
+ path?: string;
55
+ /**
56
+ * Maximum payload size in bytes
57
+ */
58
+ maxPayload?: number;
59
+ /**
60
+ * Ping interval in milliseconds
61
+ */
62
+ pingInterval?: number;
63
+ /**
64
+ * Client timeout in milliseconds
65
+ */
66
+ clientTimeout?: number;
67
+ /**
68
+ * Heartbeat interval in milliseconds
69
+ */
38
70
  heartbeatInterval?: number;
39
- maxPayloadSize?: number;
40
- cors?: {
41
- origin: string;
42
- methods: string[];
43
- };
44
- onConnection?: (socket: NostrWSSocket) => void;
45
- onDisconnect?: (socket: NostrWSSocket) => void;
46
- onError?: (error: Error, socket?: NostrWSSocket) => void;
71
+ /**
72
+ * Handlers for various events
73
+ */
47
74
  handlers?: {
48
- message: (socket: NostrWSSocket, message: NostrWSServerMessage) => void | Promise<void>;
75
+ message?: (socket: NostrWSSocket, message: NostrWSServerMessage) => Promise<void>;
49
76
  error?: (socket: NostrWSSocket, error: Error) => void;
50
77
  close?: (socket: NostrWSSocket) => void;
51
78
  };
79
+ /**
80
+ * Callback for when a new connection is established
81
+ */
82
+ onConnection?: (socket: NostrWSSocket) => void;
83
+ }
84
+ /**
85
+ * Server message structure
86
+ */
87
+ export interface NostrWSServerMessage {
88
+ /**
89
+ * Message type
90
+ */
91
+ type: string;
92
+ /**
93
+ * Message data
94
+ */
95
+ data?: unknown;
96
+ /**
97
+ * Error message if applicable
98
+ */
99
+ error?: string;
100
+ }
101
+ /**
102
+ * Extended WebSocket interface for Nostr server
103
+ */
104
+ export interface NostrWSServerSocket extends WSClient {
105
+ clientId: string;
106
+ subscriptions: Set<string>;
107
+ lastPing?: number;
108
+ isAlive: boolean;
109
+ }
110
+ /**
111
+ * Nostr WebSocket server options
112
+ */
113
+ export interface NostrWSServerServerOptions {
114
+ port: number;
115
+ host?: string;
116
+ path?: string;
117
+ logger: Logger;
118
+ maxClients?: number;
119
+ pingInterval?: number;
120
+ pingTimeout?: number;
121
+ handlers?: {
122
+ message?: (message: NostrWSServerMessage, ws: NostrWSServerSocket) => Promise<void>;
123
+ error?: (error: Error, ws: NostrWSServerSocket) => void;
124
+ close?: (ws: NostrWSServerSocket) => void;
125
+ };
126
+ }
127
+ /**
128
+ * Nostr WebSocket server message
129
+ */
130
+ export interface NostrWSServerServerMessage extends NostrWSMessage {
131
+ clientId?: string;
132
+ timestamp?: number;
52
133
  }
@@ -1,10 +1,5 @@
1
- export var NostrWSMessageType;
2
- (function (NostrWSMessageType) {
3
- NostrWSMessageType["EVENT"] = "EVENT";
4
- NostrWSMessageType["REQ"] = "REQ";
5
- NostrWSMessageType["CLOSE"] = "CLOSE";
6
- NostrWSMessageType["NOTICE"] = "NOTICE";
7
- NostrWSMessageType["OK"] = "OK";
8
- NostrWSMessageType["AUTH"] = "AUTH";
9
- NostrWSMessageType["EOSE"] = "EOSE";
10
- })(NostrWSMessageType || (NostrWSMessageType = {}));
1
+ /**
2
+ * @file Nostr protocol type definitions
3
+ * @module types/nostr
4
+ */
5
+ export {};