@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/lib/catalog.js ADDED
@@ -0,0 +1,113 @@
1
+ const CatalogConnect = require("./catalog/connect").CatalogConnect;
2
+ const CatalogNode = require("./catalog/node").CatalogNode;
3
+ const CatalogService = require("./catalog/service").CatalogService;
4
+ const utils = require("./utils");
5
+ const errors = require("./errors");
6
+
7
+ class Catalog {
8
+ constructor(consul) {
9
+ this.consul = consul;
10
+
11
+ this.connect = new Catalog.Connect(consul);
12
+ this.node = new Catalog.Node(consul);
13
+ this.service = new Catalog.Service(consul);
14
+ }
15
+
16
+ /**
17
+ * Lists known datacenters
18
+ */
19
+ async datacenters(opts) {
20
+ opts = utils.normalizeKeys(opts);
21
+ opts = utils.defaults(opts, this.consul._defaults);
22
+
23
+ const req = {
24
+ name: "catalog.datacenters",
25
+ path: "/catalog/datacenters",
26
+ };
27
+
28
+ utils.options(req, opts);
29
+
30
+ return await this.consul._get(req, utils.body);
31
+ }
32
+
33
+ /**
34
+ * Lists nodes in a given DC
35
+ */
36
+ nodes(...args) {
37
+ return this.node.list(...args);
38
+ }
39
+
40
+ /**
41
+ * Registers or updates entries in the catalog
42
+ */
43
+ async register(opts) {
44
+ if (typeof opts === "string") {
45
+ opts = { node: opts, address: "127.0.0.1" };
46
+ }
47
+
48
+ opts = utils.normalizeKeys(opts);
49
+ opts = utils.defaults(opts, this.consul._defaults);
50
+
51
+ const req = {
52
+ name: "catalog.register",
53
+ path: "/catalog/register",
54
+ type: "json",
55
+ body: {},
56
+ };
57
+
58
+ if (!opts.node || !opts.address) {
59
+ throw this.consul._err(
60
+ errors.Validation("node and address required"),
61
+ req,
62
+ );
63
+ }
64
+
65
+ req.body = utils.createCatalogRegistration(opts);
66
+
67
+ utils.options(req, opts);
68
+
69
+ return await this.consul._put(req, utils.empty);
70
+ }
71
+
72
+ /**
73
+ * Deregister entries in the catalog
74
+ */
75
+ async deregister(opts) {
76
+ if (typeof opts === "string") {
77
+ opts = { node: opts };
78
+ }
79
+
80
+ opts = utils.normalizeKeys(opts);
81
+ opts = utils.defaults(opts, this.consul._defaults);
82
+
83
+ const req = {
84
+ name: "catalog.deregister",
85
+ path: "/catalog/deregister",
86
+ type: "json",
87
+ body: {},
88
+ };
89
+
90
+ if (!opts.node) {
91
+ throw this.consul._err(errors.Validation("node required"), req);
92
+ }
93
+
94
+ req.body = utils.createCatalogDeregistration(opts);
95
+
96
+ utils.options(req, opts);
97
+
98
+ return await this.consul._put(req, utils.empty);
99
+ }
100
+
101
+ /**
102
+ * Lists services in a given DC
103
+ */
104
+ services(...args) {
105
+ return this.service.list(...args);
106
+ }
107
+ }
108
+
109
+ Catalog.Connect = CatalogConnect;
110
+ Catalog.Node = CatalogNode;
111
+ Catalog.Service = CatalogService;
112
+
113
+ exports.Catalog = Catalog;
@@ -0,0 +1,21 @@
1
+ exports.DEFAULT_OPTIONS = [
2
+ "consistent",
3
+ "dc",
4
+ "partition",
5
+ "stale",
6
+ "timeout",
7
+ "token",
8
+ "wait",
9
+ "wan",
10
+ ];
11
+
12
+ exports.AGENT_STATUS = ["none", "alive", "leaving", "left", "failed"];
13
+
14
+ exports.CHECK_STATE = ["unknown", "passing", "warning", "critical"];
15
+
16
+ const du = (exports.DURATION_UNITS = { ns: 1 });
17
+ du.us = 1000 * du.ns;
18
+ du.ms = 1000 * du.us;
19
+ du.s = 1000 * du.ms;
20
+ du.m = 60 * du.s;
21
+ du.h = 60 * du.m;
@@ -0,0 +1,93 @@
1
+ import { Agent as httpAgent } from "http";
2
+ import { Agent as httpsAgent } from "https";
3
+ import { Acl } from "./acl";
4
+ import { Agent } from "./agent";
5
+ import { Catalog } from "./catalog";
6
+ import { Event } from "./event";
7
+ import { Health } from "./health";
8
+ import { Intention } from "./intention";
9
+ import { Kv } from "./kv";
10
+ import { Query } from "./query";
11
+ import {
12
+ Resolver,
13
+ ConsulResolverConfig,
14
+ SelectionAlgorithm,
15
+ ServiceInfo,
16
+ OptimalServiceResult,
17
+ ServiceMetrics,
18
+ } from "./resolver";
19
+ import { Session } from "./session";
20
+ import { Status } from "./status";
21
+ import { Transaction } from "./transaction";
22
+ import { Watch, WatchOptions } from "./watch";
23
+
24
+ export interface CommonOptions {
25
+ token?: string;
26
+ }
27
+
28
+ interface DefaultOptions extends CommonOptions {
29
+ dc?: string;
30
+ partition?: string;
31
+ wan?: boolean;
32
+ consistent?: boolean;
33
+ stale?: boolean;
34
+ index?: string;
35
+ wait?: string;
36
+ near?: string;
37
+ filter?: string;
38
+ }
39
+
40
+ interface ConsulOptions {
41
+ host?: string;
42
+ port?: number;
43
+ secure?: boolean;
44
+ defaults?: DefaultOptions;
45
+ agent?: httpAgent | httpsAgent;
46
+ }
47
+
48
+ declare class Consul {
49
+ constructor(options?: ConsulOptions);
50
+
51
+ acl: Acl;
52
+ agent: Agent;
53
+ catalog: Catalog;
54
+ event: Event;
55
+ health: Health;
56
+ intention: Intention;
57
+ kv: Kv;
58
+ query: Query;
59
+ session: Session;
60
+ status: Status;
61
+ transaction: Transaction;
62
+
63
+ static Acl: typeof Acl;
64
+ static Agent: typeof Agent;
65
+ static Catalog: typeof Catalog;
66
+ static Event: typeof Event;
67
+ static Health: typeof Health;
68
+ static Intention: typeof Intention;
69
+ static Kv: typeof Kv;
70
+ static Query: typeof Query;
71
+ static Resolver: typeof Resolver;
72
+ static Session: typeof Session;
73
+ static Status: typeof Status;
74
+ static Transaction: typeof Transaction;
75
+ static Watch: typeof Watch;
76
+
77
+ destroy(): void;
78
+
79
+ watch(options: WatchOptions): Watch;
80
+
81
+ resolver(config: ConsulResolverConfig): Resolver;
82
+ }
83
+
84
+ export {
85
+ SelectionAlgorithm,
86
+ ConsulResolverConfig,
87
+ ServiceInfo,
88
+ OptimalServiceResult,
89
+ ServiceMetrics,
90
+ Resolver,
91
+ };
92
+
93
+ export { Consul };
package/lib/consul.js ADDED
@@ -0,0 +1,98 @@
1
+ const papi = require("papi");
2
+
3
+ const Acl = require("./acl").Acl;
4
+ const Agent = require("./agent").Agent;
5
+ const Catalog = require("./catalog").Catalog;
6
+ const Event = require("./event").Event;
7
+ const Health = require("./health").Health;
8
+ const Intention = require("./intention").Intention;
9
+ const Kv = require("./kv").Kv;
10
+ const Query = require("./query").Query;
11
+ const Resolver = require("./resolver").Resolver;
12
+ const Session = require("./session").Session;
13
+ const Status = require("./status").Status;
14
+ const Watch = require("./watch").Watch;
15
+ const Transaction = require("./transaction").Transaction;
16
+ const utils = require("./utils");
17
+
18
+ class Consul extends papi.Client {
19
+ constructor(opts) {
20
+ opts = utils.defaults({}, opts);
21
+
22
+ if (!opts.baseUrl) {
23
+ opts.baseUrl =
24
+ (opts.secure ? "https:" : "http:") +
25
+ "//" +
26
+ (opts.host || "127.0.0.1") +
27
+ ":" +
28
+ (opts.port || 8500) +
29
+ "/v1";
30
+ }
31
+ opts.name = "consul";
32
+ opts.type = "json";
33
+
34
+ let agent;
35
+ if (!opts.agent) {
36
+ agent = utils.getAgent(opts.baseUrl);
37
+ if (agent) {
38
+ opts.agent = agent;
39
+ }
40
+ }
41
+
42
+ let defaults;
43
+ if (opts.defaults) {
44
+ defaults = utils.defaultCommonOptions(opts.defaults);
45
+ }
46
+ delete opts.defaults;
47
+
48
+ super(opts);
49
+
50
+ if (defaults) this._defaults = defaults;
51
+
52
+ this.acl = new Consul.Acl(this);
53
+ this.agent = new Consul.Agent(this);
54
+ this.catalog = new Consul.Catalog(this);
55
+ this.event = new Consul.Event(this);
56
+ this.health = new Consul.Health(this);
57
+ this.intention = new Consul.Intention(this);
58
+ this.kv = new Consul.Kv(this);
59
+ this.query = new Consul.Query(this);
60
+ this.session = new Consul.Session(this);
61
+ this.status = new Consul.Status(this);
62
+ this.transaction = new Consul.Transaction(this);
63
+ }
64
+
65
+ destroy() {
66
+ if (this._opts.agent && this._opts.agent.destroy) {
67
+ this._opts.agent.destroy();
68
+ }
69
+ }
70
+
71
+ watch(opts) {
72
+ return new Consul.Watch(this, opts);
73
+ }
74
+
75
+ resolver(config) {
76
+ return new Consul.Resolver(this, config);
77
+ }
78
+
79
+ static parseQueryMeta(res) {
80
+ return utils.parseQueryMeta(res);
81
+ }
82
+ }
83
+
84
+ Consul.Acl = Acl;
85
+ Consul.Agent = Agent;
86
+ Consul.Catalog = Catalog;
87
+ Consul.Event = Event;
88
+ Consul.Health = Health;
89
+ Consul.Intention = Intention;
90
+ Consul.Kv = Kv;
91
+ Consul.Query = Query;
92
+ Consul.Resolver = Resolver;
93
+ Consul.Session = Session;
94
+ Consul.Status = Status;
95
+ Consul.Transaction = Transaction;
96
+ Consul.Watch = Watch;
97
+
98
+ exports.Consul = Consul;
package/lib/errors.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Create error
5
+ */
6
+ function create(message) {
7
+ const error =
8
+ message instanceof Error
9
+ ? message
10
+ : new Error(message ? message : undefined);
11
+
12
+ error.isConsul = true;
13
+
14
+ return error;
15
+ }
16
+
17
+ /**
18
+ * Create validation error
19
+ */
20
+ function validation(message) {
21
+ const error = create(message);
22
+
23
+ error.isValidation = true;
24
+
25
+ return error;
26
+ }
27
+
28
+ exports.Consul = create;
29
+ exports.Validation = validation;
package/lib/event.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface FireOptions extends CommonOptions {
4
+ name: string;
5
+ payload: string | Buffer;
6
+ dc?: string;
7
+ node?: string;
8
+ service?: string;
9
+ tag?: string;
10
+ }
11
+
12
+ interface FireResult {
13
+ ID: string;
14
+ Name: string;
15
+ Payload: string;
16
+ NodeFilter: string;
17
+ ServiceFilter: string;
18
+ TagFilter: string;
19
+ Version: number;
20
+ LTime: number;
21
+ }
22
+
23
+ interface ListOptions extends CommonOptions {
24
+ name?: string;
25
+ node?: string;
26
+ service?: string;
27
+ tag?: string;
28
+ }
29
+
30
+ type ListResult = FireResult[];
31
+
32
+ declare class Event {
33
+ constructor(consul: Consul);
34
+
35
+ consul: Consul;
36
+
37
+ fire(name: string): Promise<FireResult>;
38
+ fire(name: string, payload: string | Buffer): Promise<FireResult>;
39
+ fire(options: FireOptions): Promise<FireResult>;
40
+
41
+ list(options?: ListOptions): Promise<ListResult>;
42
+ list(name: string): Promise<ListResult>;
43
+ }
package/lib/event.js ADDED
@@ -0,0 +1,91 @@
1
+ const errors = require("./errors");
2
+ const utils = require("./utils");
3
+
4
+ class Event {
5
+ constructor(consul) {
6
+ this.consul = consul;
7
+ }
8
+
9
+ /**
10
+ * Fires a new user event
11
+ */
12
+ async fire(opts) {
13
+ let options;
14
+ if (arguments.length === 2) {
15
+ options = {
16
+ name: arguments[0],
17
+ payload: arguments[1],
18
+ };
19
+ } else if (typeof opts === "string") {
20
+ options = { name: opts };
21
+ } else {
22
+ options = opts;
23
+ }
24
+
25
+ options = utils.normalizeKeys(options);
26
+ options = utils.defaults(options, this.consul._defaults);
27
+
28
+ const req = {
29
+ name: "event.fire",
30
+ path: "/event/fire/{name}",
31
+ params: { name: options.name },
32
+ query: {},
33
+ };
34
+
35
+ if (!options.name) {
36
+ throw this.consul._err(errors.Validation("name required"), req);
37
+ }
38
+
39
+ let buffer;
40
+
41
+ if (options.hasOwnProperty("payload")) {
42
+ buffer = Buffer.isBuffer(options.payload);
43
+ req.body = buffer ? options.payload : Buffer.from(options.payload);
44
+ }
45
+ if (options.node) req.query.node = options.node;
46
+ if (options.service) req.query.service = options.service;
47
+ if (options.tag) req.query.tag = options.tag;
48
+
49
+ utils.options(req, options);
50
+
51
+ return await this.consul._put(req, utils.body).then((data) => {
52
+ if (data.hasOwnProperty("Payload")) {
53
+ data.Payload = utils.decode(data.Payload, { buffer: buffer });
54
+ }
55
+ return data;
56
+ });
57
+ }
58
+
59
+ /**
60
+ * Lists the most recent events an agent has seen
61
+ */
62
+ async list(opts) {
63
+ if (typeof opts === "string") {
64
+ opts = { name: opts };
65
+ }
66
+
67
+ opts = utils.normalizeKeys(opts);
68
+ opts = utils.defaults(opts, this.consul._defaults);
69
+
70
+ const req = {
71
+ name: "event.list",
72
+ path: "/event/list",
73
+ query: {},
74
+ };
75
+
76
+ if (opts.name) req.query.name = opts.name;
77
+
78
+ utils.options(req, opts);
79
+
80
+ return await this.consul._get(req, utils.body).then((data) => {
81
+ data.forEach((item) => {
82
+ if (item.hasOwnProperty("Payload")) {
83
+ item.Payload = utils.decode(item.Payload, opts);
84
+ }
85
+ });
86
+ return data;
87
+ });
88
+ }
89
+ }
90
+
91
+ exports.Event = Event;
@@ -0,0 +1,77 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface NodeOptions extends CommonOptions {
4
+ name: string;
5
+ dc?: string;
6
+ filter?: string;
7
+ ns?: string;
8
+ }
9
+
10
+ interface Node {
11
+ ID: string;
12
+ Node: string;
13
+ CheckID: string;
14
+ Name: string;
15
+ Status: "passing" | "warning" | "critical";
16
+ Notes: string;
17
+ Output: string;
18
+ ServiceID: string;
19
+ ServiceName: string;
20
+ ServiceTags: string[];
21
+ Namespace: string;
22
+ }
23
+
24
+ type NodeResult = Node[];
25
+
26
+ interface ChecksOptions extends CommonOptions {
27
+ service: string;
28
+ dc?: string;
29
+ near?: string;
30
+ filter?: string;
31
+ ns?: string;
32
+ }
33
+
34
+ type ChecksResult = Node[];
35
+
36
+ interface ServiceOptions extends CommonOptions {
37
+ service: string;
38
+ dc?: string;
39
+ near?: string;
40
+ tag?: string;
41
+ passing?: boolean;
42
+ filter?: string;
43
+ peer?: string;
44
+ ns?: string;
45
+ }
46
+
47
+ type ServiceResult = any[];
48
+
49
+ interface StateOptions extends CommonOptions {
50
+ state: "any" | "passing" | "warning" | "critical";
51
+ dc?: string;
52
+ near?: string;
53
+ filter?: string;
54
+ ns?: string;
55
+ }
56
+
57
+ type StateResult = Node[];
58
+
59
+ declare class Health {
60
+ constructor(consul: Consul);
61
+
62
+ consul: Consul;
63
+
64
+ node(options: NodeOptions): Promise<NodeResult>;
65
+ node(name: string): Promise<NodeResult>;
66
+
67
+ checks(options: ChecksOptions): Promise<ChecksResult>;
68
+ checks(service: string): Promise<ChecksResult>;
69
+
70
+ service(options: ServiceOptions): Promise<ServiceResult>;
71
+ service(service: string): Promise<ServiceResult>;
72
+
73
+ state(options: StateOptions): Promise<StateResult>;
74
+ state(
75
+ state: "any" | "passing" | "warning" | "critical",
76
+ ): Promise<StateResult>;
77
+ }
package/lib/health.js ADDED
@@ -0,0 +1,126 @@
1
+ const constants = require("./constants");
2
+ const errors = require("./errors");
3
+ const utils = require("./utils");
4
+
5
+ class Health {
6
+ constructor(consul) {
7
+ this.consul = consul;
8
+ }
9
+
10
+ /**
11
+ * Returns the health info of a node
12
+ */
13
+ async node(opts) {
14
+ if (typeof opts === "string") {
15
+ opts = { node: opts };
16
+ }
17
+
18
+ opts = utils.normalizeKeys(opts);
19
+ opts = utils.defaults(opts, this.consul._defaults);
20
+
21
+ const req = {
22
+ name: "health.node",
23
+ path: "/health/node/{node}",
24
+ params: { node: opts.node },
25
+ };
26
+
27
+ if (!opts.node) {
28
+ throw this.consul._err(errors.Validation("node required"), req);
29
+ }
30
+
31
+ utils.options(req, opts);
32
+
33
+ return await this.consul._get(req, utils.body);
34
+ }
35
+
36
+ /**
37
+ * Returns the checks of a service
38
+ */
39
+ async checks(opts) {
40
+ if (typeof opts === "string") {
41
+ opts = { service: opts };
42
+ }
43
+
44
+ opts = utils.normalizeKeys(opts);
45
+ opts = utils.defaults(opts, this.consul._defaults);
46
+
47
+ const req = {
48
+ name: "health.checks",
49
+ path: "/health/checks/{service}",
50
+ params: { service: opts.service },
51
+ };
52
+
53
+ if (!opts.service) {
54
+ throw this.consul._err(errors.Validation("service required"), req);
55
+ }
56
+
57
+ utils.options(req, opts);
58
+
59
+ return await this.consul._get(req, utils.body);
60
+ }
61
+
62
+ /**
63
+ * Returns the nodes and health info of a service
64
+ */
65
+ async service(opts) {
66
+ if (typeof opts === "string") {
67
+ opts = { service: opts };
68
+ }
69
+
70
+ opts = utils.normalizeKeys(opts);
71
+ opts = utils.defaults(opts, this.consul._defaults);
72
+
73
+ const req = {
74
+ name: "health.service",
75
+ path: "/health/service/{service}",
76
+ params: { service: opts.service },
77
+ query: {},
78
+ };
79
+
80
+ if (!opts.service) {
81
+ throw this.consul._err(errors.Validation("service required"), req);
82
+ }
83
+
84
+ if (opts.tag) req.query.tag = opts.tag;
85
+ if (opts.passing) req.query.passing = "true";
86
+
87
+ utils.options(req, opts);
88
+
89
+ return await this.consul._get(req, utils.body);
90
+ }
91
+
92
+ /**
93
+ * Returns the checks in a given state
94
+ */
95
+ async state(opts) {
96
+ if (typeof opts === "string") {
97
+ opts = { state: opts };
98
+ }
99
+
100
+ opts = utils.normalizeKeys(opts);
101
+ opts = utils.defaults(opts, this.consul._defaults);
102
+
103
+ const req = {
104
+ name: "health.state",
105
+ path: "/health/state/{state}",
106
+ params: { state: opts.state },
107
+ };
108
+
109
+ if (!opts.state) {
110
+ throw this.consul._err(errors.Validation("state required"), req);
111
+ }
112
+
113
+ if (opts.state !== "any" && constants.CHECK_STATE.indexOf(opts.state) < 0) {
114
+ throw this.consul._err(
115
+ errors.Validation("state invalid: " + opts.state),
116
+ req,
117
+ );
118
+ }
119
+
120
+ utils.options(req, opts);
121
+
122
+ return await this.consul._get(req, utils.body);
123
+ }
124
+ }
125
+
126
+ exports.Health = Health;
package/lib/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { Consul, SelectionAlgorithm } from "./consul";
2
+ import {
3
+ ConsulResolverConfig,
4
+ ServiceInfo,
5
+ OptimalServiceResult,
6
+ ServiceMetrics,
7
+ Resolver,
8
+ } from "./resolver";
9
+
10
+ export = Consul;
11
+
12
+ export type {
13
+ ConsulResolverConfig,
14
+ ServiceInfo,
15
+ OptimalServiceResult,
16
+ ServiceMetrics,
17
+ };
18
+ export { SelectionAlgorithm, Resolver };