Files
openclaw/extensions/browser/src/plugin-service.ts
T
Peter Steinberger 45572dbf36 fix(gateway): keep shutdown reliable during updates (#124582)
* fix(gateway): resolve shutdown dependencies before stopping

* fix(gateway): isolate prepared shutdown runtimes
2026-08-16 07:13:36 -07:00

64 lines
2.1 KiB
TypeScript

/**
* Browser plugin service factory that lazily starts the control server.
*/
import { isTruthyEnvValue } from "openclaw/plugin-sdk/runtime-env";
import {
startLazyPluginServiceModule,
type LazyPluginServiceHandle,
type OpenClawPluginService,
} from "./sdk-node-runtime.js";
type BrowserControlHandle = LazyPluginServiceHandle | null;
const EAGER_BROWSER_CONTROL_SERVICE_ENV = "OPENCLAW_EAGER_BROWSER_CONTROL_SERVER";
const UNSAFE_BROWSER_CONTROL_OVERRIDE_SPECIFIER = /^(?:data|http|https|node):/i;
function validateBrowserControlOverrideSpecifier(specifier: string): string {
const trimmed = specifier.trim();
if (UNSAFE_BROWSER_CONTROL_OVERRIDE_SPECIFIER.test(trimmed)) {
throw new Error(`Refusing unsafe browser control override specifier: ${trimmed}`);
}
return trimmed;
}
/** Creates the Browser plugin service registered by the plugin entrypoint. */
export function createBrowserPluginService(params: {
stopOnDemand: () => Promise<void>;
}): OpenClawPluginService {
let handle: BrowserControlHandle = null;
return {
id: "browser-control",
start: async () => {
if (!isTruthyEnvValue(process.env[EAGER_BROWSER_CONTROL_SERVICE_ENV])) {
return;
}
if (handle) {
return;
}
handle = await startLazyPluginServiceModule({
skipEnvVar: "OPENCLAW_SKIP_BROWSER_CONTROL_SERVER",
overrideEnvVar: "OPENCLAW_BROWSER_CONTROL_MODULE",
validateOverrideSpecifier: validateBrowserControlOverrideSpecifier,
// Keep the default module import static so compiled builds still bundle it.
loadDefaultModule: async () => await import("./server.js"),
startExportNames: [
"startBrowserControlServiceFromConfig",
"startBrowserControlServerFromConfig",
],
stopExportNames: ["stopBrowserControlService", "stopBrowserControlServer"],
});
},
stop: async () => {
const current = handle;
if (current) {
await current.stop();
if (handle === current) {
handle = null;
}
return;
}
await params.stopOnDemand();
},
};
}