
Company News
Socket Joins the OpenJS Foundation
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.
protobuf2flowtype
Advanced tools
Given protobuf file will generate node packages which export protobufjs builders annotated with flowtype type anotations
The aim of this project is to allow flowtype checking of protobuf messages created using protobufjs
This is achieved by generating modules that match the namespaces within your protobuf deinition which export the protobufjs builder for that namespace but also include all the nessisary flowtype annotations for the messages and enums declared in that namespace.
The generated code has a runtime dependency on this module for flowtype checking only. The generated code includes the flowtype annotations inside special comment blocks so the code is fully ES5 compatible and doesn't require transpiling to run.
npm install --save protobuf2flowtype
node_modules/.bin/protobuf2flowtype --outDir src/proto --protoFile main.proto --protoDir proto
const generate = require('protobuf2flowtype/build/generate');
generate('src/proto', 'main.proto', 'proto');
###main.proto
package foo;
message Greeting {
required string text = 1;
};
message Chat {
required Greeting greeting = 1;
optional string body = 2;
};
###generated code
.
├── foo
│ ├── Chat
│ │ └── index.js
│ ├── Greeting
│ │ └── index.js
│ └── index.js
└── index.js
###index.js
// @flow
Object.defineProperty(exports, "__esModule", {
value: true
});
const _package = require('./package.json');
const jsonDescriptor = {
"package": "foo",
"messages": [{
"name": "Greeting",
"fields": [{
"rule": "required",
"type": "string",
"name": "text",
"id": 1
}]
}, {
"name": "Chat",
"fields": [{
"rule": "required",
"type": "Greeting",
"name": "greeting",
"id": 1
}, {
"rule": "optional",
"type": "string",
"name": "body",
"id": 2
}]
}]
};
const ProtoBuf = require('protobufjs');
const builder = exports.builder = ProtoBuf.loadJson(jsonDescriptor);
exports.default = builder;
###foo/index.js
// @flow
Object.defineProperty(exports, "__esModule", {
value: true
});
const _ = require('../index');
exports.builder = _.builder; /*::
import { Protobuf$Message } from 'protobuf2flowtype';
import type { ByteBuffer, ProtoBuf$Builder, ProtoBuf$MessageLiteral, Protobuf$RefectType } from 'protobuf2flowtype';
interface GreetingInterface extends ProtoBuf$MessageLiteral {
text: string
}
;
type GreetingReflect = Protobuf$RefectType<GreetingInterface>;
declare class GreetingBuilder extends Protobuf$Message<GreetingInterface, GreetingReflect> {
constructor: (values: GreetingInterface) => GreetingBuilder;
text: string;
getText: () => string;
setText: (text: string) => void;
}
;
interface ChatInterface extends ProtoBuf$MessageLiteral {
greeting: $Subtype<GreetingInterface>;
body?: ?string;
}
;
type ChatReflect = Protobuf$RefectType<ChatInterface>;
declare class ChatBuilder extends Protobuf$Message<ChatInterface, ChatReflect> {
constructor: (values: ChatInterface) => ChatBuilder;
greeting: GreetingBuilder;
getGreeting: () => GreetingBuilder;
setGreeting: (greeting: GreetingBuilder) => void;
body?: ?string;
getBody: () => ?string;
setBody: (body: ?string) => void;
}
;
type foo = {
Greeting: Class<GreetingBuilder>;
Chat: Class<ChatBuilder>;
};
export type { GreetingInterface, GreetingBuilder, GreetingReflect, ChatInterface, ChatBuilder, ChatReflect }; */
const namespace /*: foo */ = _.builder.build('foo');
exports.Greeting = namespace.Greeting; /*::
export const Greeting = namespace.Greeting; */
exports.Chat = namespace.Chat; /*::
export const Chat = namespace.Chat; */
exports.default = namespace; /*::
export default namespace; */
###Usage
/* @flow */
import {Chat, Greating} from './proto/foo';
const chat: Chat = new Chat({
greeting: new Greating({ text: 'hello'}),
body: 'some text'
});
##Missing features Currently doesn't support
FAQs
Given protobuf file will generate node packages which export protobufjs builders annotated with flowtype type anotations
The npm package protobuf2flowtype receives a total of 9 weekly downloads. As such, protobuf2flowtype popularity was classified as not popular.
We found that protobuf2flowtype demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.