diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/when/monitor | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/when/monitor')
| -rw-r--r-- | node_modules/when/monitor/ConsoleReporter.js | 102 | ||||
| -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, 402 insertions, 0 deletions
diff --git a/node_modules/when/monitor/ConsoleReporter.js b/node_modules/when/monitor/ConsoleReporter.js new file mode 100644 index 00000000..79e56ed9 --- /dev/null +++ b/node_modules/when/monitor/ConsoleReporter.js @@ -0,0 +1,102 @@ +/** @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 + if (typeof localConsole.log ==='function' + && typeof JSON !== 'undefined') { + log = warn = function (x) { + if(typeof x !== 'string') { + try { + x = JSON.stringify(x); + } catch(e) {} + } + localConsole.log(x); + }; + } + } + } + + 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 00000000..2becc0e3 --- /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 00000000..57acff9d --- /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 00000000..931caa64 --- /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 00000000..63825e2c --- /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(); })); |
