@routr/dispatcher 2.0.5-alpha.9 → 2.0.6-alpha.0

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.
@@ -1,3 +1,3 @@
1
1
  import { MessageDispatcherConfig } from "../types";
2
- import * as E from 'fp-ts/Either';
2
+ import * as E from "fp-ts/Either";
3
3
  export declare const getConfig: (path: string) => E.Either<Error, MessageDispatcherConfig>;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -36,8 +40,8 @@ const getConfig = (path) => {
36
40
  }
37
41
  // convert funcMatch to actual functions
38
42
  const processors = c.spec.processors.map((p) => {
39
- const { ref, isFallback, addr, methods, matchFunc } = p;
40
- return { ref, isFallback, addr, methods, matchFunc: eval(`(${p.matchFunc})`) };
43
+ const { ref, addr, methods, matchFunc } = p;
44
+ return { ref, addr, methods, matchFunc: eval(`(${matchFunc})`) };
41
45
  });
42
46
  // Re-insert processors with matchFunc now as a function
43
47
  c.spec.processors = processors;
@@ -57,9 +57,6 @@ export declare const schema: {
57
57
  ref: {
58
58
  type: string;
59
59
  };
60
- isFallback: {
61
- type: string;
62
- };
63
60
  addr: {
64
61
  type: string;
65
62
  };
@@ -20,84 +20,81 @@ exports.schema = void 0;
20
20
  * limitations under the License.
21
21
  */
22
22
  exports.schema = {
23
- "$id": "https://json-schema.org/draft/2020-12/schema",
24
- "title": "Message Dispatcher configuration",
25
- "description": "Configuration for a Message Dispatcher instance",
26
- "type": "object",
27
- "properties": {
28
- "kind": {
29
- "description": "Resouce type",
30
- "type": "string"
23
+ $id: "https://json-schema.org/draft/2020-12/schema",
24
+ title: "Message Dispatcher configuration",
25
+ description: "Configuration for a Message Dispatcher instance",
26
+ type: "object",
27
+ properties: {
28
+ kind: {
29
+ description: "Resouce type",
30
+ type: "string"
31
31
  },
32
- "apiVersion": {
33
- "description": "Resource version",
34
- "type": "string"
32
+ apiVersion: {
33
+ description: "Resource version",
34
+ type: "string"
35
35
  },
36
- "metadata": {
37
- "description": "Resource metadata",
38
- "type": "object",
39
- "properties": {
40
- "ref": {
41
- "description": "EdgePort reference",
42
- "type": "string"
36
+ metadata: {
37
+ description: "Resource metadata",
38
+ type: "object",
39
+ properties: {
40
+ ref: {
41
+ description: "EdgePort reference",
42
+ type: "string"
43
43
  }
44
44
  },
45
- "required": ["ref"]
45
+ required: ["ref"]
46
46
  },
47
- "spec": {
48
- "description": "Operations spec for EdgePort",
49
- "type": "object",
50
- "properties": {
51
- "bindAddr": {
52
- "description": "Ipv4 interface to accept request on",
53
- "type": "string"
47
+ spec: {
48
+ description: "Operations spec for EdgePort",
49
+ type: "object",
50
+ properties: {
51
+ bindAddr: {
52
+ description: "Ipv4 interface to accept request on",
53
+ type: "string"
54
54
  },
55
- "middlewares": {
56
- "description": "Middleware Processors",
57
- "type": "array",
58
- "items": {
59
- "type": "object"
55
+ middlewares: {
56
+ description: "Middleware Processors",
57
+ type: "array",
58
+ items: {
59
+ type: "object"
60
60
  },
61
- "properties": {
62
- "ref": {
63
- "type": "string"
61
+ properties: {
62
+ ref: {
63
+ type: "string"
64
64
  },
65
- "addr": {
66
- "type": "string"
65
+ addr: {
66
+ type: "string"
67
67
  }
68
68
  },
69
- "required": ["ref", "addr"]
69
+ required: ["ref", "addr"]
70
70
  },
71
- "processors": {
72
- "description": "Message Processors",
73
- "type": "array",
74
- "items": {
75
- "type": "object"
71
+ processors: {
72
+ description: "Message Processors",
73
+ type: "array",
74
+ items: {
75
+ type: "object"
76
76
  },
77
- "properties": {
78
- "ref": {
79
- "type": "string"
77
+ properties: {
78
+ ref: {
79
+ type: "string"
80
80
  },
81
- "isFallback": {
82
- "type": "boolean"
81
+ addr: {
82
+ type: "string"
83
83
  },
84
- "addr": {
85
- "type": "string"
84
+ matchFunc: {
85
+ type: "string"
86
86
  },
87
- "matchFunc": {
88
- "type": "string"
89
- },
90
- "methods": {
91
- "type": "array",
92
- "items": {
93
- "type": "string"
87
+ methods: {
88
+ type: "array",
89
+ items: {
90
+ type: "string"
94
91
  }
95
92
  }
96
93
  },
97
- "required": ["ref", "addr", "methods", "matchFunc"]
94
+ required: ["ref", "addr", "methods", "matchFunc"]
98
95
  }
99
96
  }
100
97
  }
101
98
  },
102
- "required": ["kind", "metadata", "spec", "apiVersion"]
99
+ required: ["kind", "metadata", "spec", "apiVersion"]
103
100
  };
@@ -1,12 +1,11 @@
1
1
  import { ProcessorGPRCConnection } from "./types";
2
+ import { ProcessorConfig } from "@routr/common";
3
+ import { MiddlewareConfig } from "@routr/common/src/types";
2
4
  /**
3
5
  * Creates a connection to all the backend processors. The function will
4
6
  * fail if any of the backends is unavailable during initialization.
5
7
  *
6
- * @param processors list of backend processors
7
- * @returns list of connections
8
+ * @param {ProcessorConfig} processors list of backend processors
9
+ * @return {ProcessorGPRCConnection}
8
10
  */
9
- export default function connectToBackend(processors: {
10
- ref: string;
11
- addr: string;
12
- }[]): Map<string, ProcessorGPRCConnection>;
11
+ export default function connectToBackend(processors: ProcessorConfig[] | MiddlewareConfig[]): Map<string, ProcessorGPRCConnection>;
@@ -1,19 +1,45 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  const common_1 = require("@routr/common");
4
- const grpc = require("@grpc/grpc-js");
27
+ const grpc = __importStar(require("@grpc/grpc-js"));
5
28
  /**
6
29
  * Creates a connection to all the backend processors. The function will
7
30
  * fail if any of the backends is unavailable during initialization.
8
31
  *
9
- * @param processors list of backend processors
10
- * @returns list of connections
32
+ * @param {ProcessorConfig} processors list of backend processors
33
+ * @return {ProcessorGPRCConnection}
11
34
  */
12
35
  function connectToBackend(processors) {
13
36
  const procs = processors ? [...processors] : [];
14
37
  const connections = new Map();
38
+ // eslint-disable-next-line no-loops/no-loops
15
39
  for (const processor of procs) {
16
- connections.set(processor.ref, new common_1.PROCESSOR_OBJECT_PROTO.Processor(processor.addr, grpc.credentials.createInsecure()));
40
+ connections.set(processor.ref,
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ new common_1.PROCESSOR_OBJECT_PROTO.Processor(processor.addr, grpc.credentials.createInsecure()));
17
43
  }
18
44
  return connections;
19
45
  }
package/dist/errors.d.ts CHANGED
@@ -1,13 +1,37 @@
1
- import grpc = require("@grpc/grpc-js");
1
+ import * as grpc from "@grpc/grpc-js";
2
+ /**
3
+ * Thrown when no backened matches the request.
4
+ */
2
5
  export declare class NotMatchingProcessorFound extends Error {
3
6
  code: grpc.status;
7
+ /**
8
+ * Creates a new instance of NotMatchingProcessorFound.
9
+ *
10
+ * @param {string} ref - The reference of the processor
11
+ */
4
12
  constructor(ref: string);
5
13
  }
14
+ /**
15
+ * Thrown when the processor is not available.
16
+ */
6
17
  export declare class ProcessorUnavailableError extends Error {
7
18
  code: number;
19
+ /**
20
+ * Creates a new instance of NotMatchingProcessorFound.
21
+ *
22
+ * @param {string} ref - The reference of the processor
23
+ */
8
24
  constructor(ref: string);
9
25
  }
26
+ /**
27
+ * Thrown when the middleware is not available.
28
+ */
10
29
  export declare class MiddlewareUnavailableError extends Error {
11
30
  code: number;
31
+ /**
32
+ * Creates a new instance of NotMatchingProcessorFound.
33
+ *
34
+ * @param {string} ref - The reference of the processor
35
+ */
12
36
  constructor(ref: string);
13
37
  }
package/dist/errors.js CHANGED
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.MiddlewareUnavailableError = exports.ProcessorUnavailableError = exports.NotMatchingProcessorFound = void 0;
4
27
  /*
@@ -19,8 +42,16 @@ exports.MiddlewareUnavailableError = exports.ProcessorUnavailableError = exports
19
42
  * See the License for the specific language governing permissions and
20
43
  * limitations under the License.
21
44
  */
22
- const grpc = require("@grpc/grpc-js");
45
+ const grpc = __importStar(require("@grpc/grpc-js"));
46
+ /**
47
+ * Thrown when no backened matches the request.
48
+ */
23
49
  class NotMatchingProcessorFound extends Error {
50
+ /**
51
+ * Creates a new instance of NotMatchingProcessorFound.
52
+ *
53
+ * @param {string} ref - The reference of the processor
54
+ */
24
55
  constructor(ref) {
25
56
  super(`not matching processor found for request ref: ${ref}`);
26
57
  this.code = grpc.status.NOT_FOUND;
@@ -29,7 +60,15 @@ class NotMatchingProcessorFound extends Error {
29
60
  }
30
61
  }
31
62
  exports.NotMatchingProcessorFound = NotMatchingProcessorFound;
63
+ /**
64
+ * Thrown when the processor is not available.
65
+ */
32
66
  class ProcessorUnavailableError extends Error {
67
+ /**
68
+ * Creates a new instance of NotMatchingProcessorFound.
69
+ *
70
+ * @param {string} ref - The reference of the processor
71
+ */
33
72
  constructor(ref) {
34
73
  super(`processor ref = ${ref} is unavailable`);
35
74
  this.code = grpc.status.UNAVAILABLE;
@@ -38,7 +77,15 @@ class ProcessorUnavailableError extends Error {
38
77
  }
39
78
  }
40
79
  exports.ProcessorUnavailableError = ProcessorUnavailableError;
80
+ /**
81
+ * Thrown when the middleware is not available.
82
+ */
41
83
  class MiddlewareUnavailableError extends Error {
84
+ /**
85
+ * Creates a new instance of NotMatchingProcessorFound.
86
+ *
87
+ * @param {string} ref - The reference of the processor
88
+ */
42
89
  constructor(ref) {
43
90
  super(`middleware ref = ${ref} is unavailable`);
44
91
  this.code = grpc.status.UNAVAILABLE;
@@ -5,9 +5,10 @@ const errors_1 = require("./errors");
5
5
  const hasMethod = (config, request) => config.methods.includes(request.method);
6
6
  exports.hasMethod = hasMethod;
7
7
  // TODO: We need a way to test the matching function
8
- const filter = (request, config) => exports.hasMethod(config, request)
9
- && (config.isFallback || config.matchFunc(request));
8
+ const filter = (request, config) => (0, exports.hasMethod)(config, request) && (config === null || config === void 0 ? void 0 : config.matchFunc(request));
10
9
  exports.filter = filter;
11
- const findProcessor = (list) => (request) => list.find((config) => exports.filter(request, config))
12
- || new errors_1.NotMatchingProcessorFound(request.ref);
10
+ const findProcessor = (list) => (request) => {
11
+ var _a;
12
+ return (_a = list.find((config) => (0, exports.filter)(request, config))) !== null && _a !== void 0 ? _a : new errors_1.NotMatchingProcessorFound(request === null || request === void 0 ? void 0 : request.ref);
13
+ };
13
14
  exports.findProcessor = findProcessor;
@@ -1,7 +1,13 @@
1
1
  import { ProcessorConfig } from "@routr/common";
2
- import { ProcessorCallback } from "./types";
3
- import { MiddlewareConfig } from "@routr/common/src/types";
2
+ import { RunProcessorParams } from "./types";
3
+ import { CommonTypes as CT } from "@routr/common";
4
+ /**
5
+ * Creates a new instance of Processor.
6
+ *
7
+ * @param {object} params - The parameters of the processor
8
+ * @return {Processor}
9
+ */
4
10
  export default function processor(params: {
5
11
  processors: ProcessorConfig[];
6
- middlewares?: MiddlewareConfig[];
7
- }): (call: any, callback: ProcessorCallback) => void;
12
+ middlewares?: CT.MiddlewareConfig[];
13
+ }): (call: RunProcessorParams, callback: any) => void;
package/dist/processor.js CHANGED
@@ -23,27 +23,46 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  */
24
24
  const connections_1 = __importDefault(require("./connections"));
25
25
  const run_processor_1 = require("./run_processor");
26
+ const common_1 = require("@routr/common");
26
27
  const run_middlewares_1 = require("./run_middlewares");
27
- const logger_1 = __importDefault(require("@fonoster/logger"));
28
+ const logger_1 = require("@fonoster/logger");
29
+ const logger = (0, logger_1.getLogger)({ service: "dispatcher", filePath: __filename });
30
+ /**
31
+ * Creates a new instance of Processor.
32
+ *
33
+ * @param {object} params - The parameters of the processor
34
+ * @return {Processor}
35
+ */
28
36
  function processor(params) {
29
37
  const { processors, middlewares } = params;
30
- const procConns = connections_1.default(processors);
31
- const middConns = connections_1.default(middlewares);
38
+ const procConns = (0, connections_1.default)(processors);
39
+ const middConns = (0, connections_1.default)(middlewares);
32
40
  // Upstream request and callback
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
42
  return (call, callback) => {
34
43
  const { request } = call;
35
44
  // Messages type reponse will not be sent to middleware chain
36
- if (request.message.messageType === 'responseType') {
37
- return run_processor_1.runProcessor({ callback, request, processors, connections: procConns });
45
+ if (request.message.messageType === common_1.CommonTypes.MessageType.RESPONSE) {
46
+ return (0, run_processor_1.runProcessor)({
47
+ callback,
48
+ request,
49
+ processors,
50
+ connections: procConns
51
+ });
38
52
  }
39
- run_middlewares_1.runMiddlewares({ callback, request, middlewares, connections: middConns })
53
+ (0, run_middlewares_1.runMiddlewares)({ callback, request, middlewares, connections: middConns })
40
54
  .then((req) => {
41
55
  // Since the chain was broken we need to skip the processor and return the updated request
42
- if (req.message.messageType === "responseType") {
43
- logger_1.default.verbose("skipped processsing request ref " + req.ref);
56
+ if (req.message.messageType === common_1.CommonTypes.MessageType.RESPONSE) {
57
+ logger.silly("skipped processsing request", { ref: req.ref });
44
58
  return callback(null, req);
45
59
  }
46
- run_processor_1.runProcessor({ callback, request: req, processors, connections: procConns });
60
+ (0, run_processor_1.runProcessor)({
61
+ callback,
62
+ request: req,
63
+ processors,
64
+ connections: procConns
65
+ });
47
66
  })
48
67
  .catch(callback);
49
68
  };
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,9 +31,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
32
  });
10
33
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
34
  Object.defineProperty(exports, "__esModule", { value: true });
15
35
  exports.runMiddlewares = void 0;
16
36
  /*
@@ -32,8 +52,11 @@ exports.runMiddlewares = void 0;
32
52
  * limitations under the License.
33
53
  */
34
54
  const errors_1 = require("./errors");
35
- const grpc = require("@grpc/grpc-js");
36
- const logger_1 = __importDefault(require("@fonoster/logger"));
55
+ const common_1 = require("@routr/common");
56
+ const grpc = __importStar(require("@grpc/grpc-js"));
57
+ const logger_1 = require("@fonoster/logger");
58
+ const logger = (0, logger_1.getLogger)({ service: "dispatcher", filePath: __filename });
59
+ // eslint-disable-next-line require-jsdoc
37
60
  function processMessage(middlewareRef, conn, request) {
38
61
  return __awaiter(this, void 0, void 0, function* () {
39
62
  return new Promise((resolve, reject) => {
@@ -45,20 +68,32 @@ function processMessage(middlewareRef, conn, request) {
45
68
  });
46
69
  });
47
70
  }
71
+ // eslint-disable-next-line require-jsdoc
48
72
  function runMiddlewares(params) {
49
73
  return __awaiter(this, void 0, void 0, function* () {
50
74
  const { connections, request, middlewares = [] } = params;
51
75
  const req = Object.assign({}, request);
76
+ // eslint-disable-next-line no-async-promise-executor
52
77
  return new Promise((resolve, rejects) => __awaiter(this, void 0, void 0, function* () {
78
+ // eslint-disable-next-line no-loops/no-loops
53
79
  for (const midd of middlewares) {
54
- logger_1.default.verbose("forwarding request to middleware with ref => " + midd.ref);
80
+ logger.silly("sending request to middleware", {
81
+ ref: request.ref,
82
+ middlewareRef: midd.ref,
83
+ addr: midd.addr
84
+ });
55
85
  // Get the next middleware
56
86
  const conn = connections.get(midd.ref);
57
87
  // Send message and re-insert response for next middleware
58
88
  try {
59
- req.message = (yield processMessage(midd.ref, conn, req)).message;
60
- if (req.message.messageType === "responseType") {
61
- logger_1.default.verbose("found messageType to be responseType and broke the chain");
89
+ req.message = (yield processMessage(midd.ref, conn, req))
90
+ .message;
91
+ if (req.message.messageType === common_1.CommonTypes.MessageType.RESPONSE) {
92
+ logger.silly("found messageType to be responseType and broke the chain", {
93
+ ref: request.ref,
94
+ middlewareRef: midd.ref,
95
+ addr: midd.addr
96
+ });
62
97
  break;
63
98
  }
64
99
  }
@@ -1,2 +1,8 @@
1
1
  import { RunProcessorParams } from "./types";
2
- export declare function runProcessor(params: RunProcessorParams): any;
2
+ /**
3
+ * Runs the processor.
4
+ *
5
+ * @param {RunProcessorParams} params - The parameters for the run processor
6
+ * @return {Promise<any>}
7
+ */
8
+ export declare function runProcessor(params: RunProcessorParams): void;
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
@@ -24,15 +47,31 @@ exports.runProcessor = void 0;
24
47
  */
25
48
  const errors_1 = require("./errors");
26
49
  const find_processor_1 = require("./find_processor");
27
- const grpc = require("@grpc/grpc-js");
28
- const logger_1 = __importDefault(require("@fonoster/logger"));
50
+ const api_1 = __importDefault(require("@opentelemetry/api"));
51
+ const grpc = __importStar(require("@grpc/grpc-js"));
52
+ const logger_1 = require("@fonoster/logger");
53
+ const logger = (0, logger_1.getLogger)({ service: "dispatcher", filePath: __filename });
54
+ /**
55
+ * Runs the processor.
56
+ *
57
+ * @param {RunProcessorParams} params - The parameters for the run processor
58
+ * @return {Promise<any>}
59
+ */
29
60
  function runProcessor(params) {
61
+ const currentSpan = api_1.default.trace.getSpan(api_1.default.context.active());
62
+ // display traceid in the terminal
63
+ logger.silly(`traceid: ${currentSpan === null || currentSpan === void 0 ? void 0 : currentSpan.spanContext().traceId}`);
64
+ const tracer = api_1.default.trace.getTracer("routr-tracer");
65
+ const span = tracer.startSpan("server.js:sayHello()", { kind: 1 });
66
+ span.addEvent("invoking sayHello() to...");
30
67
  const { callback, connections, processors, request } = params;
31
- const matchResult = find_processor_1.findProcessor(processors)(request);
32
- if ('code' in matchResult) {
68
+ const matchResult = (0, find_processor_1.findProcessor)(processors)(request);
69
+ if ("code" in matchResult) {
33
70
  return callback(matchResult);
34
71
  }
35
- logger_1.default.verbose("forwarding request to processor with ref => " + matchResult.ref);
72
+ logger.silly("forwarding request to processor", {
73
+ processorRef: matchResult.ref
74
+ });
36
75
  const conn = connections.get(matchResult.ref);
37
76
  // Connects to downstream processor
38
77
  conn.processMessage(request, (err, response) => {
package/dist/runner.js CHANGED
@@ -24,11 +24,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  */
25
25
  const service_1 = __importDefault(require("./service"));
26
26
  const get_config_1 = require("./config/get_config");
27
- const logger_1 = __importDefault(require("@fonoster/logger"));
28
- const result = get_config_1.getConfig(process.env.CONFIG_PATH);
29
- if (result._tag === 'Right') {
30
- service_1.default(result.right);
27
+ const common_1 = require("@routr/common");
28
+ const logger_1 = require("@fonoster/logger");
29
+ const logger = (0, logger_1.getLogger)({ service: "dispatcher", filePath: __filename });
30
+ common_1.Assertions.assertEnvsAreSet(["CONFIG_PATH"]);
31
+ const result = (0, get_config_1.getConfig)(process.env.CONFIG_PATH);
32
+ if (result._tag === "Right") {
33
+ (0, service_1.default)(result.right);
31
34
  }
32
35
  else {
33
- logger_1.default.error(result.left);
36
+ logger.error(result.left);
34
37
  }
package/dist/service.d.ts CHANGED
@@ -1,2 +1,7 @@
1
1
  import { MessageDispatcherConfig } from "./types";
2
- export default function MessageDispatcher(config: MessageDispatcherConfig): void;
2
+ /**
3
+ * Creates a service that can be used to dispatch messages to the backend processors.
4
+ *
5
+ * @param {MessageDispatcherConfig} config - Configuration for the dispatcher
6
+ */
7
+ export default function messageDispatcher(config: MessageDispatcherConfig): void;
package/dist/service.js CHANGED
@@ -1,9 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /*
4
+ * Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
5
+ * http://github.com/fonoster/routr
6
+ *
7
+ * This file is part of Routr
8
+ *
9
+ * Licensed under the MIT License (the "License")
10
+ * you may not use this file except in compliance with
11
+ * the License. You may obtain a copy of the License at
12
+ *
13
+ * https://opensource.org/licenses/MIT
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
22
+ require("./tracer").init("dispatcher");
3
23
  const common_1 = require("@routr/common");
4
24
  const util_1 = require("./util");
5
- function MessageDispatcher(config) {
25
+ /**
26
+ * Creates a service that can be used to dispatch messages to the backend processors.
27
+ *
28
+ * @param {MessageDispatcherConfig} config - Configuration for the dispatcher
29
+ */
30
+ function messageDispatcher(config) {
6
31
  const { bindAddr, processors, middlewares } = config;
7
- common_1.createService(util_1.getServiceInfo(bindAddr, { processors, middlewares }));
32
+ (0, common_1.createService)((0, util_1.getServiceInfo)(bindAddr, { processors, middlewares }));
8
33
  }
9
- exports.default = MessageDispatcher;
34
+ exports.default = messageDispatcher;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * This function registers the instrumentations for the service.
3
+ *
4
+ * @param {string} serviceName - The name of the service.
5
+ * @return {Tracer} The tracer object.
6
+ */
7
+ export declare function init(serviceName: string): import("@opentelemetry/api").Tracer;
package/dist/tracer.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.init = void 0;
7
+ /*
8
+ * Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
9
+ * http://github.com/fonoster/routr
10
+ *
11
+ * This file is part of Routr
12
+ *
13
+ * Licensed under the MIT License (the "License")
14
+ * you may not use this file except in compliance with
15
+ * the License. You may obtain a copy of the License at
16
+ *
17
+ * https://opensource.org/licenses/MIT
18
+ *
19
+ * Unless required by applicable law or agreed to in writing, software
20
+ * distributed under the License is distributed on an "AS IS" BASIS,
21
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ * See the License for the specific language governing permissions and
23
+ * limitations under the License.
24
+ */
25
+ const api_1 = __importDefault(require("@opentelemetry/api"));
26
+ const instrumentation_1 = require("@opentelemetry/instrumentation");
27
+ const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
28
+ const resources_1 = require("@opentelemetry/resources");
29
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
30
+ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
31
+ const exporter_jaeger_1 = require("@opentelemetry/exporter-jaeger");
32
+ const instrumentation_grpc_1 = require("@opentelemetry/instrumentation-grpc");
33
+ /**
34
+ * This function registers the instrumentations for the service.
35
+ *
36
+ * @param {string} serviceName - The name of the service.
37
+ * @return {Tracer} The tracer object.
38
+ */
39
+ function init(serviceName) {
40
+ const provider = new sdk_trace_node_1.NodeTracerProvider({
41
+ resource: new resources_1.Resource({
42
+ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName
43
+ })
44
+ });
45
+ const exporter = new exporter_jaeger_1.JaegerExporter();
46
+ provider.addSpanProcessor(new sdk_trace_base_1.SimpleSpanProcessor(exporter));
47
+ provider.register();
48
+ (0, instrumentation_1.registerInstrumentations)({
49
+ instrumentations: [new instrumentation_grpc_1.GrpcInstrumentation()]
50
+ });
51
+ return api_1.default.trace.getTracer("routr-tracer");
52
+ }
53
+ exports.init = init;
package/dist/types.d.ts CHANGED
@@ -1,25 +1,27 @@
1
- import { MessageRequest, ProcessorConfig } from "@routr/common";
2
- import { MiddlewareConfig } from "@routr/common/src/types";
1
+ import { MessageRequest, ProcessorConfig, CommonTypes as CT } from "@routr/common";
3
2
  import { NotMatchingProcessorFound, ProcessorUnavailableError } from "./errors";
4
3
  export interface MessageDispatcherConfig {
5
4
  bindAddr: string;
6
5
  processors: ProcessorConfig[];
7
- middlewares: MiddlewareConfig[];
6
+ middlewares: CT.MiddlewareConfig[];
8
7
  }
9
8
  export interface ProcessorGPRCConnection {
10
- processMessage: (request: unknown, callback: Function) => void;
9
+ processMessage: (request: unknown, callback: CT.GrpcCallback) => void;
11
10
  }
12
- export declare type CallbackErrors = NotMatchingProcessorFound | ProcessorUnavailableError | Error;
11
+ export declare type CallbackErrors = NotMatchingProcessorFound | ProcessorUnavailableError | Error | {
12
+ code: number;
13
+ message?: string;
14
+ } | null;
13
15
  export declare type ProcessorCallback = (err: CallbackErrors, reponse?: MessageRequest) => void;
14
16
  export interface RunProcessorParams {
15
- callback: Function;
17
+ callback: CT.GrpcCallback;
16
18
  connections: Map<string, ProcessorGPRCConnection>;
17
19
  processors: ProcessorConfig[];
18
20
  request: MessageRequest;
19
21
  }
20
22
  export interface RunMiddlewaresParams {
21
- callback: Function;
23
+ callback: CT.GrpcCallback;
22
24
  connections: Map<string, ProcessorGPRCConnection>;
23
- middlewares: MiddlewareConfig[];
25
+ middlewares: CT.MiddlewareConfig[];
24
26
  request: MessageRequest;
25
27
  }
package/dist/util.d.ts CHANGED
@@ -1,6 +1,12 @@
1
- import { ProcessorConfig, ServiceInfo } from "@routr/common";
2
- import { MiddlewareConfig } from "@routr/common/src/types";
1
+ import { ProcessorConfig, ServiceInfo, CommonTypes as CT } from "@routr/common";
2
+ /**
3
+ * Gets the service metadata for the dispatcher.
4
+ *
5
+ * @param {string} bindAddr - The address to bind to
6
+ * @param {object} backends - The backends to use
7
+ * @return {ServiceInfo}
8
+ */
3
9
  export declare function getServiceInfo(bindAddr: string, backends: {
4
- middlewares: MiddlewareConfig[];
10
+ middlewares: CT.MiddlewareConfig[];
5
11
  processors: ProcessorConfig[];
6
12
  }): ServiceInfo;
package/dist/util.js CHANGED
@@ -24,13 +24,21 @@ exports.getServiceInfo = void 0;
24
24
  */
25
25
  const processor_1 = __importDefault(require("./processor"));
26
26
  const common_1 = require("@routr/common");
27
+ /**
28
+ * Gets the service metadata for the dispatcher.
29
+ *
30
+ * @param {string} bindAddr - The address to bind to
31
+ * @param {object} backends - The backends to use
32
+ * @return {ServiceInfo}
33
+ */
27
34
  function getServiceInfo(bindAddr, backends) {
28
35
  return {
29
36
  name: "dispatcher",
30
37
  bindAddr,
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
39
  service: common_1.PROCESSOR_OBJECT_PROTO.Processor.service,
32
40
  handlers: {
33
- processMessage: processor_1.default(backends)
41
+ processMessage: (0, processor_1.default)(backends)
34
42
  }
35
43
  };
36
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/dispatcher",
3
- "version": "2.0.5-alpha.9",
3
+ "version": "2.0.6-alpha.0",
4
4
  "description": "Receives SIP messages and forwards them to the corresponding Message Processor",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -25,8 +25,17 @@
25
25
  "access": "public"
26
26
  },
27
27
  "dependencies": {
28
+ "@fonoster/logger": "0.3.17-alpha.2",
28
29
  "@grpc/grpc-js": "^1.5.9",
29
- "@routr/common": "^2.0.5-alpha.9",
30
+ "@opentelemetry/api": "^1.0.4",
31
+ "@opentelemetry/exporter-jaeger": "^1.0.4",
32
+ "@opentelemetry/instrumentation": "^0.27.0",
33
+ "@opentelemetry/instrumentation-grpc": "^0.27.0",
34
+ "@opentelemetry/resources": "^1.0.4",
35
+ "@opentelemetry/sdk-trace-base": "^1.0.4",
36
+ "@opentelemetry/sdk-trace-node": "^1.0.4",
37
+ "@opentelemetry/semantic-conventions": "^1.0.4",
38
+ "@routr/common": "^2.0.6-alpha.0",
30
39
  "ajv": "^6.12.6",
31
40
  "fp-ts": "^2.11.8"
32
41
  },
@@ -37,5 +46,5 @@
37
46
  "bugs": {
38
47
  "url": "https://github.com/fonoster/routr/issues"
39
48
  },
40
- "gitHead": "9eeafebb3f16625aca520268ed6f44896656f3a6"
49
+ "gitHead": "0829f7e9b5b3ebba7eb984da5436d7c07689d776"
41
50
  }