@routr/common 2.0.8-alpha.12 → 2.0.8-alpha.13
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/helper.d.ts +12 -0
- package/dist/helper.js +67 -1
- package/package.json +8 -3
package/dist/helper.d.ts
CHANGED
|
@@ -14,3 +14,15 @@ export declare const deepCopy: <T>(source: T) => T;
|
|
|
14
14
|
* @return {NetInterface} transport The deep copy of the source object.
|
|
15
15
|
*/
|
|
16
16
|
export declare const getListeningPoint: (listeningPoints: NetInterface[], transport: Transport) => NetInterface;
|
|
17
|
+
/**
|
|
18
|
+
* Reads a file and returns a JSON object or throws an error.
|
|
19
|
+
* The file must be a valid JSON, YAML, or TOML file.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} path - The path to the file.
|
|
22
|
+
* @return {object} The JSON object.
|
|
23
|
+
* @throws {Error} If the file is not a valid JSON, YAML, or TOML file.
|
|
24
|
+
* @throws {Error} If the file does not exist.
|
|
25
|
+
* @throws {Error} If the file is not readable.
|
|
26
|
+
* @throws {Error} If the file is empty.
|
|
27
|
+
*/
|
|
28
|
+
export declare const readConfigFile: (path: string) => Record<string, any>;
|
package/dist/helper.js
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
2
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getListeningPoint = exports.deepCopy = void 0;
|
|
29
|
+
exports.readConfigFile = exports.getListeningPoint = exports.deepCopy = void 0;
|
|
4
30
|
/*
|
|
5
31
|
* Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
|
|
6
32
|
* http://github.com/fonoster/routr
|
|
@@ -20,6 +46,9 @@ exports.getListeningPoint = exports.deepCopy = void 0;
|
|
|
20
46
|
* limitations under the License.
|
|
21
47
|
*/
|
|
22
48
|
const types_1 = require("./types");
|
|
49
|
+
const fs_1 = __importDefault(require("fs"));
|
|
50
|
+
const yaml = __importStar(require("js-yaml"));
|
|
51
|
+
const toml = __importStar(require("toml"));
|
|
23
52
|
/**
|
|
24
53
|
* Makes a deep copy of an object.
|
|
25
54
|
*
|
|
@@ -58,3 +87,40 @@ const getListeningPoint = (listeningPoints, transport) => {
|
|
|
58
87
|
return listeningPoint;
|
|
59
88
|
};
|
|
60
89
|
exports.getListeningPoint = getListeningPoint;
|
|
90
|
+
/**
|
|
91
|
+
* Reads a file and returns a JSON object or throws an error.
|
|
92
|
+
* The file must be a valid JSON, YAML, or TOML file.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} path - The path to the file.
|
|
95
|
+
* @return {object} The JSON object.
|
|
96
|
+
* @throws {Error} If the file is not a valid JSON, YAML, or TOML file.
|
|
97
|
+
* @throws {Error} If the file does not exist.
|
|
98
|
+
* @throws {Error} If the file is not readable.
|
|
99
|
+
* @throws {Error} If the file is empty.
|
|
100
|
+
*/
|
|
101
|
+
const readConfigFile = (path) => {
|
|
102
|
+
if (!fs_1.default.existsSync(path) || !fs_1.default.statSync(path).isFile()) {
|
|
103
|
+
throw new Error(`config file ${path} does not exist`);
|
|
104
|
+
}
|
|
105
|
+
const content = fs_1.default.readFileSync(path, "utf8");
|
|
106
|
+
try {
|
|
107
|
+
return yaml.load(content);
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
// Ignore
|
|
111
|
+
}
|
|
112
|
+
// Experimental TOML support
|
|
113
|
+
try {
|
|
114
|
+
return toml.parse(content);
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
// Ignore
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
return JSON.parse(content);
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
throw new Error("file is not a valid JSON or YAML file");
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
exports.readConfigFile = readConfigFile;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@routr/common",
|
|
3
|
-
"version": "2.0.8-alpha.
|
|
3
|
+
"version": "2.0.8-alpha.13",
|
|
4
4
|
"description": "Common package",
|
|
5
5
|
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
6
|
"homepage": "https://github.com/fonoster/routr#readme",
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
"@opentelemetry/semantic-conventions": "^1.0.4",
|
|
27
27
|
"fp-ts": "^2.11.8",
|
|
28
28
|
"ip-utils": "^2.4.0",
|
|
29
|
-
"
|
|
29
|
+
"js-yaml": "^4.1.0",
|
|
30
|
+
"pb-util": "^1.0.3",
|
|
31
|
+
"toml": "^3.0.0"
|
|
30
32
|
},
|
|
31
33
|
"directories": {
|
|
32
34
|
"src": "src",
|
|
@@ -45,5 +47,8 @@
|
|
|
45
47
|
"bugs": {
|
|
46
48
|
"url": "https://github.com/fonoster/routr/issues"
|
|
47
49
|
},
|
|
48
|
-
"
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/js-yaml": "^4.0.5"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "d866d6c03c5ea7e8fc3ee074804005df3a5be42f"
|
|
49
54
|
}
|