Skip to content

Commit

Permalink
[build][s]rebuild package
Browse files Browse the repository at this point in the history
  • Loading branch information
risenW committed Nov 16, 2020
1 parent b568f9c commit f9259a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
44 changes: 32 additions & 12 deletions dist/node/file-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.computeHash = computeHash;
exports.File = void 0;

var _data = require("./data");
Expand All @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -175,14 +191,18 @@ 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);

let offset = 0;
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) {
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions dist/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit f9259a2

Please sign in to comment.