forked from vercel/vercel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.js
44 lines (36 loc) · 808 Bytes
/
hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Native
const { createHash } = require('crypto')
// Packages
const { readFile } = require('fs-extra')
/**
* Computes hashes for the contents of each file given.
*
* @param {Array} of {String} full paths
* @return {Map}
*/
async function hashes(files) {
const map = new Map()
await Promise.all(
files.map(async name => {
const data = await readFile(name)
const h = hash(data)
const entry = map.get(h)
if (entry) {
entry.names.push(name)
} else {
map.set(hash(data), { names: [name], data })
}
})
)
return map
}
/**
* Computes a hash for the given buf.
*
* @param {Buffer} file data
* @return {String} hex digest
*/
function hash(buf) {
return createHash('sha1').update(buf).digest('hex')
}
module.exports = hashes