@brimble/consul 0.0.1

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.
Files changed (54) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +2423 -0
  3. package/lib/acl/legacy.d.ts +75 -0
  4. package/lib/acl/legacy.js +167 -0
  5. package/lib/acl.d.ts +37 -0
  6. package/lib/acl.js +49 -0
  7. package/lib/agent/check.d.ts +113 -0
  8. package/lib/agent/check.js +164 -0
  9. package/lib/agent/service.d.ts +63 -0
  10. package/lib/agent/service.js +113 -0
  11. package/lib/agent.d.ts +77 -0
  12. package/lib/agent.js +162 -0
  13. package/lib/catalog/connect.d.ts +11 -0
  14. package/lib/catalog/connect.js +37 -0
  15. package/lib/catalog/node.d.ts +30 -0
  16. package/lib/catalog/node.js +57 -0
  17. package/lib/catalog/service.d.ts +32 -0
  18. package/lib/catalog/service.js +61 -0
  19. package/lib/catalog.d.ts +118 -0
  20. package/lib/catalog.js +113 -0
  21. package/lib/constants.js +21 -0
  22. package/lib/consul.d.ts +93 -0
  23. package/lib/consul.js +98 -0
  24. package/lib/errors.js +29 -0
  25. package/lib/event.d.ts +43 -0
  26. package/lib/event.js +91 -0
  27. package/lib/health.d.ts +77 -0
  28. package/lib/health.js +126 -0
  29. package/lib/index.d.ts +18 -0
  30. package/lib/index.js +3 -0
  31. package/lib/intention.d.ts +73 -0
  32. package/lib/intention.js +180 -0
  33. package/lib/kv.d.ts +80 -0
  34. package/lib/kv.js +180 -0
  35. package/lib/query.d.ts +84 -0
  36. package/lib/query.js +244 -0
  37. package/lib/resolver/algorithms.js +127 -0
  38. package/lib/resolver/dns.js +182 -0
  39. package/lib/resolver/health.js +51 -0
  40. package/lib/resolver/metrics.js +199 -0
  41. package/lib/resolver/scoring.js +95 -0
  42. package/lib/resolver/types.js +28 -0
  43. package/lib/resolver.d.ts +76 -0
  44. package/lib/resolver.js +290 -0
  45. package/lib/session.d.ts +92 -0
  46. package/lib/session.js +164 -0
  47. package/lib/status.d.ts +19 -0
  48. package/lib/status.js +43 -0
  49. package/lib/transaction.d.ts +50 -0
  50. package/lib/transaction.js +58 -0
  51. package/lib/utils.js +655 -0
  52. package/lib/watch.d.ts +22 -0
  53. package/lib/watch.js +183 -0
  54. package/package.json +55 -0
package/README.md ADDED
@@ -0,0 +1,2423 @@
1
+ > [!CAUTION]
2
+ > This project is no longer maintained.
3
+
4
+ # Consul
5
+
6
+ This is a [Consul][consul] client.
7
+
8
+ - [Documentation](#documentation)
9
+ - [License](#license)
10
+
11
+ ## Documentation
12
+
13
+ See the official [HTTP API][consul-docs-api] docs for more information.
14
+
15
+ - [Consul](#init)
16
+ - [Common Method Call Options](#common-options)
17
+
18
+ * [ACL](#acl)
19
+ - [Legacy](#acl-legacy)
20
+ * [Agent](#agent)
21
+ - [Check](#agent-check)
22
+ - [Service](#agent-service)
23
+ * [Catalog](#catalog)
24
+ - [Connect](#catalog-connect)
25
+ - [Node](#catalog-node)
26
+ - [Service](#catalog-service)
27
+ * [Event](#event)
28
+ * [Health](#health)
29
+ * [Intention](#intention)
30
+ * [KV](#kv)
31
+ * [Query](#query)
32
+ * [Resolver](#resolver)
33
+ * [Session](#session)
34
+ * [Status](#status)
35
+ * [Transaction](#transaction)
36
+ * [Watch](#watch)
37
+
38
+ <a id="init"></a>
39
+
40
+ ### Consul([options])
41
+
42
+ Initialize a new Consul client.
43
+
44
+ Options
45
+
46
+ - host (String, default: 127.0.0.1): agent address
47
+ - port (Integer, default: 8500): agent HTTP(S) port
48
+ - secure (Boolean, default: false): enable HTTPS
49
+ - defaults (Object, optional): common method call options that will be included with every call (ex: set default `token`), these options can be override on a per call basis
50
+
51
+ Advanced options
52
+
53
+ - agent (http.Agent|https.Agent, optionals): if not set uses the global agent
54
+ - baseUrl, headers, tags, socketPath, and timeout (see [Papi](https://github.com/silas/node-papi/blob/main/README.md#client) for details)
55
+ - tls options: ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, and sessionIdContext (see [Node.js docs](https://nodejs.org/dist/latest/docs/api/tls.html#tls_tls_connect_options_callback) for details)
56
+
57
+ Usage
58
+
59
+ ```javascript
60
+ import Consul from "consul";
61
+
62
+ const consul = new Consul();
63
+ ```
64
+
65
+ <a id="common-options"></a>
66
+
67
+ ### Common Method Call Options
68
+
69
+ These options can be included with any method call, although only certain endpoints support them. See the [HTTP API][consul-docs-api] for more information.
70
+
71
+ - dc (String, optional): datacenter (defaults to local for agent)
72
+ - partition (String, optional): partition (defaults to 'default' partition)
73
+ - wan (Boolean, default: false): return WAN members instead of LAN members
74
+ - consistent (Boolean, default: false): require strong consistency
75
+ - stale (Boolean, default: false): use whatever is available, can be arbitrarily stale
76
+ - index (String, optional): used with `ModifyIndex` to block and wait for changes
77
+ - wait (String, optional): limit how long to wait for changes (ex: `5m`), used with index
78
+ - token (String, optional): ACL token
79
+ - near (String, optional): used to sort the node list in ascending order based on the estimated round trip time from that node
80
+ - node-meta (String[], optional): used to specify a desired node metadata key/value pair of the form key:value
81
+ - filter (String, optional): used to [refine a data query](https://www.consul.io/api/features/filtering.html) for some API listing endpoints
82
+
83
+ These options work for all methods.
84
+
85
+ - ctx (EventEmitter, optional): emit `cancel` to abort request
86
+ - timeout (Number|String, optional): number of milliseconds before request is aborted (ex: `1000` or `1s`)
87
+
88
+ <a id="acl"></a>
89
+
90
+ ### consul.acl
91
+
92
+ - [bootstrap](#acl-bootstrap)
93
+ - [legacy](#acl-legacy)
94
+ - [replication](#acl-replication)
95
+
96
+ <a id="acl-bootstrap"></a>
97
+
98
+ ### consul.acl.bootstrap()
99
+
100
+ Creates one-time management token if not configured.
101
+
102
+ Usage
103
+
104
+ ```javascript
105
+ await consul.acl.bootstrap();
106
+ ```
107
+
108
+ Result
109
+
110
+ ```json
111
+ {
112
+ "ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
113
+ }
114
+ ```
115
+
116
+ <a id="acl-replication"></a>
117
+
118
+ ### consul.acl.replication([options])
119
+
120
+ Get the status of the ACL replication process in the datacenter.
121
+
122
+ Usage
123
+
124
+ ```javascript
125
+ await consul.acl.replication();
126
+ ```
127
+
128
+ Result
129
+
130
+ ```json
131
+ {
132
+ "Enabled": true,
133
+ "Running": true,
134
+ "SourceDatacenter": "dc1",
135
+ "ReplicatedIndex": 1976,
136
+ "LastSuccess": "2016-08-05T06:28:58Z",
137
+ "LastError": "2016-08-05T06:28:28Z"
138
+ }
139
+ ```
140
+
141
+ <a id="acl-legacy"></a>
142
+
143
+ ### consul.acl.legacy
144
+
145
+ - [create](#acl-legacy-create)
146
+ - [update](#acl-legacy-update)
147
+ - [destroy](#acl-legacy-destroy)
148
+ - [get](#acl-legacy-get)
149
+ - [clone](#acl-legacy-clone)
150
+ - [list](#acl-legacy-list)
151
+
152
+ <a id="acl-legacy-create"></a>
153
+
154
+ ### consul.acl.legacy.create([options])
155
+
156
+ Creates a new token with policy.
157
+
158
+ Options
159
+
160
+ - name (String, optional): human readable name for the token
161
+ - type (String, enum: client, management; default: client): type of token
162
+ - rules (String, optional): string encoded HCL or JSON
163
+
164
+ Usage
165
+
166
+ ```javascript
167
+ await consul.acl.legacy.create();
168
+ ```
169
+
170
+ Result
171
+
172
+ ```json
173
+ {
174
+ "ID": "b1f4c10e-b61b-e1de-de95-218c9fefdd3e"
175
+ }
176
+ ```
177
+
178
+ <a id="acl-legacy-update"></a>
179
+
180
+ ### consul.acl.legacy.update(options)
181
+
182
+ Update the policy of a token.
183
+
184
+ Options
185
+
186
+ - id (String): token ID
187
+ - name (String, optional): human readable name for the token
188
+ - type (String, enum: client, management; default: client): type of token
189
+ - rules (String, optional): string encoded HCL or JSON
190
+
191
+ Usage
192
+
193
+ ```javascript
194
+ await consul.acl.legacy.update({
195
+ id: "63e1d82e-f718-eb92-3b7d-61f0c71d45b4",
196
+ name: "test",
197
+ });
198
+ ```
199
+
200
+ <a id="acl-legacy-destroy"></a>
201
+
202
+ ### consul.acl.legacy.destroy(options)
203
+
204
+ Destroys a given token.
205
+
206
+ Options
207
+
208
+ - id (String): token ID
209
+
210
+ Usage
211
+
212
+ ```javascript
213
+ await consul.acl.legacy.destroy("b1f4c10e-b61b-e1de-de95-218c9fefdd3e");
214
+ ```
215
+
216
+ <a id="acl-legacy-get"></a>
217
+
218
+ ### consul.acl.legacy.get(options)
219
+
220
+ Queries the policy of a given token.
221
+
222
+ Options
223
+
224
+ - id (String): token ID
225
+
226
+ Usage
227
+
228
+ ```javascript
229
+ await consul.acl.legacy.get("63e1d82e-f718-eb92-3b7d-61f0c71d45b4");
230
+ ```
231
+
232
+ Result
233
+
234
+ ```json
235
+ {
236
+ "CreateIndex": 7,
237
+ "ModifyIndex": 7,
238
+ "ID": "63e1d82e-f718-eb92-3b7d-61f0c71d45b4",
239
+ "Name": "Read only",
240
+ "Type": "client",
241
+ "Rules": "{\"key\":{\"\":{\"policy\":\"read\"}}}"
242
+ }
243
+ ```
244
+
245
+ <a id="acl-legacy-clone"></a>
246
+
247
+ ### consul.acl.legacy.clone(options)
248
+
249
+ Creates a new token by cloning an existing token.
250
+
251
+ Options
252
+
253
+ - id (String): token ID
254
+
255
+ Usage
256
+
257
+ ```javascript
258
+ await consul.acl.legacy.clone("63e1d82e-f718-eb92-3b7d-61f0c71d45b4");
259
+ ```
260
+
261
+ Result
262
+
263
+ ```json
264
+ {
265
+ "ID": "9fb8b20b-2636-adbb-9b99-d879df3305ec"
266
+ }
267
+ ```
268
+
269
+ <a id="acl-legacy-list"></a>
270
+
271
+ ### consul.acl.legacy.list([options])
272
+
273
+ Lists all the active tokens.
274
+
275
+ Usage
276
+
277
+ ```javascript
278
+ await consul.acl.legacy.list();
279
+ ```
280
+
281
+ Result
282
+
283
+ ```json
284
+ [
285
+ {
286
+ "CreateIndex": 2,
287
+ "ModifyIndex": 2,
288
+ "ID": "anonymous",
289
+ "Name": "Anonymous Token",
290
+ "Type": "client",
291
+ "Rules": ""
292
+ }
293
+ {
294
+ "CreateIndex": 3,
295
+ "ModifyIndex": 3,
296
+ "ID": "root",
297
+ "Name": "Master Token",
298
+ "Type": "management",
299
+ "Rules": ""
300
+ }
301
+ ]
302
+ ```
303
+
304
+ <a id="agent"></a>
305
+
306
+ ### consul.agent
307
+
308
+ - [check](#agent-check)
309
+ - [service](#agent-service)
310
+ - [members](#agent-members)
311
+ - [reload](#agent-reload)
312
+ - [self](#agent-self)
313
+ - [maintenance](#agent-maintenance)
314
+ - [join](#agent-join)
315
+ - [forceLeave](#agent-force-leave)
316
+
317
+ <a id="agent-members"></a>
318
+
319
+ ### consul.agent.members([options])
320
+
321
+ Returns the members as seen by the consul agent.
322
+
323
+ Options
324
+
325
+ - wan (Boolean, default: false): return WAN members instead of LAN members
326
+
327
+ Usage
328
+
329
+ ```javascript
330
+ await consul.agent.members();
331
+ ```
332
+
333
+ Result
334
+
335
+ ```json
336
+ [
337
+ {
338
+ "Name": "node1",
339
+ "Addr": "127.0.0.1",
340
+ "Port": 8301,
341
+ "Tags": {
342
+ "bootstrap": "1",
343
+ "build": "0.3.0:441d613e",
344
+ "dc": "dc1",
345
+ "port": "8300",
346
+ "role": "consul",
347
+ "vsn": "2",
348
+ "vsn_max": "2",
349
+ "vsn_min": "1"
350
+ },
351
+ "Status": 1,
352
+ "ProtocolMin": 1,
353
+ "ProtocolMax": 2,
354
+ "ProtocolCur": 2,
355
+ "DelegateMin": 2,
356
+ "DelegateMax": 4,
357
+ "DelegateCur": 4
358
+ }
359
+ ]
360
+ ```
361
+
362
+ <a id="agent-reload"></a>
363
+
364
+ ### consul.agent.reload([options])
365
+
366
+ Reload agent configuration.
367
+
368
+ Usage
369
+
370
+ ```javascript
371
+ await consul.agent.reload();
372
+ ```
373
+
374
+ <a id="agent-self"></a>
375
+
376
+ ### consul.agent.self()
377
+
378
+ Returns the agent node configuration.
379
+
380
+ Usage
381
+
382
+ ```javascript
383
+ await consul.agent.self();
384
+ ```
385
+
386
+ Result
387
+
388
+ ```json
389
+ {
390
+ "Config": {
391
+ "Bootstrap": true,
392
+ "Server": true,
393
+ "Datacenter": "dc1",
394
+ "DataDir": "/tmp/node1/data",
395
+ "DNSRecursor": "",
396
+ "DNSConfig": {
397
+ "NodeTTL": 0,
398
+ "ServiceTTL": null,
399
+ "AllowStale": false,
400
+ "MaxStale": 5000000000
401
+ },
402
+ "Domain": "consul.",
403
+ "LogLevel": "INFO",
404
+ "NodeName": "node1",
405
+ "ClientAddr": "127.0.0.1",
406
+ "BindAddr": "127.0.0.1",
407
+ "AdvertiseAddr": "127.0.0.1",
408
+ "Ports": {
409
+ "DNS": 8600,
410
+ "HTTP": 8500,
411
+ "RPC": 8400,
412
+ "SerfLan": 8301,
413
+ "SerfWan": 8302,
414
+ "Server": 8300
415
+ },
416
+ "LeaveOnTerm": false,
417
+ "SkipLeaveOnInt": false,
418
+ "StatsiteAddr": "",
419
+ "Protocol": 2,
420
+ "EnableDebug": false,
421
+ "VerifyIncoming": false,
422
+ "VerifyOutgoing": false,
423
+ "CAFile": "",
424
+ "CertFile": "",
425
+ "KeyFile": "",
426
+ "ServerName": "",
427
+ "StartJoin": [],
428
+ "UiDir": "",
429
+ "PidFile": "/tmp/node1/pid",
430
+ "EnableSyslog": false,
431
+ "SyslogFacility": "LOCAL0",
432
+ "RejoinAfterLeave": false,
433
+ "CheckUpdateInterval": 300000000000,
434
+ "Revision": "441d613e1bd96254c78c46ee7c1b35c161fc7295+CHANGES",
435
+ "Version": "0.3.0",
436
+ "VersionPrerelease": ""
437
+ },
438
+ "Member": {
439
+ "Name": "node1",
440
+ "Addr": "127.0.0.1",
441
+ "Port": 8301,
442
+ "Tags": {
443
+ "bootstrap": "1",
444
+ "build": "0.3.0:441d613e",
445
+ "dc": "dc1",
446
+ "port": "8300",
447
+ "role": "consul",
448
+ "vsn": "2",
449
+ "vsn_max": "2",
450
+ "vsn_min": "1"
451
+ },
452
+ "Status": 1,
453
+ "ProtocolMin": 1,
454
+ "ProtocolMax": 2,
455
+ "ProtocolCur": 2,
456
+ "DelegateMin": 2,
457
+ "DelegateMax": 4,
458
+ "DelegateCur": 4
459
+ }
460
+ }
461
+ ```
462
+
463
+ <a id="agent-maintenance"></a>
464
+
465
+ ### consul.agent.maintenance(options)
466
+
467
+ Set node maintenance mode.
468
+
469
+ Options
470
+
471
+ - enable (Boolean): maintenance mode enabled
472
+ - reason (String, optional): human readable reason for maintenance
473
+
474
+ Usage
475
+
476
+ ```javascript
477
+ await consul.agent.maintenance(true);
478
+ ```
479
+
480
+ <a id="agent-join"></a>
481
+
482
+ ### consul.agent.join(options)
483
+
484
+ Trigger agent to join a node.
485
+
486
+ Options
487
+
488
+ - address (String): node IP address to join
489
+ - wan (Boolean, default false): attempt to join using the WAN pool
490
+
491
+ Usage
492
+
493
+ ```javascript
494
+ await consul.agent.join("127.0.0.2");
495
+ ```
496
+
497
+ <a id="agent-force-leave"></a>
498
+
499
+ ### consul.agent.forceLeave(options)
500
+
501
+ Force remove node.
502
+
503
+ Options
504
+
505
+ - node (String): node name to remove
506
+
507
+ Usage
508
+
509
+ ```javascript
510
+ await consul.agent.forceLeave("node2");
511
+ ```
512
+
513
+ <a id="agent-check"></a>
514
+
515
+ ### consul.agent.check
516
+
517
+ - [list](#agent-check-list)
518
+ - [register](#agent-check-register)
519
+ - [deregister](#agent-check-deregister)
520
+ - [pass](#agent-check-pass)
521
+ - [warn](#agent-check-warn)
522
+ - [fail](#agent-check-fail)
523
+
524
+ <a id="agent-check-list"></a>
525
+
526
+ ### consul.agent.check.list()
527
+
528
+ Returns the checks the agent is managing.
529
+
530
+ Usage
531
+
532
+ ```javascript
533
+ await consul.agent.check.list();
534
+ ```
535
+
536
+ Result
537
+
538
+ ```json
539
+ {
540
+ "example": {
541
+ "Node": "node1",
542
+ "CheckID": "example",
543
+ "Name": "example",
544
+ "Status": "passing",
545
+ "Notes": "This is an example check.",
546
+ "Output": "",
547
+ "ServiceID": "",
548
+ "ServiceName": ""
549
+ }
550
+ }
551
+ ```
552
+
553
+ <a id="agent-check-register"></a>
554
+
555
+ ### consul.agent.check.register(options)
556
+
557
+ Registers a new check.
558
+
559
+ Options
560
+
561
+ - name (String): check name
562
+ - id (String, optional): check ID
563
+ - serviceid (String, optional): service ID, associate check with existing service
564
+ - http (String): url to test, 2xx passes, 429 warns, and all others fail
565
+ - tlsskipverify (Boolean, default: false): skip HTTPS verification
566
+ - tcp (String): host:port to test, passes if connection is established, fails otherwise
567
+ - args (String[]): path to check script, requires interval
568
+ - script (String): path to check script, requires interval (DEPRECATED)
569
+ - dockercontainerid (String, optional): Docker container ID to run script
570
+ - grpc (String, optional): gRPC endpoint (ex: `127.0.0.1:12345`)
571
+ - grpcusetls (Boolean, optional): enable TLS for gRPC check
572
+ - shell (String, optional): shell in which to run script (currently only supported with Docker)
573
+ - interval (String): interval to run check, requires script (ex: `15s`)
574
+ - timeout (String, optional): timeout for the check (ex: `10s`)
575
+ - ttl (String): time to live before check must be updated (ex: `60s`)
576
+ - aliasnode (String): ID of a node for an alias check (ex: `web1`)
577
+ - aliasservice (String): ID of a service for an alias check (ex: `web`)
578
+ - notes (String, optional): human readable description of check
579
+ - status (String, optional): initial service status
580
+ - deregistercriticalserviceafter (String, optional, Consul 0.7+): timeout after
581
+ which to automatically deregister service if check remains in critical state
582
+ - successbeforepassing (Number, optional): number of consecutive successful
583
+ results required before check status transitions to passing
584
+ - failuresbeforecritical (Number, optional): number of consecutive unsuccessful
585
+ results required before check status transitions to critical
586
+
587
+ Usage
588
+
589
+ ```javascript
590
+ await consul.agent.check.register({
591
+ name: "example",
592
+ ttl: "15s",
593
+ notes: "This is an example check.",
594
+ });
595
+ ```
596
+
597
+ <a id="agent-check-deregister"></a>
598
+
599
+ ### consul.agent.check.deregister(options)
600
+
601
+ Deregister a check.
602
+
603
+ Options
604
+
605
+ - id (String): check ID
606
+
607
+ Usage
608
+
609
+ ```javascript
610
+ await consul.agent.check.deregister("example");
611
+ ```
612
+
613
+ <a id="agent-check-pass"></a>
614
+
615
+ ### consul.agent.check.pass(options)
616
+
617
+ Mark a test as passing.
618
+
619
+ Options
620
+
621
+ - id (String): check ID
622
+ - note (String, optional): human readable message
623
+
624
+ Usage
625
+
626
+ ```javascript
627
+ await consul.agent.check.pass("example");
628
+ ```
629
+
630
+ <a id="agent-check-warn"></a>
631
+
632
+ ### consul.agent.check.warn(options)
633
+
634
+ Mark a test as warning.
635
+
636
+ Options
637
+
638
+ - id (String): check ID
639
+ - note (String, optional): human readable message
640
+
641
+ Usage
642
+
643
+ ```javascript
644
+ await consul.agent.check.warn("example");
645
+ ```
646
+
647
+ <a id="agent-check-fail"></a>
648
+
649
+ ### consul.agent.check.fail(options)
650
+
651
+ Mark a test as critical.
652
+
653
+ Options
654
+
655
+ - id (String): check ID
656
+ - note (String, optional): human readable message
657
+
658
+ Usage
659
+
660
+ ```javascript
661
+ await consul.agent.check.fail("example");
662
+ ```
663
+
664
+ <a id="agent-service"></a>
665
+
666
+ ### consul.agent.service
667
+
668
+ - [list](#agent-service-list)
669
+ - [register](#agent-service-register)
670
+ - [deregister](#agent-service-deregister)
671
+ - [maintenance](#agent-service-maintenance)
672
+
673
+ <a id="agent-service-list"></a>
674
+
675
+ ### consul.agent.service.list()
676
+
677
+ Returns the services the agent is managing.
678
+
679
+ Usage
680
+
681
+ ```javascript
682
+ await consul.agent.service.list();
683
+ ```
684
+
685
+ Result
686
+
687
+ ```json
688
+ {
689
+ "example": {
690
+ "ID": "example",
691
+ "Service": "example",
692
+ "Tags": ["dev", "web"],
693
+ "Port": 80
694
+ }
695
+ }
696
+ ```
697
+
698
+ <a id="agent-service-register"></a>
699
+
700
+ ### consul.agent.service.register(options)
701
+
702
+ Registers a new service.
703
+
704
+ Options
705
+
706
+ - name (String): service name
707
+ - id (String, optional): service ID
708
+ - tags (String[], optional): service tags
709
+ - address (String, optional): service IP address
710
+ - port (Integer, optional): service port
711
+ - meta (Object, optional): metadata linked to the service instance
712
+ - check (Object, optional): service check
713
+ - http (String): URL endpoint, requires interval
714
+ - tcp (String): host:port to test, passes if connection is established, fails otherwise
715
+ - script (String): path to check script, requires interval
716
+ - dockercontainerid (String, optional): Docker container ID to run script
717
+ - shell (String, optional): shell in which to run script (currently only supported with Docker)
718
+ - interval (String): interval to run check, requires script (ex: `15s`)
719
+ - timeout (String, optional): timeout for the check (ex: `10s`)
720
+ - ttl (String): time to live before check must be updated, instead of http/tcp/script and interval (ex: `60s`)
721
+ - notes (String, optional): human readable description of check
722
+ - status (String, optional): initial service status
723
+ - deregistercriticalserviceafter (String, optional, Consul 0.7+): timeout after
724
+ which to automatically deregister service if check remains in critical state
725
+ - checks (Object[], optional): service checks (see `check` above)
726
+ - connect (Object, optional): specifies the [configuration](https://www.consul.io/api/agent/service.html#connect-structure) for Connect
727
+ - proxy (Object, optional): specifies the [configuration](https://www.consul.io/docs/connect/registration/service-registration.html) for a Connect proxy instance
728
+ - taggedAddresses (Object, optional): specifies a map of explicit LAN and WAN addresses for the service instance
729
+
730
+ Usage
731
+
732
+ ```javascript
733
+ await consul.agent.service.register("example");
734
+ ```
735
+
736
+ <a id="agent-service-deregister"></a>
737
+
738
+ ### consul.agent.service.deregister(options)
739
+
740
+ Deregister a service.
741
+
742
+ Options
743
+
744
+ - id (String): service ID
745
+
746
+ Usage
747
+
748
+ ```javascript
749
+ await consul.agent.service.deregister("example");
750
+ ```
751
+
752
+ <a id="agent-service-maintenance"></a>
753
+
754
+ ### consul.agent.service.maintenance(options)
755
+
756
+ Set service maintenance mode.
757
+
758
+ Options
759
+
760
+ - id (String): service ID
761
+ - enable (Boolean): maintenance mode enabled
762
+ - reason (String, optional): human readable reason for maintenance
763
+
764
+ Usage
765
+
766
+ ```javascript
767
+ await consul.agent.service.maintenance({ id: "example", enable: true });
768
+ ```
769
+
770
+ <a id="catalog"></a>
771
+
772
+ ### consul.catalog
773
+
774
+ - [register](#catalog-register)
775
+ - [deregister](#catalog-deregister)
776
+ - [datacenters](#catalog-datacenters)
777
+ - [connect](#catalog-connect)
778
+ - [node](#catalog-node)
779
+ - [service](#catalog-service)
780
+
781
+ <a id="catalog-register"></a>
782
+
783
+ ### consul.catalog.register(options)
784
+
785
+ Registers or updates entries in the catalog.
786
+
787
+ NOTE: this endpoint is a low-level mechanism for registering or updating entries in the catalog. It is usually preferable to instead use the agent endpoints for registration as they are simpler and perform anti-entropy. It is suggested to read the [catalog API](https://developer.hashicorp.com/consul/api-docs/catalog) documentation before using that.
788
+
789
+ Options
790
+
791
+ - id (String, optional): an optional UUID to assign to the node. This must be a 36-character UUID-formatted string
792
+ - node (String, required): specifies the node ID to register
793
+ - address (String, required): specifies the address to register.
794
+ - taggedaddresses (Object, optional): specifies the tagged addresses
795
+ - nodemeta (Object, optional): specifies arbitrary KV metadata pairs for filtering purposes
796
+ - service (Objet, optional): specifies to register a service
797
+ - id (String): service ID. If ID is not provided, it will be defaulted to the value of the Service.Service property.
798
+ Only one service with a given ID may be present per node.
799
+ - service (String): service name
800
+ - tags (String[], optional): service tags
801
+ - meta (Object, optional): metadata linked to the service instance
802
+ - address (String): service IP address
803
+ - port (Integer): service port
804
+ - check (Object, optional): specifies to register a check.The register API manipulates the health check entry in the Catalog, but it does not setup the
805
+ TCP/HTTP check to monitor the node's health.
806
+ - node (String): the node id this check will bind to
807
+ - name (String): check name
808
+ - checkid (String): the CheckID can be omitted and will default to the value of Name. The CheckID must be unique on this node.
809
+ - serviceid (String): if a ServiceID is provided that matches the ID of a service on that node, the check is treated as a service level health check, instead of a node level health check.
810
+ - notes (String): notes is an opaque field that is meant to hold human-readable text
811
+ - status (String): initial status. The Status must be one of `passing`, `warning`, or `critical`.
812
+ - definition (Object): health check definition
813
+ - http (String): URL endpoint, requires interval
814
+ - tlsskipverify (Boolean, default: false): skip HTTPS verification
815
+ - tlsservername (String): SNI
816
+ - tcp (String): host:port to test, passes if connection is established, fails otherwise
817
+ - intervalduration (String): interval to run check, requires script (ex: `15s`)
818
+ - timeoutduration (String): timeout for the check (ex: `10s`)
819
+ - deregistercriticalserviceafterduration (String): timeout after
820
+ which to automatically deregister service if check remains in critical state (ex: `120s`)
821
+ - checks (Object[], optional): multiple checks can be provided by replacing `check` with `checks` and sending an array of `check` objects.
822
+ - skipnodeupdate (Bool, optional): pecifies whether to skip updating the node's information in the registration. Note, if the parameter is enabled for a node that doesn't exist, it will still be created
823
+
824
+ Usage
825
+
826
+ ```javascript
827
+ await consul.catalog.register("example");
828
+ ```
829
+
830
+ <a id="catalog-deregister"></a>
831
+
832
+ ### consul.catalog.deregister(options)
833
+
834
+ Deregister entries in the catalog.
835
+
836
+ NOTE:This endpoint is a low-level mechanism for directly removing entries from the Catalog. It is usually preferable to instead use the agent endpoints for deregistration as they are simpler and perform anti-entropy. It is suggested to read the [catalog API](https://developer.hashicorp.com/consul/api-docs/catalog) documentation before using that.
837
+
838
+ Options
839
+
840
+ - node (String, required): specifies the ID of the node. If no other values are provided, this node, all its services, and all its checks are removed.
841
+ - checkid (String, optional): specifies the ID of the check to remove.
842
+ - serviceid (String, optional): specifies the ID of the service to remove. The service and all associated checks will be removed.
843
+
844
+ Usage
845
+
846
+ ```javascript
847
+ await consul.catalog.deregister("example");
848
+ ```
849
+
850
+ or
851
+
852
+ ```javascript
853
+ await consul.catalog.deregister({ id: "example" });
854
+ ```
855
+
856
+ <a id="catalog-datacenters"></a>
857
+
858
+ ### consul.catalog.datacenters()
859
+
860
+ Lists known datacenters.
861
+
862
+ Usage
863
+
864
+ ```javascript
865
+ await consul.catalog.datacenters();
866
+ ```
867
+
868
+ Result
869
+
870
+ ```json
871
+ ["dc1"]
872
+ ```
873
+
874
+ <a id="catalog-connect"></a>
875
+
876
+ ### consul.catalog.connect
877
+
878
+ - [nodes](#catalog-connect-nodes)
879
+
880
+ <a id="catalog-connect-nodes"></a>
881
+
882
+ ### consul.catalog.connect.nodes(options)
883
+
884
+ Lists the nodes for a given Connect-capable service.
885
+
886
+ Options
887
+
888
+ - service (String): service name
889
+ - dc (String, optional): datacenter (defaults to local for agent)
890
+
891
+ Usage
892
+
893
+ ```javascript
894
+ await consul.catalog.connect.nodes("example");
895
+ ```
896
+
897
+ Result
898
+
899
+ ```json
900
+ [
901
+ {
902
+ "ID": "40e4a748-2192-161a-0510-9bf59fe950b5",
903
+ "Node": "foobar",
904
+ "Address": "192.168.10.10",
905
+ "Datacenter": "dc1",
906
+ "TaggedAddresses": {
907
+ "lan": "192.168.10.10",
908
+ "wan": "10.0.10.10"
909
+ },
910
+ "NodeMeta": {
911
+ "somekey": "somevalue"
912
+ },
913
+ "CreateIndex": 51,
914
+ "ModifyIndex": 51,
915
+ "ServiceAddress": "172.17.0.3",
916
+ "ServiceEnableTagOverride": false,
917
+ "ServiceID": "32a2a47f7992:nodea:5000",
918
+ "ServiceName": "foobar",
919
+ "ServiceKind": "connect-proxy",
920
+ "ServiceProxyDestination": "my-service",
921
+ "ServicePort": 5000,
922
+ "ServiceMeta": {
923
+ "foobar_meta_value": "baz"
924
+ },
925
+ "ServiceTags": ["tacos"]
926
+ }
927
+ ]
928
+ ```
929
+
930
+ <a id="catalog-node"></a>
931
+
932
+ ### consul.catalog.node
933
+
934
+ - [list](#catalog-node-list)
935
+ - [services](#catalog-node-services)
936
+
937
+ <a id="catalog-node-list"></a>
938
+
939
+ ### consul.catalog.node.list([options])
940
+
941
+ Lists nodes in a given datacenter.
942
+
943
+ Options
944
+
945
+ - dc (String, optional): datacenter (defaults to local for agent)
946
+
947
+ Usage
948
+
949
+ ```javascript
950
+ await consul.catalog.node.list();
951
+ ```
952
+
953
+ Result
954
+
955
+ ```json
956
+ [
957
+ {
958
+ "Node": "node1",
959
+ "Address": "127.0.0.1"
960
+ }
961
+ ]
962
+ ```
963
+
964
+ <a id="catalog-node-services"></a>
965
+
966
+ ### consul.catalog.node.services(options)
967
+
968
+ Lists the services provided by a node.
969
+
970
+ Options
971
+
972
+ - node (String): node ID
973
+
974
+ Usage
975
+
976
+ ```javascript
977
+ await consul.catalog.node.services("node1");
978
+ ```
979
+
980
+ Result
981
+
982
+ ```json
983
+ {
984
+ "Node": {
985
+ "Node": "node1",
986
+ "Address": "127.0.0.1"
987
+ },
988
+ "Services": {
989
+ "consul": {
990
+ "ID": "consul",
991
+ "Service": "consul",
992
+ "Tags": [],
993
+ "Port": 8300
994
+ },
995
+ "example": {
996
+ "ID": "example",
997
+ "Service": "example",
998
+ "Tags": ["dev", "web"],
999
+ "Port": 80
1000
+ }
1001
+ }
1002
+ }
1003
+ ```
1004
+
1005
+ <a id="catalog-service"></a>
1006
+
1007
+ ### consul.catalog.service
1008
+
1009
+ - [list](#catalog-service-list)
1010
+ - [nodes](#catalog-service-nodes)
1011
+
1012
+ <a id="catalog-service-list"></a>
1013
+
1014
+ ### consul.catalog.service.list([options])
1015
+
1016
+ Lists services in a given datacenter.
1017
+
1018
+ Options
1019
+
1020
+ - dc (String): datacenter (defaults to local for agent)
1021
+
1022
+ Usage
1023
+
1024
+ ```javascript
1025
+ await consul.catalog.service.list();
1026
+ ```
1027
+
1028
+ Result
1029
+
1030
+ ```json
1031
+ {
1032
+ "consul": [],
1033
+ "example": ["dev", "web"]
1034
+ }
1035
+ ```
1036
+
1037
+ <a id="catalog-service-nodes"></a>
1038
+
1039
+ ### consul.catalog.service.nodes(options)
1040
+
1041
+ Lists the nodes for a given service.
1042
+
1043
+ Options
1044
+
1045
+ - service (String): service name
1046
+ - dc (String, optional): datacenter (defaults to local for agent)
1047
+ - tag (String, optional): filter by tag
1048
+
1049
+ Usage
1050
+
1051
+ ```javascript
1052
+ await consul.catalog.service.nodes("example");
1053
+ ```
1054
+
1055
+ Result
1056
+
1057
+ ```json
1058
+ [
1059
+ {
1060
+ "Node": "node1",
1061
+ "Address": "127.0.0.1",
1062
+ "ServiceID": "example",
1063
+ "ServiceName": "example",
1064
+ "ServiceTags": ["dev", "web"],
1065
+ "ServicePort": 80
1066
+ }
1067
+ ]
1068
+ ```
1069
+
1070
+ <a id="event"></a>
1071
+
1072
+ ### consul.event
1073
+
1074
+ - [fire](#event-fire)
1075
+ - [list](#event-list)
1076
+
1077
+ <a id="event-fire"></a>
1078
+
1079
+ ### consul.event.fire(options)
1080
+
1081
+ Fires a new user event.
1082
+
1083
+ Options
1084
+
1085
+ - name (String): event name
1086
+ - payload (String|Buffer): payload
1087
+ - node (String, optional): regular expression to filter by node
1088
+ - service (String, optional): regular expression to filter by service
1089
+ - tag (String, optional): regular expression to filter by tag
1090
+
1091
+ Usage
1092
+
1093
+ ```javascript
1094
+ await consul.event.fire("deploy", "53");
1095
+ ```
1096
+
1097
+ Result
1098
+
1099
+ ```json
1100
+ {
1101
+ "ID": "4730953b-3135-7ff2-47a7-9d9fc9c4e5a2",
1102
+ "Name": "deploy",
1103
+ "Payload": "53",
1104
+ "NodeFilter": "",
1105
+ "ServiceFilter": "",
1106
+ "TagFilter": "",
1107
+ "Version": 1,
1108
+ "LTime": 0
1109
+ }
1110
+ ```
1111
+
1112
+ <a id="event-list"></a>
1113
+
1114
+ ### consul.event.list([options])
1115
+
1116
+ Lists the most recent events an agent has seen.
1117
+
1118
+ Options
1119
+
1120
+ - name (String, optional): filter by event name
1121
+
1122
+ Usage
1123
+
1124
+ ```javascript
1125
+ await consul.event.list("deploy");
1126
+ ```
1127
+
1128
+ Result
1129
+
1130
+ ```json
1131
+ [
1132
+ {
1133
+ "ID": "4730953b-3135-7ff2-47a7-9d9fc9c4e5a2",
1134
+ "Name": "deploy",
1135
+ "Payload": "53",
1136
+ "NodeFilter": "",
1137
+ "ServiceFilter": "",
1138
+ "TagFilter": "",
1139
+ "Version": 1,
1140
+ "LTime": 2
1141
+ }
1142
+ ]
1143
+ ```
1144
+
1145
+ <a id="health"></a>
1146
+
1147
+ ### consul.health
1148
+
1149
+ - [node](#health-node)
1150
+ - [checks](#health-checks)
1151
+ - [service](#health-service)
1152
+ - [state](#health-state)
1153
+
1154
+ <a id="health-node"></a>
1155
+
1156
+ ### consul.health.node(options)
1157
+
1158
+ Returns the health info of a node.
1159
+
1160
+ Options
1161
+
1162
+ - node (String): node
1163
+ - dc (String, optional): datacenter (defaults to local for agent)
1164
+
1165
+ Usage
1166
+
1167
+ ```javascript
1168
+ await consul.health.node("node1");
1169
+ ```
1170
+
1171
+ Result
1172
+
1173
+ ```json
1174
+ [
1175
+ {
1176
+ "Node": "node1",
1177
+ "CheckID": "serfHealth",
1178
+ "Name": "Serf Health Status",
1179
+ "Status": "passing",
1180
+ "Notes": "",
1181
+ "Output": "Agent alive and reachable",
1182
+ "ServiceID": "",
1183
+ "ServiceName": ""
1184
+ },
1185
+ {
1186
+ "Node": "node1",
1187
+ "CheckID": "service:example",
1188
+ "Name": "Service 'example' check",
1189
+ "Status": "critical",
1190
+ "Notes": "",
1191
+ "Output": "",
1192
+ "ServiceID": "example",
1193
+ "ServiceName": "example"
1194
+ }
1195
+ ]
1196
+ ```
1197
+
1198
+ <a id="health-checks"></a>
1199
+
1200
+ ### consul.health.checks(options)
1201
+
1202
+ Returns the checks of a service.
1203
+
1204
+ Options
1205
+
1206
+ - service (String): service name
1207
+ - dc (String, optional): datacenter (defaults to local for agent)
1208
+
1209
+ Usage
1210
+
1211
+ ```javascript
1212
+ await consul.health.checks("example");
1213
+ ```
1214
+
1215
+ Result
1216
+
1217
+ ```json
1218
+ [
1219
+ {
1220
+ "Node": "node1",
1221
+ "CheckID": "service:example",
1222
+ "Name": "Service 'example' check",
1223
+ "Status": "critical",
1224
+ "Notes": "",
1225
+ "Output": "",
1226
+ "ServiceID": "example",
1227
+ "ServiceName": "example"
1228
+ }
1229
+ ]
1230
+ ```
1231
+
1232
+ <a id="health-service"></a>
1233
+
1234
+ ### consul.health.service(options)
1235
+
1236
+ Returns the nodes and health info of a service.
1237
+
1238
+ Options
1239
+
1240
+ - service (String): service name
1241
+ - dc (String, optional): datacenter (defaults to local for agent)
1242
+ - tag (String, optional): filter by tag
1243
+ - passing (Boolean, optional): restrict to passing checks
1244
+
1245
+ Usage
1246
+
1247
+ ```javascript
1248
+ await consul.health.service("example");
1249
+ ```
1250
+
1251
+ Result
1252
+
1253
+ ```json
1254
+ [
1255
+ {
1256
+ "Node": {
1257
+ "Node": "node1",
1258
+ "Address": "127.0.0.1"
1259
+ },
1260
+ "Service": {
1261
+ "ID": "example",
1262
+ "Service": "example",
1263
+ "Tags": [],
1264
+ "Port": 0
1265
+ },
1266
+ "Checks": [
1267
+ {
1268
+ "Node": "node1",
1269
+ "CheckID": "service:example",
1270
+ "Name": "Service 'example' check",
1271
+ "Status": "critical",
1272
+ "Notes": "",
1273
+ "Output": "",
1274
+ "ServiceID": "example",
1275
+ "ServiceName": "example"
1276
+ },
1277
+ {
1278
+ "Node": "node1",
1279
+ "CheckID": "serfHealth",
1280
+ "Name": "Serf Health Status",
1281
+ "Status": "passing",
1282
+ "Notes": "",
1283
+ "Output": "Agent alive and reachable",
1284
+ "ServiceID": "",
1285
+ "ServiceName": ""
1286
+ }
1287
+ ]
1288
+ }
1289
+ ]
1290
+ ```
1291
+
1292
+ <a id="health-state"></a>
1293
+
1294
+ ### consul.health.state(options)
1295
+
1296
+ Returns the checks in a given state.
1297
+
1298
+ Options
1299
+
1300
+ - state (String, enum: any, passing, warning, critical): state
1301
+ - dc (String, optional): datacenter (defaults to local for agent)
1302
+
1303
+ Usage
1304
+
1305
+ ```javascript
1306
+ await consul.health.state("critical");
1307
+ ```
1308
+
1309
+ Result
1310
+
1311
+ ```json
1312
+ [
1313
+ {
1314
+ "Node": "node1",
1315
+ "CheckID": "service:example",
1316
+ "Name": "Service 'example' check",
1317
+ "Status": "critical",
1318
+ "Notes": "",
1319
+ "Output": "",
1320
+ "ServiceID": "example",
1321
+ "ServiceName": "example"
1322
+ }
1323
+ ]
1324
+ ```
1325
+
1326
+ <a id="intention"></a>
1327
+
1328
+ ### consul.intention
1329
+
1330
+ - [list](#intention-list)
1331
+ - [create](#intention-create)
1332
+ - [get](#intention-get)
1333
+ - [update](#intention-update)
1334
+ - [destroy](#intention-destroy)
1335
+
1336
+ <a id="intention-list"></a>
1337
+
1338
+ ### consul.intention.list([options])
1339
+
1340
+ List all Connect intentions.
1341
+
1342
+ Options
1343
+
1344
+ - dc (String, optional): datacenter (defaults to local for agent)
1345
+
1346
+ Usage
1347
+
1348
+ ```javascript
1349
+ await consul.intention.list();
1350
+ ```
1351
+
1352
+ Result
1353
+
1354
+ ```json
1355
+ [
1356
+ {
1357
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
1358
+ "SourceName": "web",
1359
+ "DestinationName": "db",
1360
+ "SourceType": "",
1361
+ "Action": "allow",
1362
+ "Description": "",
1363
+ "Meta": null,
1364
+ "Precedence": 9,
1365
+ "CreateIndex": 11,
1366
+ "ModifyIndex": 12
1367
+ }
1368
+ ]
1369
+ ```
1370
+
1371
+ <a id="intention-create"></a>
1372
+
1373
+ ### consul.intention.create(options)
1374
+
1375
+ Create a new Connect intention.
1376
+
1377
+ Options
1378
+
1379
+ - sourcename (String, required): source service name
1380
+ - destinationname (String, required): destination service name
1381
+ - action (String, enum: allow, deny; required): action to take
1382
+ - description (String, optional): human readable description
1383
+ - sourcetype (String, optional): source type (e.g., "consul")
1384
+ - meta (Object, optional): metadata key/value pairs
1385
+ - precedence (Number, optional): precedence for the intention
1386
+ - permissions (Array, optional): L7 permissions for application-aware intentions
1387
+ - dc (String, optional): datacenter (defaults to local for agent)
1388
+
1389
+ Usage
1390
+
1391
+ ```javascript
1392
+ await consul.intention.create({
1393
+ sourcename: "web",
1394
+ destinationname: "db",
1395
+ action: "allow",
1396
+ description: "Allow web service to access database",
1397
+ });
1398
+ ```
1399
+
1400
+ Result
1401
+
1402
+ ```json
1403
+ {
1404
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1"
1405
+ }
1406
+ ```
1407
+
1408
+ <a id="intention-get"></a>
1409
+
1410
+ ### consul.intention.get(options)
1411
+
1412
+ Get a specific Connect intention.
1413
+
1414
+ Options
1415
+
1416
+ - id (String, required): intention ID
1417
+ - dc (String, optional): datacenter (defaults to local for agent)
1418
+
1419
+ Usage
1420
+
1421
+ ```javascript
1422
+ await consul.intention.get("a0f5dc05-84c3-5f5a-1d88-05b875e524e1");
1423
+ ```
1424
+
1425
+ Result
1426
+
1427
+ ```json
1428
+ {
1429
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
1430
+ "SourceName": "web",
1431
+ "DestinationName": "db",
1432
+ "SourceType": "",
1433
+ "Action": "allow",
1434
+ "Description": "Allow web service to access database",
1435
+ "Meta": null,
1436
+ "Precedence": 9,
1437
+ "CreateIndex": 11,
1438
+ "ModifyIndex": 12
1439
+ }
1440
+ ```
1441
+
1442
+ <a id="intention-update"></a>
1443
+
1444
+ ### consul.intention.update(options)
1445
+
1446
+ Update an existing Connect intention.
1447
+
1448
+ Options
1449
+
1450
+ - id (String, required): intention ID
1451
+ - sourcename (String, required): source service name
1452
+ - destinationname (String, required): destination service name
1453
+ - action (String, enum: allow, deny; required): action to take
1454
+ - description (String, optional): human readable description
1455
+ - sourcetype (String, optional): source type (e.g., "consul")
1456
+ - meta (Object, optional): metadata key/value pairs
1457
+ - precedence (Number, optional): precedence for the intention
1458
+ - permissions (Array, optional): L7 permissions for application-aware intentions
1459
+ - dc (String, optional): datacenter (defaults to local for agent)
1460
+
1461
+ Usage
1462
+
1463
+ ```javascript
1464
+ await consul.intention.update({
1465
+ id: "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
1466
+ sourcename: "web",
1467
+ destinationname: "db",
1468
+ action: "deny",
1469
+ description: "Block web service from accessing database",
1470
+ });
1471
+ ```
1472
+
1473
+ <a id="intention-destroy"></a>
1474
+
1475
+ ### consul.intention.destroy(options)
1476
+
1477
+ Delete a Connect intention.
1478
+
1479
+ Options
1480
+
1481
+ - id (String, required): intention ID
1482
+ - dc (String, optional): datacenter (defaults to local for agent)
1483
+
1484
+ Usage
1485
+
1486
+ ```javascript
1487
+ await consul.intention.destroy("a0f5dc05-84c3-5f5a-1d88-05b875e524e1");
1488
+ ```
1489
+
1490
+ <a id="resolver"></a>
1491
+
1492
+ ### consul.resolver(config)
1493
+
1494
+ Create a DNS resolver instance for load balancing and service discovery with Consul.
1495
+
1496
+ The resolver provides intelligent service selection using multiple algorithms, DNS resolution, health checks, and Redis-based metrics tracking.
1497
+
1498
+ Options
1499
+
1500
+ - redis (Redis, optional): ioredis instance for caching (required if cacheEnabled is true)
1501
+ - cacheEnabled (Boolean, default: false): enable Redis caching
1502
+ - cachePrefix (String, required): prefix for Redis cache keys
1503
+ - debug (Boolean, default: false): enable debug logging
1504
+ - weights (Object, optional): custom weights for weighted round robin algorithm
1505
+ - health (Number, default: 0.25): weight for health score
1506
+ - responseTime (Number, default: 0.2): weight for response time
1507
+ - errorRate (Number, default: 0.2): weight for error rate
1508
+ - resources (Number, default: 0.15): weight for CPU/memory usage
1509
+ - connections (Number, default: 0.1): weight for active connections
1510
+ - distribution (Number, default: 0.1): weight for distribution fairness
1511
+ - metrics (Object, optional): default metrics for new services
1512
+ - responseTime (Number, default: 100): default response time in ms
1513
+ - errorRate (Number, default: 0): default error rate percentage
1514
+ - cpuUsage (Number, default: 50): default CPU usage percentage
1515
+ - memoryUsage (Number, default: 50): default memory usage percentage
1516
+ - activeConnections (Number, default: 0): default active connections
1517
+ - cacheTTL (Number, default: 60000): cache TTL in milliseconds
1518
+ - dnsEndpoints (String[], optional): custom DNS endpoints
1519
+ - dnsTimeout (Number, default: 1500): DNS query timeout in milliseconds
1520
+ - dnsRetries (Number, default: 2): number of DNS retry attempts
1521
+
1522
+ Usage
1523
+
1524
+ ```javascript
1525
+ import Consul from "@brimble/consul";
1526
+ import Redis from "ioredis";
1527
+
1528
+ const consul = new Consul({
1529
+ host: "127.0.0.1",
1530
+ port: 8500,
1531
+ });
1532
+
1533
+ const redis = new Redis({
1534
+ host: "localhost",
1535
+ port: 6379,
1536
+ });
1537
+
1538
+ const resolver = consul.resolver({
1539
+ redis,
1540
+ cacheEnabled: true,
1541
+ cachePrefix: "myapp",
1542
+ debug: false,
1543
+ });
1544
+ ```
1545
+
1546
+ <a id="resolver-select"></a>
1547
+
1548
+ ### resolver.selectOptimalService(service, algorithm)
1549
+
1550
+ Select the optimal service instance based on the specified algorithm.
1551
+
1552
+ Options
1553
+
1554
+ - service (String, required): service name to resolve
1555
+ - algorithm (String, optional): selection algorithm
1556
+ - `SelectionAlgorithm.RoundRobin` (default): round-robin selection
1557
+ - `SelectionAlgorithm.LeastConnection`: select service with fewest connections
1558
+ - `SelectionAlgorithm.WeightedRoundRobin`: weighted selection based on metrics
1559
+
1560
+ Usage
1561
+
1562
+ ```javascript
1563
+ const Consul = require("@brimble/consul");
1564
+ const { SelectionAlgorithm } = Consul.Resolver;
1565
+
1566
+ const result = await resolver.selectOptimalService(
1567
+ "my-service",
1568
+ SelectionAlgorithm.LeastConnection,
1569
+ );
1570
+
1571
+ if (result.selected) {
1572
+ console.log(`Selected: ${result.selected.ip}:${result.selected.port}`);
1573
+ }
1574
+ ```
1575
+
1576
+ Result
1577
+
1578
+ ```json
1579
+ {
1580
+ "selected": {
1581
+ "ip": "192.168.1.10",
1582
+ "port": 8080
1583
+ },
1584
+ "services": [
1585
+ {
1586
+ "ip": "192.168.1.10",
1587
+ "port": 8080
1588
+ },
1589
+ {
1590
+ "ip": "192.168.1.11",
1591
+ "port": 8080
1592
+ }
1593
+ ]
1594
+ }
1595
+ ```
1596
+
1597
+ <a id="resolver-increment"></a>
1598
+
1599
+ ### resolver.incrementConnections(serviceId)
1600
+
1601
+ Increment the active connection count for a service.
1602
+
1603
+ Options
1604
+
1605
+ - serviceId (String, required): service ID
1606
+
1607
+ Usage
1608
+
1609
+ ```javascript
1610
+ await resolver.incrementConnections("service-id-123");
1611
+ ```
1612
+
1613
+ <a id="resolver-decrement"></a>
1614
+
1615
+ ### resolver.decrementConnections(serviceId)
1616
+
1617
+ Decrement the active connection count for a service.
1618
+
1619
+ Options
1620
+
1621
+ - serviceId (String, required): service ID
1622
+
1623
+ Usage
1624
+
1625
+ ```javascript
1626
+ await resolver.decrementConnections("service-id-123");
1627
+ ```
1628
+
1629
+ <a id="resolver-metrics"></a>
1630
+
1631
+ ### resolver.getSelectionMetrics(serviceId)
1632
+
1633
+ Get current metrics for a specific service.
1634
+
1635
+ Options
1636
+
1637
+ - serviceId (String, required): service ID
1638
+
1639
+ Usage
1640
+
1641
+ ```javascript
1642
+ const metrics = await resolver.getSelectionMetrics("service-id-123");
1643
+ ```
1644
+
1645
+ Result
1646
+
1647
+ ```json
1648
+ {
1649
+ "responseTime": 150,
1650
+ "errorRate": 0.5,
1651
+ "cpuUsage": 45,
1652
+ "memoryUsage": 60,
1653
+ "activeConnections": 10,
1654
+ "lastSelectedTime": 1234567890
1655
+ }
1656
+ ```
1657
+
1658
+ <a id="resolver-refresh"></a>
1659
+
1660
+ ### resolver.refresh()
1661
+
1662
+ Clear all stored metrics from Redis cache.
1663
+
1664
+ Usage
1665
+
1666
+ ```javascript
1667
+ await resolver.refresh();
1668
+ ```
1669
+
1670
+ <a id="kv"></a>
1671
+
1672
+ ### consul.kv
1673
+
1674
+ - [get](#kv-get)
1675
+ - [keys](#kv-keys)
1676
+ - [set](#kv-set)
1677
+ - [del](#kv-del)
1678
+
1679
+ <a id="kv-get"></a>
1680
+
1681
+ ### consul.kv.get(options)
1682
+
1683
+ Return key/value (kv) pair(s) or `undefined` if key not found.
1684
+
1685
+ Options
1686
+
1687
+ - key (String): path to value
1688
+ - dc (String, optional): datacenter (defaults to local for agent)
1689
+ - recurse (Boolean, default: false): return all keys with given key prefix
1690
+ - index (String, optional): used with `ModifyIndex` to block and wait for changes
1691
+ - wait (String, optional): limit how long to wait for changes (ex: `5m`), used with index
1692
+ - raw (Boolean, optional): return raw value (can't be used with recursive, implies buffer)
1693
+ - buffer (Boolean, default: false): decode value into Buffer instead of String
1694
+
1695
+ Usage
1696
+
1697
+ ```javascript
1698
+ await consul.kv.get("hello");
1699
+ ```
1700
+
1701
+ Result
1702
+
1703
+ ```json
1704
+ {
1705
+ "CreateIndex": 6,
1706
+ "ModifyIndex": 6,
1707
+ "LockIndex": 0,
1708
+ "Key": "hello",
1709
+ "Flags": 0,
1710
+ "Value": "world"
1711
+ }
1712
+ ```
1713
+
1714
+ <a id="kv-keys"></a>
1715
+
1716
+ ### consul.kv.keys(options)
1717
+
1718
+ Return keys for a given prefix.
1719
+
1720
+ Options
1721
+
1722
+ - key (String): path prefix
1723
+ - dc (String, optional): datacenter (defaults to local for agent)
1724
+ - separator (String, optional): list keys up to a given separator
1725
+
1726
+ Usage
1727
+
1728
+ ```javascript
1729
+ await consul.kv.keys("a/");
1730
+ ```
1731
+
1732
+ Result
1733
+
1734
+ ```json
1735
+ ["a/b", "a/c"]
1736
+ ```
1737
+
1738
+ <a id="kv-set"></a>
1739
+
1740
+ ### consul.kv.set(options)
1741
+
1742
+ Set key/value (kv) pair.
1743
+
1744
+ Options
1745
+
1746
+ - key (String): key
1747
+ - value (String|Buffer): value
1748
+ - dc (String, optional): datacenter (defaults to local for agent)
1749
+ - flags (Number, optional): unsigned integer opaque to user, can be used by application
1750
+ - cas (String, optional): use with `ModifyIndex` to do a check-and-set operation
1751
+ - acquire (String, optional): session ID, lock acquisition operation
1752
+ - release (String, optional): session ID, lock release operation
1753
+
1754
+ Usage
1755
+
1756
+ ```javascript
1757
+ await consul.kv.set("hello", "world");
1758
+ ```
1759
+
1760
+ Result
1761
+
1762
+ ```json
1763
+ true
1764
+ ```
1765
+
1766
+ <a id="kv-del"></a>
1767
+
1768
+ ### consul.kv.del(options)
1769
+
1770
+ Delete key/value (kv) pair(s).
1771
+
1772
+ Options
1773
+
1774
+ - key (String): key
1775
+ - dc (String, optional): datacenter (defaults to local for agent)
1776
+ - recurse (Boolean, default: false): delete all keys with given key prefix
1777
+ - cas (String, optional): use with `ModifyIndex` to do a check-and-set operation (must be greater than `0`)
1778
+
1779
+ Usage
1780
+
1781
+ ```javascript
1782
+ await consul.kv.del("hello");
1783
+ ```
1784
+
1785
+ <a id="query"></a>
1786
+
1787
+ ### consul.query
1788
+
1789
+ - [list](#query-list)
1790
+ - [create](#query-create)
1791
+ - [update](#query-update)
1792
+ - [get](#query-get)
1793
+ - [destroy](#query-destroy)
1794
+ - [execute](#query-execute)
1795
+ - [explain](#query-explain)
1796
+
1797
+ <a id="query-list"></a>
1798
+
1799
+ ### consul.query.list()
1800
+
1801
+ List prepared query.
1802
+
1803
+ Usage
1804
+
1805
+ ```javascript
1806
+ await consul.query.list();
1807
+ ```
1808
+
1809
+ Result
1810
+
1811
+ ```json
1812
+ [
1813
+ {
1814
+ "ID": "422b14b9-874b-4520-bd2e-e149a42b0066",
1815
+ "Name": "redis",
1816
+ "Session": "",
1817
+ "Token": "",
1818
+ "Template": {
1819
+ "Type": "",
1820
+ "Regexp": ""
1821
+ },
1822
+ "Service": {
1823
+ "Service": "redis",
1824
+ "Failover": {
1825
+ "NearestN": 3,
1826
+ "Datacenters": ["dc1", "dc2"]
1827
+ },
1828
+ "OnlyPassing": false,
1829
+ "Tags": ["master", "!experimental"]
1830
+ },
1831
+ "DNS": {
1832
+ "TTL": "10s"
1833
+ },
1834
+ "RaftIndex": {
1835
+ "CreateIndex": 23,
1836
+ "ModifyIndex": 42
1837
+ }
1838
+ }
1839
+ ]
1840
+ ```
1841
+
1842
+ <a id="query-create"></a>
1843
+
1844
+ ### consul.query.create(options)
1845
+
1846
+ Create a new prepared query.
1847
+
1848
+ Options
1849
+
1850
+ - name (String, optional): name that can be used to execute a query instead of using its ID
1851
+ - session (String, optional): provides a way to automatically remove a prepared query when the given session is invalidated
1852
+ - token (String, optional): captured ACL Token that is reused as the ACL Token every time the query is executed
1853
+ - near (String, optional): allows specifying a particular node to sort near based on distance sorting using Network Coordinates
1854
+ - service.service (String, required): name of the service to query
1855
+ - service.failover.nearestn (Number, optional): when set the query will be forwarded to up to nearest N other datacenters based on their estimated network round trip time using Network Coordinates from the WAN gossip pool
1856
+ - service.failover.datacenters (String[], optional): fixed list of remote datacenters to forward the query to if there are no healthy nodes in the local datacenter
1857
+ - service.onlypassing (Boolean, default: false): filter results to only nodes with a passing state
1858
+ - service.tags (String[], optional): list of service tags to filter the query results
1859
+ - ttl.dns (String, optional, ex: `10s`): controls how the TTL is set when query results are served over DNS
1860
+
1861
+ Usage
1862
+
1863
+ ```javascript
1864
+ await consul.query.create({
1865
+ name: 'redis',
1866
+ service: {
1867
+ service: 'redis'
1868
+ onlypassing: true
1869
+ },
1870
+ });
1871
+ ```
1872
+
1873
+ Result
1874
+
1875
+ ```json
1876
+ {
1877
+ "ID": "422b14b9-874b-4520-bd2e-e149a42b0066"
1878
+ }
1879
+ ```
1880
+
1881
+ <a id="query-update"></a>
1882
+
1883
+ ### consul.query.update(options)
1884
+
1885
+ Update existing prepared query.
1886
+
1887
+ Options
1888
+
1889
+ - query (String, required): ID of the query
1890
+
1891
+ And all [create options][query-create].
1892
+
1893
+ Usage
1894
+
1895
+ ```javascript
1896
+ await consul.query.update({
1897
+ query: '422b14b9-874b-4520-bd2e-e149a42b0066',
1898
+ name: 'redis',
1899
+ service: {
1900
+ service: 'redis'
1901
+ onlypassing: false
1902
+ },
1903
+ });
1904
+ ```
1905
+
1906
+ <a id="query-get"></a>
1907
+
1908
+ ### consul.query.get(options)
1909
+
1910
+ Get prepared query.
1911
+
1912
+ Options
1913
+
1914
+ - query (String, required): ID of the query
1915
+
1916
+ Usage
1917
+
1918
+ ```javascript
1919
+ await consul.query.get("6119cabf-c052-48fe-9f07-711762e52931");
1920
+ ```
1921
+
1922
+ Result
1923
+
1924
+ ```json
1925
+ {
1926
+ "ID": "6119cabf-c052-48fe-9f07-711762e52931",
1927
+ "Name": "redis",
1928
+ "Session": "",
1929
+ "Token": "",
1930
+ "Template": {
1931
+ "Type": "",
1932
+ "Regexp": ""
1933
+ },
1934
+ "Service": {
1935
+ "Service": "redis",
1936
+ "Failover": {
1937
+ "NearestN": 3,
1938
+ "Datacenters": ["dc1", "dc2"]
1939
+ },
1940
+ "OnlyPassing": false,
1941
+ "Tags": ["master", "!experimental"]
1942
+ },
1943
+ "DNS": {
1944
+ "TTL": "10s"
1945
+ },
1946
+ "RaftIndex": {
1947
+ "CreateIndex": 23,
1948
+ "ModifyIndex": 42
1949
+ }
1950
+ }
1951
+ ```
1952
+
1953
+ <a id="query-destroy"></a>
1954
+
1955
+ ### consul.query.destroy(options)
1956
+
1957
+ Delete prepared query.
1958
+
1959
+ Options
1960
+
1961
+ - query (String, required): ID of the query
1962
+
1963
+ Usage
1964
+
1965
+ ```javascript
1966
+ await consul.query.destroy("422b14b9-874b-4520-bd2e-e149a42b0066");
1967
+ ```
1968
+
1969
+ <a id="query-execute"></a>
1970
+
1971
+ ### consul.query.execute(options)
1972
+
1973
+ Execute prepared query.
1974
+
1975
+ Options
1976
+
1977
+ - query (String, required): ID of the query
1978
+
1979
+ Usage
1980
+
1981
+ ```javascript
1982
+ await consul.query.execute("6119cabf-c052-48fe-9f07-711762e52931");
1983
+ ```
1984
+
1985
+ Result
1986
+
1987
+ ```json
1988
+ {
1989
+ "Service": "redis",
1990
+ "Nodes": [
1991
+ {
1992
+ "Node": {
1993
+ "Node": "foobar",
1994
+ "Address": "10.1.10.12",
1995
+ "TaggedAddresses": {
1996
+ "lan": "10.1.10.12",
1997
+ "wan": "10.1.10.12"
1998
+ }
1999
+ },
2000
+ "Service": {
2001
+ "ID": "redis",
2002
+ "Service": "redis",
2003
+ "Tags": null,
2004
+ "Port": 8000
2005
+ },
2006
+ "Checks": [
2007
+ {
2008
+ "Node": "foobar",
2009
+ "CheckID": "service:redis",
2010
+ "Name": "Service 'redis' check",
2011
+ "Status": "passing",
2012
+ "Notes": "",
2013
+ "Output": "",
2014
+ "ServiceID": "redis",
2015
+ "ServiceName": "redis"
2016
+ },
2017
+ {
2018
+ "Node": "foobar",
2019
+ "CheckID": "serfHealth",
2020
+ "Name": "Serf Health Status",
2021
+ "Status": "passing",
2022
+ "Notes": "",
2023
+ "Output": "",
2024
+ "ServiceID": "",
2025
+ "ServiceName": ""
2026
+ }
2027
+ ],
2028
+ "DNS": {
2029
+ "TTL": "10s"
2030
+ },
2031
+ "Datacenter": "dc3",
2032
+ "Failovers": 2
2033
+ }
2034
+ ]
2035
+ }
2036
+ ```
2037
+
2038
+ <a id="query-explain"></a>
2039
+
2040
+ ### consul.query.explain(options)
2041
+
2042
+ Explain prepared query.
2043
+
2044
+ Options
2045
+
2046
+ - query (String, required): ID of the query
2047
+
2048
+ Usage
2049
+
2050
+ ```javascript
2051
+ await consul.query.explain("422b14b9-874b-4520-bd2e-e149a42b0066");
2052
+ ```
2053
+
2054
+ Result
2055
+
2056
+ ```json
2057
+ {
2058
+ "Query": {
2059
+ "ID": "422b14b9-874b-4520-bd2e-e149a42b0066",
2060
+ "Name": "redis",
2061
+ "Session": "",
2062
+ "Token": "",
2063
+ "Template": {
2064
+ "Type": "",
2065
+ "Regexp": ""
2066
+ },
2067
+ "Service": {
2068
+ "Service": "redis",
2069
+ "Failover": {
2070
+ "NearestN": 3,
2071
+ "Datacenters": ["dc1", "dc2"]
2072
+ },
2073
+ "OnlyPassing": false,
2074
+ "Tags": ["master", "!experimental"]
2075
+ },
2076
+ "DNS": {
2077
+ "TTL": "10s"
2078
+ },
2079
+ "RaftIndex": {
2080
+ "CreateIndex": 23,
2081
+ "ModifyIndex": 42
2082
+ }
2083
+ }
2084
+ }
2085
+ ```
2086
+
2087
+ <a id="session"></a>
2088
+
2089
+ ### consul.session
2090
+
2091
+ - [create](#session-create)
2092
+ - [destroy](#session-destroy)
2093
+ - [get](#session-get)
2094
+ - [node](#session-node)
2095
+ - [list](#session-list)
2096
+ - [renew](#session-renew)
2097
+
2098
+ <a id="session-create"></a>
2099
+
2100
+ ### consul.session.create([options])
2101
+
2102
+ Create a new session.
2103
+
2104
+ Options
2105
+
2106
+ - dc (String, optional): datacenter (defaults to local for agent)
2107
+ - lockdelay (String, range: 1s-60s, default: `15s`): the time consul prevents locks held by the session from being acquired after a session has been invalidated
2108
+ - name (String, optional): human readable name for the session
2109
+ - node (String, optional): node with which to associate session (defaults to connected agent)
2110
+ - checks (String[], optional): checks to associate with session
2111
+ - behavior (String, enum: release, delete; default: release): controls the behavior when a session is invalidated
2112
+ - ttl (String, optional, valid: `10s`-`86400s`): interval session must be renewed
2113
+
2114
+ Usage
2115
+
2116
+ ```javascript
2117
+ await consul.session.create();
2118
+ ```
2119
+
2120
+ Result
2121
+
2122
+ ```json
2123
+ {
2124
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1"
2125
+ }
2126
+ ```
2127
+
2128
+ <a id="session-destroy"></a>
2129
+
2130
+ ### consul.session.destroy(options)
2131
+
2132
+ Destroy a given session.
2133
+
2134
+ Options
2135
+
2136
+ - id (String): session ID
2137
+ - dc (String, optional): datacenter (defaults to local for agent)
2138
+
2139
+ Usage
2140
+
2141
+ ```javascript
2142
+ await consul.session.destroy("a0f5dc05-84c3-5f5a-1d88-05b875e524e1");
2143
+ ```
2144
+
2145
+ <a id="session-get"></a>
2146
+
2147
+ ### consul.session.get(options)
2148
+
2149
+ Queries a given session.
2150
+
2151
+ Options
2152
+
2153
+ - id (String): session ID
2154
+ - dc (String, optional): datacenter (defaults to local for agent)
2155
+
2156
+ Usage
2157
+
2158
+ ```javascript
2159
+ await consul.session.get("a0f5dc05-84c3-5f5a-1d88-05b875e524e1");
2160
+ ```
2161
+
2162
+ Result
2163
+
2164
+ ```json
2165
+ {
2166
+ "CreateIndex": 11,
2167
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
2168
+ "Name": "",
2169
+ "Node": "node1",
2170
+ "Checks": ["serfHealth"],
2171
+ "LockDelay": 15000000000
2172
+ }
2173
+ ```
2174
+
2175
+ <a id="session-node"></a>
2176
+
2177
+ ### consul.session.node(options)
2178
+
2179
+ Lists sessions belonging to a node.
2180
+
2181
+ Options
2182
+
2183
+ - node (String): node
2184
+ - dc (String, optional): datacenter (defaults to local for agent)
2185
+
2186
+ Usage
2187
+
2188
+ ```javascript
2189
+ await consul.session.node("node1");
2190
+ ```
2191
+
2192
+ Result
2193
+
2194
+ ```json
2195
+ [
2196
+ {
2197
+ "CreateIndex": 13,
2198
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
2199
+ "Name": "",
2200
+ "Node": "node1",
2201
+ "Checks": ["serfHealth"],
2202
+ "LockDelay": 15000000000
2203
+ }
2204
+ ]
2205
+ ```
2206
+
2207
+ <a id="session-list"></a>
2208
+
2209
+ ### consul.session.list([options])
2210
+
2211
+ Lists all the active sessions.
2212
+
2213
+ Options
2214
+
2215
+ - dc (String, optional): datacenter (defaults to local for agent)
2216
+
2217
+ Usage
2218
+
2219
+ ```javascript
2220
+ await consul.session.list();
2221
+ ```
2222
+
2223
+ Result
2224
+
2225
+ ```json
2226
+ [
2227
+ {
2228
+ "CreateIndex": 15,
2229
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
2230
+ "Name": "",
2231
+ "Node": "node1",
2232
+ "Checks": ["serfHealth"],
2233
+ "LockDelay": 15000000000
2234
+ }
2235
+ ]
2236
+ ```
2237
+
2238
+ <a id="session-renew"></a>
2239
+
2240
+ ### consul.session.renew(options)
2241
+
2242
+ Renew a given session.
2243
+
2244
+ Options
2245
+
2246
+ - id (String): session ID
2247
+ - dc (String, optional): datacenter (defaults to local for agent)
2248
+
2249
+ Usage
2250
+
2251
+ ```javascript
2252
+ await consul.session.renew("a0f5dc05-84c3-5f5a-1d88-05b875e524e1");
2253
+ ```
2254
+
2255
+ Result
2256
+
2257
+ ```json
2258
+ [
2259
+ {
2260
+ "CreateIndex": 15,
2261
+ "ID": "a0f5dc05-84c3-5f5a-1d88-05b875e524e1",
2262
+ "Name": "",
2263
+ "Node": "node1",
2264
+ "Checks": ["serfHealth"],
2265
+ "LockDelay": 15000000000,
2266
+ "Behavior": "release",
2267
+ "TTL": ""
2268
+ }
2269
+ ]
2270
+ ```
2271
+
2272
+ <a id="status"></a>
2273
+
2274
+ ### consul.status
2275
+
2276
+ - [leader](#status-leader)
2277
+ - [peers](#status-peers)
2278
+
2279
+ <a id="status-leader"></a>
2280
+
2281
+ ### consul.status.leader()
2282
+
2283
+ Returns the current Raft leader.
2284
+
2285
+ Usage
2286
+
2287
+ ```javascript
2288
+ await consul.status.leader();
2289
+ ```
2290
+
2291
+ Result
2292
+
2293
+ ```json
2294
+ "127.0.0.1:8300"
2295
+ ```
2296
+
2297
+ <a id="status-peers"></a>
2298
+
2299
+ ### consul.status.peers()
2300
+
2301
+ Returns the current Raft peer set.
2302
+
2303
+ Usage
2304
+
2305
+ ```javascript
2306
+ await consul.status.peers();
2307
+ ```
2308
+
2309
+ Result
2310
+
2311
+ ```json
2312
+ ["127.0.0.1:8300"]
2313
+ ```
2314
+
2315
+ <a id="transaction"></a>
2316
+
2317
+ ### consul.transaction.create(operations)
2318
+
2319
+ operations: The body of the request should be a list of operations to perform inside the atomic transaction. Up to 64 operations may be present in a single transaction.
2320
+
2321
+ Usage
2322
+
2323
+ ```javascript
2324
+ await consul.transaction.create([
2325
+ {
2326
+ {
2327
+ KV: {
2328
+ Verb: 'set',
2329
+ Key: 'key1',
2330
+ Value: Buffer.from('value1').toString('base64')
2331
+ }
2332
+ },{
2333
+ KV: {
2334
+ Verb: 'delete',
2335
+ Key: 'key2'
2336
+ }
2337
+ }
2338
+ }
2339
+ ]);
2340
+ ```
2341
+
2342
+ <a id="watch"></a>
2343
+
2344
+ ### consul.watch(options)
2345
+
2346
+ Watch an endpoint for changes.
2347
+
2348
+ The watch relies on blocking queries, adding the `index` and `wait` parameters as per [Consul's documentation](https://www.consul.io/docs/agent/http.html)
2349
+
2350
+ If a blocking query is dropped due to a Consul crash or disconnect, watch will attempt to reinitiate the blocking query with logarithmic backoff.
2351
+
2352
+ Upon reconnect, unlike the first call to watch() in which the latest `x-consul-index` is unknown, the last known `x-consul-index` will be reused, thus not emitting the `change` event unless it has been incremented since.
2353
+
2354
+ NOTE: If you specify an alternative options.timeout keep in mind that a small random amount of additional wait is added to all requests (wait / 16). The default timeout is currently set to (wait + wait \* 0.1), you should use something similar to avoid issues.
2355
+
2356
+ Options
2357
+
2358
+ - method (Function): method to watch
2359
+ - options (Object): method options
2360
+ - backoffFactor (Integer, default: 100): backoff factor in milliseconds to apply between attempts (`backoffFactor * (2 ^ retry attempt)`)
2361
+ - backoffMax (Integer, default: 30000): maximum backoff time in milliseconds to wait between attempts
2362
+ - maxAttempts (Integer): maximum number of retry attempts to make before giving up
2363
+
2364
+ Usage
2365
+
2366
+ ```javascript
2367
+ const watch = consul.watch({
2368
+ method: consul.kv.get,
2369
+ options: { key: "test" },
2370
+ backoffFactor: 1000,
2371
+ });
2372
+
2373
+ watch.on("change", (data, res) => {
2374
+ console.log("data:", data);
2375
+ });
2376
+
2377
+ watch.on("error", (err) => {
2378
+ console.log("error:", err);
2379
+ });
2380
+
2381
+ setTimeout(() => {
2382
+ watch.end();
2383
+ }, 30 * 1000);
2384
+ ```
2385
+
2386
+ ## Acceptance Tests
2387
+
2388
+ 1. Install [Consul][download] into your `PATH`
2389
+
2390
+ ```console
2391
+ $ brew install consul
2392
+ ```
2393
+
2394
+ 1. Attach required IPs
2395
+
2396
+ ```console
2397
+ $ sudo ifconfig lo0 alias 127.0.0.2 up
2398
+ $ sudo ifconfig lo0 alias 127.0.0.3 up
2399
+ ```
2400
+
2401
+ 1. Install client dependencies
2402
+
2403
+ ```console
2404
+ $ npm install
2405
+ ```
2406
+
2407
+ 1. Run tests
2408
+
2409
+ ```console
2410
+ $ npm run acceptance
2411
+ ```
2412
+
2413
+ ## License
2414
+
2415
+ This work is licensed under the MIT License (see the LICENSE file).
2416
+
2417
+ Parts of the Documentation were copied from the official
2418
+ [Consul website][consul-docs-api], see the NOTICE file for license
2419
+ information.
2420
+
2421
+ [consul]: https://www.consul.io/
2422
+ [consul-docs-api]: https://www.consul.io/api-docs
2423
+ [download]: https://www.consul.io/downloads