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 |
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.
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.
Source code in wavexis/serve.py
acquire
async
¶
release ¶
get_backend
async
¶
Get a backend from the pool or create a new one.
Reuses an idle backend if available, otherwise creates a new one.
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
AbstractBackend
|
The backend instance to return. |
required |
Source code in wavexis/serve.py
close_all
async
¶
Close all pooled backends and clear the pool.
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
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
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | |
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) -> 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
|
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
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 | |
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) -> 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
|
Raises:
| Type | Description |
|---|---|
WavexisError
|
If aiohttp is not installed. |
BackendNotAvailableError
|
If no backend is available. |