Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/server/src/AppConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { afterEach, describe, expect, it, jest } from '@jest/globals'

const ORIGINAL_ENV = process.env

describe('AppConfig', () => {
afterEach(() => {
process.env = ORIGINAL_ENV
jest.resetModules()
})

it('treats whitespace-padded true values as enabled', async () => {
process.env = { ...ORIGINAL_ENV, SHOW_COMMUNITY_NODES: ' true ' }

const { appConfig } = await import('./AppConfig')

expect(appConfig.showCommunityNodes).toBe(true)
})

it('keeps whitespace-padded false values disabled', async () => {
process.env = { ...ORIGINAL_ENV, SHOW_COMMUNITY_NODES: ' false ' }

const { appConfig } = await import('./AppConfig')

expect(appConfig.showCommunityNodes).toBe(false)
})

it('defaults to disabled when the env var is unset', async () => {
process.env = { ...ORIGINAL_ENV }
delete process.env.SHOW_COMMUNITY_NODES

const { appConfig } = await import('./AppConfig')

expect(appConfig.showCommunityNodes).toBe(false)
})
})
10 changes: 9 additions & 1 deletion packages/server/src/AppConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const parseBooleanEnv = (value: string | undefined, defaultValue: boolean) => {
if (value === undefined) {
return defaultValue
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the project's general rules, loose equality (== null) should be used as a standard idiom for nullish checks to cover both null and undefined. This is more idiomatic in this codebase and also makes the helper function more robust if it's ever used with values that could be null.

Suggested change
if (value === undefined) {
return defaultValue
}
if (value == null) {
return defaultValue
}
References
  1. In JavaScript/TypeScript, use loose equality (== null) as a standard idiom for a 'nullish' check that covers both null and undefined.


return value.trim().toLowerCase() === 'true'
}

export const appConfig = {
showCommunityNodes: process.env.SHOW_COMMUNITY_NODES ? process.env.SHOW_COMMUNITY_NODES.toLowerCase() === 'true' : false
showCommunityNodes: parseBooleanEnv(process.env.SHOW_COMMUNITY_NODES, false)
// todo: add more config options here like database, log, storage, credential and allow modification from UI
}