Skip to content

Commit

Permalink
feat(web): add nostr ws proxy
Browse files Browse the repository at this point in the history
refactor(web): make node getting code less goofy and more reusable, ignore link types for now
  • Loading branch information
IncognitoTGT committed Jan 10, 2025
1 parent 4f0eaa1 commit fca2b77
Show file tree
Hide file tree
Showing 9 changed files with 575 additions and 18 deletions.
5 changes: 3 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"type": "module",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "tsx server.ts",
"build": "next build",
"start": "next start",
"start": "NODE_ENV=production tsx server.ts",
"check-types": "tsc --noEmit",
"shadcn": "shadcn"
},
Expand All @@ -30,6 +30,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"geist": "^1.3.1",
"http-proxy-middleware": "^3.0.3",
"lucide-react": "^0.460.0",
"next": "15.1.1-canary.23",
"next-themes": "^0.4.4",
Expand Down
33 changes: 33 additions & 0 deletions apps/web/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createServer } from "node:http";
import auth from "@stardust/common/auth";
import { fromNodeHeaders } from "better-auth/node";
import { createProxyMiddleware } from "http-proxy-middleware";
import next from "next";
const dev = process.env.NODE_ENV !== "production";
import type { Socket } from "node:net";
import { getConfig } from "@stardust/config";
const { nostrUrl } = getConfig();
const port = Number.parseInt(process.env.PORT as string) || 3000;
if (process.argv.includes("--turbo")) {
process.env.TURBOPACK = "1";
}
console.log(
`✨ Stardust: Starting ${dev ? "development" : "production"} server ${process.env.TURBOPACK ? "With turbopack" : ""}...`,
);
const httpServer = createServer();
const nostrProxy = createProxyMiddleware({
target: nostrUrl,
ws: true,
});
const app = next({
dev,
port,
httpServer,
});
await app.prepare();
httpServer.on("request", app.getRequestHandler());
httpServer.on("upgrade", (req, socket, head) =>
req.url?.startsWith("/nostr")
? nostrProxy.upgrade(req, socket as Socket, head)
: app.getUpgradeHandler()(req, socket, head),
);
10 changes: 6 additions & 4 deletions apps/web/src/app/(main)/sessions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AspectRatio } from "@/components/ui/aspect-ratio";
import { Button } from "@/components/ui/button";
import { CardTitle } from "@/components/ui/card";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { getNode } from "@/lib/sessions/client";
import { inspectSession } from "@/lib/sessions/inspect";
import auth from "@stardust/common/auth";
import db, { type SelectSession } from "@stardust/db";
Expand All @@ -22,16 +23,16 @@ const ManageSessionButton = ({
icon,
}: {
session: SelectSession;
action: string;
// world class code
action: Parameters<ReturnType<ReturnType<typeof getNode>["sessions"]>["patch"]>[0]["action"];
redirectToView?: boolean;
icon: React.ReactNode;
}) => (
<form
action={async () => {
"use server";

console.log("managing", session.id, action);

const node = getNode(session);
await node.sessions({ id: session.id }).patch({ action });
if (redirectToView) redirect(`/view/${session.id}`);
else revalidatePath("/sessions");
}}
Expand Down Expand Up @@ -134,6 +135,7 @@ export default async function Dashboard() {
<Tooltip>
<TooltipTrigger asChild>
<Button size="icon" variant="ghost" asChild>
{/* @ts-expect-error this will be fixed in the future */}
<Link href={`/view/${session.id}`}>
<ScreenShare />
</Link>
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function Navigation({
},
{
label: "Admin",
// @ts-expect-error this will be fixed soon
href: "/admin",
icon: <Settings />,
adminOnly: true,
Expand Down
9 changes: 9 additions & 0 deletions apps/web/src/lib/sessions/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { stardustConnector } from "@stardust/common/daemon/client";
import { getConfig } from "@stardust/config";
import type { SelectSession } from "@stardust/db";

export function getNode(session: SelectSession) {
const sessionNode = getConfig().nodes.find(({ id }) => id === session.node);
if (!sessionNode) throw new Error("well this is awkward why does the session not have a node");
return stardustConnector(`http://${sessionNode.hostname}:${sessionNode.port || 4000}`, sessionNode.token);
}
9 changes: 2 additions & 7 deletions apps/web/src/lib/sessions/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { stardustConnector } from "@stardust/common/daemon/client";
import { getConfig } from "@stardust/config";
import type { SelectSession } from "@stardust/db";
import { getNode } from "./client";

export async function inspectSession(session: SelectSession) {
const config = getConfig();
const sessionNode = config.nodes.find(({ id }) => id === session.node);
if (!sessionNode) throw new Error("well this is awkward why does the session not have a node");
const node = stardustConnector(`http://${sessionNode.hostname}:${sessionNode.port || 4000}`, sessionNode.token);
// why tf are the types not working
const node = getNode(session);
const { data, error } = await node.sessions({ id: session.id }).get();
if (error || !data.success) throw new Error(`Error while fetching session inspect: ${error}`);
return data;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
},
"devDependencies": {
"@biomejs/biome": "1.9",
"@types/node": "^20",
"@types/bun": "^1.1.15",
"@types/node": "^20",
"tsx": "^4.19.2",
"turbo": "^2.3.0",
"typescript": "5.5.4"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/config/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface Config {
* The public URL of your Stardust instance. Use this if you want to display site metadata.
*/
metadataUrl?: string;
/**
* The URL of the Nostr relay to connect to.
*/
nostrUrl: string;
nodes: NodeConfig[];
auth: AuthConfig;
session?: SessionConfig;
Expand Down
Loading

0 comments on commit fca2b77

Please sign in to comment.