latch-asgi 1.0.6.dev3__tar.gz → 1.1.0__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.
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: latch-asgi
3
- Version: 1.0.6.dev3
3
+ Version: 1.1.0
4
4
  Summary: ASGI python server
5
5
  Author-Email: Max Smolin <max@latch.bio>
6
6
  License: CC0-1.0
7
7
  Requires-Python: <4.0,>=3.11
8
8
  Requires-Dist: hypercorn[uvloop]<1.0.0,>=0.14.3
9
9
  Requires-Dist: latch-data-validation<1.0.0,>=0.1.3
10
- Requires-Dist: latch-o11y<1.0.0,>=0.1.4
10
+ Requires-Dist: latch-o11y<2.0.0,>=1.2.0
11
11
  Requires-Dist: latch-config<1.0.0,>=0.1.6
12
12
  Requires-Dist: PyJWT[crypto]<3.0.0,>=2.6.0
13
13
  Requires-Dist: orjson<4.0.0,>=3.8.5
@@ -0,0 +1,49 @@
1
+ from collections.abc import Awaitable, Callable
2
+ from dataclasses import dataclass
3
+ from typing import Any, Self, TypeAlias, TypeVar
4
+
5
+ from hypercorn.typing import WebsocketScope
6
+ from latch_o11y.o11y import dict_to_attrs, trace_app_function
7
+ from opentelemetry.trace import get_current_span
8
+
9
+ from ..asgi_iface import WebsocketReceiveCallable, WebsocketSendCallable
10
+ from ..framework.common import Headers
11
+ from ..framework.websocket import (
12
+ accept_connection,
13
+ receive_class_ext,
14
+ send_websocket_auto,
15
+ websocket_session_span_key,
16
+ )
17
+ from . import common
18
+
19
+ T = TypeVar("T")
20
+
21
+
22
+ @dataclass
23
+ class Context(
24
+ common.Context[WebsocketScope, WebsocketReceiveCallable, WebsocketSendCallable]
25
+ ):
26
+ _request_span_key = websocket_session_span_key
27
+
28
+ @trace_app_function
29
+ async def accept_connection(
30
+ self: Self, *, subprotocol: str | None = None, headers: Headers | None = None
31
+ ) -> None:
32
+ await accept_connection(self.send, subprotocol=subprotocol, headers=headers)
33
+
34
+ @trace_app_function
35
+ async def receive_message(self: Self, cls: type[T]) -> T:
36
+ json, res = await receive_class_ext(self.receive, cls)
37
+
38
+ get_current_span().set_attributes(dict_to_attrs(json, "payload"))
39
+
40
+ return res
41
+
42
+ @trace_app_function
43
+ async def send_message(self: Self, data: Any) -> None:
44
+ await send_websocket_auto(self.send, data)
45
+
46
+
47
+ HandlerResult = str
48
+ Handler: TypeAlias = Callable[[Context], Awaitable[HandlerResult]]
49
+ Route: TypeAlias = Handler
@@ -22,7 +22,6 @@ otel_header_whitelist = {
22
22
  "sec-gpc",
23
23
  "te",
24
24
  "upgrade-insecure-requests",
25
- "sec-websocket-extensions",
26
25
  "device-memory",
27
26
  "downlink",
28
27
  "dpr",
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "latch-asgi"
3
- version = "1.0.6.dev3"
3
+ version = "1.1.0"
4
4
  description = "ASGI python server"
5
5
  authors = [
6
6
  { name = "Max Smolin", email = "max@latch.bio" },
@@ -8,7 +8,7 @@ authors = [
8
8
  dependencies = [
9
9
  "hypercorn[uvloop]<1.0.0,>=0.14.3",
10
10
  "latch-data-validation<1.0.0,>=0.1.3",
11
- "latch-o11y<1.0.0,>=0.1.4",
11
+ "latch-o11y<2.0.0,>=1.2.0",
12
12
  "latch-config<1.0.0,>=0.1.6",
13
13
  "PyJWT[crypto]<3.0.0,>=2.6.0",
14
14
  "orjson<4.0.0,>=3.8.5",
@@ -1,93 +0,0 @@
1
- from collections.abc import Awaitable, Callable
2
- from dataclasses import dataclass
3
- from typing import Any, Self, TypeAlias, TypeVar
4
-
5
- from hypercorn.typing import WebsocketScope
6
- from latch_o11y.o11y import dict_to_attrs, trace_app_function
7
- from opentelemetry.trace import get_current_span
8
-
9
- from ..asgi_iface import WebsocketReceiveCallable, WebsocketSendCallable
10
- from ..framework.common import Headers
11
- from ..framework.websocket import (
12
- accept_connection,
13
- receive_class_ext,
14
- send_websocket_auto,
15
- websocket_session_span_key,
16
- )
17
- from . import common
18
-
19
- T = TypeVar("T")
20
-
21
-
22
- @dataclass
23
- class Context(
24
- common.Context[WebsocketScope, WebsocketReceiveCallable, WebsocketSendCallable]
25
- ):
26
- _request_span_key = websocket_session_span_key
27
-
28
- @trace_app_function
29
- async def accept_connection(
30
- self: Self,
31
- *,
32
- subprotocol: str | None = None,
33
- headers: Headers | None = None,
34
- negotiate_permessage_deflate: bool = False,
35
- ) -> None:
36
- if not negotiate_permessage_deflate:
37
- await accept_connection(self.send, subprotocol=subprotocol, headers=headers)
38
- return
39
-
40
- offer = self.header_str("sec-websocket-extensions")
41
-
42
- headers_out: Headers = {}
43
- if headers is not None:
44
- headers_out = dict(headers)
45
-
46
- def _has_extensions_header(h: Headers) -> bool:
47
- for k in h.keys():
48
- if isinstance(k, bytes):
49
- if k.decode("latin-1").lower() == "sec-websocket-extensions":
50
- return True
51
- else:
52
- if k.lower() == "sec-websocket-extensions":
53
- return True
54
- return False
55
-
56
- if (
57
- offer is not None
58
- and "permessage-deflate" in offer.replace(" ", "").lower()
59
- and not _has_extensions_header(headers_out)
60
- ):
61
- # Minimal negotiation: if the client offered client_max_window_bits without a value,
62
- # reply with a concrete value to satisfy browsers like Chrome.
63
- offer_no_space = offer.replace(" ", "").lower()
64
- if (
65
- "client_max_window_bits" in offer_no_space
66
- and "client_max_window_bits=" not in offer_no_space
67
- ):
68
- headers_out["Sec-WebSocket-Extensions"] = (
69
- "permessage-deflate; client_max_window_bits=15"
70
- )
71
- else:
72
- headers_out["Sec-WebSocket-Extensions"] = "permessage-deflate"
73
-
74
- await accept_connection(
75
- self.send, subprotocol=subprotocol, headers=headers_out or headers
76
- )
77
-
78
- @trace_app_function
79
- async def receive_message(self: Self, cls: type[T]) -> T:
80
- json, res = await receive_class_ext(self.receive, cls)
81
-
82
- get_current_span().set_attributes(dict_to_attrs(json, "payload"))
83
-
84
- return res
85
-
86
- @trace_app_function
87
- async def send_message(self: Self, data: Any) -> None:
88
- await send_websocket_auto(self.send, data)
89
-
90
-
91
- HandlerResult = str
92
- Handler: TypeAlias = Callable[[Context], Awaitable[HandlerResult]]
93
- Route: TypeAlias = Handler
File without changes
File without changes