Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions astrbot/dashboard/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import hashlib
import logging
import mimetypes
import os
import socket
from datetime import datetime
Expand Down Expand Up @@ -44,6 +45,13 @@ class _AddrWithPort(Protocol):
APP: Quart


def _ensure_static_asset_mime_types() -> None:
# Override platform-specific mappings so module scripts are always served
# with a JavaScript MIME type, especially on Windows hosts.
mimetypes.add_type("application/javascript", ".js")
mimetypes.add_type("application/javascript", ".mjs")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For one-time initializations like this, it's best practice to execute them at the module level. This ensures the setup runs only once when the module is imported, avoiding redundant calls if AstrBotDashboard is instantiated multiple times.

Please call _ensure_static_asset_mime_types() here, and remove the call from AstrBotDashboard.__init__.

Suggested change
_ensure_static_asset_mime_types()


def _parse_env_bool(value: str | None, default: bool) -> bool:
if value is None:
return default
Expand All @@ -68,6 +76,7 @@ def __init__(
self.core_lifecycle = core_lifecycle
self.config = core_lifecycle.astrbot_config
self.db = db
_ensure_static_asset_mime_types()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the MIME type registration is now handled at the module level, this call inside __init__ is redundant and should be removed.


# Path priority:
# 1. Explicit webui_dir argument
Expand Down
17 changes: 17 additions & 0 deletions tests/test_dashboard_static_mime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mimetypes

from astrbot.dashboard.server import _ensure_static_asset_mime_types


def test_ensure_static_asset_mime_types_registers_javascript_types(monkeypatch):
calls = []

def fake_add_type(mime_type: str, extension: str, strict: bool = True):
calls.append((mime_type, extension, strict))

monkeypatch.setattr(mimetypes, "add_type", fake_add_type)

_ensure_static_asset_mime_types()

assert ("application/javascript", ".js", True) in calls
assert ("application/javascript", ".mjs", True) in calls
Loading