fix: avoid manipulating non-index files in frontend
Massimo Melina committed
Dec 19, 2021 at 01:35 UTC
a223081e04480cb2278f8297eb267c7db0f0990c
2 files changed
+11
-9
dev-notes.md
+1
-4
@@ -4,15 +4,12 @@
4
- material
5
- css+js https://materializecss.com/
6
7
-# Study
8
-- https://www.tutorialspoint.com/koajs/koajs_quick_guide.htm
9
-
7
# To do
8
- search
9
+- sticky breadcrumbs
10
- frontend: dialogs
11
- vfs: serve an html for a folder?
12
- "default" property for vfsNode?
15
-- sticky breadcrumbs
13
- interruption of long requests if client aborted (searching/listing)
14
- throttle speed
15
- frontend
src/frontend.ts
+10
-5
@@ -2,7 +2,7 @@ import proxy from 'koa-better-http-proxy'
2
import Koa from 'koa'
3
import MemoMap from './MemoMap'
4
import mime from 'mime-types'
5
-import { readFile } from 'fs'
5
+import { createReadStream, readFile } from 'fs'
6
import { DEV, FRONTEND_URI } from './const'
7
8
const FRONTEND = __dirname + '/frontend/'
@@ -13,7 +13,10 @@ function serveProxyFrontend() {
13
console.debug('fronted: proxied')
14
return proxy('localhost:3000', {
15
proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
16
- userResDecorator: (res, data) => replaceFrontEndRes(data.toString('utf8'))
16
+ userResDecorator: (res, data, req) => {
17
+ return req.url.endsWith('/') ? replaceFrontEndRes(data.toString('utf8'))
18
+ : data
19
+ }
20
})
21
}
22
@@ -23,9 +26,11 @@ function serveStaticFrontend() : Koa.Middleware {
26
let file = ctx.path
27
if (file.startsWith('/'))
28
file = file.slice(1)
26
- ctx.body = await cache.getOrSet(file, () =>
27
- filePromise(FRONTEND + (file || 'index.html')).then(res =>
28
- replaceFrontEndRes(res.toString('utf8')) ))
29
+ const untouched = Boolean(file)
30
+ ctx.body = untouched ? createReadStream(FRONTEND + file)
31
+ : await cache.getOrSet(file, () =>
32
+ filePromise(FRONTEND + (file || 'index.html')).then(res =>
33
+ replaceFrontEndRes(res.toString('utf8')) ))
34
ctx.type = file ? (mime.lookup(file) || 'application/octet-stream') : 'html'
35
await next()
36
}