better code: use standard function
Massimo Melina committed
Jan 12, 2022 at 11:15 UTC
ebf3647f1f35e898ad6f51440e8d267cedcd4fe4
3 files changed
+5
-9
src/index.ts
+1
@@ -25,6 +25,7 @@ export const SESSION_DURATION = 30*60_000
25
26
console.log('started', new Date().toLocaleString())
27
console.log('build', BUILD_TIMESTAMP)
28
+console.debug('cwd', process.cwd())
29
const app = new Koa()
30
app.keys = ['hfs-keys-test']
31
app.use(session({
src/perm.ts
+1
@@ -94,6 +94,7 @@ subscribeConfig({ k:'accounts', defaultValue:'accounts.yaml' }, v => {
94
const a = data?.accounts
95
if (!a)
96
return console.error('accounts file must contain "accounts" key')
97
+ console.debug('#accounts', Object.keys(a).length)
98
await applyAccounts(a)
99
})
100
})
src/serveFrontend.ts
+3
-9
@@ -1,6 +1,6 @@
1
import proxy from 'koa-better-http-proxy'
2
import Koa from 'koa'
3
-import { readFile } from 'fs'
3
+import fs from 'fs/promises'
4
import { DEV, FRONTEND_URI, METHOD_NOT_ALLOWED, NO_CONTENT } from './const'
5
import { serveFile } from './serveFile'
6
@@ -30,12 +30,12 @@ function serveStaticFrontend() : Koa.Middleware {
30
if (method !== 'GET')
31
return ctx.status = METHOD_NOT_ALLOWED
32
if (path.endsWith('/')) {
33
- ctx.body = replaceFrontEndRes(String(await filePromise(BASE + 'index.html')))
33
+ ctx.body = replaceFrontEndRes(String(await fs.readFile(BASE + 'index.html')))
34
ctx.type = 'html'
35
} else {
36
const fullPath = BASE + path.slice(1)
37
if (path.includes('static/js')) {// webpack
38
- ctx.body = String(await filePromise(fullPath))
38
+ ctx.body = String(await fs.readFile(fullPath))
39
.replace(/(return")(static\/)/g, '$1' + FRONTEND_URI.substring(1) + '$2')
40
ctx.type = 'js'
41
}
@@ -46,12 +46,6 @@ function serveStaticFrontend() : Koa.Middleware {
46
}
47
}
48
49
-function filePromise(path: string) : Promise<Buffer> {
50
- return new Promise((resolve, reject) =>
51
- readFile(path, (err,res) =>
52
- err ? reject(err) : resolve(res) ))
53
-}
54
-
49
function replaceFrontEndRes(body: string) {
50
return body.replace(/((?:src|href) *= *['"])\/?(?![a-z]+:\/\/)/g, '$1'+FRONTEND_URI)
51
}