Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/main/embedding.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function embedderRunCjs(content) {
function: compiledWrapper,
cachedDataRejected,
sourceMapURL,
sourceURL,
} = compileFunctionForCJSLoader(
content,
filename,
Expand All @@ -69,7 +70,7 @@ function embedderRunCjs(content) {
content,
customModule,
false, // isGeneratedSource
undefined, // sourceURL, TODO(joyeecheung): should be extracted by V8
sourceURL,
sourceMapURL,
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1640,9 +1640,9 @@ function wrapSafe(filename, content, cjsModuleInstance, format) {
);

// Cache the source map for the module if present.
const { sourceMapURL } = script;
const { sourceMapURL, sourceURL } = script;
if (sourceMapURL) {
maybeCacheSourceMap(filename, content, cjsModuleInstance, false, undefined, sourceMapURL);
maybeCacheSourceMap(filename, content, cjsModuleInstance, false, sourceURL, sourceMapURL);
}

return {
Expand All @@ -1667,7 +1667,7 @@ function wrapSafe(filename, content, cjsModuleInstance, format) {

// Cache the source map for the module if present.
if (result.sourceMapURL) {
maybeCacheSourceMap(filename, content, cjsModuleInstance, false, undefined, result.sourceMapURL);
maybeCacheSourceMap(filename, content, cjsModuleInstance, false, result.sourceURL, result.sourceMapURL);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ translators.set('module', function moduleStrategy(url, source, isMain) {
function loadCJSModule(module, source, url, filename, isMain) {
const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false);

const { function: compiledWrapper, sourceMapURL } = compileResult;
const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
// Cache the source map for the cjs module if present.
if (sourceMapURL) {
maybeCacheSourceMap(url, source, module, false, undefined, sourceMapURL);
maybeCacheSourceMap(url, source, module, false, sourceURL, sourceMapURL);
}

const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function compileSourceTextModule(url, source, cascadedLoader) {
}
// Cache the source map for the module if present.
if (wrap.sourceMapURL) {
maybeCacheSourceMap(url, source, wrap, false, undefined, wrap.sourceMapURL);
maybeCacheSourceMap(url, source, wrap, false, wrap.sourceURL, wrap.sourceMapURL);
}
return wrap;
}
Expand Down
26 changes: 12 additions & 14 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ function extractSourceMapURLMagicComment(content) {
}

/**
* Caches the source map if it is present in the content, with the given filename, moduleInstance, and sourceURL.
* Caches the source map, with the given filename, moduleInstance, sourceURL and sourceMapURL.
* This function does not automatically extract the source map from the content. The caller should either
* extract the source map from the content via V8 API or use {@link extractSourceURLMagicComment} explicitly.
* @param {string} filename - the actual filename
* @param {string} content - the actual source content
* @param {import('internal/modules/cjs/loader').Module | ModuleWrap} moduleInstance - a module instance that
Expand All @@ -162,20 +164,13 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
return;
}

if (sourceMapURL === undefined) {
sourceMapURL = extractSourceMapURLMagicComment(content);
}

// Bail out when there is no source map url.
if (typeof sourceMapURL !== 'string') {
return;
}

// FIXME: callers should obtain sourceURL from v8 and pass it
// rather than leaving it undefined and extract by regex.
if (sourceURL === undefined) {
sourceURL = extractSourceURLMagicComment(content);
}
// Normalize the sourceURL to a file URL if it is a path.
sourceURL = normalizeReferrerURL(sourceURL);

const data = dataFromUrl(filename, sourceMapURL);
// `data` could be null if the source map is invalid.
Expand All @@ -192,9 +187,6 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc

if (isGeneratedSource) {
generatedSourceMapCache.set(filename, entry);
if (sourceURL) {
generatedSourceMapCache.set(sourceURL, entry);
}
return;
}
// If it is not a generated source, we assume we are in a "cjs/esm"
Expand All @@ -215,8 +207,14 @@ function maybeCacheGeneratedSourceMap(content) {
if (sourceURL === null) {
return;
}
const sourceMapURL = extractSourceMapURLMagicComment(content);
if (sourceMapURL === null) {
return;
}

try {
maybeCacheSourceMap(sourceURL, content, null, true, sourceURL);
// Use the sourceURL as the filename, and do not create a duplicate entry.
maybeCacheSourceMap(sourceURL, content, null, true, undefined /** no duplicated sourceURL */, sourceMapURL);
} catch (err) {
// This can happen if the filename is not a valid URL.
// If we fail to cache the source map, we should not fail the whole process.
Expand Down
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
V(sni_context_string, "sni_context") \
V(source_string, "source") \
V(source_map_url_string, "sourceMapURL") \
V(source_url_string, "sourceURL") \
V(specifier_string, "specifier") \
V(stack_string, "stack") \
V(standard_name_string, "standardName") \
Expand Down
7 changes: 7 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
return;
}

if (that->Set(context,
realm->env()->source_url_string(),
module->GetUnboundModuleScript()->GetSourceURL())
.IsNothing()) {
return;
}

if (that->Set(context,
realm->env()->source_map_url_string(),
module->GetUnboundModuleScript()->GetSourceMappingURL())
Expand Down
24 changes: 23 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,13 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
return;
}

if (args.This()
->Set(env->context(),
env->source_url_string(),
v8_script->GetSourceURL())
.IsNothing())
return;

if (args.This()
->Set(env->context(),
env->source_map_url_string(),
Expand Down Expand Up @@ -1575,12 +1582,23 @@ MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult(
Local<Object> result = Object::New(isolate);
if (result->Set(parsing_context, env->function_string(), fn).IsNothing())
return {};

// ScriptOrigin::ResourceName() returns SourceURL magic comment content if
// present.
if (result
->Set(parsing_context,
env->source_url_string(),
fn->GetScriptOrigin().ResourceName())
.IsNothing()) {
return {};
}
if (result
->Set(parsing_context,
env->source_map_url_string(),
fn->GetScriptOrigin().SourceMapUrl())
.IsNothing())
.IsNothing()) {
return {};
}

std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
if (produce_cached_data) {
Expand Down Expand Up @@ -1818,12 +1836,16 @@ static void CompileFunctionForCJSLoader(
Local<Name> names[] = {
env->cached_data_rejected_string(),
env->source_map_url_string(),
env->source_url_string(),
env->function_string(),
FIXED_ONE_BYTE_STRING(isolate, "canParseAsESM"),
};
Local<Value> values[] = {
Boolean::New(isolate, cache_rejected),
fn.IsEmpty() ? undefined : fn->GetScriptOrigin().SourceMapUrl(),
// ScriptOrigin::ResourceName() returns SourceURL magic comment content if
// present.
fn.IsEmpty() ? undefined : fn->GetScriptOrigin().ResourceName(),
fn.IsEmpty() ? undefined : fn.As<Value>(),
Boolean::New(isolate, can_parse_as_esm),
};
Expand Down
14 changes: 11 additions & 3 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function replaceStackTrace(str, replacement = '$1*$7$8\n') {
return str.replace(stackFramesRegexp, replacement);
}

function replaceInternalStackTrace(str) {
return str.replaceAll(/(\W+).*node:internal.*/g, '$1*');
}

function replaceWindowsLineEndings(str) {
return str.replace(windowNewlineRegexp, '');
}
Expand All @@ -24,8 +28,11 @@ function replaceWindowsPaths(str) {
return common.isWindows ? str.replaceAll(path.win32.sep, path.posix.sep) : str;
}

function replaceFullPaths(str) {
return str.replaceAll('\\\'', "'").replaceAll(path.resolve(__dirname, '../..'), '');
function transformProjectRoot(replacement = '') {
const projectRoot = path.resolve(__dirname, '../..');
return (str) => {
return str.replaceAll('\\\'', "'").replaceAll(projectRoot, replacement);
};
}

function transform(...args) {
Expand Down Expand Up @@ -94,11 +101,12 @@ async function spawnAndAssert(filename, transform = (x) => x, { tty = false, ...
module.exports = {
assertSnapshot,
getSnapshotPath,
replaceFullPaths,
replaceNodeVersion,
replaceStackTrace,
replaceInternalStackTrace,
replaceWindowsLineEndings,
replaceWindowsPaths,
spawnAndAssert,
transform,
transformProjectRoot,
};
20 changes: 10 additions & 10 deletions test/fixtures/source-map/output/source_map_disabled_by_api.snapshot
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Error: an error!
at functionD (*enclosing-call-site-min.js:1:156)
at functionC (*enclosing-call-site-min.js:1:97)
at functionB (*enclosing-call-site-min.js:1:60)
at functionA (*enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*enclosing-call-site-min.js:1:199)
at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156)
at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97)
at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60)
at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199)
Error: an error!
at functionD (*enclosing-call-site.js:16:17)
at functionC (*enclosing-call-site.js:10:3)
at functionB (*enclosing-call-site.js:6:3)
at functionA (*enclosing-call-site.js:2:3)
at Object.<anonymous> (*enclosing-call-site.js:24:3)
at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17)
at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3)
at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3)
at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site.js:24:3)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Error: an error!
at functionD (*enclosing-call-site-min.js:1:156)
at functionC (*enclosing-call-site-min.js:1:97)
at functionB (*enclosing-call-site-min.js:1:60)
at functionA (*enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*enclosing-call-site-min.js:1:199)
at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156)
at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97)
at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60)
at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199)
Error: an error!
at functionD (*enclosing-call-site.js:16:17)
at functionC (*enclosing-call-site.js:10:3)
at functionB (*enclosing-call-site.js:6:3)
at functionA (*enclosing-call-site.js:2:3)
at Object.<anonymous> (*enclosing-call-site.js:24:3)
at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17)
at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3)
at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3)
at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site.js:24:3)
20 changes: 10 additions & 10 deletions test/fixtures/source-map/output/source_map_enabled_by_api.snapshot
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Error: an error!
at functionD (*enclosing-call-site.js:16:17)
at functionC (*enclosing-call-site.js:10:3)
at functionB (*enclosing-call-site.js:6:3)
at functionA (*enclosing-call-site.js:2:3)
at Object.<anonymous> (*enclosing-call-site.js:24:3)
at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17)
at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3)
at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3)
at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site.js:24:3)
Error: an error!
at functionD (*enclosing-call-site-min.js:1:156)
at functionC (*enclosing-call-site-min.js:1:97)
at functionB (*enclosing-call-site-min.js:1:60)
at functionA (*enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*enclosing-call-site-min.js:1:199)
at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156)
at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97)
at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60)
at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Error: an error!
at functionD (*node_modules*error-stack*enclosing-call-site.js:16:17)
at functionC (*node_modules*error-stack*enclosing-call-site.js:10:3)
at functionB (*node_modules*error-stack*enclosing-call-site.js:6:3)
at functionA (*node_modules*error-stack*enclosing-call-site.js:2:3)
at Object.<anonymous> (*node_modules*error-stack*enclosing-call-site.js:24:3)
at functionD (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:16:17)
at functionC (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:10:3)
at functionB (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:6:3)
at functionA (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:2:3)
at Object.<anonymous> (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site.js:24:3)
Error: an error!
at functionD (*node_modules*error-stack*enclosing-call-site-min.js:1:156)
at functionC (*node_modules*error-stack*enclosing-call-site-min.js:1:97)
at functionB (*node_modules*error-stack*enclosing-call-site-min.js:1:60)
at functionA (*node_modules*error-stack*enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*node_modules*error-stack*enclosing-call-site-min.js:1:199)
at functionD (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:156)
at functionC (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:97)
at functionB (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:60)
at functionA (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:199)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Error: an error!
at functionD (*enclosing-call-site.js:16:17)
at functionC (*enclosing-call-site.js:10:3)
at functionB (*enclosing-call-site.js:6:3)
at functionA (*enclosing-call-site.js:2:3)
at Object.<anonymous> (*enclosing-call-site.js:24:3)
at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17)
at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3)
at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3)
at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site.js:24:3)
Error: an error!
at functionD (*enclosing-call-site-min.js:1:156)
at functionC (*enclosing-call-site-min.js:1:97)
at functionB (*enclosing-call-site-min.js:1:60)
at functionA (*enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*enclosing-call-site-min.js:1:199)
at functionD (*/test/fixtures/source-map/enclosing-call-site-min.js:1:156)
at functionC (*/test/fixtures/source-map/enclosing-call-site-min.js:1:97)
at functionB (*/test/fixtures/source-map/enclosing-call-site-min.js:1:60)
at functionA (*/test/fixtures/source-map/enclosing-call-site-min.js:1:26)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site-min.js:1:199)
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
*enclosing-call-site.js:26
*/test/fixtures/source-map/enclosing-call-site.js:26
throw err
^


Error: an error!
at functionD (*enclosing-call-site.js:16:17)
at functionC (*enclosing-call-site.js:10:3)
at functionB (*enclosing-call-site.js:6:3)
at functionA (*enclosing-call-site.js:2:3)
at Object.<anonymous> (*enclosing-call-site.js:24:3)
at functionD (*/test/fixtures/source-map/enclosing-call-site.js:16:17)
at functionC (*/test/fixtures/source-map/enclosing-call-site.js:10:3)
at functionB (*/test/fixtures/source-map/enclosing-call-site.js:6:3)
at functionA (*/test/fixtures/source-map/enclosing-call-site.js:2:3)
at Object.<anonymous> (*/test/fixtures/source-map/enclosing-call-site.js:24:3)

Node.js *
3 changes: 2 additions & 1 deletion test/fixtures/source-map/output/source_map_eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Error.stackTraceLimit = 3;

const fs = require('fs');

const content = fs.readFileSync(require.resolve('../tabs.js'), 'utf8');
const content = fs.readFileSync(require.resolve('../tabs-source-url.js'), 'utf8');
// SourceURL magic comment is hardcoded in the source content.
eval(content);
8 changes: 4 additions & 4 deletions test/fixtures/source-map/output/source_map_eval.snapshot
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
*tabs.coffee:26
*/synthesized/workspace/tabs-source-url.coffee:26
alert "I knew it!"
^


ReferenceError: alert is not defined
at Object.eval (*tabs.coffee:26:2)
at eval (*tabs.coffee:1:14)
at Object.<anonymous> (*output*source_map_eval.js:10:1)
at Object.eval (*/synthesized/workspace/tabs-source-url.coffee:26:2)
at eval (*/synthesized/workspace/tabs-source-url.coffee:1:14)
at Object.<anonymous> (*/test/fixtures/source-map/output/source_map_eval.js:11:1)

Node.js *
Loading
Loading