Skip to content

Commit

Permalink
feat(instrumentation): implement require-in-the-middle singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mhassan1 committed Aug 11, 2022
1 parent 038df3f commit d5e4bf2
Showing 1 changed file with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export abstract class InstrumentationBase<T = any>
extends InstrumentationAbstract
implements types.Instrumentation {
private _modules: InstrumentationModuleDefinition<T>[];
private _hooks: RequireInTheMiddle.Hooked[] = [];
private _hooked = false;
private _enabled = false;

constructor(
Expand Down Expand Up @@ -142,7 +142,7 @@ export abstract class InstrumentationBase<T = any>
this._enabled = true;

// already hooked, just call patch again
if (this._hooks.length > 0) {
if (this._hooked) {
for (const module of this._modules) {
if (typeof module.patch === 'function' && module.moduleExports) {
module.patch(module.moduleExports, module.moduleVersion);
Expand All @@ -158,22 +158,20 @@ export abstract class InstrumentationBase<T = any>

this._warnOnPreloadedModules();
for (const module of this._modules) {
this._hooks.push(
RequireInTheMiddle(
[module.name],
{ internals: true },
(exports, name, baseDir) => {
return this._onRequire<typeof exports>(
(module as unknown) as InstrumentationModuleDefinition<
typeof exports
requireInTheMiddleSingleton.register(
module.name,
(exports, name, baseDir) => {
return this._onRequire<typeof exports>(
(module as unknown) as InstrumentationModuleDefinition<
typeof exports
>,
exports,
name,
baseDir
);
}
)
exports,
name,
baseDir
);
}
);
this._hooked = true;
}
}

Expand Down Expand Up @@ -210,3 +208,41 @@ function isSupported(supportedVersions: string[], version?: string, includePrere
return satisfies(version, supportedVersion, { includePrerelease });
});
}

/**
* Singleton class for `require-in-the-middle`
* Allows instrumentation plugins to patch modules with only a single `require` patch
* TODO: Move this to a higher level to ensure it is a singleton across the Node.js process
*/
class RequireInTheMiddleSingleton {
private _modulesToHook: Array<{ moduleName: string, onRequire: RequireInTheMiddle.OnRequireFn }> = [];

constructor() {
this.initialize();
}

initialize() {
RequireInTheMiddle(
// Intercept all `require` calls; we will filter the matching ones below
null,
{ internals: true },
(exports, name, baseDir) => {
const matches = this._modulesToHook.filter(({ moduleName: hookedName }) => {
return name === hookedName || name.startsWith(hookedName + path.sep);
});

for (const { onRequire } of matches) {
exports = onRequire(exports, name, baseDir);
}

return exports;
}
);
}

register(moduleName: string, onRequire: RequireInTheMiddle.OnRequireFn) {
this._modulesToHook.push({ moduleName, onRequire });
}
}

const requireInTheMiddleSingleton = new RequireInTheMiddleSingleton();

0 comments on commit d5e4bf2

Please sign in to comment.