Skip to content
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

bootstrap: simplify initialization of source map handlers #48304

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 16 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,22 @@ process.emitWarning = emitWarning;
// Note: only after this point are the timers effective
}

{
const {
setSourceMapsEnabled,
maybeCacheGeneratedSourceMap,
} = require('internal/source_map/source_map_cache');
const {
setMaybeCacheGeneratedSourceMap,
} = internalBinding('errors');

process.setSourceMapsEnabled = setSourceMapsEnabled;
// The C++ land calls back to maybeCacheGeneratedSourceMap()
// when code is generated by user with eval() or new Function()
// to cache the source maps from the evaluated code, if any.
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);
}

function setupProcessObject() {
const EventEmitter = require('events');
const origProcProto = ObjectGetPrototypeOf(process);
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ if (internalBinding('config').hasInspector) {
internalBinding('wasm_web_api');
// Needed to detect whether it's on main thread.
internalBinding('worker');
// Needed to setup source maps.
require('internal/source_map/source_map_cache');
// Needed by most execution modes.
require('internal/modules/run_main');
// Needed to refresh DNS configurations.
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,10 @@ function initializeESMLoader(isLoaderWorker) {
}

function initializeSourceMapsHandlers() {
const { setSourceMapsEnabled, getSourceMapsEnabled } =
require('internal/source_map/source_map_cache');
process.setSourceMapsEnabled = setSourceMapsEnabled;
// Initialize the environment flag of source maps.
getSourceMapsEnabled();
const {
setSourceMapsEnabled,
} = require('internal/source_map/source_map_cache');
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
}

function initializeFrozenIntrinsics() {
Expand Down
20 changes: 8 additions & 12 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ const { Buffer } = require('buffer');
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
debug = fn;
});
const { getOptionValue } = require('internal/options');

const { validateBoolean } = require('internal/validators');
const { setMaybeCacheGeneratedSourceMap } = internalBinding('errors');
const {
setSourceMapsEnabled: setSourceMapsNative,
setPrepareStackTraceCallback,
} = internalBinding('errors');
const { getLazy } = require('internal/util');

// Since the CJS module cache is mutable, which leads to memory leaks when
Expand All @@ -41,22 +43,16 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url');

let SourceMap;

let sourceMapsEnabled;
// This is configured with --enable-source-maps during pre-execution.
let sourceMapsEnabled = false;
function getSourceMapsEnabled() {
if (sourceMapsEnabled === undefined) {
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
}
return sourceMapsEnabled;
}

function setSourceMapsEnabled(val) {
validateBoolean(val, 'val');

const {
setSourceMapsEnabled,
setPrepareStackTraceCallback,
} = internalBinding('errors');
setSourceMapsEnabled(val);
setSourceMapsNative(val);
if (val) {
const {
prepareStackTrace,
Expand Down Expand Up @@ -186,7 +182,6 @@ function maybeCacheGeneratedSourceMap(content) {
debug(err);
}
}
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);

function dataFromUrl(sourceURL, sourceMappingURL) {
try {
Expand Down Expand Up @@ -333,5 +328,6 @@ module.exports = {
getSourceMapsEnabled,
setSourceMapsEnabled,
maybeCacheSourceMap,
maybeCacheGeneratedSourceMap,
sourceMapCacheToObject,
};