Compare commits

...
Author SHA1 Message Date
dni 842fb53742 mock import 2026-07-09 08:19:25 +02:00
dni 2f9b3723ae black 2026-07-09 08:05:59 +02:00
dni 04397b85f0 also activate extension for start task in test 2026-07-09 08:05:59 +02:00
dni 8982852516 dont start tasks twice 2026-07-09 08:05:59 +02:00
dni e2bee23daf fix pydantic v1 errors 2026-07-09 08:05:59 +02:00
dni a822523460 clear cached modules 2026-07-09 08:05:59 +02:00
dni 4971f4c5c0 fix import 2026-07-09 08:05:59 +02:00
dni d157c43ebb remove old files 2026-07-09 08:05:59 +02:00
dni bb53fe41c8 remove uppgrade_hash 2026-07-09 08:05:59 +02:00
dni 5865a3f96a refactor: get rid of url rewrites for extensions /upgrades/
should open the way for custom root-path
2026-07-09 08:05:59 +02:00
10 changed files with 72 additions and 86 deletions
+46 -12
View File
@@ -386,7 +386,6 @@ def register_custom_extensions_path():
upgrades_dir = settings.lnbits_extensions_upgrade_path
shutil.rmtree(upgrades_dir, True)
Path(upgrades_dir).mkdir(parents=True, exist_ok=True)
sys.path.append(str(upgrades_dir))
if settings.has_default_extension_path:
return
@@ -441,10 +440,54 @@ def register_ext_tasks(ext: Extension) -> None:
def register_ext_routes(app: FastAPI, ext: Extension) -> None:
"""Register FastAPI routes for extension."""
ext_module = importlib.import_module(ext.module_name)
module_name = ext.module_name
# Clear all cached sub-modules so a fresh import picks up new files from ext_dir.
# A simple reload() would reuse cached sub-modules (e.g. views_api) and serve
# stale code even after the extension files have been replaced on disk.
stale = [
k for k in sys.modules if k == module_name or k.startswith(f"{module_name}.")
]
for k in stale:
del sys.modules[k]
if stale:
# Pydantic v1 keeps a global _FUNCS set of validator qualnames to detect
# duplicates. Clear the extension's entries so reimport doesn't raise
# "duplicate validator" errors for validators with the same qualname.
try:
import pydantic.class_validators as _pydantic_cv
_pydantic_cv._FUNCS = {
f for f in _pydantic_cv._FUNCS if not f.startswith(f"{module_name}.")
}
except (ImportError, AttributeError):
pass
ext_module = importlib.import_module(module_name)
ext_route = getattr(ext_module, f"{ext.code}_ext")
ext_redirects = (
getattr(ext_module, f"{ext.code}_redirect_paths")
if hasattr(ext_module, f"{ext.code}_redirect_paths")
else []
)
settings.activate_extension_paths(ext.code, ext_redirects)
# Remove existing routes for this extension before re-registering so that
# an upgraded extension replaces the old one at the same paths (no prefix).
ext_prefix = f"/{ext.code}"
app.router.routes = [
r
for r in app.router.routes
if not (
getattr(r, "path", "") == ext_prefix
or getattr(r, "path", "").startswith(f"{ext_prefix}/")
)
]
# Invalidate FastAPI's cached OpenAPI schema so the next /openapi.json
# request reflects the updated routes.
app.openapi_schema = None
if hasattr(ext_module, f"{ext.code}_static_files"):
ext_statics = getattr(ext_module, f"{ext.code}_static_files")
for s in ext_statics:
@@ -453,17 +496,8 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None:
)
app.mount(s["path"], StaticFiles(directory=static_dir), s["name"])
ext_redirects = (
getattr(ext_module, f"{ext.code}_redirect_paths")
if hasattr(ext_module, f"{ext.code}_redirect_paths")
else []
)
settings.activate_extension_paths(ext.code, ext.upgrade_hash, ext_redirects)
logger.trace(f"Adding route for extension {ext_module}.")
prefix = f"/upgrades/{ext.upgrade_hash}" if ext.upgrade_hash != "" else ""
app.include_router(router=ext_route, prefix=prefix)
app.include_router(router=ext_route)
async def check_and_register_extensions(app: FastAPI) -> None:
-5
View File
@@ -1,6 +1,5 @@
import asyncio
import importlib
import sys
import time
from functools import wraps
from getpass import getpass
@@ -377,10 +376,6 @@ async def extensions_update( # noqa: C901
if not await _can_run_operation(url):
return
upgrades_dir = settings.lnbits_extensions_upgrade_path
Path(upgrades_dir).mkdir(parents=True, exist_ok=True)
sys.path.append(str(upgrades_dir))
if extension:
await update_extension(extension, repo_index, source_repo, url, admin_user)
return
+1 -12
View File
@@ -147,21 +147,13 @@ class Extension(BaseModel):
name: str | None = None
short_description: str | None = None
tile: str | None = None
upgrade_hash: str | None = ""
@property
def module_name(self) -> str:
if self.is_upgrade_extension:
return f"{self.code}-{self.upgrade_hash}"
if settings.has_default_extension_path:
return f"lnbits.extensions.{self.code}"
return self.code
@property
def is_upgrade_extension(self) -> bool:
return self.upgrade_hash != ""
@classmethod
def from_installable_ext(cls, ext_info: InstallableExtension) -> Extension:
return Extension(
@@ -170,7 +162,6 @@ class Extension(BaseModel):
name=ext_info.name,
short_description=ext_info.short_description,
tile=ext_info.icon,
upgrade_hash=ext_info.hash if ext_info.ext_upgrade_dir.is_dir() else "",
)
@@ -375,9 +366,6 @@ class InstallableExtension(BaseModel):
@property
def module_name(self) -> str:
if self.ext_upgrade_dir.is_dir():
return f"{self.id}-{self.hash}"
if settings.has_default_extension_path:
return f"lnbits.extensions.{self.id}"
return self.id
@@ -468,6 +456,7 @@ class InstallableExtension(BaseModel):
shutil.rmtree(self.ext_dir, True)
shutil.copytree(Path(self.ext_upgrade_dir), Path(self.ext_dir))
shutil.rmtree(self.ext_upgrade_dir, True)
logger.info(f"Extension {self.name} ({self.installed_version}) extracted.")
def clean_extension_files(self):
+15 -19
View File
@@ -56,14 +56,10 @@ async def install_extension(
else:
await update_installed_extension(ext_info)
extension = Extension.from_installable_ext(ext_info)
if extension.is_upgrade_extension:
# call stop while the old routes are still active
if installed_ext:
await stop_extension_background_work(ext_info.id)
await start_extension_background_work(ext_info.id)
return extension
return Extension.from_installable_ext(ext_info)
async def check_extensions_limit(installed_ext: InstallableExtension | None = None):
@@ -103,16 +99,16 @@ async def stop_extension_background_work(ext_id: str) -> bool:
Stop background work for extension (like asyncio.Tasks, WebSockets, etc).
Extension must expose a `myextension_stop()` function if it is starting tasks.
"""
upgrade_hash = settings.extension_upgrade_hash(ext_id)
ext = Extension(code=ext_id, is_valid=True, upgrade_hash=upgrade_hash)
ext = Extension(code=ext_id, is_valid=True)
module_name = ext.module_name
try:
logger.info(f"Stopping background work for extension '{ext.module_name}'.")
old_module = importlib.import_module(ext.module_name)
logger.info(f"Stopping background work for extension '{module_name}'.")
old_module = importlib.import_module(module_name)
stop_fn_name = f"{ext_id}_stop"
if not hasattr(old_module, stop_fn_name):
raise ValueError(f"No stop function found for '{ext.module_name}'.")
raise ValueError(f"No stop function found for '{module_name}'.")
stop_fn = getattr(old_module, stop_fn_name)
if stop_fn:
@@ -120,9 +116,9 @@ async def stop_extension_background_work(ext_id: str) -> bool:
await stop_fn()
else:
stop_fn()
logger.info(f"Stopped background work for extension '{ext.module_name}'.")
logger.info(f"Stopped background work for extension '{module_name}'.")
except Exception as ex:
logger.warning(f"Failed to stop background work for '{ext.module_name}'.")
logger.warning(f"Failed to stop background work for '{module_name}'.")
logger.warning(ex)
return False
@@ -135,12 +131,12 @@ async def start_extension_background_work(ext_id: str) -> bool:
Extension CAN expose a `myextension_start()` function if it is starting tasks.
Extension MUST expose a `myextension_stop()` in that case.
"""
upgrade_hash = settings.extension_upgrade_hash(ext_id)
ext = Extension(code=ext_id, is_valid=True, upgrade_hash=upgrade_hash)
ext = Extension(code=ext_id, is_valid=True)
module_name = ext.module_name
try:
logger.info(f"Starting background work for extension '{ext.module_name}'.")
new_module = importlib.import_module(ext.module_name)
logger.info(f"Starting background work for extension '{module_name}'.")
new_module = importlib.import_module(module_name)
start_fn_name = f"{ext_id}_start"
# start function is optional, return False if not found
@@ -153,10 +149,10 @@ async def start_extension_background_work(ext_id: str) -> bool:
await start_fn()
else:
start_fn()
logger.info(f"Started background work for extension '{ext.module_name}'.")
logger.info(f"Started background work for extension '{module_name}'.")
return True
except Exception as ex:
logger.warning(f"Failed to start background work for '{ext.module_name}'.")
logger.warning(f"Failed to start background work for '{module_name}'.")
logger.warning(ex)
return False
+1 -6
View File
@@ -310,12 +310,7 @@ def get_api_routes(routes: list) -> dict[str, str]:
def path_segments(path: str) -> list[str]:
path = path.strip("/")
segments = path.split("/")
if len(segments) < 2:
return segments
if segments[0] == "upgrades":
return segments[2:]
return segments[0:]
return path.split("/")
def normalize_path(path: str | None) -> str:
-8
View File
@@ -51,14 +51,6 @@ class InstalledExtensionMiddleware:
await self.app(scope, receive, send)
return
# re-route all trafic if the extension has been upgraded
if top_path in settings.lnbits_upgraded_extensions:
upgrade_path = (
f"""{settings.lnbits_upgraded_extensions[top_path]}/{top_path}"""
)
tail = "/".join(rest)
scope["path"] = f"/upgrades/{upgrade_path}/{tail}"
await self.app(scope, receive, send)
def _response_by_accepted_type(
-13
View File
@@ -166,8 +166,6 @@ class ExchangeRateProvider(BaseModel):
class InstalledExtensionsSettings(LNbitsSettings):
# installed extensions that have been deactivated
lnbits_deactivated_extensions: set[str] = Field(default=set())
# upgraded extensions that require API redirects
lnbits_upgraded_extensions: dict[str, str] = Field(default={})
# list of redirects that extensions want to perform
lnbits_extensions_redirects: list[RedirectPath] = Field(default=[])
@@ -190,18 +188,10 @@ class InstalledExtensionsSettings(LNbitsSettings):
def activate_extension_paths(
self,
ext_id: str,
upgrade_hash: str | None = None,
ext_redirects: list[dict] | None = None,
):
self.lnbits_deactivated_extensions.discard(ext_id)
"""
Update the list of upgraded extensions. The middleware will perform
redirects based on this
"""
if upgrade_hash:
self.lnbits_upgraded_extensions[ext_id] = upgrade_hash
if ext_redirects:
self._activate_extension_redirects(ext_id, ext_redirects)
@@ -211,9 +201,6 @@ class InstalledExtensionsSettings(LNbitsSettings):
self.lnbits_deactivated_extensions.add(ext_id)
self._remove_extension_redirects(ext_id)
def extension_upgrade_hash(self, ext_id: str) -> str:
return settings.lnbits_upgraded_extensions.get(ext_id, "")
def _activate_extension_redirects(self, ext_id: str, ext_redirects: list[dict]):
ext_redirect_paths = [
RedirectPath(**{"ext_id": ext_id, **er}) for er in ext_redirects
-2
View File
@@ -259,9 +259,7 @@ def test_get_api_routes_extracts_v1_paths():
def test_path_and_case_helpers():
assert path_segments("/wallet/path") == ["wallet", "path"]
assert path_segments("/upgrades/ext/assets/app.js") == ["assets", "app.js"]
assert normalize_path(None) == "/"
assert normalize_path("/upgrades/ext/assets/app.js") == "/assets/app.js"
assert normalize_endpoint("example.com/") == "https://example.com"
assert normalize_endpoint("ws://socket.example.com") == "ws://socket.example.com"
assert (
+8 -3
View File
@@ -62,6 +62,9 @@ async def test_install_extension_creates_new_extension_and_starts_background_wor
"lnbits.core.services.extensions.start_extension_background_work",
mocker.AsyncMock(return_value=True),
)
mocker.patch(
"lnbits.core.services.extensions.core_app_extra.register_new_ext_routes"
)
mocker.patch(
"lnbits.core.services.extensions.get_db_version",
mocker.AsyncMock(return_value=0),
@@ -76,6 +79,7 @@ async def test_install_extension_creates_new_extension_and_starts_background_wor
settings.lnbits_extensions_path = str(tmp_path / "code")
extension = await install_extension(ext_info)
await activate_extension(extension) # starts background task
stored = await get_installed_extension(ext_id)
finally:
await delete_installed_extension(ext_id=ext_id)
@@ -111,6 +115,9 @@ async def test_install_extension_updates_existing_upgrade_and_preserves_payments
"lnbits.core.services.extensions.stop_extension_background_work",
mocker.AsyncMock(return_value=True),
)
mocker.patch(
"lnbits.core.services.extensions.core_app_extra.register_new_ext_routes"
)
mocker.patch(
"lnbits.core.services.extensions.get_db_version",
mocker.AsyncMock(return_value=1),
@@ -124,9 +131,8 @@ async def test_install_extension_updates_existing_upgrade_and_preserves_payments
settings.lnbits_data_folder = str(tmp_path / "data")
settings.lnbits_extensions_path = str(tmp_path / "code")
await create_installed_extension(existing_ext)
updated_ext.ext_upgrade_dir.mkdir(parents=True, exist_ok=True)
extension = await install_extension(updated_ext, skip_download=True)
await activate_extension(extension) # starts background task
stored = await get_installed_extension(ext_id)
finally:
await delete_installed_extension(ext_id=ext_id)
@@ -134,7 +140,6 @@ async def test_install_extension_updates_existing_upgrade_and_preserves_payments
settings.lnbits_extensions_path = original_extensions_path
assert extension.code == ext_id
assert extension.is_upgrade_extension is True
assert stored is not None
assert stored.meta is not None
assert stored.meta.payments == [existing_payment]
+1 -6
View File
@@ -216,16 +216,11 @@ def test_installed_extensions_settings_activate_and_deactivate_paths():
}
]
installed.activate_extension_paths(
"lnurlp",
upgrade_hash="hash123",
ext_redirects=redirects,
)
installed.activate_extension_paths("lnurlp", ext_redirects=redirects)
redirect = installed.find_extension_redirect("/.well-known/lnurlp", [])
assert redirect is not None
assert redirect.ext_id == "lnurlp"
assert installed.lnbits_upgraded_extensions["lnurlp"] == "hash123"
assert "lnurlp" in installed.lnbits_installed_extensions_ids
installed.deactivate_extension_paths("lnurlp")