Skip to content

Commit fdc3102

Browse files
committed
Docs: Added extended usage instructions for TypeScript and custom classes to README, see #666
1 parent 579068a commit fdc3102

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

Diff for: README.md

+39-4
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,19 @@ var root = new Root().define("awesomepackage").add(AwesomeMessage);
150150
```js
151151
...
152152

153+
// define your own prototypical class
153154
function AwesomeMessage(properties) {
154-
protobuf.Message.call(this, properties);
155+
protobuf.Message.call(this, properties); // call the super constructor
155156
}
157+
158+
// register your custom class with its reflected type
156159
protobuf.Class.create(root.lookup("awesomepackage.AwesomeMessage") /* or use reflection */, AwesomeMessage);
157160

161+
// define your custom functionality
162+
AwesomeMessage.customStaticMethod = function() { ... };
163+
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
164+
165+
// create a message
158166
var message = new AwesomeMessage({ awesomeField: "AwesomeString" });
159167

160168
// Continue at "Encode a message" above
@@ -212,15 +220,42 @@ There is also an [example for streaming RPC](https://github.com/dcodeIO/protobuf
212220

213221
### Usage with TypeScript
214222

223+
The library ships with its own [type definitions](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) should automatically detect and use them for code completion when following this pattern:
224+
215225
```ts
226+
// node.js
216227
import * as protobuf from "protobufjs";
217228
import * as Long from "long"; // optional
218-
...
229+
230+
// browser only (alternatively)
231+
import * as protobuf from "./node_modules/protobufjs/index.js";
232+
import * as Long from "./node_modules/long/dist/long.js"; // optional
233+
234+
protobuf.load("awesome.proto", function(err, root) {
235+
if (err)
236+
throw err;
237+
238+
// example code
239+
var AwesomeMessage = root.lookupType("AwesomeMessage");
240+
var message = AwesomeMessage.create({ awesomeField: "hello" });
241+
var buffer = AwesomeMessage.encode(message).finish();
242+
...
243+
});
219244
```
220245

221-
See also: [Generating your own TypeScript definitions](https://github.com/dcodeIO/protobuf.js#generating-typescript-definitions-from-static-modules)
246+
To achieve the same with static code generated by [pbjs](https://github.com/dcodeIO/protobuf.js#command-line), there is the [pbts](https://github.com/dcodeIO/protobuf.js#generating-typescript-definitions-from-static-modules) command line utility to generate type definitions from static code as well.
247+
248+
Let's say you generated your static code to `bundle.js` and its type definitions to `bundle.d.ts`, then you can do:
249+
250+
```ts
251+
import * as root from "./bundle.js";
222252

223-
Additional configuration might be necessary when not utilizing node, i.e. reference [protobuf.js.d.ts](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and [long.js.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/types-2.0/long/index.d.ts).
253+
// example code
254+
var AwesomeMessage = root.AwesomeMessage;
255+
var message = AwesomeMessage.create({ awesomeField: "hello" });
256+
var buffer = AwesomeMessage.encode(message).finish();
257+
...
258+
```
224259

225260
Documentation
226261
-------------

Diff for: src/util/minimal.js

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
"use strict";
22
var util = exports;
33

4-
util.asPromise = require("@protobufjs/aspromise");
5-
util.base64 = require("@protobufjs/base64");
4+
// used to return a Promise where callback is omitted
5+
util.asPromise = require("@protobufjs/aspromise");
6+
7+
// converts to / from base64 encoded strings
8+
util.base64 = require("@protobufjs/base64");
9+
10+
// base class of rpc.Service
611
util.EventEmitter = require("@protobufjs/eventemitter");
7-
util.inquire = require("@protobufjs/inquire");
8-
util.utf8 = require("@protobufjs/utf8");
9-
util.pool = require("@protobufjs/pool");
1012

11-
util.LongBits = require("./longbits");
13+
// requires modules optionally and hides the call from bundlers
14+
util.inquire = require("@protobufjs/inquire");
15+
16+
// convert to / from utf8 encoded strings
17+
util.utf8 = require("@protobufjs/utf8");
18+
19+
// provides a node-like buffer pool in the browser
20+
util.pool = require("@protobufjs/pool");
21+
22+
// utility to work with the low and high bits of a 64 bit value
23+
util.LongBits = require("./longbits");
1224

1325
/**
1426
* An immuable empty array.
1527
* @memberof util
1628
* @type {Array.<*>}
1729
*/
18-
util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ [];
30+
util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ []; // used on prototypes
1931

2032
/**
2133
* An immutable empty object.
2234
* @type {Object}
2335
*/
24-
util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {};
36+
util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes
2537

2638
/**
2739
* Whether running within node or not.
@@ -73,8 +85,23 @@ util.Buffer = (function() {
7385
}
7486
})();
7587

76-
// Aliases where supported, otherwise polyfills
88+
/**
89+
* Internal alias of or polyfull for Buffer.from.
90+
* @type {?function}
91+
* @param {string|number[]} value Value
92+
* @param {string} [encoding] Encoding if value is a string
93+
* @returns {Uint8Array}
94+
* @private
95+
*/
7796
util._Buffer_from = null;
97+
98+
/**
99+
* Internal alias of or polyfill for Buffer.allocUnsafe.
100+
* @type {?function}
101+
* @param {number} size Buffer size
102+
* @returns {Uint8Array}
103+
* @private
104+
*/
78105
util._Buffer_allocUnsafe = null;
79106

80107
/**
@@ -230,7 +257,7 @@ util._configure = function() {
230257
util._Buffer_from = util._Buffer_allocUnsafe = null;
231258
return;
232259
}
233-
// node 4.2.0 - 4.4.7 support makes it impossible to just polyfill these.
260+
// because node 4.x buffers are incompatible & immutable
234261
// see: https://github.com/dcodeIO/protobuf.js/pull/665
235262
util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
236263
/* istanbul ignore next */

0 commit comments

Comments
 (0)