Skip to content

Commit

Permalink
Use extend module; Attempt to alias reserved js names in static codeg…
Browse files Browse the repository at this point in the history
…en, see #559; Some README
  • Loading branch information
dcodeIO committed Dec 15, 2016
1 parent 766171e commit be3e0d9
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 188 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,5 +1,4 @@
*.log
npm-debug.*
node_modules/
types/types.d.ts
docs/
3 changes: 2 additions & 1 deletion .npmignore
Expand Up @@ -3,14 +3,15 @@
*.log
npm-debug.*
node_modules/
types/types.d.ts
docs/
examples/
src/util/aspromise/
src/util/base64/
src/util/codegen/
src/util/eventemitter/
src/util/extend/
src/util/fetch/
src/util/fs/
src/util/path/
src/util/pool/
src/util/utf8/
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -215,6 +215,8 @@ import * as protobuf from "protobufjs";
...
```

See also: [Generating your own TypeScript definitions](https://github.com/dcodeIO/protobuf.js#generating-typescript-definitions-from-static-modules)

Module Structure
----------------
The library exports a flat `protobuf` namespace including but not restricted to the following members, ordered by category:
Expand Down Expand Up @@ -352,7 +354,7 @@ Likewise, the `pbts` command line utility can be used to generate TypeScript def
```
Generates TypeScript definitions from annotated JavaScript files.
-n, --name Specifies the module name.
-n, --name Wraps everything in a module of the specified name.
-o, --out Saves to a file instead of writing to stdout.
Expand All @@ -361,11 +363,11 @@ usage: pbts [options] file1.js file2.js ...

### Descriptors vs. static modules

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

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

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

Building
--------
Expand Down
29 changes: 17 additions & 12 deletions bench/suite.js
@@ -1,9 +1,12 @@
"use strict";
module.exports = newSuite;

var benchmark = require("benchmark"),
chalk = require("chalk");

var padSize = 27;

module.exports = function newSuite(name) {
function newSuite(name) {
var benches = [];
return new benchmark.Suite(name)
.on("add", function(event) {
Expand All @@ -19,17 +22,19 @@ module.exports = function newSuite(name) {
console.log(String(event.target));
})
.on("complete", function(event) {
var fastest = this.filter('fastest'),
slowest = this.filter('slowest');
var fastestHz = getHz(fastest[0]);
console.log("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest"));
benches.forEach(function(bench) {
if (fastest.indexOf(bench) > -1)
return;
var hz = hz = getHz(bench);
var percent = (1 - (hz / fastestHz)) * 100;
console.log(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1)+'% slower'));
});
if (benches.length > 1) {
var fastest = this.filter('fastest'),
slowest = this.filter('slowest');
var fastestHz = getHz(fastest[0]);
console.log("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest"));
benches.forEach(function(bench) {
if (fastest.indexOf(bench) === 0)
return;
var hz = hz = getHz(bench);
var percent = (1 - (hz / fastestHz)) * 100;
console.log(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1)+'% slower'));
});
}
console.log();
});
}
Expand Down
5 changes: 2 additions & 3 deletions cli/pbts.js
Expand Up @@ -8,8 +8,7 @@ var minimist = util.require("minimist", pkg.devDependencies.minimist),
chalk = util.require("chalk", pkg.devDependencies.chalk),
glob = util.require("glob", pkg.devDependencies.glob);

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

var protobuf = require("..");

Expand All @@ -30,7 +29,7 @@ exports.main = function(args) {
"",
"Generates TypeScript definitions from annotated JavaScript files.",
"",
" -n, --name Specifies the module name.",
" -n, --name Wraps everything in a module of the specified name.",
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
Expand Down
4 changes: 3 additions & 1 deletion cli/targets/static.js
Expand Up @@ -74,10 +74,12 @@ function pushComment(lines) {
push(" */");
}

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$/;

function name(name) {
if (!name)
return "$root";
return name;
return reservedRe.test(name) ? name + "_" : name;
}

function buildNamespace(ref, ns) {
Expand Down

0 comments on commit be3e0d9

Please sign in to comment.