API Reference (Browser / WASM)¶
Complete API for @protoruf/wasm. All binary values are Uint8Array. Every function throws
on error. Call await init() once before any other call.
init() — default export¶
Loads and instantiates the WASM module. With a bundler you can await init() with no arguments;
otherwise pass a URL / Response / bytes for the .wasm. Call it once before using any
other export.
The
nodejswasm-pack target initialises synchronously and does not requireinit().
compileProtoFromSources()¶
Compile .proto sources provided in memory — no filesystem access.
| Parameter | Type | Required | Description |
|---|---|---|---|
files |
Record<string, string> |
yes | Map of logical file name → source text. imports resolve by name; Google well-known types are provided automatically. |
root |
string |
yes | Entry file (a key of files) |
Returns Uint8Array. Throws if compilation fails or root/an import is missing.
compileProto(path)is intentionally not exposed in WASM (no filesystem).
jsonToProtobuf()¶
function jsonToProtobuf(jsonStr: string, descriptorBytes: Uint8Array, messageType: string): Uint8Array;
| Parameter | Type | Description |
|---|---|---|
jsonStr |
string |
Valid JSON to convert |
descriptorBytes |
Uint8Array |
Descriptor from compileProtoFromSources |
messageType |
string |
Fully-qualified message name (e.g. "user.User") |
Returns Uint8Array — the encoded message. Throws on invalid JSON or unknown messageType.
protobufToJson()¶
function protobufToJson(
protobufBytes: Uint8Array,
descriptorBytes: Uint8Array,
messageType: string,
pretty?: boolean,
): string;
| Parameter | Type | Description |
|---|---|---|
protobufBytes |
Uint8Array |
Encoded message |
descriptorBytes |
Uint8Array |
Descriptor set |
messageType |
string |
Fully-qualified message name |
pretty |
boolean |
Indent the output (optional, default false) |
Returns string. Throws on decode/serialization failure.
class DescriptorCache¶
A reusable, pre-decoded descriptor pool. Decode the pool once and reuse it across conversions.
class DescriptorCache {
constructor(descriptorBytes: Uint8Array);
jsonToProtobuf(jsonStr: string, messageType: string): Uint8Array;
protobufToJson(protobufBytes: Uint8Array, messageType: string, pretty?: boolean): string;
free(): void;
}
- constructor throws if
descriptorBytesis not a valid descriptor set jsonToProtobuf/protobufToJsonthrow on invalid input or unknownmessageTypefree()releases the instance's WASM linear-memory state — call it when done, or use ausingdeclaration (the class implementsSymbol.dispose)
using cache = new DescriptorCache(descriptor);
const wire = cache.jsonToProtobuf('{"id":"123"}', "user.User");
const json = cache.protobufToJson(wire, "user.User", false);
JSON shape¶
Output matches protoruf's shared format across all targets:
- field names use the proto field name (snake_case)
- enums are emitted as their numeric value (input accepts name or number)
- 64-bit integers are emitted as JSON numbers —
JSON.parseloses precision above 2^53 - proto3 default values are omitted
Errors¶
Failures throw a standard Error; inspect err.message (e.g. Invalid JSON: ...,
Message type '...' not found in descriptor).