fastapi-reloader 1.3.3__py2.py3-none-any.whl → 1.3.4__py2.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.
- fastapi_reloader/patcher.py +15 -1
- {fastapi_reloader-1.3.3.dist-info → fastapi_reloader-1.3.4.dist-info}/METADATA +3 -1
- fastapi_reloader-1.3.4.dist-info/RECORD +8 -0
- fastapi_reloader-1.3.3.dist-info/RECORD +0 -8
- {fastapi_reloader-1.3.3.dist-info → fastapi_reloader-1.3.4.dist-info}/WHEEL +0 -0
- {fastapi_reloader-1.3.3.dist-info → fastapi_reloader-1.3.4.dist-info}/entry_points.txt +0 -0
fastapi_reloader/patcher.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
from collections.abc import Awaitable, Callable
|
2
2
|
from contextlib import asynccontextmanager
|
3
3
|
from copy import copy
|
4
|
+
from inspect import ismethod
|
4
5
|
from math import inf
|
5
6
|
from pathlib import Path
|
7
|
+
from types import MethodType
|
6
8
|
from typing import Generic, TypeGuard, TypeVar
|
7
9
|
|
8
10
|
from asgi_lifespan import LifespanManager
|
@@ -23,13 +25,17 @@ def is_streaming_response(response: Response) -> TypeGuard[StreamingResponse]:
|
|
23
25
|
|
24
26
|
INJECTION = f"\n\n<script>\n{Path(__file__, '../runtime.js').resolve().read_text()}\n</script>".encode()
|
25
27
|
|
28
|
+
FLAG = " fastapi-reloader-injected " # to avoid double injection
|
29
|
+
|
26
30
|
|
27
31
|
async def _injection_http_middleware(request: Request, call_next: Callable[[Request], Awaitable[Response]]):
|
28
32
|
res = await call_next(request)
|
29
33
|
|
30
|
-
if request.method != "GET" or "html" not in (res.headers.get("content-type", "")) or res.headers.get("content-encoding", "identity") != "identity":
|
34
|
+
if request.scope.get(FLAG) or request.method != "GET" or "html" not in (res.headers.get("content-type", "")) or res.headers.get("content-encoding", "identity") != "identity":
|
31
35
|
return res
|
32
36
|
|
37
|
+
request.scope[FLAG] = True
|
38
|
+
|
33
39
|
async def response():
|
34
40
|
if is_streaming_response(res):
|
35
41
|
async for chunk in res.body_iterator:
|
@@ -83,6 +89,14 @@ def patch_for_auto_reloading(app: ASGIApp): # this function is preserved for ba
|
|
83
89
|
if isinstance(app, Starlette): # both FastAPI and Starlette have user_middleware attribute
|
84
90
|
new_app = copy(app)
|
85
91
|
new_app.user_middleware = [*app.user_middleware, html_injection_middleware] # before compression middlewares
|
92
|
+
|
93
|
+
# OTEL patches the app's build_middleware_stack method and keep a reference to the original build_middleware_stack.
|
94
|
+
# But both methods are bound to the original app instance, so we need to rebind them to the new app instance.
|
95
|
+
# The following loop generically rebinds all these methods, preventing potential issues caused by similar patches.
|
96
|
+
for i in dir(new_app):
|
97
|
+
if ismethod(method := getattr(new_app, i)) and method.__self__ is app:
|
98
|
+
setattr(new_app, i, MethodType(method.__func__, new_app))
|
99
|
+
|
86
100
|
return _wrap_asgi_app(new_app)
|
87
101
|
|
88
102
|
new_app = _wrap_asgi_app(app)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: fastapi-reloader
|
3
|
-
Version: 1.3.
|
3
|
+
Version: 1.3.4
|
4
4
|
Project-URL: Homepage, https://github.com/promplate/hmr
|
5
5
|
Requires-Dist: asgi-lifespan~=2.0
|
6
6
|
Requires-Dist: fastapi~=0.115
|
@@ -74,6 +74,8 @@ app = html_injection_middleware(app)
|
|
74
74
|
app.user_middleware.append(html_injection_middleware)
|
75
75
|
```
|
76
76
|
|
77
|
+
> It's safe to add `html_injection_middleware` in multiple places, even if their scopes overlap. We have safeguards in place to prevent double injection on both server and client side.
|
78
|
+
|
77
79
|
The `auto_refresh_middleware` is a convenient wrapper that applies both `reloader_route_middleware` and `html_injection_middleware`. However, you can add them separately for more control:
|
78
80
|
|
79
81
|
- **Fine-grained control**: If a sub-router in your application uses compression, you must add `html_injection_middleware` before the compression middleware on that router.
|
@@ -0,0 +1,8 @@
|
|
1
|
+
fastapi_reloader-1.3.4.dist-info/METADATA,sha256=7h8S-serIhPPn1UnyUCuIJOA6NgOI-I6oUDQ9mu-xOs,6500
|
2
|
+
fastapi_reloader-1.3.4.dist-info/WHEEL,sha256=pz1FfwQ2kf9tI4G8U2ObRTKdvsTSmrreuBTtdnO8pJw,94
|
3
|
+
fastapi_reloader-1.3.4.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
|
4
|
+
fastapi_reloader/__init__.py,sha256=qWjX076aoLEZxZoOIvtmg84khic2FBXpAWuRWVbouTY,309
|
5
|
+
fastapi_reloader/core.py,sha256=cqB9uVdPa-0SMafaFH2Ho7m8npD1u503JIGX43oZ4es,1485
|
6
|
+
fastapi_reloader/patcher.py,sha256=KJjjbT0uDjZv1Q95slW3MBoUggPrMdCamjnlTy-iTKc,4133
|
7
|
+
fastapi_reloader/runtime.js,sha256=kG73Qx2civNzrG36Hfsu9U_esmjYh4EqynpVcmUqB8o,1036
|
8
|
+
fastapi_reloader-1.3.4.dist-info/RECORD,,
|
@@ -1,8 +0,0 @@
|
|
1
|
-
fastapi_reloader-1.3.3.dist-info/METADATA,sha256=mHNM1pVEfzrBw-m5HgNzkKnNA846lS8EfnJvL51QGAY,6314
|
2
|
-
fastapi_reloader-1.3.3.dist-info/WHEEL,sha256=pz1FfwQ2kf9tI4G8U2ObRTKdvsTSmrreuBTtdnO8pJw,94
|
3
|
-
fastapi_reloader-1.3.3.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
|
4
|
-
fastapi_reloader/__init__.py,sha256=qWjX076aoLEZxZoOIvtmg84khic2FBXpAWuRWVbouTY,309
|
5
|
-
fastapi_reloader/core.py,sha256=cqB9uVdPa-0SMafaFH2Ho7m8npD1u503JIGX43oZ4es,1485
|
6
|
-
fastapi_reloader/patcher.py,sha256=Wlq3c0-bcFAMFIMVmM0TMH07-tnlswsJgokdBnB48qk,3394
|
7
|
-
fastapi_reloader/runtime.js,sha256=kG73Qx2civNzrG36Hfsu9U_esmjYh4EqynpVcmUqB8o,1036
|
8
|
-
fastapi_reloader-1.3.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|