Skip to content

Commit 2d81864

Browse files
committed
Other: Root#resolvePath skips files when returning null, see #368
1 parent de89035 commit 2d81864

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/root.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Root.fromJSON = function fromJSON(json, root) {
5656
* @function
5757
* @param {string} origin The file name of the importing file
5858
* @param {string} target The file name being imported
59-
* @returns {string} Resolved path to `target`
59+
* @returns {?string} Resolved path to `target` or `null` to skip the file
6060
*/
6161
Root.prototype.resolvePath = util.path.resolve;
6262

@@ -104,13 +104,16 @@ Root.prototype.load = function load(filename, options, callback) {
104104
else {
105105
parse.filename = filename;
106106
var parsed = parse(source, self, options),
107+
resolved,
107108
i = 0;
108109
if (parsed.imports)
109110
for (; i < parsed.imports.length; ++i)
110-
fetch(self.resolvePath(filename, parsed.imports[i]));
111+
if (resolved = self.resolvePath(filename, parsed.imports[i]))
112+
fetch(resolved);
111113
if (parsed.weakImports)
112114
for (i = 0; i < parsed.weakImports.length; ++i)
113-
fetch(self.resolvePath(filename, parsed.weakImports[i]), true);
115+
if (resolved = self.resolvePath(filename, parsed.weakImports[i]))
116+
fetch(resolved, true);
114117
}
115118
} catch (err) {
116119
finish(err);
@@ -184,8 +187,9 @@ Root.prototype.load = function load(filename, options, callback) {
184187
// references anymore, so we can load everything in parallel
185188
if (util.isString(filename))
186189
filename = [ filename ];
187-
for (var i = 0; i < filename.length; ++i)
188-
fetch(self.resolvePath("", filename[i]));
190+
for (var i = 0, resolved; i < filename.length; ++i)
191+
if (resolved = self.resolvePath("", filename[i]))
192+
fetch(resolved);
189193

190194
if (sync)
191195
return self;

tests/api_root.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tape.test("reflected roots", function(test) {
2020
if (typeof Promise !== "undefined")
2121
test.ok(root.load("tests/data/common.proto") instanceof Promise, "should return a Promise when loading without a callback");
2222

23-
var count = 0, total = 4;
23+
var count = 0, total = 6;
2424

2525
root = new Root();
2626
root.load("tests/data/common.json", function(err, root) {
@@ -50,4 +50,28 @@ tape.test("reflected roots", function(test) {
5050
if (++count === total)
5151
test.end();
5252
});
53+
54+
var root3 = new Root();
55+
root3.resolvePath = function() {
56+
return null;
57+
};
58+
root3.load("tests/data/NOTFOUND2", function(err, root) {
59+
test.notOk(err, "should skip files without error when resolvePath returns null");
60+
test.equal(root, root3, "should still return itself");
61+
if (++count === total)
62+
test.end();
63+
});
64+
65+
var root4 = new Root();
66+
root4.resolvePath = function(origin, target) {
67+
if (/weak\.proto$/.test(target))
68+
return protobuf.util.path.resolve(origin, target);
69+
return null;
70+
};
71+
root4.load("tests/data/weak.proto", function(err, root) {
72+
test.notOk(err, "should skip files without error when resolvePath returns null");
73+
test.equal(root, root4, "should still return itself");
74+
if (++count === total)
75+
test.end();
76+
});
5377
});

0 commit comments

Comments
 (0)