@@ -25,7 +25,6 @@ const { Buffer } = require('buffer');
25
25
let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'source_map' , ( fn ) => {
26
26
debug = fn ;
27
27
} ) ;
28
- const { dirname, resolve } = require ( 'path' ) ;
29
28
const fs = require ( 'fs' ) ;
30
29
const { getOptionValue } = require ( 'internal/options' ) ;
31
30
const {
@@ -63,10 +62,8 @@ function getSourceMapsEnabled() {
63
62
function maybeCacheSourceMap ( filename , content , cjsModuleInstance ) {
64
63
const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
65
64
if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
66
- let basePath ;
67
65
try {
68
66
filename = normalizeReferrerURL ( filename ) ;
69
- basePath = dirname ( fileURLToPath ( filename ) ) ;
70
67
} catch ( err ) {
71
68
// This is most likely an [eval]-wrapper, which is currently not
72
69
// supported.
@@ -76,7 +73,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
76
73
77
74
const match = content . match ( / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = (?< sourceMappingURL > [ ^ \s ] + ) / ) ;
78
75
if ( match ) {
79
- const data = dataFromUrl ( basePath , match . groups . sourceMappingURL ) ;
76
+ const data = dataFromUrl ( filename , match . groups . sourceMappingURL ) ;
80
77
const url = data ? null : match . groups . sourceMappingURL ;
81
78
if ( cjsModuleInstance ) {
82
79
if ( ! Module ) Module = require ( 'internal/modules/cjs/loader' ) . Module ;
@@ -98,21 +95,21 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
98
95
}
99
96
}
100
97
101
- function dataFromUrl ( basePath , sourceMappingURL ) {
98
+ function dataFromUrl ( sourceURL , sourceMappingURL ) {
102
99
try {
103
100
const url = new URL ( sourceMappingURL ) ;
104
101
switch ( url . protocol ) {
105
102
case 'data:' :
106
- return sourceMapFromDataUrl ( basePath , url . pathname ) ;
103
+ return sourceMapFromDataUrl ( sourceURL , url . pathname ) ;
107
104
default :
108
105
debug ( `unknown protocol ${ url . protocol } ` ) ;
109
106
return null ;
110
107
}
111
108
} catch ( err ) {
112
109
debug ( err . stack ) ;
113
110
// 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 ) ;
116
113
}
117
114
}
118
115
@@ -128,11 +125,11 @@ function lineLengths(content) {
128
125
} ) ;
129
126
}
130
127
131
- function sourceMapFromFile ( sourceMapFile ) {
128
+ function sourceMapFromFile ( mapURL ) {
132
129
try {
133
- const content = fs . readFileSync ( sourceMapFile , 'utf8' ) ;
130
+ const content = fs . readFileSync ( fileURLToPath ( mapURL ) , 'utf8' ) ;
134
131
const data = JSONParse ( content ) ;
135
- return sourcesToAbsolute ( dirname ( sourceMapFile ) , data ) ;
132
+ return sourcesToAbsolute ( mapURL , data ) ;
136
133
} catch ( err ) {
137
134
debug ( err . stack ) ;
138
135
return null ;
@@ -141,7 +138,7 @@ function sourceMapFromFile(sourceMapFile) {
141
138
142
139
// data:[<mediatype>][;base64],<data> see:
143
140
// https://tools.ietf.org/html/rfc2397#section-2
144
- function sourceMapFromDataUrl ( basePath , url ) {
141
+ function sourceMapFromDataUrl ( sourceURL , url ) {
145
142
const [ format , data ] = url . split ( ',' ) ;
146
143
const splitFormat = format . split ( ';' ) ;
147
144
const contentType = splitFormat [ 0 ] ;
@@ -151,7 +148,7 @@ function sourceMapFromDataUrl(basePath, url) {
151
148
Buffer . from ( data , 'base64' ) . toString ( 'utf8' ) : data ;
152
149
try {
153
150
const parsedData = JSONParse ( decodedData ) ;
154
- return sourcesToAbsolute ( basePath , parsedData ) ;
151
+ return sourcesToAbsolute ( sourceURL , parsedData ) ;
155
152
} catch ( err ) {
156
153
debug ( err . stack ) ;
157
154
return null ;
@@ -165,14 +162,10 @@ function sourceMapFromDataUrl(basePath, url) {
165
162
// If the sources are not absolute URLs after prepending of the "sourceRoot",
166
163
// the sources are resolved relative to the SourceMap (like resolving script
167
164
// src in a html document).
168
- function sourcesToAbsolute ( base , data ) {
165
+ function sourcesToAbsolute ( baseURL , data ) {
169
166
data . sources = data . sources . map ( ( source ) => {
170
167
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 ;
176
169
} ) ;
177
170
// The sources array is now resolved to absolute URLs, sourceRoot should
178
171
// be updated to noop.
0 commit comments