Skip to content

Commit

Permalink
chore: use require.resolve to find already required modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Feb 1, 2020
1 parent 8063119 commit 34bfa5c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 0 additions & 27 deletions packages/opentelemetry-node/src/instrumentation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 34bfa5c

Please sign in to comment.