Skip to content

Commit

Permalink
Other: Minor optimizations to dependencies, build process and tsd
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Feb 3, 2017
1 parent 33d14c9 commit d246024
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 64 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"browser": true
},
"globals": {
"ArrayBuffer": true,
"Uint8Array": true,
"Float32Array": true,
"Float64Array": true,
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ branches:
matrix:
include:
- node_js: 6
script: set -e; if [ -n "$SAUCE_USERNAME" ]; then travis_wait npm run zuul; sleep 3; fi
script: set -e; if [ -n "$SAUCE_USERNAME" ]; then npm install [email protected] [email protected]; travis_wait npm run zuul; sleep 3; fi
- node_js: 6
script: npm run coverage-ci
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ The `pbjs` utility is also capable of generating static code (hint: works with j
$> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
```

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).
will generate static code for definitions within `file1.proto` and `file2.proto` to a CommonJS module `compiled.js`.

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

Expand Down Expand Up @@ -465,9 +465,9 @@ $> pbjs -t static-module file1.proto file2.proto | pbts -o bundle.d.ts -

### On reflection vs. static code

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.
While using .proto files directly requires the full library respectively pure reflection/JSON the light library, pretty much all code but the relatively short descriptors is shared.

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

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.

Expand Down
4 changes: 2 additions & 2 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = static_target;
var protobuf = require("../.."),
cliUtil = require("../util"),
UglifyJS = require("uglify-js"),
esprima = require("esprima"),
espree = require("espree"),
escodegen = require("escodegen"),
estraverse = require("estraverse");

Expand Down Expand Up @@ -161,7 +161,7 @@ function beautifyCode(code) {
}
}).code;
// Properly beautify
var ast = esprima.parse(code);
var ast = espree.parse(code);
estraverse.replace(ast, {
enter: function(node, parent) {
// rename short vars
Expand Down
5 changes: 3 additions & 2 deletions dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/light/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/minimal/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/minimal/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/minimal/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/minimal/protobuf.min.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function defineTask(name, entry, target) {
entry : entry,
target : target
}));
gulp.task(name + "-minify" , bundle.bind(this, {
gulp.task(name + "-minify" , [ name + "-bundle" ], bundle.bind(this, {
entry : entry,
target : target,
compress : true
Expand Down
3 changes: 0 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,6 @@ export class Type extends NamespaceBase {
* @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false`
* @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false`
*/

interface ConversionOptions {
longs?: any;
enums?: any;
Expand Down Expand Up @@ -1997,7 +1996,6 @@ export namespace types {
* @property {number} high High bits
* @property {boolean} unsigned Whether unsigned or not
*/

interface Long {
low: number;
high: number;
Expand Down Expand Up @@ -2760,7 +2758,6 @@ type FetchCallback = (error: Error, contents?: string) => void;
* @property {boolean} [binary=false] Whether expecting a binary response
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
*/

interface FetchOptions {
binary?: boolean;
xhr?: boolean;
Expand Down
49 changes: 26 additions & 23 deletions lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ exports.publish = function publish(taffy, opts) {

// query overrides options
if (options.query)
for (var i in options.query)
if (options.query.hasOwnProperty(i))
switch (options[i] = options.query[i]) {
Object.keys(options.query).forEach(function(key) {
if (key !== "query")
switch (options[key] = options.query[key]) {
case "true":
options[i] = true;
options[key] = true;
break;
case "false":
options[i] = false;
options[key] = false;
break;
case "null":
options[i] = null;
options[key] = null;
break;
}
});

// remove undocumented
taffy({ undocumented: true }).remove();
Expand Down Expand Up @@ -105,7 +106,7 @@ exports.publish = function publish(taffy, opts) {
seen = options = {};
queuedInterfaces = [];
}
}
};

//
// Utility
Expand All @@ -123,7 +124,7 @@ function write() {
firstLine = false;
}

// writes one or multiple strings, followed by a new line
// writes zero or multiple strings, followed by a new line
function writeln() {
var s = Array.prototype.slice.call(arguments).join("");
if (s.length)
Expand Down Expand Up @@ -156,12 +157,15 @@ function writeComment(comment, otherwiseNewline) {
// recursively replaces all occurencies of re's match
function replaceRecursive(name, re, fn) {
var found;

function replacer() {
found = true;
return fn.apply(null, arguments);
}

do {
found = false;
name = name.replace(re, function() {
found = true;
return fn.apply(null, arguments);
});
name = name.replace(re, replacer);
} while (found);
return name;
}
Expand All @@ -173,7 +177,7 @@ function isClassLike(element) {

// tests if an element is considered to be an interface
function isInterface(element) {
return element && (element.kind === "interface" || (getTypeOf(element) === 'Object' && element.properties && element.properties.length));
return element && (element.kind === "interface" || getTypeOf(element) === "Object" && element.properties && element.properties.length);
}

// tests if an element is considered to be a namespace
Expand Down Expand Up @@ -219,11 +223,11 @@ function getTypeOf(element) {

// Replace Object.<string,number> with { [k: string]: number }
name = replaceRecursive(name, /\bObject\.?<([^,]*), *([^>]*)>/gi, function($0, $1, $2) {
return '{ [k: ' + $1 + ']: ' + $2 + ' }';
return "{ [k: " + $1 + "]: " + $2 + " }";
});

// Replace functions (there are no signatures) with () => any
name = name.replace(/\bfunction(?:\(\))?([^\w]|$)/gi, '() => any');
name = name.replace(/\bfunction(?:\(\))?([^\w]|$)/gi, "() => any");

return name;
}
Expand All @@ -238,7 +242,7 @@ function begin(element, is_interface) {

// writes the function signature describing element
function writeFunctionSignature(element, isConstructor, isTypeDef) {
write('(');
write("(");

var params = {};

Expand All @@ -258,7 +262,7 @@ function writeFunctionSignature(element, isConstructor, isTypeDef) {
type: getTypeOf(param),
variable: param.variable === true,
optional: param.optional === true,
defaultValue: param.defaultvalue // TODO
defaultValue: param.defaultvalue // Not used yet (TODO)
};
else // Property syntax (TODO)
params[path[0]].type = "{ [k: string]: any }";
Expand Down Expand Up @@ -292,7 +296,6 @@ function writeFunctionSignature(element, isConstructor, isTypeDef) {

// writes (a typedef as) an interface
function writeInterface(element) {
writeln();
writeln("interface ", element.name, " {");
++indent;
element.properties.forEach(function(property) {
Expand Down Expand Up @@ -401,23 +404,23 @@ function handleClass(element, parent) {
handleFunction(element, parent, true);

// class-compatible members
var inner = [];
var incompatible = [];
getChildrenOf(element).forEach(function(child) {
if (!handleElement(child, element, true))
inner.push(child);
incompatible.push(child);
});

--indent;
writeln("}");

// class-incompatible members
if (inner.length) {
if (incompatible.length) {
writeln();
if (element.scope === "global" && !options.module)
write("export ");
writeln("namespace ", element.name, " {");
++indent;
inner.forEach(function(child) {
incompatible.forEach(function(child) {
handleElement(child, element);
});
--indent;
Expand Down Expand Up @@ -512,4 +515,4 @@ function handleTypeDef(element, parent) {
write(getTypeOf(element));
writeln(";");
}
}
}
18 changes: 7 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.6.3",
"version": "6.6.4",
"versionScheme": "~",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <[email protected]>",
Expand All @@ -13,11 +13,9 @@
"homepage": "http://dcode.io/protobuf.js",
"keywords": [
"protobuf",
"protocol",
"buffers",
"protocol buffers",
"protocol-buffers",
"serialization",
"encoding"
"typescript"
],
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -69,7 +67,7 @@
"coveralls": "^2.11.15",
"escodegen": "^1.8.1",
"eslint": "^3.14.1",
"esprima": "^3.1.3",
"espree": "^3.1.3",
"estraverse": "^4.2.0",
"gh-pages": "^0.12.0",
"git-raw-commits": "^1.1.2",
Expand All @@ -89,13 +87,11 @@
"tap-spec": "^4.1.1",
"tape": "^4.6.3",
"tmp": "0.0.31",
"typescript": "^2.1.5",
"typescript": "^2.2.0",
"uglify-js": "^2.7.5",
"vinyl-buffer": "^1.0.0",
"vinyl-fs": "^2.4.4",
"vinyl-source-stream": "^1.1.0",
"zuul": "^3.11.1",
"zuul-ngrok": "^4.0.0"
"vinyl-source-stream": "^1.1.0"
},
"cliDependencies": [
"semver",
Expand All @@ -105,7 +101,7 @@
"minimist",
"tmp",
"uglify-js",
"esprima",
"espree",
"escodegen",
"estraverse"
]
Expand Down
1 change: 1 addition & 0 deletions src/util/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ util.toJSONOptions = {

util._configure = function() {
var Buffer = util.Buffer;
/* istanbul ignore if */
if (!Buffer) {
util._Buffer_from = util._Buffer_allocUnsafe = null;
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/api_reader-writer-reuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tape.test("reusing", function(test) {
}, writer);

B.encodeDelimited({
b: 'a'
b: "a"
}, writer);

var buffer = writer.finish();
Expand All @@ -45,7 +45,7 @@ tape.test("reusing", function(test) {
test.deepEqual(a, { a: 1 }, "read back the first message");

var b = B.decodeDelimited(reader);
test.deepEqual(b, { b: 'a' }, "read back the second message");
test.deepEqual(b, { b: "a" }, "read back the second message");

test.equal(reader.pos, reader.len, "consume the reader");

Expand Down

0 comments on commit d246024

Please sign in to comment.