random bun scripts that dont fit anywhere else
at main 1.9 kB view raw
1import { serve } from "bun"; 2import { file } from "bun"; 3 4const PORT = Number.parseInt(process.env.PORT || "3000"); 5const ZIP_FILE_PATH = "./bigboy.gz"; // Path to your compressed file 6 7serve({ 8 port: PORT, 9 async fetch(req) { 10 const url = new URL(req.url); 11 12 // Only serve the file at the /download endpoint 13 if (url.pathname === "/download") { 14 try { 15 // Use Bun's file API to get the compressed file 16 const compressedFile = file(ZIP_FILE_PATH); 17 const fileSize = compressedFile.size; 18 19 // Set headers to trigger browser decompression 20 const headers = new Headers(); 21 headers.set("Content-Type", "application/octet-stream"); 22 headers.set("Content-Length", fileSize.toString()); 23 headers.set("Content-Encoding", "gzip"); 24 headers.set("Content-Disposition", `attachment; filename="bomb.gz"`); 25 headers.set("Cache-Control", "no-store, max-age=0"); 26 headers.set("ETag", `"${Math.random().toString(36).substring(2, 15)}"`); 27 headers.set("X-Compression-Ratio", "666:1"); 28 29 return new Response(compressedFile.stream(), { 30 status: 200, 31 headers: headers, 32 }); 33 } catch (error) { 34 console.error("Error serving file:", error); 35 return new Response("Error serving file", { status: 500 }); 36 } 37 } 38 39 // Serve a simple HTML page with a download link 40 return new Response( 41 ` 42 <!DOCTYPE html> 43 <html> 44 <head> 45 <title>File Download</title> 46 </head> 47 <body> 48 <h1>Compressed File Server</h1> 49 <p>Click below to download the file (104MB compressed, 100GB uncompressed)</p> 50 <a href="/download">Download File</a> 51 </body> 52 </html> 53 `, 54 { 55 headers: { 56 "Content-Type": "text/html", 57 }, 58 }, 59 ); 60 }, 61}); 62 63console.log(`Server running at http://localhost:${PORT}`);