@panyam/servicekit-client 0.1.11 → 0.1.12
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/README.md +33 -4
- package/dist/base-client.d.ts +4 -4
- package/dist/base-client.d.ts.map +1 -1
- package/dist/base-client.js +8 -4
- package/dist/base-client.js.map +1 -1
- package/dist/grpcws-client.d.ts +19 -5
- package/dist/grpcws-client.d.ts.map +1 -1
- package/dist/grpcws-client.js +11 -5
- package/dist/grpcws-client.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +24 -14
- package/dist/mock.d.ts +5 -2
- package/dist/mock.d.ts.map +1 -1
- package/dist/mock.js +3 -3
- package/dist/mock.js.map +1 -1
- package/dist/servicekit-client.browser.js +24 -14
- package/dist/typed-client.d.ts +4 -3
- package/dist/typed-client.d.ts.map +1 -1
- package/dist/typed-client.js +3 -3
- package/dist/typed-client.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +14 -2
- package/src/base-client.ts +10 -6
- package/src/grpcws-client.ts +32 -8
- package/src/index.ts +3 -1
- package/src/mock.ts +8 -5
- package/src/typed-client.ts +6 -6
- package/src/types.ts +16 -0
package/README.md
CHANGED
|
@@ -8,6 +8,17 @@ TypeScript client library for [ServiceKit](https://github.com/panyam/servicekit)
|
|
|
8
8
|
npm install @panyam/servicekit-client
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
Bundlers and Node get the ES build (`dist/index.mjs`) through `import`, and `require` gets the
|
|
12
|
+
CommonJS build (`dist/index.js`). For a page with no bundler, the script build defines a
|
|
13
|
+
`ServiceKit` global:
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<script src="https://unpkg.com/@panyam/servicekit-client/dist/servicekit-client.browser.js"></script>
|
|
17
|
+
<script>
|
|
18
|
+
const client = new ServiceKit.GRPCWSClient();
|
|
19
|
+
</script>
|
|
20
|
+
```
|
|
21
|
+
|
|
11
22
|
## Architecture
|
|
12
23
|
|
|
13
24
|
The client mirrors the server-side architecture with clear separation of concerns:
|
|
@@ -63,8 +74,8 @@ client.onMessage = (data) => {
|
|
|
63
74
|
console.log('Received:', data);
|
|
64
75
|
};
|
|
65
76
|
|
|
66
|
-
client.onClose = () => {
|
|
67
|
-
console.log('Disconnected');
|
|
77
|
+
client.onClose = ({ code, reason }) => {
|
|
78
|
+
console.log('Disconnected', code, reason);
|
|
68
79
|
};
|
|
69
80
|
|
|
70
81
|
await client.connect('ws://localhost:8080/ws');
|
|
@@ -218,7 +229,7 @@ class BaseWSClient<I = unknown, O = unknown> {
|
|
|
218
229
|
// Events
|
|
219
230
|
onMessage: (data: I) => void
|
|
220
231
|
onPing: (pingId: number) => void
|
|
221
|
-
onClose: () => void
|
|
232
|
+
onClose: (info: CloseInfo) => void // { code, reason, wasClean }
|
|
222
233
|
onError: (error: string) => void
|
|
223
234
|
|
|
224
235
|
// State
|
|
@@ -282,7 +293,7 @@ class GRPCWSClient {
|
|
|
282
293
|
onMessage: (data: unknown) => void
|
|
283
294
|
onStreamEnd: () => void
|
|
284
295
|
onError: (error: string) => void
|
|
285
|
-
onClose: () => void
|
|
296
|
+
onClose: (info: StreamCloseInfo) => void // CloseInfo + streamEnded
|
|
286
297
|
onPing: (pingId: number) => void
|
|
287
298
|
|
|
288
299
|
// State
|
|
@@ -294,6 +305,20 @@ class GRPCWSClient {
|
|
|
294
305
|
}
|
|
295
306
|
```
|
|
296
307
|
|
|
308
|
+
A server with `CloseOnStreamEnd` set sends `stream_end` and then closes with
|
|
309
|
+
code 1000. `onClose` reports `streamEnded: true` in that case, so the app can
|
|
310
|
+
tell a normal end from a dropped connection:
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
client.onClose = ({ code, streamEnded }) => {
|
|
314
|
+
if (streamEnded && code === 1000) {
|
|
315
|
+
showSessionEnded();
|
|
316
|
+
} else {
|
|
317
|
+
showConnectionLost();
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
```
|
|
321
|
+
|
|
297
322
|
### TypedGRPCWSClient<TIn, TOut>
|
|
298
323
|
|
|
299
324
|
Type-safe wrapper for GRPCWSClient.
|
|
@@ -338,6 +363,10 @@ expect(controller.sentMessages[0]).toMatchObject({ action: { case: 'join' } });
|
|
|
338
363
|
// Simulate errors and close
|
|
339
364
|
controller.simulateError('connection failed');
|
|
340
365
|
controller.simulateClose(1006);
|
|
366
|
+
|
|
367
|
+
// Or a server-side end: stream_end, then a normal close
|
|
368
|
+
controller.simulateStreamEnd();
|
|
369
|
+
controller.simulateClose(1000, 'stream_end');
|
|
341
370
|
```
|
|
342
371
|
|
|
343
372
|
### Mock BaseWSClient
|
package/dist/base-client.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Use this directly for http/JSONConn servers, or as the base for GRPCWSClient.
|
|
8
8
|
*/
|
|
9
|
-
import { ClientOptions, Codec, MessageHandler, ErrorHandler,
|
|
9
|
+
import { ClientOptions, Codec, MessageHandler, ErrorHandler, CloseHandler } from './types';
|
|
10
10
|
/**
|
|
11
11
|
* Base WebSocket client with automatic ping/pong heartbeat handling.
|
|
12
12
|
*
|
|
@@ -20,7 +20,7 @@ import { ClientOptions, Codec, MessageHandler, ErrorHandler, VoidHandler } from
|
|
|
20
20
|
* ```typescript
|
|
21
21
|
* const client = new BaseWSClient();
|
|
22
22
|
* client.onMessage = (data) => console.log('Received:', data);
|
|
23
|
-
* client.onClose = () => console.log('Disconnected');
|
|
23
|
+
* client.onClose = ({ code, reason }) => console.log('Disconnected', code, reason);
|
|
24
24
|
* await client.connect('ws://localhost:8080/ws');
|
|
25
25
|
* client.send({ hello: 'world' });
|
|
26
26
|
* ```
|
|
@@ -34,8 +34,8 @@ export declare class BaseWSClient<I = unknown, O = unknown> {
|
|
|
34
34
|
onMessage: MessageHandler<I>;
|
|
35
35
|
/** Called when a ping is received (after auto-pong if enabled) */
|
|
36
36
|
onPing: (pingId: number) => void;
|
|
37
|
-
/** Called when the connection closes */
|
|
38
|
-
onClose:
|
|
37
|
+
/** Called when the connection closes, with the close code and reason */
|
|
38
|
+
onClose: CloseHandler;
|
|
39
39
|
/** Called when a WebSocket error occurs */
|
|
40
40
|
onError: ErrorHandler;
|
|
41
41
|
constructor(options?: ClientOptions<I, O>);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-client.d.ts","sourceRoot":"","sources":["../src/base-client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAEL,aAAa,EACb,KAAK,EAEL,cAAc,EACd,YAAY,EACZ,
|
|
1
|
+
{"version":3,"file":"base-client.d.ts","sourceRoot":"","sources":["../src/base-client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAEL,aAAa,EACb,KAAK,EAEL,cAAc,EACd,YAAY,EACZ,YAAY,EAEb,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,YAAY,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO;IAChD,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,MAAM,CAAc;IAC5B,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,UAAU,CAAmB;IAErC,gEAAgE;IACzD,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,CAAY;IAE/C,kEAAkE;IAC3D,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAY;IAEnD,wEAAwE;IACjE,OAAO,EAAE,YAAY,CAAY;IAExC,2CAA2C;IACpC,OAAO,EAAE,YAAY,CAAY;gBAE5B,OAAO,GAAE,aAAa,CAAC,CAAC,EAAE,CAAC,CAAM;IAM7C,6DAA6D;IAC7D,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAEvB;IAED;;;;OAIG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAyCnC;;;OAGG;IACH,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI;IAQnB;;;;OAIG;IACH,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI;IAO5C;;OAEG;IACH,KAAK,IAAI,IAAI;IAOb;;OAEG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;OAEG;IACH,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;;;;;;OAOG;IACH,OAAO,CAAC,gBAAgB;IAgDxB;;;OAGG;IACH,OAAO,CAAC,aAAa;IASrB;;;OAGG;IACH,OAAO,CAAC,QAAQ;CAGjB"}
|
package/dist/base-client.js
CHANGED
|
@@ -23,7 +23,7 @@ const types_1 = require("./types");
|
|
|
23
23
|
* ```typescript
|
|
24
24
|
* const client = new BaseWSClient();
|
|
25
25
|
* client.onMessage = (data) => console.log('Received:', data);
|
|
26
|
-
* client.onClose = () => console.log('Disconnected');
|
|
26
|
+
* client.onClose = ({ code, reason }) => console.log('Disconnected', code, reason);
|
|
27
27
|
* await client.connect('ws://localhost:8080/ws');
|
|
28
28
|
* client.send({ hello: 'world' });
|
|
29
29
|
* ```
|
|
@@ -35,7 +35,7 @@ class BaseWSClient {
|
|
|
35
35
|
this.onMessage = () => { };
|
|
36
36
|
/** Called when a ping is received (after auto-pong if enabled) */
|
|
37
37
|
this.onPing = () => { };
|
|
38
|
-
/** Called when the connection closes */
|
|
38
|
+
/** Called when the connection closes, with the close code and reason */
|
|
39
39
|
this.onClose = () => { };
|
|
40
40
|
/** Called when a WebSocket error occurs */
|
|
41
41
|
this.onError = () => { };
|
|
@@ -75,8 +75,12 @@ class BaseWSClient {
|
|
|
75
75
|
this.onError(errorMsg);
|
|
76
76
|
reject(new Error(errorMsg));
|
|
77
77
|
};
|
|
78
|
-
this.ws.onclose = () => {
|
|
79
|
-
this.onClose(
|
|
78
|
+
this.ws.onclose = (event) => {
|
|
79
|
+
this.onClose({
|
|
80
|
+
code: event.code,
|
|
81
|
+
reason: event.reason,
|
|
82
|
+
wasClean: event.wasClean,
|
|
83
|
+
});
|
|
80
84
|
};
|
|
81
85
|
this.ws.onmessage = (event) => {
|
|
82
86
|
this.handleRawMessage(event.data);
|
package/dist/base-client.js.map
CHANGED
|
@@ -1 +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,
|
|
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,wEAAwE;QACjE,YAAO,GAAiB,GAAG,EAAE,GAAE,CAAC,CAAC;QAExC,2CAA2C;QACpC,YAAO,GAAiB,GAAG,EAAE,GAAE,CAAC,CAAC;QAGtC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC;QAC1C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,I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AI,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;AAvMD,oCAuMC"}
|
package/dist/grpcws-client.d.ts
CHANGED
|
@@ -8,7 +8,18 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Uses BaseWSClient for connection and ping/pong handling.
|
|
10
10
|
*/
|
|
11
|
-
import { ClientOptions, MessageHandler, ErrorHandler, VoidHandler } from './types';
|
|
11
|
+
import { ClientOptions, MessageHandler, ErrorHandler, VoidHandler, CloseInfo, CloseHandler } from './types';
|
|
12
|
+
/**
|
|
13
|
+
* Close details for a grpcws stream.
|
|
14
|
+
*
|
|
15
|
+
* `streamEnded` is true when the server sent stream_end before the close.
|
|
16
|
+
* A server with `CloseOnStreamEnd` set closes with code 1000 right after
|
|
17
|
+
* stream_end, so `streamEnded && code === 1000` is a normal end of the RPC.
|
|
18
|
+
* Anything else means the connection dropped or was closed mid-stream.
|
|
19
|
+
*/
|
|
20
|
+
export interface StreamCloseInfo extends CloseInfo {
|
|
21
|
+
streamEnded: boolean;
|
|
22
|
+
}
|
|
12
23
|
/**
|
|
13
24
|
* Mock controller for testing GRPCWSClient without real WebSocket connections.
|
|
14
25
|
*
|
|
@@ -34,8 +45,10 @@ export interface MockController {
|
|
|
34
45
|
simulateMessage(data: unknown): void;
|
|
35
46
|
/** Simulate a server error (delivers {type: "error"} envelope to onError) */
|
|
36
47
|
simulateError(message?: string): void;
|
|
37
|
-
/** Simulate
|
|
38
|
-
|
|
48
|
+
/** Simulate the server ending the stream (delivers a {type: "stream_end"} envelope) */
|
|
49
|
+
simulateStreamEnd(): void;
|
|
50
|
+
/** Simulate WebSocket close. `code` defaults to 1000. */
|
|
51
|
+
simulateClose(code?: number, reason?: string): void;
|
|
39
52
|
/** Current WebSocket ready state */
|
|
40
53
|
readonly readyState: number;
|
|
41
54
|
}
|
|
@@ -80,14 +93,15 @@ export interface MockController {
|
|
|
80
93
|
*/
|
|
81
94
|
export declare class GRPCWSClient {
|
|
82
95
|
private base;
|
|
96
|
+
private streamEnded;
|
|
83
97
|
/** Called when a data message is received */
|
|
84
98
|
onMessage: MessageHandler;
|
|
85
99
|
/** Called when the stream ends normally */
|
|
86
100
|
onStreamEnd: VoidHandler;
|
|
87
101
|
/** Called when the server sends an error */
|
|
88
102
|
onError: ErrorHandler;
|
|
89
|
-
/** Called when the connection closes */
|
|
90
|
-
onClose:
|
|
103
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
104
|
+
onClose: CloseHandler<StreamCloseInfo>;
|
|
91
105
|
/** Called when a ping is received */
|
|
92
106
|
onPing: (pingId: number) => void;
|
|
93
107
|
constructor(options?: ClientOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpcws-client.d.ts","sourceRoot":"","sources":["../src/grpcws-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAEL,aAAa,EACb,cAAc,EACd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AAE,MAAM,KAAK,IAAI,CAAY;gBAEvC,OAAO,GAAE,aAAkB;IAKvC;;;;OAIG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKnC;;;;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;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CAAC,UAAU,IAAI;QAAE,MAAM,EAAE,YAAY,CAAC;QAAC,UAAU,EAAE,cAAc,CAAA;KAAE;IAkDzE;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAkBzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA6B7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;CAQzB"}
|
package/dist/grpcws-client.js
CHANGED
|
@@ -54,13 +54,14 @@ const mock_1 = require("./mock");
|
|
|
54
54
|
*/
|
|
55
55
|
class GRPCWSClient {
|
|
56
56
|
constructor(options = {}) {
|
|
57
|
+
this.streamEnded = false;
|
|
57
58
|
/** Called when a data message is received */
|
|
58
59
|
this.onMessage = () => { };
|
|
59
60
|
/** Called when the stream ends normally */
|
|
60
61
|
this.onStreamEnd = () => { };
|
|
61
62
|
/** Called when the server sends an error */
|
|
62
63
|
this.onError = () => { };
|
|
63
|
-
/** Called when the connection closes */
|
|
64
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
64
65
|
this.onClose = () => { };
|
|
65
66
|
/** Called when a ping is received */
|
|
66
67
|
this.onPing = () => { };
|
|
@@ -73,6 +74,7 @@ class GRPCWSClient {
|
|
|
73
74
|
* @returns Promise that resolves when connected
|
|
74
75
|
*/
|
|
75
76
|
connect(url) {
|
|
77
|
+
this.streamEnded = false;
|
|
76
78
|
return this.base.connect(url);
|
|
77
79
|
}
|
|
78
80
|
/**
|
|
@@ -170,8 +172,11 @@ class GRPCWSClient {
|
|
|
170
172
|
simulateError(message) {
|
|
171
173
|
wsCtrl.simulateRawMessage(JSON.stringify({ type: 'error', error: message ?? 'Mock error' }));
|
|
172
174
|
},
|
|
173
|
-
|
|
174
|
-
wsCtrl.
|
|
175
|
+
simulateStreamEnd() {
|
|
176
|
+
wsCtrl.simulateRawMessage(JSON.stringify({ type: 'stream_end' }));
|
|
177
|
+
},
|
|
178
|
+
simulateClose(code, reason) {
|
|
179
|
+
wsCtrl.simulateClose(code, reason);
|
|
175
180
|
},
|
|
176
181
|
get readyState() {
|
|
177
182
|
return wsCtrl.readyState;
|
|
@@ -186,8 +191,8 @@ class GRPCWSClient {
|
|
|
186
191
|
this.base.onMessage = (msg) => {
|
|
187
192
|
this.handleEnvelopeMessage(msg);
|
|
188
193
|
};
|
|
189
|
-
this.base.onClose = () => {
|
|
190
|
-
this.onClose();
|
|
194
|
+
this.base.onClose = (info) => {
|
|
195
|
+
this.onClose({ ...info, streamEnded: this.streamEnded });
|
|
191
196
|
};
|
|
192
197
|
this.base.onError = (error) => {
|
|
193
198
|
this.onError(error);
|
|
@@ -211,6 +216,7 @@ class GRPCWSClient {
|
|
|
211
216
|
this.onMessage(controlMsg.data);
|
|
212
217
|
break;
|
|
213
218
|
case 'stream_end':
|
|
219
|
+
this.streamEnded = true;
|
|
214
220
|
this.onStreamEnd();
|
|
215
221
|
break;
|
|
216
222
|
case 'error':
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpcws-client.js","sourceRoot":"","sources":["../src/grpcws-client.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEH,+CAA6C;AAC7C,iCAA0C;
|
|
1
|
+
{"version":3,"file":"grpcws-client.js","sourceRoot":"","sources":["../src/grpcws-client.ts"],"names":[],"mappings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OAAO,CAAC,IAAI,CAAC,gCAAgC,EAAE,GAAG,CAAC,CAAC;YACpD,OAAO;QACT,CAAC;QAED,MAAM,UAAU,GAAG,GAAqB,CAAC;QAEzC,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;YACxB,KAAK,MAAM;gBACT,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;gBAChC,MAAM;YAER,KAAK,YAAY;gBACf,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;gBACxB,IAAI,CAAC,WAAW,EAAE,CAAC;gBACnB,MAAM;YAER,KAAK,OAAO;gBACV,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,IAAI,eAAe,CAAC,CAAC;gBAClD,MAAM;YAER;gBACE,+BAA+B;gBAC/B,OAAO,CAAC,IAAI,CAAC,uBAAuB,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC;QAC3D,CAAC;IACH,CAAC;IAED;;OAEG;IACK,gBAAgB,CAAC,GAAY;QACnC,OAAO,CACL,OAAO,GAAG,KAAK,QAAQ;YACvB,GAAG,KAAK,IAAI;YACZ,MAAM,IAAI,GAAG;YACb,OAAQ,GAAsB,CAAC,IAAI,KAAK,QAAQ,CACjD,CAAC;IACJ,CAAC;CACF;AA7ND,oCA6NC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
*
|
|
65
65
|
* @packageDocumentation
|
|
66
66
|
*/
|
|
67
|
-
export { MessageType, ControlMessage, ReadyState, ReadyStateType, ClientOptions, Codec, JSONCodec, BinaryCodec, MessageHandler, ErrorHandler, VoidHandler, } from './types';
|
|
67
|
+
export { MessageType, ControlMessage, ReadyState, ReadyStateType, ClientOptions, Codec, JSONCodec, BinaryCodec, MessageHandler, ErrorHandler, VoidHandler, CloseInfo, CloseHandler, } from './types';
|
|
68
68
|
export { BaseWSClient } from './base-client';
|
|
69
|
-
export { GRPCWSClient, MockController } from './grpcws-client';
|
|
69
|
+
export { GRPCWSClient, MockController, StreamCloseInfo } from './grpcws-client';
|
|
70
70
|
export { TypedGRPCWSClient } from './typed-client';
|
|
71
71
|
export { SSEClient, SSEClientOptions } from './sse-client';
|
|
72
72
|
export { StreamableClient, StreamableClientOptions } from './streamable-client';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAGH,OAAO,EACL,WAAW,EACX,cAAc,EACd,UAAU,EACV,cAAc,EACd,aAAa,EACb,KAAK,EACL,SAAS,EACT,WAAW,EACX,cAAc,EACd,YAAY,EACZ,WAAW,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAGH,OAAO,EACL,WAAW,EACX,cAAc,EACd,UAAU,EACV,cAAc,EACd,aAAa,EACb,KAAK,EACL,SAAS,EACT,WAAW,EACX,cAAc,EACd,YAAY,EACZ,WAAW,EACX,SAAS,EACT,YAAY,GACb,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAGnD,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAGhF,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,UAAU,GACX,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;;;AAEH,mBAAmB;AACnB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;;;AAEH,mBAAmB;AACnB,iCAciB;AAXf,mGAAA,UAAU,OAAA;AAIV,kGAAA,SAAS,OAAA;AACT,oGAAA,WAAW,OAAA;AAQb,UAAU;AACV,6CAA6C;AAApC,2GAAA,YAAY,OAAA;AACrB,iDAAgF;AAAvE,6GAAA,YAAY,OAAA;AACrB,+CAAmD;AAA1C,iHAAA,iBAAiB,OAAA;AAE1B,cAAc;AACd,2CAA2D;AAAlD,uGAAA,SAAS,OAAA;AAClB,yDAAgF;AAAvE,qHAAA,gBAAgB,OAAA;AAEzB,qDAAqD;AACrD,2CAMsB;AALpB,0GAAA,YAAY,OAAA;AAIZ,wGAAA,UAAU,OAAA;AAGZ,iBAAiB;AACjB,+BAA4D;AAAnD,wGAAA,gBAAgB,OAAA;AACzB,uCAAkE;AAAzD,6GAAA,iBAAiB,OAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -44,7 +44,7 @@ var BaseWSClient = class {
|
|
|
44
44
|
/** Called when a ping is received (after auto-pong if enabled) */
|
|
45
45
|
this.onPing = () => {
|
|
46
46
|
};
|
|
47
|
-
/** Called when the connection closes */
|
|
47
|
+
/** Called when the connection closes, with the close code and reason */
|
|
48
48
|
this.onClose = () => {
|
|
49
49
|
};
|
|
50
50
|
/** Called when a WebSocket error occurs */
|
|
@@ -84,8 +84,12 @@ var BaseWSClient = class {
|
|
|
84
84
|
this.onError(errorMsg);
|
|
85
85
|
reject(new Error(errorMsg));
|
|
86
86
|
};
|
|
87
|
-
this.ws.onclose = () => {
|
|
88
|
-
this.onClose(
|
|
87
|
+
this.ws.onclose = (event) => {
|
|
88
|
+
this.onClose({
|
|
89
|
+
code: event.code,
|
|
90
|
+
reason: event.reason,
|
|
91
|
+
wasClean: event.wasClean
|
|
92
|
+
});
|
|
89
93
|
};
|
|
90
94
|
this.ws.onmessage = (event) => {
|
|
91
95
|
this.handleRawMessage(event.data);
|
|
@@ -215,7 +219,7 @@ function createMockWSPair() {
|
|
|
215
219
|
}
|
|
216
220
|
close() {
|
|
217
221
|
this.readyState = ReadyState.CLOSED;
|
|
218
|
-
this.onclose?.({});
|
|
222
|
+
this.onclose?.({ code: 1e3, reason: "", wasClean: true });
|
|
219
223
|
}
|
|
220
224
|
get sentRaw() {
|
|
221
225
|
return this._sent;
|
|
@@ -241,10 +245,10 @@ function createMockWSPair() {
|
|
|
241
245
|
assertConnected("simulateWsError");
|
|
242
246
|
ws.onerror?.({});
|
|
243
247
|
},
|
|
244
|
-
simulateClose(code) {
|
|
248
|
+
simulateClose(code = 1e3, reason = "") {
|
|
245
249
|
assertConnected("simulateClose");
|
|
246
250
|
ws.readyState = ReadyState.CLOSED;
|
|
247
|
-
ws.onclose?.({});
|
|
251
|
+
ws.onclose?.({ code, reason, wasClean: code !== 1006 });
|
|
248
252
|
},
|
|
249
253
|
get readyState() {
|
|
250
254
|
return ws?.readyState ?? ReadyState.CLOSED;
|
|
@@ -259,6 +263,7 @@ function createMockWSPair() {
|
|
|
259
263
|
// src/grpcws-client.ts
|
|
260
264
|
var GRPCWSClient = class _GRPCWSClient {
|
|
261
265
|
constructor(options = {}) {
|
|
266
|
+
this.streamEnded = false;
|
|
262
267
|
/** Called when a data message is received */
|
|
263
268
|
this.onMessage = () => {
|
|
264
269
|
};
|
|
@@ -268,7 +273,7 @@ var GRPCWSClient = class _GRPCWSClient {
|
|
|
268
273
|
/** Called when the server sends an error */
|
|
269
274
|
this.onError = () => {
|
|
270
275
|
};
|
|
271
|
-
/** Called when the connection closes */
|
|
276
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
272
277
|
this.onClose = () => {
|
|
273
278
|
};
|
|
274
279
|
/** Called when a ping is received */
|
|
@@ -283,6 +288,7 @@ var GRPCWSClient = class _GRPCWSClient {
|
|
|
283
288
|
* @returns Promise that resolves when connected
|
|
284
289
|
*/
|
|
285
290
|
connect(url) {
|
|
291
|
+
this.streamEnded = false;
|
|
286
292
|
return this.base.connect(url);
|
|
287
293
|
}
|
|
288
294
|
/**
|
|
@@ -380,8 +386,11 @@ var GRPCWSClient = class _GRPCWSClient {
|
|
|
380
386
|
JSON.stringify({ type: "error", error: message ?? "Mock error" })
|
|
381
387
|
);
|
|
382
388
|
},
|
|
383
|
-
|
|
384
|
-
wsCtrl.
|
|
389
|
+
simulateStreamEnd() {
|
|
390
|
+
wsCtrl.simulateRawMessage(JSON.stringify({ type: "stream_end" }));
|
|
391
|
+
},
|
|
392
|
+
simulateClose(code, reason) {
|
|
393
|
+
wsCtrl.simulateClose(code, reason);
|
|
385
394
|
},
|
|
386
395
|
get readyState() {
|
|
387
396
|
return wsCtrl.readyState;
|
|
@@ -396,8 +405,8 @@ var GRPCWSClient = class _GRPCWSClient {
|
|
|
396
405
|
this.base.onMessage = (msg) => {
|
|
397
406
|
this.handleEnvelopeMessage(msg);
|
|
398
407
|
};
|
|
399
|
-
this.base.onClose = () => {
|
|
400
|
-
this.onClose();
|
|
408
|
+
this.base.onClose = (info) => {
|
|
409
|
+
this.onClose({ ...info, streamEnded: this.streamEnded });
|
|
401
410
|
};
|
|
402
411
|
this.base.onError = (error) => {
|
|
403
412
|
this.onError(error);
|
|
@@ -420,6 +429,7 @@ var GRPCWSClient = class _GRPCWSClient {
|
|
|
420
429
|
this.onMessage(controlMsg.data);
|
|
421
430
|
break;
|
|
422
431
|
case "stream_end":
|
|
432
|
+
this.streamEnded = true;
|
|
423
433
|
this.onStreamEnd();
|
|
424
434
|
break;
|
|
425
435
|
case "error":
|
|
@@ -449,7 +459,7 @@ var TypedGRPCWSClient = class {
|
|
|
449
459
|
/** Called when the server sends an error */
|
|
450
460
|
this.onError = () => {
|
|
451
461
|
};
|
|
452
|
-
/** Called when the connection closes */
|
|
462
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
453
463
|
this.onClose = () => {
|
|
454
464
|
};
|
|
455
465
|
/** Called when a ping is received */
|
|
@@ -516,8 +526,8 @@ var TypedGRPCWSClient = class {
|
|
|
516
526
|
this.client.onError = (error) => {
|
|
517
527
|
this.onError(error);
|
|
518
528
|
};
|
|
519
|
-
this.client.onClose = () => {
|
|
520
|
-
this.onClose();
|
|
529
|
+
this.client.onClose = (info) => {
|
|
530
|
+
this.onClose(info);
|
|
521
531
|
};
|
|
522
532
|
this.client.onPing = (pingId) => {
|
|
523
533
|
this.onPing(pingId);
|
package/dist/mock.d.ts
CHANGED
|
@@ -32,8 +32,11 @@ export interface MockWSController {
|
|
|
32
32
|
simulateRawMessage(data: string | ArrayBuffer): void;
|
|
33
33
|
/** Simulate a WebSocket error event */
|
|
34
34
|
simulateWsError(): void;
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Simulate WebSocket close. `code` defaults to 1000. Any code other than
|
|
37
|
+
* 1006 counts as a clean close, as in the browser.
|
|
38
|
+
*/
|
|
39
|
+
simulateClose(code?: number, reason?: string): void;
|
|
37
40
|
/** Current WebSocket ready state */
|
|
38
41
|
readonly readyState: number;
|
|
39
42
|
}
|
package/dist/mock.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gEAAgE;IAChE,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,MAAM,GAAG,WAAW,CAAC,EAAE,CAAC;IAEpD,+DAA+D;IAC/D,YAAY,IAAI,IAAI,CAAC;IAErB,+DAA+D;IAC/D,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAAC;IAErD,uCAAuC;IACvC,eAAe,IAAI,IAAI,CAAC;IAExB
|
|
1
|
+
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gEAAgE;IAChE,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,MAAM,GAAG,WAAW,CAAC,EAAE,CAAC;IAEpD,+DAA+D;IAC/D,YAAY,IAAI,IAAI,CAAC;IAErB,+DAA+D;IAC/D,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAAC;IAErD,uCAAuC;IACvC,eAAe,IAAI,IAAI,CAAC;IAExB;;;OAGG;IACH,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAEpD,oCAAoC;IACpC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,IAAI;IAClC,SAAS,EAAE,OAAO,UAAU,CAAC,SAAS,CAAC;IACvC,UAAU,EAAE,gBAAgB,CAAC;CAC9B,CAuEA"}
|
package/dist/mock.js
CHANGED
|
@@ -47,7 +47,7 @@ function createMockWSPair() {
|
|
|
47
47
|
}
|
|
48
48
|
close() {
|
|
49
49
|
this.readyState = types_1.ReadyState.CLOSED;
|
|
50
|
-
this.onclose?.({});
|
|
50
|
+
this.onclose?.({ code: 1000, reason: '', wasClean: true });
|
|
51
51
|
}
|
|
52
52
|
get sentRaw() {
|
|
53
53
|
return this._sent;
|
|
@@ -74,10 +74,10 @@ function createMockWSPair() {
|
|
|
74
74
|
assertConnected('simulateWsError');
|
|
75
75
|
ws.onerror?.({});
|
|
76
76
|
},
|
|
77
|
-
simulateClose(code) {
|
|
77
|
+
simulateClose(code = 1000, reason = '') {
|
|
78
78
|
assertConnected('simulateClose');
|
|
79
79
|
ws.readyState = types_1.ReadyState.CLOSED;
|
|
80
|
-
ws.onclose?.({});
|
|
80
|
+
ws.onclose?.({ code, reason, wasClean: code !== 1006 });
|
|
81
81
|
},
|
|
82
82
|
get readyState() {
|
|
83
83
|
return ws?.readyState ?? types_1.ReadyState.CLOSED;
|
package/dist/mock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;
|
|
1
|
+
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;AA2CH,4CA0EC;AAnHD,mCAAqC;AAgCrC;;;;;;;;GAQG;AACH,SAAgB,gBAAgB;IAI9B,IAAI,EAAE,GAAyB,IAAI,CAAC;IAEpC,MAAM,aAAa;QAUjB,YAAY,IAAY;YATxB,eAAU,GAAW,kBAAU,CAAC,UAAU,CAAC;YAC3C,eAAU,GAAG,EAAE,CAAC;YAChB,WAAM,GAAmC,IAAI,CAAC;YAC9C,cAAS,GAA0D,IAAI,CAAC;YACxE,YAAO,GAAmC,IAAI,CAAC;YAC/C,YAAO,GAAmC,IAAI,CAAC;YAEvC,UAAK,GAA6B,EAAE,CAAC;YAG3C,EAAE,GAAG,IAAI,CAAC;QACZ,CAAC;QAED,IAAI,CAAC,IAA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EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,KAAK,IAAI,EAAE,CAAC,CAAC;QAC3D,CAAC;QAED,IAAI,UAAU;YACZ,OAAO,EAAE,EAAE,UAAU,IAAI,kBAAU,CAAC,MAAM,CAAC;QAC7C,CAAC;KACF,CAAC;IAEF,OAAO;QACL,SAAS,EAAE,aAAuD;QAClE,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -81,7 +81,7 @@ var ServiceKit = (() => {
|
|
|
81
81
|
/** Called when a ping is received (after auto-pong if enabled) */
|
|
82
82
|
this.onPing = () => {
|
|
83
83
|
};
|
|
84
|
-
/** Called when the connection closes */
|
|
84
|
+
/** Called when the connection closes, with the close code and reason */
|
|
85
85
|
this.onClose = () => {
|
|
86
86
|
};
|
|
87
87
|
/** Called when a WebSocket error occurs */
|
|
@@ -121,8 +121,12 @@ var ServiceKit = (() => {
|
|
|
121
121
|
this.onError(errorMsg);
|
|
122
122
|
reject(new Error(errorMsg));
|
|
123
123
|
};
|
|
124
|
-
this.ws.onclose = () => {
|
|
125
|
-
this.onClose(
|
|
124
|
+
this.ws.onclose = (event) => {
|
|
125
|
+
this.onClose({
|
|
126
|
+
code: event.code,
|
|
127
|
+
reason: event.reason,
|
|
128
|
+
wasClean: event.wasClean
|
|
129
|
+
});
|
|
126
130
|
};
|
|
127
131
|
this.ws.onmessage = (event) => {
|
|
128
132
|
this.handleRawMessage(event.data);
|
|
@@ -252,7 +256,7 @@ var ServiceKit = (() => {
|
|
|
252
256
|
}
|
|
253
257
|
close() {
|
|
254
258
|
this.readyState = ReadyState.CLOSED;
|
|
255
|
-
this.onclose?.({});
|
|
259
|
+
this.onclose?.({ code: 1e3, reason: "", wasClean: true });
|
|
256
260
|
}
|
|
257
261
|
get sentRaw() {
|
|
258
262
|
return this._sent;
|
|
@@ -278,10 +282,10 @@ var ServiceKit = (() => {
|
|
|
278
282
|
assertConnected("simulateWsError");
|
|
279
283
|
ws.onerror?.({});
|
|
280
284
|
},
|
|
281
|
-
simulateClose(code) {
|
|
285
|
+
simulateClose(code = 1e3, reason = "") {
|
|
282
286
|
assertConnected("simulateClose");
|
|
283
287
|
ws.readyState = ReadyState.CLOSED;
|
|
284
|
-
ws.onclose?.({});
|
|
288
|
+
ws.onclose?.({ code, reason, wasClean: code !== 1006 });
|
|
285
289
|
},
|
|
286
290
|
get readyState() {
|
|
287
291
|
return ws?.readyState ?? ReadyState.CLOSED;
|
|
@@ -296,6 +300,7 @@ var ServiceKit = (() => {
|
|
|
296
300
|
// src/grpcws-client.ts
|
|
297
301
|
var GRPCWSClient = class _GRPCWSClient {
|
|
298
302
|
constructor(options = {}) {
|
|
303
|
+
this.streamEnded = false;
|
|
299
304
|
/** Called when a data message is received */
|
|
300
305
|
this.onMessage = () => {
|
|
301
306
|
};
|
|
@@ -305,7 +310,7 @@ var ServiceKit = (() => {
|
|
|
305
310
|
/** Called when the server sends an error */
|
|
306
311
|
this.onError = () => {
|
|
307
312
|
};
|
|
308
|
-
/** Called when the connection closes */
|
|
313
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
309
314
|
this.onClose = () => {
|
|
310
315
|
};
|
|
311
316
|
/** Called when a ping is received */
|
|
@@ -320,6 +325,7 @@ var ServiceKit = (() => {
|
|
|
320
325
|
* @returns Promise that resolves when connected
|
|
321
326
|
*/
|
|
322
327
|
connect(url) {
|
|
328
|
+
this.streamEnded = false;
|
|
323
329
|
return this.base.connect(url);
|
|
324
330
|
}
|
|
325
331
|
/**
|
|
@@ -417,8 +423,11 @@ var ServiceKit = (() => {
|
|
|
417
423
|
JSON.stringify({ type: "error", error: message ?? "Mock error" })
|
|
418
424
|
);
|
|
419
425
|
},
|
|
420
|
-
|
|
421
|
-
wsCtrl.
|
|
426
|
+
simulateStreamEnd() {
|
|
427
|
+
wsCtrl.simulateRawMessage(JSON.stringify({ type: "stream_end" }));
|
|
428
|
+
},
|
|
429
|
+
simulateClose(code, reason) {
|
|
430
|
+
wsCtrl.simulateClose(code, reason);
|
|
422
431
|
},
|
|
423
432
|
get readyState() {
|
|
424
433
|
return wsCtrl.readyState;
|
|
@@ -433,8 +442,8 @@ var ServiceKit = (() => {
|
|
|
433
442
|
this.base.onMessage = (msg) => {
|
|
434
443
|
this.handleEnvelopeMessage(msg);
|
|
435
444
|
};
|
|
436
|
-
this.base.onClose = () => {
|
|
437
|
-
this.onClose();
|
|
445
|
+
this.base.onClose = (info) => {
|
|
446
|
+
this.onClose({ ...info, streamEnded: this.streamEnded });
|
|
438
447
|
};
|
|
439
448
|
this.base.onError = (error) => {
|
|
440
449
|
this.onError(error);
|
|
@@ -457,6 +466,7 @@ var ServiceKit = (() => {
|
|
|
457
466
|
this.onMessage(controlMsg.data);
|
|
458
467
|
break;
|
|
459
468
|
case "stream_end":
|
|
469
|
+
this.streamEnded = true;
|
|
460
470
|
this.onStreamEnd();
|
|
461
471
|
break;
|
|
462
472
|
case "error":
|
|
@@ -486,7 +496,7 @@ var ServiceKit = (() => {
|
|
|
486
496
|
/** Called when the server sends an error */
|
|
487
497
|
this.onError = () => {
|
|
488
498
|
};
|
|
489
|
-
/** Called when the connection closes */
|
|
499
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
490
500
|
this.onClose = () => {
|
|
491
501
|
};
|
|
492
502
|
/** Called when a ping is received */
|
|
@@ -553,8 +563,8 @@ var ServiceKit = (() => {
|
|
|
553
563
|
this.client.onError = (error) => {
|
|
554
564
|
this.onError(error);
|
|
555
565
|
};
|
|
556
|
-
this.client.onClose = () => {
|
|
557
|
-
this.onClose();
|
|
566
|
+
this.client.onClose = (info) => {
|
|
567
|
+
this.onClose(info);
|
|
558
568
|
};
|
|
559
569
|
this.client.onPing = (pingId) => {
|
|
560
570
|
this.onPing(pingId);
|
package/dist/typed-client.d.ts
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* - ts-proto
|
|
9
9
|
* - etc.
|
|
10
10
|
*/
|
|
11
|
-
import {
|
|
11
|
+
import { StreamCloseInfo } from './grpcws-client';
|
|
12
|
+
import { ClientOptions, VoidHandler, ErrorHandler, CloseHandler } from './types';
|
|
12
13
|
/**
|
|
13
14
|
* Type-safe gRPC-WebSocket client with generic input/output types.
|
|
14
15
|
*
|
|
@@ -52,8 +53,8 @@ export declare class TypedGRPCWSClient<TIn, TOut> {
|
|
|
52
53
|
onStreamEnd: VoidHandler;
|
|
53
54
|
/** Called when the server sends an error */
|
|
54
55
|
onError: ErrorHandler;
|
|
55
|
-
/** Called when the connection closes */
|
|
56
|
-
onClose:
|
|
56
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
57
|
+
onClose: CloseHandler<StreamCloseInfo>;
|
|
57
58
|
/** Called when a ping is received */
|
|
58
59
|
onPing: (pingId: number) => void;
|
|
59
60
|
constructor(options?: ClientOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-client.d.ts","sourceRoot":"","sources":["../src/typed-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"typed-client.d.ts","sourceRoot":"","sources":["../src/typed-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAgB,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEjF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,+FAA+F;IACxF,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,CAAY;IAEzD,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"}
|
package/dist/typed-client.js
CHANGED
|
@@ -55,7 +55,7 @@ class TypedGRPCWSClient {
|
|
|
55
55
|
this.onStreamEnd = () => { };
|
|
56
56
|
/** Called when the server sends an error */
|
|
57
57
|
this.onError = () => { };
|
|
58
|
-
/** Called when the connection closes */
|
|
58
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
59
59
|
this.onClose = () => { };
|
|
60
60
|
/** Called when a ping is received */
|
|
61
61
|
this.onPing = () => { };
|
|
@@ -121,8 +121,8 @@ class TypedGRPCWSClient {
|
|
|
121
121
|
this.client.onError = (error) => {
|
|
122
122
|
this.onError(error);
|
|
123
123
|
};
|
|
124
|
-
this.client.onClose = () => {
|
|
125
|
-
this.onClose();
|
|
124
|
+
this.client.onClose = (info) => {
|
|
125
|
+
this.onClose(info);
|
|
126
126
|
};
|
|
127
127
|
this.client.onPing = (pingId) => {
|
|
128
128
|
this.onPing(pingId);
|
package/dist/typed-client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-client.js","sourceRoot":"","sources":["../src/typed-client.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEH,
|
|
1
|
+
{"version":3,"file":"typed-client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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,CAAC,IAAqB,EAAE,EAAE;YAC9C,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrB,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"}
|
package/dist/types.d.ts
CHANGED
|
@@ -72,6 +72,20 @@ export interface ClientOptions<I = unknown, O = unknown> {
|
|
|
72
72
|
export type MessageHandler<T = unknown> = (data: T) => void;
|
|
73
73
|
export type ErrorHandler = (error: string) => void;
|
|
74
74
|
export type VoidHandler = () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Why the WebSocket closed, taken from the browser's CloseEvent.
|
|
77
|
+
*
|
|
78
|
+
* `code` is the close code the server sent (1000 for a normal close), or
|
|
79
|
+
* 1006 when the connection dropped without a close frame. `wasClean` is
|
|
80
|
+
* false in that case.
|
|
81
|
+
*/
|
|
82
|
+
export interface CloseInfo {
|
|
83
|
+
code: number;
|
|
84
|
+
reason: string;
|
|
85
|
+
wasClean: boolean;
|
|
86
|
+
}
|
|
87
|
+
/** Called when the connection closes, with the reason. */
|
|
88
|
+
export type CloseHandler<T extends CloseInfo = CloseInfo> = (info: T) => void;
|
|
75
89
|
/**
|
|
76
90
|
* Codec interface for encoding/decoding data messages.
|
|
77
91
|
* This mirrors the server-side Codec interface.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;;GAUG;AACH,MAAM,MAAM,WAAW,GACnB,MAAM,GACN,OAAO,GACP,YAAY,GACZ,MAAM,GACN,MAAM,GACN,QAAQ,GACR,UAAU,CAAC;AAEf;;;;;;;;GAQG;AACH,MAAM,WAAW,cAAc;IAC7B,kCAAkC;IAClC,IAAI,EAAE,WAAW,CAAC;IAClB,0DAA0D;IAC1D,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,6CAA6C;IAC7C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU;;;;;CAKb,CAAC;AAEX,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,CAAC;AAE1E;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO;IACrD;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,SAAS,CAAC;IAE7B;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC;AAC5D,MAAM,M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,OAAO;IAC7C;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,CAAC,CAAC;IAEtC;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,GAAG,WAAW,CAAC;CACtC;AAED;;;GAGG;AACH,qBAAa,SAAS,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,CAAE,YAAW,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;IACrE,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,CAAC;IASrC,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM;CAGvB;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,WAAW,CAAC,CAAC,EAAE,CAAC,CAAE,YAAW,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;IAEjD,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,UAAU;gBADV,UAAU,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,CAAC,EACpC,UAAU,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,UAAU;IAG5C,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,CAAC;IAOrC,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,WAAW;CAK5B"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AA0CH;;;GAGG;AACU,QAAA,UAAU,GAAG;IACxB,UAAU,EAAE,CAAC;IACb,IAAI,EAAE,CAAC;IACP,OAAO,EAAE,CAAC;IACV,MAAM,EAAE,CAAC;CACD,CAAC;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AA0CH;;;GAGG;AACU,QAAA,UAAU,GAAG;IACxB,UAAU,EAAE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servicekit-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "TypeScript client for ServiceKit WebSocket and SSE protocols",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"browser": "./dist/index.mjs",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
"./dist/*": "./dist/*"
|
|
18
|
+
},
|
|
19
|
+
"unpkg": "dist/servicekit-client.browser.js",
|
|
20
|
+
"jsdelivr": "dist/servicekit-client.browser.js",
|
|
9
21
|
"files": [
|
|
10
22
|
"dist",
|
|
11
23
|
"src",
|
package/src/base-client.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
JSONCodec,
|
|
15
15
|
MessageHandler,
|
|
16
16
|
ErrorHandler,
|
|
17
|
-
|
|
17
|
+
CloseHandler,
|
|
18
18
|
ReadyState,
|
|
19
19
|
} from './types';
|
|
20
20
|
|
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
* ```typescript
|
|
32
32
|
* const client = new BaseWSClient();
|
|
33
33
|
* client.onMessage = (data) => console.log('Received:', data);
|
|
34
|
-
* client.onClose = () => console.log('Disconnected');
|
|
34
|
+
* client.onClose = ({ code, reason }) => console.log('Disconnected', code, reason);
|
|
35
35
|
* await client.connect('ws://localhost:8080/ws');
|
|
36
36
|
* client.send({ hello: 'world' });
|
|
37
37
|
* ```
|
|
@@ -48,8 +48,8 @@ export class BaseWSClient<I = unknown, O = unknown> {
|
|
|
48
48
|
/** Called when a ping is received (after auto-pong if enabled) */
|
|
49
49
|
public onPing: (pingId: number) => void = () => {};
|
|
50
50
|
|
|
51
|
-
/** Called when the connection closes */
|
|
52
|
-
public onClose:
|
|
51
|
+
/** Called when the connection closes, with the close code and reason */
|
|
52
|
+
public onClose: CloseHandler = () => {};
|
|
53
53
|
|
|
54
54
|
/** Called when a WebSocket error occurs */
|
|
55
55
|
public onError: ErrorHandler = () => {};
|
|
@@ -97,8 +97,12 @@ export class BaseWSClient<I = unknown, O = unknown> {
|
|
|
97
97
|
reject(new Error(errorMsg));
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
-
this.ws.onclose = () => {
|
|
101
|
-
this.onClose(
|
|
100
|
+
this.ws.onclose = (event: CloseEvent) => {
|
|
101
|
+
this.onClose({
|
|
102
|
+
code: event.code,
|
|
103
|
+
reason: event.reason,
|
|
104
|
+
wasClean: event.wasClean,
|
|
105
|
+
});
|
|
102
106
|
};
|
|
103
107
|
|
|
104
108
|
this.ws.onmessage = (event) => {
|
package/src/grpcws-client.ts
CHANGED
|
@@ -17,8 +17,22 @@ import {
|
|
|
17
17
|
MessageHandler,
|
|
18
18
|
ErrorHandler,
|
|
19
19
|
VoidHandler,
|
|
20
|
+
CloseInfo,
|
|
21
|
+
CloseHandler,
|
|
20
22
|
} from './types';
|
|
21
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Close details for a grpcws stream.
|
|
26
|
+
*
|
|
27
|
+
* `streamEnded` is true when the server sent stream_end before the close.
|
|
28
|
+
* A server with `CloseOnStreamEnd` set closes with code 1000 right after
|
|
29
|
+
* stream_end, so `streamEnded && code === 1000` is a normal end of the RPC.
|
|
30
|
+
* Anything else means the connection dropped or was closed mid-stream.
|
|
31
|
+
*/
|
|
32
|
+
export interface StreamCloseInfo extends CloseInfo {
|
|
33
|
+
streamEnded: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
22
36
|
/**
|
|
23
37
|
* Mock controller for testing GRPCWSClient without real WebSocket connections.
|
|
24
38
|
*
|
|
@@ -48,8 +62,11 @@ export interface MockController {
|
|
|
48
62
|
/** Simulate a server error (delivers {type: "error"} envelope to onError) */
|
|
49
63
|
simulateError(message?: string): void;
|
|
50
64
|
|
|
51
|
-
/** Simulate
|
|
52
|
-
|
|
65
|
+
/** Simulate the server ending the stream (delivers a {type: "stream_end"} envelope) */
|
|
66
|
+
simulateStreamEnd(): void;
|
|
67
|
+
|
|
68
|
+
/** Simulate WebSocket close. `code` defaults to 1000. */
|
|
69
|
+
simulateClose(code?: number, reason?: string): void;
|
|
53
70
|
|
|
54
71
|
/** Current WebSocket ready state */
|
|
55
72
|
readonly readyState: number;
|
|
@@ -96,6 +113,7 @@ export interface MockController {
|
|
|
96
113
|
*/
|
|
97
114
|
export class GRPCWSClient {
|
|
98
115
|
private base: BaseWSClient;
|
|
116
|
+
private streamEnded = false;
|
|
99
117
|
|
|
100
118
|
/** Called when a data message is received */
|
|
101
119
|
public onMessage: MessageHandler = () => {};
|
|
@@ -106,8 +124,8 @@ export class GRPCWSClient {
|
|
|
106
124
|
/** Called when the server sends an error */
|
|
107
125
|
public onError: ErrorHandler = () => {};
|
|
108
126
|
|
|
109
|
-
/** Called when the connection closes */
|
|
110
|
-
public onClose:
|
|
127
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
128
|
+
public onClose: CloseHandler<StreamCloseInfo> = () => {};
|
|
111
129
|
|
|
112
130
|
/** Called when a ping is received */
|
|
113
131
|
public onPing: (pingId: number) => void = () => {};
|
|
@@ -123,6 +141,7 @@ export class GRPCWSClient {
|
|
|
123
141
|
* @returns Promise that resolves when connected
|
|
124
142
|
*/
|
|
125
143
|
connect(url: string): Promise<void> {
|
|
144
|
+
this.streamEnded = false;
|
|
126
145
|
return this.base.connect(url);
|
|
127
146
|
}
|
|
128
147
|
|
|
@@ -233,8 +252,12 @@ export class GRPCWSClient {
|
|
|
233
252
|
);
|
|
234
253
|
},
|
|
235
254
|
|
|
236
|
-
|
|
237
|
-
wsCtrl.
|
|
255
|
+
simulateStreamEnd() {
|
|
256
|
+
wsCtrl.simulateRawMessage(JSON.stringify({ type: 'stream_end' }));
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
simulateClose(code?: number, reason?: string) {
|
|
260
|
+
wsCtrl.simulateClose(code, reason);
|
|
238
261
|
},
|
|
239
262
|
|
|
240
263
|
get readyState(): number {
|
|
@@ -253,8 +276,8 @@ export class GRPCWSClient {
|
|
|
253
276
|
this.handleEnvelopeMessage(msg);
|
|
254
277
|
};
|
|
255
278
|
|
|
256
|
-
this.base.onClose = () => {
|
|
257
|
-
this.onClose();
|
|
279
|
+
this.base.onClose = (info: CloseInfo) => {
|
|
280
|
+
this.onClose({ ...info, streamEnded: this.streamEnded });
|
|
258
281
|
};
|
|
259
282
|
|
|
260
283
|
this.base.onError = (error: string) => {
|
|
@@ -284,6 +307,7 @@ export class GRPCWSClient {
|
|
|
284
307
|
break;
|
|
285
308
|
|
|
286
309
|
case 'stream_end':
|
|
310
|
+
this.streamEnded = true;
|
|
287
311
|
this.onStreamEnd();
|
|
288
312
|
break;
|
|
289
313
|
|
package/src/index.ts
CHANGED
|
@@ -78,11 +78,13 @@ export {
|
|
|
78
78
|
MessageHandler,
|
|
79
79
|
ErrorHandler,
|
|
80
80
|
VoidHandler,
|
|
81
|
+
CloseInfo,
|
|
82
|
+
CloseHandler,
|
|
81
83
|
} from './types';
|
|
82
84
|
|
|
83
85
|
// Clients
|
|
84
86
|
export { BaseWSClient } from './base-client';
|
|
85
|
-
export { GRPCWSClient, MockController } from './grpcws-client';
|
|
87
|
+
export { GRPCWSClient, MockController, StreamCloseInfo } from './grpcws-client';
|
|
86
88
|
export { TypedGRPCWSClient } from './typed-client';
|
|
87
89
|
|
|
88
90
|
// SSE Clients
|
package/src/mock.ts
CHANGED
|
@@ -39,8 +39,11 @@ export interface MockWSController {
|
|
|
39
39
|
/** Simulate a WebSocket error event */
|
|
40
40
|
simulateWsError(): void;
|
|
41
41
|
|
|
42
|
-
/**
|
|
43
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Simulate WebSocket close. `code` defaults to 1000. Any code other than
|
|
44
|
+
* 1006 counts as a clean close, as in the browser.
|
|
45
|
+
*/
|
|
46
|
+
simulateClose(code?: number, reason?: string): void;
|
|
44
47
|
|
|
45
48
|
/** Current WebSocket ready state */
|
|
46
49
|
readonly readyState: number;
|
|
@@ -81,7 +84,7 @@ export function createMockWSPair(): {
|
|
|
81
84
|
|
|
82
85
|
close() {
|
|
83
86
|
this.readyState = ReadyState.CLOSED;
|
|
84
|
-
this.onclose?.({});
|
|
87
|
+
this.onclose?.({ code: 1000, reason: '', wasClean: true });
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
get sentRaw(): readonly (string | ArrayBuffer)[] {
|
|
@@ -114,10 +117,10 @@ export function createMockWSPair(): {
|
|
|
114
117
|
ws!.onerror?.({});
|
|
115
118
|
},
|
|
116
119
|
|
|
117
|
-
simulateClose(code
|
|
120
|
+
simulateClose(code = 1000, reason = '') {
|
|
118
121
|
assertConnected('simulateClose');
|
|
119
122
|
ws!.readyState = ReadyState.CLOSED;
|
|
120
|
-
ws!.onclose?.({});
|
|
123
|
+
ws!.onclose?.({ code, reason, wasClean: code !== 1006 });
|
|
121
124
|
},
|
|
122
125
|
|
|
123
126
|
get readyState() {
|
package/src/typed-client.ts
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* - etc.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { GRPCWSClient } from './grpcws-client';
|
|
13
|
-
import { ClientOptions, VoidHandler, ErrorHandler } from './types';
|
|
12
|
+
import { GRPCWSClient, StreamCloseInfo } from './grpcws-client';
|
|
13
|
+
import { ClientOptions, VoidHandler, ErrorHandler, CloseHandler } from './types';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Type-safe gRPC-WebSocket client with generic input/output types.
|
|
@@ -59,8 +59,8 @@ export class TypedGRPCWSClient<TIn, TOut> {
|
|
|
59
59
|
/** Called when the server sends an error */
|
|
60
60
|
public onError: ErrorHandler = () => {};
|
|
61
61
|
|
|
62
|
-
/** Called when the connection closes */
|
|
63
|
-
public onClose:
|
|
62
|
+
/** Called when the connection closes, with the close code and whether stream_end came first */
|
|
63
|
+
public onClose: CloseHandler<StreamCloseInfo> = () => {};
|
|
64
64
|
|
|
65
65
|
/** Called when a ping is received */
|
|
66
66
|
public onPing: (pingId: number) => void = () => {};
|
|
@@ -139,8 +139,8 @@ export class TypedGRPCWSClient<TIn, TOut> {
|
|
|
139
139
|
this.onError(error);
|
|
140
140
|
};
|
|
141
141
|
|
|
142
|
-
this.client.onClose = () => {
|
|
143
|
-
this.onClose();
|
|
142
|
+
this.client.onClose = (info: StreamCloseInfo) => {
|
|
143
|
+
this.onClose(info);
|
|
144
144
|
};
|
|
145
145
|
|
|
146
146
|
this.client.onPing = (pingId: number) => {
|
package/src/types.ts
CHANGED
|
@@ -88,6 +88,22 @@ export type MessageHandler<T = unknown> = (data: T) => void;
|
|
|
88
88
|
export type ErrorHandler = (error: string) => void;
|
|
89
89
|
export type VoidHandler = () => void;
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Why the WebSocket closed, taken from the browser's CloseEvent.
|
|
93
|
+
*
|
|
94
|
+
* `code` is the close code the server sent (1000 for a normal close), or
|
|
95
|
+
* 1006 when the connection dropped without a close frame. `wasClean` is
|
|
96
|
+
* false in that case.
|
|
97
|
+
*/
|
|
98
|
+
export interface CloseInfo {
|
|
99
|
+
code: number;
|
|
100
|
+
reason: string;
|
|
101
|
+
wasClean: boolean;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Called when the connection closes, with the reason. */
|
|
105
|
+
export type CloseHandler<T extends CloseInfo = CloseInfo> = (info: T) => void;
|
|
106
|
+
|
|
91
107
|
/**
|
|
92
108
|
* Codec interface for encoding/decoding data messages.
|
|
93
109
|
* This mirrors the server-side Codec interface.
|