From 13a069a798c3f25748aa5abea90cf23dd1e78df8 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 2 Aug 2019 14:30:18 +0200 Subject: [PATCH] Reimplement hashFile locally Replace the 10 MB of dependencies that is hash-files with a homebrew Promise based implementation of hashFile that uses crypto and fs. This somehow also ends up being the same amount of code as the one using hash-files. --- client.js | 54 +++++++++++++++++----------------------------------- lib/hash.js | 22 +++++++++++++++++++++ package.json | 1 - 3 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 lib/hash.js diff --git a/client.js b/client.js index adcc8cab..67b8bd04 100644 --- a/client.js +++ b/client.js @@ -8,7 +8,6 @@ const syncRequest = require('sync-request'); const request = require("request"); const ncp = require('ncp').ncp; const Rcon = require('rcon-client-fork').Rcon; -const hashFiles = require('hash-files'); const _ = require('underscore'); const deepmerge = require("deepmerge"); const getMac = require('getmac').getMac; @@ -20,6 +19,7 @@ const objectOps = require("lib/objectOps.js"); const fileOps = require("lib/fileOps"); const stringUtils = require("lib/stringUtils.js"); const configManager = require("lib/manager/configManager.js"); +const hashFile = require('lib/hash').hashFile; // argument parsing const args = require('minimist')(process.argv.slice(2)); @@ -773,42 +773,22 @@ function hashMods(instanceName, callback) { if(!callback) { throw new Error("ERROR in function hashMods NO CALLBACK"); } - function callback2(hash, modName){ - hashedMods[hashedMods.length] = { - modName: modName, - hash: hash, - }; - // check if this callback has ran once for each mod - if(hashedMods.length == instanceMods.length) { - callback(hashedMods); - } - //console.log(modname); - } - let hashedMods = []; - /*let mods = fs.readdirSync("./sharedMods/")*/ - let instanceMods = fs.readdirSync(config.instanceDirectory+"/"+instanceName+"/mods/"); - if(instanceMods.length == 0){ - callback({}); // there are no mods installed, return an empty object - } - for(let o=0;o ( + {modName: name, hash: hash} + )); } } - for(let i=0; i { + // Remove null entries from hashMod + callback(hashes.filter(entry => entry !== null)); + }); } diff --git a/lib/hash.js b/lib/hash.js new file mode 100644 index 00000000..f0bdfd7a --- /dev/null +++ b/lib/hash.js @@ -0,0 +1,22 @@ +const crypto = require('crypto'); +const fs = require('fs'); + +/** + * Returns a promise that resolves to the SHA1 hash of the file given by path + */ +exports.hashFile = function(path) { + return new Promise(function(resolve, reject) { + let hasher = crypto.createHash('sha1') + hasher.setEncoding('hex') + hasher.on('finish', function() { + resolve(hasher.read()); + }); + + let stream = fs.createReadStream(path); + stream.on('error', function(error) { + // the docs doesn't say anything about what's passed on error + reject(error); + }); + stream.pipe(hasher); + }); +} diff --git a/package.json b/package.json index fdf26872..a906cbd7 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "follow-redirects": "^1.5.0", "fs-extra": "^6.0.1", "getmac": "^1.4.0", - "hash-files": "^1.1.1", "html5-validator": "^1.0.7", "ini": "^1.3.5", "jsonwebtoken": "^8.2.1",