skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
251 lines • 10.6 kB
JavaScript
"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-backend-js/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;
var _a;
// if we are running bundled then topModule.require and module.constructor._resolveFilename are undefined (in webpack at least)
this.require = (_a = topModule.require) === null || _a === void 0 ? void 0 : _a.bind(topModule);
this.resolve = function (request) { return module.constructor._resolveFilename(request, topModule); };
this.checkModuleVersion = function (plugin) {
var _a;
try {
if (plugin.isBuiltIn) {
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)) {
return {
version: version || 'not found,',
isSupported: false,
};
}
return {
version: version,
isSupported: true,
};
};
this.isPluginEnabled = function (name) { return !name.match(AgentConfig_1.default.reDisablePlugins); };
this.checkBundledModuleVersion = function (plugin, version) {
try {
if (plugin.versions === '!' || plugin.isBuiltIn || version === '*') {
return {
version: '*',
isSupported: true,
};
}
}
catch (_a) {
// module not found
return {
version: 'not found,',
isSupported: false,
};
}
if (!semver.satisfies(version, plugin.versions)) {
return {
version: version,
isSupported: false,
};
}
return {
version: version,
isSupported: true,
};
};
this.installBundledPlugin = function (pluginFile, plugin, packageVersion) {
if (pluginFile.match(AgentConfig_1.default.reDisablePlugins)) {
logger.info("Plugin " + pluginFile + " not installed because it is disabled");
return;
}
try {
var _a = _this.checkBundledModuleVersion(plugin, packageVersion), isSupported = _a.isSupported, version = _a.version;
if (!isSupported) {
logger.info("Plugin " + plugin.module + " " + version + " doesn't satisfy the supported version " + plugin.versions);
return;
}
if (plugin.versions === '!') {
logger.info("Explicit instrumentation plugin " + plugin.module + " available");
}
else {
logger.info("Installing plugin " + plugin.module + " " + plugin.versions);
}
plugin.install(_this);
}
catch (e) {
console.error(e);
logger.error("Error installing plugin " + plugin.module + " " + plugin.versions);
}
};
this.pluginDir = path.resolve(__dirname, '..', 'plugins');
}
PluginInstaller.prototype.installNormal = 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);
}
}
});
};
PluginInstaller.prototype.installBundled = function () {
// XXX: Initial support for running in a bundle, not ideal and doesn't support some plugins but at least it works.
// Webpack does not support dynamic `require(var)`, all imports must be of static form `require('module')`.
try {
this.installBundledPlugin('AMQPLibPlugin', require('../plugins/AMQPLibPlugin').default, require('amqplib/package.json').version);
}
catch (_a) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('AWS2DynamoDBPlugin', require('../plugins/AWS2DynamoDBPlugin').default, require('aws-sdk/package.json').version);
}
catch (_b) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('AWS2LambdaPlugin', require('../plugins/AWS2LambdaPlugin').default, require('aws-sdk/package.json').version);
}
catch (_c) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('AWS2SNSPlugin', require('../plugins/AWS2SNSPlugin').default, require('aws-sdk/package.json').version);
}
catch (_d) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('AWS2SQSPlugin', require('../plugins/AWS2SQSPlugin').default, require('aws-sdk/package.json').version);
}
catch (_e) {
// ESLINT SUCKS!
}
// this.installBundledPlugin('AxiosPlugin', require('../plugins/AxiosPlugin').default, require('axios/package.json').version); // this package in all its wisdom disallows import of its package.json where the version number lives
try {
this.installBundledPlugin('ExpressPlugin', require('../plugins/ExpressPlugin').default, require('express/package.json').version);
}
catch (_f) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('HttpPlugin', require('../plugins/HttpPlugin').default, '*');
}
catch (_g) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('IORedisPlugin', require('../plugins/IORedisPlugin').default, require('ioredis/package.json').version);
}
catch (_h) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('MongoDBPlugin', require('../plugins/MongoDBPlugin').default, require('mongodb/package.json').version);
}
catch (_j) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('MongoosePlugin', require('../plugins/MongoosePlugin').default, require('mongoose/package.json').version);
}
catch (_k) {
// ESLINT SUCKS!
}
// this.installBundledPlugin('MySQL2Plugin', require('../plugins/MySQL2Plugin').default, require('mysql2/package.json').version); // this package in all its wisdom disallows import of its package.json where the version number lives
try {
this.installBundledPlugin('MySQLPlugin', require('../plugins/MySQLPlugin').default, require('mysql/package.json').version);
}
catch (_l) {
// ESLINT SUCKS!
}
try {
this.installBundledPlugin('PgPlugin', require('../plugins/PgPlugin').default, require('pg/package.json').version);
}
catch (_m) {
// ESLINT SUCKS!
}
};
PluginInstaller.prototype.install = function () {
if (this.require)
this.installNormal();
else
this.installBundled();
};
return PluginInstaller;
}());
exports.default = PluginInstaller;
//# sourceMappingURL=PluginInstaller.js.map