Skip to content

Commit

Permalink
build: create lib and std modules
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Mar 18, 2024
1 parent 7b25d39 commit db1ba3b
Show file tree
Hide file tree
Showing 25 changed files with 146 additions and 103 deletions.
2 changes: 1 addition & 1 deletion core/clients_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "@std/assert";
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { MockWebSocket } from "../lib/testing.ts";
import { MockWebSocket } from "@lophus/lib/testing";
import { Client } from "./clients.ts";

describe("Client", () => {
Expand Down
12 changes: 5 additions & 7 deletions core/deno.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "@lophus/core",
"version": "0.0.14",
"exports": "./mod.ts",

"publish": {
"exclude": [
"deno.json",
"*_test.ts"
]
"exports": {
".": "./mod.ts",
"./clients": "./clients.ts",
"./protocol": "./protocol.ts",
"./relays": "./relays.ts"
}
}
2 changes: 1 addition & 1 deletion core/nodes_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "@std/assert";
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { MockWebSocket } from "../lib/testing.ts";
import { MockWebSocket } from "@lophus/lib/testing";
import { NostrNode, NostrNodeBase } from "./nodes.ts";

describe("NostrNodeBase", () => {
Expand Down
2 changes: 1 addition & 1 deletion core/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @module
*/

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

// ----------------------
// Extendable interfaces
Expand Down
2 changes: 1 addition & 1 deletion core/relays.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Stringified } from "../lib/types.ts";
import type { Stringified } from "@lophus/lib/types";
import type {
ClientToRelayMessage,
EventId,
Expand Down
2 changes: 1 addition & 1 deletion core/websockets_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, assertEquals } from "@std/assert";
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { MockWebSocket } from "../lib/testing.ts";
import { MockWebSocket } from "@lophus/lib/testing";
import { LazyWebSocket } from "./websockets.ts";

describe("LazyWebSocket", () => {
Expand Down
16 changes: 15 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"imports": {
"@noble/curves": "npm:@noble/curves@^1.3.0",
"@noble/hashes": "npm:@noble/hashes@^1.3.3",
"@lophus/core/clients": "./core/clients.ts",
"@lophus/core/protocol": "./core/protocol.ts",
"@lophus/core/relays": "./core/relays.ts",
"@lophus/lib/types": "./lib/types.ts",
"@lophus/lib/testing": "./lib/testing.ts",
"@lophus/lib/streams": "./lib/streams.ts",
"@lophus/std/env": "./std/env.ts",
"@lophus/std/events": "./std/events.ts",
"@lophus/std/notes": "./std/notes.ts",
"@lophus/std/pools": "./std/pools.ts",
"@lophus/std/signs": "./std/signs.ts",
"@lophus/std/times": "./std/times.ts",
"@std/assert": "jsr:@std/assert@^0.219.1",
"@std/dotenv": "jsr:@std/dotenv@^0.219.1",
"@std/streams": "jsr:@std/streams@^0.219.1",
Expand All @@ -21,6 +33,8 @@
"update:commit": "deno task -q update --commit --prefix 'build(deps):' --prefix-lock 'build(lock):'"
},
"workspaces": [
"./core"
"./core",
"./lib",
"./std"
]
}
3 changes: 2 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@lophus/lib",
"version": "0.0.14",
"exports": {
"./streams": "./streams.ts",
"./testing": "./testing.ts",
"./types": "./types.ts"
}
}
19 changes: 19 additions & 0 deletions lib/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,22 @@ export class Transformer<R = unknown, W = unknown>
});
}
}

export type LogLevel = "error" | "warn" | "info" | "debug";

export interface ConsoleLoggerOptions {
level?: LogLevel;
}

export class ConsoleLogger<W = unknown> extends WritableStream<W> {
readonly level: LogLevel;
constructor(options?: ConsoleLoggerOptions) {
const level = options?.level ?? "info";
super({
write(chunk) {
console[level](chunk);
},
});
this.level = level;
}
}
31 changes: 28 additions & 3 deletions lib/streams_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertArrayIncludes, assertEquals } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import { Distinctor } from "./streams.ts";
import { assert, assertArrayIncludes, assertEquals } from "@std/assert";
import { beforeEach, describe, it } from "@std/testing/bdd";
import { ConsoleLogger, Distinctor } from "@lophus/lib/streams";

describe("Distinctor", () => {
it("filters out duplicate values from a stream", async () => {
Expand All @@ -20,3 +20,28 @@ describe("Distinctor", () => {
assertArrayIncludes(values, [1, 2, 3]);
});
});

describe("ConsoleLogger", () => {
let output = "";

beforeEach(() => {
globalThis.console = new Proxy(globalThis.console, {
get: () => {
return (data: unknown) => {
output += data;
};
},
});
});

it("should be a writable stream", () => {
const logger = new ConsoleLogger();
assert(logger instanceof WritableStream);
});

it("should log to console", async () => {
const logger = new ConsoleLogger();
await logger.getWriter().write("ConsoleLogger");
assertEquals(output, "ConsoleLogger");
});
});
2 changes: 1 addition & 1 deletion lib/testing_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeAll, describe, it } from "@std/testing/bdd";
import { assert, assertEquals } from "@std/assert";
import { MockWebSocket } from "./testing.ts";
import { MockWebSocket } from "@lophus/lib/testing";

describe("MockWebSocket", () => {
let ws: MockWebSocket;
Expand Down
13 changes: 13 additions & 0 deletions std/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@lophus/std",
"version": "0.0.14",
"exports": {
".": "./mod.ts",
"./env": "./env.ts",
"./events": "./events.ts",
"./notes": "./notes.ts",
"./pools": "./pools.ts",
"./signs": "./signs.ts",
"./times": "./times.ts"
}
}
8 changes: 4 additions & 4 deletions std/env.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "@std/dotenv";
import type { PrivateKey, PublicKey } from "../core/protocol.ts";
import type { PrivateKey, PublicKey } from "@lophus/core/protocol";

class Env {
#nsec?: PrivateKey;
#pubkey?: PublicKey;

get PRIVATE_KEY() {
get PRIVATE_KEY(): PrivateKey {
if (!this.#nsec) {
const value = Deno.env.get("PRIVATE_KEY");
if (!value) {
Expand All @@ -16,7 +16,7 @@ class Env {
return this.#nsec;
}

get PUBLIC_KEY() {
get PUBLIC_KEY(): PublicKey {
if (!this.#pubkey) {
const value = Deno.env.get("PUBLIC_KEY");
if (!value) {
Expand All @@ -28,4 +28,4 @@ class Env {
}
}

export const env = new Env();
export const env: Env = new Env();
5 changes: 3 additions & 2 deletions std/events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Stringified } from "@lophus/lib/types";
import type {
ClientToRelayMessage,
EventContent,
EventKind,
NostrEvent,
} from "../core/protocol.ts";
import { Stringified } from "../lib/types.ts";
} from "@lophus/core/protocol";
import "../nips/01/protocol.ts";

export interface EventInit<K extends EventKind = EventKind> {
kind: NostrEvent<K>["kind"];
Expand Down
6 changes: 3 additions & 3 deletions std/events_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { assertEquals, assertInstanceOf } from "@std/assert";
import { Relay } from "../core/relays.ts";
import { PrivateKey, Signer } from "./signs.ts";
import { MockWebSocket } from "@lophus/lib/testing";
import { Relay } from "@lophus/core/relays";
import { PrivateKey, Signer } from "@lophus/std/signs";
import { EventInit, EventPublisher } from "./events.ts";
import { MockWebSocket } from "../lib/testing.ts";

describe("EventPublisher", () => {
let relay: Relay;
Expand Down
18 changes: 0 additions & 18 deletions std/logging.ts

This file was deleted.

28 changes: 0 additions & 28 deletions std/logging_test.ts

This file was deleted.

6 changes: 6 additions & 0 deletions std/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./env.ts";
export * from "./events.ts";
export * from "./notes.ts";
export * from "./times.ts";
export * from "./signs.ts";
export * from "./pools.ts";
6 changes: 3 additions & 3 deletions std/notes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Optional } from "../lib/types.ts";
import type { NostrEvent, RelayUrl } from "../core/protocol.ts";
import { EventInit } from "./events.ts";
import type { Optional } from "@lophus/lib/types";
import type { NostrEvent, RelayUrl } from "@lophus/core/protocol";
import { type EventInit } from "@lophus/std/events";

export type TextNote = EventInit<1>;

Expand Down
10 changes: 5 additions & 5 deletions std/relays.ts → std/pools.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { mergeReadableStreams as merge } from "@std/streams";
import { Distinctor } from "@lophus/lib/streams";
import type {
ClientToRelayMessage,
EventKind,
NostrEvent,
SubscriptionFilter,
} from "../core/protocol.ts";
} from "@lophus/core/protocol";
import {
RelayLike,
RelayLikeConfig,
RelayLikeOptions,
SubscriptionOptions,
} from "../core/relays.ts";
import { Distinctor } from "../lib/streams.ts";
} from "@lophus/core/relays";

/**
* A pool of relays that can be used as a single relay.
*/
export class RelayGroup implements RelayLike {
export class RelayPool implements RelayLike {
readonly writable: WritableStream<ClientToRelayMessage>;
readonly config: Readonly<RelayLikeConfig>;
#relays_read: RelayLike[];
Expand Down Expand Up @@ -45,7 +45,7 @@ export class RelayGroup implements RelayLike {
subscribe<K extends EventKind>(
filter: SubscriptionFilter<K> | SubscriptionFilter<K>[],
opts: Partial<SubscriptionOptions> = {},
) {
): ReadableStream<NostrEvent<K>> {
const subs = this.#relays_read.map((r) => r.subscribe(filter, opts));
return merge(...subs).pipeThrough(new Distinctor((m) => m.id));
}
Expand Down
22 changes: 11 additions & 11 deletions std/relays_test.ts → std/pools_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { assertEquals, assertInstanceOf } from "@std/assert";
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { NostrEvent } from "../core/protocol.ts";
import { Relay } from "../core/relays.ts";
import { RelayGroup } from "../std/relays.ts";
import { MockWebSocket } from "../lib/testing.ts";
import { MockWebSocket } from "@lophus/lib/testing";
import type { NostrEvent } from "@lophus/core/protocol";
import { Relay } from "@lophus/core/relays";
import { RelayPool } from "./pools.ts";

describe("RelayGroup", () => {
describe("RelayPool", () => {
let relays: Relay[];
let group: RelayGroup;
let group: RelayPool;
let sub: ReadableStream<NostrEvent>;

// ----------------------
Expand Down Expand Up @@ -42,26 +42,26 @@ describe("RelayGroup", () => {
// ----------------------

it("should create a group of relays", () => {
group = new RelayGroup(relays);
assertInstanceOf(group, RelayGroup);
group = new RelayPool(relays);
assertInstanceOf(group, RelayPool);
});
it("should not have a url", () => {
// @ts-expect-error RelayGroup does not have a url
// @ts-expect-error RelayPool does not have a url
assertEquals(group.url, undefined);
});
it("should have a default name", () => {
assertEquals(group.config.name, "relay-1, relay-2, relay-3");
});
it("should have a custom name if provided", () => {
const group = new RelayGroup(relays, { name: "custom" });
const group = new RelayPool(relays, { name: "custom" });
assertEquals(group.config.name, "custom");
});
it("should have default read and write config", () => {
assertEquals(group.config.read, true);
assertEquals(group.config.write, true);
});
it("should have custom read and write config if provided", () => {
const group = new RelayGroup(relays, { read: false, write: false });
const group = new RelayPool(relays, { read: false, write: false });
assertEquals(group.config.read, false);
assertEquals(group.config.write, false);
});
Expand Down
Loading

0 comments on commit db1ba3b

Please sign in to comment.