DUBLK 2000 Admin commited on
Commit
f09e7cd
·
1 Parent(s): f5342d9

Add optimized WebP poster hosting, payment idempotency, key separation

Browse files
Files changed (1) hide show
  1. server.js +52 -3
server.js CHANGED
@@ -105,6 +105,7 @@ const inflightFile = new Map();
105
  const hotMap = new Map();
106
  const inflightHot = new Map();
107
  const durCache = new Map();
 
108
  const inflightDur = new Map();
109
  const lvlCache = new Map();
110
  const rateMap = new Map();
@@ -425,6 +426,10 @@ function sweepMaps() {
425
  const ids = [...durCache.keys()].slice(0, durCache.size - 35000);
426
  for (const id of ids) durCache.delete(id);
427
  }
 
 
 
 
428
  if (rateMap.size > 20000) {
429
  const slot = Math.floor(now / 60000);
430
  for (const k of rateMap.keys()) {
@@ -1209,10 +1214,52 @@ function ticketLvl(ticket) {
1209
  return "anonymous";
1210
  }
1211
 
1212
- function previewMaxEnd(size, l) {
1213
  const pct = lvlPct(l);
1214
  if (!size || pct >= 100) return size - 1;
1215
- return Math.max(0, Math.floor(Number(size) * pct / 100) - 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1216
  }
1217
 
1218
  function mp4IndexOff(size, msgId) {
@@ -1247,7 +1294,9 @@ function applyPreview(rng, size, l, msgId, ticket) {
1247
  partial: rng.start > 0 || endCap < size - 1
1248
  };
1249
  }
1250
- const maxEnd = previewMaxEnd(size, l);
 
 
1251
  const tailAllowed = size >= TAIL_MIN_SIZE;
1252
  const tailWin = tailAllowed ? Math.max(0, size - Math.min(TAIL_MAX, Math.floor(size * 0.01))) : Infinity;
1253
  if (tailAllowed && rng.start >= tailWin) {
 
105
  const hotMap = new Map();
106
  const inflightHot = new Map();
107
  const durCache = new Map();
108
+ const mdatCache = new Map();
109
  const inflightDur = new Map();
110
  const lvlCache = new Map();
111
  const rateMap = new Map();
 
426
  const ids = [...durCache.keys()].slice(0, durCache.size - 35000);
427
  for (const id of ids) durCache.delete(id);
428
  }
429
+ if (mdatCache.size > 50000) {
430
+ const ids = [...mdatCache.keys()].slice(0, mdatCache.size - 35000);
431
+ for (const id of ids) mdatCache.delete(id);
432
+ }
433
  if (rateMap.size > 20000) {
434
  const slot = Math.floor(now / 60000);
435
  for (const k of rateMap.keys()) {
 
1214
  return "anonymous";
1215
  }
1216
 
1217
+ function previewMaxEnd(size, l, msgId, dur) {
1218
  const pct = lvlPct(l);
1219
  if (!size || pct >= 100) return size - 1;
1220
+ let base = Math.floor(Number(size) * pct / 100);
1221
+ const d = Number(dur);
1222
+ if (d > 1 && d < 86400) {
1223
+ const bitrate = Number(size) * 8 / d;
1224
+ if (bitrate > 16000 && bitrate < 40000000) {
1225
+ const ms0 = mdatStartOff(size, msgId);
1226
+ const span = Math.max(0, Number(size) - ms0);
1227
+ const tCut = ms0 + Math.floor(span * pct / 100);
1228
+ if (tCut > base) base = tCut;
1229
+ base += Math.min(Math.floor(base * 0.04), Math.floor(size * 0.04));
1230
+ }
1231
+ }
1232
+ const head = Math.min(48 * MB, Math.max(4 * MB, Math.floor(size * 0.06)));
1233
+ return Math.max(0, Math.min(size - 1, base + head - 1));
1234
+ }
1235
+
1236
+ function mdatStartOff(size, msgId) {
1237
+ const id = String(msgId || "");
1238
+ const cached = mdatCache.get(id);
1239
+ if (cached !== undefined) return cached;
1240
+ const h = hotMap.get(id);
1241
+ const buf = h && h.headOff === 0 ? h.head : null;
1242
+ let out = 0;
1243
+ if (buf && buf.length >= 16 && size) {
1244
+ let i = 0;
1245
+ for (let n = 0; i + 8 <= buf.length && n < 16; n++) {
1246
+ let sz = buf.readUInt32BE(i);
1247
+ const typ = buf.toString("ascii", i + 4, i + 8);
1248
+ let big = false;
1249
+ if (sz === 1) {
1250
+ if (i + 16 > buf.length) break;
1251
+ sz = Number(buf.readBigUInt64BE(i + 8));
1252
+ big = true;
1253
+ }
1254
+ if (!sz || sz < 8) break;
1255
+ if (typ === "mdat") { out = i + (big ? 16 : 8); break; }
1256
+ if (typ === "moov") { out = i + sz; break; }
1257
+ if (i + sz > size) break;
1258
+ i += sz;
1259
+ }
1260
+ }
1261
+ mdatCache.set(id, out);
1262
+ return out;
1263
  }
1264
 
1265
  function mp4IndexOff(size, msgId) {
 
1294
  partial: rng.start > 0 || endCap < size - 1
1295
  };
1296
  }
1297
+ const td = Number(ticket && ticket.d);
1298
+ const dur = td > 1 ? td : (durCache.get(String(msgId || "")) || 0);
1299
+ const maxEnd = previewMaxEnd(size, l, msgId, dur);
1300
  const tailAllowed = size >= TAIL_MIN_SIZE;
1301
  const tailWin = tailAllowed ? Math.max(0, size - Math.min(TAIL_MAX, Math.floor(size * 0.01))) : Infinity;
1302
  if (tailAllowed && rng.start >= tailWin) {