clone

a self-hosted sync node for lofi apps: jazz sync over iroh, ticket-gated, with no static ip or cloud needed

release/0.4.0
lofi-node / src / jazz.ts
jazz.ts 1.5K
1// Thin wrapper over jazz-napi's JazzServer. Version invariant: this package2// pins the exact jazz alpha the consuming lofi app pins; wire compatibility3// across alphas is not guaranteed.45import { JazzServer } from "jazz-napi";67export interface JazzOptions {8 appId: string;9 backendSecret: string;10 adminSecret: string;11 port?: number;12 dataDir?: string;13 inMemory?: boolean;14 upstreamUrl?: string;15 allowLocalFirstAuth?: boolean;16}1718export interface JazzHandle {19 url: string;20 port: number;21 stop(): Promise<void>;22}2324export function allocatePort(): number {25 const listener = Deno.listen({ hostname: "127.0.0.1", port: 0 });26 const port = (listener.addr as Deno.NetAddr).port;27 listener.close();28 return port;29}3031export async function startJazz(options: JazzOptions): Promise<JazzHandle> {32 const port = options.port ?? allocatePort();33 const server = await JazzServer.start({34 appId: options.appId,35 backendSecret: options.backendSecret,36 adminSecret: options.adminSecret,37 port,38 dataDir: options.inMemory ? undefined : options.dataDir,39 inMemory: options.inMemory,40 upstreamUrl: options.upstreamUrl,41 allowLocalFirstAuth: options.allowLocalFirstAuth ?? true,42 });43 let stopped: Promise<void> | null = null;44 return {45 url: toWsUrl(server.url),46 port: server.port,47 stop: () => (stopped ??= server.stop()),48 };49}5051/** JazzServer reports an http(s) URL; clients dial ws(s). */52export function toWsUrl(url: string): string {53 return url.replace(/^http/, "ws");54}