Lines 196-236javascript
196 const result = await runner(process.execPath, [injector, ...args]);
197 if (!result || result.code !== 0) {
198 const detail = result?.stderr?.trim() || result?.stdout?.trim() || `exit code ${result?.code ?? "unknown"}`;
199 throw new Error(detail);
201 return result.stdout?.trim() ?? "";
204async function loadOwnershipHelper(libraryRoot, { requireInstalled = false } = {}) {
205 const installed = path.join(libraryRoot, "runtime", "lib", "cdp-owner.mjs");
206 const candidates = requireInstalled ? [installed] : [
208 path.join(PACKAGE_ROOT, "resources", "runtime", "lib", "cdp-owner.mjs"),
209 path.join(REPOSITORY_ROOT, "runtime", "lib", "cdp-owner.mjs"),
211 const helperPath = candidates.find(existsSync);
213 throw new Error(requireInstalled
214 ? `CDP ownership helper is not installed: ${installed}`
215 : "CDP ownership helper is unavailable in the library, package resources, and source repository.");
217 return import(pathToFileURL(helperPath).href);
218}
MediumDynamic Require
Package source references dynamic require/import behavior.
src/runtime-control.mjsView on unpkg · L216 220async function captureScreenshot(libraryRoot, port, outputPath, {
221 fetchImpl = globalThis.fetch,
223 ownershipPlatform = process.platform,
226 const ownership = await loadOwnershipHelper(libraryRoot, { requireInstalled: true });
227 const targets = await ownership.listOwnedCodexTargets(port, { platform: ownershipPlatform, runner: ownershipRunner, fetchImpl });
228 const target = targets[0] ?? null;
229 if (!target?.webSocketDebuggerUrl) throw new Error("The verified endpoint has no main Codex renderer.");
230 await ownership.verifyCodexCdpOwner({ port, platform: ownershipPlatform, runner: ownershipRunner });
231 const endpoint = ownership.assertLoopbackWebSocket(target.webSocketDebuggerUrl, port);
232 const createWebSocket = webSocketFactory ?? ((url) => {
233 if (typeof WebSocket !== "function") throw new Error("Node.js 22 or later is required to capture a runtime screenshot.");
234 return new WebSocket(url);
236 const socket = createWebSocket(endpoint);