Updates
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { WebSocketData } from "../types";
|
||||
import socketInit from "./socket-init";
|
||||
|
||||
const server = Bun.serve<WebSocketData>({
|
||||
async fetch(req, server) {
|
||||
const { user } = await socketInit({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
return new Response("Unauthorized!");
|
||||
}
|
||||
|
||||
const success = server.upgrade(req, {
|
||||
data: { user },
|
||||
});
|
||||
|
||||
if (success) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return new Response("Web Socket Connection Failed!");
|
||||
},
|
||||
websocket: {
|
||||
async message(ws, message) {
|
||||
if (typeof message == "string") {
|
||||
}
|
||||
},
|
||||
async open(ws) {},
|
||||
async close(ws, code, message) {},
|
||||
idleTimeout: 600,
|
||||
maxPayloadLength: 1024 * 1024 * 10,
|
||||
},
|
||||
port: process.env.WEB_SOCKET_PORT,
|
||||
});
|
||||
|
||||
console.log(`Websocket Listening on http://${server.hostname}:${server.port}`);
|
||||
@@ -0,0 +1,39 @@
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import { User } from "../types";
|
||||
|
||||
type Param = {
|
||||
req: Request;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
user: User | null;
|
||||
};
|
||||
|
||||
export default async function socketInit({
|
||||
req,
|
||||
debug,
|
||||
}: Param): Promise<Return> {
|
||||
const cookieString = req.headers.get("Cookie") || undefined;
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::socketInit:cookieString", cookieString);
|
||||
}
|
||||
|
||||
if (!cookieString)
|
||||
return {
|
||||
user: null,
|
||||
};
|
||||
|
||||
const user = datasquirel.user.auth.auth({
|
||||
cookieString,
|
||||
database: process.env.DSQL_DB_NAME || "",
|
||||
skipFileCheck: true,
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::socketInit:user", user);
|
||||
}
|
||||
|
||||
return { user: user.payload as User | null };
|
||||
}
|
||||
Reference in New Issue
Block a user