From 23cd31e0640009b9386fbcfcb36997e26b1fce1c Mon Sep 17 00:00:00 2001 From: baldo Date: Tue, 21 Jun 2016 18:20:36 +0200 Subject: [PATCH] Fix: More asynchronous operations to fix stack overflow. --- package.json | 2 +- server/services/nodeService.js | 158 +++++++++++++++++++-------------- 2 files changed, 90 insertions(+), 70 deletions(-) diff --git a/package.json b/package.json index 3a59070..39e174c 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "dependencies": { - "async": "1.5.2", + "async": "2.0.0-rc.6", "body-parser": "1.15.1", "command-line-args": "3.0.0", "command-line-usage": "3.0.1", diff --git a/server/services/nodeService.js b/server/services/nodeService.js index 24285a9..e9e0107 100644 --- a/server/services/nodeService.js +++ b/server/services/nodeService.js @@ -44,11 +44,25 @@ angular.module('ffffng') return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase()); } - function findFilesInPeersPath() { - var files = glob.sync(config.server.peersPath + '/*'); + function findFilesInPeersPath(callback) { + glob(config.server.peersPath + '/*', function (err, files) { + if (err) { + return callback(err); + } - return _.filter(files, function (file) { - return file[0] !== '.' && fs.lstatSync(file).isFile(); + async.filter(files, function (file, fileCallback) { + 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) { - var lines = fs.readFileSync(file).toString(); + fs.readFile(file, function (err, contents) { + if (err) { + return callback(err); + } - var node = {}; - var nodeSecrets = {}; + var lines = contents.toString(); - _.each(lines.split('\n'), function (line) { - var entries = {}; + var node = {}; + var nodeSecrets = {}; - for (var key in linePrefixes) { - if (linePrefixes.hasOwnProperty(key)) { - var prefix = linePrefixes[key]; - if (line.substring(0, prefix.length) === prefix) { - entries[key] = Strings.normalizeString(line.substr(prefix.length)); - break; + _.each(lines.split('\n'), function (line) { + var entries = {}; + + for (var key in linePrefixes) { + if (linePrefixes.hasOwnProperty(key)) { + 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 "') { - 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; + if (_.isEmpty(entries) && line.substring(0, 5) === 'key "') { + entries.key = Strings.normalizeString(line.split('"')[1]); } - }); - }); - 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) { @@ -414,39 +434,39 @@ angular.module('ffffng') }, fixNodeFilenames: function (callback) { - var files = findFilesInPeersPath(); - - 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) { - 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); + findFilesInPeersPath(function (err, files) { + if (err) { + return callback(err); } - ); + + 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 + ); + }); } }; });