diff --git a/cli.ts b/cli.ts new file mode 100644 index 0000000..d83a07d --- /dev/null +++ b/cli.ts @@ -0,0 +1,22 @@ +import "@std/dotenv/load"; +import { parseArgs } from "@std/cli"; + +import { generate_code } from "@/src/crypto.ts"; +import { db } from "@/src/db.ts"; + +async function create_admin() { + const code = generate_code(); + await db.create_entry("users", { + name: "admin", + code, + admin: true, + }); + + console.log("code", code); +} + +const args = parseArgs(Deno.args, {}); + +if (args.create_admin) { + create_admin(); +} diff --git a/deno.json b/deno.json index 1a0070f..1420ced 100644 --- a/deno.json +++ b/deno.json @@ -20,6 +20,9 @@ ], "imports": { "@/": "./", + "@paulaboks/boksy": "jsr:@paulaboks/boksy@^0.4.2", + "@std/cli": "jsr:@std/cli@^1.0.32", + "@std/dotenv": "jsr:@std/dotenv@^0.225.8", "@std/ulid": "jsr:@std/ulid@^1.0.0", "@zod/zod": "jsr:@zod/zod@^4.6.7", "fresh": "jsr:@fresh/core@^2.3.3", @@ -64,5 +67,6 @@ "indentWidth": 4, "lineWidth": 120, "newLineKind": "lf" - } + }, + "unstable": ["kv"] } diff --git a/deno.lock b/deno.lock index a126be1..a3900cd 100644 --- a/deno.lock +++ b/deno.lock @@ -9,9 +9,13 @@ "jsr:@fresh/core@2": "2.3.3", "jsr:@fresh/core@^2.3.3": "2.3.3", "jsr:@fresh/plugin-vite@^1.1.2": "1.1.2", + "jsr:@paulaboks/boksy@~0.4.2": "0.4.2", "jsr:@std/bytes@^1.0.6": "1.0.6", - "jsr:@std/dotenv@~0.225.5": "0.225.6", + "jsr:@std/cli@^1.0.32": "1.0.32", + "jsr:@std/dotenv@~0.225.5": "0.225.8", + "jsr:@std/dotenv@~0.225.8": "0.225.8", "jsr:@std/encoding@^1.0.10": "1.0.10", + "jsr:@std/fmt@^1.0.10": "1.0.10", "jsr:@std/fmt@^1.0.7": "1.0.10", "jsr:@std/fmt@^1.0.8": "1.0.10", "jsr:@std/fs@^1.0.19": "1.0.24", @@ -103,7 +107,7 @@ "jsr:@deno/loader@0.4", "jsr:@fresh/core@2", "jsr:@fresh/core@^2.3.3", - "jsr:@std/dotenv", + "jsr:@std/dotenv@~0.225.5", "jsr:@std/fmt@^1.0.7", "jsr:@std/media-types@1", "jsr:@std/path@1", @@ -116,12 +120,28 @@ "npm:vite@^7.1.4" ] }, + "@paulaboks/boksy@0.4.2": { + "integrity": "6537c5ccd24db14edb7f3ab5eebb3acd64575175254d57ee9212dc64c8f42b2f", + "dependencies": [ + "jsr:@std/ulid" + ] + }, "@std/bytes@1.0.6": { "integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a" }, + "@std/cli@1.0.32": { + "integrity": "188b3a100d6202d64e3f5bd3d799c7fa4f6d77f92cc65eb7f641c1fa0aa92a66", + "dependencies": [ + "jsr:@std/fmt@^1.0.10", + "jsr:@std/internal" + ] + }, "@std/dotenv@0.225.6": { "integrity": "1d6f9db72f565bd26790fa034c26e45ecb260b5245417be76c2279e5734c421b" }, + "@std/dotenv@0.225.8": { + "integrity": "27b8c1a9d00cbf938345a5924487c04d9b27bdd7e7bd1983f54bcf76d0a0174a" + }, "@std/encoding@1.0.10": { "integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1" }, @@ -1491,6 +1511,9 @@ "dependencies": [ "jsr:@fresh/core@^2.3.3", "jsr:@fresh/plugin-vite@^1.1.2", + "jsr:@paulaboks/boksy@~0.4.2", + "jsr:@std/cli@^1.0.32", + "jsr:@std/dotenv@~0.225.8", "jsr:@std/ulid@1", "jsr:@zod/zod@^4.6.7", "npm:@preact/signals@^2.9.0", diff --git a/routes/api/verify_code.ts b/routes/api/verify_code.ts index b471941..aec5bf5 100644 --- a/routes/api/verify_code.ts +++ b/routes/api/verify_code.ts @@ -1,6 +1,5 @@ import { define } from "@/utils.ts"; -import { get_all_users, new_user } from "@/src/db.ts"; -import { generate_code } from "@/src/crypto.ts"; +import { db } from "@/src/db.ts"; export const handler = define.handlers({ async POST(ctx) { @@ -8,21 +7,11 @@ export const handler = define.handlers({ // TODO: zod schema const code = data.code; - const users = await get_all_users(); - if (users.length >= 1) { - const maybe_user = users.find((user) => user.code === code); - - if (maybe_user) { - return Response.json({ code }); - } - - return Response.json({ error: "Invalid code" }, { status: 401 }); + const user = await db.get_entry_by_index("users", "code", code); + if (user) { + return Response.json({ code }); } - // create admin - const admin_code = generate_code(); - await new_user({ name: "admin", code: admin_code, admin: true }); - - return Response.json({ code: admin_code }); + return Response.json({ error: "Invalid code" }, { status: 401 }); }, }); diff --git a/src/db.ts b/src/db.ts index b6eee2d..0b45aa5 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1,66 +1,17 @@ -import { monotonicUlid } from "@std/ulid"; +import { Database } from "@paulaboks/boksy"; import { DATABASE_PATH } from "@/src/server_constants.ts"; -export interface User { - id: string; +export interface User extends Record { name: string; code: string; admin: boolean; } -interface Database { - users: Record; -} +type Schema = { + users: User; +}; -let write_queue: Promise = Promise.resolve(); - -function get_database_path() { - if (!DATABASE_PATH) { - throw new Error("DATABASE_PATH is not set"); - } - return DATABASE_PATH; -} - -async function read_database(): Promise { - try { - return JSON.parse(await Deno.readTextFile(get_database_path())); - } catch (error) { - if (error instanceof Deno.errors.NotFound) return { users: {} }; - throw error; - } -} - -// Writes to a temp file first so a crash mid-write can't corrupt the database -async function write_database(database: Database) { - const path = get_database_path(); - await Deno.writeTextFile(`${path}.tmp`, JSON.stringify(database, null, "\t")); - await Deno.rename(`${path}.tmp`, path); -} - -// Runs read-modify-write operations one at a time so concurrent calls don't overwrite each other -function with_write_lock(fn: () => Promise): Promise { - const result = write_queue.then(fn); - write_queue = result.catch(() => {}); - return result; -} - -export async function get_user(id: string): Promise { - const database = await read_database(); - return database.users[id]; -} - -export function new_user(user: Omit): Promise { - return with_write_lock(async () => { - const database = await read_database(); - const created: User = { ...user, id: monotonicUlid() }; - database.users[created.id] = created; - await write_database(database); - return created; - }); -} - -export async function get_all_users(): Promise { - const database = await read_database(); - return Object.values(database.users); -} +export const db = await Database.open(DATABASE_PATH ?? ":memory:", { + users: ["code"], +});