prefer string literals to enums when possible

This commit is contained in:
ennmichael 2023-03-02 22:02:17 +01:00
parent 6ca4ab71b2
commit 459f3b98de
No known key found for this signature in database
GPG Key ID: 6E6E183431A26AF7
3 changed files with 20 additions and 27 deletions

View File

@ -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<IncomingMessage> {
}
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<IncomingMessage> {
)
}
return {
kind: IncomingKind.Notice,
kind: "notice",
notice: json[1],
}
}
@ -224,17 +217,17 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
}
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)}`)

View File

@ -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)
}

View File

@ -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,
})
}