
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
protoscript
Advanced tools
A Protobuf runtime and code generation tool for JavaScript and TypeScript
ProtoScript is a protocol buffers runtime and code generation tool for JavaScript, written in TypeScript.
ProtoScript consists of two parts:
Runtime. This is nearly identical to google-protobuf, but significantly smaller (9.6KB vs 46KB gzipped) and written in ESM to enable dead-code elimination.
Code generation. This is a replacement of protoc
's JavaScript generation that generates idiomatic JavaScript code, JSON serializers/deserializers, and includes TSDoc comments.
If you're looking for an RPC framework, you may be interested in TwirpScript.
Idiomatic JavaScript / TypeScript code. None of the Java idioms that protoc --js_out
generates such as the List
suffix naming for repeated fields, Map
suffix for maps, or the various getter and setter methods. ProtoScript generates and consumes plain JavaScript objects over classes. Compare the TypeScript example to the protoc example.
In-editor API documentation. Comments in your .proto
files become TSDoc comments in the generated code and will show inline documentation in supported editors.
JSON Serialization/Deserialization. Unlike protoc
, ProtoScript's code generation generates JSON serialization and deserialization methods.
Small. ProtoScript's runtime and generated code are built with tree shaking to minimize bundle sizes. This results in a significantly smaller bundle size than google-protobuf. ProtoScript's runtime is 67KB (9.6KB gzipped) compared to google-protobuf's 231KB (46KB gzipped).
Isomorphic. ProtoScript's generated serializers/deserializers can be consumed in the browser or Node.js runtimes.
No additional runtime dependencies.
Install the protocol buffers compiler:
MacOS:
brew install protobuf
Linux:
apt install -y protobuf-compiler
Windows:
choco install protoc
Or install from a precompiled binary.
Add this package to your project:
npm install protoscript
or yarn add protoscript
npx protoscript
protoc \
--plugin=protoc-gen-protoscript=./node_modules/protoscript/compiler.js
--protoscript_out=. \
--protoscript_opt=language=typescript \
Note: Windows users replace ./node_modules/protoscript/compiler.js
above with ./node_modules/protoscript/compiler.cmd
ProtoScript can be used with Buf.
buf.gen.yaml
version: v1
plugins:
- name: protoc-gen-protoscript
path: ./node_modules/protoscript/compiler.js
out: .
opt:
- language=typescript
strategy: all
As a design goal, ProtoScript should always work with the strictest TypeScript compiler settings. If your generated ProtoScript files are failing type checking, please open an issue.
ProtoScript does not make any guarantees for tools like linters and formatters such as prettier or eslint. It generally does not make sense to run these tools against code generation artifacts, like the .pb.ts
or .pb.js
files generated by ProtoScript. This is because:
ProtoScript aims to be zero config, but can be configured via the cli interface, or when using the npx protoscript
command, by creating a proto.config.mjs
(or .js
or .cjs
) file in your project root.
Name | Description | Type |
---|---|---|
root |
The root directory. `.proto` files will be searched under this directory, and `proto` import paths will be resolved relative to this directory. ProtoScript will recursively search all subdirectories for `.proto` files.
Defaults to the project root. Example: If we have the following project structure:
Default: A.proto would
Setting // proto.config.mjs
A.proto would
TypeScript projects will generally want to set this value to match their | string (filepath) |
exclude |
An array of patterns that should be skipped when searching for `.proto` files.
Example: If we have the following project structure: /src /foo A.proto /bar B.proto Setting // proto.config.mjs
Will only process A.proto (B.proto) will be excluded from ProtoScript's code generation. | string[] (RegExp pattern) |
dest |
The destination folder for generated files.
Defaults to colocating generated files with the corresponding If we have the following project structure:
ProtoScript will generate the following:
Setting // proto.config.mjs
Note that the generated directory structure will mirror the Setting // proto.config.mjs
| string (filepath) |
language |
Whether to generate JavaScript or TypeScript.
If omitted, ProtoScript will attempt to autodetect the language by looking for a | javascript | typescript |
json |
JSON serializer options.
See https://developers.google.com/protocol-buffers/docs/proto3#json for more context. | { emitFieldsWithDefaultValues?: boolean, useProtoFieldName?: boolean } |
typecript |
TypeScript options.
| { emitDeclarationOnly?: boolean } |
ProtoScript's JSON serialization/deserialization implements the proto3 specification. This is nearly complete, but still in progress.
ProtoScript will serialize JSON keys as lowerCamelCase
versions of the proto field. Per the proto3 spec, the runtime will accept both lowerCamelCase
and the original proto field name when deserializing. You can provide the json_name
field option to specify an alternate key name. When doing so, the runtime will encode JSON messages using the the json_name
as the key, and will decode JSON messages using the json_name
if present, otherwise falling back to the lowerCamelCase
name and finally to the original proto field name.
If you're using a compiler other than TypeScript, such as webpack, this outstanding issue in TypeScript adds an additional hurdle.
The short summary of the issue:
.js
extensions for imports..ts
extensions for imports.The TypeScript team is investigating improving this issue in their 4.9 plan
Webpack users can use extensionAlias to solve this problem:
module.exports = {
+ resolve: {
+ extensionAlias: {
+ ".js": [".ts", ".js"],
+ },
+ },
};
For more context, see TypeScript#37582 and Webpack#13252.
PR's and issues welcomed! For more guidance check out CONTRIBUTING.md
See the project's MIT License.
v0.0.10
The following .protoscript.json
:
{
"root": "src",
};
Would be renamed to proto.config.mjs
and changed to the following:
/** @type {import('protoscript').Config} */
export default {
root: "src",
};
Use relative file path for determining path to compiler instead of hard coding from project root. This should interop better with more exotic package tooling and repo setup.
Fix: Improved map
detection. Previously field types suffixed with Entry
were incorrectly flagged as maps. This has been fixed.
FAQs
A Protobuf runtime and code generation tool for JavaScript and TypeScript
The npm package protoscript receives a total of 11,840 weekly downloads. As such, protoscript popularity was classified as popular.
We found that protoscript demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.