better code
Massimo Melina committed
Apr 30, 2024 at 18:30 UTC
699769e2cf0abf29a60ce41964e1857318f84b89
2 files changed
+8
-7
src/config.ts
+1
-1
@@ -135,7 +135,7 @@ export function setConfig(newCfg: Record<string,unknown>, save?: boolean) {
135
if (save === false) // false is used when loading whole config, and in such case we should not leave previous values untreated. Also, we need this only after we already `started`.
136
for (const k of Object.keys(state))
137
if (!newCfg.hasOwnProperty(k))
138
- apply(k, newCfg[k])
138
+ apply(k, undefined)
139
return
140
}
141
// first time we emit also for the default values
src/perm.ts
+7
-6
@@ -1,7 +1,7 @@
1
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3
import _ from 'lodash'
4
-import { HTTP_BAD_REQUEST, objRenameKey, objSameKeys, setHidden, wantArray } from './misc'
4
+import { HTTP_BAD_REQUEST, objRenameKey, objSameKeys, setHidden, typedEntries, wantArray } from './misc'
5
import { defineConfig, saveConfigAsap } from './config'
6
import { createVerifierAndSalt, SRPParameters, SRPRoutines } from 'tssrp6a'
7
import events from './events'
@@ -73,7 +73,8 @@ export async function updateAccount(account: Account, change: Partial<Account> |
73
await change?.(account)
74
else
75
Object.assign(account, objSameKeys(change, x => x || undefined))
76
-
76
+ for (const [k,v] of typedEntries(account))
77
+ if (!v) delete account[k] // we consider all account fields, when falsy, as equivalent to be missing (so, default value applies)
78
const { username, password } = account
79
if (password) {
80
console.debug('hashing password for', username)
@@ -145,11 +146,11 @@ export async function addAccount(username: string, props: Partial<Account>) {
146
username = normalizeUsername(username)
147
if (!username || getAccount(username, false))
148
return
148
- const copy: Account = setHidden(_.pickBy(props, Boolean), { username }) // have the field in the object but hidden so that stringification won't include it
149
+ const newAccount: Account = setHidden({}, { username }) // have the field in the object but hidden so that stringification won't include it
150
accountsConfig.set(accounts =>
150
- Object.assign(accounts, { [username]: copy }))
151
- await updateAccount(copy, copy)
152
- return copy
151
+ Object.assign(accounts, { [username]: newAccount }))
152
+ await updateAccount(newAccount, props)
153
+ return newAccount
154
}
155
156
export function delAccount(username: string) {