sonamu 0.13.7 → 0.13.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/entity/entity.d.ts +5 -0
- package/dist/entity/entity.d.ts.map +1 -1
- package/dist/entity/entity.js +15 -5
- package/dist/stream/__tests__/ws-test-helpers.d.ts +2 -1
- package/dist/stream/__tests__/ws-test-helpers.d.ts.map +1 -1
- package/dist/stream/__tests__/ws-test-helpers.js +5 -1
- package/dist/stream/ws-cluster-bus.d.ts +21 -3
- package/dist/stream/ws-cluster-bus.d.ts.map +1 -1
- package/dist/stream/ws-cluster-bus.js +1 -1
- package/dist/stream/ws-delivery.d.ts +11 -0
- package/dist/stream/ws-delivery.d.ts.map +1 -1
- package/dist/stream/ws-delivery.js +95 -4
- package/dist/stream/ws-presence-index.d.ts +2 -0
- package/dist/stream/ws-presence-index.d.ts.map +1 -1
- package/dist/stream/ws-presence-index.js +9 -3
- package/dist/stream/ws-presence-store.d.ts +2 -0
- package/dist/stream/ws-presence-store.d.ts.map +1 -1
- package/dist/stream/ws-presence-store.js +4 -1
- package/dist/stream/ws-registry.d.ts +12 -0
- package/dist/stream/ws-registry.d.ts.map +1 -1
- package/dist/stream/ws-registry.js +49 -3
- package/dist/stream/ws.d.ts +2 -1
- package/dist/stream/ws.d.ts.map +1 -1
- package/dist/stream/ws.js +4 -1
- package/package.json +1 -1
- package/src/entity/__tests__/entity-subset-query.test.ts +83 -0
- package/src/entity/entity.ts +32 -5
- package/src/stream/__tests__/ws-contracts.test.ts +1 -0
- package/src/stream/__tests__/ws-leave-user.test.ts +293 -0
- package/src/stream/__tests__/ws-multinode.test.ts +3 -1
- package/src/stream/__tests__/ws-test-helpers.ts +6 -0
- package/src/stream/ws-cluster-bus.ts +26 -3
- package/src/stream/ws-delivery.ts +117 -6
- package/src/stream/ws-presence-index.ts +10 -1
- package/src/stream/ws-presence-store.ts +6 -0
- package/src/stream/ws-registry.ts +78 -2
- package/src/stream/ws.ts +9 -0
|
@@ -34,6 +34,14 @@ export type {
|
|
|
34
34
|
WebSocketRoomId,
|
|
35
35
|
WebSocketUserId,
|
|
36
36
|
};
|
|
37
|
+
export type WebSocketLeaveUserResult = {
|
|
38
|
+
localLeftCount: number;
|
|
39
|
+
// 발행 성공 여부와 무관하게 전송을 시도한 원격 노드 수
|
|
40
|
+
targetNodeCount: number;
|
|
41
|
+
// 로컬 store 반영, 노드 조회 또는 제어 메시지 발행이 실패했으면 true
|
|
42
|
+
partial: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
37
45
|
export type WebSocketConnectionMeta = WebSocketSessionPresence;
|
|
38
46
|
export type WebSocketRoomMemberCount = {
|
|
39
47
|
count: number;
|
|
@@ -92,6 +100,18 @@ export class WebSocketRegistry {
|
|
|
92
100
|
localConnections: this.localConnections,
|
|
93
101
|
resolveLocalSessionIds: (audience) =>
|
|
94
102
|
this.memory.queryAudience(audience).map((session) => session.sessionId),
|
|
103
|
+
leaveLocalUser: (userId, roomId, namespace) => this.leaveLocalUser(userId, roomId, namespace),
|
|
104
|
+
resolveControlNodeIds: async (userId, namespace) => {
|
|
105
|
+
const nodes = this.presenceStore.getLiveNodeIds
|
|
106
|
+
? await this.presenceStore.getLiveNodeIds()
|
|
107
|
+
: (
|
|
108
|
+
await this.presenceStore.resolveRoute(
|
|
109
|
+
WebSocketAudience.user(userId, namespace),
|
|
110
|
+
this.nodeId,
|
|
111
|
+
)
|
|
112
|
+
).remoteNodeIds;
|
|
113
|
+
return [...new Set(nodes)].filter((nodeId) => nodeId !== this.nodeId);
|
|
114
|
+
},
|
|
95
115
|
audienceResolver: new WebSocketAudienceResolver({
|
|
96
116
|
nodeId: this.nodeId,
|
|
97
117
|
presenceStore: this.presenceStore,
|
|
@@ -298,6 +318,57 @@ export class WebSocketRegistry {
|
|
|
298
318
|
}
|
|
299
319
|
|
|
300
320
|
leave(connectionId: string, roomId: WebSocketRoomId): Promise<void> {
|
|
321
|
+
return this.leaveSession(connectionId, roomId);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* 사용자의 모든 로컬 세션을 방에서 즉시 제거하고, live 노드에 같은 퇴장을 요청한다.
|
|
326
|
+
* namespace 생략 또는 빈 문자열은 전체 namespace를 뜻한다. 원격 적용 완료를 확인하는 응답은 기다리지 않는다.
|
|
327
|
+
*/
|
|
328
|
+
leaveUser(
|
|
329
|
+
userId: WebSocketUserId,
|
|
330
|
+
roomId: WebSocketRoomId,
|
|
331
|
+
namespace?: string,
|
|
332
|
+
): Promise<WebSocketLeaveUserResult> {
|
|
333
|
+
const local = this.leaveLocalUser(userId, roomId, namespace);
|
|
334
|
+
// 로컬 변경 직후 큐 자리를 확보해 뒤따르는 publish가 제어 메시지를 앞지르지 못하게 한다
|
|
335
|
+
const remote = this.deliveryEngine.publishLeaveUser(userId, roomId, namespace);
|
|
336
|
+
return Promise.all([local.completion, remote]).then(([localPartial, result]) => {
|
|
337
|
+
const summary = {
|
|
338
|
+
localLeftCount: local.localLeftCount,
|
|
339
|
+
targetNodeCount: result.targetNodeCount,
|
|
340
|
+
partial: localPartial || result.partial,
|
|
341
|
+
};
|
|
342
|
+
this.telemetryController.emit({
|
|
343
|
+
name: "ws.room.user_left",
|
|
344
|
+
level: "info",
|
|
345
|
+
detail: { userId, roomId, namespace, ...summary },
|
|
346
|
+
});
|
|
347
|
+
return summary;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private leaveLocalUser(userId: WebSocketUserId, roomId: WebSocketRoomId, namespace?: string) {
|
|
352
|
+
let partial = false;
|
|
353
|
+
const sessions = this.memory
|
|
354
|
+
.getUserSessions(userId, namespace)
|
|
355
|
+
.filter((session) => session.rooms.has(roomId));
|
|
356
|
+
const mutations = sessions.map((session) =>
|
|
357
|
+
this.leaveSession(session.sessionId, roomId, () => {
|
|
358
|
+
partial = true;
|
|
359
|
+
}),
|
|
360
|
+
);
|
|
361
|
+
return {
|
|
362
|
+
localLeftCount: sessions.length,
|
|
363
|
+
completion: Promise.all(mutations).then(() => partial),
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
private leaveSession(
|
|
368
|
+
connectionId: string,
|
|
369
|
+
roomId: WebSocketRoomId,
|
|
370
|
+
onFailed?: () => void,
|
|
371
|
+
): Promise<void> {
|
|
301
372
|
const meta = this.memory.getConnection(connectionId);
|
|
302
373
|
if (!meta) {
|
|
303
374
|
this.emitMutationSkipped(connectionId, { operation: "leave", roomId });
|
|
@@ -315,8 +386,11 @@ export class WebSocketRegistry {
|
|
|
315
386
|
detail: { roomId },
|
|
316
387
|
...getConnectionTelemetryContext(connection),
|
|
317
388
|
});
|
|
318
|
-
return this.enqueueStoreMutation(
|
|
319
|
-
|
|
389
|
+
return this.enqueueStoreMutation(
|
|
390
|
+
connectionId,
|
|
391
|
+
"leave",
|
|
392
|
+
() => this.presenceStore.leave(connectionId, roomId),
|
|
393
|
+
onFailed,
|
|
320
394
|
);
|
|
321
395
|
}
|
|
322
396
|
|
|
@@ -430,6 +504,7 @@ export class WebSocketRegistry {
|
|
|
430
504
|
sessionId: string,
|
|
431
505
|
operation: StoreMutationOperation,
|
|
432
506
|
call: () => MaybePromise<void>,
|
|
507
|
+
onFailed?: () => void,
|
|
433
508
|
): Promise<void> {
|
|
434
509
|
const previous = this.mutationQueues.get(sessionId) ?? Promise.resolve();
|
|
435
510
|
const next: Promise<void> = previous
|
|
@@ -437,6 +512,7 @@ export class WebSocketRegistry {
|
|
|
437
512
|
.then(
|
|
438
513
|
() => undefined,
|
|
439
514
|
(cause: unknown) => {
|
|
515
|
+
onFailed?.();
|
|
440
516
|
this.telemetryController.emit({
|
|
441
517
|
name: "ws.registry.mutation.failed",
|
|
442
518
|
level: "warn",
|
package/src/stream/ws.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type ManagedWebSocketConnection,
|
|
17
17
|
WebSocketRegistry,
|
|
18
18
|
type WebSocketRegistryOptions,
|
|
19
|
+
type WebSocketLeaveUserResult,
|
|
19
20
|
type WebSocketRoomId,
|
|
20
21
|
type WebSocketUserId,
|
|
21
22
|
} from "./ws-registry";
|
|
@@ -202,6 +203,14 @@ export class WebSocketRuntime {
|
|
|
202
203
|
return this.registry.activate(connectionId);
|
|
203
204
|
}
|
|
204
205
|
|
|
206
|
+
leaveUser(
|
|
207
|
+
userId: WebSocketUserId,
|
|
208
|
+
roomId: WebSocketRoomId,
|
|
209
|
+
namespace?: string,
|
|
210
|
+
): Promise<WebSocketLeaveUserResult> {
|
|
211
|
+
return this.registry.leaveUser(userId, roomId, namespace);
|
|
212
|
+
}
|
|
213
|
+
|
|
205
214
|
broadcast<Data>(event: string, data: Data, namespace?: string): Promise<void> {
|
|
206
215
|
return this.registry.broadcast(event, data, namespace);
|
|
207
216
|
}
|