|
| 1 | +import type { StackFrame } from '@sentry/types'; |
| 2 | + |
| 3 | +import { dropUndefinedKeys } from './object'; |
| 4 | +import { filenameIsInApp, stripSentryFramesAndReverse } from './stacktrace'; |
| 5 | + |
| 6 | +type WatchdogReturn = { |
| 7 | + /** Resets the watchdog timer */ |
| 8 | + poll: () => void; |
| 9 | + /** Enables or disables the watchdog timer */ |
| 10 | + enabled: (state: boolean) => void; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * A node.js watchdog timer |
| 15 | + * @param pollInterval The interval that we expect to get polled at |
| 16 | + * @param anrThreshold The threshold for when we consider ANR |
| 17 | + * @param callback The callback to call for ANR |
| 18 | + * @returns An object with `poll` and `enabled` functions {@link WatchdogReturn} |
| 19 | + */ |
| 20 | +export function watchdogTimer(pollInterval: number, anrThreshold: number, callback: () => void): WatchdogReturn { |
| 21 | + let lastPoll = process.hrtime(); |
| 22 | + let triggered = false; |
| 23 | + let enabled = true; |
| 24 | + |
| 25 | + setInterval(() => { |
| 26 | + const [seconds, nanoSeconds] = process.hrtime(lastPoll); |
| 27 | + const diffMs = Math.floor(seconds * 1e3 + nanoSeconds / 1e6); |
| 28 | + |
| 29 | + if (triggered === false && diffMs > pollInterval + anrThreshold) { |
| 30 | + triggered = true; |
| 31 | + if (enabled) { |
| 32 | + callback(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (diffMs < pollInterval + anrThreshold) { |
| 37 | + triggered = false; |
| 38 | + } |
| 39 | + }, 20); |
| 40 | + |
| 41 | + return { |
| 42 | + poll: () => { |
| 43 | + lastPoll = process.hrtime(); |
| 44 | + }, |
| 45 | + enabled: (state: boolean) => { |
| 46 | + enabled = state; |
| 47 | + }, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +// types copied from inspector.d.ts |
| 52 | +interface Location { |
| 53 | + scriptId: string; |
| 54 | + lineNumber: number; |
| 55 | + columnNumber?: number; |
| 56 | +} |
| 57 | + |
| 58 | +interface CallFrame { |
| 59 | + functionName: string; |
| 60 | + location: Location; |
| 61 | + url: string; |
| 62 | +} |
| 63 | + |
| 64 | +interface ScriptParsedEventDataType { |
| 65 | + scriptId: string; |
| 66 | + url: string; |
| 67 | +} |
| 68 | + |
| 69 | +interface PausedEventDataType { |
| 70 | + callFrames: CallFrame[]; |
| 71 | + reason: string; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Converts Debugger.CallFrame to Sentry StackFrame |
| 76 | + */ |
| 77 | +function callFrameToStackFrame( |
| 78 | + frame: CallFrame, |
| 79 | + url: string | undefined, |
| 80 | + getModuleFromFilename: (filename: string | undefined) => string | undefined, |
| 81 | +): StackFrame { |
| 82 | + const filename = url ? url.replace(/^file:\/\//, '') : undefined; |
| 83 | + |
| 84 | + // CallFrame row/col are 0 based, whereas StackFrame are 1 based |
| 85 | + const colno = frame.location.columnNumber ? frame.location.columnNumber + 1 : undefined; |
| 86 | + const lineno = frame.location.lineNumber ? frame.location.lineNumber + 1 : undefined; |
| 87 | + |
| 88 | + return dropUndefinedKeys({ |
| 89 | + filename, |
| 90 | + module: getModuleFromFilename(filename), |
| 91 | + function: frame.functionName || '?', |
| 92 | + colno, |
| 93 | + lineno, |
| 94 | + in_app: filename ? filenameIsInApp(filename) : undefined, |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +// The only messages we care about |
| 99 | +type DebugMessage = |
| 100 | + | { method: 'Debugger.scriptParsed'; params: ScriptParsedEventDataType } |
| 101 | + | { method: 'Debugger.paused'; params: PausedEventDataType }; |
| 102 | + |
| 103 | +/** |
| 104 | + * Creates a message handler from the v8 debugger protocol and passed stack frames to the callback when paused. |
| 105 | + */ |
| 106 | +export function createDebugPauseMessageHandler( |
| 107 | + sendCommand: (message: string) => void, |
| 108 | + getModuleFromFilename: (filename?: string) => string | undefined, |
| 109 | + pausedStackFrames: (frames: StackFrame[]) => void, |
| 110 | +): (message: DebugMessage) => void { |
| 111 | + // Collect scriptId -> url map so we can look up the filenames later |
| 112 | + const scripts = new Map<string, string>(); |
| 113 | + |
| 114 | + return message => { |
| 115 | + if (message.method === 'Debugger.scriptParsed') { |
| 116 | + scripts.set(message.params.scriptId, message.params.url); |
| 117 | + } else if (message.method === 'Debugger.paused') { |
| 118 | + // copy the frames |
| 119 | + const callFrames = [...message.params.callFrames]; |
| 120 | + // and resume immediately |
| 121 | + sendCommand('Debugger.resume'); |
| 122 | + sendCommand('Debugger.disable'); |
| 123 | + |
| 124 | + const stackFrames = stripSentryFramesAndReverse( |
| 125 | + callFrames.map(frame => |
| 126 | + callFrameToStackFrame(frame, scripts.get(frame.location.scriptId), getModuleFromFilename), |
| 127 | + ), |
| 128 | + ); |
| 129 | + |
| 130 | + pausedStackFrames(stackFrames); |
| 131 | + } |
| 132 | + }; |
| 133 | +} |
0 commit comments