better code: use constant
Massimo Melina committed
Mar 13, 2022 at 20:26 UTC
316710adfb8c7f714c3084b1228a4ba107e21595
4 files changed
+9
-9
server/src/adminApis.ts
+2
-2
@@ -3,7 +3,7 @@
3
import { ApiError, ApiHandlers } from './apiMiddleware'
4
import { getConfig, getWholeConfig, setConfig } from './config'
5
import { getStatus } from './listen'
6
-import { BUILD_TIMESTAMP, HFS_STARTED, VERSION } from './const'
6
+import { BUILD_TIMESTAMP, FORBIDDEN, HFS_STARTED, VERSION } from './const'
7
import vfsApis from './api.vfs'
8
import accountsApis from './api.accounts'
9
import { Connection, getConnections } from './connections'
@@ -24,7 +24,7 @@ export const adminApis: ApiHandlers = {
24
const noHttp = (v.port ?? getConfig('port')) < 0 || !st.httpSrv.listening
25
const noHttps = (v.https_port ?? getConfig('https_port')) < 0 || !st.httpsSrv.listening
26
if (noHttp && noHttps)
27
- return new ApiError(403, "You cannot switch off both http and https ports")
27
+ return new ApiError(FORBIDDEN, "You cannot switch off both http and https ports")
28
await setConfig(v)
29
}
30
return {}
server/src/api.accounts.ts
+2
-2
@@ -13,7 +13,7 @@ import {
13
setAccount
14
} from './perm'
15
import _ from 'lodash'
16
-import { getConfig } from './config'
16
+import { FORBIDDEN } from './const'
17
18
const apis: ApiHandlers = {
19
@@ -42,7 +42,7 @@ const apis: ApiHandlers = {
42
43
add_account({ username, ...rest }) {
44
if (getAccount(username))
45
- return new ApiError(403)
45
+ return new ApiError(FORBIDDEN)
46
if (!addAccount(username, rest))
47
return new ApiError(400)
48
return {}
server/src/api.auth.ts
+2
-2
@@ -4,7 +4,7 @@ import { getAccount, getCurrentUsername, getFromAccount } from './perm'
4
import { verifyPassword } from './crypt'
5
import { ApiError, ApiHandler } from './apiMiddleware'
6
import { SRPParameters, SRPRoutines, SRPServerSession, SRPServerSessionStep1 } from 'tssrp6a'
7
-import { ADMIN_URI, SESSION_DURATION } from './const'
7
+import { ADMIN_URI, FORBIDDEN, SESSION_DURATION } from './const'
8
import { randomId } from './misc'
9
import Koa from 'koa'
10
import { changeSrpHelper, changePasswordHelper } from './api.helpers'
@@ -66,7 +66,7 @@ export const loginSrp1: ApiHandler = async ({ username }, ctx) => {
66
if (!account.srp)
67
return new ApiError(406) // unacceptable
68
if (!getFromAccount(account, a => a.admin))
69
- return new ApiError(403)
69
+ return new ApiError(FORBIDDEN)
70
71
const [salt, verifier] = account.srp.split('|')
72
const step1 = await srpSession.step1(account.username, BigInt(salt), BigInt(verifier))
server/src/api.vfs.ts
+3
-3
@@ -9,7 +9,7 @@ import glob from 'fast-glob'
9
import { enforceFinal, isWindowsDrive, objSameKeys } from './misc'
10
import { exec } from 'child_process'
11
import { promisify } from 'util'
12
-import { IS_WINDOWS } from './const'
12
+import { FORBIDDEN, IS_WINDOWS } from './const'
13
14
type VfsAdmin = {
15
type?: string,
@@ -72,7 +72,7 @@ const apis: ApiHandlers = {
72
if (!n)
73
return new ApiError(404, 'invalid under')
74
if (n.isTemp || !await nodeIsDirectory(n))
75
- return new ApiError(403, 'invalid under')
75
+ return new ApiError(FORBIDDEN, 'invalid under')
76
const a = n.children || (n.children = [])
77
if (source && a.find(x => x.source === source))
78
return new ApiError(409, 'already present')
@@ -94,7 +94,7 @@ const apis: ApiHandlers = {
94
const parent = dirname(uri)
95
const parentNode = await urlToNodeOriginal(parent)
96
if (!parentNode)
97
- return 403
97
+ return FORBIDDEN
98
const { children } = parentNode
99
if (!children) // shouldn't happen
100
return 500