(endpoint, dir)
| 116 | const all = (a, b) => { add('ALL', a, b); }; |
| 117 | const getRoutes = () => ROUTES.reduce((acc, { method, path }) => ((acc[method] = acc[method] || []).push(path), acc), {}); |
| 118 | const serverStatic = (endpoint, dir) => { |
| 119 | const a = path.resolve(dir); |
| 120 | if (!fs.existsSync(a) || !fs.statSync(a).isDirectory()) { |
| 121 | console.error(`[STATIC] Directory not found or is not a directory: ${a}`); |
| 122 | return (req, res, next) => next(); |
| 123 | } |
| 124 | let b = (endpoint.endsWith('/') ? endpoint : endpoint + '/'); |
| 125 | return function staticMiddleware(req, res, next) { |
| 126 | if (req.method !== 'GET' && req.method !== 'HEAD') |
| 127 | return next(); |
| 128 | if (!req.path.startsWith(b)) |
| 129 | return next(); |
| 130 | let c = path.join(a, req.path.substring(b.length)); |
| 131 | let d = path.relative(a, c); |
| 132 | if (!(d && !d.startsWith('..') && !path.isAbsolute(d))) |
| 133 | return next(); |
| 134 | fs.stat(c, (err, stats) => { |
| 135 | if (err || !stats.isFile()) |
| 136 | return next(); |
| 137 | res.setHeader('Content-Type', getContentType(c)); |
| 138 | fs.createReadStream(c).pipe(res); |
| 139 | }); |
| 140 | }; |
| 141 | function getContentType(a) { |
| 142 | switch (path.extname(a).toLowerCase()) { |
| 143 | case '.html': return 'text/html'; |
| 144 | case '.js': return 'text/javascript'; |
| 145 | case '.css': return 'text/css'; |
| 146 | case '.json': return 'application/json'; |
| 147 | case '.txt': return 'text/plain'; |
| 148 | case '.ico': return 'image/x-icon'; |
| 149 | case '.png': return 'image/png'; |
| 150 | case '.webp': return 'image/webp'; |
| 151 | case '.jpg': return 'image/jpeg'; |
| 152 | case '.jpeg': return 'image/jpeg'; |
| 153 | case '.gif': return 'image/gif'; |
| 154 | case '.svg': return 'image/svg+xml'; |
| 155 | default: return 'application/octet-stream'; |
| 156 | } |
| 157 | } |
| 158 | }; |
| 159 | use((req, res, next) => { |
| 160 | if (req.headers['content-type']?.includes('application/json')) { |
| 161 | let d = ''; |
nothing calls this directly
no test coverage detected