3
3
const {
4
4
ArrayPrototypePush,
5
5
JSONParse,
6
+ ObjectFreeze,
6
7
RegExpPrototypeExec,
7
8
SafeMap,
8
9
StringPrototypeCodePointAt,
@@ -15,15 +16,15 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
15
16
debug = fn ;
16
17
} ) ;
17
18
18
- const { validateBoolean } = require ( 'internal/validators' ) ;
19
+ const { validateBoolean, validateObject } = require ( 'internal/validators' ) ;
19
20
const {
20
21
setSourceMapsEnabled : setSourceMapsNative ,
21
22
} = internalBinding ( 'errors' ) ;
22
23
const {
23
24
defaultPrepareStackTrace,
24
25
setInternalPrepareStackTrace,
25
26
} = require ( 'internal/errors' ) ;
26
- const { getLazy } = require ( 'internal/util' ) ;
27
+ const { getLazy, isUnderNodeModules , kEmptyObject } = require ( 'internal/util' ) ;
27
28
28
29
const getModuleSourceMapCache = getLazy ( ( ) => {
29
30
const { SourceMapCacheMap } = require ( 'internal/source_map/source_map_cache_map' ) ;
@@ -45,30 +46,48 @@ const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
45
46
let SourceMap ;
46
47
47
48
// This is configured with --enable-source-maps during pre-execution.
48
- let sourceMapsEnabled = false ;
49
- function getSourceMapsEnabled ( ) {
50
- return sourceMapsEnabled ;
49
+ let sourceMapsSupport = ObjectFreeze ( {
50
+ __proto__ : null ,
51
+ enabled : false ,
52
+ nodeModules : false ,
53
+ generatedCode : false ,
54
+ } ) ;
55
+ function getSourceMapsSupport ( ) {
56
+ // Return a read-only object.
57
+ return sourceMapsSupport ;
51
58
}
52
59
53
60
/**
54
61
* Enables or disables source maps programmatically.
55
- * @param {boolean } val
62
+ * @param {boolean } enabled
63
+ * @param {object } options
64
+ * @param {boolean } [options.nodeModules]
65
+ * @param {boolean } [options.generatedCode]
56
66
*/
57
- function setSourceMapsEnabled ( val ) {
58
- validateBoolean ( val , 'val' ) ;
67
+ function setSourceMapsSupport ( enabled , options = kEmptyObject ) {
68
+ validateBoolean ( enabled , 'enabled' ) ;
69
+ validateObject ( options , 'options' ) ;
70
+
71
+ const { nodeModules = false , generatedCode = false } = options ;
72
+ validateBoolean ( nodeModules , 'options.nodeModules' ) ;
73
+ validateBoolean ( generatedCode , 'options.generatedCode' ) ;
59
74
60
- setSourceMapsNative ( val ) ;
61
- if ( val ) {
75
+ setSourceMapsNative ( enabled ) ;
76
+ if ( enabled ) {
62
77
const {
63
78
prepareStackTraceWithSourceMaps,
64
79
} = require ( 'internal/source_map/prepare_stack_trace' ) ;
65
80
setInternalPrepareStackTrace ( prepareStackTraceWithSourceMaps ) ;
66
- } else if ( sourceMapsEnabled !== undefined ) {
67
- // Reset prepare stack trace callback only when disabling source maps.
81
+ } else {
68
82
setInternalPrepareStackTrace ( defaultPrepareStackTrace ) ;
69
83
}
70
84
71
- sourceMapsEnabled = val ;
85
+ sourceMapsSupport = ObjectFreeze ( {
86
+ __proto__ : null ,
87
+ enabled,
88
+ nodeModules : nodeModules ,
89
+ generatedCode : generatedCode ,
90
+ } ) ;
72
91
}
73
92
74
93
/**
@@ -130,14 +149,18 @@ function extractSourceMapURLMagicComment(content) {
130
149
* @param {string | undefined } sourceMapURL - the source map url
131
150
*/
132
151
function maybeCacheSourceMap ( filename , content , moduleInstance , isGeneratedSource , sourceURL , sourceMapURL ) {
133
- const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
134
- if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
152
+ const support = getSourceMapsSupport ( ) ;
153
+ if ( ! ( process . env . NODE_V8_COVERAGE || support . enabled ) ) return ;
135
154
const { normalizeReferrerURL } = require ( 'internal/modules/helpers' ) ;
136
155
filename = normalizeReferrerURL ( filename ) ;
137
156
if ( filename === undefined ) {
138
157
// This is most likely an invalid filename in sourceURL of [eval]-wrapper.
139
158
return ;
140
159
}
160
+ if ( ! support . nodeModules && isUnderNodeModules ( filename ) ) {
161
+ // Skip file under node_modules if not enabled.
162
+ return ;
163
+ }
141
164
142
165
if ( sourceMapURL === undefined ) {
143
166
sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
@@ -185,8 +208,8 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
185
208
* @param {string } content - the eval'd source code
186
209
*/
187
210
function maybeCacheGeneratedSourceMap ( content ) {
188
- const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
189
- if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
211
+ const support = getSourceMapsSupport ( ) ;
212
+ if ( ! ( process . env . NODE_V8_COVERAGE || support . enabled || support . generated ) ) return ;
190
213
191
214
const sourceURL = extractSourceURLMagicComment ( content ) ;
192
215
if ( sourceURL === null ) {
@@ -352,6 +375,10 @@ function findSourceMap(sourceURL) {
352
375
return undefined ;
353
376
}
354
377
378
+ if ( ! getSourceMapsSupport ( ) . nodeModules && isUnderNodeModules ( sourceURL ) ) {
379
+ return undefined ;
380
+ }
381
+
355
382
SourceMap ??= require ( 'internal/source_map/source_map' ) . SourceMap ;
356
383
try {
357
384
if ( RegExpPrototypeExec ( kLeadingProtocol , sourceURL ) === null ) {
@@ -377,8 +404,8 @@ function findSourceMap(sourceURL) {
377
404
378
405
module . exports = {
379
406
findSourceMap,
380
- getSourceMapsEnabled ,
381
- setSourceMapsEnabled ,
407
+ getSourceMapsSupport ,
408
+ setSourceMapsSupport ,
382
409
maybeCacheSourceMap,
383
410
maybeCacheGeneratedSourceMap,
384
411
sourceMapCacheToObject,
0 commit comments