-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (42 loc) · 1.16 KB
/
index.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
'use strict'
const debug = require('debug')('stack-sites')
const parseStackLine = require('./parse-stack-line')
function hasLineInfo (line) {
return line.includes('(') && line.includes(')')
}
function isModule (line) {
return line.includes('(/') ||
line.includes(' (') // on Windows: at stackSites (c:\Users...)
}
function isNative (line) {
return line.includes('(native)')
}
function isInternal (line) {
return line.includes('(internal/')
}
function isAnonymous (line) {
return line.includes('(<anonymous>)')
}
function stackSites (stack) {
stack = stack || ((new Error('stack-sites').stack))
debug(stack)
try {
const parsed = stack.split('\n')
.slice(1) // remove exception itself
.filter(hasLineInfo)
.filter(line => !isNative(line))
.filter(line => !isInternal(line))
.filter(line => !isAnonymous(line))
.filter(isModule)
.map(parseStackLine)
// maybe limit stack sites to valid existing file?
debug('parsed and cleaned up stack')
debug(parsed)
return parsed
} catch (e) {
console.error('Cannot parse stack trace')
console.error(stack)
throw e
}
}
module.exports = stackSites