Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -9,9 +9,14 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 9 |
const { u_id } = req.params;
|
| 10 |
const auth = TOKEN ? { "Authorization": `Bearer ${TOKEN}` } : {};
|
| 11 |
|
| 12 |
-
// ⚡ CRITICAL:
|
| 13 |
let clientConnected = true;
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
try {
|
| 17 |
let meta = null;
|
|
@@ -43,12 +48,10 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
-
// ⚡ EXOPLAYER FAST HEAD RESPONSE
|
| 47 |
if (req.method === 'HEAD') {
|
| 48 |
return res.status(200).set({ "Content-Length": totalSize, "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes" }).end();
|
| 49 |
}
|
| 50 |
|
| 51 |
-
// Range Parsing
|
| 52 |
const rangeHeader = req.headers.range;
|
| 53 |
let reqStart = 0;
|
| 54 |
let reqEnd = totalSize - 1;
|
|
@@ -75,7 +78,6 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 75 |
const mbToSend = (bytesToSend / 1024 / 1024).toFixed(2);
|
| 76 |
const shortId = u_id.substring(0, 15);
|
| 77 |
|
| 78 |
-
// ⚡ MINIMAL LOGGING: Know exactly what the TV is asking for
|
| 79 |
console.log(`▶️ [STREAM START] ${shortId}... | Range: ${reqStart}-${reqEnd} (${mbToSend} MB)`);
|
| 80 |
|
| 81 |
// ==========================================
|
|
@@ -88,7 +90,6 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 88 |
const firstOffset = reqStart % chunkSizeInBytes;
|
| 89 |
|
| 90 |
for (let i = startChunkIdx; i < numChunks; i++) {
|
| 91 |
-
// ⚡ Break instantly if TV stopped watching
|
| 92 |
if (!clientConnected || bytesSent >= bytesToSend) break;
|
| 93 |
|
| 94 |
const chunkUrl = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${i}.bin`;
|
|
@@ -100,7 +101,14 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 100 |
const chunkReqEnd = currentOffset + bytesToFetchFromChunk - 1;
|
| 101 |
|
| 102 |
try {
|
| 103 |
-
const chunkStream = await axios({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
await new Promise((resolve, reject) => {
|
| 105 |
chunkStream.data.pipe(res, { end: false });
|
| 106 |
chunkStream.data.on('end', () => { bytesSent += bytesToFetchFromChunk; resolve(); });
|
|
@@ -109,7 +117,7 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 109 |
} catch (err) {
|
| 110 |
if (clientConnected) {
|
| 111 |
console.log(`⚠️ [NETWORK] Cloudflare dropped chunk ${i}. Retrying...`);
|
| 112 |
-
i--;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
}
|
|
@@ -133,7 +141,14 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 133 |
const chunkReqEnd = offsetInChunk + bytesToFetch - 1;
|
| 134 |
|
| 135 |
try {
|
| 136 |
-
const chunkStream = await axios({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
await new Promise((resolve, reject) => {
|
| 138 |
chunkStream.data.pipe(res, { end: false });
|
| 139 |
chunkStream.data.on('end', () => {
|
|
@@ -150,7 +165,6 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
-
// ⚡ MINIMAL LOGGING: Know how the stream ended
|
| 154 |
if (clientConnected) {
|
| 155 |
console.log(`✅ [STREAM FINISHED] ${shortId}...`);
|
| 156 |
res.end();
|
|
@@ -159,7 +173,7 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 159 |
}
|
| 160 |
|
| 161 |
} catch (err) {
|
| 162 |
-
if (!res.headersSent) res.status(500).send("Engine Malfunction");
|
| 163 |
}
|
| 164 |
});
|
| 165 |
|
|
|
|
| 9 |
const { u_id } = req.params;
|
| 10 |
const auth = TOKEN ? { "Authorization": `Bearer ${TOKEN}` } : {};
|
| 11 |
|
| 12 |
+
// ⚡ CRITICAL: The instant kill-switch
|
| 13 |
let clientConnected = true;
|
| 14 |
+
const controller = new AbortController();
|
| 15 |
+
|
| 16 |
+
req.on('close', () => {
|
| 17 |
+
clientConnected = false;
|
| 18 |
+
controller.abort(); // Instantly sever the Hugging Face download
|
| 19 |
+
});
|
| 20 |
|
| 21 |
try {
|
| 22 |
let meta = null;
|
|
|
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
if (req.method === 'HEAD') {
|
| 52 |
return res.status(200).set({ "Content-Length": totalSize, "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes" }).end();
|
| 53 |
}
|
| 54 |
|
|
|
|
| 55 |
const rangeHeader = req.headers.range;
|
| 56 |
let reqStart = 0;
|
| 57 |
let reqEnd = totalSize - 1;
|
|
|
|
| 78 |
const mbToSend = (bytesToSend / 1024 / 1024).toFixed(2);
|
| 79 |
const shortId = u_id.substring(0, 15);
|
| 80 |
|
|
|
|
| 81 |
console.log(`▶️ [STREAM START] ${shortId}... | Range: ${reqStart}-${reqEnd} (${mbToSend} MB)`);
|
| 82 |
|
| 83 |
// ==========================================
|
|
|
|
| 90 |
const firstOffset = reqStart % chunkSizeInBytes;
|
| 91 |
|
| 92 |
for (let i = startChunkIdx; i < numChunks; i++) {
|
|
|
|
| 93 |
if (!clientConnected || bytesSent >= bytesToSend) break;
|
| 94 |
|
| 95 |
const chunkUrl = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${i}.bin`;
|
|
|
|
| 101 |
const chunkReqEnd = currentOffset + bytesToFetchFromChunk - 1;
|
| 102 |
|
| 103 |
try {
|
| 104 |
+
const chunkStream = await axios({
|
| 105 |
+
method: 'get',
|
| 106 |
+
url: chunkUrl,
|
| 107 |
+
headers: { ...auth, "Range": `bytes=${currentOffset}-${chunkReqEnd}` },
|
| 108 |
+
responseType: 'stream',
|
| 109 |
+
signal: controller.signal // ⚡ Kill switch attached
|
| 110 |
+
});
|
| 111 |
+
|
| 112 |
await new Promise((resolve, reject) => {
|
| 113 |
chunkStream.data.pipe(res, { end: false });
|
| 114 |
chunkStream.data.on('end', () => { bytesSent += bytesToFetchFromChunk; resolve(); });
|
|
|
|
| 117 |
} catch (err) {
|
| 118 |
if (clientConnected) {
|
| 119 |
console.log(`⚠️ [NETWORK] Cloudflare dropped chunk ${i}. Retrying...`);
|
| 120 |
+
i--;
|
| 121 |
}
|
| 122 |
}
|
| 123 |
}
|
|
|
|
| 141 |
const chunkReqEnd = offsetInChunk + bytesToFetch - 1;
|
| 142 |
|
| 143 |
try {
|
| 144 |
+
const chunkStream = await axios({
|
| 145 |
+
method: 'get',
|
| 146 |
+
url: url,
|
| 147 |
+
headers: { ...auth, "Range": `bytes=${offsetInChunk}-${chunkReqEnd}` },
|
| 148 |
+
responseType: 'stream',
|
| 149 |
+
signal: controller.signal // ⚡ Kill switch attached
|
| 150 |
+
});
|
| 151 |
+
|
| 152 |
await new Promise((resolve, reject) => {
|
| 153 |
chunkStream.data.pipe(res, { end: false });
|
| 154 |
chunkStream.data.on('end', () => {
|
|
|
|
| 165 |
}
|
| 166 |
}
|
| 167 |
|
|
|
|
| 168 |
if (clientConnected) {
|
| 169 |
console.log(`✅ [STREAM FINISHED] ${shortId}...`);
|
| 170 |
res.end();
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
} catch (err) {
|
| 176 |
+
if (!res.headersSent && clientConnected) res.status(500).send("Engine Malfunction");
|
| 177 |
}
|
| 178 |
});
|
| 179 |
|