better code: simplify false middleware

Massimo Melina committed Mar 13, 2023 at 15:59 UTC e71b8569818908475964498d98c1c1496b3abe61
4 files changed +36 -38
src/middlewares.ts
+1 -1
@@ -101,7 +101,7 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
101 return
102 }
103 if (ctx.originalUrl === '/favicon.ico' && favicon.get()) // originalUrl to not be subject to changes (vhosting plugin)
104 - return serveFile(favicon.get())(ctx,next)
104 + return serveFile(ctx, favicon.get())
105 const node = await urlToNode(path, ctx)
106 if (!node)
107 return ctx.status = HTTP_NOT_FOUND
src/plugins.ts
+1 -1
@@ -96,7 +96,7 @@ export function pluginsMiddleware(): Koa.Middleware {
96 const a = path.substring(PLUGINS_PUB_URI.length).split('/')
97 const name = a.shift()!
98 if (plugins.hasOwnProperty(name)) // do it only if the plugin is loaded
99 - await serveFile(plugins[name]!.folder + '/public/' + a.join('/'), 'auto')(ctx, next)
99 + await serveFile(ctx, plugins[name]!.folder + '/public/' + a.join('/'), 'auto')
100 return
101 }
102 await next()
src/serveFile.ts
+33 -35
@@ -41,46 +41,44 @@ export function serveFileNode(node: VfsNode) : Koa.Middleware {
41 }
42
43 ctx.vfsNode = node // useful to tell service files from files shared by the user
44 - return serveFile(source||'', mimeString)(ctx, next)
44 + return serveFile(ctx, source||'', mimeString)
45 }
46 }
47
48 const mimeCfg = defineConfig<Record<string,string>>('mime', { '*': 'auto' })
49
50 -export function serveFile(source:string, mime?:string, content?: string | Buffer) : Koa.Middleware {
51 - return async (ctx) => {
52 - if (!source)
53 - return
54 - const fn = path.basename(source)
55 - if (ctx.params.dl !== undefined) // please, download
56 - ctx.attachment(fn)
57 - mime = mime ?? _.find(mimeCfg.get(), (v,k) => k>'' && isMatch(fn, k)) // isMatch throws on an empty string
58 - if (mime === MIME_AUTO)
59 - mime = mimetypes.lookup(source) || ''
60 - if (mime)
61 - ctx.type = mime
62 - if (ctx.method === 'OPTIONS') {
63 - ctx.status = HTTP_NO_CONTENT
64 - ctx.set({ Allow: 'OPTIONS, GET, HEAD' })
65 - return
66 - }
67 - if (ctx.method !== 'GET')
68 - return ctx.status = HTTP_METHOD_NOT_ALLOWED
69 - try {
70 - const stats = await promisify(stat)(source) // using fs's function instead of fs/promises, because only the former is supported by pkg
71 - ctx.set('Last-Modified', stats.mtime.toUTCString())
72 - ctx.fileSource = source
73 - ctx.status = HTTP_OK
74 - if (ctx.fresh)
75 - return ctx.status = HTTP_NOT_MODIFIED
76 - if (content !== undefined)
77 - return ctx.body = content
78 - const range = getRange(ctx, stats.size)
79 - ctx.body = createReadStream(source, range)
80 - }
81 - catch (e: any) {
82 - return ctx.status = HTTP_NOT_FOUND
83 - }
50 +export async function serveFile(ctx: Koa.Context, source:string, mime?:string, content?: string | Buffer) {
51 + if (!source)
52 + return
53 + const fn = path.basename(source)
54 + if (ctx.params.dl !== undefined) // please, download
55 + ctx.attachment(fn)
56 + mime = mime ?? _.find(mimeCfg.get(), (v,k) => k>'' && isMatch(fn, k)) // isMatch throws on an empty string
57 + if (mime === MIME_AUTO)
58 + mime = mimetypes.lookup(source) || ''
59 + if (mime)
60 + ctx.type = mime
61 + if (ctx.method === 'OPTIONS') {
62 + ctx.status = HTTP_NO_CONTENT
63 + ctx.set({ Allow: 'OPTIONS, GET, HEAD' })
64 + return
65 + }
66 + if (ctx.method !== 'GET')
67 + return ctx.status = HTTP_METHOD_NOT_ALLOWED
68 + try {
69 + const stats = await promisify(stat)(source) // using fs's function instead of fs/promises, because only the former is supported by pkg
70 + ctx.set('Last-Modified', stats.mtime.toUTCString())
71 + ctx.fileSource = source
72 + ctx.status = HTTP_OK
73 + if (ctx.fresh)
74 + return ctx.status = HTTP_NOT_MODIFIED
75 + if (content !== undefined)
76 + return ctx.body = content
77 + const range = getRange(ctx, stats.size)
78 + ctx.body = createReadStream(source, range)
79 + }
80 + catch (e: any) {
81 + return ctx.status = HTTP_NOT_FOUND
82 }
83 }
84
src/serveGuiFiles.ts
+1 -1
@@ -48,7 +48,7 @@ function serveStatic(uri: string): Koa.Middleware {
48 if (content === null)
49 return ctx.status = HTTP_NOT_FOUND
50 if (!serveApp)
51 - return serveFile(fullPath, 'auto', content)(ctx, next)
51 + return serveFile(ctx, fullPath, 'auto', content)
52 // we don't cache the index as it's small and may prevent plugins change to apply
53 ctx.body = await treatIndex(ctx, uri, String(content))
54 ctx.type = 'html'