Skip to content

Commit

Permalink
fix(instrumentation): support multiple module definitions with differ…
Browse files Browse the repository at this point in the history
…ent versions (#2120)

Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
seemk and vmarchaud authored Apr 20, 2021
1 parent 27f64d9 commit c516264
Showing 1 changed file with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ export abstract class InstrumentationBase<T = any>
}
}

private _isSupported(name: string, version: string): boolean {
for (const module of this._modules) {
if (module.name === name) {
if (!module.supportedVersions) {
return true;
}

return module.supportedVersions.some(supportedVersion => {
return semver.satisfies(version, supportedVersion);
});
}
}

return false;
}

private _onRequire<T>(
module: InstrumentationModuleDefinition<T>,
exports: T,
Expand All @@ -93,7 +77,10 @@ export abstract class InstrumentationBase<T = any>
module.moduleVersion = version;
if (module.name === name) {
// main module
if (typeof version === 'string' && this._isSupported(name, version)) {
if (
typeof version === 'string' &&
isSupported(module.supportedVersions, version)
) {
if (typeof module.patch === 'function') {
module.moduleExports = exports;
if (this._enabled) {
Expand All @@ -105,12 +92,7 @@ export abstract class InstrumentationBase<T = any>
// internal file
const files = module.files ?? [];
const file = files.find(file => file.name === name);
if (
file &&
file.supportedVersions.some(supportedVersion =>
semver.satisfies(version, supportedVersion)
)
) {
if (file && isSupported(file.supportedVersions, version)) {
file.moduleExports = exports;
if (this._enabled) {
return file.patch(exports, module.moduleVersion);
Expand Down Expand Up @@ -179,3 +161,9 @@ export abstract class InstrumentationBase<T = any>
}
}
}

function isSupported(supportedVersions: string[], version: string): boolean {
return supportedVersions.some(supportedVersion => {
return semver.satisfies(version, supportedVersion);
});
}

0 comments on commit c516264

Please sign in to comment.