fix: default config values not working correctly

Massimo Melina committed Jan 3, 2022 at 15:17 UTC 5d9f67fc4e0d705fdd6a734f154ea332c69fbd8d
5 files changed +32 -17
src/config.ts
+24 -7
@@ -16,16 +16,33 @@ watchLoad(PATH, data => {
16 function check(k: string) {
17 const oldV = state[k]
18 const newV = data[k]
19 - if (JSON.stringify(newV) === JSON.stringify(oldV)) return
20 - state[k] = newV
21 - emitter.emit('new.'+k, newV, oldV)
19 + const { caster, defaultValue } = configProps[k] ?? {}
20 + let v = newV === undefined ? defaultValue : newV
21 + if (caster)
22 + v = caster(v)
23 + if (JSON.stringify(v) === JSON.stringify(oldV)) return
24 + state[k] = v
25 + emitter.emit('new.'+k, v, oldV)
26 }
27 })
28
25 -export function subscribe(k:string, cb:(v:any,was?:any)=>void, defaultValue?:any, caster?:(argV:string)=>any) {
26 - if (!caster)
27 - if (typeof defaultValue === 'number')
28 - caster = Number
29 +const configProps:Record<string, ConfigProps> = {}
30 +
31 +interface ConfigProps {
32 + defaultValue?:any,
33 + caster?:(argV:string)=>any
34 +}
35 +export function defineConfig(k:string, definition:ConfigProps) {
36 + configProps[k] = definition
37 + if (!definition.caster)
38 + if (typeof definition.defaultValue === 'number')
39 + definition.caster = Number
40 +}
41 +
42 +export function subscribeConfig({ k, ...definition }:{ k:string } & ConfigProps, cb:(v:any, was?:any)=>void) {
43 + if (definition)
44 + defineConfig(k, definition)
45 + const { caster, defaultValue } = configProps[k] ?? {}
46 const a = argv[k]
47 if (a !== undefined)
48 return cb(caster ? caster(a) : a)
src/index.ts
+3 -3
@@ -12,7 +12,7 @@ import compress from 'koa-compress'
12 // @ts-ignore
13 import accesslog from 'koa-accesslog'
14 import { Server } from 'http'
15 -import { subscribe } from './config'
15 +import { subscribeConfig } from './config'
16 import session from 'koa-session'
17 import { zipStreamFromFolder } from './zip'
18 import { frontEndApis } from './frontEndApis'
@@ -83,7 +83,7 @@ app.on('error', err => {
83 })
84
85 let srv: Server
86 -subscribe('port', async (port: number) => {
86 +subscribeConfig({ k:'port', defaultValue: 80 }, async (port: number) => {
87 await new Promise(resolve => {
88 if (!srv)
89 return resolve(null)
@@ -103,4 +103,4 @@ subscribe('port', async (port: number) => {
103 console.error(`couldn't listen on busy port ${port}`)
104 })
105 })
106 -}, 80)
106 +})
src/log.ts
+3 -3
@@ -1,6 +1,6 @@
1 import Koa from 'koa'
2 import { Writable } from 'stream'
3 -import { subscribe } from './config'
3 +import { subscribeConfig } from './config'
4 import { createWriteStream } from 'fs'
5 // @ts-ignore
6 import accesslog from 'koa-accesslog'
@@ -18,10 +18,10 @@ class Logger {
18 }
19 const accessLogger = new Logger()
20
21 -subscribe('log', path => {
21 +subscribeConfig({ k:'log', defaultValue:'access.log' }, path => {
22 console.debug('log file: ' + (path || 'disabled'))
23 accessLogger.setPath(path)
24 -}, 'access.log')
24 +})
25
26 export function log(): Koa.Middleware {
27 return (ctx, next) => // wrapping in a function will make it use current 'mw' value
src/vfs.ts
+2 -2
@@ -6,7 +6,7 @@ import { getCurrentUsernameExpanded } from './perm'
6 import Koa from 'koa'
7 import glob from 'fast-glob'
8 import _ from 'lodash'
9 -import { subscribe } from './config'
9 +import { subscribeConfig } from './config'
10
11 export enum VfsNodeType {
12 root,
@@ -74,7 +74,7 @@ export class Vfs {
74 }
75
76 export const vfs = new Vfs()
77 -subscribe('vfs', data => {
77 +subscribeConfig({ k: 'vfs' }, data => {
78 // we should validate content now
79 recur(data)
80 vfs.root = data
todo.md
-2
@@ -2,9 +2,7 @@
2 - anti-csrf
3 - upload
4 - search and login dialogs should push to history so that mobile can use back button to close them
5 -- get config values from command line
5 - accounts path in config
7 -- search dialog to add to history
6 - node.comment
7 - config: max speed (total/per-ip)
8 - config: max connections (total/per-ip)