1
1
const { TraceMap, originalPositionFor, AnyMap } = require ( '@jridgewell/trace-mapping' ) ;
2
2
const resolveUri = require ( '@jridgewell/resolve-uri' ) ;
3
3
var path = require ( 'path' ) ;
4
- const { fileURLToPath } = require ( 'url' ) ;
4
+ const { fileURLToPath, pathToFileURL } = require ( 'url' ) ;
5
5
var util = require ( 'util' ) ;
6
6
7
7
var fs ;
@@ -94,11 +94,11 @@ var sharedData = initializeSharedData({
94
94
emptyCacheBetweenOperations : false ,
95
95
96
96
// Maps a file path to a string containing the file contents
97
- fileContentsCache : { } ,
97
+ fileContentsCache : Object . create ( null ) ,
98
98
99
99
// Maps a file path to a source map for that file
100
100
/** @type {Record<string, {url: string, map: TraceMap} } */
101
- sourceMapCache : { } ,
101
+ sourceMapCache : Object . create ( null ) ,
102
102
103
103
// Priority list of retrieve handlers
104
104
retrieveFileHandlers : [ ] ,
@@ -129,6 +129,44 @@ function hasGlobalProcessEventEmitter() {
129
129
return ( ( typeof process === 'object' ) && ( process !== null ) && ( typeof process . on === 'function' ) ) ;
130
130
}
131
131
132
+ // #region Caches
133
+ /** @param {string } pathOrFileUrl */
134
+ function getCacheKey ( pathOrFileUrl ) {
135
+ if ( pathOrFileUrl . startsWith ( 'file:/' ) ) {
136
+ // Must normalize spaces to %20, stuff like that
137
+ return new URL ( pathOrFileUrl ) . toString ( ) ;
138
+ } else {
139
+ try {
140
+ return pathToFileURL ( pathOrFileUrl ) . toString ( ) ;
141
+ } catch {
142
+ return pathOrFileUrl ;
143
+ }
144
+ }
145
+ }
146
+ function getFileContentsCache ( key ) {
147
+ return sharedData . fileContentsCache [ getCacheKey ( key ) ] ;
148
+ }
149
+ function hasFileContentsCacheFromKey ( key ) {
150
+ return Object . prototype . hasOwnProperty . call ( sharedData . fileContentsCache , key ) ;
151
+ }
152
+ function getFileContentsCacheFromKey ( key ) {
153
+ return sharedData . fileContentsCache [ key ] ;
154
+ }
155
+ function setFileContentsCache ( key , value ) {
156
+ return sharedData . fileContentsCache [ getCacheKey ( key ) ] = value ;
157
+ }
158
+ function getSourceMapCache ( key ) {
159
+ return sharedData . sourceMapCache [ getCacheKey ( key ) ] ;
160
+ }
161
+ function setSourceMapCache ( key , value ) {
162
+ return sharedData . sourceMapCache [ getCacheKey ( key ) ] = value ;
163
+ }
164
+ function clearCaches ( ) {
165
+ sharedData . fileContentsCache = Object . create ( null ) ;
166
+ sharedData . sourceMapCache = Object . create ( null ) ;
167
+ }
168
+ // #endregion Caches
169
+
132
170
function handlerExec ( list , internalList ) {
133
171
return function ( arg ) {
134
172
for ( var i = 0 ; i < list . length ; i ++ ) {
@@ -160,8 +198,9 @@ sharedData.internalRetrieveFileHandlers.push(function(path) {
160
198
'/' ; // file:///root-dir/file -> /root-dir/file
161
199
} ) ;
162
200
}
163
- if ( path in sharedData . fileContentsCache ) {
164
- return sharedData . fileContentsCache [ path ] ;
201
+ const key = getCacheKey ( path ) ;
202
+ if ( hasFileContentsCacheFromKey ( key ) ) {
203
+ return getFileContentsCacheFromKey ( key ) ;
165
204
}
166
205
167
206
var contents = '' ;
@@ -182,7 +221,7 @@ sharedData.internalRetrieveFileHandlers.push(function(path) {
182
221
/* ignore any errors */
183
222
}
184
223
185
- return sharedData . fileContentsCache [ path ] = contents ;
224
+ return setFileContentsCache ( path , contents ) ;
186
225
} ) ;
187
226
188
227
// Support URLs relative to a directory, but be careful about a protocol prefix
@@ -257,15 +296,15 @@ sharedData.internalRetrieveMapHandlers.push(function(source) {
257
296
} ) ;
258
297
259
298
function mapSourcePosition ( position ) {
260
- var sourceMap = sharedData . sourceMapCache [ position . source ] ;
299
+ var sourceMap = getSourceMapCache ( position . source ) ;
261
300
if ( ! sourceMap ) {
262
301
// Call the (overrideable) retrieveSourceMap function to get the source map.
263
302
var urlAndMap = retrieveSourceMap ( position . source ) ;
264
303
if ( urlAndMap ) {
265
- sourceMap = sharedData . sourceMapCache [ position . source ] = {
304
+ sourceMap = setSourceMapCache ( position . source , {
266
305
url : urlAndMap . url ,
267
306
map : new AnyMap ( urlAndMap . map , urlAndMap . url )
268
- } ;
307
+ } ) ;
269
308
270
309
// Load all sources stored inline with the source map into the file cache
271
310
// to pretend like they are already loaded. They may not exist on disk.
@@ -274,15 +313,15 @@ function mapSourcePosition(position) {
274
313
var contents = sourceMap . map . sourcesContent [ i ] ;
275
314
if ( contents ) {
276
315
var url = supportRelativeURL ( sourceMap . url , source ) ;
277
- sharedData . fileContentsCache [ url ] = contents ;
316
+ setFileContentsCache ( url , contents ) ;
278
317
}
279
318
} ) ;
280
319
}
281
320
} else {
282
- sourceMap = sharedData . sourceMapCache [ position . source ] = {
321
+ sourceMap = setSourceMapCache ( position . source , {
283
322
url : null ,
284
323
map : null
285
- } ;
324
+ } ) ;
286
325
}
287
326
}
288
327
@@ -509,8 +548,7 @@ function createPrepareStackTrace(hookState) {
509
548
if ( ! hookState . enabled ) return hookState . originalValue . apply ( this , arguments ) ;
510
549
511
550
if ( sharedData . emptyCacheBetweenOperations ) {
512
- sharedData . fileContentsCache = { } ;
513
- sharedData . sourceMapCache = { } ;
551
+ clearCaches ( ) ;
514
552
}
515
553
516
554
// node gives its own errors special treatment. Mimic that behavior
@@ -550,7 +588,7 @@ function getErrorSource(error) {
550
588
var column = + match [ 3 ] ;
551
589
552
590
// Support the inline sourceContents inside the source map
553
- var contents = sharedData . fileContentsCache [ source ] ;
591
+ var contents = getFileContentsCache ( source ) ;
554
592
555
593
// Support files on disk
556
594
if ( ! contents && fs && fs . existsSync ( source ) ) {
@@ -705,8 +743,8 @@ exports.install = function(options) {
705
743
706
744
if ( ! $compile . __sourceMapSupport ) {
707
745
Module . prototype . _compile = function ( content , filename ) {
708
- sharedData . fileContentsCache [ filename ] = content ;
709
- sharedData . sourceMapCache [ filename ] = undefined ;
746
+ setFileContentsCache ( filename , content ) ;
747
+ setSourceMapCache ( filename , undefined ) ;
710
748
return $compile . call ( this , content , filename ) ;
711
749
} ;
712
750
0 commit comments