@panyam/servicekit-client 0.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/LICENSE +21 -0
- package/README.md +385 -0
- package/dist/base-client.d.ts +93 -0
- package/dist/base-client.d.ts.map +1 -0
- package/dist/base-client.js +204 -0
- package/dist/base-client.js.map +1 -0
- package/dist/grpcws-client.d.ts +112 -0
- package/dist/grpcws-client.d.ts.map +1 -0
- package/dist/grpcws-client.js +175 -0
- package/dist/grpcws-client.js.map +1 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +77 -0
- package/dist/index.js.map +1 -0
- package/dist/servicekit-client.browser.js +440 -0
- package/dist/typed-client.d.ts +96 -0
- package/dist/typed-client.d.ts.map +1 -0
- package/dist/typed-client.js +133 -0
- package/dist/typed-client.js.map +1 -0
- package/dist/types.d.ts +127 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +72 -0
- package/dist/types.js.map +1 -0
- package/package.json +41 -0
- package/src/base-client.ts +234 -0
- package/src/grpcws-client.ts +206 -0
- package/src/index.ts +81 -0
- package/src/typed-client.ts +150 -0
- package/src/types.ts +167 -0
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var ServiceKit = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
BaseWSClient: () => BaseWSClient,
|
|
25
|
+
BinaryCodec: () => BinaryCodec,
|
|
26
|
+
GRPCWSClient: () => GRPCWSClient,
|
|
27
|
+
JSONCodec: () => JSONCodec,
|
|
28
|
+
ReadyState: () => ReadyState,
|
|
29
|
+
TypedGRPCWSClient: () => TypedGRPCWSClient
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// src/types.ts
|
|
33
|
+
var ReadyState = {
|
|
34
|
+
CONNECTING: 0,
|
|
35
|
+
OPEN: 1,
|
|
36
|
+
CLOSING: 2,
|
|
37
|
+
CLOSED: 3
|
|
38
|
+
};
|
|
39
|
+
var JSONCodec = class {
|
|
40
|
+
decode(data) {
|
|
41
|
+
if (typeof data === "string") {
|
|
42
|
+
return JSON.parse(data);
|
|
43
|
+
}
|
|
44
|
+
const text = new TextDecoder().decode(data);
|
|
45
|
+
return JSON.parse(text);
|
|
46
|
+
}
|
|
47
|
+
encode(msg) {
|
|
48
|
+
return JSON.stringify(msg);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var BinaryCodec = class {
|
|
52
|
+
constructor(decodeFunc, encodeFunc) {
|
|
53
|
+
this.decodeFunc = decodeFunc;
|
|
54
|
+
this.encodeFunc = encodeFunc;
|
|
55
|
+
}
|
|
56
|
+
decode(data) {
|
|
57
|
+
if (typeof data === "string") {
|
|
58
|
+
throw new Error("BinaryCodec received text data, expected binary");
|
|
59
|
+
}
|
|
60
|
+
return this.decodeFunc(data);
|
|
61
|
+
}
|
|
62
|
+
encode(msg) {
|
|
63
|
+
const encoded = this.encodeFunc(msg);
|
|
64
|
+
return encoded.buffer.slice(encoded.byteOffset, encoded.byteOffset + encoded.byteLength);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/base-client.ts
|
|
69
|
+
var BaseWSClient = class {
|
|
70
|
+
constructor(options = {}) {
|
|
71
|
+
this.ws = null;
|
|
72
|
+
/** Called when a data message is received (decoded by codec) */
|
|
73
|
+
this.onMessage = () => {
|
|
74
|
+
};
|
|
75
|
+
/** Called when a ping is received (after auto-pong if enabled) */
|
|
76
|
+
this.onPing = () => {
|
|
77
|
+
};
|
|
78
|
+
/** Called when the connection closes */
|
|
79
|
+
this.onClose = () => {
|
|
80
|
+
};
|
|
81
|
+
/** Called when a WebSocket error occurs */
|
|
82
|
+
this.onError = () => {
|
|
83
|
+
};
|
|
84
|
+
this._autoPong = options.autoPong ?? true;
|
|
85
|
+
this._WebSocket = options.WebSocket ?? globalThis.WebSocket;
|
|
86
|
+
this._codec = options.codec ?? new JSONCodec();
|
|
87
|
+
}
|
|
88
|
+
/** Get the codec used for encoding/decoding data messages */
|
|
89
|
+
get codec() {
|
|
90
|
+
return this._codec;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Connect to a WebSocket server.
|
|
94
|
+
* @param url The WebSocket URL to connect to
|
|
95
|
+
* @returns Promise that resolves when connected
|
|
96
|
+
*/
|
|
97
|
+
connect(url) {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
if (this.ws && this.ws.readyState === ReadyState.OPEN) {
|
|
100
|
+
resolve();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
this.ws = new this._WebSocket(url);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
reject(error);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.ws.binaryType = "arraybuffer";
|
|
110
|
+
this.ws.onopen = () => {
|
|
111
|
+
resolve();
|
|
112
|
+
};
|
|
113
|
+
this.ws.onerror = (event) => {
|
|
114
|
+
const errorMsg = "WebSocket error";
|
|
115
|
+
this.onError(errorMsg);
|
|
116
|
+
reject(new Error(errorMsg));
|
|
117
|
+
};
|
|
118
|
+
this.ws.onclose = () => {
|
|
119
|
+
this.onClose();
|
|
120
|
+
};
|
|
121
|
+
this.ws.onmessage = (event) => {
|
|
122
|
+
this.handleRawMessage(event.data);
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Send a data message to the server using the configured codec.
|
|
128
|
+
* @param data The data to send (will be encoded by codec)
|
|
129
|
+
*/
|
|
130
|
+
send(data) {
|
|
131
|
+
if (!this.ws || this.ws.readyState !== ReadyState.OPEN) {
|
|
132
|
+
throw new Error("WebSocket is not connected");
|
|
133
|
+
}
|
|
134
|
+
const encoded = this._codec.encode(data);
|
|
135
|
+
this.ws.send(encoded);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Send a raw message to the server (bypasses codec).
|
|
139
|
+
* Useful for control messages like pong.
|
|
140
|
+
* @param message The raw string or ArrayBuffer to send
|
|
141
|
+
*/
|
|
142
|
+
sendRaw(message) {
|
|
143
|
+
if (!this.ws || this.ws.readyState !== ReadyState.OPEN) {
|
|
144
|
+
throw new Error("WebSocket is not connected");
|
|
145
|
+
}
|
|
146
|
+
this.ws.send(message);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Close the WebSocket connection.
|
|
150
|
+
*/
|
|
151
|
+
close() {
|
|
152
|
+
if (this.ws) {
|
|
153
|
+
this.ws.close();
|
|
154
|
+
this.ws = null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Check if the client is connected.
|
|
159
|
+
*/
|
|
160
|
+
get isConnected() {
|
|
161
|
+
return this.ws !== null && this.ws.readyState === ReadyState.OPEN;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get the WebSocket ready state.
|
|
165
|
+
*/
|
|
166
|
+
get readyState() {
|
|
167
|
+
return this.ws?.readyState ?? ReadyState.CLOSED;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Handle incoming raw message data.
|
|
171
|
+
* - Text frames: Check for control messages (ping), then decode with codec
|
|
172
|
+
* - Binary frames: Decode directly with codec
|
|
173
|
+
*
|
|
174
|
+
* Control messages (ping/pong/error) are always JSON text frames,
|
|
175
|
+
* regardless of what codec is used for data messages.
|
|
176
|
+
*/
|
|
177
|
+
handleRawMessage(data) {
|
|
178
|
+
if (data instanceof ArrayBuffer) {
|
|
179
|
+
try {
|
|
180
|
+
const decoded = this._codec.decode(data);
|
|
181
|
+
this.onMessage(decoded);
|
|
182
|
+
} catch (err) {
|
|
183
|
+
this.onError(`Failed to decode binary message: ${err}`);
|
|
184
|
+
}
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
let parsed;
|
|
188
|
+
try {
|
|
189
|
+
parsed = JSON.parse(data);
|
|
190
|
+
} catch {
|
|
191
|
+
try {
|
|
192
|
+
const decoded = this._codec.decode(data);
|
|
193
|
+
this.onMessage(decoded);
|
|
194
|
+
} catch (err) {
|
|
195
|
+
this.onError(`Failed to decode text message: ${err}`);
|
|
196
|
+
}
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (this.isPingMessage(parsed)) {
|
|
200
|
+
const pingId = parsed.pingId;
|
|
201
|
+
if (this._autoPong && pingId !== void 0) {
|
|
202
|
+
this.sendPong(pingId);
|
|
203
|
+
}
|
|
204
|
+
this.onPing(pingId ?? 0);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const decoded = this._codec.decode(data);
|
|
209
|
+
this.onMessage(decoded);
|
|
210
|
+
} catch (err) {
|
|
211
|
+
this.onError(`Failed to decode message: ${err}`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Check if a message is a ping message.
|
|
216
|
+
* Pings are always JSON with type: "ping".
|
|
217
|
+
*/
|
|
218
|
+
isPingMessage(msg) {
|
|
219
|
+
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "ping";
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Send a pong response.
|
|
223
|
+
* Pongs are always JSON, bypassing the codec.
|
|
224
|
+
*/
|
|
225
|
+
sendPong(pingId) {
|
|
226
|
+
this.sendRaw(JSON.stringify({ type: "pong", pingId }));
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// src/grpcws-client.ts
|
|
231
|
+
var GRPCWSClient = class {
|
|
232
|
+
constructor(options = {}) {
|
|
233
|
+
/** Called when a data message is received */
|
|
234
|
+
this.onMessage = () => {
|
|
235
|
+
};
|
|
236
|
+
/** Called when the stream ends normally */
|
|
237
|
+
this.onStreamEnd = () => {
|
|
238
|
+
};
|
|
239
|
+
/** Called when the server sends an error */
|
|
240
|
+
this.onError = () => {
|
|
241
|
+
};
|
|
242
|
+
/** Called when the connection closes */
|
|
243
|
+
this.onClose = () => {
|
|
244
|
+
};
|
|
245
|
+
/** Called when a ping is received */
|
|
246
|
+
this.onPing = () => {
|
|
247
|
+
};
|
|
248
|
+
this.base = new BaseWSClient(options);
|
|
249
|
+
this.setupBaseHandlers();
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Connect to a grpcws WebSocket server.
|
|
253
|
+
* @param url The WebSocket URL to connect to
|
|
254
|
+
* @returns Promise that resolves when connected
|
|
255
|
+
*/
|
|
256
|
+
connect(url) {
|
|
257
|
+
return this.base.connect(url);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Send data to the server.
|
|
261
|
+
* The data is wrapped in a {type: "data", data: ...} envelope.
|
|
262
|
+
* @param data The data payload to send
|
|
263
|
+
*/
|
|
264
|
+
send(data) {
|
|
265
|
+
const envelope = {
|
|
266
|
+
type: "data",
|
|
267
|
+
data
|
|
268
|
+
};
|
|
269
|
+
this.base.send(envelope);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Signal that the client is done sending (half-close).
|
|
273
|
+
* Used in client streaming and bidirectional streaming to indicate
|
|
274
|
+
* the client won't send any more messages.
|
|
275
|
+
*/
|
|
276
|
+
endSend() {
|
|
277
|
+
this.base.send({ type: "end_send" });
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Cancel the stream.
|
|
281
|
+
* Signals to the server that the client wants to terminate the stream.
|
|
282
|
+
*/
|
|
283
|
+
cancel() {
|
|
284
|
+
this.base.send({ type: "cancel" });
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Close the WebSocket connection.
|
|
288
|
+
*/
|
|
289
|
+
close() {
|
|
290
|
+
this.base.close();
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Check if the client is connected.
|
|
294
|
+
*/
|
|
295
|
+
get isConnected() {
|
|
296
|
+
return this.base.isConnected;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Get the WebSocket ready state.
|
|
300
|
+
*/
|
|
301
|
+
get readyState() {
|
|
302
|
+
return this.base.readyState;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Set up handlers on the base client to process grpcws envelope messages.
|
|
306
|
+
*/
|
|
307
|
+
setupBaseHandlers() {
|
|
308
|
+
this.base.onMessage = (msg) => {
|
|
309
|
+
this.handleEnvelopeMessage(msg);
|
|
310
|
+
};
|
|
311
|
+
this.base.onClose = () => {
|
|
312
|
+
this.onClose();
|
|
313
|
+
};
|
|
314
|
+
this.base.onError = (error) => {
|
|
315
|
+
this.onError(error);
|
|
316
|
+
};
|
|
317
|
+
this.base.onPing = (pingId) => {
|
|
318
|
+
this.onPing(pingId);
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Handle an incoming envelope message.
|
|
323
|
+
*/
|
|
324
|
+
handleEnvelopeMessage(msg) {
|
|
325
|
+
if (!this.isControlMessage(msg)) {
|
|
326
|
+
console.warn("Received non-envelope message:", msg);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const controlMsg = msg;
|
|
330
|
+
switch (controlMsg.type) {
|
|
331
|
+
case "data":
|
|
332
|
+
this.onMessage(controlMsg.data);
|
|
333
|
+
break;
|
|
334
|
+
case "stream_end":
|
|
335
|
+
this.onStreamEnd();
|
|
336
|
+
break;
|
|
337
|
+
case "error":
|
|
338
|
+
this.onError(controlMsg.error ?? "Unknown error");
|
|
339
|
+
break;
|
|
340
|
+
default:
|
|
341
|
+
console.warn("Unknown message type:", controlMsg.type);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Check if a message is a valid control message.
|
|
346
|
+
*/
|
|
347
|
+
isControlMessage(msg) {
|
|
348
|
+
return typeof msg === "object" && msg !== null && "type" in msg && typeof msg.type === "string";
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// src/typed-client.ts
|
|
353
|
+
var TypedGRPCWSClient = class {
|
|
354
|
+
constructor(options = {}) {
|
|
355
|
+
/** Called when a typed message is received */
|
|
356
|
+
this.onMessage = () => {
|
|
357
|
+
};
|
|
358
|
+
/** Called when the stream ends normally */
|
|
359
|
+
this.onStreamEnd = () => {
|
|
360
|
+
};
|
|
361
|
+
/** Called when the server sends an error */
|
|
362
|
+
this.onError = () => {
|
|
363
|
+
};
|
|
364
|
+
/** Called when the connection closes */
|
|
365
|
+
this.onClose = () => {
|
|
366
|
+
};
|
|
367
|
+
/** Called when a ping is received */
|
|
368
|
+
this.onPing = () => {
|
|
369
|
+
};
|
|
370
|
+
this.client = new GRPCWSClient(options);
|
|
371
|
+
this.setupHandlers();
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Connect to a grpcws WebSocket server.
|
|
375
|
+
* @param url The WebSocket URL to connect to
|
|
376
|
+
* @returns Promise that resolves when connected
|
|
377
|
+
*/
|
|
378
|
+
connect(url) {
|
|
379
|
+
return this.client.connect(url);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Send a typed message to the server.
|
|
383
|
+
* @param data The typed data payload to send
|
|
384
|
+
*/
|
|
385
|
+
send(data) {
|
|
386
|
+
this.client.send(data);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Signal that the client is done sending (half-close).
|
|
390
|
+
*/
|
|
391
|
+
endSend() {
|
|
392
|
+
this.client.endSend();
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Cancel the stream.
|
|
396
|
+
*/
|
|
397
|
+
cancel() {
|
|
398
|
+
this.client.cancel();
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Close the WebSocket connection.
|
|
402
|
+
*/
|
|
403
|
+
close() {
|
|
404
|
+
this.client.close();
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Check if the client is connected.
|
|
408
|
+
*/
|
|
409
|
+
get isConnected() {
|
|
410
|
+
return this.client.isConnected;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Get the WebSocket ready state.
|
|
414
|
+
*/
|
|
415
|
+
get readyState() {
|
|
416
|
+
return this.client.readyState;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Set up handlers to forward events with proper typing.
|
|
420
|
+
*/
|
|
421
|
+
setupHandlers() {
|
|
422
|
+
this.client.onMessage = (data) => {
|
|
423
|
+
this.onMessage(data);
|
|
424
|
+
};
|
|
425
|
+
this.client.onStreamEnd = () => {
|
|
426
|
+
this.onStreamEnd();
|
|
427
|
+
};
|
|
428
|
+
this.client.onError = (error) => {
|
|
429
|
+
this.onError(error);
|
|
430
|
+
};
|
|
431
|
+
this.client.onClose = () => {
|
|
432
|
+
this.onClose();
|
|
433
|
+
};
|
|
434
|
+
this.client.onPing = (pingId) => {
|
|
435
|
+
this.onPing(pingId);
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
return __toCommonJS(index_exports);
|
|
440
|
+
})();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypedGRPCWSClient - Type-safe wrapper for GRPCWSClient.
|
|
3
|
+
*
|
|
4
|
+
* This wrapper provides compile-time type safety for protobuf messages.
|
|
5
|
+
* Use with types generated by any TypeScript protoc plugin:
|
|
6
|
+
* - @bufbuild/protobuf
|
|
7
|
+
* - protobuf-ts
|
|
8
|
+
* - ts-proto
|
|
9
|
+
* - etc.
|
|
10
|
+
*/
|
|
11
|
+
import { ClientOptions, VoidHandler, ErrorHandler } from './types';
|
|
12
|
+
/**
|
|
13
|
+
* Type-safe gRPC-WebSocket client with generic input/output types.
|
|
14
|
+
*
|
|
15
|
+
* This wrapper enforces type safety at compile time by parameterizing
|
|
16
|
+
* the send and receive message types. Use with your protobuf-generated
|
|
17
|
+
* TypeScript types.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam TIn - The type of messages sent to the server
|
|
20
|
+
* @typeParam TOut - The type of messages received from the server
|
|
21
|
+
*
|
|
22
|
+
* @example With @bufbuild/protobuf types
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { PlayerAction, GameState } from './gen/game_pb';
|
|
25
|
+
*
|
|
26
|
+
* const client = new TypedGRPCWSClient<PlayerAction, GameState>();
|
|
27
|
+
* client.onMessage = (state: GameState) => {
|
|
28
|
+
* console.log('Players:', state.players);
|
|
29
|
+
* };
|
|
30
|
+
* await client.connect('ws://localhost:8080/ws/v1/sync');
|
|
31
|
+
* client.send({ actionId: '1', move: { x: 10, y: 20 } });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example With ts-proto types
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { GameCommand, CommandSummary } from './gen/game';
|
|
37
|
+
*
|
|
38
|
+
* const client = new TypedGRPCWSClient<GameCommand, CommandSummary>();
|
|
39
|
+
* client.onMessage = (summary: CommandSummary) => {
|
|
40
|
+
* console.log(`Executed ${summary.commandsExecuted} commands`);
|
|
41
|
+
* };
|
|
42
|
+
* await client.connect('ws://localhost:8080/ws/v1/commands');
|
|
43
|
+
* client.send({ commandId: '1', commandType: 'move' });
|
|
44
|
+
* client.endSend();
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare class TypedGRPCWSClient<TIn, TOut> {
|
|
48
|
+
private client;
|
|
49
|
+
/** Called when a typed message is received */
|
|
50
|
+
onMessage: (data: TOut) => void;
|
|
51
|
+
/** Called when the stream ends normally */
|
|
52
|
+
onStreamEnd: VoidHandler;
|
|
53
|
+
/** Called when the server sends an error */
|
|
54
|
+
onError: ErrorHandler;
|
|
55
|
+
/** Called when the connection closes */
|
|
56
|
+
onClose: VoidHandler;
|
|
57
|
+
/** Called when a ping is received */
|
|
58
|
+
onPing: (pingId: number) => void;
|
|
59
|
+
constructor(options?: ClientOptions);
|
|
60
|
+
/**
|
|
61
|
+
* Connect to a grpcws WebSocket server.
|
|
62
|
+
* @param url The WebSocket URL to connect to
|
|
63
|
+
* @returns Promise that resolves when connected
|
|
64
|
+
*/
|
|
65
|
+
connect(url: string): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Send a typed message to the server.
|
|
68
|
+
* @param data The typed data payload to send
|
|
69
|
+
*/
|
|
70
|
+
send(data: TIn): void;
|
|
71
|
+
/**
|
|
72
|
+
* Signal that the client is done sending (half-close).
|
|
73
|
+
*/
|
|
74
|
+
endSend(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Cancel the stream.
|
|
77
|
+
*/
|
|
78
|
+
cancel(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Close the WebSocket connection.
|
|
81
|
+
*/
|
|
82
|
+
close(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Check if the client is connected.
|
|
85
|
+
*/
|
|
86
|
+
get isConnected(): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Get the WebSocket ready state.
|
|
89
|
+
*/
|
|
90
|
+
get readyState(): number;
|
|
91
|
+
/**
|
|
92
|
+
* Set up handlers to forward events with proper typing.
|
|
93
|
+
*/
|
|
94
|
+
private setupHandlers;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=typed-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-client.d.ts","sourceRoot":"","sources":["../src/typed-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,iBAAiB,CAAC,GAAG,EAAE,IAAI;IACtC,OAAO,CAAC,MAAM,CAAe;IAE7B,8CAA8C;IACvC,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAY;IAElD,2CAA2C;IACpC,WAAW,EAAE,WAAW,CAAY;IAE3C,4CAA4C;IACrC,OAAO,EAAE,YAAY,CAAY;IAExC,wCAAwC;IACjC,OAAO,EAAE,WAAW,CAAY;IAEvC,qCAAqC;IAC9B,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAY;gBAEvC,OAAO,GAAE,aAAkB;IAKvC;;;;OAIG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInC;;;OAGG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI;IAIrB;;OAEG;IACH,OAAO,IAAI,IAAI;IAIf;;OAEG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;OAEG;IACH,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;OAEG;IACH,OAAO,CAAC,aAAa;CAsBtB"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TypedGRPCWSClient - Type-safe wrapper for GRPCWSClient.
|
|
4
|
+
*
|
|
5
|
+
* This wrapper provides compile-time type safety for protobuf messages.
|
|
6
|
+
* Use with types generated by any TypeScript protoc plugin:
|
|
7
|
+
* - @bufbuild/protobuf
|
|
8
|
+
* - protobuf-ts
|
|
9
|
+
* - ts-proto
|
|
10
|
+
* - etc.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.TypedGRPCWSClient = void 0;
|
|
14
|
+
const grpcws_client_1 = require("./grpcws-client");
|
|
15
|
+
/**
|
|
16
|
+
* Type-safe gRPC-WebSocket client with generic input/output types.
|
|
17
|
+
*
|
|
18
|
+
* This wrapper enforces type safety at compile time by parameterizing
|
|
19
|
+
* the send and receive message types. Use with your protobuf-generated
|
|
20
|
+
* TypeScript types.
|
|
21
|
+
*
|
|
22
|
+
* @typeParam TIn - The type of messages sent to the server
|
|
23
|
+
* @typeParam TOut - The type of messages received from the server
|
|
24
|
+
*
|
|
25
|
+
* @example With @bufbuild/protobuf types
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { PlayerAction, GameState } from './gen/game_pb';
|
|
28
|
+
*
|
|
29
|
+
* const client = new TypedGRPCWSClient<PlayerAction, GameState>();
|
|
30
|
+
* client.onMessage = (state: GameState) => {
|
|
31
|
+
* console.log('Players:', state.players);
|
|
32
|
+
* };
|
|
33
|
+
* await client.connect('ws://localhost:8080/ws/v1/sync');
|
|
34
|
+
* client.send({ actionId: '1', move: { x: 10, y: 20 } });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example With ts-proto types
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import { GameCommand, CommandSummary } from './gen/game';
|
|
40
|
+
*
|
|
41
|
+
* const client = new TypedGRPCWSClient<GameCommand, CommandSummary>();
|
|
42
|
+
* client.onMessage = (summary: CommandSummary) => {
|
|
43
|
+
* console.log(`Executed ${summary.commandsExecuted} commands`);
|
|
44
|
+
* };
|
|
45
|
+
* await client.connect('ws://localhost:8080/ws/v1/commands');
|
|
46
|
+
* client.send({ commandId: '1', commandType: 'move' });
|
|
47
|
+
* client.endSend();
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
class TypedGRPCWSClient {
|
|
51
|
+
constructor(options = {}) {
|
|
52
|
+
/** Called when a typed message is received */
|
|
53
|
+
this.onMessage = () => { };
|
|
54
|
+
/** Called when the stream ends normally */
|
|
55
|
+
this.onStreamEnd = () => { };
|
|
56
|
+
/** Called when the server sends an error */
|
|
57
|
+
this.onError = () => { };
|
|
58
|
+
/** Called when the connection closes */
|
|
59
|
+
this.onClose = () => { };
|
|
60
|
+
/** Called when a ping is received */
|
|
61
|
+
this.onPing = () => { };
|
|
62
|
+
this.client = new grpcws_client_1.GRPCWSClient(options);
|
|
63
|
+
this.setupHandlers();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Connect to a grpcws WebSocket server.
|
|
67
|
+
* @param url The WebSocket URL to connect to
|
|
68
|
+
* @returns Promise that resolves when connected
|
|
69
|
+
*/
|
|
70
|
+
connect(url) {
|
|
71
|
+
return this.client.connect(url);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Send a typed message to the server.
|
|
75
|
+
* @param data The typed data payload to send
|
|
76
|
+
*/
|
|
77
|
+
send(data) {
|
|
78
|
+
this.client.send(data);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Signal that the client is done sending (half-close).
|
|
82
|
+
*/
|
|
83
|
+
endSend() {
|
|
84
|
+
this.client.endSend();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Cancel the stream.
|
|
88
|
+
*/
|
|
89
|
+
cancel() {
|
|
90
|
+
this.client.cancel();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Close the WebSocket connection.
|
|
94
|
+
*/
|
|
95
|
+
close() {
|
|
96
|
+
this.client.close();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Check if the client is connected.
|
|
100
|
+
*/
|
|
101
|
+
get isConnected() {
|
|
102
|
+
return this.client.isConnected;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get the WebSocket ready state.
|
|
106
|
+
*/
|
|
107
|
+
get readyState() {
|
|
108
|
+
return this.client.readyState;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Set up handlers to forward events with proper typing.
|
|
112
|
+
*/
|
|
113
|
+
setupHandlers() {
|
|
114
|
+
this.client.onMessage = (data) => {
|
|
115
|
+
// Cast to TOut - the protobuf JSON matches the TypeScript type
|
|
116
|
+
this.onMessage(data);
|
|
117
|
+
};
|
|
118
|
+
this.client.onStreamEnd = () => {
|
|
119
|
+
this.onStreamEnd();
|
|
120
|
+
};
|
|
121
|
+
this.client.onError = (error) => {
|
|
122
|
+
this.onError(error);
|
|
123
|
+
};
|
|
124
|
+
this.client.onClose = () => {
|
|
125
|
+
this.onClose();
|
|
126
|
+
};
|
|
127
|
+
this.client.onPing = (pingId) => {
|
|
128
|
+
this.onPing(pingId);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.TypedGRPCWSClient = TypedGRPCWSClient;
|
|
133
|
+
//# sourceMappingURL=typed-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-client.js","sourceRoot":"","sources":["../src/typed-client.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEH,mDAA+C;AAG/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAa,iBAAiB;IAkB5B,YAAY,UAAyB,EAAE;QAfvC,8CAA8C;QACvC,cAAS,GAAyB,GAAG,EAAE,GAAE,CAAC,CAAC;QAElD,2CAA2C;QACpC,gBAAW,GAAgB,GAAG,EAAE,GAAE,CAAC,CAAC;QAE3C,4CAA4C;QACrC,YAAO,GAAiB,GAAG,EAAE,GAAE,CAAC,CAAC;QAExC,wCAAwC;QACjC,YAAO,GAAgB,GAAG,EAAE,GAAE,CAAC,CAAC;QAEvC,qCAAqC;QAC9B,WAAM,GAA6B,GAAG,EAAE,GAAE,CAAC,CAAC;QAGjD,IAAI,CAAC,MAAM,GAAG,IAAI,4BAAY,CAAC,OAAO,CAAC,CAAC;QACxC,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,GAAW;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,IAAS;QACZ,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;IACjC,CAAC;IAED;;OAEG;IACH,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;IAChC,CAAC;IAED;;OAEG;IACK,aAAa;QACnB,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC,IAAa,EAAE,EAAE;YACxC,+DAA+D;YAC/D,IAAI,CAAC,SAAS,CAAC,IAAY,CAAC,CAAC;QAC/B,CAAC,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,WAAW,GAAG,GAAG,EAAE;YAC7B,IAAI,CAAC,WAAW,EAAE,CAAC;QACrB,CAAC,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,OAAO,GAAG,CAAC,KAAa,EAAE,EAAE;YACtC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,OAAO,GAAG,GAAG,EAAE;YACzB,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,CAAC,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,MAAc,EAAE,EAAE;YACtC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtB,CAAC,CAAC;IACJ,CAAC;CACF;AApGD,8CAoGC"}
|