Fixed cookie problems
This commit is contained in:
+56
-31
@@ -3,7 +3,7 @@ import { LiveRunMove } from "./types";
|
|||||||
import { randomUUID } from "crypto";
|
import { randomUUID } from "crypto";
|
||||||
|
|
||||||
let socket: WebSocket | null = null;
|
let socket: WebSocket | null = null;
|
||||||
let cookies: string | undefined;
|
let cookies: Record<string, string> | undefined;
|
||||||
|
|
||||||
type PendingResolver = {
|
type PendingResolver = {
|
||||||
resolve: (value: any) => void;
|
resolve: (value: any) => void;
|
||||||
@@ -12,11 +12,11 @@ type PendingResolver = {
|
|||||||
|
|
||||||
const pending = new Map<string, PendingResolver>();
|
const pending = new Map<string, PendingResolver>();
|
||||||
|
|
||||||
export function getCookies(): string | undefined {
|
export function getCookies(): Record<string, string> | undefined {
|
||||||
return cookies;
|
return cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setCookies(c: string) {
|
export function setCookies(c: Record<string, string>) {
|
||||||
cookies = c;
|
cookies = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,55 +26,80 @@ function getWsOrigin(useLocalhost: boolean) {
|
|||||||
: "wss://dev-run.selimaj.dev/api/run";
|
: "wss://dev-run.selimaj.dev/api/run";
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureSocket(useLocalhost: boolean): WebSocket {
|
async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
||||||
if (socket && socket.readyState === WebSocket.OPEN) {
|
if (
|
||||||
|
socket &&
|
||||||
|
socket.url === getWsOrigin(useLocalhost) &&
|
||||||
|
socket.readyState === WebSocket.OPEN
|
||||||
|
) {
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket = new WebSocket(getWsOrigin(useLocalhost));
|
if (socket && socket.url !== getWsOrigin(useLocalhost)) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
socket.onmessage = (e) => {
|
return new Promise((resolve, reject) => {
|
||||||
const msg = JSON.parse(e.data.toString());
|
const ws = new WebSocket(getWsOrigin(useLocalhost));
|
||||||
const { requestId, ok, data, error } = msg;
|
|
||||||
|
|
||||||
if (!requestId) return;
|
ws.onopen = () => {
|
||||||
|
socket = ws;
|
||||||
|
|
||||||
const pendingReq = pending.get(requestId);
|
console.log("Authenticating with cookies:", getCookies());
|
||||||
if (!pendingReq) return;
|
|
||||||
|
|
||||||
pending.delete(requestId);
|
sendUnsafe(useLocalhost, { cookies: getCookies() });
|
||||||
|
|
||||||
ok ? pendingReq.resolve(data) : pendingReq.reject(error);
|
resolve(ws);
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onclose = () => {
|
ws.onerror = (err) => {
|
||||||
socket = null;
|
reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
return socket;
|
ws.onmessage = (e) => {
|
||||||
|
const msg = JSON.parse(e.data.toString());
|
||||||
|
const { requestId, ok, data, error } = msg;
|
||||||
|
|
||||||
|
if (!requestId) return;
|
||||||
|
|
||||||
|
const pendingReq = pending.get(requestId);
|
||||||
|
if (!pendingReq) return;
|
||||||
|
|
||||||
|
pending.delete(requestId);
|
||||||
|
|
||||||
|
ok ? pendingReq.resolve(data) : pendingReq.reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
socket = null;
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function send<T>(
|
async function sendUnsafe<T>(
|
||||||
useLocalhost: boolean,
|
useLocalhost: boolean,
|
||||||
payload: Record<string, any>,
|
payload: Record<string, any>,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
return new Promise((resolve, reject) => {
|
if (!socket) {
|
||||||
const ws = ensureSocket(useLocalhost);
|
throw new Error("WebSocket is not connected");
|
||||||
|
}
|
||||||
|
|
||||||
const requestId = randomUUID();
|
const requestId = randomUUID();
|
||||||
|
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
pending.set(requestId, { resolve, reject });
|
pending.set(requestId, { resolve, reject });
|
||||||
|
socket?.send(JSON.stringify({ ...payload, requestId }));
|
||||||
ws.send(
|
|
||||||
JSON.stringify({
|
|
||||||
...payload,
|
|
||||||
requestId,
|
|
||||||
cookies, // optional: only needed if you authenticate via cookies manually
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function send<T>(
|
||||||
|
useLocalhost: boolean,
|
||||||
|
payload: Record<string, any>,
|
||||||
|
): Promise<T> {
|
||||||
|
await ensureSocket(useLocalhost);
|
||||||
|
return await sendUnsafe(useLocalhost, payload);
|
||||||
|
}
|
||||||
|
|
||||||
export async function addRun(
|
export async function addRun(
|
||||||
useLocalhost: boolean,
|
useLocalhost: boolean,
|
||||||
problem: string,
|
problem: string,
|
||||||
|
|||||||
Reference in New Issue
Block a user