forked from Kieran/snort
prefer string literals to enums when possible
This commit is contained in:
parent
6ca4ab71b2
commit
459f3b98de
@ -106,16 +106,13 @@ export class Conn {
|
|||||||
*/
|
*/
|
||||||
export type IncomingMessage = IncomingEvent | IncomingNotice
|
export type IncomingMessage = IncomingEvent | IncomingNotice
|
||||||
|
|
||||||
export const enum IncomingKind {
|
export type IncomingKind = "event" | "notice"
|
||||||
Event,
|
|
||||||
Notice,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Incoming "EVENT" message.
|
* Incoming "EVENT" message.
|
||||||
*/
|
*/
|
||||||
export interface IncomingEvent {
|
export interface IncomingEvent {
|
||||||
kind: IncomingKind.Event
|
kind: "event"
|
||||||
subscriptionId: SubscriptionId
|
subscriptionId: SubscriptionId
|
||||||
signed: SignedEvent
|
signed: SignedEvent
|
||||||
raw: RawEvent
|
raw: RawEvent
|
||||||
@ -125,7 +122,7 @@ export interface IncomingEvent {
|
|||||||
* Incoming "NOTICE" message.
|
* Incoming "NOTICE" message.
|
||||||
*/
|
*/
|
||||||
export interface IncomingNotice {
|
export interface IncomingNotice {
|
||||||
kind: IncomingKind.Notice
|
kind: "notice"
|
||||||
notice: string
|
notice: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,17 +134,13 @@ export type OutgoingMessage =
|
|||||||
| OutgoingOpenSubscription
|
| OutgoingOpenSubscription
|
||||||
| OutgoingCloseSubscription
|
| OutgoingCloseSubscription
|
||||||
|
|
||||||
export const enum OutgoingKind {
|
export type OutgoingKind = "event" | "openSubscription" | "closeSubscription"
|
||||||
Event,
|
|
||||||
OpenSubscription,
|
|
||||||
CloseSubscription,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outgoing "EVENT" message.
|
* Outgoing "EVENT" message.
|
||||||
*/
|
*/
|
||||||
export interface OutgoingEvent {
|
export interface OutgoingEvent {
|
||||||
kind: OutgoingKind.Event
|
kind: "event"
|
||||||
event: SignedEvent | RawEvent
|
event: SignedEvent | RawEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +148,7 @@ export interface OutgoingEvent {
|
|||||||
* Outgoing "REQ" message, which opens a subscription.
|
* Outgoing "REQ" message, which opens a subscription.
|
||||||
*/
|
*/
|
||||||
export interface OutgoingOpenSubscription {
|
export interface OutgoingOpenSubscription {
|
||||||
kind: OutgoingKind.OpenSubscription
|
kind: "openSubscription"
|
||||||
id: SubscriptionId
|
id: SubscriptionId
|
||||||
filters: Filters[]
|
filters: Filters[]
|
||||||
}
|
}
|
||||||
@ -164,7 +157,7 @@ export interface OutgoingOpenSubscription {
|
|||||||
* Outgoing "CLOSE" message, which closes a subscription.
|
* Outgoing "CLOSE" message, which closes a subscription.
|
||||||
*/
|
*/
|
||||||
export interface OutgoingCloseSubscription {
|
export interface OutgoingCloseSubscription {
|
||||||
kind: OutgoingKind.CloseSubscription
|
kind: "closeSubscription"
|
||||||
id: SubscriptionId
|
id: SubscriptionId
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,7 +196,7 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
|
|||||||
}
|
}
|
||||||
const raw = parseEventData(json[2])
|
const raw = parseEventData(json[2])
|
||||||
return {
|
return {
|
||||||
kind: IncomingKind.Event,
|
kind: "event",
|
||||||
subscriptionId: new SubscriptionId(json[1]),
|
subscriptionId: new SubscriptionId(json[1]),
|
||||||
signed: await SignedEvent.verify(raw),
|
signed: await SignedEvent.verify(raw),
|
||||||
raw,
|
raw,
|
||||||
@ -216,7 +209,7 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
kind: IncomingKind.Notice,
|
kind: "notice",
|
||||||
notice: json[1],
|
notice: json[1],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,17 +217,17 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function serializeOutgoingMessage(msg: OutgoingMessage): string {
|
function serializeOutgoingMessage(msg: OutgoingMessage): string {
|
||||||
if (msg.kind === OutgoingKind.Event) {
|
if (msg.kind === "event") {
|
||||||
const raw =
|
const raw =
|
||||||
msg.event instanceof SignedEvent ? msg.event.serialize() : msg.event
|
msg.event instanceof SignedEvent ? msg.event.serialize() : msg.event
|
||||||
return JSON.stringify(["EVENT", raw])
|
return JSON.stringify(["EVENT", raw])
|
||||||
} else if (msg.kind === OutgoingKind.OpenSubscription) {
|
} else if (msg.kind === "openSubscription") {
|
||||||
return JSON.stringify([
|
return JSON.stringify([
|
||||||
"REQ",
|
"REQ",
|
||||||
msg.id.toString(),
|
msg.id.toString(),
|
||||||
...serializeFilters(msg.filters),
|
...serializeFilters(msg.filters),
|
||||||
])
|
])
|
||||||
} else if (msg.kind === OutgoingKind.CloseSubscription) {
|
} else if (msg.kind === "closeSubscription") {
|
||||||
return JSON.stringify(["CLOSE", msg.id.toString()])
|
return JSON.stringify(["CLOSE", msg.id.toString()])
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`invalid message: ${JSON.stringify(msg)}`)
|
throw new Error(`invalid message: ${JSON.stringify(msg)}`)
|
||||||
|
@ -44,7 +44,7 @@ export class EventEmitter extends Base {
|
|||||||
override off(eventName: "event", listener: EventListener): this
|
override off(eventName: "event", listener: EventListener): this
|
||||||
override off(eventName: "notice", listener: NoticeListener): this
|
override off(eventName: "notice", listener: NoticeListener): this
|
||||||
override off(eventName: "error", listener: ErrorListener): 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)
|
return super.off(eventName, listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { ProtocolError } from "../error"
|
import { ProtocolError } from "../error"
|
||||||
import { EventId, Event, EventKind, SignedEvent, RawEvent } from "../event"
|
import { EventId, Event, EventKind, SignedEvent, RawEvent } from "../event"
|
||||||
import { PrivateKey, PublicKey } from "../keypair"
|
import { PrivateKey, PublicKey } from "../keypair"
|
||||||
import { Conn, IncomingKind, OutgoingKind } from "./conn"
|
import { Conn } from "./conn"
|
||||||
import * as secp from "@noble/secp256k1"
|
import * as secp from "@noble/secp256k1"
|
||||||
import { EventEmitter } from "./emitter"
|
import { EventEmitter } from "./emitter"
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ export class Nostr extends EventEmitter {
|
|||||||
// Handle messages on this connection.
|
// Handle messages on this connection.
|
||||||
conn.on("message", async (msg) => {
|
conn.on("message", async (msg) => {
|
||||||
try {
|
try {
|
||||||
if (msg.kind === IncomingKind.Event) {
|
if (msg.kind === "event") {
|
||||||
this.emit(
|
this.emit(
|
||||||
"event",
|
"event",
|
||||||
{
|
{
|
||||||
@ -62,7 +62,7 @@ export class Nostr extends EventEmitter {
|
|||||||
},
|
},
|
||||||
this
|
this
|
||||||
)
|
)
|
||||||
} else if (msg.kind === IncomingKind.Notice) {
|
} else if (msg.kind === "notice") {
|
||||||
this.emit("notice", msg.notice, this)
|
this.emit("notice", msg.notice, this)
|
||||||
} else {
|
} else {
|
||||||
throw new ProtocolError(`invalid message ${msg}`)
|
throw new ProtocolError(`invalid message ${msg}`)
|
||||||
@ -81,7 +81,7 @@ export class Nostr extends EventEmitter {
|
|||||||
for (const [key, filters] of this.#subscriptions.entries()) {
|
for (const [key, filters] of this.#subscriptions.entries()) {
|
||||||
const subscriptionId = new SubscriptionId(key)
|
const subscriptionId = new SubscriptionId(key)
|
||||||
conn.send({
|
conn.send({
|
||||||
kind: OutgoingKind.OpenSubscription,
|
kind: "openSubscription",
|
||||||
id: subscriptionId,
|
id: subscriptionId,
|
||||||
filters,
|
filters,
|
||||||
})
|
})
|
||||||
@ -145,7 +145,7 @@ export class Nostr extends EventEmitter {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conn.send({
|
conn.send({
|
||||||
kind: OutgoingKind.OpenSubscription,
|
kind: "openSubscription",
|
||||||
id: subscriptionId,
|
id: subscriptionId,
|
||||||
filters,
|
filters,
|
||||||
})
|
})
|
||||||
@ -167,7 +167,7 @@ export class Nostr extends EventEmitter {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conn.send({
|
conn.send({
|
||||||
kind: OutgoingKind.CloseSubscription,
|
kind: "closeSubscription",
|
||||||
id: subscriptionId,
|
id: subscriptionId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -210,7 +210,7 @@ export class Nostr extends EventEmitter {
|
|||||||
event = await SignedEvent.sign(event, key)
|
event = await SignedEvent.sign(event, key)
|
||||||
}
|
}
|
||||||
conn.send({
|
conn.send({
|
||||||
kind: OutgoingKind.Event,
|
kind: "event",
|
||||||
event,
|
event,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user