Skip to content

Commit

Permalink
refactor(nips/01): abondan enum for EventKind
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Oct 4, 2023
1 parent 0d96936 commit 131e952
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 41 deletions.
3 changes: 2 additions & 1 deletion core/clients_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
EventKind,
EventMessage,
NostrEvent,
OkMessage,
Expand Down Expand Up @@ -57,7 +58,7 @@ describe("Client", () => {
});
it("should receive requests", async () => {
subid = "test" as SubscriptionId;
const req = { kinds: [0] };
const req = { kinds: [EventKind[0]] };
ws.dispatchEvent(
new MessageEvent("message", {
data: JSON.stringify(["REQ", subid, req]),
Expand Down
77 changes: 51 additions & 26 deletions core/nips/01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,42 +139,67 @@ export type SubscriptionFilter<
// Basic event kinds
// ----------------------

export enum EventKind {
Metadata = 0,
TextNote = 1,
}
export type EventKind<T extends number = number> = Brand<T, "EventKind">;

export type RegularEventKind = Brand<EventKind, "RegularEventKind">;
export type ReplaceableEventKind = Brand<EventKind, "ReplaceableEventKind">;
export type EphemeralEventKind = Brand<EventKind, "EphemeralEventKind">;
export type ParameterizedReplaceableEventKind = Brand<
EventKind,
"ParameterizedReplaceableEventKind"
>;
export type MetadataEvent = NostrEvent<EventKind<0>>;
export type TextNoteEvent = NostrEvent<EventKind<1>>;

// TODO: Use template literal for T

// deno-lint-ignore no-namespace
export namespace EventKind {
export function isRegularEventKind(
kind: EventKind | number,
export type RegularEventKind<T extends number = number> = Brand<
T,
"EventKind",
"Regular"
>;
export type ReplaceableEventKind<T extends number = number> = Brand<
T,
"EventKind",
"Replaceable"
>;
export type EphemeralEventKind<T extends number = number> = Brand<
T,
"EventKind",
"Ephemeral"
>;
export type ParameterizedReplaceableEventKind<T extends number = number> =
Brand<
T,
"EventKind",
"ParameterizedReplaceable"
>;

export const EventKind = {
0: 0 as EventKind<0>,
Metadata: 0 as EventKind<0>,

1: 1 as EventKind<1>,
TextNote: 1 as EventKind<1>,

$<T extends number>(kind: T): EventKind<T> {
return kind as EventKind<T>;
},

isRegularEventKind(
kind: EventKind,
): kind is RegularEventKind {
return 1000 <= kind && kind < 10000;
}
export function isReplaceableEventKind(
kind: EventKind | number,
},
isReplaceableEventKind(
kind: EventKind,
): kind is ReplaceableEventKind {
return (10000 <= kind && kind < 20000) || kind === 0 || kind === 3;
}
export function isEphemeralEventKind(
kind: EventKind | number,
},
isEphemeralEventKind(
kind: EventKind,
): kind is EphemeralEventKind {
return 20000 <= kind && kind < 30000;
}
export function isParameterizedReplaceableEventKind(
kind: EventKind | number,
},
isParameterizedReplaceableEventKind(
kind: EventKind,
): kind is ParameterizedReplaceableEventKind {
return 30000 <= kind && kind < 40000;
}
}
},
};

export type EventContent = [
MetadataContent,
Expand Down
18 changes: 11 additions & 7 deletions core/relays_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
EventId,
NostrEvent,
EventKind,
MetadataEvent,
OkMessage,
PublishMessage,
TextNoteEvent,
} from "../core/nips/01.ts";
import { EventRejected, Relay, RelayClosed } from "./relays.ts";
import { afterAll, beforeAll, describe, it } from "../lib/std/testing.ts";
Expand Down Expand Up @@ -74,8 +76,8 @@ describe("Relay constructor", () => {
describe("Relay", () => {
const url = "wss://localhost:8080";
let relay: Relay;
let sub_0: ReadableStream<NostrEvent<0>>;
let sub_1: ReadableStream<NostrEvent<1>>;
let sub_0: ReadableStream<MetadataEvent>;
let sub_1: ReadableStream<TextNoteEvent>;

beforeAll(() => {
globalThis.WebSocket = MockWebSocket;
Expand All @@ -91,7 +93,7 @@ describe("Relay", () => {
assertEquals(relay.status, WebSocket.CLOSED);
});
it("should not connect when a subscription is created", () => {
sub_1 = relay.subscribe({ kinds: [1] }, { id: "test-1" });
sub_1 = relay.subscribe({ kinds: [EventKind[1]] }, { id: "test-1" });
assert(sub_1 instanceof ReadableStream);
assertEquals(relay.status, WebSocket.CLOSED);
});
Expand All @@ -110,7 +112,9 @@ describe("Relay", () => {
reader.releaseLock();
});
it("should be able to open multiple subscriptions", () => {
sub_0 = relay.subscribe({ kinds: [0], limit: 1 }, { id: "test-0" });
sub_0 = relay.subscribe({ kinds: [EventKind[0]], limit: 1 }, {
id: "test-0",
});
assert(sub_0 instanceof ReadableStream);
});
it("should recieve metas and notes simultaneously", async () => {
Expand Down Expand Up @@ -146,7 +150,7 @@ describe("Relay", () => {
ws.remote.addEventListener(
"message",
(ev: MessageEvent<string>) => {
const [, event] = JSON.parse(ev.data) as PublishMessage<1>;
const [, event] = JSON.parse(ev.data) as PublishMessage<EventKind<1>>;
if (event.id === eid) {
assertEquals(event.kind, 1);
resolve(true);
Expand All @@ -168,7 +172,7 @@ describe("Relay", () => {
ws.remote.addEventListener(
"message",
(ev: MessageEvent<string>) => {
const [, event] = JSON.parse(ev.data) as PublishMessage<1>;
const [, event] = JSON.parse(ev.data) as PublishMessage<EventKind<1>>;
if (event.id === eid) {
assertEquals(event.kind, 1);
resolve(true);
Expand Down
3 changes: 2 additions & 1 deletion core/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Constructor of branded types
*/
export type Brand<K, T> = K & { __brand: T };
// deno-lint-ignore no-explicit-any
export type Brand<T, B, K = any> = T & { __brand: B; __kind: K };

//
// Records and maps
Expand Down
2 changes: 1 addition & 1 deletion lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
Stringified,
Tag,
} from "../core/nips/01.ts";
import type { RelayLike } from "../client.ts";
import type { RelayLike } from "../core/relays.ts";

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

Expand Down
4 changes: 2 additions & 2 deletions lib/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventKind, NostrEvent, RelayUrl, TagName } from "../core/nips/01.ts";
import type { Optional } from "../core/types.ts";
import { EventInit } from "./events.ts";

export type TextNote = EventInit<EventKind.TextNote>;
export type TextNote = EventInit<EventKind<1>>;

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

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

return { ...init, kind: EventKind.TextNote, tags };
return { ...init, kind: EventKind.$(1), tags };
}
}
7 changes: 6 additions & 1 deletion lib/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import type {
RelayUrl,
SubscriptionFilter,
} from "../core/nips/01.ts";
import { Relay, RelayInit, RelayLike, SubscriptionOptions } from "../client.ts";
import {
Relay,
RelayInit,
RelayLike,
SubscriptionOptions,
} from "../core/relays.ts";
import { NonExclusiveWritableStream } from "../core/streams.ts";
import { Distinctor, merge } from "../lib/streams.ts";

Expand Down
3 changes: 2 additions & 1 deletion lib/signs_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventKind } from "../core/nips/01.ts";
import { describe, it } from "../lib/std/testing.ts";
import { assert, assertEquals } from "../lib/std/assert.ts";
import { Timestamp } from "../lib/times.ts";
Expand All @@ -24,7 +25,7 @@ describe("Signer/Verifier", () => {
const event = {
pubkey: PublicKey.from(nsec),
created_at: Timestamp.now,
kind: 1,
kind: EventKind[1],
tags: [],
content: "lophus",
};
Expand Down
1 change: 1 addition & 0 deletions lib/std/streams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mergeReadableStreams } from "https://deno.land/[email protected]/streams/mod.ts";
2 changes: 1 addition & 1 deletion lib/streams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { mergeReadableStreams as merge } from "https://deno.land/std@0.203.0/streams/mod.ts";
export { mergeReadableStreams as merge } from "./std/streams.ts";

/**
* TransformStream which filters out duplicate values from a stream.
Expand Down

0 comments on commit 131e952

Please sign in to comment.