UNPKG

4.65 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018, OpenCensus Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.BasePlugin = void 0;
19const path = require("path");
20const semver = require("semver");
21const noop_logger_1 = require("../../common/noop-logger");
22/** This class represent the base to patch plugin. */
23class BasePlugin {
24 /**
25 * Constructs a new BasePlugin instance.
26 * @param moduleName The module name.
27 */
28 constructor(moduleName) {
29 /** a logger */
30 this.logger = noop_logger_1.noopLogger;
31 this.moduleName = moduleName;
32 }
33 /**
34 * Sets modified plugin to the context.
35 * @param moduleExports nodejs module exports to set as context
36 * @param tracer tracer relating to context
37 * @param version module version description
38 * @param options plugin options
39 * @param basedir module absolute path
40 * @param stats a stats instance
41 */
42 setPluginContext(
43 // tslint:disable-next-line:no-any
44 moduleExports, tracer, version, options, basedir, stats) {
45 this.moduleExports = moduleExports;
46 this.tracer = tracer;
47 this.version = version;
48 if (basedir)
49 this.basedir = basedir;
50 this.logger = tracer.logger;
51 this.options = options;
52 this.stats = stats;
53 this.internalFilesExports = this.loadInternalFiles();
54 }
55 /**
56 * Method that enables the instrumentation patch.
57 *
58 * This method implements the GoF Template Method Pattern
59 * 'enable' is the invariant part of the pattern and
60 * 'applyPatch' the variant.
61 *
62 * @param moduleExports nodejs module exports from the module to patch
63 * @param tracer a tracer instance
64 * @param version version of the current instaled module to patch
65 * @param options plugin options
66 * @param basedir an optional module absolute path
67 * @param stats an optional stats instance
68 */
69 enable(moduleExports, tracer, version, options, basedir, stats) {
70 this.setPluginContext(moduleExports, tracer, version, options, basedir, stats);
71 return this.applyPatch();
72 }
73 /** Method to disable the instrumentation */
74 disable() {
75 this.applyUnpatch();
76 }
77 /**
78 * Load internal files according to version range
79 */
80 loadInternalFiles() {
81 let result = {};
82 if (this.internalFileList) {
83 this.logger.debug('loadInternalFiles %o', this.internalFileList);
84 Object.keys(this.internalFileList).forEach(versionRange => {
85 if (semver.satisfies(this.version, versionRange)) {
86 if (result) {
87 this.logger.warn('Plugin for %s@%s, has overlap version range (%s) for internal files: %o', this.moduleName, this.version, versionRange, this.internalFileList);
88 }
89 result = this.loadInternalModuleFiles(this.internalFileList[versionRange], this.basedir);
90 }
91 });
92 if (Object.keys(result).length === 0) {
93 this.logger.debug('No internal file could be loaded for %s@%s', this.moduleName, this.version);
94 }
95 }
96 return result;
97 }
98 /**
99 * Load internal files from a module and set internalFilesExports
100 */
101 loadInternalModuleFiles(extraModulesList, basedir) {
102 const extraModules = {};
103 if (extraModulesList) {
104 Object.keys(extraModulesList).forEach(moduleName => {
105 try {
106 this.logger.debug('loading File %s', extraModulesList[moduleName]);
107 extraModules[moduleName] = require(path.join(basedir, extraModulesList[moduleName]));
108 }
109 catch (e) {
110 this.logger.error('Could not load internal file %s of module %s. Error: %s', path.join(basedir, extraModulesList[moduleName]), this.moduleName, e.message);
111 }
112 });
113 }
114 return extraModules;
115 }
116}
117exports.BasePlugin = BasePlugin;
118//# sourceMappingURL=base-plugin.js.map
\No newline at end of file