diff --git a/comps/dialog/Dialog.tsx b/comps/dialog/Dialog.tsx index 6b2d594..08106f4 100755 --- a/comps/dialog/Dialog.tsx +++ b/comps/dialog/Dialog.tsx @@ -1,6 +1,4 @@ -import { useLocal } from "lib/utils/use-local"; -import { glb } from "app/lib/goal"; -import { Button } from "lib/comps/ui/button"; +import * as DialogPrimitive from "@radix-ui/react-dialog"; import { Dialog, DialogContent, @@ -9,9 +7,9 @@ import { DialogTitle, DialogTrigger, } from "lib/comps/ui/dialog"; -import * as DialogPrimitive from "@radix-ui/react-dialog"; +import { useLocal } from "lib/utils/use-local"; import { X } from "lucide-react"; -import { FC, useEffect } from "react"; +import { FC } from "react"; export const Pop: FC<{ child: any; diff --git a/server/server-route.ts b/server/server-route.ts index e50457d..00316fb 100755 --- a/server/server-route.ts +++ b/server/server-route.ts @@ -66,7 +66,7 @@ export const useServerRouter = >( return { async handle(arg: ServerContext | SessionContext) { - const { url, req } = arg; + const { url, req, handle } = arg; const found = findRoute(rou, undefined, url.pathname); if (found) { @@ -93,6 +93,7 @@ export const useServerRouter = >( return new Response(JSON.stringify(result)); } + return handle(req); }, }; }; diff --git a/session/client-session.ts b/session/client-session.ts deleted file mode 100755 index c6504f8..0000000 --- a/session/client-session.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { ConsoleLogWriter } from "drizzle-orm"; -import { newClientRouter } from "../server/server-route"; -import { sessionRouter } from "./router/router"; -import { sessionClientStore } from "./store/client-store"; -import { ClientSession, SessionAuth, SessionData } from "./type"; - -export const newClientSession = (arg?: { - tracker?: { enabled?: boolean }; - on?: Partial<{ - messageReceived: (session: ClientSession) => Promise; - afterLogin: (session: ClientSession) => Promise; - afterLogout: (session: ClientSession) => Promise; - afterInit: (session: ClientSession) => Promise; - }>; -}) => { - const store = sessionClientStore(); - const client = newClientRouter(sessionRouter); - - const login_promise = { resolve: null as any, reject: null as any }; - const logout_promise = { resolve: null as any, reject: null as any }; - - const session: ClientSession = { - status: "checking", - current: null, - connected: false, - async connect(auth) {}, - async init() { - const current = await store.load(); - if (!current) { - this.status = "guest"; - } else { - this.current = current; - this.status = await client.check(current.uid, current.sid); - if (this.status !== "active") { - await store.clear(); - this.current = null; - } else { - await this.connect(); - } - } - if (arg?.on?.afterInit) { - arg.on.afterInit(session); - } - - return { status: this.status }; - }, - login(auth: SessionAuth) { - return new Promise>(async (resolve, reject) => { - if (isEditor) { - resolve({} as any); - return; - } - - login_promise.resolve = resolve; - login_promise.reject = reject; - if (this.status === "checking") { - await new Promise((done) => { - const ival = setInterval(() => { - if (this.status !== "checking") { - clearInterval(ival); - done(); - } - }, 100); - }); - } - - if (this.status === "guest") { - this.connect(auth); - } else { - console.error( - `\ -Session login failed, current status is: ${this.status}. -Login is prevented, please logout first before re-login!` - ); - if (this.current) { - resolve(this.current); - } else { - if (auth) { - this.connect(auth); - } else { - reject("Current session not found"); - } - } - } - }); - }, - logout() { - return new Promise(async (resolve, reject) => { - if (isEditor) { - resolve({} as any); - return; - } - - logout_promise.resolve = resolve; - logout_promise.reject = reject; - }); - }, - }; - - if (!isEditor) { - session.init(); - } - return session; -}; - -const wsReady = async (ws: WebSocket) => { - if (ws) { - if (ws.readyState === ws.OPEN) return; - else { - ws.close(); - await new Promise((done) => { - const ival = setInterval(() => { - if (ws.readyState === ws.CLOSED) { - clearInterval(ival); - done(); - } - }, 100); - }); - } - } -}; diff --git a/session/router/router.ts b/session/router/router.ts deleted file mode 100755 index 61524d3..0000000 --- a/session/router/router.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { newServerRouter } from "lib/server/server-route"; - -export const sessionRouter = newServerRouter({ - check: ["/_session/check", () => import("./session-check")], - logout: ["/_session/logout", () => import("./session-logout")], - track: ["/_session/track", () => import("./session-track")], -}); diff --git a/session/router/session-check.ts b/session/router/session-check.ts deleted file mode 100755 index 290694e..0000000 --- a/session/router/session-check.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { EsensiSession } from "app/server/session"; -import { sessionContext } from "lib/server/context"; -import { ClientSessionStatus } from "../type"; - -export default async function (this: any, uid: string, sid: string) { - const ctx = sessionContext(this); - let result = "invalid" as ClientSessionStatus; - const session = ctx.session.findFirst({ uid, sid }); - if (session) { - if (!session.expired_at || session.expired_at > Date.now()) { - result = "active"; - } else { - result = "expired"; - } - } - - return result; -} diff --git a/session/router/session-logout.ts b/session/router/session-logout.ts deleted file mode 100755 index 40418c0..0000000 --- a/session/router/session-logout.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { EsensiSession } from "app/server/session"; -import { sessionContext } from "lib/server/context"; -import { SessionAuth } from "../type"; - -export default async function (this: any, arg: SessionAuth) { - const ctx = sessionContext(this); - // let result = "invalid" as ClientSessionStatus; - // const session = ctx.session.findFirst({ uid, sid }); - // if (session) { - // if (!session.expired_at || session.expired_at > Date.now()) { - // result = "active"; - // } else { - // result = "expired"; - // } - // } - - return { status: "ok" }; -} diff --git a/session/router/session-track.ts b/session/router/session-track.ts deleted file mode 100755 index 40418c0..0000000 --- a/session/router/session-track.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { EsensiSession } from "app/server/session"; -import { sessionContext } from "lib/server/context"; -import { SessionAuth } from "../type"; - -export default async function (this: any, arg: SessionAuth) { - const ctx = sessionContext(this); - // let result = "invalid" as ClientSessionStatus; - // const session = ctx.session.findFirst({ uid, sid }); - // if (session) { - // if (!session.expired_at || session.expired_at > Date.now()) { - // result = "active"; - // } else { - // result = "expired"; - // } - // } - - return { status: "ok" }; -} diff --git a/session/server-session.ts b/session/server-session.ts deleted file mode 100755 index 2276631..0000000 --- a/session/server-session.ts +++ /dev/null @@ -1,62 +0,0 @@ -/// - -import { ServerWebSocket } from "bun"; -import { useServerRouter } from "../server/server-route"; -import { sessionRouter } from "./router/router"; -import { newSessionStore } from "./store/session-store"; -import { - ServerContext, - SessionAuth, - SessionStore, - SingleSession, -} from "./type"; - -type WS = ServerWebSocket<{ url: string }>; -type SessionServerHandler = { - cleanup: () => Promise; - handle: (arg: ServerContext) => Promise; -}; - -export const initSessionServer = ( - server: PrasiServer, - arg: { - encrypt?: boolean; - router?: ReturnType; - } -) => { - try { - const session_store = newSessionStore(server.site_id); - const session_router = useServerRouter(sessionRouter); - const server_handler: SessionServerHandler = { - async cleanup() {}, - - async handle(server_arg) { - const { req, handle, url } = server_arg; - - const route_arg = { - ...server_arg, - session: { - ...session_store, - current: undefined, - }, - }; - - if (url.pathname.startsWith("/_session/")) { - const res = await session_router.handle(route_arg); - if (res) return res; - } - - if (arg.router) { - const res = await arg.router.handle(route_arg); - if (res) return res; - } - - return handle(req); - }, - }; - - server.session = server_handler; - } catch (e) { - console.log(e); - } -}; diff --git a/session/store/schema.ts b/session/store/schema.ts deleted file mode 100755 index 352062b..0000000 --- a/session/store/schema.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { createId } from "@paralleldrive/cuid2"; -import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; - -export const session = sqliteTable( - "session", - { - sid: text("sid") - .notNull() - .primaryKey() - .$defaultFn(() => createId()), - uid: text("uid").notNull(), - created_at: integer("created_at", { mode: "timestamp_ms" }).default( - new Date() - ), - active: integer("active", { mode: "boolean" }), - data: text("data", { mode: "json" }).notNull(), - wsid: text("wsid", { mode: "json" }).default([]), - expired_at: integer("expired_at", { mode: "timestamp_ms" }), - }, - (table) => { - return { - expired_at_idx: index("expired_at_idx").on(table.expired_at), - }; - } -); - -export const track = sqliteTable( - "track", - { - id: text("id") - .notNull() - .primaryKey() - .$defaultFn(() => createId()), - created_at: integer("created_at", { mode: "timestamp_ms" }).default( - new Date() - ), - session_id: text("session_id"), - url: text("url").notNull(), - referer: text("referer"), - user_ip: text("user_ip"), - data: text("data", { mode: "json" }), - tstamp: integer("tstamp", { mode: "timestamp_ms" }).default(new Date()), - }, - (table) => { - return { - session_id: index("session_id").on(table.session_id), - }; - } -); diff --git a/utils/crypto-alias.ts b/utils/crypto-alias.ts old mode 100644 new mode 100755 diff --git a/utils/fetch-link-params.ts b/utils/fetch-link-params.ts old mode 100644 new mode 100755