Skip to content
Open
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@contextvm/sdk": "0.12.0",
"@noble/ciphers": "2.2.0",
"@noble/secp256k1": "3.1.0",
"@sqlite.org/sqlite-wasm": "3.53.0-build1",
"nostr-tools": "2.23.5",
"svelte": "5.56.3",
"ts-mls": "2.0.0-rc.12",
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

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

43 changes: 42 additions & 1 deletion src/components/RelayConfigPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
const input = event.currentTarget as HTMLInputElement;
config.setMaxUsers(input.valueAsNumber);
}

function updateStorageBackend(event: Event): void {
const input = event.currentTarget as HTMLSelectElement;
config.setStorageBackend(input.value);
}

function updateMessageBufferLimit(event: Event): void {
const input = event.currentTarget as HTMLInputElement;
config.setMessageBufferLimit(input.valueAsNumber);
}
</script>

<section class="mx-auto max-w-3xl py-8">
Expand Down Expand Up @@ -152,7 +162,7 @@
<div class="mb-4 flex items-center justify-between gap-4">
<h3 class="text-xs uppercase tracking-[0.18em] text-[#87ff9f]">Runtime options</h3>
<span class="border border-[#21482b] px-3 py-1 text-xs uppercase text-[#a7b0aa]" data-testid="max-users-state">
{config.maxUsers} key packages / identity
{config.storageBackend} storage · {config.messageBufferLimit} buffered · {config.maxUsers} key packages / identity
</span>
</div>

Expand Down Expand Up @@ -184,6 +194,37 @@
onchange={updateMaxUsers}
/>
</label>

<label class="grid gap-2 border border-[#16331f] bg-[#050805] p-3 text-sm text-[#d1ffd9]">
<span class="uppercase tracking-[0.12em] text-[#6d746f]">storage</span>
<select
class="border border-[#21482b] bg-black px-3 py-2 text-[#87ff9f] outline-none focus:border-[#87ff9f] disabled:cursor-not-allowed disabled:text-[#4b554e]"
value={config.storageBackend}
disabled={!editingAllowed}
aria-label="Storage backend"
data-testid="storage-backend-select"
onchange={updateStorageBackend}
>
<option value="memory">memory</option>
<option value="indexeddb">IndexedDB</option>
</select>
</label>

<label class="grid gap-2 border border-[#16331f] bg-[#050805] p-3 text-sm text-[#d1ffd9]">
<span class="uppercase tracking-[0.12em] text-[#6d746f]">message buffer</span>
<input
class="border border-[#21482b] bg-black px-3 py-2 text-[#87ff9f] outline-none focus:border-[#87ff9f] disabled:cursor-not-allowed disabled:text-[#4b554e]"
type="number"
min="1"
max="50000"
step="1"
value={config.messageBufferLimit}
disabled={!editingAllowed}
aria-label="Message buffer"
data-testid="message-buffer-input"
onchange={updateMessageBufferLimit}
/>
</label>
</div>

{#if config.limitError}
Expand Down
29 changes: 29 additions & 0 deletions src/config/config-validator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
MAX_MEMORY_MESSAGE_BUFFER_LIMIT,
MIN_MEMORY_MESSAGE_BUFFER_LIMIT,
} from "../cordn/coordinator/storage/inMemoryStorage";
import type { BrowserCoordinatorStorageBackend } from "../cordn/coordinator/storage/browserCoordinatorStorage";

export function validateRelayUrl(value: string): string | null {
const trimmed = value.trim();
if (trimmed.length === 0) {
Expand Down Expand Up @@ -25,6 +31,7 @@ export function validateRelayUrl(value: string): string | null {
export const MIN_MAX_USERS = 1;
export const DEFAULT_MAX_USERS = 64;
export const BROWSER_MAX_USERS_CAP = 256;
export const DEFAULT_STORAGE_BACKEND: BrowserCoordinatorStorageBackend = "memory";

export function validateMaxUsers(value: number): string | null {
if (!Number.isSafeInteger(value)) {
Expand All @@ -41,3 +48,25 @@ export function validateMaxUsers(value: number): string | null {

return null;
}

export function validateStorageBackend(
value: string,
): value is BrowserCoordinatorStorageBackend {
return value === "memory" || value === "indexeddb";
}

export function validateMessageBufferLimit(value: number): string | null {
if (!Number.isSafeInteger(value)) {
return "Message buffer must be a whole number";
}

if (value < MIN_MEMORY_MESSAGE_BUFFER_LIMIT) {
return `Message buffer must be at least ${MIN_MEMORY_MESSAGE_BUFFER_LIMIT}`;
}

if (value > MAX_MEMORY_MESSAGE_BUFFER_LIMIT) {
return `Browser limit is ${MAX_MEMORY_MESSAGE_BUFFER_LIMIT} buffered messages`;
}

return null;
}
62 changes: 61 additions & 1 deletion src/config/config.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { DEFAULT_MAX_USERS, validateMaxUsers, validateRelayUrl } from "./config-validator";
import {
DEFAULT_MAX_USERS,
DEFAULT_STORAGE_BACKEND,
validateMaxUsers,
validateMessageBufferLimit,
validateRelayUrl,
validateStorageBackend,
} from "./config-validator";
import type { BrowserCoordinatorStorageBackend } from "../cordn/coordinator/storage/browserCoordinatorStorage";
import { DEFAULT_MEMORY_MESSAGE_BUFFER_LIMIT } from "../cordn/coordinator/storage/inMemoryStorage";

const CONFIG_STORAGE_KEY = "cordn:v1:config";
const CONFIG_STORAGE_VERSION = 1;
Expand All @@ -15,13 +24,17 @@ export interface RelayConfig {
export interface BrowserCoordinatorOptions {
announce: boolean;
maxUsers: number;
storageBackend: BrowserCoordinatorStorageBackend;
messageBufferLimit: number;
}

interface PersistedConfig {
version: typeof CONFIG_STORAGE_VERSION;
relays: Array<Pick<RelayConfig, "url" | "enabled">>;
announce: boolean;
maxUsers: number;
storageBackend?: BrowserCoordinatorStorageBackend;
messageBufferLimit?: number;
}

export class ConfigStore {
Expand All @@ -31,6 +44,8 @@ export class ConfigStore {
limitError = $state<string | null>(null);
announce = $state(false);
maxUsers = $state(DEFAULT_MAX_USERS);
storageBackend = $state<BrowserCoordinatorStorageBackend>(DEFAULT_STORAGE_BACKEND);
messageBufferLimit = $state(DEFAULT_MEMORY_MESSAGE_BUFFER_LIMIT);

constructor() {
this.loadPersistedConfig();
Expand All @@ -44,6 +59,8 @@ export class ConfigStore {
return {
announce: this.announce,
maxUsers: this.maxUsers,
storageBackend: this.storageBackend,
messageBufferLimit: this.messageBufferLimit,
};
}

Expand Down Expand Up @@ -112,6 +129,31 @@ export class ConfigStore {
return true;
}

setStorageBackend(value: string): boolean {
if (!validateStorageBackend(value)) {
this.limitError = "Storage backend must be memory or IndexedDB";
return false;
}

this.storageBackend = value;
this.limitError = null;
this.persistConfig();
return true;
}

setMessageBufferLimit(value: number): boolean {
const error = validateMessageBufferLimit(value);
if (error) {
this.limitError = error;
return false;
}

this.messageBufferLimit = value;
this.limitError = null;
this.persistConfig();
return true;
}

resetToDefaults(): void {
clearPersistedConfig();
this.relays = cloneDefaultRelays();
Expand All @@ -120,6 +162,8 @@ export class ConfigStore {
this.limitError = null;
this.announce = false;
this.maxUsers = DEFAULT_MAX_USERS;
this.storageBackend = DEFAULT_STORAGE_BACKEND;
this.messageBufferLimit = DEFAULT_MEMORY_MESSAGE_BUFFER_LIMIT;
}

private persistConfig(): void {
Expand All @@ -132,6 +176,8 @@ export class ConfigStore {
relays: this.relays.map((relay) => ({ url: relay.url, enabled: relay.enabled })),
announce: this.announce,
maxUsers: this.maxUsers,
storageBackend: this.storageBackend,
messageBufferLimit: this.messageBufferLimit,
};
localStorage.setItem(CONFIG_STORAGE_KEY, JSON.stringify(config));
}
Expand All @@ -149,6 +195,8 @@ export class ConfigStore {
}));
this.announce = persisted.announce;
this.maxUsers = persisted.maxUsers;
this.storageBackend = persisted.storageBackend ?? DEFAULT_STORAGE_BACKEND;
this.messageBufferLimit = persisted.messageBufferLimit ?? DEFAULT_MEMORY_MESSAGE_BUFFER_LIMIT;
}
}

Expand Down Expand Up @@ -186,12 +234,24 @@ function readPersistedConfig(): PersistedConfig | null {

const maxUsers = typeof parsed.maxUsers === "number" ? Math.trunc(parsed.maxUsers) : DEFAULT_MAX_USERS;
const limitError = validateMaxUsers(maxUsers);
const storageBackend =
typeof parsed.storageBackend === "string" && validateStorageBackend(parsed.storageBackend)
? parsed.storageBackend
: DEFAULT_STORAGE_BACKEND;
const messageBufferLimit =
typeof parsed.messageBufferLimit === "number"
? Math.trunc(parsed.messageBufferLimit)
: DEFAULT_MEMORY_MESSAGE_BUFFER_LIMIT;

return {
version: CONFIG_STORAGE_VERSION,
relays,
announce: parsed.announce === true,
maxUsers: limitError ? DEFAULT_MAX_USERS : maxUsers,
storageBackend,
messageBufferLimit: validateMessageBufferLimit(messageBufferLimit)
? DEFAULT_MEMORY_MESSAGE_BUFFER_LIMIT
: messageBufferLimit,
};
} catch {
return null;
Expand Down
3 changes: 1 addition & 2 deletions src/coordinator/coordinator.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { configStore } from "../config/config.svelte";
import { clearPersistedCoordinatorState } from "../cordn/coordinator/storage/browserSqliteStorage";
import { clearPersistedCoordinatorState } from "../cordn/coordinator/storage/browserCoordinatorStorage";
import { KeyManager } from "../crypto/key-manager";
import { keyStorage, WrongPassphraseError } from "../crypto/key-storage";
import { transportFactory, type RunningTransport } from "../lib/transport";
Expand Down Expand Up @@ -132,7 +132,6 @@ export class CoordinatorStore {
keyManager.getSecretKeyBytes(),
configStore.enabledRelayUrls,
configStore.coordinatorOptions,
this.persistenceEnabled,
{
onStarted: ({ publicKeyHex, relayUrls }) => {
this.addDebugLog(
Expand Down
Loading