@@ -23,39 +23,87 @@ function dynamicRequire(mod, request) {
23
23
return mod . require ( request ) ;
24
24
}
25
25
26
- // Only install once if called multiple times
27
- // Remember how the environment looked before installation so we can restore if able
28
- /** @type {HookState } */
29
- var errorPrepareStackTraceHook ;
30
- /** @type {HookState } */
31
- var processEmitHook ;
32
26
/**
33
27
* @typedef {{
34
28
* enabled: boolean;
35
29
* originalValue: any;
36
30
* installedValue: any;
37
31
* }} HookState
32
+ * Used for installing and uninstalling hooks
38
33
*/
39
34
40
- // If true, the caches are reset before a stack trace formatting operation
41
- var emptyCacheBetweenOperations = false ;
35
+ // Increment this if the format of sharedData changes in a breaking way.
36
+ var sharedDataVersion = 1 ;
42
37
43
- // Supports {browser, node, auto}
44
- var environment = "auto" ;
38
+ function initializeSharedData ( defaults ) {
39
+ var sharedDataKey = 'source-map-support/sharedData' ;
40
+ if ( typeof Symbol !== 'undefined' ) {
41
+ sharedDataKey = Symbol . for ( sharedDataKey ) ;
42
+ }
43
+ var sharedData = this [ sharedDataKey ] ;
44
+ if ( ! sharedData ) {
45
+ sharedData = { version : sharedDataVersion } ;
46
+ if ( Object . defineProperty ) {
47
+ Object . defineProperty ( this , sharedDataKey , { value : sharedData } ) ;
48
+ } else {
49
+ this [ sharedDataKey ] = sharedData ;
50
+ }
51
+ }
52
+ if ( sharedDataVersion !== sharedData . version ) {
53
+ throw new Error ( "Multiple incompatible instances of source-map-support were loaded" ) ;
54
+ }
55
+ for ( var key in defaults ) {
56
+ if ( ! ( key in sharedData ) ) {
57
+ sharedData [ key ] = defaults [ key ] ;
58
+ }
59
+ }
60
+ return sharedData ;
61
+ }
45
62
46
- // Maps a file path to a string containing the file contents
47
- var fileContentsCache = { } ;
63
+ // If multiple instances of source-map-support are loaded into the same
64
+ // context, they shouldn't overwrite each other. By storing handlers, caches,
65
+ // and other state on a shared object, different instances of
66
+ // source-map-support can work together in a limited way. This does require
67
+ // that future versions of source-map-support continue to support the fields on
68
+ // this object. If this internal contract ever needs to be broken, increment
69
+ // sharedDataVersion. (This version number is not the same as any of the
70
+ // package's version numbers, which should reflect the *external* API of
71
+ // source-map-support.)
72
+ var sharedData = initializeSharedData ( {
73
+
74
+ // Only install once if called multiple times
75
+ // Remember how the environment looked before installation so we can restore if able
76
+ /** @type {HookState } */
77
+ errorPrepareStackTraceHook : undefined ,
78
+ /** @type {HookState } */
79
+ processEmitHook : undefined ,
80
+
81
+ // If true, the caches are reset before a stack trace formatting operation
82
+ emptyCacheBetweenOperations : false ,
83
+
84
+ // Maps a file path to a string containing the file contents
85
+ fileContentsCache : { } ,
86
+
87
+ // Maps a file path to a source map for that file
88
+ sourceMapCache : { } ,
89
+
90
+ // Priority list of retrieve handlers
91
+ retrieveFileHandlers : [ ] ,
92
+ retrieveMapHandlers : [ ] ,
93
+
94
+ // Priority list of internally-implemented handlers.
95
+ // When resetting state, we must keep these.
96
+ internalRetrieveFileHandlers : [ ] ,
97
+ internalRetrieveMapHandlers : [ ] ,
48
98
49
- // Maps a file path to a source map for that file
50
- var sourceMapCache = { } ;
99
+ } ) ;
100
+
101
+ // Supports {browser, node, auto}
102
+ var environment = "auto" ;
51
103
52
104
// Regex for detecting source maps
53
105
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 , / ;
54
106
55
- // Priority list of retrieve handlers
56
- var retrieveFileHandlers = [ ] ;
57
- var retrieveMapHandlers = [ ] ;
58
-
59
107
function isInBrowser ( ) {
60
108
if ( environment === "browser" )
61
109
return true ;
@@ -68,21 +116,27 @@ function hasGlobalProcessEventEmitter() {
68
116
return ( ( typeof process === 'object' ) && ( process !== null ) && ( typeof process . on === 'function' ) ) ;
69
117
}
70
118
71
- function handlerExec ( list ) {
119
+ function handlerExec ( list , internalList ) {
72
120
return function ( arg ) {
73
121
for ( var i = 0 ; i < list . length ; i ++ ) {
74
122
var ret = list [ i ] ( arg ) ;
75
123
if ( ret ) {
76
124
return ret ;
77
125
}
78
126
}
127
+ for ( var i = 0 ; i < internalList . length ; i ++ ) {
128
+ var ret = internalList [ i ] ( arg ) ;
129
+ if ( ret ) {
130
+ return ret ;
131
+ }
132
+ }
79
133
return null ;
80
134
} ;
81
135
}
82
136
83
- var retrieveFile = handlerExec ( retrieveFileHandlers ) ;
137
+ var retrieveFile = handlerExec ( sharedData . retrieveFileHandlers , sharedData . internalRetrieveFileHandlers ) ;
84
138
85
- retrieveFileHandlers . push ( function ( path ) {
139
+ sharedData . internalRetrieveFileHandlers . push ( function ( path ) {
86
140
// Trim the path to make sure there is no extra whitespace.
87
141
path = path . trim ( ) ;
88
142
if ( / ^ f i l e : / . test ( path ) ) {
@@ -93,8 +147,8 @@ retrieveFileHandlers.push(function(path) {
93
147
'/' ; // file:///root-dir/file -> /root-dir/file
94
148
} ) ;
95
149
}
96
- if ( path in fileContentsCache ) {
97
- return fileContentsCache [ path ] ;
150
+ if ( path in sharedData . fileContentsCache ) {
151
+ return sharedData . fileContentsCache [ path ] ;
98
152
}
99
153
100
154
var contents = '' ;
@@ -115,7 +169,7 @@ retrieveFileHandlers.push(function(path) {
115
169
/* ignore any errors */
116
170
}
117
171
118
- return fileContentsCache [ path ] = contents ;
172
+ return sharedData . fileContentsCache [ path ] = contents ;
119
173
} ) ;
120
174
121
175
// Support URLs relative to a directory, but be careful about a protocol prefix
@@ -170,8 +224,8 @@ function retrieveSourceMapURL(source) {
170
224
// there is no source map. The map field may be either a string or the parsed
171
225
// JSON object (ie, it must be a valid argument to the SourceMapConsumer
172
226
// constructor).
173
- var retrieveSourceMap = handlerExec ( retrieveMapHandlers ) ;
174
- retrieveMapHandlers . push ( function ( source ) {
227
+ var retrieveSourceMap = handlerExec ( sharedData . retrieveMapHandlers , sharedData . internalRetrieveMapHandlers ) ;
228
+ sharedData . internalRetrieveMapHandlers . push ( function ( source ) {
175
229
var sourceMappingURL = retrieveSourceMapURL ( source ) ;
176
230
if ( ! sourceMappingURL ) return null ;
177
231
@@ -199,12 +253,12 @@ retrieveMapHandlers.push(function(source) {
199
253
} ) ;
200
254
201
255
function mapSourcePosition ( position ) {
202
- var sourceMap = sourceMapCache [ position . source ] ;
256
+ var sourceMap = sharedData . sourceMapCache [ position . source ] ;
203
257
if ( ! sourceMap ) {
204
258
// Call the (overrideable) retrieveSourceMap function to get the source map.
205
259
var urlAndMap = retrieveSourceMap ( position . source ) ;
206
260
if ( urlAndMap ) {
207
- sourceMap = sourceMapCache [ position . source ] = {
261
+ sourceMap = sharedData . sourceMapCache [ position . source ] = {
208
262
url : urlAndMap . url ,
209
263
map : new SourceMapConsumer ( urlAndMap . map )
210
264
} ;
@@ -216,12 +270,12 @@ function mapSourcePosition(position) {
216
270
var contents = sourceMap . map . sourcesContent [ i ] ;
217
271
if ( contents ) {
218
272
var url = supportRelativeURL ( sourceMap . url , source ) ;
219
- fileContentsCache [ url ] = contents ;
273
+ sharedData . fileContentsCache [ url ] = contents ;
220
274
}
221
275
} ) ;
222
276
}
223
277
} else {
224
- sourceMap = sourceMapCache [ position . source ] = {
278
+ sourceMap = sharedData . sourceMapCache [ position . source ] = {
225
279
url : null ,
226
280
map : null
227
281
} ;
@@ -450,9 +504,9 @@ function createPrepareStackTrace(hookState) {
450
504
function prepareStackTrace ( error , stack ) {
451
505
if ( ! hookState . enabled ) return hookState . originalValue . apply ( this , arguments ) ;
452
506
453
- if ( emptyCacheBetweenOperations ) {
454
- fileContentsCache = { } ;
455
- sourceMapCache = { } ;
507
+ if ( sharedData . emptyCacheBetweenOperations ) {
508
+ sharedData . fileContentsCache = { } ;
509
+ sharedData . sourceMapCache = { } ;
456
510
}
457
511
458
512
// node gives its own errors special treatment. Mimic that behavior
@@ -491,7 +545,7 @@ function getErrorSource(error) {
491
545
var column = + match [ 3 ] ;
492
546
493
547
// Support the inline sourceContents inside the source map
494
- var contents = fileContentsCache [ source ] ;
548
+ var contents = sharedData . fileContentsCache [ source ] ;
495
549
496
550
// Support files on disk
497
551
if ( ! contents && fs && fs . existsSync ( source ) ) {
@@ -537,15 +591,15 @@ function printFatalErrorUponExit (error) {
537
591
538
592
function shimEmitUncaughtException ( ) {
539
593
const originalValue = process . emit ;
540
- var hook = processEmitHook = {
594
+ var hook = sharedData . processEmitHook = {
541
595
enabled : true ,
542
596
originalValue,
543
597
installedValue : undefined
544
598
} ;
545
599
var isTerminatingDueToFatalException = false ;
546
600
var fatalException ;
547
601
548
- process . emit = processEmitHook . installedValue = function ( type ) {
602
+ process . emit = sharedData . processEmitHook . installedValue = function ( type ) {
549
603
const hadListeners = originalValue . apply ( this , arguments ) ;
550
604
if ( hook . enabled ) {
551
605
if ( type === 'uncaughtException' && ! hadListeners ) {
@@ -561,8 +615,8 @@ function shimEmitUncaughtException () {
561
615
} ;
562
616
}
563
617
564
- var originalRetrieveFileHandlers = retrieveFileHandlers . slice ( 0 ) ;
565
- var originalRetrieveMapHandlers = retrieveMapHandlers . slice ( 0 ) ;
618
+ var originalRetrieveFileHandlers = sharedData . retrieveFileHandlers . slice ( 0 ) ;
619
+ var originalRetrieveMapHandlers = sharedData . retrieveMapHandlers . slice ( 0 ) ;
566
620
567
621
exports . wrapCallSite = wrapCallSite ;
568
622
exports . getErrorSource = getErrorSource ;
@@ -583,20 +637,20 @@ exports.install = function(options) {
583
637
// directly from disk.
584
638
if ( options . retrieveFile ) {
585
639
if ( options . overrideRetrieveFile ) {
586
- retrieveFileHandlers . length = 0 ;
640
+ sharedData . retrieveFileHandlers . length = 0 ;
587
641
}
588
642
589
- retrieveFileHandlers . unshift ( options . retrieveFile ) ;
643
+ sharedData . retrieveFileHandlers . unshift ( options . retrieveFile ) ;
590
644
}
591
645
592
646
// Allow source maps to be found by methods other than reading the files
593
647
// directly from disk.
594
648
if ( options . retrieveSourceMap ) {
595
649
if ( options . overrideRetrieveSourceMap ) {
596
- retrieveMapHandlers . length = 0 ;
650
+ sharedData . retrieveMapHandlers . length = 0 ;
597
651
}
598
652
599
- retrieveMapHandlers . unshift ( options . retrieveSourceMap ) ;
653
+ sharedData . retrieveMapHandlers . unshift ( options . retrieveSourceMap ) ;
600
654
}
601
655
602
656
// Support runtime transpilers that include inline source maps
@@ -607,8 +661,8 @@ exports.install = function(options) {
607
661
608
662
if ( ! $compile . __sourceMapSupport ) {
609
663
Module . prototype . _compile = function ( content , filename ) {
610
- fileContentsCache [ filename ] = content ;
611
- sourceMapCache [ filename ] = undefined ;
664
+ sharedData . fileContentsCache [ filename ] = content ;
665
+ sharedData . sourceMapCache [ filename ] = undefined ;
612
666
return $compile . call ( this , content , filename ) ;
613
667
} ;
614
668
@@ -617,24 +671,24 @@ exports.install = function(options) {
617
671
}
618
672
619
673
// Configure options
620
- if ( ! emptyCacheBetweenOperations ) {
621
- emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
674
+ if ( ! sharedData . emptyCacheBetweenOperations ) {
675
+ sharedData . emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
622
676
options . emptyCacheBetweenOperations : false ;
623
677
}
624
678
625
679
626
680
// Install the error reformatter
627
- if ( ! errorPrepareStackTraceHook ) {
681
+ if ( ! sharedData . errorPrepareStackTraceHook ) {
628
682
const originalValue = Error . prepareStackTrace ;
629
- errorPrepareStackTraceHook = {
683
+ sharedData . errorPrepareStackTraceHook = {
630
684
enabled : true ,
631
685
originalValue,
632
686
installedValue : undefined
633
687
} ;
634
- Error . prepareStackTrace = errorPrepareStackTraceHook . installedValue = createPrepareStackTrace ( errorPrepareStackTraceHook ) ;
688
+ Error . prepareStackTrace = sharedData . errorPrepareStackTraceHook . installedValue = createPrepareStackTrace ( sharedData . errorPrepareStackTraceHook ) ;
635
689
}
636
690
637
- if ( ! processEmitHook ) {
691
+ if ( ! sharedData . processEmitHook ) {
638
692
var installHandler = 'handleUncaughtExceptions' in options ?
639
693
options . handleUncaughtExceptions : true ;
640
694
@@ -663,36 +717,30 @@ exports.install = function(options) {
663
717
} ;
664
718
665
719
exports . uninstall = function ( ) {
666
- if ( processEmitHook ) {
720
+ if ( sharedData . processEmitHook ) {
667
721
// Disable behavior
668
- processEmitHook . enabled = false ;
722
+ sharedData . processEmitHook . enabled = false ;
669
723
// If possible, remove our hook function. May not be possible if subsequent third-party hooks have wrapped around us.
670
- if ( process . emit === processEmitHook . installedValue ) {
671
- process . emit = processEmitHook . originalValue ;
724
+ if ( process . emit === sharedData . processEmitHook . installedValue ) {
725
+ process . emit = sharedData . processEmitHook . originalValue ;
672
726
}
673
- processEmitHook = undefined ;
727
+ sharedData . processEmitHook = undefined ;
674
728
}
675
- if ( errorPrepareStackTraceHook ) {
729
+ if ( sharedData . errorPrepareStackTraceHook ) {
676
730
// Disable behavior
677
- errorPrepareStackTraceHook . enabled = false ;
731
+ sharedData . errorPrepareStackTraceHook . enabled = false ;
678
732
// If possible or necessary, remove our hook function.
679
733
// In vanilla environments, prepareStackTrace is `undefined`.
680
734
// We cannot delegate to `undefined` the way we can to a function w/`.apply()`; our only option is to remove the function.
681
735
// If we are the *first* hook installed, and another was installed on top of us, we have no choice but to remove both.
682
- if ( Error . prepareStackTrace === errorPrepareStackTraceHook . installedValue || typeof errorPrepareStackTraceHook . originalValue !== 'function' ) {
683
- Error . prepareStackTrace = errorPrepareStackTraceHook . originalValue ;
736
+ if ( Error . prepareStackTrace === sharedData . errorPrepareStackTraceHook . installedValue || typeof sharedData . errorPrepareStackTraceHook . originalValue !== 'function' ) {
737
+ Error . prepareStackTrace = sharedData . errorPrepareStackTraceHook . originalValue ;
684
738
}
685
- errorPrepareStackTraceHook = undefined ;
739
+ sharedData . errorPrepareStackTraceHook = undefined ;
686
740
}
687
741
}
688
742
689
743
exports . resetRetrieveHandlers = function ( ) {
690
- retrieveFileHandlers . length = 0 ;
691
- retrieveMapHandlers . length = 0 ;
692
-
693
- retrieveFileHandlers = originalRetrieveFileHandlers . slice ( 0 ) ;
694
- retrieveMapHandlers = originalRetrieveMapHandlers . slice ( 0 ) ;
695
-
696
- retrieveSourceMap = handlerExec ( retrieveMapHandlers ) ;
697
- retrieveFile = handlerExec ( retrieveFileHandlers ) ;
744
+ sharedData . retrieveFileHandlers . length = 0 ;
745
+ sharedData . retrieveMapHandlers . length = 0 ;
698
746
}
0 commit comments