( req: FastifyRequest, reply: FastifyReply, assetsDir: string, )
| 35 | } |
| 36 | |
| 37 | async function serveWebAsset( |
| 38 | req: FastifyRequest, |
| 39 | reply: FastifyReply, |
| 40 | assetsDir: string, |
| 41 | ): Promise<unknown> { |
| 42 | const requestUrl = new URL(req.url, 'http://kimi-web.local'); |
| 43 | if (isReservedPath(requestUrl.pathname)) { |
| 44 | return reply.callNotFound(); |
| 45 | } |
| 46 | |
| 47 | const filePath = await resolveStaticFile(assetsDir, requestUrl.pathname); |
| 48 | if (filePath === undefined) { |
| 49 | return reply.code(404).type('text/plain; charset=utf-8').send('Not found'); |
| 50 | } |
| 51 | |
| 52 | const fileInfo = await stat(filePath).catch(() => undefined); |
| 53 | if (fileInfo === undefined || !fileInfo.isFile()) { |
| 54 | return reply.code(404).type('text/plain; charset=utf-8').send('Not found'); |
| 55 | } |
| 56 | |
| 57 | return reply |
| 58 | .type(mimeType(filePath)) |
| 59 | .header('Content-Length', String(fileInfo.size)) |
| 60 | .send(createReadStream(filePath)); |
| 61 | } |
| 62 | |
| 63 | async function resolveStaticFile( |
| 64 | assetsDir: string, |
no test coverage detected