Skip to content

Commit

Permalink
perf(config): fixed set of object keys (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Dec 2, 2022
1 parent eee9367 commit 952f919
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
12 changes: 8 additions & 4 deletions src/config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("Configuration", () => {
const PROC_LOAD_MAX_RSS_BYTES = "";
const PROC_LOAD_MAX_EVENT_LOOP_UTILIZATION = "";
const RATE_LIMIT_MAX_CONNECTIONS_PER_MIN = "";
const RATE_LIMIT_EXCLUDED_ARRAY = '["127.0.0.1"]';
const RATE_LIMIT_EXCLUDED_ARRAY = "";
const AUTH_BEARER_TOKEN_ARRAY = "";
const OCR_ENABLED = "";
const OCR_LANGUAGES = "";
Expand Down Expand Up @@ -111,8 +111,12 @@ describe("Configuration", () => {
expect(config.fastifyInit.http2).toBeUndefined();

expect(config.cors).toEqual({
origin: false,
allowedHeaders: null,
credentials: false,
exposedHeaders: null,
hideOptionsRoute: true,
maxAge: null,
origin: false,
});

expect(config.processLoad).toEqual({
Expand All @@ -123,7 +127,7 @@ describe("Configuration", () => {
});

expect(config.rateLimit).toEqual({
allowList: JSON.parse(RATE_LIMIT_EXCLUDED_ARRAY),
allowList: null,
continueExceeding: true,
hook: "onSend",
max: 1000,
Expand Down Expand Up @@ -434,7 +438,7 @@ describe("Configuration", () => {
expect(config.cors).toEqual({
origin: expected.origin,
allowedHeaders: CORS_ALLOWED_HEADERS,
credentials: expected?.credentials,
credentials: expected?.credentials || false,
exposedHeaders: CORS_EXPOSED_HEADERS,
hideOptionsRoute: true,
maxAge: CORS_MAX_AGE,
Expand Down
28 changes: 8 additions & 20 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,12 @@ async function getConfig() {
ignoreTrailingSlash: true,
},
cors: {
origin: parseCorsParameter(env.CORS_ORIGIN) || false,
allowedHeaders: env.CORS_ALLOWED_HEADERS || null,
credentials: env.CORS_ALLOW_CREDENTIALS === true,
exposedHeaders: env.CORS_EXPOSED_HEADERS || null,
hideOptionsRoute: true,
maxAge: env.CORS_MAX_AGE || null,
origin: parseCorsParameter(env.CORS_ORIGIN) || false,
},
processLoad: {
maxEventLoopDelay: env.PROC_LOAD_MAX_EVENT_LOOP_DELAY || 0,
Expand All @@ -190,6 +194,9 @@ async function getConfig() {
maxRssBytes: env.PROC_LOAD_MAX_RSS_BYTES || 0,
},
rateLimit: {
allowList: env.RATE_LIMIT_EXCLUDED_ARRAY
? secJSON.parse(env.RATE_LIMIT_EXCLUDED_ARRAY)
: null,
continueExceeding: true,
hook: "onSend",
max: env.RATE_LIMIT_MAX_CONNECTIONS_PER_MIN || 1000,
Expand Down Expand Up @@ -324,12 +331,6 @@ async function getConfig() {
});
}

if (env.RATE_LIMIT_EXCLUDED_ARRAY) {
config.rateLimit.allowList = secJSON.parse(
env.RATE_LIMIT_EXCLUDED_ARRAY
);
}

if (env.AUTH_BEARER_TOKEN_ARRAY) {
const keys = new Set();
secJSON.parse(env.AUTH_BEARER_TOKEN_ARRAY).forEach((element) => {
Expand All @@ -348,19 +349,6 @@ async function getConfig() {
};
}

if (env.CORS_ALLOW_CREDENTIALS === true) {
config.cors.credentials = true;
}
if (env.CORS_ALLOWED_HEADERS) {
config.cors.allowedHeaders = env.CORS_ALLOWED_HEADERS;
}
if (env.CORS_EXPOSED_HEADERS) {
config.cors.exposedHeaders = env.CORS_EXPOSED_HEADERS;
}
if (env.CORS_MAX_AGE) {
config.cors.maxAge = env.CORS_MAX_AGE;
}

// Enable HTTPS using cert/key or passphrase/pfx combinations
if (env.HTTPS_SSL_CERT_PATH && env.HTTPS_SSL_KEY_PATH) {
try {
Expand Down

0 comments on commit 952f919

Please sign in to comment.