You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+79-18Lines changed: 79 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,11 +67,9 @@ Production:
67
67
68
68
**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/tags) your project depends upon.
69
69
70
-
Or [download](https://github.com/dcodeIO/protobuf.js/tree/master/dist) the library.
71
-
72
70
The `protobuf` namespace will always be available globally / also supports AMD loaders.
73
71
74
-
Additionally, the library is compiled in different versions. Which one to use depends on whether size is a factor and your use case:
72
+
Additionally, the library is compiled in different versions. Which one to use depends on whether bundle size is a factor and your use case:
75
73
76
74
| Build | Downloads | How to require | Description
It's super easy to load an existing .proto file using the full build, which parses and compiles the definitions to ready to use runtime message classes:
The library utilizes a JSON format that is equivalent to a .proto definition (see also: [Command line usage](#command-line)). The following is identical to the .proto definition seen above:
135
+
136
+
```json
137
+
// awesome.json
138
+
{
139
+
"nested": {
140
+
"AwesomeMessage": {
141
+
"fields": {
142
+
"awesomeField": {
143
+
"type": "string",
144
+
"id": 1
145
+
}
146
+
}
147
+
}
148
+
}
149
+
}
150
+
```
151
+
152
+
A JSON descriptor can either be loaded the usual way:
Both the full and the light build include full reflection support. You could, for example, define the .proto definitions seen in the example above using just reflection:
174
+
134
175
```js
135
176
...
136
177
var Root =protobuf.Root,
@@ -145,36 +186,41 @@ var root = new Root().define("awesomepackage").add(AwesomeMessage);
145
186
...
146
187
```
147
188
189
+
Detailed information on the reflection structure is available within the [documentation](#documentation).
190
+
148
191
### Using custom classes
149
192
193
+
You can also extend runtime message classes with your own custom functionality by registering your own class with a reflected message type:
194
+
150
195
```js
151
196
...
152
197
153
-
//define your own prototypical class
198
+
//Define your own prototypal class
154
199
functionAwesomeMessage(properties) {
155
200
protobuf.Message.call(this, properties); // call the super constructor
156
201
}
157
202
158
-
//register your custom class with its reflected type
203
+
//Register your custom class with its reflected type (*)
159
204
protobuf.Class.create(root.lookup("awesomepackage.AwesomeMessage") /* or use reflection */, AwesomeMessage);
var message =newAwesomeMessage({ awesomeField:"AwesomeString" });
167
-
168
-
// Continue at "Encode a message" above
210
+
// Continue at "Create a message" above (you can also use the constructor directly)
169
211
```
170
212
171
-
Custom classes are automatically populated with static `encode`, `encodeDelimited`, `decode`, `decodeDelimited` and `verify` methods and reference their reflected type via the `$type`property. Note that there are no methods (just `$type`) on instances by default as method names might conflict with field names.
213
+
(*) Besides referencing its reflected type through `AwesomeMessage.$type`and `AwesomeMesage#$type`, the respective custom class is automatically populated with:
172
214
173
-
### Using the Reader/Writer interface directly
215
+
*`AwesomeMessage.create`
216
+
*`AwesomeMessage.encode` and `AwesomeMessage.encodeDelimited`
217
+
*`AwesomeMessage.decode` and `AwesomeMessage.decodeDelimited`
218
+
*`AwesomeMessage.verify`
219
+
*`AwesomeMessage.fromObject`, `AwesomeMessage.toObject`, `AwesomeMessage#toObject` and `AwesomeMessage#toJSON`
174
220
175
-
While only useful for the adventurous cherishing an aversion to [generated static code](https://github.com/dcodeIO/protobuf.js#command-line), it's also possible to use the Reader/Writer interface directly depending just on the [minimal library][dist-minimal] ([basic example](https://github.com/dcodeIO/protobuf.js/blob/master/examples/reader-writer.js)).
221
+
### Using the Reader/Writer interface directly
176
222
177
-
Easy ways to obtain example code snippets are either setting `protobuf.util.codegen.verbose = true` while watching the magic as it happens, or simply inspecting generated static code.
223
+
While only useful for the adventurous cherishing an aversion to [generated static code](https://github.com/dcodeIO/protobuf.js#command-line), it's also possible to use the Reader/Writer interface directly depending just on the [minimal library][dist-minimal] ([basic example](https://github.com/dcodeIO/protobuf.js/blob/master/examples/reader-writer.js)). Easy ways to obtain example code snippets are either setting `protobuf.util.codegen.verbose = true` while watching the console as code generation happens, or simply inspecting generated static code.
178
224
179
225
### Using services
180
226
@@ -198,13 +244,22 @@ message HelloReply {
198
244
```js
199
245
...
200
246
var Greeter =root.lookup("Greeter");
201
-
var greeter =Greeter.create(rpcImpl, false, false); // rpcImpl (see below), requestDelimited?, responseDelimited?
247
+
var greeter =Greeter.create(/* see below */ rpcImpl, /* request delimited? */false, /* response delimited? */false);
Services can also be used with promises instead of node-style callbacks:
255
+
256
+
```js
257
+
greeter.sayHello({ name:'you' })
258
+
.then(function(response) {
259
+
console.log('Greeting:', response.message);
260
+
});
261
+
```
262
+
208
263
To make this work, all you have to do is provide an `rpcImpl`, which is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters. For example:
209
264
210
265
```js
@@ -260,15 +315,21 @@ var buffer = AwesomeMessage.encode(message).finish();
*[protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
0 commit comments