amri12310 commited on
Commit
bdd1186
·
verified ·
1 Parent(s): 33579a4

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +145 -0
server.js ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const axios = require('axios');
3
+ const app = express();
4
+
5
+ const TOKEN = process.env.HF_TOKEN;
6
+ const REPO_ID = "amri12310/hf-vault-99281-xk9-p02-mlq8-r4x-v9928-zb-v2-alpha-sync";
7
+
8
+ 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;
15
+ let allLegacyChunks = [];
16
+ let totalSize = 0;
17
+
18
+ // 1. Try New Format (meta.json)
19
+ try {
20
+ const r_meta = await axios.get(`https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/meta.json`, { headers: auth, timeout: 5000 });
21
+ meta = r_meta.data;
22
+ totalSize = meta.size;
23
+ } catch (e) {
24
+ // 2. Fallback to Legacy Format (meta_part1.json)
25
+ for (let p of [1, 2, 3]) {
26
+ try {
27
+ const r_leg = await axios.get(`https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/meta_part${p}.json`, { headers: auth, timeout: 5000 });
28
+ if (r_leg.data) {
29
+ totalSize = Math.max(totalSize, r_leg.data.total_size || 0);
30
+ allLegacyChunks.push(...(r_leg.data.chunks || []));
31
+ }
32
+ } catch (err) {}
33
+ }
34
+ if (allLegacyChunks.length > 0) {
35
+ isLegacy = true;
36
+ allLegacyChunks.sort((a, b) => a.start - b.start);
37
+ } else {
38
+ return res.status(404).send("Metadata not found.");
39
+ }
40
+ }
41
+
42
+ // ⚡ EXOPLAYER FAST HEAD RESPONSE
43
+ if (req.method === 'HEAD') {
44
+ return res.status(200).set({ "Content-Length": totalSize, "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes" }).end();
45
+ }
46
+
47
+ // Range Parsing
48
+ const rangeHeader = req.headers.range;
49
+ let reqStart = 0;
50
+ let reqEnd = totalSize - 1;
51
+
52
+ if (rangeHeader) {
53
+ const parts = rangeHeader.replace(/bytes=/, "").split("-");
54
+ reqStart = parseInt(parts[0], 10);
55
+ reqEnd = parts[1] ? parseInt(parts[1], 10) : totalSize - 1;
56
+ }
57
+
58
+ const bytesToSend = (reqEnd - reqStart) + 1;
59
+ const responseHeaders = {
60
+ "Content-Range": `bytes ${reqStart}-${reqEnd}/${totalSize}`,
61
+ "Accept-Ranges": "bytes",
62
+ "Content-Length": bytesToSend,
63
+ "Content-Type": "video/x-matroska",
64
+ "Connection": "keep-alive"
65
+ };
66
+
67
+ if (req.query.dl === '1') responseHeaders["Content-Disposition"] = `attachment; filename="${u_id}.mkv"`;
68
+ res.writeHead(rangeHeader ? 206 : 200, responseHeaders);
69
+
70
+ let bytesSent = 0;
71
+
72
+ // ==========================================
73
+ // ROUTE A: NEW TURBO FORMAT
74
+ // ==========================================
75
+ if (!isLegacy) {
76
+ const numChunks = meta.chunks;
77
+ const chunkSizeInBytes = (meta.chunk_size_mb || 500) * 1024 * 1024;
78
+ const startChunkIdx = Math.floor(reqStart / chunkSizeInBytes);
79
+ const firstOffset = reqStart % chunkSizeInBytes;
80
+
81
+ for (let i = startChunkIdx; i < numChunks; i++) {
82
+ if (bytesSent >= bytesToSend) break;
83
+ const chunkUrl = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${i}.bin`;
84
+ const currentOffset = (i === startChunkIdx) ? firstOffset : 0;
85
+
86
+ const remainingBytesNeeded = bytesToSend - bytesSent;
87
+ const bytesAvailableInChunk = chunkSizeInBytes - currentOffset;
88
+ const bytesToFetchFromChunk = Math.min(remainingBytesNeeded, bytesAvailableInChunk);
89
+ const chunkReqEnd = currentOffset + bytesToFetchFromChunk - 1;
90
+
91
+ try {
92
+ const chunkStream = await axios({ method: 'get', url: chunkUrl, headers: { ...auth, "Range": `bytes=${currentOffset}-${chunkReqEnd}` }, responseType: 'stream' });
93
+ await new Promise((resolve, reject) => {
94
+ chunkStream.data.pipe(res, { end: false });
95
+ chunkStream.data.on('end', () => { bytesSent += bytesToFetchFromChunk; resolve(); });
96
+ chunkStream.data.on('error', reject);
97
+ });
98
+ } catch (err) {
99
+ console.log(`[NETWORK] Cloudflare dropped chunk ${i}. Retrying...`);
100
+ i--; // Retry exact chunk on drop
101
+ }
102
+ }
103
+ }
104
+ // ==========================================
105
+ // ROUTE B: LEGACY SHARDED FORMAT
106
+ // ==========================================
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
+
117
+ const remainingBytesNeeded = bytesToSend - bytesSent;
118
+ const bytesAvailableInChunk = chunk.end - currentStart;
119
+ const bytesToFetch = Math.min(remainingBytesNeeded, bytesAvailableInChunk);
120
+ const chunkReqEnd = offsetInChunk + bytesToFetch - 1;
121
+
122
+ try {
123
+ const chunkStream = await axios({ method: 'get', url: url, headers: { ...auth, "Range": `bytes=${offsetInChunk}-${chunkReqEnd}` }, responseType: 'stream' });
124
+ await new Promise((resolve, reject) => {
125
+ chunkStream.data.pipe(res, { end: false });
126
+ chunkStream.data.on('end', () => {
127
+ bytesSent += bytesToFetch;
128
+ currentStart += bytesToFetch;
129
+ resolve();
130
+ });
131
+ chunkStream.data.on('error', reject);
132
+ });
133
+ } catch (err) {
134
+ console.log(`[NETWORK] Legacy chunk dropped. Retrying...`);
135
+ }
136
+ }
137
+ }
138
+ }
139
+ res.end();
140
+ } catch (err) {
141
+ if (!res.headersSent) res.status(500).send("Engine Malfunction");
142
+ }
143
+ });
144
+
145
+ app.listen(7860, () => console.log('🚀 Engine E (Node.js Egress) running on port 7860'));