impplement pageing between native host and browser extension
This commit is contained in:
+83
-15
@@ -22,7 +22,26 @@ from browser_cli.platform import DEFAULT_ALIAS, endpoint_for_alias, is_windows,
|
||||
SOCKET_PATH: str = "" # set after hello handshake
|
||||
PENDING: dict[str, queue.Queue] = {}
|
||||
PENDING_LOCK = threading.Lock()
|
||||
WRITE_LOCK = threading.Lock()
|
||||
REGISTRY_PATH = registry_path()
|
||||
PAGE_SIZE = int(os.environ.get("BROWSER_CLI_PAGE_SIZE", "100"))
|
||||
PAGEABLE_COMMANDS = {
|
||||
"tabs.list",
|
||||
"tabs.filter",
|
||||
"tabs.query",
|
||||
"group.list",
|
||||
"group.tabs",
|
||||
"group.query",
|
||||
"windows.list",
|
||||
"dom.query",
|
||||
"dom.text",
|
||||
"dom.attr",
|
||||
"extract.links",
|
||||
"extract.images",
|
||||
"extract.json",
|
||||
"cookies.list",
|
||||
"session.list",
|
||||
}
|
||||
|
||||
# --- Native Messaging protocol (4-byte LE length prefix + UTF-8 JSON) ---
|
||||
|
||||
@@ -143,21 +162,7 @@ def handle_cli_connection(conn, listener=None) -> None:
|
||||
if "id" not in cmd:
|
||||
cmd["id"] = str(uuid.uuid4())
|
||||
|
||||
msg_id = cmd["id"]
|
||||
response_queue: queue.Queue = queue.Queue()
|
||||
|
||||
with PENDING_LOCK:
|
||||
PENDING[msg_id] = response_queue
|
||||
|
||||
write_native_message(sys.stdout.buffer, cmd)
|
||||
|
||||
try:
|
||||
result = response_queue.get(timeout=30)
|
||||
except queue.Empty:
|
||||
result = {"id": msg_id, "success": False, "error": "timeout waiting for browser response"}
|
||||
|
||||
with PENDING_LOCK:
|
||||
PENDING.pop(msg_id, None)
|
||||
result = _handle_browser_command(cmd)
|
||||
|
||||
response = json.dumps(result).encode("utf-8")
|
||||
if is_windows():
|
||||
@@ -179,6 +184,69 @@ def handle_cli_connection(conn, listener=None) -> None:
|
||||
listener.close()
|
||||
|
||||
|
||||
def _handle_browser_command(cmd: dict) -> dict:
|
||||
command = cmd.get("command")
|
||||
if command in PAGEABLE_COMMANDS:
|
||||
return _collect_paged_browser_command(cmd)
|
||||
return _send_browser_command(cmd)
|
||||
|
||||
|
||||
def _send_browser_command(cmd: dict, timeout: int = 30) -> dict:
|
||||
msg_id = cmd.get("id") or str(uuid.uuid4())
|
||||
cmd["id"] = msg_id
|
||||
response_queue: queue.Queue = queue.Queue()
|
||||
|
||||
with PENDING_LOCK:
|
||||
PENDING[msg_id] = response_queue
|
||||
|
||||
try:
|
||||
with WRITE_LOCK:
|
||||
write_native_message(sys.stdout.buffer, cmd)
|
||||
|
||||
try:
|
||||
return response_queue.get(timeout=timeout)
|
||||
except queue.Empty:
|
||||
return {"id": msg_id, "success": False, "error": "timeout waiting for browser response"}
|
||||
finally:
|
||||
with PENDING_LOCK:
|
||||
PENDING.pop(msg_id, None)
|
||||
|
||||
|
||||
def _collect_paged_browser_command(cmd: dict) -> dict:
|
||||
original_id = cmd.get("id") or str(uuid.uuid4())
|
||||
offset = 0
|
||||
items = []
|
||||
total = None
|
||||
|
||||
while True:
|
||||
page_cmd = dict(cmd)
|
||||
page_cmd["id"] = str(uuid.uuid4())
|
||||
page_args = dict(cmd.get("args") or {})
|
||||
page_args["__page"] = {"offset": offset, "limit": PAGE_SIZE}
|
||||
page_cmd["args"] = page_args
|
||||
|
||||
result = _send_browser_command(page_cmd)
|
||||
result["id"] = original_id
|
||||
if not result.get("success", True):
|
||||
return result
|
||||
|
||||
data = result.get("data")
|
||||
if not isinstance(data, dict) or data.get("__browserCliPage") is not True:
|
||||
return result
|
||||
|
||||
page_items = data.get("items") or []
|
||||
if not isinstance(page_items, list):
|
||||
return {"id": original_id, "success": False, "error": "invalid paged response from browser"}
|
||||
items.extend(page_items)
|
||||
total = data.get("total", total)
|
||||
next_offset = data.get("nextOffset")
|
||||
if next_offset is None:
|
||||
break
|
||||
offset = int(next_offset)
|
||||
|
||||
return {"id": original_id, "success": True, "data": items, "pageSize": PAGE_SIZE, "total": total}
|
||||
|
||||
|
||||
# --- Socket helpers (length-prefixed framing) ---
|
||||
|
||||
def _send_all(conn: socket.socket, data: bytes) -> None:
|
||||
|
||||
Reference in New Issue
Block a user