What is @protobufjs/codegen?
@protobufjs/codegen is a utility for generating JavaScript code dynamically. It is particularly useful in the context of Protocol Buffers, where it can be used to generate efficient serialization and deserialization code.
What are @protobufjs/codegen's main functionalities?
Dynamic Code Generation
This feature allows you to dynamically generate JavaScript functions. In this example, a function `encode` is generated which calls a method `m.encode` on a message and returns the result.
const codegen = require('@protobufjs/codegen');
const gen = codegen('m', 'exports')
('function encode(message) {')
(' return m.encode(message).finish();')
('}');
console.log(gen.toString());
Function Compilation
This feature allows you to compile the generated code into a real JavaScript function. In this example, the `encode` function is compiled and can be used directly.
const codegen = require('@protobufjs/codegen');
const gen = codegen('m', 'exports')
('function encode(message) {')
(' return m.encode(message).finish();')
('}');
const encode = gen(m, {});
console.log(encode.toString());
Other packages similar to @protobufjs/codegen
protobufjs
protobufjs is a comprehensive library for working with Protocol Buffers in JavaScript. It includes functionality for parsing .proto files, encoding and decoding messages, and more. While @protobufjs/codegen focuses on dynamic code generation, protobufjs provides a full suite of tools for working with Protocol Buffers.
google-protobuf
google-protobuf is the official Protocol Buffers library for JavaScript provided by Google. It offers similar functionalities for encoding and decoding Protocol Buffers messages but does not focus on dynamic code generation like @protobufjs/codegen.
typescript-json-schema
typescript-json-schema is a tool for generating JSON schemas from TypeScript types. While it is not directly related to Protocol Buffers, it shares the concept of generating code (in this case, schemas) from a defined structure. It does not offer dynamic code generation like @protobufjs/codegen.
@protobufjs/codegen
A minimalistic code generation utility.
API
-
codegen([functionParams: string[]
], [functionName: string]): Codegen
Begins generating a function.
-
codegen.verbose = false
When set to true, codegen will log generated code to console. Useful for debugging.
Invoking codegen returns an appender function that appends code to the function's body and returns itself:
-
Codegen(formatString: string
, [...formatParams: any
]): Codegen
Appends code to the function's body. The format string can contain placeholders specifying the types of inserted format parameters:
%d
: Number (integer or floating point value)%f
: Floating point value%i
: Integer value%j
: JSON.stringify'ed value%s
: String value%%
: Percent sign
-
Codegen([scope: Object.<string,*>
]): Function
Finishes the function and returns it.
-
Codegen.toString([functionNameOverride: string
]): string
Returns the function as a string.
Example
var codegen = require("@protobufjs/codegen");
var add = codegen(["a", "b"], "add")
("// awesome comment")
("return a + b - c + %d", 1)
({ c: 1 });
console.log(add.toString());
console.log(add(1, 2));
License: BSD 3-Clause License