Skip to content

Commit 17246be

Browse files
sapphi-redtargos
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 bbafd42 commit 17246be

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
@@ -3458,6 +3458,19 @@ throw an error.
34583458
Using this function is mutually exclusive with using the deprecated
34593459
[`domain`][] built-in module.
34603460
3461+
## `process.sourceMapsEnabled`
3462+
3463+
<!-- YAML
3464+
added: REPLACEME
3465+
-->
3466+
3467+
> Stability: 1 - Experimental
3468+
3469+
* {boolean}
3470+
3471+
The `process.sourceMapsEnabled` property returns whether the
3472+
[Source Map v3][Source Map] support for stack traces is enabled.
3473+
34613474
## `process.stderr`
34623475
34633476
* {Stream}

lib/internal/bootstrap/node.js

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

360360
{
361361
const {
362+
getSourceMapsEnabled,
362363
setSourceMapsEnabled,
363364
maybeCacheGeneratedSourceMap,
364365
} = require('internal/source_map/source_map_cache');
365366
const {
366367
setMaybeCacheGeneratedSourceMap,
367368
} = internalBinding('errors');
368369

370+
ObjectDefineProperty(process, 'sourceMapsEnabled', {
371+
__proto__: null,
372+
enumerable: true,
373+
configurable: true,
374+
get() {
375+
return getSourceMapsEnabled();
376+
},
377+
});
369378
process.setSourceMapsEnabled = setSourceMapsEnabled;
370379
// The C++ land calls back to maybeCacheGeneratedSourceMap()
371380
// 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)