From c65596ff3e52c7912de7785e5742072b6ef3808e Mon Sep 17 00:00:00 2001 From: hasundue Date: Mon, 12 Jun 2023 15:20:39 +0900 Subject: [PATCH] refactor(lib): merge agents.ts and profiles.ts into streams.ts --- docs/EXAMPLES.md | 10 ++++------ docs/src/examples/echo.ts | 6 ++---- docs/src/examples/publish.ts | 4 ++-- lib/agents.ts | 14 -------------- lib/profiles.ts | 28 ---------------------------- lib/streams.ts | 16 +++++++++++++++- 6 files changed, 23 insertions(+), 55 deletions(-) delete mode 100644 lib/agents.ts delete mode 100644 lib/profiles.ts diff --git a/docs/EXAMPLES.md b/docs/EXAMPLES.md index 5b23c09..8bf91b2 100644 --- a/docs/EXAMPLES.md +++ b/docs/EXAMPLES.md @@ -26,14 +26,14 @@ new RelayPool("wss://nos.lol", "wss://relay.nostr.band") ```ts import { Relay } from "../../../client.ts"; -import { EventKind, EventPublisher } from "../../../lib/events.ts"; +import { EventPublisher } from "../../../lib/events.ts"; import { env } from "../../../lib/env.ts"; const relay = new Relay("wss://nos.lol"); new EventPublisher(relay, env.PRIVATE_KEY) .publish({ - kind: EventKind.TextNote, + kind: 1, content: "Hello, Nostr! This is Lophus, yet another JS/TS library for Nostr!", }) @@ -44,16 +44,14 @@ new EventPublisher(relay, env.PRIVATE_KEY) ```ts import { Relay } from "../../../client.ts"; -import { DefaultAgent } from "../../../lib/agents.ts"; +import { Transformer } from "../../../lib/streams.ts"; import { EventPublisher } from "../../../lib/events.ts"; -import { TextNoteComposer } from "../../../lib/notes.ts"; import { env } from "../../../lib/env.ts"; const relay = new Relay("wss://nostr-dev.wellorder.net"); relay.subscribe({ kinds: [1], "#p": [env.PUBLIC_KEY] }) - .pipeThrough(new DefaultAgent((ev) => ({ content: ev.content }))) - .pipeThrough(new TextNoteComposer()) + .pipeThrough(new Transformer((ev) => ({ kind: 1, content: ev.content }))) .pipeTo(new EventPublisher(relay, env.PRIVATE_KEY)); ``` diff --git a/docs/src/examples/echo.ts b/docs/src/examples/echo.ts index 941a58f..36afc2f 100644 --- a/docs/src/examples/echo.ts +++ b/docs/src/examples/echo.ts @@ -1,13 +1,11 @@ // Echo bot import { Relay } from "../../../client.ts"; -import { DefaultAgent } from "../../../lib/agents.ts"; +import { Transformer } from "../../../lib/streams.ts"; import { EventPublisher } from "../../../lib/events.ts"; -import { TextNoteComposer } from "../../../lib/notes.ts"; import { env } from "../../../lib/env.ts"; const relay = new Relay("wss://nostr-dev.wellorder.net"); relay.subscribe({ kinds: [1], "#p": [env.PUBLIC_KEY] }) - .pipeThrough(new DefaultAgent((ev) => ({ content: ev.content }))) - .pipeThrough(new TextNoteComposer()) + .pipeThrough(new Transformer((ev) => ({ kind: 1, content: ev.content }))) .pipeTo(new EventPublisher(relay, env.PRIVATE_KEY)); diff --git a/docs/src/examples/publish.ts b/docs/src/examples/publish.ts index a252279..5a01a38 100644 --- a/docs/src/examples/publish.ts +++ b/docs/src/examples/publish.ts @@ -1,13 +1,13 @@ // Publish a text note import { Relay } from "../../../client.ts"; -import { EventKind, EventPublisher } from "../../../lib/events.ts"; +import { EventPublisher } from "../../../lib/events.ts"; import { env } from "../../../lib/env.ts"; const relay = new Relay("wss://nos.lol"); new EventPublisher(relay, env.PRIVATE_KEY) .publish({ - kind: EventKind.TextNote, + kind: 1, content: "Hello, Nostr! This is Lophus, yet another JS/TS library for Nostr!", }) diff --git a/lib/agents.ts b/lib/agents.ts deleted file mode 100644 index cf56465..0000000 --- a/lib/agents.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { NostrEvent } from "../core/types.ts"; - -export class DefaultAgent extends TransformStream { - constructor(fn: (event: NostrEvent) => T) { - super({ - transform(event, controller) { - const result = fn(event); - if (result) { - controller.enqueue(result); - } - }, - }); - } -} diff --git a/lib/profiles.ts b/lib/profiles.ts deleted file mode 100644 index a47a2b6..0000000 --- a/lib/profiles.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { EventKind, MetadataContent, Optional } from "../core/types.ts"; -import { EventInit } from "./events.ts"; - -export type ProfileContentTemplate = Optional< - MetadataContent, - "about" | "picture" ->; - -export type ProfileEvent = EventInit; - -export class ProfileComposer - extends TransformStream { - constructor() { - super({ - transform: (template, controller) => - controller.enqueue(this.compose(template)), - }); - } - compose( - template: ProfileContentTemplate, - ): EventInit { - return { - kind: EventKind.Metadata, - tags: [], - content: { about: "", picture: "", ...template }, - }; - } -} diff --git a/lib/streams.ts b/lib/streams.ts index 160320e..f832fa9 100644 --- a/lib/streams.ts +++ b/lib/streams.ts @@ -3,7 +3,7 @@ export { mergeReadableStreams as merge } from "https://deno.land/std@0.187.0/str /** * TransformStream which filters out duplicate values from a stream. */ -export class Distinctor +export class Distinctor extends TransformStream { #seen: Set; @@ -21,3 +21,17 @@ export class Distinctor this.#seen = new Set(); } } + +export class Transformer + extends TransformStream { + constructor(fn: (chunk: R) => W) { + super({ + transform(event, controller) { + const result = fn(event); + if (result) { + controller.enqueue(result); + } + }, + }); + } +}