node-consul-service 1.0.69 → 1.0.70
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/dist/consul/types.d.ts +1 -0
- package/dist/index.cjs.js +52 -38
- package/dist/index.esm.js +52 -38
- package/dist/utils/callLogger.d.ts +1 -1
- package/dist/utils/logger.d.ts +1 -1
- package/package.json +1 -1
package/dist/consul/types.d.ts
CHANGED
package/dist/index.cjs.js
CHANGED
|
@@ -88,29 +88,31 @@ async function deregisterService(id) {
|
|
|
88
88
|
console.log(`🛑 Service "${id}" deregistered successfully.`);
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
async function ensureLogDir$1() {
|
|
95
|
-
await require$$6.promises.mkdir(
|
|
91
|
+
const BASE_LOG_DIR$1 = '/home/log';
|
|
92
|
+
// اسم السيرفس الحالي اللي بيعمل الـ discovery
|
|
93
|
+
const CURRENT_SERVICE$1 = process.env.SERVICE_NAME || 'unknown-service';
|
|
94
|
+
async function ensureLogDir$1(dir) {
|
|
95
|
+
await require$$6.promises.mkdir(dir, { recursive: true });
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
function formatLogLine(serviceName, instance, elapsedMs) {
|
|
97
|
+
function formatLogLine(targetService, instance, elapsedMs) {
|
|
99
98
|
var _a, _b, _c, _d, _e;
|
|
100
99
|
const ts = new Date().toISOString();
|
|
101
|
-
const addr = (_b = (_a = instance.ServiceAddress) !== null && _a !== void 0 ? _a : instance.
|
|
100
|
+
const addr = (_b = (_a = instance.ServiceAddress) !== null && _a !== void 0 ? _a : instance.Address) !== null && _b !== void 0 ? _b : 'unknown';
|
|
102
101
|
const port = (_c = instance.ServicePort) !== null && _c !== void 0 ? _c : 'unknown';
|
|
103
102
|
const node = (_d = instance.Node) !== null && _d !== void 0 ? _d : 'N/A';
|
|
104
103
|
const id = (_e = instance.ServiceID) !== null && _e !== void 0 ? _e : 'N/A';
|
|
105
104
|
const elapsed = elapsedMs !== undefined ? ` | Elapsed=${elapsedMs}ms` : '';
|
|
106
|
-
return `${ts} |
|
|
105
|
+
return `${ts} | Target=${targetService} | ${addr}:${port} | Node=${node} | ID=${id}${elapsed}\n`;
|
|
107
106
|
}
|
|
108
|
-
|
|
109
|
-
async function appendDiscoveryLog(serviceName, instance, elapsedMs) {
|
|
107
|
+
async function appendDiscoveryLog(targetService, instance, elapsedMs) {
|
|
110
108
|
try {
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
await
|
|
109
|
+
// فولدر خاص بالسيرفس الحالي
|
|
110
|
+
const serviceDir = path.join(BASE_LOG_DIR$1, CURRENT_SERVICE$1);
|
|
111
|
+
await ensureLogDir$1(serviceDir);
|
|
112
|
+
// اللوج يروح في ملف ثابت detect.log
|
|
113
|
+
const file = path.join(serviceDir, 'detect.log');
|
|
114
|
+
const line = formatLogLine(targetService, instance, elapsedMs);
|
|
115
|
+
await require$$6.promises.appendFile(file, line, { encoding: 'utf8' });
|
|
114
116
|
}
|
|
115
117
|
catch (err) {
|
|
116
118
|
console.error('❌ Failed to write discovery log:', err);
|
|
@@ -20259,20 +20261,34 @@ const {
|
|
|
20259
20261
|
mergeConfig
|
|
20260
20262
|
} = axios;
|
|
20261
20263
|
|
|
20262
|
-
const
|
|
20263
|
-
|
|
20264
|
-
|
|
20265
|
-
|
|
20264
|
+
const BASE_LOG_DIR = '/home/log';
|
|
20265
|
+
// لازم تكون ضابط اسم السيرفس الحالي في env
|
|
20266
|
+
const CURRENT_SERVICE = process.env.SERVICE_NAME || 'unknown-service';
|
|
20267
|
+
async function ensureLogDir(dir) {
|
|
20268
|
+
await require$$6.promises.mkdir(dir, { recursive: true });
|
|
20266
20269
|
}
|
|
20267
|
-
function
|
|
20270
|
+
function getLogFileForStatus(status) {
|
|
20271
|
+
const serviceDir = path.join(BASE_LOG_DIR, CURRENT_SERVICE);
|
|
20272
|
+
if (status >= 200 && status < 300)
|
|
20273
|
+
return path.join(serviceDir, 'calls-2xx.log');
|
|
20274
|
+
if (status >= 400 && status < 500)
|
|
20275
|
+
return path.join(serviceDir, 'calls-4xx.log');
|
|
20276
|
+
if (status >= 500 && status < 600)
|
|
20277
|
+
return path.join(serviceDir, 'calls-5xx.log');
|
|
20278
|
+
return path.join(serviceDir, 'calls-other.log');
|
|
20279
|
+
}
|
|
20280
|
+
function formatCallLogLine(targetService, url, method, status, elapsedMs, errorMessage) {
|
|
20268
20281
|
const ts = new Date().toISOString();
|
|
20269
|
-
|
|
20282
|
+
const errPart = errorMessage ? ` | Error="${errorMessage}"` : '';
|
|
20283
|
+
return `${ts} | Target=${targetService} | ${method.toUpperCase()} ${url} | Status=${status} | Elapsed=${elapsedMs}ms${errPart}\n`;
|
|
20270
20284
|
}
|
|
20271
|
-
async function appendCallLog(
|
|
20285
|
+
async function appendCallLog(targetService, url, method, status, elapsedMs, errorMessage) {
|
|
20272
20286
|
try {
|
|
20273
|
-
|
|
20274
|
-
const
|
|
20275
|
-
await
|
|
20287
|
+
const file = getLogFileForStatus(status);
|
|
20288
|
+
const dir = path.dirname(file);
|
|
20289
|
+
await ensureLogDir(dir);
|
|
20290
|
+
const line = formatCallLogLine(targetService, url, method, status, elapsedMs, errorMessage);
|
|
20291
|
+
await require$$6.promises.appendFile(file, line, { encoding: 'utf8' });
|
|
20276
20292
|
}
|
|
20277
20293
|
catch (err) {
|
|
20278
20294
|
console.error('❌ Failed to write call log:', err);
|
|
@@ -20280,35 +20296,33 @@ async function appendCallLog(serviceName, url, method, status, elapsedMs) {
|
|
|
20280
20296
|
}
|
|
20281
20297
|
|
|
20282
20298
|
async function callService(serviceName, options = {}) {
|
|
20283
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
20299
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
20284
20300
|
const { method = 'GET', path = '/', data, headers, params, } = options;
|
|
20285
20301
|
const start = Date.now();
|
|
20286
20302
|
try {
|
|
20303
|
+
// ...
|
|
20287
20304
|
const instance = await getRandomServiceInstance(serviceName);
|
|
20288
20305
|
const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
|
|
20289
|
-
const response = await axios.request({
|
|
20290
|
-
url,
|
|
20291
|
-
method,
|
|
20292
|
-
data,
|
|
20293
|
-
headers,
|
|
20294
|
-
params,
|
|
20295
|
-
});
|
|
20306
|
+
const response = await axios.request({ url, method, data, headers, params });
|
|
20296
20307
|
const elapsed = Date.now() - start;
|
|
20297
|
-
//
|
|
20308
|
+
// سجل في فولدر السيرفس الحالي (مش target)
|
|
20298
20309
|
await appendCallLog(serviceName, url, method, response.status, elapsed);
|
|
20299
20310
|
return response.data;
|
|
20300
20311
|
}
|
|
20301
20312
|
catch (error) {
|
|
20302
20313
|
const elapsed = Date.now() - start;
|
|
20303
|
-
// 📌 سجل الفشل كمان
|
|
20304
20314
|
const status = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0;
|
|
20305
|
-
|
|
20315
|
+
const url = (_d = (_c = error.config) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : '';
|
|
20316
|
+
const methodUsed = (_f = (_e = error.config) === null || _e === void 0 ? void 0 : _e.method) !== null && _f !== void 0 ? _f : method;
|
|
20317
|
+
const message = (_g = error.message) !== null && _g !== void 0 ? _g : 'Unknown error';
|
|
20318
|
+
// 📌 سجل الفشل مع رسالة الخطأ
|
|
20319
|
+
await appendCallLog(serviceName, url, methodUsed, status, elapsed, message);
|
|
20306
20320
|
return {
|
|
20307
20321
|
success: false,
|
|
20308
|
-
message
|
|
20322
|
+
message,
|
|
20309
20323
|
status,
|
|
20310
|
-
code: (
|
|
20311
|
-
data: (
|
|
20324
|
+
code: (_k = (_j = (_h = error.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.code) !== null && _k !== void 0 ? _k : "",
|
|
20325
|
+
data: (_l = error.response) === null || _l === void 0 ? void 0 : _l.data,
|
|
20312
20326
|
};
|
|
20313
20327
|
}
|
|
20314
20328
|
}
|
package/dist/index.esm.js
CHANGED
|
@@ -86,29 +86,31 @@ async function deregisterService(id) {
|
|
|
86
86
|
console.log(`🛑 Service "${id}" deregistered successfully.`);
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
async function ensureLogDir$1() {
|
|
93
|
-
await promises.mkdir(
|
|
89
|
+
const BASE_LOG_DIR$1 = '/home/log';
|
|
90
|
+
// اسم السيرفس الحالي اللي بيعمل الـ discovery
|
|
91
|
+
const CURRENT_SERVICE$1 = process.env.SERVICE_NAME || 'unknown-service';
|
|
92
|
+
async function ensureLogDir$1(dir) {
|
|
93
|
+
await promises.mkdir(dir, { recursive: true });
|
|
94
94
|
}
|
|
95
|
-
|
|
96
|
-
function formatLogLine(serviceName, instance, elapsedMs) {
|
|
95
|
+
function formatLogLine(targetService, instance, elapsedMs) {
|
|
97
96
|
var _a, _b, _c, _d, _e;
|
|
98
97
|
const ts = new Date().toISOString();
|
|
99
|
-
const addr = (_b = (_a = instance.ServiceAddress) !== null && _a !== void 0 ? _a : instance.
|
|
98
|
+
const addr = (_b = (_a = instance.ServiceAddress) !== null && _a !== void 0 ? _a : instance.Address) !== null && _b !== void 0 ? _b : 'unknown';
|
|
100
99
|
const port = (_c = instance.ServicePort) !== null && _c !== void 0 ? _c : 'unknown';
|
|
101
100
|
const node = (_d = instance.Node) !== null && _d !== void 0 ? _d : 'N/A';
|
|
102
101
|
const id = (_e = instance.ServiceID) !== null && _e !== void 0 ? _e : 'N/A';
|
|
103
102
|
const elapsed = elapsedMs !== undefined ? ` | Elapsed=${elapsedMs}ms` : '';
|
|
104
|
-
return `${ts} |
|
|
103
|
+
return `${ts} | Target=${targetService} | ${addr}:${port} | Node=${node} | ID=${id}${elapsed}\n`;
|
|
105
104
|
}
|
|
106
|
-
|
|
107
|
-
async function appendDiscoveryLog(serviceName, instance, elapsedMs) {
|
|
105
|
+
async function appendDiscoveryLog(targetService, instance, elapsedMs) {
|
|
108
106
|
try {
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
await
|
|
107
|
+
// فولدر خاص بالسيرفس الحالي
|
|
108
|
+
const serviceDir = path.join(BASE_LOG_DIR$1, CURRENT_SERVICE$1);
|
|
109
|
+
await ensureLogDir$1(serviceDir);
|
|
110
|
+
// اللوج يروح في ملف ثابت detect.log
|
|
111
|
+
const file = path.join(serviceDir, 'detect.log');
|
|
112
|
+
const line = formatLogLine(targetService, instance, elapsedMs);
|
|
113
|
+
await promises.appendFile(file, line, { encoding: 'utf8' });
|
|
112
114
|
}
|
|
113
115
|
catch (err) {
|
|
114
116
|
console.error('❌ Failed to write discovery log:', err);
|
|
@@ -20257,20 +20259,34 @@ const {
|
|
|
20257
20259
|
mergeConfig
|
|
20258
20260
|
} = axios;
|
|
20259
20261
|
|
|
20260
|
-
const
|
|
20261
|
-
|
|
20262
|
-
|
|
20263
|
-
|
|
20262
|
+
const BASE_LOG_DIR = '/home/log';
|
|
20263
|
+
// لازم تكون ضابط اسم السيرفس الحالي في env
|
|
20264
|
+
const CURRENT_SERVICE = process.env.SERVICE_NAME || 'unknown-service';
|
|
20265
|
+
async function ensureLogDir(dir) {
|
|
20266
|
+
await promises.mkdir(dir, { recursive: true });
|
|
20264
20267
|
}
|
|
20265
|
-
function
|
|
20268
|
+
function getLogFileForStatus(status) {
|
|
20269
|
+
const serviceDir = path.join(BASE_LOG_DIR, CURRENT_SERVICE);
|
|
20270
|
+
if (status >= 200 && status < 300)
|
|
20271
|
+
return path.join(serviceDir, 'calls-2xx.log');
|
|
20272
|
+
if (status >= 400 && status < 500)
|
|
20273
|
+
return path.join(serviceDir, 'calls-4xx.log');
|
|
20274
|
+
if (status >= 500 && status < 600)
|
|
20275
|
+
return path.join(serviceDir, 'calls-5xx.log');
|
|
20276
|
+
return path.join(serviceDir, 'calls-other.log');
|
|
20277
|
+
}
|
|
20278
|
+
function formatCallLogLine(targetService, url, method, status, elapsedMs, errorMessage) {
|
|
20266
20279
|
const ts = new Date().toISOString();
|
|
20267
|
-
|
|
20280
|
+
const errPart = errorMessage ? ` | Error="${errorMessage}"` : '';
|
|
20281
|
+
return `${ts} | Target=${targetService} | ${method.toUpperCase()} ${url} | Status=${status} | Elapsed=${elapsedMs}ms${errPart}\n`;
|
|
20268
20282
|
}
|
|
20269
|
-
async function appendCallLog(
|
|
20283
|
+
async function appendCallLog(targetService, url, method, status, elapsedMs, errorMessage) {
|
|
20270
20284
|
try {
|
|
20271
|
-
|
|
20272
|
-
const
|
|
20273
|
-
await
|
|
20285
|
+
const file = getLogFileForStatus(status);
|
|
20286
|
+
const dir = path.dirname(file);
|
|
20287
|
+
await ensureLogDir(dir);
|
|
20288
|
+
const line = formatCallLogLine(targetService, url, method, status, elapsedMs, errorMessage);
|
|
20289
|
+
await promises.appendFile(file, line, { encoding: 'utf8' });
|
|
20274
20290
|
}
|
|
20275
20291
|
catch (err) {
|
|
20276
20292
|
console.error('❌ Failed to write call log:', err);
|
|
@@ -20278,35 +20294,33 @@ async function appendCallLog(serviceName, url, method, status, elapsedMs) {
|
|
|
20278
20294
|
}
|
|
20279
20295
|
|
|
20280
20296
|
async function callService(serviceName, options = {}) {
|
|
20281
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
20297
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
20282
20298
|
const { method = 'GET', path = '/', data, headers, params, } = options;
|
|
20283
20299
|
const start = Date.now();
|
|
20284
20300
|
try {
|
|
20301
|
+
// ...
|
|
20285
20302
|
const instance = await getRandomServiceInstance(serviceName);
|
|
20286
20303
|
const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
|
|
20287
|
-
const response = await axios.request({
|
|
20288
|
-
url,
|
|
20289
|
-
method,
|
|
20290
|
-
data,
|
|
20291
|
-
headers,
|
|
20292
|
-
params,
|
|
20293
|
-
});
|
|
20304
|
+
const response = await axios.request({ url, method, data, headers, params });
|
|
20294
20305
|
const elapsed = Date.now() - start;
|
|
20295
|
-
//
|
|
20306
|
+
// سجل في فولدر السيرفس الحالي (مش target)
|
|
20296
20307
|
await appendCallLog(serviceName, url, method, response.status, elapsed);
|
|
20297
20308
|
return response.data;
|
|
20298
20309
|
}
|
|
20299
20310
|
catch (error) {
|
|
20300
20311
|
const elapsed = Date.now() - start;
|
|
20301
|
-
// 📌 سجل الفشل كمان
|
|
20302
20312
|
const status = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0;
|
|
20303
|
-
|
|
20313
|
+
const url = (_d = (_c = error.config) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : '';
|
|
20314
|
+
const methodUsed = (_f = (_e = error.config) === null || _e === void 0 ? void 0 : _e.method) !== null && _f !== void 0 ? _f : method;
|
|
20315
|
+
const message = (_g = error.message) !== null && _g !== void 0 ? _g : 'Unknown error';
|
|
20316
|
+
// 📌 سجل الفشل مع رسالة الخطأ
|
|
20317
|
+
await appendCallLog(serviceName, url, methodUsed, status, elapsed, message);
|
|
20304
20318
|
return {
|
|
20305
20319
|
success: false,
|
|
20306
|
-
message
|
|
20320
|
+
message,
|
|
20307
20321
|
status,
|
|
20308
|
-
code: (
|
|
20309
|
-
data: (
|
|
20322
|
+
code: (_k = (_j = (_h = error.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.code) !== null && _k !== void 0 ? _k : "",
|
|
20323
|
+
data: (_l = error.response) === null || _l === void 0 ? void 0 : _l.data,
|
|
20310
20324
|
};
|
|
20311
20325
|
}
|
|
20312
20326
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function appendCallLog(
|
|
1
|
+
export declare function appendCallLog(targetService: string, url: string, method: string, status: number, elapsedMs: number, errorMessage?: string): Promise<void>;
|
package/dist/utils/logger.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { ServiceInstance } from '../consul/types';
|
|
2
|
-
export declare function appendDiscoveryLog(
|
|
2
|
+
export declare function appendDiscoveryLog(targetService: string, instance: ServiceInstance, elapsedMs?: number): Promise<void>;
|