chickinfry 0.5.0.dev1__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.
- chickinfry/__init__.py +18 -0
- chickinfry/_auth_database.py +765 -0
- chickinfry/_conformance.py +208 -0
- chickinfry/_device.py +1405 -0
- chickinfry/_device_windows.py +250 -0
- chickinfry/_error_details.py +76 -0
- chickinfry/_http_common.py +165 -0
- chickinfry/_input.py +129 -0
- chickinfry/_origin.py +60 -0
- chickinfry/_password.py +38 -0
- chickinfry/_request_logs.py +66 -0
- chickinfry/_request_state.py +32 -0
- chickinfry/_sync_http.py +546 -0
- chickinfry/_sync_network.py +288 -0
- chickinfry/_workerd.py +731 -0
- chickinfry/auth.py +1227 -0
- chickinfry/errors.py +101 -0
- chickinfry/http.py +427 -0
- chickinfry/orm.py +506 -0
- chickinfry/postgres.py +281 -0
- chickinfry/py.typed +0 -0
- chickinfry/realtime.py +522 -0
- chickinfry/server.py +540 -0
- chickinfry/session.py +465 -0
- chickinfry/signup.py +134 -0
- chickinfry/wire.py +23 -0
- chickinfry-0.5.0.dev1.dist-info/METADATA +375 -0
- chickinfry-0.5.0.dev1.dist-info/RECORD +30 -0
- chickinfry-0.5.0.dev1.dist-info/WHEEL +4 -0
- chickinfry-0.5.0.dev1.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import select
|
|
2
|
+
import socket
|
|
3
|
+
import ssl
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
from collections.abc import Iterable, Iterator
|
|
7
|
+
from contextlib import contextmanager
|
|
8
|
+
from contextvars import ContextVar
|
|
9
|
+
from typing import cast
|
|
10
|
+
|
|
11
|
+
import httpcore
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
from ._conformance import _observer
|
|
15
|
+
from .errors import _local_error
|
|
16
|
+
|
|
17
|
+
_deadline: ContextVar[float] = ContextVar("chickinfry_sync_deadline")
|
|
18
|
+
SocketOption = (
|
|
19
|
+
tuple[int, int, int]
|
|
20
|
+
| tuple[int, int, bytes | bytearray]
|
|
21
|
+
| tuple[int, int, None, int]
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _remaining(phase_timeout: float | None = None) -> float:
|
|
26
|
+
remaining = _deadline.get() - time.monotonic()
|
|
27
|
+
if remaining <= 0:
|
|
28
|
+
raise TimeoutError
|
|
29
|
+
return remaining if phase_timeout is None else min(remaining, phase_timeout)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class _Stream(httpcore.NetworkStream):
|
|
33
|
+
def __init__(self, sock: socket.socket, backend: "_Backend") -> None:
|
|
34
|
+
self._socket = sock
|
|
35
|
+
self._backend = backend
|
|
36
|
+
self._lock = threading.Lock()
|
|
37
|
+
self._closed = False
|
|
38
|
+
self._active = 0
|
|
39
|
+
|
|
40
|
+
@contextmanager
|
|
41
|
+
def _io(self) -> Iterator[socket.socket]:
|
|
42
|
+
with self._lock:
|
|
43
|
+
if self._closed:
|
|
44
|
+
raise OSError
|
|
45
|
+
self._active += 1
|
|
46
|
+
sock = self._socket
|
|
47
|
+
try:
|
|
48
|
+
yield sock
|
|
49
|
+
finally:
|
|
50
|
+
with self._lock:
|
|
51
|
+
self._active -= 1
|
|
52
|
+
close = self._closed and self._active == 0
|
|
53
|
+
sock = self._socket
|
|
54
|
+
if close:
|
|
55
|
+
sock.close()
|
|
56
|
+
self._backend._discard(self)
|
|
57
|
+
observation = _observer.get()
|
|
58
|
+
if observation is not None:
|
|
59
|
+
observation.notify("socket-closed", self)
|
|
60
|
+
|
|
61
|
+
def read(self, max_bytes: int, timeout: float | None = None) -> bytes:
|
|
62
|
+
with self._io() as sock:
|
|
63
|
+
sock.settimeout(_remaining(timeout))
|
|
64
|
+
data = sock.recv(max_bytes)
|
|
65
|
+
observation = _observer.get()
|
|
66
|
+
if observation is not None:
|
|
67
|
+
observation.notify("wire", (self, data))
|
|
68
|
+
return data
|
|
69
|
+
|
|
70
|
+
def write(self, buffer: bytes, timeout: float | None = None) -> None:
|
|
71
|
+
with self._io() as sock:
|
|
72
|
+
pending = memoryview(buffer)
|
|
73
|
+
while pending:
|
|
74
|
+
sock.settimeout(_remaining(timeout))
|
|
75
|
+
count = sock.send(pending)
|
|
76
|
+
if count == 0:
|
|
77
|
+
raise OSError
|
|
78
|
+
pending = pending[count:]
|
|
79
|
+
|
|
80
|
+
def start_tls(
|
|
81
|
+
self,
|
|
82
|
+
ssl_context: ssl.SSLContext,
|
|
83
|
+
server_hostname: str | None = None,
|
|
84
|
+
timeout: float | None = None,
|
|
85
|
+
) -> httpcore.NetworkStream:
|
|
86
|
+
try:
|
|
87
|
+
with self._io() as sock:
|
|
88
|
+
sock.settimeout(_remaining(timeout))
|
|
89
|
+
secured = ssl_context.wrap_socket(
|
|
90
|
+
sock,
|
|
91
|
+
server_hostname=server_hostname,
|
|
92
|
+
do_handshake_on_connect=False,
|
|
93
|
+
)
|
|
94
|
+
with self._lock:
|
|
95
|
+
self._socket = secured
|
|
96
|
+
if self._closed:
|
|
97
|
+
raise OSError
|
|
98
|
+
secured.settimeout(_remaining(timeout))
|
|
99
|
+
secured.do_handshake()
|
|
100
|
+
_remaining(timeout)
|
|
101
|
+
return self
|
|
102
|
+
except BaseException:
|
|
103
|
+
self.close()
|
|
104
|
+
raise
|
|
105
|
+
|
|
106
|
+
def close(self) -> None:
|
|
107
|
+
with self._lock:
|
|
108
|
+
if self._closed:
|
|
109
|
+
return
|
|
110
|
+
self._closed = True
|
|
111
|
+
sock = self._socket
|
|
112
|
+
close = self._active == 0
|
|
113
|
+
try:
|
|
114
|
+
sock.shutdown(socket.SHUT_RDWR)
|
|
115
|
+
except OSError:
|
|
116
|
+
pass
|
|
117
|
+
# Closing the fd before in-flight recv/send calls exit may never wake the macOS waits.
|
|
118
|
+
if close:
|
|
119
|
+
sock.close()
|
|
120
|
+
self._backend._discard(self)
|
|
121
|
+
observation = _observer.get()
|
|
122
|
+
if observation is not None:
|
|
123
|
+
observation.notify("socket-closed", self)
|
|
124
|
+
|
|
125
|
+
def get_extra_info(self, info: str) -> object:
|
|
126
|
+
if info == "ssl_object":
|
|
127
|
+
return self._socket if isinstance(self._socket, ssl.SSLSocket) else None
|
|
128
|
+
if info == "socket":
|
|
129
|
+
return self._socket
|
|
130
|
+
if info == "is_readable":
|
|
131
|
+
return bool(select.select([self._socket], [], [], 0)[0])
|
|
132
|
+
if info == "client_addr":
|
|
133
|
+
return self._socket.getsockname()
|
|
134
|
+
if info == "server_addr":
|
|
135
|
+
return self._socket.getpeername()
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class _Backend(httpcore.NetworkBackend):
|
|
140
|
+
def __init__(self) -> None:
|
|
141
|
+
self._lock = threading.Lock()
|
|
142
|
+
self._streams: set[_Stream] = set()
|
|
143
|
+
self._closed = False
|
|
144
|
+
|
|
145
|
+
def _discard(self, stream: _Stream) -> None:
|
|
146
|
+
with self._lock:
|
|
147
|
+
self._streams.discard(stream)
|
|
148
|
+
|
|
149
|
+
def close(self) -> None:
|
|
150
|
+
with self._lock:
|
|
151
|
+
self._closed = True
|
|
152
|
+
streams = list(self._streams)
|
|
153
|
+
for stream in streams:
|
|
154
|
+
stream.close()
|
|
155
|
+
|
|
156
|
+
def connect_tcp(
|
|
157
|
+
self,
|
|
158
|
+
host: str,
|
|
159
|
+
port: int,
|
|
160
|
+
timeout: float | None = None,
|
|
161
|
+
local_address: str | None = None,
|
|
162
|
+
socket_options: Iterable[SocketOption] | None = None,
|
|
163
|
+
) -> httpcore.NetworkStream:
|
|
164
|
+
_remaining(timeout)
|
|
165
|
+
# The OS resolver cannot be interrupted, but the budget is rechecked before new I/O.
|
|
166
|
+
addresses = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
|
|
167
|
+
for family, kind, protocol, _, address in addresses:
|
|
168
|
+
_remaining(timeout)
|
|
169
|
+
sock = socket.socket(family, kind, protocol)
|
|
170
|
+
try:
|
|
171
|
+
if local_address is not None:
|
|
172
|
+
sock.bind((local_address, 0))
|
|
173
|
+
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
174
|
+
for option in socket_options or ():
|
|
175
|
+
if len(option) == 3:
|
|
176
|
+
sock.setsockopt(*option)
|
|
177
|
+
else:
|
|
178
|
+
sock.setsockopt(*option)
|
|
179
|
+
sock.settimeout(_remaining(timeout))
|
|
180
|
+
sock.connect(address)
|
|
181
|
+
_remaining(timeout)
|
|
182
|
+
stream = _Stream(sock, self)
|
|
183
|
+
with self._lock:
|
|
184
|
+
if self._closed:
|
|
185
|
+
raise OSError
|
|
186
|
+
self._streams.add(stream)
|
|
187
|
+
return stream
|
|
188
|
+
except TimeoutError:
|
|
189
|
+
sock.close()
|
|
190
|
+
raise
|
|
191
|
+
except OSError:
|
|
192
|
+
sock.close()
|
|
193
|
+
except BaseException:
|
|
194
|
+
sock.close()
|
|
195
|
+
raise
|
|
196
|
+
raise OSError
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class _ResponseStream(httpx.SyncByteStream):
|
|
200
|
+
def __init__(
|
|
201
|
+
self, response: httpcore.Response, slots: threading.BoundedSemaphore
|
|
202
|
+
) -> None:
|
|
203
|
+
self._response = response
|
|
204
|
+
self._slots = slots
|
|
205
|
+
self._closed = False
|
|
206
|
+
|
|
207
|
+
def __iter__(self) -> Iterator[bytes]:
|
|
208
|
+
for chunk in self._response.iter_stream():
|
|
209
|
+
_remaining()
|
|
210
|
+
yield chunk
|
|
211
|
+
|
|
212
|
+
def close(self) -> None:
|
|
213
|
+
if self._closed:
|
|
214
|
+
return
|
|
215
|
+
self._closed = True
|
|
216
|
+
try:
|
|
217
|
+
self._response.close()
|
|
218
|
+
finally:
|
|
219
|
+
self._slots.release()
|
|
220
|
+
observation = _observer.get()
|
|
221
|
+
if observation is not None:
|
|
222
|
+
observation.notify("reader-closed", self)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
class _Transport(httpx.BaseTransport):
|
|
226
|
+
def __init__(self, *, max_connections: int = 100) -> None:
|
|
227
|
+
self._slots = threading.BoundedSemaphore(max_connections)
|
|
228
|
+
self._backend = _Backend()
|
|
229
|
+
# The admission lock owns the whole pool budget, so httpcore never re-waits internally.
|
|
230
|
+
self._pool = httpcore.ConnectionPool(
|
|
231
|
+
ssl_context=httpx.create_ssl_context(verify=True, trust_env=False),
|
|
232
|
+
network_backend=self._backend,
|
|
233
|
+
max_connections=None,
|
|
234
|
+
max_keepalive_connections=20,
|
|
235
|
+
retries=0,
|
|
236
|
+
http1=True,
|
|
237
|
+
http2=False,
|
|
238
|
+
)
|
|
239
|
+
self._closed = False
|
|
240
|
+
self._lock = threading.Lock()
|
|
241
|
+
|
|
242
|
+
def handle_request(self, request: httpx.Request) -> httpx.Response:
|
|
243
|
+
if not self._slots.acquire(timeout=_remaining()):
|
|
244
|
+
raise TimeoutError
|
|
245
|
+
response: httpcore.Response | None = None
|
|
246
|
+
transferred = False
|
|
247
|
+
try:
|
|
248
|
+
_remaining()
|
|
249
|
+
with self._lock:
|
|
250
|
+
if self._closed:
|
|
251
|
+
raise _local_error("failed_precondition", "transport_closed")
|
|
252
|
+
core_request = httpcore.Request(
|
|
253
|
+
method=request.method,
|
|
254
|
+
url=httpcore.URL(
|
|
255
|
+
scheme=request.url.raw_scheme,
|
|
256
|
+
host=request.url.raw_host,
|
|
257
|
+
port=request.url.port,
|
|
258
|
+
target=request.url.raw_path,
|
|
259
|
+
),
|
|
260
|
+
headers=request.headers.raw,
|
|
261
|
+
content=cast(Iterable[bytes], request.stream),
|
|
262
|
+
extensions=request.extensions,
|
|
263
|
+
)
|
|
264
|
+
response = self._pool.handle_request(core_request)
|
|
265
|
+
_remaining()
|
|
266
|
+
result = httpx.Response(
|
|
267
|
+
response.status,
|
|
268
|
+
headers=response.headers,
|
|
269
|
+
stream=_ResponseStream(response, self._slots),
|
|
270
|
+
extensions=response.extensions,
|
|
271
|
+
)
|
|
272
|
+
transferred = True
|
|
273
|
+
return result
|
|
274
|
+
finally:
|
|
275
|
+
if not transferred:
|
|
276
|
+
try:
|
|
277
|
+
if response is not None:
|
|
278
|
+
response.close()
|
|
279
|
+
finally:
|
|
280
|
+
self._slots.release()
|
|
281
|
+
|
|
282
|
+
def close(self) -> None:
|
|
283
|
+
with self._lock:
|
|
284
|
+
if self._closed:
|
|
285
|
+
return
|
|
286
|
+
self._closed = True
|
|
287
|
+
self._backend.close()
|
|
288
|
+
self._pool.close()
|