This repository was archived by the owner on Oct 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
chore: add rivet as a default driver #1004
Closed
NathanFlurry
wants to merge
1
commit into
06-24-chore_rename_rivetkit_-_rivetkit_core_expose_rivetkit_worker
from
06-24-chore_add_rivet_as_a_default_driver
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,20 @@ | ||
| import { betterAuth } from "better-auth"; | ||
| import { sqliteAdapter } from "@better-auth/sqlite"; | ||
| import Database from "better-sqlite3"; | ||
|
|
||
| const db = new Database("./auth.db"); | ||
|
|
||
| export const auth = betterAuth({ | ||
| database: sqliteAdapter(db), | ||
| emailAndPassword: { | ||
| enabled: true, | ||
| }, | ||
| session: { | ||
| expiresIn: 60 * 60 * 24 * 7, // 7 days | ||
| updateAge: 60 * 60 * 24, // 1 day (every day the session expiry is updated) | ||
| }, | ||
| plugins: [], | ||
| }); | ||
|
|
||
| export type Session = typeof auth.$Infer.Session; | ||
| export type User = typeof auth.$Infer.User; | ||
| // import { betterAuth } from "better-auth"; | ||
| // import { sqliteAdapter } from "@better-auth/sqlite"; | ||
| // import Database from "better-sqlite3"; | ||
| // | ||
| // const db = new Database("./auth.db"); | ||
| // | ||
| // export const auth = betterAuth({ | ||
| // database: sqliteAdapter(db), | ||
| // emailAndPassword: { | ||
| // enabled: true, | ||
| // }, | ||
| // session: { | ||
| // expiresIn: 60 * 60 * 24 * 7, // 7 days | ||
| // updateAge: 60 * 60 * 24, // 1 day (every day the session expiry is updated) | ||
| // }, | ||
| // plugins: [], | ||
| // }); | ||
| // | ||
| // export type Session = typeof auth.$Infer.Session; | ||
| // export type User = typeof auth.$Infer.User; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,48 @@ | ||
| import { worker, setup } from "@rivetkit/worker"; | ||
| import { auth, type Session, type User } from "./auth"; | ||
|
|
||
| export const chatRoom = worker({ | ||
| onAuth: async (c) => { | ||
| const authResult = await auth.api.getSession({ | ||
| headers: c.req.headers, | ||
| }); | ||
|
|
||
| if (!authResult?.session || !authResult?.user) { | ||
| throw new Error("Unauthorized"); | ||
| } | ||
|
|
||
| return { | ||
| userId: authResult.user.id, | ||
| user: authResult.user, | ||
| session: authResult.session, | ||
| }; | ||
| }, | ||
| state: { | ||
| messages: [] as Array<{ id: string; userId: string; username: string; message: string; timestamp: number }> | ||
| }, | ||
| actions: { | ||
| sendMessage: (c, message: string) => { | ||
| const newMessage = { | ||
| id: crypto.randomUUID(), | ||
| userId: c.auth.userId, | ||
| username: c.auth.user.email, | ||
| message, | ||
| timestamp: Date.now(), | ||
| }; | ||
| c.state.messages.push(newMessage); | ||
| c.broadcast("newMessage", newMessage); | ||
| return newMessage; | ||
| }, | ||
| getMessages: (c) => { | ||
| return c.state.messages; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export const registry = setup({ | ||
| workers: { chatRoom }, | ||
| }); | ||
|
|
||
| export type Registry = typeof registry; | ||
| // import { worker, setup } from "@rivetkit/worker"; | ||
| // import { auth, type Session, type User } from "./auth"; | ||
| // | ||
| // export const chatRoom = worker({ | ||
| // onAuth: async (c) => { | ||
| // const authResult = await auth.api.getSession({ | ||
| // headers: c.req.headers, | ||
| // }); | ||
| // | ||
| // if (!authResult?.session || !authResult?.user) { | ||
| // throw new Error("Unauthorized"); | ||
| // } | ||
| // | ||
| // return { | ||
| // userId: authResult.user.id, | ||
| // user: authResult.user, | ||
| // session: authResult.session, | ||
| // }; | ||
| // }, | ||
| // state: { | ||
| // messages: [] as Array<{ id: string; userId: string; username: string; message: string; timestamp: number }> | ||
| // }, | ||
| // actions: { | ||
| // sendMessage: (c, message: string) => { | ||
| // const newMessage = { | ||
| // id: crypto.randomUUID(), | ||
| // userId: c.auth.userId, | ||
| // username: c.auth.user.email, | ||
| // message, | ||
| // timestamp: Date.now(), | ||
| // }; | ||
| // | ||
| // c.state.messages.push(newMessage); | ||
| // c.broadcast("newMessage", newMessage); | ||
| // | ||
| // return newMessage; | ||
| // }, | ||
| // getMessages: (c) => { | ||
| // return c.state.messages; | ||
| // }, | ||
| // }, | ||
| // }); | ||
| // | ||
| // export const registry = setup({ | ||
| // workers: { chatRoom }, | ||
| // }); | ||
| // | ||
| // export type Registry = typeof registry; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,54 @@ | ||
| import { registry } from "./registry"; | ||
| import { auth } from "./auth"; | ||
| import { Hono } from "hono"; | ||
| import { serve } from "@hono/node-server"; | ||
| import { createMemoryDriver } from "@rivetkit/memory"; | ||
|
|
||
| // Setup router | ||
| const app = new Hono(); | ||
|
|
||
| // Start RivetKit | ||
| const { client, hono } = registry.run({ | ||
| driver: createMemoryDriver(), | ||
| cors: { | ||
| // IMPORTANT: Configure origins in production | ||
| origin: "*", | ||
| }, | ||
| }); | ||
|
|
||
| // Mount Better Auth routes | ||
| app.on(["GET", "POST"], "/api/auth/**", (c) => auth.handler(c.req.raw)); | ||
|
|
||
| // Expose RivetKit to the frontend | ||
| app.route("/registry", hono); | ||
|
|
||
| // Example HTTP endpoint to join chat room | ||
| app.post("/api/join-room/:roomId", async (c) => { | ||
| const roomId = c.req.param("roomId"); | ||
|
|
||
| // Verify authentication | ||
| const authResult = await auth.api.getSession({ | ||
| headers: c.req.header(), | ||
| }); | ||
|
|
||
| if (!authResult?.session || !authResult?.user) { | ||
| return c.json({ error: "Unauthorized" }, 401); | ||
| } | ||
|
|
||
| try { | ||
| const room = client.chatRoom.getOrCreate(roomId); | ||
| const messages = await room.getMessages(); | ||
|
|
||
| return c.json({ | ||
| success: true, | ||
| roomId, | ||
| messages, | ||
| user: authResult.user | ||
| }); | ||
| } catch (error) { | ||
| return c.json({ error: "Failed to join room" }, 500); | ||
| } | ||
| }); | ||
|
|
||
| serve({ fetch: app.fetch, port: 6420 }, () => | ||
| console.log("Listening at http://localhost:6420"), | ||
| ); | ||
| // import { registry } from "./registry"; | ||
| // import { auth } from "./auth"; | ||
| // import { Hono } from "hono"; | ||
| // import { serve } from "@hono/node-server"; | ||
| // | ||
| // // Setup router | ||
| // const app = new Hono(); | ||
| // | ||
| // // Start RivetKit | ||
| // const { client, hono } = registry.run({ | ||
| // driver: createMemoryDriver(), | ||
| // cors: { | ||
| // // IMPORTANT: Configure origins in production | ||
| // origin: "*", | ||
| // }, | ||
| // }); | ||
| // | ||
| // // Mount Better Auth routes | ||
| // app.on(["GET", "POST"], "/api/auth/**", (c) => auth.handler(c.req.raw)); | ||
| // | ||
| // // Expose RivetKit to the frontend | ||
| // app.route("/registry", hono); | ||
| // | ||
| // // Example HTTP endpoint to join chat room | ||
| // app.post("/api/join-room/:roomId", async (c) => { | ||
| // const roomId = c.req.param("roomId"); | ||
| // | ||
| // // Verify authentication | ||
| // const authResult = await auth.api.getSession({ | ||
| // headers: c.req.header(), | ||
| // }); | ||
| // | ||
| // if (!authResult?.session || !authResult?.user) { | ||
| // return c.json({ error: "Unauthorized" }, 401); | ||
| // } | ||
| // | ||
| // try { | ||
| // const room = client.chatRoom.getOrCreate(roomId); | ||
| // const messages = await room.getMessages(); | ||
| // | ||
| // return c.json({ | ||
| // success: true, | ||
| // roomId, | ||
| // messages, | ||
| // user: authResult.user | ||
| // }); | ||
| // } catch (error) { | ||
| // return c.json({ error: "Failed to join room" }, 500); | ||
| // } | ||
| // }); | ||
| // | ||
| // serve({ fetch: app.fetch, port: 6420 }, () => | ||
| // console.log("Listening at http://localhost:6420"), | ||
| // ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
servervariable is undefined at this point. Theservefunction call on lines 67-69 doesn't assign its return value to a variable, soinjectWebSocket(server)will throw a ReferenceError. Consider capturing the return value fromservefirst:Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.