forked from sk-musik-production-inc/mempool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-assets.js
More file actions
378 lines (315 loc) · 12 KB
/
sync-assets.js
File metadata and controls
378 lines (315 loc) · 12 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
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
const https = require('https');
const fs = require('fs').promises;
const fsSync = require('fs');
const crypto = require('crypto');
const path = require('node:path');
// Configuration
const LOG_TAG = '[sync-assets]';
const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
const config = {
verbose: parseInt(process.env.VERBOSE) === 1,
mempoolCDN: parseInt(process.env.MEMPOOL_CDN) === 1,
dryRun: parseInt(process.env.DRY_RUN) === 1,
githubToken: process.env.GITHUB_TOKEN,
};
// Early exit if SKIP_SYNC is set
if (parseInt(process.env.SKIP_SYNC) === 1) {
console.log(`${LOG_TAG} SKIP_SYNC is set, not checking any assets`);
process.exit(0);
}
// Log configuration
if (config.verbose) console.log(`${LOG_TAG} VERBOSE is set, logs will be more verbose`);
if (config.mempoolCDN) console.log(`${LOG_TAG} MEMPOOL_CDN is set, assets will be downloaded from mempool.space`);
if (config.dryRun) console.log(`${LOG_TAG} DRY_RUN is set, not downloading any assets`);
// Setup assets path
const ASSETS_PATH = (() => {
if (!process.argv[2]) {
throw new Error('Resource path argument is not set');
}
const rawPath = process.argv[2].endsWith("/") ? process.argv[2] : `${process.argv[2]}/`;
const normalizedPath = path.resolve(path.normalize(rawPath));
console.log(`${LOG_TAG} using ASSETS_PATH ${normalizedPath}`);
if (!fsSync.existsSync(normalizedPath)) {
console.log(`${LOG_TAG} ${normalizedPath} does not exist, creating`);
fsSync.mkdirSync(normalizedPath, { recursive: true });
}
return normalizedPath;
})();
// Load frontend config
const loadConfig = () => {
try {
const rawConfig = fsSync.readFileSync(CONFIG_FILE_NAME, 'utf8');
console.log(`${LOG_TAG} ${CONFIG_FILE_NAME} file found, using provided config`);
return JSON.parse(rawConfig);
} catch (e) {
if (e.code !== 'ENOENT') throw e;
console.log(`${LOG_TAG} ${CONFIG_FILE_NAME} file not found, using default config`);
return {};
}
};
const configContent = loadConfig();
// Utility: Make HTTPS request
const httpsRequest = (options) => {
return new Promise((resolve, reject) => {
https.get(options, (response) => {
const chunks = [];
response.on('data', (chunk) => chunks.push(chunk));
response.on('end', () => resolve(Buffer.concat(chunks)));
response.on('error', reject);
}).on('error', reject);
});
};
// Utility: Download file
const downloadFile = (filePath, url) => {
if (!filePath || !url) {
if (config.verbose) {
console.log('skipping malformed download request: ', filePath, url);
}
return Promise.resolve();
}
return new Promise((resolve, reject) => {
https.get(url, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error(`HTTP Error ${response.statusCode} while fetching '${filePath}'`));
return;
}
const writeStream = fsSync.createWriteStream(filePath);
response.pipe(writeStream);
writeStream.on('finish', () => {
if (config.verbose) {
console.log(`${LOG_TAG} \tFinished downloading ${url} to ${filePath}`);
}
resolve();
});
writeStream.on('error', reject);
}).on('error', reject);
});
};
// Utility: Get local file hash (Git blob format)
const getLocalHash = (filePath) => {
const stats = fsSync.statSync(filePath);
const buffer = fsSync.readFileSync(filePath);
const bufferWithHeader = Buffer.concat([
Buffer.from('blob '),
Buffer.from(`${stats.size}`),
Buffer.from('\0'),
buffer
]);
const hash = crypto.createHash('sha1').update(bufferWithHeader).digest('hex');
if (config.verbose) {
console.log(`${LOG_TAG} \t\tgetLocalHash ${filePath} ${hash}`);
}
return hash;
};
// Utility: Create GitHub API options
const createGitHubOptions = (repoPath) => {
const options = {
host: 'api.github.com',
path: repoPath,
method: 'GET',
headers: { 'user-agent': 'node.js' }
};
if (config.githubToken) {
options.headers['authorization'] = `Bearer ${config.githubToken}`;
options.headers['X-GitHub-Api-Version'] = '2022-11-28';
}
return options;
};
// Utility: Replace URL for CDN
const getCDNUrl = (url, replacePattern) => {
return config.mempoolCDN ? url.replace(replacePattern.from, replacePattern.to) : url;
};
// Utility: Ensure directory exists
const ensureDirectory = (dirPath) => {
if (!fsSync.existsSync(dirPath)) {
fsSync.mkdirSync(dirPath, { recursive: true });
}
};
// Core: Process file item (handles checking and downloading)
const processFileItem = async (item, options) => {
const {
filePath,
remoteHash,
downloadUrl,
cdnPattern,
itemName,
downloadDir
} = options;
const fileExists = fsSync.existsSync(filePath);
if (fileExists) {
const localHash = getLocalHash(filePath);
if (config.verbose) {
console.log(`${LOG_TAG} \t\tremote ${itemName} hash ${remoteHash}`);
}
if (localHash !== remoteHash) {
console.log(`${LOG_TAG} \t\t${itemName} is different on the remote, downloading...`);
if (config.dryRun) {
console.log(`${LOG_TAG} \t\tDRY_RUN is set, not downloading ${itemName} but we should`);
return false;
}
const url = getCDNUrl(downloadUrl, cdnPattern);
if (config.verbose) {
console.log(`${LOG_TAG} \t\tDownloading ${url} to ${filePath}`);
}
await downloadFile(filePath, url);
return true;
} else {
console.log(`${LOG_TAG} \t\t${itemName} is already up to date. Skipping.`);
return false;
}
} else {
console.log(`${LOG_TAG} \t\t${itemName} is missing, downloading...`);
ensureDirectory(downloadDir);
if (config.dryRun) {
console.log(`${LOG_TAG} \t\tDRY_RUN is set, not downloading ${itemName} but we should`);
return false;
}
const url = getCDNUrl(downloadUrl, cdnPattern);
if (config.verbose) {
console.log(`${LOG_TAG} \t\tDownloading ${url} to ${filePath}`);
}
await downloadFile(filePath, url);
return true;
}
};
// Core: Fetch GitHub directory contents
const fetchGitHubContents = async (repoPath, useAuth = false) => {
if (useAuth && config.githubToken) {
console.log(`${LOG_TAG} \tDownloading with authentication`);
}
const options = createGitHubOptions(repoPath);
const responseBody = await httpsRequest(options);
const contents = JSON.parse(responseBody.toString());
if (contents.message) {
throw new Error(contents.message);
}
return contents;
};
// Main: Download mining pool logos
const downloadMiningPoolLogos = async () => {
console.log(`${LOG_TAG} \tChecking if mining pool logos needs downloading or updating...`);
try {
const poolLogos = await fetchGitHubContents('/repos/mempool/mining-pool-logos/contents/', !!config.githubToken);
let downloadedCount = 0;
const validFiles = poolLogos.filter(item => item.type === 'file' && item.download_url);
for (const poolLogo of validFiles) {
if (config.verbose) {
console.log(`${LOG_TAG} Processing ${poolLogo.name}`);
}
const downloaded = await processFileItem(poolLogo, {
filePath: `${ASSETS_PATH}/mining-pools/${poolLogo.name}`,
remoteHash: poolLogo.sha,
downloadUrl: poolLogo.download_url,
cdnPattern: {
from: "raw.githubusercontent.com/mempool/mining-pool-logos/master",
to: "mempool.space/resources/mining-pools"
},
itemName: poolLogo.name,
downloadDir: `${ASSETS_PATH}/mining-pools/`
});
if (downloaded) downloadedCount++;
}
console.log(`${LOG_TAG} \t\tDownloaded ${downloadedCount} and skipped ${validFiles.length - downloadedCount} existing mining pool logos`);
} catch (e) {
throw new Error(`Unable to download mining pool logos. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
};
// Main: Download promo video subtitles
const downloadPromoVideoSubtitles = async () => {
console.log(`${LOG_TAG} \tChecking if promo video subtitles needs downloading or updating...`);
try {
const subtitles = await fetchGitHubContents('/repos/mempool/mempool-promo/contents/subtitles', !!config.githubToken);
let downloadedCount = 0;
const validFiles = subtitles.filter(item => item.type === 'file' && item.download_url);
for (const subtitle of validFiles) {
if (config.verbose) {
console.log(`${LOG_TAG} Processing ${subtitle.name}`);
}
const downloaded = await processFileItem(subtitle, {
filePath: `${ASSETS_PATH}/promo-video/${subtitle.name}`,
remoteHash: subtitle.sha,
downloadUrl: subtitle.download_url,
cdnPattern: {
from: "raw.githubusercontent.com/mempool/mempool-promo/master/subtitles",
to: "mempool.space/resources/promo-video"
},
itemName: subtitle.name,
downloadDir: `${ASSETS_PATH}/promo-video/`
});
if (downloaded) downloadedCount++;
}
console.log(`${LOG_TAG} Downloaded ${downloadedCount} and skipped ${validFiles.length - downloadedCount} existing video subtitles`);
} catch (e) {
throw new Error(`Unable to download video subtitles. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
};
// Main: Download promo video
const downloadPromoVideo = async () => {
console.log(`${LOG_TAG} \tChecking if promo video needs downloading or updating...`);
try {
const contents = await fetchGitHubContents('/repos/mempool/mempool-promo/contents', !!config.githubToken);
const videoItem = contents.find(item => item.name === 'promo.mp4');
if (!videoItem) {
console.log(`${LOG_TAG} \tpromo.mp4 not found in repository`);
return;
}
await processFileItem(videoItem, {
filePath: `${ASSETS_PATH}/promo-video/mempool-promo.mp4`,
remoteHash: videoItem.sha,
downloadUrl: videoItem.download_url,
cdnPattern: {
from: "raw.githubusercontent.com/mempool/mempool-promo/master/promo.mp4",
to: "mempool.space/resources/promo-video/mempool-promo.mp4"
},
itemName: 'mempool-promo.mp4',
downloadDir: `${ASSETS_PATH}/promo-video/`
});
} catch (e) {
throw new Error(`Unable to download video. Trying again at next restart. Reason: ${e instanceof Error ? e.message : e}`);
}
};
// Download Liquid assets if configured
const downloadLiquidAssets = () => {
if (configContent.BASE_MODULE !== 'liquid') {
if (config.verbose) {
console.log(`${LOG_TAG} BASE_MODULE is not set to Liquid (currently ${configContent.BASE_MODULE}), skipping downloading assets`);
}
return;
}
const liquidAssets = [
{ file: 'assets.json', url: 'https://raw.githubusercontent.com/Blockstream/asset_registry_db/master/index.json' },
{ file: 'assets.minimal.json', url: 'https://raw.githubusercontent.com/Blockstream/asset_registry_db/master/index.minimal.json' },
{ file: 'assets-testnet.json', url: 'https://raw.githubusercontent.com/Blockstream/asset_registry_testnet_db/master/index.json' },
{ file: 'assets-testnet.minimal.json', url: 'https://raw.githubusercontent.com/Blockstream/asset_registry_testnet_db/master/index.minimal.json' }
];
console.log(`${LOG_TAG} Downloading assets`);
liquidAssets.forEach(({ file, url }) => {
const fileName = file.replace(/^assets/, 'assets');
console.log(`${LOG_TAG} Downloading ${fileName}`);
downloadFile(`${ASSETS_PATH}/${fileName}`, url);
});
};
// Main execution
(async () => {
try {
// Download Liquid assets (non-blocking)
downloadLiquidAssets();
// Download GitHub assets sequentially
if (config.verbose) {
console.log(`${LOG_TAG} Downloading mining pool logos`);
}
await downloadMiningPoolLogos();
if (config.verbose) {
console.log(`${LOG_TAG} Downloading promo video subtitles`);
}
await downloadPromoVideoSubtitles();
if (config.verbose) {
console.log(`${LOG_TAG} Downloading promo video`);
}
await downloadPromoVideo();
console.log(`${LOG_TAG} Asset synchronization complete`);
} catch (error) {
console.error(`${LOG_TAG} Error:`, error.message);
process.exit(1);
}
})();