node-consul-service 1.0.3 → 1.0.5

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
@@ -13,31 +13,55 @@ var require$$4$1 = require('assert');
13
13
  var zlib = require('zlib');
14
14
  var events = require('events');
15
15
 
16
+ const { CONSUL_HOST, CONSUL_PORT, CONSUL_SECURE } = process.env;
17
+ if (!CONSUL_HOST) {
18
+ throw new Error("Missing environment variable: CONSUL_HOST");
19
+ }
20
+ if (!CONSUL_PORT) {
21
+ throw new Error("Missing environment variable: CONSUL_PORT");
22
+ }
16
23
  const client = new Consul({
17
- host: process.env.CONSUL_HOST || "localhost",
18
- port: Number(process.env.CONSUL_PORT || 8500),
19
- secure: process.env.CONSUL_SECURE === "true",
24
+ host: CONSUL_HOST,
25
+ port: Number(CONSUL_PORT),
26
+ secure: CONSUL_SECURE === "true",
27
+ });
28
+ // اختبار الاتصال
29
+ client.agent.self((err) => {
30
+ if (err) {
31
+ console.error("❌ Failed to connect to Consul:", err.message);
32
+ }
33
+ else {
34
+ console.log("✅ Connected to Consul successfully");
35
+ }
20
36
  });
21
37
 
22
38
  const registeredServices = new Set();
23
- async function registerService(name, id, port, address = 'localhost') {
39
+ async function registerService(options) {
40
+ var _a, _b, _c, _d, _e;
41
+ const { name, id, port, address = 'localhost', check, tags, meta, } = options;
24
42
  if (registeredServices.has(id)) {
25
43
  console.log(`⚠️ Service "${id}" is already registered.`);
26
44
  return;
27
45
  }
28
- await client.agent.service.register({
46
+ const serviceDefinition = {
29
47
  name,
30
48
  id,
31
49
  address,
32
50
  port,
33
- check: {
34
- name: `${name}-check`,
35
- http: `http://${address}:${port}/health`,
36
- interval: '10s',
37
- timeout: '5s',
38
- deregistercriticalserviceafter: '1m',
39
- },
40
- });
51
+ tags,
52
+ meta,
53
+ };
54
+ // لو check مش موجود نضيف واحد افتراضي
55
+ if (check !== false) {
56
+ serviceDefinition.check = {
57
+ name: (_a = check === null || check === void 0 ? void 0 : check.name) !== null && _a !== void 0 ? _a : `${name}-check`,
58
+ http: (_b = check === null || check === void 0 ? void 0 : check.http) !== null && _b !== void 0 ? _b : `http://${address}:${port}/health`,
59
+ interval: (_c = check === null || check === void 0 ? void 0 : check.interval) !== null && _c !== void 0 ? _c : '10s',
60
+ timeout: (_d = check === null || check === void 0 ? void 0 : check.timeout) !== null && _d !== void 0 ? _d : '5s',
61
+ deregistercriticalserviceafter: (_e = check === null || check === void 0 ? void 0 : check.deregistercriticalserviceafter) !== null && _e !== void 0 ? _e : '1m',
62
+ };
63
+ }
64
+ await client.agent.service.register(serviceDefinition);
41
65
  registeredServices.add(id);
42
66
  console.log(`✅ Service "${id}" registered successfully.`);
43
67
  }
package/dist/index.esm.js CHANGED
@@ -11,31 +11,55 @@ import require$$4$1 from 'assert';
11
11
  import zlib from 'zlib';
12
12
  import { EventEmitter } from 'events';
13
13
 
14
+ const { CONSUL_HOST, CONSUL_PORT, CONSUL_SECURE } = process.env;
15
+ if (!CONSUL_HOST) {
16
+ throw new Error("Missing environment variable: CONSUL_HOST");
17
+ }
18
+ if (!CONSUL_PORT) {
19
+ throw new Error("Missing environment variable: CONSUL_PORT");
20
+ }
14
21
  const client = new Consul({
15
- host: process.env.CONSUL_HOST || "localhost",
16
- port: Number(process.env.CONSUL_PORT || 8500),
17
- secure: process.env.CONSUL_SECURE === "true",
22
+ host: CONSUL_HOST,
23
+ port: Number(CONSUL_PORT),
24
+ secure: CONSUL_SECURE === "true",
25
+ });
26
+ // اختبار الاتصال
27
+ client.agent.self((err) => {
28
+ if (err) {
29
+ console.error("❌ Failed to connect to Consul:", err.message);
30
+ }
31
+ else {
32
+ console.log("✅ Connected to Consul successfully");
33
+ }
18
34
  });
19
35
 
20
36
  const registeredServices = new Set();
21
- async function registerService(name, id, port, address = 'localhost') {
37
+ async function registerService(options) {
38
+ var _a, _b, _c, _d, _e;
39
+ const { name, id, port, address = 'localhost', check, tags, meta, } = options;
22
40
  if (registeredServices.has(id)) {
23
41
  console.log(`⚠️ Service "${id}" is already registered.`);
24
42
  return;
25
43
  }
26
- await client.agent.service.register({
44
+ const serviceDefinition = {
27
45
  name,
28
46
  id,
29
47
  address,
30
48
  port,
31
- check: {
32
- name: `${name}-check`,
33
- http: `http://${address}:${port}/health`,
34
- interval: '10s',
35
- timeout: '5s',
36
- deregistercriticalserviceafter: '1m',
37
- },
38
- });
49
+ tags,
50
+ meta,
51
+ };
52
+ // لو check مش موجود نضيف واحد افتراضي
53
+ if (check !== false) {
54
+ serviceDefinition.check = {
55
+ name: (_a = check === null || check === void 0 ? void 0 : check.name) !== null && _a !== void 0 ? _a : `${name}-check`,
56
+ http: (_b = check === null || check === void 0 ? void 0 : check.http) !== null && _b !== void 0 ? _b : `http://${address}:${port}/health`,
57
+ interval: (_c = check === null || check === void 0 ? void 0 : check.interval) !== null && _c !== void 0 ? _c : '10s',
58
+ timeout: (_d = check === null || check === void 0 ? void 0 : check.timeout) !== null && _d !== void 0 ? _d : '5s',
59
+ deregistercriticalserviceafter: (_e = check === null || check === void 0 ? void 0 : check.deregistercriticalserviceafter) !== null && _e !== void 0 ? _e : '1m',
60
+ };
61
+ }
62
+ await client.agent.service.register(serviceDefinition);
39
63
  registeredServices.add(id);
40
64
  console.log(`✅ Service "${id}" registered successfully.`);
41
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",