Dominevo
Develop

Moving bytes between addons

How one addon hands a file or a buffer to another with a one-shot ticket, and why a cap method never carries base64.

An addon cannot dial another addon's HTTP server, and a cap method that returns base64 holds the whole payload twice (producer and caller) on a hub that has run out of memory before. The one supported way to move bytes from one addon to another is ctx.peerBytes.

The shape

Nothing is reachable that was not offered. The producer calls an offer from inside a cap method it already gates, gets a ticket, and returns it in the method's answer. The consumer opens the ticket. A consumer cannot address a producer, and one offer is one body taken once.

ctx.peerBytes is optional on the context (absent when an addon is loaded in isolation, such as a test), so check it.

Producer

const offered = await this.ctx.peerBytes?.offerFile({
  path: clipPath,
  contentType: 'video/mp4',
  label: 'clip export',        // names the body in the log, not a filename
  deviceId,
})
if (offered?.kind !== 'ticket') {
  throw new Error(`cannot offer bytes: ${offered?.kind === 'refused' ? offered.code : 'unavailable'}`)
}
return { ticket: offered.ticket }   // PeerBytesTicketSchema is the wire shape

Use offerBuffer({ bytes, contentType, label }) for bytes you hold, offerFile for a file on disk, and offer({ handler, label }) for a body you generate while streaming. The first two honour back-pressure, cut a peer that never reads, and log every exit. A refusal carries a code: offer-exhausted or listener-failed.

Consumer

const opened = await this.ctx.peerBytes?.open(ticket)
if (opened?.kind !== 'body') {
  throw new Error(`cannot open: ${opened?.kind === 'refused' ? opened.code : 'unavailable'}`)
}
const written = await opened.body.writeToFile(targetPath, { maxBytes: 2 * 1024 ** 3 })
if (written.kind === 'refused') throw new Error(written.detail)

body.stream is a Node Readable if you would rather pipe it yourself; body.declaredBytes is the producer's declared size, or null when unknown (never 0 for unknown). A refusal on open is one of cross-node, ticket-expired, ticket-malformed, unreachable or producer-refused.

Rules and pitfalls

  • Same host only. The listener binds loopback, so a ticket minted on one node means something else on another. open refuses it as cross-node without dialling. Device providers often run on agents, so handle that branch: fall back to a path that does not need the bytes on another node.
  • A ticket is a bearer credential with a seconds-long life. Never persist one and never log its url.
  • Not a frame pipe. One offer is one body, with a short life and a bound on outstanding offers. Minting one per frame is refused by name. Frames do not cross a process boundary at frame rate; media is fetched on demand, compressed, by handle.
  • Do not start a private HTTP server with a token map for this. That is the habit this facility replaced.

Then