Skip to content

Commit

Permalink
lib: add api to detect whether source-maps are enabled
Browse files Browse the repository at this point in the history
Add `process.getSourceMapsEnabled` to detect
whether source-maps are enabled.

Fixes: nodejs#46304
  • Loading branch information
sapphi-red committed Jan 28, 2023
1 parent 47cd966 commit 7d1f4f8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
13 changes: 13 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,19 @@ if (process.getgroups) {
This function is only available on POSIX platforms (i.e. not Windows or
Android).
## `process.getSourceMapsEnabled()`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* Returns: {boolean}
The `process.getSourceMapsEnabled()` method returns whether the
[Source Map v3][Source Map] support for stack traces is enabled.
## `process.getuid()`
<!-- YAML
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,11 @@ function initializeESMLoader() {
}

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

function initializeFrozenIntrinsics() {
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
let SourceMap;

let sourceMapsEnabled;

function initializeSourceMapsEnabled() {
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
}

function getSourceMapsEnabled() {
if (sourceMapsEnabled === undefined) {
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
}
return sourceMapsEnabled;
}

Expand Down Expand Up @@ -340,6 +342,7 @@ function findSourceMap(sourceURL) {

module.exports = {
findSourceMap,
initializeSourceMapsEnabled,
getSourceMapsEnabled,
setSourceMapsEnabled,
maybeCacheSourceMap,
Expand Down
4 changes: 4 additions & 0 deletions test/message/source_map_disabled_by_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

'use strict';
require('../common');
const assert = require('assert');
Error.stackTraceLimit = 5;

assert.strictEqual(process.getSourceMapsEnabled(), true);
process.setSourceMapsEnabled(false);
assert.strictEqual(process.getSourceMapsEnabled(), false);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
Expand All @@ -17,6 +20,7 @@ delete require.cache[require

// Re-enable.
process.setSourceMapsEnabled(true);
assert.strictEqual(process.getSourceMapsEnabled(), true);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
Expand Down
4 changes: 4 additions & 0 deletions test/message/source_map_enabled_by_api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';
require('../common');
const assert = require('assert');
Error.stackTraceLimit = 5;

assert.strictEqual(process.getSourceMapsEnabled(), false);
process.setSourceMapsEnabled(true);
assert.strictEqual(process.getSourceMapsEnabled(), true);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
Expand All @@ -14,6 +17,7 @@ delete require.cache[require
.resolve('../fixtures/source-map/enclosing-call-site-min.js')];

process.setSourceMapsEnabled(false);
assert.strictEqual(process.getSourceMapsEnabled(), false);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
Expand Down

0 comments on commit 7d1f4f8

Please sign in to comment.