pondsocket 0.0.2__tar.gz → 0.0.4__tar.gz
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.
- {pondsocket-0.0.2 → pondsocket-0.0.4}/PKG-INFO +2 -2
- {pondsocket-0.0.2 → pondsocket-0.0.4}/pyproject.toml +2 -2
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/__init__.py +16 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/channel.py +160 -10
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/endpoint.py +27 -5
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/presence.py +8 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/pubsub_local.py +6 -0
- pondsocket-0.0.4/src/pondsocket/py.typed +1 -0
- pondsocket-0.0.4/src/pondsocket/typed.py +249 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/types.py +4 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_smoke.py +1 -1
- pondsocket-0.0.2/src/pondsocket/py.typed +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/.gitignore +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/README.md +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/contexts/__init__.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/contexts/connection_context.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/contexts/event_context.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/contexts/join_context.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/contexts/leave_context.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/contexts/outgoing_context.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/distributed/__init__.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/distributed/redis_pubsub.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/errors.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/hooks.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/in_memory_transport.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/lobby.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/middleware.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/parser.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/pubsub.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/server.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/sse_transport.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/store.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/transport.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/src/pondsocket/wire.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_channel.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_distributed_local.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_errors.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_hooks_integration.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_hooks_unit.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_in_memory_transport.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_integration.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_middleware.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_parser.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_presence.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_pubsub.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_redis_pubsub.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_sse_transport.py +0 -0
- {pondsocket-0.0.2 → pondsocket-0.0.4}/tests/test_pondsocket_store.py +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pondsocket
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: PondSocket - channel-based real-time framework with bring-your-own-server design.
|
|
5
5
|
Author: eleven-am
|
|
6
6
|
License: GPL-3.0-or-later
|
|
7
7
|
Requires-Python: >=3.11
|
|
8
|
-
Requires-Dist: pondsocket-common<0.1.0,>=0.0.
|
|
8
|
+
Requires-Dist: pondsocket-common<0.1.0,>=0.0.4
|
|
9
9
|
Provides-Extra: redis
|
|
10
10
|
Requires-Dist: redis>=5; extra == 'redis'
|
|
11
11
|
Description-Content-Type: text/markdown
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pondsocket"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.4"
|
|
4
4
|
description = "PondSocket - channel-based real-time framework with bring-your-own-server design."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.11"
|
|
7
7
|
license = { text = "GPL-3.0-or-later" }
|
|
8
8
|
authors = [{ name = "eleven-am" }]
|
|
9
9
|
dependencies = [
|
|
10
|
-
"pondsocket-common>=0.0.
|
|
10
|
+
"pondsocket-common>=0.0.4,<0.1.0",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
13
|
[project.optional-dependencies]
|
|
@@ -78,6 +78,15 @@ from .server import EndpointMatch, PondSocket
|
|
|
78
78
|
from .sse_transport import SSEServerTransport
|
|
79
79
|
from .store import Store
|
|
80
80
|
from .transport import OnCloseCallback, OnMessageHandler, PushableTransport, Transport
|
|
81
|
+
from .typed import (
|
|
82
|
+
TypedChannel,
|
|
83
|
+
TypedEventContext,
|
|
84
|
+
TypedEventHandler,
|
|
85
|
+
TypedJoinContext,
|
|
86
|
+
TypedJoinHandler,
|
|
87
|
+
TypedLobby,
|
|
88
|
+
typed_lobby,
|
|
89
|
+
)
|
|
81
90
|
from .types import (
|
|
82
91
|
AllExceptSender,
|
|
83
92
|
AllUsers,
|
|
@@ -176,6 +185,12 @@ __all__ = [
|
|
|
176
185
|
"TokenBucketRateLimiter",
|
|
177
186
|
"Transport",
|
|
178
187
|
"TransportType",
|
|
188
|
+
"TypedChannel",
|
|
189
|
+
"TypedEventContext",
|
|
190
|
+
"TypedEventHandler",
|
|
191
|
+
"TypedJoinContext",
|
|
192
|
+
"TypedJoinHandler",
|
|
193
|
+
"TypedLobby",
|
|
179
194
|
"User",
|
|
180
195
|
"UserData",
|
|
181
196
|
"__version__",
|
|
@@ -197,6 +212,7 @@ __all__ = [
|
|
|
197
212
|
"parse_inbound_text",
|
|
198
213
|
"pubsub_bytes_to_event",
|
|
199
214
|
"too_many_requests",
|
|
215
|
+
"typed_lobby",
|
|
200
216
|
"unauthorized",
|
|
201
217
|
"uuid",
|
|
202
218
|
"wire_to_event",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
from collections.abc import Awaitable, Callable
|
|
5
6
|
from dataclasses import dataclass, field
|
|
6
7
|
from typing import Any, TypeAlias
|
|
@@ -128,6 +129,9 @@ class Channel:
|
|
|
128
129
|
self._active = False
|
|
129
130
|
if self._dispatch_task is not None and not self._dispatch_task.done():
|
|
130
131
|
self._dispatch_task.cancel()
|
|
132
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
133
|
+
await self._dispatch_task
|
|
134
|
+
self._dispatch_task = None
|
|
131
135
|
await self._unsubscribe_from_pubsub()
|
|
132
136
|
self._fire_metric_channel_destroyed()
|
|
133
137
|
await self._fire_on_destroy()
|
|
@@ -164,6 +168,7 @@ class Channel:
|
|
|
164
168
|
self, user_id: str, reason: str = LeaveReason.EXPLICIT_LEAVE.value
|
|
165
169
|
) -> None:
|
|
166
170
|
if not await self._connections.discard(user_id):
|
|
171
|
+
await self._publish_user_command("user:remove", user_id, reason)
|
|
167
172
|
return
|
|
168
173
|
assigns = await self._assigns.get(user_id, {}) or {}
|
|
169
174
|
presence: PondPresence | None = None
|
|
@@ -210,6 +215,15 @@ class Channel:
|
|
|
210
215
|
current = dict(current)
|
|
211
216
|
current[key] = value
|
|
212
217
|
await self._assigns.update(user_id, current)
|
|
218
|
+
ev = Event(
|
|
219
|
+
action=InternalActions.ASSIGNS.value,
|
|
220
|
+
channel_name=self.name,
|
|
221
|
+
request_id=uuid(),
|
|
222
|
+
event="assigns:update",
|
|
223
|
+
payload={"userId": user_id, "key": key, "value": value, "assigns": dict(current)},
|
|
224
|
+
node_id=self.node_id,
|
|
225
|
+
)
|
|
226
|
+
await self._publish_event_to_pubsub(ev, "assigns:update")
|
|
213
227
|
|
|
214
228
|
async def get_assigns(self) -> dict[str, PondAssigns]:
|
|
215
229
|
snap = await self._assigns.snapshot()
|
|
@@ -228,6 +242,9 @@ class Channel:
|
|
|
228
242
|
await self._presence.untrack(user_id)
|
|
229
243
|
|
|
230
244
|
async def evict_user(self, user_id: str, reason: str) -> None:
|
|
245
|
+
if not await self._connections.has(user_id):
|
|
246
|
+
await self._publish_user_command("user:evict", user_id, reason)
|
|
247
|
+
return
|
|
231
248
|
ev = Event(
|
|
232
249
|
action=ServerActions.SYSTEM.value,
|
|
233
250
|
channel_name=self.name,
|
|
@@ -363,6 +380,7 @@ class Channel:
|
|
|
363
380
|
event=event_type.value,
|
|
364
381
|
payload={"presence": presence_snapshot, "changed": changed},
|
|
365
382
|
node_id=self.node_id,
|
|
383
|
+
user_id=changed_user_id,
|
|
366
384
|
)
|
|
367
385
|
recipients = await self._presence.keys()
|
|
368
386
|
if recipients:
|
|
@@ -395,6 +413,15 @@ class Channel:
|
|
|
395
413
|
payload=payload,
|
|
396
414
|
node_id=self.node_id,
|
|
397
415
|
)
|
|
416
|
+
match recipients:
|
|
417
|
+
case AllUsers():
|
|
418
|
+
ev.recipient_descriptor = "ALL_USERS"
|
|
419
|
+
case AllExceptSender(sender_id=sender):
|
|
420
|
+
ev.recipient_descriptor = "ALL_EXCEPT_SENDER"
|
|
421
|
+
ev.from_user_id = sender
|
|
422
|
+
case ToUsers(user_ids=ids):
|
|
423
|
+
ev.recipient_descriptor = list(ids)
|
|
424
|
+
ev.recipients = list(ids)
|
|
398
425
|
resolved = await self._resolve_recipients(recipients)
|
|
399
426
|
if resolved:
|
|
400
427
|
await self._enqueue_dispatch(ev, resolved)
|
|
@@ -515,15 +542,98 @@ class Channel:
|
|
|
515
542
|
if not self._active:
|
|
516
543
|
return
|
|
517
544
|
if event.action == ServerActions.BROADCAST.value:
|
|
518
|
-
recipients = await self.
|
|
545
|
+
recipients = await self._resolve_distributed_recipients(event)
|
|
519
546
|
if recipients:
|
|
520
547
|
await self._enqueue_dispatch(event, recipients)
|
|
521
548
|
return
|
|
522
549
|
if event.action == ServerActions.PRESENCE.value:
|
|
550
|
+
await self._merge_remote_presence(event)
|
|
523
551
|
recipients = await self._presence.keys()
|
|
524
552
|
if recipients:
|
|
525
553
|
await self._enqueue_dispatch(event, recipients)
|
|
526
554
|
return
|
|
555
|
+
if event.action == InternalActions.ASSIGNS.value:
|
|
556
|
+
await self._handle_remote_assigns(event)
|
|
557
|
+
return
|
|
558
|
+
if event.action == InternalActions.USER_COMMAND.value:
|
|
559
|
+
await self._handle_remote_user_command(event)
|
|
560
|
+
return
|
|
561
|
+
|
|
562
|
+
async def _resolve_distributed_recipients(self, event: Event) -> list[str]:
|
|
563
|
+
local = await self._connections.keys()
|
|
564
|
+
descriptor = event.recipient_descriptor
|
|
565
|
+
if isinstance(descriptor, list):
|
|
566
|
+
wanted = {str(item) for item in descriptor}
|
|
567
|
+
return [uid for uid in local if uid in wanted]
|
|
568
|
+
if descriptor == "ALL_EXCEPT_SENDER":
|
|
569
|
+
return [uid for uid in local if uid != event.from_user_id]
|
|
570
|
+
return local
|
|
571
|
+
|
|
572
|
+
async def _merge_remote_presence(self, event: Event) -> None:
|
|
573
|
+
if not isinstance(event.payload, dict):
|
|
574
|
+
return
|
|
575
|
+
changed = event.payload.get("changed")
|
|
576
|
+
user_id = ""
|
|
577
|
+
if isinstance(changed, dict):
|
|
578
|
+
raw_id = event.user_id or changed.get("userId") or changed.get("id")
|
|
579
|
+
user_id = str(raw_id) if raw_id else ""
|
|
580
|
+
if not user_id:
|
|
581
|
+
return
|
|
582
|
+
if event.event == PresenceEventTypes.LEAVE.value:
|
|
583
|
+
await self._presence.discard(user_id)
|
|
584
|
+
return
|
|
585
|
+
if isinstance(changed, dict):
|
|
586
|
+
presence = {k: v for k, v in changed.items() if k not in {"userId", "id"}}
|
|
587
|
+
if not presence:
|
|
588
|
+
presence = dict(changed)
|
|
589
|
+
await self._presence.set_remote(user_id, presence)
|
|
590
|
+
|
|
591
|
+
async def _handle_remote_assigns(self, event: Event) -> None:
|
|
592
|
+
if not isinstance(event.payload, dict):
|
|
593
|
+
return
|
|
594
|
+
user_id = str(event.payload.get("userId") or "")
|
|
595
|
+
key = event.payload.get("key")
|
|
596
|
+
if not user_id or not isinstance(key, str):
|
|
597
|
+
remote_assigns = event.payload.get("assigns")
|
|
598
|
+
if user_id and isinstance(remote_assigns, dict) and await self._assigns.has(user_id):
|
|
599
|
+
await self._assigns.update(user_id, dict(remote_assigns))
|
|
600
|
+
return
|
|
601
|
+
if not await self._assigns.has(user_id):
|
|
602
|
+
return
|
|
603
|
+
current = await self._assigns.get(user_id, {}) or {}
|
|
604
|
+
updated = dict(current)
|
|
605
|
+
updated[key] = event.payload.get("value")
|
|
606
|
+
await self._assigns.update(user_id, updated)
|
|
607
|
+
|
|
608
|
+
async def _handle_remote_user_command(self, event: Event) -> None:
|
|
609
|
+
if not isinstance(event.payload, dict):
|
|
610
|
+
return
|
|
611
|
+
user_id = str(event.payload.get("userId") or "")
|
|
612
|
+
reason = str(event.payload.get("reason") or "remote command")
|
|
613
|
+
if not user_id or not await self._connections.has(user_id):
|
|
614
|
+
return
|
|
615
|
+
if event.event == "user:evict":
|
|
616
|
+
ev = Event(
|
|
617
|
+
action=ServerActions.SYSTEM.value,
|
|
618
|
+
channel_name=self.name,
|
|
619
|
+
request_id=uuid(),
|
|
620
|
+
event="EVICTED",
|
|
621
|
+
payload={"reason": reason},
|
|
622
|
+
node_id=self.node_id,
|
|
623
|
+
)
|
|
624
|
+
await self._send_direct([user_id], ev)
|
|
625
|
+
await self.remove_user(user_id, reason=reason)
|
|
626
|
+
|
|
627
|
+
async def _publish_user_command(self, command: str, user_id: str, reason: str) -> None:
|
|
628
|
+
ev = Event(
|
|
629
|
+
action=InternalActions.USER_COMMAND.value,
|
|
630
|
+
channel_name=self.name,
|
|
631
|
+
request_id=uuid(),
|
|
632
|
+
event=command,
|
|
633
|
+
payload={"userId": user_id, "reason": reason},
|
|
634
|
+
node_id=self.node_id,
|
|
635
|
+
)
|
|
636
|
+
await self._publish_event_to_pubsub(ev, command)
|
|
527
637
|
|
|
528
638
|
def _fire_metric_channel_created(self) -> None:
|
|
529
639
|
hooks = self._options.hooks
|
|
@@ -622,23 +732,44 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
622
732
|
if message_type == "USER_MESSAGE":
|
|
623
733
|
envelope.update(
|
|
624
734
|
{
|
|
625
|
-
"fromUserId":
|
|
735
|
+
"fromUserId": event.from_user_id,
|
|
626
736
|
"event": event.event,
|
|
627
737
|
"payload": event.payload if event.payload is not None else {},
|
|
628
738
|
"requestId": event.request_id,
|
|
629
|
-
"recipientDescriptor":
|
|
739
|
+
"recipientDescriptor": event.recipient_descriptor,
|
|
630
740
|
}
|
|
631
741
|
)
|
|
632
742
|
elif message_type == "PRESENCE_UPDATE":
|
|
633
743
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
634
744
|
envelope["event"] = event.event
|
|
635
|
-
|
|
745
|
+
changed = payload.get("changed", {})
|
|
746
|
+
if event.user_id:
|
|
747
|
+
envelope["userId"] = event.user_id
|
|
748
|
+
elif isinstance(changed, dict):
|
|
749
|
+
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
750
|
+
else:
|
|
751
|
+
envelope["userId"] = ""
|
|
636
752
|
envelope["presence"] = payload.get("changed", {})
|
|
753
|
+
envelope["payload"] = payload
|
|
637
754
|
elif message_type == "PRESENCE_REMOVED":
|
|
638
|
-
|
|
755
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
756
|
+
changed = payload.get("changed", {})
|
|
757
|
+
if event.user_id:
|
|
758
|
+
envelope["userId"] = event.user_id
|
|
759
|
+
elif isinstance(changed, dict):
|
|
760
|
+
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
761
|
+
else:
|
|
762
|
+
envelope["userId"] = ""
|
|
763
|
+
envelope["payload"] = payload
|
|
639
764
|
elif message_type == "ASSIGNS_UPDATE":
|
|
640
|
-
|
|
641
|
-
envelope["
|
|
765
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
766
|
+
envelope["userId"] = payload.get("userId", "")
|
|
767
|
+
if "key" in payload:
|
|
768
|
+
envelope["key"] = payload.get("key")
|
|
769
|
+
if "value" in payload:
|
|
770
|
+
envelope["value"] = payload.get("value")
|
|
771
|
+
envelope["assigns"] = payload.get("assigns", {})
|
|
772
|
+
envelope["payload"] = payload
|
|
642
773
|
elif message_type in {"EVICT_USER", "USER_REMOVE"}:
|
|
643
774
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
644
775
|
envelope["userId"] = payload.get("userId", "")
|
|
@@ -666,6 +797,9 @@ def _distributed_bytes_to_event(data: bytes) -> Event | None:
|
|
|
666
797
|
return None
|
|
667
798
|
request_id = str(raw.get("requestId") or uuid())
|
|
668
799
|
if message_type == "USER_MESSAGE":
|
|
800
|
+
descriptor = raw.get("recipientDescriptor") or "ALL_USERS"
|
|
801
|
+
if isinstance(descriptor, list):
|
|
802
|
+
descriptor = [str(item) for item in descriptor]
|
|
669
803
|
return Event(
|
|
670
804
|
action=ServerActions.BROADCAST.value,
|
|
671
805
|
channel_name=channel_name,
|
|
@@ -673,32 +807,48 @@ def _distributed_bytes_to_event(data: bytes) -> Event | None:
|
|
|
673
807
|
event=str(raw.get("event") or ""),
|
|
674
808
|
payload=raw.get("payload") if raw.get("payload") is not None else {},
|
|
675
809
|
node_id=node_id,
|
|
810
|
+
recipients=descriptor if isinstance(descriptor, list) else [],
|
|
811
|
+
recipient_descriptor=descriptor,
|
|
812
|
+
from_user_id=str(raw.get("fromUserId") or "CHANNEL"),
|
|
676
813
|
)
|
|
677
814
|
if message_type == "PRESENCE_UPDATE":
|
|
815
|
+
payload = raw.get("payload") if isinstance(raw.get("payload"), dict) else None
|
|
678
816
|
return Event(
|
|
679
817
|
action=ServerActions.PRESENCE.value,
|
|
680
818
|
channel_name=channel_name,
|
|
681
819
|
request_id=request_id,
|
|
682
820
|
event=str(raw.get("event") or PresenceEventTypes.UPDATE.value),
|
|
683
|
-
payload={"presence": [], "changed": raw.get("presence") or {}},
|
|
821
|
+
payload=payload or {"presence": [], "changed": raw.get("presence") or {}},
|
|
684
822
|
node_id=node_id,
|
|
823
|
+
user_id=str(raw.get("userId") or ""),
|
|
685
824
|
)
|
|
686
825
|
if message_type == "PRESENCE_REMOVED":
|
|
826
|
+
payload = raw.get("payload") if isinstance(raw.get("payload"), dict) else None
|
|
687
827
|
return Event(
|
|
688
828
|
action=ServerActions.PRESENCE.value,
|
|
689
829
|
channel_name=channel_name,
|
|
690
830
|
request_id=request_id,
|
|
691
831
|
event=PresenceEventTypes.LEAVE.value,
|
|
692
|
-
payload={"presence": [], "changed": {"userId": raw.get("userId") or ""}},
|
|
832
|
+
payload=payload or {"presence": [], "changed": {"userId": raw.get("userId") or ""}},
|
|
693
833
|
node_id=node_id,
|
|
834
|
+
user_id=str(raw.get("userId") or ""),
|
|
694
835
|
)
|
|
695
836
|
if message_type == "ASSIGNS_UPDATE":
|
|
837
|
+
payload = raw.get("payload") if isinstance(raw.get("payload"), dict) else None
|
|
838
|
+
assigns_payload = {
|
|
839
|
+
"userId": raw.get("userId") or "",
|
|
840
|
+
"assigns": raw.get("assigns") or {},
|
|
841
|
+
}
|
|
842
|
+
if "key" in raw:
|
|
843
|
+
assigns_payload["key"] = raw.get("key")
|
|
844
|
+
if "value" in raw:
|
|
845
|
+
assigns_payload["value"] = raw.get("value")
|
|
696
846
|
return Event(
|
|
697
847
|
action=InternalActions.ASSIGNS.value,
|
|
698
848
|
channel_name=channel_name,
|
|
699
849
|
request_id=request_id,
|
|
700
850
|
event="assigns:update",
|
|
701
|
-
payload=
|
|
851
|
+
payload=payload or assigns_payload,
|
|
702
852
|
node_id=node_id,
|
|
703
853
|
)
|
|
704
854
|
if message_type in {"EVICT_USER", "USER_REMOVE"}:
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import asyncio
|
|
3
4
|
import time
|
|
4
5
|
from collections.abc import Awaitable, Callable
|
|
5
6
|
from dataclasses import dataclass
|
|
6
|
-
from typing import TypeAlias
|
|
7
|
+
from typing import TypeAlias, TypeVar
|
|
7
8
|
|
|
8
|
-
from pondsocket_common import ClientActions, IncomingConnection, ServerActions, uuid
|
|
9
|
+
from pondsocket_common import ClientActions, IncomingConnection, PondSchema, ServerActions, uuid
|
|
9
10
|
|
|
10
11
|
from .channel import Channel
|
|
11
12
|
from .contexts.connection_context import ConnectionContext, ConnectionDecision
|
|
@@ -17,6 +18,7 @@ from .parser import parse
|
|
|
17
18
|
from .pubsub import PubSub
|
|
18
19
|
from .store import Store
|
|
19
20
|
from .transport import Transport
|
|
21
|
+
from .typed import TypedJoinContext, TypedJoinHandler, TypedLobby, typed_lobby
|
|
20
22
|
from .types import (
|
|
21
23
|
Event,
|
|
22
24
|
LeaveReason,
|
|
@@ -34,6 +36,9 @@ JoinHandler: TypeAlias = Callable[[JoinContext], Awaitable[None]]
|
|
|
34
36
|
JoinMiddlewareFn: TypeAlias = Callable[
|
|
35
37
|
[JoinContext, Callable[[], Awaitable[None]]], Awaitable[None]
|
|
36
38
|
]
|
|
39
|
+
PresenceT = TypeVar("PresenceT")
|
|
40
|
+
AssignsT = TypeVar("AssignsT")
|
|
41
|
+
JoinParamsT = TypeVar("JoinParamsT")
|
|
37
42
|
|
|
38
43
|
|
|
39
44
|
@dataclass(slots=True)
|
|
@@ -54,6 +59,7 @@ class Endpoint:
|
|
|
54
59
|
"_options",
|
|
55
60
|
"_path",
|
|
56
61
|
"_pubsub",
|
|
62
|
+
"_registration_lock",
|
|
57
63
|
)
|
|
58
64
|
|
|
59
65
|
def __init__(
|
|
@@ -73,6 +79,7 @@ class Endpoint:
|
|
|
73
79
|
self._connections: Store[Transport] = Store()
|
|
74
80
|
self._channel_registrations: list[_ChannelRegistration] = []
|
|
75
81
|
self._connect_times: dict[str, float] = {}
|
|
82
|
+
self._registration_lock = asyncio.Lock()
|
|
76
83
|
|
|
77
84
|
@property
|
|
78
85
|
def path(self) -> str:
|
|
@@ -103,6 +110,18 @@ class Endpoint:
|
|
|
103
110
|
)
|
|
104
111
|
return lobby
|
|
105
112
|
|
|
113
|
+
def create_typed_channel(
|
|
114
|
+
self,
|
|
115
|
+
schema: PondSchema[PresenceT, AssignsT, JoinParamsT],
|
|
116
|
+
pattern: str,
|
|
117
|
+
handler: TypedJoinHandler[PresenceT, AssignsT, JoinParamsT],
|
|
118
|
+
) -> TypedLobby[PresenceT, AssignsT]:
|
|
119
|
+
async def wrapped(ctx: JoinContext) -> None:
|
|
120
|
+
await handler(TypedJoinContext(ctx))
|
|
121
|
+
|
|
122
|
+
lobby = self.create_channel(pattern, wrapped)
|
|
123
|
+
return typed_lobby(lobby, schema)
|
|
124
|
+
|
|
106
125
|
async def request_connection(
|
|
107
126
|
self,
|
|
108
127
|
incoming: IncomingConnection,
|
|
@@ -126,9 +145,10 @@ class Endpoint:
|
|
|
126
145
|
return ctx
|
|
127
146
|
|
|
128
147
|
async def register_transport(self, transport: Transport) -> None:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
148
|
+
async with self._registration_lock:
|
|
149
|
+
await self._check_max_connections()
|
|
150
|
+
await self._connections.create(transport.get_id(), transport)
|
|
151
|
+
self._connect_times[transport.get_id()] = time.monotonic()
|
|
132
152
|
await self._send_connect_event(transport)
|
|
133
153
|
transport.on_close(self._on_transport_closed)
|
|
134
154
|
transport.on_message(self._handle_transport_message)
|
|
@@ -212,6 +232,8 @@ class Endpoint:
|
|
|
212
232
|
await ctx.decline(401, "Join handler did not respond")
|
|
213
233
|
if ctx.is_accepted:
|
|
214
234
|
await self._fire_after_join(transport, channel_name)
|
|
235
|
+
elif await channel.user_count() == 0:
|
|
236
|
+
await channel.close()
|
|
215
237
|
return
|
|
216
238
|
await self._send_not_found(transport, event)
|
|
217
239
|
|
|
@@ -100,6 +100,14 @@ class PresenceClient:
|
|
|
100
100
|
async with self._lock:
|
|
101
101
|
return list(self._store.keys())
|
|
102
102
|
|
|
103
|
+
async def set_remote(self, user_id: str, data: PondPresence) -> None:
|
|
104
|
+
async with self._lock:
|
|
105
|
+
self._store[user_id] = data
|
|
106
|
+
|
|
107
|
+
async def discard(self, user_id: str) -> None:
|
|
108
|
+
async with self._lock:
|
|
109
|
+
self._store.pop(user_id, None)
|
|
110
|
+
|
|
103
111
|
async def values(self) -> list[PondPresence]:
|
|
104
112
|
async with self._lock:
|
|
105
113
|
return [dict(v) for v in self._store.values()]
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
from dataclasses import dataclass
|
|
5
6
|
|
|
6
7
|
from .errors import not_found
|
|
@@ -59,6 +60,8 @@ class LocalPubSub:
|
|
|
59
60
|
subs = self._subs.pop(pattern)
|
|
60
61
|
for sub in subs:
|
|
61
62
|
sub.task.cancel()
|
|
63
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
64
|
+
await sub.task
|
|
62
65
|
|
|
63
66
|
async def publish(self, topic: str, data: bytes) -> None:
|
|
64
67
|
if self._closed:
|
|
@@ -83,3 +86,6 @@ class LocalPubSub:
|
|
|
83
86
|
self._subs.clear()
|
|
84
87
|
for sub in all_subs:
|
|
85
88
|
sub.task.cancel()
|
|
89
|
+
for sub in all_subs:
|
|
90
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
91
|
+
await sub.task
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Awaitable, Callable
|
|
4
|
+
from typing import Any, Generic, TypeVar, cast
|
|
5
|
+
|
|
6
|
+
from pondsocket_common import PondEvent, PondSchema, to_pond_object
|
|
7
|
+
|
|
8
|
+
from .channel import Channel
|
|
9
|
+
from .contexts.event_context import EventContext
|
|
10
|
+
from .contexts.join_context import JoinContext
|
|
11
|
+
from .lobby import Lobby
|
|
12
|
+
from .types import User
|
|
13
|
+
|
|
14
|
+
PayloadT = TypeVar("PayloadT")
|
|
15
|
+
ResponseT = TypeVar("ResponseT")
|
|
16
|
+
PresenceT = TypeVar("PresenceT")
|
|
17
|
+
AssignsT = TypeVar("AssignsT")
|
|
18
|
+
JoinParamsT = TypeVar("JoinParamsT")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class TypedChannel(Generic[PresenceT, AssignsT]):
|
|
22
|
+
__slots__ = ("_raw",)
|
|
23
|
+
|
|
24
|
+
def __init__(self, raw: Channel) -> None:
|
|
25
|
+
self._raw = raw
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def raw(self) -> Channel:
|
|
29
|
+
return self._raw
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def name(self) -> str:
|
|
33
|
+
return self._raw.name
|
|
34
|
+
|
|
35
|
+
async def broadcast(
|
|
36
|
+
self,
|
|
37
|
+
event: PondEvent[PayloadT, ResponseT],
|
|
38
|
+
payload: PayloadT,
|
|
39
|
+
) -> None:
|
|
40
|
+
await self._raw.broadcast(event.name, to_pond_object(payload))
|
|
41
|
+
|
|
42
|
+
async def broadcast_to(
|
|
43
|
+
self,
|
|
44
|
+
event: PondEvent[PayloadT, ResponseT],
|
|
45
|
+
payload: PayloadT,
|
|
46
|
+
*user_ids: str,
|
|
47
|
+
) -> None:
|
|
48
|
+
await self._raw.broadcast_to(event.name, to_pond_object(payload), *user_ids)
|
|
49
|
+
|
|
50
|
+
async def broadcast_from(
|
|
51
|
+
self,
|
|
52
|
+
event: PondEvent[PayloadT, ResponseT],
|
|
53
|
+
payload: PayloadT,
|
|
54
|
+
sender_id: str,
|
|
55
|
+
) -> None:
|
|
56
|
+
await self._raw.broadcast_from(event.name, to_pond_object(payload), sender_id)
|
|
57
|
+
|
|
58
|
+
async def track_presence(self, user_id: str, presence: PresenceT) -> None:
|
|
59
|
+
await self._raw.track_presence(user_id, to_pond_object(presence))
|
|
60
|
+
|
|
61
|
+
async def update_presence(self, user_id: str, presence: PresenceT) -> None:
|
|
62
|
+
await self._raw.update_presence(user_id, to_pond_object(presence))
|
|
63
|
+
|
|
64
|
+
async def get_presence(self) -> dict[str, PresenceT]:
|
|
65
|
+
return cast(dict[str, PresenceT], await self._raw.get_presence())
|
|
66
|
+
|
|
67
|
+
async def get_assigns(self) -> dict[str, AssignsT]:
|
|
68
|
+
return cast(dict[str, AssignsT], await self._raw.get_assigns())
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class TypedJoinContext(Generic[PresenceT, AssignsT, JoinParamsT]):
|
|
72
|
+
__slots__ = ("_raw",)
|
|
73
|
+
|
|
74
|
+
def __init__(self, raw: JoinContext) -> None:
|
|
75
|
+
self._raw = raw
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def raw(self) -> JoinContext:
|
|
79
|
+
return self._raw
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def join_params(self) -> JoinParamsT:
|
|
83
|
+
return cast(JoinParamsT, self._raw.join_params)
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def user_id(self) -> str:
|
|
87
|
+
return self._raw.transport.get_id()
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def channel(self) -> TypedChannel[PresenceT, AssignsT]:
|
|
91
|
+
return TypedChannel(self._raw.channel)
|
|
92
|
+
|
|
93
|
+
async def accept(self, assigns: AssignsT | None = None) -> TypedJoinContext[PresenceT, AssignsT, JoinParamsT]:
|
|
94
|
+
await self._raw.accept(to_pond_object(assigns))
|
|
95
|
+
return self
|
|
96
|
+
|
|
97
|
+
async def decline(self, code: int, message: str) -> None:
|
|
98
|
+
await self._raw.decline(code, message)
|
|
99
|
+
|
|
100
|
+
async def reply(
|
|
101
|
+
self,
|
|
102
|
+
event: PondEvent[PayloadT, ResponseT],
|
|
103
|
+
payload: ResponseT,
|
|
104
|
+
) -> TypedJoinContext[PresenceT, AssignsT, JoinParamsT]:
|
|
105
|
+
await self._raw.reply(event.name, to_pond_object(payload))
|
|
106
|
+
return self
|
|
107
|
+
|
|
108
|
+
async def track(self, presence: PresenceT) -> TypedJoinContext[PresenceT, AssignsT, JoinParamsT]:
|
|
109
|
+
await self._raw.track(to_pond_object(presence))
|
|
110
|
+
return self
|
|
111
|
+
|
|
112
|
+
async def broadcast(
|
|
113
|
+
self,
|
|
114
|
+
event: PondEvent[PayloadT, ResponseT],
|
|
115
|
+
payload: PayloadT,
|
|
116
|
+
) -> TypedJoinContext[PresenceT, AssignsT, JoinParamsT]:
|
|
117
|
+
await self._raw.broadcast(event.name, to_pond_object(payload))
|
|
118
|
+
return self
|
|
119
|
+
|
|
120
|
+
async def assign(self, assigns: AssignsT) -> TypedJoinContext[PresenceT, AssignsT, JoinParamsT]:
|
|
121
|
+
await self._raw.assign(to_pond_object(assigns))
|
|
122
|
+
return self
|
|
123
|
+
|
|
124
|
+
async def get_all_presence(self) -> dict[str, PresenceT]:
|
|
125
|
+
return cast(dict[str, PresenceT], await self._raw.get_all_presence())
|
|
126
|
+
|
|
127
|
+
async def get_all_assigns(self) -> dict[str, AssignsT]:
|
|
128
|
+
return cast(dict[str, AssignsT], await self._raw.get_all_assigns())
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class TypedEventContext(Generic[PayloadT, PresenceT, AssignsT]):
|
|
132
|
+
__slots__ = ("_raw",)
|
|
133
|
+
|
|
134
|
+
def __init__(self, raw: EventContext) -> None:
|
|
135
|
+
self._raw = raw
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def raw(self) -> EventContext:
|
|
139
|
+
return self._raw
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def payload(self) -> PayloadT:
|
|
143
|
+
return cast(PayloadT, self._raw.get_payload())
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def event_name(self) -> str:
|
|
147
|
+
return self._raw.event_name
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def user(self) -> User:
|
|
151
|
+
return self._raw.user
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def assigns(self) -> AssignsT:
|
|
155
|
+
return cast(AssignsT, self._raw.user.assigns)
|
|
156
|
+
|
|
157
|
+
@property
|
|
158
|
+
def presence(self) -> PresenceT | None:
|
|
159
|
+
return cast(PresenceT | None, self._raw.user.presence)
|
|
160
|
+
|
|
161
|
+
@property
|
|
162
|
+
def channel(self) -> TypedChannel[PresenceT, AssignsT]:
|
|
163
|
+
return TypedChannel(self._raw.channel)
|
|
164
|
+
|
|
165
|
+
async def reply(
|
|
166
|
+
self,
|
|
167
|
+
event: PondEvent[Any, ResponseT],
|
|
168
|
+
payload: ResponseT,
|
|
169
|
+
) -> TypedEventContext[PayloadT, PresenceT, AssignsT]:
|
|
170
|
+
await self._raw.reply(event.name, to_pond_object(payload))
|
|
171
|
+
return self
|
|
172
|
+
|
|
173
|
+
async def broadcast(
|
|
174
|
+
self,
|
|
175
|
+
event: PondEvent[ResponseT, Any],
|
|
176
|
+
payload: ResponseT,
|
|
177
|
+
) -> TypedEventContext[PayloadT, PresenceT, AssignsT]:
|
|
178
|
+
await self._raw.broadcast(event.name, to_pond_object(payload))
|
|
179
|
+
return self
|
|
180
|
+
|
|
181
|
+
async def track(self, presence: PresenceT) -> TypedEventContext[PayloadT, PresenceT, AssignsT]:
|
|
182
|
+
await self._raw.track(to_pond_object(presence))
|
|
183
|
+
return self
|
|
184
|
+
|
|
185
|
+
async def update_presence(
|
|
186
|
+
self,
|
|
187
|
+
presence: PresenceT,
|
|
188
|
+
) -> TypedEventContext[PayloadT, PresenceT, AssignsT]:
|
|
189
|
+
await self._raw.update_presence(to_pond_object(presence))
|
|
190
|
+
return self
|
|
191
|
+
|
|
192
|
+
async def assign(self, assigns: AssignsT) -> TypedEventContext[PayloadT, PresenceT, AssignsT]:
|
|
193
|
+
await self._raw.assign(to_pond_object(assigns))
|
|
194
|
+
return self
|
|
195
|
+
|
|
196
|
+
async def get_all_presence(self) -> dict[str, PresenceT]:
|
|
197
|
+
return cast(dict[str, PresenceT], await self._raw.get_all_presence())
|
|
198
|
+
|
|
199
|
+
async def get_all_assigns(self) -> dict[str, AssignsT]:
|
|
200
|
+
return cast(dict[str, AssignsT], await self._raw.get_all_assigns())
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
TypedJoinHandler = Callable[[TypedJoinContext[PresenceT, AssignsT, JoinParamsT]], Awaitable[None]]
|
|
204
|
+
TypedEventHandler = Callable[[TypedEventContext[PayloadT, PresenceT, AssignsT]], Awaitable[None]]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
class TypedLobby(Generic[PresenceT, AssignsT]):
|
|
208
|
+
__slots__ = ("_raw",)
|
|
209
|
+
|
|
210
|
+
def __init__(self, raw: Lobby) -> None:
|
|
211
|
+
self._raw = raw
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def raw(self) -> Lobby:
|
|
215
|
+
return self._raw
|
|
216
|
+
|
|
217
|
+
def on(
|
|
218
|
+
self,
|
|
219
|
+
event: PondEvent[PayloadT, ResponseT],
|
|
220
|
+
handler: TypedEventHandler[PayloadT, PresenceT, AssignsT],
|
|
221
|
+
) -> None:
|
|
222
|
+
async def wrapped(ctx: EventContext) -> None:
|
|
223
|
+
await handler(TypedEventContext(ctx))
|
|
224
|
+
|
|
225
|
+
self._raw.on_message(event.name, wrapped)
|
|
226
|
+
|
|
227
|
+
async def get_channel(self, name: str) -> TypedChannel[PresenceT, AssignsT] | None:
|
|
228
|
+
channel = await self._raw.get_channel(name)
|
|
229
|
+
if channel is None:
|
|
230
|
+
return None
|
|
231
|
+
return TypedChannel(channel)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def typed_lobby(
|
|
235
|
+
raw: Lobby,
|
|
236
|
+
_schema: PondSchema[PresenceT, AssignsT, JoinParamsT],
|
|
237
|
+
) -> TypedLobby[PresenceT, AssignsT]:
|
|
238
|
+
return TypedLobby(raw)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
__all__ = [
|
|
242
|
+
"TypedChannel",
|
|
243
|
+
"TypedEventContext",
|
|
244
|
+
"TypedEventHandler",
|
|
245
|
+
"TypedJoinContext",
|
|
246
|
+
"TypedJoinHandler",
|
|
247
|
+
"TypedLobby",
|
|
248
|
+
"typed_lobby",
|
|
249
|
+
]
|
|
@@ -79,6 +79,10 @@ class Event:
|
|
|
79
79
|
event: str
|
|
80
80
|
payload: Any = None
|
|
81
81
|
node_id: str = ""
|
|
82
|
+
recipients: list[str] = field(default_factory=list)
|
|
83
|
+
recipient_descriptor: object = "ALL_USERS"
|
|
84
|
+
from_user_id: str = "CHANNEL"
|
|
85
|
+
user_id: str = ""
|
|
82
86
|
|
|
83
87
|
def __post_init__(self) -> None:
|
|
84
88
|
if not self.action:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|