fix: requiring missing files for frontend/admin was spamming the console
Massimo Melina committed
Mar 10, 2022 at 16:16 UTC
ecb71569d05cd90c08dc0561b7c99691bc7e9ec4
2 files changed
+39
-34
server/src/index.ts
+1
-1
@@ -19,7 +19,7 @@ console.log(`License https://www.gnu.org/licenses/gpl-3.0.txt`)
19
console.log('started', HFS_STARTED.toLocaleString(), DEV)
20
console.log('version', VERSION||'-')
21
console.log('build', BUILD_TIMESTAMP||'-')
22
-console.debug('cwd', process.cwd())
22
+console.log('cwd', process.cwd())
23
24
const keys = ['hfs-keys-test']
25
server/src/serveFile.ts
+38
-33
@@ -46,41 +46,46 @@ export function serveFile(source:string, mime?:string, modifier?:(s:string)=>str
46
}
47
if (ctx.method !== 'GET')
48
return ctx.status = METHOD_NOT_ALLOWED
49
- const stats = await promisify(stat)(source) // using fs's function instead of fs/promises, because only the former is supported by pkg
50
- ctx.set('Last-Modified', stats.mtime.toUTCString())
51
- ctx.fileSource = source
52
- ctx.status = 200
53
- if (ctx.fresh)
54
- return ctx.status = 304
49
+ try {
50
+ const stats = await promisify(stat)(source) // using fs's function instead of fs/promises, because only the former is supported by pkg
51
+ ctx.set('Last-Modified', stats.mtime.toUTCString())
52
+ ctx.fileSource = source
53
+ ctx.status = 200
54
+ if (ctx.fresh)
55
+ return ctx.status = 304
56
56
- const conn = socket2connection(ctx.socket)
57
- if (conn)
58
- updateConnection(conn, { path: ctx.path })
59
- if (modifier)
60
- return ctx.body = modifier(String(await fs.readFile(source)))
61
- if (!range) {
62
- ctx.body = createReadStream(source)
63
- ctx.response.length = stats.size
64
- return
57
+ const conn = socket2connection(ctx.socket)
58
+ if (conn)
59
+ updateConnection(conn, { path: ctx.path })
60
+ if (modifier)
61
+ return ctx.body = modifier(String(await fs.readFile(source)))
62
+ if (!range) {
63
+ ctx.body = createReadStream(source)
64
+ ctx.response.length = stats.size
65
+ return
66
+ }
67
+ const ranges = range.split('=')[1]
68
+ if (ranges.includes(','))
69
+ return ctx.throw(400, 'multi-range not supported')
70
+ let bytes = ranges?.split('-')
71
+ if (!bytes?.length)
72
+ return ctx.throw(400, 'bad range')
73
+ const max = stats.size - 1
74
+ let start = Number(bytes[0]) || 0
75
+ let end = Number(bytes[1]) || max
76
+ if (end > max || start > max) {
77
+ ctx.status = 416
78
+ ctx.set('Content-Range', `bytes ${stats.size}`)
79
+ ctx.body = 'Requested Range Not Satisfiable'
80
+ return
81
+ }
82
+ ctx.status = 206
83
+ ctx.set('Content-Range', `bytes ${start}-${end}/${stats.size}`)
84
+ ctx.body = createReadStream(source, { start, end })
85
+ ctx.response.length = end - start + 1
86
}
66
- const ranges = range.split('=')[1]
67
- if (ranges.includes(','))
68
- return ctx.throw(400, 'multi-range not supported')
69
- let bytes = ranges?.split('-')
70
- if (!bytes?.length)
71
- return ctx.throw(400, 'bad range')
72
- const max = stats.size - 1
73
- let start = Number(bytes[0]) || 0
74
- let end = Number(bytes[1]) || max
75
- if (end > max || start > max) {
76
- ctx.status = 416
77
- ctx.set('Content-Range', `bytes ${stats.size}`)
78
- ctx.body = 'Requested Range Not Satisfiable'
79
- return
87
+ catch {
88
+ return ctx.status = 404
89
}
81
- ctx.status = 206
82
- ctx.set('Content-Range', `bytes ${start}-${end}/${stats.size}`)
83
- ctx.body = createReadStream(source, { start, end })
84
- ctx.response.length = end - start + 1
90
}
91
}