| 1 | |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { once } from 'node:events'; |
| 4 | import { mkdtemp, realpath, rm, writeFile } from 'node:fs/promises'; |
| 5 | import { Session } from 'node:inspector/promises'; |
| 6 | import { tmpdir } from 'node:os'; |
| 7 | import path from 'node:path'; |
| 8 | import { pathToFileURL } from 'node:url'; |
| 9 | import { isMainThread, parentPort, Worker, workerData } from 'node:worker_threads'; |
| 10 | |
| 11 | async function debugInvoice(url) { |
| 12 | |
| 13 | |
| 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 | |
| 41 | if (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 | } |