Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor nips #9

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ edge environments.

## Documentation

- [API Reference](https://deno.land/x/lophus) (WIP)
- [API Reference](https://deno.land/x/lophus/mod.ts) (WIP)
- [Lophus by Examples](https://github.com/hasundue/lophus-by-example)

## Supported NIPs
Expand Down
4 changes: 2 additions & 2 deletions relay.ts → core/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type {
RelayToClientMessage,
SubscriptionFilter,
SubscriptionId,
} from "./core/nips/01.ts";
import { NostrNode, NostrNodeConfig } from "./core/nodes.ts";
} from "./nips/01.ts";
import { NostrNode, NostrNodeConfig } from "./nodes.ts";

/**
* A class that represents a remote Nostr client.
Expand Down
5 changes: 3 additions & 2 deletions tests/relay_test.ts → core/clients_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
EventKind,
EventMessage,
NostrEvent,
OkMessage,
SubscriptionId,
} from "../core/nips/01.ts";
import { Client } from "../relay.ts";
import { Client } from "./clients.ts";
import { afterAll, beforeAll, describe, it } from "../lib/std/testing.ts";
import { assert, assertEquals } from "../lib/std/assert.ts";
import { MockWebSocket } from "../lib/testing.ts";
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 @@
// 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>;
},

Check warning on line 180 in core/nips/01.ts

View check run for this annotation

Codecov / codecov/patch

core/nips/01.ts#L178-L180

Added lines #L178 - L180 were not covered by tests

isRegularEventKind(
kind: EventKind,

Check warning on line 183 in core/nips/01.ts

View check run for this annotation

Codecov / codecov/patch

core/nips/01.ts#L182-L183

Added lines #L182 - L183 were not covered by tests
): kind is RegularEventKind {
return 1000 <= kind && kind < 10000;
}
export function isReplaceableEventKind(
kind: EventKind | number,
},
isReplaceableEventKind(
kind: EventKind,

Check warning on line 188 in core/nips/01.ts

View check run for this annotation

Codecov / codecov/patch

core/nips/01.ts#L186-L188

Added lines #L186 - L188 were not covered by tests
): kind is ReplaceableEventKind {
return (10000 <= kind && kind < 20000) || kind === 0 || kind === 3;
}
export function isEphemeralEventKind(
kind: EventKind | number,
},
isEphemeralEventKind(
kind: EventKind,

Check warning on line 193 in core/nips/01.ts

View check run for this annotation

Codecov / codecov/patch

core/nips/01.ts#L191-L193

Added lines #L191 - L193 were not covered by tests
): kind is EphemeralEventKind {
return 20000 <= kind && kind < 30000;
}
export function isParameterizedReplaceableEventKind(
kind: EventKind | number,
},
isParameterizedReplaceableEventKind(
kind: EventKind,

Check warning on line 198 in core/nips/01.ts

View check run for this annotation

Codecov / codecov/patch

core/nips/01.ts#L196-L198

Added lines #L196 - L198 were not covered by tests
): kind is ParameterizedReplaceableEventKind {
return 30000 <= kind && kind < 40000;
}
}
},

Check warning on line 201 in core/nips/01.ts

View check run for this annotation

Codecov / codecov/patch

core/nips/01.ts#L201

Added line #L201 was not covered by tests
};

export type EventContent = [
MetadataContent,
Expand Down
12 changes: 6 additions & 6 deletions client.ts → core/relays.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lock } from "./core/x/async.ts";
import type { PromiseCallbacks } from "./core/types.ts";
import { Lock } from "./x/async.ts";
import type { PromiseCallbacks } from "./types.ts";
import type {
ClientToRelayMessage,
EoseMessage,
Expand All @@ -12,10 +12,10 @@ import type {
RelayUrl,
SubscriptionFilter,
SubscriptionId,
} from "./core/nips/01.ts";
import { NostrNode, NostrNodeConfig } from "./core/nodes.ts";
import { NonExclusiveWritableStream } from "./core/streams.ts";
import { LazyWebSocket } from "./core/websockets.ts";
} from "./nips/01.ts";
import { NostrNode, NostrNodeConfig } from "./nodes.ts";
import { NonExclusiveWritableStream } from "./streams.ts";
import { LazyWebSocket } from "./websockets.ts";

export class EventRejected extends Error {}
export class RelayClosed extends Error {}
Expand Down
20 changes: 12 additions & 8 deletions tests/client_test.ts → core/relays_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
EventId,
NostrEvent,
EventKind,
MetadataEvent,
OkMessage,
PublishMessage,
TextNoteEvent,
} from "../core/nips/01.ts";
import { EventRejected, Relay, RelayClosed } from "../client.ts";
import { EventRejected, Relay, RelayClosed } from "./relays.ts";
import { afterAll, beforeAll, describe, it } from "../lib/std/testing.ts";
import {
assert,
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
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./core/relays.ts";
export * from "./core/clients.ts";