node-consul-service 1.0.44 → 1.0.46
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 +55 -9
- package/dist/index.cjs.js +23 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +22 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ A robust Node.js service that integrates with HashiCorp Consul for service disco
|
|
|
6
6
|
- [Features](#features)
|
|
7
7
|
- [Prerequisites](#prerequisites)
|
|
8
8
|
- [Installation](#installation)
|
|
9
|
-
- [
|
|
9
|
+
- [Initialization](#initialization)
|
|
10
10
|
- [API Documentation](#api-documentation)
|
|
11
11
|
- [Examples](#examples)
|
|
12
12
|
- [Contributing](#contributing)
|
|
@@ -35,16 +35,62 @@ npm install node-consul-service
|
|
|
35
35
|
yarn add node-consul-service
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
##
|
|
38
|
+
## Initialization
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
### Service Initialization
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
Before using any functions from the library, you must initialize the client first. You can initialize the client by passing configuration options:
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
const { initClient } = require('node-consul-service');
|
|
46
|
+
|
|
47
|
+
await initClient({
|
|
48
|
+
host: 'localhost',
|
|
49
|
+
port: 8500,
|
|
50
|
+
secure: false,
|
|
51
|
+
token: 'your-token', // Optional
|
|
52
|
+
datacenter: 'dc1', // Optional
|
|
53
|
+
retryAttempts: 3, // Number of connection retry attempts
|
|
54
|
+
retryDelay: 1000 // Delay between retries in milliseconds
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Initialization Features
|
|
59
|
+
|
|
60
|
+
- **Automatic Retry**: Attempts to connect multiple times on failure
|
|
61
|
+
- **Connection Verification**: Verifies successful connection before proceeding
|
|
62
|
+
- **State Management**: Tracks initialization state to prevent multiple initializations
|
|
63
|
+
- **Error Handling**: Clear and helpful error messages
|
|
64
|
+
|
|
65
|
+
### Complete Usage Example
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
const { initClient, registerService } = require('node-consul-service');
|
|
69
|
+
|
|
70
|
+
async function startService() {
|
|
71
|
+
try {
|
|
72
|
+
// Initialize client
|
|
73
|
+
await initClient({
|
|
74
|
+
host: 'localhost',
|
|
75
|
+
port: 8500,
|
|
76
|
+
retryAttempts: 3
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Register service
|
|
80
|
+
await registerService({
|
|
81
|
+
name: 'my-service',
|
|
82
|
+
id: 'my-service-1',
|
|
83
|
+
port: 3000
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
console.log('✅ Service started successfully');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error('❌ Failed to start service:', error);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
startService();
|
|
48
94
|
```
|
|
49
95
|
|
|
50
96
|
## API Documentation
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
require('consul');
|
|
3
|
+
var Consul = require('consul');
|
|
4
4
|
var require$$1 = require('util');
|
|
5
5
|
var stream = require('stream');
|
|
6
6
|
var require$$1$1 = require('path');
|
|
@@ -14,10 +14,29 @@ var zlib = require('zlib');
|
|
|
14
14
|
var events = require('events');
|
|
15
15
|
|
|
16
16
|
// src/lib/client.ts
|
|
17
|
+
let consulClient = null;
|
|
18
|
+
function initClient(config) {
|
|
19
|
+
var _a;
|
|
20
|
+
consulClient = new Consul({
|
|
21
|
+
host: config.host,
|
|
22
|
+
port: config.port,
|
|
23
|
+
secure: (_a = config.secure) !== null && _a !== void 0 ? _a : true,
|
|
24
|
+
});
|
|
25
|
+
// optional test connection
|
|
26
|
+
consulClient.agent.self((err) => {
|
|
27
|
+
if (err) {
|
|
28
|
+
console.error("❌ Failed to connect to Consul:", err.message);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
console.log("✅ Connected to Consul successfully");
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
17
35
|
function getClient() {
|
|
18
|
-
{
|
|
36
|
+
if (!consulClient) {
|
|
19
37
|
throw new Error("Consul client not initialized. Call initClient() first.");
|
|
20
38
|
}
|
|
39
|
+
return consulClient;
|
|
21
40
|
}
|
|
22
41
|
|
|
23
42
|
const registeredServices = new Set();
|
|
@@ -19156,9 +19175,11 @@ exports.DataLinkError = DataLinkError;
|
|
|
19156
19175
|
exports.callService = callService;
|
|
19157
19176
|
exports.dataLink = dataLink;
|
|
19158
19177
|
exports.deregisterService = deregisterService;
|
|
19178
|
+
exports.getClient = getClient;
|
|
19159
19179
|
exports.getRandomServiceInstance = getRandomServiceInstance;
|
|
19160
19180
|
exports.getServiceInstances = getServiceInstances;
|
|
19161
19181
|
exports.getServiceUrl = getServiceUrl;
|
|
19182
|
+
exports.initClient = initClient;
|
|
19162
19183
|
exports.listServices = listServices;
|
|
19163
19184
|
exports.registerService = registerService;
|
|
19164
19185
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import 'consul';
|
|
1
|
+
import Consul from 'consul';
|
|
2
2
|
import require$$1 from 'util';
|
|
3
3
|
import stream, { Readable } from 'stream';
|
|
4
4
|
import require$$1$1 from 'path';
|
|
@@ -12,10 +12,29 @@ import zlib from 'zlib';
|
|
|
12
12
|
import { EventEmitter } from 'events';
|
|
13
13
|
|
|
14
14
|
// src/lib/client.ts
|
|
15
|
+
let consulClient = null;
|
|
16
|
+
function initClient(config) {
|
|
17
|
+
var _a;
|
|
18
|
+
consulClient = new Consul({
|
|
19
|
+
host: config.host,
|
|
20
|
+
port: config.port,
|
|
21
|
+
secure: (_a = config.secure) !== null && _a !== void 0 ? _a : true,
|
|
22
|
+
});
|
|
23
|
+
// optional test connection
|
|
24
|
+
consulClient.agent.self((err) => {
|
|
25
|
+
if (err) {
|
|
26
|
+
console.error("❌ Failed to connect to Consul:", err.message);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
console.log("✅ Connected to Consul successfully");
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
15
33
|
function getClient() {
|
|
16
|
-
{
|
|
34
|
+
if (!consulClient) {
|
|
17
35
|
throw new Error("Consul client not initialized. Call initClient() first.");
|
|
18
36
|
}
|
|
37
|
+
return consulClient;
|
|
19
38
|
}
|
|
20
39
|
|
|
21
40
|
const registeredServices = new Set();
|
|
@@ -19150,5 +19169,5 @@ async function dataLink(data, schema) {
|
|
|
19150
19169
|
return isArray ? result : result[0];
|
|
19151
19170
|
}
|
|
19152
19171
|
|
|
19153
|
-
export { DataLinkError, callService, dataLink, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
|
|
19172
|
+
export { DataLinkError, callService, dataLink, deregisterService, getClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService };
|
|
19154
19173
|
//# sourceMappingURL=index.esm.js.map
|