protobufjs 8.1.5-experimental → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +219 -565
- package/dist/light/protobuf.js +1986 -1483
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +1122 -861
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +2089 -1513
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +81 -0
- package/ext/descriptor/README.md +3 -70
- package/ext/descriptor/index.d.ts +1 -191
- package/ext/descriptor/index.js +1 -1156
- package/ext/descriptor.d.ts +309 -0
- package/ext/descriptor.js +1236 -0
- package/ext/textformat.d.ts +30 -0
- package/ext/textformat.js +1249 -0
- package/google/protobuf/compiler/plugin.json +126 -0
- package/google/protobuf/compiler/plugin.proto +47 -0
- package/google/protobuf/descriptor.json +114 -10
- package/google/protobuf/descriptor.proto +35 -9
- package/index.d.ts +590 -476
- package/package.json +23 -38
- package/src/converter.js +60 -24
- package/src/decoder.js +122 -49
- package/src/encoder.js +10 -2
- package/src/enum.js +4 -1
- package/src/field.js +10 -7
- package/src/mapfield.js +1 -0
- package/src/message.js +7 -6
- package/src/method.js +4 -3
- package/src/namespace.js +23 -12
- package/src/object.js +24 -19
- package/src/oneof.js +2 -0
- package/src/parse.js +114 -46
- package/src/reader.js +145 -30
- package/src/reader_buffer.js +24 -3
- package/src/root.js +7 -4
- package/src/service.js +12 -6
- package/src/tokenize.js +6 -1
- package/src/type.js +48 -25
- package/src/types.js +1 -1
- package/src/util/aspromise.d.ts +13 -0
- package/src/util/aspromise.js +52 -0
- package/src/util/base64.d.ts +32 -0
- package/src/util/base64.js +146 -0
- package/src/util/codegen.d.ts +31 -0
- package/src/util/codegen.js +113 -0
- package/src/util/eventemitter.d.ts +45 -0
- package/src/util/eventemitter.js +84 -0
- package/src/util/fetch.d.ts +56 -0
- package/src/util/fetch.js +112 -0
- package/src/util/float.d.ts +83 -0
- package/src/util/float.js +335 -0
- package/src/util/fs.js +11 -0
- package/src/util/inquire.d.ts +10 -0
- package/src/util/inquire.js +38 -0
- package/src/util/minimal.js +67 -12
- package/src/util/path.d.ts +22 -0
- package/src/util/path.js +72 -0
- package/src/util/patterns.js +8 -0
- package/src/util/pool.d.ts +32 -0
- package/src/util/pool.js +48 -0
- package/src/util/utf8.d.ts +24 -0
- package/src/util/utf8.js +104 -0
- package/src/util.js +30 -13
- package/src/verifier.js +7 -4
- package/src/wrappers.js +4 -3
- package/src/writer.js +27 -4
- package/src/writer_buffer.js +12 -0
- package/tsconfig.json +2 -2
- package/ext/descriptor/test.js +0 -54
package/README.md
CHANGED
|
@@ -9,719 +9,373 @@
|
|
|
9
9
|
|
|
10
10
|
**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://protobuf.dev/)).
|
|
11
11
|
|
|
12
|
-
**protobuf.js** is a
|
|
12
|
+
**protobuf.js** is a standalone JavaScript implementation of Protocol Buffers with TypeScript support for Node.js and the browser. It works with `.proto` files out of the box, is optimized for fast binary I/O, and supports runtime reflection as well as static code generation.
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
--------
|
|
14
|
+
## Getting started
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
How to include protobuf.js in your project.
|
|
19
|
-
|
|
20
|
-
* [Usage](#usage)<br />
|
|
21
|
-
A brief introduction to using the toolset.
|
|
22
|
-
|
|
23
|
-
* [Valid Message](#valid-message)
|
|
24
|
-
* [Toolset](#toolset)<br />
|
|
25
|
-
|
|
26
|
-
* [Examples](#examples)<br />
|
|
27
|
-
A few examples to get you started.
|
|
28
|
-
|
|
29
|
-
* [Using .proto files](#using-proto-files)
|
|
30
|
-
* [Using JSON descriptors](#using-json-descriptors)
|
|
31
|
-
* [Using reflection only](#using-reflection-only)
|
|
32
|
-
* [Using custom classes](#using-custom-classes)
|
|
33
|
-
* [Using services](#using-services)
|
|
34
|
-
* [Usage with TypeScript](#usage-with-typescript)<br />
|
|
35
|
-
|
|
36
|
-
* [Additional documentation](#additional-documentation)<br />
|
|
37
|
-
A list of available documentation resources.
|
|
38
|
-
|
|
39
|
-
* [Performance](#performance)<br />
|
|
40
|
-
A few internals and a benchmark on performance.
|
|
41
|
-
|
|
42
|
-
* [Compatibility](#compatibility)<br />
|
|
43
|
-
Notes on compatibility regarding browsers and optional libraries.
|
|
44
|
-
|
|
45
|
-
* [Building](#building)<br />
|
|
46
|
-
How to build the library and its components yourself.
|
|
47
|
-
|
|
48
|
-
Installation
|
|
49
|
-
---------------
|
|
50
|
-
|
|
51
|
-
### Node.js
|
|
16
|
+
### Install
|
|
52
17
|
|
|
53
18
|
```sh
|
|
54
|
-
npm install protobufjs
|
|
19
|
+
npm install protobufjs
|
|
55
20
|
```
|
|
56
21
|
|
|
57
|
-
|
|
58
|
-
// Static code + Reflection + .proto parser
|
|
59
|
-
var protobuf = require("protobufjs");
|
|
60
|
-
|
|
61
|
-
// Static code + Reflection
|
|
62
|
-
var protobuf = require("protobufjs/light");
|
|
63
|
-
|
|
64
|
-
// Static code only
|
|
65
|
-
var protobuf = require("protobufjs/minimal");
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
The optional [command line utility](./cli/) to generate static code and reflection bundles lives in the `protobufjs-cli` package and can be installed separately:
|
|
22
|
+
The [command line utility](./cli/) for generating reflection bundles, static code and TypeScript declarations is published as an add-on package:
|
|
69
23
|
|
|
70
24
|
```sh
|
|
71
|
-
npm install
|
|
25
|
+
npm install --save-dev protobufjs-cli
|
|
72
26
|
```
|
|
73
27
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
Pick the variant matching your needs and replace the version tag with the exact [release](https://github.com/protobufjs/protobuf.js/tags) your project depends upon. For example, to use the minified full variant:
|
|
77
|
-
|
|
78
|
-
```html
|
|
79
|
-
<script src="//cdn.jsdelivr.net/npm/protobufjs@7.X.X/dist/protobuf.min.js"></script>
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
| Distribution | Location
|
|
83
|
-
|--------------|--------------------------------------------------------
|
|
84
|
-
| Full | <https://cdn.jsdelivr.net/npm/protobufjs/dist/>
|
|
85
|
-
| Light | <https://cdn.jsdelivr.net/npm/protobufjs/dist/light/>
|
|
86
|
-
| Minimal | <https://cdn.jsdelivr.net/npm/protobufjs/dist/minimal/>
|
|
87
|
-
|
|
88
|
-
All variants support CommonJS and AMD loaders and export globally as `window.protobuf`.
|
|
89
|
-
|
|
90
|
-
Usage
|
|
91
|
-
-----
|
|
92
|
-
|
|
93
|
-
Because JavaScript is a dynamically typed language, protobuf.js utilizes the concept of a **valid message** in order to provide the best possible [performance](#performance) (and, as a side product, proper typings):
|
|
94
|
-
|
|
95
|
-
### Valid message
|
|
96
|
-
|
|
97
|
-
> 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.
|
|
98
|
-
|
|
99
|
-
There are two possible types of valid messages and the encoder is able to work with both of these for convenience:
|
|
100
|
-
|
|
101
|
-
* **Message instances** (explicit instances of message classes with default values on their prototype) naturally satisfy the requirements of a valid message and
|
|
102
|
-
* **Plain JavaScript objects** that just so happen to be composed in a way satisfying the requirements of a valid message as well.
|
|
103
|
-
|
|
104
|
-
In a nutshell, the wire format writer understands the following types:
|
|
105
|
-
|
|
106
|
-
| Field type | Expected JS type (create, encode) | Conversion (fromObject)
|
|
107
|
-
|------------|-----------------------------------|------------------------
|
|
108
|
-
| s-/u-/int32<br />s-/fixed32 | `number` (32 bit integer) | <code>value | 0</code> if signed<br />`value >>> 0` if unsigned
|
|
109
|
-
| 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
|
|
110
|
-
| float<br />double | `number` | `Number(value)`
|
|
111
|
-
| bool | `boolean` | `Boolean(value)`
|
|
112
|
-
| string | `string` | `String(value)`
|
|
113
|
-
| 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
|
|
114
|
-
| enum | `number` (32 bit integer) | Looks up the numeric id if a `string`
|
|
115
|
-
| message | Valid message | `Message.fromObject(value)`
|
|
116
|
-
| repeated T | `Array<T>` | Copy
|
|
117
|
-
| map<K, V> | `Object<K,V>` | Copy
|
|
118
|
-
|
|
119
|
-
* Explicit `undefined` and `null` are considered as not set if the field is optional.
|
|
120
|
-
* Maps are objects where the key is the string representation of the respective value or an 8 characters long hash string for `Long`-likes.
|
|
121
|
-
|
|
122
|
-
### Toolset
|
|
123
|
-
|
|
124
|
-
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.
|
|
125
|
-
|
|
126
|
-
**Note** that `Message` below refers to any message class.
|
|
127
|
-
|
|
128
|
-
* **Message.verify**(message: `Object`): `null|string`<br />
|
|
129
|
-
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.
|
|
130
|
-
|
|
131
|
-
```js
|
|
132
|
-
var payload = "invalid (not an object)";
|
|
133
|
-
var err = AwesomeMessage.verify(payload);
|
|
134
|
-
if (err)
|
|
135
|
-
throw Error(err);
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
* **Message.encode**(message: `Message|Object` [, writer: `Writer`]): `Writer`<br />
|
|
139
|
-
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.
|
|
140
|
-
|
|
141
|
-
```js
|
|
142
|
-
var buffer = AwesomeMessage.encode(message).finish();
|
|
143
|
-
```
|
|
28
|
+
The CLI is a small but capable standalone protobuf.js toolchain. It does not require `protoc` or a language plugin.
|
|
144
29
|
|
|
145
|
-
|
|
146
|
-
works like `Message.encode` but additionally prepends the length of the message as a varint.
|
|
30
|
+
### Choose a runtime
|
|
147
31
|
|
|
148
|
-
|
|
149
|
-
|
|
32
|
+
| Import | Includes | Use when
|
|
33
|
+
| ----------------------- | ------------------ | --------
|
|
34
|
+
| `protobufjs` | Reflection, Parser | You load `.proto` files at runtime
|
|
35
|
+
| `protobufjs/light.js` | Reflection | You load JSON bundles or build schemas programmatically
|
|
36
|
+
| `protobufjs/minimal.js` | Static runtime | You only use generated static code
|
|
150
37
|
|
|
151
|
-
|
|
152
|
-
try {
|
|
153
|
-
var decodedMessage = AwesomeMessage.decode(buffer);
|
|
154
|
-
} catch (e) {
|
|
155
|
-
if (e instanceof protobuf.util.ProtocolError) {
|
|
156
|
-
// e.instance holds the so far decoded message with missing required fields
|
|
157
|
-
} else {
|
|
158
|
-
// wire format is invalid
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
```
|
|
38
|
+
The full build includes the light build, and the light build includes the minimal runtime.
|
|
162
39
|
|
|
163
|
-
|
|
164
|
-
works like `Message.decode` but additionally reads the length of the message prepended as a varint.
|
|
40
|
+
### Browser builds
|
|
165
41
|
|
|
166
|
-
|
|
167
|
-
creates a new **message instance** from 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.
|
|
42
|
+
Pick the distribution matching your runtime variant and pin an exact version:
|
|
168
43
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
var message = AwesomeMessage.fromObject({ awesomeField: 42 });
|
|
178
|
-
// converts awesomeField to a string
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
* **Message.toObject**(message: `Message` [, options: `ConversionOptions`]): `Object`<br />
|
|
182
|
-
converts a **message instance** to an arbitrary **plain JavaScript object** for interoperability with other libraries or storage. The resulting plain JavaScript object *might* still satisfy the requirements of a valid message depending on the actual conversion options specified, but most of the time it does not.
|
|
183
|
-
|
|
184
|
-
```js
|
|
185
|
-
var object = AwesomeMessage.toObject(message, {
|
|
186
|
-
enums: String, // enums as string names
|
|
187
|
-
longs: String, // longs as strings (requires long.js)
|
|
188
|
-
bytes: String, // bytes as base64 encoded strings
|
|
189
|
-
defaults: true, // includes default values
|
|
190
|
-
arrays: true, // populates empty arrays (repeated fields) even if defaults=false
|
|
191
|
-
objects: true, // populates empty objects (map fields) even if defaults=false
|
|
192
|
-
oneofs: true // includes virtual oneof fields set to the present field's name
|
|
193
|
-
});
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
For reference, the following diagram aims to display relationships between the different methods and the concept of a valid message:
|
|
44
|
+
```html
|
|
45
|
+
<!-- Full -->
|
|
46
|
+
<script src="https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/protobuf.min.js"></script>
|
|
47
|
+
<!-- Light -->
|
|
48
|
+
<script src="https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/light/protobuf.min.js"></script>
|
|
49
|
+
<!-- Minimal -->
|
|
50
|
+
<script src="https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/minimal/protobuf.min.js"></script>
|
|
51
|
+
```
|
|
197
52
|
|
|
198
|
-
|
|
53
|
+
Browser builds support CommonJS and AMD loaders and export globally as `window.protobuf`.
|
|
199
54
|
|
|
200
|
-
|
|
55
|
+
## Usage
|
|
201
56
|
|
|
202
|
-
|
|
203
|
-
--------
|
|
57
|
+
The examples below use this schema:
|
|
204
58
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
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:
|
|
59
|
+
```proto
|
|
60
|
+
syntax = "proto3";
|
|
208
61
|
|
|
209
|
-
```protobuf
|
|
210
|
-
// awesome.proto
|
|
211
62
|
package awesomepackage;
|
|
212
|
-
syntax = "proto3";
|
|
213
63
|
|
|
214
64
|
message AwesomeMessage {
|
|
215
|
-
|
|
65
|
+
string awesome_field = 1;
|
|
216
66
|
}
|
|
217
67
|
```
|
|
218
68
|
|
|
219
|
-
|
|
220
|
-
protobuf.load("awesome.proto", function(err, root) {
|
|
221
|
-
if (err)
|
|
222
|
-
throw err;
|
|
69
|
+
protobuf.js converts `.proto` field names to camelCase by default, so `awesome_field` is used as `awesomeField` in JavaScript. Use the `keepCase` option when loading or parsing `.proto` files to preserve field names as written.
|
|
223
70
|
|
|
224
|
-
|
|
225
|
-
var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
71
|
+
### Load a schema
|
|
226
72
|
|
|
227
|
-
|
|
228
|
-
|
|
73
|
+
```js
|
|
74
|
+
const protobuf = require("protobufjs");
|
|
229
75
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
throw Error(errMsg);
|
|
76
|
+
const root = protobuf.loadSync("awesome.proto");
|
|
77
|
+
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
78
|
+
```
|
|
234
79
|
|
|
235
|
-
|
|
236
|
-
var message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary
|
|
80
|
+
Use `protobuf.load()` for the asynchronous variant.
|
|
237
81
|
|
|
238
|
-
|
|
239
|
-
var buffer = AwesomeMessage.encode(message).finish();
|
|
240
|
-
// ... do something with buffer
|
|
82
|
+
### Encode and decode
|
|
241
83
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
// ... do something with message
|
|
84
|
+
```js
|
|
85
|
+
const payload = { awesomeField: "hello" };
|
|
245
86
|
|
|
246
|
-
|
|
87
|
+
// Optionally verify if the payload is of uncertain shape
|
|
88
|
+
const err = AwesomeMessage.verify(payload);
|
|
89
|
+
if (err) throw Error(err);
|
|
247
90
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
longs: String,
|
|
251
|
-
enums: String,
|
|
252
|
-
bytes: String,
|
|
253
|
-
// see ConversionOptions
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
```
|
|
91
|
+
// Optionally create a message instance from already valid data
|
|
92
|
+
const message = AwesomeMessage.create(payload);
|
|
257
93
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
```js
|
|
261
|
-
protobuf.load("awesome.proto")
|
|
262
|
-
.then(function(root) {
|
|
263
|
-
...
|
|
264
|
-
});
|
|
94
|
+
const encoded = AwesomeMessage.encode(message).finish();
|
|
95
|
+
const decoded = AwesomeMessage.decode(encoded);
|
|
265
96
|
```
|
|
266
97
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
The library utilizes JSON descriptors that are equivalent to a .proto definition. For example, the following is identical to the .proto definition seen above:
|
|
270
|
-
|
|
271
|
-
```json
|
|
272
|
-
// awesome.json
|
|
273
|
-
{
|
|
274
|
-
"nested": {
|
|
275
|
-
"awesomepackage": {
|
|
276
|
-
"nested": {
|
|
277
|
-
"AwesomeMessage": {
|
|
278
|
-
"fields": {
|
|
279
|
-
"awesomeField": {
|
|
280
|
-
"type": "string",
|
|
281
|
-
"id": 1
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
```
|
|
98
|
+
`encode` expects a message instance or equivalent plain object and does not verify input implicitly. Use `verify` for plain objects whose shape is not guaranteed, `create` to create a message instance from already valid data when useful, and `fromObject` when conversion from broader JavaScript input is needed.
|
|
290
99
|
|
|
291
|
-
|
|
100
|
+
Plain objects can be encoded directly when they already use protobuf.js runtime types: numbers for 32-bit numeric fields, booleans for `bool`, strings for `string`, `Uint8Array` or `Buffer` for `bytes`, arrays for repeated fields, and plain objects for maps. Map keys are the string representation of the respective value or an 8-character hash string for 64-bit/`Long` keys. Use `fromObject` when input may use broader JSON-style forms such as enum names, base64 strings for bytes, or decimal strings for 64-bit values.
|
|
292
101
|
|
|
293
|
-
|
|
294
|
-
|--------------------|--------------------|-------------------------
|
|
295
|
-
| *ReflectionObject* | | options
|
|
296
|
-
| *Namespace* | *ReflectionObject* | nested
|
|
297
|
-
| Root | *Namespace* | **nested**
|
|
298
|
-
| Type | *Namespace* | **fields**
|
|
299
|
-
| Enum | *ReflectionObject* | **values**
|
|
300
|
-
| Field | *ReflectionObject* | rule, **type**, **id**
|
|
301
|
-
| MapField | Field | **keyType**
|
|
302
|
-
| OneOf | *ReflectionObject* | **oneof** (array of field names)
|
|
303
|
-
| Service | *Namespace* | **methods**
|
|
304
|
-
| Method | *ReflectionObject* | type, **requestType**, **responseType**, requestStream, responseStream
|
|
102
|
+
Install [`long`](https://github.com/dcodeIO/long.js) with protobuf.js when exact 64-bit integer support is required.
|
|
305
103
|
|
|
306
|
-
|
|
307
|
-
* `T.fromJSON(name, json)` creates the respective reflection object from a JSON descriptor
|
|
308
|
-
* `T#toJSON()` creates a JSON descriptor from the respective reflection object (its name is used as the key within the parent)
|
|
309
|
-
|
|
310
|
-
Exclusively using JSON descriptors instead of .proto files enables the use of just the light library (the parser isn't required in this case).
|
|
311
|
-
|
|
312
|
-
A JSON descriptor can either be loaded the usual way:
|
|
104
|
+
### Convert plain objects
|
|
313
105
|
|
|
314
106
|
```js
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
107
|
+
const message = AwesomeMessage.fromObject({ awesomeField: 42 });
|
|
108
|
+
const object = AwesomeMessage.toObject(message, {
|
|
109
|
+
longs: String,
|
|
110
|
+
enums: String,
|
|
111
|
+
bytes: String
|
|
319
112
|
});
|
|
320
113
|
```
|
|
321
114
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
```js
|
|
325
|
-
var jsonDescriptor = require("./awesome.json"); // exemplary for node
|
|
326
|
-
|
|
327
|
-
var root = protobuf.Root.fromJSON(jsonDescriptor);
|
|
328
|
-
|
|
329
|
-
// Continue at "Obtain a message type" above
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
### Using reflection only
|
|
115
|
+
Common `ConversionOptions` are:
|
|
333
116
|
|
|
334
|
-
|
|
117
|
+
| Option | Effect |
|
|
118
|
+
|--------|--------|
|
|
119
|
+
| `longs: String` | Converts 64-bit values to decimal strings |
|
|
120
|
+
| `longs: Number` | Converts 64-bit values to JS numbers (may lose precision) |
|
|
121
|
+
| `enums: String` | Converts enum values to names |
|
|
122
|
+
| `bytes: String` | Converts bytes to base64 strings |
|
|
123
|
+
| `defaults: true` | Includes default values for unset fields |
|
|
124
|
+
| `arrays: true` | Includes empty arrays for repeated fields |
|
|
125
|
+
| `objects: true` | Includes empty objects for map fields |
|
|
126
|
+
| `oneofs: true` | Includes virtual oneof discriminator properties |
|
|
335
127
|
|
|
336
|
-
|
|
337
|
-
...
|
|
338
|
-
var Root = protobuf.Root,
|
|
339
|
-
Type = protobuf.Type,
|
|
340
|
-
Field = protobuf.Field;
|
|
128
|
+
## Message API
|
|
341
129
|
|
|
342
|
-
|
|
130
|
+
Message types expose focused methods for validation, conversion, and binary I/O.
|
|
343
131
|
|
|
344
|
-
|
|
132
|
+
* **verify**(object: `object`): `null | string`
|
|
133
|
+
Checks whether a plain object can be encoded as-is. Returns `null` if valid, otherwise an error message.
|
|
345
134
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
```
|
|
135
|
+
* **create**(properties?: `object`): `Message`
|
|
136
|
+
Creates a message instance from already valid data.
|
|
349
137
|
|
|
350
|
-
|
|
138
|
+
* **fromObject**(object: `object`): `Message`
|
|
139
|
+
Converts broader JavaScript input into a message instance.
|
|
351
140
|
|
|
352
|
-
|
|
141
|
+
* **toObject**(message: `Message`, options?: `ConversionOptions`): `object`
|
|
142
|
+
Converts a message instance to a plain object for JSON or interoperability.
|
|
353
143
|
|
|
354
|
-
|
|
144
|
+
* **encode**(message: `Message | object`, writer?: `Writer`): `Writer`
|
|
145
|
+
Encodes a message or equivalent plain object. Call `.finish()` on the returned writer to obtain a buffer.
|
|
355
146
|
|
|
356
|
-
|
|
357
|
-
|
|
147
|
+
* **encodeDelimited**(message: `Message | object`, writer?: `Writer`): `Writer`
|
|
148
|
+
Encodes a length-delimited message.
|
|
358
149
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
// custom initialization code
|
|
362
|
-
...
|
|
363
|
-
}
|
|
150
|
+
* **decode**(reader: `Reader | Uint8Array`): `Message`
|
|
151
|
+
Decodes a message from protobuf binary data.
|
|
364
152
|
|
|
365
|
-
|
|
366
|
-
|
|
153
|
+
* **decodeDelimited**(reader: `Reader | Uint8Array`): `Message`
|
|
154
|
+
Decodes a length-delimited message.
|
|
367
155
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
|
|
156
|
+
* **message#toJSON**(): `object`
|
|
157
|
+
Converts a message instance to JSON-compatible output using default conversion options.
|
|
371
158
|
|
|
372
|
-
|
|
373
|
-
```
|
|
159
|
+
Length-delimited methods read and write a varint byte length before the message, which is useful for streams and framed protocols.
|
|
374
160
|
|
|
375
|
-
|
|
161
|
+
If required fields are missing while decoding proto2 data, `decode` throws `protobuf.util.ProtocolError` with the partially decoded message available as `err.instance`.
|
|
376
162
|
|
|
377
|
-
|
|
378
|
-
* `AwesomeMessage.encode` and `AwesomeMessage.encodeDelimited`
|
|
379
|
-
* `AwesomeMessage.decode` and `AwesomeMessage.decodeDelimited`
|
|
380
|
-
* `AwesomeMessage.verify`
|
|
381
|
-
* `AwesomeMessage.fromObject`, `AwesomeMessage.toObject` and `AwesomeMessage#toJSON`
|
|
163
|
+
## Code generation
|
|
382
164
|
|
|
383
|
-
|
|
165
|
+
Use [`protobufjs-cli`](./cli/) to generate reflection bundles, static JavaScript code and TypeScript declarations, either directly with `pbjs` or through its `protoc-gen-pbjs` plugin.
|
|
384
166
|
|
|
385
|
-
|
|
167
|
+
Reflection keeps schemas as descriptors and generates optimized functions at runtime. Static code emits the same optimized functions ahead of time. The main tradeoffs are how schemas are loaded, how bundle size scales with schema size, whether runtime code generation is allowed by your environment, and whether reflection metadata should remain available at runtime.
|
|
386
168
|
|
|
387
|
-
|
|
388
|
-
|
|
169
|
+
| Target | Output | Minimum Runtime |
|
|
170
|
+
|--------|--------|-----------------|
|
|
171
|
+
| `json` | JSON bundle | `protobufjs/light.js` |
|
|
172
|
+
| `json-module` | JSON bundle module | `protobufjs/light.js` |
|
|
173
|
+
| `static` | Static code | custom wrapper/integration, not standalone |
|
|
174
|
+
| `static-module` | Static code module | `protobufjs/minimal.js` |
|
|
389
175
|
|
|
390
|
-
|
|
391
|
-
var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
|
|
176
|
+
Module targets support `--wrap default` for CommonJS and AMD, plus `commonjs`, `amd`, `esm`, and `closure`; `--wrap` can also load a custom wrapper module.
|
|
392
177
|
|
|
393
|
-
|
|
394
|
-
AwesomeMessage.customStaticMethod = function() { ... };
|
|
395
|
-
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
|
|
178
|
+
### Static modules
|
|
396
179
|
|
|
397
|
-
|
|
398
|
-
```
|
|
180
|
+
Static modules generate dedicated JavaScript code for your schema, so they only need `protobufjs/minimal.js`.
|
|
399
181
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
The library also supports consuming services but it doesn't make any assumptions about the actual transport channel. Instead, a user must provide a suitable RPC implementation, which is an asynchronous function that takes the reflected service method, the binary request and a node-style callback as its parameters:
|
|
403
|
-
|
|
404
|
-
```js
|
|
405
|
-
function rpcImpl(method, requestData, callback) {
|
|
406
|
-
// perform the request using an HTTP request or a WebSocket for example
|
|
407
|
-
var responseData = ...;
|
|
408
|
-
// and call the callback with the binary response afterwards:
|
|
409
|
-
callback(null, responseData);
|
|
410
|
-
}
|
|
182
|
+
```sh
|
|
183
|
+
npx pbjs -t static-module -w esm -o awesome.js --dts awesome.proto
|
|
411
184
|
```
|
|
412
185
|
|
|
413
|
-
Below is a working example with a typescript implementation using grpc npm package.
|
|
414
186
|
```ts
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
const client = new Client(
|
|
419
|
-
grpcServerUrl,
|
|
420
|
-
grpc.credentials.createInsecure()
|
|
421
|
-
)
|
|
422
|
-
|
|
423
|
-
const rpcImpl = function(method, requestData, callback) {
|
|
424
|
-
client.makeUnaryRequest(
|
|
425
|
-
method.name,
|
|
426
|
-
arg => arg,
|
|
427
|
-
arg => arg,
|
|
428
|
-
requestData,
|
|
429
|
-
callback
|
|
430
|
-
)
|
|
431
|
-
}
|
|
187
|
+
import { awesomepackage } from "./awesome.js";
|
|
188
|
+
|
|
189
|
+
const message = awesomepackage.AwesomeMessage.create({ awesomeField: "hello" });
|
|
432
190
|
```
|
|
433
191
|
|
|
434
|
-
|
|
192
|
+
### Reflection bundles
|
|
435
193
|
|
|
436
|
-
|
|
437
|
-
// greeter.proto
|
|
438
|
-
syntax = "proto3";
|
|
439
|
-
|
|
440
|
-
service Greeter {
|
|
441
|
-
rpc SayHello (HelloRequest) returns (HelloReply) {}
|
|
442
|
-
}
|
|
194
|
+
Bundling schemas avoids reparsing `.proto` files at runtime and can reduce browser requests when schemas would otherwise be loaded separately. While reflection requires at least `protobufjs/light.js`, large schemas often produce smaller bundles than equivalent static modules because most code is shared via reflection.
|
|
443
195
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
message HelloReply {
|
|
449
|
-
string message = 1;
|
|
450
|
-
}
|
|
196
|
+
```sh
|
|
197
|
+
npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
|
|
451
198
|
```
|
|
452
199
|
|
|
453
200
|
```js
|
|
454
|
-
|
|
455
|
-
var Greeter = root.lookup("Greeter");
|
|
456
|
-
var greeter = Greeter.create(/* see above */ rpcImpl, /* request delimited? */ false, /* response delimited? */ false);
|
|
201
|
+
const bundle = require("./awesome.json");
|
|
457
202
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
});
|
|
203
|
+
const root = protobuf.Root.fromJSON(bundle);
|
|
204
|
+
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
461
205
|
```
|
|
462
206
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
```js
|
|
466
|
-
greeter.sayHello({ name: 'you' })
|
|
467
|
-
.then(function(response) {
|
|
468
|
-
console.log('Greeting:', response.message);
|
|
469
|
-
});
|
|
207
|
+
```sh
|
|
208
|
+
npx pbjs -t json-module -w esm -o awesome.js --dts awesome.proto
|
|
470
209
|
```
|
|
471
210
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
Note that the service API is meant for clients. Implementing a server-side endpoint pretty much always requires transport channel (i.e. http, websocket, etc.) specific code with the only common denominator being that it decodes and encodes messages.
|
|
475
|
-
|
|
476
|
-
### Usage with TypeScript
|
|
477
|
-
|
|
478
|
-
The library ships with its own [type definitions](https://github.com/protobufjs/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) will automatically detect and use them for code completion.
|
|
211
|
+
```js
|
|
212
|
+
import { awesomepackage } from "./awesome.js";
|
|
479
213
|
|
|
480
|
-
|
|
214
|
+
const AwesomeMessage = awesomepackage.AwesomeMessage;
|
|
215
|
+
```
|
|
481
216
|
|
|
482
|
-
|
|
217
|
+
JSON modules export the reflection root and, with `-w esm`, also provide top-level named exports that align with static modules. Their declarations mirror `static-module` typings, but because JSON modules are backed by reflection objects, message instances should be created with `MyMessage.create(...)` instead of constructors. Code using `create(...)` works with static modules as well.
|
|
483
218
|
|
|
484
|
-
|
|
219
|
+
### TypeScript integration
|
|
485
220
|
|
|
486
|
-
|
|
487
|
-
import { load } from "protobufjs"; // respectively "./node_modules/protobufjs"
|
|
221
|
+
TypeScript is a first-class target in protobuf.js. The runtime API is typed, and with `--dts`, both `json-module` and `static-module` emit ready-to-run JavaScript modules together with matching TypeScript declarations. No separate transpile step is necessary.
|
|
488
222
|
|
|
489
|
-
|
|
490
|
-
if (err)
|
|
491
|
-
throw err;
|
|
223
|
+
## Advanced usage
|
|
492
224
|
|
|
493
|
-
|
|
494
|
-
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
225
|
+
### Programmatic schemas
|
|
495
226
|
|
|
496
|
-
|
|
497
|
-
console.log(`message = ${JSON.stringify(message)}`);
|
|
227
|
+
The full and light builds can construct schemas directly through reflection:
|
|
498
228
|
|
|
499
|
-
|
|
500
|
-
|
|
229
|
+
```js
|
|
230
|
+
const AwesomeMessage = new protobuf.Type("AwesomeMessage")
|
|
231
|
+
.add(new protobuf.Field("awesomeField", 1, "string"));
|
|
501
232
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
233
|
+
const root = new protobuf.Root()
|
|
234
|
+
.define("awesomepackage")
|
|
235
|
+
.add(AwesomeMessage);
|
|
505
236
|
```
|
|
506
237
|
|
|
507
|
-
|
|
238
|
+
### Custom message classes
|
|
508
239
|
|
|
509
|
-
|
|
240
|
+
Message classes can be extended by reusing the generated constructor:
|
|
510
241
|
|
|
511
|
-
```
|
|
512
|
-
|
|
242
|
+
```js
|
|
243
|
+
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
|
|
513
244
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
245
|
+
AwesomeMessage.customStaticMethod = function() {
|
|
246
|
+
// ...
|
|
247
|
+
};
|
|
248
|
+
AwesomeMessage.prototype.customInstanceMethod = function() {
|
|
249
|
+
// ...
|
|
250
|
+
};
|
|
518
251
|
```
|
|
519
252
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
The library also includes an early implementation of [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
|
|
523
|
-
|
|
524
|
-
**Note** that decorators are an experimental feature in TypeScript and that declaration order is important depending on the JS target. For example, `@Field.d(2, AwesomeArrayMessage)` requires that `AwesomeArrayMessage` has been defined earlier when targeting `ES5`.
|
|
525
|
-
|
|
526
|
-
```ts
|
|
527
|
-
import { Message, Type, Field, OneOf } from "protobufjs/light"; // respectively "./node_modules/protobufjs/light.js"
|
|
528
|
-
|
|
529
|
-
export class AwesomeSubMessage extends Message<AwesomeSubMessage> {
|
|
530
|
-
|
|
531
|
-
@Field.d(1, "string")
|
|
532
|
-
public awesomeString: string;
|
|
253
|
+
Alternatively, a custom constructor can be registered:
|
|
533
254
|
|
|
255
|
+
```js
|
|
256
|
+
function AwesomeMessage(properties) {
|
|
257
|
+
// ...
|
|
534
258
|
}
|
|
535
259
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
TWO = 2
|
|
539
|
-
}
|
|
260
|
+
root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;
|
|
261
|
+
```
|
|
540
262
|
|
|
541
|
-
|
|
542
|
-
export class AwesomeMessage extends Message<AwesomeMessage> {
|
|
263
|
+
Custom constructors are populated with static `create`, `encode`, `encodeDelimited`, `decode`, `decodeDelimited`, `verify`, `fromObject`, `toObject`, and the instance method `toJSON`. The reflected type is available as `AwesomeMessage.$type` and `message.$type`.
|
|
543
264
|
|
|
544
|
-
|
|
545
|
-
public awesomeField: string;
|
|
265
|
+
### Services
|
|
546
266
|
|
|
547
|
-
|
|
548
|
-
public awesomeSubMessage: AwesomeSubMessage;
|
|
267
|
+
protobuf.js supports service clients built from reflected service definitions. The service API is transport-agnostic: provide an `rpcImpl` function to connect it to HTTP, WebSocket, gRPC, or another transport. See [examples/streaming-rpc.js](./examples/streaming-rpc.js) for details.
|
|
549
268
|
|
|
550
|
-
|
|
551
|
-
public awesomeEnum: AwesomeEnum;
|
|
269
|
+
### Descriptors
|
|
552
270
|
|
|
553
|
-
|
|
554
|
-
public which: string;
|
|
271
|
+
For `google/protobuf/descriptor.proto` interoperability, see [ext/descriptor](./ext/README.md#descriptor). Note that because protobuf.js does not use `descriptor.proto` internally, options are parsed and presented literally.
|
|
555
272
|
|
|
556
|
-
|
|
273
|
+
### Text format
|
|
557
274
|
|
|
558
|
-
|
|
559
|
-
let message = new AwesomeMessage({ awesomeField: "hello" });
|
|
560
|
-
let buffer = AwesomeMessage.encode(message).finish();
|
|
561
|
-
let decoded = AwesomeMessage.decode(buffer);
|
|
562
|
-
```
|
|
275
|
+
Protocol Buffers [Text Format](https://protobuf.dev/reference/protobuf/textformat-spec/) is supported via [ext/textformat](./ext/README.md#textformat).
|
|
563
276
|
|
|
564
|
-
|
|
277
|
+
### Content Security Policy
|
|
565
278
|
|
|
566
|
-
|
|
567
|
-
annotates a class as a protobuf message type. If `typeName` is not specified, the constructor's runtime function name is used for the reflected type.
|
|
279
|
+
In [CSP](https://w3c.github.io/webappsec-csp/)-restricted environments that disallow unsafe-eval, use generated static code instead of runtime code generation.
|
|
568
280
|
|
|
569
|
-
|
|
570
|
-
annotates a property as a protobuf field with the specified id and protobuf type.
|
|
281
|
+
## Compatibility
|
|
571
282
|
|
|
572
|
-
|
|
573
|
-
annotates a property as a protobuf map field with the specified id, protobuf key and value type.
|
|
283
|
+
Supported runtimes are browsers, Node.js v12+, Deno (`deno add npm:protobufjs`) and Bun (`bun add protobufjs`). When using the CLI with Bun, Node.js must also be installed.
|
|
574
284
|
|
|
575
|
-
|
|
576
|
-
annotates a property as a protobuf oneof covering the specified fields.
|
|
285
|
+
## Security
|
|
577
286
|
|
|
578
|
-
|
|
287
|
+
protobuf.js favors transparent disclosure. Security-impacting reports are handled through coordinated GitHub Security Advisories where appropriate. See [SECURITY.md](./SECURITY.md) for supported release lines, reporting instructions, and notes on untrusted schema input.
|
|
579
288
|
|
|
580
|
-
|
|
581
|
-
* Enums are copied to a reflected enum with a generic name on decorator evaluation because referenced enum objects have no runtime name the decorator could use.
|
|
582
|
-
* Default values must be specified as arguments to the decorator instead of using a property initializer for proper prototype behavior.
|
|
583
|
-
* Property names on decorated classes must not be renamed on compile time (i.e. by a minifier) because decorators just receive the original field name as a string.
|
|
289
|
+
## Conformance
|
|
584
290
|
|
|
585
|
-
**
|
|
291
|
+
protobuf.js targets full binary wire-format conformance for **Proto2**, **Proto3** and **Editions**. CI runs the official Protocol Buffers conformance suite, with logs uploaded as artifacts.
|
|
586
292
|
|
|
587
|
-
|
|
588
|
-
|
|
293
|
+
| Syntax | Total | Required | Recommended |
|
|
294
|
+
| -------- | ------------------: | ----------------: | ----------------: |
|
|
295
|
+
| Proto2 | 100.00% (694/694) | 100.00% (485/485) | 100.00% (209/209) |
|
|
296
|
+
| Proto3 | 100.00% (689/689) | 100.00% (482/482) | 100.00% (207/207) |
|
|
297
|
+
| Editions | 100.00% (1176/1176) | 100.00% (926/926) | 100.00% (250/250) |
|
|
589
298
|
|
|
590
|
-
|
|
591
|
-
* [Google's Developer Guide](https://protobuf.dev/overview/)
|
|
299
|
+
## Performance
|
|
592
300
|
|
|
593
|
-
|
|
594
|
-
* [API Documentation](https://protobufjs.github.io/protobuf.js)
|
|
595
|
-
* [CHANGELOG](https://github.com/protobufjs/protobuf.js/blob/master/CHANGELOG.md)
|
|
596
|
-
* [Frequently asked questions](https://github.com/protobufjs/protobuf.js/wiki) on our wiki
|
|
301
|
+
In both reflection and static modes, protobuf.js builds specialized encoders and decoders for each message type instead of interpreting descriptors at runtime.
|
|
597
302
|
|
|
598
|
-
|
|
599
|
-
* [Questions and answers](http://stackoverflow.com/search?tab=newest&q=protobuf.js) on StackOverflow
|
|
303
|
+
The repository includes a small benchmark for the bundled fixture in [`bench/`](./bench/). It compares protobuf.js reflection and static code against native `JSON.stringify`/`JSON.parse` and [google-protobuf](https://www.npmjs.com/package/google-protobuf) (`protoc-gen-js`). Results depend on hardware, Node.js version, and the message shape, so they should be treated as indicative rather than absolute.
|
|
600
304
|
|
|
601
|
-
|
|
602
|
-
-----------
|
|
603
|
-
The package includes a benchmark that compares protobuf.js performance to native JSON (as far as this is possible) and [Google's JS implementation](https://github.com/google/protobuf/tree/master/js). On an i7-2600K running node 6.9.1 it yields:
|
|
305
|
+
One run on an AMD Ryzen 9 9950X3D with Node.js 24.13.0 and google-protobuf 4.0.2 produced:
|
|
604
306
|
|
|
605
307
|
```
|
|
606
308
|
benchmarking encoding performance ...
|
|
607
309
|
|
|
608
|
-
protobuf.js (reflect) x
|
|
609
|
-
protobuf.js (static) x
|
|
610
|
-
JSON (string) x
|
|
611
|
-
JSON (buffer) x
|
|
612
|
-
google-protobuf x
|
|
310
|
+
protobuf.js (reflect) x 2,492,315 ops/sec ±0.56% (94 runs sampled)
|
|
311
|
+
protobuf.js (static) x 2,316,421 ops/sec ±0.49% (96 runs sampled)
|
|
312
|
+
JSON (string) x 2,581,565 ops/sec ±0.22% (99 runs sampled)
|
|
313
|
+
JSON (buffer) x 2,086,216 ops/sec ±0.68% (92 runs sampled)
|
|
314
|
+
google-protobuf x 1,052,145 ops/sec ±0.24% (101 runs sampled)
|
|
613
315
|
|
|
614
|
-
|
|
615
|
-
protobuf.js (reflect) was
|
|
616
|
-
|
|
617
|
-
JSON (buffer) was
|
|
618
|
-
google-protobuf was
|
|
316
|
+
JSON (string) was fastest
|
|
317
|
+
protobuf.js (reflect) was 3.8% ops/sec slower (factor 1.0)
|
|
318
|
+
protobuf.js (static) was 10.5% ops/sec slower (factor 1.1)
|
|
319
|
+
JSON (buffer) was 19.6% ops/sec slower (factor 1.2)
|
|
320
|
+
google-protobuf was 59.3% ops/sec slower (factor 2.5)
|
|
619
321
|
|
|
620
322
|
benchmarking decoding performance ...
|
|
621
323
|
|
|
622
|
-
protobuf.js (reflect) x
|
|
623
|
-
protobuf.js (static) x
|
|
624
|
-
JSON (string) x
|
|
625
|
-
JSON (buffer) x
|
|
626
|
-
google-protobuf x
|
|
324
|
+
protobuf.js (reflect) x 6,053,370 ops/sec ±0.25% (98 runs sampled)
|
|
325
|
+
protobuf.js (static) x 6,081,190 ops/sec ±0.35% (97 runs sampled)
|
|
326
|
+
JSON (string) x 1,579,677 ops/sec ±0.11% (100 runs sampled)
|
|
327
|
+
JSON (buffer) x 1,383,484 ops/sec ±0.15% (98 runs sampled)
|
|
328
|
+
google-protobuf x 931,575 ops/sec ±0.25% (97 runs sampled)
|
|
627
329
|
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
JSON (string) was
|
|
631
|
-
JSON (buffer) was
|
|
632
|
-
google-protobuf was
|
|
330
|
+
protobuf.js (static) was fastest
|
|
331
|
+
protobuf.js (reflect) was 0.4% ops/sec slower (factor 1.0)
|
|
332
|
+
JSON (string) was 74.0% ops/sec slower (factor 3.8)
|
|
333
|
+
JSON (buffer) was 77.2% ops/sec slower (factor 4.4)
|
|
334
|
+
google-protobuf was 84.7% ops/sec slower (factor 6.5)
|
|
633
335
|
|
|
634
336
|
benchmarking combined performance ...
|
|
635
337
|
|
|
636
|
-
protobuf.js (reflect) x
|
|
637
|
-
protobuf.js (static) x
|
|
638
|
-
JSON (string) x
|
|
639
|
-
JSON (buffer) x
|
|
640
|
-
google-protobuf x
|
|
338
|
+
protobuf.js (reflect) x 1,259,417 ops/sec ±0.33% (101 runs sampled)
|
|
339
|
+
protobuf.js (static) x 1,296,628 ops/sec ±0.20% (98 runs sampled)
|
|
340
|
+
JSON (string) x 848,422 ops/sec ±0.10% (100 runs sampled)
|
|
341
|
+
JSON (buffer) x 727,866 ops/sec ±0.30% (100 runs sampled)
|
|
342
|
+
google-protobuf x 477,041 ops/sec ±0.22% (101 runs sampled)
|
|
641
343
|
|
|
642
344
|
protobuf.js (static) was fastest
|
|
643
|
-
protobuf.js (reflect) was
|
|
644
|
-
JSON (string) was
|
|
645
|
-
JSON (buffer) was
|
|
646
|
-
google-protobuf was
|
|
647
|
-
```
|
|
648
|
-
|
|
649
|
-
These results are achieved by
|
|
650
|
-
|
|
651
|
-
* generating type-specific encoders, decoders, verifiers and converters at runtime
|
|
652
|
-
* configuring the reader/writer interface according to the environment
|
|
653
|
-
* using node-specific functionality where beneficial and, of course
|
|
654
|
-
* avoiding unnecessary operations through splitting up [the toolset](#toolset).
|
|
655
|
-
|
|
656
|
-
You can also run [the benchmark](https://github.com/protobufjs/protobuf.js/blob/master/bench/index.js) ...
|
|
657
|
-
|
|
345
|
+
protobuf.js (reflect) was 3.0% ops/sec slower (factor 1.0)
|
|
346
|
+
JSON (string) was 34.5% ops/sec slower (factor 1.5)
|
|
347
|
+
JSON (buffer) was 43.9% ops/sec slower (factor 1.8)
|
|
348
|
+
google-protobuf was 63.2% ops/sec slower (factor 2.7)
|
|
658
349
|
```
|
|
659
|
-
$> npm run bench
|
|
660
|
-
```
|
|
661
|
-
|
|
662
|
-
and [the profiler](https://github.com/protobufjs/protobuf.js/blob/master/bench/prof.js) yourself (the latter requires a recent version of node):
|
|
663
|
-
|
|
664
|
-
```
|
|
665
|
-
$> npm run prof <encode|decode|encode-browser|decode-browser> [iterations=10000000]
|
|
666
|
-
```
|
|
667
|
-
|
|
668
|
-
Note that as of this writing, the benchmark suite performs significantly slower on node 7.2.0 compared to 6.9.1 because moths.
|
|
669
350
|
|
|
670
|
-
|
|
671
|
-
-------------
|
|
351
|
+
Run it locally with:
|
|
672
352
|
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
* If typed arrays are not supported by the environment, plain arrays will be used instead.
|
|
676
|
-
* Support for pre-ES5 environments (except IE8) can be achieved by [using a polyfill](https://github.com/protobufjs/protobuf.js/blob/master/lib/polyfill.js).
|
|
677
|
-
* Support for [Content Security Policy](https://w3c.github.io/webappsec-csp/)-restricted environments (like Chrome extensions without unsafe-eval) can be achieved by generating and using static code instead.
|
|
678
|
-
* If a proper way to work with 64 bit values (uint64, int64 etc.) is required, just install [long.js](https://github.com/dcodeIO/long.js) alongside this library. All 64 bit numbers will then be returned as a `Long` instance instead of a possibly unsafe JavaScript number ([see](https://github.com/dcodeIO/long.js)).
|
|
679
|
-
* For descriptor.proto interoperability, see [ext/descriptor](https://github.com/protobufjs/protobuf.js/tree/master/ext/descriptor)
|
|
680
|
-
|
|
681
|
-
Building
|
|
682
|
-
--------
|
|
683
|
-
|
|
684
|
-
To build the library or its components yourself, clone it from GitHub and install the development dependencies:
|
|
685
|
-
|
|
686
|
-
```
|
|
687
|
-
$> git clone https://github.com/protobufjs/protobuf.js.git
|
|
688
|
-
$> cd protobuf.js
|
|
689
|
-
$> npm install
|
|
353
|
+
```sh
|
|
354
|
+
npm run bench
|
|
690
355
|
```
|
|
691
356
|
|
|
692
|
-
|
|
357
|
+
## Development
|
|
693
358
|
|
|
694
|
-
```
|
|
695
|
-
|
|
359
|
+
```sh
|
|
360
|
+
git clone https://github.com/protobufjs/protobuf.js
|
|
361
|
+
cd protobuf.js
|
|
362
|
+
npm install
|
|
696
363
|
```
|
|
697
364
|
|
|
698
|
-
|
|
365
|
+
Running the tests:
|
|
699
366
|
|
|
700
|
-
```
|
|
701
|
-
|
|
367
|
+
```sh
|
|
368
|
+
npm test
|
|
702
369
|
```
|
|
703
370
|
|
|
704
|
-
Building the
|
|
371
|
+
Building the development and production versions with their respective source maps to `dist/`:
|
|
705
372
|
|
|
373
|
+
```sh
|
|
374
|
+
npm run build
|
|
706
375
|
```
|
|
707
|
-
$> npm run build:types
|
|
708
|
-
```
|
|
709
|
-
|
|
710
|
-
### Browserify integration
|
|
711
|
-
|
|
712
|
-
By default, protobuf.js integrates into any browserify build-process without requiring any optional modules. Hence:
|
|
713
|
-
|
|
714
|
-
* If int64 support is required, explicitly require the `long` module somewhere in your project as it will be excluded otherwise. This assumes that a global `require` function is present that protobuf.js can call to obtain the long module.
|
|
715
376
|
|
|
716
|
-
|
|
377
|
+
## Additional documentation
|
|
717
378
|
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
protobuf.util.Long = Long;
|
|
722
|
-
protobuf.configure();
|
|
723
|
-
```
|
|
724
|
-
|
|
725
|
-
* If you have any special requirements, there is [the bundler](https://github.com/protobufjs/protobuf.js/blob/master/scripts/bundle.js) for reference.
|
|
726
|
-
|
|
727
|
-
**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
|
|
379
|
+
* [API Documentation](https://protobufjs.github.io/protobuf.js)
|
|
380
|
+
* [Changelog](./CHANGELOG.md)
|
|
381
|
+
* [Protocol Buffers Documentation](https://protobuf.dev/)
|