UNPKG

@nx.js/http

Version:
52 lines 1.63 kB
import mime from 'mime'; export function resolvePath(url, root) { const { pathname } = new URL(url); const resolved = new URL(pathname.replace(/^\/*/, ''), root); return resolved; } /** * Creates an HTTP handler function which serves file contents from the filesystem. * * @example * * ```typescript * import { createStaticFileHandler, listen } from '@nx.js/http'; * * const fileHandler = createStaticFileHandler('sdmc:/switch/'); * * listen({ * port: 8080, * async fetch(req) { * let res = await fileHandler(req); * if (!res) { * res = new Response('File not found', { status: 404 }); * } * return res; * } * }); * ``` * * @param root Root directory where static files are served from * @param opts Optional options object */ export function createStaticFileHandler(root, opts) { let rootStr = String(root); if (!rootStr.endsWith('/')) rootStr += '/'; const oHeaders = opts?.headers; return async (req) => { const url = resolvePath(req.url, rootStr); const stat = await Switch.stat(url); if (!stat) return null; // TODO: replace with readable stream API const data = await Switch.readFile(url); if (!data) return null; const headers = new Headers(typeof oHeaders === 'function' ? oHeaders(req, url) : oHeaders); headers.set('content-length', String(data.byteLength)); headers.set('content-type', mime.getType(url.pathname) || 'application/octet-stream'); return new Response(data, { headers }); }; } //# sourceMappingURL=static.js.map