Download src/server.ts from Reflectus/iper: direct link, hf CLI and curl.
- Browser
- Download file 8.84 kB
-
https://huggingface.co/spaces/Reflectus/iper/resolve/main/src/server.ts
- Command line
-
hf download hf://spaces/Reflectus/iper/src/server.ts
-
curl -L -o server.ts https://huggingface.co/spaces/Reflectus/iper/resolve/main/src/server.ts
8.84 kB
| import { createServer, IncomingMessage, ServerResponse } from "node:http"; | |
| import { OpenAIService } from "./openai-service"; | |
| const openAIService = new OpenAIService(); | |
| const log = (msg: string) => { | |
| const time = new Date().toISOString().split('T')[1].split('.')[0]; | |
| console.log(`[${time}] ${msg}`); | |
| }; | |
| const HTML_FRONTEND = ` | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>AI Research Playground</title> | |
| <style> | |
| body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #0b0e14; color: #e6edf3; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; } | |
| .container { text-align: center; border: 1px solid #30363d; padding: 40px; border-radius: 12px; background: #161b22; max-width: 500px; } | |
| h1 { color: #58a6ff; margin-bottom: 10px; } | |
| p { color: #8b949e; line-height: 1.6; } | |
| .status { display: inline-block; padding: 4px 12px; border-radius: 20px; background: #238636; color: white; font-size: 14px; margin-top: 20px; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>🔬 Research API</h1> | |
| <p>This Space hosts a distributed language model inference engine for academic research and integration testing.</p> | |
| <div class="status">System Online</div> | |
| <p style="font-size: 12px; margin-top: 30px;">Authorized access only via encrypted endpoints.</p> | |
| </div> | |
| </body> | |
| </html> | |
| `; | |
| const API_KEY = process.env.API_KEY || "MySecretKey_12345"; | |
| const PORT = Number(process.env.PORT) || 7860; | |
| const server = createServer(async (req: IncomingMessage, res: ServerResponse) => { | |
| try { | |
| const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`); | |
| const method = req.method; | |
| // CORS Headers | |
| const corsHeaders = { | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Methods": "GET, POST, OPTIONS", | |
| "Access-Control-Allow-Headers": "Content-Type, Authorization", | |
| }; | |
| // Helper to send JSON response | |
| const sendJSON = (statusCode: number, data: any) => { | |
| res.writeHead(statusCode, { "Content-Type": "application/json", ...corsHeaders }); | |
| res.end(JSON.stringify(data)); | |
| }; | |
| // Helper to send HTML | |
| const sendHTML = (html: string) => { | |
| res.writeHead(200, { "Content-Type": "text/html", ...corsHeaders }); | |
| res.end(html); | |
| }; | |
| // Masking Frontend | |
| if (url.pathname === "/" && method === "GET") { | |
| return sendHTML(HTML_FRONTEND); | |
| } | |
| // Handle Preflight | |
| if (method === "OPTIONS") { | |
| res.writeHead(204, corsHeaders); | |
| res.end(); | |
| return; | |
| } | |
| // Auth Check | |
| const authHeader = req.headers["authorization"]; | |
| if (url.pathname.startsWith("/v1/") && authHeader !== `Bearer ${API_KEY}`) { | |
| return sendJSON(401, { error: "Unauthorized" }); | |
| } | |
| // Health Check | |
| if (url.pathname === "/health") { | |
| return sendJSON(200, { status: "online", model: "distributed-v1" }); | |
| } | |
| // Chat Completions | |
| if (url.pathname === "/v1/chat/completions" && method === "POST") { | |
| let body = ""; | |
| req.on("data", (chunk) => { body += chunk; }); | |
| req.on("end", async () => { | |
| try { | |
| log(`INCOMING REQUEST (Size: ${body.length})`); | |
| const incomingVqd = req.headers["x-vqd-4"] as string | undefined; | |
| const jsonBody = JSON.parse(body); | |
| if (jsonBody.stream) { | |
| const { stream, vqd } = await openAIService.createChatCompletionStream(jsonBody, incomingVqd); | |
| res.writeHead(200, { | |
| "Content-Type": "text/event-stream", | |
| ...corsHeaders, | |
| "x-vqd-4": vqd || "", | |
| "X-Content-Type-Options": "nosniff" | |
| }); | |
| const reader = stream.getReader(); | |
| try { | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| res.write(value); | |
| } | |
| } finally { | |
| res.end(); | |
| reader.releaseLock(); | |
| } | |
| return; | |
| } | |
| const { completion, vqd } = await openAIService.createChatCompletion(jsonBody, incomingVqd); | |
| log(`SUCCESS: Sent response for ${jsonBody.model}`); | |
| res.writeHead(200, { | |
| "Content-Type": "application/json", | |
| ...corsHeaders, | |
| "x-vqd-4": vqd || "" | |
| }); | |
| res.write(JSON.stringify(completion)); | |
| res.end(); | |
| } catch (error: any) { | |
| log(`ERROR: ${error.message}`); | |
| res.writeHead(500, { "Content-Type": "application/json", ...corsHeaders }); | |
| res.write(JSON.stringify({ error: error.message, details: error.stack })); | |
| res.end(); | |
| } | |
| }); | |
| return; | |
| } | |
| // === DIAGNOSTIC ENDPOINT === | |
| if (url.pathname === "/v1/diagnose" && method === "GET") { | |
| log("RUNNING SYSTEM DIAGNOSTICS..."); | |
| const report: any = { | |
| status: "running", | |
| checks: {} | |
| }; | |
| // 1. Check Node.js Environment | |
| report.checks.node = { | |
| version: process.version, | |
| memory: process.memoryUsage(), | |
| uptime: process.uptime() | |
| }; | |
| // 2. Check Network & DuckDuckGo Connectivity | |
| try { | |
| const start = Date.now(); | |
| // Check IP | |
| const ipRes = await fetch("https://api.ipify.org?format=json"); | |
| const ipData = await ipRes.json() as any; | |
| const res = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { | |
| headers: { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36", | |
| "x-vqd-accept": "1" | |
| } | |
| }); const hash = res.headers.get("x-vqd-hash-1"); | |
| report.checks.duckduckgo = { | |
| status: res.status, | |
| latency_ms: Date.now() - start, | |
| vqd_present: !!hash, | |
| ip_blocked: res.status === 403 || res.status === 418, | |
| server_ip: ipData.ip | |
| }; | |
| } catch (e: any) { | |
| report.checks.duckduckgo = { error: e.message }; | |
| } | |
| // 3. Check JSDOM & Canvas Mock | |
| try { | |
| const { JSDOM } = require("jsdom"); | |
| const dom = new JSDOM("<!DOCTYPE html><canvas id='c'></canvas>"); | |
| const canvas = dom.window.document.getElementById('c'); | |
| const ctx = canvas.getContext('2d'); // Should use our patch logic if imported, but here testing raw lib | |
| report.checks.jsdom = { | |
| ok: true, | |
| canvas_created: !!canvas, | |
| context_2d: !!ctx | |
| }; | |
| } catch (e: any) { | |
| report.checks.jsdom = { error: e.message }; | |
| } | |
| sendJSON(200, report); | |
| return; | |
| } | |
| // Not Found | |
| res.writeHead(404, corsHeaders); res.end(); | |
| } catch (error: any) { | |
| log(`CRITICAL ERR: ${error.message}`); | |
| res.writeHead(500, { "Content-Type": "application/json" }); | |
| res.end(JSON.stringify({ error: "Internal Server Error" })); | |
| } | |
| }); | |
| // Prevent server from crashing | |
| process.on('uncaughtException', (err) => { | |
| log(`CRITICAL: Uncaught Exception: ${err.message}\n${err.stack}`); | |
| }); | |
| process.on('unhandledRejection', (reason, promise) => { | |
| log(`CRITICAL: Unhandled Rejection: ${reason}`); | |
| }); | |
| server.listen(PORT, () => { | |
| log(`API Gateway started on port ${PORT}`); | |
| }); | |