Skip to content

Commit db1ba3b

Browse files
committed
build: create lib and std modules
1 parent 7b25d39 commit db1ba3b

25 files changed

+146
-103
lines changed

core/clients_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert } from "@std/assert";
22
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
3-
import { MockWebSocket } from "../lib/testing.ts";
3+
import { MockWebSocket } from "@lophus/lib/testing";
44
import { Client } from "./clients.ts";
55

66
describe("Client", () => {

core/deno.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
22
"name": "@lophus/core",
33
"version": "0.0.14",
4-
"exports": "./mod.ts",
5-
6-
"publish": {
7-
"exclude": [
8-
"deno.json",
9-
"*_test.ts"
10-
]
4+
"exports": {
5+
".": "./mod.ts",
6+
"./clients": "./clients.ts",
7+
"./protocol": "./protocol.ts",
8+
"./relays": "./relays.ts"
119
}
1210
}

core/nodes_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals } from "@std/assert";
22
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
3-
import { MockWebSocket } from "../lib/testing.ts";
3+
import { MockWebSocket } from "@lophus/lib/testing";
44
import { NostrNode, NostrNodeBase } from "./nodes.ts";
55

66
describe("NostrNodeBase", () => {

core/protocol.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @module
1010
*/
1111

12-
import type { AlphabetLetter, Brand, Stringified } from "../lib/types.ts";
12+
import type { AlphabetLetter, Brand, Stringified } from "@lophus/lib/types";
1313

1414
// ----------------------
1515
// Extendable interfaces

core/relays.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Stringified } from "../lib/types.ts";
1+
import type { Stringified } from "@lophus/lib/types";
22
import type {
33
ClientToRelayMessage,
44
EventId,

core/websockets_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert, assertEquals } from "@std/assert";
22
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
3-
import { MockWebSocket } from "../lib/testing.ts";
3+
import { MockWebSocket } from "@lophus/lib/testing";
44
import { LazyWebSocket } from "./websockets.ts";
55

66
describe("LazyWebSocket", () => {

deno.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
"imports": {
66
"@noble/curves": "npm:@noble/curves@^1.3.0",
77
"@noble/hashes": "npm:@noble/hashes@^1.3.3",
8+
"@lophus/core/clients": "./core/clients.ts",
9+
"@lophus/core/protocol": "./core/protocol.ts",
10+
"@lophus/core/relays": "./core/relays.ts",
11+
"@lophus/lib/types": "./lib/types.ts",
12+
"@lophus/lib/testing": "./lib/testing.ts",
13+
"@lophus/lib/streams": "./lib/streams.ts",
14+
"@lophus/std/env": "./std/env.ts",
15+
"@lophus/std/events": "./std/events.ts",
16+
"@lophus/std/notes": "./std/notes.ts",
17+
"@lophus/std/pools": "./std/pools.ts",
18+
"@lophus/std/signs": "./std/signs.ts",
19+
"@lophus/std/times": "./std/times.ts",
820
"@std/assert": "jsr:@std/assert@^0.219.1",
921
"@std/dotenv": "jsr:@std/dotenv@^0.219.1",
1022
"@std/streams": "jsr:@std/streams@^0.219.1",
@@ -21,6 +33,8 @@
2133
"update:commit": "deno task -q update --commit --prefix 'build(deps):' --prefix-lock 'build(lock):'"
2234
},
2335
"workspaces": [
24-
"./core"
36+
"./core",
37+
"./lib",
38+
"./std"
2539
]
2640
}

deno.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/deno.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@lophus/lib",
3+
"version": "0.0.14",
4+
"exports": {
5+
"./streams": "./streams.ts",
6+
"./testing": "./testing.ts",
7+
"./types": "./types.ts"
8+
}
9+
}

lib/streams.ts

+19
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@ export class Transformer<R = unknown, W = unknown>
3232
});
3333
}
3434
}
35+
36+
export type LogLevel = "error" | "warn" | "info" | "debug";
37+
38+
export interface ConsoleLoggerOptions {
39+
level?: LogLevel;
40+
}
41+
42+
export class ConsoleLogger<W = unknown> extends WritableStream<W> {
43+
readonly level: LogLevel;
44+
constructor(options?: ConsoleLoggerOptions) {
45+
const level = options?.level ?? "info";
46+
super({
47+
write(chunk) {
48+
console[level](chunk);
49+
},
50+
});
51+
this.level = level;
52+
}
53+
}

lib/streams_test.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { assertArrayIncludes, assertEquals } from "@std/assert";
2-
import { describe, it } from "@std/testing/bdd";
3-
import { Distinctor } from "./streams.ts";
1+
import { assert, assertArrayIncludes, assertEquals } from "@std/assert";
2+
import { beforeEach, describe, it } from "@std/testing/bdd";
3+
import { ConsoleLogger, Distinctor } from "@lophus/lib/streams";
44

55
describe("Distinctor", () => {
66
it("filters out duplicate values from a stream", async () => {
@@ -20,3 +20,28 @@ describe("Distinctor", () => {
2020
assertArrayIncludes(values, [1, 2, 3]);
2121
});
2222
});
23+
24+
describe("ConsoleLogger", () => {
25+
let output = "";
26+
27+
beforeEach(() => {
28+
globalThis.console = new Proxy(globalThis.console, {
29+
get: () => {
30+
return (data: unknown) => {
31+
output += data;
32+
};
33+
},
34+
});
35+
});
36+
37+
it("should be a writable stream", () => {
38+
const logger = new ConsoleLogger();
39+
assert(logger instanceof WritableStream);
40+
});
41+
42+
it("should log to console", async () => {
43+
const logger = new ConsoleLogger();
44+
await logger.getWriter().write("ConsoleLogger");
45+
assertEquals(output, "ConsoleLogger");
46+
});
47+
});

lib/testing_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeAll, describe, it } from "@std/testing/bdd";
22
import { assert, assertEquals } from "@std/assert";
3-
import { MockWebSocket } from "./testing.ts";
3+
import { MockWebSocket } from "@lophus/lib/testing";
44

55
describe("MockWebSocket", () => {
66
let ws: MockWebSocket;

std/deno.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@lophus/std",
3+
"version": "0.0.14",
4+
"exports": {
5+
".": "./mod.ts",
6+
"./env": "./env.ts",
7+
"./events": "./events.ts",
8+
"./notes": "./notes.ts",
9+
"./pools": "./pools.ts",
10+
"./signs": "./signs.ts",
11+
"./times": "./times.ts"
12+
}
13+
}

std/env.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import "@std/dotenv";
2-
import type { PrivateKey, PublicKey } from "../core/protocol.ts";
2+
import type { PrivateKey, PublicKey } from "@lophus/core/protocol";
33

44
class Env {
55
#nsec?: PrivateKey;
66
#pubkey?: PublicKey;
77

8-
get PRIVATE_KEY() {
8+
get PRIVATE_KEY(): PrivateKey {
99
if (!this.#nsec) {
1010
const value = Deno.env.get("PRIVATE_KEY");
1111
if (!value) {
@@ -16,7 +16,7 @@ class Env {
1616
return this.#nsec;
1717
}
1818

19-
get PUBLIC_KEY() {
19+
get PUBLIC_KEY(): PublicKey {
2020
if (!this.#pubkey) {
2121
const value = Deno.env.get("PUBLIC_KEY");
2222
if (!value) {
@@ -28,4 +28,4 @@ class Env {
2828
}
2929
}
3030

31-
export const env = new Env();
31+
export const env: Env = new Env();

std/events.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import type { Stringified } from "@lophus/lib/types";
12
import type {
23
ClientToRelayMessage,
34
EventContent,
45
EventKind,
56
NostrEvent,
6-
} from "../core/protocol.ts";
7-
import { Stringified } from "../lib/types.ts";
7+
} from "@lophus/core/protocol";
8+
import "../nips/01/protocol.ts";
89

910
export interface EventInit<K extends EventKind = EventKind> {
1011
kind: NostrEvent<K>["kind"];

std/events_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
22
import { assertEquals, assertInstanceOf } from "@std/assert";
3-
import { Relay } from "../core/relays.ts";
4-
import { PrivateKey, Signer } from "./signs.ts";
3+
import { MockWebSocket } from "@lophus/lib/testing";
4+
import { Relay } from "@lophus/core/relays";
5+
import { PrivateKey, Signer } from "@lophus/std/signs";
56
import { EventInit, EventPublisher } from "./events.ts";
6-
import { MockWebSocket } from "../lib/testing.ts";
77

88
describe("EventPublisher", () => {
99
let relay: Relay;

std/logging.ts

-18
This file was deleted.

std/logging_test.ts

-28
This file was deleted.

std/mod.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from "./env.ts";
2+
export * from "./events.ts";
3+
export * from "./notes.ts";
4+
export * from "./times.ts";
5+
export * from "./signs.ts";
6+
export * from "./pools.ts";

std/notes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Optional } from "../lib/types.ts";
2-
import type { NostrEvent, RelayUrl } from "../core/protocol.ts";
3-
import { EventInit } from "./events.ts";
1+
import type { Optional } from "@lophus/lib/types";
2+
import type { NostrEvent, RelayUrl } from "@lophus/core/protocol";
3+
import { type EventInit } from "@lophus/std/events";
44

55
export type TextNote = EventInit<1>;
66

std/relays.ts std/pools.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { mergeReadableStreams as merge } from "@std/streams";
2+
import { Distinctor } from "@lophus/lib/streams";
23
import type {
34
ClientToRelayMessage,
45
EventKind,
56
NostrEvent,
67
SubscriptionFilter,
7-
} from "../core/protocol.ts";
8+
} from "@lophus/core/protocol";
89
import {
910
RelayLike,
1011
RelayLikeConfig,
1112
RelayLikeOptions,
1213
SubscriptionOptions,
13-
} from "../core/relays.ts";
14-
import { Distinctor } from "../lib/streams.ts";
14+
} from "@lophus/core/relays";
1515

1616
/**
1717
* A pool of relays that can be used as a single relay.
1818
*/
19-
export class RelayGroup implements RelayLike {
19+
export class RelayPool implements RelayLike {
2020
readonly writable: WritableStream<ClientToRelayMessage>;
2121
readonly config: Readonly<RelayLikeConfig>;
2222
#relays_read: RelayLike[];
@@ -45,7 +45,7 @@ export class RelayGroup implements RelayLike {
4545
subscribe<K extends EventKind>(
4646
filter: SubscriptionFilter<K> | SubscriptionFilter<K>[],
4747
opts: Partial<SubscriptionOptions> = {},
48-
) {
48+
): ReadableStream<NostrEvent<K>> {
4949
const subs = this.#relays_read.map((r) => r.subscribe(filter, opts));
5050
return merge(...subs).pipeThrough(new Distinctor((m) => m.id));
5151
}

std/relays_test.ts std/pools_test.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { assertEquals, assertInstanceOf } from "@std/assert";
22
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
3-
import { NostrEvent } from "../core/protocol.ts";
4-
import { Relay } from "../core/relays.ts";
5-
import { RelayGroup } from "../std/relays.ts";
6-
import { MockWebSocket } from "../lib/testing.ts";
3+
import { MockWebSocket } from "@lophus/lib/testing";
4+
import type { NostrEvent } from "@lophus/core/protocol";
5+
import { Relay } from "@lophus/core/relays";
6+
import { RelayPool } from "./pools.ts";
77

8-
describe("RelayGroup", () => {
8+
describe("RelayPool", () => {
99
let relays: Relay[];
10-
let group: RelayGroup;
10+
let group: RelayPool;
1111
let sub: ReadableStream<NostrEvent>;
1212

1313
// ----------------------
@@ -42,26 +42,26 @@ describe("RelayGroup", () => {
4242
// ----------------------
4343

4444
it("should create a group of relays", () => {
45-
group = new RelayGroup(relays);
46-
assertInstanceOf(group, RelayGroup);
45+
group = new RelayPool(relays);
46+
assertInstanceOf(group, RelayPool);
4747
});
4848
it("should not have a url", () => {
49-
// @ts-expect-error RelayGroup does not have a url
49+
// @ts-expect-error RelayPool does not have a url
5050
assertEquals(group.url, undefined);
5151
});
5252
it("should have a default name", () => {
5353
assertEquals(group.config.name, "relay-1, relay-2, relay-3");
5454
});
5555
it("should have a custom name if provided", () => {
56-
const group = new RelayGroup(relays, { name: "custom" });
56+
const group = new RelayPool(relays, { name: "custom" });
5757
assertEquals(group.config.name, "custom");
5858
});
5959
it("should have default read and write config", () => {
6060
assertEquals(group.config.read, true);
6161
assertEquals(group.config.write, true);
6262
});
6363
it("should have custom read and write config if provided", () => {
64-
const group = new RelayGroup(relays, { read: false, write: false });
64+
const group = new RelayPool(relays, { read: false, write: false });
6565
assertEquals(group.config.read, false);
6666
assertEquals(group.config.write, false);
6767
});

0 commit comments

Comments
 (0)