Skip to content

Commit 131e952

Browse files
committed
refactor(nips/01): abondan enum for EventKind
1 parent 0d96936 commit 131e952

10 files changed

+79
-41
lines changed

core/clients_test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
EventKind,
23
EventMessage,
34
NostrEvent,
45
OkMessage,
@@ -57,7 +58,7 @@ describe("Client", () => {
5758
});
5859
it("should receive requests", async () => {
5960
subid = "test" as SubscriptionId;
60-
const req = { kinds: [0] };
61+
const req = { kinds: [EventKind[0]] };
6162
ws.dispatchEvent(
6263
new MessageEvent("message", {
6364
data: JSON.stringify(["REQ", subid, req]),

core/nips/01.ts

+51-26
Original file line numberDiff line numberDiff line change
@@ -139,42 +139,67 @@ export type SubscriptionFilter<
139139
// Basic event kinds
140140
// ----------------------
141141

142-
export enum EventKind {
143-
Metadata = 0,
144-
TextNote = 1,
145-
}
142+
export type EventKind<T extends number = number> = Brand<T, "EventKind">;
146143

147-
export type RegularEventKind = Brand<EventKind, "RegularEventKind">;
148-
export type ReplaceableEventKind = Brand<EventKind, "ReplaceableEventKind">;
149-
export type EphemeralEventKind = Brand<EventKind, "EphemeralEventKind">;
150-
export type ParameterizedReplaceableEventKind = Brand<
151-
EventKind,
152-
"ParameterizedReplaceableEventKind"
153-
>;
144+
export type MetadataEvent = NostrEvent<EventKind<0>>;
145+
export type TextNoteEvent = NostrEvent<EventKind<1>>;
146+
147+
// TODO: Use template literal for T
154148

155-
// deno-lint-ignore no-namespace
156-
export namespace EventKind {
157-
export function isRegularEventKind(
158-
kind: EventKind | number,
149+
export type RegularEventKind<T extends number = number> = Brand<
150+
T,
151+
"EventKind",
152+
"Regular"
153+
>;
154+
export type ReplaceableEventKind<T extends number = number> = Brand<
155+
T,
156+
"EventKind",
157+
"Replaceable"
158+
>;
159+
export type EphemeralEventKind<T extends number = number> = Brand<
160+
T,
161+
"EventKind",
162+
"Ephemeral"
163+
>;
164+
export type ParameterizedReplaceableEventKind<T extends number = number> =
165+
Brand<
166+
T,
167+
"EventKind",
168+
"ParameterizedReplaceable"
169+
>;
170+
171+
export const EventKind = {
172+
0: 0 as EventKind<0>,
173+
Metadata: 0 as EventKind<0>,
174+
175+
1: 1 as EventKind<1>,
176+
TextNote: 1 as EventKind<1>,
177+
178+
$<T extends number>(kind: T): EventKind<T> {
179+
return kind as EventKind<T>;
180+
},
181+
182+
isRegularEventKind(
183+
kind: EventKind,
159184
): kind is RegularEventKind {
160185
return 1000 <= kind && kind < 10000;
161-
}
162-
export function isReplaceableEventKind(
163-
kind: EventKind | number,
186+
},
187+
isReplaceableEventKind(
188+
kind: EventKind,
164189
): kind is ReplaceableEventKind {
165190
return (10000 <= kind && kind < 20000) || kind === 0 || kind === 3;
166-
}
167-
export function isEphemeralEventKind(
168-
kind: EventKind | number,
191+
},
192+
isEphemeralEventKind(
193+
kind: EventKind,
169194
): kind is EphemeralEventKind {
170195
return 20000 <= kind && kind < 30000;
171-
}
172-
export function isParameterizedReplaceableEventKind(
173-
kind: EventKind | number,
196+
},
197+
isParameterizedReplaceableEventKind(
198+
kind: EventKind,
174199
): kind is ParameterizedReplaceableEventKind {
175200
return 30000 <= kind && kind < 40000;
176-
}
177-
}
201+
},
202+
};
178203

179204
export type EventContent = [
180205
MetadataContent,

core/relays_test.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
22
EventId,
3-
NostrEvent,
3+
EventKind,
4+
MetadataEvent,
45
OkMessage,
56
PublishMessage,
7+
TextNoteEvent,
68
} from "../core/nips/01.ts";
79
import { EventRejected, Relay, RelayClosed } from "./relays.ts";
810
import { afterAll, beforeAll, describe, it } from "../lib/std/testing.ts";
@@ -74,8 +76,8 @@ describe("Relay constructor", () => {
7476
describe("Relay", () => {
7577
const url = "wss://localhost:8080";
7678
let relay: Relay;
77-
let sub_0: ReadableStream<NostrEvent<0>>;
78-
let sub_1: ReadableStream<NostrEvent<1>>;
79+
let sub_0: ReadableStream<MetadataEvent>;
80+
let sub_1: ReadableStream<TextNoteEvent>;
7981

8082
beforeAll(() => {
8183
globalThis.WebSocket = MockWebSocket;
@@ -91,7 +93,7 @@ describe("Relay", () => {
9193
assertEquals(relay.status, WebSocket.CLOSED);
9294
});
9395
it("should not connect when a subscription is created", () => {
94-
sub_1 = relay.subscribe({ kinds: [1] }, { id: "test-1" });
96+
sub_1 = relay.subscribe({ kinds: [EventKind[1]] }, { id: "test-1" });
9597
assert(sub_1 instanceof ReadableStream);
9698
assertEquals(relay.status, WebSocket.CLOSED);
9799
});
@@ -110,7 +112,9 @@ describe("Relay", () => {
110112
reader.releaseLock();
111113
});
112114
it("should be able to open multiple subscriptions", () => {
113-
sub_0 = relay.subscribe({ kinds: [0], limit: 1 }, { id: "test-0" });
115+
sub_0 = relay.subscribe({ kinds: [EventKind[0]], limit: 1 }, {
116+
id: "test-0",
117+
});
114118
assert(sub_0 instanceof ReadableStream);
115119
});
116120
it("should recieve metas and notes simultaneously", async () => {
@@ -146,7 +150,7 @@ describe("Relay", () => {
146150
ws.remote.addEventListener(
147151
"message",
148152
(ev: MessageEvent<string>) => {
149-
const [, event] = JSON.parse(ev.data) as PublishMessage<1>;
153+
const [, event] = JSON.parse(ev.data) as PublishMessage<EventKind<1>>;
150154
if (event.id === eid) {
151155
assertEquals(event.kind, 1);
152156
resolve(true);
@@ -168,7 +172,7 @@ describe("Relay", () => {
168172
ws.remote.addEventListener(
169173
"message",
170174
(ev: MessageEvent<string>) => {
171-
const [, event] = JSON.parse(ev.data) as PublishMessage<1>;
175+
const [, event] = JSON.parse(ev.data) as PublishMessage<EventKind<1>>;
172176
if (event.id === eid) {
173177
assertEquals(event.kind, 1);
174178
resolve(true);

core/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Constructor of branded types
33
*/
4-
export type Brand<K, T> = K & { __brand: T };
4+
// deno-lint-ignore no-explicit-any
5+
export type Brand<T, B, K = any> = T & { __brand: B; __kind: K };
56

67
//
78
// Records and maps

lib/events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
Stringified,
77
Tag,
88
} from "../core/nips/01.ts";
9-
import type { RelayLike } from "../client.ts";
9+
import type { RelayLike } from "../core/relays.ts";
1010

1111
export { EventKind } from "../core/nips/01.ts";
1212

lib/notes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EventKind, NostrEvent, RelayUrl, TagName } from "../core/nips/01.ts";
22
import type { Optional } from "../core/types.ts";
33
import { EventInit } from "./events.ts";
44

5-
export type TextNote = EventInit<EventKind.TextNote>;
5+
export type TextNote = EventInit<EventKind<1>>;
66

77
export type TextNoteInit = Optional<TextNote, "kind">;
88

@@ -29,6 +29,6 @@ export class TextNoteComposer extends TransformStream<TextNoteInit, TextNote> {
2929
["p" as TagName, opts.replyTo.pubkey, relayRecommend ?? ""],
3030
] : []);
3131

32-
return { ...init, kind: EventKind.TextNote, tags };
32+
return { ...init, kind: EventKind.$(1), tags };
3333
}
3434
}

lib/pools.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import type {
44
RelayUrl,
55
SubscriptionFilter,
66
} from "../core/nips/01.ts";
7-
import { Relay, RelayInit, RelayLike, SubscriptionOptions } from "../client.ts";
7+
import {
8+
Relay,
9+
RelayInit,
10+
RelayLike,
11+
SubscriptionOptions,
12+
} from "../core/relays.ts";
813
import { NonExclusiveWritableStream } from "../core/streams.ts";
914
import { Distinctor, merge } from "../lib/streams.ts";
1015

lib/signs_test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventKind } from "../core/nips/01.ts";
12
import { describe, it } from "../lib/std/testing.ts";
23
import { assert, assertEquals } from "../lib/std/assert.ts";
34
import { Timestamp } from "../lib/times.ts";
@@ -24,7 +25,7 @@ describe("Signer/Verifier", () => {
2425
const event = {
2526
pubkey: PublicKey.from(nsec),
2627
created_at: Timestamp.now,
27-
kind: 1,
28+
kind: EventKind[1],
2829
tags: [],
2930
content: "lophus",
3031
};

lib/std/streams.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { mergeReadableStreams } from "https://deno.land/std@0.203.0/streams/mod.ts";

lib/streams.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { mergeReadableStreams as merge } from "https://deno.land/std@0.203.0/streams/mod.ts";
1+
export { mergeReadableStreams as merge } from "./std/streams.ts";
22

33
/**
44
* TransformStream which filters out duplicate values from a stream.

0 commit comments

Comments
 (0)