@debros/network-ts-sdk 0.1.4

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,142 @@
1
+ import { HttpClient } from "../core/http";
2
+ import { WSClient, WSClientConfig } from "../core/ws";
3
+
4
+ export interface Message {
5
+ data: string;
6
+ topic: string;
7
+ timestamp?: number;
8
+ }
9
+
10
+ export type MessageHandler = (message: Message) => void;
11
+ export type ErrorHandler = (error: Error) => void;
12
+ export type CloseHandler = () => void;
13
+
14
+ export class PubSubClient {
15
+ private httpClient: HttpClient;
16
+ private wsConfig: Partial<WSClientConfig>;
17
+
18
+ constructor(httpClient: HttpClient, wsConfig: Partial<WSClientConfig> = {}) {
19
+ this.httpClient = httpClient;
20
+ this.wsConfig = wsConfig;
21
+ }
22
+
23
+ /**
24
+ * Publish a message to a topic.
25
+ */
26
+ async publish(topic: string, data: string | Uint8Array): Promise<void> {
27
+ const dataBase64 =
28
+ typeof data === "string" ? Buffer.from(data).toString("base64") : Buffer.from(data).toString("base64");
29
+
30
+ await this.httpClient.post("/v1/pubsub/publish", {
31
+ topic,
32
+ data_base64: dataBase64,
33
+ });
34
+ }
35
+
36
+ /**
37
+ * List active topics in the current namespace.
38
+ */
39
+ async topics(): Promise<string[]> {
40
+ const response = await this.httpClient.get<{ topics: string[] }>(
41
+ "/v1/pubsub/topics"
42
+ );
43
+ return response.topics || [];
44
+ }
45
+
46
+ /**
47
+ * Subscribe to a topic via WebSocket.
48
+ * Returns a subscription object with event handlers.
49
+ */
50
+ async subscribe(
51
+ topic: string,
52
+ handlers: {
53
+ onMessage?: MessageHandler;
54
+ onError?: ErrorHandler;
55
+ onClose?: CloseHandler;
56
+ } = {}
57
+ ): Promise<Subscription> {
58
+ const wsUrl = new URL(this.wsConfig.wsURL || "ws://localhost:6001");
59
+ wsUrl.pathname = "/v1/pubsub/ws";
60
+ wsUrl.searchParams.set("topic", topic);
61
+
62
+ const wsClient = new WSClient({
63
+ ...this.wsConfig,
64
+ wsURL: wsUrl.toString(),
65
+ authToken: this.httpClient.getToken(),
66
+ });
67
+
68
+ const subscription = new Subscription(wsClient, topic);
69
+
70
+ if (handlers.onMessage) {
71
+ subscription.onMessage(handlers.onMessage);
72
+ }
73
+ if (handlers.onError) {
74
+ subscription.onError(handlers.onError);
75
+ }
76
+ if (handlers.onClose) {
77
+ subscription.onClose(handlers.onClose);
78
+ }
79
+
80
+ await wsClient.connect();
81
+ return subscription;
82
+ }
83
+ }
84
+
85
+ export class Subscription {
86
+ private wsClient: WSClient;
87
+ private topic: string;
88
+ private messageHandlers: Set<MessageHandler> = new Set();
89
+ private errorHandlers: Set<ErrorHandler> = new Set();
90
+ private closeHandlers: Set<CloseHandler> = new Set();
91
+
92
+ constructor(wsClient: WSClient, topic: string) {
93
+ this.wsClient = wsClient;
94
+ this.topic = topic;
95
+
96
+ this.wsClient.onMessage((data) => {
97
+ try {
98
+ const message: Message = {
99
+ topic: this.topic,
100
+ data: data,
101
+ timestamp: Date.now(),
102
+ };
103
+ this.messageHandlers.forEach((handler) => handler(message));
104
+ } catch (error) {
105
+ this.errorHandlers.forEach((handler) =>
106
+ handler(error instanceof Error ? error : new Error(String(error)))
107
+ );
108
+ }
109
+ });
110
+
111
+ this.wsClient.onError((error) => {
112
+ this.errorHandlers.forEach((handler) => handler(error));
113
+ });
114
+
115
+ this.wsClient.onClose(() => {
116
+ this.closeHandlers.forEach((handler) => handler());
117
+ });
118
+ }
119
+
120
+ onMessage(handler: MessageHandler) {
121
+ this.messageHandlers.add(handler);
122
+ return () => this.messageHandlers.delete(handler);
123
+ }
124
+
125
+ onError(handler: ErrorHandler) {
126
+ this.errorHandlers.add(handler);
127
+ return () => this.errorHandlers.delete(handler);
128
+ }
129
+
130
+ onClose(handler: CloseHandler) {
131
+ this.closeHandlers.add(handler);
132
+ return () => this.closeHandlers.delete(handler);
133
+ }
134
+
135
+ close() {
136
+ this.wsClient.close();
137
+ }
138
+
139
+ isConnected(): boolean {
140
+ return this.wsClient.isConnected();
141
+ }
142
+ }