Я пытаюсь хэшировать каждый файл в каталоге, а затем распечатывать вывод в файл .txt на nodeJS. Но проблема, с которой я сталкиваюсь, заключается в том, что хеши печатаются не в тех местах файла .txt.
Чтобы получить доступ ко всем файлам в каталоге, я использовал Klaw API https://dustinpfister.github.io/2018/07/19/nodejs-klaw/?. Я ссылался на NodeJS хеш-файлы рекурсивно в каталоге для рекурсивного хеширования каждого файла. Я подозревал, что проблема была в том, что console.log не стандартизирован, особенно при вызове функции console.log () async или sync? ?. В Интернете я нашел несколько решений, например, setTimeout (но я не знаю, как применить его в случае, когда функция имеет функцию «бросить и поймать»), я буду признателен, если будет оказана помощь!
Это мой код на NodeJS
let klaw = require('klaw'),
path = require('path'),
// the dir to walk
dir_walk = process.argv[2] || process.cwd();
var crypto = require('crypto');
var fs = require('fs');
// walking dir_walk with the following options
klaw(dir_walk, {
// default to full recursion, if now depth is given
depthLimit: process.argv[3] || -1
}).on('data', function (item) {
if (!item.stats.isDirectory()) {
//hash function
generateHash(item.path, function (e, hash) {
if (e) done(e);
console.log('Hash : ' + hash);
});
console.log('\nType : File');
console.log('Name : ' + path.basename(item.path));
console.log('Path: ' + item.path); // the full absolute path of of the item
console.log('Mtime: ' + item.stats.mtime);
console.log('Ctime: ' + item.stats.ctime);// the stats of the item
}
else{
console.log('\nType : Folder');
console.log('Name : ' + path.basename(item.path));
console.log('Path: ' + item.path);
console.log('Mtime: ' + item.stats.mtime);
console.log('Ctime: ' + item.stats.ctime);
}
})
//Function for generating hashes
function generateHash (filename, callback) {
var algorithm = 'sha256';
var hashTable = new Array();
var hash = crypto.createHash(algorithm);
var fileStream = fs.ReadStream(filename);
fileStream.on('data', function(data) {
hash.update(data);
});
fileStream.on('end', function() {
var digest = hash.digest('hex');
callback(null, digest);
});
}
Вывод для моего каталога
Type : Folder
Name : TESTING
Path: /Users/*/Documents/TESTING
Mtime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)
Type : File
Name : .DS_Store
Path: /Users/*/Documents/TESTING/.DS_Store
Mtime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Type : File
Name : basic.js
Path: /Users/*/Documents/TESTING/basic.js
Mtime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Type : File
Name : basicv1.js
Path: /Users/*/Documents/TESTING/basicv1.js
Mtime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Hash : 1ebe514c6032f1bcb6c50a0e07fde487d8a38ca2a2a67948198bf48bc8877951
Hash : 56d7fadb2f8d37eda3986c73726fecb4469e84ac1fbe0b1108f2d141bb8a509f
Type : Folder
Name : folder
Path: /Users/*/Documents/TESTING/folder
Mtime: Sat Jun 08 2019 15:36:35 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:43:20 GMT+0800 (Singapore Standard Time)
Hash : 038789863d605a4f6219bbd0c087a32876e633ff4e678d1744d423e9abca1260
Но фактический результат, который я должен ожидать, равен
Type : Folder
Name : TESTING
Path: /Users/*/Documents/TESTING
Mtime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:29 GMT+0800 (Singapore Standard Time)
Type : File
Name : .DS_Store
Path: /Users/*/Documents/TESTING/.DS_Store
Mtime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:44:03 GMT+0800 (Singapore Standard Time)
Hash : 1ebe514c6032f1bcb6c50a0e07fde487d8a38ca2a2a67948198bf48bc8877951
Type : File
Name : basic.js
Path: /Users/*/Documents/TESTING/basic.js
Mtime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 16:09:08 GMT+0800 (Singapore Standard Time)
Hash : 56d7fadb2f8d37eda3986c73726fecb4469e84ac1fbe0b1108f2d141bb8a509f
Type : File
Name : basicv1.js
Path: /Users/*/Documents/TESTING/basicv1.js
Mtime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 17:37:02 GMT+0800 (Singapore Standard Time)
Hash : 038789863d605a4f6219bbd0c087a32876e633ff4e678d1744d423e9abca1260
Type : Folder
Name : folder
Path: /Users/*/Documents/TESTING/folder
Mtime: Sat Jun 08 2019 15:36:35 GMT+0800 (Singapore Standard Time)
Ctime: Tue Jun 11 2019 15:43:20 GMT+0800 (Singapore Standard Time)