Skip to content

Commit be3e0d9

Browse files
committed
Use extend module; Attempt to alias reserved js names in static codegen, see #559; Some README
1 parent 766171e commit be3e0d9

File tree

19 files changed

+206
-188
lines changed

19 files changed

+206
-188
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
*.log
22
npm-debug.*
33
node_modules/
4-
types/types.d.ts
54
docs/

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*.log
44
npm-debug.*
55
node_modules/
6-
types/types.d.ts
76
docs/
87
examples/
98
src/util/aspromise/
109
src/util/base64/
1110
src/util/codegen/
1211
src/util/eventemitter/
12+
src/util/extend/
1313
src/util/fetch/
1414
src/util/fs/
15+
src/util/path/
1516
src/util/pool/
1617
src/util/utf8/

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ import * as protobuf from "protobufjs";
215215
...
216216
```
217217

218+
See also: [Generating your own TypeScript definitions](https://github.com/dcodeIO/protobuf.js#generating-typescript-definitions-from-static-modules)
219+
218220
Module Structure
219221
----------------
220222
The library exports a flat `protobuf` namespace including but not restricted to the following members, ordered by category:
@@ -352,7 +354,7 @@ Likewise, the `pbts` command line utility can be used to generate TypeScript def
352354
```
353355
Generates TypeScript definitions from annotated JavaScript files.
354356
355-
-n, --name Specifies the module name.
357+
-n, --name Wraps everything in a module of the specified name.
356358
357359
-o, --out Saves to a file instead of writing to stdout.
358360
@@ -361,11 +363,11 @@ usage: pbts [options] file1.js file2.js ...
361363

362364
### Descriptors vs. static modules
363365

364-
While .proto and JSON files require the full library (about 18kb gzipped, all features including reflection, parser and utility), pretty much all code but the relatively short descriptors is shared.
366+
While .proto and JSON files require the full library (about 18kb gzipped), pretty much all code but the relatively short descriptors is shared and all features including reflection and the parser are available.
365367

366-
Static code, on the other hand, requires just the minimal runtime (about 5.5kb gzipped, i.e. no reflection features), but generates additional, albeit editable and customizable, source code.
368+
Static code, on the other hand, requires just the minimal runtime (about 5.5kb gzipped), but generates additional, albeit editable, source code without any reflection features.
367369

368-
When `new Function(...)` is supported (and it usually is), there is no difference performance-wise as the code generated statically is the same as generated at runtime.
370+
When `new Function(...)` is supported (and it usually is), there is no difference performance-wise as the code generated statically is pretty much the same as generated at runtime.
369371

370372
Building
371373
--------

bench/suite.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
"use strict";
2+
module.exports = newSuite;
3+
14
var benchmark = require("benchmark"),
25
chalk = require("chalk");
36

47
var padSize = 27;
58

6-
module.exports = function newSuite(name) {
9+
function newSuite(name) {
710
var benches = [];
811
return new benchmark.Suite(name)
912
.on("add", function(event) {
@@ -19,17 +22,19 @@ module.exports = function newSuite(name) {
1922
console.log(String(event.target));
2023
})
2124
.on("complete", function(event) {
22-
var fastest = this.filter('fastest'),
23-
slowest = this.filter('slowest');
24-
var fastestHz = getHz(fastest[0]);
25-
console.log("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest"));
26-
benches.forEach(function(bench) {
27-
if (fastest.indexOf(bench) > -1)
28-
return;
29-
var hz = hz = getHz(bench);
30-
var percent = (1 - (hz / fastestHz)) * 100;
31-
console.log(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1)+'% slower'));
32-
});
25+
if (benches.length > 1) {
26+
var fastest = this.filter('fastest'),
27+
slowest = this.filter('slowest');
28+
var fastestHz = getHz(fastest[0]);
29+
console.log("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest"));
30+
benches.forEach(function(bench) {
31+
if (fastest.indexOf(bench) === 0)
32+
return;
33+
var hz = hz = getHz(bench);
34+
var percent = (1 - (hz / fastestHz)) * 100;
35+
console.log(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1)+'% slower'));
36+
});
37+
}
3338
console.log();
3439
});
3540
}

cli/pbts.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var minimist = util.require("minimist", pkg.devDependencies.minimist),
88
chalk = util.require("chalk", pkg.devDependencies.chalk),
99
glob = util.require("glob", pkg.devDependencies.glob);
1010

11-
var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc),
12-
tsdjsdoc = util.require("tsd-jsdoc/package.json", pkg.devDependencies['tsd-jsdoc']);
11+
var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc);
1312

1413
var protobuf = require("..");
1514

@@ -30,7 +29,7 @@ exports.main = function(args) {
3029
"",
3130
"Generates TypeScript definitions from annotated JavaScript files.",
3231
"",
33-
" -n, --name Specifies the module name.",
32+
" -n, --name Wraps everything in a module of the specified name.",
3433
"",
3534
" -o, --out Saves to a file instead of writing to stdout.",
3635
"",

cli/targets/static.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ function pushComment(lines) {
7474
push(" */");
7575
}
7676

77+
var reservedRe = /^do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof$/;
78+
7779
function name(name) {
7880
if (!name)
7981
return "$root";
80-
return name;
82+
return reservedRe.test(name) ? name + "_" : name;
8183
}
8284

8385
function buildNamespace(ref, ns) {

0 commit comments

Comments
 (0)