openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
133 lines (132 loc) • 4.71 kB
JavaScript
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { a as createDeferred } from "./extension-shared-CJ4teJ7p.mjs";
import "./runtime-env-D-Pa5lD6.mjs";
import { a as readSystemProfileCookies, n as assertSystemCookiePlatform, o as resolveSystemCookieSource, s as cacheKeychainSecret, t as parseSystemProfileDomains } from "./system-profile-domains-BKFvpfPm.mjs";
import { c as runBrowserCliCommand, n as callBrowserRequest } from "./browser-cli-shared-BEDAHNiM.mjs";
import fs from "node:fs";
import path from "node:path";
//#region extensions/browser/src/cli/browser-cli-cookie-sync.ts
/** Host-local macOS system-cookie sync into a managed Browser profile. */
const COOKIE_SYNC_DEBOUNCE_MS = 1500;
function formatCookieSyncSummary(e) {
const t = e.domains.length > 0 ? e.domains.join(",") : "none";
return `cookie sync ${e.browser}/${e.systemProfile} -> ${e.into} via ${e.gateway}: total=${e.total} pushed=${e.pushed} skipped=${e.skipped} failed=${e.failed} domains=${t}`;
}
async function pushSystemProfileCookies(e) {
const t = await readSystemProfileCookies({
browser: e.options.browser,
systemProfile: e.options.system,
domains: e.domains,
signal: e.signal
}, { readSecret: e.readSecret });
let n = 0;
if (t.cookies.length > 0) n = (await callBrowserRequest(e.parent, {
method: "POST",
path: "/cookies/set-many",
query: { profile: e.options.into },
body: { cookies: t.cookies }
})).added;
return {
browser: t.browser,
systemProfile: t.systemProfile,
into: e.options.into,
gateway: e.parent.url?.trim() || "configured/default",
total: t.counts.total,
pushed: n,
skipped: t.counts.skipped,
failed: t.counts.failed + Math.max(0, t.cookies.length - n),
domains: t.domains.toSorted()
};
}
function registerBrowserCookieSyncCommand(e, t) {
e.command("cookie-sync").description("Sync allowlisted macOS Chrome-family cookies into a managed profile").option("--domains <list>", "Required comma-separated domain allowlist").option("--into <profile>", "Target managed Browser profile", "imported").option("--browser <browser>", "System browser: chrome, brave, edge, or chromium", "chrome").option("--system <id>", "System browser profile directory", "Default").option("--watch", "Watch the source Cookies database and re-sync changes", false).action((e, n) => runBrowserCliCommand(async () => {
if (e.domains === void 0) throw new Error("--domains is required; cookie sync never sends an unrestricted cookie jar");
const r = parseSystemProfileDomains(e.domains.split(","));
if (!r) throw new Error("--domains must include at least one non-empty domain");
const i = t(n);
if (e.watch) {
assertSystemCookiePlatform();
const t = await resolveSystemCookieSource({
browser: e.browser,
systemProfile: e.system
});
const n = new AbortController();
const a = await cacheKeychainSecret(t.browser, n.signal);
let o;
let s = false;
let c = false;
let l = false;
let u;
const d = createDeferred();
const settleStopped = () => {
if (u) d.reject(u);
else d.resolve();
};
const runCycle = async () => {
if (l) return;
if (s) {
c = true;
return;
}
c = false;
s = true;
try {
const t = await pushSystemProfileCookies({
options: e,
parent: i,
domains: r,
readSecret: a,
signal: n.signal
});
defaultRuntime.error(formatCookieSyncSummary(t));
} catch (e) {
if (!l) defaultRuntime.error(`cookie sync failed: ${String(e)}`);
} finally {
s = false;
if (l) settleStopped();
else if (c) runCycle();
}
};
const f = path.basename(t.cookiesFile);
const p = fs.watch(path.dirname(t.cookiesFile), (e, t) => {
const n = t === null ? null : path.basename(t);
if (n !== null && n !== f && !n.startsWith(`${f}-`)) return;
clearTimeout(o);
o = setTimeout(() => void runCycle(), COOKIE_SYNC_DEBOUNCE_MS);
});
const stop = () => {
if (l) return;
l = true;
clearTimeout(o);
p.close();
n.abort(/* @__PURE__ */ new Error("cookie sync stopped"));
if (!s) settleStopped();
};
const fail = (e) => {
u = e;
stop();
};
p.on("error", fail);
process.once("SIGINT", stop);
process.once("SIGTERM", stop);
try {
await runCycle();
await d.promise;
} finally {
process.removeListener("SIGINT", stop);
process.removeListener("SIGTERM", stop);
p.removeListener("error", fail);
stop();
}
return;
}
const a = await pushSystemProfileCookies({
options: e,
parent: i,
domains: r
});
defaultRuntime.log(formatCookieSyncSummary(a));
}));
}
//#endregion
export { registerBrowserCookieSyncCommand };