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.sourceMapsEnabled` to detect
whether source-maps are enabled.

Fixes: #46304
  • Loading branch information
sapphi-red committed Aug 15, 2023
1 parent 115f801 commit 92c088d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -3517,6 +3517,19 @@ throw an error.
Using this function is mutually exclusive with using the deprecated
[`domain`][] built-in module.
## `process.sourceMapsEnabled`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* {boolean}
The `process.sourceMapsEnabled` property returns whether the
[Source Map v3][Source Map] support for stack traces is enabled.
## `process.stderr`
* {Stream}
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,22 @@ process.emitWarning = emitWarning;

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

ObjectDefineProperty(process, 'sourceMapsEnabled', {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getSourceMapsEnabled();
},
});
process.setSourceMapsEnabled = setSourceMapsEnabled;
// The C++ land calls back to maybeCacheGeneratedSourceMap()
// when code is generated by user with eval() or new Function()
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/source-map/output/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.sourceMapsEnabled, true);
process.setSourceMapsEnabled(false);
assert.strictEqual(process.sourceMapsEnabled, false);

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

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

try {
require('../enclosing-call-site-min.js');
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/source-map/output/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.sourceMapsEnabled, false);
process.setSourceMapsEnabled(true);
assert.strictEqual(process.sourceMapsEnabled, true);

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

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

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

0 comments on commit 92c088d

Please sign in to comment.