Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -9,6 +9,10 @@ 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 |
try {
|
| 13 |
let meta = null;
|
| 14 |
let isLegacy = false;
|
|
@@ -68,6 +72,11 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 68 |
res.writeHead(rangeHeader ? 206 : 200, responseHeaders);
|
| 69 |
|
| 70 |
let bytesSent = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
// ==========================================
|
| 73 |
// ROUTE A: NEW TURBO FORMAT
|
|
@@ -79,7 +88,9 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 79 |
const firstOffset = reqStart % chunkSizeInBytes;
|
| 80 |
|
| 81 |
for (let i = startChunkIdx; i < numChunks; i++) {
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
const chunkUrl = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${i}.bin`;
|
| 84 |
const currentOffset = (i === startChunkIdx) ? firstOffset : 0;
|
| 85 |
|
|
@@ -96,8 +107,10 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 96 |
chunkStream.data.on('error', reject);
|
| 97 |
});
|
| 98 |
} catch (err) {
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
}
|
| 102 |
}
|
| 103 |
}
|
|
@@ -107,10 +120,10 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 107 |
else {
|
| 108 |
let currentStart = reqStart;
|
| 109 |
for (let chunk of allLegacyChunks) {
|
| 110 |
-
if (bytesSent >= bytesToSend) break;
|
| 111 |
if (chunk.end <= currentStart) continue;
|
| 112 |
|
| 113 |
-
while (currentStart < chunk.end && bytesSent < bytesToSend) {
|
| 114 |
const url = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${chunk.name}`;
|
| 115 |
const offsetInChunk = currentStart - chunk.start;
|
| 116 |
|
|
@@ -131,16 +144,25 @@ app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
|
|
| 131 |
chunkStream.data.on('error', reject);
|
| 132 |
});
|
| 133 |
} catch (err) {
|
| 134 |
-
console.log(`[NETWORK] Legacy chunk dropped. Retrying...`);
|
| 135 |
}
|
| 136 |
}
|
| 137 |
}
|
| 138 |
}
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
} catch (err) {
|
| 141 |
if (!res.headersSent) res.status(500).send("Engine Malfunction");
|
| 142 |
}
|
| 143 |
});
|
|
|
|
| 144 |
// ⚡ The Heartbeat Route
|
| 145 |
app.get('/', (req, res) => res.status(200).send("Engine E is awake!"));
|
| 146 |
|
|
|
|
| 9 |
const { u_id } = req.params;
|
| 10 |
const auth = TOKEN ? { "Authorization": `Bearer ${TOKEN}` } : {};
|
| 11 |
|
| 12 |
+
// ⚡ CRITICAL: Track if the TV is actually still watching
|
| 13 |
+
let clientConnected = true;
|
| 14 |
+
req.on('close', () => { clientConnected = false; });
|
| 15 |
+
|
| 16 |
try {
|
| 17 |
let meta = null;
|
| 18 |
let isLegacy = false;
|
|
|
|
| 72 |
res.writeHead(rangeHeader ? 206 : 200, responseHeaders);
|
| 73 |
|
| 74 |
let bytesSent = 0;
|
| 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 |
// ==========================================
|
| 82 |
// ROUTE A: NEW TURBO FORMAT
|
|
|
|
| 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`;
|
| 95 |
const currentOffset = (i === startChunkIdx) ? firstOffset : 0;
|
| 96 |
|
|
|
|
| 107 |
chunkStream.data.on('error', reject);
|
| 108 |
});
|
| 109 |
} catch (err) {
|
| 110 |
+
if (clientConnected) {
|
| 111 |
+
console.log(`⚠️ [NETWORK] Cloudflare dropped chunk ${i}. Retrying...`);
|
| 112 |
+
i--; // Retry exact chunk on drop
|
| 113 |
+
}
|
| 114 |
}
|
| 115 |
}
|
| 116 |
}
|
|
|
|
| 120 |
else {
|
| 121 |
let currentStart = reqStart;
|
| 122 |
for (let chunk of allLegacyChunks) {
|
| 123 |
+
if (!clientConnected || bytesSent >= bytesToSend) break;
|
| 124 |
if (chunk.end <= currentStart) continue;
|
| 125 |
|
| 126 |
+
while (currentStart < chunk.end && bytesSent < bytesToSend && clientConnected) {
|
| 127 |
const url = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${chunk.name}`;
|
| 128 |
const offsetInChunk = currentStart - chunk.start;
|
| 129 |
|
|
|
|
| 144 |
chunkStream.data.on('error', reject);
|
| 145 |
});
|
| 146 |
} catch (err) {
|
| 147 |
+
if (clientConnected) console.log(`⚠️ [NETWORK] Legacy chunk dropped. Retrying...`);
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
}
|
| 152 |
+
|
| 153 |
+
// ⚡ MINIMAL LOGGING: Know how the stream ended
|
| 154 |
+
if (clientConnected) {
|
| 155 |
+
console.log(`✅ [STREAM FINISHED] ${shortId}...`);
|
| 156 |
+
res.end();
|
| 157 |
+
} else {
|
| 158 |
+
console.log(`⏹️ [STREAM HALTED] TV closed connection early.`);
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
} catch (err) {
|
| 162 |
if (!res.headersSent) res.status(500).send("Engine Malfunction");
|
| 163 |
}
|
| 164 |
});
|
| 165 |
+
|
| 166 |
// ⚡ The Heartbeat Route
|
| 167 |
app.get('/', (req, res) => res.status(200).send("Engine E is awake!"));
|
| 168 |
|