Skip to content

Commit

Permalink
feat: global config validation
Browse files Browse the repository at this point in the history
fixes #399
  • Loading branch information
SgtPooki committed Nov 13, 2024
1 parent 0e40784 commit 3ff8a79
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/lib/config-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ export async function getConfig (logger: ComponentLogger): Promise<ConfigDb> {
debug
}
}

export async function validateConfig (config: ConfigDb, logger: ComponentLogger): Promise<void> {
const log = logger.forComponent('validate-config')

if (!config.enableRecursiveGateways && !config.enableGatewayProviders && !config.enableWss && !config.enableWebTransport) {
log.error('Config is invalid. At least one of the following must be enabled: recursive gateways, gateway providers, wss, or webtransport.')
throw new Error('Config is invalid. At least one of the following must be enabled: recursive gateways, gateway providers, wss, or webtransport.')
}
}
5 changes: 0 additions & 5 deletions src/lib/get-verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ export async function getVerifiedFetch (config: ConfigDb, logger: ComponentLogge
const log = logger.forComponent('get-verified-fetch')
log(`config-debug: got config for sw location ${self.location.origin}`, JSON.stringify(config, null, 2))

if (!config.enableRecursiveGateways && !config.enableGatewayProviders && !config.enableWss && !config.enableWebTransport) {
// crude validation
throw new Error('Config is invalid. At least one of the following must be enabled: recursive gateways, gateway providers, wss, or webtransport.')
}

// Start by adding the config routers as delegated routers
const routers: Array<Partial<Routing>> = []

Expand Down
4 changes: 3 additions & 1 deletion src/pages/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Input from '../components/textarea-input.jsx'
import { ConfigContext, ConfigProvider } from '../context/config-context.jsx'
import { RouteContext } from '../context/router-context.jsx'
import { ServiceWorkerProvider } from '../context/service-worker-context.jsx'
import { setConfig as storeConfig } from '../lib/config-db.js'
import { setConfig as storeConfig, validateConfig } from '../lib/config-db.js'
import { convertDnsResolverInputToObject, convertDnsResolverObjectToInput, convertUrlArrayToInput, convertUrlInputToArray } from '../lib/input-helpers.js'
import { getUiComponentLogger, uiLogger } from '../lib/logger.js'
import './default-page-styles.css'
Expand Down Expand Up @@ -110,6 +110,8 @@ function ConfigPage (): React.JSX.Element | null {

const saveConfig = useCallback(async () => {
try {
const config = { gateways, routers, dnsJsonResolvers, debug, enableGatewayProviders, enableRecursiveGateways, enableWss, enableWebTransport }
await validateConfig(config, uiComponentLogger)
setIsSaving(true)
await storeConfig({ gateways, routers, dnsJsonResolvers, debug, enableGatewayProviders, enableRecursiveGateways, enableWss, enableWebTransport }, uiComponentLogger)
log.trace('config-page: sending RELOAD_CONFIG to service worker')
Expand Down

0 comments on commit 3ff8a79

Please sign in to comment.