anr.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const object = require('./object.js');
  3. const nodeStackTrace = require('./node-stack-trace.js');
  4. /**
  5. * A node.js watchdog timer
  6. * @param pollInterval The interval that we expect to get polled at
  7. * @param anrThreshold The threshold for when we consider ANR
  8. * @param callback The callback to call for ANR
  9. * @returns An object with `poll` and `enabled` functions {@link WatchdogReturn}
  10. */
  11. function watchdogTimer(
  12. createTimer,
  13. pollInterval,
  14. anrThreshold,
  15. callback,
  16. ) {
  17. const timer = createTimer();
  18. let triggered = false;
  19. let enabled = true;
  20. setInterval(() => {
  21. const diffMs = timer.getTimeMs();
  22. if (triggered === false && diffMs > pollInterval + anrThreshold) {
  23. triggered = true;
  24. if (enabled) {
  25. callback();
  26. }
  27. }
  28. if (diffMs < pollInterval + anrThreshold) {
  29. triggered = false;
  30. }
  31. }, 20);
  32. return {
  33. poll: () => {
  34. timer.reset();
  35. },
  36. enabled: (state) => {
  37. enabled = state;
  38. },
  39. };
  40. }
  41. // types copied from inspector.d.ts
  42. /**
  43. * Converts Debugger.CallFrame to Sentry StackFrame
  44. */
  45. function callFrameToStackFrame(
  46. frame,
  47. url,
  48. getModuleFromFilename,
  49. ) {
  50. const filename = url ? url.replace(/^file:\/\//, '') : undefined;
  51. // CallFrame row/col are 0 based, whereas StackFrame are 1 based
  52. const colno = frame.location.columnNumber ? frame.location.columnNumber + 1 : undefined;
  53. const lineno = frame.location.lineNumber ? frame.location.lineNumber + 1 : undefined;
  54. return object.dropUndefinedKeys({
  55. filename,
  56. module: getModuleFromFilename(filename),
  57. function: frame.functionName || '?',
  58. colno,
  59. lineno,
  60. in_app: filename ? nodeStackTrace.filenameIsInApp(filename) : undefined,
  61. });
  62. }
  63. exports.callFrameToStackFrame = callFrameToStackFrame;
  64. exports.watchdogTimer = watchdogTimer;
  65. //# sourceMappingURL=anr.js.map