@@ -25,7 +25,6 @@ const { Buffer } = require('buffer');
2525let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'source_map' , ( fn ) => {
2626 debug = fn ;
2727} ) ;
28- const { dirname, resolve } = require ( 'path' ) ;
2928const fs = require ( 'fs' ) ;
3029const { getOptionValue } = require ( 'internal/options' ) ;
3130const {
@@ -63,10 +62,8 @@ function getSourceMapsEnabled() {
6362function maybeCacheSourceMap ( filename , content , cjsModuleInstance ) {
6463 const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
6564 if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
66- let basePath ;
6765 try {
6866 filename = normalizeReferrerURL ( filename ) ;
69- basePath = dirname ( fileURLToPath ( filename ) ) ;
7067 } catch ( err ) {
7168 // This is most likely an [eval]-wrapper, which is currently not
7269 // supported.
@@ -76,7 +73,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
7673
7774 const match = content . match ( / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = (?< sourceMappingURL > [ ^ \s ] + ) / ) ;
7875 if ( match ) {
79- const data = dataFromUrl ( basePath , match . groups . sourceMappingURL ) ;
76+ const data = dataFromUrl ( filename , match . groups . sourceMappingURL ) ;
8077 const url = data ? null : match . groups . sourceMappingURL ;
8178 if ( cjsModuleInstance ) {
8279 if ( ! Module ) Module = require ( 'internal/modules/cjs/loader' ) . Module ;
@@ -98,21 +95,21 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
9895 }
9996}
10097
101- function dataFromUrl ( basePath , sourceMappingURL ) {
98+ function dataFromUrl ( sourceURL , sourceMappingURL ) {
10299 try {
103100 const url = new URL ( sourceMappingURL ) ;
104101 switch ( url . protocol ) {
105102 case 'data:' :
106- return sourceMapFromDataUrl ( basePath , url . pathname ) ;
103+ return sourceMapFromDataUrl ( sourceURL , url . pathname ) ;
107104 default :
108105 debug ( `unknown protocol ${ url . protocol } ` ) ;
109106 return null ;
110107 }
111108 } catch ( err ) {
112109 debug ( err . stack ) ;
113110 // If no scheme is present, we assume we are dealing with a file path.
114- const sourceMapFile = resolve ( basePath , sourceMappingURL ) ;
115- return sourceMapFromFile ( sourceMapFile ) ;
111+ const mapURL = new URL ( sourceMappingURL , sourceURL ) . href ;
112+ return sourceMapFromFile ( mapURL ) ;
116113 }
117114}
118115
@@ -128,11 +125,11 @@ function lineLengths(content) {
128125 } ) ;
129126}
130127
131- function sourceMapFromFile ( sourceMapFile ) {
128+ function sourceMapFromFile ( mapURL ) {
132129 try {
133- const content = fs . readFileSync ( sourceMapFile , 'utf8' ) ;
130+ const content = fs . readFileSync ( fileURLToPath ( mapURL ) , 'utf8' ) ;
134131 const data = JSONParse ( content ) ;
135- return sourcesToAbsolute ( dirname ( sourceMapFile ) , data ) ;
132+ return sourcesToAbsolute ( mapURL , data ) ;
136133 } catch ( err ) {
137134 debug ( err . stack ) ;
138135 return null ;
@@ -141,7 +138,7 @@ function sourceMapFromFile(sourceMapFile) {
141138
142139// data:[<mediatype>][;base64],<data> see:
143140// https://tools.ietf.org/html/rfc2397#section-2
144- function sourceMapFromDataUrl ( basePath , url ) {
141+ function sourceMapFromDataUrl ( sourceURL , url ) {
145142 const [ format , data ] = url . split ( ',' ) ;
146143 const splitFormat = format . split ( ';' ) ;
147144 const contentType = splitFormat [ 0 ] ;
@@ -151,7 +148,7 @@ function sourceMapFromDataUrl(basePath, url) {
151148 Buffer . from ( data , 'base64' ) . toString ( 'utf8' ) : data ;
152149 try {
153150 const parsedData = JSONParse ( decodedData ) ;
154- return sourcesToAbsolute ( basePath , parsedData ) ;
151+ return sourcesToAbsolute ( sourceURL , parsedData ) ;
155152 } catch ( err ) {
156153 debug ( err . stack ) ;
157154 return null ;
@@ -165,14 +162,10 @@ function sourceMapFromDataUrl(basePath, url) {
165162// If the sources are not absolute URLs after prepending of the "sourceRoot",
166163// the sources are resolved relative to the SourceMap (like resolving script
167164// src in a html document).
168- function sourcesToAbsolute ( base , data ) {
165+ function sourcesToAbsolute ( baseURL , data ) {
169166 data . sources = data . sources . map ( ( source ) => {
170167 source = ( data . sourceRoot || '' ) + source ;
171- if ( ! / ^ [ \\ / ] / . test ( source [ 0 ] ) ) {
172- source = resolve ( base , source ) ;
173- }
174- if ( ! source . startsWith ( 'file://' ) ) source = `file://${ source } ` ;
175- return source ;
168+ return new URL ( source , baseURL ) . href ;
176169 } ) ;
177170 // The sources array is now resolved to absolute URLs, sourceRoot should
178171 // be updated to noop.
0 commit comments