whatap 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2016 the WHATAP project authors. All rights reserved.
|
|
3
|
+
* Use of this source code is governed by a license that
|
|
4
|
+
* can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const conf = require('../conf/configure');
|
|
10
|
+
const Logger = require('../logger');
|
|
11
|
+
const PackageCtrHelper = require('../control/packagectr-helper');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* CustomMethodObserver - Handles hook_method_patterns configuration
|
|
15
|
+
* Allows users to hook custom methods in their application code
|
|
16
|
+
*/
|
|
17
|
+
const CustomMethodObserver = function(agent) {
|
|
18
|
+
this.agent = agent;
|
|
19
|
+
this.packages = []; // No specific packages to observe
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Initialize the observer - set up configuration listener
|
|
24
|
+
*/
|
|
25
|
+
CustomMethodObserver.prototype.inject = function(mod, moduleName) {
|
|
26
|
+
// Configuration listener for hook_method_patterns
|
|
27
|
+
conf.on('hook_method_patterns', function (value) {
|
|
28
|
+
if (!value || typeof value !== 'string') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const methods = value.split(',');
|
|
33
|
+
|
|
34
|
+
methods.forEach(function (methodPath) {
|
|
35
|
+
methodPath = methodPath.trim();
|
|
36
|
+
|
|
37
|
+
// Parse path: ./src/user/user.service/ClassName.methodName or ./src/user/user.service/methodName
|
|
38
|
+
const parts = methodPath.split('/').filter(function(p) { return p && p !== '.'; });
|
|
39
|
+
|
|
40
|
+
if (parts.length < 1) {
|
|
41
|
+
Logger.printError('WHATAP-221',
|
|
42
|
+
'Invalid hook_method_patterns format: ' + methodPath +
|
|
43
|
+
'. Expected format: ./path/to/file/ClassName.methodName or ./path/to/file/methodName',
|
|
44
|
+
null, false);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const lastPart = parts.pop();
|
|
49
|
+
let methodName = null;
|
|
50
|
+
let className = null;
|
|
51
|
+
|
|
52
|
+
// Check if lastPart contains a dot (ClassName.methodName)
|
|
53
|
+
if (lastPart.indexOf('.') > 0) {
|
|
54
|
+
const classMethod = lastPart.split('.');
|
|
55
|
+
if (classMethod.length === 2) {
|
|
56
|
+
className = classMethod[0];
|
|
57
|
+
methodName = classMethod[1];
|
|
58
|
+
} else {
|
|
59
|
+
Logger.printError('WHATAP-222',
|
|
60
|
+
'Invalid class.method format: ' + lastPart +
|
|
61
|
+
'. Expected format: ClassName.methodName',
|
|
62
|
+
null, false);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
// No dot, just a method name
|
|
67
|
+
methodName = lastPart;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const relative_path = '/' + parts.join('/');
|
|
71
|
+
|
|
72
|
+
let root = process.cwd();
|
|
73
|
+
if(root.indexOf('/bin') >= 0) {
|
|
74
|
+
root = root.substr(0, root.indexOf('/bin'));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Try multiple paths for TypeScript/ESM support
|
|
78
|
+
const pathsToTry = [
|
|
79
|
+
root + relative_path, // Original path
|
|
80
|
+
root + relative_path.replace('/src/', '/dist/'), // TypeScript compiled path
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
// Add .js and .mjs extension variants
|
|
84
|
+
const extendedPaths = [];
|
|
85
|
+
pathsToTry.forEach(function(p) {
|
|
86
|
+
extendedPaths.push(p);
|
|
87
|
+
if (!p.endsWith('.js') && !p.endsWith('.ts') && !p.endsWith('.mjs')) {
|
|
88
|
+
extendedPaths.push(p + '.js');
|
|
89
|
+
extendedPaths.push(p + '.mjs');
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Async function to handle module loading and hooking
|
|
94
|
+
const tryLoadAndHook = function(pathIndex) {
|
|
95
|
+
if (pathIndex >= extendedPaths.length) {
|
|
96
|
+
Logger.printError('WHATAP-223',
|
|
97
|
+
'Could not load module for hooking. Tried paths: ' + extendedPaths.join(', '),
|
|
98
|
+
null, false);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const tryPath = extendedPaths[pathIndex];
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
if (!fs.existsSync(tryPath) && !fs.existsSync(tryPath + '.js') && !fs.existsSync(tryPath + '.mjs')) {
|
|
106
|
+
// Path doesn't exist, try next
|
|
107
|
+
tryLoadAndHook(pathIndex + 1);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// addPackage now returns a Promise
|
|
112
|
+
PackageCtrHelper.addPackage(tryPath).then(function(moduleExports) {
|
|
113
|
+
if (!moduleExports) {
|
|
114
|
+
// Failed to load, try next path
|
|
115
|
+
tryLoadAndHook(pathIndex + 1);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Handle TypeScript/ES6 named exports
|
|
120
|
+
if (className) {
|
|
121
|
+
// For TypeScript classes: module exports { ClassName: [Class] }
|
|
122
|
+
if (moduleExports[className]) {
|
|
123
|
+
if (moduleExports[className].prototype && typeof moduleExports[className].prototype[methodName] === 'function') {
|
|
124
|
+
PackageCtrHelper.hookMethod(moduleExports[className].prototype, tryPath, methodName, className);
|
|
125
|
+
} else {
|
|
126
|
+
PackageCtrHelper.hookMethod(moduleExports[className], tryPath, methodName, className);
|
|
127
|
+
}
|
|
128
|
+
Logger.print('WHATAP-224',
|
|
129
|
+
'Hooked class method: ' + className + '.' + methodName,
|
|
130
|
+
false);
|
|
131
|
+
} else if (moduleExports.default) {
|
|
132
|
+
// ESM default export
|
|
133
|
+
if (typeof moduleExports.default === 'function' && moduleExports.default.name === className) {
|
|
134
|
+
// Default export is the class itself
|
|
135
|
+
if (moduleExports.default.prototype && typeof moduleExports.default.prototype[methodName] === 'function') {
|
|
136
|
+
PackageCtrHelper.hookMethod(moduleExports.default.prototype, tryPath, methodName, className);
|
|
137
|
+
} else {
|
|
138
|
+
PackageCtrHelper.hookMethod(moduleExports.default, tryPath, methodName, className);
|
|
139
|
+
}
|
|
140
|
+
Logger.print('WHATAP-225',
|
|
141
|
+
'Hooked ESM default class method: ' + className + '.' + methodName,
|
|
142
|
+
false);
|
|
143
|
+
} else if (moduleExports.default[className]) {
|
|
144
|
+
// Default export contains the class
|
|
145
|
+
if (moduleExports.default[className].prototype && typeof moduleExports.default[className].prototype[methodName] === 'function') {
|
|
146
|
+
PackageCtrHelper.hookMethod(moduleExports.default[className].prototype, tryPath, methodName, className);
|
|
147
|
+
} else {
|
|
148
|
+
PackageCtrHelper.hookMethod(moduleExports.default[className], tryPath, methodName, className);
|
|
149
|
+
}
|
|
150
|
+
Logger.print('WHATAP-226',
|
|
151
|
+
'Hooked ESM class method (in default): ' + className + '.' + methodName,
|
|
152
|
+
false);
|
|
153
|
+
} else {
|
|
154
|
+
Logger.printError('WHATAP-227',
|
|
155
|
+
'Class "' + className + '" not found in module exports for ' + tryPath,
|
|
156
|
+
null, false);
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
Logger.printError('WHATAP-228',
|
|
160
|
+
'Class "' + className + '" not found in module exports for ' + tryPath,
|
|
161
|
+
null, false);
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
// Direct method or try to find the method in exports
|
|
165
|
+
// For ESM, check default export first
|
|
166
|
+
if (moduleExports.default && typeof moduleExports.default === 'object') {
|
|
167
|
+
if (typeof moduleExports.default[methodName] === 'function') {
|
|
168
|
+
PackageCtrHelper.hookMethod(moduleExports.default, tryPath, methodName, null);
|
|
169
|
+
} else {
|
|
170
|
+
PackageCtrHelper.dynamicHook(moduleExports.default, tryPath, methodName);
|
|
171
|
+
}
|
|
172
|
+
Logger.print('WHATAP-229',
|
|
173
|
+
'Hooked ESM default export method: ' + tryPath + '/' + methodName,
|
|
174
|
+
false);
|
|
175
|
+
} else {
|
|
176
|
+
if (typeof moduleExports[methodName] === 'function') {
|
|
177
|
+
PackageCtrHelper.hookMethod(moduleExports, tryPath, methodName, null);
|
|
178
|
+
} else {
|
|
179
|
+
PackageCtrHelper.dynamicHook(moduleExports, tryPath, methodName);
|
|
180
|
+
}
|
|
181
|
+
Logger.print('WHATAP-230',
|
|
182
|
+
'Hooked method: ' + tryPath + '/' + methodName,
|
|
183
|
+
false);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}).catch(function(e) {
|
|
187
|
+
// Error loading this path, try next
|
|
188
|
+
Logger.printError('WHATAP-231',
|
|
189
|
+
'Error loading module ' + tryPath + ': ' + e.message,
|
|
190
|
+
e, false);
|
|
191
|
+
tryLoadAndHook(pathIndex + 1);
|
|
192
|
+
});
|
|
193
|
+
} catch (e) {
|
|
194
|
+
// Error with this path, try next
|
|
195
|
+
tryLoadAndHook(pathIndex + 1);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// Start trying paths from index 0
|
|
200
|
+
tryLoadAndHook(0);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
module.exports.CustomMethodObserver = CustomMethodObserver;
|
|
@@ -315,7 +315,7 @@ function initCtx(req, res) {
|
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
317
|
} catch (e) {
|
|
318
|
-
Logger.printError('WHATAP-237', 'Multi Server Transaction ', e,
|
|
318
|
+
Logger.printError('WHATAP-237', 'Multi Server Transaction ', e, false);
|
|
319
319
|
}
|
|
320
320
|
}
|
|
321
321
|
}
|
|
@@ -500,7 +500,7 @@ HttpObserver.prototype.inject = function (mod, moduleName) {
|
|
|
500
500
|
ctx.httpc_port = 80
|
|
501
501
|
}
|
|
502
502
|
} catch (e) {
|
|
503
|
-
Logger.printError('WHATAP-240', 'Http Setup Error', e,
|
|
503
|
+
Logger.printError('WHATAP-240', 'Http Setup Error', e, false);
|
|
504
504
|
return original.apply(this, arguments);
|
|
505
505
|
}
|
|
506
506
|
} else {
|
package/package.json
CHANGED