forked from dominictarr/noderify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterministic.js
More file actions
46 lines (35 loc) · 933 Bytes
/
deterministic.js
File metadata and controls
46 lines (35 loc) · 933 Bytes
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
45
46
var through = require('through2')
var createHash = require('crypto').createHash
function hash(s) {
return createHash('sha256').update(s).digest('base64')
}
function sort (obj) {
var o = {}
Object.keys(obj).sort().forEach(function (k) {
o[k] = obj[k]
})
return o
}
module.exports = function (cb) {
var entry = null
var content = {}, files = {}
return through.obj(function (data, enc, cb) {
//address each unique file by it's hash
content[data.sha256 = hash(data.source)] = data.source
//map of which file has what hash, plus what deps it uses.
files[data.id] = [data.sha256, sort(data.deps)]
if(data.entry)
entry = data.id
cb()
}, function () {
cb(null,
//{<hash>: source,...}
sort(content),
//{<filename>: [<hash>, {<require>: <resolve-filename>}],..}
sort(files),
//filename to start with.
entry
)
this.push(null)
})
}