node-consul-service 1.0.74 → 1.0.75

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,5 @@
1
+ import { AtlasServiceInstance } from './types';
2
+ export declare function listServices(): Promise<any[]>;
3
+ export declare function getServiceInstances(serviceName: string): Promise<AtlasServiceInstance[]>;
4
+ export declare function getRandomServiceInstance(serviceName: string): Promise<AtlasServiceInstance>;
5
+ export declare function getServiceUrl(serviceName: string): Promise<string>;
@@ -1,4 +1,3 @@
1
- import Consul from 'consul';
2
1
  import { AtlasClient } from '../atlas/atlas.client';
3
2
  export interface ConsulClientOptions {
4
3
  host?: string;
@@ -8,4 +7,3 @@ export interface ConsulClientOptions {
8
7
  export declare function toBoolean(value?: string | boolean | null): boolean;
9
8
  export declare function initClient({ host, port, secure }: ConsulClientOptions): void;
10
9
  export declare function getAtlasClient(): AtlasClient;
11
- export declare function getClient(): Consul;
@@ -1,18 +1,9 @@
1
1
  type RegisterOptions = {
2
2
  name: string;
3
- id: string;
4
3
  port: number;
5
4
  address?: string;
6
- check?: {
7
- name?: string;
8
- http?: string;
9
- interval?: string;
10
- timeout?: string;
11
- deregistercriticalserviceafter?: string;
12
- } | false;
13
5
  tags?: string[];
14
6
  meta?: Record<string, string>;
15
7
  };
16
8
  export declare function registerService(options: RegisterOptions): Promise<void>;
17
- export declare function deregisterService(id: string): Promise<void>;
18
9
  export {};
@@ -5,3 +5,9 @@ export interface ServiceInstance {
5
5
  ServicePort: number;
6
6
  Address: any;
7
7
  }
8
+ export interface AtlasServiceInstance {
9
+ _id: string;
10
+ name: string;
11
+ address: string;
12
+ port: number;
13
+ }
package/dist/index.cjs.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var Consul = require('consul');
4
3
  var require$$1 = require('util');
5
4
  var stream = require('stream');
6
5
  var path = require('path');
@@ -20164,8 +20163,6 @@ class AtlasClient {
20164
20163
  }
20165
20164
  }
20166
20165
 
20167
- // src/lib/client.ts
20168
- let consulClient = null;
20169
20166
  let atlasClient = null;
20170
20167
  function toBoolean(value) {
20171
20168
  if (typeof value === 'boolean')
@@ -20180,19 +20177,6 @@ function initClient({ host = '127.0.0.1', port = 8500, secure = false }) {
20180
20177
  port: 371,
20181
20178
  version: '1',
20182
20179
  });
20183
- consulClient = new Consul({
20184
- host: host !== null && host !== void 0 ? host : '127.0.0.1',
20185
- port: port !== null && port !== void 0 ? port : 8500,
20186
- secure: toBoolean(secure),
20187
- });
20188
- consulClient.agent.self(err => {
20189
- if (err) {
20190
- console.error('❌ Failed to connect to Consul:', err.message);
20191
- }
20192
- else {
20193
- console.log('✅ Connected to Consul successfully');
20194
- }
20195
- });
20196
20180
  }
20197
20181
  function getAtlasClient() {
20198
20182
  if (!atlasClient) {
@@ -20200,61 +20184,19 @@ function getAtlasClient() {
20200
20184
  }
20201
20185
  return atlasClient;
20202
20186
  }
20203
- function getClient() {
20204
- if (!consulClient) {
20205
- throw new Error('Consul client not initialized. Call initClient() first.');
20206
- }
20207
- return consulClient;
20208
- }
20209
20187
 
20210
- const registeredServices = new Set();
20211
20188
  async function registerService(options) {
20212
- var _a, _b, _c, _d;
20213
- const { name, id, port, address = 'localhost', check, tags, meta } = options;
20189
+ const { name, port, address = 'localhost', tags, meta } = options;
20214
20190
  const atlasClient = getAtlasClient();
20215
- await atlasClient.registerService({
20191
+ await atlasClient
20192
+ .registerService({
20216
20193
  address,
20217
20194
  port,
20218
20195
  name,
20219
- }).then((res) => {
20220
- console.log("🚀 ~ registerService ~ res:", res);
20196
+ })
20197
+ .then(res => {
20198
+ console.log("Service Registered");
20221
20199
  });
20222
- if (registeredServices.has(id)) {
20223
- console.log(`⚠️ Service "${id}" is already registered.`);
20224
- return;
20225
- }
20226
- const serviceDefinition = {
20227
- name,
20228
- id,
20229
- address,
20230
- port,
20231
- tags,
20232
- meta,
20233
- };
20234
- if (check !== false) {
20235
- const checkDefinition = {
20236
- name: (_a = check === null || check === void 0 ? void 0 : check.name) !== null && _a !== void 0 ? _a : `${name}-check`,
20237
- http: (_b = check === null || check === void 0 ? void 0 : check.http) !== null && _b !== void 0 ? _b : `http://${address}:${port}/health`,
20238
- interval: (_c = check === null || check === void 0 ? void 0 : check.interval) !== null && _c !== void 0 ? _c : '10s',
20239
- timeout: (_d = check === null || check === void 0 ? void 0 : check.timeout) !== null && _d !== void 0 ? _d : '5s',
20240
- };
20241
- if (check === null || check === void 0 ? void 0 : check.deregistercriticalserviceafter) {
20242
- checkDefinition.deregistercriticalserviceafter = check.deregistercriticalserviceafter;
20243
- }
20244
- serviceDefinition.check = checkDefinition;
20245
- }
20246
- await getClient().agent.service.register(serviceDefinition);
20247
- registeredServices.add(id);
20248
- console.log(`✅ Service "${id}" registered successfully.`);
20249
- }
20250
- async function deregisterService(id) {
20251
- if (!registeredServices.has(id)) {
20252
- console.log(`⚠️ Service "${id}" is not registered.`);
20253
- return;
20254
- }
20255
- await getClient().agent.service.deregister(id);
20256
- registeredServices.delete(id);
20257
- console.log(`🛑 Service "${id}" deregistered successfully.`);
20258
20200
  }
20259
20201
 
20260
20202
  const BASE_LOG_DIR$2 = '/home/log';
@@ -20264,12 +20206,12 @@ async function ensureLogDir$2(dir) {
20264
20206
  await require$$6.promises.mkdir(dir, { recursive: true });
20265
20207
  }
20266
20208
  function formatLogLine(targetService, instance, elapsedMs) {
20267
- var _a, _b, _c, _d, _e;
20209
+ var _a, _b, _c, _d;
20268
20210
  const ts = new Date().toISOString();
20269
- const addr = (_b = (_a = instance.ServiceAddress) !== null && _a !== void 0 ? _a : instance.Address) !== null && _b !== void 0 ? _b : 'unknown';
20270
- const port = (_c = instance.ServicePort) !== null && _c !== void 0 ? _c : 'unknown';
20271
- const node = (_d = instance.Node) !== null && _d !== void 0 ? _d : 'N/A';
20272
- const id = (_e = instance.ServiceID) !== null && _e !== void 0 ? _e : 'N/A';
20211
+ const addr = (_a = instance.address) !== null && _a !== void 0 ? _a : 'unknown';
20212
+ const port = (_b = instance.port) !== null && _b !== void 0 ? _b : 'unknown';
20213
+ const node = (_c = instance.Node) !== null && _c !== void 0 ? _c : 'N/A';
20214
+ const id = (_d = instance.ServiceID) !== null && _d !== void 0 ? _d : 'N/A';
20273
20215
  const elapsed = elapsedMs !== undefined ? ` | Elapsed=${elapsedMs}ms` : '';
20274
20216
  return `${ts} | Target=${targetService} | ${addr}:${port} | Node=${node} | ID=${id}${elapsed}\n`;
20275
20217
  }
@@ -20314,29 +20256,33 @@ function get(key) {
20314
20256
  return record.value;
20315
20257
  }
20316
20258
 
20317
- const CACHE_TTL = 60000;
20259
+ const CACHE_TTL = 60000; // 1 دقيقة
20260
+ // جلب كل الخدمات
20318
20261
  async function listServices() {
20319
20262
  const cacheKey = 'services:list';
20320
20263
  const cached = get(cacheKey);
20321
20264
  if (cached)
20322
20265
  return cached;
20323
- const services = await getClient().agent.service.list();
20324
- const values = Object.values(services);
20325
- set(cacheKey, values, CACHE_TTL);
20326
- return values;
20266
+ const services = await getAtlasClient().listServices();
20267
+ set(cacheKey, services, CACHE_TTL);
20268
+ return services;
20327
20269
  }
20270
+ // جلب كل الـ instances لسيرفس معين
20328
20271
  async function getServiceInstances(serviceName) {
20329
20272
  const cacheKey = `services:${serviceName}`;
20330
20273
  const cached = get(cacheKey);
20331
20274
  if (cached)
20332
20275
  return cached;
20333
20276
  const start = Date.now();
20334
- const services = await getClient().catalog.service.nodes(serviceName);
20277
+ const services = await getAtlasClient().listServices(); // هنجيب كل السيرفسات
20335
20278
  const elapsed = Date.now() - start;
20336
20279
  console.log(`⏱️ Discovery for ${serviceName} took ${elapsed}ms`);
20337
- set(cacheKey, services, CACHE_TTL);
20338
- return services;
20280
+ // فلترة السيرفس المطلوب
20281
+ const instances = services.filter(s => s.name === serviceName);
20282
+ set(cacheKey, instances, CACHE_TTL);
20283
+ return instances;
20339
20284
  }
20285
+ // جلب instance عشوائي لسيرفس معين
20340
20286
  async function getRandomServiceInstance(serviceName) {
20341
20287
  const cacheKey = `random:${serviceName}`;
20342
20288
  const cached = get(cacheKey);
@@ -20350,18 +20296,17 @@ async function getRandomServiceInstance(serviceName) {
20350
20296
  const instance = instances[randomIndex];
20351
20297
  const elapsed = Date.now() - start;
20352
20298
  await appendDiscoveryLog(serviceName, instance, elapsed);
20353
- // cache random instance
20354
20299
  set(cacheKey, instance, CACHE_TTL);
20355
20300
  return instance;
20356
20301
  }
20302
+ // جلب URL جاهز للاتصال
20357
20303
  async function getServiceUrl(serviceName) {
20358
20304
  const cacheKey = `url:${serviceName}`;
20359
20305
  const cached = get(cacheKey);
20360
20306
  if (cached)
20361
20307
  return cached;
20362
20308
  const instance = await getRandomServiceInstance(serviceName);
20363
- const url = `http://${instance.ServiceAddress}:${instance.ServicePort}`;
20364
- // cache url
20309
+ const url = `http://${instance.address}:${instance.port}`;
20365
20310
  set(cacheKey, url, CACHE_TTL);
20366
20311
  return url;
20367
20312
  }
@@ -20435,7 +20380,7 @@ async function callService(serviceName, options = {}) {
20435
20380
  const start = Date.now();
20436
20381
  try {
20437
20382
  const instance = await getRandomServiceInstance(serviceName);
20438
- const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
20383
+ const url = `http://${instance.address}:${instance.port}${path}`;
20439
20384
  const response = await axios.request({ url, method, data, headers, params });
20440
20385
  const elapsed = Date.now() - start;
20441
20386
  // 📌 سجل الكول
@@ -20624,9 +20569,7 @@ async function dataLink(data, schema) {
20624
20569
 
20625
20570
  exports.callService = callService;
20626
20571
  exports.dataLink = dataLink;
20627
- exports.deregisterService = deregisterService;
20628
20572
  exports.getAtlasClient = getAtlasClient;
20629
- exports.getClient = getClient;
20630
20573
  exports.getRandomServiceInstance = getRandomServiceInstance;
20631
20574
  exports.getServiceInstances = getServiceInstances;
20632
20575
  exports.getServiceUrl = getServiceUrl;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './consul/client';
2
2
  export * from './consul/registry';
3
- export * from './consul/discovery';
3
+ export * from './consul/atlasDiscovery';
4
4
  export * from './consul/httpCaller';
5
5
  export * from './consul/dataLink';
package/dist/index.esm.js CHANGED
@@ -1,4 +1,3 @@
1
- import Consul from 'consul';
2
1
  import require$$1 from 'util';
3
2
  import stream, { Readable } from 'stream';
4
3
  import path from 'path';
@@ -20162,8 +20161,6 @@ class AtlasClient {
20162
20161
  }
20163
20162
  }
20164
20163
 
20165
- // src/lib/client.ts
20166
- let consulClient = null;
20167
20164
  let atlasClient = null;
20168
20165
  function toBoolean(value) {
20169
20166
  if (typeof value === 'boolean')
@@ -20178,19 +20175,6 @@ function initClient({ host = '127.0.0.1', port = 8500, secure = false }) {
20178
20175
  port: 371,
20179
20176
  version: '1',
20180
20177
  });
20181
- consulClient = new Consul({
20182
- host: host !== null && host !== void 0 ? host : '127.0.0.1',
20183
- port: port !== null && port !== void 0 ? port : 8500,
20184
- secure: toBoolean(secure),
20185
- });
20186
- consulClient.agent.self(err => {
20187
- if (err) {
20188
- console.error('❌ Failed to connect to Consul:', err.message);
20189
- }
20190
- else {
20191
- console.log('✅ Connected to Consul successfully');
20192
- }
20193
- });
20194
20178
  }
20195
20179
  function getAtlasClient() {
20196
20180
  if (!atlasClient) {
@@ -20198,61 +20182,19 @@ function getAtlasClient() {
20198
20182
  }
20199
20183
  return atlasClient;
20200
20184
  }
20201
- function getClient() {
20202
- if (!consulClient) {
20203
- throw new Error('Consul client not initialized. Call initClient() first.');
20204
- }
20205
- return consulClient;
20206
- }
20207
20185
 
20208
- const registeredServices = new Set();
20209
20186
  async function registerService(options) {
20210
- var _a, _b, _c, _d;
20211
- const { name, id, port, address = 'localhost', check, tags, meta } = options;
20187
+ const { name, port, address = 'localhost', tags, meta } = options;
20212
20188
  const atlasClient = getAtlasClient();
20213
- await atlasClient.registerService({
20189
+ await atlasClient
20190
+ .registerService({
20214
20191
  address,
20215
20192
  port,
20216
20193
  name,
20217
- }).then((res) => {
20218
- console.log("🚀 ~ registerService ~ res:", res);
20194
+ })
20195
+ .then(res => {
20196
+ console.log("Service Registered");
20219
20197
  });
20220
- if (registeredServices.has(id)) {
20221
- console.log(`⚠️ Service "${id}" is already registered.`);
20222
- return;
20223
- }
20224
- const serviceDefinition = {
20225
- name,
20226
- id,
20227
- address,
20228
- port,
20229
- tags,
20230
- meta,
20231
- };
20232
- if (check !== false) {
20233
- const checkDefinition = {
20234
- name: (_a = check === null || check === void 0 ? void 0 : check.name) !== null && _a !== void 0 ? _a : `${name}-check`,
20235
- http: (_b = check === null || check === void 0 ? void 0 : check.http) !== null && _b !== void 0 ? _b : `http://${address}:${port}/health`,
20236
- interval: (_c = check === null || check === void 0 ? void 0 : check.interval) !== null && _c !== void 0 ? _c : '10s',
20237
- timeout: (_d = check === null || check === void 0 ? void 0 : check.timeout) !== null && _d !== void 0 ? _d : '5s',
20238
- };
20239
- if (check === null || check === void 0 ? void 0 : check.deregistercriticalserviceafter) {
20240
- checkDefinition.deregistercriticalserviceafter = check.deregistercriticalserviceafter;
20241
- }
20242
- serviceDefinition.check = checkDefinition;
20243
- }
20244
- await getClient().agent.service.register(serviceDefinition);
20245
- registeredServices.add(id);
20246
- console.log(`✅ Service "${id}" registered successfully.`);
20247
- }
20248
- async function deregisterService(id) {
20249
- if (!registeredServices.has(id)) {
20250
- console.log(`⚠️ Service "${id}" is not registered.`);
20251
- return;
20252
- }
20253
- await getClient().agent.service.deregister(id);
20254
- registeredServices.delete(id);
20255
- console.log(`🛑 Service "${id}" deregistered successfully.`);
20256
20198
  }
20257
20199
 
20258
20200
  const BASE_LOG_DIR$2 = '/home/log';
@@ -20262,12 +20204,12 @@ async function ensureLogDir$2(dir) {
20262
20204
  await promises.mkdir(dir, { recursive: true });
20263
20205
  }
20264
20206
  function formatLogLine(targetService, instance, elapsedMs) {
20265
- var _a, _b, _c, _d, _e;
20207
+ var _a, _b, _c, _d;
20266
20208
  const ts = new Date().toISOString();
20267
- const addr = (_b = (_a = instance.ServiceAddress) !== null && _a !== void 0 ? _a : instance.Address) !== null && _b !== void 0 ? _b : 'unknown';
20268
- const port = (_c = instance.ServicePort) !== null && _c !== void 0 ? _c : 'unknown';
20269
- const node = (_d = instance.Node) !== null && _d !== void 0 ? _d : 'N/A';
20270
- const id = (_e = instance.ServiceID) !== null && _e !== void 0 ? _e : 'N/A';
20209
+ const addr = (_a = instance.address) !== null && _a !== void 0 ? _a : 'unknown';
20210
+ const port = (_b = instance.port) !== null && _b !== void 0 ? _b : 'unknown';
20211
+ const node = (_c = instance.Node) !== null && _c !== void 0 ? _c : 'N/A';
20212
+ const id = (_d = instance.ServiceID) !== null && _d !== void 0 ? _d : 'N/A';
20271
20213
  const elapsed = elapsedMs !== undefined ? ` | Elapsed=${elapsedMs}ms` : '';
20272
20214
  return `${ts} | Target=${targetService} | ${addr}:${port} | Node=${node} | ID=${id}${elapsed}\n`;
20273
20215
  }
@@ -20312,29 +20254,33 @@ function get(key) {
20312
20254
  return record.value;
20313
20255
  }
20314
20256
 
20315
- const CACHE_TTL = 60000;
20257
+ const CACHE_TTL = 60000; // 1 دقيقة
20258
+ // جلب كل الخدمات
20316
20259
  async function listServices() {
20317
20260
  const cacheKey = 'services:list';
20318
20261
  const cached = get(cacheKey);
20319
20262
  if (cached)
20320
20263
  return cached;
20321
- const services = await getClient().agent.service.list();
20322
- const values = Object.values(services);
20323
- set(cacheKey, values, CACHE_TTL);
20324
- return values;
20264
+ const services = await getAtlasClient().listServices();
20265
+ set(cacheKey, services, CACHE_TTL);
20266
+ return services;
20325
20267
  }
20268
+ // جلب كل الـ instances لسيرفس معين
20326
20269
  async function getServiceInstances(serviceName) {
20327
20270
  const cacheKey = `services:${serviceName}`;
20328
20271
  const cached = get(cacheKey);
20329
20272
  if (cached)
20330
20273
  return cached;
20331
20274
  const start = Date.now();
20332
- const services = await getClient().catalog.service.nodes(serviceName);
20275
+ const services = await getAtlasClient().listServices(); // هنجيب كل السيرفسات
20333
20276
  const elapsed = Date.now() - start;
20334
20277
  console.log(`⏱️ Discovery for ${serviceName} took ${elapsed}ms`);
20335
- set(cacheKey, services, CACHE_TTL);
20336
- return services;
20278
+ // فلترة السيرفس المطلوب
20279
+ const instances = services.filter(s => s.name === serviceName);
20280
+ set(cacheKey, instances, CACHE_TTL);
20281
+ return instances;
20337
20282
  }
20283
+ // جلب instance عشوائي لسيرفس معين
20338
20284
  async function getRandomServiceInstance(serviceName) {
20339
20285
  const cacheKey = `random:${serviceName}`;
20340
20286
  const cached = get(cacheKey);
@@ -20348,18 +20294,17 @@ async function getRandomServiceInstance(serviceName) {
20348
20294
  const instance = instances[randomIndex];
20349
20295
  const elapsed = Date.now() - start;
20350
20296
  await appendDiscoveryLog(serviceName, instance, elapsed);
20351
- // cache random instance
20352
20297
  set(cacheKey, instance, CACHE_TTL);
20353
20298
  return instance;
20354
20299
  }
20300
+ // جلب URL جاهز للاتصال
20355
20301
  async function getServiceUrl(serviceName) {
20356
20302
  const cacheKey = `url:${serviceName}`;
20357
20303
  const cached = get(cacheKey);
20358
20304
  if (cached)
20359
20305
  return cached;
20360
20306
  const instance = await getRandomServiceInstance(serviceName);
20361
- const url = `http://${instance.ServiceAddress}:${instance.ServicePort}`;
20362
- // cache url
20307
+ const url = `http://${instance.address}:${instance.port}`;
20363
20308
  set(cacheKey, url, CACHE_TTL);
20364
20309
  return url;
20365
20310
  }
@@ -20433,7 +20378,7 @@ async function callService(serviceName, options = {}) {
20433
20378
  const start = Date.now();
20434
20379
  try {
20435
20380
  const instance = await getRandomServiceInstance(serviceName);
20436
- const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
20381
+ const url = `http://${instance.address}:${instance.port}${path}`;
20437
20382
  const response = await axios.request({ url, method, data, headers, params });
20438
20383
  const elapsed = Date.now() - start;
20439
20384
  // 📌 سجل الكول
@@ -20620,5 +20565,5 @@ async function dataLink(data, schema) {
20620
20565
  return isArray ? result : result[0];
20621
20566
  }
20622
20567
 
20623
- export { callService, dataLink, deregisterService, getAtlasClient, getClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService, toBoolean };
20568
+ export { callService, dataLink, getAtlasClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService, toBoolean };
20624
20569
  //# sourceMappingURL=index.esm.js.map
@@ -1,2 +1,2 @@
1
- import { ServiceInstance } from '../consul/types';
2
- export declare function appendDiscoveryLog(targetService: string, instance: ServiceInstance, elapsedMs?: number): Promise<void>;
1
+ import { AtlasServiceInstance } from '../consul/types';
2
+ export declare function appendDiscoveryLog(targetService: string, instance: AtlasServiceInstance, elapsedMs?: number): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.74",
3
+ "version": "1.0.75",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",