common.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. /**
  3. * Creates a rate limiter that will call the disable callback when the rate limit is reached and the enable callback
  4. * when a timeout has occurred.
  5. * @param maxPerSecond Maximum number of calls per second
  6. * @param enable Callback to enable capture
  7. * @param disable Callback to disable capture
  8. * @returns A function to call to increment the rate limiter count
  9. */
  10. function createRateLimiter(
  11. maxPerSecond,
  12. enable,
  13. disable,
  14. ) {
  15. let count = 0;
  16. let retrySeconds = 5;
  17. let disabledTimeout = 0;
  18. setInterval(() => {
  19. if (disabledTimeout === 0) {
  20. if (count > maxPerSecond) {
  21. retrySeconds *= 2;
  22. disable(retrySeconds);
  23. // Cap at one day
  24. if (retrySeconds > 86400) {
  25. retrySeconds = 86400;
  26. }
  27. disabledTimeout = retrySeconds;
  28. }
  29. } else {
  30. disabledTimeout -= 1;
  31. if (disabledTimeout === 0) {
  32. enable();
  33. }
  34. }
  35. count = 0;
  36. }, 1000).unref();
  37. return () => {
  38. count += 1;
  39. };
  40. }
  41. // Add types for the exception event data
  42. /** Could this be an anonymous function? */
  43. function isAnonymous(name) {
  44. return name !== undefined && (name.length === 0 || name === '?' || name === '<anonymous>');
  45. }
  46. /** Do the function names appear to match? */
  47. function functionNamesMatch(a, b) {
  48. return a === b || (isAnonymous(a) && isAnonymous(b));
  49. }
  50. /** Creates a unique hash from stack frames */
  51. function hashFrames(frames) {
  52. if (frames === undefined) {
  53. return;
  54. }
  55. // Only hash the 10 most recent frames (ie. the last 10)
  56. return frames.slice(-10).reduce((acc, frame) => `${acc},${frame.function},${frame.lineno},${frame.colno}`, '');
  57. }
  58. /**
  59. * We use the stack parser to create a unique hash from the exception stack trace
  60. * This is used to lookup vars when the exception passes through the event processor
  61. */
  62. function hashFromStack(stackParser, stack) {
  63. if (stack === undefined) {
  64. return undefined;
  65. }
  66. return hashFrames(stackParser(stack, 1));
  67. }
  68. exports.createRateLimiter = createRateLimiter;
  69. exports.functionNamesMatch = functionNamesMatch;
  70. exports.hashFrames = hashFrames;
  71. exports.hashFromStack = hashFromStack;
  72. exports.isAnonymous = isAnonymous;
  73. //# sourceMappingURL=common.js.map