@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
@@ -0,0 +1,92 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface CreateOptions extends CommonOptions {
4
+ dc?: string;
5
+ lockdelay?: string;
6
+ node?: string;
7
+ name?: string;
8
+ checks?: string[];
9
+ nodechecks?: string[];
10
+ servicechecks?: { id: string; namespace?: string }[];
11
+ behavior?: "release" | "delete";
12
+ ttl?: string;
13
+ }
14
+
15
+ interface CreateResult {
16
+ ID: string;
17
+ }
18
+
19
+ interface DestroyOptions extends CommonOptions {
20
+ id: string;
21
+ dc?: string;
22
+ ns?: string;
23
+ }
24
+
25
+ type DestroyResult = boolean;
26
+
27
+ interface InfoOptions extends CommonOptions {
28
+ id: string;
29
+ dc?: string;
30
+ }
31
+
32
+ interface InfoResult {
33
+ ID: string;
34
+ Name: string;
35
+ Node: string;
36
+ LockDelay: number;
37
+ Behavior: "release" | "delete";
38
+ TTL: string;
39
+ NodeChecks: string[] | null;
40
+ ServiceChecks: string[] | null;
41
+ CreateIndex: number;
42
+ ModifyIndex: number;
43
+ }
44
+
45
+ type GetOptions = InfoOptions;
46
+
47
+ type GetResult = InfoResult;
48
+
49
+ interface NodeOptions extends CommonOptions {
50
+ node: string;
51
+ dc?: string;
52
+ }
53
+
54
+ type NodeResult = InfoResult[];
55
+
56
+ interface ListOptions extends CommonOptions {
57
+ dc?: string;
58
+ }
59
+
60
+ type ListResult = InfoResult[];
61
+
62
+ interface RenewOptions extends CommonOptions {
63
+ id: string;
64
+ dc?: string;
65
+ }
66
+
67
+ type RenewResult = InfoResult[];
68
+
69
+ declare class Session {
70
+ constructor(consul: Consul);
71
+
72
+ consul: Consul;
73
+
74
+ create(options?: CreateOptions): Promise<CreateResult>;
75
+
76
+ destroy(options: DestroyOptions): Promise<DestroyResult>;
77
+ destroy(id: string): Promise<DestroyResult>;
78
+
79
+ info(options: InfoOptions): Promise<InfoResult>;
80
+ info(id: string): Promise<InfoResult>;
81
+
82
+ get(options: GetOptions): Promise<GetResult>;
83
+ get(id: string): Promise<GetResult>;
84
+
85
+ node(options: NodeOptions): Promise<NodeResult>;
86
+ node(node: string): Promise<NodeResult>;
87
+
88
+ list(options?: ListOptions): Promise<ListResult>;
89
+
90
+ renew(options: RenewOptions): Promise<RenewResult>;
91
+ renew(id: string): Promise<RenewResult>;
92
+ }
package/lib/session.js ADDED
@@ -0,0 +1,164 @@
1
+ const errors = require("./errors");
2
+ const utils = require("./utils");
3
+
4
+ class Session {
5
+ constructor(consul) {
6
+ this.consul = consul;
7
+ }
8
+
9
+ /**
10
+ * Creates a new session
11
+ */
12
+ async create(opts) {
13
+ opts = utils.normalizeKeys(opts);
14
+ opts = utils.defaults(opts, this.consul._defaults);
15
+
16
+ const req = {
17
+ name: "session.create",
18
+ path: "/session/create",
19
+ query: {},
20
+ type: "json",
21
+ body: {},
22
+ };
23
+
24
+ if (opts.lockdelay) req.body.LockDelay = opts.lockdelay;
25
+ if (opts.name) req.body.Name = opts.name;
26
+ if (opts.node) req.body.Node = opts.node;
27
+ if (opts.checks) req.body.Checks = opts.checks;
28
+ if (opts.behavior) req.body.Behavior = opts.behavior;
29
+ if (opts.ttl) req.body.TTL = opts.ttl;
30
+
31
+ utils.options(req, opts);
32
+
33
+ return await this.consul._put(req, utils.body);
34
+ }
35
+
36
+ /**
37
+ * Destroys a given session
38
+ */
39
+ async destroy(opts) {
40
+ if (typeof opts === "string") {
41
+ opts = { id: opts };
42
+ }
43
+
44
+ opts = utils.normalizeKeys(opts);
45
+ opts = utils.defaults(opts, this.consul._defaults);
46
+
47
+ const req = {
48
+ name: "session.destroy",
49
+ path: "/session/destroy/{id}",
50
+ params: { id: opts.id },
51
+ query: {},
52
+ };
53
+
54
+ if (!opts.id) {
55
+ throw this.consul._err(errors.Validation("id required"), req);
56
+ }
57
+
58
+ utils.options(req, opts);
59
+
60
+ return await this.consul._put(req, utils.empty);
61
+ }
62
+
63
+ /**
64
+ * Queries a given session
65
+ */
66
+ async info(opts) {
67
+ if (typeof opts === "string") {
68
+ opts = { id: opts };
69
+ }
70
+
71
+ opts = utils.normalizeKeys(opts);
72
+ opts = utils.defaults(opts, this.consul._defaults);
73
+
74
+ const req = {
75
+ name: "session.info",
76
+ path: "/session/info/{id}",
77
+ params: { id: opts.id },
78
+ query: {},
79
+ };
80
+
81
+ if (!opts.id) {
82
+ throw this.consul._err(errors.Validation("id required"), req);
83
+ }
84
+
85
+ utils.options(req, opts);
86
+
87
+ return await this.consul._get(req, utils.bodyItem);
88
+ }
89
+
90
+ get(opts) {
91
+ return this.info(opts);
92
+ }
93
+
94
+ /**
95
+ * Lists sessions belonging to a node
96
+ */
97
+ async node(opts) {
98
+ if (typeof opts === "string") {
99
+ opts = { node: opts };
100
+ }
101
+
102
+ opts = utils.normalizeKeys(opts);
103
+ opts = utils.defaults(opts, this.consul._defaults);
104
+
105
+ const req = {
106
+ name: "session.node",
107
+ path: "/session/node/{node}",
108
+ params: { node: opts.node },
109
+ };
110
+
111
+ if (!opts.node) {
112
+ throw this.consul._err(errors.Validation("node required"), req);
113
+ }
114
+
115
+ utils.options(req, opts);
116
+
117
+ return await this.consul._get(req, utils.body);
118
+ }
119
+
120
+ /**
121
+ * Lists all the active sessions
122
+ */
123
+ async list(opts) {
124
+ opts = utils.normalizeKeys(opts);
125
+ opts = utils.defaults(opts, this.consul._defaults);
126
+
127
+ const req = {
128
+ name: "session.list",
129
+ path: "/session/list",
130
+ };
131
+
132
+ utils.options(req, opts);
133
+
134
+ return await this.consul._get(req, utils.body);
135
+ }
136
+
137
+ /**
138
+ * Renews a TTL-based session
139
+ */
140
+ async renew(opts) {
141
+ if (typeof opts === "string") {
142
+ opts = { id: opts };
143
+ }
144
+
145
+ opts = utils.normalizeKeys(opts);
146
+ opts = utils.defaults(opts, this.consul._defaults);
147
+
148
+ const req = {
149
+ name: "session.renew",
150
+ path: "/session/renew/{id}",
151
+ params: { id: opts.id },
152
+ };
153
+
154
+ if (!opts.id) {
155
+ throw this.consul._err(errors.Validation("id required"), req);
156
+ }
157
+
158
+ utils.options(req, opts);
159
+
160
+ return await this.consul._put(req, utils.body);
161
+ }
162
+ }
163
+
164
+ exports.Session = Session;
@@ -0,0 +1,19 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface LeaderOptions extends CommonOptions {}
4
+
5
+ type LeaderResult = string;
6
+
7
+ interface PeersOptions extends CommonOptions {}
8
+
9
+ type PeersResult = string[];
10
+
11
+ declare class Status {
12
+ constructor(consul: Consul);
13
+
14
+ consul: Consul;
15
+
16
+ leader(options?: LeaderOptions): Promise<LeaderResult>;
17
+
18
+ peers(options?: PeersOptions): Promise<PeersResult>;
19
+ }
package/lib/status.js ADDED
@@ -0,0 +1,43 @@
1
+ const utils = require("./utils");
2
+
3
+ class Status {
4
+ constructor(consul) {
5
+ this.consul = consul;
6
+ }
7
+
8
+ /**
9
+ * Returns the current Raft leader.
10
+ */
11
+ async leader(opts) {
12
+ opts = utils.normalizeKeys(opts);
13
+ opts = utils.defaults(opts, this.consul._defaults);
14
+
15
+ const req = {
16
+ name: "status.leader",
17
+ path: "/status/leader",
18
+ };
19
+
20
+ utils.options(req, opts);
21
+
22
+ return await this.consul._get(req, utils.body);
23
+ }
24
+
25
+ /**
26
+ * Returns the current Raft peer set
27
+ */
28
+ async peers(opts) {
29
+ opts = utils.normalizeKeys(opts);
30
+ opts = utils.defaults(opts, this.consul._defaults);
31
+
32
+ const req = {
33
+ name: "status.peers",
34
+ path: "/status/peers",
35
+ };
36
+
37
+ utils.options(req, opts);
38
+
39
+ return await this.consul._get(req, utils.body);
40
+ }
41
+ }
42
+
43
+ exports.Status = Status;
@@ -0,0 +1,50 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface KVOption {
4
+ verb: string;
5
+ key: string;
6
+ value?: string;
7
+ flags?: number;
8
+ index?: number;
9
+ session?: string;
10
+ namespace?: string;
11
+ }
12
+
13
+ interface NodeOption {
14
+ verb: string;
15
+ node: any;
16
+ }
17
+
18
+ interface ServiceOption {
19
+ verb: string;
20
+ node: string;
21
+ service: any;
22
+ }
23
+
24
+ interface CheckOption {
25
+ verb: string;
26
+ check: any;
27
+ }
28
+
29
+ type Operation = KVOption | NodeOption | ServiceOption | CheckOption;
30
+
31
+ interface CreateOptions extends CommonOptions {
32
+ operations: Operation[];
33
+ }
34
+
35
+ interface CreateResult {
36
+ Results?: Record<"KV" | "Node" | "Service" | "Check", any>[];
37
+ Errors?: any[];
38
+ }
39
+
40
+ declare class Transaction {
41
+ constructor(consul: Consul);
42
+
43
+ consul: Consul;
44
+
45
+ create(options: CreateOptions): Promise<CreateResult>;
46
+ create(
47
+ operations: Operation[],
48
+ options: CreateOptions,
49
+ ): Promise<CreateResult>;
50
+ }
@@ -0,0 +1,58 @@
1
+ const errors = require("./errors");
2
+ const utils = require("./utils");
3
+
4
+ class Transaction {
5
+ constructor(consul) {
6
+ this.consul = consul;
7
+ }
8
+
9
+ /**
10
+ * Create
11
+ */
12
+ async create(operations) {
13
+ let opts;
14
+ switch (arguments.length) {
15
+ case 2:
16
+ // create(operations, opts)
17
+ opts = utils.clone(arguments[1]);
18
+ opts.operations = operations;
19
+ break;
20
+ case 1:
21
+ // create(operations)
22
+ opts = { operations };
23
+ break;
24
+ default:
25
+ throw this.consul._err(
26
+ errors.Validation(
27
+ "a list of operations are required as first arguments",
28
+ ),
29
+ { name: "Transaction.create" },
30
+ );
31
+ }
32
+
33
+ opts = utils.normalizeKeys(opts);
34
+ opts = utils.defaults(opts, this.consul._defaults);
35
+
36
+ const req = {
37
+ name: "Transaction.create",
38
+ path: "/txn",
39
+ params: {},
40
+ query: {},
41
+ type: "json",
42
+ body: opts.operations,
43
+ };
44
+
45
+ if (!(Array.isArray(opts.operations) && opts.operations.length > 0)) {
46
+ throw this.consul._err(
47
+ errors.Validation("operations must be an array with at least one item"),
48
+ req,
49
+ );
50
+ }
51
+
52
+ utils.options(req, opts);
53
+
54
+ return await this.consul._put(req, utils.body);
55
+ }
56
+ }
57
+
58
+ exports.Transaction = Transaction;