1
1
'use strict'
2
- const path = require ( 'path' )
2
+
3
3
const {
4
4
workerData : {
5
5
breakpointSetChannel,
@@ -8,10 +8,11 @@ const {
8
8
}
9
9
} = require ( 'worker_threads' )
10
10
const { randomUUID } = require ( 'crypto' )
11
- const sourceMap = require ( 'source-map' )
12
11
13
12
// TODO: move debugger/devtools_client/session to common place
14
13
const session = require ( '../../../debugger/devtools_client/session' )
14
+ // TODO: move debugger/devtools_client/source-maps to common place
15
+ const { getSourceMappedLine } = require ( '../../../debugger/devtools_client/source-maps' )
15
16
// TODO: move debugger/devtools_client/snapshot to common place
16
17
const { getLocalStateForCallFrame } = require ( '../../../debugger/devtools_client/snapshot' )
17
18
// TODO: move debugger/devtools_client/state to common place
@@ -98,17 +99,21 @@ async function addBreakpoint (probe) {
98
99
throw new Error ( `No loaded script found for ${ file } ` )
99
100
}
100
101
101
- const [ path , scriptId , sourceMapURL ] = script
102
+ const { url , scriptId, sourceMapURL, source } = script
102
103
103
- log . warn ( `Adding breakpoint at ${ path } :${ line } ` )
104
+ log . warn ( `Adding breakpoint at ${ url } :${ line } ` )
104
105
105
106
let lineNumber = line
106
107
107
- if ( sourceMapURL && sourceMapURL . startsWith ( 'data:' ) ) {
108
+ if ( sourceMapURL ) {
108
109
try {
109
- lineNumber = await processScriptWithInlineSourceMap ( { file , line, sourceMapURL } )
110
+ lineNumber = await getSourceMappedLine ( url , source , line , sourceMapURL )
110
111
} catch ( err ) {
111
- log . error ( 'Error processing script with inline source map' , err )
112
+ log . error ( 'Error processing script with source map' , err )
113
+ }
114
+ if ( lineNumber === null ) {
115
+ log . error ( 'Could not find generated position for %s:%s' , url , line )
116
+ lineNumber = line
112
117
}
113
118
}
114
119
@@ -123,51 +128,11 @@ async function addBreakpoint (probe) {
123
128
breakpointIdToProbe . set ( breakpointId , probe )
124
129
probeIdToBreakpointId . set ( probe . id , breakpointId )
125
130
} catch ( e ) {
126
- log . error ( ` Error setting breakpoint at ${ path } : ${ line } :` , e )
131
+ log . error ( ' Error setting breakpoint at %s:%s' , url , line , e )
127
132
}
128
133
}
129
134
130
135
function start ( ) {
131
136
sessionStarted = true
132
137
return session . post ( 'Debugger.enable' ) // return instead of await to reduce number of promises created
133
138
}
134
-
135
- async function processScriptWithInlineSourceMap ( params ) {
136
- const { file, line, sourceMapURL } = params
137
-
138
- // Extract the base64-encoded source map
139
- const base64SourceMap = sourceMapURL . split ( 'base64,' ) [ 1 ]
140
-
141
- // Decode the base64 source map
142
- const decodedSourceMap = Buffer . from ( base64SourceMap , 'base64' ) . toString ( 'utf8' )
143
-
144
- // Parse the source map
145
- const consumer = await new sourceMap . SourceMapConsumer ( decodedSourceMap )
146
-
147
- let generatedPosition
148
-
149
- // Map to the generated position. We'll attempt with the full file path first, then with the basename.
150
- // TODO: figure out why sometimes the full path doesn't work
151
- generatedPosition = consumer . generatedPositionFor ( {
152
- source : file ,
153
- line,
154
- column : 0
155
- } )
156
- if ( generatedPosition . line === null ) {
157
- generatedPosition = consumer . generatedPositionFor ( {
158
- source : path . basename ( file ) ,
159
- line,
160
- column : 0
161
- } )
162
- }
163
-
164
- consumer . destroy ( )
165
-
166
- // If we can't find the line, just return the original line
167
- if ( generatedPosition . line === null ) {
168
- log . error ( `Could not find generated position for ${ file } :${ line } ` )
169
- return line
170
- }
171
-
172
- return generatedPosition . line
173
- }
0 commit comments