@polytric/openws-sdkgen 0.0.12 → 0.0.13
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.
|
@@ -23,7 +23,7 @@ export interface OpenWsEndpoint {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export type Unsubscribe = () => void
|
|
26
|
-
export type TransportEvent = 'message' | 'error'
|
|
26
|
+
export type TransportEvent = 'message' | 'error' | 'close'
|
|
27
27
|
export type TransportHandler = (data: unknown) => void | Promise<void>
|
|
28
28
|
|
|
29
29
|
export interface Transport {
|
|
@@ -101,6 +101,7 @@ export class WsTransport implements Transport {
|
|
|
101
101
|
private readonly listeners: Record<TransportEvent, Set<TransportHandler>> = {
|
|
102
102
|
message: new Set(),
|
|
103
103
|
error: new Set(),
|
|
104
|
+
close: new Set(),
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
constructor(socket?: unknown) {
|
|
@@ -142,14 +143,11 @@ export class WsTransport implements Transport {
|
|
|
142
143
|
close(): void {
|
|
143
144
|
const socket = this.socket as { close?: () => void } | undefined
|
|
144
145
|
socket?.close?.()
|
|
145
|
-
this.
|
|
146
|
-
this.socket = undefined
|
|
147
|
-
this.socketUnsubscribe = undefined
|
|
148
|
-
this.openPromise = undefined
|
|
146
|
+
this.clearSocket()
|
|
149
147
|
}
|
|
150
148
|
|
|
151
149
|
private bindSocket(socket: unknown): void {
|
|
152
|
-
this.
|
|
150
|
+
this.clearSocket()
|
|
153
151
|
this.socket = socket
|
|
154
152
|
this.openPromise = undefined
|
|
155
153
|
|
|
@@ -159,10 +157,29 @@ export class WsTransport implements Transport {
|
|
|
159
157
|
const unsubscribeError = addSocketListener(socket, 'error', error => {
|
|
160
158
|
void this.emit('error', error)
|
|
161
159
|
})
|
|
160
|
+
const unsubscribeClose = addSocketListener(socket, 'close', event => {
|
|
161
|
+
void this.handleSocketClose(socket, event)
|
|
162
|
+
})
|
|
162
163
|
this.socketUnsubscribe = () => {
|
|
163
164
|
unsubscribeMessage()
|
|
164
165
|
unsubscribeError()
|
|
166
|
+
unsubscribeClose()
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private async handleSocketClose(socket: unknown, event: unknown): Promise<void> {
|
|
171
|
+
if (this.socket !== socket) {
|
|
172
|
+
return
|
|
165
173
|
}
|
|
174
|
+
this.clearSocket()
|
|
175
|
+
await this.emit('close', event)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private clearSocket(): void {
|
|
179
|
+
this.socketUnsubscribe?.()
|
|
180
|
+
this.socket = undefined
|
|
181
|
+
this.socketUnsubscribe = undefined
|
|
182
|
+
this.openPromise = undefined
|
|
166
183
|
}
|
|
167
184
|
|
|
168
185
|
private async emit(event: TransportEvent, data: unknown): Promise<void> {
|
|
@@ -196,9 +213,14 @@ export class WsTransport implements Transport {
|
|
|
196
213
|
cleanup()
|
|
197
214
|
reject(error)
|
|
198
215
|
})
|
|
216
|
+
const unsubscribeClose = addSocketListener(socket, 'close', event => {
|
|
217
|
+
cleanup()
|
|
218
|
+
reject(event instanceof Error ? event : new Error('WebSocket closed before opening'))
|
|
219
|
+
})
|
|
199
220
|
cleanup = () => {
|
|
200
221
|
unsubscribeOpen()
|
|
201
222
|
unsubscribeError()
|
|
223
|
+
unsubscribeClose()
|
|
202
224
|
}
|
|
203
225
|
})
|
|
204
226
|
await this.openPromise
|
|
@@ -243,7 +265,7 @@ function getReadyState(socket: unknown): number | undefined {
|
|
|
243
265
|
|
|
244
266
|
function addSocketListener(
|
|
245
267
|
socket: unknown,
|
|
246
|
-
event: 'open' | 'message' | 'error',
|
|
268
|
+
event: 'open' | 'message' | 'error' | 'close',
|
|
247
269
|
handler: (...args: unknown[]) => void
|
|
248
270
|
): Unsubscribe {
|
|
249
271
|
const target = socket as {
|
|
@@ -342,6 +364,7 @@ export class WsTransport {
|
|
|
342
364
|
listeners = {
|
|
343
365
|
message: new Set(),
|
|
344
366
|
error: new Set(),
|
|
367
|
+
close: new Set(),
|
|
345
368
|
}
|
|
346
369
|
|
|
347
370
|
constructor(socket) {
|
|
@@ -382,14 +405,11 @@ export class WsTransport {
|
|
|
382
405
|
|
|
383
406
|
close() {
|
|
384
407
|
this.socket?.close?.()
|
|
385
|
-
this.
|
|
386
|
-
this.socket = undefined
|
|
387
|
-
this.socketUnsubscribe = undefined
|
|
388
|
-
this.openPromise = undefined
|
|
408
|
+
this.clearSocket()
|
|
389
409
|
}
|
|
390
410
|
|
|
391
411
|
bindSocket(socket) {
|
|
392
|
-
this.
|
|
412
|
+
this.clearSocket()
|
|
393
413
|
this.socket = socket
|
|
394
414
|
this.openPromise = undefined
|
|
395
415
|
|
|
@@ -399,10 +419,29 @@ export class WsTransport {
|
|
|
399
419
|
const unsubscribeError = addSocketListener(socket, 'error', error => {
|
|
400
420
|
void this.emit('error', error)
|
|
401
421
|
})
|
|
422
|
+
const unsubscribeClose = addSocketListener(socket, 'close', event => {
|
|
423
|
+
void this.handleSocketClose(socket, event)
|
|
424
|
+
})
|
|
402
425
|
this.socketUnsubscribe = () => {
|
|
403
426
|
unsubscribeMessage()
|
|
404
427
|
unsubscribeError()
|
|
428
|
+
unsubscribeClose()
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async handleSocketClose(socket, event) {
|
|
433
|
+
if (this.socket !== socket) {
|
|
434
|
+
return
|
|
405
435
|
}
|
|
436
|
+
this.clearSocket()
|
|
437
|
+
await this.emit('close', event)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
clearSocket() {
|
|
441
|
+
this.socketUnsubscribe?.()
|
|
442
|
+
this.socket = undefined
|
|
443
|
+
this.socketUnsubscribe = undefined
|
|
444
|
+
this.openPromise = undefined
|
|
406
445
|
}
|
|
407
446
|
|
|
408
447
|
async emit(event, data) {
|
|
@@ -436,9 +475,14 @@ export class WsTransport {
|
|
|
436
475
|
cleanup()
|
|
437
476
|
reject(error)
|
|
438
477
|
})
|
|
478
|
+
const unsubscribeClose = addSocketListener(socket, 'close', event => {
|
|
479
|
+
cleanup()
|
|
480
|
+
reject(event instanceof Error ? event : new Error('WebSocket closed before opening'))
|
|
481
|
+
})
|
|
439
482
|
cleanup = () => {
|
|
440
483
|
unsubscribeOpen()
|
|
441
484
|
unsubscribeError()
|
|
485
|
+
unsubscribeClose()
|
|
442
486
|
}
|
|
443
487
|
})
|
|
444
488
|
await this.openPromise
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polytric/openws-sdkgen",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "OpenWS SDK generator CLI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"test:javascript:node": "node dist/main.cjs --spec ./test/spec.json --out ./generated/javascript/node --project Example --network core --hostRole client --language javascript --environment node",
|
|
60
60
|
"test:typescript:node": "node dist/main.cjs --spec ./test/spec.json --out ./generated/typescript/node --project Example --network core --hostRole client --language typescript --environment node",
|
|
61
61
|
"test:typescript:server": "tsx ./test/typescript/server.ts",
|
|
62
|
-
"test:typescript:client": "tsx ./test/typescript/client.ts"
|
|
62
|
+
"test:typescript:client": "tsx ./test/typescript/client.ts",
|
|
63
|
+
"test:transport-close": "tsx ./test/typescript/transport-close.ts"
|
|
63
64
|
}
|
|
64
65
|
}
|