connect-dc 0.50.0__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.
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: connect-dc
|
|
3
|
+
Version: 0.50.0
|
|
4
|
+
Summary: Connect over WebRTC DataChannels — Connect-ES transports and the dc-connect mux runtime (browser + Node), via the grpc-dc runtime (Python)
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
# connect-dc (Python)
|
|
10
|
+
|
|
11
|
+
Connect over WebRTC DataChannels — Connect-ES transports and the dc-connect mux runtime (browser + Node), via the grpc-dc runtime (Python).
|
|
12
|
+
|
|
13
|
+
What this is: generated Connect **client transports and mux shims** — not a Connect handler implementation. To reach an existing Connect backend from browsers/peers, run connect-gateway; for the protocol implementation in-process, use dc-connect. Decision guide: https://github.com/browser-grpc/grpc-over-datachannels/blob/main/CONNECT.md ("Which Connect package do I need?").
|
|
14
|
+
|
|
15
|
+
Install: `pip install connect-dc`. The native core library (libdc_connect) ships with the dc-connect release binaries.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
grpcdc/__init__.py,sha256=SS6B3ibFydkSidwANI2Tb7XZQsVum_jqMrRkKR6UjEk,70
|
|
2
|
+
grpcdc/connectmux.py,sha256=Lv-Rl_8ubTDd7PbNZiusbrMVOnIf57YqEhDJDKav_YM,7412
|
|
3
|
+
connect_dc-0.50.0.dist-info/METADATA,sha256=QQUFXl_pELgI0hGKpFcaelNFzZaAT8Twx5F4p-r0Wcs,954
|
|
4
|
+
connect_dc-0.50.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
connect_dc-0.50.0.dist-info/top_level.txt,sha256=tFGcjs4laFCaKZJAgCsJWc4lfx_NcdgzKVtw9m9cbn8,7
|
|
6
|
+
connect_dc-0.50.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
grpcdc
|
grpcdc/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""connect-dc: Connect over WebRTC DataChannels (Python mux shim)."""
|
grpcdc/connectmux.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# @generated by grpc-dc codegen. DO NOT EDIT.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
# @generated by grpc-dc codegen. DO NOT EDIT.
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
"""Connect mux client for Python (ctypes, native only).
|
|
6
|
+
|
|
7
|
+
Generated shim over the dc-connect Rust core (C ABI via
|
|
8
|
+
crates/dc-connect-ffi): the same envelope/stream-mux/
|
|
9
|
+
status implementation the Rust gateway, the JS
|
|
10
|
+
connectmux runtime, and the Go shim speak. Frames are
|
|
11
|
+
wire-identical across languages — parity is asserted in
|
|
12
|
+
tests, not assumed.
|
|
13
|
+
|
|
14
|
+
The shared library is located via
|
|
15
|
+
GRPC_DC_CONNECT_LIB, else the usual library search.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import ctypes
|
|
19
|
+
import json
|
|
20
|
+
import os
|
|
21
|
+
|
|
22
|
+
_EVENT_KINDS = {1: "headers", 2: "data", 3: "end", 4: "reset", 5: "unknown"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _load_lib():
|
|
26
|
+
path = os.environ.get("GRPC_DC_CONNECT_LIB")
|
|
27
|
+
names = ("libdc_connect.so", "libdc_connect.dylib", "dc_connect.dll")
|
|
28
|
+
candidates = [path] if path else []
|
|
29
|
+
here = os.path.dirname(os.path.abspath(__file__))
|
|
30
|
+
candidates += [os.path.join(here, n) for n in names]
|
|
31
|
+
candidates += list(names)
|
|
32
|
+
last = None
|
|
33
|
+
for cand in candidates:
|
|
34
|
+
if not cand:
|
|
35
|
+
continue
|
|
36
|
+
try:
|
|
37
|
+
return ctypes.CDLL(cand)
|
|
38
|
+
except OSError as e:
|
|
39
|
+
last = e
|
|
40
|
+
raise OSError(f"libdc_connect not found (set GRPC_DC_CONNECT_LIB): {last}")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_lib = None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _lib_once():
|
|
47
|
+
global _lib
|
|
48
|
+
if _lib is None:
|
|
49
|
+
lib = _load_lib()
|
|
50
|
+
lib.dc_connect_mux_new.restype = ctypes.c_void_p
|
|
51
|
+
lib.dc_connect_mux_new.argtypes = [ctypes.c_uint8]
|
|
52
|
+
lib.dc_connect_mux_free.restype = None
|
|
53
|
+
lib.dc_connect_mux_free.argtypes = [ctypes.c_void_p]
|
|
54
|
+
for fn in (
|
|
55
|
+
"dc_connect_mux_data",
|
|
56
|
+
"dc_connect_mux_half_close",
|
|
57
|
+
):
|
|
58
|
+
f = getattr(lib, fn)
|
|
59
|
+
f.restype = ctypes.c_int64
|
|
60
|
+
lib.dc_connect_mux_data.argtypes = [
|
|
61
|
+
ctypes.c_void_p, ctypes.c_uint32, ctypes.c_char_p,
|
|
62
|
+
ctypes.c_size_t, ctypes.c_char_p, ctypes.c_size_t,
|
|
63
|
+
]
|
|
64
|
+
lib.dc_connect_mux_half_close.argtypes = [
|
|
65
|
+
ctypes.c_void_p, ctypes.c_uint32, ctypes.c_char_p, ctypes.c_size_t,
|
|
66
|
+
]
|
|
67
|
+
lib.dc_connect_mux_open_stream.restype = ctypes.c_int64
|
|
68
|
+
lib.dc_connect_mux_open_stream.argtypes = [
|
|
69
|
+
ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint32),
|
|
70
|
+
ctypes.c_char_p, ctypes.c_size_t,
|
|
71
|
+
]
|
|
72
|
+
lib.dc_connect_mux_end.restype = ctypes.c_int64
|
|
73
|
+
lib.dc_connect_mux_end.argtypes = [
|
|
74
|
+
ctypes.c_void_p, ctypes.c_uint32, ctypes.c_char_p,
|
|
75
|
+
ctypes.c_char_p, ctypes.c_size_t,
|
|
76
|
+
]
|
|
77
|
+
lib.dc_connect_mux_reset.restype = ctypes.c_int64
|
|
78
|
+
lib.dc_connect_mux_reset.argtypes = [
|
|
79
|
+
ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint8,
|
|
80
|
+
ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t,
|
|
81
|
+
]
|
|
82
|
+
lib.dc_connect_mux_on_close.restype = ctypes.c_int64
|
|
83
|
+
lib.dc_connect_mux_on_close.argtypes = [
|
|
84
|
+
ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32), ctypes.c_size_t,
|
|
85
|
+
]
|
|
86
|
+
lib.dc_connect_mux_on_message.restype = ctypes.c_int64
|
|
87
|
+
lib.dc_connect_mux_on_message.argtypes = [
|
|
88
|
+
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t,
|
|
89
|
+
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint8),
|
|
90
|
+
ctypes.c_char_p, ctypes.c_size_t,
|
|
91
|
+
]
|
|
92
|
+
lib.dc_connect_code_string.restype = ctypes.c_char_p
|
|
93
|
+
lib.dc_connect_code_string.argtypes = [ctypes.c_uint8]
|
|
94
|
+
lib.dc_connect_code_from_string.restype = ctypes.c_int32
|
|
95
|
+
lib.dc_connect_code_from_string.argtypes = [ctypes.c_char_p]
|
|
96
|
+
lib.dc_connect_code_http_status.restype = ctypes.c_uint16
|
|
97
|
+
lib.dc_connect_code_http_status.argtypes = [ctypes.c_uint8]
|
|
98
|
+
_lib = lib
|
|
99
|
+
return _lib
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _write_call(what, fn):
|
|
103
|
+
size = 64 * 1024
|
|
104
|
+
for _ in range(2):
|
|
105
|
+
buf = ctypes.create_string_buffer(size)
|
|
106
|
+
rc = fn(buf, size)
|
|
107
|
+
if rc == -2:
|
|
108
|
+
size = 16 * 1024 * 1024
|
|
109
|
+
continue
|
|
110
|
+
if rc < 0:
|
|
111
|
+
raise RuntimeError(f"dc_connect {what} failed: {rc}")
|
|
112
|
+
return buf.raw[:rc]
|
|
113
|
+
raise RuntimeError(f"dc_connect {what}: frame too large")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class MuxEvent:
|
|
117
|
+
def __init__(self, kind, stream, payload):
|
|
118
|
+
self.kind = kind
|
|
119
|
+
self.stream = stream
|
|
120
|
+
self.payload = payload
|
|
121
|
+
|
|
122
|
+
def __repr__(self):
|
|
123
|
+
return f"MuxEvent({self.kind!r}, stream={self.stream}, {len(self.payload)}B)"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class ConnectMux:
|
|
127
|
+
"""A dc-connect stream multiplexer handle."""
|
|
128
|
+
|
|
129
|
+
def __init__(self, client_side=True):
|
|
130
|
+
self._lib = _lib_once()
|
|
131
|
+
self._handle = self._lib.dc_connect_mux_new(1 if client_side else 0)
|
|
132
|
+
if not self._handle:
|
|
133
|
+
raise RuntimeError("dc_connect_mux_new failed")
|
|
134
|
+
|
|
135
|
+
def close(self):
|
|
136
|
+
if self._handle:
|
|
137
|
+
self._lib.dc_connect_mux_free(self._handle)
|
|
138
|
+
self._handle = None
|
|
139
|
+
|
|
140
|
+
def __enter__(self):
|
|
141
|
+
return self
|
|
142
|
+
|
|
143
|
+
def __exit__(self, *exc):
|
|
144
|
+
self.close()
|
|
145
|
+
|
|
146
|
+
def open_stream(self, path, kind="streaming", metadata=None):
|
|
147
|
+
doc = {"path": path, "kind": kind}
|
|
148
|
+
if metadata:
|
|
149
|
+
doc["metadata"] = metadata
|
|
150
|
+
raw = json.dumps(doc).encode()
|
|
151
|
+
stream = ctypes.c_uint32(0)
|
|
152
|
+
|
|
153
|
+
def call(buf, cap):
|
|
154
|
+
return self._lib.dc_connect_mux_open_stream(
|
|
155
|
+
self._handle, raw, ctypes.byref(stream), buf, cap)
|
|
156
|
+
|
|
157
|
+
frame = _write_call("open_stream", call)
|
|
158
|
+
return stream.value, frame
|
|
159
|
+
|
|
160
|
+
def data(self, stream, payload):
|
|
161
|
+
def call(buf, cap):
|
|
162
|
+
return self._lib.dc_connect_mux_data(
|
|
163
|
+
self._handle, stream, payload, len(payload), buf, cap)
|
|
164
|
+
|
|
165
|
+
return _write_call("data", call)
|
|
166
|
+
|
|
167
|
+
def half_close(self, stream):
|
|
168
|
+
def call(buf, cap):
|
|
169
|
+
return self._lib.dc_connect_mux_half_close(
|
|
170
|
+
self._handle, stream, buf, cap)
|
|
171
|
+
|
|
172
|
+
return _write_call("half_close", call)
|
|
173
|
+
|
|
174
|
+
def end(self, stream, end_json="{}"):
|
|
175
|
+
raw = end_json.encode()
|
|
176
|
+
|
|
177
|
+
def call(buf, cap):
|
|
178
|
+
return self._lib.dc_connect_mux_end(
|
|
179
|
+
self._handle, stream, raw, buf, cap)
|
|
180
|
+
|
|
181
|
+
return _write_call("end", call)
|
|
182
|
+
|
|
183
|
+
def reset(self, stream, code, message):
|
|
184
|
+
raw = message.encode() if isinstance(message, str) else message
|
|
185
|
+
|
|
186
|
+
def call(buf, cap):
|
|
187
|
+
return self._lib.dc_connect_mux_reset(
|
|
188
|
+
self._handle, stream, code, raw, buf, cap)
|
|
189
|
+
|
|
190
|
+
return _write_call("reset", call)
|
|
191
|
+
|
|
192
|
+
def on_close(self):
|
|
193
|
+
ids = (ctypes.c_uint32 * 1024)()
|
|
194
|
+
n = self._lib.dc_connect_mux_on_close(self._handle, ids, 1024)
|
|
195
|
+
if n < 0:
|
|
196
|
+
raise RuntimeError(f"dc_connect on_close failed: {n}")
|
|
197
|
+
return list(ids[:n])
|
|
198
|
+
|
|
199
|
+
def on_message(self, frame):
|
|
200
|
+
stream = ctypes.c_uint32(0)
|
|
201
|
+
kind = ctypes.c_uint8(0)
|
|
202
|
+
|
|
203
|
+
def call(buf, cap):
|
|
204
|
+
return self._lib.dc_connect_mux_on_message(
|
|
205
|
+
self._handle, frame, len(frame),
|
|
206
|
+
ctypes.byref(stream), ctypes.byref(kind), buf, cap)
|
|
207
|
+
|
|
208
|
+
payload = _write_call("on_message", call)
|
|
209
|
+
return MuxEvent(_EVENT_KINDS.get(kind.value, "unknown"), stream.value, payload)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def code_string(code):
|
|
213
|
+
return _lib_once().dc_connect_code_string(code).decode()
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def code_from_string(s):
|
|
217
|
+
return _lib_once().dc_connect_code_from_string(s.encode())
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def code_http_status(code):
|
|
221
|
+
return _lib_once().dc_connect_code_http_status(code)
|