Skip to content

Commit

Permalink
Other: Added utility to enable/disable debugging extensions to experi…
Browse files Browse the repository at this point in the history
…mental debug build
  • Loading branch information
dcodeIO committed Jan 20, 2017
1 parent fdb1a72 commit 33da148
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/index-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@
"use strict";
var protobuf = module.exports = require("./index");

// Count number of calls to any generated function
protobuf.util.codegen = (function(codegen) { return function codegen_debug() {
var codegen = protobuf.util.codegen;

// Counts number of calls to any generated function
function codegen_debug() {
codegen_debug.supported = codegen.supported;
codegen_debug.verbose = codegen.verbose;
var gen = codegen.apply(null, Array.prototype.slice.call(arguments));
gen.str = (function(str) { return function str_debug() {
return str.apply(null, Array.prototype.slice.call(arguments)).replace(/function ([^(]+)\(([^)]*)\) {/g, "function $1($2) {\n\t$1.calls=($1.calls|0)+1");
};})(gen.str);
return gen;
};})(protobuf.util.codegen);
}

/**
* Debugging utility functions. Only present in debug builds.
* @namespace
*/
var debug = protobuf.debug = {};

/**
* Enables debugging extensions.
* @returns {undefined}
*/
debug.enable = function enable() {
protobuf.util.codegen = codegen_debug;
return protobuf;
};

/**
* Disables debugging extensions.
* @returns {undefined}
*/
debug.disable = function disable() {
protobuf.util.codegen = codegen;
return protobuf;
};

/**
* Returns a list of unused types within the specified root.
* @param {NamespaceBase} ns Namespace to search
Expand All @@ -30,7 +50,7 @@ debug.unusedTypes = function unusedTypes(ns) {

/* istanbul ignore next */
if (!(ns instanceof protobuf.Namespace))
throw TypeError("ns must be a namespace");
throw TypeError("ns must be a Namespace");
/* istanbul ignore next */
if (!ns.nested)
return [];
Expand Down
3 changes: 3 additions & 0 deletions tests/other_basics-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var tape = require("tape");
var protobuf = require("../debug");

tape.test("google.protobuf.Any type", function(test) {
protobuf.debug.enable();
protobuf.load("tests/data/common.proto", function(err, root) {
if (err)
return test.fail(err.message);
Expand Down Expand Up @@ -93,6 +94,8 @@ tape.test("google.protobuf.Any type", function(test) {
".google.protobuf.ListValue",
".google.protobuf.Timestamp"
], "should recognize unused types (all but .google.protobuf.Any)");

protobuf.debug.disable();
test.end();
});

Expand Down

0 comments on commit 33da148

Please sign in to comment.