beavuck-time 2.4.0 → 2.4.1
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/README.md +7 -3
- package/build/config/corsOptions.js +7 -2
- package/build/server.js +38 -22
- package/build/utils/urlUtil.js +4 -2
- package/openapi/swagger.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -87,9 +87,13 @@ If you want to run this service programmatically in your Node.js project:
|
|
|
87
87
|
import { startServer } from 'beavuck-time'
|
|
88
88
|
|
|
89
89
|
startServer({
|
|
90
|
-
port: 3000,
|
|
91
90
|
hostUrl: 'https://time-api.example.com',
|
|
92
|
-
trustedOrigins: 'https://my.app.com,https://my-other.app.com'
|
|
91
|
+
trustedOrigins: 'https://my.app.com, https://my.app.com/*, https://my-other.app.com, https://my-other.app.com/*',
|
|
92
|
+
apiPort: 3000,
|
|
93
|
+
rateLimit: -1,
|
|
94
|
+
logLevel: 'info',
|
|
95
|
+
maxLogFiles: 64,
|
|
96
|
+
maxSizeLogFiles: '1m',
|
|
93
97
|
})
|
|
94
98
|
```
|
|
95
99
|
|
|
@@ -130,7 +134,7 @@ time:
|
|
|
130
134
|
# BEAVUCK_TIME_TRUSTED_ORIGINS: To allow requests from any origin, include * (not recommended). If empty, will only allow requests from the HOST_URL's origin. Defaults to the HOST_URL's origin
|
|
131
135
|
- BEAVUCK_TIME_API_PORT: 3000
|
|
132
136
|
# BEAVUCK_TIME_API_PORT: Optional. Internal port when in a container. Defaults to 3000
|
|
133
|
-
- BEAVUCK_TIME_RATE_LIMIT:
|
|
137
|
+
- BEAVUCK_TIME_RATE_LIMIT: -1
|
|
134
138
|
# BEAVUCK_TIME_RATE_LIMIT: Optional. Max allowed number of requests per minute for each IP address. If negative or 0, no limit. Defaults to no limit
|
|
135
139
|
- BEAVUCK_TIME_LOG_LEVEL: info
|
|
136
140
|
# BEAVUCK_TIME_LOG_LEVEL: Optional. Logging levels include error, warn, info, http, verbose, debug, silly. Defaults to info
|
|
@@ -50,11 +50,16 @@ function getTrustedOrigins() {
|
|
|
50
50
|
}
|
|
51
51
|
function initTrustedOrigins() {
|
|
52
52
|
const trustedOrigins = process.env.BEAVUCK_TIME_TRUSTED_ORIGINS;
|
|
53
|
-
TRUSTED_ORIGINS = trustedOrigins
|
|
53
|
+
TRUSTED_ORIGINS = trustedOrigins
|
|
54
|
+
.split(',')
|
|
55
|
+
.map(to => to.trim())
|
|
56
|
+
.filter(to => to.length > 0);
|
|
54
57
|
}
|
|
55
58
|
function isAllTrusted() {
|
|
56
59
|
return getTrustedOrigins().includes('*');
|
|
57
60
|
}
|
|
58
61
|
function isOriginAbsentOrTrusted(origin) {
|
|
59
|
-
return !origin ||
|
|
62
|
+
return (!origin ||
|
|
63
|
+
getTrustedOrigins().includes(origin) ||
|
|
64
|
+
getTrustedOrigins().some(to => to.includes('/*') && origin.startsWith(to)));
|
|
60
65
|
}
|
package/build/server.js
CHANGED
|
@@ -10,31 +10,14 @@ const api_1 = require("./api");
|
|
|
10
10
|
const logger_1 = require("./config/logger");
|
|
11
11
|
const urlUtil_1 = require("./utils/urlUtil");
|
|
12
12
|
const dotenvx_1 = __importDefault(require("@dotenvx/dotenvx"));
|
|
13
|
+
// Load environment variables from .env file, if available
|
|
14
|
+
// but don't error if the file is missing
|
|
13
15
|
dotenvx_1.default.config({
|
|
14
16
|
ignore: ['MISSING_ENV_FILE'],
|
|
15
17
|
});
|
|
16
|
-
function validateEnv({ hostUrl, trustedOrigins, }) {
|
|
17
|
-
if (!hostUrl || !(0, urlUtil_1.tryParseUrl)(hostUrl)) {
|
|
18
|
-
logger_1.logger.error('HOST_URL not set or is not a valid URL');
|
|
19
|
-
process.exit(1);
|
|
20
|
-
}
|
|
21
|
-
if (!trustedOrigins) {
|
|
22
|
-
logger_1.logger.error('TRUSTED_ORIGINS not set');
|
|
23
|
-
process.exit(1);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function propagateOptionsInEnv(HOST_URL, TRUSTED_ORIGINS, API_PORT) {
|
|
27
|
-
process.env.BEAVUCK_TIME_HOST_URL = HOST_URL;
|
|
28
|
-
process.env.BEAVUCK_TIME_TRUSTED_ORIGINS = TRUSTED_ORIGINS;
|
|
29
|
-
process.env.BEAVUCK_TIME_API_PORT = String(API_PORT);
|
|
30
|
-
}
|
|
31
18
|
function startServer(options = {}) {
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const TRUSTED_ORIGINS = options.trustedOrigins ?? process.env.BEAVUCK_TIME_TRUSTED_ORIGINS ?? '';
|
|
35
|
-
propagateOptionsInEnv(HOST_URL, TRUSTED_ORIGINS, API_PORT);
|
|
36
|
-
validateEnv({ hostUrl: HOST_URL, trustedOrigins: TRUSTED_ORIGINS });
|
|
37
|
-
const server = api_1.api.listen(API_PORT, () => {
|
|
19
|
+
manageEnvVars(options);
|
|
20
|
+
const server = api_1.api.listen(process.env.BEAVUCK_TIME_API_PORT, () => {
|
|
38
21
|
logger_1.logger.info(`
|
|
39
22
|
| | | | ,=.
|
|
40
23
|
| |__ ___ __ ___ ___ _ ___| | __ ,=""""==.__.=" o".___
|
|
@@ -45,7 +28,7 @@ function startServer(options = {}) {
|
|
|
45
28
|
|
|
46
29
|
Beavuck Time microservice started successfully
|
|
47
30
|
|
|
48
|
-
Ready on API port ${
|
|
31
|
+
Ready on API port ${process.env.BEAVUCK_TIME_API_PORT} (if this is running in a container, this port number is internal to the container)
|
|
49
32
|
`);
|
|
50
33
|
});
|
|
51
34
|
server.on('error', (err) => {
|
|
@@ -65,6 +48,39 @@ function startServer(options = {}) {
|
|
|
65
48
|
process.on(SIGINT, () => gracefulShutdown(SIGINT));
|
|
66
49
|
return server;
|
|
67
50
|
}
|
|
51
|
+
function manageEnvVars(options) {
|
|
52
|
+
process.env.BEAVUCK_TIME_API_PORT =
|
|
53
|
+
options.apiPort === undefined
|
|
54
|
+
? '3000'
|
|
55
|
+
: String(options.apiPort);
|
|
56
|
+
process.env.BEAVUCK_TIME_HOST_URL =
|
|
57
|
+
options.hostUrl ?? process.env.BEAVUCK_TIME_HOST_URL;
|
|
58
|
+
process.env.BEAVUCK_TIME_TRUSTED_ORIGINS =
|
|
59
|
+
options.trustedOrigins ?? process.env.BEAVUCK_TIME_TRUSTED_ORIGINS;
|
|
60
|
+
process.env.BEAVUCK_TIME_RATE_LIMIT =
|
|
61
|
+
options.rateLimit === undefined
|
|
62
|
+
? process.env.BEAVUCK_TIME_RATE_LIMIT
|
|
63
|
+
: String(options.rateLimit);
|
|
64
|
+
process.env.BEAVUCK_TIME_LOG_LEVEL =
|
|
65
|
+
options.logLevel ?? process.env.BEAVUCK_TIME_LOG_LEVEL;
|
|
66
|
+
process.env.BEAVUCK_TIME_MAX_LOG_FILES =
|
|
67
|
+
options.maxLogFiles === undefined
|
|
68
|
+
? process.env.BEAVUCK_TIME_MAX_LOG_FILES
|
|
69
|
+
: String(options.maxLogFiles);
|
|
70
|
+
process.env.BEAVUCK_TIME_MAX_SIZE_LOG_FILES =
|
|
71
|
+
options.maxSizeLogFiles ?? process.env.BEAVUCK_TIME_MAX_SIZE_LOG_FILES;
|
|
72
|
+
validateEnv();
|
|
73
|
+
}
|
|
74
|
+
function validateEnv() {
|
|
75
|
+
if (!process.env.BEAVUCK_TIME_HOST_URL || !(0, urlUtil_1.tryParseUrl)(process.env.BEAVUCK_TIME_HOST_URL)) {
|
|
76
|
+
logger_1.logger.error('HOST_URL not set or is not a valid URL');
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
if (!process.env.BEAVUCK_TIME_TRUSTED_ORIGINS) {
|
|
80
|
+
logger_1.logger.error('TRUSTED_ORIGINS not set');
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
68
84
|
// Support running as standalone binary
|
|
69
85
|
if (require.main === module) {
|
|
70
86
|
startServer();
|
package/build/utils/urlUtil.js
CHANGED
|
@@ -7,10 +7,12 @@ const node_url_1 = require("node:url");
|
|
|
7
7
|
const logger_1 = require("../config/logger");
|
|
8
8
|
function isSameOrigin(thisUrl, thatUrl) {
|
|
9
9
|
logger_1.logger.debug(`Comparing origins: ${thisUrl?.origin} and ${thatUrl?.origin}`);
|
|
10
|
-
if (!thisUrl || !thatUrl)
|
|
10
|
+
if (!thisUrl || !thatUrl) {
|
|
11
11
|
return false;
|
|
12
|
-
|
|
12
|
+
}
|
|
13
|
+
if (thisUrl.origin === thatUrl.origin) {
|
|
13
14
|
return true;
|
|
15
|
+
}
|
|
14
16
|
const isSameProtocol = thisUrl.protocol === thatUrl.protocol;
|
|
15
17
|
const isSameHost = thisUrl.hostname === thatUrl.hostname;
|
|
16
18
|
const isSamePort = thisUrl.port === thatUrl.port;
|
package/openapi/swagger.json
CHANGED