GeezerVison is showing github.com — process: add api to enable source-maps programmatically · fredericp/node@82b1b55
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/process.md +19Lines changed: 19 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3249,6 +3249,24 @@ This function is only available on POSIX platforms (i.e. not Windows or | 3249 | 3249 | Android). | 3250 | 3250 | This feature is not available in [`Worker`][] threads. | 3251 | 3251 |
| | 3252 | +## `process.setSourceMapsEnabled(val)` | | 3253 | +<!-- YAML | | 3254 | +added: REPLACEME | | 3255 | +--> | | 3256 | +
| | 3257 | +> Stability: 1 - Experimental | | 3258 | +
| | 3259 | +* `val` {boolean} | | 3260 | +
| | 3261 | +This function enables or disables the [Source Map v3][Source Map] support for | | 3262 | +stack traces. | | 3263 | +
| | 3264 | +It provides same features as launching Node.js process with commandline options | | 3265 | +`--enable-source-maps`. | | 3266 | +
| | 3267 | +Only source maps in JavaScript files that are loaded after source maps has been | | 3268 | +enabled will be parsed and loaded. | | 3269 | +
| 3252 | 3270 | ## `process.setUncaughtExceptionCaptureCallback(fn)` | 3253 | 3271 | <!-- YAML | 3254 | 3272 | added: v9.3.0 | @@ -3670,6 +3688,7 @@ cases: | 3670 | 3688 | [LTS]: https://github.com/nodejs/Release | 3671 | 3689 | [Readable]: stream.md#stream_readable_streams | 3672 | 3690 | [Signal Events]: #process_signal_events | | 3691 | +[Source Map]: https://sourcemaps.info/spec.html | 3673 | 3692 | [Stream compatibility]: stream.md#stream_compatibility_with_older_node_js_versions | 3674 | 3693 | [TTY]: tty.md#tty_tty | 3675 | 3694 | [Writable]: stream.md#stream_writable_streams | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,7 @@ function prepareMainThreadExecution(expandArgv1 = false) { | 66 | 66 | // (including preload modules). | 67 | 67 | initializeClusterIPC(); | 68 | 68 |
| | 69 | + initializeSourceMapsHandlers(); | 69 | 70 | initializeDeprecations(); | 70 | 71 | initializeWASI(); | 71 | 72 | initializeCJSLoader(); | @@ -454,6 +455,12 @@ function initializeESMLoader() { | 454 | 455 | } | 455 | 456 | } | 456 | 457 |
| | 458 | +function initializeSourceMapsHandlers() { | | 459 | + const { setSourceMapsEnabled } = | | 460 | + require('internal/source_map/source_map_cache'); | | 461 | + process.setSourceMapsEnabled = setSourceMapsEnabled; | | 462 | +} | | 463 | + | 457 | 464 | function initializeFrozenIntrinsics() { | 458 | 465 | if (getOptionValue('--frozen-intrinsics')) { | 459 | 466 | process.emitWarning('The --frozen-intrinsics flag is experimental', | @@ -485,6 +492,7 @@ module.exports = { | 485 | 492 | initializeDeprecations, | 486 | 493 | initializeESMLoader, | 487 | 494 | initializeFrozenIntrinsics, | | 495 | + initializeSourceMapsHandlers, | 488 | 496 | loadPreloadModules, | 489 | 497 | setupTraceCategoryState, | 490 | 498 | setupInspectorHooks, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/worker_thread.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,7 @@ const { | 25 | 25 | initializeESMLoader, | 26 | 26 | initializeFrozenIntrinsics, | 27 | 27 | initializeReport, | | 28 | + initializeSourceMapsHandlers, | 28 | 29 | loadPreloadModules, | 29 | 30 | setupTraceCategoryState | 30 | 31 | } = require('internal/bootstrap/pre_execution'); | @@ -66,6 +67,7 @@ setupInspectorHooks(); | 66 | 67 | setupDebugEnv(); | 67 | 68 |
| 68 | 69 | setupWarningHandler(); | | 70 | +initializeSourceMapsHandlers(); | 69 | 71 |
| 70 | 72 | // Since worker threads cannot switch cwd, we do not need to | 71 | 73 | // overwrite the process.env.NODE_V8_COVERAGE variable. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/source_map/source_map_cache.js +27-12Lines changed: 27 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map'); | 29 | 29 | const { | 30 | 30 | normalizeReferrerURL, | 31 | 31 | } = require('internal/modules/cjs/helpers'); | | 32 | +const { validateBoolean } = require('internal/validators'); | 32 | 33 | // Since the CJS module cache is mutable, which leads to memory leaks when | 33 | 34 | // modules are deleted, we use a WeakMap so that the source map cache will | 34 | 35 | // be purged automatically: | @@ -41,22 +42,35 @@ let SourceMap; | 41 | 42 | let sourceMapsEnabled; | 42 | 43 | function getSourceMapsEnabled() { | 43 | 44 | if (sourceMapsEnabled === undefined) { | 44 | | - sourceMapsEnabled = getOptionValue('--enable-source-maps'); | 45 | | - if (sourceMapsEnabled) { | 46 | | - const { | 47 | | - enableSourceMaps, | 48 | | - setPrepareStackTraceCallback | 49 | | - } = internalBinding('errors'); | 50 | | - const { | 51 | | - prepareStackTrace | 52 | | - } = require('internal/source_map/prepare_stack_trace'); | 53 | | - setPrepareStackTraceCallback(prepareStackTrace); | 54 | | - enableSourceMaps(); | 55 | | - } | | 45 | + setSourceMapsEnabled(getOptionValue('--enable-source-maps')); | 56 | 46 | } | 57 | 47 | return sourceMapsEnabled; | 58 | 48 | } | 59 | 49 |
| | 50 | +function setSourceMapsEnabled(val) { | | 51 | + validateBoolean(val, 'val'); | | 52 | + | | 53 | + const { | | 54 | + setSourceMapsEnabled, | | 55 | + setPrepareStackTraceCallback | | 56 | + } = internalBinding('errors'); | | 57 | + setSourceMapsEnabled(val); | | 58 | + if (val) { | | 59 | + const { | | 60 | + prepareStackTrace | | 61 | + } = require('internal/source_map/prepare_stack_trace'); | | 62 | + setPrepareStackTraceCallback(prepareStackTrace); | | 63 | + } else if (sourceMapsEnabled !== undefined) { | | 64 | + // Reset prepare stack trace callback only when disabling source maps. | | 65 | + const { | | 66 | + prepareStackTrace, | | 67 | + } = require('internal/errors'); | | 68 | + setPrepareStackTraceCallback(prepareStackTrace); | | 69 | + } | | 70 | + | | 71 | + sourceMapsEnabled = val; | | 72 | +} | | 73 | + | 60 | 74 | function maybeCacheSourceMap(filename, content, cjsModuleInstance) { | 61 | 75 | const sourceMapsEnabled = getSourceMapsEnabled(); | 62 | 76 | if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return; | @@ -231,6 +245,7 @@ function findSourceMap(sourceURL) { | 231 | 245 | module.exports = { | 232 | 246 | findSourceMap, | 233 | 247 | getSourceMapsEnabled, | | 248 | + setSourceMapsEnabled, | 234 | 249 | maybeCacheSourceMap, | 235 | 250 | sourceMapCacheToObject, | 236 | 251 | }; |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.cc +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -820,9 +820,10 @@ void SetPrepareStackTraceCallback(const FunctionCallbackInfo<Value>& args) { | 820 | 820 | env->set_prepare_stack_trace_callback(args[0].As<Function>()); | 821 | 821 | } | 822 | 822 |
| 823 | | -static void EnableSourceMaps(const FunctionCallbackInfo<Value>& args) { | | 823 | +static void SetSourceMapsEnabled(const FunctionCallbackInfo<Value>& args) { | 824 | 824 | Environment* env = Environment::GetCurrent(args); | 825 | | - env->set_source_maps_enabled(true); | | 825 | + CHECK(args[0]->IsBoolean()); | | 826 | + env->set_source_maps_enabled(args[0].As<Boolean>()->Value()); | 826 | 827 | } | 827 | 828 |
| 828 | 829 | static void SetEnhanceStackForFatalException( | @@ -858,7 +859,7 @@ static void TriggerUncaughtException(const FunctionCallbackInfo<Value>& args) { | 858 | 859 |
| 859 | 860 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | 860 | 861 | registry->Register(SetPrepareStackTraceCallback); | 861 | | - registry->Register(EnableSourceMaps); | | 862 | + registry->Register(SetSourceMapsEnabled); | 862 | 863 | registry->Register(SetEnhanceStackForFatalException); | 863 | 864 | registry->Register(NoSideEffectsToString); | 864 | 865 | registry->Register(TriggerUncaughtException); | @@ -871,7 +872,7 @@ void Initialize(Local<Object> target, | 871 | 872 | Environment* env = Environment::GetCurrent(context); | 872 | 873 | env->SetMethod( | 873 | 874 | target, "setPrepareStackTraceCallback", SetPrepareStackTraceCallback); | 874 | | - env->SetMethod(target, "enableSourceMaps", EnableSourceMaps); | | 875 | + env->SetMethod(target, "setSourceMapsEnabled", SetSourceMapsEnabled); | 875 | 876 | env->SetMethod(target, | 876 | 877 | "setEnhanceStackForFatalException", | 877 | 878 | SetEnhanceStackForFatalException); | |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --enable-source-maps | | 2 | + | | 3 | +'use strict'; | | 4 | +require('../common'); | | 5 | + | | 6 | +process.setSourceMapsEnabled(false); | | 7 | + | | 8 | +try { | | 9 | + require('../fixtures/source-map/enclosing-call-site-min.js'); | | 10 | +} catch (e) { | | 11 | + console.log(e); | | 12 | +} | | 13 | + | | 14 | +delete require.cache[require | | 15 | + .resolve('../fixtures/source-map/enclosing-call-site-min.js')]; | | 16 | + | | 17 | +// Re-enable. | | 18 | +process.setSourceMapsEnabled(true); | | 19 | + | | 20 | +require('../fixtures/source-map/enclosing-call-site-min.js'); |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Error: an error! | | 2 | + at functionD (*enclosing-call-site-min.js:1:156) | | 3 | + at functionC (*enclosing-call-site-min.js:1:97) | | 4 | + at functionB (*enclosing-call-site-min.js:1:60) | | 5 | + at functionA (*enclosing-call-site-min.js:1:26) | | 6 | + at Object.<anonymous> (*enclosing-call-site-min.js:1:199) | | 7 | + at Module._compile (node:internal/modules/cjs/loader:*) | | 8 | + at Object.Module._extensions..js (node:internal/modules/cjs/loader:*) | | 9 | + at Module.load (node:internal/modules/cjs/loader:*) | | 10 | + at Function.Module._load (node:internal/modules/cjs/loader:*) | | 11 | + at Module.require (node:internal/modules/cjs/loader:*) | | 12 | +*enclosing-call-site.js:16 | | 13 | + throw new Error('an error!') | | 14 | + ^ | | 15 | + | | 16 | +Error: an error! | | 17 | + at functionD (*enclosing-call-site.js:16:17) | | 18 | + at functionC (*enclosing-call-site.js:10:3) | | 19 | + at functionB (*enclosing-call-site.js:6:3) | | 20 | + at functionA (*enclosing-call-site.js:2:3) | | 21 | + at Object.<anonymous> (*enclosing-call-site.js:24:3) | | 22 | + at Module._compile (node:internal/modules/cjs/loader:*) | | 23 | + at Object.Module._extensions..js (node:internal/modules/cjs/loader:*) | | 24 | + at Module.load (node:internal/modules/cjs/loader:*) | | 25 | + at Function.Module._load (node:internal/modules/cjs/loader:*) | | 26 | + at Module.require (node:internal/modules/cjs/loader:*) |
Collapse file Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | + | | 4 | +process.setSourceMapsEnabled(true); | | 5 | + | | 6 | +try { | | 7 | + require('../fixtures/source-map/enclosing-call-site-min.js'); | | 8 | +} catch (e) { | | 9 | + console.log(e); | | 10 | +} | | 11 | + | | 12 | +delete require.cache[require | | 13 | + .resolve('../fixtures/source-map/enclosing-call-site-min.js')]; | | 14 | + | | 15 | +process.setSourceMapsEnabled(false); | | 16 | + | | 17 | +require('../fixtures/source-map/enclosing-call-site-min.js'); |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +*enclosing-call-site.js:16 | | 2 | + throw new Error('an error!') | | 3 | + ^ | | 4 | + | | 5 | +Error: an error! | | 6 | + at functionD (*enclosing-call-site.js:16:17) | | 7 | + at functionC (*enclosing-call-site.js:10:3) | | 8 | + at functionB (*enclosing-call-site.js:6:3) | | 9 | + at functionA (*enclosing-call-site.js:2:3) | | 10 | + at Object.<anonymous> (*enclosing-call-site.js:24:3) | | 11 | + at Module._compile (node:internal/modules/cjs/loader:*) | | 12 | + at Object.Module._extensions..js (node:internal/modules/cjs/loader:*) | | 13 | + at Module.load (node:internal/modules/cjs/loader:*) | | 14 | + at Function.Module._load (node:internal/modules/cjs/loader:*) | | 15 | + at Module.require (node:internal/modules/cjs/loader:*) | | 16 | +*enclosing-call-site-min.js:1 | | 17 | +var functionA=function(){functionB()};function functionB(){functionC()}var functionC=function(){functionD()},functionD=function(){if(0<Math.random())throw Error("an error!");},thrower=functionA;try{functionA()}catch(a){throw a;}; | | 18 | + ^ | | 19 | + | | 20 | +Error: an error! | | 21 | + at functionD (*enclosing-call-site-min.js:1:156) | | 22 | + at functionC (*enclosing-call-site-min.js:1:97) | | 23 | + at functionB (*enclosing-call-site-min.js:1:60) | | 24 | + at functionA (*enclosing-call-site-min.js:1:26) | | 25 | + at Object.<anonymous> (*enclosing-call-site-min.js:1:199) | | 26 | + at Module._compile (node:internal/modules/cjs/loader:*) | | 27 | + at Object.Module._extensions..js (node:internal/modules/cjs/loader:*) | | 28 | + at Module.load (node:internal/modules/cjs/loader:*) | | 29 | + at Function.Module._load (node:internal/modules/cjs/loader:*) | | 30 | + at Module.require (node:internal/modules/cjs/loader:*) |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | +const assert = require('assert'); | | 4 | + | | 5 | +const unexpectedValues = [ | | 6 | + undefined, | | 7 | + null, | | 8 | + 1, | | 9 | + {}, | | 10 | + () => {}, | | 11 | +]; | | 12 | +for (const it of unexpectedValues) { | | 13 | + assert.throws(() => { | | 14 | + process.setSourceMapsEnabled(it); | | 15 | + }, /ERR_INVALID_ARG_TYPE/); | | 16 | +} |
|