diff --git a/src/root.js b/src/root.js index 3a2af60a3..d59d655ea 100644 --- a/src/root.js +++ b/src/root.js @@ -56,7 +56,7 @@ Root.fromJSON = function fromJSON(json, root) { * @function * @param {string} origin The file name of the importing file * @param {string} target The file name being imported - * @returns {string} Resolved path to `target` + * @returns {?string} Resolved path to `target` or `null` to skip the file */ Root.prototype.resolvePath = util.path.resolve; @@ -104,13 +104,16 @@ Root.prototype.load = function load(filename, options, callback) { else { parse.filename = filename; var parsed = parse(source, self, options), + resolved, i = 0; if (parsed.imports) for (; i < parsed.imports.length; ++i) - fetch(self.resolvePath(filename, parsed.imports[i])); + if (resolved = self.resolvePath(filename, parsed.imports[i])) + fetch(resolved); if (parsed.weakImports) for (i = 0; i < parsed.weakImports.length; ++i) - fetch(self.resolvePath(filename, parsed.weakImports[i]), true); + if (resolved = self.resolvePath(filename, parsed.weakImports[i])) + fetch(resolved, true); } } catch (err) { finish(err); @@ -184,8 +187,9 @@ Root.prototype.load = function load(filename, options, callback) { // references anymore, so we can load everything in parallel if (util.isString(filename)) filename = [ filename ]; - for (var i = 0; i < filename.length; ++i) - fetch(self.resolvePath("", filename[i])); + for (var i = 0, resolved; i < filename.length; ++i) + if (resolved = self.resolvePath("", filename[i])) + fetch(resolved); if (sync) return self; diff --git a/tests/api_root.js b/tests/api_root.js index 2d1015f76..a292b92df 100644 --- a/tests/api_root.js +++ b/tests/api_root.js @@ -20,7 +20,7 @@ tape.test("reflected roots", function(test) { if (typeof Promise !== "undefined") test.ok(root.load("tests/data/common.proto") instanceof Promise, "should return a Promise when loading without a callback"); - var count = 0, total = 4; + var count = 0, total = 6; root = new Root(); root.load("tests/data/common.json", function(err, root) { @@ -50,4 +50,28 @@ tape.test("reflected roots", function(test) { if (++count === total) test.end(); }); + + var root3 = new Root(); + root3.resolvePath = function() { + return null; + }; + root3.load("tests/data/NOTFOUND2", function(err, root) { + test.notOk(err, "should skip files without error when resolvePath returns null"); + test.equal(root, root3, "should still return itself"); + if (++count === total) + test.end(); + }); + + var root4 = new Root(); + root4.resolvePath = function(origin, target) { + if (/weak\.proto$/.test(target)) + return protobuf.util.path.resolve(origin, target); + return null; + }; + root4.load("tests/data/weak.proto", function(err, root) { + test.notOk(err, "should skip files without error when resolvePath returns null"); + test.equal(root, root4, "should still return itself"); + if (++count === total) + test.end(); + }); }); \ No newline at end of file