🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@standardserver/peer

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@standardserver/peer

Source
npmnpm
Version
0.0.25
Version published
Weekly downloads
6.6K
48.9%
Maintainers
1
Weekly downloads
 
Created
Source

@standardserver/peer

codecov weekly downloads MIT License Discord Ask DeepWiki

@standardserver/peer adapts message-based transports to the transport-agnostic request and response model defined by Standard Server.

Standard Server provides a unified interface for client-server communication across HTTP and message-based transports. It lets you write handlers and clients against the same request, response, body, and streaming primitives whether the underlying transport is Fetch, Node.js HTTP, WebSocket, MessagePort, or another peer-style channel.

This package is the peer adapter for that model. It converts between Standard Server requests and responses and a structured peer message protocol that can be sent through any transport capable of carrying strings or binary data.

Entry Point

The package exports a single entry point:

ExportPurpose
@standardserver/peerPeer adapter helpers for requests, responses, codecs, streams, and validators

Package overview

The main entry point exposes four groups of helpers:

GroupExportsPurpose
Peer runtimeClientPeer, ServerPeerSend Standard Server requests and responses over a peer transport
Message codecencodePeerMessage(), decodePeerMessage()Encode peer messages as strings or bytes for transport
Stream utilitiestoEventIterator(), EventStreamTransmitter, HibernationEventIteratorBridge peer messages with Standard Server event-stream semantics
Types and validatorsPeerMessage, PeerRequestMessage, PeerResponseMessage, PeerCancelMessage, PeerEventStreamMessage, PeerOctetStreamMessage, PeerStreamCancelMessage, ClientPeerSendMessage, ServerPeerSendMessage, isPeerMessage(), isPeerRequestMessage(), isPeerResponseMessage(), isPeerCancelMessage(), isPeerEventStreamMessage(), isPeerOctetStreamMessage(), isPeerStreamCancelMessage(), isClientPeerSendMessage(), isServerPeerSendMessage()Describe and validate the peer protocol payloads

Use these helpers when you want Standard Server handlers or clients to run over message-based transports such as MessagePort, WebSocket, Electron IPC, or a custom channel.

Request and response flow

ClientPeer starts a request and waits for a StandardLazyResponse. ServerPeer receives peer messages, reconstructs a StandardLazyRequest, calls your handler, and sends the resulting StandardResponse back over the same transport.

import type { StandardLazyRequest, StandardResponse } from '@standardserver/core'
import {
  ClientPeer,
  decodePeerMessage,
  encodePeerMessage,
  isClientPeerSendMessage,
  isServerPeerSendMessage,
  ServerPeer,
} from '@standardserver/peer'

async function handle(request: StandardLazyRequest): Promise<StandardResponse> {
  const body = await request.resolveBody()

  return {
    status: 200,
    headers: { 'content-type': 'application/json' },
    body: {
      ok: true,
      method: request.method,
      url: request.url,
      received: body,
    },
  }
}

const { port1, port2 } = new MessageChannel()

const clientPeer = new ClientPeer(async (message) => {
  port1.postMessage(await encodePeerMessage(message, { /** options */ }))
})

const serverPeer = new ServerPeer(async (message) => {
  port2.postMessage(await encodePeerMessage(message, { /** options */ }))
})

port1.addEventListener('message', async (event) => {
  const decoded = decodePeerMessage(event.data, { /** options */ })

  if (decoded.matched && isServerPeerSendMessage(decoded.message)) {
    await clientPeer.message(decoded.message)
  }
})

port2.addEventListener('message', async (event) => {
  const decoded = decodePeerMessage(event.data, { /** options */ })

  if (decoded.matched && isClientPeerSendMessage(decoded.message)) {
    await serverPeer.message(decoded.message, handle)
  }
})

port1.start()
port2.start()

const response = await clientPeer.request({
  method: 'POST',
  url: '/echo',
  headers: { 'content-type': 'application/json' },
  body: { message: 'hello' },
})

const payload = await response.resolveBody()

[!TIP] When encoding or decoding peer messages, you can pass additional options, such as prefix, to prevent collisions when the same peer is used for multiple purposes.

Codec helpers

Use encodePeerMessage() and decodePeerMessage() to bridge between the peer protocol and your underlying transport.

import { decodePeerMessage, encodePeerMessage } from '@standardserver/peer'

const encoded = await encodePeerMessage(
  {
    id: '1',
    kind: 'request',
    json: { method: 'GET', url: '/health', headers: {}, body: undefined },
  },
  { prefix: 'rpc:' },
)

const decoded = decodePeerMessage(encoded, { prefix: 'rpc:' })

if (decoded.matched) {
  console.log(decoded.message.kind)
}

Encoding rules:

  • Messages without binary payloads are encoded as strings.
  • Messages with binary payloads are encoded as Uint8Array values containing JSON, a delimiter byte, and the raw binary bytes.
  • The optional prefix lets you share the same transport between multiple protocols without collisions.

Learn more

For the higher-level project overview, see the root Standard Server README.

Sponsors

Like what we build over at middleapi? You can help keep it going here: GitHub Sponsors. Every bit helps! 🚀

🏆 Platinum Sponsor

ScreenshotOne.com
ScreenshotOne.com

🥈 Silver Sponsor

村上さん
村上さん

Generous Sponsors

LN Markets
LN Markets

Sponsors

Reece McDonald
Reece McDonald
nk
nk
supastarter
supastarter
Dexter Miguel
Dexter Miguel
herrfugbaum
herrfugbaum
Ryota Murakami
Ryota Murakami
David Cramer
David Cramer
Valerii Petryniak
Valerii Petryniak
Valerii Strilets
Valerii Strilets
Kyle Mistele
Kyle Mistele
Andrew Peters
Andrew Peters
Ryan Vogel
Ryan Vogel
christ12938
christ12938
Peter Adam
Peter Adam
Ryan Soderberg
Ryan Soderberg
shota
shota

Backers

David Walsh
David Walsh
Robbe Vaes
Robbe Vaes
Aidan Sunbury
Aidan Sunbury
soonoo
soonoo
Kevin Porten
Kevin Porten
Denis
Denis
Christopher Kapic
Christopher Kapic
Tom Ballinger
Tom Ballinger
Sam
Sam
Titoine
Titoine
Igor Makowski
Igor Makowski
hanayashiki
hanayashiki
Lev Dubinets
Lev Dubinets
Kelly Peilin Chan
Kelly Peilin Chan
Alex
Alex
nattstack
nattstack
Andrey Gubanov
Andrey Gubanov

Past Sponsors

Maxie Stijn Timmer あわわわとーにゅ Zuplo motopods Francisco Hermida Théo LUDWIG Abhay Ramesh shr.ink oü 0x4e32 Ryuz happyboy yicchi Saksham Roman Hrynevych rokitg Omar Khatib Yu-Sabo Bapusaheb Patil grim Nelson Lai Lê Cao Nguyên Robert Soriano SKostyukovich Fabworks Novak Antonijevic Laduni Estu Syalwa Chen, Zhi-Yuan Illarion Koperski Anees Iqbal Sefa Eyeoglu Adam Tkaczyk plancraft

FAQs

Package last updated on 21 Jun 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts