跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

test-native-debugger.mjs

74 行 · 3.1 KB历史下载
1// Run with the native Node.js binary inside the guest, e.g. node test-native-debugger.mjs win32 x64.
2import assert from 'node:assert/strict';
3import { once } from 'node:events';
4import { mkdtemp, realpath, rm, writeFile } from 'node:fs/promises';
5import { Session } from 'node:inspector/promises';
6import { tmpdir } from 'node:os';
7import path from 'node:path';
8import { pathToFileURL } from 'node:url';
9import { isMainThread, parentPort, Worker, workerData } from 'node:worker_threads';
10
11async function debugInvoice(url) {
12 // A debugger on its own thread can inspect the paused main thread without
13 // opening a TCP port or relying on an external developer service.
14 const session = new Session();
15 session.connectToMainThread();
16 try {
17 await session.post('Debugger.enable');
18 const { breakpointId } = await session.post('Debugger.setBreakpointByUrl', { url, lineNumber: 2 });
19 const paused = once(session, 'Debugger.paused', { signal: AbortSignal.timeout(20000) });
20 parentPort.postMessage('ready');
21 const [{ params }] = await paused;
22 try {
23 assert.ok(params.hitBreakpoints.includes(breakpointId));
24 const [frame] = params.callFrames;
25 assert.equal(frame.functionName, 'invoiceTotal');
26 assert.equal(frame.location.lineNumber + 1, 3);
27 const { result, exceptionDetails } = await session.post('Debugger.evaluateOnCallFrame', {
28 callFrameId: frame.callFrameId, expression: 'subtotal', throwOnSideEffect: true, returnByValue: true,
29 });
30 assert.equal(exceptionDetails, undefined);
31 assert.equal(result.value, 120);
32 return { file: 'invoice.mjs', line: 3, function: frame.functionName, subtotal: result.value };
33 } finally {
34 await session.post('Debugger.resume');
35 }
36 } finally {
37 session.disconnect();
38 }
39}
40
41if (isMainThread) {
42 assert.equal(process.platform, process.argv[2], 'Unexpected guest operating system');
43 assert.equal(process.arch, process.argv[3], 'Unexpected Node.js architecture');
44 const directory = await mkdtemp(path.join(tmpdir(), 'Cage native project '));
45 let debuggerWorker;
46 try {
47 const url = pathToFileURL(path.join(await realpath(directory), 'invoice.mjs'));
48 await writeFile(url, [
49 'export function invoiceTotal(quantity, unitPrice) {',
50 ' const subtotal = quantity * unitPrice;',
51 ' return subtotal + 12;',
52 '}',
53 ].join('\n'));
54 debuggerWorker = new Worker(new URL(import.meta.url), { workerData: url.href });
55 const [ready] = await once(debuggerWorker, 'message');
56 assert.equal(ready, 'ready');
57 const report = once(debuggerWorker, 'message');
58 const { invoiceTotal } = await import(url);
59 const total = invoiceTotal(5, 24);
60 const [breakpoint] = await report;
61 assert.equal(total, 132);
62 console.log(JSON.stringify({ os: process.platform, arch: process.arch, node: process.version, breakpoint, total }));
63 } finally {
64 await debuggerWorker?.terminate();
65 await rm(directory, { recursive: true, force: true });
66 }
67} else {
68 parentPort.ref();
69 try {
70 parentPort.postMessage(await debugInvoice(workerData));
71 } finally {
72 parentPort.unref();
73 }
74}