incomingLongpollRequestAction = {
const req = incomingLongpollRequest.req;
const res = incomingLongpollRequest.res;
const callbackId = req.query.cb;
if (req.query.start) {
const cid = Math.random().toString(16).substring(3);
sessions[cid] = {
id: cid,
responseId: 0,
sessionId: Math.random().toString(16).substring(3),
password: Math.random().toString(16).substring(3),
hanginingRequest: null,
serverToClientQueue: []
};
res.header("content-type", "application/javascript");
res.send(newSessionResponse(callbackId, sessions[cid]));
} else if (req.query.dframe) {
res.header("content-type", "text/html");
res.send(disconnectFrame(sessions[req.query.id]));
} else {
const session = sessions[req.query.id];
if (!session) {
return viewof incomingLongpollRequest.reject(
new Error("Unrecognized connection id for " + req.query.id)
);
}
if (session.hangingRequest) {
session.hangingRequest.send(`pRTLPCB(${session.responseId++},[])`);
session.hangingRequest = null;
}
res.header("content-type", "application/javascript");
var commandIndex = 0;
while (req.query[`d${commandIndex}`]) {
const data = JSON.parse(
atob(req.query[`d${commandIndex}`].replaceAll(".", "="))
);
commandIndex++;
console.log("Incoming request", data);
try {
const response = await viewof incomingRequest.send({
session,
request: data
});
session.serverToClientQueue.push({
t: "d",
d: { r: data.d.r, b: response }
});
} catch (err) {
console.error(err);
session.serverToClientQueue.push({
t: "d",
d: { s: "fail", d: err.message }
});
}
}
if (session.serverToClientQueue.length > 0) {
res.send(
`pRTLPCB(${session.responseId++},${JSON.stringify([
session.serverToClientQueue.shift()
])});`
);
} else {
session.hangingRequest = res;
}
}
viewof incomingLongpollRequest.respond();
}