33const {
44 ArrayPrototypePush,
55 JSONParse,
6+ ObjectFreeze,
67 RegExpPrototypeExec,
78 SafeMap,
89 StringPrototypeCodePointAt,
@@ -15,15 +16,15 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
1516 debug = fn ;
1617} ) ;
1718
18- const { validateBoolean } = require ( 'internal/validators' ) ;
19+ const { validateBoolean, validateObject } = require ( 'internal/validators' ) ;
1920const {
2021 setSourceMapsEnabled : setSourceMapsNative ,
2122} = internalBinding ( 'errors' ) ;
2223const {
2324 defaultPrepareStackTrace,
2425 setInternalPrepareStackTrace,
2526} = require ( 'internal/errors' ) ;
26- const { getLazy } = require ( 'internal/util' ) ;
27+ const { getLazy, isUnderNodeModules , kEmptyObject } = require ( 'internal/util' ) ;
2728
2829const getModuleSourceMapCache = getLazy ( ( ) => {
2930 const { SourceMapCacheMap } = require ( 'internal/source_map/source_map_cache_map' ) ;
@@ -45,30 +46,48 @@ const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
4546let SourceMap ;
4647
4748// 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 ;
5158}
5259
5360/**
5461 * 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]
5666 */
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' ) ;
5974
60- setSourceMapsNative ( val ) ;
61- if ( val ) {
75+ setSourceMapsNative ( enabled ) ;
76+ if ( enabled ) {
6277 const {
6378 prepareStackTraceWithSourceMaps,
6479 } = require ( 'internal/source_map/prepare_stack_trace' ) ;
6580 setInternalPrepareStackTrace ( prepareStackTraceWithSourceMaps ) ;
66- } else if ( sourceMapsEnabled !== undefined ) {
67- // Reset prepare stack trace callback only when disabling source maps.
81+ } else {
6882 setInternalPrepareStackTrace ( defaultPrepareStackTrace ) ;
6983 }
7084
71- sourceMapsEnabled = val ;
85+ sourceMapsSupport = ObjectFreeze ( {
86+ __proto__ : null ,
87+ enabled,
88+ nodeModules : nodeModules ,
89+ generatedCode : generatedCode ,
90+ } ) ;
7291}
7392
7493/**
@@ -130,14 +149,18 @@ function extractSourceMapURLMagicComment(content) {
130149 * @param {string | undefined } sourceMapURL - the source map url
131150 */
132151function 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 ;
135154 const { normalizeReferrerURL } = require ( 'internal/modules/helpers' ) ;
136155 filename = normalizeReferrerURL ( filename ) ;
137156 if ( filename === undefined ) {
138157 // This is most likely an invalid filename in sourceURL of [eval]-wrapper.
139158 return ;
140159 }
160+ if ( ! support . nodeModules && isUnderNodeModules ( filename ) ) {
161+ // Skip file under node_modules if not enabled.
162+ return ;
163+ }
141164
142165 if ( sourceMapURL === undefined ) {
143166 sourceMapURL = extractSourceMapURLMagicComment ( content ) ;
@@ -185,8 +208,8 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
185208 * @param {string } content - the eval'd source code
186209 */
187210function 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 ;
190213
191214 const sourceURL = extractSourceURLMagicComment ( content ) ;
192215 if ( sourceURL === null ) {
@@ -352,6 +375,10 @@ function findSourceMap(sourceURL) {
352375 return undefined ;
353376 }
354377
378+ if ( ! getSourceMapsSupport ( ) . nodeModules && isUnderNodeModules ( sourceURL ) ) {
379+ return undefined ;
380+ }
381+
355382 SourceMap ??= require ( 'internal/source_map/source_map' ) . SourceMap ;
356383 try {
357384 if ( RegExpPrototypeExec ( kLeadingProtocol , sourceURL ) === null ) {
@@ -377,8 +404,8 @@ function findSourceMap(sourceURL) {
377404
378405module . exports = {
379406 findSourceMap,
380- getSourceMapsEnabled ,
381- setSourceMapsEnabled ,
407+ getSourceMapsSupport ,
408+ setSourceMapsSupport ,
382409 maybeCacheSourceMap,
383410 maybeCacheGeneratedSourceMap,
384411 sourceMapCacheToObject,
0 commit comments