@sygnl/talon 1.0.1

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.
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Message sent TO the edge
3
+ */
4
+ interface OutboundMessage {
5
+ /** Message type/event name */
6
+ type: string;
7
+ /** Message payload */
8
+ data: Record<string, unknown>;
9
+ /** Optional message ID for tracking */
10
+ id?: string;
11
+ /** Timestamp */
12
+ timestamp?: number;
13
+ }
14
+ /**
15
+ * Message received FROM the edge
16
+ */
17
+ interface InboundMessage {
18
+ /** Message type/event name */
19
+ type: string;
20
+ /** Message payload */
21
+ data: Record<string, unknown>;
22
+ /** Optional message ID */
23
+ id?: string;
24
+ /** Timestamp */
25
+ timestamp?: number;
26
+ }
27
+ /**
28
+ * Transport connection state
29
+ */
30
+ type TransportState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'error';
31
+ /**
32
+ * Transport interface - all transports must implement this
33
+ */
34
+ interface TalonTransport {
35
+ /**
36
+ * Current connection state
37
+ */
38
+ readonly state: TransportState;
39
+ /**
40
+ * Connect to the transport
41
+ */
42
+ connect(): Promise<void>;
43
+ /**
44
+ * Disconnect from the transport
45
+ */
46
+ disconnect(): void;
47
+ /**
48
+ * Send a message to the edge
49
+ * @param message - Message to send
50
+ */
51
+ send(message: OutboundMessage): Promise<void>;
52
+ /**
53
+ * Subscribe to messages from the edge
54
+ * @param handler - Function to call when message received
55
+ * @returns Unsubscribe function
56
+ */
57
+ onMessage(handler: (message: InboundMessage) => void): () => void;
58
+ /**
59
+ * Subscribe to connection state changes
60
+ * @param handler - Function to call when state changes
61
+ * @returns Unsubscribe function
62
+ */
63
+ onStateChange(handler: (state: TransportState) => void): () => void;
64
+ /**
65
+ * Subscribe to errors
66
+ * @param handler - Function to call when error occurs
67
+ * @returns Unsubscribe function
68
+ */
69
+ onError(handler: (error: Error) => void): () => void;
70
+ }
71
+
72
+ /**
73
+ * WebSocket transport options
74
+ */
75
+ interface WebSocketTransportOptions {
76
+ /** WebSocket URL */
77
+ url: string;
78
+ /** Auto-reconnect on disconnect */
79
+ autoReconnect?: boolean;
80
+ /** Reconnect delay in ms */
81
+ reconnectDelay?: number;
82
+ /** Max reconnect attempts (0 = infinite) */
83
+ maxReconnectAttempts?: number;
84
+ /** Connection timeout in ms */
85
+ connectionTimeout?: number;
86
+ /** Debug mode */
87
+ debug?: boolean;
88
+ }
89
+ /**
90
+ * WebSocket transport for bidirectional communication
91
+ *
92
+ * Features:
93
+ * - Real-time bidirectional communication
94
+ * - Auto-reconnect with exponential backoff
95
+ * - Connection state management
96
+ * - Message queuing during disconnection
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * const transport = new WebSocketTransport({
101
+ * url: 'wss://edge.sygnl.io',
102
+ * autoReconnect: true
103
+ * });
104
+ * ```
105
+ */
106
+ declare class WebSocketTransport implements TalonTransport {
107
+ private url;
108
+ private ws;
109
+ private _state;
110
+ private autoReconnect;
111
+ private reconnectDelay;
112
+ private maxReconnectAttempts;
113
+ private reconnectAttempts;
114
+ private reconnectTimeout;
115
+ private connectionTimeout;
116
+ private debug;
117
+ private messageHandlers;
118
+ private stateHandlers;
119
+ private errorHandlers;
120
+ private messageQueue;
121
+ constructor(options: WebSocketTransportOptions);
122
+ get state(): TransportState;
123
+ connect(): Promise<void>;
124
+ disconnect(): void;
125
+ send(message: OutboundMessage): Promise<void>;
126
+ onMessage(handler: (message: InboundMessage) => void): () => void;
127
+ onStateChange(handler: (state: TransportState) => void): () => void;
128
+ onError(handler: (error: Error) => void): () => void;
129
+ /**
130
+ * Handle incoming WebSocket message
131
+ */
132
+ private handleMessage;
133
+ /**
134
+ * Handle WebSocket disconnect
135
+ */
136
+ private handleDisconnect;
137
+ /**
138
+ * Schedule reconnection attempt with exponential backoff
139
+ */
140
+ private scheduleReconnect;
141
+ /**
142
+ * Flush queued messages
143
+ */
144
+ private flushMessageQueue;
145
+ /**
146
+ * Set connection state
147
+ */
148
+ private setState;
149
+ /**
150
+ * Emit error to handlers
151
+ */
152
+ private emitError;
153
+ /**
154
+ * Debug logging
155
+ */
156
+ private log;
157
+ }
158
+
159
+ /**
160
+ * Webhook transport options
161
+ */
162
+ interface WebhookTransportOptions {
163
+ /** Webhook URL */
164
+ url: string;
165
+ /** HTTP method */
166
+ method?: 'POST' | 'PUT';
167
+ /** Custom headers */
168
+ headers?: Record<string, string>;
169
+ /** Request timeout in ms */
170
+ timeout?: number;
171
+ /** Retry failed requests */
172
+ retry?: boolean;
173
+ /** Max retry attempts */
174
+ maxRetries?: number;
175
+ /** Retry delay in ms */
176
+ retryDelay?: number;
177
+ /** Debug mode */
178
+ debug?: boolean;
179
+ }
180
+ /**
181
+ * Webhook transport for fire-and-forget HTTP requests
182
+ *
183
+ * Features:
184
+ * - One-way communication (no responses)
185
+ * - HTTP POST/PUT requests
186
+ * - Optional retry logic
187
+ * - Custom headers support
188
+ *
189
+ * Use this for:
190
+ * - Server-side event delivery
191
+ * - Fire-and-forget scenarios
192
+ * - When WebSocket not available/needed
193
+ *
194
+ * @example
195
+ * ```typescript
196
+ * const transport = new WebhookTransport({
197
+ * url: 'https://edge.sygnl.io/webhook',
198
+ * method: 'POST',
199
+ * retry: true
200
+ * });
201
+ * ```
202
+ */
203
+ declare class WebhookTransport implements TalonTransport {
204
+ private url;
205
+ private method;
206
+ private headers;
207
+ private timeout;
208
+ private retry;
209
+ private maxRetries;
210
+ private retryDelay;
211
+ private debug;
212
+ private _state;
213
+ private messageHandlers;
214
+ private stateHandlers;
215
+ private errorHandlers;
216
+ constructor(options: WebhookTransportOptions);
217
+ get state(): TransportState;
218
+ connect(): Promise<void>;
219
+ disconnect(): void;
220
+ send(message: OutboundMessage): Promise<void>;
221
+ onMessage(handler: (message: InboundMessage) => void): () => void;
222
+ onStateChange(handler: (state: TransportState) => void): () => void;
223
+ onError(handler: (error: Error) => void): () => void;
224
+ /**
225
+ * Send HTTP request
226
+ */
227
+ private sendRequest;
228
+ /**
229
+ * Set connection state
230
+ */
231
+ private setState;
232
+ /**
233
+ * Emit error to handlers
234
+ */
235
+ private emitError;
236
+ /**
237
+ * Debug logging
238
+ */
239
+ private log;
240
+ }
241
+
242
+ export { WebSocketTransport, type WebSocketTransportOptions, WebhookTransport, type WebhookTransportOptions };
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Message sent TO the edge
3
+ */
4
+ interface OutboundMessage {
5
+ /** Message type/event name */
6
+ type: string;
7
+ /** Message payload */
8
+ data: Record<string, unknown>;
9
+ /** Optional message ID for tracking */
10
+ id?: string;
11
+ /** Timestamp */
12
+ timestamp?: number;
13
+ }
14
+ /**
15
+ * Message received FROM the edge
16
+ */
17
+ interface InboundMessage {
18
+ /** Message type/event name */
19
+ type: string;
20
+ /** Message payload */
21
+ data: Record<string, unknown>;
22
+ /** Optional message ID */
23
+ id?: string;
24
+ /** Timestamp */
25
+ timestamp?: number;
26
+ }
27
+ /**
28
+ * Transport connection state
29
+ */
30
+ type TransportState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'error';
31
+ /**
32
+ * Transport interface - all transports must implement this
33
+ */
34
+ interface TalonTransport {
35
+ /**
36
+ * Current connection state
37
+ */
38
+ readonly state: TransportState;
39
+ /**
40
+ * Connect to the transport
41
+ */
42
+ connect(): Promise<void>;
43
+ /**
44
+ * Disconnect from the transport
45
+ */
46
+ disconnect(): void;
47
+ /**
48
+ * Send a message to the edge
49
+ * @param message - Message to send
50
+ */
51
+ send(message: OutboundMessage): Promise<void>;
52
+ /**
53
+ * Subscribe to messages from the edge
54
+ * @param handler - Function to call when message received
55
+ * @returns Unsubscribe function
56
+ */
57
+ onMessage(handler: (message: InboundMessage) => void): () => void;
58
+ /**
59
+ * Subscribe to connection state changes
60
+ * @param handler - Function to call when state changes
61
+ * @returns Unsubscribe function
62
+ */
63
+ onStateChange(handler: (state: TransportState) => void): () => void;
64
+ /**
65
+ * Subscribe to errors
66
+ * @param handler - Function to call when error occurs
67
+ * @returns Unsubscribe function
68
+ */
69
+ onError(handler: (error: Error) => void): () => void;
70
+ }
71
+
72
+ /**
73
+ * WebSocket transport options
74
+ */
75
+ interface WebSocketTransportOptions {
76
+ /** WebSocket URL */
77
+ url: string;
78
+ /** Auto-reconnect on disconnect */
79
+ autoReconnect?: boolean;
80
+ /** Reconnect delay in ms */
81
+ reconnectDelay?: number;
82
+ /** Max reconnect attempts (0 = infinite) */
83
+ maxReconnectAttempts?: number;
84
+ /** Connection timeout in ms */
85
+ connectionTimeout?: number;
86
+ /** Debug mode */
87
+ debug?: boolean;
88
+ }
89
+ /**
90
+ * WebSocket transport for bidirectional communication
91
+ *
92
+ * Features:
93
+ * - Real-time bidirectional communication
94
+ * - Auto-reconnect with exponential backoff
95
+ * - Connection state management
96
+ * - Message queuing during disconnection
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * const transport = new WebSocketTransport({
101
+ * url: 'wss://edge.sygnl.io',
102
+ * autoReconnect: true
103
+ * });
104
+ * ```
105
+ */
106
+ declare class WebSocketTransport implements TalonTransport {
107
+ private url;
108
+ private ws;
109
+ private _state;
110
+ private autoReconnect;
111
+ private reconnectDelay;
112
+ private maxReconnectAttempts;
113
+ private reconnectAttempts;
114
+ private reconnectTimeout;
115
+ private connectionTimeout;
116
+ private debug;
117
+ private messageHandlers;
118
+ private stateHandlers;
119
+ private errorHandlers;
120
+ private messageQueue;
121
+ constructor(options: WebSocketTransportOptions);
122
+ get state(): TransportState;
123
+ connect(): Promise<void>;
124
+ disconnect(): void;
125
+ send(message: OutboundMessage): Promise<void>;
126
+ onMessage(handler: (message: InboundMessage) => void): () => void;
127
+ onStateChange(handler: (state: TransportState) => void): () => void;
128
+ onError(handler: (error: Error) => void): () => void;
129
+ /**
130
+ * Handle incoming WebSocket message
131
+ */
132
+ private handleMessage;
133
+ /**
134
+ * Handle WebSocket disconnect
135
+ */
136
+ private handleDisconnect;
137
+ /**
138
+ * Schedule reconnection attempt with exponential backoff
139
+ */
140
+ private scheduleReconnect;
141
+ /**
142
+ * Flush queued messages
143
+ */
144
+ private flushMessageQueue;
145
+ /**
146
+ * Set connection state
147
+ */
148
+ private setState;
149
+ /**
150
+ * Emit error to handlers
151
+ */
152
+ private emitError;
153
+ /**
154
+ * Debug logging
155
+ */
156
+ private log;
157
+ }
158
+
159
+ /**
160
+ * Webhook transport options
161
+ */
162
+ interface WebhookTransportOptions {
163
+ /** Webhook URL */
164
+ url: string;
165
+ /** HTTP method */
166
+ method?: 'POST' | 'PUT';
167
+ /** Custom headers */
168
+ headers?: Record<string, string>;
169
+ /** Request timeout in ms */
170
+ timeout?: number;
171
+ /** Retry failed requests */
172
+ retry?: boolean;
173
+ /** Max retry attempts */
174
+ maxRetries?: number;
175
+ /** Retry delay in ms */
176
+ retryDelay?: number;
177
+ /** Debug mode */
178
+ debug?: boolean;
179
+ }
180
+ /**
181
+ * Webhook transport for fire-and-forget HTTP requests
182
+ *
183
+ * Features:
184
+ * - One-way communication (no responses)
185
+ * - HTTP POST/PUT requests
186
+ * - Optional retry logic
187
+ * - Custom headers support
188
+ *
189
+ * Use this for:
190
+ * - Server-side event delivery
191
+ * - Fire-and-forget scenarios
192
+ * - When WebSocket not available/needed
193
+ *
194
+ * @example
195
+ * ```typescript
196
+ * const transport = new WebhookTransport({
197
+ * url: 'https://edge.sygnl.io/webhook',
198
+ * method: 'POST',
199
+ * retry: true
200
+ * });
201
+ * ```
202
+ */
203
+ declare class WebhookTransport implements TalonTransport {
204
+ private url;
205
+ private method;
206
+ private headers;
207
+ private timeout;
208
+ private retry;
209
+ private maxRetries;
210
+ private retryDelay;
211
+ private debug;
212
+ private _state;
213
+ private messageHandlers;
214
+ private stateHandlers;
215
+ private errorHandlers;
216
+ constructor(options: WebhookTransportOptions);
217
+ get state(): TransportState;
218
+ connect(): Promise<void>;
219
+ disconnect(): void;
220
+ send(message: OutboundMessage): Promise<void>;
221
+ onMessage(handler: (message: InboundMessage) => void): () => void;
222
+ onStateChange(handler: (state: TransportState) => void): () => void;
223
+ onError(handler: (error: Error) => void): () => void;
224
+ /**
225
+ * Send HTTP request
226
+ */
227
+ private sendRequest;
228
+ /**
229
+ * Set connection state
230
+ */
231
+ private setState;
232
+ /**
233
+ * Emit error to handlers
234
+ */
235
+ private emitError;
236
+ /**
237
+ * Debug logging
238
+ */
239
+ private log;
240
+ }
241
+
242
+ export { WebSocketTransport, type WebSocketTransportOptions, WebhookTransport, type WebhookTransportOptions };