From d62e42ccc3089f4f1c30f2b0c4dfe814ef871867 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Tue, 20 Oct 2020 11:43:55 -0400 Subject: [PATCH] Filter out namespaced internal modules Fixes: https://github.com/tapjs/stack-utils/issues/54 Refs: https://github.com/nodejs/node/pull/35498 --- index.js | 6 +++--- test/internals.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test/internals.js diff --git a/index.js b/index.js index c0f6acf..48c7645 100644 --- a/index.js +++ b/index.js @@ -6,11 +6,11 @@ const natives = [].concat( require('module').builtinModules, 'bootstrap_node', 'node', -).map(n => new RegExp(`(?:\\(${n}\\.js:\\d+:\\d+\\)$|^\\s*at ${n}\\.js:\\d+:\\d+$)`)); +).map(n => new RegExp(`(?:\\((?:node:)?${n}(?:\\.js)?:\\d+:\\d+\\)$|^\\s*at (?:node:)?${n}(?:\\.js)?:\\d+:\\d+$)`)); natives.push( - /\(internal\/[^:]+:\d+:\d+\)$/, - /\s*at internal\/[^:]+:\d+:\d+$/, + /\((?:node:)?internal\/[^:]+:\d+:\d+\)$/, + /\s*at (?:node:)?internal\/[^:]+:\d+:\d+$/, /\/\.node-spawn-wrap-\w+-\w+\/node:\d+:\d+\)?$/ ); diff --git a/test/internals.js b/test/internals.js new file mode 100644 index 0000000..22f75c9 --- /dev/null +++ b/test/internals.js @@ -0,0 +1,42 @@ +'use strict'; + +const t = require('tap'); +const StackUtils = require('../'); + +const utils = require('./_utils'); + +const stackUtils = new StackUtils({ cwd: '/home/user' }); + +t.test('removes namespaced internal modules', t => { + const stack = utils.join([ + 'at Test. (test/test.js:99:5)', + 'at TapWrap.runInAsyncScope (node:async_hooks:193:9)', + 'at Object. (test/test.js:94:3)', + 'at Module._compile (node:internal/modules/cjs/loader:1083:30)', + 'at Module.replacementCompile (node_modules/append-transform/index.js:58:13)', + 'at Module._extensions..js (node:internal/modules/cjs/loader:1112:10)', + 'at Object. (node_modules/append-transform/index.js:62:4)', + 'at Module.load (node:internal/modules/cjs/loader:948:32)', + 'at Function.Module._load (node:internal/modules/cjs/loader:789:14)', + 'at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)', + 'at Module._compile (node:internal/modules/cjs/loader:1083:30)', + 'at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)', + 'at Module.load (node:internal/modules/cjs/loader:948:32)', + 'at Function.Module._load (node:internal/modules/cjs/loader:789:14)', + 'at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)', + 'at Module._compile (node:internal/modules/cjs/loader:1083:30)', + 'at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)', + 'at Module.load (node:internal/modules/cjs/loader:948:32)', + 'at Function.Module._load (node:internal/modules/cjs/loader:789:14)', + 'at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)', + 'at node:internal/main/run_main_module:17:47' + ]); + const expectedStack = utils.join([ + 'Test. (test/test.js:99:5)', + 'Object. (test/test.js:94:3)', + 'Module.replacementCompile (node_modules/append-transform/index.js:58:13)', + 'Object. (node_modules/append-transform/index.js:62:4)', + ]) + t.plan(1); + t.is(stackUtils.clean(stack), expectedStack); +});