diff --git a/packages/system/src/nostr-system.ts b/packages/system/src/nostr-system.ts index 365b221c..0029305f 100644 --- a/packages/system/src/nostr-system.ts +++ b/packages/system/src/nostr-system.ts @@ -193,6 +193,13 @@ export class NostrSystem extends EventEmitter implements Syst this.requestRouter = OutboxModel.fromSystem(this); } + // Cache everything + if (this.#config.cachingRelay) { + this.on("event", async (_, ev) => { + await this.#config.cachingRelay?.event(ev); + }) + } + // Hook on-event when building follow graph if (this.#config.buildFollowGraph) { let evBuf: Array = []; diff --git a/packages/worker-relay/README.md b/packages/worker-relay/README.md index 27cde2e7..18aefde9 100644 --- a/packages/worker-relay/README.md +++ b/packages/worker-relay/README.md @@ -6,26 +6,23 @@ Worker relay is a Nostr relay built on `sqlite-wasm` `sqlite-wasm` uses OFPS in order to persist the database. -OPFS requires special headers to be present when serving your application. Read more about it [here](https://sqlite.org/wasm/doc/trunk/persistence.md#opfs) - -``` -Cross-Origin-Opener-Policy: same-origin -Cross-Origin-Embedder-Policy: require-corp -``` - -### Usage (Vite) - -```typescript -import WorkerRelayPath from "@snort/worker-relay/dist/worker?worker&url"; -``` - ### Example ```typescript -const relay = new WorkerRelayInterface(WorkerRelayPath); +import { WorkerRelayInterface } from "@snort/worker-relay"; + +// in debug mode you may need this, to map to the correct sqlite-wasm path +// this is needed because sqlite-wasm will otherwise look inside @snort/worker-relay directory for sqlite3.wasm +const basePath = new URL("@sqlite.org/sqlite-wasm", import.meta.url); + +// internally we resolve the script path like this: +const scriptPath = new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url); + +// scriptPath & basePath are optional +const relay = new WorkerRelayInterface(scriptPath.href, basePath.href); // load sqlite database and run migrations -await relay.init(); +await relay.init("my-relay.db"); // Query worker relay with regular nostr REQ command const results = await relay.query(["REQ", "1", { kinds: [1], limit: 10 }]); @@ -33,7 +30,9 @@ const results = await relay.query(["REQ", "1", { kinds: [1], limit: 10 }]); // publish a new event to the relay const myEvent = { kind: 1, + created_at: Math.floor(new Date().getTime() / 1000), content: "test", + tags: [] }; if (await relay.event(myEvent)) { console.log("Success"); diff --git a/packages/worker-relay/example/basic.ts b/packages/worker-relay/example/basic.ts new file mode 100644 index 00000000..1c8de1d2 --- /dev/null +++ b/packages/worker-relay/example/basic.ts @@ -0,0 +1,28 @@ +import { WorkerRelayInterface } from "@snort/worker-relay"; + +// in debug mode you may need this, to map to the correct sqlite-wasm path +// this is needed because sqlite-wasm will otherwise look inside @snort/worker-relay directory for sqlite3.wasm +const basePath = new URL("@sqlite.org/sqlite-wasm", import.meta.url); + +// internally we resolve the script path like this: +const scriptPath = new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url); + +// scriptPath & basePath are optional +const relay = new WorkerRelayInterface(scriptPath, basePath.href); + +// load sqlite database and run migrations +await relay.init("my-relay.db"); + +// Query worker relay with regular nostr REQ command +const results = await relay.query(["REQ", "1", { kinds: [1], limit: 10 }]); + +// publish a new event to the relay +const myEvent = { + kind: 1, + created_at: Math.floor(new Date().getTime() / 1000), + content: "test", + tags: [] +}; +if (await relay.event(myEvent)) { + console.log("Success"); +} \ No newline at end of file diff --git a/packages/worker-relay/package.json b/packages/worker-relay/package.json index 43040db7..3272d3c7 100644 --- a/packages/worker-relay/package.json +++ b/packages/worker-relay/package.json @@ -1,6 +1,6 @@ { "name": "@snort/worker-relay", - "version": "1.0.5", + "version": "1.0.6", "description": "A nostr relay in a service worker", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -17,14 +17,16 @@ "dist" ], "dependencies": { - "@sqlite.org/sqlite-wasm": "^3.45.1-build1", "eventemitter3": "^5.0.1", "uuid": "^9.0.1" }, + "peerDependencies": { + "@sqlite.org/sqlite-wasm": "^3.45.1-build1" + }, "devDependencies": { "@types/debug": "^4.1.12", "@types/uuid": "^9.0.7", "esbuild": "^0.20.1", "typescript": "^5.2.2" } -} +} \ No newline at end of file diff --git a/packages/worker-relay/src/interface.ts b/packages/worker-relay/src/interface.ts index 0da99608..dce01492 100644 --- a/packages/worker-relay/src/interface.ts +++ b/packages/worker-relay/src/interface.ts @@ -11,17 +11,16 @@ export class WorkerRelayInterface { /** * Interface wrapper for worker relay - * @param path Path to worker script or Worker script object + * @param scriptPath Path to worker script or Worker script object * @param sqlite3Dir Directory to search for sqlite3 depends */ - constructor(path?: string | Worker, sqlite3Dir?: string) { - if (path instanceof Worker) { - this.#worker = path; + constructor(scriptPath?: string | URL | Worker, sqlite3Dir?: string) { + this.#sqliteDir = sqlite3Dir; + if (scriptPath instanceof Worker) { + this.#worker = scriptPath; } else { - const sqliteBase = new URL("@sqlite.org/sqlite-wasm?url", import.meta.url); - this.#sqliteDir = sqlite3Dir ?? sqliteBase.href; - const scriptPath = path ? new URL(path) : new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url); - this.#worker = new Worker(scriptPath, { type: "module" }) + const sp = scriptPath ? scriptPath : new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url); + this.#worker = new Worker(sp, { type: "module" }) }; this.#worker.onerror = e => { console.error(e.message, e);