rpc-express-toolkit 2.3.2 → 3.0.0
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.
- package/README.md +221 -9
- package/examples/example.js +17 -18
- package/examples/server-client-example.js +120 -114
- package/examples/ssl-development-example.js +100 -74
- package/package.json +5 -5
- package/src/batch.js +256 -237
- package/src/clients/rpc-client.js +125 -44
- package/src/clients/rpc-client.min.js +1 -1
- package/src/clients/rpc-client.min.mjs +1 -1
- package/src/clients/rpc-client.mjs +159 -96
- package/src/index.d.ts +220 -58
- package/src/index.js +319 -115
- package/src/logger.js +184 -153
- package/src/middleware.js +229 -224
- package/src/validation.js +220 -221
package/src/logger.js
CHANGED
|
@@ -1,153 +1,184 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
|
|
13
|
-
this.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
* @param {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
*
|
|
112
|
-
* @param {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
*
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file Logger Class
|
|
3
|
+
* @description A flexible logging system supporting multiple levels and custom formatting
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Logger class for handling RPC logging
|
|
8
|
+
*/
|
|
9
|
+
class Logger {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.level = options.level || 'info';
|
|
12
|
+
this.enabled = options.enabled !== false;
|
|
13
|
+
this.prefix = options.prefix || '[JSON-RPC]';
|
|
14
|
+
this.customLogger = options.logger || null;
|
|
15
|
+
|
|
16
|
+
this.levels = {
|
|
17
|
+
error: 0,
|
|
18
|
+
warn: 1,
|
|
19
|
+
info: 2,
|
|
20
|
+
debug: 3,
|
|
21
|
+
trace: 4,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check if the given level should be logged
|
|
27
|
+
* @param {string} level
|
|
28
|
+
* @returns {boolean}
|
|
29
|
+
*/
|
|
30
|
+
shouldLog(level) {
|
|
31
|
+
return this.enabled && this.levels[level] <= this.levels[this.level];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Format log message with metadata
|
|
36
|
+
* @param {string} level
|
|
37
|
+
* @param {string} message
|
|
38
|
+
* @param {Object} meta
|
|
39
|
+
* @returns {Object}
|
|
40
|
+
*/
|
|
41
|
+
formatMessage(level, message, meta = {}) {
|
|
42
|
+
return {
|
|
43
|
+
timestamp: new Date().toISOString(),
|
|
44
|
+
level: level.toUpperCase(),
|
|
45
|
+
message,
|
|
46
|
+
...meta,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Generic log method
|
|
52
|
+
* @param {string} level
|
|
53
|
+
* @param {string} message
|
|
54
|
+
* @param {Object} meta
|
|
55
|
+
*/
|
|
56
|
+
log(level, message, meta = {}) {
|
|
57
|
+
if (!this.shouldLog(level)) return;
|
|
58
|
+
|
|
59
|
+
const logData = this.formatMessage(level, message, meta);
|
|
60
|
+
|
|
61
|
+
if (this.customLogger) {
|
|
62
|
+
this.customLogger[level](logData);
|
|
63
|
+
} else {
|
|
64
|
+
const logMessage = `${this.prefix} ${logData.timestamp} [${logData.level}] ${message}`;
|
|
65
|
+
const metaStr =
|
|
66
|
+
Object.keys(meta).length > 0 ? ` | ${JSON.stringify(meta)}` : '';
|
|
67
|
+
console[level === 'debug' || level === 'trace' ? 'log' : level](
|
|
68
|
+
`${logMessage}${metaStr}`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
error(message, meta) {
|
|
74
|
+
this.log('error', message, meta);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
warn(message, meta) {
|
|
78
|
+
this.log('warn', message, meta);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
info(message, meta) {
|
|
82
|
+
this.log('info', message, meta);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
debug(message, meta) {
|
|
86
|
+
this.log('debug', message, meta);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
trace(message, meta) {
|
|
90
|
+
this.log('trace', message, meta);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Log RPC call start
|
|
95
|
+
* @param {string} method
|
|
96
|
+
* @param {any} params
|
|
97
|
+
* @param {string|number} id
|
|
98
|
+
* @param {Object} req
|
|
99
|
+
*/
|
|
100
|
+
rpcCall(method, params, id, req) {
|
|
101
|
+
this.info('RPC call started', {
|
|
102
|
+
method,
|
|
103
|
+
id,
|
|
104
|
+
params: this.sanitizeParams(params),
|
|
105
|
+
userAgent: req.headers['user-agent'],
|
|
106
|
+
ip: req.ip || req.connection?.remoteAddress,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Log RPC call success
|
|
112
|
+
* @param {string} method
|
|
113
|
+
* @param {string|number} id
|
|
114
|
+
* @param {number} duration
|
|
115
|
+
* @param {any} result
|
|
116
|
+
*/
|
|
117
|
+
rpcSuccess(method, id, duration, result) {
|
|
118
|
+
this.info('RPC call completed', {
|
|
119
|
+
method,
|
|
120
|
+
id,
|
|
121
|
+
duration: `${duration}ms`,
|
|
122
|
+
resultType: typeof result,
|
|
123
|
+
resultSize: result ? JSON.stringify(result).length : 0,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Log RPC call error
|
|
129
|
+
* @param {string} method
|
|
130
|
+
* @param {string|number} id
|
|
131
|
+
* @param {number} duration
|
|
132
|
+
* @param {Error} error
|
|
133
|
+
*/
|
|
134
|
+
rpcError(method, id, duration, error) {
|
|
135
|
+
this.error('RPC call failed', {
|
|
136
|
+
method,
|
|
137
|
+
id,
|
|
138
|
+
duration: `${duration}ms`,
|
|
139
|
+
error: error.message,
|
|
140
|
+
code: error.code,
|
|
141
|
+
stack: error.stack,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Sanitize parameters for logging (remove sensitive data)
|
|
147
|
+
* @param {any} params
|
|
148
|
+
* @returns {any}
|
|
149
|
+
*/
|
|
150
|
+
sanitizeParams(params) {
|
|
151
|
+
if (!params || typeof params !== 'object') return params;
|
|
152
|
+
|
|
153
|
+
const sensitiveKeys = [
|
|
154
|
+
'password',
|
|
155
|
+
'token',
|
|
156
|
+
'key',
|
|
157
|
+
'secret',
|
|
158
|
+
'auth',
|
|
159
|
+
'authorization',
|
|
160
|
+
];
|
|
161
|
+
const sanitized = Array.isArray(params) ? [...params] : { ...params };
|
|
162
|
+
|
|
163
|
+
const sanitizeObj = (obj) => {
|
|
164
|
+
if (typeof obj !== 'object' || obj === null) return obj;
|
|
165
|
+
|
|
166
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
167
|
+
if (
|
|
168
|
+
sensitiveKeys.some((sensitive) =>
|
|
169
|
+
key.toLowerCase().includes(sensitive)
|
|
170
|
+
)
|
|
171
|
+
) {
|
|
172
|
+
obj[key] = '[REDACTED]';
|
|
173
|
+
} else if (typeof value === 'object') {
|
|
174
|
+
sanitizeObj(value);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return obj;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
return sanitizeObj(sanitized);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
module.exports = Logger;
|