Skip to content

Commit f9259a2

Browse files
committed
[build][s]rebuild package
1 parent b568f9c commit f9259a2

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

dist/node/file-base.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.computeHash = computeHash;
67
exports.File = void 0;
78

89
var _data = require("./data");
@@ -26,6 +27,15 @@ const {
2627
} = require('stream');
2728

2829
class File {
30+
constructor(descriptor, {
31+
basePath
32+
} = {}) {
33+
this._descriptor = descriptor;
34+
this._basePath = basePath;
35+
this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING;
36+
this._computedHashes = {};
37+
}
38+
2939
static load(pathOrDescriptor, {
3040
basePath,
3141
format
@@ -37,14 +47,6 @@ class File {
3747
});
3848
}
3949

40-
constructor(descriptor, {
41-
basePath
42-
} = {}) {
43-
this._descriptor = descriptor;
44-
this._basePath = basePath;
45-
this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING;
46-
}
47-
4850
get descriptor() {
4951
return this._descriptor;
5052
}
@@ -99,8 +101,22 @@ class File {
99101
})();
100102
}
101103

102-
async hash(hashType = 'md5', progress) {
103-
return _computeHash(await this.stream(), this.size, hashType, progress);
104+
async hash(hashType = 'md5', progress, cache = true) {
105+
if (cache && hashType in this._computedHashes) {
106+
if (typeof progress === 'function') {
107+
progress(100);
108+
}
109+
110+
return this._computedHashes[hashType];
111+
} else {
112+
let hash = await computeHash(await this.stream(), this.size, hashType, progress);
113+
114+
if (cache && this != null) {
115+
this._computedHashes[hashType] = hash;
116+
}
117+
118+
return hash;
119+
}
104120
}
105121

106122
async hashSha256(progress) {
@@ -175,14 +191,18 @@ class File {
175191

176192
exports.File = File;
177193

178-
function _computeHash(fileStream, fileSize, algorithm, progress) {
194+
function computeHash(fileStream, fileSize, algorithm, progress, encoding = 'hex') {
179195
return new Promise((resolve, reject) => {
180196
let hash = _crypto.default.createHash(algorithm);
181197

182198
let offset = 0;
183199
let totalChunkSize = 0;
184200
let chunkCount = 0;
185201

202+
if (!['hex', 'latin1', 'binary', 'base64'].includes(encoding)) {
203+
throw new Error(`Invalid encoding value: ${encoding}; Expecting 'hex', 'latin1', 'binary' or 'base64'`);
204+
}
205+
186206
const _reportProgress = new Transform({
187207
transform(chunk, encoding, callback) {
188208
if (chunkCount % 20 == 0) {
@@ -206,7 +226,7 @@ function _computeHash(fileStream, fileSize, algorithm, progress) {
206226
chunkCount += 1;
207227
hash.update(chunk);
208228
}).on('end', function () {
209-
hash = hash.digest('hex');
229+
hash = hash.digest(encoding);
210230

211231
if (typeof progress === 'function') {
212232
progress(100);

dist/node/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ Object.defineProperty(exports, "File", {
3939
return _fileBase.File;
4040
}
4141
});
42+
Object.defineProperty(exports, "computeHash", {
43+
enumerable: true,
44+
get: function () {
45+
return _fileBase.computeHash;
46+
}
47+
});
4248
Object.defineProperty(exports, "FileInterface", {
4349
enumerable: true,
4450
get: function () {

0 commit comments

Comments
 (0)