stack.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.lazyJoinStacks = exports.joinStacks = exports.isWritableStack = exports.isLazyStack = void 0;
  4. const newline = /\r?\n/;
  5. const onoCall = /\bono[ @]/;
  6. /**
  7. * Is the property lazily computed?
  8. */
  9. function isLazyStack(stackProp) {
  10. return Boolean(stackProp &&
  11. stackProp.configurable &&
  12. typeof stackProp.get === "function");
  13. }
  14. exports.isLazyStack = isLazyStack;
  15. /**
  16. * Is the stack property writable?
  17. */
  18. function isWritableStack(stackProp) {
  19. return Boolean(
  20. // If there is no stack property, then it's writable, since assigning it will create it
  21. !stackProp ||
  22. stackProp.writable ||
  23. typeof stackProp.set === "function");
  24. }
  25. exports.isWritableStack = isWritableStack;
  26. /**
  27. * Appends the original `Error.stack` property to the new Error's stack.
  28. */
  29. function joinStacks(newError, originalError) {
  30. let newStack = popStack(newError.stack);
  31. let originalStack = originalError ? originalError.stack : undefined;
  32. if (newStack && originalStack) {
  33. return newStack + "\n\n" + originalStack;
  34. }
  35. else {
  36. return newStack || originalStack;
  37. }
  38. }
  39. exports.joinStacks = joinStacks;
  40. /**
  41. * Calls `joinStacks` lazily, when the `Error.stack` property is accessed.
  42. */
  43. function lazyJoinStacks(lazyStack, newError, originalError) {
  44. if (originalError) {
  45. Object.defineProperty(newError, "stack", {
  46. get: () => {
  47. let newStack = lazyStack.get.apply(newError);
  48. return joinStacks({ stack: newStack }, originalError);
  49. },
  50. enumerable: false,
  51. configurable: true
  52. });
  53. }
  54. else {
  55. lazyPopStack(newError, lazyStack);
  56. }
  57. }
  58. exports.lazyJoinStacks = lazyJoinStacks;
  59. /**
  60. * Removes Ono from the stack, so that the stack starts at the original error location
  61. */
  62. function popStack(stack) {
  63. if (stack) {
  64. let lines = stack.split(newline);
  65. // Find the Ono call(s) in the stack, and remove them
  66. let onoStart;
  67. for (let i = 0; i < lines.length; i++) {
  68. let line = lines[i];
  69. if (onoCall.test(line)) {
  70. if (onoStart === undefined) {
  71. // We found the first Ono call in the stack trace.
  72. // There may be other subsequent Ono calls as well.
  73. onoStart = i;
  74. }
  75. }
  76. else if (onoStart !== undefined) {
  77. // We found the first non-Ono call after one or more Ono calls.
  78. // So remove the Ono call lines from the stack trace
  79. lines.splice(onoStart, i - onoStart);
  80. break;
  81. }
  82. }
  83. if (lines.length > 0) {
  84. return lines.join("\n");
  85. }
  86. }
  87. // If we get here, then the stack doesn't contain a call to `ono`.
  88. // This may be due to minification or some optimization of the JS engine.
  89. // So just return the stack as-is.
  90. return stack;
  91. }
  92. /**
  93. * Calls `popStack` lazily, when the `Error.stack` property is accessed.
  94. */
  95. function lazyPopStack(error, lazyStack) {
  96. Object.defineProperty(error, "stack", {
  97. get: () => popStack(lazyStack.get.apply(error)),
  98. enumerable: false,
  99. configurable: true
  100. });
  101. }
  102. //# sourceMappingURL=stack.js.map