Skip to content

Commit 11c85ff

Browse files
sapphi-redUlisesGascon
authored andcommitted
lib: add api to detect whether source-maps are enabled
Add `process.sourceMapsEnabled` to detect whether source-maps are enabled. Fixes: #46304 PR-URL: #46391 Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 6ee74be commit 11c85ff

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

doc/api/process.md

+13
Original file line numberDiff line numberDiff line change
@@ -3517,6 +3517,19 @@ throw an error.
35173517
Using this function is mutually exclusive with using the deprecated
35183518
[`domain`][] built-in module.
35193519
3520+
## `process.sourceMapsEnabled`
3521+
3522+
<!-- YAML
3523+
added: REPLACEME
3524+
-->
3525+
3526+
> Stability: 1 - Experimental
3527+
3528+
* {boolean}
3529+
3530+
The `process.sourceMapsEnabled` property returns whether the
3531+
[Source Map v3][Source Map] support for stack traces is enabled.
3532+
35203533
## `process.stderr`
35213534
35223535
* {Stream}

lib/internal/bootstrap/node.js

+9
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,22 @@ process.emitWarning = emitWarning;
326326

327327
{
328328
const {
329+
getSourceMapsEnabled,
329330
setSourceMapsEnabled,
330331
maybeCacheGeneratedSourceMap,
331332
} = require('internal/source_map/source_map_cache');
332333
const {
333334
setMaybeCacheGeneratedSourceMap,
334335
} = internalBinding('errors');
335336

337+
ObjectDefineProperty(process, 'sourceMapsEnabled', {
338+
__proto__: null,
339+
enumerable: true,
340+
configurable: true,
341+
get() {
342+
return getSourceMapsEnabled();
343+
},
344+
});
336345
process.setSourceMapsEnabled = setSourceMapsEnabled;
337346
// The C++ land calls back to maybeCacheGeneratedSourceMap()
338347
// when code is generated by user with eval() or new Function()

test/fixtures/source-map/output/source_map_disabled_by_api.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
'use strict';
44
require('../../../common');
5+
const assert = require('assert');
56
Error.stackTraceLimit = 5;
67

8+
assert.strictEqual(process.sourceMapsEnabled, true);
79
process.setSourceMapsEnabled(false);
10+
assert.strictEqual(process.sourceMapsEnabled, false);
811

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

1821
// Re-enable.
1922
process.setSourceMapsEnabled(true);
23+
assert.strictEqual(process.sourceMapsEnabled, true);
2024

2125
try {
2226
require('../enclosing-call-site-min.js');

test/fixtures/source-map/output/source_map_enabled_by_api.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22
require('../../../common');
3+
const assert = require('assert');
34
Error.stackTraceLimit = 5;
45

6+
assert.strictEqual(process.sourceMapsEnabled, false);
57
process.setSourceMapsEnabled(true);
8+
assert.strictEqual(process.sourceMapsEnabled, true);
69

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

1619
process.setSourceMapsEnabled(false);
20+
assert.strictEqual(process.sourceMapsEnabled, false);
1721

1822
try {
1923
require('../enclosing-call-site-min.js');

0 commit comments

Comments
 (0)