This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbrowser.js
346 lines (291 loc) · 10.8 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Stacky = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
*
* This code may only be used under the BSD style license found at polymer.github.io/LICENSE.txt
* The complete set of authors may be found at polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also subject to
* an additional IP rights grant found at polymer.github.io/PATENTS.txt
*/
(function(scope) {
'use strict';
var parse = scope.parse || require('./parsing').parse;
scope.defaults = {
// Methods are aligned up to this much padding.
maxMethodPadding: 40,
// A string to prefix each line with.
indent: '',
// A string to show for stack lines that are missing a method.
methodPlaceholder: '<unknown>',
// A list of Strings/RegExps that will be stripped from `location` values on
// each line (via `String#replace`).
locationStrip: [],
// A list of Strings/RegExps that indicate that a line is *not* important, and
// should be styled as such.
unimportantLocation: [],
// A filter function to completely remove lines
filter: function() { return false; },
// styles are functions that take a string and return that string when styled.
styles: {
method: passthrough,
location: passthrough,
line: passthrough,
column: passthrough,
unimportant: passthrough,
},
};
// See Tero Tolonen's answer at
// http://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser
/*jshint -W054 */
var isNode = new Function('try {return this===global;}catch(e){return false;}');
// For Stacky-in-Node, we default to colored stacks.
if (isNode()) {
var chalk = require('chalk');
scope.defaults.styles = {
method: chalk.magenta,
location: chalk.blue,
line: chalk.cyan,
column: chalk.cyan,
unimportant: chalk.dim
};
}
function pretty(stackOrParsed, options) {
options = mergeDefaults(options || {}, scope.defaults);
var lines = Array.isArray(stackOrParsed) ? stackOrParsed : parse(stackOrParsed);
lines = clean(lines, options);
var padSize = methodPadding(lines, options);
var parts = lines.map(function(line) {
var method = line.method || options.methodPlaceholder;
var pad = options.indent + padding(padSize - method.length);
var locationBits = [
options.styles.location(line.location),
options.styles.line(line.line),
];
if ('column' in line) {
locationBits.push(options.styles.column(line.column));
}
var location = locationBits.join(':');
var text = pad + options.styles.method(method) + ' at ' + location;
if (!line.important) {
text = options.styles.unimportant(text);
}
return text;
});
return parts.join('\n');
}
function clean(lines, options) {
var result = [];
for (var i = 0, line; line = lines[i]; i++) {
if (options.filter(line)) continue;
line.location = cleanLocation(line.location, options);
line.important = isImportant(line, options);
result.push(line);
}
return result;
}
// Utility
function passthrough(string) {
return string;
}
function mergeDefaults(options, defaults) {
var result = Object.create(defaults);
Object.keys(options).forEach(function(key) {
var value = options[key];
if (typeof value === 'object' && !Array.isArray(value)) {
value = mergeDefaults(value, defaults[key]);
}
result[key] = value;
});
return result;
}
function methodPadding(lines, options) {
var size = options.methodPlaceholder.length;
for (var i = 0, line; line = lines[i]; i++) {
size = Math.min(options.maxMethodPadding, Math.max(size, line.method.length));
}
return size;
}
function padding(length) {
var result = '';
for (var i = 0; i < length; i++) {
result = result + ' ';
}
return result;
}
function cleanLocation(location, options) {
if (options.locationStrip) {
for (var i = 0, matcher; matcher = options.locationStrip[i]; i++) {
location = location.replace(matcher, '');
}
}
return location;
}
function isImportant(line, options) {
if (options.unimportantLocation) {
for (var i = 0, matcher; matcher = options.unimportantLocation[i]; i++) {
if (line.location.match(matcher)) return false;
}
}
return true;
}
scope.clean = clean;
scope.pretty = pretty;
})(typeof module !== 'undefined' ? module.exports : (this.Stacky = this.Stacky || {}));
},{"./parsing":4,"chalk":undefined}],2:[function(require,module,exports){
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
*
* This code may only be used under the BSD style license found at polymer.github.io/LICENSE.txt
* The complete set of authors may be found at polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also subject to
* an additional IP rights grant found at polymer.github.io/PATENTS.txt
*/
'use strict';
var formatting = require('./formatting');
var normalization = require('./normalization');
var parsing = require('./parsing');
module.exports = {
// Shorthands for your convenience.
normalize: normalization.normalize,
parse: parsing.parse,
pretty: formatting.pretty,
// Or the full modules.
formatting: formatting,
normalization: normalization,
parsing: parsing,
};
},{"./formatting":1,"./normalization":3,"./parsing":4}],3:[function(require,module,exports){
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
*
* This code may only be used under the BSD style license found at polymer.github.io/LICENSE.txt
* The complete set of authors may be found at polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also subject to
* an additional IP rights grant found at polymer.github.io/PATENTS.txt
*/
(function(scope) {
'use strict';
var parse = scope.parse || require('./parsing').parse;
var pretty = scope.pretty || require('./formatting').pretty;
function normalize(error, prettyOptions) {
if (error.parsedStack) return error;
var message = error.message || error.description || error || '<unknown error>';
var parsedStack = [];
try {
parsedStack = parse(error.stack || error.toString());
} catch (error) {
// Ah well.
}
if (parsedStack.length === 0 && error.fileName) {
parsedStack.push({
method: '',
location: error.fileName,
line: error.lineNumber,
column: error.columnNumber,
});
}
if (!prettyOptions || !prettyOptions.showColumns) {
for (var i = 0, line; line = parsedStack[i]; i++) {
delete line.column;
}
}
var prettyStack = message;
if (parsedStack.length > 0) {
prettyStack = prettyStack + '\n' + pretty(parsedStack, prettyOptions);
}
var cleanErr = Object.create(Error.prototype);
cleanErr.message = message;
cleanErr.stack = prettyStack;
cleanErr.parsedStack = parsedStack;
return cleanErr;
}
scope.normalize = normalize;
})(typeof module !== 'undefined' ? module.exports : (this.Stacky = this.Stacky || {}));
},{"./formatting":1,"./parsing":4}],4:[function(require,module,exports){
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
*
* This code may only be used under the BSD style license found at polymer.github.io/LICENSE.txt
* The complete set of authors may be found at polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also subject to
* an additional IP rights grant found at polymer.github.io/PATENTS.txt
*/
(function(scope) {
'use strict';
function parse(stack) {
var rawLines = stack.split('\n');
var stackyLines = compact(rawLines.map(parseStackyLine));
if (stackyLines.length === rawLines.length) return stackyLines;
var v8Lines = compact(rawLines.map(parseV8Line));
if (v8Lines.length > 0) return v8Lines;
var geckoLines = compact(rawLines.map(parseGeckoLine));
if (geckoLines.length > 0) return geckoLines;
throw new Error('Unknown stack format: ' + stack);
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack
var GECKO_LINE = /^(?:([^@]*)@)?(.*?):(\d+)(?::(\d+))?$/;
function parseGeckoLine(line) {
var match = line.match(GECKO_LINE);
if (!match) return null;
return {
method: match[1] || '',
location: match[2] || '',
line: parseInt(match[3]) || 0,
column: parseInt(match[4]) || 0,
};
}
// https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
var V8_OUTER1 = /^\s*(eval )?at (.*) \((.*)\)$/;
var V8_OUTER2 = /^\s*at()() (\S+)$/;
var V8_INNER = /^\(?([^\(]+):(\d+):(\d+)\)?$/;
function parseV8Line(line) {
var outer = line.match(V8_OUTER1) || line.match(V8_OUTER2);
if (!outer) return null;
var inner = outer[3].match(V8_INNER);
if (!inner) return null;
var method = outer[2] || '';
if (outer[1]) method = 'eval at ' + method;
return {
method: method,
location: inner[1] || '',
line: parseInt(inner[2]) || 0,
column: parseInt(inner[3]) || 0,
};
}
// Stacky.formatting.pretty
var STACKY_LINE = /^\s*(.+) at (.+):(\d+):(\d+)$/;
function parseStackyLine(line) {
var match = line.match(STACKY_LINE);
if (!match) return null;
return {
method: match[1] || '',
location: match[2] || '',
line: parseInt(match[3]) || 0,
column: parseInt(match[4]) || 0,
};
}
// Helpers
function compact(array) {
var result = [];
array.forEach(function(value) {
if (value) {
result.push(value);
}
});
return result;
}
scope.parse = parse;
scope.parseGeckoLine = parseGeckoLine;
scope.parseV8Line = parseV8Line;
scope.parseStackyLine = parseStackyLine;
})(typeof module !== 'undefined' ? module.exports : (this.Stacky = this.Stacky || {}));
},{}]},{},[2])(2)
});