@routr/location 2.0.5-alpha.2
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 +4 -0
- package/dist/config/get_config.js +92 -0
- package/dist/config/schema.d.ts +58 -0
- package/dist/config/schema.js +79 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.js +91 -0
- package/dist/locator.d.ts +13 -0
- package/dist/locator.js +152 -0
- package/dist/memory_store.d.ts +12 -0
- package/dist/memory_store.js +77 -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 +14 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.js +26 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +129 -0
- package/package.json +40 -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,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
26
|
+
if (mod && mod.__esModule) return mod;
|
|
27
|
+
var result = {};
|
|
28
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
29
|
+
__setModuleDefault(result, mod);
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
33
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
34
|
+
};
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getConfig = void 0;
|
|
37
|
+
/*
|
|
38
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
39
|
+
* http://github.com/fonoster/routr
|
|
40
|
+
*
|
|
41
|
+
* This file is part of Routr
|
|
42
|
+
*
|
|
43
|
+
* Licensed under the MIT License (the "License");
|
|
44
|
+
* you may not use this file except in compliance with
|
|
45
|
+
* the License. You may obtain a copy of the License at
|
|
46
|
+
*
|
|
47
|
+
* https://opensource.org/licenses/MIT
|
|
48
|
+
*
|
|
49
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
50
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
51
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
52
|
+
* See the License for the specific language governing permissions and
|
|
53
|
+
* limitations under the License.
|
|
54
|
+
*/
|
|
55
|
+
var types_1 = require("../types");
|
|
56
|
+
var fs_1 = __importDefault(require("fs"));
|
|
57
|
+
var schema_1 = require("./schema");
|
|
58
|
+
var ajv_1 = __importDefault(require("ajv"));
|
|
59
|
+
var E = __importStar(require("fp-ts/Either"));
|
|
60
|
+
var errors_1 = require("../errors");
|
|
61
|
+
var ajv = new ajv_1.default();
|
|
62
|
+
var validate = ajv.compile(schema_1.schema);
|
|
63
|
+
var getConfig = function (path) {
|
|
64
|
+
var c = JSON.parse(fs_1.default.readFileSync(path, "utf8"));
|
|
65
|
+
if (!validate(__assign({}, c))) {
|
|
66
|
+
return E.left(new errors_1.InvalidSchemaConfiguration(JSON.stringify(validate.errors[0].message)));
|
|
67
|
+
}
|
|
68
|
+
var config = c.spec;
|
|
69
|
+
if (config.backends) {
|
|
70
|
+
config.backends.map(function (b) {
|
|
71
|
+
// Setting round-robin by default
|
|
72
|
+
b.balancingAlgorithm = b.balancingAlgorithm
|
|
73
|
+
? b.balancingAlgorithm : types_1.LB_ALGORITHM.ROUND_ROBIN;
|
|
74
|
+
return b;
|
|
75
|
+
});
|
|
76
|
+
var hasBadCombiniation = config.backends.some(function (b) {
|
|
77
|
+
return b.balancingAlgorithm === types_1.LB_ALGORITHM.ROUND_ROBIN && b.sessionAffinity;
|
|
78
|
+
});
|
|
79
|
+
var hasBadAlgorithm = config.backends.some(function (b) {
|
|
80
|
+
return b.balancingAlgorithm !== types_1.LB_ALGORITHM.ROUND_ROBIN
|
|
81
|
+
&& b.balancingAlgorithm !== types_1.LB_ALGORITHM.LEAST_SESSIONS;
|
|
82
|
+
});
|
|
83
|
+
if (hasBadCombiniation) {
|
|
84
|
+
return E.left(new errors_1.BadAlgorithmAndAffinityCombination());
|
|
85
|
+
}
|
|
86
|
+
if (hasBadAlgorithm) {
|
|
87
|
+
return E.left(new errors_1.InvalidLoadBalancerAlgorithm());
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return E.right(config);
|
|
91
|
+
};
|
|
92
|
+
exports.getConfig = getConfig;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export declare const schema: {
|
|
2
|
+
$id: string;
|
|
3
|
+
title: string;
|
|
4
|
+
description: string;
|
|
5
|
+
type: string;
|
|
6
|
+
properties: {
|
|
7
|
+
kind: {
|
|
8
|
+
enum: string[];
|
|
9
|
+
};
|
|
10
|
+
apiVersion: {
|
|
11
|
+
enum: string[];
|
|
12
|
+
};
|
|
13
|
+
metadata: {
|
|
14
|
+
description: string;
|
|
15
|
+
type: string;
|
|
16
|
+
};
|
|
17
|
+
spec: {
|
|
18
|
+
description: string;
|
|
19
|
+
type: string;
|
|
20
|
+
properties: {
|
|
21
|
+
bindAddr: {
|
|
22
|
+
description: string;
|
|
23
|
+
type: string;
|
|
24
|
+
};
|
|
25
|
+
backends: {
|
|
26
|
+
description: string;
|
|
27
|
+
type: string;
|
|
28
|
+
items: {
|
|
29
|
+
type: string;
|
|
30
|
+
};
|
|
31
|
+
properties: {
|
|
32
|
+
ref: {
|
|
33
|
+
type: string;
|
|
34
|
+
};
|
|
35
|
+
balancingAlgorithm: {
|
|
36
|
+
enum: string[];
|
|
37
|
+
};
|
|
38
|
+
sessionAffinity: {
|
|
39
|
+
description: string;
|
|
40
|
+
type: string;
|
|
41
|
+
properties: {
|
|
42
|
+
enabled: {
|
|
43
|
+
type: string;
|
|
44
|
+
};
|
|
45
|
+
ref: {
|
|
46
|
+
type: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
required: string[];
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
required: string[];
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
required: string[];
|
|
58
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
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": "Location Service configuration",
|
|
25
|
+
"description": "Configuration for an instance of the Location Service",
|
|
26
|
+
"type": "object",
|
|
27
|
+
"properties": {
|
|
28
|
+
"kind": {
|
|
29
|
+
"enum": ["Location", "location"]
|
|
30
|
+
},
|
|
31
|
+
"apiVersion": {
|
|
32
|
+
"enum": ["v2draft1"]
|
|
33
|
+
},
|
|
34
|
+
"metadata": {
|
|
35
|
+
"description": "Resource metadata",
|
|
36
|
+
"type": "object"
|
|
37
|
+
},
|
|
38
|
+
"spec": {
|
|
39
|
+
"description": "Operations spec for Location",
|
|
40
|
+
"type": "object",
|
|
41
|
+
"properties": {
|
|
42
|
+
"bindAddr": {
|
|
43
|
+
"description": "Ipv4 interface to accept request on",
|
|
44
|
+
"type": "string"
|
|
45
|
+
},
|
|
46
|
+
"backends": {
|
|
47
|
+
"description": "Optional SIP backends",
|
|
48
|
+
"type": "array",
|
|
49
|
+
"items": {
|
|
50
|
+
"type": "object"
|
|
51
|
+
},
|
|
52
|
+
"properties": {
|
|
53
|
+
"ref": {
|
|
54
|
+
"type": "string"
|
|
55
|
+
},
|
|
56
|
+
"balancingAlgorithm": {
|
|
57
|
+
"enum": ["ROUND_ROBIN", "LEAST_SESSIONS"]
|
|
58
|
+
},
|
|
59
|
+
"sessionAffinity": {
|
|
60
|
+
"description": "Optional session affinity",
|
|
61
|
+
"type": "object",
|
|
62
|
+
"properties": {
|
|
63
|
+
"enabled": {
|
|
64
|
+
"type": "boolean"
|
|
65
|
+
},
|
|
66
|
+
"ref": {
|
|
67
|
+
"type": "string"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"required": ["ref"]
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
"required": ["ref"]
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"required": ["kind", "metadata", "spec", "apiVersion"]
|
|
79
|
+
};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import grpc = require("@grpc/grpc-js");
|
|
2
|
+
export declare type LocationError = UnsupportedSchema | NotRoutesFoundForAOR;
|
|
3
|
+
export declare type InvalidConfiguration = BadAlgorithmAndAffinityCombination | InvalidSchemaConfiguration;
|
|
4
|
+
export declare class UnsupportedSchema extends Error {
|
|
5
|
+
code: grpc.status;
|
|
6
|
+
constructor(aor: string);
|
|
7
|
+
}
|
|
8
|
+
export declare class NotRoutesFoundForAOR extends Error {
|
|
9
|
+
code: number;
|
|
10
|
+
constructor(aor: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class InvalidSchemaConfiguration extends Error {
|
|
13
|
+
code: grpc.status;
|
|
14
|
+
constructor(msg: string);
|
|
15
|
+
}
|
|
16
|
+
export declare class BadAlgorithmAndAffinityCombination extends Error {
|
|
17
|
+
code: grpc.status;
|
|
18
|
+
constructor();
|
|
19
|
+
}
|
|
20
|
+
export declare class InvalidLoadBalancerAlgorithm extends Error {
|
|
21
|
+
code: grpc.status;
|
|
22
|
+
constructor();
|
|
23
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.InvalidLoadBalancerAlgorithm = exports.BadAlgorithmAndAffinityCombination = exports.InvalidSchemaConfiguration = exports.NotRoutesFoundForAOR = exports.UnsupportedSchema = void 0;
|
|
19
|
+
/*
|
|
20
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
21
|
+
* http://github.com/fonoster/routr
|
|
22
|
+
*
|
|
23
|
+
* This file is part of Routr
|
|
24
|
+
*
|
|
25
|
+
* Licensed under the MIT License (the "License")
|
|
26
|
+
* you may not use this file except in compliance with
|
|
27
|
+
* the License. You may obtain a copy of the License at
|
|
28
|
+
*
|
|
29
|
+
* https://opensource.org/licenses/MIT
|
|
30
|
+
*
|
|
31
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
32
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
33
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
34
|
+
* See the License for the specific language governing permissions and
|
|
35
|
+
* limitations under the License.
|
|
36
|
+
*/
|
|
37
|
+
var grpc = require("@grpc/grpc-js");
|
|
38
|
+
var UnsupportedSchema = /** @class */ (function (_super) {
|
|
39
|
+
__extends(UnsupportedSchema, _super);
|
|
40
|
+
function UnsupportedSchema(aor) {
|
|
41
|
+
var _this = _super.call(this, "aor " + aor + " has an invalid schema, only \"sip:\" or \"backend:\" are allowed") || this;
|
|
42
|
+
_this.code = grpc.status.INVALID_ARGUMENT;
|
|
43
|
+
// Set the prototype explicitly.
|
|
44
|
+
Object.setPrototypeOf(_this, UnsupportedSchema.prototype);
|
|
45
|
+
return _this;
|
|
46
|
+
}
|
|
47
|
+
return UnsupportedSchema;
|
|
48
|
+
}(Error));
|
|
49
|
+
exports.UnsupportedSchema = UnsupportedSchema;
|
|
50
|
+
var NotRoutesFoundForAOR = /** @class */ (function (_super) {
|
|
51
|
+
__extends(NotRoutesFoundForAOR, _super);
|
|
52
|
+
function NotRoutesFoundForAOR(aor) {
|
|
53
|
+
var _this = _super.call(this, "no routes found for aor " + aor) || this;
|
|
54
|
+
_this.code = grpc.status.NOT_FOUND;
|
|
55
|
+
Object.setPrototypeOf(_this, NotRoutesFoundForAOR.prototype);
|
|
56
|
+
return _this;
|
|
57
|
+
}
|
|
58
|
+
return NotRoutesFoundForAOR;
|
|
59
|
+
}(Error));
|
|
60
|
+
exports.NotRoutesFoundForAOR = NotRoutesFoundForAOR;
|
|
61
|
+
var InvalidSchemaConfiguration = /** @class */ (function (_super) {
|
|
62
|
+
__extends(InvalidSchemaConfiguration, _super);
|
|
63
|
+
function InvalidSchemaConfiguration(msg) {
|
|
64
|
+
var _this = _super.call(this, msg) || this;
|
|
65
|
+
_this.code = grpc.status.INVALID_ARGUMENT;
|
|
66
|
+
Object.setPrototypeOf(_this, InvalidSchemaConfiguration.prototype);
|
|
67
|
+
return _this;
|
|
68
|
+
}
|
|
69
|
+
return InvalidSchemaConfiguration;
|
|
70
|
+
}(Error));
|
|
71
|
+
exports.InvalidSchemaConfiguration = InvalidSchemaConfiguration;
|
|
72
|
+
var BadAlgorithmAndAffinityCombination = /** @class */ (function (_super) {
|
|
73
|
+
__extends(BadAlgorithmAndAffinityCombination, _super);
|
|
74
|
+
function BadAlgorithmAndAffinityCombination() {
|
|
75
|
+
var _this = _super.call(this, "session affinity can not be combined with round-robin load balancing") || this;
|
|
76
|
+
Object.setPrototypeOf(_this, BadAlgorithmAndAffinityCombination.prototype);
|
|
77
|
+
return _this;
|
|
78
|
+
}
|
|
79
|
+
return BadAlgorithmAndAffinityCombination;
|
|
80
|
+
}(Error));
|
|
81
|
+
exports.BadAlgorithmAndAffinityCombination = BadAlgorithmAndAffinityCombination;
|
|
82
|
+
var InvalidLoadBalancerAlgorithm = /** @class */ (function (_super) {
|
|
83
|
+
__extends(InvalidLoadBalancerAlgorithm, _super);
|
|
84
|
+
function InvalidLoadBalancerAlgorithm() {
|
|
85
|
+
var _this = _super.call(this, "found invalid load balancing algorithm. must be round-robin or least-sessions") || this;
|
|
86
|
+
Object.setPrototypeOf(_this, BadAlgorithmAndAffinityCombination.prototype);
|
|
87
|
+
return _this;
|
|
88
|
+
}
|
|
89
|
+
return InvalidLoadBalancerAlgorithm;
|
|
90
|
+
}(Error));
|
|
91
|
+
exports.InvalidLoadBalancerAlgorithm = InvalidLoadBalancerAlgorithm;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AddRouteRequest, Backend, FindRoutesRequest, ILocationService, ILocatorStore, RemoveRouteRequest, Route } from "./types";
|
|
2
|
+
export default class Locator implements ILocationService {
|
|
3
|
+
private store;
|
|
4
|
+
private backends;
|
|
5
|
+
private rrCount;
|
|
6
|
+
private affinityStore;
|
|
7
|
+
constructor(store: ILocatorStore, backends?: Map<string, Backend>);
|
|
8
|
+
addRoute(request: AddRouteRequest): Promise<void>;
|
|
9
|
+
findRoutes(request: FindRoutesRequest): Promise<Route[]>;
|
|
10
|
+
removeRoutes(request: RemoveRouteRequest): Promise<void>;
|
|
11
|
+
private next;
|
|
12
|
+
private nextWithAffinity;
|
|
13
|
+
}
|
package/dist/locator.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
/*
|
|
40
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
41
|
+
* http://github.com/fonoster/routr
|
|
42
|
+
*
|
|
43
|
+
* This file is part of Routr
|
|
44
|
+
*
|
|
45
|
+
* Licensed under the MIT License (the "License")
|
|
46
|
+
* you may not use this file except in compliance with
|
|
47
|
+
* the License. You may obtain a copy of the License at
|
|
48
|
+
*
|
|
49
|
+
* https://opensource.org/licenses/MIT
|
|
50
|
+
*
|
|
51
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
52
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
53
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
54
|
+
* See the License for the specific language governing permissions and
|
|
55
|
+
* limitations under the License.
|
|
56
|
+
*/
|
|
57
|
+
var errors_1 = require("./errors");
|
|
58
|
+
var types_1 = require("./types");
|
|
59
|
+
var utils_1 = require("./utils");
|
|
60
|
+
var Locator = /** @class */ (function () {
|
|
61
|
+
// Should fail if any backend has sessionAffinity and round-robin
|
|
62
|
+
function Locator(store, backends) {
|
|
63
|
+
var _this = this;
|
|
64
|
+
if (backends === void 0) { backends = new Map(); }
|
|
65
|
+
this.store = store;
|
|
66
|
+
this.backends = backends;
|
|
67
|
+
this.rrCount = new Map();
|
|
68
|
+
this.affinityStore = new Map();
|
|
69
|
+
this.backends.forEach(function (value, key) { return _this.rrCount.set(key, 0); });
|
|
70
|
+
}
|
|
71
|
+
Locator.prototype.addRoute = function (request) {
|
|
72
|
+
if (!request.aor.startsWith('sip:') &&
|
|
73
|
+
!request.aor.startsWith('backend:')) {
|
|
74
|
+
throw new errors_1.UnsupportedSchema(request.aor);
|
|
75
|
+
}
|
|
76
|
+
return this.store.put(request.aor, request.route);
|
|
77
|
+
};
|
|
78
|
+
Locator.prototype.findRoutes = function (request) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
80
|
+
var routes, _a, backend, _b, _c;
|
|
81
|
+
return __generator(this, function (_d) {
|
|
82
|
+
switch (_d.label) {
|
|
83
|
+
case 0:
|
|
84
|
+
if (!request.labels) return [3 /*break*/, 2];
|
|
85
|
+
return [4 /*yield*/, this.store.get(request.aor)];
|
|
86
|
+
case 1:
|
|
87
|
+
_a = (_d.sent())
|
|
88
|
+
.filter(utils_1.filterOnlyMatchingLabels(request.labels));
|
|
89
|
+
return [3 /*break*/, 4];
|
|
90
|
+
case 2: return [4 /*yield*/, this.store.get(request.aor)];
|
|
91
|
+
case 3:
|
|
92
|
+
_a = (_d.sent()) || [];
|
|
93
|
+
_d.label = 4;
|
|
94
|
+
case 4:
|
|
95
|
+
routes = _a;
|
|
96
|
+
if (!request.aor.startsWith('sip:')) return [3 /*break*/, 5];
|
|
97
|
+
return [2 /*return*/, routes];
|
|
98
|
+
case 5:
|
|
99
|
+
if (!request.aor.startsWith('backend:')) return [3 /*break*/, 9];
|
|
100
|
+
backend = this.backends.get(request.aor);
|
|
101
|
+
if (!backend) {
|
|
102
|
+
throw new errors_1.NotRoutesFoundForAOR(request.aor);
|
|
103
|
+
}
|
|
104
|
+
if (!!utils_1.hasAffinitySession(backend)) return [3 /*break*/, 6];
|
|
105
|
+
_b = [this.next(routes, backend)];
|
|
106
|
+
return [3 /*break*/, 8];
|
|
107
|
+
case 6:
|
|
108
|
+
_c = this.nextWithAffinity;
|
|
109
|
+
return [4 /*yield*/, routes];
|
|
110
|
+
case 7:
|
|
111
|
+
_b = [_c.apply(this, [_d.sent(), backend.sessionAffinity.ref])];
|
|
112
|
+
_d.label = 8;
|
|
113
|
+
case 8:
|
|
114
|
+
// If it has not affinity sesssion then get next
|
|
115
|
+
return [2 /*return*/, _b];
|
|
116
|
+
case 9: throw new errors_1.UnsupportedSchema(request.aor);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
Locator.prototype.removeRoutes = function (request) {
|
|
122
|
+
return this.store.delete(request.aor);
|
|
123
|
+
};
|
|
124
|
+
Locator.prototype.next = function (routes, backend) {
|
|
125
|
+
if (backend.balancingAlgorithm === types_1.LB_ALGORITHM.LEAST_SESSIONS) {
|
|
126
|
+
return routes.sort(function (r1, r2) { return r1.sessionCount - r2.sessionCount; })[0];
|
|
127
|
+
}
|
|
128
|
+
// Continues using round-robin
|
|
129
|
+
var nextPosition = this.rrCount.get("backend:" + backend.ref);
|
|
130
|
+
var result = routes[nextPosition];
|
|
131
|
+
if (nextPosition >= (routes.length - 1)) {
|
|
132
|
+
// Restarting round-robin counter
|
|
133
|
+
this.rrCount.set("backend:" + backend.ref, 0);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.rrCount.set("backend:" + backend.ref, nextPosition + 1);
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
};
|
|
140
|
+
// Backend with session affinity does not support round-robin
|
|
141
|
+
Locator.prototype.nextWithAffinity = function (routes, sessionAffinityRef) {
|
|
142
|
+
var route = this.affinityStore.get(sessionAffinityRef);
|
|
143
|
+
if (route) {
|
|
144
|
+
return route;
|
|
145
|
+
}
|
|
146
|
+
route = routes.sort(function (r1, r2) { return r1.sessionCount - r2.sessionCount; })[0];
|
|
147
|
+
this.affinityStore.set(sessionAffinityRef, route);
|
|
148
|
+
return route;
|
|
149
|
+
};
|
|
150
|
+
return Locator;
|
|
151
|
+
}());
|
|
152
|
+
exports.default = Locator;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ILocatorStore, Route } from "./types";
|
|
2
|
+
export default class MemoryStore implements ILocatorStore {
|
|
3
|
+
private collections;
|
|
4
|
+
private cleanupCount;
|
|
5
|
+
private maxCyclesBeforeCleanup;
|
|
6
|
+
constructor(maxCyclesBeforeCleanup?: number);
|
|
7
|
+
put(key: string, route: Route): Promise<void>;
|
|
8
|
+
get(key: string): Promise<Route[]>;
|
|
9
|
+
delete(key: string): Promise<void>;
|
|
10
|
+
size(): number;
|
|
11
|
+
cleanup(): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
19
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
20
|
+
to[j] = from[i];
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
var utils_1 = require("./utils");
|
|
25
|
+
var MAX_CYCLES_BEFORE_CLEANUP = 10000;
|
|
26
|
+
var MemoryStore = /** @class */ (function () {
|
|
27
|
+
function MemoryStore(maxCyclesBeforeCleanup) {
|
|
28
|
+
if (maxCyclesBeforeCleanup === void 0) { maxCyclesBeforeCleanup = MAX_CYCLES_BEFORE_CLEANUP; }
|
|
29
|
+
this.collections = new Map();
|
|
30
|
+
this.maxCyclesBeforeCleanup = maxCyclesBeforeCleanup;
|
|
31
|
+
}
|
|
32
|
+
MemoryStore.prototype.put = function (key, route) {
|
|
33
|
+
var routes = __spreadArray([], __read(this.collections.get(key) || []));
|
|
34
|
+
// Avoids duplicate or expired routes
|
|
35
|
+
var filteredRoutes = routes
|
|
36
|
+
.filter(utils_1.expiredFilter)
|
|
37
|
+
.filter(function (r) { return utils_1.duplicateFilter(r, route); });
|
|
38
|
+
if (filteredRoutes.length > 0) {
|
|
39
|
+
this.collections.set(key, __spreadArray([route], __read(filteredRoutes)));
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.collections.set(key, [route]);
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
};
|
|
46
|
+
MemoryStore.prototype.get = function (key) {
|
|
47
|
+
// Cleanup every so often to avoid memory build up
|
|
48
|
+
this.cleanupCount++;
|
|
49
|
+
if (this.cleanupCount >= this.maxCyclesBeforeCleanup) {
|
|
50
|
+
this.cleanupCount = 0;
|
|
51
|
+
this.cleanup();
|
|
52
|
+
}
|
|
53
|
+
return Promise.resolve(this.collections.get(key) || []);
|
|
54
|
+
};
|
|
55
|
+
MemoryStore.prototype.delete = function (key) {
|
|
56
|
+
this.collections.delete(key);
|
|
57
|
+
return;
|
|
58
|
+
};
|
|
59
|
+
MemoryStore.prototype.size = function () {
|
|
60
|
+
return this.collections.size;
|
|
61
|
+
};
|
|
62
|
+
MemoryStore.prototype.cleanup = function () {
|
|
63
|
+
var _this = this;
|
|
64
|
+
// Remove any expired route from collection
|
|
65
|
+
this.collections.forEach(function (routes, key) {
|
|
66
|
+
var filteredRoutes = routes.filter(utils_1.expiredFilter);
|
|
67
|
+
if (filteredRoutes.length > 0) {
|
|
68
|
+
_this.collections.set(key, filteredRoutes);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
_this.collections.delete(key);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
return MemoryStore;
|
|
76
|
+
}());
|
|
77
|
+
exports.default = MemoryStore;
|
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
|
+
var logger_1 = __importDefault(require("@fonoster/logger"));
|
|
26
|
+
var service_1 = __importDefault(require("./service"));
|
|
27
|
+
var get_config_1 = require("./config/get_config");
|
|
28
|
+
var 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,14 @@
|
|
|
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
|
+
var common_1 = require("@routr/common");
|
|
7
|
+
var utils_1 = require("./utils");
|
|
8
|
+
var locator_1 = __importDefault(require("./locator"));
|
|
9
|
+
var memory_store_1 = __importDefault(require("./memory_store"));
|
|
10
|
+
function LocationService(config) {
|
|
11
|
+
var bindAddr = config.bindAddr;
|
|
12
|
+
common_1.createService(utils_1.getServiceInfo(bindAddr, new locator_1.default(new memory_store_1.default())));
|
|
13
|
+
}
|
|
14
|
+
exports.default = LocationService;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export declare enum LB_ALGORITHM {
|
|
2
|
+
ROUND_ROBIN = "round-robin",
|
|
3
|
+
LEAST_SESSIONS = "least-sessions"
|
|
4
|
+
}
|
|
5
|
+
export interface ILocationService {
|
|
6
|
+
addRoute(request: AddRouteRequest): Promise<void>;
|
|
7
|
+
findRoutes(request: FindRoutesRequest): Promise<Route[]>;
|
|
8
|
+
removeRoutes(request: RemoveRouteRequest): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export interface ILocatorStore {
|
|
11
|
+
put(key: string, route: Route): Promise<void>;
|
|
12
|
+
get(key: string): Promise<Route[]>;
|
|
13
|
+
delete(key: string): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export interface Route {
|
|
16
|
+
user: string;
|
|
17
|
+
host: string;
|
|
18
|
+
port: number;
|
|
19
|
+
transport: string;
|
|
20
|
+
registeredOn: number;
|
|
21
|
+
sessionCount?: number;
|
|
22
|
+
expires: number;
|
|
23
|
+
edgePortRef: string;
|
|
24
|
+
labels?: Map<string, string>;
|
|
25
|
+
}
|
|
26
|
+
export interface AddRouteRequest {
|
|
27
|
+
aor: string;
|
|
28
|
+
route: Route;
|
|
29
|
+
}
|
|
30
|
+
export interface FindRoutesRequest {
|
|
31
|
+
aor: string;
|
|
32
|
+
labels?: Map<string, string>;
|
|
33
|
+
sessionAffinityRef?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface FindRoutesResponse {
|
|
36
|
+
routes: Array<Route>;
|
|
37
|
+
}
|
|
38
|
+
export interface RemoveRouteRequest {
|
|
39
|
+
aor: string;
|
|
40
|
+
}
|
|
41
|
+
export interface Backend {
|
|
42
|
+
ref: string;
|
|
43
|
+
balancingAlgorithm?: LB_ALGORITHM;
|
|
44
|
+
sessionAffinity?: {
|
|
45
|
+
enabled: boolean;
|
|
46
|
+
ref: string;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export interface LocationConfig {
|
|
50
|
+
bindAddr: string;
|
|
51
|
+
backends?: Backend[];
|
|
52
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LB_ALGORITHM = 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
|
+
var LB_ALGORITHM;
|
|
23
|
+
(function (LB_ALGORITHM) {
|
|
24
|
+
LB_ALGORITHM["ROUND_ROBIN"] = "round-robin";
|
|
25
|
+
LB_ALGORITHM["LEAST_SESSIONS"] = "least-sessions";
|
|
26
|
+
})(LB_ALGORITHM = exports.LB_ALGORITHM || (exports.LB_ALGORITHM = {}));
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Backend, ILocationService, Route } from "./types";
|
|
2
|
+
import { ServiceInfo } from "@routr/common";
|
|
3
|
+
export declare const expiredFilter: (r: Route) => boolean;
|
|
4
|
+
export declare const duplicateFilter: (r1: Route, r2: Route) => boolean;
|
|
5
|
+
export declare const mergeKeyValue: (map: Map<string, string>) => string[];
|
|
6
|
+
export declare const compareArrays: (arr: string[], target: string[]) => boolean;
|
|
7
|
+
export declare const filterOnlyMatchingLabels: (requestLabels: Map<string, string>) => (route: Route) => boolean;
|
|
8
|
+
export declare const hasAffinitySession: (backend: Backend) => boolean;
|
|
9
|
+
export declare function getServiceInfo(bindAddr: string, locator: ILocationService): ServiceInfo;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.getServiceInfo = exports.hasAffinitySession = exports.filterOnlyMatchingLabels = exports.compareArrays = exports.mergeKeyValue = exports.duplicateFilter = exports.expiredFilter = void 0;
|
|
40
|
+
var common_1 = require("@routr/common");
|
|
41
|
+
var errors_1 = require("./errors");
|
|
42
|
+
var expiredFilter = function (r) {
|
|
43
|
+
return (r.expires - (Date.now() - r.registeredOn) / 1000) > 0;
|
|
44
|
+
};
|
|
45
|
+
exports.expiredFilter = expiredFilter;
|
|
46
|
+
var duplicateFilter = function (r1, r2) {
|
|
47
|
+
return !(r1.host === r2.host && r1.port === r2.port);
|
|
48
|
+
};
|
|
49
|
+
exports.duplicateFilter = duplicateFilter;
|
|
50
|
+
var mergeKeyValue = function (map) { return Array.from(map).map(function (l) { return l[0] + l[1]; }); };
|
|
51
|
+
exports.mergeKeyValue = mergeKeyValue;
|
|
52
|
+
var compareArrays = function (arr, target) {
|
|
53
|
+
return target.every(function (v) { return arr.includes(v); }) && arr.length === target.length;
|
|
54
|
+
};
|
|
55
|
+
exports.compareArrays = compareArrays;
|
|
56
|
+
var filterOnlyMatchingLabels = function (requestLabels) {
|
|
57
|
+
return function (route) { return route.labels
|
|
58
|
+
? exports.compareArrays(exports.mergeKeyValue(requestLabels), exports.mergeKeyValue(route.labels))
|
|
59
|
+
: false; };
|
|
60
|
+
};
|
|
61
|
+
exports.filterOnlyMatchingLabels = filterOnlyMatchingLabels;
|
|
62
|
+
var hasAffinitySession = function (backend) { var _a; return (_a = backend === null || backend === void 0 ? void 0 : backend.sessionAffinity) === null || _a === void 0 ? void 0 : _a.enabled; };
|
|
63
|
+
exports.hasAffinitySession = hasAffinitySession;
|
|
64
|
+
function getServiceInfo(bindAddr, locator) {
|
|
65
|
+
var _this = this;
|
|
66
|
+
return {
|
|
67
|
+
name: "location",
|
|
68
|
+
bindAddr: bindAddr,
|
|
69
|
+
service: common_1.LOCATION_OBJECT_PROTO.Location.service,
|
|
70
|
+
handlers: {
|
|
71
|
+
addRoute: function (call, callback) {
|
|
72
|
+
try {
|
|
73
|
+
locator.addRoute(call.request);
|
|
74
|
+
callback(null, {});
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
callback(e, null);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
findRoutes: function (call, callback) { return __awaiter(_this, void 0, void 0, function () {
|
|
81
|
+
var routes, e_1;
|
|
82
|
+
return __generator(this, function (_a) {
|
|
83
|
+
switch (_a.label) {
|
|
84
|
+
case 0:
|
|
85
|
+
_a.trys.push([0, 2, , 3]);
|
|
86
|
+
return [4 /*yield*/, locator.findRoutes(call.request)];
|
|
87
|
+
case 1:
|
|
88
|
+
routes = _a.sent();
|
|
89
|
+
if (routes.length == 0)
|
|
90
|
+
throw new errors_1.NotRoutesFoundForAOR(call.request.aor);
|
|
91
|
+
callback(null, {
|
|
92
|
+
routes: routes
|
|
93
|
+
});
|
|
94
|
+
return [3 /*break*/, 3];
|
|
95
|
+
case 2:
|
|
96
|
+
e_1 = _a.sent();
|
|
97
|
+
callback(e_1, null);
|
|
98
|
+
return [3 /*break*/, 3];
|
|
99
|
+
case 3: return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}); },
|
|
103
|
+
removeRoutes: function (call, callback) { return __awaiter(_this, void 0, void 0, function () {
|
|
104
|
+
var _a, _b, e_2;
|
|
105
|
+
var _c;
|
|
106
|
+
return __generator(this, function (_d) {
|
|
107
|
+
switch (_d.label) {
|
|
108
|
+
case 0:
|
|
109
|
+
_d.trys.push([0, 2, , 3]);
|
|
110
|
+
_a = callback;
|
|
111
|
+
_b = [null];
|
|
112
|
+
_c = {};
|
|
113
|
+
return [4 /*yield*/, locator.findRoutes(call.request)];
|
|
114
|
+
case 1:
|
|
115
|
+
_a.apply(void 0, _b.concat([(_c.routes = _d.sent(),
|
|
116
|
+
_c)]));
|
|
117
|
+
return [3 /*break*/, 3];
|
|
118
|
+
case 2:
|
|
119
|
+
e_2 = _d.sent();
|
|
120
|
+
callback(e_2, null);
|
|
121
|
+
return [3 /*break*/, 3];
|
|
122
|
+
case 3: return [2 /*return*/];
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}); }
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
exports.getServiceInfo = getServiceInfo;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@routr/location",
|
|
3
|
+
"version": "2.0.5-alpha.2",
|
|
4
|
+
"description": "Creates and maintains a list of routes to all registered endpoints",
|
|
5
|
+
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
|
+
"homepage": "https://github.com/fonoster/routr#readme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "dist/location",
|
|
9
|
+
"types": "dist/location",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prebuild": "rimraf ./dist tsconfig.tsbuildinfo",
|
|
12
|
+
"build": "tsc -b tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"run_location": "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
|
+
"@routr/common": "^2.0.5-alpha.2",
|
|
29
|
+
"ajv": "^6.12.6",
|
|
30
|
+
"fp-ts": "^2.11.8"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/fonoster/routr.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/fonoster/routr/issues"
|
|
38
|
+
},
|
|
39
|
+
"gitHead": "3570106be9a96cb6672de4a850a377bfa7d9bb3e"
|
|
40
|
+
}
|