forked from holochain/holochain-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpin_n3h
executable file
·109 lines (97 loc) · 3.03 KB
/
pin_n3h
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
#!/usr/bin/env node
/* Helper script for pinning downloadable version of n3h binaries.
* Bakes the sha256 hashes into the code for security.
*
* Run with the target n3h release name (tag name) e.g.
* `./pin_n3h adhoc-2019-03-19`
*/
const path = require('path')
const fs = require('fs')
const https = require('https')
const { URL } = require('url')
if (!process.argv[2]) {
throw new Error('required tag, usage: pin_n3h n3h_tag')
}
const TAG = process.argv[2]
const OS_ARCH = [
'linux-ia32.AppImage',
'linux-ia32.tar.gz',
'linux-x64.AppImage',
'linux-x64.tar.gz',
'linux-arm.AppImage',
'linux-arm.tar.gz',
'linux-arm64.AppImage',
'linux-arm64.tar.gz',
'mac-x64.dmg',
'win-x64.exe'
]
function fetch (url) {
return new Promise((resolve, reject) => {
try {
url = new URL(url)
console.log('fetch', url.toString(), url.hostname, url.pathname)
https.get({
hostname: url.hostname,
path: url.pathname + url.search,
headers: {
'User-Agent': 'Mozilla/5.0 () AppleWebKit/537.36 (KHTML, like Gecko) NodeJs'
}
}, res => {
if (res.statusCode === 302) {
return resolve(fetch(res.headers.location))
}
let data = Buffer.alloc(0)
res.on('data', chunk => {
data = Buffer.concat([data, chunk])
})
res.on('end', () => {
if (res.statusCode !== 200) {
return reject(new Error('bad status: ' + res.statusCode + ' ' + data.toString('utf8')))
}
resolve(data)
})
})
} catch (e) {
reject(e)
}
})
}
async function main () {
const version = JSON.parse((await fetch(`https://github.com/holochain/n3h/raw/${TAG}/package.json`)).toString('utf8')).version
const commitish = JSON.parse((await fetch(`https://api.github.com/repos/holochain/n3h/releases/tags/${TAG}`)).toString('utf8'))['target_commitish']
const out = {
warning: 'DO NOT MODIFY - This file is generated by the holochain-rust/pin_n3h script',
release: TAG,
version: 'v' + version,
commitish,
artifacts: {}
}
for (let osArch of OS_ARCH) {
const sum = (await fetch(`https://github.com/holochain/n3h/releases/download/${TAG}/n3h-${version}-${osArch}.sha256`)).toString('utf8').trim()
const m = sum.match(/([^\s]+)\s+([^\s]+)/)
const os = osArch.split('.')[0].split('-')[0]
const arch = osArch.split('.')[0].split('-')[1]
const type = osArch.split('.')[1].toLowerCase()
if (!(os in out.artifacts)) {
out.artifacts[os] = {}
}
let ref = out.artifacts[os]
if (!(arch in ref)) {
ref[arch] = {}
}
ref = ref[arch]
ref[type] = {
url: `https://github.com/holochain/n3h/releases/download/${TAG}/n3h-${version}-${osArch}`,
file: m[2],
hash: m[1]
}
}
const fn = path.resolve(__dirname, 'net', 'src', 'ipc', 'n3h_pin.json')
fs.writeFileSync(fn, JSON.stringify(out, null, 2))
console.log('')
console.log(fn, fs.readFileSync(fn).toString('utf8'))
}
main().then(() => {}, err => {
console.error(err)
process.exit(1)
})