summaryrefslogtreecommitdiff
path: root/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.js
blob: d29a7d9366392e76d1aacbea0d834a6b324ac51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serveStatic = void 0;
const filepath_1 = require("../../utils/filepath");
const mime_1 = require("../../utils/mime");
// @ts-ignore
const { file } = Bun;
const DEFAULT_DOCUMENT = 'index.html';
const serveStatic = (options = { root: '' }) => {
    return async (c, next) => {
        // Do nothing if Response is already set
        if (c.res && c.finalized) {
            await next();
        }
        const url = new URL(c.req.url);
        let path = (0, filepath_1.getFilePath)({
            filename: options.path ?? url.pathname,
            root: options.root,
            defaultDocument: DEFAULT_DOCUMENT,
        });
        path = `./${path}`;
        const content = file(path);
        if (content) {
            const mimeType = (0, mime_1.getMimeType)(path);
            if (mimeType) {
                c.header('Content-Type', mimeType);
            }
            // Return Response object
            return c.body(content);
        }
        else {
            console.warn(`Static file: ${path} is not found`);
            await next();
        }
        return;
    };
};
exports.serveStatic = serveStatic;