|
| 1 | +import type { StackFrame } from '@sentry/types'; |
| 2 | +import { dropUndefinedKeys, filenameIsInApp } from '@sentry/utils'; |
| 3 | +import type { Debugger } from 'inspector'; |
| 4 | + |
| 5 | +import { getModuleFromFilename } from '../module'; |
| 6 | +import { createWebSocketClient } from './websocket'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Converts Debugger.CallFrame to Sentry StackFrame |
| 10 | + */ |
| 11 | +function callFrameToStackFrame( |
| 12 | + frame: Debugger.CallFrame, |
| 13 | + filenameFromScriptId: (id: string) => string | undefined, |
| 14 | +): StackFrame { |
| 15 | + const filename = filenameFromScriptId(frame.location.scriptId)?.replace(/^file:\/\//, ''); |
| 16 | + |
| 17 | + // CallFrame row/col are 0 based, whereas StackFrame are 1 based |
| 18 | + const colno = frame.location.columnNumber ? frame.location.columnNumber + 1 : undefined; |
| 19 | + const lineno = frame.location.lineNumber ? frame.location.lineNumber + 1 : undefined; |
| 20 | + |
| 21 | + return dropUndefinedKeys({ |
| 22 | + filename, |
| 23 | + module: getModuleFromFilename(filename), |
| 24 | + function: frame.functionName || '?', |
| 25 | + colno, |
| 26 | + lineno, |
| 27 | + in_app: filename ? filenameIsInApp(filename) : undefined, |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +// The only messages we care about |
| 32 | +type DebugMessage = |
| 33 | + | { |
| 34 | + method: 'Debugger.scriptParsed'; |
| 35 | + params: Debugger.ScriptParsedEventDataType; |
| 36 | + } |
| 37 | + | { method: 'Debugger.paused'; params: Debugger.PausedEventDataType }; |
| 38 | + |
| 39 | +/** |
| 40 | + * Wraps a websocket connection with the basic logic of the Node debugger protocol. |
| 41 | + * @param url The URL to connect to |
| 42 | + * @param onMessage A callback that will be called with each return message from the debugger |
| 43 | + * @returns A function that can be used to send commands to the debugger |
| 44 | + */ |
| 45 | +async function webSocketDebugger( |
| 46 | + url: string, |
| 47 | + onMessage: (message: DebugMessage) => void, |
| 48 | +): Promise<(method: string, params?: unknown) => void> { |
| 49 | + let id = 0; |
| 50 | + const webSocket = await createWebSocketClient(url); |
| 51 | + |
| 52 | + webSocket.on('message', (data: Buffer) => { |
| 53 | + const message = JSON.parse(data.toString()) as DebugMessage; |
| 54 | + onMessage(message); |
| 55 | + }); |
| 56 | + |
| 57 | + return (method: string, params?: unknown) => { |
| 58 | + webSocket.send(JSON.stringify({ id: id++, method, params })); |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Captures stack traces from the Node debugger. |
| 64 | + * @param url The URL to connect to |
| 65 | + * @param callback A callback that will be called with the stack frames |
| 66 | + * @returns A function that triggers the debugger to pause and capture a stack trace |
| 67 | + */ |
| 68 | +export async function captureStackTrace(url: string, callback: (frames: StackFrame[]) => void): Promise<() => void> { |
| 69 | + // Collect scriptId -> url map so we can look up the filenames later |
| 70 | + const scripts = new Map<string, string>(); |
| 71 | + |
| 72 | + const sendCommand = await webSocketDebugger(url, message => { |
| 73 | + if (message.method === 'Debugger.scriptParsed') { |
| 74 | + scripts.set(message.params.scriptId, message.params.url); |
| 75 | + } else if (message.method === 'Debugger.paused') { |
| 76 | + // copy the frames |
| 77 | + const callFrames = [...message.params.callFrames]; |
| 78 | + // and resume immediately! |
| 79 | + sendCommand('Debugger.resume'); |
| 80 | + sendCommand('Debugger.disable'); |
| 81 | + |
| 82 | + const frames = callFrames |
| 83 | + .map(frame => callFrameToStackFrame(frame, id => scripts.get(id))) |
| 84 | + // Sentry expects the frames to be in the opposite order |
| 85 | + .reverse(); |
| 86 | + |
| 87 | + callback(frames); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + return () => { |
| 92 | + sendCommand('Debugger.enable'); |
| 93 | + sendCommand('Debugger.pause'); |
| 94 | + }; |
| 95 | +} |
0 commit comments