@@ -23,29 +23,75 @@ function dynamicRequire(mod, request) {
23
23
return mod . require ( request ) ;
24
24
}
25
25
26
- // Only install once if called multiple times
27
- var errorFormatterInstalled = false ;
28
- var uncaughtShimInstalled = false ;
26
+ // Increment this if the format of sharedData changes in a breaking way.
27
+ var sharedDataVersion = 1 ;
28
+
29
+ function initializeSharedData ( defaults ) {
30
+ var sharedDataKey = 'source-map-support/sharedData' ;
31
+ if ( typeof Symbol !== 'undefined' ) {
32
+ sharedDataKey = Symbol . for ( sharedDataKey ) ;
33
+ }
34
+ var sharedData = this [ sharedDataKey ] ;
35
+ if ( ! sharedData ) {
36
+ sharedData = { version : sharedDataVersion } ;
37
+ if ( Object . defineProperty ) {
38
+ Object . defineProperty ( this , sharedDataKey , { value : sharedData } ) ;
39
+ } else {
40
+ this [ sharedDataKey ] = sharedData ;
41
+ }
42
+ }
43
+ if ( sharedDataVersion !== sharedData . version ) {
44
+ throw new Error ( "Multiple incompatible instances of source-map-support were loaded" ) ;
45
+ }
46
+ for ( var key in defaults ) {
47
+ if ( ! ( key in sharedData ) ) {
48
+ sharedData [ key ] = defaults [ key ] ;
49
+ }
50
+ }
51
+ return sharedData ;
52
+ }
29
53
30
- // If true, the caches are reset before a stack trace formatting operation
31
- var emptyCacheBetweenOperations = false ;
54
+ // If multiple instances of source-map-support are loaded into the same
55
+ // context, they shouldn't overwrite each other. By storing handlers, caches,
56
+ // and other state on a shared object, different instances of
57
+ // source-map-support can work together in a limited way. This does require
58
+ // that future versions of source-map-support continue to support the fields on
59
+ // this object. If this internal contract ever needs to be broken, increment
60
+ // sharedDataVersion. (This version number is not the same as any of the
61
+ // package's version numbers, which should reflect the *external* API of
62
+ // source-map-support.)
63
+ var sharedData = initializeSharedData ( {
32
64
33
- // Supports {browser, node, auto}
34
- var environment = "auto" ;
65
+ // Only install once if called multiple times
66
+ errorFormatterInstalled : false ,
67
+ uncaughtShimInstalled : false ,
68
+
69
+ // If true, the caches are reset before a stack trace formatting operation
70
+ emptyCacheBetweenOperations : false ,
71
+
72
+ // Maps a file path to a string containing the file contents
73
+ fileContentsCache : { } ,
74
+
75
+ // Maps a file path to a source map for that file
76
+ sourceMapCache : { } ,
35
77
36
- // Maps a file path to a string containing the file contents
37
- var fileContentsCache = { } ;
78
+ // Priority list of retrieve handlers
79
+ retrieveFileHandlers : [ ] ,
80
+ retrieveMapHandlers : [ ] ,
38
81
39
- // Maps a file path to a source map for that file
40
- var sourceMapCache = { } ;
82
+ // Priority list of internally-implemented handlers.
83
+ // When resetting state, we must keep these.
84
+ internalRetrieveFileHandlers : [ ] ,
85
+ internalRetrieveMapHandlers : [ ] ,
86
+
87
+ } ) ;
88
+
89
+ // Supports {browser, node, auto}
90
+ var environment = "auto" ;
41
91
42
92
// Regex for detecting source maps
43
93
var reSourceMap = / ^ d a t a : a p p l i c a t i o n \/ j s o n [ ^ , ] + b a s e 6 4 , / ;
44
94
45
- // Priority list of retrieve handlers
46
- var retrieveFileHandlers = [ ] ;
47
- var retrieveMapHandlers = [ ] ;
48
-
49
95
function isInBrowser ( ) {
50
96
if ( environment === "browser" )
51
97
return true ;
@@ -58,21 +104,27 @@ function hasGlobalProcessEventEmitter() {
58
104
return ( ( typeof process === 'object' ) && ( process !== null ) && ( typeof process . on === 'function' ) ) ;
59
105
}
60
106
61
- function handlerExec ( list ) {
107
+ function handlerExec ( list , internalList ) {
62
108
return function ( arg ) {
63
109
for ( var i = 0 ; i < list . length ; i ++ ) {
64
110
var ret = list [ i ] ( arg ) ;
65
111
if ( ret ) {
66
112
return ret ;
67
113
}
68
114
}
115
+ for ( var i = 0 ; i < internalList . length ; i ++ ) {
116
+ var ret = internalList [ i ] ( arg ) ;
117
+ if ( ret ) {
118
+ return ret ;
119
+ }
120
+ }
69
121
return null ;
70
122
} ;
71
123
}
72
124
73
- var retrieveFile = handlerExec ( retrieveFileHandlers ) ;
125
+ var retrieveFile = handlerExec ( sharedData . retrieveFileHandlers , sharedData . internalRetrieveFileHandlers ) ;
74
126
75
- retrieveFileHandlers . push ( function ( path ) {
127
+ sharedData . internalRetrieveFileHandlers . push ( function ( path ) {
76
128
// Trim the path to make sure there is no extra whitespace.
77
129
path = path . trim ( ) ;
78
130
if ( / ^ f i l e : / . test ( path ) ) {
@@ -83,8 +135,8 @@ retrieveFileHandlers.push(function(path) {
83
135
'/' ; // file:///root-dir/file -> /root-dir/file
84
136
} ) ;
85
137
}
86
- if ( path in fileContentsCache ) {
87
- return fileContentsCache [ path ] ;
138
+ if ( path in sharedData . fileContentsCache ) {
139
+ return sharedData . fileContentsCache [ path ] ;
88
140
}
89
141
90
142
var contents = '' ;
@@ -105,7 +157,7 @@ retrieveFileHandlers.push(function(path) {
105
157
/* ignore any errors */
106
158
}
107
159
108
- return fileContentsCache [ path ] = contents ;
160
+ return sharedData . fileContentsCache [ path ] = contents ;
109
161
} ) ;
110
162
111
163
// Support URLs relative to a directory, but be careful about a protocol prefix
@@ -160,8 +212,8 @@ function retrieveSourceMapURL(source) {
160
212
// there is no source map. The map field may be either a string or the parsed
161
213
// JSON object (ie, it must be a valid argument to the SourceMapConsumer
162
214
// constructor).
163
- var retrieveSourceMap = handlerExec ( retrieveMapHandlers ) ;
164
- retrieveMapHandlers . push ( function ( source ) {
215
+ var retrieveSourceMap = handlerExec ( sharedData . retrieveMapHandlers , sharedData . internalRetrieveMapHandlers ) ;
216
+ sharedData . internalRetrieveMapHandlers . push ( function ( source ) {
165
217
var sourceMappingURL = retrieveSourceMapURL ( source ) ;
166
218
if ( ! sourceMappingURL ) return null ;
167
219
@@ -189,12 +241,12 @@ retrieveMapHandlers.push(function(source) {
189
241
} ) ;
190
242
191
243
function mapSourcePosition ( position ) {
192
- var sourceMap = sourceMapCache [ position . source ] ;
244
+ var sourceMap = sharedData . sourceMapCache [ position . source ] ;
193
245
if ( ! sourceMap ) {
194
246
// Call the (overrideable) retrieveSourceMap function to get the source map.
195
247
var urlAndMap = retrieveSourceMap ( position . source ) ;
196
248
if ( urlAndMap ) {
197
- sourceMap = sourceMapCache [ position . source ] = {
249
+ sourceMap = sharedData . sourceMapCache [ position . source ] = {
198
250
url : urlAndMap . url ,
199
251
map : new SourceMapConsumer ( urlAndMap . map )
200
252
} ;
@@ -206,12 +258,12 @@ function mapSourcePosition(position) {
206
258
var contents = sourceMap . map . sourcesContent [ i ] ;
207
259
if ( contents ) {
208
260
var url = supportRelativeURL ( sourceMap . url , source ) ;
209
- fileContentsCache [ url ] = contents ;
261
+ sharedData . fileContentsCache [ url ] = contents ;
210
262
}
211
263
} ) ;
212
264
}
213
265
} else {
214
- sourceMap = sourceMapCache [ position . source ] = {
266
+ sourceMap = sharedData . sourceMapCache [ position . source ] = {
215
267
url : null ,
216
268
map : null
217
269
} ;
@@ -434,9 +486,9 @@ const ErrorPrototypeToString = (err) =>Error.prototype.toString.call(err);
434
486
// This function is part of the V8 stack trace API, for more info see:
435
487
// https://v8.dev/docs/stack-trace-api
436
488
function prepareStackTrace ( error , stack ) {
437
- if ( emptyCacheBetweenOperations ) {
438
- fileContentsCache = { } ;
439
- sourceMapCache = { } ;
489
+ if ( sharedData . emptyCacheBetweenOperations ) {
490
+ sharedData . fileContentsCache = { } ;
491
+ sharedData . sourceMapCache = { } ;
440
492
}
441
493
442
494
// node gives its own errors special treatment. Mimic that behavior
@@ -474,7 +526,7 @@ function getErrorSource(error) {
474
526
var column = + match [ 3 ] ;
475
527
476
528
// Support the inline sourceContents inside the source map
477
- var contents = fileContentsCache [ source ] ;
529
+ var contents = sharedData . fileContentsCache [ source ] ;
478
530
479
531
// Support files on disk
480
532
if ( ! contents && fs && fs . existsSync ( source ) ) {
@@ -537,8 +589,8 @@ function shimEmitUncaughtException () {
537
589
} ;
538
590
}
539
591
540
- var originalRetrieveFileHandlers = retrieveFileHandlers . slice ( 0 ) ;
541
- var originalRetrieveMapHandlers = retrieveMapHandlers . slice ( 0 ) ;
592
+ var originalRetrieveFileHandlers = sharedData . retrieveFileHandlers . slice ( 0 ) ;
593
+ var originalRetrieveMapHandlers = sharedData . retrieveMapHandlers . slice ( 0 ) ;
542
594
543
595
exports . wrapCallSite = wrapCallSite ;
544
596
exports . getErrorSource = getErrorSource ;
@@ -582,20 +634,20 @@ exports.install = function(options) {
582
634
// directly from disk.
583
635
if ( options . retrieveFile ) {
584
636
if ( options . overrideRetrieveFile ) {
585
- retrieveFileHandlers . length = 0 ;
637
+ sharedData . retrieveFileHandlers . length = 0 ;
586
638
}
587
639
588
- retrieveFileHandlers . unshift ( options . retrieveFile ) ;
640
+ sharedData . retrieveFileHandlers . unshift ( options . retrieveFile ) ;
589
641
}
590
642
591
643
// Allow source maps to be found by methods other than reading the files
592
644
// directly from disk.
593
645
if ( options . retrieveSourceMap ) {
594
646
if ( options . overrideRetrieveSourceMap ) {
595
- retrieveMapHandlers . length = 0 ;
647
+ sharedData . retrieveMapHandlers . length = 0 ;
596
648
}
597
649
598
- retrieveMapHandlers . unshift ( options . retrieveSourceMap ) ;
650
+ sharedData . retrieveMapHandlers . unshift ( options . retrieveSourceMap ) ;
599
651
}
600
652
601
653
// Support runtime transpilers that include inline source maps
@@ -604,8 +656,8 @@ exports.install = function(options) {
604
656
605
657
if ( ! $compile . __sourceMapSupport ) {
606
658
Module . prototype . _compile = function ( content , filename ) {
607
- fileContentsCache [ filename ] = content ;
608
- sourceMapCache [ filename ] = undefined ;
659
+ sharedData . fileContentsCache [ filename ] = content ;
660
+ sharedData . sourceMapCache [ filename ] = undefined ;
609
661
return $compile . call ( this , content , filename ) ;
610
662
} ;
611
663
@@ -614,18 +666,18 @@ exports.install = function(options) {
614
666
}
615
667
616
668
// Configure options
617
- if ( ! emptyCacheBetweenOperations ) {
618
- emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
669
+ if ( ! sharedData . emptyCacheBetweenOperations ) {
670
+ sharedData . emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
619
671
options . emptyCacheBetweenOperations : false ;
620
672
}
621
673
622
674
// Install the error reformatter
623
- if ( ! errorFormatterInstalled ) {
624
- errorFormatterInstalled = true ;
675
+ if ( ! sharedData . errorFormatterInstalled ) {
676
+ sharedData . errorFormatterInstalled = true ;
625
677
Error . prepareStackTrace = prepareStackTrace ;
626
678
}
627
679
628
- if ( ! uncaughtShimInstalled ) {
680
+ if ( ! sharedData . uncaughtShimInstalled ) {
629
681
var installHandler = 'handleUncaughtExceptions' in options ?
630
682
options . handleUncaughtExceptions : true ;
631
683
@@ -648,19 +700,13 @@ exports.install = function(options) {
648
700
// generated JavaScript code will be shown above the stack trace instead of
649
701
// the original source code.
650
702
if ( installHandler && hasGlobalProcessEventEmitter ( ) ) {
651
- uncaughtShimInstalled = true ;
703
+ sharedData . uncaughtShimInstalled = true ;
652
704
shimEmitUncaughtException ( ) ;
653
705
}
654
706
}
655
707
} ;
656
708
657
709
exports . resetRetrieveHandlers = function ( ) {
658
- retrieveFileHandlers . length = 0 ;
659
- retrieveMapHandlers . length = 0 ;
660
-
661
- retrieveFileHandlers = originalRetrieveFileHandlers . slice ( 0 ) ;
662
- retrieveMapHandlers = originalRetrieveMapHandlers . slice ( 0 ) ;
663
-
664
- retrieveSourceMap = handlerExec ( retrieveMapHandlers ) ;
665
- retrieveFile = handlerExec ( retrieveFileHandlers ) ;
710
+ sharedData . retrieveFileHandlers . length = 0 ;
711
+ sharedData . retrieveMapHandlers . length = 0 ;
666
712
}
0 commit comments