Skip to content

Instantly share code, notes, and snippets.

@steveseguin
Created September 29, 2025 01:10
Show Gist options
  • Select an option

  • Save steveseguin/5cd4278d133624f677603f253bf1b24d to your computer and use it in GitHub Desktop.

Select an option

Save steveseguin/5cd4278d133624f677603f253bf1b24d to your computer and use it in GitHub Desktop.
add to line 715 of main.js
async function limitConcurrentViewers(maxCount) {
if (!session.whepInput || !session.salt) {
throw new Error("Missing session.whepInput or session.salt");
}
const WORKER_URL = "https://hash-counter-worker.vdo.workers.dev";
const WINDOW_MS = 5 * 60 * 1000;
const hashValue = `${session.whepInput}${session.salt}`;
let intervalId;
const handleLimit = (count) => {
if (count < maxCount) {
return;
}
if (typeof warnUser === "function") {
warnUser("Too many viewers; try again later");
}
console.assert(false, "Too many viewers; try again later");
throw new Error("Too many viewers; try again later");
};
const requestCount = async () => {
const response = await fetch(
`${WORKER_URL}?hash=${encodeURIComponent(hashValue)}`,
{ cache: "no-store" }
);
if (!response.ok) {
throw new Error(`Worker responded with ${response.status}`);
}
const data = await response.json();
const count = Number(data.requestCount);
if (!Number.isFinite(count)) {
throw new Error("Invalid worker response");
}
handleLimit(count);
};
await requestCount();
intervalId = setInterval(requestCount, WINDOW_MS);
return () => clearInterval(intervalId);
}
limitConcurrentViewers(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment