From 079e31cf05a78bf097a3bc1736fe7e471f2b4c03 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 15 Oct 2019 23:13:56 +0200 Subject: [PATCH] Implement hashStream Add a hashStream method to lib/hash that does the same as hashFile only with a stream instead of a file path and implement tests for both of these methods. --- lib/hash.js | 23 ++++++++++++++++++++--- test/file/hash.txt | 1 + test/lib/hash.js | 24 ++++++++++++++++++++++++ test/missing.js | 1 - 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/file/hash.txt create mode 100644 test/lib/hash.js diff --git a/lib/hash.js b/lib/hash.js index f0bdfd7a..9cf9c4bf 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -1,10 +1,13 @@ const crypto = require('crypto'); const fs = require('fs'); + /** - * Returns a promise that resolves to the SHA1 hash of the file given by path + * Returns a promise that resolves to the SHA1 hash of the stream given + * + * @param stream - Node stream of the content to hash. */ -exports.hashFile = function(path) { +function hashStream(stream) { return new Promise(function(resolve, reject) { let hasher = crypto.createHash('sha1') hasher.setEncoding('hex') @@ -12,11 +15,25 @@ exports.hashFile = function(path) { 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); }); } + +/** + * Returns a promise that resolves to the SHA1 hash of the file given by path + * + * @param {string} path - Path to the file to hash. + */ +function hashFile(path) { + return hashStream(fs.createReadStream(path)); +} + +module.exports = { + hashStream, + hashFile, +} diff --git a/test/file/hash.txt b/test/file/hash.txt new file mode 100644 index 00000000..84102df4 --- /dev/null +++ b/test/file/hash.txt @@ -0,0 +1 @@ +The quick brown fox jumps over the lazy dog diff --git a/test/lib/hash.js b/test/lib/hash.js new file mode 100644 index 00000000..edcc5104 --- /dev/null +++ b/test/lib/hash.js @@ -0,0 +1,24 @@ +const assert = require('assert').strict; +const fs = require('fs'); +const path = require('path'); +const stream = require('stream'); + +const hash = require('lib/hash'); + + +describe("lib/hash", function() { + describe("hashStream()", function() { + it("should hash a stream of bytes", async function() { + let stream = fs.createReadStream(path.join("test", "file", "hash.txt")); + let result = await hash.hashStream(stream); + assert.equal(result, "be417768b5c3c5c1d9bcb2e7c119196dd76b5570"); + }); + }); + + describe("hashFile()", function() { + it("should hash a file", async function() { + let result = await hash.hashFile(path.join("test", "file", "hash.txt")); + assert.equal(result, "be417768b5c3c5c1d9bcb2e7c119196dd76b5570"); + }); + }); +}); diff --git a/test/missing.js b/test/missing.js index 3a34f174..59938262 100644 --- a/test/missing.js +++ b/test/missing.js @@ -4,7 +4,6 @@ require('lib/clusterTools'); require('lib/dole_nn_base'); require('lib/getInstances'); -require('lib/hash'); require('lib/manager/instanceManager'); require('lib/manager/modManager'); require('lib/manager/pluginManager');