iroh-ts

Peer-to-peer networking for TypeScript — connections, streams, blobs, and documents — powered by WASM.

Install

npm install @salvatoret/iroh # or pnpm add / yarn add

Built on iroh v0.97 (Rust → WASM via wasm-pack). Works in Node.js ≥18 and modern browsers. Zero native dependencies.

Quick Start

Two nodes connect and exchange a message:

import { Endpoint } from "@salvatoret/iroh";

const ALPN = new TextEncoder().encode("my-app/1");

// --- Node A: accept a connection ---
const nodeA = await Endpoint.create();
await nodeA.online();
nodeA.setAlpns([ALPN]);
const addr = nodeA.endpointAddr();

// --- Node B: connect and send ---
const nodeB = await Endpoint.create();
await nodeB.online();
const conn = await nodeB.connect(addr, ALPN);
const stream = await conn.openBi();
await stream.send.writeAll(new TextEncoder().encode("hello from B"));
stream.send.finish();

// --- Node A: receive ---
const accepted = await nodeA.accept();
const incoming = await accepted.acceptBi();
const data = await incoming.recv.readToEnd(65536);
console.log(new TextDecoder().decode(data)); // "hello from B"
Note: acceptBi() won't resolve until the opener writes to its SendStream. Always write before awaiting the accept side.

Live Examples

API Reference

Endpoint

The main entry point. Creates a QUIC endpoint with relay connectivity for NAT traversal.

MethodSignatureDescription
createstatic create(): Promise<Endpoint>Create endpoint with default n0 relay settings
endpointIdendpointId(): stringHex-encoded Ed25519 public key (64 chars)
endpointAddrendpointAddr(): EndpointAddrFull address info (id + relay URL)
onlineonline(): Promise<void>Wait until connected to a relay server
connectconnect(addr, alpn): Promise<Connection>Connect to a remote peer
acceptaccept(): Promise<Connection | undefined>Accept an incoming connection
setAlpnssetAlpns(alpns: Uint8Array[]): voidSet accepted ALPN protocols
closeclose(): Promise<void>Gracefully shut down

EndpointAddr

Address information for reaching an endpoint (ID + relay URL + direct addresses).

MethodSignatureDescription
fromEndpointIdstatic fromEndpointId(id: string): EndpointAddrCreate from hex endpoint ID
endpointIdendpointId(): stringGet the endpoint ID
relayUrlrelayUrl(): string | undefinedGet the relay URL, if any

Connection

A QUIC connection to a remote peer. Supports bidirectional streams, unidirectional streams, and datagrams.

MethodSignatureDescription
openBiopenBi(): Promise<BiStream>Open a bidirectional stream
acceptBiacceptBi(): Promise<BiStream>Accept incoming bidirectional stream
openUniopenUni(): Promise<SendStream>Open a unidirectional send stream
acceptUniacceptUni(): Promise<RecvStream>Accept incoming unidirectional stream
sendDatagramsendDatagram(data: Uint8Array): voidSend unreliable datagram
readDatagramreadDatagram(): Promise<Uint8Array>Read unreliable datagram
remoteEndpointIdremoteEndpointId(): stringRemote peer's endpoint ID
closeclose(error_code, reason): voidClose with error code
stableIdstableId(): numberStable connection identifier

BiStream

PropertyTypeDescription
sendSendStreamSend half
recvRecvStreamReceive half

SendStream

MethodSignatureDescription
writewrite(data: Uint8Array): Promise<number>Write data, returns bytes written
writeAllwriteAll(data: Uint8Array): Promise<void>Write all data
finishfinish(): voidSignal end of stream

RecvStream

MethodSignatureDescription
readChunkreadChunk(max: number): Promise<Uint8Array | null>Read a chunk incrementally
readToEndreadToEnd(limit: number): Promise<Uint8Array>Read all data up to limit
stopstop(error_code: number): voidStop reading with error code

BlobStore

In-memory content-addressable storage using BLAKE3 hashing.

MethodSignatureDescription
constructornew BlobStore()Create in-memory store
addBytesaddBytes(data: Uint8Array): Promise<string>Store data, returns BLAKE3 hash
getBytesgetBytes(hash: string): Promise<Uint8Array>Retrieve by hash
hashas(hash: string): Promise<boolean>Check existence
listlist(): Promise<string[]>List all stored hashes

DocEngine

CRDT-based replicated document engine. Creates its own Endpoint, gossip, and blob store internally.

MethodSignatureDescription
createstatic create(): Promise<DocEngine>Create engine with in-memory storage
createDoccreateDoc(): Promise<Doc>Create a new document
authorDefaultauthorDefault(): Promise<string>Get/create default author ID
authorCreateauthorCreate(): Promise<string>Create a new author
shutdownshutdown(): Promise<void>Shut down the engine

Doc

A replicated key-value document.

MethodSignatureDescription
idid(): stringNamespace ID (hex)
setBytessetBytes(author, key, value): Promise<string>Set entry, returns content hash
getExactgetExact(author, key): Promise<Uint8Array | undefined>Get entry by author + key
deldel(author, prefix): Promise<number>Delete by prefix, returns count
closeclose(): Promise<void>Close the document

Memory Management

All WASM objects have a free() method and support Symbol.dispose. Call free() when done to release WASM memory:

const ep = await Endpoint.create();
// ... use it ...
await ep.close();
ep.free();