reverb-ws-client 1.0.2 → 1.0.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.
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +39 -8
- package/dist/index.mjs +39 -8
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -8,6 +8,7 @@ type ReverbClientOptions = {
|
|
|
8
8
|
port?: number;
|
|
9
9
|
scheme?: ReverbScheme;
|
|
10
10
|
authorizer?: Authorizer;
|
|
11
|
+
debug?: boolean;
|
|
11
12
|
};
|
|
12
13
|
type PusherOutgoingMessage = {
|
|
13
14
|
event: string;
|
|
@@ -28,6 +29,7 @@ declare class Channel {
|
|
|
28
29
|
private client;
|
|
29
30
|
private listeners;
|
|
30
31
|
private subscribed;
|
|
32
|
+
private logger;
|
|
31
33
|
constructor(name: string, client: ReverbClient);
|
|
32
34
|
subscribe(): Promise<void>;
|
|
33
35
|
listen(event: string, callback: EventCallback): this;
|
|
@@ -49,6 +51,7 @@ declare class ReverbClient {
|
|
|
49
51
|
presence(name: string): Channel;
|
|
50
52
|
send(data: PusherOutgoingMessage | object): void;
|
|
51
53
|
getSocketId(): string | null;
|
|
54
|
+
getOptions(): ReverbClientOptions;
|
|
52
55
|
authorize(socketId: string, channelName: string): Promise<AuthResponse>;
|
|
53
56
|
private getChannel;
|
|
54
57
|
private handleMessage;
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ type ReverbClientOptions = {
|
|
|
8
8
|
port?: number;
|
|
9
9
|
scheme?: ReverbScheme;
|
|
10
10
|
authorizer?: Authorizer;
|
|
11
|
+
debug?: boolean;
|
|
11
12
|
};
|
|
12
13
|
type PusherOutgoingMessage = {
|
|
13
14
|
event: string;
|
|
@@ -28,6 +29,7 @@ declare class Channel {
|
|
|
28
29
|
private client;
|
|
29
30
|
private listeners;
|
|
30
31
|
private subscribed;
|
|
32
|
+
private logger;
|
|
31
33
|
constructor(name: string, client: ReverbClient);
|
|
32
34
|
subscribe(): Promise<void>;
|
|
33
35
|
listen(event: string, callback: EventCallback): this;
|
|
@@ -49,6 +51,7 @@ declare class ReverbClient {
|
|
|
49
51
|
presence(name: string): Channel;
|
|
50
52
|
send(data: PusherOutgoingMessage | object): void;
|
|
51
53
|
getSocketId(): string | null;
|
|
54
|
+
getOptions(): ReverbClientOptions;
|
|
52
55
|
authorize(socketId: string, channelName: string): Promise<AuthResponse>;
|
|
53
56
|
private getChannel;
|
|
54
57
|
private handleMessage;
|
package/dist/index.js
CHANGED
|
@@ -24,16 +24,40 @@ __export(index_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
26
|
|
|
27
|
+
// src/logger.ts
|
|
28
|
+
var Logger = class {
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
options;
|
|
33
|
+
log(...args) {
|
|
34
|
+
if (!this.options.debug) return;
|
|
35
|
+
console.log(...args);
|
|
36
|
+
}
|
|
37
|
+
warn(...args) {
|
|
38
|
+
if (!this.options.debug) return;
|
|
39
|
+
console.warn(...args);
|
|
40
|
+
}
|
|
41
|
+
error(...args) {
|
|
42
|
+
if (!this.options.debug) return;
|
|
43
|
+
console.error(...args);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
27
47
|
// src/Channel.ts
|
|
28
48
|
var Channel = class {
|
|
29
49
|
constructor(name, client) {
|
|
30
50
|
this.name = name;
|
|
31
51
|
this.client = client;
|
|
52
|
+
this.logger = new Logger({
|
|
53
|
+
debug: this.client.getOptions().debug ?? false
|
|
54
|
+
});
|
|
32
55
|
}
|
|
33
56
|
name;
|
|
34
57
|
client;
|
|
35
58
|
listeners = /* @__PURE__ */ new Map();
|
|
36
59
|
subscribed = false;
|
|
60
|
+
logger;
|
|
37
61
|
async subscribe() {
|
|
38
62
|
if (this.subscribed) return;
|
|
39
63
|
const isPrivate = this.name.startsWith("private-");
|
|
@@ -65,7 +89,7 @@ var Channel = class {
|
|
|
65
89
|
}
|
|
66
90
|
this.listeners.get(event).add(callback);
|
|
67
91
|
this.subscribe().catch((err) => {
|
|
68
|
-
|
|
92
|
+
this.logger.error("[Reverb] subscribe failed", err);
|
|
69
93
|
});
|
|
70
94
|
return this;
|
|
71
95
|
}
|
|
@@ -101,6 +125,9 @@ var ConnectionManager = class {
|
|
|
101
125
|
constructor(options, onMessage) {
|
|
102
126
|
this.options = options;
|
|
103
127
|
this.onMessage = onMessage;
|
|
128
|
+
this.logger = new Logger({
|
|
129
|
+
debug: options.debug ?? false
|
|
130
|
+
});
|
|
104
131
|
}
|
|
105
132
|
options;
|
|
106
133
|
onMessage;
|
|
@@ -110,6 +137,7 @@ var ConnectionManager = class {
|
|
|
110
137
|
maxReconnectAttempts = 5;
|
|
111
138
|
reconnectDelay = 2e3;
|
|
112
139
|
manuallyDisconnected = false;
|
|
140
|
+
logger;
|
|
113
141
|
connect() {
|
|
114
142
|
if (this.state === "connecting" || this.state === "connected") return;
|
|
115
143
|
this.manuallyDisconnected = false;
|
|
@@ -119,23 +147,23 @@ var ConnectionManager = class {
|
|
|
119
147
|
this.ws.onopen = () => {
|
|
120
148
|
this.state = "connected";
|
|
121
149
|
this.reconnectAttempts = 0;
|
|
122
|
-
|
|
150
|
+
this.logger.log("[Reverb] Connected");
|
|
123
151
|
};
|
|
124
152
|
this.ws.onmessage = (event) => {
|
|
125
153
|
try {
|
|
126
154
|
const parsed = JSON.parse(event.data);
|
|
127
155
|
this.onMessage(parsed);
|
|
128
156
|
} catch (err) {
|
|
129
|
-
|
|
157
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
130
158
|
}
|
|
131
159
|
};
|
|
132
160
|
this.ws.onerror = (err) => {
|
|
133
|
-
|
|
161
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
134
162
|
};
|
|
135
163
|
this.ws.onclose = () => {
|
|
136
164
|
this.ws = null;
|
|
137
165
|
this.state = "disconnected";
|
|
138
|
-
|
|
166
|
+
this.logger.log("[Reverb] Disconnected");
|
|
139
167
|
if (!this.manuallyDisconnected) {
|
|
140
168
|
this.reconnect();
|
|
141
169
|
}
|
|
@@ -151,7 +179,7 @@ var ConnectionManager = class {
|
|
|
151
179
|
}
|
|
152
180
|
send(data) {
|
|
153
181
|
if (!this.ws || this.state !== "connected") {
|
|
154
|
-
|
|
182
|
+
this.logger.warn("[Reverb] Cannot send, socket not connected");
|
|
155
183
|
return;
|
|
156
184
|
}
|
|
157
185
|
this.ws.send(JSON.stringify(data));
|
|
@@ -166,13 +194,13 @@ var ConnectionManager = class {
|
|
|
166
194
|
}
|
|
167
195
|
reconnect() {
|
|
168
196
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
169
|
-
|
|
197
|
+
this.logger.warn("[Reverb] Max reconnect attempts reached");
|
|
170
198
|
return;
|
|
171
199
|
}
|
|
172
200
|
this.state = "reconnecting";
|
|
173
201
|
this.reconnectAttempts++;
|
|
174
202
|
setTimeout(() => {
|
|
175
|
-
|
|
203
|
+
this.logger.log("[Reverb] Reconnecting...");
|
|
176
204
|
this.connect();
|
|
177
205
|
}, this.reconnectDelay);
|
|
178
206
|
}
|
|
@@ -212,6 +240,9 @@ var ReverbClient = class {
|
|
|
212
240
|
getSocketId() {
|
|
213
241
|
return this.socketId;
|
|
214
242
|
}
|
|
243
|
+
getOptions() {
|
|
244
|
+
return this.options;
|
|
245
|
+
}
|
|
215
246
|
authorize(socketId, channelName) {
|
|
216
247
|
if (!this.options.authorizer) {
|
|
217
248
|
throw new Error("[Reverb] authorizer is required for private channels");
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
|
+
// src/logger.ts
|
|
2
|
+
var Logger = class {
|
|
3
|
+
constructor(options = {}) {
|
|
4
|
+
this.options = options;
|
|
5
|
+
}
|
|
6
|
+
options;
|
|
7
|
+
log(...args) {
|
|
8
|
+
if (!this.options.debug) return;
|
|
9
|
+
console.log(...args);
|
|
10
|
+
}
|
|
11
|
+
warn(...args) {
|
|
12
|
+
if (!this.options.debug) return;
|
|
13
|
+
console.warn(...args);
|
|
14
|
+
}
|
|
15
|
+
error(...args) {
|
|
16
|
+
if (!this.options.debug) return;
|
|
17
|
+
console.error(...args);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
1
21
|
// src/Channel.ts
|
|
2
22
|
var Channel = class {
|
|
3
23
|
constructor(name, client) {
|
|
4
24
|
this.name = name;
|
|
5
25
|
this.client = client;
|
|
26
|
+
this.logger = new Logger({
|
|
27
|
+
debug: this.client.getOptions().debug ?? false
|
|
28
|
+
});
|
|
6
29
|
}
|
|
7
30
|
name;
|
|
8
31
|
client;
|
|
9
32
|
listeners = /* @__PURE__ */ new Map();
|
|
10
33
|
subscribed = false;
|
|
34
|
+
logger;
|
|
11
35
|
async subscribe() {
|
|
12
36
|
if (this.subscribed) return;
|
|
13
37
|
const isPrivate = this.name.startsWith("private-");
|
|
@@ -39,7 +63,7 @@ var Channel = class {
|
|
|
39
63
|
}
|
|
40
64
|
this.listeners.get(event).add(callback);
|
|
41
65
|
this.subscribe().catch((err) => {
|
|
42
|
-
|
|
66
|
+
this.logger.error("[Reverb] subscribe failed", err);
|
|
43
67
|
});
|
|
44
68
|
return this;
|
|
45
69
|
}
|
|
@@ -75,6 +99,9 @@ var ConnectionManager = class {
|
|
|
75
99
|
constructor(options, onMessage) {
|
|
76
100
|
this.options = options;
|
|
77
101
|
this.onMessage = onMessage;
|
|
102
|
+
this.logger = new Logger({
|
|
103
|
+
debug: options.debug ?? false
|
|
104
|
+
});
|
|
78
105
|
}
|
|
79
106
|
options;
|
|
80
107
|
onMessage;
|
|
@@ -84,6 +111,7 @@ var ConnectionManager = class {
|
|
|
84
111
|
maxReconnectAttempts = 5;
|
|
85
112
|
reconnectDelay = 2e3;
|
|
86
113
|
manuallyDisconnected = false;
|
|
114
|
+
logger;
|
|
87
115
|
connect() {
|
|
88
116
|
if (this.state === "connecting" || this.state === "connected") return;
|
|
89
117
|
this.manuallyDisconnected = false;
|
|
@@ -93,23 +121,23 @@ var ConnectionManager = class {
|
|
|
93
121
|
this.ws.onopen = () => {
|
|
94
122
|
this.state = "connected";
|
|
95
123
|
this.reconnectAttempts = 0;
|
|
96
|
-
|
|
124
|
+
this.logger.log("[Reverb] Connected");
|
|
97
125
|
};
|
|
98
126
|
this.ws.onmessage = (event) => {
|
|
99
127
|
try {
|
|
100
128
|
const parsed = JSON.parse(event.data);
|
|
101
129
|
this.onMessage(parsed);
|
|
102
130
|
} catch (err) {
|
|
103
|
-
|
|
131
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
104
132
|
}
|
|
105
133
|
};
|
|
106
134
|
this.ws.onerror = (err) => {
|
|
107
|
-
|
|
135
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
108
136
|
};
|
|
109
137
|
this.ws.onclose = () => {
|
|
110
138
|
this.ws = null;
|
|
111
139
|
this.state = "disconnected";
|
|
112
|
-
|
|
140
|
+
this.logger.log("[Reverb] Disconnected");
|
|
113
141
|
if (!this.manuallyDisconnected) {
|
|
114
142
|
this.reconnect();
|
|
115
143
|
}
|
|
@@ -125,7 +153,7 @@ var ConnectionManager = class {
|
|
|
125
153
|
}
|
|
126
154
|
send(data) {
|
|
127
155
|
if (!this.ws || this.state !== "connected") {
|
|
128
|
-
|
|
156
|
+
this.logger.warn("[Reverb] Cannot send, socket not connected");
|
|
129
157
|
return;
|
|
130
158
|
}
|
|
131
159
|
this.ws.send(JSON.stringify(data));
|
|
@@ -140,13 +168,13 @@ var ConnectionManager = class {
|
|
|
140
168
|
}
|
|
141
169
|
reconnect() {
|
|
142
170
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
143
|
-
|
|
171
|
+
this.logger.warn("[Reverb] Max reconnect attempts reached");
|
|
144
172
|
return;
|
|
145
173
|
}
|
|
146
174
|
this.state = "reconnecting";
|
|
147
175
|
this.reconnectAttempts++;
|
|
148
176
|
setTimeout(() => {
|
|
149
|
-
|
|
177
|
+
this.logger.log("[Reverb] Reconnecting...");
|
|
150
178
|
this.connect();
|
|
151
179
|
}, this.reconnectDelay);
|
|
152
180
|
}
|
|
@@ -186,6 +214,9 @@ var ReverbClient = class {
|
|
|
186
214
|
getSocketId() {
|
|
187
215
|
return this.socketId;
|
|
188
216
|
}
|
|
217
|
+
getOptions() {
|
|
218
|
+
return this.options;
|
|
219
|
+
}
|
|
189
220
|
authorize(socketId, channelName) {
|
|
190
221
|
if (!this.options.authorizer) {
|
|
191
222
|
throw new Error("[Reverb] authorizer is required for private channels");
|