Serve¶
serve ¶
HTTP server mode for wavexis using aiohttp.
aiohttp is an optional dependency under the [serve] extra.
All imports are lazy — WavexisError is raised if aiohttp is not installed.
TokenBucket ¶
Token bucket rate limiter for the HTTP API.
Allows up to capacity requests per refill_period seconds.
Tokens refill continuously at a rate of capacity/refill_period per second.
Source code in wavexis/serve.py
__init__ ¶
Initialize the token bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
Maximum number of tokens (burst size). |
required |
refill_period
|
float
|
Seconds to fully refill from empty. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If capacity or refill_period are not valid. |
Source code in wavexis/serve.py
acquire
async
¶
Try to acquire a token.
Returns:
| Type | Description |
|---|---|
bool
|
True if a token was acquired, False if rate limited. |
Source code in wavexis/serve.py
retry_after
async
¶
Return seconds until the next token is available.
Source code in wavexis/serve.py
BackendPool ¶
Concurrency limiter and connection pool for browser backends.
Uses a semaphore to cap the number of simultaneous browser instances. Maintains a pool of reusable backend instances to avoid launching a new browser per request.
get_backend acquires a slot and return_backend/discard_backend
release it, so callers cannot leak the semaphore if backend creation fails.
Source code in wavexis/serve.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
get_backend
async
¶
Acquire a slot and get a backend from the pool or create a new one.
Reuses an idle backend if available, otherwise creates a new one.
The acquired slot is released by return_backend or
discard_backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preferred
|
str | None
|
Preferred backend name for new instances. |
None
|
Returns:
| Type | Description |
|---|---|
AbstractBackend
|
A backend instance (may or may not be launched yet). |
Source code in wavexis/serve.py
return_backend
async
¶
Return a backend to the pool for reuse and release its slot.
The backend is returned without closing it so it can be reused
by subsequent requests. Backends are closed only by close_all
during shutdown or when the pool is full.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
AbstractBackend
|
The backend instance to return. |
required |
Source code in wavexis/serve.py
discard_backend
async
¶
Close a broken backend and release its slot.
Use this when a backend failed to launch or is in an unknown state and must not be reused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
AbstractBackend
|
The backend instance to discard. |
required |
Source code in wavexis/serve.py
close_all
async
¶
Close all pooled backends, drain the pool, and reset all slots.
Source code in wavexis/serve.py
set_allowed_base_dir ¶
Set the base directory that serve-mode file paths must be inside of.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Absolute path to the allowed base directory, or None to allow any path (default, not recommended for production). |
required |
Source code in wavexis/serve.py
with_backend ¶
with_backend(launch_options: BrowserOptions | None = None) -> Callable[[Callable[..., Any]], Callable[[Any], Any]]
Decorator that manages backend lifecycle for serve handlers.
Acquires a backend from the pool, launches it, calls the handler with the backend, and ensures cleanup in a finally block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
launch_options
|
BrowserOptions | None
|
BrowserOptions to pass to launch(). Defaults to a plain BrowserOptions(). |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., Any]], Callable[[Any], Any]]
|
A decorator function. |
Source code in wavexis/serve.py
handle_screenshot
async
¶
Handle POST /screenshot — return PNG bytes.
Source code in wavexis/serve.py
handle_pdf
async
¶
Handle POST /pdf — return PDF bytes.
Source code in wavexis/serve.py
handle_eval
async
¶
Handle POST /eval — return JSON result.
Source code in wavexis/serve.py
handle_scrape
async
¶
Handle POST /scrape — return JSON or CSV.
Source code in wavexis/serve.py
handle_dom_get
async
¶
Handle POST /dom/get — return HTML as JSON.
Source code in wavexis/serve.py
handle_dom_query
async
¶
Handle POST /dom/query — return elements as JSON.
Source code in wavexis/serve.py
handle_navigate
async
¶
Handle POST /navigate — navigate and return status.
Source code in wavexis/serve.py
handle_har
async
¶
Handle POST /har — return HAR data as JSON.
Source code in wavexis/serve.py
handle_cookies_get
async
¶
Handle POST /cookies/get — return cookies as JSON.
Source code in wavexis/serve.py
handle_cookies_set
async
¶
Handle POST /cookies/set — set a cookie and return status.
Source code in wavexis/serve.py
handle_input_click
async
¶
Handle POST /input/click — click an element.
Source code in wavexis/serve.py
handle_input_type
async
¶
Handle POST /input/type — type text into an element.
Source code in wavexis/serve.py
handle_perf_metrics
async
¶
Handle POST /perf/metrics — return performance metrics.
Source code in wavexis/serve.py
handle_perf_trace
async
¶
Handle POST /perf/trace — return performance trace.
Source code in wavexis/serve.py
handle_health
async
¶
handle_backends
async
¶
Handle GET /backends — return available backends.
Source code in wavexis/serve.py
handle_version
async
¶
handle_cwv
async
¶
Handle POST /cwv — measure Core Web Vitals with scoring.
Body: {"url": "...", "observe_ms": 5000, "budgets": {"lcp_ms": 2500}}
Source code in wavexis/serve.py
handle_auth
async
¶
Handle POST /auth — apply auth context and navigate.
Source code in wavexis/serve.py
handle_user_agent
async
¶
Handle POST /user-agent — set custom user agent.
Source code in wavexis/serve.py
handle_headers
async
¶
Handle POST /headers — set custom HTTP headers.
Source code in wavexis/serve.py
handle_device
async
¶
Handle POST /device — emulate a device preset.
Source code in wavexis/serve.py
handle_modify_request
async
¶
Handle POST /modify-request — intercept and modify requests in-flight.
{"url": "...", "pattern": "/api/",
"modifications": {"headers": [...], "method": "...", "post_data": "..."}}
Source code in wavexis/serve.py
handle_modify_response
async
¶
Handle POST /modify-response — intercept and modify responses in-flight.
{"url": "...", "pattern": "/api/",
"modifications": {"status": 200, "body": "...", "content_type": "application/json"}}
Source code in wavexis/serve.py
handle_multi
async
¶
Handle POST /multi — execute multiple actions from YAML.
Source code in wavexis/serve.py
set_ws_max_connections ¶
Set the maximum number of concurrent WebSocket connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_conn
|
int
|
Maximum concurrent WebSocket connections allowed. |
required |
Source code in wavexis/serve.py
set_ws_max_messages_per_minute ¶
Set the maximum number of WebSocket messages per minute per connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_messages
|
int
|
Maximum messages per minute allowed per WebSocket connection. |
required |
Source code in wavexis/serve.py
handle_websocket
async
¶
Handle GET /ws — WebSocket endpoint for real-time streaming.
Server streams events as JSON messages until the client disconnects.
Source code in wavexis/serve.py
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 | |
handle_plugins
async
¶
Handle GET /plugins — list discovered plugins.
Source code in wavexis/serve.py
create_app ¶
create_app(backend_name: str | None = None, rate_limit: int | None = None, base_dir: str | None = None, api_key: str | None = None, cors_origins: list[str] | None = None, max_concurrent: int = 5, max_request_size: int = 10 * 1024 * 1024) -> Any
Create and configure the aiohttp web application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend_name
|
str | None
|
Preferred backend name (e.g. "cdp", "bidi"). If None, auto-detects the first available backend. |
None
|
rate_limit
|
int | None
|
Max requests per minute (0 or None = no limit). |
None
|
base_dir
|
str | None
|
Base directory for validating file paths in requests. If None, file path access is disabled. |
None
|
api_key
|
str | None
|
If set, all requests must include this key as a Bearer
token or |
None
|
cors_origins
|
list[str] | None
|
List of allowed CORS origins. Use ["*"] for all. |
None
|
max_concurrent
|
int
|
Max number of concurrent browser backends. |
5
|
max_request_size
|
int
|
Maximum request body size in bytes (default 10MB). |
10 * 1024 * 1024
|
Returns:
| Type | Description |
|---|---|
Any
|
aiohttp.web.Application with all routes registered. |
Raises:
| Type | Description |
|---|---|
WavexisError
|
If aiohttp is not installed. |
BackendNotAvailableError
|
If no backend is available. |
Source code in wavexis/serve.py
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 | |
serve ¶
serve(port: int = 8080, host: str = 'localhost', backend: str | None = None, rate_limit: int | None = None, base_dir: str | None = None, api_key: str | None = None, cors_origins: list[str] | None = None, max_concurrent: int = 5, max_request_size: int = 10 * 1024 * 1024) -> None
Start the wavexis HTTP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
int
|
Port to listen on (default 8080). |
8080
|
host
|
str
|
Host to bind to (default "localhost"). |
'localhost'
|
backend
|
str | None
|
Preferred backend name (default auto-detect). |
None
|
rate_limit
|
int | None
|
Max requests per minute (0 or None = no limit). |
None
|
base_dir
|
str | None
|
Base directory for validating file paths in requests. |
None
|
api_key
|
str | None
|
If set, all requests must include this key. |
None
|
cors_origins
|
list[str] | None
|
List of allowed CORS origins. Use ["*"] for all. |
None
|
max_concurrent
|
int
|
Max concurrent browser backends (default 5). |
5
|
max_request_size
|
int
|
Maximum request body size in bytes (default 10MB). |
10 * 1024 * 1024
|
Raises:
| Type | Description |
|---|---|
WavexisError
|
If aiohttp is not installed. |
BackendNotAvailableError
|
If no backend is available. |