node-consul-service 1.0.68 → 1.0.69

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/index.cjs.js CHANGED
@@ -88,11 +88,11 @@ async function deregisterService(id) {
88
88
  console.log(`🛑 Service "${id}" deregistered successfully.`);
89
89
  }
90
90
 
91
- const LOG_DIR = '/home/log';
92
- const LOG_FILE = path.join(LOG_DIR, 'detect.log');
91
+ const LOG_DIR$1 = '/home/log';
92
+ const LOG_FILE$1 = path.join(LOG_DIR$1, 'detect.log');
93
93
  // تأكد من وجود المجلد
94
- async function ensureLogDir() {
95
- await require$$6.promises.mkdir(LOG_DIR, { recursive: true });
94
+ async function ensureLogDir$1() {
95
+ await require$$6.promises.mkdir(LOG_DIR$1, { recursive: true });
96
96
  }
97
97
  // صيغة السطر
98
98
  function formatLogLine(serviceName, instance, elapsedMs) {
@@ -108,9 +108,9 @@ function formatLogLine(serviceName, instance, elapsedMs) {
108
108
  // دالة تسجيل
109
109
  async function appendDiscoveryLog(serviceName, instance, elapsedMs) {
110
110
  try {
111
- await ensureLogDir();
111
+ await ensureLogDir$1();
112
112
  const line = formatLogLine(serviceName, instance, elapsedMs);
113
- await require$$6.promises.appendFile(LOG_FILE, line, { encoding: 'utf8' });
113
+ await require$$6.promises.appendFile(LOG_FILE$1, line, { encoding: 'utf8' });
114
114
  }
115
115
  catch (err) {
116
116
  console.error('❌ Failed to write discovery log:', err);
@@ -122,7 +122,10 @@ async function listServices() {
122
122
  return Object.values(services);
123
123
  }
124
124
  async function getServiceInstances(serviceName) {
125
+ const start = Date.now();
125
126
  const services = await getClient().catalog.service.nodes(serviceName);
127
+ const elapsed = Date.now() - start;
128
+ console.log(`⏱️ Discovery for ${serviceName} took ${elapsed}ms`);
126
129
  return services;
127
130
  }
128
131
  async function getRandomServiceInstance(serviceName) {
@@ -133,7 +136,6 @@ async function getRandomServiceInstance(serviceName) {
133
136
  const randomIndex = Math.floor(Math.random() * instances.length);
134
137
  const instance = instances[randomIndex];
135
138
  const elapsed = Date.now() - start;
136
- // 📌 سجل الإنستانس والمدة المستغرقة
137
139
  await appendDiscoveryLog(serviceName, instance, elapsed);
138
140
  return instance;
139
141
  }
@@ -20257,9 +20259,30 @@ const {
20257
20259
  mergeConfig
20258
20260
  } = axios;
20259
20261
 
20262
+ const LOG_DIR = '/home/log';
20263
+ const LOG_FILE = path.join(LOG_DIR, 'calls.log');
20264
+ async function ensureLogDir() {
20265
+ await require$$6.promises.mkdir(LOG_DIR, { recursive: true });
20266
+ }
20267
+ function formatCallLogLine(serviceName, url, method, status, elapsedMs) {
20268
+ const ts = new Date().toISOString();
20269
+ return `${ts} | ${serviceName} | ${method.toUpperCase()} ${url} | Status=${status} | Elapsed=${elapsedMs}ms\n`;
20270
+ }
20271
+ async function appendCallLog(serviceName, url, method, status, elapsedMs) {
20272
+ try {
20273
+ await ensureLogDir();
20274
+ const line = formatCallLogLine(serviceName, url, method, status, elapsedMs);
20275
+ await require$$6.promises.appendFile(LOG_FILE, line, { encoding: 'utf8' });
20276
+ }
20277
+ catch (err) {
20278
+ console.error('❌ Failed to write call log:', err);
20279
+ }
20280
+ }
20281
+
20260
20282
  async function callService(serviceName, options = {}) {
20261
- var _a, _b, _c, _d;
20283
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
20262
20284
  const { method = 'GET', path = '/', data, headers, params, } = options;
20285
+ const start = Date.now();
20263
20286
  try {
20264
20287
  const instance = await getRandomServiceInstance(serviceName);
20265
20288
  const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
@@ -20270,15 +20293,22 @@ async function callService(serviceName, options = {}) {
20270
20293
  headers,
20271
20294
  params,
20272
20295
  });
20296
+ const elapsed = Date.now() - start;
20297
+ // 📌 سجل نجاح الكول
20298
+ await appendCallLog(serviceName, url, method, response.status, elapsed);
20273
20299
  return response.data;
20274
20300
  }
20275
20301
  catch (error) {
20302
+ const elapsed = Date.now() - start;
20303
+ // 📌 سجل الفشل كمان
20304
+ const status = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0;
20305
+ await appendCallLog(serviceName, (_d = (_c = error.config) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : '', (_f = (_e = error.config) === null || _e === void 0 ? void 0 : _e.method) !== null && _f !== void 0 ? _f : method, status, elapsed);
20276
20306
  return {
20277
20307
  success: false,
20278
20308
  message: error.message,
20279
- status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status,
20280
- code: (_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data.code) !== null && _c !== void 0 ? _c : "",
20281
- data: (_d = error.response) === null || _d === void 0 ? void 0 : _d.data,
20309
+ status,
20310
+ code: (_j = (_h = (_g = error.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.code) !== null && _j !== void 0 ? _j : "",
20311
+ data: (_k = error.response) === null || _k === void 0 ? void 0 : _k.data,
20282
20312
  };
20283
20313
  }
20284
20314
  }
package/dist/index.esm.js CHANGED
@@ -86,11 +86,11 @@ async function deregisterService(id) {
86
86
  console.log(`🛑 Service "${id}" deregistered successfully.`);
87
87
  }
88
88
 
89
- const LOG_DIR = '/home/log';
90
- const LOG_FILE = path.join(LOG_DIR, 'detect.log');
89
+ const LOG_DIR$1 = '/home/log';
90
+ const LOG_FILE$1 = path.join(LOG_DIR$1, 'detect.log');
91
91
  // تأكد من وجود المجلد
92
- async function ensureLogDir() {
93
- await promises.mkdir(LOG_DIR, { recursive: true });
92
+ async function ensureLogDir$1() {
93
+ await promises.mkdir(LOG_DIR$1, { recursive: true });
94
94
  }
95
95
  // صيغة السطر
96
96
  function formatLogLine(serviceName, instance, elapsedMs) {
@@ -106,9 +106,9 @@ function formatLogLine(serviceName, instance, elapsedMs) {
106
106
  // دالة تسجيل
107
107
  async function appendDiscoveryLog(serviceName, instance, elapsedMs) {
108
108
  try {
109
- await ensureLogDir();
109
+ await ensureLogDir$1();
110
110
  const line = formatLogLine(serviceName, instance, elapsedMs);
111
- await promises.appendFile(LOG_FILE, line, { encoding: 'utf8' });
111
+ await promises.appendFile(LOG_FILE$1, line, { encoding: 'utf8' });
112
112
  }
113
113
  catch (err) {
114
114
  console.error('❌ Failed to write discovery log:', err);
@@ -120,7 +120,10 @@ async function listServices() {
120
120
  return Object.values(services);
121
121
  }
122
122
  async function getServiceInstances(serviceName) {
123
+ const start = Date.now();
123
124
  const services = await getClient().catalog.service.nodes(serviceName);
125
+ const elapsed = Date.now() - start;
126
+ console.log(`⏱️ Discovery for ${serviceName} took ${elapsed}ms`);
124
127
  return services;
125
128
  }
126
129
  async function getRandomServiceInstance(serviceName) {
@@ -131,7 +134,6 @@ async function getRandomServiceInstance(serviceName) {
131
134
  const randomIndex = Math.floor(Math.random() * instances.length);
132
135
  const instance = instances[randomIndex];
133
136
  const elapsed = Date.now() - start;
134
- // 📌 سجل الإنستانس والمدة المستغرقة
135
137
  await appendDiscoveryLog(serviceName, instance, elapsed);
136
138
  return instance;
137
139
  }
@@ -20255,9 +20257,30 @@ const {
20255
20257
  mergeConfig
20256
20258
  } = axios;
20257
20259
 
20260
+ const LOG_DIR = '/home/log';
20261
+ const LOG_FILE = path.join(LOG_DIR, 'calls.log');
20262
+ async function ensureLogDir() {
20263
+ await promises.mkdir(LOG_DIR, { recursive: true });
20264
+ }
20265
+ function formatCallLogLine(serviceName, url, method, status, elapsedMs) {
20266
+ const ts = new Date().toISOString();
20267
+ return `${ts} | ${serviceName} | ${method.toUpperCase()} ${url} | Status=${status} | Elapsed=${elapsedMs}ms\n`;
20268
+ }
20269
+ async function appendCallLog(serviceName, url, method, status, elapsedMs) {
20270
+ try {
20271
+ await ensureLogDir();
20272
+ const line = formatCallLogLine(serviceName, url, method, status, elapsedMs);
20273
+ await promises.appendFile(LOG_FILE, line, { encoding: 'utf8' });
20274
+ }
20275
+ catch (err) {
20276
+ console.error('❌ Failed to write call log:', err);
20277
+ }
20278
+ }
20279
+
20258
20280
  async function callService(serviceName, options = {}) {
20259
- var _a, _b, _c, _d;
20281
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
20260
20282
  const { method = 'GET', path = '/', data, headers, params, } = options;
20283
+ const start = Date.now();
20261
20284
  try {
20262
20285
  const instance = await getRandomServiceInstance(serviceName);
20263
20286
  const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
@@ -20268,15 +20291,22 @@ async function callService(serviceName, options = {}) {
20268
20291
  headers,
20269
20292
  params,
20270
20293
  });
20294
+ const elapsed = Date.now() - start;
20295
+ // 📌 سجل نجاح الكول
20296
+ await appendCallLog(serviceName, url, method, response.status, elapsed);
20271
20297
  return response.data;
20272
20298
  }
20273
20299
  catch (error) {
20300
+ const elapsed = Date.now() - start;
20301
+ // 📌 سجل الفشل كمان
20302
+ const status = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0;
20303
+ await appendCallLog(serviceName, (_d = (_c = error.config) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : '', (_f = (_e = error.config) === null || _e === void 0 ? void 0 : _e.method) !== null && _f !== void 0 ? _f : method, status, elapsed);
20274
20304
  return {
20275
20305
  success: false,
20276
20306
  message: error.message,
20277
- status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status,
20278
- code: (_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data.code) !== null && _c !== void 0 ? _c : "",
20279
- data: (_d = error.response) === null || _d === void 0 ? void 0 : _d.data,
20307
+ status,
20308
+ code: (_j = (_h = (_g = error.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.code) !== null && _j !== void 0 ? _j : "",
20309
+ data: (_k = error.response) === null || _k === void 0 ? void 0 : _k.data,
20280
20310
  };
20281
20311
  }
20282
20312
  }
@@ -0,0 +1 @@
1
+ export declare function appendCallLog(serviceName: string, url: string, method: string, status: number, elapsedMs: number): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.68",
3
+ "version": "1.0.69",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",