-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process: initial SourceMap support via NODE_V8_COVERAGE #28960
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use strict'; | ||
|
||
// See https://sourcemaps.info/spec.html for SourceMap V3 specification. | ||
const { Buffer } = require('buffer'); | ||
const debug = require('internal/util/debuglog').debuglog('source_map'); | ||
const { dirname, resolve } = require('path'); | ||
const fs = require('fs'); | ||
const { | ||
normalizeReferrerURL, | ||
} = require('internal/modules/cjs/helpers'); | ||
const { JSON, Object } = primordials; | ||
// For cjs, since Module._cache is exposed to users, we use a WeakMap | ||
// keyed on module, facilitating garbage collection. | ||
const cjsSourceMapCache = new WeakMap(); | ||
// The esm cache is not exposed to users, so we can use a Map keyed | ||
// on filenames. | ||
const esmSourceMapCache = new Map(); | ||
const { fileURLToPath, URL } = require('url'); | ||
|
||
function maybeCacheSourceMap(filename, content, cjsModuleInstance) { | ||
if (!process.env.NODE_V8_COVERAGE) return; | ||
|
||
let basePath; | ||
try { | ||
filename = normalizeReferrerURL(filename); | ||
basePath = dirname(fileURLToPath(filename)); | ||
} catch (err) { | ||
// This is most likely an [eval]-wrapper, which is currently not | ||
// supported. | ||
debug(err.stack); | ||
return; | ||
} | ||
|
||
const match = content.match(/\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/); | ||
if (match) { | ||
if (cjsModuleInstance) { | ||
cjsSourceMapCache.set(cjsModuleInstance, { | ||
url: match.groups.sourceMappingURL, | ||
data: dataFromUrl(basePath, match.groups.sourceMappingURL) | ||
}); | ||
} else { | ||
// If there is no cjsModuleInstance assume we are in a | ||
// "modules/esm" context. | ||
esmSourceMapCache.set(filename, { | ||
url: match.groups.sourceMappingURL, | ||
data: dataFromUrl(basePath, match.groups.sourceMappingURL) | ||
}); | ||
} | ||
} | ||
} | ||
|
||
function dataFromUrl(basePath, sourceMappingURL) { | ||
try { | ||
const url = new URL(sourceMappingURL); | ||
switch (url.protocol) { | ||
case 'data:': | ||
return sourceMapFromDataUrl(basePath, url.pathname); | ||
default: | ||
debug(`unknown protocol ${url.protocol}`); | ||
return null; | ||
} | ||
} catch (err) { | ||
debug(err.stack); | ||
// If no scheme is present, we assume we are dealing with a file path. | ||
const sourceMapFile = resolve(basePath, sourceMappingURL); | ||
return sourceMapFromFile(sourceMapFile); | ||
} | ||
} | ||
|
||
function sourceMapFromFile(sourceMapFile) { | ||
try { | ||
const content = fs.readFileSync(sourceMapFile, 'utf8'); | ||
const data = JSON.parse(content); | ||
bcoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sourcesToAbsolute(dirname(sourceMapFile), data); | ||
} catch (err) { | ||
debug(err.stack); | ||
return null; | ||
} | ||
} | ||
|
||
// data:[<mediatype>][;base64],<data> see: | ||
// https://tools.ietf.org/html/rfc2397#section-2 | ||
function sourceMapFromDataUrl(basePath, url) { | ||
const [format, data] = url.split(','); | ||
const splitFormat = format.split(';'); | ||
const contentType = splitFormat[0]; | ||
const base64 = splitFormat[splitFormat.length - 1] === 'base64'; | ||
if (contentType === 'application/json') { | ||
const decodedData = base64 ? | ||
Buffer.from(data, 'base64').toString('utf8') : data; | ||
try { | ||
const parsedData = JSON.parse(decodedData); | ||
return sourcesToAbsolute(basePath, parsedData); | ||
} catch (err) { | ||
debug(err.stack); | ||
return null; | ||
} | ||
} else { | ||
debug(`unknown content-type ${contentType}`); | ||
return null; | ||
} | ||
} | ||
|
||
// If the sources are not absolute URLs after prepending of the "sourceRoot", | ||
// the sources are resolved relative to the SourceMap (like resolving script | ||
// src in a html document). | ||
function sourcesToAbsolute(base, data) { | ||
data.sources = data.sources.map((source) => { | ||
source = (data.sourceRoot || '') + source; | ||
if (!/^[\\/]/.test(source[0])) { | ||
source = resolve(base, source); | ||
} | ||
if (!source.startsWith('file://')) source = `file://${source}`; | ||
addaleax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return source; | ||
}); | ||
// The sources array is now resolved to absolute URLs, sourceRoot should | ||
// be updated to noop. | ||
data.sourceRoot = ''; | ||
return data; | ||
} | ||
|
||
function sourceMapCacheToObject() { | ||
const obj = Object.create(null); | ||
|
||
for (const [k, v] of esmSourceMapCache) { | ||
obj[k] = v; | ||
} | ||
appendCJSCache(obj); | ||
|
||
if (Object.keys(obj).length === 0) { | ||
return undefined; | ||
} else { | ||
return obj; | ||
} | ||
} | ||
|
||
// Since WeakMap can't be iterated over, we use Module._cache's | ||
// keys to facilitate Source Map serialization. | ||
function appendCJSCache(obj) { | ||
const { Module } = require('internal/modules/cjs/loader'); | ||
Object.keys(Module._cache).forEach((key) => { | ||
const value = cjsSourceMapCache.get(Module._cache[key]); | ||
if (value) { | ||
obj[`file://${key}`] = value; | ||
} | ||
}); | ||
} | ||
bcoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = { | ||
sourceMapCacheToObject, | ||
maybeCacheSourceMap | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a future improvement we can support valid but not file URLs here (like http URLs) although I believe data urls are by far the most common with relative file URLs a close second