Spaces:
Running
Running
JOHN SILLVESTOR commited on
fix preview load and VIP ticket level
Browse files
server.js
CHANGED
|
@@ -150,7 +150,7 @@ function cors(req, res) {
|
|
| 150 |
function json(res, code, body) {
|
| 151 |
if (res.headersSent || res.writableEnded) return;
|
| 152 |
const s = JSON.stringify(body);
|
| 153 |
-
res.setHeader("Cross-Origin-Resource-Policy", "
|
| 154 |
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
|
| 155 |
res.end(s);
|
| 156 |
}
|
|
@@ -158,7 +158,9 @@ function json(res, code, body) {
|
|
| 158 |
function destOk(req, allow) {
|
| 159 |
const d = String(req.headers["sec-fetch-dest"] || "").toLowerCase();
|
| 160 |
if (!d) return true;
|
| 161 |
-
|
|
|
|
|
|
|
| 162 |
}
|
| 163 |
|
| 164 |
function streamDestOk(req) {
|
|
@@ -510,7 +512,8 @@ async function accessLevel(uid) {
|
|
| 510 |
let lvl = "registered";
|
| 511 |
let got = false;
|
| 512 |
try {
|
| 513 |
-
|
|
|
|
| 514 |
if (sub === LVL_MISS) return "registered";
|
| 515 |
got = true;
|
| 516 |
if (vipActive(sub)) lvl = "subscribed";
|
|
@@ -798,7 +801,7 @@ async function fillDuration(bot, rec, msgId) {
|
|
| 798 |
}
|
| 799 |
|
| 800 |
const LVL_PCT = { anonymous: 30, registered: 60, subscribed: 100 };
|
| 801 |
-
const INDEX_TAIL =
|
| 802 |
|
| 803 |
function lvlPct(l) {
|
| 804 |
const p = LVL_PCT[String(l || "")];
|
|
@@ -819,6 +822,15 @@ function previewMaxEnd(size, l) {
|
|
| 819 |
return Math.min(size - 1, n - 1);
|
| 820 |
}
|
| 821 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
function applyPreview(rng, size, l) {
|
| 823 |
if (!rng || !size) return rng;
|
| 824 |
if (lvlPct(l) >= 100) {
|
|
@@ -830,10 +842,12 @@ function applyPreview(rng, size, l) {
|
|
| 830 |
};
|
| 831 |
}
|
| 832 |
const maxEnd = previewMaxEnd(size, l);
|
| 833 |
-
const tail =
|
| 834 |
let tailStart = size;
|
| 835 |
-
|
| 836 |
-
|
|
|
|
|
|
|
| 837 |
if (rng.start <= maxEnd) {
|
| 838 |
const end = Math.min(rng.end, maxEnd);
|
| 839 |
return {
|
|
@@ -1159,7 +1173,7 @@ async function handleGenerate(req, res, url) {
|
|
| 1159 |
const rawAuth = String(req.headers.authorization || "").trim();
|
| 1160 |
if (rawAuth) {
|
| 1161 |
try {
|
| 1162 |
-
const idp = await withTime(verifyIdToken(rawAuth),
|
| 1163 |
if (idp) {
|
| 1164 |
uid = idp.sub;
|
| 1165 |
lvl = await accessLevel(uid);
|
|
|
|
| 150 |
function json(res, code, body) {
|
| 151 |
if (res.headersSent || res.writableEnded) return;
|
| 152 |
const s = JSON.stringify(body);
|
| 153 |
+
res.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
|
| 154 |
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
|
| 155 |
res.end(s);
|
| 156 |
}
|
|
|
|
| 158 |
function destOk(req, allow) {
|
| 159 |
const d = String(req.headers["sec-fetch-dest"] || "").toLowerCase();
|
| 160 |
if (!d) return true;
|
| 161 |
+
if (allow.indexOf(d) >= 0) return true;
|
| 162 |
+
if (d === "json" && allow.indexOf("empty") >= 0) return true;
|
| 163 |
+
return false;
|
| 164 |
}
|
| 165 |
|
| 166 |
function streamDestOk(req) {
|
|
|
|
| 512 |
let lvl = "registered";
|
| 513 |
let got = false;
|
| 514 |
try {
|
| 515 |
+
let sub = await withTime(rtdb("users/" + uid + "/subscription", "GET"), 5000, LVL_MISS);
|
| 516 |
+
if (sub === LVL_MISS) sub = await withTime(rtdb("users/" + uid + "/subscription", "GET"), 4000, LVL_MISS);
|
| 517 |
if (sub === LVL_MISS) return "registered";
|
| 518 |
got = true;
|
| 519 |
if (vipActive(sub)) lvl = "subscribed";
|
|
|
|
| 801 |
}
|
| 802 |
|
| 803 |
const LVL_PCT = { anonymous: 30, registered: 60, subscribed: 100 };
|
| 804 |
+
const INDEX_TAIL = 2 * 1024 * 1024;
|
| 805 |
|
| 806 |
function lvlPct(l) {
|
| 807 |
const p = LVL_PCT[String(l || "")];
|
|
|
|
| 822 |
return Math.min(size - 1, n - 1);
|
| 823 |
}
|
| 824 |
|
| 825 |
+
function previewTailLen(size, maxEnd) {
|
| 826 |
+
const unpaid = size - (maxEnd + 1);
|
| 827 |
+
if (unpaid <= 0) return 0;
|
| 828 |
+
const want = Math.min(INDEX_TAIL, Math.max(512 * 1024, Math.floor(size * 0.02)));
|
| 829 |
+
const minGap = Math.min(Math.floor(size * 0.2), Math.floor(unpaid * 0.55));
|
| 830 |
+
if (unpaid <= minGap + 65536) return Math.min(unpaid, Math.max(262144, want));
|
| 831 |
+
return Math.min(want, unpaid - minGap);
|
| 832 |
+
}
|
| 833 |
+
|
| 834 |
function applyPreview(rng, size, l) {
|
| 835 |
if (!rng || !size) return rng;
|
| 836 |
if (lvlPct(l) >= 100) {
|
|
|
|
| 842 |
};
|
| 843 |
}
|
| 844 |
const maxEnd = previewMaxEnd(size, l);
|
| 845 |
+
const tail = previewTailLen(size, maxEnd);
|
| 846 |
let tailStart = size;
|
| 847 |
+
if (tail > 0) {
|
| 848 |
+
const ts = size - tail;
|
| 849 |
+
if (ts > maxEnd + 1) tailStart = ts;
|
| 850 |
+
}
|
| 851 |
if (rng.start <= maxEnd) {
|
| 852 |
const end = Math.min(rng.end, maxEnd);
|
| 853 |
return {
|
|
|
|
| 1173 |
const rawAuth = String(req.headers.authorization || "").trim();
|
| 1174 |
if (rawAuth) {
|
| 1175 |
try {
|
| 1176 |
+
const idp = await withTime(verifyIdToken(rawAuth), 7000, null);
|
| 1177 |
if (idp) {
|
| 1178 |
uid = idp.sub;
|
| 1179 |
lvl = await accessLevel(uid);
|