node.mime

Massimo Melina committed Dec 24, 2021 at 00:22 UTC fea94ffafe2fad3d89c04f5cf733d98bd2f04eb6
6 files changed +14 -4
src/apis.ts
+1 -1
@@ -138,7 +138,7 @@ async function nodeToDirEntry(node: VfsNode): Promise<DirEntry | null> {
138 : null
139 }
140 catch (err:any) {
141 - console.error('ERR', node.source, err?.code || err)
141 + console.error(String(err))
142 return null
143 }
144 }
src/index.ts
+1 -1
@@ -52,7 +52,7 @@ srv.use(async (ctx, next) => {
52 }
53 if (source)
54 return source.includes('//') ? mount(path,proxy(source,{}))(ctx,next)
55 - : serveFile(source)(ctx,next)
55 + : serveFile(node)(ctx,next)
56 await next()
57 })
58
src/serveFile.ts
+7 -1
@@ -2,11 +2,17 @@ import Koa from 'koa'
2 import { createReadStream } from 'fs'
3 import fs from 'fs/promises'
4 import { METHOD_NOT_ALLOWED, NO_CONTENT } from './const'
5 +import { VfsNode } from './vfs'
6
6 -export function serveFile(source: string) : Koa.Middleware {
7 +export function serveFile(node: VfsNode) : Koa.Middleware {
8 return async (ctx) => {
9 + let { source, mime } = node
10 + if (!source)
11 + return
12 const { range } = ctx.request.header
13 ctx.set('Accept-Ranges', 'bytes')
14 + if (mime)
15 + ctx.type = mime
16 if (ctx.method === 'OPTIONS') {
17 ctx.status = NO_CONTENT
18 ctx.set({ Allow: 'OPTIONS, GET' })
src/vfs.ts
+2 -1
@@ -24,7 +24,8 @@ export interface VfsNode {
24 remove?: string | string[],
25 hidden?: boolean,
26 rename?: Record<string,string>,
27 - perm?: Record<string, SinglePerm>
27 + perm?: Record<string, SinglePerm>,
28 + mime?: string
29 }
30
31 type SinglePerm = 'r' | 'w'
tests/index.html new
+1
@@ -0,0 +1 @@
1 +<h1>This is a test</h1>
vfs.yaml
+2
@@ -5,6 +5,8 @@ children:
5 children:
6 - source: tests/alfa.txt
7 - name: f3
8 + mime: html
9 + source: tests/index.html
10 - name: proxy
11 source: https://raw.githubusercontent.com/nodejs/node/master/README.md
12 - name: for-rejetto