1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var agentSetting = require('./agent-setting')
|
7 | var util = require('./util')
|
8 |
|
9 | var COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
10 | var DEFAULT_PARAMS = /=[^,]+/mg;
|
11 | var FAT_ARROWS = /=>.*$/mg;
|
12 | var functionCount = 0;
|
13 | var moduleCount = 0;
|
14 | var list;
|
15 |
|
16 | function autoDiscovery(){}
|
17 |
|
18 | function getParameterNames(fn) {
|
19 | var code = fn.toString()
|
20 | .replace(COMMENTS, '')
|
21 | .replace(FAT_ARROWS, '')
|
22 | .replace(DEFAULT_PARAMS, '');
|
23 |
|
24 | var result = code.slice(code.indexOf('(') + 1, code.indexOf(')'))
|
25 | .match(/([^\s,]+)/g);
|
26 |
|
27 | return result === null
|
28 | ? []
|
29 | : result;
|
30 | }
|
31 |
|
32 | autoDiscovery.findModules = function(ID,classFilters,methodFilters,dataSocket){
|
33 | try {
|
34 | var loadedModules = Object.keys(require('module')._cache);
|
35 | var modulePattern,
|
36 | functionPattern;
|
37 | try{
|
38 | if(classFilters.includes(',')){
|
39 | classFilters=classFilters.split(',').join('|');
|
40 | modulePattern=new RegExp(classFilters);
|
41 | }
|
42 | else {
|
43 | modulePattern = new RegExp(classFilters);
|
44 | }
|
45 | }catch(e){
|
46 | modulePattern = '/NA/';
|
47 | util.logger.error(agentSetting.currentTestRun +' | Invalid Regular Expression in classFilters So, setting as NA :',e)
|
48 | }
|
49 | try{
|
50 | if(methodFilters.includes(',')){
|
51 | methodFilters=methodFilters.split(',').join('|');
|
52 | functionPattern=new RegExp(methodFilters);
|
53 | }else{
|
54 | functionPattern = new RegExp(methodFilters);
|
55 | }
|
56 | }catch(e){
|
57 | functionPattern = '/NA/';
|
58 | util.logger.error(agentSetting.currentTestRun +' | Invalid Regular Expression in methodFilters So, setting as NA :',e)
|
59 | }
|
60 | var map = new Map();
|
61 | functionCount = 0;
|
62 | moduleCount = 0;
|
63 | var listValue;
|
64 |
|
65 | var start = new Date().getTime();
|
66 | loadedModules.forEach(function(module){
|
67 | if(modulePattern == '/NA/' && module.match('netjsagent') == null){
|
68 | ++moduleCount;
|
69 | listValue = requireModule(module,functionPattern)
|
70 | map.set(module,listValue);
|
71 | }
|
72 | else if(module.match(modulePattern) !== null && module.match('netjsagent') == null){
|
73 | ++moduleCount;
|
74 | listValue = requireModule(module,functionPattern)
|
75 | map.set(module,listValue);
|
76 | }
|
77 | })
|
78 | var end = new Date().getTime();
|
79 |
|
80 | dataSocket.write("agent_post_data_req:action=discover_loaded_classes;Id="+ID+";Tier="+agentSetting.getTierName()+";Server="+agentSetting.getServerName()+";Instance="+agentSetting.getInstance()+";\n");
|
81 | map.forEach(function(value,key,map){
|
82 | if(value){
|
83 | dataSocket.write(key+'|'+value.toString()+'\n')
|
84 | }else{
|
85 | dataSocket.write(key+'|\n')
|
86 | }
|
87 | })
|
88 | util.logger.info(agentSetting.currentTestRun + ' | agent_post_data_req:totalTimeTaken='+(end-start)+';totalClasses='+moduleCount+';totalMethods='+functionCount+';');
|
89 | dataSocket.write("agent_post_data_req:totalTimeTaken="+(end-start)+";totalClasses="+moduleCount+";totalMethods="+functionCount+";\n");
|
90 | try{
|
91 | if(dataSocket){
|
92 | dataSocket.closeConnection()
|
93 | delete dataSocket
|
94 | }
|
95 | }catch(e){
|
96 | util.logger.error(agentSetting.currentTestRun +' | Error in Closing New Connection :',e)
|
97 | }
|
98 | }catch(err){
|
99 | util.logger.error(agentSetting.currentTestRun +' | Error in finding the Module :',err)
|
100 | }
|
101 | }
|
102 |
|
103 | function requireModule(module,functionPattern){
|
104 |
|
105 | try{
|
106 | if(module){
|
107 | var moduleObject = require(module);
|
108 | if(moduleObject)
|
109 | return getmethodName(module, moduleObject, moduleObject.prototype ? moduleObject.prototype:undefined ,functionPattern);
|
110 | }
|
111 | }catch(e){
|
112 | util.logger.error(agentSetting.currentTestRun +' | Error during Loading the Module :',e)
|
113 | }
|
114 | }
|
115 |
|
116 | function getmethodName(moduleName,mainObject,protoObject,functionPattern){
|
117 | try{
|
118 | |
119 |
|
120 |
|
121 | var list =[];
|
122 |
|
123 | if(mainObject){
|
124 | for (var name in mainObject ) {
|
125 |
|
126 | if(mainObject.hasOwnProperty(name)){
|
127 |
|
128 | if (!mainObject.__lookupGetter__(name) && !mainObject.__lookupSetter__(name)) {
|
129 |
|
130 | if(typeof(mainObject[name]) == 'function') {
|
131 |
|
132 | if (!mainObject[name].__super__ &&
|
133 | (mainObject[name].prototype ||
|
134 | (mainObject[name].prototype && Object.keys(mainObject[name].prototype).length == 0))
|
135 | && Object.keys(mainObject[name]).length == 0) {
|
136 |
|
137 | if(mainObject[name] && mainObject[name].name ){
|
138 | filterMethodName(mainObject[name],list,functionPattern)
|
139 | }
|
140 | }
|
141 | }
|
142 | }
|
143 | }
|
144 | }
|
145 | }
|
146 |
|
147 |
|
148 | if(protoObject){
|
149 | for(var name in protoObject){
|
150 |
|
151 | if(protoObject.hasOwnProperty(name)) {
|
152 |
|
153 | if(!protoObject.__lookupGetter__(name) && !protoObject.__lookupSetter__(name)){
|
154 |
|
155 | if(typeof(protoObject[name]) == 'function'){
|
156 | if(protoObject[name] && protoObject[name].name){
|
157 | filterMethodName(protoObject[name],list,functionPattern)
|
158 | }
|
159 | }
|
160 | }
|
161 | }
|
162 | }
|
163 | }
|
164 |
|
165 |
|
166 | return list;
|
167 | }catch(e){
|
168 | if(agentSetting.enableBciDebug > 4)
|
169 | util.logger.error(agentSetting.currentTestRun +' | Error in getmethodName'+i+' :',e)
|
170 | }
|
171 | }
|
172 |
|
173 | function filterMethodName(mainObject,list,functionPattern){
|
174 | try{
|
175 | var method = '',
|
176 | methodArgs;
|
177 | method = mainObject.name;
|
178 | if(functionPattern == '/NA/'){
|
179 | ++functionCount;
|
180 | methodArgs = getParameterNames(mainObject);
|
181 | concateMethodName(method,methodArgs,list);
|
182 | }else if(method.match(functionPattern) !== null){
|
183 | ++functionCount;
|
184 | methodArgs = getParameterNames(mainObject);
|
185 | concateMethodName(method,methodArgs,list);
|
186 | }
|
187 | }catch(e){
|
188 | util.logger.error(agentSetting.currentTestRun +' | Error in filterMethodName :',e)
|
189 | }
|
190 | }
|
191 |
|
192 | function concateMethodName(methodName,methodArgs,list){
|
193 | try{
|
194 | var method = ''
|
195 | method = methodName+'('
|
196 | for(var i = 0 ;i< methodArgs.length;i++){
|
197 | method += methodArgs[i]
|
198 | if( i != methodArgs.length-1)
|
199 | method += ','
|
200 | }
|
201 | method += ')'
|
202 | list.push(method);
|
203 | }catch(e){
|
204 | util.logger.error(agentSetting.currentTestRun +' | Error in concateMethodName :',e)
|
205 | }
|
206 | }
|
207 |
|
208 | module.exports = autoDiscovery; |
\ | No newline at end of file |