From 459f3b98de6775965bcab15d4078a49a6b77ca39 Mon Sep 17 00:00:00 2001 From: ennmichael Date: Thu, 2 Mar 2023 22:02:17 +0100 Subject: [PATCH] prefer string literals to enums when possible --- packages/nostr/src/client/conn.ts | 31 +++++++++++----------------- packages/nostr/src/client/emitter.ts | 2 +- packages/nostr/src/client/index.ts | 14 ++++++------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/packages/nostr/src/client/conn.ts b/packages/nostr/src/client/conn.ts index 3019710..145f88f 100644 --- a/packages/nostr/src/client/conn.ts +++ b/packages/nostr/src/client/conn.ts @@ -106,16 +106,13 @@ export class Conn { */ export type IncomingMessage = IncomingEvent | IncomingNotice -export const enum IncomingKind { - Event, - Notice, -} +export type IncomingKind = "event" | "notice" /** * Incoming "EVENT" message. */ export interface IncomingEvent { - kind: IncomingKind.Event + kind: "event" subscriptionId: SubscriptionId signed: SignedEvent raw: RawEvent @@ -125,7 +122,7 @@ export interface IncomingEvent { * Incoming "NOTICE" message. */ export interface IncomingNotice { - kind: IncomingKind.Notice + kind: "notice" notice: string } @@ -137,17 +134,13 @@ export type OutgoingMessage = | OutgoingOpenSubscription | OutgoingCloseSubscription -export const enum OutgoingKind { - Event, - OpenSubscription, - CloseSubscription, -} +export type OutgoingKind = "event" | "openSubscription" | "closeSubscription" /** * Outgoing "EVENT" message. */ export interface OutgoingEvent { - kind: OutgoingKind.Event + kind: "event" event: SignedEvent | RawEvent } @@ -155,7 +148,7 @@ export interface OutgoingEvent { * Outgoing "REQ" message, which opens a subscription. */ export interface OutgoingOpenSubscription { - kind: OutgoingKind.OpenSubscription + kind: "openSubscription" id: SubscriptionId filters: Filters[] } @@ -164,7 +157,7 @@ export interface OutgoingOpenSubscription { * Outgoing "CLOSE" message, which closes a subscription. */ export interface OutgoingCloseSubscription { - kind: OutgoingKind.CloseSubscription + kind: "closeSubscription" id: SubscriptionId } @@ -203,7 +196,7 @@ async function parseIncomingMessage(data: string): Promise { } const raw = parseEventData(json[2]) return { - kind: IncomingKind.Event, + kind: "event", subscriptionId: new SubscriptionId(json[1]), signed: await SignedEvent.verify(raw), raw, @@ -216,7 +209,7 @@ async function parseIncomingMessage(data: string): Promise { ) } return { - kind: IncomingKind.Notice, + kind: "notice", notice: json[1], } } @@ -224,17 +217,17 @@ async function parseIncomingMessage(data: string): Promise { } function serializeOutgoingMessage(msg: OutgoingMessage): string { - if (msg.kind === OutgoingKind.Event) { + if (msg.kind === "event") { const raw = msg.event instanceof SignedEvent ? msg.event.serialize() : msg.event return JSON.stringify(["EVENT", raw]) - } else if (msg.kind === OutgoingKind.OpenSubscription) { + } else if (msg.kind === "openSubscription") { return JSON.stringify([ "REQ", msg.id.toString(), ...serializeFilters(msg.filters), ]) - } else if (msg.kind === OutgoingKind.CloseSubscription) { + } else if (msg.kind === "closeSubscription") { return JSON.stringify(["CLOSE", msg.id.toString()]) } else { throw new Error(`invalid message: ${JSON.stringify(msg)}`) diff --git a/packages/nostr/src/client/emitter.ts b/packages/nostr/src/client/emitter.ts index 3c98021..f209c0b 100644 --- a/packages/nostr/src/client/emitter.ts +++ b/packages/nostr/src/client/emitter.ts @@ -44,7 +44,7 @@ export class EventEmitter extends Base { override off(eventName: "event", listener: EventListener): this override off(eventName: "notice", listener: NoticeListener): this override off(eventName: "error", listener: ErrorListener): this - override off(eventName: string, listener: Listener): this { + override off(eventName: EventName, listener: Listener): this { return super.off(eventName, listener) } diff --git a/packages/nostr/src/client/index.ts b/packages/nostr/src/client/index.ts index 48be246..328d590 100644 --- a/packages/nostr/src/client/index.ts +++ b/packages/nostr/src/client/index.ts @@ -1,7 +1,7 @@ import { ProtocolError } from "../error" import { EventId, Event, EventKind, SignedEvent, RawEvent } from "../event" import { PrivateKey, PublicKey } from "../keypair" -import { Conn, IncomingKind, OutgoingKind } from "./conn" +import { Conn } from "./conn" import * as secp from "@noble/secp256k1" import { EventEmitter } from "./emitter" @@ -52,7 +52,7 @@ export class Nostr extends EventEmitter { // Handle messages on this connection. conn.on("message", async (msg) => { try { - if (msg.kind === IncomingKind.Event) { + if (msg.kind === "event") { this.emit( "event", { @@ -62,7 +62,7 @@ export class Nostr extends EventEmitter { }, this ) - } else if (msg.kind === IncomingKind.Notice) { + } else if (msg.kind === "notice") { this.emit("notice", msg.notice, this) } else { throw new ProtocolError(`invalid message ${msg}`) @@ -81,7 +81,7 @@ export class Nostr extends EventEmitter { for (const [key, filters] of this.#subscriptions.entries()) { const subscriptionId = new SubscriptionId(key) conn.send({ - kind: OutgoingKind.OpenSubscription, + kind: "openSubscription", id: subscriptionId, filters, }) @@ -145,7 +145,7 @@ export class Nostr extends EventEmitter { continue } conn.send({ - kind: OutgoingKind.OpenSubscription, + kind: "openSubscription", id: subscriptionId, filters, }) @@ -167,7 +167,7 @@ export class Nostr extends EventEmitter { continue } conn.send({ - kind: OutgoingKind.CloseSubscription, + kind: "closeSubscription", id: subscriptionId, }) } @@ -210,7 +210,7 @@ export class Nostr extends EventEmitter { event = await SignedEvent.sign(event, key) } conn.send({ - kind: OutgoingKind.Event, + kind: "event", event, }) }