You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@protobufjs/codegen

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@protobufjs/codegen

A minimalistic code generation utility.


Version published
Weekly downloads
13M
increased by0.48%
Maintainers
2
Install size
9.93 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

@protobufjs/codegen

npm

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") // A function with parameters "a" and "b" named "add"
  ("// awesome comment")             // adds the line to the function's body
  ("return a + b - c + %d", 1)       // replaces %d with 1 and adds the line to the body
  ({ c: 1 });                        // adds "c" with a value of 1 to the function's scope

console.log(add.toString()); // function add(a, b) { return a + b - c + 1 }
console.log(add(1, 2));      // calculates 1 + 2 - 1 + 1 = 3

License: BSD 3-Clause License

FAQs

Package last updated on 09 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc