protoruf — LLM Usage Prompt (Node.js)¶
Feed this file to an LLM (or paste it into a system prompt) so it can use the Node.js target of
protorufcorrectly with no other context. It is a complete, self-contained reference for version 0.2.0.
What protoruf is¶
protoruf converts between JSON and Protobuf dynamically — no protoc, no
generated classes, no codegen step. You compile a .proto to a descriptor set
once, then convert JSON ↔ Protobuf wire bytes in both directions. The Node target
is a native addon (built with napi-rs) wrapping a pure-Rust engine, so it runs at
Rust speed and needs no Rust toolchain at install time.
Core idea: a descriptor (compiled schema, a Buffer) + a messageType (fully
qualified name like "user.User") are required for every conversion.
Install¶
The npm registry release is coming soon. For v0.2.0, install the self-contained prebuilt tarball by URL:
npm install https://github.com/EdwinAlkins/protoruf/releases/download/v0.2.0/protoruf-node-0.2.0.tgz
Requires Node.js ≥ 18 (N-API v8). The tarball bundles native binaries for
Linux x64/arm64, macOS x64/arm64, and Windows x64; the loader picks the right one
at runtime. TypeScript definitions ship in the package (index.d.ts) — no
@types/... needed. Once published, the install becomes npm i @protoruf/node
with identical imports.
The mental model (3 steps)¶
- Compile a
.protoschema to a descriptor (Buffer) — do this once. - Convert with
jsonToProtobuf/protobufToJsonusing that descriptor and amessageType. - For throughput, wrap the descriptor in a
DescriptorCacheand reuse it.
Complete public API¶
Binary values are Node Buffers (which are Uint8Arrays at runtime). Every
function throws a standard Error on failure.
Compilation¶
Compile a.proto from disk. includePaths defaults to the parent directory
of protoPath. Node-only (reads the filesystem).
function compileProtoFromSources(
files: Record<string, string>,
root: string,
includeImports?: boolean, // default true
): Buffer;
.proto sources held in memory (no filesystem). files maps a
logical filename → its source text; imports resolve by name within this map, and
Google well-known types are provided automatically. root is the entry file (a
key of files). includeImports=true embeds imported files so the descriptor is
self-contained.
Conversion (one-shot)¶
function jsonToProtobuf(jsonStr: string, descriptorBytes: Buffer, messageType: string): Buffer;
function protobufToJson(protobufBytes: Buffer, descriptorBytes: Buffer, messageType: string, pretty?: boolean): string;
messageType is the fully qualified name "<package>.<Message>", e.g.
"user.User". pretty (default false) indents the JSON output. These
re-resolve the message type on every call; for hot loops use DescriptorCache.
DescriptorCache (high-throughput)¶
Free functions reuse decoded pools through a process-wide LRU, but hash the
descriptor bytes and resolve the message type on every call. DescriptorCache
holds the pool and memoizes message descriptors, avoiding that per-call work.
One instance handles every message type in the descriptor.
class DescriptorCache {
constructor(descriptorBytes: Buffer);
jsonToProtobuf(jsonStr: string, messageType: string): Buffer;
protobufToJson(protobufBytes: Buffer, messageType: string, pretty?: boolean): string;
}
descriptorBytes argument (the pool is already
decoded). Output format is identical to the free functions.
Canonical examples¶
Minimal round-trip (from disk)¶
import { compileProto, jsonToProtobuf, protobufToJson } from "@protoruf/node";
const descriptor = compileProto("message.proto");
const pb = jsonToProtobuf('{"id":"123"}', descriptor, "message.Message");
console.log(protobufToJson(pb, descriptor, "message.Message", true));
Compile from in-memory sources¶
import { compileProtoFromSources, jsonToProtobuf } from "@protoruf/node";
const descriptor = compileProtoFromSources(
{ "user.proto": 'syntax="proto3"; package user; message User { string id = 1; string email = 2; }' },
"user.proto",
);
const pb = jsonToProtobuf('{"id":"1","email":"a@b.com"}', descriptor, "user.User");
High-throughput loop with DescriptorCache¶
import { compileProto, DescriptorCache } from "@protoruf/node";
const cache = new DescriptorCache(compileProto("schema.proto")); // decode pool once
for (const jsonData of jsonStream) {
const pb = cache.jsonToProtobuf(jsonData, "message.Message");
process(pb);
}
const restored = cache.protobufToJson(pb, "message.Message", true);
JSON shape — READ THIS (non-standard)¶
protoruf uses a specific, non-default JSON mapping. When generating or validating JSON for it, follow these rules exactly:
- Field names are proto names (
snake_case), NOT the camelCase of canonical protobuf JSON. Usecreated_at, notcreatedAt. - Enums are emitted as numbers (their integer value), not names.
- 64-bit integers (
int64/uint64/…) are JSON numbers, not strings. ⚠️ In JavaScript, numbers aboveNumber.MAX_SAFE_INTEGER(2^53 − 1) lose precision. For exact large 64-bit values, parse the JSON with a BigInt-aware reader;JSON.parsewill silently round. bytesfields map to standard base64-encoded strings.- Fields at their proto3 default (0,
"",false, empty repeated/map) are omitted from the output JSON. Don't rely on them being present after a round-trip; re-parsing yields the default anyway. - Standard proto3 JSON applies otherwise (nested messages → objects, repeated →
arrays, map → object,
oneof→ the single set field, well-known types likeTimestamp/Duration/Structuse their canonical JSON forms).
Errors¶
All functions throw a standard Error whose message describes the failure:
proto compilation errors (syntax, missing import), invalid JSON, an unknown
messageType, or a protobuf decode/serialize failure. Wrap calls in try/catch.
Rules for the LLM¶
- Always compile (or load) a descriptor before converting; every conversion
needs both
descriptorBytesand a fully-qualifiedmessageType. messageTypemust be"<package>.<Message>"matching the.proto'spackageand message name — a wrong name throws.compileProto(path)reads the filesystem and is Node-only — for the browser use the WASM target'scompileProtoFromSources.- For more than a few conversions, build one
DescriptorCacheand reuse it; don't call the free functions in a tight loop. - Emit JSON in protoruf's shape (snake_case fields, numeric enums, numeric 64-bit ints) — do not assume canonical camelCase protobuf JSON.
- Binary payloads are
Buffers; pass the descriptorBufferthrough unchanged.