vercel-internal-core 0.2.2__tar.gz → 0.3.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.
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/PKG-INFO +2 -2
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/_vercel_hatch_build.py +1 -5
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/pyproject.toml +1 -1
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/__init__.py +2 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/transport.py +24 -2
- vercel_internal_core-0.3.0/vercel/_internal/core/polyfills/__init__.py +5 -0
- vercel_internal_core-0.3.0/vercel/_internal/core/polyfills/_typing.py +8 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/session.py +63 -22
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/time.py +3 -3
- vercel_internal_core-0.3.0/vercel/_internal/core/version.py +1 -0
- vercel_internal_core-0.2.2/vercel/_internal/core/polyfills/__init__.py +0 -9
- vercel_internal_core-0.2.2/vercel/_internal/core/polyfills/_datetime.py +0 -5
- vercel_internal_core-0.2.2/vercel/_internal/core/polyfills/_strenum.py +0 -10
- vercel_internal_core-0.2.2/vercel/_internal/core/polyfills/_strenum_impl.py +0 -14
- vercel_internal_core-0.2.2/vercel/_internal/core/polyfills/_typing.py +0 -13
- vercel_internal_core-0.2.2/vercel/_internal/core/version.py +0 -1
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/.gitignore +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/LICENSE +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/README.md +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/hatch_build.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/__init__.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/byte_stream.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/detect_agent.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/errors.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/_compat.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/config.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/httpx.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/retry.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/iter_coroutine.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/options.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/py.typed +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/typeutils.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/url.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/api/__init__.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/api/py.typed +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/errors/__init__.py +0 -0
- {vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/errors/py.typed +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: vercel-internal-core
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Shared internal runtime for Vercel Python packages
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
7
|
-
Requires-Python: >=3.
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
8
|
Requires-Dist: anyio<5,>=4.11.0
|
|
9
9
|
Requires-Dist: httpx2>=2
|
|
10
10
|
Requires-Dist: typing-extensions<5,>=4.6.0; python_version < '3.12'
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import ast
|
|
6
|
+
import tomllib
|
|
6
7
|
from pathlib import Path
|
|
7
8
|
from typing import Any
|
|
8
9
|
|
|
@@ -10,11 +11,6 @@ from hatchling.metadata.plugin.interface import MetadataHookInterface
|
|
|
10
11
|
from packaging.requirements import Requirement
|
|
11
12
|
from packaging.specifiers import SpecifierSet
|
|
12
13
|
|
|
13
|
-
try:
|
|
14
|
-
import tomllib
|
|
15
|
-
except ModuleNotFoundError: # pragma: no cover - Python < 3.11
|
|
16
|
-
import tomli as tomllib # type: ignore[no-redef]
|
|
17
|
-
|
|
18
14
|
|
|
19
15
|
class WorkspaceDependenciesMetadataHook(MetadataHookInterface):
|
|
20
16
|
"""Generate package dependencies from the repo-owned dependency table."""
|
|
@@ -7,7 +7,7 @@ name = "vercel-internal-core"
|
|
|
7
7
|
dynamic = ["version", "dependencies"]
|
|
8
8
|
description = "Shared internal runtime for Vercel Python packages"
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
11
|
license = "MIT"
|
|
12
12
|
license-files = ["LICENSE", "LICENSE.*"]
|
|
13
13
|
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/__init__.py
RENAMED
|
@@ -10,6 +10,7 @@ from vercel._internal.core.http.retry import (
|
|
|
10
10
|
SleepFn,
|
|
11
11
|
)
|
|
12
12
|
from vercel._internal.core.http.transport import (
|
|
13
|
+
NO_READ_TIMEOUT,
|
|
13
14
|
NO_TIMEOUT,
|
|
14
15
|
AsyncTransport,
|
|
15
16
|
BaseTransport,
|
|
@@ -36,6 +37,7 @@ __all__ = [
|
|
|
36
37
|
"JSONBody",
|
|
37
38
|
"BytesBody",
|
|
38
39
|
"NO_TIMEOUT",
|
|
40
|
+
"NO_READ_TIMEOUT",
|
|
39
41
|
"RawBody",
|
|
40
42
|
"ReadResponsePolicy",
|
|
41
43
|
"RequestBody",
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/transport.py
RENAMED
|
@@ -10,6 +10,7 @@ from collections.abc import AsyncIterator, Callable, Iterator, Mapping, Sequence
|
|
|
10
10
|
from contextlib import AbstractAsyncContextManager, asynccontextmanager
|
|
11
11
|
from dataclasses import dataclass
|
|
12
12
|
from datetime import timedelta
|
|
13
|
+
from enum import StrEnum
|
|
13
14
|
from types import TracebackType
|
|
14
15
|
from typing import Any, Final, TypeAlias, cast, final
|
|
15
16
|
|
|
@@ -18,7 +19,6 @@ import httpx2 as httpx
|
|
|
18
19
|
from anyio.abc import ObjectReceiveStream, ObjectSendStream
|
|
19
20
|
|
|
20
21
|
from vercel._internal.core.http._compat import is_async_http_client
|
|
21
|
-
from vercel._internal.core.polyfills import StrEnum
|
|
22
22
|
from vercel._internal.core.time import to_seconds_float
|
|
23
23
|
|
|
24
24
|
PrimitiveData: TypeAlias = str | int | float | bool | None
|
|
@@ -72,7 +72,17 @@ class _NoTimeout:
|
|
|
72
72
|
|
|
73
73
|
|
|
74
74
|
NO_TIMEOUT: Final = _NoTimeout()
|
|
75
|
-
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@final
|
|
78
|
+
class _NoReadTimeout:
|
|
79
|
+
"""Disable read timeouts while retaining the client's other limits."""
|
|
80
|
+
|
|
81
|
+
__slots__ = ()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
NO_READ_TIMEOUT: Final = _NoReadTimeout()
|
|
85
|
+
RequestTimeout: TypeAlias = timedelta | _NoTimeout | _NoReadTimeout | None
|
|
76
86
|
|
|
77
87
|
|
|
78
88
|
class ReadResponsePolicy(StrEnum):
|
|
@@ -167,6 +177,18 @@ class BaseTransport(abc.ABC):
|
|
|
167
177
|
case RawBody():
|
|
168
178
|
content = body.data
|
|
169
179
|
|
|
180
|
+
if timeout is NO_READ_TIMEOUT:
|
|
181
|
+
request = client.build_request(
|
|
182
|
+
method,
|
|
183
|
+
_normalize_path(path),
|
|
184
|
+
params=params,
|
|
185
|
+
headers=headers,
|
|
186
|
+
json=json,
|
|
187
|
+
content=content,
|
|
188
|
+
)
|
|
189
|
+
request.extensions["timeout"] = {**request.extensions["timeout"], "read": None}
|
|
190
|
+
return request
|
|
191
|
+
|
|
170
192
|
if timeout is NO_TIMEOUT:
|
|
171
193
|
return client.build_request(
|
|
172
194
|
method,
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"""Mode-specific sessions shared by Vercel Python services."""
|
|
2
2
|
|
|
3
|
-
import asyncio
|
|
4
3
|
import threading
|
|
5
4
|
import time
|
|
6
5
|
from collections.abc import Callable, Mapping, Sequence
|
|
7
6
|
from contextvars import ContextVar, Token
|
|
8
7
|
from types import TracebackType
|
|
9
|
-
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar, cast, overload
|
|
8
|
+
from typing import TYPE_CHECKING, Any, ClassVar, Self, TypeVar, cast, overload
|
|
10
9
|
|
|
11
10
|
import anyio
|
|
11
|
+
import anyio.lowlevel
|
|
12
12
|
import httpx2 as httpx
|
|
13
13
|
|
|
14
14
|
from vercel._internal.core.errors import (
|
|
@@ -21,7 +21,6 @@ from vercel._internal.core.http._compat import (
|
|
|
21
21
|
is_sync_http_client,
|
|
22
22
|
)
|
|
23
23
|
from vercel._internal.core.options import ServiceOptions, merge_service_options
|
|
24
|
-
from vercel._internal.core.polyfills import Self
|
|
25
24
|
|
|
26
25
|
if TYPE_CHECKING:
|
|
27
26
|
from vercel._internal.core.byte_stream import (
|
|
@@ -37,6 +36,45 @@ HttpxClientFactory = Callable[[], httpx.AsyncClient] | Callable[[], httpx.Client
|
|
|
37
36
|
_UNSET = object()
|
|
38
37
|
|
|
39
38
|
|
|
39
|
+
def _current_event_loop() -> object | None:
|
|
40
|
+
"""Return the native AnyIO token of the running event loop, or `None` outside one.
|
|
41
|
+
|
|
42
|
+
The token is the loop itself on asyncio and the run's `TrioToken` on Trio. It
|
|
43
|
+
stays the same object for the whole run, so it can key per-loop state.
|
|
44
|
+
"""
|
|
45
|
+
try:
|
|
46
|
+
return anyio.lowlevel.current_token().native_token
|
|
47
|
+
except RuntimeError:
|
|
48
|
+
# Outside an event loop AnyIO raises `NoEventLoopError`, or sniffio's
|
|
49
|
+
# `AsyncLibraryNotFoundError` on older releases; both are `RuntimeError`s.
|
|
50
|
+
return None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _ignore() -> None:
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _event_loop_has_finished(token: object) -> bool:
|
|
58
|
+
"""Tell whether the event loop behind a native AnyIO token can no longer run.
|
|
59
|
+
|
|
60
|
+
AnyIO does not expose this, so each backend's own token is asked directly.
|
|
61
|
+
"""
|
|
62
|
+
is_closed = getattr(token, "is_closed", None)
|
|
63
|
+
if callable(is_closed):
|
|
64
|
+
# asyncio: the token is the event loop.
|
|
65
|
+
return bool(is_closed())
|
|
66
|
+
run_sync_soon = getattr(token, "run_sync_soon", None)
|
|
67
|
+
if callable(run_sync_soon):
|
|
68
|
+
# Trio: a token whose run has ended raises `RunFinishedError`, a
|
|
69
|
+
# `RuntimeError`, instead of queueing the callback. For a live run the
|
|
70
|
+
# no-op is deduplicated and is harmless.
|
|
71
|
+
try:
|
|
72
|
+
run_sync_soon(_ignore, idempotent=True)
|
|
73
|
+
except RuntimeError:
|
|
74
|
+
return True
|
|
75
|
+
return False
|
|
76
|
+
|
|
77
|
+
|
|
40
78
|
class _BaseSdkSession:
|
|
41
79
|
"""Common scoped state shared by one runtime-mode session."""
|
|
42
80
|
|
|
@@ -116,9 +154,10 @@ class SdkSession(_BaseSdkSession):
|
|
|
116
154
|
)
|
|
117
155
|
self._transport: AsyncTransport | None = None
|
|
118
156
|
self._staging_file_runtime: AsyncByteStreamRuntime | None = None
|
|
119
|
-
# Keyed by loop, with `None` for a client
|
|
120
|
-
# session is process-wide, so it outlives
|
|
121
|
-
|
|
157
|
+
# Keyed by the event loop's native AnyIO token, with `None` for a client
|
|
158
|
+
# built outside one. The default session is process-wide, so it outlives
|
|
159
|
+
# any loop its sockets belong to.
|
|
160
|
+
self._clients: dict[object | None, httpx.AsyncClient] = {}
|
|
122
161
|
# One session can be reached from threads running loops of their own.
|
|
123
162
|
self._clients_lock = threading.Lock()
|
|
124
163
|
|
|
@@ -150,13 +189,6 @@ class SdkSession(_BaseSdkSession):
|
|
|
150
189
|
),
|
|
151
190
|
)
|
|
152
191
|
|
|
153
|
-
@staticmethod
|
|
154
|
-
def _running_loop() -> "asyncio.AbstractEventLoop | None":
|
|
155
|
-
try:
|
|
156
|
-
return asyncio.get_running_loop()
|
|
157
|
-
except RuntimeError:
|
|
158
|
-
return None
|
|
159
|
-
|
|
160
192
|
def _client_for_running_loop(self) -> httpx.AsyncClient:
|
|
161
193
|
"""Return the client owned by the loop making this request.
|
|
162
194
|
|
|
@@ -166,7 +198,7 @@ class SdkSession(_BaseSdkSession):
|
|
|
166
198
|
closed`. The caller sees an error for work the server already did, so the
|
|
167
199
|
client is chosen per loop up front rather than repaired afterwards.
|
|
168
200
|
"""
|
|
169
|
-
loop =
|
|
201
|
+
loop = _current_event_loop()
|
|
170
202
|
with self._clients_lock:
|
|
171
203
|
# Under the lock, so a request holding this transport cannot build a
|
|
172
204
|
# client that outlives the session it belongs to.
|
|
@@ -175,7 +207,9 @@ class SdkSession(_BaseSdkSession):
|
|
|
175
207
|
if client is not None:
|
|
176
208
|
return client
|
|
177
209
|
for known in [
|
|
178
|
-
known
|
|
210
|
+
known
|
|
211
|
+
for known in self._clients
|
|
212
|
+
if known is not None and _event_loop_has_finished(known)
|
|
179
213
|
]:
|
|
180
214
|
# Dropped, not closed: `aclose` needs the loop that owns the
|
|
181
215
|
# sockets, and that loop is gone.
|
|
@@ -239,7 +273,7 @@ class SdkSession(_BaseSdkSession):
|
|
|
239
273
|
return
|
|
240
274
|
self._clear_services()
|
|
241
275
|
self._transport = None
|
|
242
|
-
loop =
|
|
276
|
+
loop = _current_event_loop()
|
|
243
277
|
with self._clients_lock:
|
|
244
278
|
# Closing and the flag that stops new clients have to be one step, or
|
|
245
279
|
# a resolver already past the check registers a client nothing closes.
|
|
@@ -300,15 +334,22 @@ class SyncSdkSession(_BaseSdkSession):
|
|
|
300
334
|
)
|
|
301
335
|
|
|
302
336
|
def _close_wrong_async_client(self, client: Any) -> None:
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
337
|
+
"""Close, best effort, an async client a factory returned to this sync session.
|
|
338
|
+
|
|
339
|
+
The client was just built and has sent nothing, so no event loop owns its
|
|
340
|
+
resources and any loop can close it. A fresh one runs in a worker thread,
|
|
341
|
+
because this sync path may itself be called from inside a running loop.
|
|
342
|
+
"""
|
|
343
|
+
|
|
344
|
+
def close() -> None:
|
|
306
345
|
try:
|
|
307
|
-
|
|
346
|
+
anyio.run(client.aclose)
|
|
308
347
|
except Exception:
|
|
309
348
|
pass
|
|
310
|
-
|
|
311
|
-
|
|
349
|
+
|
|
350
|
+
worker = threading.Thread(target=close, name="vercel-close-async-client", daemon=True)
|
|
351
|
+
worker.start()
|
|
352
|
+
worker.join()
|
|
312
353
|
|
|
313
354
|
def get_transport(self) -> "SyncTransport":
|
|
314
355
|
from vercel._internal.core.http import (
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from datetime import datetime, timedelta
|
|
3
|
+
from datetime import UTC, datetime, timedelta
|
|
4
4
|
|
|
5
5
|
MILLISECOND = timedelta(milliseconds=1)
|
|
6
6
|
SECOND = timedelta(seconds=1)
|
|
@@ -51,12 +51,12 @@ def to_seconds_float(td: timedelta) -> float:
|
|
|
51
51
|
|
|
52
52
|
def from_epoch_ms(value: int | float) -> datetime:
|
|
53
53
|
"""Read a millisecond epoch timestamp as an aware UTC datetime."""
|
|
54
|
-
return datetime.fromtimestamp(value / 1000, tz=
|
|
54
|
+
return datetime.fromtimestamp(value / 1000, tz=UTC)
|
|
55
55
|
|
|
56
56
|
|
|
57
57
|
def from_epoch_seconds(value: int | float) -> datetime:
|
|
58
58
|
"""Read a second epoch timestamp as an aware UTC datetime."""
|
|
59
|
-
return datetime.fromtimestamp(value, tz=
|
|
59
|
+
return datetime.fromtimestamp(value, tz=UTC)
|
|
60
60
|
|
|
61
61
|
|
|
62
62
|
def parse_epoch_seconds(value: object) -> datetime | None:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.0"
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import enum as _enum
|
|
2
|
-
from typing import Any
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class StrEnum(str, _enum.Enum):
|
|
6
|
-
__str__ = str.__str__
|
|
7
|
-
__format__ = str.__format__ # type: ignore[assignment]
|
|
8
|
-
|
|
9
|
-
@staticmethod
|
|
10
|
-
def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> str:
|
|
11
|
-
return name.lower()
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
__all__ = ("StrEnum",)
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
|
|
3
|
-
if sys.version_info >= (3, 12):
|
|
4
|
-
from collections.abc import Buffer
|
|
5
|
-
else:
|
|
6
|
-
from typing_extensions import Buffer
|
|
7
|
-
|
|
8
|
-
if sys.version_info >= (3, 11):
|
|
9
|
-
from typing import Self
|
|
10
|
-
else:
|
|
11
|
-
from typing_extensions import Self
|
|
12
|
-
|
|
13
|
-
__all__ = ("Buffer", "Self")
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.2"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/byte_stream.py
RENAMED
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/detect_agent.py
RENAMED
|
File without changes
|
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/_compat.py
RENAMED
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/config.py
RENAMED
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/httpx.py
RENAMED
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/http/retry.py
RENAMED
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/iter_coroutine.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{vercel_internal_core-0.2.2 → vercel_internal_core-0.3.0}/vercel/_internal/core/typeutils.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|