in src/diagnostic-channel/src/patchRequire.ts [23:71]
export function makePatchingRequire(knownPatches: IModulePatchMap) {
const patchedModules: {[path: string]: any} = {};
return function patchedRequire(moduleId: string): any {
const originalModule = originalRequire.apply(this, arguments);
if (knownPatches[moduleId]) {
// Fetch the specific path of the module
const modulePath = moduleModule._resolveFilename(moduleId, this);
if (patchedModules.hasOwnProperty(modulePath)) {
// This module has already been patched, no need to reapply
return patchedModules[modulePath];
}
let moduleVersion: string;
if (nativeModules.indexOf(moduleId) < 0) {
try {
moduleVersion = originalRequire.call(this, path.join(moduleId, "package.json")).version;
} catch (e) {
// This should only happen if moduleId is actually a path rather than a module
// This is not a supported scenario
return originalModule;
}
} else {
// This module is implemented natively so we cannot find a package.json
// Instead, take the version of node itself
moduleVersion = process.version.substring(1);
}
const prereleaseTagIndex = moduleVersion.indexOf("-");
if (prereleaseTagIndex >= 0) {
// We ignore prerelease tags to avoid impossible to fix gaps in support
// e.g. supporting console in >= 4.0.0 would otherwise not include
// 8.0.0-pre
moduleVersion = moduleVersion.substring(0, prereleaseTagIndex);
}
let modifiedModule = originalModule;
for (const modulePatcher of knownPatches[moduleId]) {
if (semver.satisfies(moduleVersion, modulePatcher.versionSpecifier)) {
modifiedModule = modulePatcher.patch(modifiedModule, modulePath);
}
}
return patchedModules[modulePath] = modifiedModule;
}
return originalModule;
};
}