webcompy-server 0.0.14__py3-none-any.whl
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.
- webcompy_server/__init__.py +91 -0
- webcompy_server/_asgi.py +832 -0
- webcompy_server/_context.py +106 -0
- webcompy_server/_html.py +925 -0
- webcompy_server/_prebuild.py +487 -0
- webcompy_server/_static_route.py +41 -0
- webcompy_server/_teleport_emission.py +94 -0
- webcompy_server/contrib/__init__.py +0 -0
- webcompy_server/contrib/fastapi.py +42 -0
- webcompy_server/ports/__init__.py +35 -0
- webcompy_server/ports/_async_scheduler.py +74 -0
- webcompy_server/ports/_cookie.py +154 -0
- webcompy_server/ports/_custom_element.py +72 -0
- webcompy_server/ports/_dom.py +380 -0
- webcompy_server/ports/_event_source.py +48 -0
- webcompy_server/ports/_fetch.py +290 -0
- webcompy_server/ports/_ffi.py +72 -0
- webcompy_server/ports/_history.py +47 -0
- webcompy_server/ports/_host.py +71 -0
- webcompy_server/ports/_media_query.py +27 -0
- webcompy_server/ports/_resource.py +280 -0
- webcompy_server/ports/_selector.py +238 -0
- webcompy_server/ports/_transition.py +75 -0
- webcompy_server/ports/_virtual_dom.py +590 -0
- webcompy_server/ports/_websocket.py +54 -0
- webcompy_server/py.typed +0 -0
- webcompy_server/rpc/__init__.py +8 -0
- webcompy_server/rpc/_dispatcher.py +424 -0
- webcompy_server/rpc/_streams.py +102 -0
- webcompy_server/rpc/_subscriptions.py +235 -0
- webcompy_server/rpc/_ws_endpoint.py +102 -0
- webcompy_server-0.0.14.dist-info/METADATA +45 -0
- webcompy_server-0.0.14.dist-info/RECORD +35 -0
- webcompy_server-0.0.14.dist-info/WHEEL +4 -0
- webcompy_server-0.0.14.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Server-side port implementations for WebComPy."""
|
|
2
|
+
|
|
3
|
+
from webcompy_server.ports._async_scheduler import ServerAsyncSchedulerPort
|
|
4
|
+
from webcompy_server.ports._cookie import ServerCookiePort
|
|
5
|
+
from webcompy_server.ports._custom_element import ServerCustomElementPort
|
|
6
|
+
from webcompy_server.ports._dom import ServerDOMPort
|
|
7
|
+
from webcompy_server.ports._event_source import ServerEventSourcePort
|
|
8
|
+
from webcompy_server.ports._fetch import ServerFetchPort
|
|
9
|
+
from webcompy_server.ports._ffi import ServerFFIPort
|
|
10
|
+
from webcompy_server.ports._history import ServerHistoryPort
|
|
11
|
+
from webcompy_server.ports._host import ServerHostPort
|
|
12
|
+
from webcompy_server.ports._media_query import ServerMediaQueryPort
|
|
13
|
+
from webcompy_server.ports._resource import InMemoryServerResourcePort, ServerResourcePort
|
|
14
|
+
from webcompy_server.ports._transition import ServerTransitionPort
|
|
15
|
+
from webcompy_server.ports._virtual_dom import VirtualDOMEvent, VirtualDOMNode
|
|
16
|
+
from webcompy_server.ports._websocket import ServerWebSocketPort
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"InMemoryServerResourcePort",
|
|
20
|
+
"ServerAsyncSchedulerPort",
|
|
21
|
+
"ServerCookiePort",
|
|
22
|
+
"ServerCustomElementPort",
|
|
23
|
+
"ServerDOMPort",
|
|
24
|
+
"ServerEventSourcePort",
|
|
25
|
+
"ServerFFIPort",
|
|
26
|
+
"ServerFetchPort",
|
|
27
|
+
"ServerHistoryPort",
|
|
28
|
+
"ServerHostPort",
|
|
29
|
+
"ServerMediaQueryPort",
|
|
30
|
+
"ServerResourcePort",
|
|
31
|
+
"ServerTransitionPort",
|
|
32
|
+
"ServerWebSocketPort",
|
|
33
|
+
"VirtualDOMEvent",
|
|
34
|
+
"VirtualDOMNode",
|
|
35
|
+
]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Server-side async scheduler port."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import logging
|
|
7
|
+
from collections.abc import Coroutine
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from webcompy.ports._async_scheduler import AsyncSchedulerPort
|
|
11
|
+
|
|
12
|
+
_logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
_MAX_DRAIN_ITERATIONS = 20
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ServerAsyncSchedulerPort(AsyncSchedulerPort):
|
|
18
|
+
"""Server-side async task scheduler for SSR.
|
|
19
|
+
|
|
20
|
+
Tracks scheduled tasks and drains them during ``await_pending``.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self) -> None:
|
|
24
|
+
self._registry: list[asyncio.Task[Any]] = []
|
|
25
|
+
|
|
26
|
+
def schedule(
|
|
27
|
+
self,
|
|
28
|
+
coro: Coroutine[Any, Any, Any],
|
|
29
|
+
*,
|
|
30
|
+
render: bool = False,
|
|
31
|
+
) -> asyncio.Task[Any]:
|
|
32
|
+
"""Schedule a coroutine for execution.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
coro: Coroutine to schedule.
|
|
36
|
+
render: Whether the task is render-scoped.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
Created ``asyncio.Task``.
|
|
40
|
+
|
|
41
|
+
"""
|
|
42
|
+
loop = asyncio.get_running_loop()
|
|
43
|
+
task = loop.create_task(coro)
|
|
44
|
+
self._registry.append(task)
|
|
45
|
+
task.add_done_callback(self._on_done)
|
|
46
|
+
return task
|
|
47
|
+
|
|
48
|
+
def _on_done(self, task: asyncio.Task[Any]) -> None:
|
|
49
|
+
if task in self._registry:
|
|
50
|
+
self._registry.remove(task)
|
|
51
|
+
|
|
52
|
+
async def await_pending(self, *, only_render: bool = False) -> None:
|
|
53
|
+
"""Wait for pending scheduled tasks to complete.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
only_render: If ``True``, wait only for render-scoped tasks.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
``None``.
|
|
60
|
+
|
|
61
|
+
"""
|
|
62
|
+
iteration = 0
|
|
63
|
+
while self._registry:
|
|
64
|
+
tasks = list(self._registry)
|
|
65
|
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
66
|
+
iteration += 1
|
|
67
|
+
if iteration > _MAX_DRAIN_ITERATIONS:
|
|
68
|
+
_logger.warning(
|
|
69
|
+
"ServerAsyncSchedulerPort.await_pending exceeded %d drain iterations; "
|
|
70
|
+
"possible recursive scheduling bug (%d tasks still registered)",
|
|
71
|
+
_MAX_DRAIN_ITERATIONS,
|
|
72
|
+
len(self._registry),
|
|
73
|
+
)
|
|
74
|
+
break
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Server-side cookie port."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import UTC, datetime
|
|
7
|
+
from email.utils import format_datetime
|
|
8
|
+
from http.cookies import SimpleCookie
|
|
9
|
+
from urllib.parse import unquote
|
|
10
|
+
|
|
11
|
+
from webcompy.ports._cookie import CookiePort
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class _PendingCookieWrite:
|
|
16
|
+
name: str
|
|
17
|
+
value: str
|
|
18
|
+
max_age: int | None = None
|
|
19
|
+
expires: datetime | None = None
|
|
20
|
+
path: str = "/"
|
|
21
|
+
domain: str | None = None
|
|
22
|
+
secure: bool = False
|
|
23
|
+
httponly: bool = False
|
|
24
|
+
samesite: str | None = None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ServerCookiePort(CookiePort):
|
|
28
|
+
"""Server-side cookie storage backed by request headers and pending writes.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
cookie_header: Raw ``Cookie`` header value from the request.
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, cookie_header: str = "") -> None:
|
|
36
|
+
self._cookies: dict[str, str] = {}
|
|
37
|
+
self._pending: dict[tuple[str, str], _PendingCookieWrite] = {}
|
|
38
|
+
if cookie_header:
|
|
39
|
+
for item in cookie_header.split("; "):
|
|
40
|
+
if "=" in item:
|
|
41
|
+
key, _, value = item.partition("=")
|
|
42
|
+
self._cookies[unquote(key)] = unquote(value)
|
|
43
|
+
|
|
44
|
+
def get(self, name: str) -> str | None:
|
|
45
|
+
"""Return the value of ``name`` if present.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
name: Cookie name.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
Cookie value or ``None`` when not set.
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
return self._cookies.get(name)
|
|
55
|
+
|
|
56
|
+
def set(
|
|
57
|
+
self,
|
|
58
|
+
name: str,
|
|
59
|
+
value: str,
|
|
60
|
+
*,
|
|
61
|
+
max_age: int | None = None,
|
|
62
|
+
expires: datetime | None = None,
|
|
63
|
+
path: str = "/",
|
|
64
|
+
domain: str | None = None,
|
|
65
|
+
secure: bool = False,
|
|
66
|
+
httponly: bool = False,
|
|
67
|
+
samesite: str | None = None,
|
|
68
|
+
) -> None:
|
|
69
|
+
"""Set a cookie and record a pending ``Set-Cookie`` header.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
name: Cookie name.
|
|
73
|
+
value: Cookie value.
|
|
74
|
+
max_age: Max-Age in seconds.
|
|
75
|
+
expires: Expiration datetime.
|
|
76
|
+
path: Cookie path.
|
|
77
|
+
domain: Cookie domain.
|
|
78
|
+
secure: Whether to set the ``Secure`` flag.
|
|
79
|
+
httponly: Whether to set the ``HttpOnly`` flag.
|
|
80
|
+
samesite: ``SameSite`` attribute value.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
``None``.
|
|
84
|
+
|
|
85
|
+
"""
|
|
86
|
+
self._cookies[name] = value
|
|
87
|
+
self._pending[(name, path)] = _PendingCookieWrite(
|
|
88
|
+
name=name,
|
|
89
|
+
value=value,
|
|
90
|
+
max_age=max_age,
|
|
91
|
+
expires=expires,
|
|
92
|
+
path=path,
|
|
93
|
+
domain=domain,
|
|
94
|
+
secure=secure,
|
|
95
|
+
httponly=httponly,
|
|
96
|
+
samesite=samesite,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def delete(self, name: str, path: str = "/") -> None:
|
|
100
|
+
"""Delete a cookie and record a clearing ``Set-Cookie`` header.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
name: Cookie name.
|
|
104
|
+
path: Cookie path.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
``None``.
|
|
108
|
+
|
|
109
|
+
"""
|
|
110
|
+
self._cookies.pop(name, None)
|
|
111
|
+
self._pending[(name, path)] = _PendingCookieWrite(name=name, value="", max_age=0, path=path)
|
|
112
|
+
|
|
113
|
+
def get_all(self) -> dict[str, str]:
|
|
114
|
+
"""Return a copy of all cookies.
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
Mapping of cookie names to values.
|
|
118
|
+
|
|
119
|
+
"""
|
|
120
|
+
return dict(self._cookies)
|
|
121
|
+
|
|
122
|
+
def get_pending_set_cookie_headers(self) -> list[str]:
|
|
123
|
+
"""Build pending ``Set-Cookie`` header strings.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
List of serialized ``Set-Cookie`` header values.
|
|
127
|
+
|
|
128
|
+
"""
|
|
129
|
+
headers: list[str] = []
|
|
130
|
+
for write in self._pending.values():
|
|
131
|
+
cookie = SimpleCookie()
|
|
132
|
+
cookie[write.name] = write.value
|
|
133
|
+
morsel = cookie[write.name]
|
|
134
|
+
if write.max_age is not None:
|
|
135
|
+
morsel["max-age"] = str(write.max_age)
|
|
136
|
+
if write.expires is not None:
|
|
137
|
+
expires_utc = (
|
|
138
|
+
write.expires.astimezone(UTC)
|
|
139
|
+
if write.expires.tzinfo is not None
|
|
140
|
+
else write.expires.replace(tzinfo=UTC)
|
|
141
|
+
)
|
|
142
|
+
morsel["expires"] = format_datetime(expires_utc, usegmt=True)
|
|
143
|
+
if write.path:
|
|
144
|
+
morsel["path"] = write.path
|
|
145
|
+
if write.domain:
|
|
146
|
+
morsel["domain"] = write.domain
|
|
147
|
+
if write.secure:
|
|
148
|
+
morsel["secure"] = True
|
|
149
|
+
if write.httponly:
|
|
150
|
+
morsel["httponly"] = True
|
|
151
|
+
if write.samesite:
|
|
152
|
+
morsel["samesite"] = write.samesite
|
|
153
|
+
headers.append(morsel.OutputString())
|
|
154
|
+
return headers
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Server-side custom element port."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
|
|
7
|
+
from webcompy.ports._custom_element import CustomElementBinding, CustomElementPort
|
|
8
|
+
from webcompy.ports._dom import DOMNode
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _ServerCustomElementBinding(CustomElementBinding):
|
|
12
|
+
def dispose(self) -> None:
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ServerCustomElementPort(CustomElementPort):
|
|
17
|
+
"""Server-side no-op custom element port."""
|
|
18
|
+
|
|
19
|
+
def ensure_defined(
|
|
20
|
+
self,
|
|
21
|
+
name: str,
|
|
22
|
+
observed_attributes: tuple[str, ...],
|
|
23
|
+
definition_key: str,
|
|
24
|
+
) -> None:
|
|
25
|
+
"""Ensure a custom element definition is registered.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
name: Tag name of the custom element.
|
|
29
|
+
observed_attributes: Attributes to observe.
|
|
30
|
+
definition_key: Deduplication key for the definition.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
``None``.
|
|
34
|
+
|
|
35
|
+
"""
|
|
36
|
+
pass
|
|
37
|
+
|
|
38
|
+
def bind(
|
|
39
|
+
self,
|
|
40
|
+
node: DOMNode,
|
|
41
|
+
*,
|
|
42
|
+
observed_attributes: tuple[str, ...],
|
|
43
|
+
on_connected: Callable[[], None],
|
|
44
|
+
on_disconnected: Callable[[], None],
|
|
45
|
+
on_attribute_changed: Callable[[str, str | None], None],
|
|
46
|
+
) -> CustomElementBinding:
|
|
47
|
+
"""Bind lifecycle callbacks to ``node``.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
node: DOM node to bind.
|
|
51
|
+
observed_attributes: Attributes to observe.
|
|
52
|
+
on_connected: Callback when the element connects.
|
|
53
|
+
on_disconnected: Callback when the element disconnects.
|
|
54
|
+
on_attribute_changed: Callback for attribute changes.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Binding handle whose ``dispose`` disconnects the callbacks.
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
return _ServerCustomElementBinding()
|
|
61
|
+
|
|
62
|
+
def is_document_connected(self, node: DOMNode) -> bool:
|
|
63
|
+
"""Return whether ``node`` is connected to the document.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
node: Node to check.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
``False`` on the server.
|
|
70
|
+
|
|
71
|
+
"""
|
|
72
|
+
return False
|