UNPKG

@kangc/skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

116 lines 4.97 kB
"use strict"; /*! * * 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 }); var tslib_1 = require("tslib"); var fs = tslib_1.__importStar(require("fs")); var path = tslib_1.__importStar(require("path")); var logging_1 = require("../logging"); var semver = tslib_1.__importStar(require("semver")); var AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig")); var logger = logging_1.createLogger(__filename); var topModule = module; while (topModule.parent) { var 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; } var PluginInstaller = /** @class */ (function () { function PluginInstaller() { var _this = this; this.require = topModule.require.bind(topModule); this.resolve = function (request) { return module.constructor._resolveFilename(request, topModule); }; this.isBuiltIn = function (module) { return _this.resolve(module) === module; }; this.checkModuleVersion = function (plugin) { var _a; try { if (_this.isBuiltIn(plugin.module)) { return { version: '*', isSupported: true, }; } } catch (_b) { // module not found return { version: 'not found,', isSupported: false, }; } var version = null; try { var 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: version, isSupported: false, }; } return { version: version, isSupported: true, }; }; this.isPluginEnabled = function (name) { return !name.match(AgentConfig_1.default.reDisablePlugins); }; this.pluginDir = path.resolve(__dirname, '..', 'plugins'); } PluginInstaller.prototype.install = function () { var _this = this; fs.readdirSync(this.pluginDir) .filter(function (file) { return !(file.endsWith('.d.ts') || file.endsWith('.js.map')); }) .forEach(function (file) { if (file.replace(/(?:Plugin)?\.js$/i, '').match(AgentConfig_1.default.reDisablePlugins)) { logger.info("Plugin " + file + " not installed because it is disabled"); return; } var plugin; var pluginFile = path.join(_this.pluginDir, file); try { plugin = _this.require(pluginFile).default; var _a = _this.checkModuleVersion(plugin), isSupported = _a.isSupported, version = _a.version; 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); } } }); }; return PluginInstaller; }()); exports.default = PluginInstaller; //# sourceMappingURL=PluginInstaller.js.map