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

lib: add api to detect whether source-maps are enabled #46391

Merged
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
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
anonrig marked this conversation as resolved.
Show resolved Hide resolved

* {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