typed-channel

typed-channel logo
  • Typed messages. One type describes every message a side can send or receive. A wrong name or payload is a compile error, rather than a silent runtime bug.
  • Requests with responses. createTypedRpcChannel adds request/response calls on top of plain messages, with abort and timeout support.
  • Pluggable transports. EventTarget and postMessage transports ship with the package. A custom transport is a small object with on and emit functions.
  • No runtime dependencies. The core starts at 341 B.
import { createTypedChannel } from "typed-channel"
import { createPostMessageTransport } from "typed-channel/transports/postMessage"

type Inbound = { status: { code: number } }
type Outbound = { fetch: { id: string }; cancel: never }

const channel = createTypedChannel(createPostMessageTransport<Inbound, Outbound>(worker))

channel.on("status", ({ code }) => console.log(code))
channel.emit("fetch", { id: "123" })

Guides for messages, requests, and transports are at typed-channel.psdcoder.dev.

Explore more open source projects