<h1><p align="center"><img alt="protobuf.js" src="https://github.com/dcodeIO/protobuf.js/raw/master/pbjs.png" width="120" height="104" /></p></h1>
<p align="center"><a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/v/protobufjs.svg" alt=""></a> <a href="https://travis-ci.org/dcodeIO/protobuf.js"><img src="https://travis-ci.org/dcodeIO/protobuf.js.svg?branch=master" alt=""></a> <a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/dm/protobufjs.svg" alt=""></a> <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=dcode%40dcode.io&item_name=Open%20Source%20Software%20Donation&item_number=dcodeIO%2Fprotobuf.js"><img alt="donate ❤" src="https://img.shields.io/badge/donate-❤-ff2244.svg"></a></p>
**Protocol Buffers** are 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](https://developers.google.com/protocol-buffers/)).
**protobuf.js** is a pure JavaScript implementation with [TypeScript](https://www.typescriptlang.org) support for [node.js](https://nodejs.org) and the browser. It's easy to use, blazingly fast and works out of the box with [.proto](https://developers.google.com/protocol-buffers/docs/proto) files!
Contents
--------
* [Installation](#installation)<br />
How to include protobuf.js in your project.
* [Usage](#usage)<br />
A brief introduction to using the toolset.
* [Valid Message](#valid-message)
* [Toolset](#toolset)<br />
* [Examples](#examples)<br />
A few examples to get you started.
* [Using .proto files](#using-proto-files)
* [Using JSON descriptors](#using-json-descriptors)
* [Using reflection only](#using-reflection-only)
* [Using custom classes](#using-custom-classes)
* [Using services](#using-services)
* [Usage with TypeScript](#usage-with-typescript)<br />
* [Command line](#command-line)<br />
How to use the command line utility.
* [pbjs for JavaScript](#pbjs-for-javascript)
* [pbts for TypeScript](#pbts-for-typescript)
* [Reflection vs. static code](#reflection-vs-static-code)
* [Command line API](#command-line-api)<br />
* [Additional documentation](#additional-documentation)<br />
A list of available documentation resources.
* [Performance](#performance)<br />
A few internals and a benchmark on performance.
* [Compatibility](#compatibility)<br />
Notes on compatibility regarding browsers and optional libraries.
* [Building](#building)<br />
How to build the library and its components yourself.
Installation
---------------
### node.js
```
$> npm install protobufjs [--save --save-prefix=~]
```
```js
var protobuf = require("protobufjs");
```
**Note** that 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:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.js"></script>
```
Production:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.min.js"></script>
```
**Remember** to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/tags) 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](#pbjs-for-javascript)) and/or reflection only, see the [light library][dist-light] (~16kb gzipped) that excludes the parser. CommonJS entry point is:
```js
var protobuf = require("protobufjs/light");
```
* When working with statically generated code only, see the [minimal library][dist-minimal] (~6.5kb gzipped) that also excludes reflection. CommonJS entry point is:
```js
var protobuf = require("protobufjs/minimal");
```
[dist-full]: https://github.com/dcodeIO/protobuf.js/tree/master/dist
[dist-light]: https://github.com/dcodeIO/protobuf.js/tree/master/dist/light
[dist-minimal]: https://github.com/dcodeIO/protobuf.js/tree/master/dist/minimal
Usage
-----
Because JavaScript is a dynamically typed language, protobuf.js introduces the concept of a **valid message** in order to provide the best possible [performance](#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 objects** that 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-/int32<br />s-/fixed32 | `number` (32 bit integer) | <code>value | 0</code> if signed<br />`value >>> 0` if unsigned
| s-/u-/int64<br />s-/fixed64 | `Long`-like (optimal)<br />`number` (53 bit integer) | `Long.fromValue(value)` with long.js<br />`parseInt(value, 10)` otherwise
| float<br />double | `number` | `Number(value)`
| bool | `boolean` | `Boolean(value)`
| string | `string` | `String(value)`
| bytes | `Uint8Array` (optimal)<br />`Buffer` (optimal under node)<br />`Array.<number>` (8 bit integers) | `base64.decode(value)` if a `string`<br />`Object` 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 *optimal* provide 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 *might* just so happen to be a valid message) explicitly where necessary - for example when dealing with user input.
**Note** that `Message` below refers to any message class.
* **Message.verify**(message: `Object`): `null|string`<br />
verifies that a **plain JavaScript object** satisfies 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.
```js
var payload = "invalid (not an object)";
var err = AwesomeMessage.verify(payload);
if (err)
throw Error(err);
```
* **Message.encode**(message: `Message|Object` [, writer: `Writer`]): `Writer`<br />
encodes a **message instance** or 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.
```js
var buffer = Aweso
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计-微信小程序商城源码 (2000个子文件)
example.html 200B
tslib.es6.html 36B
tslib.html 32B
lodash.js 530KB
bson.browser.umd.js 294KB
bson.bundle.js 294KB
bson.browser.esm.js 286KB
protobuf.js 268KB
source-map.debug.js 266KB
bson.esm.js 229KB
protobuf.js 224KB
bluebird.js 179KB
psl.js 149KB
psl.min.js 125KB
bluebird.core.js 120KB
core.js 113KB
source-map.js 104KB
bluebird.min.js 80KB
protobuf.js 78KB
lodash.min.js 71KB
protobuf.min.js 70KB
nacl-fast.js 61KB
protobuf.min.js 61KB
bluebird.core.min.js 56KB
browser-source-map-support.js 52KB
index.js 50KB
merge.js 49KB
index.js 49KB
diff.js 47KB
index.js 44KB
sax.js 43KB
index.es6.js 42KB
index.js 41KB
cookie.js 40KB
source-map-consumer.js 40KB
long.js 39KB
semver.js 38KB
index.js 35KB
serializer.js 35KB
dashdash.js 34KB
long.js 34KB
nacl.js 32KB
virtual-websocket-client.js 32KB
virtual-websocket-client.js 32KB
nacl-fast.min.js 32KB
debuggability.js 31KB
virtual-websocket-client.js 31KB
virtual-websocket-client.js 31KB
base.js 30KB
static.js 27KB
decimal128.js 27KB
source-map.min.js 26KB
promise.js 26KB
deserializer.js 25KB
index.js 25KB
index.js 25KB
index.js 23KB
websocket-client.js 23KB
websocket-client.js 23KB
websocket-client.js 22KB
websocket-client.js 22KB
parse.js 22KB
parse.js 22KB
create.js 21KB
type.js 21KB
protobuf.min.js 20KB
stringify.js 20KB
publish.js 20KB
apply.js 20KB
source-map-support.js 19KB
x509.js 19KB
qs.js 19KB
nacl.min.js 19KB
index.js 18KB
diff.min.js 17KB
parse.js 16KB
_baseConvert.js 16KB
tests.js 15KB
bson.js 15KB
namespace.js 15KB
ec.js 15KB
tmp.js 15KB
punycode.js 14KB
pkcs8.js 14KB
source-map-generator.js 14KB
extended_json.js 13KB
source-node.js 13KB
core.min.js 13KB
tslib.js 13KB
command.js 13KB
minimal.js 13KB
util.js 13KB
json.js 13KB
command.js 13KB
pbjs.js 12KB
punycode.es6.js 12KB
punycode.es6.js 12KB
root.js 12KB
punycode.js 12KB
punycode.js 12KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
code.song
- 粉丝: 381
- 资源: 916
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功