Skip to content

Commit c079c90

Browse files
committed
Post-merge; Reader/Writer example
1 parent 8d7bb1f commit c079c90

12 files changed

+89
-21
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,33 @@ var message = new AwesomeMessage({ awesomeField: "AwesomeString" });
166166

167167
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.
168168

169+
### Using the Reader/Writer interface directly
170+
171+
While meant for the adventurous, it's also possible to use the Reader/Writer interface directly to build custom encoders and decoders that work accross modern to ancient browsers and, of course, node:
172+
173+
```js
174+
var writer = protobuf.Writer.create();
175+
var buffer = writer
176+
.int32(/* id */ 1 << 3 | /* wireType */ 2)
177+
.string("hello world!")
178+
.finish();
179+
180+
var reader = protobuf.Reader.create(buffer);
181+
while (reader.pos < reader.len) {
182+
var tag = reader.int32();
183+
switch (/* id */ tag >>> 3) {
184+
case 1:
185+
console.log(reader.string());
186+
break;
187+
default:
188+
reader.skipType(/* wireType */ tag & 7);
189+
break;
190+
}
191+
}
192+
```
193+
194+
You can take pretty much any generated code snippet as a reference. Easy ways to obtain these are either setting `protobuf.util.codegen.verbose = true` while watching the magic as it happens, or simply inspecting [generated static code](https://github.com/dcodeIO/protobuf.js#command-line).
195+
169196
### Using services
170197

171198
```protobuf

dist/protobuf.js

+16-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

52 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js.gz

0 Bytes
Binary file not shown.

examples/reader-writer.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var protobuf = require("..");
2+
3+
var writer = protobuf.Writer.create();
4+
var buffer = writer
5+
.int32(1 << 3 | 2) // id 1, wireType 2
6+
.string("hello world!")
7+
.finish();
8+
9+
var reader = protobuf.Reader.create(buffer);
10+
while (reader.pos < reader.len) {
11+
var tag = reader.int32();
12+
switch (tag>>>3) {
13+
case 1:
14+
console.log(reader.string());
15+
break;
16+
default:
17+
reader.skipType(tag&7);
18+
break;
19+
}
20+
}

src/field.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ function Field(name, id, type, rule, extend, options) {
119119
*/
120120
this.long = util.Long ? types.long[type] !== undefined : false;
121121

122+
/**
123+
* Whether this field's value is a buffer.
124+
* @type {boolean}
125+
*/
126+
this.bytes = type === "bytes";
127+
122128
/**
123129
* Resolved type if not a basic type.
124130
* @type {?(Type|Enum)}
@@ -268,20 +274,20 @@ FieldPrototype.resolve = function resolve() {
268274
*/
269275
FieldPrototype.jsonConvert = function(value, options) {
270276
if (options) {
277+
if (value instanceof Message)
278+
return value.asJSON(options);
271279
if (this.resolvedType instanceof Enum && options["enum"] === String) // eslint-disable-line dot-notation
272280
return this.resolvedType.getValuesById()[value];
273-
else if (this.long && options.long)
281+
if (options.long && this.long)
274282
return options.long === Number
275283
? typeof value === "number"
276-
? value
277-
: util.Long.fromValue(value).toNumber()
284+
? value
285+
: util.LongBits.from(value).toNumber(this.type.charAt(0) === "u")
278286
: util.Long.fromValue(value, this.type.charAt(0) === "u").toString();
279-
else if (options.bytes && this.type === "bytes")
287+
if (options.bytes && this.bytes)
280288
return options.bytes === Array
281289
? Array.prototype.slice.call(value)
282290
: util.base64.encode(value, 0, value.length);
283-
else if(value instanceof Message)
284-
return value.asJSON(options);
285291
}
286292
return value;
287293
};

types/protobuf.js.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
2-
// Generated Thu, 15 Dec 2016 16:49:02 UTC
2+
// Generated Thu, 15 Dec 2016 17:47:18 UTC
33
declare module "protobufjs" {
44

55
/**
@@ -306,6 +306,12 @@ declare module "protobufjs" {
306306
*/
307307
long: boolean;
308308

309+
/**
310+
* Whether this field's value is a buffer.
311+
* @type {boolean}
312+
*/
313+
bytes: boolean;
314+
309315
/**
310316
* Resolved type if not a basic type.
311317
* @type {?(Type|Enum)}

0 commit comments

Comments
 (0)