Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcharlie committed Dec 1, 2023
1 parent c4ec271 commit bf4330f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
data
config.json
data.db
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions src/WebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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") {
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/data_providers/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
42 changes: 30 additions & 12 deletions src/data_providers/sqlite/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,38 +10,56 @@ let db: sqlite.Database<Database, Statement>;

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<Event | undefined> => {
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<Event[]> => {
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<void> => {
await db.run("DELETE FROM Event WHERE id = ?", id);
},
"save": async (event: Event): Promise<void> => {
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<void> => {
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;

0 comments on commit bf4330f

Please sign in to comment.