TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置


Protocol Buffersare a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google (see ).

protobuf.jsis a pure JavaScript implementation with TypeScript support for node.js and the browser. It's easy to use, blazingly fast and works out of the box with .proto files!

Contents


Installation How to include protobuf.js in your project.

Usage A brief introduction to using the toolset.

Valid Message
Toolset

Examples A few examples to get you started.

Using .proto files
Using JSON descriptors
Using reflection only
Using custom classes
Using services
Usage with TypeScript

Additional documentation A list of available documentation resources.

Performance A few internals and a benchmark on performance.

Compatibility Notes on compatibility regarding browsers and optional libraries.

Building How to build the library and its components yourself.

Installation


node.js


  1. ``` sh
  2. $> npm install protobufjs [--save --save-prefix=~]

  3. ```

  1. ``` js
  2. var protobuf = require("protobufjs");
  3. ```

The command line utility lives in the protobufjs-cli package and must be installed separately:

  1. ``` sh
  2. $> npm install protobufjs-cli [--save --save-prefix=~]

  3. ```

Notethat this library's versioning scheme is not semver-compatible for historical reasons. For guaranteed backward compatibility, always depend on ~6.A.B instead of ^6.A.B (hence the --save-prefix above).

Browsers


Development:

  1. ``` sh
  2. <script src="//cdn.jsdelivr.net/npm/protobufjs@7.X.X/dist/protobuf.js"></script>

  3. ```

Production:

  1. ``` sh
  2. <script src="//cdn.jsdelivr.net/npm/protobufjs@7.X.X/dist/protobuf.min.js"></script>

  3. ```

Rememberto replace the version tag with the exact release your project depends upon.

The library supports CommonJS and AMD loaders and also exports globally as protobuf.

Distributions


Where bundle size is a factor, there are additional stripped-down versions of the [full library][dist-full] (~19kb gzipped) available that exclude certain functionality:

When working with JSON descriptors (i.e. generated by pbjs ) and/or reflection only, see the [light library][dist-light] (~16kb gzipped) that excludes the parser. CommonJS entry point is:

  1. ``` js
  2. var protobuf = require("protobufjs/light");
  3. ```

When working with statically generated code only, see the [minimal library][dist-minimal] (~6.5kb gzipped) that also excludes reflection. CommonJS entry point is:

  1. ``` js
  2. var protobuf = require("protobufjs/minimal");
  3. ```

Distribution Location
:--- :---
Full https://cdn.jsdelivr.net/npm/protobufjs/dist/
Light https://cdn.jsdelivr.net/npm/protobufjs/dist/light/
Minimal https://cdn.jsdelivr.net/npm/protobufjs/dist/minimal/

Usage


Because JavaScript is a dynamically typed language, protobuf.js introduces the concept of a valid messagein order to provide the best possible performance (and, as a side product, proper typings):

Valid message


A valid message is an object (1) not missing any required fields and (2) exclusively composed of JS types understood by the wire format writer.


There are two possible types of valid messages and the encoder is able to work with both of these for convenience:

Message instances(explicit instances of message classes with default values on their prototype) always (have to) satisfy the requirements of a valid message by design and
Plain JavaScript objectsthat just so happen to be composed in a way satisfying the requirements of a valid message as well.

In a nutshell, the wire format writer understands the following types:

Field type Expected JS type (create, encode) Conversion (fromObject)
:--- :--- :---
s-/u-/int32s-/fixed32 number (32 bit integer) `value
s-/u-/int64s-/fixed64 Long-like (optimal)number (53 bit integer) Long.fromValue(value) with long.jsparseInt(value, 10) otherwise
floatdouble number Number(value)
bool boolean Boolean(value)
string string String(value)
bytes Uint8Array (optimal)Buffer (optimal under node)Array.<number> (8 bit integers) base64.decode(value) if a stringObject with non-zero .length is assumed to be buffer-like
enum number (32 bit integer) Looks up the numeric id if a string
message Valid message Message.fromObject(value)

Explicit undefined and null are considered as not set if the field is optional.
Repeated fields are Array.<T>.
Map fields are Object.<string,T> with the key being the string representation of the respective value or an 8 characters long binary hash string for Long -likes.
Types marked as optimalprovide the best performance because no conversion step (i.e. number to low and high bits or base64 string to buffer) is required.

Toolset


With that in mind and again for performance reasons, each message class provides a distinct set of methods with each method doing just one thing. This avoids unnecessary assertions / redundant operations where performance is a concern but also forces a user to perform verification (of plain JavaScript objects that mightjust so happen to be a valid message) explicitly where necessary - for example when dealing with user input.

Notethat Message below refers to any message class.

Message.verify(message: Object ): null|string verifies that a plain JavaScript objectsatisfies the requirements of a valid message and thus can be encoded without issues. Instead of throwing, it returns the error message as a string, if any.

  1. ``` js
  2. var payload = "invalid (not an object)";
  3. var err = AwesomeMessage.verify(payload);
  4. if (err)
  5.   throw Error(err);
  6. ```

Message.encode(message: Message|Object [, writer: Writer ]): Writer encodes a message instanceor valid plain JavaScript object. This method does not implicitly verify the message and it's up to the user to make sure that the payload is a valid message.

  1. ``` js
  2. var buffer = AwesomeMessage.encode(message).finish();
  3. ```

Message.encodeDelimited(message: Message|Object [, writer: Writer ]): Writer works like Message.encode but additionally prepends the length of the message as a varint.

Message.decode(reader: Reader|Uint8Array ): Message decodes a buffer to a message instance. If required fields are missing, it throws a util.ProtocolError with an instance property set to the so far decoded message. If the wire format is invalid, it throws an Error.

  1. ``` js
  2. try {
  3.   var decodedMessage = AwesomeMessage.decode(buffer);
  4. } catch (e) {
  5.     if (e instanceof protobuf.util.ProtocolError) {
  6.       // e.instance holds the so far decoded message with missing required fields
  7.     } else {
  8.       // wire format is invalid
  9.     }
  10. }
  11. ```

Message.decodeDelimited(reader: Reader|Uint8Array ): Message works like Message.decode but additionally reads the length of the message prepended as a varint.

Message.create(properties: Object ): Message creates a new message instancefrom a set of properties that satisfy the requirements of a valid message. Where applicable, it is recommended to prefer Message.create over Message.fromObject because it doesn't perform possibly redundant conversion.

  1. ``` js
  2. var message = AwesomeMessage.create({ awesomeField: "AwesomeString" });
  3. ```

Message.fromObject(object: Object ): Message converts any non-valid plain JavaScript objectto a message instanceusing the conversion steps outlined within the table above.

  1. ``` js
  2. var message = AwesomeMessage.fromObject({ awesomeField: 42 });
  3. // converts awesomeField to a string
  4. ```

Message.toObject(message: Message [, options: ConversionOptions ]): Object converts a message instanceto an arbitrary plain JavaScript objectfor interoperability with other libraries or storage. The resulting plain JavaScript object mightstill satisfy the requirements of a valid message depending on the actual conversion options specified, but most of the time it does not.

  1. ``` js
  2. var object = AwesomeMessage.toObject(message, {
  3.   enums: String,  // enums as string names
  4.   longs: String,  // longs as strings (requires long.js)
  5.   bytes: String,  // bytes as base64 encoded strings
  6.   defaults: true, // includes default values
  7.   arrays: true,   // populates empty arrays (repeated fields) even if defaults=false
  8.   objects: true,  // populates empty objects (map fields) even if defaults=false
  9.   oneofs: true    // includes virtual oneof fields set to the present field's name
  10. });
  11. ```

For reference, the following diagram aims to display relationships between the different methods and the concept of a valid message:

In other words: verify indicates that calling create or encode directly on the plain object will [result in a valid message respectively] succeed. fromObject, on the other hand, does conversion from a broader range of plain objects to create valid messages. (ref )


Examples


Using .proto files


It is possible to load existing .proto files using the full library, which parses and compiles the definitions to ready to use (reflection-based) message classes:

  1. ``` proto
  2. // awesome.proto
  3. package awesomepackage;
  4. syntax = "proto3";

  5. message AwesomeMessage {
  6.     string awesome_field = 1; // becomes awesomeField
  7. }
  8. ```

  1. ``` js
  2. protobuf.load("awesome.proto", function(err, root) {
  3.     if (err)
  4.         throw err;

  5.     // Obtain a message type
  6.     var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");

  7.     // Exemplary payload
  8.     var payload = { awesomeField: "AwesomeString" };

  9.     // Verify the payload if necessary (i.e. when possibly incomplete or invalid)
  10.     var errMsg = AwesomeMessage.verify(payload);
  11.     if (errMsg)
  12.         throw Error(errMsg);

  13.     // Create a new message
  14.     var message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary

  15.     // Encode a message to an Uint8Array (browser) or Buffer (node)
  16.     var buffer = AwesomeMessage.encode(message).finish();
  17.     // ... do something with buffer

  18.     // Decode an Uint8Array (browser) or Buffer (node) to a message
  19.     var message = AwesomeMessage.decode(buffer);
  20.     // ... do something with message

  21.     // If the application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited.

  22.     // Maybe convert the message back to a plain object
  23.     var object = AwesomeMessage.toObject(message, {
  24.         longs: String,
  25.         enums: String,
  26.         bytes: String,
  27.         // see ConversionOptions
  28.     });
  29. });
  30. ```

Additionally, promise syntax can be used by omitting the callback, if preferred:

  1. ``` js
  2. protobuf.load("awesome.proto")
  3.     .then(function(root) {
  4.        ...
  5.     });
  6. ```

Using JSON descriptors


The library utilizes JSON descriptors that are equivalent to a .proto definition. For example, the following is identical to the .proto definition seen above:

  1. ``` json
  2. // awesome.json
  3. {
  4.   "nested": {
  5.     "awesomepackage": {
  6.       "nested": {
  7.         "AwesomeMessage": {
  8.           "fields": {
  9.             "awesomeField": {
  10.               "type": "string",
  11.               "id": 1
  12.             }
  13.           }
  14.         }
  15.       }
  16.     }
  17.   }
  18. }
  19. ```

JSON descriptors closely resemble the internal reflection structure:

Type (T) Extends Type-specific properties
:--- :--- :---
ReflectionObject options
Namespace ReflectionObject nested
Root Namespace nested
Type Namespace fields
Enum ReflectionObject values
Field ReflectionObject rule, type, id
MapField Field keyType
OneOf ReflectionObject oneof (array of field names)
Service Namespace methods
Method ReflectionObject type, requestType, responseType, requestStream, responseStream

Bold propertiesare required. Italic typesare abstract.
T.fromJSON(name, json) creates the respective reflection object from a JSON descriptor
T#toJSON() creates a JSON descriptor from the respective reflection object (its name is used as the key within the parent)

Exclusively using JSON descriptors instead of .proto files enables the use of just the light library (the parser isn't required in this case).

A JSON descriptor can either be loaded the usual way:

  1. ``` js
  2. protobuf.load("awesome.json", function(err, root) {
  3.     if (err) throw err;

  4.     // Continue at "Obtain a message type" above
  5. });
  6. ```

Or it can be loaded inline:

  1. ``` js
  2. var jsonDescriptor = require("./awesome.json"); // exemplary for node

  3. var root = protobuf.Root.fromJSON(jsonDescriptor);

  4. // Continue at "Obtain a message type" above
  5. ```

Using reflection only


Both the full and the light library include full reflection support. One could, for example, define the .proto definitions seen in the examples above using just reflection:

  1. ``` js
  2. ...
  3. var Root  = protobuf.Root,
  4.     Type  = protobuf.Type,
  5.     Field = protobuf.Field;

  6. var AwesomeMessage = new Type("AwesomeMessage").add(new Field("awesomeField", 1, "string"));

  7. var root = new Root().define("awesomepackage").add(AwesomeMessage);

  8. // Continue at "Create a new message" above
  9. ...
  10. ```

Detailed information on the reflection structure is available within the API documentation.

Using custom classes


Message classes can also be extended with custom functionality and it is also possible to register a custom constructor with a reflected message type:

  1. ``` js
  2. ...

  3. // Define a custom constructor
  4. function AwesomeMessage(properties) {
  5.     // custom initialization code
  6.     ...
  7. }

  8. // Register the custom constructor with its reflected type (*)
  9. root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;

  10. // Define custom functionality
  11. AwesomeMessage.customStaticMethod = function() { ... };
  12. AwesomeMessage.prototype.customInstanceMethod = function() { ... };

  13. // Continue at "Create a new message" above
  14. ```

(*) Besides referencing its reflected type through AwesomeMessage.$type and AwesomeMesage#$type, the respective custom class is automatically populated with:

AwesomeMessage.create
AwesomeMessage.encode and AwesomeMessage.encodeDelimited
AwesomeMessage.decode and AwesomeMessage.decodeDelimited
AwesomeMessage.verify
AwesomeMessage.fromObject, AwesomeMessage.toObject and AwesomeMessage#toJSON

Afterwards, decoded messages of this type are instanceof AwesomeMessage.

Alternatively, it is also possible to reuse and extend the internal constructor if custom initialization code is not required:

  1. ``` js
  2. ...

  3. // Reuse the internal constructor
  4. var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;

  5. // Define custom functionality
  6. AwesomeMessage.customStaticMethod = function() { ... };
  7. AwesomeMessage.prototype.customInstanceMethod = function() { ... };

  8. // Continue at "Create a new m
Last Updated: 2023-09-03 17:10:52