fix lib
This commit is contained in:
parent
ab9b40b1c4
commit
dd8ea2e1ec
|
|
@ -1,6 +1,4 @@
|
||||||
import { useLocal } from "lib/utils/use-local";
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
||||||
import { glb } from "app/lib/goal";
|
|
||||||
import { Button } from "lib/comps/ui/button";
|
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
|
|
@ -9,9 +7,9 @@ import {
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
DialogTrigger,
|
DialogTrigger,
|
||||||
} from "lib/comps/ui/dialog";
|
} 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 { X } from "lucide-react";
|
||||||
import { FC, useEffect } from "react";
|
import { FC } from "react";
|
||||||
|
|
||||||
export const Pop: FC<{
|
export const Pop: FC<{
|
||||||
child: any;
|
child: any;
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ export const useServerRouter = <T extends ReturnType<typeof newServerRouter>>(
|
||||||
|
|
||||||
return {
|
return {
|
||||||
async handle(arg: ServerContext | SessionContext<any>) {
|
async handle(arg: ServerContext | SessionContext<any>) {
|
||||||
const { url, req } = arg;
|
const { url, req, handle } = arg;
|
||||||
const found = findRoute(rou, undefined, url.pathname);
|
const found = findRoute(rou, undefined, url.pathname);
|
||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
|
|
@ -93,6 +93,7 @@ export const useServerRouter = <T extends ReturnType<typeof newServerRouter>>(
|
||||||
|
|
||||||
return new Response(JSON.stringify(result));
|
return new Response(JSON.stringify(result));
|
||||||
}
|
}
|
||||||
|
return handle(req);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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 = <T>(arg?: {
|
|
||||||
tracker?: { enabled?: boolean };
|
|
||||||
on?: Partial<{
|
|
||||||
messageReceived: (session: ClientSession<T>) => Promise<void>;
|
|
||||||
afterLogin: (session: ClientSession<T>) => Promise<void>;
|
|
||||||
afterLogout: (session: ClientSession<T>) => Promise<void>;
|
|
||||||
afterInit: (session: ClientSession<T>) => Promise<void>;
|
|
||||||
}>;
|
|
||||||
}) => {
|
|
||||||
const store = sessionClientStore<T>();
|
|
||||||
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<T> = {
|
|
||||||
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<SessionData<T>>(async (resolve, reject) => {
|
|
||||||
if (isEditor) {
|
|
||||||
resolve({} as any);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
login_promise.resolve = resolve;
|
|
||||||
login_promise.reject = reject;
|
|
||||||
if (this.status === "checking") {
|
|
||||||
await new Promise<void>((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<void>(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<void>((done) => {
|
|
||||||
const ival = setInterval(() => {
|
|
||||||
if (ws.readyState === ws.CLOSED) {
|
|
||||||
clearInterval(ival);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
}, 100);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
@ -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")],
|
|
||||||
});
|
|
||||||
|
|
@ -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<EsensiSession>(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;
|
|
||||||
}
|
|
||||||
|
|
@ -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<EsensiSession>(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" };
|
|
||||||
}
|
|
||||||
|
|
@ -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<EsensiSession>(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" };
|
|
||||||
}
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
/// <reference types="bun-types" />
|
|
||||||
|
|
||||||
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<void>;
|
|
||||||
handle: (arg: ServerContext) => Promise<Response>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const initSessionServer = <T>(
|
|
||||||
server: PrasiServer,
|
|
||||||
arg: {
|
|
||||||
encrypt?: boolean;
|
|
||||||
router?: ReturnType<typeof useServerRouter>;
|
|
||||||
}
|
|
||||||
) => {
|
|
||||||
try {
|
|
||||||
const session_store = newSessionStore<T>(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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
@ -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),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
);
|
|
||||||
Loading…
Reference in New Issue