@routr/dispatcher 2.0.8-alpha.27 → 2.0.8-alpha.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/schema.d.ts +3 -0
- package/dist/config/schema.js +3 -0
- package/dist/processor.d.ts +1 -1
- package/dist/processor.js +44 -23
- package/dist/run_middlewares.js +9 -5
- package/dist/run_processor.d.ts +3 -2
- package/dist/run_processor.js +32 -26
- package/dist/types.d.ts +1 -2
- package/package.json +4 -3
package/dist/config/schema.d.ts
CHANGED
package/dist/config/schema.js
CHANGED
package/dist/processor.d.ts
CHANGED
|
@@ -10,4 +10,4 @@ import { CommonTypes as CT } from "@routr/common";
|
|
|
10
10
|
export default function processor(params: {
|
|
11
11
|
processors: ProcessorConfig[];
|
|
12
12
|
middlewares?: CT.MiddlewareConfig[];
|
|
13
|
-
}): (call: RunProcessorParams, callback: any) => void
|
|
13
|
+
}): (call: RunProcessorParams, callback: any) => Promise<void>;
|
package/dist/processor.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -23,10 +32,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
23
32
|
*/
|
|
24
33
|
const connections_1 = __importDefault(require("./connections"));
|
|
25
34
|
const run_processor_1 = require("./run_processor");
|
|
26
|
-
const
|
|
35
|
+
const processor_1 = require("@routr/processor");
|
|
27
36
|
const run_middlewares_1 = require("./run_middlewares");
|
|
28
|
-
const
|
|
29
|
-
const logger = (0, logger_1.getLogger)({ service: "dispatcher", filePath: __filename });
|
|
37
|
+
const isErrorResponse = (request) => processor_1.Helper.isTypeResponse(request) && !processor_1.Helper.isOk(request) && !processor_1.Helper.isRinging(request);
|
|
30
38
|
/**
|
|
31
39
|
* Creates a new instance of Processor.
|
|
32
40
|
*
|
|
@@ -39,32 +47,45 @@ function processor(params) {
|
|
|
39
47
|
const middConns = (0, connections_1.default)(middlewares);
|
|
40
48
|
// Upstream request and callback
|
|
41
49
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
-
return (call, callback) => {
|
|
50
|
+
return (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
43
51
|
const { request } = call;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
try {
|
|
53
|
+
if (isErrorResponse(request)) {
|
|
54
|
+
callback(null, yield (0, run_processor_1.runProcessor)({
|
|
55
|
+
request,
|
|
56
|
+
processors,
|
|
57
|
+
connections: procConns
|
|
58
|
+
}));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const preProcessorRequest = yield (0, run_middlewares_1.runMiddlewares)({
|
|
48
62
|
request,
|
|
49
|
-
|
|
50
|
-
connections:
|
|
63
|
+
middlewares,
|
|
64
|
+
connections: middConns,
|
|
65
|
+
runPostProcessorMiddlewares: false
|
|
51
66
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.then((req) => {
|
|
55
|
-
// Since the chain was broken we need to skip the processor and return the updated request
|
|
56
|
-
if (req.message.messageType === common_1.CommonTypes.MessageType.RESPONSE) {
|
|
57
|
-
logger.silly("skipped processsing request", { ref: req.ref });
|
|
58
|
-
return callback(null, req);
|
|
67
|
+
if (isErrorResponse(request)) {
|
|
68
|
+
return callback(null, preProcessorRequest);
|
|
59
69
|
}
|
|
60
|
-
(0, run_processor_1.runProcessor)({
|
|
61
|
-
|
|
62
|
-
request: req,
|
|
70
|
+
const processedRequest = yield (0, run_processor_1.runProcessor)({
|
|
71
|
+
request: preProcessorRequest,
|
|
63
72
|
processors,
|
|
64
73
|
connections: procConns
|
|
65
74
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
75
|
+
if (isErrorResponse(request)) {
|
|
76
|
+
return callback(null, processedRequest);
|
|
77
|
+
}
|
|
78
|
+
const postProcessorRequest = yield (0, run_middlewares_1.runMiddlewares)({
|
|
79
|
+
request: processedRequest,
|
|
80
|
+
middlewares,
|
|
81
|
+
connections: middConns,
|
|
82
|
+
runPostProcessorMiddlewares: true
|
|
83
|
+
});
|
|
84
|
+
return callback(null, postProcessorRequest);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
return callback(err, null);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
69
90
|
}
|
|
70
91
|
exports.default = processor;
|
package/dist/run_middlewares.js
CHANGED
|
@@ -71,17 +71,21 @@ function processMessage(middlewareRef, conn, request) {
|
|
|
71
71
|
// eslint-disable-next-line require-jsdoc
|
|
72
72
|
function runMiddlewares(params) {
|
|
73
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
const { connections, request, middlewares = [] } = params;
|
|
74
|
+
const { connections, request, middlewares = [], runPostProcessorMiddlewares } = params;
|
|
75
75
|
const req = Object.assign({}, request);
|
|
76
76
|
// eslint-disable-next-line no-async-promise-executor
|
|
77
77
|
return new Promise((resolve, rejects) => __awaiter(this, void 0, void 0, function* () {
|
|
78
78
|
// eslint-disable-next-line no-loops/no-loops
|
|
79
79
|
for (const midd of middlewares) {
|
|
80
|
-
logger.
|
|
80
|
+
logger.verbose("sending request to middleware", {
|
|
81
81
|
ref: request.ref,
|
|
82
82
|
middlewareRef: midd.ref,
|
|
83
|
-
addr: midd.addr
|
|
83
|
+
addr: midd.addr,
|
|
84
|
+
postProcessor: midd.postProcessor,
|
|
85
|
+
runPostProcessorMiddlewares
|
|
84
86
|
});
|
|
87
|
+
if (runPostProcessorMiddlewares !== midd.postProcessor)
|
|
88
|
+
continue;
|
|
85
89
|
// Get the next middleware
|
|
86
90
|
const conn = connections.get(midd.ref);
|
|
87
91
|
// Send message and re-insert response for next middleware
|
|
@@ -89,7 +93,7 @@ function runMiddlewares(params) {
|
|
|
89
93
|
req.message = (yield processMessage(midd.ref, conn, req))
|
|
90
94
|
.message;
|
|
91
95
|
if (req.message.messageType === common_1.CommonTypes.MessageType.RESPONSE) {
|
|
92
|
-
logger.
|
|
96
|
+
logger.verbose("found messageType to be responseType and broke the chain", {
|
|
93
97
|
ref: request.ref,
|
|
94
98
|
middlewareRef: midd.ref,
|
|
95
99
|
addr: midd.addr
|
|
@@ -98,7 +102,7 @@ function runMiddlewares(params) {
|
|
|
98
102
|
}
|
|
99
103
|
}
|
|
100
104
|
catch (e) {
|
|
101
|
-
rejects(e);
|
|
105
|
+
return rejects(e);
|
|
102
106
|
}
|
|
103
107
|
}
|
|
104
108
|
resolve(req);
|
package/dist/run_processor.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { RunProcessorParams } from "./types";
|
|
2
|
+
import { MessageRequest } from "@routr/common";
|
|
2
3
|
/**
|
|
3
4
|
* Runs the processor.
|
|
4
5
|
*
|
|
5
6
|
* @param {RunProcessorParams} params - The parameters for the run processor
|
|
6
|
-
* @return {Promise<
|
|
7
|
+
* @return {Promise<MessageRequest>}
|
|
7
8
|
*/
|
|
8
|
-
export declare function runProcessor(params: RunProcessorParams):
|
|
9
|
+
export declare function runProcessor(params: RunProcessorParams): Promise<MessageRequest>;
|
package/dist/run_processor.js
CHANGED
|
@@ -22,6 +22,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
25
34
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
36
|
};
|
|
@@ -45,42 +54,39 @@ exports.runProcessor = void 0;
|
|
|
45
54
|
* See the License for the specific language governing permissions and
|
|
46
55
|
* limitations under the License.
|
|
47
56
|
*/
|
|
57
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
58
|
+
const api_1 = __importDefault(require("@opentelemetry/api"));
|
|
48
59
|
const errors_1 = require("./errors");
|
|
49
60
|
const find_processor_1 = require("./find_processor");
|
|
50
|
-
const api_1 = __importDefault(require("@opentelemetry/api"));
|
|
51
|
-
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
52
61
|
const logger_1 = require("@fonoster/logger");
|
|
53
62
|
const logger = (0, logger_1.getLogger)({ service: "dispatcher", filePath: __filename });
|
|
54
63
|
/**
|
|
55
64
|
* Runs the processor.
|
|
56
65
|
*
|
|
57
66
|
* @param {RunProcessorParams} params - The parameters for the run processor
|
|
58
|
-
* @return {Promise<
|
|
67
|
+
* @return {Promise<MessageRequest>}
|
|
59
68
|
*/
|
|
60
69
|
function runProcessor(params) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
callback(err, response);
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const currentSpan = api_1.default.trace.getSpan(api_1.default.context.active());
|
|
72
|
+
// Display traceid in the terminal
|
|
73
|
+
logger.verbose(`traceid: ${currentSpan === null || currentSpan === void 0 ? void 0 : currentSpan.spanContext().traceId}`);
|
|
74
|
+
const { connections, processors, request } = params;
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const matchResult = (0, find_processor_1.findProcessor)(processors)(request);
|
|
77
|
+
if ("code" in matchResult) {
|
|
78
|
+
return reject(matchResult);
|
|
79
|
+
}
|
|
80
|
+
const conn = connections.get(matchResult.ref);
|
|
81
|
+
logger.verbose("forwarding request to processor", { ref: matchResult.ref });
|
|
82
|
+
conn.processMessage(request, (err, result) => {
|
|
83
|
+
if ((err === null || err === void 0 ? void 0 : err.code) === grpc.status.UNAVAILABLE) {
|
|
84
|
+
// We augment the error to indicate which processor failed
|
|
85
|
+
return reject(new errors_1.ProcessorUnavailableError(matchResult.ref));
|
|
86
|
+
}
|
|
87
|
+
resolve(Object.assign(Object.assign({}, request), result));
|
|
88
|
+
});
|
|
89
|
+
});
|
|
84
90
|
});
|
|
85
91
|
}
|
|
86
92
|
exports.runProcessor = runProcessor;
|
package/dist/types.d.ts
CHANGED
|
@@ -14,14 +14,13 @@ export type CallbackErrors = NotMatchingProcessorFound | ProcessorUnavailableErr
|
|
|
14
14
|
} | null;
|
|
15
15
|
export type ProcessorCallback = (err: CallbackErrors, reponse?: MessageRequest) => void;
|
|
16
16
|
export interface RunProcessorParams {
|
|
17
|
-
callback: CT.GrpcCallback;
|
|
18
17
|
connections: Map<string, ProcessorGPRCConnection>;
|
|
19
18
|
processors: ProcessorConfig[];
|
|
20
19
|
request: MessageRequest;
|
|
21
20
|
}
|
|
22
21
|
export interface RunMiddlewaresParams {
|
|
23
|
-
callback: CT.GrpcCallback;
|
|
24
22
|
connections: Map<string, ProcessorGPRCConnection>;
|
|
25
23
|
middlewares: CT.MiddlewareConfig[];
|
|
26
24
|
request: MessageRequest;
|
|
25
|
+
runPostProcessorMiddlewares: boolean;
|
|
27
26
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@routr/dispatcher",
|
|
3
|
-
"version": "2.0.8-alpha.
|
|
3
|
+
"version": "2.0.8-alpha.29",
|
|
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",
|
|
@@ -35,7 +35,8 @@
|
|
|
35
35
|
"@opentelemetry/sdk-trace-base": "^1.0.4",
|
|
36
36
|
"@opentelemetry/sdk-trace-node": "^1.0.4",
|
|
37
37
|
"@opentelemetry/semantic-conventions": "^1.0.4",
|
|
38
|
-
"@routr/common": "^2.0.8-alpha.
|
|
38
|
+
"@routr/common": "^2.0.8-alpha.29",
|
|
39
|
+
"@routr/processor": "^2.0.8-alpha.29",
|
|
39
40
|
"ajv": "^6.12.6",
|
|
40
41
|
"fp-ts": "^2.11.8"
|
|
41
42
|
},
|
|
@@ -46,5 +47,5 @@
|
|
|
46
47
|
"bugs": {
|
|
47
48
|
"url": "https://github.com/fonoster/routr/issues"
|
|
48
49
|
},
|
|
49
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "f273cfb052bd734a906ab97011e1338ceac53111"
|
|
50
51
|
}
|