Fix: More asynchronous operations to fix stack overflow.

This commit is contained in:
baldo 2016-06-21 18:20:36 +02:00
parent a3b5580d40
commit 23cd31e064
2 changed files with 90 additions and 70 deletions

View file

@ -24,7 +24,7 @@
}, },
"dependencies": { "dependencies": {
"async": "1.5.2", "async": "2.0.0-rc.6",
"body-parser": "1.15.1", "body-parser": "1.15.1",
"command-line-args": "3.0.0", "command-line-args": "3.0.0",
"command-line-usage": "3.0.1", "command-line-usage": "3.0.1",

View file

@ -44,11 +44,25 @@ angular.module('ffffng')
return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase()); return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase());
} }
function findFilesInPeersPath() { function findFilesInPeersPath(callback) {
var files = glob.sync(config.server.peersPath + '/*'); glob(config.server.peersPath + '/*', function (err, files) {
if (err) {
return callback(err);
}
return _.filter(files, function (file) { async.filter(files, function (file, fileCallback) {
return file[0] !== '.' && fs.lstatSync(file).isFile(); if (file[0] === '.') {
return fileCallback(null, false);
}
fs.lstat(file, function (err, stats) {
if (err) {
return fileCallback(err);
}
fileCallback(null, stats.isFile());
});
}, callback);
}); });
} }
@ -199,47 +213,53 @@ angular.module('ffffng')
} }
function parseNodeFile(file, callback) { function parseNodeFile(file, callback) {
var lines = fs.readFileSync(file).toString(); fs.readFile(file, function (err, contents) {
if (err) {
return callback(err);
}
var node = {}; var lines = contents.toString();
var nodeSecrets = {};
_.each(lines.split('\n'), function (line) { var node = {};
var entries = {}; var nodeSecrets = {};
for (var key in linePrefixes) { _.each(lines.split('\n'), function (line) {
if (linePrefixes.hasOwnProperty(key)) { var entries = {};
var prefix = linePrefixes[key];
if (line.substring(0, prefix.length) === prefix) { for (var key in linePrefixes) {
entries[key] = Strings.normalizeString(line.substr(prefix.length)); if (linePrefixes.hasOwnProperty(key)) {
break; var prefix = linePrefixes[key];
if (line.substring(0, prefix.length) === prefix) {
entries[key] = Strings.normalizeString(line.substr(prefix.length));
break;
}
} }
} }
}
if (_.isEmpty(entries) && line.substring(0, 5) === 'key "') { if (_.isEmpty(entries) && line.substring(0, 5) === 'key "') {
entries.key = Strings.normalizeString(line.split('"')[1]); entries.key = Strings.normalizeString(line.split('"')[1]);
}
_.each(entries, function (value, key) {
if (key === 'mac') {
node['mac'] = value;
node['mapId'] = _.toLower(value).replace(/:/g, '')
} else if (key === 'monitoring') {
var active = value === 'aktiv';
var pending = value === 'pending';
node.monitoring = active || pending;
node.monitoringConfirmed = active;
node.monitoringState = active ? 'active' : (pending ? 'pending' : '');
} else if (key === 'monitoringToken') {
nodeSecrets.monitoringToken = value;
} else {
node[key] = value;
} }
});
});
callback(null, node, nodeSecrets); _.each(entries, function (value, key) {
if (key === 'mac') {
node['mac'] = value;
node['mapId'] = _.toLower(value).replace(/:/g, '')
} else if (key === 'monitoring') {
var active = value === 'aktiv';
var pending = value === 'pending';
node.monitoring = active || pending;
node.monitoringConfirmed = active;
node.monitoringState = active ? 'active' : (pending ? 'pending' : '');
} else if (key === 'monitoringToken') {
nodeSecrets.monitoringToken = value;
} else {
node[key] = value;
}
});
});
callback(null, node, nodeSecrets);
});
} }
function findNodeDataByFilePattern(filter, callback) { function findNodeDataByFilePattern(filter, callback) {
@ -414,39 +434,39 @@ angular.module('ffffng')
}, },
fixNodeFilenames: function (callback) { fixNodeFilenames: function (callback) {
var files = findFilesInPeersPath(); findFilesInPeersPath(function (err, files) {
if (err) {
async.mapLimit( return callback(err);
files,
MAX_PARALLEL_NODES_PARSING,
function (file, fileCallback) {
parseNodeFile(file, function (err, node, nodeSecrets) {
if (err) {
return fileCallback(err);
}
var expectedFilename = toNodeFilename(node.token, node, nodeSecrets);
if (file !== expectedFilename) {
try {
fs.renameSync(file, expectedFilename);
} catch (e) {
return fileCallback(new Error(
'Cannot rename file ' + file + ' to ' + expectedFilename + ' => ' + e
));
}
}
fileCallback(null);
});
},
function (err) {
if (err) {
return callback(err);
}
return callback(null);
} }
);
async.mapLimit(
files,
MAX_PARALLEL_NODES_PARSING,
function (file, fileCallback) {
parseNodeFile(file, function (err, node, nodeSecrets) {
if (err) {
return fileCallback(err);
}
var expectedFilename = toNodeFilename(node.token, node, nodeSecrets);
if (file !== expectedFilename) {
return fs.rename(file, expectedFilename, function (err) {
if (err) {
return fileCallback(new Error(
'Cannot rename file ' + file + ' to ' + expectedFilename + ' => ' + err
));
}
fileCallback(null);
});
}
fileCallback(null);
});
},
callback
);
});
} }
}; };
}); });