vercel-headers-bundle 0.7.1__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,144 @@
1
+ from __future__ import annotations
2
+
3
+ import urllib.parse
4
+ from collections.abc import Callable, Iterator, Mapping
5
+ from contextlib import contextmanager
6
+ from contextvars import ContextVar
7
+ from dataclasses import dataclass
8
+ from typing import Any, ParamSpec, Protocol, TypedDict, TypeVar
9
+
10
+ __all__ = [
11
+ "ip_address",
12
+ "geolocation",
13
+ "Geo",
14
+ "HeadersContext",
15
+ "set_headers",
16
+ "get_headers",
17
+ "headers_from_asgi_scope",
18
+ "headers_from_wsgi_environ",
19
+ ]
20
+
21
+
22
+ _cv_headers: ContextVar[Mapping[str, str] | None] = ContextVar("vercel_headers", default=None)
23
+ P = ParamSpec("P")
24
+ R = TypeVar("R")
25
+
26
+ # Header constants (same as TS names)
27
+ CITY_HEADER_NAME = "x-vercel-ip-city"
28
+ COUNTRY_HEADER_NAME = "x-vercel-ip-country"
29
+ IP_HEADER_NAME = "x-real-ip"
30
+ LATITUDE_HEADER_NAME = "x-vercel-ip-latitude"
31
+ LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude"
32
+ REGION_HEADER_NAME = "x-vercel-ip-country-region"
33
+ POSTAL_CODE_HEADER_NAME = "x-vercel-ip-postal-code"
34
+ REQUEST_ID_HEADER_NAME = "x-vercel-id"
35
+
36
+ EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397
37
+
38
+
39
+ def set_headers(headers: Mapping[str, str] | None) -> None:
40
+ _cv_headers.set(headers)
41
+
42
+
43
+ def get_headers() -> Mapping[str, str] | None:
44
+ return _cv_headers.get()
45
+
46
+
47
+ @dataclass(frozen=True)
48
+ class HeadersContext:
49
+ """Immutable snapshot of the current Vercel request headers."""
50
+
51
+ headers: Mapping[str, str] | None
52
+
53
+ @contextmanager
54
+ def use(self) -> Iterator[None]:
55
+ token = _cv_headers.set(self.headers)
56
+ try:
57
+ yield
58
+ finally:
59
+ _cv_headers.reset(token)
60
+
61
+ def run(self, func: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
62
+ with self.use():
63
+ return func(*args, **kwargs)
64
+
65
+
66
+ class _HeadersLike(Protocol):
67
+ def get(self, name: str) -> str | None: ...
68
+
69
+
70
+ class _RequestLike(Protocol):
71
+ headers: _HeadersLike
72
+
73
+
74
+ class Geo(TypedDict, total=False):
75
+ city: str | None
76
+ country: str | None
77
+ flag: str | None
78
+ region: str | None
79
+ countryRegion: str | None
80
+ latitude: str | None
81
+ longitude: str | None
82
+ postalCode: str | None
83
+
84
+
85
+ def _get_header(headers: _HeadersLike, key: str) -> str | None:
86
+ return headers.get(key)
87
+
88
+
89
+ def _get_header_decode(req: _RequestLike, key: str) -> str | None:
90
+ raw = _get_header(req.headers, key)
91
+ return urllib.parse.unquote(raw) if raw is not None else None
92
+
93
+
94
+ def _get_flag(country_code: str | None) -> str | None:
95
+ if not country_code or len(country_code) != 2 or not country_code.isalpha():
96
+ return None
97
+ return "".join(chr(EMOJI_FLAG_UNICODE_STARTING_POSITION + ord(c)) for c in country_code.upper())
98
+
99
+
100
+ def headers_from_asgi_scope(scope: Mapping[str, Any]) -> dict[str, str]:
101
+ """Return request headers decoded from an ASGI scope."""
102
+ return {
103
+ name.decode("latin-1"): value.decode("latin-1") for name, value in scope.get("headers", [])
104
+ }
105
+
106
+
107
+ def headers_from_wsgi_environ(environ: Mapping[str, Any]) -> dict[str, str]:
108
+ """Return request headers decoded from a WSGI environ mapping."""
109
+ headers: dict[str, str] = {}
110
+ if "CONTENT_TYPE" in environ:
111
+ headers["Content-Type"] = str(environ["CONTENT_TYPE"])
112
+ if "CONTENT_LENGTH" in environ:
113
+ headers["Content-Length"] = str(environ["CONTENT_LENGTH"])
114
+ for name, value in environ.items():
115
+ if not name.startswith("HTTP_"):
116
+ continue
117
+ header_name = name[5:].replace("_", "-").title()
118
+ headers[header_name] = str(value)
119
+ return headers
120
+
121
+
122
+ def ip_address(input: _RequestLike | _HeadersLike) -> str | None:
123
+ headers = input.headers if hasattr(input, "headers") else input
124
+ return _get_header(headers, IP_HEADER_NAME)
125
+
126
+
127
+ def _region_from_request_id(request_id: str | None) -> str | None:
128
+ if request_id is None:
129
+ return "dev1"
130
+ return request_id.split(":")[0]
131
+
132
+
133
+ def geolocation(request: _RequestLike) -> Geo:
134
+ headers = request.headers
135
+ return {
136
+ "city": _get_header_decode(request, CITY_HEADER_NAME),
137
+ "country": _get_header(headers, COUNTRY_HEADER_NAME),
138
+ "flag": _get_flag(_get_header(headers, COUNTRY_HEADER_NAME)),
139
+ "countryRegion": _get_header(headers, REGION_HEADER_NAME),
140
+ "region": _region_from_request_id(_get_header(headers, REQUEST_ID_HEADER_NAME)),
141
+ "latitude": _get_header(headers, LATITUDE_HEADER_NAME),
142
+ "longitude": _get_header(headers, LONGITUDE_HEADER_NAME),
143
+ "postalCode": _get_header(headers, POSTAL_CODE_HEADER_NAME),
144
+ }
@@ -0,0 +1 @@
1
+ """Generated vendored dependencies for vercel-headers-bundle."""
File without changes
@@ -0,0 +1 @@
1
+ __version__ = "0.7.1"
@@ -0,0 +1,27 @@
1
+ Metadata-Version: 2.4
2
+ Name: vercel-headers-bundle
3
+ Version: 0.7.1
4
+ Summary: Request header helpers for Vercel Python applications
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+
10
+ # vercel-headers-bundle
11
+
12
+ This is a version of `vercel-headers` with third-party dependencies bundled. For normal use, install the unbundled `vercel-headers` package instead: https://pypi.org/project/vercel-headers/
13
+
14
+ # Headers
15
+
16
+ `vercel.headers` stores request headers for Vercel Function helpers and exposes
17
+ IP address and geolocation helpers.
18
+
19
+ Register request headers once per request:
20
+
21
+ ```python
22
+ from vercel.headers import set_headers
23
+
24
+ set_headers(request.headers)
25
+ ```
26
+
27
+ OIDC and cache helpers read the same registered header context.
@@ -0,0 +1,8 @@
1
+ vercel/headers/__init__.py,sha256=blQhLacX7FqefHOSMDiHGfjDdeW437jNzL0qv3-k6zQ,4439
2
+ vercel/headers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ vercel/headers/version.py,sha256=2KJZDSMOG7KS82AxYOrZ4ZihYxX0wjfUjDsIZh3L024,22
4
+ vercel/headers/_vendor/__init__.py,sha256=eNgwm8aDiNnSnNnz-wKVqoqnn80GlSsHZMETGb4EKsk,65
5
+ vercel_headers_bundle-0.7.1.dist-info/METADATA,sha256=g2aDNGVNXYYhZ09srrTA3CpoPU3Gi60Y4gkYd10GKso,775
6
+ vercel_headers_bundle-0.7.1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
7
+ vercel_headers_bundle-0.7.1.dist-info/licenses/LICENSE,sha256=ZhFC5TwxPSu14bBV9cCjkAFFD_G14nuJ3EvH3ppjUso,1069
8
+ vercel_headers_bundle-0.7.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vercel, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.