fastapi-reloader 1.3.2__py2.py3-none-any.whl → 1.3.3__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/core.py CHANGED
@@ -1,7 +1,6 @@
1
1
  from asyncio import Queue, ensure_future, sleep
2
2
  from collections import defaultdict
3
3
  from itertools import count
4
- from pathlib import Path
5
4
  from typing import Literal
6
5
 
7
6
  from fastapi import APIRouter, Response
@@ -22,26 +21,15 @@ def send_reload_signal():
22
21
  reload_router = APIRouter(prefix="/---fastapi-reloader---", tags=["hmr"])
23
22
 
24
23
 
25
- runtime_js = Path(__file__, "../runtime.js").resolve().read_text()
26
-
27
-
28
- def get_js():
29
- return runtime_js.replace("/0", f"/{get_id()}")
30
-
31
-
32
24
  @reload_router.head("")
33
25
  async def heartbeat():
34
- return Response(status_code=200)
35
-
36
-
37
- @reload_router.get("/poller.js")
38
- async def get_poller_js():
39
- return Response(get_js(), media_type="application/javascript")
26
+ return Response(status_code=202)
40
27
 
41
28
 
42
- @reload_router.get("/{key:int}")
43
- async def simple_refresh_trigger(key: int):
29
+ @reload_router.get("")
30
+ async def subscribe():
44
31
  async def event_generator():
32
+ key = get_id()
45
33
  queue = Queue[Literal[0, 1]]()
46
34
 
47
35
  stopped = False
@@ -66,4 +54,4 @@ async def simple_refresh_trigger(key: int):
66
54
  heartbeat_future.cancel()
67
55
  requests[key].remove(queue)
68
56
 
69
- return StreamingResponse(event_generator(), media_type="text/plain")
57
+ return StreamingResponse(event_generator(), 201, media_type="text/plain")
@@ -2,6 +2,7 @@ from collections.abc import Awaitable, Callable
2
2
  from contextlib import asynccontextmanager
3
3
  from copy import copy
4
4
  from math import inf
5
+ from pathlib import Path
5
6
  from typing import Generic, TypeGuard, TypeVar
6
7
 
7
8
  from asgi_lifespan import LifespanManager
@@ -20,6 +21,9 @@ def is_streaming_response(response: Response) -> TypeGuard[StreamingResponse]:
20
21
  return hasattr(response, "body_iterator")
21
22
 
22
23
 
24
+ INJECTION = f"\n\n<script>\n{Path(__file__, '../runtime.js').resolve().read_text()}\n</script>".encode()
25
+
26
+
23
27
  async def _injection_http_middleware(request: Request, call_next: Callable[[Request], Awaitable[Response]]):
24
28
  res = await call_next(request)
25
29
 
@@ -33,7 +37,7 @@ async def _injection_http_middleware(request: Request, call_next: Callable[[Requ
33
37
  else:
34
38
  yield res.body
35
39
 
36
- yield b'\n\n <script src="/---fastapi-reloader---/poller.js"></script>'
40
+ yield INJECTION
37
41
 
38
42
  headers = {k: v for k, v in res.headers.items() if k.lower() not in {"content-length", "transfer-encoding"}}
39
43
 
@@ -11,23 +11,37 @@ async function poll() {
11
11
  }
12
12
  }
13
13
 
14
- async function main() {
15
- const response = await fetch("/---fastapi-reloader---/0");
14
+ /** @param {Response} response */
15
+ async function wait(response) {
16
16
  const reader = response.body.getReader();
17
17
  const decoder = new TextDecoder();
18
18
  while (true) {
19
19
  const { done, value } = await reader.read();
20
20
  if (done) {
21
- break;
21
+ return;
22
22
  }
23
23
  if (value) {
24
24
  const chunk = decoder.decode(value, { stream: true });
25
25
  if (chunk.includes("1")) {
26
- await poll();
27
- location.reload();
26
+ return;
28
27
  }
29
28
  }
30
29
  }
31
30
  }
32
31
 
33
- main();
32
+ async function main() {
33
+ const response = await fetch("/---fastapi-reloader---").catch(() => null);
34
+ if (response?.ok && response.body) {
35
+ await wait(response).catch(() => null);
36
+ await poll();
37
+ location.reload();
38
+ } else {
39
+ await poll();
40
+ return await main();
41
+ }
42
+ }
43
+
44
+ if (!window.__fastapi_reloader_loaded) {
45
+ window.__fastapi_reloader_loaded = true;
46
+ main();
47
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastapi-reloader
3
- Version: 1.3.2
3
+ Version: 1.3.3
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
@@ -0,0 +1,8 @@
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,,
@@ -1,8 +0,0 @@
1
- fastapi_reloader-1.3.2.dist-info/METADATA,sha256=D7-0CWUurSwKz-MdsPAQL-Tm4SE6Lg0X5V_3q3I2qSQ,6314
2
- fastapi_reloader-1.3.2.dist-info/WHEEL,sha256=pz1FfwQ2kf9tI4G8U2ObRTKdvsTSmrreuBTtdnO8pJw,94
3
- fastapi_reloader-1.3.2.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
4
- fastapi_reloader/__init__.py,sha256=qWjX076aoLEZxZoOIvtmg84khic2FBXpAWuRWVbouTY,309
5
- fastapi_reloader/core.py,sha256=pug78QfiDLaX7uBj9zbjAwphMlsFGlam0qEpUnHOhfo,1779
6
- fastapi_reloader/patcher.py,sha256=MXZjva7zIdSYN4j5RGxBMy2UtJ34pSj3ylwzcKfoAKU,3318
7
- fastapi_reloader/runtime.js,sha256=pUPeMnMuKGHhBTudp6bFuNTTyZhGSvrvvWy_lWVN8nA,713
8
- fastapi_reloader-1.3.2.dist-info/RECORD,,