mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:30:42 +00:00
perf: inline browser ws decoder
This commit is contained in:
@@ -6,7 +6,6 @@ import {
|
||||
type SsrFPolicy,
|
||||
resolvePinnedHostnameWithPolicy,
|
||||
} from "../infra/net/ssrf.js";
|
||||
import { rawDataToString } from "../infra/ws.js";
|
||||
import { redactSensitiveText } from "../logging/redact.js";
|
||||
import { getDirectAgentForCdp, withNoProxyForCdpUrl } from "./cdp-proxy-bypass.js";
|
||||
import { CDP_HTTP_REQUEST_TIMEOUT_MS, CDP_WS_HANDSHAKE_TIMEOUT_MS } from "./cdp-timeouts.js";
|
||||
@@ -151,6 +150,22 @@ export type CdpSendFn = (
|
||||
sessionId?: string,
|
||||
) => Promise<unknown>;
|
||||
|
||||
function rawCdpMessageToString(data: WebSocket.RawData): string {
|
||||
if (typeof data === "string") {
|
||||
return data;
|
||||
}
|
||||
if (Buffer.isBuffer(data)) {
|
||||
return data.toString("utf8");
|
||||
}
|
||||
if (Array.isArray(data)) {
|
||||
return Buffer.concat(data).toString("utf8");
|
||||
}
|
||||
if (ArrayBuffer.isView(data)) {
|
||||
return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString("utf8");
|
||||
}
|
||||
return Buffer.from(data).toString("utf8");
|
||||
}
|
||||
|
||||
export function getHeadersWithAuth(url: string, headers: Record<string, string> = {}) {
|
||||
const mergedHeaders = { ...headers };
|
||||
try {
|
||||
@@ -246,7 +261,7 @@ function createCdpSender(ws: WebSocket) {
|
||||
|
||||
ws.on("message", (data) => {
|
||||
try {
|
||||
const parsed = JSON.parse(rawDataToString(data)) as CdpResponse;
|
||||
const parsed = JSON.parse(rawCdpMessageToString(data)) as CdpResponse;
|
||||
if (typeof parsed.id !== "number") {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1 +1,18 @@
|
||||
export { rawDataToString } from "openclaw/plugin-sdk/browser-node-runtime";
|
||||
export function rawDataToString(data: unknown): string {
|
||||
if (typeof data === "string") {
|
||||
return data;
|
||||
}
|
||||
if (Buffer.isBuffer(data)) {
|
||||
return data.toString("utf8");
|
||||
}
|
||||
if (Array.isArray(data)) {
|
||||
return Buffer.concat(data).toString("utf8");
|
||||
}
|
||||
if (ArrayBuffer.isView(data)) {
|
||||
return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString("utf8");
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return Buffer.from(data).toString("utf8");
|
||||
}
|
||||
return String(data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user