|
1 |
| -import asyncio |
2 |
| -import functools |
3 | 1 | import os
|
4 | 2 |
|
5 |
| -from django.core.cache import caches |
| 3 | +from aiofile import async_open |
| 4 | +from django.core.exceptions import SuspiciousOperation |
6 | 5 | from django.http import HttpRequest, HttpResponse
|
7 | 6 | from idom.config import IDOM_WED_MODULES_DIR
|
8 | 7 |
|
9 |
| -from .config import IDOM_WEB_MODULE_CACHE, IDOM_WEB_MODULE_LRU_CACHE_SIZE |
10 |
| - |
11 |
| - |
12 |
| -if IDOM_WEB_MODULE_CACHE is None: |
13 |
| - |
14 |
| - def async_lru_cache(*lru_cache_args, **lru_cache_kwargs): |
15 |
| - def async_lru_cache_decorator(async_function): |
16 |
| - @functools.lru_cache(*lru_cache_args, **lru_cache_kwargs) |
17 |
| - def cached_async_function(*args, **kwargs): |
18 |
| - coroutine = async_function(*args, **kwargs) |
19 |
| - return asyncio.ensure_future(coroutine) |
20 |
| - |
21 |
| - return cached_async_function |
22 |
| - |
23 |
| - return async_lru_cache_decorator |
24 |
| - |
25 |
| - @async_lru_cache(IDOM_WEB_MODULE_LRU_CACHE_SIZE) |
26 |
| - async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse: |
27 |
| - file_path = IDOM_WED_MODULES_DIR.current.joinpath(*file.split("/")) |
28 |
| - return HttpResponse(file_path.read_text(), content_type="text/javascript") |
29 |
| - |
30 |
| -else: |
31 |
| - _web_module_cache = caches[IDOM_WEB_MODULE_CACHE] |
32 |
| - |
33 |
| - async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse: |
34 |
| - file = IDOM_WED_MODULES_DIR.current.joinpath(*file.split("/")).absolute() |
35 |
| - last_modified_time = os.stat(file).st_mtime |
36 |
| - cache_key = f"{file}:{last_modified_time}" |
37 |
| - |
38 |
| - response = _web_module_cache.get(cache_key) |
39 |
| - if response is None: |
40 |
| - response = HttpResponse(file.read_text(), content_type="text/javascript") |
41 |
| - _web_module_cache.set(cache_key, response, timeout=None) |
42 |
| - |
43 |
| - return response |
| 8 | +from .config import IDOM_CACHE |
| 9 | + |
| 10 | + |
| 11 | +async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse: |
| 12 | + """Gets JavaScript required for IDOM modules at runtime. These modules are |
| 13 | + returned from cache if available.""" |
| 14 | + web_modules_dir = IDOM_WED_MODULES_DIR.current |
| 15 | + path = web_modules_dir.joinpath(*file.split("/")).absolute() |
| 16 | + |
| 17 | + # Prevent attempts to walk outside of the web modules dir |
| 18 | + if str(web_modules_dir) != os.path.commonpath((path, web_modules_dir)): |
| 19 | + raise SuspiciousOperation( |
| 20 | + "Attempt to access a directory outside of IDOM_WED_MODULES_DIR." |
| 21 | + ) |
| 22 | + |
| 23 | + # Fetch the file from cache, if available |
| 24 | + last_modified_time = os.stat(path).st_mtime |
| 25 | + cache_key = f"django_idom:web_module:{str(path).lstrip(str(web_modules_dir))}" |
| 26 | + response = await IDOM_CACHE.aget(cache_key, version=last_modified_time) |
| 27 | + if response is None: |
| 28 | + async with async_open(path, "r") as fp: |
| 29 | + response = HttpResponse(await fp.read(), content_type="text/javascript") |
| 30 | + await IDOM_CACHE.adelete(cache_key) |
| 31 | + await IDOM_CACHE.aset( |
| 32 | + cache_key, response, timeout=None, version=last_modified_time |
| 33 | + ) |
| 34 | + return response |
0 commit comments