forked from mempool/mempool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-assets.js
93 lines (79 loc) · 3.09 KB
/
sync-assets.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
var https = require('https');
var fs = require('fs');
const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
let configContent = {};
var PATH;
if (process.argv[2]) {
PATH = process.argv[2];
}
if (!PATH) {
throw new Error('Resource path argument is not set');
}
try {
const rawConfig = fs.readFileSync(CONFIG_FILE_NAME);
configContent = JSON.parse(rawConfig);
console.log(`${CONFIG_FILE_NAME} file found, using provided config`);
} catch (e) {
if (e.code !== 'ENOENT') {
throw new Error(e);
} else {
console.log(`${CONFIG_FILE_NAME} file not found, using default config`);
}
}
function download(filename, url) {
https.get(url, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
throw new Error('HTTP Error ' + response.statusCode + ' while fetching \'' + filename + '\'');
}
response.pipe(fs.createWriteStream(filename));
})
.on('error', function(e) {
throw new Error(e);
});
}
function downloadMiningPoolLogos() {
const options = {
host: 'api.github.com',
path: '/repos/mempool/mining-pool-logos/contents/',
method: 'GET',
headers: {'user-agent': 'node.js'}
};
https.get(options, (response) => {
let chunks_of_data = [];
response.on('data', (fragments) => {
chunks_of_data.push(fragments);
});
response.on('end', () => {
let response_body = Buffer.concat(chunks_of_data);
try {
const poolLogos = JSON.parse(response_body.toString());
for (const poolLogo of poolLogos) {
download(`${PATH}/mining-pools/${poolLogo.name}`, poolLogo.download_url);
}
} catch (e) {
console.error(`Unable to download mining pool logos. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
});
response.on('error', (error) => {
throw new Error(error);
});
})
}
let assetsJsonUrl = 'https://raw.githubusercontent.com/mempool/asset_registry_db/master/index.json';
let assetsMinimalJsonUrl = 'https://raw.githubusercontent.com/mempool/asset_registry_db/master/index.minimal.json';
if (configContent.BASE_MODULE && configContent.BASE_MODULE === 'liquid') {
assetsJsonUrl = 'https://raw.githubusercontent.com/Blockstream/asset_registry_db/master/index.json';
assetsMinimalJsonUrl = 'https://raw.githubusercontent.com/Blockstream/asset_registry_db/master/index.minimal.json';
}
const testnetAssetsJsonUrl = 'https://raw.githubusercontent.com/Blockstream/asset_registry_testnet_db/master/index.json';
const testnetAssetsMinimalJsonUrl = 'https://raw.githubusercontent.com/Blockstream/asset_registry_testnet_db/master/index.minimal.json';
console.log('Downloading assets');
download(PATH + 'assets.json', assetsJsonUrl);
console.log('Downloading assets minimal');
download(PATH + 'assets.minimal.json', assetsMinimalJsonUrl);
console.log('Downloading testnet assets');
download(PATH + 'assets-testnet.json', testnetAssetsJsonUrl);
console.log('Downloading testnet assets minimal');
download(PATH + 'assets-testnet.minimal.json', testnetAssetsMinimalJsonUrl);
console.log('Downloading mining pool logos');
downloadMiningPoolLogos();