Skip to content

Commit c365242

Browse files
committed
Docs: Reference Buffer for BufferReader/Writer, see #668
1 parent 6a0920b commit c365242

File tree

6 files changed

+99
-30
lines changed

6 files changed

+99
-30
lines changed

README.md

+31-17
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,18 @@ The library supports both reflection-based and code-based use cases:
7272

7373
There is a suitable distribution for each of these:
7474

75-
| Build | GZ Size | Downloads | How to require | Description
75+
| | Gzipped | Downloads | How to require | Description
7676
|---------|---------|------------------------------|---------------------------------|-------------
7777
| full | 18.5kb | [dist][dist-full] | `require("protobufjs")` | All features. Works with everything.
7878
| light | 15.5kb | [dist/light][dist-light] | `require("protobufjs/light")` | All features except tokenizer, parser and bundled common types. Works with JSON definitions, pure reflection and static code.
7979
| minimal | 6.0kb+ | [dist/minimal][dist-minimal] | `require("protobufjs/minimal")` | Just enough to run static code. No reflection.
8080

8181
In case of doubt you can just use the full library.
8282

83+
[dist-full]: https://github.com/dcodeIO/protobuf.js/tree/master/dist
84+
[dist-light]: https://github.com/dcodeIO/protobuf.js/tree/master/dist/light
85+
[dist-minimal]: https://github.com/dcodeIO/protobuf.js/tree/master/dist/minimal
86+
8387
Examples
8488
--------
8589

@@ -302,7 +306,7 @@ protobuf.load("awesome.proto", function(err, root) {
302306
});
303307
```
304308

305-
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.
309+
To achieve the same with static code generated by [pbjs](#command-line), there is the [pbts](#generating-typescript-definitions-from-static-modules) command line utility to generate type definitions from static code as well.
306310

307311
Let's say you generated your static code to `bundle.js` and its type definitions to `bundle.d.ts`, then you can do:
308312

@@ -358,13 +362,15 @@ Consolidates imports and converts between file formats.
358362
default Default wrapper supporting both CommonJS and AMD
359363
commonjs CommonJS wrapper
360364
amd AMD wrapper
361-
es6 ES6 wrapper
365+
es6 ES6 wrapper (implies --es6)
362366
363367
-r, --root Specifies an alternative protobuf.roots name.
364368
365369
-l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:
366370
367-
eslint-disable block-scoped-var, no-redeclare, no-control-regex
371+
eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins
372+
373+
--es6 Enables ES6 syntax (const/let instead of var)
368374
369375
Proto sources only:
370376
@@ -410,7 +416,7 @@ As you might have noticed, `pbjs` is also capable of generating static code. For
410416
$> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
411417
```
412418

413-
will generate static code for definitions within `file1.proto` and `file2.proto` to a CommonJS module `compiled.js`, which then can be used with just the [minimal library][dist-minimal].
419+
will generate static code for definitions within `file1.proto` and `file2.proto` to a CommonJS module `compiled.js`, which then can be used with just the [minimal library](#distributions).
414420

415421
**ProTip!** Documenting your .proto files with `/** ... */`-blocks or (trailing) `/// ...` lines translates to generated static code.
416422

@@ -421,16 +427,18 @@ Likewise, the `pbts` command line utility can be used to generate TypeScript def
421427
```
422428
Generates TypeScript definitions from annotated JavaScript files.
423429
424-
-n, --name Wraps everything in a module of the specified name.
425-
426430
-o, --out Saves to a file instead of writing to stdout.
427431
428-
-m, --main Whether building the main library without any imports.
429-
430432
-g, --global Name of the global object in browser environments, if any.
431433
432434
--no-comments Does not output any JSDoc comments.
433435
436+
Internal flags:
437+
438+
-n, --name Wraps everything in a module of the specified name.
439+
440+
-m, --main Whether building the main library without any imports.
441+
434442
usage: pbts [options] file1.js file2.js ... (or) other | pbts [options] -
435443
```
436444

@@ -455,9 +463,9 @@ $> pbjs -t static-module file1.proto file2.proto | pbts -o bundle.d.ts -
455463

456464
### On reflection vs. static code
457465

458-
While using .proto files directly requires the [full library][dist-full] respectively pure reflection/JSON the [light library][dist-light], pretty much all code but the relatively short descriptors is shared.
466+
While using .proto files directly requires the [full library](#distributions) respectively pure reflection/JSON the [light library](#distributions), pretty much all code but the relatively short descriptors is shared.
459467

460-
Static code, on the other hand, requires just the [minimal library][dist-minimal], but generates additional, albeit editable, source code without any reflection features.
468+
Static code, on the other hand, requires just the [minimal library](#distributions), but generates additional, albeit editable, source code without any reflection features.
461469

462470
There is no significant difference performance-wise as the code generated statically is pretty much the same as generated at runtime and both are largely interchangeable as seen in the previous section.
463471

@@ -590,11 +598,17 @@ $> npm run types
590598

591599
By default, protobuf.js integrates into your browserify build-process without requiring any optional modules. Hence:
592600

593-
* If you need int64 support, explicitly require the `long` module somewhere in your project. It will be excluded otherwise.
594-
* If you have any special requirements, there is [the bundler](https://github.com/dcodeIO/protobuf.js/blob/master/scripts/bundle.js) as a reference.
601+
* If you need int64 support, 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.
595602

596-
**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
603+
If there is no global `require` function present after bundling, it's also possible to assign the long module programmatically:
597604

598-
[dist-full]: https://github.com/dcodeIO/protobuf.js/tree/master/dist
599-
[dist-light]: https://github.com/dcodeIO/protobuf.js/tree/master/dist/light
600-
[dist-minimal]: https://github.com/dcodeIO/protobuf.js/tree/master/dist/minimal
605+
```js
606+
var Long = ...;
607+
608+
protobuf.util.Long = Long;
609+
protobuf.configure();
610+
```
611+
612+
* If you have any special requirements, there is [the bundler](https://github.com/dcodeIO/protobuf.js/blob/master/scripts/bundle.js) for reference.
613+
614+
**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)

cli/pbts.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ exports.main = function(args, callback) {
4545
"",
4646
chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
4747
"",
48-
" -n, --name Wraps everything in a module of the specified name.",
49-
"",
5048
" -o, --out Saves to a file instead of writing to stdout.",
5149
"",
52-
" -m, --main Whether building the main library without any imports.",
53-
"",
5450
" -g, --global Name of the global object in browser environments, if any.",
5551
"",
5652
" --no-comments Does not output any JSDoc comments.",
5753
"",
54+
chalk.bold.gray(" Internal flags:"),
55+
"",
56+
" -n, --name Wraps everything in a module of the specified name.",
57+
"",
58+
" -m, --main Whether building the main library without any imports.",
59+
"",
5860
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -"
5961
].join("\n"));
6062
if (callback)

index.d.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,10 @@ export class Reader {
11741174
/**
11751175
* Creates a new reader using the specified buffer.
11761176
* @function
1177-
* @param {Uint8Array} buffer Buffer to read from
1178-
* @returns {BufferReader|Reader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
1177+
* @param {Uint8Array|Buffer} buffer Buffer to read from
1178+
* @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
11791179
*/
1180-
static create(buffer: Uint8Array): (BufferReader|Reader);
1180+
static create(buffer: (Uint8Array|Buffer)): (Reader|BufferReader);
11811181

11821182
/**
11831183
* Reads a varint as an unsigned 32 bit value.
@@ -1314,6 +1314,21 @@ export class BufferReader extends Reader {
13141314
* @param {Buffer} buffer Buffer to read from
13151315
*/
13161316
constructor(buffer: Buffer);
1317+
1318+
/**
1319+
* Read buffer.
1320+
* @name BufferReader#buf
1321+
* @type {Buffer}
1322+
*/
1323+
buf: Buffer;
1324+
1325+
/**
1326+
* Reads a sequence of bytes preceeded by its length as a varint.
1327+
* @name BufferReader#bytes
1328+
* @function
1329+
* @returns {Buffer} Value read
1330+
*/
1331+
bytes(): Buffer;
13171332
}
13181333

13191334
/**
@@ -2703,9 +2718,24 @@ export class BufferWriter extends Writer {
27032718
/**
27042719
* Allocates a buffer of the specified size.
27052720
* @param {number} size Buffer size
2706-
* @returns {Uint8Array} Buffer
2721+
* @returns {Buffer} Buffer
27072722
*/
2708-
static alloc(size: number): Uint8Array;
2723+
static alloc(size: number): Buffer;
2724+
2725+
/**
2726+
* Writes a sequence of bytes.
2727+
* @param {Buffer|string} value Buffer or base64 encoded string to write
2728+
* @returns {Writer} `this`
2729+
*/
2730+
bytes(value: (Buffer|string)): Writer;
2731+
2732+
/**
2733+
* Finishes the write operation.
2734+
* @name BufferWriter#finish
2735+
* @function
2736+
* @returns {Buffer} Finished buffer
2737+
*/
2738+
finish(): Buffer;
27092739
}
27102740

27112741
/**

src/reader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function Reader(buffer) {
4343
/**
4444
* Creates a new reader using the specified buffer.
4545
* @function
46-
* @param {Uint8Array} buffer Buffer to read from
47-
* @returns {BufferReader|Reader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
46+
* @param {Uint8Array|Buffer} buffer Buffer to read from
47+
* @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
4848
*/
4949
Reader.create = util.Buffer
5050
? function create_buffer_setup(buffer) {

src/reader_buffer.js

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ var util = require("./util/minimal");
1616
*/
1717
function BufferReader(buffer) {
1818
Reader.call(this, buffer);
19+
20+
/**
21+
* Read buffer.
22+
* @name BufferReader#buf
23+
* @type {Buffer}
24+
*/
1925
}
2026

2127
/* istanbul ignore else */
@@ -29,3 +35,10 @@ BufferReader.prototype.string = function read_string_buffer() {
2935
var len = this.uint32(); // modifies pos
3036
return this.buf.utf8Slice(this.pos, this.pos = Math.min(this.pos + len, this.len));
3137
};
38+
39+
/**
40+
* Reads a sequence of bytes preceeded by its length as a varint.
41+
* @name BufferReader#bytes
42+
* @function
43+
* @returns {Buffer} Value read
44+
*/

src/writer_buffer.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function BufferWriter() {
2222
/**
2323
* Allocates a buffer of the specified size.
2424
* @param {number} size Buffer size
25-
* @returns {Uint8Array} Buffer
25+
* @returns {Buffer} Buffer
2626
*/
2727
BufferWriter.alloc = function alloc_buffer(size) {
2828
return (BufferWriter.alloc = util._Buffer_allocUnsafe)(size);
@@ -42,7 +42,9 @@ var writeBytesBuffer = Buffer && Buffer.prototype instanceof Uint8Array && Buffe
4242
};
4343

4444
/**
45-
* @override
45+
* Writes a sequence of bytes.
46+
* @param {Buffer|string} value Buffer or base64 encoded string to write
47+
* @returns {Writer} `this`
4648
*/
4749
BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
4850
if (util.isString(value))
@@ -71,3 +73,11 @@ BufferWriter.prototype.string = function write_string_buffer(value) {
7173
this.push(writeStringBuffer, len, value);
7274
return this;
7375
};
76+
77+
78+
/**
79+
* Finishes the write operation.
80+
* @name BufferWriter#finish
81+
* @function
82+
* @returns {Buffer} Finished buffer
83+
*/

0 commit comments

Comments
 (0)