-
Notifications
You must be signed in to change notification settings - Fork 7
/
integrity.js
101 lines (88 loc) · 2.8 KB
/
integrity.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
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
const crypto = require('crypto')
const https = require('https')
const cp = require('child_process')
const fs = require('fs')
const BASE='https://raw.githubusercontent.com/VSCodium/versions/master/'
const TYPES = {
win32Archive: 'win32/ia32/archive/',
win64Archive: 'win32/x64/archive/',
win32System: 'win32/ia32/system/',
win64System: 'win32/x64/system/',
win32User: 'win32/ia32/user/',
win64User: 'win32/x64/user/',
darwin: 'darwin/',
linux32: 'linux/ia32/',
linux64: 'linux/x64/'
}
const FILENAME = 'latest.json'
function getSums (data) {
let h = crypto.createHash('sha256')
h.update(data)
const sha256 = h.digest('hex')
h = crypto.createHash('sha1')
h.update(data)
const sha1 = h.digest('hex')
return { sha256, sha1 }
}
function getFile (url) {
// the "easy way"; returns file buffer
const filename = url.substring(url.lastIndexOf('/')+1)
cp.execSync(`curl -Lso /tmp/${filename} ${url}`)
const contents = fs.readFileSync(`/tmp/${filename}`)
fs.unlinkSync(`/tmp/${filename}`) // delete tmp file
return contents
}
function getJson (type) {
return new Promise((resolve, reject) => {
https.get(`${BASE}${type}${FILENAME}`, (res) => {
const { statusCode } = res;
if (statusCode > 200) {
res.resume()
reject(new Error(`could not get version json for ${type}`))
return
}
res.setEncoding('utf8')
let rawData = ''
res.on('data', (chunk) => { rawData += chunk })
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData)
resolve(parsedData)
} catch (e) {
reject(e)
}
})
}).on('error', (e) => {
reject(e)
})
})
}
function compareSums (sums, json) {
return sums.sha256 === json.sha256hash && sums.sha1 === json.hash
}
async function validateAssets (throwErrors = false) {
const results = {}
for (let type in TYPES) {
console.log('Checking', type, '...')
try {
const json = await getJson(TYPES[type])
console.log('Got version JSON. Downloading asset ...')
const file = await getFile(json.url)
console.log('Downloaded asset. Computing sums ...')
const sums = await getSums(file)
const valid = compareSums(sums, json)
if (!valid && throwErrors) throw new Error(`Invalid hashes for ${type} ${json.productVersion}`)
results[type] = valid
? `Hashes match (${json.productVersion})`
: `Invalid hashes (${json.productVersion}) - (want: ${sums.sha256}, got: ${json.sha256hash}), (want: ${sums.sha1}, got: ${json.hash})`
console.log(results[type])
} catch (e) {
if (throwErrors) throw e
console.log('Encountered an error, skipping ...')
results[type] = `Error: ${e.message}`
}
}
console.log('Summary:')
console.log(JSON.stringify(results, null, 4))
}
validateAssets(process.argv[2] === 'test')