@routr/registry 2.0.5-alpha.22 → 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.
- package/dist/config/schema.d.ts +0 -4
- package/dist/config/schema.js +0 -4
- package/dist/memory_store.d.ts +1 -1
- package/dist/memory_store.js +22 -14
- package/dist/request.js +8 -8
- package/dist/service.js +12 -6
- package/dist/types.d.ts +0 -1
- package/dist/utils.js +30 -36
- package/package.json +4 -4
package/dist/config/schema.d.ts
CHANGED
package/dist/config/schema.js
CHANGED
|
@@ -39,10 +39,6 @@ exports.schema = {
|
|
|
39
39
|
description: "Operations spec for Registry",
|
|
40
40
|
type: "object",
|
|
41
41
|
properties: {
|
|
42
|
-
bindAddr: {
|
|
43
|
-
description: "Ipv4 interface to accept request on",
|
|
44
|
-
type: "string"
|
|
45
|
-
},
|
|
46
42
|
requesterAddr: {
|
|
47
43
|
description: "Address of service to send requests to",
|
|
48
44
|
type: "string"
|
package/dist/memory_store.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { IRegistryStore, RegistrationEntry } from "./types";
|
|
|
3
3
|
* In-memory store for the registry service.
|
|
4
4
|
*/
|
|
5
5
|
export default class MemoryStore implements IRegistryStore {
|
|
6
|
-
private
|
|
6
|
+
private map;
|
|
7
7
|
private cleanupCount;
|
|
8
8
|
private maxCyclesBeforeCleanup;
|
|
9
9
|
/**
|
package/dist/memory_store.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const notExpired = (entry) => {
|
|
4
|
+
const timeElapsed = (Date.now() - entry.timeOfEntry) / 1000;
|
|
5
|
+
return timeElapsed <= entry.retentionTimeInSeconds;
|
|
6
|
+
};
|
|
3
7
|
const MAX_CYCLES_BEFORE_CLEANUP = 10000;
|
|
4
8
|
/**
|
|
5
9
|
* In-memory store for the registry service.
|
|
@@ -11,40 +15,44 @@ class MemoryStore {
|
|
|
11
15
|
* @param {number} maxCyclesBeforeCleanup - Maximum number of cycles before cleanup
|
|
12
16
|
*/
|
|
13
17
|
constructor(maxCyclesBeforeCleanup = MAX_CYCLES_BEFORE_CLEANUP) {
|
|
14
|
-
this.
|
|
18
|
+
this.map = new Map();
|
|
15
19
|
this.maxCyclesBeforeCleanup = maxCyclesBeforeCleanup;
|
|
16
20
|
}
|
|
17
21
|
/** @inheritdoc */
|
|
18
22
|
put(key, entry) {
|
|
19
|
-
this.
|
|
23
|
+
this.map.set(key, entry);
|
|
24
|
+
// Cleanup every so often to avoid memory build up
|
|
25
|
+
this.cleanupCount++;
|
|
26
|
+
if (this.cleanupCount >= this.maxCyclesBeforeCleanup) {
|
|
27
|
+
this.cleanupCount = 0;
|
|
28
|
+
this.cleanup();
|
|
29
|
+
}
|
|
20
30
|
return;
|
|
21
31
|
}
|
|
22
32
|
/** @inheritdoc */
|
|
23
33
|
list() {
|
|
24
|
-
return Promise.resolve(this.
|
|
34
|
+
return Promise.resolve(this.map.values ? Array.from(this.map.values()).filter(notExpired) : []);
|
|
25
35
|
}
|
|
26
36
|
/** @inheritdoc */
|
|
27
37
|
get(key) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
this.cleanupCount = 0;
|
|
32
|
-
this.cleanup();
|
|
38
|
+
const entry = this.map.get(key);
|
|
39
|
+
if (entry && notExpired(entry)) {
|
|
40
|
+
return Promise.resolve(entry);
|
|
33
41
|
}
|
|
34
|
-
return
|
|
42
|
+
return null;
|
|
35
43
|
}
|
|
36
44
|
/** @inheritdoc */
|
|
37
45
|
delete(key) {
|
|
38
|
-
this.
|
|
46
|
+
this.map.delete(key);
|
|
39
47
|
return;
|
|
40
48
|
}
|
|
41
49
|
/** @inheritdoc */
|
|
42
50
|
cleanup() {
|
|
43
51
|
// Remove all expired registration entries
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
this.map.forEach((entry, key) => {
|
|
53
|
+
if (!notExpired(entry)) {
|
|
54
|
+
this.map.delete(key);
|
|
55
|
+
}
|
|
48
56
|
});
|
|
49
57
|
}
|
|
50
58
|
}
|
package/dist/request.js
CHANGED
|
@@ -34,10 +34,10 @@ let cseq = 1;
|
|
|
34
34
|
* @return {RegistrationRequest}
|
|
35
35
|
*/
|
|
36
36
|
function createRegistrationRequest(params) {
|
|
37
|
-
var _a;
|
|
37
|
+
var _a, _b, _c, _d, _e;
|
|
38
38
|
const credentials = params.auth
|
|
39
39
|
? {
|
|
40
|
-
name:
|
|
40
|
+
name: common_1.CommonTypes.ExtraHeader.GATEWAY_AUTH,
|
|
41
41
|
value: Buffer.from(`${params.auth.username}:${params.auth.secret}`).toString("base64")
|
|
42
42
|
}
|
|
43
43
|
: undefined;
|
|
@@ -49,9 +49,9 @@ function createRegistrationRequest(params) {
|
|
|
49
49
|
user: params.user,
|
|
50
50
|
message: {
|
|
51
51
|
extensions: [
|
|
52
|
-
...(((_a = params.methods) === null || _a === void 0 ? void 0 : _a.map((value) => {
|
|
52
|
+
...((_b = (_a = params.methods) === null || _a === void 0 ? void 0 : _a.map((value) => {
|
|
53
53
|
return { name: "Allow", value };
|
|
54
|
-
}))
|
|
54
|
+
})) !== null && _b !== void 0 ? _b : []),
|
|
55
55
|
{
|
|
56
56
|
name: "CSeq",
|
|
57
57
|
value: `${cseq++} ${common_1.Method.REGISTER}`
|
|
@@ -59,7 +59,7 @@ function createRegistrationRequest(params) {
|
|
|
59
59
|
{
|
|
60
60
|
name: "User-Agent",
|
|
61
61
|
// TODO: Take from central location (commons?)
|
|
62
|
-
value: params.userAgent
|
|
62
|
+
value: (_c = params.userAgent) !== null && _c !== void 0 ? _c : "Routr Connect v2"
|
|
63
63
|
},
|
|
64
64
|
// TODO: Q. Should this be in the registry configuration?
|
|
65
65
|
{
|
|
@@ -122,10 +122,10 @@ function createRegistrationRequest(params) {
|
|
|
122
122
|
contentLength: 0
|
|
123
123
|
},
|
|
124
124
|
expires: {
|
|
125
|
-
expires: params.expires
|
|
125
|
+
expires: (_d = params.expires) !== null && _d !== void 0 ? _d : types_1.DEFAULT_EXPIRES
|
|
126
126
|
},
|
|
127
127
|
maxForwards: {
|
|
128
|
-
maxForwards: params.maxForwards
|
|
128
|
+
maxForwards: (_e = params.maxForwards) !== null && _e !== void 0 ? _e : types_1.DEFAULT_MAX_FORWARDS
|
|
129
129
|
},
|
|
130
130
|
requestUri: {
|
|
131
131
|
user: params.user,
|
|
@@ -135,7 +135,7 @@ function createRegistrationRequest(params) {
|
|
|
135
135
|
methodParam: common_1.Method.REGISTER,
|
|
136
136
|
secure: params.secure
|
|
137
137
|
},
|
|
138
|
-
messageType:
|
|
138
|
+
messageType: common_1.CommonTypes.MessageType.REQUEST
|
|
139
139
|
}
|
|
140
140
|
};
|
|
141
141
|
}
|
package/dist/service.js
CHANGED
|
@@ -38,8 +38,7 @@ const common_1 = require("@routr/common");
|
|
|
38
38
|
const logger_1 = require("@fonoster/logger");
|
|
39
39
|
const common_2 = require("@routr/common");
|
|
40
40
|
const logger = (0, logger_1.getLogger)({ service: "registry", filePath: __filename });
|
|
41
|
-
const DEFAULT_REGISTRATION_INTERVAL = 60
|
|
42
|
-
const DEFAULT_RETENTION_TIME = 600 * 1000;
|
|
41
|
+
const DEFAULT_REGISTRATION_INTERVAL = 60;
|
|
43
42
|
// TODO:
|
|
44
43
|
// - We need to filter quarentine trunks from final list of trunks
|
|
45
44
|
/**
|
|
@@ -48,7 +47,9 @@ const DEFAULT_RETENTION_TIME = 600 * 1000;
|
|
|
48
47
|
* @param {RegistryConfig} config
|
|
49
48
|
*/
|
|
50
49
|
function registryService(config) {
|
|
50
|
+
var _a;
|
|
51
51
|
logger.info("starting registry service");
|
|
52
|
+
const registerInterval = (_a = config.registerInterval) !== null && _a !== void 0 ? _a : DEFAULT_REGISTRATION_INTERVAL;
|
|
52
53
|
const store = (0, utils_1.buildStore)(config);
|
|
53
54
|
// Creates internval to send registration request every X seconds
|
|
54
55
|
setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -62,14 +63,18 @@ function registryService(config) {
|
|
|
62
63
|
logger.error("failed to retrieve trunks from API", err);
|
|
63
64
|
return;
|
|
64
65
|
}
|
|
65
|
-
|
|
66
|
+
// Create a list of trunks in the Store
|
|
67
|
+
const trunksInStore = (yield store.list()).map((r) => r.trunkRef);
|
|
68
|
+
const registryInvocations = resources
|
|
69
|
+
.filter((resources) => !trunksInStore.includes(resources.metadata.ref))
|
|
70
|
+
.map((resource) => __awaiter(this, void 0, void 0, function* () {
|
|
66
71
|
const registrationRequestInput = (0, utils_1.registrationRequestInputFromTrunk)(yield (0, utils_1.convertResourceToTrunk)(dataAPI, resource), config);
|
|
67
72
|
const request = (0, request_1.default)(registrationRequestInput);
|
|
68
73
|
return (0, sender_1.sendRegisterMessage)(config.requesterAddr)(request);
|
|
69
74
|
}));
|
|
70
75
|
const results = yield Promise.allSettled(registryInvocations);
|
|
71
76
|
results === null || results === void 0 ? void 0 : results.forEach((result) => __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
var
|
|
77
|
+
var _b, _c, _d, _e;
|
|
73
78
|
logger.verbose("processing registration result", { result });
|
|
74
79
|
if (result.status === "rejected") {
|
|
75
80
|
logger.error("request rejected", result.reason);
|
|
@@ -80,7 +85,8 @@ function registryService(config) {
|
|
|
80
85
|
return;
|
|
81
86
|
}
|
|
82
87
|
const message = result.value.message;
|
|
83
|
-
|
|
88
|
+
// Makes sure we send register before the last register expires
|
|
89
|
+
const retentionTime = ((_e = (_c = (_b = message.contact) === null || _b === void 0 ? void 0 : _b.expires) !== null && _c !== void 0 ? _c : (_d = message.expires) === null || _d === void 0 ? void 0 : _d.expires) !== null && _e !== void 0 ? _e : types_1.DEFAULT_EXPIRES) - registerInterval;
|
|
84
90
|
// TODO: Refactor to use ResponseType instead of string
|
|
85
91
|
const status = message.responseType.toString() == "OK"
|
|
86
92
|
? types_1.RegistrationEntryStatus.REGISTERED
|
|
@@ -98,6 +104,6 @@ function registryService(config) {
|
|
|
98
104
|
status
|
|
99
105
|
});
|
|
100
106
|
}));
|
|
101
|
-
}),
|
|
107
|
+
}), registerInterval * 1000);
|
|
102
108
|
}
|
|
103
109
|
exports.default = registryService;
|
package/dist/types.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,27 +1,4 @@
|
|
|
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
|
-
};
|
|
25
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -36,12 +13,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
13
|
};
|
|
37
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
15
|
exports.registrationRequestInputFromTrunk = exports.convertResourceToTrunk = exports.buildStore = exports.configFromString = exports.findTrunks = exports.getUnregisteredTrunks = void 0;
|
|
16
|
+
/*
|
|
17
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
18
|
+
* http://github.com/fonoster
|
|
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
|
+
*/
|
|
39
34
|
const common_1 = require("@routr/common");
|
|
35
|
+
const common_2 = require("@routr/common");
|
|
40
36
|
const memory_store_1 = __importDefault(require("./memory_store"));
|
|
41
37
|
const redis_store_1 = __importDefault(require("./redis_store"));
|
|
42
38
|
const types_1 = require("./types");
|
|
43
|
-
const protobufUtil = __importStar(require("pb-util"));
|
|
44
|
-
const jsonFromStruct = protobufUtil.struct.decode;
|
|
45
39
|
// eslint-disable-next-line require-jsdoc
|
|
46
40
|
function getUnregisteredTrunks(store) {
|
|
47
41
|
return (trunks) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -56,8 +50,8 @@ exports.getUnregisteredTrunks = getUnregisteredTrunks;
|
|
|
56
50
|
function findTrunks(dataAPI) {
|
|
57
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
52
|
return yield dataAPI.findBy({
|
|
59
|
-
kind:
|
|
60
|
-
criteria:
|
|
53
|
+
kind: common_2.CommonConnect.KIND.TRUNK,
|
|
54
|
+
criteria: common_2.CommonConnect.FindCriteria.FIND_TRUNKS_WITH_SEND_REGISTER,
|
|
61
55
|
parameters: {}
|
|
62
56
|
});
|
|
63
57
|
});
|
|
@@ -96,26 +90,26 @@ const buildStore = (config) => {
|
|
|
96
90
|
};
|
|
97
91
|
exports.buildStore = buildStore;
|
|
98
92
|
const convertResourceToTrunk = (dataAPI, resource) => __awaiter(void 0, void 0, void 0, function* () {
|
|
99
|
-
var _a, _b, _c, _d;
|
|
93
|
+
var _a, _b, _c, _d, _e, _f;
|
|
100
94
|
const metadata = resource.metadata;
|
|
101
|
-
const trunkSpec =
|
|
95
|
+
const trunkSpec = resource.spec;
|
|
102
96
|
const uri = (_b = (_a = trunkSpec.outbound) === null || _a === void 0 ? void 0 : _a.uris[0]) === null || _b === void 0 ? void 0 : _b.uri;
|
|
103
97
|
// WARNING: Perhaps we should bring this on a single API call
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
const usernameAndPassword = credSpec.credentials;
|
|
98
|
+
const cred = yield dataAPI.get((_c = trunkSpec.outbound) === null || _c === void 0 ? void 0 : _c.credentialsRef);
|
|
99
|
+
const usernameAndPassword = cred === null || cred === void 0 ? void 0 : cred.spec.credentials;
|
|
107
100
|
return {
|
|
108
101
|
ref: resource.metadata.ref,
|
|
109
102
|
name: resource.metadata.name,
|
|
110
103
|
region: metadata.region,
|
|
111
104
|
host: uri.host,
|
|
112
|
-
port: uri.port
|
|
105
|
+
port: (_d = uri.port) !== null && _d !== void 0 ? _d : 5060,
|
|
113
106
|
user: uri.user,
|
|
114
107
|
credentials: {
|
|
115
|
-
username: usernameAndPassword.username,
|
|
116
|
-
secret: usernameAndPassword.password
|
|
108
|
+
username: usernameAndPassword === null || usernameAndPassword === void 0 ? void 0 : usernameAndPassword.username,
|
|
109
|
+
secret: usernameAndPassword === null || usernameAndPassword === void 0 ? void 0 : usernameAndPassword.password
|
|
117
110
|
},
|
|
118
|
-
|
|
111
|
+
// Q. Why default to TCP?
|
|
112
|
+
transport: ((_f = (_e = uri.transport) === null || _e === void 0 ? void 0 : _e.toUpperCase()) !== null && _f !== void 0 ? _f : common_1.Transport.TCP)
|
|
119
113
|
};
|
|
120
114
|
});
|
|
121
115
|
exports.convertResourceToTrunk = convertResourceToTrunk;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@routr/registry",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6-alpha.0",
|
|
4
4
|
"description": "Sends trunks registrations and maintains their bindings",
|
|
5
5
|
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
6
|
"homepage": "https://github.com/fonoster/routr#readme",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@opentelemetry/sdk-trace-base": "^1.0.4",
|
|
35
35
|
"@opentelemetry/sdk-trace-node": "^1.0.4",
|
|
36
36
|
"@opentelemetry/semantic-conventions": "^1.0.4",
|
|
37
|
-
"@routr/common": "^2.0.
|
|
38
|
-
"@routr/processor": "^2.0.
|
|
37
|
+
"@routr/common": "^2.0.6-alpha.0",
|
|
38
|
+
"@routr/processor": "^2.0.6-alpha.0",
|
|
39
39
|
"ajv": "^6.12.6",
|
|
40
40
|
"fp-ts": "^2.11.8",
|
|
41
41
|
"pb-util": "^1.0.3",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/uniqid": "^5.3.2"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "0829f7e9b5b3ebba7eb984da5436d7c07689d776"
|
|
56
56
|
}
|