1
0
forked from baron/baron-sso
Files
baron-sso/userfront-e2e/scripts/serve-userfront-build.mjs
2026-02-24 15:23:36 +09:00

69 lines
2.2 KiB
JavaScript

import { createReadStream, existsSync, statSync } from 'node:fs';
import { dirname, extname, join, normalize } from 'node:path';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const root = normalize(join(__dirname, '../../userfront/build/web'));
if (!existsSync(root) || !statSync(root).isDirectory()) {
console.error(
'[userfront-e2e] userfront/build/web not found. Run: cd userfront && flutter build web --wasm --release',
);
process.exit(1);
}
const port = Number.parseInt(process.env.PORT ?? '4173', 10);
const contentTypes = {
'.css': 'text/css; charset=utf-8',
'.html': 'text/html; charset=utf-8',
'.ico': 'image/x-icon',
'.js': 'application/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.mjs': 'application/javascript; charset=utf-8',
'.png': 'image/png',
'.svg': 'image/svg+xml; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
'.wasm': 'application/wasm',
'.webmanifest': 'application/manifest+json; charset=utf-8',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
};
const server = createServer((req, res) => {
const url = new URL(req.url ?? '/', 'http://localhost');
const pathname = decodeURIComponent(url.pathname);
const relative = pathname === '/' ? '/index.html' : pathname;
const candidate = normalize(join(root, relative));
if (!candidate.startsWith(root)) {
res.statusCode = 403;
res.end('Forbidden');
return;
}
let filePath = candidate;
if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
// Flutter web 라우팅 경로(`/ko`, `/ko/signin`)도 index.html로 fallback 처리
filePath = join(root, 'index.html');
}
const ext = extname(filePath);
const contentType = contentTypes[ext] ?? 'application/octet-stream';
res.setHeader('Content-Type', contentType);
createReadStream(filePath)
.on('error', () => {
res.statusCode = 500;
res.end('Internal Server Error');
})
.pipe(res);
});
server.listen(port, '127.0.0.1', () => {
console.log(`[userfront-e2e] serving ${root} at http://127.0.0.1:${port}`);
});