Skip to content

Commit

Permalink
feat(relay): respond to EVENT messages with OK messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Oct 3, 2023
1 parent 3db9038 commit 09a3713
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
3 changes: 1 addition & 2 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ export class Relay extends NostrNode<ClientToRelayMessage> {
(resolve, reject) => {
this.#published.set(event.id, { resolve, reject });
},
).finally(async () => {
await this.#publisher.ready;
).finally(() => {
this.#published.delete(event.id);
});

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"check": "deno check ./**/*.ts",
"test": "deno test -A --no-check",
"build": "mkdir -p ./dist && deno run -A ./bin/bundle.ts",
"dev": "deno fmt && deno task -q check && deno task -q test"
"dev": "deno fmt && deno lint && deno task -q check && deno task -q test"
},
"exclude": [
"dist/",
Expand Down
44 changes: 29 additions & 15 deletions relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,47 @@ export class Client extends NostrNode<RelayToClientMessage> {
enqueueRequest = controller.enqueue.bind(controller);
},
});
this.ws.addEventListener("message", (ev: MessageEvent<string>) => {
const writer = this.getWriter();
this.ws.addEventListener("message", async (ev: MessageEvent<string>) => {
// TODO: Validate the type of the message.
const msg = JSON.parse(ev.data) as ClientToRelayMessage;

// TODO: Apply backpressure when a queue is full.

const kind = msg[0];
if (kind === "EVENT") {
return enqueueEvent(msg[1]);
const event = msg[1];

// TODO: Validate the event and send OkMessage<false> if necessary.

await writer.ready;
writer.write(["OK", event.id, true, ""]);
return enqueueEvent(event);
}
const id = msg[1];
const sid = msg[1];
if (kind === "CLOSE") {
const sub = this.subscriptions.get(id);
const sub = this.subscriptions.get(sid);
if (!sub) {
this.config.logger?.warn?.("Unknown subscription:", sid);
return;
}
this.subscriptions.delete(id);
this.subscriptions.delete(sid);
return sub.close();
}
// kind === "REQ"
const filter = msg[2];
const writer = this.getWriter();
const writable = new WritableStream<NostrEvent>({
write: (event) => {
return writer.write(["EVENT", id, event]);
},
});
this.subscriptions.set(id, writable);
return enqueueRequest([id, filter]);
if (kind === "REQ") {
const filter = msg[2];
this.subscriptions.set(
sid,
new WritableStream<NostrEvent>({
write: async (event) => {
await writer.ready;
return writer.write(["EVENT", sid, event]);
},
}),
);
return enqueueRequest([sid, filter]);
}
this.config.logger?.warn?.("Unknown message kind:", kind);
});
}

Expand Down
17 changes: 14 additions & 3 deletions tests/relay_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { EventMessage, NostrEvent, SubscriptionId } from "../core/nips/01.ts";
import {
EventMessage,
NostrEvent,
OkMessage,
SubscriptionId,
} from "../core/nips/01.ts";
import { Client } from "../relay.ts";
import { afterAll, beforeAll, describe, it } from "../lib/std/testing.ts";
import { assert, assertEquals } from "../lib/std/assert.ts";
Expand Down Expand Up @@ -27,8 +32,13 @@ describe("Client", () => {
it("should return a ReadableStream of events", () => {
assert(client.events instanceof ReadableStream);
});
it("should receive events", async () => {
const ev = { kind: 0 };
it("should receive an event and send a OK message", async () => {
const ev = { id: "test-ok", kind: 0 };
const received = new Promise<OkMessage>((resolve) => {
ws.remote.addEventListener("message", (ev: MessageEvent<string>) => {
resolve(JSON.parse(ev.data));
});
});
ws.dispatchEvent(
new MessageEvent("message", {
data: JSON.stringify(["EVENT", ev]),
Expand All @@ -37,6 +47,7 @@ describe("Client", () => {
const reader = client.events.getReader();
const { value } = await reader.read();
assertEquals(value, ev);
assertEquals(await received, ["OK", "test-ok", true, ""]);
});
});

Expand Down

0 comments on commit 09a3713

Please sign in to comment.