DUBLK 2000 Admin commited on
Commit
07d9437
·
1 Parent(s): 0e3b79b

Add optimized WebP poster hosting, payment idempotency, key separation

Browse files
Files changed (1) hide show
  1. server.js +18 -5
server.js CHANGED
@@ -1258,7 +1258,7 @@ function mdatStartOff(size, msgId) {
1258
  i += sz;
1259
  }
1260
  }
1261
- mdatCache.set(id, out);
1262
  return out;
1263
  }
1264
 
@@ -1328,9 +1328,18 @@ function denyRange(res, size) {
1328
  return res.end();
1329
  }
1330
 
1331
- function parseRange(h, size) {
1332
  if (!size) return { start: 0, end: 0, partial: false };
1333
- const firstEnd = Math.min(size - 1, PROBE_HEAD - 1);
 
 
 
 
 
 
 
 
 
1334
  if (!h) return { start: 0, end: firstEnd, partial: firstEnd < size - 1 };
1335
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
1336
  if (!m) return { start: 0, end: firstEnd, partial: firstEnd < size - 1 };
@@ -2381,7 +2390,7 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2381
  const typeHint = (mimeHint.indexOf("webm") >= 0) ? "video/webm" : "video/mp4";
2382
  const lvl = ticketLvl(ticket);
2383
  if (sizeHint > 0) {
2384
- const rngHot0 = parseRange(req.headers.range, sizeHint);
2385
  const rngHot = applyPreview(rngHot0, sizeHint, lvl, msgId, ticket);
2386
  if (!rngHot) return denyRange(res, sizeHint);
2387
  const needHot = rngHot.end - rngHot.start + 1;
@@ -2449,7 +2458,11 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2449
  return res.end();
2450
  }
2451
  const size = rec.size;
2452
- const rng0 = parseRange(req.headers.range, size);
 
 
 
 
2453
  const rng = applyPreview(rng0, size, lvl, msgId, ticket);
2454
  if (!rng) return denyRange(res, size);
2455
  const headers = mediaHeaders(typeHint, msgId, size, rec.hash, rng);
 
1258
  i += sz;
1259
  }
1260
  }
1261
+ if (out > 0) mdatCache.set(id, out);
1262
  return out;
1263
  }
1264
 
 
1328
  return res.end();
1329
  }
1330
 
1331
+ function parseRange(h, size, msgId) {
1332
  if (!size) return { start: 0, end: 0, partial: false };
1333
+ let firstEnd = Math.min(size - 1, PROBE_HEAD - 1);
1334
+ if (msgId && size > PROBE_HEAD) {
1335
+ try {
1336
+ const h = hotMap.get(String(msgId));
1337
+ if (h && h.headOff === 0 && h.head && h.head.length >= 16) {
1338
+ const mo = mdatStartOff(size, msgId);
1339
+ if (mo > firstEnd + 1 && mo < size) firstEnd = Math.min(size - 1, mo + CHUNK_SIZE - 1);
1340
+ }
1341
+ } catch (e) {}
1342
+ }
1343
  if (!h) return { start: 0, end: firstEnd, partial: firstEnd < size - 1 };
1344
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
1345
  if (!m) return { start: 0, end: firstEnd, partial: firstEnd < size - 1 };
 
2390
  const typeHint = (mimeHint.indexOf("webm") >= 0) ? "video/webm" : "video/mp4";
2391
  const lvl = ticketLvl(ticket);
2392
  if (sizeHint > 0) {
2393
+ const rngHot0 = parseRange(req.headers.range, sizeHint, msgId);
2394
  const rngHot = applyPreview(rngHot0, sizeHint, lvl, msgId, ticket);
2395
  if (!rngHot) return denyRange(res, sizeHint);
2396
  const needHot = rngHot.end - rngHot.start + 1;
 
2458
  return res.end();
2459
  }
2460
  const size = rec.size;
2461
+ if (size > PROBE_HEAD) {
2462
+ warmHot(bot, rec, msgId);
2463
+ await waitHotHead(msgId, 64, 800);
2464
+ }
2465
+ const rng0 = parseRange(req.headers.range, size, msgId);
2466
  const rng = applyPreview(rng0, size, lvl, msgId, ticket);
2467
  if (!rng) return denyRange(res, size);
2468
  const headers = mediaHeaders(typeHint, msgId, size, rec.hash, rng);