← Back to Official Package Registry
Tier 2: Distributed & Wire Protocols • Protobuf & HTTP/2 gRPCv1.0.0 • 58 KB • Ed25519 Verified

grpc_proto@1.0.0

Protocol Buffers v3 binary wire codec (varint, zigzag, fixed32/64, length-delimited), homoiconic schema & .proto emitter, and gRPC framing.

use [grpc_proto]use [grpc_proto/proto]
veko pkg add grpc_proto@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

grpc_proto

Pure-Veko binary Protocol Buffers (proto3) wire codec, homoiconic schema DSL, .proto IDL generator, and gRPC over HTTP/2 service router (grpc_proto, grpc_proto/proto).

Overview

grpc_proto implements the binary Protocol Buffers wire format and gRPC Length-Prefixed-Message HTTP/2 transport framing in pure Veko with deterministic ORO single-owner memory and zero external C dependencies:

  • grpc_proto/proto:
  • Low-level binary wire encoders and decoders for varints (int64, uint64, int32, uint32, bool, enum), ZigZag signed integers (sint64, sint32), 64-bit little-endian (fixed64, sfixed64, IEEE-754 double), 32-bit little-endian (fixed32, sfixed32), length-delimited bytes/str/sub-messages, packed repeated scalar slices, and forward-compatible unknown-field skipping.
  • Homoiconic schema DSL (pb/define-message) built over Veko quote [...] blocks with nested message composition (pb/with-subschema) and .proto file emission (pb/to-proto3-idl).
  • grpc_proto:
  • 5-byte gRPC Length-Prefixed-Message framing (1B compressed_flag + 4B big-endian length + payload) with optional RFC 8878 Zstandard (compress/zstd) payload compression and multi-frame server-streaming buffers.
  • Canonical gRPC status codes (0..16), grpc-timeout header parsing/formatting, percent-encoded grpc-message trailers, unary and server-streaming RPC service registry (new-server, register-unary, register-server-stream, dispatch-rpc), and a direct net/http2 handler/client adapter.

Installation

powershell
veko pkg add grpc_proto@1.0.0

Import the modules in .vk source files:

rebol
use [
    grpc = grpc_proto
    pb   = grpc_proto/proto
]

Wire Protocol & Architecture Notes

Protocol Buffers Binary Wire Format

Each field on the wire begins with a base-128 LEB128 varint key encoding (field_number << 3) | wire_type:

Wire TypeIDProto3 TypesEncoding
WIRE-VARINT0int32, int64, uint32, uint64, sint32, sint64, bool, enumBase-128 LEB128 (sint* uses (n << 1) ^ (n >> 63) ZigZag)
WIRE-I641fixed64, sfixed64, double8-byte little-endian
WIRE-LEN2string, bytes, nested message, packed repeated scalarsVarint byte length followed by raw payload bytes
WIRE-I325fixed32, sfixed324-byte little-endian

Per proto3 canonical rules, scalar fields equal to their type's zero value (0, 0.0, false, '', empty bytes) are omitted during serialization and restored as explicit defaults during pb/decode-msg. Packed repeated numeric fields serialize under a single WIRE-LEN span while pb/decode-msg accepts both packed (WIRE-LEN) and unpacked wire representations. Unknown field tags encountered on the wire are skipped cleanly via pb/skip-field.

gRPC over HTTP/2 Framing

Every gRPC request and response message on an HTTP/2 DATA stream uses a 5-byte prefix:

text
+--------------------+-------------------------------------------+--------------------+
| Compressed-Flag 1B | Message-Length 4B (uint32 big-endian)     | Message Payload    |
+--------------------+-------------------------------------------+--------------------+
| 0x00 or 0x01       | N bytes                                   | Protobuf / Zstd    |
+--------------------+-------------------------------------------+--------------------+

Unary calls carry a single 5-byte-framed message; server-streaming responses concatenate zero or more 5-byte-framed messages followed by grpc-status and percent-encoded grpc-message HTTP/2 metadata.

Complete API Reference

grpc_proto/proto

  • Wire constants (0-arg inline functions): WIRE-VARINT, WIRE-I64, WIRE-LEN, WIRE-I32
  • Low-level primitives:
  • encode-tag [field_num i64, wire_type i64] i64
  • push-tag [mut out bytes, field_num i64, wire_type i64] i64
  • read-tag [buf bytes, off i64] WireTag
  • push-varint [mut out bytes, v i64] i64 / read-varint [buf bytes, off i64] bits/VarintVal
  • zigzag-encode [n i64] i64 / zigzag-decode [u i64] i64
  • push-sint64 [mut out bytes, n i64] i64 / read-sint64 [buf bytes, off i64] bits/VarintVal
  • push-fixed64 [mut out bytes, v i64] i64 / read-fixed64 [buf bytes, off i64] FixedI64Val
  • push-double [mut out bytes, x f64] i64 / read-double [buf bytes, off i64] FixedF64Val
  • push-fixed32 [mut out bytes, v i64] i64 / read-fixed32 [buf bytes, off i64] FixedI64Val / read-sfixed32 [buf bytes, off i64] FixedI64Val
  • push-len-bytes [mut out bytes, payload bytes] i64 / push-len-str [mut out bytes, s str] i64 / read-len-bytes [buf bytes, off i64] LenSlice
  • encode-packed-varints [vals block] bytes / decode-packed-varints [payload bytes] block[i64]
  • encode-packed-sint64s [vals block] bytes / decode-packed-sint64s [payload bytes] block[i64]
  • skip-field [buf bytes, off i64, wire_type i64] i64
  • Homoiconic schema & IDL:
  • define-message [name str, spec block] MessageSchema
  • register-subschema [mut parent MessageSchema, child MessageSchema] enum
  • with-subschema [parent MessageSchema, child MessageSchema] MessageSchema
  • with-package [schema MessageSchema, pkg_name str] MessageSchema
  • encode-msg [schema MessageSchema, val map] bytes
  • decode-msg [schema MessageSchema, wire_bytes bytes] map
  • to-proto3-idl [schema MessageSchema] str

grpc_proto

  • Status constants (0-arg inline functions): STATUS-OK (0), STATUS-CANCELLED (1), STATUS-UNKNOWN (2), STATUS-INVALID-ARGUMENT (3), STATUS-DEADLINE-EXCEEDED (4), STATUS-NOT-FOUND (5), STATUS-ALREADY-EXISTS (6), STATUS-PERMISSION-DENIED (7), STATUS-RESOURCE-EXHAUSTED (8), STATUS-FAILED-PRECONDITION (9), STATUS-ABORTED (10), STATUS-OUT-OF-RANGE (11), STATUS-UNIMPLEMENTED (12), STATUS-INTERNAL (13), STATUS-UNAVAILABLE (14), STATUS-DATA-LOSS (15), STATUS-UNAUTHENTICATED (16), status-name [code i64] str, grpc-error [code i64, message str] GrpcStatus
  • Framing & headers:
  • encode-frame [payload bytes /compressed flag bool = false /zstd use_zstd bool = false] bytes
  • encode-stream-frames [payloads block[bytes] /compressed flag bool = false /zstd use_zstd bool = false] bytes
  • decode-frame-at [wire bytes, off i64 /zstd auto_zstd bool = true] FrameStep
  • decode-frames [wire bytes] block[GrpcFrame]
  • parse-timeout-ns [spec str] i64 / format-timeout-ns [ns i64] str
  • encode-grpc-message [msg str] str / decode-grpc-message [enc str] str
  • Service router & HTTP/2 integration:
  • new-server [] GrpcServer
  • register-unary [mut srv GrpcServer, service str, method str, req_schema pb/MessageSchema, resp_schema pb/MessageSchema, handler fn[GrpcContext, map -> any]] enum
  • register-server-stream [mut srv GrpcServer, service str, method str, req_schema pb/MessageSchema, resp_schema pb/MessageSchema, handler fn[GrpcContext, map -> any]] enum
  • dispatch-rpc [srv GrpcServer, path str, wire_body bytes, metadata map[str]] GrpcResponse
  • build-request-body [req_schema pb/MessageSchema, req_map map /zstd use_zstd bool = false] bytes
  • decode-unary-response [resp GrpcResponse, resp_schema pb/MessageSchema] map
  • decode-stream-response [resp GrpcResponse, resp_schema pb/MessageSchema] block
  • http2-handler [srv GrpcServer] fn[h2/Message -> h2/Message]
  • from-http2-message [msg h2/Message] GrpcResponse
  • http2-invoke [mut conn h2/H2Conn, authority str, service str, method str, req_schema pb/MessageSchema, req_map map, metadata map[str]] GrpcResponse

Idiomatic Veko Examples

Define Homoiconic Protobuf Schema, Encode/Decode & Emit .proto

rebol
use [
    grpc = grpc_proto
    pb   = grpc_proto/proto
]

telemetry_event = (pb/define-message 'TelemetryEvent' (quote [
    1 trace_id str,
    2 span_id i64,
    3 latency_us sint64,
    4 ok bool,
    5 tags repeated str
])) \ pb/with-package 'veko.telemetry.v1'

wire = pb/encode-msg telemetry_event {
    trace_id   = '4bf92f3577b34da6'
    span_id    = 9001
    latency_us = -350
    ok         = true
    tags       = ['edge' 'ca-west']
}

decoded = pb/decode-msg telemetry_event wire
print decoded.latency_us
print (pb/to-proto3-idl telemetry_event)

Unary and Server-Streaming gRPC Service Dispatch

rebol
use [
    grpc = grpc_proto
    pb   = grpc_proto/proto
]

ping_req  = pb/define-message 'PingReq'  (quote [ 1 target str, 2 count i64 ])
ping_resp = pb/define-message 'PingResp' (quote [ 1 seq i64, 2 reply str ])

mut srv = grpc/new-server

_ = grpc/register-server-stream mut srv 'veko.net.Health' 'WatchPing' ping_req ping_resp fn [ctx, req] [
    mut out = []
    mut i = 0
    while i < req.count [
        push mut out { seq = i, reply = (str/join ['pong:' req.target] '') }
        i = i + 1
    ]
    out
]

req_body = grpc/build-request-body ping_req { target = 'node-a', count = 3 }
resp = grpc/dispatch-rpc srv '/veko.net.Health/WatchPing' req_body {}
items = grpc/decode-stream-response resp ping_resp
print (len items)
packages/grpc_proto/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/grpc_proto/examples/demo.vk
// Self-contained verification suite & runnable example for `grpc_proto` and `grpc_proto/proto`.

use [
	grpc = grpc_proto
	pb   = grpc_proto/proto
	h2   = net/http2
]

assert-true = fn [cond bool, label str] enum [
	if not cond [
		println (str/join ['[FAIL] ' label] '')
		os/exit 1
	]
	print (str/join ['[PASS] ' label] '')
	.ok
]

// --- 1. Low-Level Protobuf Wire Format Primitives ---
mut raw_buf = bytes/with-cap 64
_ = pb/push-sint64 mut raw_buf -1
_ = pb/push-sint64 mut raw_buf -9876543210
_ = pb/push-fixed64 mut raw_buf 0x0102030405060708
_ = pb/push-double mut raw_buf -3.14159265
_ = pb/push-fixed32 mut raw_buf 0xdeadbeef

z0 = pb/read-sint64 raw_buf 0
z1 = pb/read-sint64 raw_buf z0.next_off
f64_i = pb/read-fixed64 raw_buf z1.next_off
dbl = pb/read-double raw_buf f64_i.next_off
f32_i = pb/read-fixed32 raw_buf dbl.next_off

_ = assert-true (z0.next_off == 1 and z0.value == -1 and z1.value == -9876543210) 'ZigZag sint64 encodes -1 in 1 byte and roundtrips large negative integers'
_ = assert-true (f64_i.value == 0x0102030405060708 and f32_i.value == 0xdeadbeef and dbl.value < -3.1415 and dbl.value > -3.1416) 'fixed64, fixed32, and IEEE-754 double little-endian wire primitives'

packed_z = pb/encode-packed-sint64s [-500 0 500 -1 42]
unpacked_z = pb/decode-packed-sint64s packed_z
_ = assert-true ((len unpacked_z) == 5 and unpacked_z[0] == -500 and unpacked_z[3] == -1 and unpacked_z[4] == 42) 'packed repeated sint64 slice roundtrip'

// --- 2. Homoiconic Message Schema, Nested Messages & `.proto` IDL Generation ---
span_attr = pb/define-message 'SpanAttr' (quote [
	1 key str,
	2 val str
])

telemetry_event = (pb/define-message 'TelemetryEvent' (quote [
	1 trace_id str,
	2 span_id i64,
	3 latency_us sint64,
	4 ok bool,
	5 tags repeated str,
	6 skew_deltas repeated sint64,
	7 cpu_ratio double,
	8 attrs repeated SpanAttr
])) \ pb/with-subschema span_attr \ pb/with-package 'veko.telemetry.v1'

idl_txt = pb/to-proto3-idl telemetry_event
idl_ok = (str/contains? idl_txt 'syntax = "proto3";') and (str/contains? idl_txt 'package veko.telemetry.v1;') and (str/contains? idl_txt 'message SpanAttr {') and (str/contains? idl_txt 'sint64 latency_us = 3;') and (str/contains? idl_txt 'repeated SpanAttr attrs = 8;')
_ = assert-true idl_ok 'to-proto3-idl emits canonical proto3 schema text'

event_in = {
	trace_id    = '4bf92f3577b34da6a3ce929d0e0e4736'
	span_id     = 81985529216486895
	latency_us  = -1425
	ok          = true
	tags        = ['edge' 'grpc' 'ca-west-1']
	skew_deltas = [-120 0 45 -99999]
	cpu_ratio   = 0.875
	attrs       = [
		{ key = 'http.method', val = 'POST' }
		{ key = 'rpc.system',  val = 'grpc' }
	]
}

mut wire_ev = pb/encode-msg telemetry_event event_in
// Append an unknown future field (#99 varint = 777) to verify forward-compatible skipping
_ = pb/encode-varint-field mut wire_ev 99 777

event_out = pb/decode-msg telemetry_event wire_ev
ev_ok_1 = event_out.trace_id == '4bf92f3577b34da6a3ce929d0e0e4736' and event_out.span_id == 81985529216486895 and event_out.latency_us == -1425 and event_out.ok == true
ev_ok_2 = (len event_out.tags) == 3 and event_out.tags[2] == 'ca-west-1' and (len event_out.skew_deltas) == 4 and event_out.skew_deltas[0] == -120 and event_out.skew_deltas[3] == -99999
ev_ok_3 = event_out.cpu_ratio == 0.875 and (len event_out.attrs) == 2 and event_out.attrs[1].val == 'grpc'
_ = assert-true (ev_ok_1 and ev_ok_2 and ev_ok_3) 'nested + packed-repeated Protobuf wire roundtrip with unknown field skipping'

// --- 3. gRPC 5-Byte Framing, Unary RPC, Server-Streaming RPC & HTTP/2 Adapter ---
ingest_ack = pb/define-message 'IngestAck' (quote [
	1 accepted bool,
	2 ingest_id str,
	3 recorded_tags i64
])

stream_query = pb/define-message 'StreamQuery' (quote [
	1 service_filter str,
	2 limit i64
])

mut srv = grpc/new-server

_ = grpc/register-unary mut srv 'veko.telemetry.v1.Collector' 'ReportSpan' telemetry_event ingest_ack fn [ctx, req] [
	if req.trace_id == 'missing' [
		return (grpc/grpc-error (grpc/STATUS-NOT-FOUND) 'trace not found: missing')
	]
	{
		accepted      = req.ok
		ingest_id     = str/join ['ing-' req.trace_id[0..8]] ''
		recorded_tags = len req.tags
	}
]

_ = grpc/register-server-stream mut srv 'veko.telemetry.v1.Collector' 'WatchSpans' stream_query telemetry_event fn [ctx, q] [
	mut items = []
	mut idx = 0
	while idx < q.limit [
		push mut items {
			trace_id    = str/join [q.service_filter '-trace-' (mold idx)] ''
			span_id     = 1000 + idx
			latency_us  = (0 - 50) * (idx + 1)
			ok          = true
			tags        = [q.service_filter 'stream']
			skew_deltas = [-10 10]
			cpu_ratio   = 0.25
			attrs       = [ { key = 'seq', val = (mold idx) } ]
		}
		idx = idx + 1
	]
	items
]

// Unary call (with Zstd frame compression enabled via metadata)
unary_req_wire = grpc/build-request-body/zstd telemetry_event event_in true
unary_resp = grpc/dispatch-rpc srv '/veko.telemetry.v1.Collector/ReportSpan' unary_req_wire {
	'grpc-encoding' = 'zstd'
	'grpc-timeout'  = '250m'
}
ack = grpc/decode-unary-response unary_resp ingest_ack
unary_ok = unary_resp.status.code == (grpc/STATUS-OK) and ack.accepted == true and ack.ingest_id == 'ing-4bf92f35' and ack.recorded_tags == 3
_ = assert-true unary_ok 'unary gRPC dispatch + Zstd compressed 5-byte frame roundtrip'

// Domain error status check (`NOT_FOUND = 5`) and `UNIMPLEMENTED = 12`
mut missing_ev = event_in
map/set mut missing_ev 'trace_id' 'missing'
not_found_wire = grpc/build-request-body telemetry_event missing_ev
nf_resp = grpc/dispatch-rpc srv '/veko.telemetry.v1.Collector/ReportSpan' not_found_wire {}
unimpl_resp = grpc/dispatch-rpc srv '/veko.telemetry.v1.Collector/UnknownRpc' not_found_wire {}
err_ok = nf_resp.status.code == (grpc/STATUS-NOT-FOUND) and unimpl_resp.status.code == (grpc/STATUS-UNIMPLEMENTED)
_ = assert-true err_ok 'gRPC NOT_FOUND (5) and UNIMPLEMENTED (12) status propagation'

// Server-streaming call via HTTP/2 `net/http2` handler adapter
h2_fn = grpc/http2-handler srv
stream_req_wire = grpc/build-request-body stream_query { service_filter = 'auth-gw', limit = 3 }
h2_req = h2/Message {
	stream_id = 1
	method    = 'POST'
	path      = '/veko.telemetry.v1.Collector/WatchSpans'
	headers   = { 'content-type' = 'application/grpc+proto' }
	body      = stream_req_wire
}
h2_raw_resp = (h2_fn h2_req)
stream_grpc_resp = grpc/from-http2-message h2_raw_resp
stream_events = grpc/decode-stream-response stream_grpc_resp telemetry_event
stream_ok = (len stream_events) == 3 and stream_events[0].trace_id == 'auth-gw-trace-0' and stream_events[2].latency_us == -150 and stream_events[2].attrs[0].val == '2'
_ = assert-true stream_ok 'server-streaming multi-frame gRPC call over net/http2 adapter'

print 'grpc_proto: all verification checks passed'
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/grpc_proto/1.0.0.vkp
Archive SHA-256: 01f021960ddbd7143cf6a3a3df721a15b6bee19cbb68ad0a6cea052081f1f794
Merkle Root: 45135de8ec0e99de4d6486abb3880374d712fe3332ff5519d64db6048fbc013f
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"grpc_proto","version":"1.0.0","tier":2.0,"tier_label":"Tier 2: Distributed & Wire Protocols","category":"Protobuf & HTTP/2 gRPC","description":"Protocol Buffers v3 binary wire codec (varint, zigzag, fixed32/64, length-delimited), homoiconic schema & .proto emitter, and gRPC framing.","modules":["grpc_proto","grpc_proto/proto"],"homepage":"https://veko-lang.org/packages/grpc_proto"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md8 KBe086b1dc390b7bf22bd51446ea77cd9960101c6fa3cc101137a5509a5f678295
examples/demo.vk6 KBdc5409bab719c1585a91c591c4a09fbd664620f3843be6bb74288d8f83e313b6
mod.vk17 KB633c86ddcea94dbc59e82a76676ea7ac6abcb1b075f6c85bfd1547ff7ac57e61
proto.vk24 KB1fb2e782c47c35c93c5811781a4656144dd770ebfcbce955c3ccf415b7039d4d