FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-inspector/lib/ScriptManager.js at master · coLlenz/node-inspector · GitHub
coLlenz
/
node-inspector
Public
forked from
node-inspector/node-inspector
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node-inspector
/
lib
/
ScriptManager.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
372 lines (320 loc) · 10.3 KB
Breadcrumbs
node-inspector
/
lib
/
ScriptManager.js
Copy path
File metadata and controls
372 lines (320 loc) · 10.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
var
events
=
require
(
'events'
)
,
path
=
require
(
'path'
)
,
async
=
require
(
'async'
)
,
debug
=
require
(
'debug'
)
(
'node-inspector:ScriptManager'
)
,
semver
=
require
(
'semver'
)
,
dataUri
=
require
(
'strong-data-uri'
)
,
pathIsAbsolute
=
require
(
'path-is-absolute'
)
,
convert
=
require
(
'./convert.js'
)
;
// see Blink inspector > ContentSearchUtils.cpp > findMagicComment()
var
SOURCE_MAP_URL_REGEX
=
/
\/
\/
[
@
#
]
[
\t
]
s
o
u
r
c
e
M
a
p
p
i
n
g
U
R
L
=
[
\t
]
*
(
[
^
\s
'
"
]
*
)
[
\t
]
*
$
/
m
;
/**
*
@param
{
{hidden}
} config
*
@param
{
FrontendClient
} frontendClient
*
@param
{
DebuggerClient
} debuggerClient
*
@constructor
*/
function
ScriptManager
(
config
,
session
)
{
config
=
config
||
{
}
;
var
self
=
Object
.
create
(
ScriptManager
.
prototype
,
{
_sources
:
{
value
:
{
}
,
writable
:
true
}
,
_hidden
:
{
value
:
config
.
hidden
||
[
]
}
,
_frontendClient
:
{
value
:
session
.
frontendClient
}
,
_debuggerClient
:
{
value
:
session
.
debuggerClient
}
}
)
;
self
.
_debuggerClient
.
on
(
'afterCompile'
,
self
.
_onAfterCompile
.
bind
(
self
)
)
;
self
.
_debuggerClient
.
on
(
'compileError'
,
self
.
_onCompileError
.
bind
(
self
)
)
;
return
self
;
}
ScriptManager
.
prototype
=
Object
.
create
(
events
.
EventEmitter
.
prototype
,
{
mainAppScript
:
{
value
:
null
,
writable
:
true
}
,
realMainAppScript
:
{
value
:
null
,
writable
:
true
}
,
_onAfterCompile
:
{
value
:
function
(
event
)
{
if
(
!
event
.
script
)
{
console
.
log
(
'Unexpected error: debugger emitted afterCompile event'
+
'with no script data.'
)
;
return
;
}
this
.
addScript
(
event
.
script
)
;
}
}
,
_onCompileError
:
{
value
:
function
(
event
)
{
var
cb
=
function
(
)
{
var
version
=
this
.
_debuggerClient
.
target
.
nodeVersion
;
if
(
semver
.
satisfies
(
version
,
'~0.12'
)
)
// Relative to https://github.com/joyent/node/issues/25266
this
.
_onAfterCompile
(
event
)
;
}
.
bind
(
this
)
;
if
(
this
.
_debuggerClient
.
isReady
)
process
.
nextTick
(
cb
)
;
else
this
.
_debuggerClient
.
once
(
'connect'
,
cb
)
;
}
}
,
_isNodeInternal
:
{
value
:
function
(
scriptName
)
{
// node.js internal scripts have no path, just a filename
// regular scripts have always a full path
// (i.e their name contains at least one path separator)
var
isFullPath
=
/
[
\/
\\
]
/
.
test
(
scriptName
)
;
return
!
isFullPath
;
}
}
,
_listAllSources
:
{
value
:
function
(
)
{
var
self
=
this
;
return
Object
.
keys
(
this
.
_sources
)
.
map
(
function
fnSelectValue
(
key
)
{
return
self
.
_sources
[
key
]
;
}
)
;
}
}
,
isScriptHidden
:
{
/**
*
@param
{
string
} scriptPath.
*
@return
{
boolean
}
*/
value
:
function
(
scriptPath
)
{
return
this
.
_hidden
.
some
(
function
fnHiddenScriptMatchesPath
(
r
)
{
return
r
.
test
(
scriptPath
)
;
}
)
;
}
}
,
resolveScriptById
:
{
value
:
function
(
id
,
done
)
{
var
source
=
this
.
findScriptByID
(
id
)
;
if
(
!
source
)
{
this
.
_requireScriptFromApp
(
id
,
done
)
;
}
else
{
process
.
nextTick
(
function
(
)
{
done
(
null
,
source
)
;
}
)
;
}
}
}
,
getScriptSourceById
:
{
value
:
function
(
id
,
callback
)
{
this
.
_debuggerClient
.
request
(
'scripts'
,
{
includeSource
:
true
,
types
:
4
,
ids
:
[
id
]
}
,
function
handleScriptSourceResponse
(
err
,
result
)
{
if
(
err
)
return
callback
(
err
)
;
// Some modules gets unloaded (?) after they are parsed,
// e.g. node_modules/express/node_modules/methods/index.js
// V8 request 'scripts' returns an empty result in such case
var
source
=
result
.
length
>
0
?
result
[
0
]
.
source
:
undefined
;
callback
(
null
,
source
)
;
}
)
;
}
}
,
_requireScriptFromApp
:
{
value
:
function
(
id
,
done
)
{
// NOTE: We can step in this function only if `afterCompile` event is broken
// This is issue for node v0.12: https://github.com/joyent/node/issues/25266
this
.
_debuggerClient
.
request
(
'scripts'
,
{
includeSource
:
false
,
filter
:
id
}
,
function
(
error
,
scripts
)
{
if
(
error
)
return
done
(
error
)
;
if
(
!
scripts
[
0
]
)
return
done
(
null
)
;
this
.
addScript
(
scripts
[
0
]
,
done
)
;
}
.
bind
(
this
)
)
;
}
}
,
findScriptIdByPath
:
{
value
:
function
(
path
)
{
return
Object
.
keys
(
this
.
_sources
)
.
filter
(
function
(
key
)
{
return
this
.
_sources
[
key
]
.
v8name
==
path
;
}
,
this
)
[
0
]
;
}
}
,
findScriptByID
:
{
/**
*
@param
{
string
} id script id.
*
@return
{
{hidden: boolean, path: string, url: string}
}
*/
value
:
function
(
id
)
{
return
this
.
_sources
[
id
]
;
}
}
,
addScript
:
{
value
:
function
(
v8data
,
done
)
{
done
=
done
||
function
(
)
{
}
;
var
localPath
=
v8data
.
name
;
if
(
this
.
_isMainAppScript
(
localPath
)
)
{
v8data
.
name
=
localPath
=
this
.
realMainAppScript
;
}
var
hidden
=
this
.
isScriptHidden
(
localPath
)
&&
localPath
!=
this
.
mainAppScript
;
var
inspectorScriptData
=
this
.
_doAddScript
(
v8data
,
hidden
)
;
debug
(
'addScript id: %s localPath: %s hidden? %s source? %s'
,
v8data
.
id
,
localPath
,
hidden
,
!
!
v8data
.
source
)
;
if
(
hidden
)
return
done
(
null
,
inspectorScriptData
)
;
if
(
this
.
_isNodeInternal
(
localPath
)
)
{
this
.
_notifyScriptParsed
(
inspectorScriptData
)
;
done
(
null
,
inspectorScriptData
)
;
}
else
{
this
.
_getSourceMapUrl
(
v8data
.
id
,
v8data
.
source
,
function
onGetSourceMapUrlReturn
(
err
,
sourceMapUrl
)
{
if
(
err
)
{
console
.
log
(
'Warning: cannot parse SourceMap URL for script %s (id %d). %s'
,
localPath
,
v8data
.
id
,
err
)
;
}
debug
(
'sourceMapUrl for script %s:%s is %s'
,
v8data
.
id
,
localPath
,
sourceMapUrl
)
;
inspectorScriptData
.
sourceMapURL
=
sourceMapUrl
;
this
.
_checkInlineSourceMap
(
inspectorScriptData
)
;
this
.
_notifyScriptParsed
(
inspectorScriptData
)
;
done
(
null
,
inspectorScriptData
)
;
}
.
bind
(
this
)
)
;
}
}
}
,
_checkInlineSourceMap
:
{
value
:
function
(
inspectorScriptData
)
{
// Source maps have some issues in different libraries.
// If source map exposed in inline mode, we can easy fix some potential issues.
var
sourceMapUrl
=
inspectorScriptData
.
sourceMapURL
;
if
(
!
sourceMapUrl
)
return
;
var
sourceMap
;
try
{
sourceMap
=
dataUri
.
decode
(
sourceMapUrl
)
.
toString
(
)
;
}
catch
(
err
)
{
return
;
}
sourceMap
=
JSON
.
parse
(
sourceMap
.
toString
(
)
)
;
this
.
_checkSourceMapIssues
(
inspectorScriptData
,
sourceMap
)
;
sourceMap
=
JSON
.
stringify
(
sourceMap
)
;
inspectorScriptData
.
sourceMapURL
=
dataUri
.
encode
(
sourceMap
,
'application/json'
)
;
}
}
,
_checkSourceMapIssues
:
{
value
:
function
(
inspectorScriptData
,
sourceMap
)
{
var
scriptName
=
inspectorScriptData
.
url
.
replace
(
/
^
f
i
l
e
:
\/
\/
/
,
''
)
;
if
(
process
.
platform
==
'win32'
)
{
scriptName
=
scriptName
.
replace
(
/
^
\/
/
,
''
)
;
}
var
scriptOrigin
=
path
.
dirname
(
scriptName
)
;
fixAbsoluteSourcePaths
(
)
;
fixWrongFileName
(
)
;
function
fixAbsoluteSourcePaths
(
)
{
// Documentation says what source maps can contain absolute paths,
// but DevTools strictly expects relative paths.
sourceMap
.
sources
=
sourceMap
.
sources
.
map
(
function
(
source
)
{
if
(
!
pathIsAbsolute
(
source
)
)
return
source
;
return
path
.
relative
(
scriptOrigin
,
source
)
;
}
)
;
}
function
fixWrongFileName
(
)
{
// Documentation says nothing about file name of bundled script.
// So, we expect a situation, when original source and bundled script have equal name.
// We need to fix this case.
sourceMap
.
sources
=
sourceMap
.
sources
.
map
(
function
(
source
)
{
var
sourceUrl
=
path
.
resolve
(
scriptOrigin
,
source
)
.
replace
(
/
\\
/
g
,
'/'
)
;
if
(
sourceUrl
==
scriptName
)
source
+=
'.source'
;
return
source
;
}
)
;
}
}
}
,
_notifyScriptParsed
:
{
value
:
function
(
scriptData
)
{
this
.
_frontendClient
.
sendEvent
(
'Debugger.scriptParsed'
,
scriptData
)
;
}
}
,
_isMainAppScript
:
{
value
:
function
(
path
)
{
if
(
!
path
||
!
this
.
mainAppScript
)
return
false
;
if
(
process
.
platform
==
'win32'
)
return
this
.
mainAppScript
.
toLowerCase
(
)
==
path
.
replace
(
/
\/
/
g
,
'\\'
)
.
toLowerCase
(
)
;
else
return
this
.
mainAppScript
==
path
;
}
}
,
normalizeName
:
{
value
:
function
(
name
)
{
var
scriptName
=
name
.
replace
(
/
^
f
i
l
e
:
\/
\/
/
,
''
)
;
if
(
process
.
platform
==
'win32'
)
{
scriptName
=
scriptName
.
replace
(
/
^
\/
/
,
''
)
;
}
if
(
this
.
_isMainAppScript
(
scriptName
)
)
{
return
convert
.
v8NameToInspectorUrl
(
this
.
mainAppScript
)
;
}
return
name
;
}
}
,
_doAddScript
:
{
value
:
function
(
v8data
,
hidden
)
{
var
inspectorUrl
=
convert
.
v8NameToInspectorUrl
(
v8data
.
name
)
;
var
inspectorScriptData
=
{
scriptId
:
String
(
v8data
.
id
)
,
url
:
inspectorUrl
,
startLine
:
v8data
.
lineOffset
,
startColumn
:
v8data
.
columnOffset
/* Properties not set:
endLine: undefined,
endColumn: undefined,
isContentScript: undefined,
hasSourceURL: undefined,
*/
}
;
var
item
=
{
hidden
:
hidden
,
v8name
:
v8data
.
name
,
url
:
inspectorUrl
}
;
this
.
_sources
[
inspectorScriptData
.
scriptId
]
=
item
;
return
inspectorScriptData
;
}
}
,
_getSourceMapUrl
:
{
value
:
function
(
scriptId
,
scriptSource
,
callback
)
{
var
getSource
;
if
(
scriptSource
==
null
)
{
debug
(
'_getSourceMapUrl(%s) - fetching source from V8'
,
scriptId
)
;
getSource
=
this
.
getScriptSourceById
.
bind
(
this
,
scriptId
)
;
}
else
{
debug
(
'_getSourceMapUrl(%s) - using the suplied source'
,
scriptId
)
;
getSource
=
function
(
cb
)
{
cb
(
null
,
scriptSource
)
;
}
;
}
async
.
waterfall
(
[
getSource
,
this
.
_parseSourceMapUrlFromScriptSource
.
bind
(
this
)
]
,
callback
)
;
}
}
,
_parseSourceMapUrlFromScriptSource
:
{
value
:
function
(
source
,
callback
)
{
var
match
=
SOURCE_MAP_URL_REGEX
.
exec
(
source
)
;
callback
(
null
,
match
?
match
[
1
]
:
undefined
)
;
}
}
,
reset
:
{
value
:
function
(
)
{
this
.
_sources
=
{
}
;
}
}
}
)
;
exports
.
ScriptManager
=
ScriptManager
;
Back
|
FazBrowse Home
|
New Git URL