diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-08-19 15:54:54 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-19 15:54:54 +0530 |
commit | 25a109d9ea27ad3a76fdce14bba283e953af9bce (patch) | |
tree | 68f0280065c9df4be8fa325ba82693879b4b46cd /cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js | |
parent | 9e576dff7c39cfd510c60ba92aa0d1c15fd24a6b (diff) |
chore(bench): add flash router benchmarks (#15495)
Diffstat (limited to 'cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js')
-rw-r--r-- | cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js new file mode 100644 index 000000000..a647b7015 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js @@ -0,0 +1,40 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.serveStatic = void 0; +const cloudflare_1 = require("../../utils/cloudflare"); +const filepath_1 = require("../../utils/filepath"); +const mime_1 = require("../../utils/mime"); +const DEFAULT_DOCUMENT = 'index.html'; +// This middleware is available only on Cloudflare Workers. +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); + const path = (0, filepath_1.getFilePath)({ + filename: options.path ?? url.pathname, + root: options.root, + defaultDocument: DEFAULT_DOCUMENT, + }); + const content = await (0, cloudflare_1.getContentFromKVAsset)(path, { + manifest: options.manifest, + namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined, + }); + 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; |