@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 @@
|
|
|
1
|
+
{"version":3,"file":"base-client.js","sourceRoot":"","sources":["../src/base-client.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAEH,mCASiB;AAEjB;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,YAAY;IAkBvB,YAAY,UAA+B,EAAE;QAjBrC,OAAE,GAAqB,IAAI,CAAC;QAKpC,gEAAgE;QACzD,cAAS,GAAsB,GAAG,EAAE,GAAE,CAAC,CAAC;QAE/C,kEAAkE;QAC3D,WAAM,GAA6B,GAAG,EAAE,GAAE,CAAC,CAAC;QAEnD,wCAAwC;QACjC,YAAO,GAAgB,GAAG,EAAE,GAAE,CAAC,CAAC;QAEvC,2CAA2C;QACpC,YAAO,GAAiB,GAAG,EAA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AAI,CAAC,CAAC,CAAC;YACzB,OAAO;QACT,CAAC;QAED,6CAA6C;QAC7C,uDAAuD;QACvD,2CAA2C;QAC3C,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACzC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,6BAA6B,GAAG,EAAE,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;IAED;;;OAGG;IACK,aAAa,CAAC,GAAY;QAChC,OAAO,CACL,OAAO,GAAG,KAAK,QAAQ;YACvB,GAAG,KAAK,IAAI;YACZ,MAAM,IAAI,GAAG;YACZ,GAAsB,CAAC,IAAI,KAAK,MAAM,CACxC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACK,QAAQ,CAAC,MAAc;QAC7B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IACzD,CAAC;CACF;AAnMD,oCAmMC"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GRPCWSClient - WebSocket client for gRPC-style streaming over WebSocket.
|
|
3
|
+
*
|
|
4
|
+
* This client handles the grpcws protocol envelope format:
|
|
5
|
+
* - Wraps outgoing data in {type: "data", data: ...}
|
|
6
|
+
* - Handles incoming stream_end, error, and data messages
|
|
7
|
+
* - Supports client streaming (end_send) and cancellation
|
|
8
|
+
*
|
|
9
|
+
* Uses BaseWSClient for connection and ping/pong handling.
|
|
10
|
+
*/
|
|
11
|
+
import { ClientOptions, MessageHandler, ErrorHandler, VoidHandler } from './types';
|
|
12
|
+
/**
|
|
13
|
+
* WebSocket client for gRPC-style streaming protocol.
|
|
14
|
+
*
|
|
15
|
+
* Implements the grpcws protocol with message envelope handling:
|
|
16
|
+
* - Server→Client: {type: "data", data: ...}, {type: "error", error: ...}, {type: "stream_end"}
|
|
17
|
+
* - Client→Server: {type: "data", data: ...}, {type: "end_send"}, {type: "cancel"}
|
|
18
|
+
*
|
|
19
|
+
* Supports all three gRPC streaming patterns:
|
|
20
|
+
* - **Server streaming**: connect, then receive multiple messages
|
|
21
|
+
* - **Client streaming**: send multiple messages, call endSend(), receive response
|
|
22
|
+
* - **Bidirectional**: send and receive messages concurrently
|
|
23
|
+
*
|
|
24
|
+
* @example Server Streaming
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const client = new GRPCWSClient();
|
|
27
|
+
* client.onMessage = (data) => console.log('Event:', data);
|
|
28
|
+
* client.onStreamEnd = () => console.log('Stream ended');
|
|
29
|
+
* await client.connect('ws://localhost:8080/ws/v1/subscribe?game_id=123');
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example Client Streaming
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const client = new GRPCWSClient();
|
|
35
|
+
* client.onMessage = (summary) => console.log('Result:', summary);
|
|
36
|
+
* await client.connect('ws://localhost:8080/ws/v1/commands');
|
|
37
|
+
* client.send({ commandId: '1', commandType: 'move' });
|
|
38
|
+
* client.send({ commandId: '2', commandType: 'attack' });
|
|
39
|
+
* client.endSend(); // Server responds after this
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example Bidirectional Streaming
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const client = new GRPCWSClient();
|
|
45
|
+
* client.onMessage = (state) => updateUI(state);
|
|
46
|
+
* await client.connect('ws://localhost:8080/ws/v1/sync');
|
|
47
|
+
* client.send({ actionId: '1', move: { x: 10, y: 20 } });
|
|
48
|
+
* // Receives responses concurrently
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare class GRPCWSClient {
|
|
52
|
+
private base;
|
|
53
|
+
/** Called when a data message is received */
|
|
54
|
+
onMessage: MessageHandler;
|
|
55
|
+
/** Called when the stream ends normally */
|
|
56
|
+
onStreamEnd: VoidHandler;
|
|
57
|
+
/** Called when the server sends an error */
|
|
58
|
+
onError: ErrorHandler;
|
|
59
|
+
/** Called when the connection closes */
|
|
60
|
+
onClose: VoidHandler;
|
|
61
|
+
/** Called when a ping is received */
|
|
62
|
+
onPing: (pingId: number) => void;
|
|
63
|
+
constructor(options?: ClientOptions);
|
|
64
|
+
/**
|
|
65
|
+
* Connect to a grpcws WebSocket server.
|
|
66
|
+
* @param url The WebSocket URL to connect to
|
|
67
|
+
* @returns Promise that resolves when connected
|
|
68
|
+
*/
|
|
69
|
+
connect(url: string): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Send data to the server.
|
|
72
|
+
* The data is wrapped in a {type: "data", data: ...} envelope.
|
|
73
|
+
* @param data The data payload to send
|
|
74
|
+
*/
|
|
75
|
+
send(data: unknown): void;
|
|
76
|
+
/**
|
|
77
|
+
* Signal that the client is done sending (half-close).
|
|
78
|
+
* Used in client streaming and bidirectional streaming to indicate
|
|
79
|
+
* the client won't send any more messages.
|
|
80
|
+
*/
|
|
81
|
+
endSend(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Cancel the stream.
|
|
84
|
+
* Signals to the server that the client wants to terminate the stream.
|
|
85
|
+
*/
|
|
86
|
+
cancel(): void;
|
|
87
|
+
/**
|
|
88
|
+
* Close the WebSocket connection.
|
|
89
|
+
*/
|
|
90
|
+
close(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Check if the client is connected.
|
|
93
|
+
*/
|
|
94
|
+
get isConnected(): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Get the WebSocket ready state.
|
|
97
|
+
*/
|
|
98
|
+
get readyState(): number;
|
|
99
|
+
/**
|
|
100
|
+
* Set up handlers on the base client to process grpcws envelope messages.
|
|
101
|
+
*/
|
|
102
|
+
private setupBaseHandlers;
|
|
103
|
+
/**
|
|
104
|
+
* Handle an incoming envelope message.
|
|
105
|
+
*/
|
|
106
|
+
private handleEnvelopeMessage;
|
|
107
|
+
/**
|
|
108
|
+
* Check if a message is a valid control message.
|
|
109
|
+
*/
|
|
110
|
+
private isControlMessage;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=grpcws-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpcws-client.d.ts","sourceRoot":"","sources":["../src/grpcws-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAEL,aAAa,EACb,cAAc,EACd,YAAY,EACZ,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,IAAI,CAAe;IAE3B,6CAA6C;IACtC,SAAS,EAAE,cAAc,CAAY;IAE5C,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;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAQzB;;;;OAIG;IACH,OAAO,IAAI,IAAI;IAIf;;;OAGG;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,iBAAiB;IAkBzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA4B7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;CAQzB"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* GRPCWSClient - WebSocket client for gRPC-style streaming over WebSocket.
|
|
4
|
+
*
|
|
5
|
+
* This client handles the grpcws protocol envelope format:
|
|
6
|
+
* - Wraps outgoing data in {type: "data", data: ...}
|
|
7
|
+
* - Handles incoming stream_end, error, and data messages
|
|
8
|
+
* - Supports client streaming (end_send) and cancellation
|
|
9
|
+
*
|
|
10
|
+
* Uses BaseWSClient for connection and ping/pong handling.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.GRPCWSClient = void 0;
|
|
14
|
+
const base_client_1 = require("./base-client");
|
|
15
|
+
/**
|
|
16
|
+
* WebSocket client for gRPC-style streaming protocol.
|
|
17
|
+
*
|
|
18
|
+
* Implements the grpcws protocol with message envelope handling:
|
|
19
|
+
* - Server→Client: {type: "data", data: ...}, {type: "error", error: ...}, {type: "stream_end"}
|
|
20
|
+
* - Client→Server: {type: "data", data: ...}, {type: "end_send"}, {type: "cancel"}
|
|
21
|
+
*
|
|
22
|
+
* Supports all three gRPC streaming patterns:
|
|
23
|
+
* - **Server streaming**: connect, then receive multiple messages
|
|
24
|
+
* - **Client streaming**: send multiple messages, call endSend(), receive response
|
|
25
|
+
* - **Bidirectional**: send and receive messages concurrently
|
|
26
|
+
*
|
|
27
|
+
* @example Server Streaming
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const client = new GRPCWSClient();
|
|
30
|
+
* client.onMessage = (data) => console.log('Event:', data);
|
|
31
|
+
* client.onStreamEnd = () => console.log('Stream ended');
|
|
32
|
+
* await client.connect('ws://localhost:8080/ws/v1/subscribe?game_id=123');
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example Client Streaming
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const client = new GRPCWSClient();
|
|
38
|
+
* client.onMessage = (summary) => console.log('Result:', summary);
|
|
39
|
+
* await client.connect('ws://localhost:8080/ws/v1/commands');
|
|
40
|
+
* client.send({ commandId: '1', commandType: 'move' });
|
|
41
|
+
* client.send({ commandId: '2', commandType: 'attack' });
|
|
42
|
+
* client.endSend(); // Server responds after this
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example Bidirectional Streaming
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const client = new GRPCWSClient();
|
|
48
|
+
* client.onMessage = (state) => updateUI(state);
|
|
49
|
+
* await client.connect('ws://localhost:8080/ws/v1/sync');
|
|
50
|
+
* client.send({ actionId: '1', move: { x: 10, y: 20 } });
|
|
51
|
+
* // Receives responses concurrently
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
class GRPCWSClient {
|
|
55
|
+
constructor(options = {}) {
|
|
56
|
+
/** Called when a data message is received */
|
|
57
|
+
this.onMessage = () => { };
|
|
58
|
+
/** Called when the stream ends normally */
|
|
59
|
+
this.onStreamEnd = () => { };
|
|
60
|
+
/** Called when the server sends an error */
|
|
61
|
+
this.onError = () => { };
|
|
62
|
+
/** Called when the connection closes */
|
|
63
|
+
this.onClose = () => { };
|
|
64
|
+
/** Called when a ping is received */
|
|
65
|
+
this.onPing = () => { };
|
|
66
|
+
this.base = new base_client_1.BaseWSClient(options);
|
|
67
|
+
this.setupBaseHandlers();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Connect to a grpcws WebSocket server.
|
|
71
|
+
* @param url The WebSocket URL to connect to
|
|
72
|
+
* @returns Promise that resolves when connected
|
|
73
|
+
*/
|
|
74
|
+
connect(url) {
|
|
75
|
+
return this.base.connect(url);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Send data to the server.
|
|
79
|
+
* The data is wrapped in a {type: "data", data: ...} envelope.
|
|
80
|
+
* @param data The data payload to send
|
|
81
|
+
*/
|
|
82
|
+
send(data) {
|
|
83
|
+
const envelope = {
|
|
84
|
+
type: 'data',
|
|
85
|
+
data,
|
|
86
|
+
};
|
|
87
|
+
this.base.send(envelope);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Signal that the client is done sending (half-close).
|
|
91
|
+
* Used in client streaming and bidirectional streaming to indicate
|
|
92
|
+
* the client won't send any more messages.
|
|
93
|
+
*/
|
|
94
|
+
endSend() {
|
|
95
|
+
this.base.send({ type: 'end_send' });
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Cancel the stream.
|
|
99
|
+
* Signals to the server that the client wants to terminate the stream.
|
|
100
|
+
*/
|
|
101
|
+
cancel() {
|
|
102
|
+
this.base.send({ type: 'cancel' });
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Close the WebSocket connection.
|
|
106
|
+
*/
|
|
107
|
+
close() {
|
|
108
|
+
this.base.close();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Check if the client is connected.
|
|
112
|
+
*/
|
|
113
|
+
get isConnected() {
|
|
114
|
+
return this.base.isConnected;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the WebSocket ready state.
|
|
118
|
+
*/
|
|
119
|
+
get readyState() {
|
|
120
|
+
return this.base.readyState;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Set up handlers on the base client to process grpcws envelope messages.
|
|
124
|
+
*/
|
|
125
|
+
setupBaseHandlers() {
|
|
126
|
+
this.base.onMessage = (msg) => {
|
|
127
|
+
this.handleEnvelopeMessage(msg);
|
|
128
|
+
};
|
|
129
|
+
this.base.onClose = () => {
|
|
130
|
+
this.onClose();
|
|
131
|
+
};
|
|
132
|
+
this.base.onError = (error) => {
|
|
133
|
+
this.onError(error);
|
|
134
|
+
};
|
|
135
|
+
this.base.onPing = (pingId) => {
|
|
136
|
+
this.onPing(pingId);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Handle an incoming envelope message.
|
|
141
|
+
*/
|
|
142
|
+
handleEnvelopeMessage(msg) {
|
|
143
|
+
if (!this.isControlMessage(msg)) {
|
|
144
|
+
// Not a valid control message, ignore or pass through
|
|
145
|
+
console.warn('Received non-envelope message:', msg);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const controlMsg = msg;
|
|
149
|
+
switch (controlMsg.type) {
|
|
150
|
+
case 'data':
|
|
151
|
+
this.onMessage(controlMsg.data);
|
|
152
|
+
break;
|
|
153
|
+
case 'stream_end':
|
|
154
|
+
this.onStreamEnd();
|
|
155
|
+
break;
|
|
156
|
+
case 'error':
|
|
157
|
+
this.onError(controlMsg.error ?? 'Unknown error');
|
|
158
|
+
break;
|
|
159
|
+
default:
|
|
160
|
+
// Unknown message type, ignore
|
|
161
|
+
console.warn('Unknown message type:', controlMsg.type);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Check if a message is a valid control message.
|
|
166
|
+
*/
|
|
167
|
+
isControlMessage(msg) {
|
|
168
|
+
return (typeof msg === 'object' &&
|
|
169
|
+
msg !== null &&
|
|
170
|
+
'type' in msg &&
|
|
171
|
+
typeof msg.type === 'string');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.GRPCWSClient = GRPCWSClient;
|
|
175
|
+
//# sourceMappingURL=grpcws-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpcws-client.js","sourceRoot":"","sources":["../src/grpcws-client.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEH,+CAA6C;AAS7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAa,YAAY;IAkBvB,YAAY,UAAyB,EAAE;QAfvC,6CAA6C;QACtC,cAAS,GAAmB,GAAG,EAAE,GAAE,CAAC,CAAC;QAE5C,2CAA2C;QACpC,gBAAW,GAAgB,GAAG,EAAE,GAAE,CAAC,CAAC;QAE3C,4CAA4C;QACrC,YAAO,GAAiB,GAAG,EAAE,GAAE,CAAC,C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C;CACF;AAlJD,oCAkJC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @panyam/servicekit-client
|
|
3
|
+
*
|
|
4
|
+
* TypeScript client library for ServiceKit WebSocket protocols.
|
|
5
|
+
*
|
|
6
|
+
* ## Architecture
|
|
7
|
+
*
|
|
8
|
+
* The client mirrors the server-side architecture with two layers:
|
|
9
|
+
* - **Transport layer**: Handles WebSocket connection, ping/pong heartbeats
|
|
10
|
+
* - **Codec layer**: Handles encoding/decoding of data messages
|
|
11
|
+
*
|
|
12
|
+
* Control messages (ping, pong, error) are always JSON at the transport layer,
|
|
13
|
+
* while data messages use the configured codec (JSON, binary protobuf, etc.).
|
|
14
|
+
*
|
|
15
|
+
* ## Clients
|
|
16
|
+
*
|
|
17
|
+
* - `BaseWSClient`: Low-level WebSocket with auto ping/pong (for http/JSONConn)
|
|
18
|
+
* - `GRPCWSClient`: gRPC-style streaming with envelope protocol (for grpcws)
|
|
19
|
+
* - `TypedGRPCWSClient`: Type-safe wrapper for GRPCWSClient
|
|
20
|
+
*
|
|
21
|
+
* ## Codecs
|
|
22
|
+
*
|
|
23
|
+
* - `JSONCodec`: Default, matches server-side JSONCodec
|
|
24
|
+
* - `BinaryCodec`: For binary protobuf, matches server-side BinaryProtoCodec
|
|
25
|
+
*
|
|
26
|
+
* @example Basic WebSocket with JSON (default)
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { BaseWSClient } from '@panyam/servicekit-client';
|
|
29
|
+
*
|
|
30
|
+
* const client = new BaseWSClient();
|
|
31
|
+
* client.onMessage = (data) => console.log('Received:', data);
|
|
32
|
+
* await client.connect('ws://localhost:8080/ws');
|
|
33
|
+
* client.send({ hello: 'world' });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example Binary Protobuf Protocol
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { BaseWSClient, BinaryCodec } from '@panyam/servicekit-client';
|
|
39
|
+
* import { MyMessage } from './gen/my_pb';
|
|
40
|
+
*
|
|
41
|
+
* const codec = new BinaryCodec<MyMessage, MyMessage>(
|
|
42
|
+
* (data) => MyMessage.decode(new Uint8Array(data)),
|
|
43
|
+
* (msg) => MyMessage.encode(msg).finish()
|
|
44
|
+
* );
|
|
45
|
+
* const client = new BaseWSClient({ codec });
|
|
46
|
+
* client.onMessage = (msg) => console.log('Received:', msg);
|
|
47
|
+
* await client.connect('ws://localhost:8080/ws');
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @example gRPC-WebSocket Streaming
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { GRPCWSClient } from '@panyam/servicekit-client';
|
|
53
|
+
*
|
|
54
|
+
* const client = new GRPCWSClient();
|
|
55
|
+
* client.onMessage = (data) => console.log('Event:', data);
|
|
56
|
+
* client.onStreamEnd = () => console.log('Done');
|
|
57
|
+
* await client.connect('ws://localhost:8080/ws/v1/subscribe');
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @packageDocumentation
|
|
61
|
+
*/
|
|
62
|
+
export { MessageType, ControlMessage, ReadyState, ReadyStateType, ClientOptions, Codec, JSONCodec, BinaryCodec, MessageHandler, ErrorHandler, VoidHandler, } from './types';
|
|
63
|
+
export { BaseWSClient } from './base-client';
|
|
64
|
+
export { GRPCWSClient } from './grpcws-client';
|
|
65
|
+
export { TypedGRPCWSClient } from './typed-client';
|
|
66
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAGH,OAAO,EACL,WAAW,EACX,cAAc,EACd,UAAU,EACV,cAAc,EACd,aAAa,EACb,KAAK,EACL,SAAS,EACT,WAAW,EACX,cAAc,EACd,YAAY,EACZ,WAAW,GACZ,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @panyam/servicekit-client
|
|
4
|
+
*
|
|
5
|
+
* TypeScript client library for ServiceKit WebSocket protocols.
|
|
6
|
+
*
|
|
7
|
+
* ## Architecture
|
|
8
|
+
*
|
|
9
|
+
* The client mirrors the server-side architecture with two layers:
|
|
10
|
+
* - **Transport layer**: Handles WebSocket connection, ping/pong heartbeats
|
|
11
|
+
* - **Codec layer**: Handles encoding/decoding of data messages
|
|
12
|
+
*
|
|
13
|
+
* Control messages (ping, pong, error) are always JSON at the transport layer,
|
|
14
|
+
* while data messages use the configured codec (JSON, binary protobuf, etc.).
|
|
15
|
+
*
|
|
16
|
+
* ## Clients
|
|
17
|
+
*
|
|
18
|
+
* - `BaseWSClient`: Low-level WebSocket with auto ping/pong (for http/JSONConn)
|
|
19
|
+
* - `GRPCWSClient`: gRPC-style streaming with envelope protocol (for grpcws)
|
|
20
|
+
* - `TypedGRPCWSClient`: Type-safe wrapper for GRPCWSClient
|
|
21
|
+
*
|
|
22
|
+
* ## Codecs
|
|
23
|
+
*
|
|
24
|
+
* - `JSONCodec`: Default, matches server-side JSONCodec
|
|
25
|
+
* - `BinaryCodec`: For binary protobuf, matches server-side BinaryProtoCodec
|
|
26
|
+
*
|
|
27
|
+
* @example Basic WebSocket with JSON (default)
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { BaseWSClient } from '@panyam/servicekit-client';
|
|
30
|
+
*
|
|
31
|
+
* const client = new BaseWSClient();
|
|
32
|
+
* client.onMessage = (data) => console.log('Received:', data);
|
|
33
|
+
* await client.connect('ws://localhost:8080/ws');
|
|
34
|
+
* client.send({ hello: 'world' });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example Binary Protobuf Protocol
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import { BaseWSClient, BinaryCodec } from '@panyam/servicekit-client';
|
|
40
|
+
* import { MyMessage } from './gen/my_pb';
|
|
41
|
+
*
|
|
42
|
+
* const codec = new BinaryCodec<MyMessage, MyMessage>(
|
|
43
|
+
* (data) => MyMessage.decode(new Uint8Array(data)),
|
|
44
|
+
* (msg) => MyMessage.encode(msg).finish()
|
|
45
|
+
* );
|
|
46
|
+
* const client = new BaseWSClient({ codec });
|
|
47
|
+
* client.onMessage = (msg) => console.log('Received:', msg);
|
|
48
|
+
* await client.connect('ws://localhost:8080/ws');
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @example gRPC-WebSocket Streaming
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { GRPCWSClient } from '@panyam/servicekit-client';
|
|
54
|
+
*
|
|
55
|
+
* const client = new GRPCWSClient();
|
|
56
|
+
* client.onMessage = (data) => console.log('Event:', data);
|
|
57
|
+
* client.onStreamEnd = () => console.log('Done');
|
|
58
|
+
* await client.connect('ws://localhost:8080/ws/v1/subscribe');
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @packageDocumentation
|
|
62
|
+
*/
|
|
63
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
|
+
exports.TypedGRPCWSClient = exports.GRPCWSClient = exports.BaseWSClient = exports.BinaryCodec = exports.JSONCodec = exports.ReadyState = void 0;
|
|
65
|
+
// Types and Codecs
|
|
66
|
+
var types_1 = require("./types");
|
|
67
|
+
Object.defineProperty(exports, "ReadyState", { enumerable: true, get: function () { return types_1.ReadyState; } });
|
|
68
|
+
Object.defineProperty(exports, "JSONCodec", { enumerable: true, get: function () { return types_1.JSONCodec; } });
|
|
69
|
+
Object.defineProperty(exports, "BinaryCodec", { enumerable: true, get: function () { return types_1.BinaryCodec; } });
|
|
70
|
+
// Clients
|
|
71
|
+
var base_client_1 = require("./base-client");
|
|
72
|
+
Object.defineProperty(exports, "BaseWSClient", { enumerable: true, get: function () { return base_client_1.BaseWSClient; } });
|
|
73
|
+
var grpcws_client_1 = require("./grpcws-client");
|
|
74
|
+
Object.defineProperty(exports, "GRPCWSClient", { enumerable: true, get: function () { return grpcws_client_1.GRPCWSClient; } });
|
|
75
|
+
var typed_client_1 = require("./typed-client");
|
|
76
|
+
Object.defineProperty(exports, "TypedGRPCWSClient", { enumerable: true, get: function () { return typed_client_1.TypedGRPCWSClient; } });
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;;;AAEH,mBAAmB;AACnB,iCAYiB;AATf,mGAAA,UAAU,OAAA;AAIV,kGAAA,SAAS,OAAA;AACT,oGAAA,WAAW,OAAA;AAMb,UAAU;AACV,6CAA6C;AAApC,2GAAA,YAAY,OAAA;AACrB,iDAA+C;AAAtC,6GAAA,YAAY,OAAA;AACrB,+CAAmD;AAA1C,iHAAA,iBAAiB,OAAA"}
|