diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
commit | abd94a7f5a50f43c797a11b53549ae48fff667c3 (patch) | |
tree | ab8ed457f65cdd72e13e0571d2975729428f1551 /node_modules/when/monitor | |
parent | a0247c6a3fd6a09a41a7e35a3441324c4dcb58be (diff) |
add node_modules to address #4364
Diffstat (limited to 'node_modules/when/monitor')
-rw-r--r-- | node_modules/when/monitor/ConsoleReporter.js | 106 | ||||
-rw-r--r-- | node_modules/when/monitor/PromiseMonitor.js | 197 | ||||
-rw-r--r-- | node_modules/when/monitor/README.md | 3 | ||||
-rw-r--r-- | node_modules/when/monitor/console.js | 14 | ||||
-rw-r--r-- | node_modules/when/monitor/error.js | 86 |
5 files changed, 406 insertions, 0 deletions
diff --git a/node_modules/when/monitor/ConsoleReporter.js b/node_modules/when/monitor/ConsoleReporter.js new file mode 100644 index 000000000..2b0c97411 --- /dev/null +++ b/node_modules/when/monitor/ConsoleReporter.js @@ -0,0 +1,106 @@ +/** @license MIT License (c) copyright 2010-2014 original author or authors */ +/** @author Brian Cavalier */ +/** @author John Hann */ + +(function(define) { 'use strict'; +define(function(require) { + + var error = require('./error'); + var unhandledRejectionsMsg = '[promises] Unhandled rejections: '; + var allHandledMsg = '[promises] All previously unhandled rejections have now been handled'; + + function ConsoleReporter() { + this._previouslyReported = false; + } + + ConsoleReporter.prototype = initDefaultLogging(); + + ConsoleReporter.prototype.log = function(traces) { + if(traces.length === 0) { + if(this._previouslyReported) { + this._previouslyReported = false; + this.msg(allHandledMsg); + } + return; + } + + this._previouslyReported = true; + this.groupStart(unhandledRejectionsMsg + traces.length); + try { + this._log(traces); + } finally { + this.groupEnd(); + } + }; + + ConsoleReporter.prototype._log = function(traces) { + for(var i=0; i<traces.length; ++i) { + this.warn(error.format(traces[i])); + } + }; + + function initDefaultLogging() { + /*jshint maxcomplexity:7*/ + var log, warn, groupStart, groupEnd; + + if(typeof console === 'undefined') { + log = warn = consoleNotAvailable; + } else { + // Alias console to prevent things like uglify's drop_console option from + // removing console.log/error. Unhandled rejections fall into the same + // category as uncaught exceptions, and build tools shouldn't silence them. + var localConsole = console; + if(typeof localConsole.error === 'function' + && typeof localConsole.dir === 'function') { + warn = function(s) { + localConsole.error(s); + }; + + log = function(s) { + localConsole.log(s); + }; + + if(typeof localConsole.groupCollapsed === 'function') { + groupStart = function(s) { + localConsole.groupCollapsed(s); + }; + groupEnd = function() { + localConsole.groupEnd(); + }; + } + } else { + // IE8 has console.log and JSON, so we can make a + // reasonably useful warn() from those. + // Credit to webpro (https://github.com/webpro) for this idea + // typeof localConsole.log will return 'object' in IE8, so can't test it with === 'function' + // Since this is more of a corner case for IE8, I'm ok to check it with !== 'undefined' to reduce complexity + if (typeof localConsole.log !== 'undefined' && typeof JSON !== 'undefined') { + log = warn = function(x) { + if (typeof x !== 'string') { + try { + x = JSON.stringify(x); + } catch (e) { + } + } + localConsole.log(x); + }; + } else { + log = warn = consoleNotAvailable; + } + } + } + + return { + msg: log, + warn: warn, + groupStart: groupStart || warn, + groupEnd: groupEnd || consoleNotAvailable + }; + } + + function consoleNotAvailable() {} + + return ConsoleReporter; + +}); +}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); })); diff --git a/node_modules/when/monitor/PromiseMonitor.js b/node_modules/when/monitor/PromiseMonitor.js new file mode 100644 index 000000000..2becc0e3c --- /dev/null +++ b/node_modules/when/monitor/PromiseMonitor.js @@ -0,0 +1,197 @@ +/** @license MIT License (c) copyright 2010-2014 original author or authors */ +/** @author Brian Cavalier */ +/** @author John Hann */ + +(function(define) { 'use strict'; +define(function(require) { + + var defaultStackJumpSeparator = 'from execution context:'; + 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; + + var setTimer = require('../lib/env').setTimer; + var error = require('./error'); + + var executionContext = []; + + function PromiseMonitor(reporter) { + this.logDelay = 0; + this.stackFilter = defaultStackFilter; + this.stackJumpSeparator = defaultStackJumpSeparator; + this.filterDuplicateFrames = true; + + this._reporter = reporter; + if(typeof reporter.configurePromiseMonitor === 'function') { + reporter.configurePromiseMonitor(this); + } + + this._traces = []; + this._traceTask = 0; + + var self = this; + this._doLogTraces = function() { + self._logTraces(); + }; + } + + PromiseMonitor.prototype.monitor = function(Promise) { + var self = this; + Promise.createContext = function(p, context) { + p.context = self.createContext(p, context); + }; + + Promise.enterContext = function(p) { + executionContext.push(p.context); + }; + + Promise.exitContext = function() { + executionContext.pop(); + }; + + Promise.onPotentiallyUnhandledRejection = function(rejection, extraContext) { + return self.addTrace(rejection, extraContext); + }; + + Promise.onPotentiallyUnhandledRejectionHandled = function(rejection) { + return self.removeTrace(rejection); + }; + + Promise.onFatalRejection = function(rejection, extraContext) { + return self.fatal(rejection, extraContext); + }; + + return this; + }; + + PromiseMonitor.prototype.createContext = function(at, parentContext) { + var context = { + parent: parentContext || executionContext[executionContext.length - 1], + stack: void 0 + }; + error.captureStack(context, at.constructor); + return context; + }; + + PromiseMonitor.prototype.addTrace = function(handler, extraContext) { + var t, i; + + for(i = this._traces.length-1; i >= 0; --i) { + t = this._traces[i]; + if(t.handler === handler) { + break; + } + } + + if(i >= 0) { + t.extraContext = extraContext; + } else { + this._traces.push({ + handler: handler, + extraContext: extraContext + }); + } + + this.logTraces(); + }; + + PromiseMonitor.prototype.removeTrace = function(/*handler*/) { + this.logTraces(); + }; + + PromiseMonitor.prototype.fatal = function(handler, extraContext) { + var err = new Error(); + err.stack = this._createLongTrace(handler.value, handler.context, extraContext).join('\n'); + setTimer(function() { + throw err; + }, 0); + }; + + PromiseMonitor.prototype.logTraces = function() { + if(!this._traceTask) { + this._traceTask = setTimer(this._doLogTraces, this.logDelay); + } + }; + + PromiseMonitor.prototype._logTraces = function() { + this._traceTask = void 0; + this._traces = this._traces.filter(filterHandled); + this._reporter.log(this.formatTraces(this._traces)); + }; + + + PromiseMonitor.prototype.formatTraces = function(traces) { + return traces.map(function(t) { + return this._createLongTrace(t.handler.value, t.handler.context, t.extraContext); + }, this); + }; + + PromiseMonitor.prototype._createLongTrace = function(e, context, extraContext) { + var trace = error.parse(e) || [String(e) + ' (WARNING: non-Error used)']; + trace = filterFrames(this.stackFilter, trace, 0); + this._appendContext(trace, context); + this._appendContext(trace, extraContext); + return this.filterDuplicateFrames ? this._removeDuplicates(trace) : trace; + }; + + PromiseMonitor.prototype._removeDuplicates = function(trace) { + var seen = {}; + var sep = this.stackJumpSeparator; + var count = 0; + return trace.reduceRight(function(deduped, line, i) { + if(i === 0) { + deduped.unshift(line); + } else if(line === sep) { + if(count > 0) { + deduped.unshift(line); + count = 0; + } + } else if(!seen[line]) { + seen[line] = true; + deduped.unshift(line); + ++count; + } + return deduped; + }, []); + }; + + PromiseMonitor.prototype._appendContext = function(trace, context) { + trace.push.apply(trace, this._createTrace(context)); + }; + + PromiseMonitor.prototype._createTrace = function(traceChain) { + var trace = []; + var stack; + + while(traceChain) { + stack = error.parse(traceChain); + + if (stack) { + stack = filterFrames(this.stackFilter, stack); + appendStack(trace, stack, this.stackJumpSeparator); + } + + traceChain = traceChain.parent; + } + + return trace; + }; + + function appendStack(trace, stack, separator) { + if (stack.length > 1) { + stack[0] = separator; + trace.push.apply(trace, stack); + } + } + + function filterFrames(stackFilter, stack) { + return stack.filter(function(frame) { + return !stackFilter.test(frame); + }); + } + + function filterHandled(t) { + return !t.handler.handled; + } + + return PromiseMonitor; +}); +}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); })); diff --git a/node_modules/when/monitor/README.md b/node_modules/when/monitor/README.md new file mode 100644 index 000000000..57acff9d8 --- /dev/null +++ b/node_modules/when/monitor/README.md @@ -0,0 +1,3 @@ +# Promise monitoring and debugging + +This dir contains experimental new promise monitoring and debugging utilities for when.js. See [the docs](../docs/api.md#debugging-promises). diff --git a/node_modules/when/monitor/console.js b/node_modules/when/monitor/console.js new file mode 100644 index 000000000..931caa641 --- /dev/null +++ b/node_modules/when/monitor/console.js @@ -0,0 +1,14 @@ +/** @license MIT License (c) copyright 2010-2014 original author or authors */ +/** @author Brian Cavalier */ +/** @author John Hann */ + +(function(define) { 'use strict'; +define(function(require) { + + var monitor = require('../monitor'); + var Promise = require('../when').Promise; + + return monitor(Promise); + +}); +}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); })); diff --git a/node_modules/when/monitor/error.js b/node_modules/when/monitor/error.js new file mode 100644 index 000000000..63825e2c4 --- /dev/null +++ b/node_modules/when/monitor/error.js @@ -0,0 +1,86 @@ +/** @license MIT License (c) copyright 2010-2014 original author or authors */ +/** @author Brian Cavalier */ +/** @author John Hann */ + +(function(define) { 'use strict'; +define(function() { + + var parse, captureStack, format; + + if(Error.captureStackTrace) { + // Use Error.captureStackTrace if available + parse = function(e) { + return e && e.stack && e.stack.split('\n'); + }; + + format = formatAsString; + captureStack = Error.captureStackTrace; + + } else { + // Otherwise, do minimal feature detection to determine + // how to capture and format reasonable stacks. + parse = function(e) { + var stack = e && e.stack && e.stack.split('\n'); + if(stack && e.message) { + stack.unshift(e.message); + } + return stack; + }; + + (function() { + var e = new Error(); + if(typeof e.stack !== 'string') { + format = formatAsString; + captureStack = captureSpiderMonkeyStack; + } else { + format = formatAsErrorWithStack; + captureStack = useStackDirectly; + } + }()); + } + + function captureSpiderMonkeyStack(host) { + try { + throw new Error(); + } catch(err) { + host.stack = err.stack; + } + } + + function useStackDirectly(host) { + host.stack = new Error().stack; + } + + function formatAsString(longTrace) { + return join(longTrace); + } + + function formatAsErrorWithStack(longTrace) { + var e = new Error(); + e.stack = formatAsString(longTrace); + return e; + } + + // About 5-10x faster than String.prototype.join o_O + function join(a) { + var sep = false; + var s = ''; + for(var i=0; i< a.length; ++i) { + if(sep) { + s += '\n' + a[i]; + } else { + s+= a[i]; + sep = true; + } + } + return s; + } + + return { + parse: parse, + format: format, + captureStack: captureStack + }; + +}); +}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); })); |