Spaces:
Running
Running
JOHN SILLVESTOR commited on
Start playback with one small piece instead of a large warm download
Browse files
server.js
CHANGED
|
@@ -1413,7 +1413,8 @@ function waitHotHead(msgId, min, ms) {
|
|
| 1413 |
async function warmHot(bot, rec, msgId) {
|
| 1414 |
const id = String(msgId);
|
| 1415 |
const hit = hotMap.get(id);
|
| 1416 |
-
|
|
|
|
| 1417 |
if (inflightHot.has(id)) return inflightHot.get(id);
|
| 1418 |
let finish;
|
| 1419 |
const p = new Promise((r) => { finish = r; });
|
|
@@ -1421,7 +1422,6 @@ async function warmHot(bot, rec, msgId) {
|
|
| 1421 |
(async () => {
|
| 1422 |
bot.load++;
|
| 1423 |
try {
|
| 1424 |
-
const headNeed = Math.min(rec.size, PROBE_HEAD);
|
| 1425 |
const headEnd = headNeed - 1;
|
| 1426 |
if (headEnd >= 0 && !(hit && hit.head && hit.head.length >= headNeed)) {
|
| 1427 |
const parts = [];
|
|
@@ -1430,20 +1430,6 @@ async function warmHot(bot, rec, msgId) {
|
|
| 1430 |
}
|
| 1431 |
if (parts.length) hotFill(id, 0, Buffer.concat(parts), rec.size);
|
| 1432 |
}
|
| 1433 |
-
if (rec.size > PROBE_HEAD && !durCache.get(id) && !(hit && hit.tail)) {
|
| 1434 |
-
const tailNeed = Math.min(HOT_TAIL, 2 * MB);
|
| 1435 |
-
const tailOff = Math.max(0, rec.size - tailNeed);
|
| 1436 |
-
const parts = [];
|
| 1437 |
-
for await (const c of rangeChunks(bot, rec, tailOff, rec.size - 1, () => false, id)) {
|
| 1438 |
-
parts.push(c);
|
| 1439 |
-
}
|
| 1440 |
-
if (parts.length) {
|
| 1441 |
-
const buf = Buffer.concat(parts);
|
| 1442 |
-
hotFill(id, tailOff, buf, rec.size);
|
| 1443 |
-
const d = durationFromMp4(buf);
|
| 1444 |
-
if (d > 1) durCache.set(id, d);
|
| 1445 |
-
}
|
| 1446 |
-
}
|
| 1447 |
} catch {
|
| 1448 |
} finally {
|
| 1449 |
bot.load = Math.max(0, bot.load - 1);
|
|
@@ -1451,9 +1437,31 @@ async function warmHot(bot, rec, msgId) {
|
|
| 1451 |
finish();
|
| 1452 |
}
|
| 1453 |
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1454 |
return p;
|
| 1455 |
}
|
| 1456 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1457 |
class SingleFlight {
|
| 1458 |
constructor() {
|
| 1459 |
this.active = new Map();
|
|
@@ -2472,6 +2480,8 @@ function payUrls(ret) {
|
|
| 2472 |
let walletReady = false;
|
| 2473 |
let walletNext = 0;
|
| 2474 |
let walletJob = null;
|
|
|
|
|
|
|
| 2475 |
|
| 2476 |
function payPref(node) {
|
| 2477 |
const d = node && node.display_preference;
|
|
@@ -2480,7 +2490,9 @@ function payPref(node) {
|
|
| 2480 |
}
|
| 2481 |
|
| 2482 |
async function ensureFreeWallets() {
|
| 2483 |
-
if (
|
|
|
|
|
|
|
| 2484 |
if (walletJob) return walletJob;
|
| 2485 |
walletJob = (async () => {
|
| 2486 |
const list = await stripeGet("payment_method_configurations?limit=10");
|
|
@@ -2488,9 +2500,17 @@ async function ensureFreeWallets() {
|
|
| 2488 |
const cfg = rows.find((x) => x && x.is_default && x.active) || rows[0];
|
| 2489 |
if (!cfg || !/^pmc_/.test(String(cfg.id || ""))) {
|
| 2490 |
walletNext = Date.now() + 600000;
|
| 2491 |
-
return;
|
| 2492 |
-
}
|
| 2493 |
-
const want = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2494 |
let failed = false;
|
| 2495 |
for (const key of Object.keys(want)) {
|
| 2496 |
if (!cfg[key]) continue;
|
|
@@ -2500,10 +2520,27 @@ async function ensureFreeWallets() {
|
|
| 2500 |
});
|
| 2501 |
if (!saved) failed = true;
|
| 2502 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2503 |
if (failed) walletNext = Date.now() + 600000;
|
| 2504 |
-
else walletReady = true;
|
|
|
|
|
|
|
| 2505 |
})().catch(() => {
|
| 2506 |
walletNext = Date.now() + 600000;
|
|
|
|
| 2507 |
}).finally(() => {
|
| 2508 |
walletJob = null;
|
| 2509 |
});
|
|
@@ -2550,6 +2587,11 @@ async function openPay(uid, planKey, email, auto) {
|
|
| 2550 |
const ses = await stripeSession(rec.sid);
|
| 2551 |
const meta = ses && ses.metadata || {};
|
| 2552 |
if (!ses || !ses.url || ses.status !== "open" || ses.payment_status === "paid") return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2553 |
if (meta.uid !== uid || meta.plan !== planKey || String(meta.email || "") !== email) return null;
|
| 2554 |
if ((meta.auto === "1") !== !!auto) return null;
|
| 2555 |
return { sid: ses.id, url: ses.url };
|
|
@@ -2575,7 +2617,7 @@ async function handleVipStart(req, res) {
|
|
| 2575 |
if (!plan || !email) return json(res, 400, { error: "plan" });
|
| 2576 |
const auto = planKey === "month" && body.auto === true;
|
| 2577 |
const urls = payUrls(body.returnUrl);
|
| 2578 |
-
await ensureFreeWallets();
|
| 2579 |
const reused = await openPay(idp.sub, planKey, email, auto);
|
| 2580 |
if (reused) return json(res, 200, reused);
|
| 2581 |
const nonce = crypto.randomBytes(8).toString("hex");
|
|
@@ -2583,7 +2625,6 @@ async function handleVipStart(req, res) {
|
|
| 2583 |
const homeMsg = "After pay you return to [DUB LK 2000 Home](" + HOME_URL + ")";
|
| 2584 |
const fields = {
|
| 2585 |
mode: "payment",
|
| 2586 |
-
"payment_method_types[0]": "card",
|
| 2587 |
success_url: urls.success,
|
| 2588 |
cancel_url: urls.cancel,
|
| 2589 |
client_reference_id: idp.sub,
|
|
@@ -2623,6 +2664,8 @@ async function handleVipStart(req, res) {
|
|
| 2623 |
delete fields["payment_intent_data[metadata][email]"];
|
| 2624 |
delete fields["payment_intent_data[receipt_email]"];
|
| 2625 |
}
|
|
|
|
|
|
|
| 2626 |
async function createSes(body) {
|
| 2627 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2628 |
method: "POST",
|
|
|
|
| 1413 |
async function warmHot(bot, rec, msgId) {
|
| 1414 |
const id = String(msgId);
|
| 1415 |
const hit = hotMap.get(id);
|
| 1416 |
+
const headNeed = Math.min(rec.size, CHUNK_SIZE);
|
| 1417 |
+
if (hit && hit.head && hit.head.length >= headNeed && (durCache.get(id) > 1 || hit.tail)) return;
|
| 1418 |
if (inflightHot.has(id)) return inflightHot.get(id);
|
| 1419 |
let finish;
|
| 1420 |
const p = new Promise((r) => { finish = r; });
|
|
|
|
| 1422 |
(async () => {
|
| 1423 |
bot.load++;
|
| 1424 |
try {
|
|
|
|
| 1425 |
const headEnd = headNeed - 1;
|
| 1426 |
if (headEnd >= 0 && !(hit && hit.head && hit.head.length >= headNeed)) {
|
| 1427 |
const parts = [];
|
|
|
|
| 1430 |
}
|
| 1431 |
if (parts.length) hotFill(id, 0, Buffer.concat(parts), rec.size);
|
| 1432 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1433 |
} catch {
|
| 1434 |
} finally {
|
| 1435 |
bot.load = Math.max(0, bot.load - 1);
|
|
|
|
| 1437 |
finish();
|
| 1438 |
}
|
| 1439 |
})();
|
| 1440 |
+
if (rec.size > PROBE_HEAD && !durCache.get(id) && !(hit && hit.tail)) {
|
| 1441 |
+
setTimeout(() => {
|
| 1442 |
+
if (durCache.get(id) > 1 || (hotMap.get(id) && hotMap.get(id).tail)) return;
|
| 1443 |
+
warmTail(bot, rec, msgId).catch(() => { });
|
| 1444 |
+
}, 8000);
|
| 1445 |
+
}
|
| 1446 |
return p;
|
| 1447 |
}
|
| 1448 |
|
| 1449 |
+
async function warmTail(bot, rec, msgId) {
|
| 1450 |
+
const id = String(msgId);
|
| 1451 |
+
if (!bot || !rec || durCache.get(id) > 1) return;
|
| 1452 |
+
const tailNeed = Math.min(HOT_TAIL, 2 * MB);
|
| 1453 |
+
const tailOff = Math.max(0, rec.size - tailNeed);
|
| 1454 |
+
const parts = [];
|
| 1455 |
+
for await (const c of rangeChunks(bot, rec, tailOff, rec.size - 1, () => false, id)) {
|
| 1456 |
+
parts.push(c);
|
| 1457 |
+
}
|
| 1458 |
+
if (!parts.length) return;
|
| 1459 |
+
const buf = Buffer.concat(parts);
|
| 1460 |
+
hotFill(id, tailOff, buf, rec.size);
|
| 1461 |
+
const d = durationFromMp4(buf);
|
| 1462 |
+
if (d > 1) durCache.set(id, d);
|
| 1463 |
+
}
|
| 1464 |
+
|
| 1465 |
class SingleFlight {
|
| 1466 |
constructor() {
|
| 1467 |
this.active = new Map();
|
|
|
|
| 2480 |
let walletReady = false;
|
| 2481 |
let walletNext = 0;
|
| 2482 |
let walletJob = null;
|
| 2483 |
+
let paypalLive = false;
|
| 2484 |
+
let payTypes = ["card"];
|
| 2485 |
|
| 2486 |
function payPref(node) {
|
| 2487 |
const d = node && node.display_preference;
|
|
|
|
| 2490 |
}
|
| 2491 |
|
| 2492 |
async function ensureFreeWallets() {
|
| 2493 |
+
if (ROLE !== "indexer" || !STRIPE_KEY) return { paypal: false, types: ["card"] };
|
| 2494 |
+
if (Date.now() < walletNext) return { paypal: paypalLive, types: payTypes.slice() };
|
| 2495 |
+
if (walletReady && paypalLive) return { paypal: true, types: payTypes.slice() };
|
| 2496 |
if (walletJob) return walletJob;
|
| 2497 |
walletJob = (async () => {
|
| 2498 |
const list = await stripeGet("payment_method_configurations?limit=10");
|
|
|
|
| 2500 |
const cfg = rows.find((x) => x && x.is_default && x.active) || rows[0];
|
| 2501 |
if (!cfg || !/^pmc_/.test(String(cfg.id || ""))) {
|
| 2502 |
walletNext = Date.now() + 600000;
|
| 2503 |
+
return { paypal: paypalLive, types: payTypes.slice() };
|
| 2504 |
+
}
|
| 2505 |
+
const want = {
|
| 2506 |
+
card: "on",
|
| 2507 |
+
apple_pay: "on",
|
| 2508 |
+
google_pay: "on",
|
| 2509 |
+
link: "on",
|
| 2510 |
+
samsung_pay: "on",
|
| 2511 |
+
revolut_pay: "on",
|
| 2512 |
+
paypal: "on"
|
| 2513 |
+
};
|
| 2514 |
let failed = false;
|
| 2515 |
for (const key of Object.keys(want)) {
|
| 2516 |
if (!cfg[key]) continue;
|
|
|
|
| 2520 |
});
|
| 2521 |
if (!saved) failed = true;
|
| 2522 |
}
|
| 2523 |
+
const fresh = await stripeGet("payment_method_configurations/" + cfg.id);
|
| 2524 |
+
const pp = fresh && fresh.paypal;
|
| 2525 |
+
paypalLive = !!(pp && pp.available === true && payPref(pp) === "on");
|
| 2526 |
+
const order = ["card", "link", "samsung_pay", "revolut_pay", "paypal"];
|
| 2527 |
+
const next = [];
|
| 2528 |
+
for (const name of order) {
|
| 2529 |
+
if (name === "card") {
|
| 2530 |
+
next.push("card");
|
| 2531 |
+
continue;
|
| 2532 |
+
}
|
| 2533 |
+
const node = fresh && fresh[name];
|
| 2534 |
+
if (node && node.available === true && payPref(node) === "on") next.push(name);
|
| 2535 |
+
}
|
| 2536 |
+
if (next.length) payTypes = next;
|
| 2537 |
if (failed) walletNext = Date.now() + 600000;
|
| 2538 |
+
else if (paypalLive) walletReady = true;
|
| 2539 |
+
else walletNext = Date.now() + 120000;
|
| 2540 |
+
return { paypal: paypalLive, types: payTypes.slice() };
|
| 2541 |
})().catch(() => {
|
| 2542 |
walletNext = Date.now() + 600000;
|
| 2543 |
+
return { paypal: paypalLive, types: payTypes.slice() };
|
| 2544 |
}).finally(() => {
|
| 2545 |
walletJob = null;
|
| 2546 |
});
|
|
|
|
| 2587 |
const ses = await stripeSession(rec.sid);
|
| 2588 |
const meta = ses && ses.metadata || {};
|
| 2589 |
if (!ses || !ses.url || ses.status !== "open" || ses.payment_status === "paid") return null;
|
| 2590 |
+
const types = Array.isArray(ses.payment_method_types) ? ses.payment_method_types : [];
|
| 2591 |
+
const need = ["card"];
|
| 2592 |
+
if (payTypes.indexOf("link") >= 0) need.push("link");
|
| 2593 |
+
if (paypalLive) need.push("paypal");
|
| 2594 |
+
if (need.some((t) => types.indexOf(t) < 0)) return null;
|
| 2595 |
if (meta.uid !== uid || meta.plan !== planKey || String(meta.email || "") !== email) return null;
|
| 2596 |
if ((meta.auto === "1") !== !!auto) return null;
|
| 2597 |
return { sid: ses.id, url: ses.url };
|
|
|
|
| 2617 |
if (!plan || !email) return json(res, 400, { error: "plan" });
|
| 2618 |
const auto = planKey === "month" && body.auto === true;
|
| 2619 |
const urls = payUrls(body.returnUrl);
|
| 2620 |
+
const wallets = await ensureFreeWallets();
|
| 2621 |
const reused = await openPay(idp.sub, planKey, email, auto);
|
| 2622 |
if (reused) return json(res, 200, reused);
|
| 2623 |
const nonce = crypto.randomBytes(8).toString("hex");
|
|
|
|
| 2625 |
const homeMsg = "After pay you return to [DUB LK 2000 Home](" + HOME_URL + ")";
|
| 2626 |
const fields = {
|
| 2627 |
mode: "payment",
|
|
|
|
| 2628 |
success_url: urls.success,
|
| 2629 |
cancel_url: urls.cancel,
|
| 2630 |
client_reference_id: idp.sub,
|
|
|
|
| 2664 |
delete fields["payment_intent_data[metadata][email]"];
|
| 2665 |
delete fields["payment_intent_data[receipt_email]"];
|
| 2666 |
}
|
| 2667 |
+
const useTypes = (wallets && wallets.types && wallets.types.length ? wallets.types : ["card"]).filter((t) => !(auto && t === "samsung_pay"));
|
| 2668 |
+
useTypes.forEach((t, i) => { fields["payment_method_types[" + i + "]"] = t; });
|
| 2669 |
async function createSes(body) {
|
| 2670 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2671 |
method: "POST",
|