diff --git a/README.md b/README.md index 7897f24e1..d191910ad 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -protobuf.js -=========== +#

+ [![travis][travis-image]][travis-url] [![david][david-image]][david-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url] **Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)). diff --git a/bench/alloc.js b/bench/alloc.js index 18db244b3..d55c7f7c8 100644 --- a/bench/alloc.js +++ b/bench/alloc.js @@ -1,10 +1,12 @@ -var protobuf = require(".."), - newSuite = require("./suite"); +/*eslint-disable no-new*//*global ArrayBuffer*/ +"use strict"; -var pool = require("../src/util/pool"), - poolAlloc = pool(function(size) { - return new Uint8Array(size); - }, Uint8Array.prototype.subarray); +var newSuite = require("./suite"), + pool = require("../src/util/pool"); + +var poolAlloc = pool(function(size) { + return new Uint8Array(size); +}, Uint8Array.prototype.subarray); [ 64, @@ -15,19 +17,19 @@ var pool = require("../src/util/pool"), newSuite("buffer[" + size + "]") .add("new Uint8Array", function() { - var buf = new Uint8Array(size); + new Uint8Array(size); }) .add("Buffer.alloc", function() { - var buf = Buffer.alloc(size); + Buffer.alloc(size); }) .add("poolAlloc", function() { - var buf = poolAlloc(size); + poolAlloc(size); }) .add("Buffer.allocUnsafe", function() { - var buf = Buffer.allocUnsafe(size); + Buffer.allocUnsafe(size); }) .add("new Buffer", function() { - var buf = new Buffer(size); + new Buffer(size); }) .run(); @@ -35,10 +37,10 @@ var pool = require("../src/util/pool"), newSuite("wrap[" + size + "]") .add("new Uint8Array(ArrayBuffer)", function() { - var buf = new Uint8Array(ab); + new Uint8Array(ab); }) .add("Buffer.from(ArrayBuffer)", function() { - var buf = Buffer.from(ab); + Buffer.from(ab); }) .run(); diff --git a/bench/index.js b/bench/index.js index 847e64610..35159ce64 100644 --- a/bench/index.js +++ b/bench/index.js @@ -1,3 +1,5 @@ +"use strict"; + var protobuf = require(".."), newSuite = require("./suite"), data = require("./bench.json"); @@ -19,21 +21,22 @@ var protobuf = require(".."), // // To experience the impact by yourself, increase string lengths within bench.json. -var root = protobuf.loadSync(require.resolve("./bench.proto")); -var Test = root.resolveAll().lookup("Test"); +var root = protobuf.loadSync(require.resolve("./bench.proto")), + Test = root.resolveAll().lookup("Test"); // protobuf.util.codegen.verbose = true; var buf = Test.encode(data).finish(); // warm up -for (var i = 0; i < 500000; ++i) +var i; +for (i = 0; i < 500000; ++i) Test.encode(data).finish(); -for (var i = 0; i < 1000000; ++i) +for (i = 0; i < 1000000; ++i) Test.decode(buf); -for (var i = 0; i < 500000; ++i) +for (i = 0; i < 500000; ++i) Test.verify(data); -console.log(""); +process.stdout.write("\n"); if (!Buffer.from) Buffer.from = function from(str, enc) { @@ -92,10 +95,13 @@ setTimeout(function() { var dataMessage = Test.from(data); var dataJson = dataMessage.asJSON(); - newSuite("converting") + newSuite("converting from JSON") .add("Message.from", function() { Test.from(dataJson); }) + .run(); + + newSuite("converting to JSON") .add("Message#asJSON", function() { dataMessage.asJSON(); }) diff --git a/bench/prof.js b/bench/prof.js index a2402cf1b..e389db75a 100644 --- a/bench/prof.js +++ b/bench/prof.js @@ -1,3 +1,5 @@ +"use strict"; + var fs = require("fs"), path = require("path"); @@ -5,36 +7,36 @@ var fs = require("fs"), var commands = ["encode", "decode", "encode-browser", "decode-browser"]; if (commands.indexOf(process.argv[2]) < 0) { // 0: node, 1: prof.js - console.error("usage: " + path.basename(process.argv[1]) + " <" + commands.join('|') + "> [iterations=10000000]"); - process.exit(0); + process.stderr.write("usage: " + path.basename(process.argv[1]) + " <" + commands.join("|") + "> [iterations=10000000]\n"); + return; } // Spin up a node process with profiling enabled and process the generated log if (process.execArgv.indexOf("--prof") < 0) { - console.log("cleaning up old logs ..."); + process.stdout.write("cleaning up old logs ...\n"); var child_process = require("child_process"); - var logRe = /^isolate\-[0-9A-F]+\-v8\.log$/; + var logRe = /^isolate-[0-9A-F]+-v8\.log$/; fs.readdirSync(process.cwd()).forEach(function readdirSync_it(file) { if (logRe.test(file)) fs.unlink(file); }); - console.log("generating profile (may take a while) ..."); - var child = child_process.execSync("node --prof --trace-deopt " + process.execArgv.join(" ") + " " + process.argv.slice(1).join(' '), { + process.stdout.write("generating profile (may take a while) ...\n"); + child_process.execSync("node --prof --trace-deopt " + process.execArgv.join(" ") + " " + process.argv.slice(1).join(' '), { cwd: process.cwd(), - stdio: 'inherit' + stdio: "inherit" }); - console.log("processing profile ..."); + process.stdout.write("processing profile ...\n"); fs.readdirSync(process.cwd()).forEach(function readdirSync_it(file) { if (logRe.test(file)) { child_process.execSync("node --prof-process " + file, { cwd: process.cwd(), - stdio: 'inherit' + stdio: "inherit" }); // fs.unlink(file); } }); - console.log("done."); - process.exit(0); + process.stdout.write("done.\n"); + return; } // Actual profiling code @@ -59,7 +61,7 @@ if (process.argv.indexOf("--alt") < 0) { if (process.argv.length > 3 && /^\d+$/.test(process.argv[3])) count = parseInt(process.argv[3], 10); -console.log(" x " + count); +process.stdout.write(" x " + count + "\n"); function setupBrowser() { protobuf.Writer.create = function create_browser() { return new protobuf.Writer(); }; @@ -69,16 +71,17 @@ function setupBrowser() { switch (process.argv[2]) { case "encode-browser": setupBrowser(); + // eslint-disable-line no-fallthrough case "encode": for (var i = 0; i < count; ++i) Test.encode(data).finish(); break; case "decode-browser": setupBrowser(); + // eslint-disable-line no-fallthrough case "decode": var buf = Test.encode(data).finish(); - for (var i = 0; i < count; ++i) + for (var j = 0; j < count; ++j) Test.decode(buf); break; } -process.exit(0); diff --git a/bench/read.js b/bench/read.js index 6ed3be5c4..fc4bf8b3e 100644 --- a/bench/read.js +++ b/bench/read.js @@ -1,11 +1,10 @@ -var protobuf = require(".."), - newSuite = require("./suite"), +"use strict"; +var newSuite = require("./suite"), ieee754 = require("../lib/ieee754"); // This benchmark compares raw data type performance of Uint8Array and Buffer. var data = [ 0xCD, 0xCC, 0xCC, 0x3D ]; // ~0.10000000149011612 LE - var array = new Uint8Array(data); var buffer = Buffer.from(data); @@ -42,10 +41,9 @@ newSuite("float") }) .run(); -var data = [ 0x9A, 0x99, 0x99, 0x99, 0x99, 0x99, 0xB9, 0x3F ]; // 0.1 LE - -var array = new Uint8Array(data); -var buffer = Buffer.from(data); +data = [ 0x9A, 0x99, 0x99, 0x99, 0x99, 0x99, 0xB9, 0x3F ]; // 0.1 LE +array = new Uint8Array(data); +buffer = Buffer.from(data); // raw double read speed newSuite("double") @@ -103,6 +101,7 @@ function readString(bytes) { } return String.fromCharCode.apply(String, out.slice(0, c)); } + return ""; } // raw string read speed diff --git a/bench/suite.js b/bench/suite.js index 728fd9bd3..f6bdeabc1 100644 --- a/bench/suite.js +++ b/bench/suite.js @@ -13,29 +13,25 @@ function newSuite(name) { benches.push(event.target); }) .on("start", function() { - console.log("benchmarking " + name + " performance ...\n"); - }) - .on("error", function(err) { - console.log("ERROR:", err); + process.stdout.write("benchmarking " + name + " performance ...\n\n"); }) .on("cycle", function(event) { - console.log(String(event.target)); + process.stdout.write(String(event.target) + "\n"); }) - .on("complete", function(event) { + .on("complete", function() { 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")); + var fastest = this.filter("fastest"), // eslint-disable-line no-invalid-this + fastestHz = getHz(fastest[0]); + process.stdout.write("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest") + "\n"); 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')); + process.stdout.write(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1) + "% slower") + "\n"); }); } - console.log(); + process.stdout.write("\n"); }); } diff --git a/bench/write.js b/bench/write.js index fc31f83df..811c528c8 100644 --- a/bench/write.js +++ b/bench/write.js @@ -1,5 +1,5 @@ -var protobuf = require(".."), - newSuite = require("./suite"), +"use strict"; +var newSuite = require("./suite"), ieee754 = require("../lib/ieee754"); // This benchmark compares raw data type performance of Uint8Array and Buffer. @@ -96,8 +96,8 @@ newSuite("double") .run(); var source = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -var array = new Uint8Array(16); -var buffer = new Buffer(16); +array = new Uint8Array(16); +buffer = new Buffer(16); // raw bytes write speed newSuite("bytes") @@ -143,7 +143,7 @@ function writeString(buf, pos, val) { } } -function byteLength(val) { +/* function byteLength(val) { var strlen = val.length >>> 0; var len = 0; for (var i = 0; i < strlen; ++i) { @@ -159,10 +159,10 @@ function byteLength(val) { len += 3; } return len; -} +} */ -var array = new Uint8Array(1000); -var buffer = new Buffer(1000); +array = new Uint8Array(1000); +buffer = new Buffer(1000); [ "Lorem ipsu", diff --git a/pbjs.png b/pbjs.png new file mode 100644 index 000000000..ed67df68c Binary files /dev/null and b/pbjs.png differ diff --git a/src/parse.js b/src/parse.js index 765463a27..dd855eae6 100644 --- a/src/parse.js +++ b/src/parse.js @@ -667,5 +667,7 @@ function parse(source, root, options) { * @param {string} source Source contents * @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted. * @returns {ParserResult} Parser result + * @property {string} filename=null Currently processing file name for error reporting, if known + * @property {ParseOptions} defaults Default {@link ParseOptions} * @variation 2 */