Ship pyfetch-based fetch utilities in lib/, run asyncio scripts via nest-asyncio in the worker, and add sample demos for HTTPS in the browser. Co-authored-by: Cursor <cursoragent@cursor.com>
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""Browser-friendly async HTTP for the Pyodide worker.
|
|
|
|
These helpers use ``pyodide.http.pyfetch``, which maps to the browser
|
|
``fetch`` API (no Python TLS stack). Prefer them for ``https://`` in
|
|
Pyodide: ``aiohttp`` in the worker often raises
|
|
``RuntimeError('SSL is not supported.')`` for HTTPS even though the wheel
|
|
exists, because user-level SSL is not wired the same as on CPython.
|
|
|
|
The browser's normal rules apply: the page is served over HTTPS so
|
|
``http://`` URLs are blocked as mixed content, and the response host must
|
|
send permissive CORS headers (e.g. ``Access-Control-Allow-Origin``) or the
|
|
browser hides the body even if the request succeeded.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
async def fetch_text(url: str) -> str:
|
|
from pyodide.http import pyfetch
|
|
|
|
r = await pyfetch(url)
|
|
return await r.text()
|
|
|
|
|
|
async def fetch_bytes(url: str) -> bytes:
|
|
from pyodide.http import pyfetch
|
|
|
|
r = await pyfetch(url)
|
|
return await r.bytes()
|
|
|
|
|
|
async def fetch_json(url: str, **kwargs: Any) -> Any:
|
|
from pyodide.http import pyfetch
|
|
|
|
r = await pyfetch(url)
|
|
return await r.json(**kwargs)
|