PromiseMonitor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /** @license MIT License (c) copyright 2010-2014 original author or authors */
  2. /** @author Brian Cavalier */
  3. /** @author John Hann */
  4. (function(define) { 'use strict';
  5. define(function(require) {
  6. var defaultStackJumpSeparator = 'from execution context:';
  7. var defaultStackFilter = /[\s\(\/\\](node|module|timers)\.js:|when([\/\\]{1,2}(lib|monitor|es6-shim)[\/\\]{1,2}|\.js)|(new\sPromise)\b|(\b(PromiseMonitor|ConsoleReporter|Scheduler|RunHandlerTask|ProgressTask|Promise|.*Handler)\.[\w_]\w\w+\b)|\b(tryCatch\w+|getHandler\w*)\b/i;
  8. var setTimer = require('../lib/env').setTimer;
  9. var error = require('./error');
  10. var executionContext = [];
  11. function PromiseMonitor(reporter) {
  12. this.logDelay = 0;
  13. this.stackFilter = defaultStackFilter;
  14. this.stackJumpSeparator = defaultStackJumpSeparator;
  15. this.filterDuplicateFrames = true;
  16. this._reporter = reporter;
  17. if(typeof reporter.configurePromiseMonitor === 'function') {
  18. reporter.configurePromiseMonitor(this);
  19. }
  20. this._traces = [];
  21. this._traceTask = 0;
  22. var self = this;
  23. this._doLogTraces = function() {
  24. self._logTraces();
  25. };
  26. }
  27. PromiseMonitor.prototype.monitor = function(Promise) {
  28. var self = this;
  29. Promise.createContext = function(p, context) {
  30. p.context = self.createContext(p, context);
  31. };
  32. Promise.enterContext = function(p) {
  33. executionContext.push(p.context);
  34. };
  35. Promise.exitContext = function() {
  36. executionContext.pop();
  37. };
  38. Promise.onPotentiallyUnhandledRejection = function(rejection, extraContext) {
  39. return self.addTrace(rejection, extraContext);
  40. };
  41. Promise.onPotentiallyUnhandledRejectionHandled = function(rejection) {
  42. return self.removeTrace(rejection);
  43. };
  44. Promise.onFatalRejection = function(rejection, extraContext) {
  45. return self.fatal(rejection, extraContext);
  46. };
  47. return this;
  48. };
  49. PromiseMonitor.prototype.createContext = function(at, parentContext) {
  50. var context = {
  51. parent: parentContext || executionContext[executionContext.length - 1],
  52. stack: void 0
  53. };
  54. error.captureStack(context, at.constructor);
  55. return context;
  56. };
  57. PromiseMonitor.prototype.addTrace = function(handler, extraContext) {
  58. var t, i;
  59. for(i = this._traces.length-1; i >= 0; --i) {
  60. t = this._traces[i];
  61. if(t.handler === handler) {
  62. break;
  63. }
  64. }
  65. if(i >= 0) {
  66. t.extraContext = extraContext;
  67. } else {
  68. this._traces.push({
  69. handler: handler,
  70. extraContext: extraContext
  71. });
  72. }
  73. this.logTraces();
  74. };
  75. PromiseMonitor.prototype.removeTrace = function(/*handler*/) {
  76. this.logTraces();
  77. };
  78. PromiseMonitor.prototype.fatal = function(handler, extraContext) {
  79. var err = new Error();
  80. err.stack = this._createLongTrace(handler.value, handler.context, extraContext).join('\n');
  81. setTimer(function() {
  82. throw err;
  83. }, 0);
  84. };
  85. PromiseMonitor.prototype.logTraces = function() {
  86. if(!this._traceTask) {
  87. this._traceTask = setTimer(this._doLogTraces, this.logDelay);
  88. }
  89. };
  90. PromiseMonitor.prototype._logTraces = function() {
  91. this._traceTask = void 0;
  92. this._traces = this._traces.filter(filterHandled);
  93. this._reporter.log(this.formatTraces(this._traces));
  94. };
  95. PromiseMonitor.prototype.formatTraces = function(traces) {
  96. return traces.map(function(t) {
  97. return this._createLongTrace(t.handler.value, t.handler.context, t.extraContext);
  98. }, this);
  99. };
  100. PromiseMonitor.prototype._createLongTrace = function(e, context, extraContext) {
  101. var trace = error.parse(e) || [String(e) + ' (WARNING: non-Error used)'];
  102. trace = filterFrames(this.stackFilter, trace, 0);
  103. this._appendContext(trace, context);
  104. this._appendContext(trace, extraContext);
  105. return this.filterDuplicateFrames ? this._removeDuplicates(trace) : trace;
  106. };
  107. PromiseMonitor.prototype._removeDuplicates = function(trace) {
  108. var seen = {};
  109. var sep = this.stackJumpSeparator;
  110. var count = 0;
  111. return trace.reduceRight(function(deduped, line, i) {
  112. if(i === 0) {
  113. deduped.unshift(line);
  114. } else if(line === sep) {
  115. if(count > 0) {
  116. deduped.unshift(line);
  117. count = 0;
  118. }
  119. } else if(!seen[line]) {
  120. seen[line] = true;
  121. deduped.unshift(line);
  122. ++count;
  123. }
  124. return deduped;
  125. }, []);
  126. };
  127. PromiseMonitor.prototype._appendContext = function(trace, context) {
  128. trace.push.apply(trace, this._createTrace(context));
  129. };
  130. PromiseMonitor.prototype._createTrace = function(traceChain) {
  131. var trace = [];
  132. var stack;
  133. while(traceChain) {
  134. stack = error.parse(traceChain);
  135. if (stack) {
  136. stack = filterFrames(this.stackFilter, stack);
  137. appendStack(trace, stack, this.stackJumpSeparator);
  138. }
  139. traceChain = traceChain.parent;
  140. }
  141. return trace;
  142. };
  143. function appendStack(trace, stack, separator) {
  144. if (stack.length > 1) {
  145. stack[0] = separator;
  146. trace.push.apply(trace, stack);
  147. }
  148. }
  149. function filterFrames(stackFilter, stack) {
  150. return stack.filter(function(frame) {
  151. return !stackFilter.test(frame);
  152. });
  153. }
  154. function filterHandled(t) {
  155. return !t.handler.handled;
  156. }
  157. return PromiseMonitor;
  158. });
  159. }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));