Skip to main content

bare-rpc-python

Pure-Python port of the bare-rpc message/frame wire codec and async RPC runtime, wire-compatible with the JavaScript reference. Built on compact-encoding-python.

Typically you don't call this directly - hrpc-python generates a typed client/server on top of it. Use it directly when you want the raw transport.

Install

pip install git+https://github.com/holepunchto/bare-rpc-python

Wire codec

Encode and decode frames without the runtime:

import bare_rpc as rpc

frame = rpc.encode_request(1, 42, data=b"hi")  # -> bytes
msg = rpc.decode_frame(frame)  # -> RequestMessage(id=1, command=42, ...)

Encoders: encode_request, encode_response, encode_error_response, encode_event, encode_stream. decode_frame returns a RequestMessage, ResponseMessage, or StreamMessage. Type and StreamFlag are the wire enums.

RPC runtime

RPC is transport-agnostic: give it a send callback for outgoing frames and feed it inbound bytes with receive. Wire those to an asyncio stream, a websocket, or an in-memory pipe.

import bare_rpc as rpc


async def on_request(req):
    await req.reply(b"pong")


server = rpc.RPC(send=my_send, on_request=on_request)
client = rpc.RPC(send=my_other_send)

await server.receive(incoming_bytes)  # feed inbound frames in

result = await client.request(command=5, data=b"ping")  # -> b"pong"
await client.event(command=3, data=b"note")  # fire-and-forget, no response

Constructor: RPC(send, *, on_request=None, on_event=None, on_error=None, max_frame_size=...). on_request receives an IncomingRequest (.command, .data, await .reply(data), await .reject(error)); on_event receives an IncomingEvent. close() rejects all in-flight requests.

Streams and backpressure

All three streaming shapes are implemented, with cork/uncork backpressure. An OutgoingStream has await write(data), await end(), and await destroy(error=None); an IncomingStream is async-iterable.

Response stream (server streams back, client reads):

async def on_request(req):
    out = await req.create_response_stream()
    await out.write(b"chunk-1")
    await out.write(b"chunk-2")
    await out.end()


incoming = await client.request_with_response_stream(command=5, data=b"go")
async for chunk in incoming:
    ...

Request stream (client streams up, server reads, then replies):

async def on_request(req):
    async for chunk in req.request_stream:
        ...
    await req.reply(b"done")


out, reply = await client.stream_request(command=6)
await out.write(b"chunk-1")
await out.end()
result = await reply  # -> b"done"

Duplex (both directions):

out, incoming = await client.create_bidirectional_stream(command=7)
await out.write(b"ping")
await out.end()
async for chunk in incoming:
    ...

Errors

A rejected request raises RPCRemoteError (.message, .code, .errno) on the caller. Pass one to await req.reject(...) to carry a specific code across the wire; any other exception is rejected with its string message.

License

Apache-2.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bare_rpc-0.0.1.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bare_rpc-0.0.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file bare_rpc-0.0.1.tar.gz.

File metadata

  • Download URL: bare_rpc-0.0.1.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for bare_rpc-0.0.1.tar.gz
Algorithm Hash digest
SHA256 787372ed360a66c05c2d2c53865ff2d675101c311630e934acc3a8c2277b5435
MD5 fa4542b00a14f2e82301c3015ec99d62
BLAKE2b-256 a97119d6a8bc040fbba6725ba16ca4025cd4075430916c0a6c8aead5c83f331e

See more details on using hashes here.

File details

Details for the file bare_rpc-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: bare_rpc-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for bare_rpc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09cd21aa83594df2d7a86892f079d08b69f90ae27000aeb41dc5815ab419d2be
MD5 0e92157a5f58ff5730c416dfb399bbc9
BLAKE2b-256 df89d95da3e26f19d18094c1021d3891e5f125c9fa23b81713373dcb9e68ec09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page