Spaces:
Paused
Paused
Download server.js from amri12310/sys-e-engine: direct link, hf CLI and curl.
- Browser
- Download file 8.08 kB
-
https://huggingface.co/spaces/amri12310/sys-e-engine/resolve/main/server.js
- Command line
-
hf download hf://spaces/amri12310/sys-e-engine/server.js
-
curl -L -o server.js https://huggingface.co/spaces/amri12310/sys-e-engine/resolve/main/server.js
8.08 kB
| const express = require('express'); | |
| const axios = require('axios'); | |
| const app = express(); | |
| const TOKEN = process.env.HF_TOKEN; | |
| const REPO_ID = "amri12310/hf-vault-99281-xk9-p02-mlq8-r4x-v9928-zb-v2-alpha-sync"; | |
| app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => { | |
| const { u_id } = req.params; | |
| const auth = TOKEN ? { "Authorization": `Bearer ${TOKEN}` } : {}; | |
| // ⚡ CRITICAL: The instant kill-switch | |
| let clientConnected = true; | |
| const controller = new AbortController(); | |
| req.on('close', () => { | |
| clientConnected = false; | |
| controller.abort(); // Instantly sever the Hugging Face download | |
| }); | |
| try { | |
| let meta = null; | |
| let isLegacy = false; | |
| let allLegacyChunks = []; | |
| let totalSize = 0; | |
| // 1. Try New Format (meta.json) | |
| try { | |
| const r_meta = await axios.get(`https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/meta.json`, { headers: auth, timeout: 5000 }); | |
| meta = r_meta.data; | |
| totalSize = meta.size; | |
| } catch (e) { | |
| // 2. Fallback to Legacy Format (meta_part1.json) | |
| for (let p of [1, 2, 3]) { | |
| try { | |
| const r_leg = await axios.get(`https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/meta_part${p}.json`, { headers: auth, timeout: 5000 }); | |
| if (r_leg.data) { | |
| totalSize = Math.max(totalSize, r_leg.data.total_size || 0); | |
| allLegacyChunks.push(...(r_leg.data.chunks || [])); | |
| } | |
| } catch (err) {} | |
| } | |
| if (allLegacyChunks.length > 0) { | |
| isLegacy = true; | |
| allLegacyChunks.sort((a, b) => a.start - b.start); | |
| } else { | |
| return res.status(404).send("Metadata not found."); | |
| } | |
| } | |
| if (req.method === 'HEAD') { | |
| return res.status(200).set({ "Content-Length": totalSize, "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes" }).end(); | |
| } | |
| const rangeHeader = req.headers.range; | |
| let reqStart = 0; | |
| let reqEnd = totalSize - 1; | |
| if (rangeHeader) { | |
| const parts = rangeHeader.replace(/bytes=/, "").split("-"); | |
| reqStart = parseInt(parts[0], 10); | |
| reqEnd = parts[1] ? parseInt(parts[1], 10) : totalSize - 1; | |
| } | |
| const bytesToSend = (reqEnd - reqStart) + 1; | |
| const responseHeaders = { | |
| "Content-Range": `bytes ${reqStart}-${reqEnd}/${totalSize}`, | |
| "Accept-Ranges": "bytes", | |
| "Content-Length": bytesToSend, | |
| "Content-Type": "video/x-matroska", | |
| "Connection": "keep-alive" | |
| }; | |
| if (req.query.dl === '1') responseHeaders["Content-Disposition"] = `attachment; filename="${u_id}.mkv"`; | |
| res.writeHead(rangeHeader ? 206 : 200, responseHeaders); | |
| let bytesSent = 0; | |
| const mbToSend = (bytesToSend / 1024 / 1024).toFixed(2); | |
| const shortId = u_id.substring(0, 15); | |
| console.log(`▶️ [STREAM START] ${shortId}... | Range: ${reqStart}-${reqEnd} (${mbToSend} MB)`); | |
| // ========================================== | |
| // ROUTE A: NEW TURBO FORMAT | |
| // ========================================== | |
| if (!isLegacy) { | |
| const numChunks = meta.chunks; | |
| const chunkSizeInBytes = (meta.chunk_size_mb || 500) * 1024 * 1024; | |
| const startChunkIdx = Math.floor(reqStart / chunkSizeInBytes); | |
| const firstOffset = reqStart % chunkSizeInBytes; | |
| for (let i = startChunkIdx; i < numChunks; i++) { | |
| if (!clientConnected || bytesSent >= bytesToSend) break; | |
| const chunkUrl = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${i}.bin`; | |
| const currentOffset = (i === startChunkIdx) ? firstOffset : 0; | |
| const remainingBytesNeeded = bytesToSend - bytesSent; | |
| const bytesAvailableInChunk = chunkSizeInBytes - currentOffset; | |
| const bytesToFetchFromChunk = Math.min(remainingBytesNeeded, bytesAvailableInChunk); | |
| const chunkReqEnd = currentOffset + bytesToFetchFromChunk - 1; | |
| try { | |
| const chunkStream = await axios({ | |
| method: 'get', | |
| url: chunkUrl, | |
| headers: { ...auth, "Range": `bytes=${currentOffset}-${chunkReqEnd}` }, | |
| responseType: 'stream', | |
| signal: controller.signal // ⚡ Kill switch attached | |
| }); | |
| await new Promise((resolve, reject) => { | |
| chunkStream.data.pipe(res, { end: false }); | |
| chunkStream.data.on('end', () => { bytesSent += bytesToFetchFromChunk; resolve(); }); | |
| chunkStream.data.on('error', reject); | |
| }); | |
| } catch (err) { | |
| if (clientConnected) { | |
| console.log(`⚠️ [NETWORK] Cloudflare dropped chunk ${i}. Retrying...`); | |
| i--; | |
| } | |
| } | |
| } | |
| } | |
| // ========================================== | |
| // ROUTE B: LEGACY SHARDED FORMAT | |
| // ========================================== | |
| else { | |
| let currentStart = reqStart; | |
| for (let chunk of allLegacyChunks) { | |
| if (!clientConnected || bytesSent >= bytesToSend) break; | |
| if (chunk.end <= currentStart) continue; | |
| while (currentStart < chunk.end && bytesSent < bytesToSend && clientConnected) { | |
| const url = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${chunk.name}`; | |
| const offsetInChunk = currentStart - chunk.start; | |
| const remainingBytesNeeded = bytesToSend - bytesSent; | |
| const bytesAvailableInChunk = chunk.end - currentStart; | |
| const bytesToFetch = Math.min(remainingBytesNeeded, bytesAvailableInChunk); | |
| const chunkReqEnd = offsetInChunk + bytesToFetch - 1; | |
| try { | |
| const chunkStream = await axios({ | |
| method: 'get', | |
| url: url, | |
| headers: { ...auth, "Range": `bytes=${offsetInChunk}-${chunkReqEnd}` }, | |
| responseType: 'stream', | |
| signal: controller.signal // ⚡ Kill switch attached | |
| }); | |
| await new Promise((resolve, reject) => { | |
| chunkStream.data.pipe(res, { end: false }); | |
| chunkStream.data.on('end', () => { | |
| bytesSent += bytesToFetch; | |
| currentStart += bytesToFetch; | |
| resolve(); | |
| }); | |
| chunkStream.data.on('error', reject); | |
| }); | |
| } catch (err) { | |
| if (clientConnected) console.log(`⚠️ [NETWORK] Legacy chunk dropped. Retrying...`); | |
| } | |
| } | |
| } | |
| } | |
| if (clientConnected) { | |
| console.log(`✅ [STREAM FINISHED] ${shortId}...`); | |
| res.end(); | |
| } else { | |
| console.log(`⏹️ [STREAM HALTED] TV closed connection early.`); | |
| } | |
| } catch (err) { | |
| if (!res.headersSent && clientConnected) res.status(500).send("Engine Malfunction"); | |
| } | |
| }); | |
| // ⚡ The Heartbeat Route | |
| app.get('/', (req, res) => res.status(200).send("Engine E is awake!")); | |
| app.listen(7860, () => console.log('🚀 Engine E (Node.js Egress) running on port 7860')); | |