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.
- package/LICENSE +1 -1
- package/README.md +151 -103
- package/dist/__mocks__/extendedWsMock.d.ts +35 -0
- package/dist/__mocks__/extendedWsMock.js +156 -0
- package/dist/__mocks__/logger.d.ts +9 -0
- package/dist/__mocks__/logger.js +6 -0
- package/dist/__mocks__/mockLogger.d.ts +41 -0
- package/dist/__mocks__/mockLogger.js +47 -0
- package/dist/__mocks__/mockserver.d.ts +31 -0
- package/dist/__mocks__/mockserver.js +39 -0
- package/dist/__mocks__/wsMock.d.ts +26 -0
- package/dist/__mocks__/wsMock.js +120 -0
- package/dist/client.d.ts +105 -0
- package/dist/client.js +105 -0
- package/dist/core/client.d.ts +94 -0
- package/dist/core/client.js +360 -0
- package/dist/core/nostr-server.d.ts +27 -0
- package/dist/core/nostr-server.js +95 -0
- package/dist/core/queue.d.ts +61 -0
- package/dist/core/queue.js +108 -0
- package/dist/core/server.d.ts +27 -0
- package/dist/core/server.js +114 -0
- package/dist/crypto/bech32.d.ts +26 -0
- package/dist/crypto/bech32.js +163 -0
- package/dist/crypto/handlers.d.ts +11 -0
- package/dist/crypto/handlers.js +36 -0
- package/dist/crypto/index.d.ts +5 -0
- package/dist/crypto/index.js +5 -0
- package/dist/crypto/schnorr.d.ts +16 -0
- package/dist/crypto/schnorr.js +51 -0
- package/dist/endpoints/metrics.d.ts +29 -0
- package/dist/endpoints/metrics.js +101 -0
- package/dist/index.d.ts +11 -6
- package/dist/index.js +16 -4
- package/dist/nips/index.d.ts +19 -0
- package/dist/nips/index.js +34 -0
- package/dist/nips/nip-01.d.ts +34 -0
- package/dist/nips/nip-01.js +145 -0
- package/dist/nips/nip-02.d.ts +83 -0
- package/dist/nips/nip-02.js +123 -0
- package/dist/nips/nip-04.d.ts +36 -0
- package/dist/nips/nip-04.js +105 -0
- package/dist/nips/nip-05.d.ts +86 -0
- package/dist/nips/nip-05.js +151 -0
- package/dist/nips/nip-09.d.ts +92 -0
- package/dist/nips/nip-09.js +190 -0
- package/dist/nips/nip-11.d.ts +64 -0
- package/dist/nips/nip-11.js +154 -0
- package/dist/nips/nip-13.d.ts +73 -0
- package/dist/nips/nip-13.js +128 -0
- package/dist/nips/nip-15.d.ts +83 -0
- package/dist/nips/nip-15.js +101 -0
- package/dist/nips/nip-16.d.ts +88 -0
- package/dist/nips/nip-16.js +150 -0
- package/dist/nips/nip-19.d.ts +28 -0
- package/dist/nips/nip-19.js +103 -0
- package/dist/nips/nip-20.d.ts +59 -0
- package/dist/nips/nip-20.js +95 -0
- package/dist/nips/nip-22.d.ts +89 -0
- package/dist/nips/nip-22.js +142 -0
- package/dist/nips/nip-26.d.ts +52 -0
- package/dist/nips/nip-26.js +139 -0
- package/dist/nips/nip-28.d.ts +103 -0
- package/dist/nips/nip-28.js +170 -0
- package/dist/nips/nip-33.d.ts +94 -0
- package/dist/nips/nip-33.js +133 -0
- package/dist/nostr-server.d.ts +23 -0
- package/dist/nostr-server.js +44 -0
- package/dist/server.d.ts +13 -3
- package/dist/server.js +60 -33
- package/dist/transport/base.d.ts +54 -0
- package/dist/transport/base.js +104 -0
- package/dist/transport/websocket.d.ts +22 -0
- package/dist/transport/websocket.js +122 -0
- package/dist/types/events.d.ts +63 -0
- package/dist/types/events.js +5 -0
- package/dist/types/filters.d.ts +19 -0
- package/dist/types/filters.js +5 -0
- package/dist/types/handlers.d.ts +80 -0
- package/dist/types/handlers.js +5 -0
- package/dist/types/index.d.ts +118 -39
- package/dist/types/index.js +21 -1
- package/dist/types/logger.d.ts +40 -0
- package/dist/types/logger.js +5 -0
- package/dist/types/messages.d.ts +135 -0
- package/dist/types/messages.js +40 -0
- package/dist/types/nostr.d.ts +120 -39
- package/dist/types/nostr.js +5 -10
- package/dist/types/options.d.ts +154 -0
- package/dist/types/options.js +5 -0
- package/dist/types/relays.d.ts +26 -0
- package/dist/types/relays.js +5 -0
- package/dist/types/scoring.d.ts +47 -0
- package/dist/types/scoring.js +29 -0
- package/dist/types/socket.d.ts +99 -0
- package/dist/types/socket.js +5 -0
- package/dist/types/transport.d.ts +97 -0
- package/dist/types/transport.js +5 -0
- package/dist/types/validation.d.ts +50 -0
- package/dist/types/validation.js +5 -0
- package/dist/types/websocket.d.ts +172 -0
- package/dist/types/websocket.js +5 -0
- package/dist/utils/http.d.ts +10 -0
- package/dist/utils/http.js +24 -0
- package/dist/utils/logger.d.ts +11 -2
- package/dist/utils/logger.js +18 -13
- package/dist/utils/metrics.d.ts +81 -0
- package/dist/utils/metrics.js +206 -0
- package/dist/utils/rate-limiter.d.ts +85 -0
- package/dist/utils/rate-limiter.js +175 -0
- package/package.json +18 -21
package/dist/types/index.d.ts
CHANGED
|
@@ -1,58 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Core type definitions
|
|
3
|
+
* @module types
|
|
4
|
+
*/
|
|
1
5
|
import type { WebSocket } from 'ws';
|
|
2
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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
|
-
}
|
package/dist/types/index.js
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
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,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
|
+
};
|
package/dist/types/nostr.d.ts
CHANGED
|
@@ -1,52 +1,133 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
|
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
|
}
|
package/dist/types/nostr.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 {};
|