From 34bfa5cfd9c9286ff2337c18b1d518843d49bbd1 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sat, 1 Feb 2020 11:33:00 +0100 Subject: [PATCH] chore: use require.resolve to find already required modules --- .../src/instrumentation/PluginLoader.ts | 2 +- .../src/instrumentation/utils.ts | 27 ------------------- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts index 3efd4e78042..0a637f93651 100644 --- a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts +++ b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts @@ -87,7 +87,7 @@ export class PluginLoader { const requiredModulesToHook = modulesToHook.filter( name => alreadyRequiredModules.find( - cached => utils.packageNameFromPath(cached) === name + cached => require.resolve(name) === cached ) !== undefined ); if (requiredModulesToHook.length > 0) { diff --git a/packages/opentelemetry-node/src/instrumentation/utils.ts b/packages/opentelemetry-node/src/instrumentation/utils.ts index 33a56391f4b..062682e3fb7 100644 --- a/packages/opentelemetry-node/src/instrumentation/utils.ts +++ b/packages/opentelemetry-node/src/instrumentation/utils.ts @@ -72,30 +72,3 @@ export function isSupportedVersion( export function searchPathForTest(searchPath: string) { module.paths.push(searchPath); } - -// Includes support for npm '@org/name' packages -// Regex: .*?node_modules(?!.*node_modules)\/(@[^\/]*\/[^\/]*|[^\/]*).* -// Tests: https://regex101.com/r/lW2bE3/6 -const moduleRegex = new RegExp( - [ - '.*?node_modules(?!.*node_modules)\\', - '(@[^\\', - ']*\\', - '[^\\', - ']*|[^\\', - ']*).*', - ].join(path.sep) -); - -/** - * Retrieves a package name from the full import path. - * For example: - * './node_modules/bar/index/foo.js' => 'bar' - * - * @param path The full import path. - * Extracted from https://github.com/googleapis/cloud-trace-nodejs/blob/master/src/util.ts#L214 - */ -export function packageNameFromPath(importPath: string) { - const matches = moduleRegex.exec(importPath); - return matches && matches.length > 1 ? matches[1] : null; -}