From f9259a2533ee16f772f3d8f1f01fd6a36d19388c Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Mon, 16 Nov 2020 16:54:08 +0100 Subject: [PATCH] [build][s]rebuild package --- dist/node/file-base.js | 44 ++++++++++++++++++++++++++++++------------ dist/node/index.js | 6 ++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/dist/node/file-base.js b/dist/node/file-base.js index 19f1296..10772e7 100644 --- a/dist/node/file-base.js +++ b/dist/node/file-base.js @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); +exports.computeHash = computeHash; exports.File = void 0; var _data = require("./data"); @@ -26,6 +27,15 @@ const { } = require('stream'); class File { + constructor(descriptor, { + basePath + } = {}) { + this._descriptor = descriptor; + this._basePath = basePath; + this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING; + this._computedHashes = {}; + } + static load(pathOrDescriptor, { basePath, format @@ -37,14 +47,6 @@ class File { }); } - constructor(descriptor, { - basePath - } = {}) { - this._descriptor = descriptor; - this._basePath = basePath; - this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING; - } - get descriptor() { return this._descriptor; } @@ -99,8 +101,22 @@ class File { })(); } - async hash(hashType = 'md5', progress) { - return _computeHash(await this.stream(), this.size, hashType, progress); + async hash(hashType = 'md5', progress, cache = true) { + if (cache && hashType in this._computedHashes) { + if (typeof progress === 'function') { + progress(100); + } + + return this._computedHashes[hashType]; + } else { + let hash = await computeHash(await this.stream(), this.size, hashType, progress); + + if (cache && this != null) { + this._computedHashes[hashType] = hash; + } + + return hash; + } } async hashSha256(progress) { @@ -175,7 +191,7 @@ class File { exports.File = File; -function _computeHash(fileStream, fileSize, algorithm, progress) { +function computeHash(fileStream, fileSize, algorithm, progress, encoding = 'hex') { return new Promise((resolve, reject) => { let hash = _crypto.default.createHash(algorithm); @@ -183,6 +199,10 @@ function _computeHash(fileStream, fileSize, algorithm, progress) { let totalChunkSize = 0; let chunkCount = 0; + if (!['hex', 'latin1', 'binary', 'base64'].includes(encoding)) { + throw new Error(`Invalid encoding value: ${encoding}; Expecting 'hex', 'latin1', 'binary' or 'base64'`); + } + const _reportProgress = new Transform({ transform(chunk, encoding, callback) { if (chunkCount % 20 == 0) { @@ -206,7 +226,7 @@ function _computeHash(fileStream, fileSize, algorithm, progress) { chunkCount += 1; hash.update(chunk); }).on('end', function () { - hash = hash.digest('hex'); + hash = hash.digest(encoding); if (typeof progress === 'function') { progress(100); diff --git a/dist/node/index.js b/dist/node/index.js index 70172ae..fabd939 100644 --- a/dist/node/index.js +++ b/dist/node/index.js @@ -39,6 +39,12 @@ Object.defineProperty(exports, "File", { return _fileBase.File; } }); +Object.defineProperty(exports, "computeHash", { + enumerable: true, + get: function () { + return _fileBase.computeHash; + } +}); Object.defineProperty(exports, "FileInterface", { enumerable: true, get: function () {