This repository has been archived on 2025-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
cetra/examples/server.js
2024-01-11 23:22:18 +00:00

41 lines
1.0 KiB
JavaScript

import { unlinkSync } from 'node:fs';
import { resolve } from 'path';
const transpiler = Bun.Transpiler({
loader: 'cjs',
});
export default {
port: Bun.env.BUN_PORT || Bun.env.APP_PORT || 3000,
async fetch(req) {
const magicKey = Math.random().toString(36).substring(2, 10);
const url = new URL(req.url);
if (url.pathname == '/main.js') {
const path = resolve(__dirname, `./.cache/`);
const filename = `${magicKey}.js`;
const file = Bun.file(resolve(path, filename));
const result = await Bun.build({
entrypoints: ['./src/main.js'],
outdir: path,
target: 'browser',
splitting: true,
naming: `[dir]/${filename}`,
});
console.log(result);
const resp = new Response(file, {
headers: { 'Cache-Control': 'no-cache' },
});
setTimeout(() => {
unlinkSync(resolve(path, filename));
}, 400);
return resp;
}
if (url.pathname == '/')
return new Response(Bun.file(resolve('./index.html')));
return new Response('ERR 404: Not Found', { status: 404 });
},
};