Implement hrpc with streaming and wire into setup
- Add spec/hyperschema and spec/hrpc codegen (scripts/build-hrpc.js) with unary, request/response-stream, duplex, and send-only commands - Run build:hrpc and build:protomux from install.sh so setup is one-step - Native host: add attachHrpc (Protomux channel + generated HRPC), handlers for ping, streamSum, fetchStream, duplex, notify - Load HRPC lazily so missing spec does not crash host; use bare-stream for Duplex under Bare; add bare-stream, compact-encoding deps
This commit is contained in:
@@ -0,0 +1,314 @@
|
||||
// This file is autogenerated by the hyperschema compiler
|
||||
// Schema Version: 1
|
||||
/* eslint-disable camelcase */
|
||||
/* eslint-disable quotes */
|
||||
/* eslint-disable space-before-function-paren */
|
||||
|
||||
const { c } = require('hyperschema/runtime')
|
||||
|
||||
const VERSION = 1
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
let version = VERSION
|
||||
|
||||
// @bridgeswarm/ping-request
|
||||
const encoding0 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 1 so always one byte
|
||||
|
||||
if (m.value) c.string.preencode(state, m.value)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = m.value ? 1 : 0
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.value) c.string.encode(state, m.value)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
value: (flags & 1) !== 0 ? c.string.decode(state) : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/ping-response
|
||||
const encoding1 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 1 so always one byte
|
||||
|
||||
if (m.pong) c.string.preencode(state, m.pong)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = m.pong ? 1 : 0
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.pong) c.string.encode(state, m.pong)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
pong: (flags & 1) !== 0 ? c.string.decode(state) : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/stream-request-chunk
|
||||
const encoding2 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 2 so always one byte
|
||||
|
||||
if (m.n) c.uint.preencode(state, m.n)
|
||||
if (m.label) c.string.preencode(state, m.label)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = (m.n ? 1 : 0) | (m.label ? 2 : 0)
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.n) c.uint.encode(state, m.n)
|
||||
if (m.label) c.string.encode(state, m.label)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
n: (flags & 1) !== 0 ? c.uint.decode(state) : 0,
|
||||
label: (flags & 2) !== 0 ? c.string.decode(state) : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/stream-sum-response
|
||||
const encoding3 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 2 so always one byte
|
||||
|
||||
if (m.sum) c.uint.preencode(state, m.sum)
|
||||
if (m.count) c.uint.preencode(state, m.count)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = (m.sum ? 1 : 0) | (m.count ? 2 : 0)
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.sum) c.uint.encode(state, m.sum)
|
||||
if (m.count) c.uint.encode(state, m.count)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
sum: (flags & 1) !== 0 ? c.uint.decode(state) : 0,
|
||||
count: (flags & 2) !== 0 ? c.uint.decode(state) : 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/fetch-request
|
||||
const encoding4 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 1 so always one byte
|
||||
|
||||
if (m.count) c.uint.preencode(state, m.count)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = m.count ? 1 : 0
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.count) c.uint.encode(state, m.count)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
count: (flags & 1) !== 0 ? c.uint.decode(state) : 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/fetch-chunk
|
||||
const encoding5 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 2 so always one byte
|
||||
|
||||
if (m.i) c.uint.preencode(state, m.i)
|
||||
if (m.data) c.string.preencode(state, m.data)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = (m.i ? 1 : 0) | (m.data ? 2 : 0)
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.i) c.uint.encode(state, m.i)
|
||||
if (m.data) c.string.encode(state, m.data)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
i: (flags & 1) !== 0 ? c.uint.decode(state) : 0,
|
||||
data: (flags & 2) !== 0 ? c.string.decode(state) : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/duplex-in
|
||||
const encoding6 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 2 so always one byte
|
||||
|
||||
if (m.x) c.uint.preencode(state, m.x)
|
||||
if (m.y) c.string.preencode(state, m.y)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = (m.x ? 1 : 0) | (m.y ? 2 : 0)
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.x) c.uint.encode(state, m.x)
|
||||
if (m.y) c.string.encode(state, m.y)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
x: (flags & 1) !== 0 ? c.uint.decode(state) : 0,
|
||||
y: (flags & 2) !== 0 ? c.string.decode(state) : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/duplex-out
|
||||
const encoding7 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 2 so always one byte
|
||||
|
||||
if (m.result) c.string.preencode(state, m.result)
|
||||
if (m.n) c.uint.preencode(state, m.n)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = (m.result ? 1 : 0) | (m.n ? 2 : 0)
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.result) c.string.encode(state, m.result)
|
||||
if (m.n) c.uint.encode(state, m.n)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
result: (flags & 1) !== 0 ? c.string.decode(state) : null,
|
||||
n: (flags & 2) !== 0 ? c.uint.decode(state) : 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @bridgeswarm/notify-request
|
||||
const encoding8 = {
|
||||
preencode(state, m) {
|
||||
state.end++ // max flag is 2 so always one byte
|
||||
|
||||
if (m.event) c.string.preencode(state, m.event)
|
||||
if (m.payload) c.uint.preencode(state, m.payload)
|
||||
},
|
||||
encode(state, m) {
|
||||
const flags = (m.event ? 1 : 0) | (m.payload ? 2 : 0)
|
||||
|
||||
c.uint.encode(state, flags)
|
||||
|
||||
if (m.event) c.string.encode(state, m.event)
|
||||
if (m.payload) c.uint.encode(state, m.payload)
|
||||
},
|
||||
decode(state) {
|
||||
const flags = c.uint.decode(state)
|
||||
|
||||
return {
|
||||
event: (flags & 1) !== 0 ? c.string.decode(state) : null,
|
||||
payload: (flags & 2) !== 0 ? c.uint.decode(state) : 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setVersion(v) {
|
||||
version = v
|
||||
}
|
||||
|
||||
function encode(name, value, v = VERSION) {
|
||||
version = v
|
||||
return c.encode(getEncoding(name), value)
|
||||
}
|
||||
|
||||
function decode(name, buffer, v = VERSION) {
|
||||
version = v
|
||||
return c.decode(getEncoding(name), buffer)
|
||||
}
|
||||
|
||||
function getEnum(name) {
|
||||
switch (name) {
|
||||
default:
|
||||
throw new Error('Enum not found ' + name)
|
||||
}
|
||||
}
|
||||
|
||||
function getEncoding(name) {
|
||||
switch (name) {
|
||||
case '@bridgeswarm/ping-request':
|
||||
return encoding0
|
||||
case '@bridgeswarm/ping-response':
|
||||
return encoding1
|
||||
case '@bridgeswarm/stream-request-chunk':
|
||||
return encoding2
|
||||
case '@bridgeswarm/stream-sum-response':
|
||||
return encoding3
|
||||
case '@bridgeswarm/fetch-request':
|
||||
return encoding4
|
||||
case '@bridgeswarm/fetch-chunk':
|
||||
return encoding5
|
||||
case '@bridgeswarm/duplex-in':
|
||||
return encoding6
|
||||
case '@bridgeswarm/duplex-out':
|
||||
return encoding7
|
||||
case '@bridgeswarm/notify-request':
|
||||
return encoding8
|
||||
default:
|
||||
throw new Error('Encoder not found ' + name)
|
||||
}
|
||||
}
|
||||
|
||||
function getStruct(name, v = VERSION) {
|
||||
const enc = getEncoding(name)
|
||||
return {
|
||||
preencode(state, m) {
|
||||
version = v
|
||||
enc.preencode(state, m)
|
||||
},
|
||||
encode(state, m) {
|
||||
version = v
|
||||
enc.encode(state, m)
|
||||
},
|
||||
decode(state) {
|
||||
version = v
|
||||
return enc.decode(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const resolveStruct = getStruct // compat
|
||||
|
||||
module.exports = {
|
||||
resolveStruct,
|
||||
getStruct,
|
||||
getEnum,
|
||||
getEncoding,
|
||||
encode,
|
||||
decode,
|
||||
setVersion,
|
||||
version
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
{
|
||||
"version": 1,
|
||||
"schema": [
|
||||
{
|
||||
"name": "ping-request",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "value",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "ping-response",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "pong",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stream-request-chunk",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "n",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"name": "label",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stream-sum-response",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "sum",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"name": "count",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "fetch-request",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "count",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "fetch-chunk",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "i",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"name": "data",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "duplex-in",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "x",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"name": "y",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "duplex-out",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "result",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"name": "n",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "notify-request",
|
||||
"namespace": "bridgeswarm",
|
||||
"compact": false,
|
||||
"flagsPosition": 0,
|
||||
"fields": [
|
||||
{
|
||||
"name": "event",
|
||||
"type": "string",
|
||||
"version": 1
|
||||
},
|
||||
{
|
||||
"name": "payload",
|
||||
"type": "uint",
|
||||
"version": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user