@routr/simpledata 2.0.5-alpha.16
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/api.d.ts +16 -0
- package/dist/api.js +110 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.js +91 -0
- package/dist/runner.d.ts +2 -0
- package/dist/runner.js +43 -0
- package/dist/service.d.ts +7 -0
- package/dist/service.js +53 -0
- package/dist/tracer.d.ts +7 -0
- package/dist/tracer.js +53 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +29 -0
- package/dist/utils.js +117 -0
- package/package.json +55 -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.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CommonTypes as CT } from "@routr/common";
|
|
2
|
+
import { CommonConnect as CC } from "@routr/common";
|
|
3
|
+
/**
|
|
4
|
+
* Enclosure with method to obtain a resource by reference.
|
|
5
|
+
*
|
|
6
|
+
* @param {Resource[]} resources - the resources to search from
|
|
7
|
+
* @return {Function } enclosed method with actual "get" logic
|
|
8
|
+
*/
|
|
9
|
+
export declare function get(resources: CC.Resource[]): (call: CT.GrpcCall, callback: CT.GrpcCallback) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Enclosure with method to obtain a resource with a query.
|
|
12
|
+
*
|
|
13
|
+
* @param {Resource[]} resources - the resources to search from
|
|
14
|
+
* @return {Function } enclosed method with actual "find" logic
|
|
15
|
+
*/
|
|
16
|
+
export declare function findBy(resources: CC.Resource[]): (call: CT.GrpcCall, callback: CT.GrpcCallback) => void;
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.findBy = exports.get = void 0;
|
|
30
|
+
/*
|
|
31
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
32
|
+
* http://github.com/fonoster/routr
|
|
33
|
+
*
|
|
34
|
+
* This file is part of Routr
|
|
35
|
+
*
|
|
36
|
+
* Licensed under the MIT License (the "License");
|
|
37
|
+
* you may not use this file except in compliance with
|
|
38
|
+
* the License. You may obtain a copy of the License at
|
|
39
|
+
*
|
|
40
|
+
* https://opensource.org/licenses/MIT
|
|
41
|
+
*
|
|
42
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
43
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
44
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
45
|
+
* See the License for the specific language governing permissions and
|
|
46
|
+
* limitations under the License.
|
|
47
|
+
*/
|
|
48
|
+
const errors_1 = require("./errors");
|
|
49
|
+
const utils_1 = require("./utils");
|
|
50
|
+
const common_1 = require("@routr/common");
|
|
51
|
+
const protobufUtil = __importStar(require("pb-util"));
|
|
52
|
+
const jsonpath_1 = __importDefault(require("jsonpath"));
|
|
53
|
+
const jsonToStruct = protobufUtil.struct.encode;
|
|
54
|
+
/**
|
|
55
|
+
* Enclosure with method to obtain a resource by reference.
|
|
56
|
+
*
|
|
57
|
+
* @param {Resource[]} resources - the resources to search from
|
|
58
|
+
* @return {Function } enclosed method with actual "get" logic
|
|
59
|
+
*/
|
|
60
|
+
function get(resources) {
|
|
61
|
+
return (call, callback) => {
|
|
62
|
+
if (resources == null || resources.length === 0) {
|
|
63
|
+
return callback(new errors_1.ResourceNotFound(call.request.ref), null);
|
|
64
|
+
}
|
|
65
|
+
if (call.request.ref == null) {
|
|
66
|
+
return callback(new errors_1.BadRequest("parameter ref is required"), null);
|
|
67
|
+
}
|
|
68
|
+
const resource = common_1.Helper.deepCopy(resources.find((r) => r.metadata.ref === call.request.ref));
|
|
69
|
+
// Serialize to protobuf
|
|
70
|
+
if (resource)
|
|
71
|
+
resource.spec = jsonToStruct(resource.spec);
|
|
72
|
+
resource
|
|
73
|
+
? callback(null, resource)
|
|
74
|
+
: callback(new errors_1.ResourceNotFound(call.request.ref), null);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
exports.get = get;
|
|
78
|
+
/**
|
|
79
|
+
* Enclosure with method to obtain a resource with a query.
|
|
80
|
+
*
|
|
81
|
+
* @param {Resource[]} resources - the resources to search from
|
|
82
|
+
* @return {Function } enclosed method with actual "find" logic
|
|
83
|
+
*/
|
|
84
|
+
function findBy(resources) {
|
|
85
|
+
return (call, callback) => {
|
|
86
|
+
const filteredResources = resources.filter((resource) => { var _a, _b; return ((_a = resource.kind) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === ((_b = call.request.kind) === null || _b === void 0 ? void 0 : _b.toLowerCase()); });
|
|
87
|
+
if (filteredResources == null || filteredResources.length === 0) {
|
|
88
|
+
return callback(new errors_1.ResourceNotFound(""), null);
|
|
89
|
+
}
|
|
90
|
+
const queryObject = (0, utils_1.createQuery)(call.request);
|
|
91
|
+
if (queryObject instanceof errors_1.BadRequest) {
|
|
92
|
+
return callback(queryObject, null);
|
|
93
|
+
}
|
|
94
|
+
const queryResult = common_1.Helper.deepCopy(jsonpath_1.default.query(filteredResources, queryObject.query));
|
|
95
|
+
try {
|
|
96
|
+
queryResult.forEach((resource) => {
|
|
97
|
+
// Serialize to protobuf
|
|
98
|
+
resource.spec = jsonToStruct(resource.spec);
|
|
99
|
+
return resource;
|
|
100
|
+
});
|
|
101
|
+
callback(null, {
|
|
102
|
+
resources: queryResult
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
return callback(new errors_1.BadRequest(`invalid JSONPath expression: ${queryObject.query}`), null);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
exports.findBy = findBy;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown for unimplemented handlers.
|
|
3
|
+
*/
|
|
4
|
+
export declare class UnimplementedError extends Error {
|
|
5
|
+
code: number;
|
|
6
|
+
/**
|
|
7
|
+
* Creates an instance of UnimplementedError.
|
|
8
|
+
*/
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Thrown if the resource is not found.
|
|
13
|
+
*/
|
|
14
|
+
export declare class ResourceNotFound extends Error {
|
|
15
|
+
code: number;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of UnimplementedError.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} ref - the reference of the resource
|
|
20
|
+
*/
|
|
21
|
+
constructor(ref: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Thrown if the request is invalid.
|
|
25
|
+
*/
|
|
26
|
+
export declare class BadRequest extends Error {
|
|
27
|
+
code: number;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an instance of UnimplementedError.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} message - optional message with the error. Defaults to "bad request."
|
|
32
|
+
*/
|
|
33
|
+
constructor(message?: string);
|
|
34
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.BadRequest = exports.ResourceNotFound = exports.UnimplementedError = void 0;
|
|
27
|
+
/*
|
|
28
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
29
|
+
* http://github.com/fonoster
|
|
30
|
+
*
|
|
31
|
+
* This file is part of nodejs-service
|
|
32
|
+
*
|
|
33
|
+
* Licensed under the MIT License (the "License");
|
|
34
|
+
* you may not use this file except in compliance with
|
|
35
|
+
* the License. You may obtain a copy of the License at
|
|
36
|
+
*
|
|
37
|
+
* https://opensource.org/licenses/MIT
|
|
38
|
+
*
|
|
39
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
40
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
41
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
42
|
+
* See the License for the specific language governing permissions and
|
|
43
|
+
* limitations under the License.
|
|
44
|
+
*/
|
|
45
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
46
|
+
/**
|
|
47
|
+
* Thrown for unimplemented handlers.
|
|
48
|
+
*/
|
|
49
|
+
class UnimplementedError extends Error {
|
|
50
|
+
/**
|
|
51
|
+
* Creates an instance of UnimplementedError.
|
|
52
|
+
*/
|
|
53
|
+
constructor() {
|
|
54
|
+
super("this operation is not supported/enabled in this data api implementation.");
|
|
55
|
+
this.code = grpc.status.UNIMPLEMENTED;
|
|
56
|
+
Object.setPrototypeOf(this, UnimplementedError.prototype);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.UnimplementedError = UnimplementedError;
|
|
60
|
+
/**
|
|
61
|
+
* Thrown if the resource is not found.
|
|
62
|
+
*/
|
|
63
|
+
class ResourceNotFound extends Error {
|
|
64
|
+
/**
|
|
65
|
+
* Creates an instance of UnimplementedError.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} ref - the reference of the resource
|
|
68
|
+
*/
|
|
69
|
+
constructor(ref) {
|
|
70
|
+
super(`resource not found: ${ref}`);
|
|
71
|
+
this.code = grpc.status.NOT_FOUND;
|
|
72
|
+
Object.setPrototypeOf(this, ResourceNotFound.prototype);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.ResourceNotFound = ResourceNotFound;
|
|
76
|
+
/**
|
|
77
|
+
* Thrown if the request is invalid.
|
|
78
|
+
*/
|
|
79
|
+
class BadRequest extends Error {
|
|
80
|
+
/**
|
|
81
|
+
* Creates an instance of UnimplementedError.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} message - optional message with the error. Defaults to "bad request."
|
|
84
|
+
*/
|
|
85
|
+
constructor(message) {
|
|
86
|
+
super(message || "bad request");
|
|
87
|
+
this.code = grpc.status.INVALID_ARGUMENT;
|
|
88
|
+
Object.setPrototypeOf(this, BadRequest.prototype);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.BadRequest = BadRequest;
|
package/dist/runner.d.ts
ADDED
package/dist/runner.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
26
|
+
require("./tracer").init("simpleauth");
|
|
27
|
+
const service_1 = __importDefault(require("./service"));
|
|
28
|
+
const utils_1 = __importDefault(require("./utils"));
|
|
29
|
+
const common_1 = require("@routr/common");
|
|
30
|
+
const logger_1 = require("@fonoster/logger");
|
|
31
|
+
const logger = (0, logger_1.getLogger)({ service: "simpledata", filePath: __filename });
|
|
32
|
+
common_1.Assertions.assertEnvsAreSet(["PATH_TO_SCHEMAS", "PATH_TO_RESOURCES"]);
|
|
33
|
+
const resources = (0, utils_1.default)(process.env.PATH_TO_SCHEMAS, process.env.PATH_TO_RESOURCES);
|
|
34
|
+
try {
|
|
35
|
+
(0, service_1.default)({
|
|
36
|
+
bindAddr: process.env.BIND_ADDR || "0.0.0.0:51903",
|
|
37
|
+
resources
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
logger.error(e);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const common_1 = require("@routr/common");
|
|
27
|
+
const utils_1 = require("./utils");
|
|
28
|
+
const api_1 = require("./api");
|
|
29
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
30
|
+
const logger_1 = require("@fonoster/logger");
|
|
31
|
+
const logger = (0, logger_1.getLogger)({ service: "simpledata", filePath: __filename });
|
|
32
|
+
/**
|
|
33
|
+
* Starts a new simple data service.
|
|
34
|
+
*
|
|
35
|
+
* @param {SimpleDataConfig} config - the configuration of the service
|
|
36
|
+
*/
|
|
37
|
+
function simpleDataService(config) {
|
|
38
|
+
const { bindAddr } = config;
|
|
39
|
+
logger.info("starting routr service", { bindAddr, name: "simpledata" });
|
|
40
|
+
const server = new grpc.Server();
|
|
41
|
+
server.addService(common_1.CommonConnect.RESOURCES_PROTO.v2draft1.Resources.service, {
|
|
42
|
+
get: (0, api_1.get)(config.resources),
|
|
43
|
+
findBy: (0, api_1.findBy)(config.resources),
|
|
44
|
+
delete: utils_1.nyi,
|
|
45
|
+
update: utils_1.nyi,
|
|
46
|
+
create: utils_1.nyi,
|
|
47
|
+
list: utils_1.nyi
|
|
48
|
+
});
|
|
49
|
+
server.bindAsync(config.bindAddr, grpc.ServerCredentials.createInsecure(), () => {
|
|
50
|
+
server.start();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
exports.default = simpleDataService;
|
package/dist/tracer.d.ts
ADDED
package/dist/tracer.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.init = void 0;
|
|
7
|
+
/*
|
|
8
|
+
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
9
|
+
* http://github.com/fonoster/routr
|
|
10
|
+
*
|
|
11
|
+
* This file is part of Routr
|
|
12
|
+
*
|
|
13
|
+
* Licensed under the MIT License (the "License")
|
|
14
|
+
* you may not use this file except in compliance with
|
|
15
|
+
* the License. You may obtain a copy of the License at
|
|
16
|
+
*
|
|
17
|
+
* https://opensource.org/licenses/MIT
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
* See the License for the specific language governing permissions and
|
|
23
|
+
* limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
const api_1 = __importDefault(require("@opentelemetry/api"));
|
|
26
|
+
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
27
|
+
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
28
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
29
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
30
|
+
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
31
|
+
const exporter_jaeger_1 = require("@opentelemetry/exporter-jaeger");
|
|
32
|
+
const instrumentation_grpc_1 = require("@opentelemetry/instrumentation-grpc");
|
|
33
|
+
/**
|
|
34
|
+
* This function registers the instrumentations for the service.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} serviceName - The name of the service.
|
|
37
|
+
* @return {Tracer} The tracer object.
|
|
38
|
+
*/
|
|
39
|
+
function init(serviceName) {
|
|
40
|
+
const provider = new sdk_trace_node_1.NodeTracerProvider({
|
|
41
|
+
resource: new resources_1.Resource({
|
|
42
|
+
[semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName
|
|
43
|
+
})
|
|
44
|
+
});
|
|
45
|
+
const exporter = new exporter_jaeger_1.JaegerExporter();
|
|
46
|
+
provider.addSpanProcessor(new sdk_trace_base_1.SimpleSpanProcessor(exporter));
|
|
47
|
+
provider.register();
|
|
48
|
+
(0, instrumentation_1.registerInstrumentations)({
|
|
49
|
+
instrumentations: [new instrumentation_grpc_1.GrpcInstrumentation()]
|
|
50
|
+
});
|
|
51
|
+
return api_1.default.trace.getTracer("routr-tracer");
|
|
52
|
+
}
|
|
53
|
+
exports.init = init;
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BadRequest } from "./errors";
|
|
2
|
+
import { CommonTypes as CT } from "@routr/common";
|
|
3
|
+
import { CommonConnect as CC } from "@routr/common";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a list of validators from the given schemas.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} path - the path to the resource
|
|
8
|
+
* @return {Validator} - a list of validators from the path
|
|
9
|
+
*/
|
|
10
|
+
export declare function createValidators(path: string): Map<string, (resource: CC.Resource) => unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Loads a list of resources from a file.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} validatorsPath - the path to the validators
|
|
15
|
+
* @param {string} resourcesPath - the path to the resources
|
|
16
|
+
* @return {Resource[]} the loaded resources
|
|
17
|
+
*/
|
|
18
|
+
export default function loadResources(validatorsPath: string, resourcesPath: string): CC.Resource[];
|
|
19
|
+
export declare function createQuery(request: CC.FindParameters): {
|
|
20
|
+
request: CC.FindParameters;
|
|
21
|
+
query: string;
|
|
22
|
+
} | BadRequest;
|
|
23
|
+
/**
|
|
24
|
+
* Returns UnimplementedError via callback.
|
|
25
|
+
*
|
|
26
|
+
* @param {GrpcCall} call - the grpc request
|
|
27
|
+
* @param {GrpcCallback} callback - the grpc callback
|
|
28
|
+
*/
|
|
29
|
+
export declare function nyi(call: CT.GrpcCall, callback: CT.GrpcCallback): void;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
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.nyi = exports.createQuery = exports.createValidators = 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 ajv_1 = __importDefault(require("ajv"));
|
|
27
|
+
const logger_1 = require("@fonoster/logger");
|
|
28
|
+
const common_1 = require("@routr/common");
|
|
29
|
+
const logger = (0, logger_1.getLogger)({ service: "simpledata", filePath: __filename });
|
|
30
|
+
const findCriteriaMap = {};
|
|
31
|
+
findCriteriaMap[common_1.CommonConnect.FindCriteria.FIND_AGENT_BY_USERNAME] = (parameters) => `$..[?(@.spec.username=='${parameters.username}')]`;
|
|
32
|
+
findCriteriaMap[common_1.CommonConnect.FindCriteria.FIND_CREDENTIAL_BY_REFERENCE] = (parameters) => `$..[?(@.metadata.ref=='${parameters.ref}')]`;
|
|
33
|
+
findCriteriaMap[common_1.CommonConnect.FindCriteria.FIND_DOMAIN_BY_DOMAINURI] = (parameters) => `$..[?(@.spec.context.domainUri=='${parameters.domainUri}')]`;
|
|
34
|
+
findCriteriaMap[common_1.CommonConnect.FindCriteria.FIND_NUMBER_BY_TELURL] = (parameters) => `$..[?(@.spec.location.telUrl=="tel:${parameters.telUrl}")]`;
|
|
35
|
+
findCriteriaMap[common_1.CommonConnect.FindCriteria.FIND_TRUNKS_WITH_SEND_REGISTER] = () => "$..[?(@.spec.outbound.sendRegister==true)]";
|
|
36
|
+
/**
|
|
37
|
+
* Creates a list of validators from the given schemas.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} path - the path to the resource
|
|
40
|
+
* @return {Validator} - a list of validators from the path
|
|
41
|
+
*/
|
|
42
|
+
function createValidators(path) {
|
|
43
|
+
const validators = new Map();
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
45
|
+
const files = require("fs").readdirSync(path);
|
|
46
|
+
files.forEach((file) => {
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
48
|
+
const schema = require(`${path}/${file}`);
|
|
49
|
+
const ajv = new ajv_1.default();
|
|
50
|
+
const validator = ajv.compile(schema);
|
|
51
|
+
validators.set(schema.properties.kind.enum[0].toLowerCase(), validator);
|
|
52
|
+
});
|
|
53
|
+
return validators;
|
|
54
|
+
}
|
|
55
|
+
exports.createValidators = createValidators;
|
|
56
|
+
/**
|
|
57
|
+
* Loads a list of resources from a file.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} validatorsPath - the path to the validators
|
|
60
|
+
* @param {string} resourcesPath - the path to the resources
|
|
61
|
+
* @return {Resource[]} the loaded resources
|
|
62
|
+
*/
|
|
63
|
+
function loadResources(validatorsPath, resourcesPath) {
|
|
64
|
+
const validators = createValidators(validatorsPath);
|
|
65
|
+
const all = [];
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
67
|
+
const files = require("fs").readdirSync(resourcesPath);
|
|
68
|
+
files.forEach((file) => {
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
70
|
+
const resources = require(`${resourcesPath}/${file}`);
|
|
71
|
+
resources.forEach((resource) => {
|
|
72
|
+
// Check if is valid using jsonschema
|
|
73
|
+
const validate = validators.get(resource.kind.toLocaleLowerCase());
|
|
74
|
+
if (!validate) {
|
|
75
|
+
logger.error(`unable to find validator for ${resource.kind}; exiting`);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
if (validate(resource)) {
|
|
79
|
+
all.push(resource);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
logger.error("found a bad resource: " +
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
JSON.stringify(validate.errors[0].message));
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
logger.verbose("loaded data resources", { total: all.length });
|
|
90
|
+
return all;
|
|
91
|
+
}
|
|
92
|
+
exports.default = loadResources;
|
|
93
|
+
// eslint-disable-next-line require-jsdoc
|
|
94
|
+
function createQuery(request) {
|
|
95
|
+
const findCriteria = request.criteria;
|
|
96
|
+
if (!request["criteria"] || !request["kind"] || !request["parameters"]) {
|
|
97
|
+
return new errors_1.BadRequest("createQuery request is missing 'criteria', 'kind', or 'parameters'");
|
|
98
|
+
}
|
|
99
|
+
if (!findCriteriaMap[findCriteria]) {
|
|
100
|
+
return new errors_1.BadRequest(`invalid find criteria ${request.criteria}`);
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
query: findCriteriaMap[findCriteria](request.parameters),
|
|
104
|
+
request
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
exports.createQuery = createQuery;
|
|
108
|
+
/**
|
|
109
|
+
* Returns UnimplementedError via callback.
|
|
110
|
+
*
|
|
111
|
+
* @param {GrpcCall} call - the grpc request
|
|
112
|
+
* @param {GrpcCallback} callback - the grpc callback
|
|
113
|
+
*/
|
|
114
|
+
function nyi(call, callback) {
|
|
115
|
+
callback(new errors_1.UnimplementedError());
|
|
116
|
+
}
|
|
117
|
+
exports.nyi = nyi;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@routr/simpledata",
|
|
3
|
+
"version": "2.0.5-alpha.16",
|
|
4
|
+
"description": "Files-based API Server for Routr Connect",
|
|
5
|
+
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
|
+
"homepage": "https://github.com/fonoster/routr#readme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "dist/simpledata",
|
|
9
|
+
"types": "dist/simpledata",
|
|
10
|
+
"directories": {
|
|
11
|
+
"src": "src",
|
|
12
|
+
"test": "test"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"prebuild": "rimraf ./dist tsconfig.tsbuildinfo",
|
|
16
|
+
"build": "tsc -b tsconfig.json"
|
|
17
|
+
},
|
|
18
|
+
"bin": {
|
|
19
|
+
"run_simpledata": "dist/runner.js"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@fonoster/logger": "0.3.17-alpha.2",
|
|
23
|
+
"@grpc/grpc-js": "^1.5.9",
|
|
24
|
+
"@opentelemetry/api": "^1.0.4",
|
|
25
|
+
"@opentelemetry/exporter-jaeger": "^1.0.4",
|
|
26
|
+
"@opentelemetry/instrumentation": "^0.27.0",
|
|
27
|
+
"@opentelemetry/instrumentation-grpc": "^0.27.0",
|
|
28
|
+
"@opentelemetry/resources": "^1.0.4",
|
|
29
|
+
"@opentelemetry/sdk-trace-base": "^1.0.4",
|
|
30
|
+
"@opentelemetry/sdk-trace-node": "^1.0.4",
|
|
31
|
+
"@opentelemetry/semantic-conventions": "^1.0.4",
|
|
32
|
+
"@routr/common": "^2.0.5-alpha.16",
|
|
33
|
+
"@routr/processor": "^2.0.5-alpha.16",
|
|
34
|
+
"google-protobuf": "^3.9.2",
|
|
35
|
+
"jsonpath": "^1.1.1"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/fonoster/routr.git"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/fonoster/routr/issues"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/google-protobuf": "^3.15.6",
|
|
52
|
+
"@types/jsonpath": "^0.2.0"
|
|
53
|
+
},
|
|
54
|
+
"gitHead": "b40323ac18cc3872b168bf4e8273f5bdb3f7510a"
|
|
55
|
+
}
|