@routr/dispatcher 2.0.5-alpha.10
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/LICENSE +21 -0
- package/dist/config/get_config.d.ts +3 -0
- package/dist/config/get_config.js +46 -0
- package/dist/config/schema.d.ts +82 -0
- package/dist/config/schema.js +103 -0
- package/dist/connections.d.ts +12 -0
- package/dist/connections.js +20 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +49 -0
- package/dist/find_processor.d.ts +5 -0
- package/dist/find_processor.js +13 -0
- package/dist/processor.d.ts +7 -0
- package/dist/processor.js +51 -0
- package/dist/run_middlewares.d.ts +3 -0
- package/dist/run_middlewares.js +73 -0
- package/dist/run_processor.d.ts +2 -0
- package/dist/run_processor.js +47 -0
- package/dist/runner.d.ts +2 -0
- package/dist/runner.js +34 -0
- package/dist/service.d.ts +2 -0
- package/dist/service.js +9 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.js +2 -0
- package/dist/util.d.ts +6 -0
- package/dist/util.js +37 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Fonoster Inc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.getConfig = void 0;
|
|
26
|
+
const fs_1 = __importDefault(require("fs"));
|
|
27
|
+
const schema_1 = require("./schema");
|
|
28
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
29
|
+
const E = __importStar(require("fp-ts/Either"));
|
|
30
|
+
const ajv = new ajv_1.default();
|
|
31
|
+
const validate = ajv.compile(schema_1.schema);
|
|
32
|
+
const getConfig = (path) => {
|
|
33
|
+
const c = JSON.parse(fs_1.default.readFileSync(path, "utf8"));
|
|
34
|
+
if (!validate(Object.assign({}, c))) {
|
|
35
|
+
return E.left(new Error(JSON.stringify(validate.errors[0].message)));
|
|
36
|
+
}
|
|
37
|
+
// convert funcMatch to actual functions
|
|
38
|
+
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})`) };
|
|
41
|
+
});
|
|
42
|
+
// Re-insert processors with matchFunc now as a function
|
|
43
|
+
c.spec.processors = processors;
|
|
44
|
+
return E.right(c.spec);
|
|
45
|
+
};
|
|
46
|
+
exports.getConfig = getConfig;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export declare const schema: {
|
|
2
|
+
$id: string;
|
|
3
|
+
title: string;
|
|
4
|
+
description: string;
|
|
5
|
+
type: string;
|
|
6
|
+
properties: {
|
|
7
|
+
kind: {
|
|
8
|
+
description: string;
|
|
9
|
+
type: string;
|
|
10
|
+
};
|
|
11
|
+
apiVersion: {
|
|
12
|
+
description: string;
|
|
13
|
+
type: string;
|
|
14
|
+
};
|
|
15
|
+
metadata: {
|
|
16
|
+
description: string;
|
|
17
|
+
type: string;
|
|
18
|
+
properties: {
|
|
19
|
+
ref: {
|
|
20
|
+
description: string;
|
|
21
|
+
type: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
required: string[];
|
|
25
|
+
};
|
|
26
|
+
spec: {
|
|
27
|
+
description: string;
|
|
28
|
+
type: string;
|
|
29
|
+
properties: {
|
|
30
|
+
bindAddr: {
|
|
31
|
+
description: string;
|
|
32
|
+
type: string;
|
|
33
|
+
};
|
|
34
|
+
middlewares: {
|
|
35
|
+
description: string;
|
|
36
|
+
type: string;
|
|
37
|
+
items: {
|
|
38
|
+
type: string;
|
|
39
|
+
};
|
|
40
|
+
properties: {
|
|
41
|
+
ref: {
|
|
42
|
+
type: string;
|
|
43
|
+
};
|
|
44
|
+
addr: {
|
|
45
|
+
type: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
required: string[];
|
|
49
|
+
};
|
|
50
|
+
processors: {
|
|
51
|
+
description: string;
|
|
52
|
+
type: string;
|
|
53
|
+
items: {
|
|
54
|
+
type: string;
|
|
55
|
+
};
|
|
56
|
+
properties: {
|
|
57
|
+
ref: {
|
|
58
|
+
type: string;
|
|
59
|
+
};
|
|
60
|
+
isFallback: {
|
|
61
|
+
type: string;
|
|
62
|
+
};
|
|
63
|
+
addr: {
|
|
64
|
+
type: string;
|
|
65
|
+
};
|
|
66
|
+
matchFunc: {
|
|
67
|
+
type: string;
|
|
68
|
+
};
|
|
69
|
+
methods: {
|
|
70
|
+
type: string;
|
|
71
|
+
items: {
|
|
72
|
+
type: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
required: string[];
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
required: string[];
|
|
82
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.schema = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
6
|
+
* http://github.com/fonoster/routr
|
|
7
|
+
*
|
|
8
|
+
* This file is part of Routr
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the MIT License (the "License");
|
|
11
|
+
* you may not use this file except in compliance with
|
|
12
|
+
* the License. You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* https://opensource.org/licenses/MIT
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
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"
|
|
31
|
+
},
|
|
32
|
+
"apiVersion": {
|
|
33
|
+
"description": "Resource version",
|
|
34
|
+
"type": "string"
|
|
35
|
+
},
|
|
36
|
+
"metadata": {
|
|
37
|
+
"description": "Resource metadata",
|
|
38
|
+
"type": "object",
|
|
39
|
+
"properties": {
|
|
40
|
+
"ref": {
|
|
41
|
+
"description": "EdgePort reference",
|
|
42
|
+
"type": "string"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"required": ["ref"]
|
|
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"
|
|
54
|
+
},
|
|
55
|
+
"middlewares": {
|
|
56
|
+
"description": "Middleware Processors",
|
|
57
|
+
"type": "array",
|
|
58
|
+
"items": {
|
|
59
|
+
"type": "object"
|
|
60
|
+
},
|
|
61
|
+
"properties": {
|
|
62
|
+
"ref": {
|
|
63
|
+
"type": "string"
|
|
64
|
+
},
|
|
65
|
+
"addr": {
|
|
66
|
+
"type": "string"
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"required": ["ref", "addr"]
|
|
70
|
+
},
|
|
71
|
+
"processors": {
|
|
72
|
+
"description": "Message Processors",
|
|
73
|
+
"type": "array",
|
|
74
|
+
"items": {
|
|
75
|
+
"type": "object"
|
|
76
|
+
},
|
|
77
|
+
"properties": {
|
|
78
|
+
"ref": {
|
|
79
|
+
"type": "string"
|
|
80
|
+
},
|
|
81
|
+
"isFallback": {
|
|
82
|
+
"type": "boolean"
|
|
83
|
+
},
|
|
84
|
+
"addr": {
|
|
85
|
+
"type": "string"
|
|
86
|
+
},
|
|
87
|
+
"matchFunc": {
|
|
88
|
+
"type": "string"
|
|
89
|
+
},
|
|
90
|
+
"methods": {
|
|
91
|
+
"type": "array",
|
|
92
|
+
"items": {
|
|
93
|
+
"type": "string"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
"required": ["ref", "addr", "methods", "matchFunc"]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"required": ["kind", "metadata", "spec", "apiVersion"]
|
|
103
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ProcessorGPRCConnection } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a connection to all the backend processors. The function will
|
|
4
|
+
* fail if any of the backends is unavailable during initialization.
|
|
5
|
+
*
|
|
6
|
+
* @param processors list of backend processors
|
|
7
|
+
* @returns list of connections
|
|
8
|
+
*/
|
|
9
|
+
export default function connectToBackend(processors: {
|
|
10
|
+
ref: string;
|
|
11
|
+
addr: string;
|
|
12
|
+
}[]): Map<string, ProcessorGPRCConnection>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@routr/common");
|
|
4
|
+
const grpc = require("@grpc/grpc-js");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a connection to all the backend processors. The function will
|
|
7
|
+
* fail if any of the backends is unavailable during initialization.
|
|
8
|
+
*
|
|
9
|
+
* @param processors list of backend processors
|
|
10
|
+
* @returns list of connections
|
|
11
|
+
*/
|
|
12
|
+
function connectToBackend(processors) {
|
|
13
|
+
const procs = processors ? [...processors] : [];
|
|
14
|
+
const connections = new Map();
|
|
15
|
+
for (const processor of procs) {
|
|
16
|
+
connections.set(processor.ref, new common_1.PROCESSOR_OBJECT_PROTO.Processor(processor.addr, grpc.credentials.createInsecure()));
|
|
17
|
+
}
|
|
18
|
+
return connections;
|
|
19
|
+
}
|
|
20
|
+
exports.default = connectToBackend;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import grpc = require("@grpc/grpc-js");
|
|
2
|
+
export declare class NotMatchingProcessorFound extends Error {
|
|
3
|
+
code: grpc.status;
|
|
4
|
+
constructor(ref: string);
|
|
5
|
+
}
|
|
6
|
+
export declare class ProcessorUnavailableError extends Error {
|
|
7
|
+
code: number;
|
|
8
|
+
constructor(ref: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class MiddlewareUnavailableError extends Error {
|
|
11
|
+
code: number;
|
|
12
|
+
constructor(ref: string);
|
|
13
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MiddlewareUnavailableError = exports.ProcessorUnavailableError = exports.NotMatchingProcessorFound = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
6
|
+
* http://github.com/fonoster/routr
|
|
7
|
+
*
|
|
8
|
+
* This file is part of Routr
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the MIT License (the "License")
|
|
11
|
+
* you may not use this file except in compliance with
|
|
12
|
+
* the License. You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* https://opensource.org/licenses/MIT
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
const grpc = require("@grpc/grpc-js");
|
|
23
|
+
class NotMatchingProcessorFound extends Error {
|
|
24
|
+
constructor(ref) {
|
|
25
|
+
super(`not matching processor found for request ref: ${ref}`);
|
|
26
|
+
this.code = grpc.status.NOT_FOUND;
|
|
27
|
+
// Set the prototype explicitly.
|
|
28
|
+
Object.setPrototypeOf(this, NotMatchingProcessorFound.prototype);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.NotMatchingProcessorFound = NotMatchingProcessorFound;
|
|
32
|
+
class ProcessorUnavailableError extends Error {
|
|
33
|
+
constructor(ref) {
|
|
34
|
+
super(`processor ref = ${ref} is unavailable`);
|
|
35
|
+
this.code = grpc.status.UNAVAILABLE;
|
|
36
|
+
// Set the prototype explicitly.
|
|
37
|
+
Object.setPrototypeOf(this, ProcessorUnavailableError.prototype);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ProcessorUnavailableError = ProcessorUnavailableError;
|
|
41
|
+
class MiddlewareUnavailableError extends Error {
|
|
42
|
+
constructor(ref) {
|
|
43
|
+
super(`middleware ref = ${ref} is unavailable`);
|
|
44
|
+
this.code = grpc.status.UNAVAILABLE;
|
|
45
|
+
// Set the prototype explicitly.
|
|
46
|
+
Object.setPrototypeOf(this, MiddlewareUnavailableError.prototype);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.MiddlewareUnavailableError = MiddlewareUnavailableError;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { MessageRequest, ProcessorConfig } from "@routr/common";
|
|
2
|
+
import { NotMatchingProcessorFound } from "./errors";
|
|
3
|
+
export declare const hasMethod: (config: ProcessorConfig, request: MessageRequest) => boolean;
|
|
4
|
+
export declare const filter: (request: MessageRequest, config: ProcessorConfig) => boolean;
|
|
5
|
+
export declare const findProcessor: (list: Array<ProcessorConfig>) => (request: MessageRequest) => NotMatchingProcessorFound | ProcessorConfig;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findProcessor = exports.filter = exports.hasMethod = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
const hasMethod = (config, request) => config.methods.includes(request.method);
|
|
6
|
+
exports.hasMethod = hasMethod;
|
|
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));
|
|
10
|
+
exports.filter = filter;
|
|
11
|
+
const findProcessor = (list) => (request) => list.find((config) => exports.filter(request, config))
|
|
12
|
+
|| new errors_1.NotMatchingProcessorFound(request.ref);
|
|
13
|
+
exports.findProcessor = findProcessor;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ProcessorConfig } from "@routr/common";
|
|
2
|
+
import { ProcessorCallback } from "./types";
|
|
3
|
+
import { MiddlewareConfig } from "@routr/common/src/types";
|
|
4
|
+
export default function processor(params: {
|
|
5
|
+
processors: ProcessorConfig[];
|
|
6
|
+
middlewares?: MiddlewareConfig[];
|
|
7
|
+
}): (call: any, callback: ProcessorCallback) => void;
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
/*
|
|
7
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
8
|
+
* http://github.com/fonoster/routr
|
|
9
|
+
*
|
|
10
|
+
* This file is part of Routr
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the MIT License (the "License")
|
|
13
|
+
* you may not use this file except in compliance with
|
|
14
|
+
* the License. You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* https://opensource.org/licenses/MIT
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
const connections_1 = __importDefault(require("./connections"));
|
|
25
|
+
const run_processor_1 = require("./run_processor");
|
|
26
|
+
const run_middlewares_1 = require("./run_middlewares");
|
|
27
|
+
const logger_1 = __importDefault(require("@fonoster/logger"));
|
|
28
|
+
function processor(params) {
|
|
29
|
+
const { processors, middlewares } = params;
|
|
30
|
+
const procConns = connections_1.default(processors);
|
|
31
|
+
const middConns = connections_1.default(middlewares);
|
|
32
|
+
// Upstream request and callback
|
|
33
|
+
return (call, callback) => {
|
|
34
|
+
const { request } = call;
|
|
35
|
+
// 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 });
|
|
38
|
+
}
|
|
39
|
+
run_middlewares_1.runMiddlewares({ callback, request, middlewares, connections: middConns })
|
|
40
|
+
.then((req) => {
|
|
41
|
+
// 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);
|
|
44
|
+
return callback(null, req);
|
|
45
|
+
}
|
|
46
|
+
run_processor_1.runProcessor({ callback, request: req, processors, connections: procConns });
|
|
47
|
+
})
|
|
48
|
+
.catch(callback);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.default = processor;
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.runMiddlewares = void 0;
|
|
16
|
+
/*
|
|
17
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
18
|
+
* http://github.com/fonoster/routr
|
|
19
|
+
*
|
|
20
|
+
* This file is part of Routr
|
|
21
|
+
*
|
|
22
|
+
* Licensed under the MIT License (the "License")
|
|
23
|
+
* you may not use this file except in compliance with
|
|
24
|
+
* the License. You may obtain a copy of the License at
|
|
25
|
+
*
|
|
26
|
+
* https://opensource.org/licenses/MIT
|
|
27
|
+
*
|
|
28
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
29
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
30
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
31
|
+
* See the License for the specific language governing permissions and
|
|
32
|
+
* limitations under the License.
|
|
33
|
+
*/
|
|
34
|
+
const errors_1 = require("./errors");
|
|
35
|
+
const grpc = require("@grpc/grpc-js");
|
|
36
|
+
const logger_1 = __importDefault(require("@fonoster/logger"));
|
|
37
|
+
function processMessage(middlewareRef, conn, request) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
conn.processMessage(request, (err, response) => {
|
|
41
|
+
return (err === null || err === void 0 ? void 0 : err.code) === grpc.status.UNAVAILABLE
|
|
42
|
+
? reject(new errors_1.MiddlewareUnavailableError(middlewareRef))
|
|
43
|
+
: resolve(response);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function runMiddlewares(params) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const { connections, request, middlewares = [] } = params;
|
|
51
|
+
const req = Object.assign({}, request);
|
|
52
|
+
return new Promise((resolve, rejects) => __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
for (const midd of middlewares) {
|
|
54
|
+
logger_1.default.verbose("forwarding request to middleware with ref => " + midd.ref);
|
|
55
|
+
// Get the next middleware
|
|
56
|
+
const conn = connections.get(midd.ref);
|
|
57
|
+
// Send message and re-insert response for next middleware
|
|
58
|
+
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");
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
rejects(e);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
resolve(req);
|
|
70
|
+
}));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
exports.runMiddlewares = runMiddlewares;
|
|
@@ -0,0 +1,47 @@
|
|
|
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.runProcessor = 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 errors_1 = require("./errors");
|
|
26
|
+
const find_processor_1 = require("./find_processor");
|
|
27
|
+
const grpc = require("@grpc/grpc-js");
|
|
28
|
+
const logger_1 = __importDefault(require("@fonoster/logger"));
|
|
29
|
+
function runProcessor(params) {
|
|
30
|
+
const { callback, connections, processors, request } = params;
|
|
31
|
+
const matchResult = find_processor_1.findProcessor(processors)(request);
|
|
32
|
+
if ('code' in matchResult) {
|
|
33
|
+
return callback(matchResult);
|
|
34
|
+
}
|
|
35
|
+
logger_1.default.verbose("forwarding request to processor with ref => " + matchResult.ref);
|
|
36
|
+
const conn = connections.get(matchResult.ref);
|
|
37
|
+
// Connects to downstream processor
|
|
38
|
+
conn.processMessage(request, (err, response) => {
|
|
39
|
+
if ((err === null || err === void 0 ? void 0 : err.code) === grpc.status.UNAVAILABLE) {
|
|
40
|
+
// We augment the error to indicate which processor failed
|
|
41
|
+
callback(new errors_1.ProcessorUnavailableError(matchResult.ref));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
callback(err, response);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
exports.runProcessor = runProcessor;
|
package/dist/runner.d.ts
ADDED
package/dist/runner.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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 service_1 = __importDefault(require("./service"));
|
|
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);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
logger_1.default.error(result.left);
|
|
34
|
+
}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@routr/common");
|
|
4
|
+
const util_1 = require("./util");
|
|
5
|
+
function MessageDispatcher(config) {
|
|
6
|
+
const { bindAddr, processors, middlewares } = config;
|
|
7
|
+
common_1.createService(util_1.getServiceInfo(bindAddr, { processors, middlewares }));
|
|
8
|
+
}
|
|
9
|
+
exports.default = MessageDispatcher;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MessageRequest, ProcessorConfig } from "@routr/common";
|
|
2
|
+
import { MiddlewareConfig } from "@routr/common/src/types";
|
|
3
|
+
import { NotMatchingProcessorFound, ProcessorUnavailableError } from "./errors";
|
|
4
|
+
export interface MessageDispatcherConfig {
|
|
5
|
+
bindAddr: string;
|
|
6
|
+
processors: ProcessorConfig[];
|
|
7
|
+
middlewares: MiddlewareConfig[];
|
|
8
|
+
}
|
|
9
|
+
export interface ProcessorGPRCConnection {
|
|
10
|
+
processMessage: (request: unknown, callback: Function) => void;
|
|
11
|
+
}
|
|
12
|
+
export declare type CallbackErrors = NotMatchingProcessorFound | ProcessorUnavailableError | Error;
|
|
13
|
+
export declare type ProcessorCallback = (err: CallbackErrors, reponse?: MessageRequest) => void;
|
|
14
|
+
export interface RunProcessorParams {
|
|
15
|
+
callback: Function;
|
|
16
|
+
connections: Map<string, ProcessorGPRCConnection>;
|
|
17
|
+
processors: ProcessorConfig[];
|
|
18
|
+
request: MessageRequest;
|
|
19
|
+
}
|
|
20
|
+
export interface RunMiddlewaresParams {
|
|
21
|
+
callback: Function;
|
|
22
|
+
connections: Map<string, ProcessorGPRCConnection>;
|
|
23
|
+
middlewares: MiddlewareConfig[];
|
|
24
|
+
request: MessageRequest;
|
|
25
|
+
}
|
package/dist/types.js
ADDED
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ProcessorConfig, ServiceInfo } from "@routr/common";
|
|
2
|
+
import { MiddlewareConfig } from "@routr/common/src/types";
|
|
3
|
+
export declare function getServiceInfo(bindAddr: string, backends: {
|
|
4
|
+
middlewares: MiddlewareConfig[];
|
|
5
|
+
processors: ProcessorConfig[];
|
|
6
|
+
}): ServiceInfo;
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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.getServiceInfo = 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 processor_1 = __importDefault(require("./processor"));
|
|
26
|
+
const common_1 = require("@routr/common");
|
|
27
|
+
function getServiceInfo(bindAddr, backends) {
|
|
28
|
+
return {
|
|
29
|
+
name: "dispatcher",
|
|
30
|
+
bindAddr,
|
|
31
|
+
service: common_1.PROCESSOR_OBJECT_PROTO.Processor.service,
|
|
32
|
+
handlers: {
|
|
33
|
+
processMessage: processor_1.default(backends)
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.getServiceInfo = getServiceInfo;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@routr/dispatcher",
|
|
3
|
+
"version": "2.0.5-alpha.10",
|
|
4
|
+
"description": "Receives SIP messages and forwards them to the corresponding Message Processor",
|
|
5
|
+
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
|
+
"homepage": "https://github.com/fonoster/routr#readme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "dist/dispatcher",
|
|
9
|
+
"types": "dist/dispatcher",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prebuild": "rimraf ./dist tsconfig.tsbuildinfo",
|
|
12
|
+
"build": "tsc -b tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"run_dispatcher": "dist/runner.js"
|
|
16
|
+
},
|
|
17
|
+
"directories": {
|
|
18
|
+
"src": "src",
|
|
19
|
+
"test": "test"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@grpc/grpc-js": "^1.5.9",
|
|
29
|
+
"@routr/common": "^2.0.5-alpha.10",
|
|
30
|
+
"ajv": "^6.12.6",
|
|
31
|
+
"fp-ts": "^2.11.8"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/fonoster/routr.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/fonoster/routr/issues"
|
|
39
|
+
},
|
|
40
|
+
"gitHead": "10c6ff135a370a4a4634478fdaf5f42c5c13e28c"
|
|
41
|
+
}
|