forked from mempool/mempool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-assets.js
413 lines (371 loc) · 15.5 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
var https = require('https');
var fs = require('fs');
var crypto = require('crypto');
var path = require('node:path');
const LOG_TAG = '[sync-assets]';
let verbose = false;
let MEMPOOL_CDN = false;
let DRY_RUN = false;
if (parseInt(process.env.SKIP_SYNC) === 1) {
console.log(`${LOG_TAG} SKIP_SYNC is set, not checking any assets`);
process.exit(0);
}
if (parseInt(process.env.VERBOSE) === 1) {
console.log(`${LOG_TAG} VERBOSE is set, logs will be more verbose`);
verbose = true;
}
if (parseInt(process.env.MEMPOOL_CDN) === 1) {
console.log(`${LOG_TAG} MEMPOOL_CDN is set, assets will be downloaded from mempool.space`);
MEMPOOL_CDN = true;
}
if (parseInt(process.env.DRY_RUN) === 1) {
console.log(`${LOG_TAG} DRY_RUN is set, not downloading any assets`);
DRY_RUN = true;
}
const githubSecret = process.env.GITHUB_TOKEN;
const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
let configContent = {};
var ASSETS_PATH;
if (process.argv[2]) {
ASSETS_PATH = process.argv[2];
ASSETS_PATH += ASSETS_PATH.endsWith("/") ? "" : "/"
ASSETS_PATH = path.resolve(path.normalize(ASSETS_PATH));
console.log(`[sync-assets] using ASSETS_PATH ${ASSETS_PATH}`);
if (!fs.existsSync(ASSETS_PATH)){
console.log(`${LOG_TAG} ${ASSETS_PATH} does not exist, creating`);
fs.mkdirSync(ASSETS_PATH, { recursive: true });
}
}
if (!ASSETS_PATH) {
throw new Error('Resource path argument is not set');
}
try {
const rawConfig = fs.readFileSync(CONFIG_FILE_NAME);
configContent = JSON.parse(rawConfig);
console.log(`${LOG_TAG} ${CONFIG_FILE_NAME} file found, using provided config`);
} catch (e) {
if (e.code !== 'ENOENT') {
throw new Error(e);
} else {
console.log(`${LOG_TAG} ${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);
})
.on('finish', () => {
if (verbose) {
console.log(`${LOG_TAG} \tFinished downloading ${url} to ${filename}`);
}
});
}
function getLocalHash(filePath) {
const size = fs.statSync(filePath);
const buffer = fs.readFileSync(filePath);
const bufferWithHeader = Buffer.concat([Buffer.from('blob '), Buffer.from(`${size.size}`), Buffer.from('\0'), buffer]);
const hash = crypto.createHash('sha1').update(bufferWithHeader).digest('hex');
if (verbose) {
console.log(`${LOG_TAG} \t\tgetLocalHash ${filePath} ${hash}`);
}
return hash;
}
function downloadMiningPoolLogos$() {
return new Promise((resolve, reject) => {
console.log(`${LOG_TAG} \tChecking if mining pool logos needs downloading or updating...`);
const options = {
host: 'api.github.com',
path: '/repos/mempool/mining-pool-logos/contents/',
method: 'GET',
headers: {'user-agent': 'node.js'}
};
if (githubSecret) {
console.log(`${LOG_TAG} Downloading the mining pool logos with authentication`);
options.headers['authorization'] = `Bearer ${githubSecret}`;
options.headers['X-GitHub-Api-Version'] = '2022-11-28';
}
https.get(options, (response) => {
const chunks_of_data = [];
response.on('data', (fragments) => {
chunks_of_data.push(fragments);
});
response.on('end', () => {
const response_body = Buffer.concat(chunks_of_data);
try {
const poolLogos = JSON.parse(response_body.toString());
if (poolLogos.message) {
reject(poolLogos.message);
}
let downloadedCount = 0;
for (const poolLogo of poolLogos) {
if (verbose) {
console.log(`${LOG_TAG} Processing ${poolLogo.name}`);
}
console.log(`${ASSETS_PATH}/mining-pools/${poolLogo.name}`);
const filePath = `${ASSETS_PATH}/mining-pools/${poolLogo.name}`;
if (fs.existsSync(filePath)) {
const localHash = getLocalHash(filePath);
if (verbose) {
console.log(`${LOG_TAG} \t\tremote ${poolLogo.name} logo hash ${poolLogo.sha}`);
console.log(`${LOG_TAG} \t\t\tchecking if ${filePath} exists: ${fs.existsSync(filePath)}`);
}
if (localHash !== poolLogo.sha) {
console.log(`${LOG_TAG} \t\t\t\t${poolLogo.name} is different on the remote, downloading...`);
let download_url = poolLogo.download_url;
if (MEMPOOL_CDN) {
download_url = download_url.replace("raw.githubusercontent.com/mempool/mining-pool-logos/master", "mempool.space/resources/mining-pools");
}
if (DRY_RUN) {
console.log(`${LOG_TAG} \t\tDRY_RUN is set, not downloading ${poolLogo.name} but we should`);
} else {
if (verbose) {
console.log(`${LOG_TAG} \t\tDownloading ${download_url} to ${filePath}`);
}
download(filePath, download_url);
downloadedCount++;
}
} else {
console.log(`${LOG_TAG} \t\t${poolLogo.name} is already up to date. Skipping.`);
}
} else {
console.log(`${LOG_TAG} \t\t${poolLogo.name} is missing, downloading...`);
const miningPoolsDir = `${ASSETS_PATH}/mining-pools/`;
if (!fs.existsSync(miningPoolsDir)){
fs.mkdirSync(miningPoolsDir, { recursive: true });
}
let download_url = poolLogo.download_url;
if (MEMPOOL_CDN) {
download_url = download_url.replace("raw.githubusercontent.com/mempool/mining-pool-logos/master", "mempool.space/resources/mining-pools");
}
if (DRY_RUN) {
console.log(`${LOG_TAG} DRY_RUN is set, not downloading ${poolLogo.name} but it should`);
} else {
console.log(`${LOG_TAG} \tDownloading ${download_url} to ${filePath}`);
download(filePath, download_url);
downloadedCount++;
}
}
}
console.log(`${LOG_TAG} \t\tDownloaded ${downloadedCount} and skipped ${poolLogos.length - downloadedCount} existing mining pool logos`);
resolve();
} catch (e) {
reject(`Unable to download mining pool logos. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
});
response.on('error', (error) => {
reject(error);
});
});
});
}
function downloadPromoVideoSubtiles$() {
return new Promise((resolve, reject) => {
console.log(`${LOG_TAG} \tChecking if promo video subtitles needs downloading or updating...`);
const options = {
host: 'api.github.com',
path: '/repos/mempool/mempool-promo/contents/subtitles',
method: 'GET',
headers: {'user-agent': 'node.js'}
};
if (githubSecret) {
console.log(`${LOG_TAG} \tDownloading the promo video subtitles with authentication`);
options.headers['authorization'] = `Bearer ${githubSecret}`;
options.headers['X-GitHub-Api-Version'] = '2022-11-28';
}
https.get(options, (response) => {
const chunks_of_data = [];
response.on('data', (fragments) => {
chunks_of_data.push(fragments);
});
response.on('end', () => {
const response_body = Buffer.concat(chunks_of_data);
try {
const videoLanguages = JSON.parse(response_body.toString());
if (videoLanguages.message) {
reject(videoLanguages.message);
}
let downloadedCount = 0;
for (const language of videoLanguages) {
if (verbose) {
console.log(`${LOG_TAG} Processing ${language.name}`);
}
const filePath = `${ASSETS_PATH}/promo-video/${language.name}`;
if (fs.existsSync(filePath)) {
if (verbose) {
console.log(`${LOG_TAG} \t${language.name} remote promo video hash ${language.sha}`);
}
const localHash = getLocalHash(filePath);
if (localHash !== language.sha) {
console.log(`${LOG_TAG} \t\t${language.name} is different on the remote, updating`);
let download_url = language.download_url;
if (MEMPOOL_CDN) {
download_url = download_url.replace("raw.githubusercontent.com/mempool/mempool-promo/master/subtitles", "mempool.space/resources/promo-video");
}
if (DRY_RUN) {
console.log(`${LOG_TAG} \t\tDRY_RUN is set, not downloading ${language.name} but we should`);
} else {
if (verbose) {
console.log(`${LOG_TAG} \t\tdownloading ${download_url} to ${filePath}`);
}
download(filePath, download_url);
downloadedCount++;
}
} else {
console.log(`${LOG_TAG} \t\t${language.name} is already up to date. Skipping.`);
}
} else {
console.log(`${LOG_TAG} \t\t${language.name} is missing, downloading`);
const promoVideosDir = `${ASSETS_PATH}/promo-video/`;
if (!fs.existsSync(promoVideosDir)){
fs.mkdirSync(promoVideosDir, { recursive: true });
}
let download_url = language.download_url;
if (MEMPOOL_CDN) {
download_url = downloadownload_url = download_url.replace("raw.githubusercontent.com/mempool/mempool-promo/master/subtitles", "mempool.space/resources/promo-video");
}
if (DRY_RUN) {
console.log(`${LOG_TAG} \tDRY_RUN is set, not downloading ${language.name} but we should`);
} else {
if (verbose) {
console.log(`${LOG_TAG} downloading ${download_url} to ${filePath}`);
}
download(filePath, download_url);
downloadedCount++;
}
}
}
console.log(`${LOG_TAG} Downloaded ${downloadedCount} and skipped ${videoLanguages.length - downloadedCount} existing video subtitles`);
resolve();
} catch (e) {
reject(`Unable to download video subtitles. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
});
response.on('error', (error) => {
reject(error);
});
});
});
}
function downloadPromoVideo$() {
return new Promise((resolve, reject) => {
console.log(`${LOG_TAG} \tChecking if promo video needs downloading or updating...`);
const options = {
host: 'api.github.com',
path: '/repos/mempool/mempool-promo/contents',
method: 'GET',
headers: {'user-agent': 'node.js'}
};
if (githubSecret) {
console.log(`${LOG_TAG} \tDownloading the promo video with authentication`);
options.headers['authorization'] = `Bearer ${githubSecret}`;
options.headers['X-GitHub-Api-Version'] = '2022-11-28';
}
https.get(options, (response) => {
const chunks_of_data = [];
response.on('data', (fragments) => {
chunks_of_data.push(fragments);
});
response.on('end', () => {
const response_body = Buffer.concat(chunks_of_data);
try {
const contents = JSON.parse(response_body.toString());
if (contents.message) {
reject(contents.message);
}
for (const item of contents) {
if (item.name !== 'promo.mp4') {
continue;
}
const filePath = `${ASSETS_PATH}/promo-video/mempool-promo.mp4`;
if (fs.existsSync(filePath)) {
const localHash = getLocalHash(filePath);
if (localHash !== item.sha) {
console.log(`${LOG_TAG} \tmempool-promo.mp4 is different on the remote, updating`);
let download_url = item.download_url;
if (MEMPOOL_CDN) {
download_url = download_url.replace("raw.githubusercontent.com/mempool/mempool-promo/master/promo.mp4", "mempool.space/resources/promo-video/mempool-promo.mp4");
}
if (DRY_RUN) {
console.log(`${LOG_TAG} DRY_RUN is set, not downloading mempool-promo.mp4 but we should`);
} else {
if (verbose) {
console.log(`${LOG_TAG} downloading ${download_url} to ${filePath}`);
}
download(filePath, download_url);
console.log(`${LOG_TAG} \tmempool-promo.mp4 downloaded.`);
}
} else {
console.log(`${LOG_TAG} \t\tmempool-promo.mp4 is already up to date. Skipping.`);
}
} else {
console.log(`${LOG_TAG} \tmempool-promo.mp4 is missing, downloading`);
let download_url = item.download_url;
if (MEMPOOL_CDN) {
download_url = download_url.replace("raw.githubusercontent.com/mempool/mempool-promo/master/promo.mp4", "mempool.space/resources/promo-video/mempool-promo.mp4");
}
if (DRY_RUN) {
console.log(`${LOG_TAG} DRY_RUN is set, not downloading mempool-promo.mp4 but we should`);
} else {
if (verbose) {
console.log(`${LOG_TAG} downloading ${download_url} to ${filePath}`);
}
download(filePath, download_url);
}
}
}
resolve();
} catch (e) {
reject(`Unable to download video. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
});
response.on('error', (error) => {
reject(error);
});
});
});
}
if (configContent.BASE_MODULE && configContent.BASE_MODULE === 'liquid') {
const assetsJsonUrl = 'https://raw.githubusercontent.com/Blockstream/asset_registry_db/master/index.json';
const 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(`${LOG_TAG} Downloading assets`);
download(`${ASSETS_PATH}/assets.json`, assetsJsonUrl);
console.log(`${LOG_TAG} Downloading assets minimal`);
download(`${ASSETS_PATH}/assets.minimal.json`, assetsMinimalJsonUrl);
console.log(`${LOG_TAG} Downloading testnet assets`);
download(`${ASSETS_PATH}/assets-testnet.json`, testnetAssetsJsonUrl);
console.log(`${LOG_TAG} Downloading testnet assets minimal`);
download(`${ASSETS_PATH}/assets-testnet.minimal.json`, testnetAssetsMinimalJsonUrl);
} else {
if (verbose) {
console.log(`${LOG_TAG} BASE_MODULE is not set to Liquid (currently ${configContent.BASE_MODULE}), skipping downloading assets`);
}
}
(() => {
if (verbose) {
console.log(`${LOG_TAG} Downloading mining pool logos`);
}
downloadMiningPoolLogos$()
.then(() => {
if (verbose) {
console.log(`${LOG_TAG} Downloading promo video subtitles`);
}
downloadPromoVideoSubtiles$();
})
.then(() => {
if (verbose) {
console.log(`${LOG_TAG} Downloading promo video`);
}
downloadPromoVideo$();
})
.catch((error) => {
throw new Error(error);
});
})();