Skip to content

Commit

Permalink
Read hosts-file async (#277)
Browse files Browse the repository at this point in the history
Since discover-providers are using a callback to return the host list, the hosts-file could be able to read the file asynchronously.
  • Loading branch information
mennopruijssers committed May 26, 2016
1 parent 6ac2c2a commit 19de198
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions discover-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ function createJsonFileDiscoverProvider(hostsFile) {
var fs = require('fs');

return function jsonFileProvider(callback) {
if (!fs.existsSync(hostsFile)) {
callback(new Error('bootstrap hosts file does not exist', {file: hostsFile}));
return;
}
fs.readFile(hostsFile, function onFileRead(err, data) {
if (err) {
callback(err);
return;
}

var hosts;
try {
hosts = JSON.parse(fs.readFileSync(hostsFile).toString());
} catch (e) {
callback(e);
var hosts;
try {
hosts = JSON.parse(data.toString());
} catch (e) {
callback(e);
return;
}
callback(null, hosts);
return;
}

return callback(null, hosts);
});
};
}

Expand Down

0 comments on commit 19de198

Please sign in to comment.