local-llm-gateway 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ import { and, eq, gt, isNotNull, isNull, lt, or } from 'drizzle-orm';
2
+ import { webCodes, webSessions } from './schema.js';
3
+ export class PgWebSessionsStore {
4
+ db;
5
+ constructor(db) {
6
+ this.db = db;
7
+ }
8
+ async createCode(userId, codeHash, expiresAt) {
9
+ await this.db.insert(webCodes).values({ userId, codeHash, expiresAt });
10
+ }
11
+ async redeemCode(codeHash, now) {
12
+ // One UPDATE: two concurrent redeems can't both see "unused" (row lock on update).
13
+ const [row] = await this.db
14
+ .update(webCodes)
15
+ .set({ usedAt: now })
16
+ .where(and(eq(webCodes.codeHash, codeHash), isNull(webCodes.usedAt), gt(webCodes.expiresAt, now)))
17
+ .returning({ userId: webCodes.userId });
18
+ return row ?? null;
19
+ }
20
+ async createSession(userId, tokenHash, now, expiresAt) {
21
+ const [row] = await this.db
22
+ .insert(webSessions)
23
+ .values({ userId, tokenHash, lastSeenAt: now, expiresAt })
24
+ .returning({ id: webSessions.id });
25
+ return row;
26
+ }
27
+ async findActiveSession(tokenHash, now) {
28
+ const [row] = await this.db
29
+ .select({ id: webSessions.id, userId: webSessions.userId, lastSeenAt: webSessions.lastSeenAt })
30
+ .from(webSessions)
31
+ .where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt), gt(webSessions.expiresAt, now)))
32
+ .limit(1);
33
+ return row ?? null;
34
+ }
35
+ async touchSession(id, now, expiresAt) {
36
+ await this.db.update(webSessions).set({ lastSeenAt: now, expiresAt }).where(eq(webSessions.id, id));
37
+ }
38
+ async revokeSession(tokenHash, now) {
39
+ const rows = await this.db
40
+ .update(webSessions)
41
+ .set({ revokedAt: now })
42
+ .where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt)))
43
+ .returning({ id: webSessions.id });
44
+ return rows.length > 0;
45
+ }
46
+ async deleteExpired(before) {
47
+ await this.db.delete(webCodes).where(or(lt(webCodes.expiresAt, before), isNotNull(webCodes.usedAt)));
48
+ await this.db.delete(webSessions).where(or(lt(webSessions.expiresAt, before), lt(webSessions.revokedAt, before)));
49
+ }
50
+ }
@@ -5,12 +5,14 @@ import { SqliteConversationsStore } from './conversations.js';
5
5
  import { SqliteGenerationsStore } from './generations.js';
6
6
  import { SqliteApiKeysStore, SqliteUsersStore } from './identity.js';
7
7
  import { SqliteMessagesStore } from './messages.js';
8
+ import { SqliteWebSessionsStore } from './web-sessions.js';
8
9
  /** `path` is a database file, or `:memory:` for a private in-memory database (tests). */
9
10
  export function createSqliteStorage(path) {
10
11
  const { sqlite, db } = openSqlite(path);
11
12
  return {
12
13
  db,
13
14
  users: new SqliteUsersStore(db),
15
+ webSessions: new SqliteWebSessionsStore(db),
14
16
  apiKeys: new SqliteApiKeysStore(db),
15
17
  conversations: new SqliteConversationsStore(db),
16
18
  messages: new SqliteMessagesStore(db),
@@ -72,3 +72,21 @@ export const generations = sqliteTable('generations', {
72
72
  index('generations_user_created_idx').on(t.userId, t.createdAt),
73
73
  index('generations_request_idx').on(t.requestId),
74
74
  ]);
75
+ // Browser sign-in (web chat). Mirrors the Postgres tables; only hashes are stored.
76
+ export const webCodes = sqliteTable('web_codes', {
77
+ id: id(),
78
+ userId: text('user_id').notNull().references(() => users.id),
79
+ codeHash: text('code_hash').notNull().unique(),
80
+ expiresAt: ms('expires_at').notNull(),
81
+ usedAt: ms('used_at'),
82
+ createdAt: createdAt(),
83
+ });
84
+ export const webSessions = sqliteTable('web_sessions', {
85
+ id: id(),
86
+ userId: text('user_id').notNull().references(() => users.id),
87
+ tokenHash: text('token_hash').notNull().unique(),
88
+ createdAt: createdAt(),
89
+ lastSeenAt: ms('last_seen_at').notNull(),
90
+ expiresAt: ms('expires_at').notNull(), // slides forward with use (idle timeout)
91
+ revokedAt: ms('revoked_at'),
92
+ }, (t) => [index('web_sessions_user_idx').on(t.userId)]);
@@ -0,0 +1,50 @@
1
+ import { and, eq, gt, isNotNull, isNull, lt, or } from 'drizzle-orm';
2
+ import { webCodes, webSessions } from './schema.js';
3
+ export class SqliteWebSessionsStore {
4
+ db;
5
+ constructor(db) {
6
+ this.db = db;
7
+ }
8
+ async createCode(userId, codeHash, expiresAt) {
9
+ this.db.insert(webCodes).values({ userId, codeHash, expiresAt }).run();
10
+ }
11
+ async redeemCode(codeHash, now) {
12
+ // One UPDATE ... RETURNING: SQLite runs it as a single write, so a code works once.
13
+ return (this.db
14
+ .update(webCodes)
15
+ .set({ usedAt: now })
16
+ .where(and(eq(webCodes.codeHash, codeHash), isNull(webCodes.usedAt), gt(webCodes.expiresAt, now)))
17
+ .returning({ userId: webCodes.userId })
18
+ .get() ?? null);
19
+ }
20
+ async createSession(userId, tokenHash, now, expiresAt) {
21
+ return this.db
22
+ .insert(webSessions)
23
+ .values({ userId, tokenHash, lastSeenAt: now, expiresAt })
24
+ .returning({ id: webSessions.id })
25
+ .get();
26
+ }
27
+ async findActiveSession(tokenHash, now) {
28
+ return (this.db
29
+ .select({ id: webSessions.id, userId: webSessions.userId, lastSeenAt: webSessions.lastSeenAt })
30
+ .from(webSessions)
31
+ .where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt), gt(webSessions.expiresAt, now)))
32
+ .get() ?? null);
33
+ }
34
+ async touchSession(id, now, expiresAt) {
35
+ this.db.update(webSessions).set({ lastSeenAt: now, expiresAt }).where(eq(webSessions.id, id)).run();
36
+ }
37
+ async revokeSession(tokenHash, now) {
38
+ const rows = this.db
39
+ .update(webSessions)
40
+ .set({ revokedAt: now })
41
+ .where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt)))
42
+ .returning({ id: webSessions.id })
43
+ .all();
44
+ return rows.length > 0;
45
+ }
46
+ async deleteExpired(before) {
47
+ this.db.delete(webCodes).where(or(lt(webCodes.expiresAt, before), isNotNull(webCodes.usedAt))).run();
48
+ this.db.delete(webSessions).where(or(lt(webSessions.expiresAt, before), lt(webSessions.revokedAt, before))).run();
49
+ }
50
+ }