beavuck-time 2.4.0 → 2.4.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/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: 100
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
@@ -15,7 +15,11 @@ const corsError_1 = require("../errors/corsError");
15
15
  const urlUtil_1 = require("../utils/urlUtil");
16
16
  const http_status_codes_1 = require("http-status-codes");
17
17
  const dotenvx_1 = __importDefault(require("@dotenvx/dotenvx"));
18
- dotenvx_1.default.config();
18
+ // Load environment variables from .env file, if available
19
+ // but don't error if the file is missing
20
+ dotenvx_1.default.config({
21
+ ignore: ['MISSING_ENV_FILE'],
22
+ });
19
23
  exports.corsOptions = {
20
24
  methods: ['GET', 'OPTIONS'],
21
25
  optionsSuccessStatus: http_status_codes_1.StatusCodes.OK,
@@ -50,11 +54,16 @@ function getTrustedOrigins() {
50
54
  }
51
55
  function initTrustedOrigins() {
52
56
  const trustedOrigins = process.env.BEAVUCK_TIME_TRUSTED_ORIGINS;
53
- TRUSTED_ORIGINS = trustedOrigins.split(',');
57
+ TRUSTED_ORIGINS = trustedOrigins
58
+ .split(',')
59
+ .map(to => to.trim())
60
+ .filter(to => to.length > 0);
54
61
  }
55
62
  function isAllTrusted() {
56
63
  return getTrustedOrigins().includes('*');
57
64
  }
58
65
  function isOriginAbsentOrTrusted(origin) {
59
- return !origin || getTrustedOrigins().includes(origin);
66
+ return (!origin ||
67
+ getTrustedOrigins().includes(origin) ||
68
+ getTrustedOrigins().some(to => to.includes('/*') && origin.startsWith(to)));
60
69
  }
@@ -8,6 +8,8 @@ exports.logger = void 0;
8
8
  const winston_1 = require("winston");
9
9
  require("winston-daily-rotate-file");
10
10
  const dotenvx_1 = __importDefault(require("@dotenvx/dotenvx"));
11
+ // Load environment variables from .env file, if available
12
+ // but don't error if the file is missing
11
13
  dotenvx_1.default.config({
12
14
  ignore: ['MISSING_ENV_FILE'],
13
15
  });
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
- const API_PORT = options.port ?? process.env.BEAVUCK_TIME_API_PORT ?? 3000;
33
- const HOST_URL = options.hostUrl ?? process.env.BEAVUCK_TIME_HOST_URL ?? '';
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 ${API_PORT} (if this is running in a container, this port number is internal to the container)
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,47 @@ function startServer(options = {}) {
65
48
  process.on(SIGINT, () => gracefulShutdown(SIGINT));
66
49
  return server;
67
50
  }
51
+ function manageEnvVars(options) {
52
+ logger_1.logger.debug('serverOptions: ' + JSON.stringify(options));
53
+ process.env.BEAVUCK_TIME_API_PORT =
54
+ options.apiPort === undefined
55
+ ? '3000'
56
+ : String(options.apiPort);
57
+ logger_1.logger.debug('API_PORT: ' + process.env.BEAVUCK_TIME_API_PORT);
58
+ process.env.BEAVUCK_TIME_HOST_URL =
59
+ options.hostUrl ?? process.env.BEAVUCK_TIME_HOST_URL;
60
+ logger_1.logger.debug('HOST_URL: ' + process.env.BEAVUCK_TIME_HOST_URL);
61
+ process.env.BEAVUCK_TIME_TRUSTED_ORIGINS =
62
+ options.trustedOrigins ?? process.env.BEAVUCK_TIME_TRUSTED_ORIGINS;
63
+ logger_1.logger.debug('TRUSTED_ORIGINS: ' + process.env.BEAVUCK_TIME_TRUSTED_ORIGINS);
64
+ process.env.BEAVUCK_TIME_RATE_LIMIT =
65
+ options.rateLimit === undefined
66
+ ? process.env.BEAVUCK_TIME_RATE_LIMIT
67
+ : String(options.rateLimit);
68
+ logger_1.logger.debug('RATE_LIMIT: ' + process.env.BEAVUCK_TIME_RATE_LIMIT);
69
+ process.env.BEAVUCK_TIME_LOG_LEVEL =
70
+ options.logLevel ?? process.env.BEAVUCK_TIME_LOG_LEVEL;
71
+ logger_1.logger.debug('LOG_LEVEL: ' + process.env.BEAVUCK_TIME_LOG_LEVEL);
72
+ process.env.BEAVUCK_TIME_MAX_LOG_FILES =
73
+ options.maxLogFiles === undefined
74
+ ? process.env.BEAVUCK_TIME_MAX_LOG_FILES
75
+ : String(options.maxLogFiles);
76
+ logger_1.logger.debug('MAX_LOG_FILES: ' + process.env.BEAVUCK_TIME_MAX_LOG_FILES);
77
+ process.env.BEAVUCK_TIME_MAX_SIZE_LOG_FILES =
78
+ options.maxSizeLogFiles ?? process.env.BEAVUCK_TIME_MAX_SIZE_LOG_FILES;
79
+ logger_1.logger.debug('MAX_SIZE_LOG_FILES: ' + process.env.BEAVUCK_TIME_MAX_SIZE_LOG_FILES);
80
+ validateEnv();
81
+ }
82
+ function validateEnv() {
83
+ if (!process.env.BEAVUCK_TIME_HOST_URL || !(0, urlUtil_1.tryParseUrl)(process.env.BEAVUCK_TIME_HOST_URL)) {
84
+ logger_1.logger.error('HOST_URL not set or is not a valid URL');
85
+ process.exit(1);
86
+ }
87
+ if (!process.env.BEAVUCK_TIME_TRUSTED_ORIGINS) {
88
+ logger_1.logger.error('TRUSTED_ORIGINS not set');
89
+ process.exit(1);
90
+ }
91
+ }
68
92
  // Support running as standalone binary
69
93
  if (require.main === module) {
70
94
  startServer();
@@ -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
- if (thisUrl.origin === thatUrl.origin)
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;
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "info": {
34
34
  "title": "beavuck-time",
35
- "version": "2.4.0",
35
+ "version": "2.4.2",
36
36
  "description": "Get time in ISO format, in UTC timezone, from a simple, lightweight node server",
37
37
  "license": {
38
38
  "name": "Unlicense"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beavuck-time",
3
- "version": "2.4.0",
3
+ "version": "2.4.2",
4
4
  "description": "Get time in ISO format, in UTC timezone, from a simple, lightweight node server",
5
5
  "keywords": [
6
6
  "time",