-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.js
138 lines (109 loc) · 3.08 KB
/
env.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
var convert = require('convert-source-map')
var smap = require('source-map')
var jsdom = require('jsdom')
var cp = require('child_process')
var util = require('util')
module.exports = run
process.once('message', function(data) {
run(data.script, data.html, function(data) {
process.send(data)
})
})
process.on('uncaughtException', function(err) {
process.send({method: 'error', message: err.stack})
})
function run(script, html, send) {
var sourcemap = convert.fromSource(script)
if(sourcemap) {
sourcemap = new smap.SourceMapConsumer(sourcemap.toJSON())
}
jsdom.env({
html: html || ' ',
src: [script],
created: setup,
loaded: loaded,
})
function setup(err, window) {
if(err) {
throw err
}
window.Error.prepareStackTrace = prepareStackTrace
var methods = ['info', 'warn', 'trace']
methods.forEach(function(method) {
window.console[method] = logger(method + ':', 'log')
})
window.console.log = logger('', 'log')
window.console.error = logger('', 'error')
function logger(prefix, method) {
return function() {
var args = [].slice.call(arguments)
send({method: method, message: prefix + util.format.apply(util, arguments)})
}
}
}
function loaded(errs, window) {
if(errs && errs.length) {
throw errs[0].data.error
}
}
function prepareStackTrace(err, stack) {
var message
var getters = [
'getTypeName',
'getFunctionName',
'getMethodName',
'getFileName',
'getLineNumber',
'getColumnNumber',
'getEvalOrigin',
'isToplevel',
'isEval',
'isNative',
'isConstructor'
]
message = stack.reduce(combineIntoTrace, err)
return message
function combineIntoTrace(trace, frame, i) {
if(!frame) return trace
var names = getters.reduce(function(map, getter) {
try {
map[getter] = frame[getter] && frame[getter]()
} catch(e) {
// uh....
}
return map
}, {})
var original
if(sourcemap && names.getLineNumber && names.getColumnNumber) {
original = sourcemap.originalPositionFor({
line: names.getLineNumber,
column: names.getColumnNumber
})
}
if(!original) {
return trace + '\n at ' + frame
}
var location = '(' + original.source + ':' +
original.line + ':' + original.column + ')'
var out = trace + '\n at '
if(names.isConstructor && names.getFunctionName) {
return out + 'new ' + names.getFunctionName + ' ' + location
}
if(names.isConstructor) {
return out + 'new <anonymous> ' + location
}
if(names.getTypeName) {
out += names.getTypeName + '.'
}
if(names.getFunctionName) {
out += names.getFunctionName
if(names.getMethodName && names.getFunctionName !== names.getMethodName) {
out += ' [as ' + names.getMethodName + ']'
}
} else {
out += names.getMethodName || '<anonymous>'
}
return out + ' ' + location
}
}
}