backup-worker -must-from -origin- purple=bird=df(perfect free n need origin v2)
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// worker.js
var ALLOWED_ORIGINS = [
"https://www.web3businesssolution.com",
"https://web3businesssolution.com"
];
var RATE_WINDOW_MS = 15e3;
var RATE_MAX = 10;
var MAX_PEERS_PER_ROOM = 16;
var MAX_MSG_BYTES = 256 * 1024;
function originAllowed(origin) {
if (!origin) return false;
return ALLOWED_ORIGINS.includes(origin);
}
__name(originAllowed, "originAllowed");
var worker_default = {
async fetch(request, env) {
const url = new URL(request.url);
const origin = request.headers.get("Origin");
if (url.pathname === "/" || url.pathname === "") {
return new Response("ok", { status: 200, headers: { "content-type": "text/plain" } });
}
if (url.pathname !== "/rtc") {
return new Response("not found", { status: 404 });
}
if (!originAllowed(origin)) {
return new Response("forbidden", { status: 403 });
}
if (request.headers.get("Upgrade") !== "websocket") {
return new Response("expected websocket", { status: 426 });
}
const roomRaw = url.searchParams.get("r") || "";
const room = roomRaw.toLowerCase().replace(/[^a-z0-9_]/g, "").slice(0, 40);
if (!room) {
return new Response("bad room", { status: 400 });
}
const id = env.ROOM.idFromName(room);
const stub = env.ROOM.get(id);
return stub.fetch(request);
}
};
var Room = class {
static {
__name(this, "Room");
}
constructor(state, env) {
this.state = state;
this.env = env;
this.peers = /* @__PURE__ */ new Map();
}
async fetch(request) {
const pair = new WebSocketPair();
const client = pair[0];
const server = pair[1];
this.accept(server);
return new Response(null, { status: 101, webSocket: client });
}
accept(ws) {
ws.accept();
let clientId = null;
const rate = [];
ws.addEventListener("message", (evt) => {
let raw = evt.data;
if (typeof raw !== "string") return;
if (raw.length > MAX_MSG_BYTES) return;
const t = Date.now();
while (rate.length && t - rate[0] > RATE_WINDOW_MS) rate.shift();
rate.push(t);
if (rate.length > RATE_MAX) return;
let msg;
try {
msg = JSON.parse(raw);
} catch (_) {
return;
}
if (!msg || typeof msg !== "object" || typeof msg.t !== "string") return;
if (typeof msg.from !== "string" || !/^[a-z0-9]{4,40}$/.test(msg.from)) return;
if (!clientId) {
clientId = msg.from;
if (this.peers.size >= MAX_PEERS_PER_ROOM && !this.peers.has(clientId)) {
try {
ws.close(1013, "room full");
} catch (_) {
}
return;
}
this.peers.set(clientId, ws);
} else if (msg.from !== clientId) {
return;
}
const to = typeof msg.to === "string" ? msg.to : null;
if (to) {
const dst = this.peers.get(to);
if (dst && dst.readyState === 1) {
try {
dst.send(raw);
} catch (_) {
}
}
} else {
for (const [pid, pws] of this.peers) {
if (pid === clientId) continue;
if (pws.readyState === 1) {
try {
pws.send(raw);
} catch (_) {
}
}
}
}
});
const cleanup = /* @__PURE__ */ __name(() => {
if (clientId && this.peers.get(clientId) === ws) {
this.peers.delete(clientId);
const byeMsg = JSON.stringify({ t: "bye", from: clientId });
for (const [, pws] of this.peers) {
if (pws.readyState === 1) {
try {
pws.send(byeMsg);
} catch (_) {
}
}
}
}
}, "cleanup");
ws.addEventListener("close", cleanup);
ws.addEventListener("error", cleanup);
}
};
export {
Room,
worker_default as default
};
//# sourceMappingURL=worker.js.map