Skip to content

js_transport

Class info

🛈 DocStrings

Pyodide fetch-based transport for httpx (used when running under Pyodide).

get_js_transport_class cached

get_js_transport_class(httpx_name: str | None = None) -> type[Any]

Return a Pyodide fetch transport class for given httpx flavour.

Targets Pyodide 314.x (Python 3.14), where to_js already turns dicts into plain JS objects (checked in a browser against 314.0.8); dict_converter=js.Object.fromEntries is kept explicitly because fetch() ignores options passed as a JS Map.

Parameters:

Name Type Description Default
httpx_name str | None

Module name of the httpx flavour ("httpx2" or "httpx"). Defaults to httpx2 if installed, else httpx.

None
Source code in src/anyenv/download/httpx_backend/js_transport.py
@functools.cache
def get_js_transport_class(httpx_name: str | None = None) -> type[Any]:
    """Return a Pyodide fetch transport class for given httpx flavour.

    Targets Pyodide 314.x (Python 3.14), where ``to_js`` already turns dicts into plain JS
    objects (checked in a browser against 314.0.8); ``dict_converter=js.Object.fromEntries``
    is kept explicitly because ``fetch()`` ignores options passed as a JS ``Map``.

    Args:
        httpx_name: Module name of the httpx flavour ("httpx2" or "httpx").
                    Defaults to httpx2 if installed, else httpx.
    """
    httpx_mod = get_httpx_module(httpx_name)

    class JSTransport(httpx_mod.AsyncBaseTransport):  # type: ignore[name-defined,misc]
        """Send httpx requests through the browser's ``fetch``."""

        async def handle_async_request(self, request: Any) -> Any:
            """Handle an asynchronous HTTP request using the browser fetch API."""
            import js  # type: ignore[import-not-found]
            from pyodide.ffi import to_js  # type: ignore[import-not-found]

            body = await request.aread()
            options: dict[str, Any] = {
                "method": request.method,
                "headers": dict(request.headers),
                "mode": "cors",
                "redirect": "follow",
            }
            if body and request.method.upper() not in _BODYLESS_METHODS:
                options["body"] = body
            if (
                (timeout := _request_timeout(request)) is not None
                and (abort_signal := getattr(js, "AbortSignal", None)) is not None
                and hasattr(abort_signal, "timeout")
            ):
                options["signal"] = abort_signal.timeout(round(timeout * 1000))
            try:
                fetch_response = await js.fetch(
                    str(request.url), to_js(options, dict_converter=js.Object.fromEntries)
                )
                buffer = await fetch_response.arrayBuffer()
            except Exception as exc:  # JsException: network error, CORS, timeout, abort
                if getattr(exc, "name", None) == "TimeoutError":
                    raise httpx_mod.TimeoutException(str(exc), request=request) from exc
                raise httpx_mod.ConnectError(str(exc), request=request) from exc
            headers = [
                (str(k), str(v))
                for k, v in fetch_response.headers.entries()
                if str(k).lower() not in _STALE_RESPONSE_HEADERS
            ]
            extensions: dict[str, Any] = {}
            if status_text := getattr(fetch_response, "statusText", ""):
                extensions["reason_phrase"] = str(status_text).encode("ascii", errors="ignore")
            return httpx_mod.Response(
                status_code=fetch_response.status,
                headers=headers,
                content=buffer.to_bytes(),
                request=request,
                extensions=extensions,
            )

    JSTransport.__module__ = __name__
    return JSTransport