skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
113 lines • 4.66 kB
JavaScript
;
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
const logging_1 = require("../logging");
const semver = tslib_1.__importStar(require("semver"));
const AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig"));
const logger = logging_1.createLogger(__filename);
let topModule = module;
while (topModule.parent) {
const filename = topModule.filename;
topModule = topModule.parent;
if (filename.endsWith('/skywalking-nodejs/lib/index.js'))
// stop at the appropriate level in case app is being run by some other framework
break;
}
class PluginInstaller {
constructor() {
this.require = topModule.require.bind(topModule);
this.resolve = (request) => module.constructor._resolveFilename(request, topModule);
this.isBuiltIn = (module) => this.resolve(module) === module;
this.checkModuleVersion = (plugin) => {
var _a;
try {
if (this.isBuiltIn(plugin.module)) {
return {
version: '*',
isSupported: true,
};
}
}
catch (_b) {
// module not found
return {
version: 'not found,',
isSupported: false,
};
}
let version = null;
try {
const packageJsonPath = this.resolve(`${plugin.module}/package.json`);
version = this.require(packageJsonPath).version;
}
catch (e) {
version = (_a = plugin.getVersion) === null || _a === void 0 ? void 0 : _a.call(plugin, this);
}
if (!semver.satisfies(version, plugin.versions)) {
logger.info(`Plugin ${plugin.module} ${version} doesn't satisfy the supported version ${plugin.versions}`);
return {
version,
isSupported: false,
};
}
return {
version,
isSupported: true,
};
};
this.isPluginEnabled = (name) => !name.match(AgentConfig_1.default.reDisablePlugins);
this.pluginDir = path.resolve(__dirname, '..', 'plugins');
}
install() {
fs.readdirSync(this.pluginDir)
.filter((file) => !(file.endsWith('.d.ts') || file.endsWith('.js.map')))
.forEach((file) => {
if (file.replace(/(?:Plugin)?\.js$/i, '').match(AgentConfig_1.default.reDisablePlugins)) {
logger.info(`Plugin ${file} not installed because it is disabled`);
return;
}
let plugin;
const pluginFile = path.join(this.pluginDir, file);
try {
plugin = this.require(pluginFile).default;
const { isSupported, version } = this.checkModuleVersion(plugin);
if (!isSupported) {
logger.info(`Plugin ${plugin.module} ${version} doesn't satisfy the supported version ${plugin.versions}`);
return;
}
logger.info(`Installing plugin ${plugin.module} ${plugin.versions}`);
plugin.install(this);
}
catch (e) {
if (plugin) {
logger.error(`Error installing plugin ${plugin.module} ${plugin.versions}`);
}
else {
logger.error(`Error processing plugin ${pluginFile}`);
}
}
});
}
}
exports.default = PluginInstaller;
//# sourceMappingURL=PluginInstaller.js.map