|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc service |
| 5 | + * @name ui.logger.sourceMapUtil |
| 6 | + * @description |
| 7 | + * # sourceMapUtil |
| 8 | + * Factory in the ui.logger. |
| 9 | + */ |
| 10 | +(function(){ |
| 11 | + function _findSourceMappingURL(source) { |
| 12 | + var m = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/.exec(source); |
| 13 | + if (m && m[1]) { |
| 14 | + return m[1]; |
| 15 | + } |
| 16 | + } |
| 17 | + function Configure(options) { |
| 18 | + angular.extend(this.options,options); |
| 19 | + } |
| 20 | + function validURL(str) { |
| 21 | + var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; |
| 22 | + return regexp.test(str); |
| 23 | + } |
| 24 | + //function format() { |
| 25 | + // var str = arguments[0]; |
| 26 | + // for (var i = 1; i < arguments.length; i++) { |
| 27 | + // var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm"); |
| 28 | + // str = str.replace(regEx, arguments[i]); |
| 29 | + // } |
| 30 | + // return str; |
| 31 | + //} |
| 32 | + function _findFunctionName(source, lineNumber) { |
| 33 | + // function {name}({args}) m[1]=name m[2]=args |
| 34 | + var reFunctionDeclaration = /function\s+([^(]*?)\s*\(([^)]*)\)/; |
| 35 | + // {name} = function ({args}) |
| 36 | + var reFunctionExpression = /['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*function\b/; |
| 37 | + // {name} = eval() |
| 38 | + var reFunctionEvaluation = /['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*(?:eval|new Function)\b/; |
| 39 | + var lines = source.split('\n'); |
| 40 | + |
| 41 | + // Walk backwards in the source lines until we find the line which matches one of the patterns above |
| 42 | + var code = '', line, maxLines = Math.min(lineNumber, 20), m, commentPos; |
| 43 | + for (var i = 0; i < maxLines; ++i) { |
| 44 | + // lineNo is 1-based, source[] is 0-based |
| 45 | + line = lines[lineNumber - i - 1]; |
| 46 | + if(line){ |
| 47 | + commentPos = line.indexOf('//'); |
| 48 | + if (commentPos >= 0) { |
| 49 | + line = line.substr(0, commentPos); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + if (line) { |
| 55 | + code = line + code; |
| 56 | + m = reFunctionExpression.exec(code); |
| 57 | + if (m && m[1]) { |
| 58 | + return m[1]; |
| 59 | + } |
| 60 | + m = reFunctionDeclaration.exec(code); |
| 61 | + if (m && m[1]) { |
| 62 | + return m[1]; |
| 63 | + } |
| 64 | + m = reFunctionEvaluation.exec(code); |
| 65 | + if (m && m[1]) { |
| 66 | + return m[1]; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + function Provider(sourceMap) { |
| 73 | + this.options={ |
| 74 | + offline:true |
| 75 | + }; |
| 76 | + var _self=this; |
| 77 | + function factory($injector) { |
| 78 | + var _cache={}; |
| 79 | + function Service(){ |
| 80 | + function getSourceFileUrl(url){ |
| 81 | + var $q=$injector.get('$q'); |
| 82 | + var def=$q.defer(); |
| 83 | + $.ajax(url).then(function(content) { |
| 84 | + def.resolve(_findSourceMappingURL(content)); |
| 85 | + }).fail(function(error) { |
| 86 | + def.reject(error); |
| 87 | + }); |
| 88 | + return def.promise; |
| 89 | + } |
| 90 | + function getOriginalLocation(stack){ |
| 91 | + var $q=$injector.get('$q'); |
| 92 | + var url=stack.fileName; |
| 93 | + var def=$q.defer(); |
| 94 | + if(!_self.options.offline){ |
| 95 | + def.resolve(stack); |
| 96 | + return; |
| 97 | + } |
| 98 | + //check if map exist in cache for the file else get the map and update the cache |
| 99 | + if(!_cache[url]){ |
| 100 | + _cache[url]={ |
| 101 | + exist:false, |
| 102 | + _map:{}, |
| 103 | + _file:'' |
| 104 | + }; |
| 105 | + getSourceFileUrl(url).then(function(mapUrl){ |
| 106 | + if(mapUrl && !validURL(mapUrl)){ |
| 107 | + mapUrl=url.substring(0,url.lastIndexOf('/')+1)+mapUrl; |
| 108 | + } |
| 109 | + $.getJSON(mapUrl, function(map) { |
| 110 | + _cache[url].exist=true; |
| 111 | + _cache[url]._map=new sourceMap.SourceMapConsumer(map); |
| 112 | + var loc=_cache[url]._map.originalPositionFor({ |
| 113 | + line: stack.lineNumber, |
| 114 | + column: stack.columnNumber |
| 115 | + }); |
| 116 | + if(_cache[url]._file){ |
| 117 | + loc.name=_findFunctionName(_cache[url]._file,loc.line, loc.column); |
| 118 | + _stack=new window.StackFrame(loc.name, stack.args, loc.source, loc.line, loc.column); |
| 119 | + def.resolve(_stack); |
| 120 | + }else{ |
| 121 | + var sourceFileUlr=url.substring(0,url.lastIndexOf('/')+1)+loc.source; |
| 122 | + $.ajax(sourceFileUlr).then(function(content) { |
| 123 | + _cache[url]._file=content; |
| 124 | + loc.name=_findFunctionName(_cache[url]._file,loc.line, loc.column); |
| 125 | + _stack=new window.StackFrame(loc.name, stack.args, loc.source, loc.line, loc.column); |
| 126 | + def.resolve(_stack); |
| 127 | + }).fail(function() { |
| 128 | + _cache[url]._file=null; |
| 129 | + def.resolve(stack); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + }).fail(function() { |
| 135 | + _cache[url].exist=false; |
| 136 | + _cache[url]._map=null; |
| 137 | + def.resolve(stack); |
| 138 | + }); |
| 139 | + },function(){ |
| 140 | + _cache[url].exist=false; |
| 141 | + _cache[url]._map=null; |
| 142 | + def.resolve(stack); |
| 143 | + }); |
| 144 | + |
| 145 | + }else{ |
| 146 | + if(_cache[url].exist){ |
| 147 | + //read map and return stack from source |
| 148 | + var _stack=_cache[url]._map.originalPositionFor(stack); |
| 149 | + def.resolve(_stack); |
| 150 | + }else{ |
| 151 | + def.resolve(stack); |
| 152 | + } |
| 153 | + } |
| 154 | + return def.promise; |
| 155 | + } |
| 156 | + return { |
| 157 | + getOriginalLocation: getOriginalLocation |
| 158 | + }; |
| 159 | + } |
| 160 | + return new Service(); |
| 161 | + } |
| 162 | + // Method for instantiating |
| 163 | + this.$get = ['$injector',factory]; |
| 164 | + } |
| 165 | + |
| 166 | + Provider.prototype.configure=Configure; |
| 167 | + angular.module('ui.logger').provider('sourceMapUtil',['sourceMap',Provider]); |
| 168 | +})(); |
0 commit comments