folder-hash
Advanced tools
Comparing version 4.0.2 to 4.0.3
72
index.js
@@ -8,3 +8,3 @@ const crypto = require('crypto'), | ||
algo: 'sha1', // see crypto.getHashes() for options | ||
encoding: 'base64', // 'base64', 'hex' or 'binary' | ||
encoding: 'base64', // 'base64', 'base64url', 'hex' or 'binary' | ||
files: { | ||
@@ -44,5 +44,9 @@ exclude: [], | ||
symlink: debug('fhash:symlink'), | ||
queue: debug('fhash:queue'), | ||
}; | ||
function prep(fs) { | ||
let queue = []; | ||
let queueTimer = undefined; | ||
function hashElement(name, dir, options, callback) { | ||
@@ -88,15 +92,45 @@ callback = arguments[arguments.length - 1]; | ||
const name = stats.name; | ||
let promise = undefined; | ||
if (stats.isDirectory()) { | ||
return hashFolderPromise(name, dirname, options, isRootElement); | ||
promise = hashFolderPromise(name, dirname, options, isRootElement); | ||
} else if (stats.isFile()) { | ||
return hashFilePromise(name, dirname, options, isRootElement); | ||
promise = hashFilePromise(name, dirname, options, isRootElement); | ||
} else if (stats.isSymbolicLink()) { | ||
return hashSymLinkPromise(name, dirname, options, isRootElement); | ||
promise = hashSymLinkPromise(name, dirname, options, isRootElement); | ||
} else { | ||
log.err('hashElementPromise cannot handle ', stats); | ||
return { name, hash: 'Error: unknown element type' }; | ||
return Promise.resolve({ name, hash: 'Error: unknown element type' }); | ||
} | ||
return promise.catch(err => { | ||
if (err.code && (err.code === 'EMFILE' || err.code === 'ENFILE')) { | ||
log.queue(`queued ${dirname}/${name} because of ${err.code}`); | ||
const promise = new Promise((resolve, reject) => { | ||
queue.push(() => { | ||
log.queue(`Will processs queued ${dirname}/${name}`); | ||
return hashElementPromise(stats, dirname, options, isRootElement) | ||
.then(ok => resolve(ok)) | ||
.catch(err => reject(err)); | ||
}); | ||
}); | ||
if (queueTimer === undefined) { | ||
queueTimer = setTimeout(processQueue, 0); | ||
} | ||
return promise; | ||
} | ||
throw err; | ||
}); | ||
} | ||
function hashFolderPromise(name, dir, options, isRootElement = false) { | ||
function processQueue() { | ||
queueTimer = undefined; | ||
const runnables = queue; | ||
queue = []; | ||
runnables.forEach(run => run()); | ||
} | ||
async function hashFolderPromise(name, dir, options, isRootElement = false) { | ||
const folderPath = path.join(dir, name); | ||
@@ -114,15 +148,12 @@ let ignoreBasenameOnce = options.ignoreBasenameOnce; | ||
return fs.promises.readdir(folderPath, { withFileTypes: true }).then(files => { | ||
const children = files | ||
const files = await fs.promises.readdir(folderPath, { withFileTypes: true }); | ||
const children = await Promise.all( | ||
files | ||
.sort((a, b) => a.name.localeCompare(b.name)) | ||
.map(child => { | ||
return hashElementPromise(child, folderPath, options); | ||
}); | ||
.map(child => hashElementPromise(child, folderPath, options)), | ||
); | ||
return Promise.all(children).then(children => { | ||
if (ignoreBasenameOnce) options.ignoreBasenameOnce = true; | ||
const hash = new HashedFolder(name, children.filter(notUndefined), options, isRootElement); | ||
return hash; | ||
}); | ||
}); | ||
if (ignoreBasenameOnce) options.ignoreBasenameOnce = true; | ||
const hash = new HashedFolder(name, children.filter(notUndefined), options, isRootElement); | ||
return hash; | ||
} | ||
@@ -138,3 +169,3 @@ | ||
} else if (ignore(name, filePath, options.files)) { | ||
return undefined; | ||
return Promise.resolve(undefined); | ||
} | ||
@@ -157,2 +188,5 @@ | ||
const f = fs.createReadStream(filePath); | ||
f.on('error', err => { | ||
reject(err); | ||
}); | ||
f.pipe(hash, { end: false }); | ||
@@ -391,3 +425,3 @@ | ||
defaults: defaultOptions, | ||
hashElement: prep(require('graceful-fs')), | ||
hashElement: prep(require('fs')), | ||
// exposed for testing | ||
@@ -394,0 +428,0 @@ prep, |
{ | ||
"name": "folder-hash", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"description": "Create a hash checksum over a folder and its content - its children and their content", | ||
@@ -39,4 +39,3 @@ "main": "index.js", | ||
"debug": "^4.3.3", | ||
"graceful-fs": "~4.2.9", | ||
"minimatch": "~5.0.0" | ||
"minimatch": "~5.1.2" | ||
}, | ||
@@ -48,7 +47,7 @@ "devDependencies": { | ||
"ignore": "^5.2.0", | ||
"jsdoc": "3.6.10", | ||
"jsdoc": "4.0.0", | ||
"memfs": "^3.4.1", | ||
"mocha": "^9.2.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "~2.5.1" | ||
"prettier": "~2.8.2" | ||
}, | ||
@@ -55,0 +54,0 @@ "engines": { |
@@ -141,3 +141,3 @@ Create a hash checksum over a folder or a file. | ||
algo: 'sha1', // see crypto.getHashes() for options in your node.js REPL | ||
encoding: 'base64', // 'base64', 'hex' or 'binary' | ||
encoding: 'base64', // 'base64', 'base64url', 'hex' or 'binary' | ||
files: { | ||
@@ -203,3 +203,3 @@ exclude: [], | ||
</td> | ||
<td>encoding of the resulting hash. One of 'base64', 'hex' or 'binary'</td> | ||
<td>encoding of the resulting hash. One of 'base64', 'base64url', 'hex' or 'binary'</td> | ||
</tr> | ||
@@ -206,0 +206,0 @@ <tr> |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
32486
2
439
1
1
+ Addedminimatch@5.1.6(transitive)
- Removedgraceful-fs@~4.2.9
- Removedgraceful-fs@4.2.11(transitive)
- Removedminimatch@5.0.1(transitive)
Updatedminimatch@~5.1.2