index.js
4.34 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
const cp = require('child_process') // eslint-disable-line security/detect-child-process
const fs = require('fs')
const path = require('path')
const gfs = require('graceful-fs')
const flattenDeep = require('lodash.flattendeep')
const hasha = require('hasha')
const releaseZalgo = require('release-zalgo')
const PACKAGE_FILE = require.resolve('./package.json')
const TEN_MEBIBYTE = 1024 * 1024 * 10
const readFile = {
async (file) {
return new Promise((resolve, reject) => {
gfs.readFile(file, (err, contents) => {
err ? reject(err) : resolve(contents)
})
})
},
sync (file) {
return fs.readFileSync(file)
}
}
const tryReadFile = {
async (file) {
return new Promise(resolve => {
gfs.readFile(file, (err, contents) => {
resolve(err ? null : contents)
})
})
},
sync (file) {
try {
return fs.readFileSync(file)
} catch (err) {
return null
}
}
}
const tryExecFile = {
async (file, args, options) {
return new Promise(resolve => {
cp.execFile(file, args, options, (err, stdout) => {
resolve(err ? null : stdout)
})
})
},
sync (file, args, options) {
try {
return cp.execFileSync(file, args, options)
} catch (err) {
return null
}
}
}
const git = {
tryGetRef (zalgo, dir, head) {
const m = /^ref: (.+)$/.exec(head.toString('utf8').trim())
if (!m) return null
return zalgo.run(tryReadFile, path.join(dir, '.git', m[1]))
},
tryGetDiff (zalgo, dir) {
return zalgo.run(tryExecFile,
'git',
// Attempt to get consistent output no matter the platform. Diff both
// staged and unstaged changes.
['--no-pager', 'diff', 'HEAD', '--no-color', '--no-ext-diff'],
{
cwd: dir,
maxBuffer: TEN_MEBIBYTE,
env: Object.assign({}, process.env, {
// Force the GIT_DIR to prevent git from diffing a parent repository
// in case the directory isn't actually a repository.
GIT_DIR: path.join(dir, '.git')
}),
// Ignore stderr.
stdio: ['ignore', 'pipe', 'ignore']
})
}
}
function addPackageData (zalgo, pkgPath) {
const dir = path.dirname(pkgPath)
return zalgo.all([
dir,
zalgo.run(readFile, pkgPath),
zalgo.run(tryReadFile, path.join(dir, '.git', 'HEAD'))
.then(head => {
if (!head) return []
return zalgo.all([
zalgo.run(tryReadFile, path.join(dir, '.git', 'packed-refs')),
git.tryGetRef(zalgo, dir, head),
git.tryGetDiff(zalgo, dir)
])
.then(results => {
return [head].concat(results.filter(Boolean))
})
})
])
}
function computeHash (zalgo, paths, pepper, salt) {
const inputs = []
if (pepper) inputs.push(pepper)
if (typeof salt !== 'undefined') {
if (Buffer.isBuffer(salt) || typeof salt === 'string') {
inputs.push(salt)
} else if (typeof salt === 'object' && salt !== null) {
inputs.push(JSON.stringify(salt))
} else {
throw new TypeError('Salt must be an Array, Buffer, Object or string')
}
}
return zalgo.all(paths.map(pkgPath => addPackageData(zalgo, pkgPath)))
.then(furtherInputs => hasha(flattenDeep([inputs, furtherInputs]), {algorithm: 'sha256'}))
}
let ownHash = null
let ownHashPromise = null
function run (zalgo, paths, salt) {
if (!ownHash) {
return zalgo.run({
async () {
if (!ownHashPromise) {
ownHashPromise = computeHash(zalgo, [PACKAGE_FILE])
}
return ownHashPromise
},
sync () {
return computeHash(zalgo, [PACKAGE_FILE])
}
})
.then(hash => {
ownHash = Buffer.from(hash, 'hex')
ownHashPromise = null
return run(zalgo, paths, salt)
})
}
if (paths === PACKAGE_FILE && typeof salt === 'undefined') {
// Special case that allow the pepper value to be obtained. Mainly here for
// testing purposes.
return zalgo.returns(ownHash.toString('hex'))
}
paths = Array.isArray(paths) ? paths : [paths]
return computeHash(zalgo, paths, ownHash, salt)
}
module.exports = (paths, salt) => {
try {
return run(releaseZalgo.async(), paths, salt)
} catch (err) {
return Promise.reject(err)
}
}
module.exports.sync = (paths, salt) => {
const result = run(releaseZalgo.sync(), paths, salt)
return releaseZalgo.unwrapSync(result)
}