From bf4330f9d8862941edbaadc26c5d356d208e54cf Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 30 Nov 2023 18:13:31 -0700 Subject: [PATCH] Bug fixes --- .gitignore | 1 + package.json | 4 +- .../sqlite/migrations/001_initial.sql | 0 src/WebSocket.ts | 12 +++--- src/data_providers/filesystem.ts | 2 +- src/data_providers/sqlite/index.ts | 42 +++++++++++++------ 6 files changed, 40 insertions(+), 21 deletions(-) rename {src => resources}/data_providers/sqlite/migrations/001_initial.sql (100%) diff --git a/.gitignore b/.gitignore index 4827ab3..3bb0da7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules dist data config.json +data.db diff --git a/package.json b/package.json index 22c1872..e87d458 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "nostr-relay", "version": "1.0.0", "description": "A simple nostr relay.", - "main": "dist/index.js", + "main": "dist/src/index.js", "scripts": { "prepare": "npm run build:clean && npm run build", "test": "jest", "build": "tsc", "build:watch": "npm run build -- --watch", "build:clean": "rimraf dist", - "start": "node dist/WebSocket.js" + "start": "node dist/src/WebSocket.js" }, "author": "rrainn, Inc.", "license": "UNLICENSED", diff --git a/src/data_providers/sqlite/migrations/001_initial.sql b/resources/data_providers/sqlite/migrations/001_initial.sql similarity index 100% rename from src/data_providers/sqlite/migrations/001_initial.sql rename to resources/data_providers/sqlite/migrations/001_initial.sql diff --git a/src/WebSocket.ts b/src/WebSocket.ts index 3e40b29..2077449 100644 --- a/src/WebSocket.ts +++ b/src/WebSocket.ts @@ -13,7 +13,7 @@ import * as http from "http"; import * as fs from "fs"; import * as path from "path"; -const configuration: Configuration = JSON.parse(fs.readFileSync(path.join(__dirname, "../config.json"), "utf8")); +const configuration: Configuration = JSON.parse(fs.readFileSync(path.join(__dirname, "../../config.json"), "utf8")); import packageJson from "../package.json"; @@ -38,24 +38,22 @@ import getEventKindType from "./utils/getEventKindType"; switch (providerType) { case "inmemory": { const { default: provider } = await import("./data_providers/inmemory"); - await provider.setup(); return provider; } case "filesystem": { const { default: provider } = await import("./data_providers/filesystem"); - await provider.setup(); return provider; } case "sqlite": { - const { default: provider } = await import("./data_providers/sqlite"); - await provider.setup(); + const { default: provider } = await import("./data_providers/sqlite/index"); return provider; } default: { throw new Error(`Invalid data provider: ${providerType}`); } } - })(); + })() as DataProvider; + await dataProvider.setup(); app.get("/", (req, res, next) => { if (req.get("Accept") === "application/nostr+json") { @@ -151,6 +149,8 @@ import getEventKindType from "./utils/getEventKindType"; console.error(`Received message with unsupported event kind`, message); break; } + + break; } case "REQ": { const id = messageObject[1]; diff --git a/src/data_providers/filesystem.ts b/src/data_providers/filesystem.ts index de1ddcd..e6ed41e 100644 --- a/src/data_providers/filesystem.ts +++ b/src/data_providers/filesystem.ts @@ -5,7 +5,7 @@ import { DataProvider } from "../types/DataProvider"; import { Event } from "../types/Event"; import isEventExpired from "../utils/isEventExpired"; -const dataDirectory = path.join(__dirname, "../../data"); +const dataDirectory = path.join(__dirname, "../../../data"); const provider: DataProvider = { "setup": async () => { diff --git a/src/data_providers/sqlite/index.ts b/src/data_providers/sqlite/index.ts index 4a90bc4..6020cf5 100644 --- a/src/data_providers/sqlite/index.ts +++ b/src/data_providers/sqlite/index.ts @@ -1,5 +1,5 @@ import * as path from "path"; -import sqlite from "sqlite"; +import * as sqlite from "sqlite"; import { Database, Statement } from "sqlite3"; import { DataProvider } from "../../types/DataProvider"; @@ -10,38 +10,56 @@ let db: sqlite.Database; const provider: DataProvider = { "setup": async () => { - db = await sqlite.open({ - "filename": path.join(__dirname, "../../../data.db"), - "driver": sqlite.Database - }); - await db.migrate({ - "migrationsPath": path.join(__dirname, "./migrations") - }); + try { + db = await sqlite.open({ + "filename": path.join(__dirname, "../../../../data.db"), + "driver": Database + }); + await db.migrate({ + "migrationsPath": path.join(__dirname, "../../../../resources/data_providers/sqlite/migrations") + }); + } catch (e) { + console.error(e); + throw e; + } }, "events": { "get": async (id: string): Promise => { - const event: Event | undefined = await db.get("SELECT * FROM Event WHERE id = ?", id); + const event: Event | undefined = convertFromSqlite(await db.get("SELECT * FROM Event WHERE id = ?", id)); if (event && isEventExpired(event) === false) { return event; } return undefined; }, "getAll": async (): Promise => { - const events: Event[] = await db.all("SELECT * FROM Event"); + const events: Event[] = (await db.all("SELECT * FROM Event")).map((event) => convertFromSqlite(event)); return events.filter((event) => isEventExpired(event) === false); }, "delete": async (id: string): Promise => { await db.run("DELETE FROM Event WHERE id = ?", id); }, "save": async (event: Event): Promise => { - await db.run("INSERT INTO Event (id, pubkey, created_at, kind, tags, content, sig) VALUES (?, ?, ?, ?, ?, ?, ?)", event.id, event.pubkey, event.created_at, event.kind, event.tags, event.content, event.sig); + await db.run("INSERT INTO Event (id, pubkey, created_at, kind, tags, content, sig) VALUES (?, ?, ?, ?, ?, ?, ?)", event.id, event.pubkey, event.created_at, event.kind, JSON.stringify(event.tags), event.content, event.sig); }, "purgeExpired": async (): Promise => { - const events: Event[] = await db.all("SELECT * FROM Event"); + const events: Event[] = (await db.all("SELECT * FROM Event")).map((event) => convertFromSqlite(event)); const expiredEvents: Event[] = events.filter((event) => isEventExpired(event)); + console.log(`Purging ${expiredEvents.length} expired events`); await Promise.all(expiredEvents.map((event) => db.run("DELETE FROM Event WHERE id = ?", event.id))); } } } +function convertFromSqlite(event: any): Event { + if (!event) { + return event; + } + + const convertedEvent: Event = { + ...event, + "tags": JSON.parse(event.tags) + }; + return convertedEvent; +} + export default provider;