This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support to download and validate snapshot from url
- Loading branch information
1 parent
eb2009f
commit 75ecee9
Showing
5 changed files
with
204 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* LiskHQ/lisk-commander | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
* | ||
*/ | ||
import * as crypto from 'crypto'; | ||
import * as axios from 'axios'; | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import { extractTarBall } from './fs'; | ||
import { FileInfo } from '../types'; | ||
|
||
export const getDownloadedFileInfo = (url: string, downloadDir: string): FileInfo => { | ||
const pathWithoutProtocol = url.replace(/(^\w+:|^)\/\//, '').split('/'); | ||
const fileName = pathWithoutProtocol.pop() as string; | ||
const filePath = path.join(downloadDir, fileName); | ||
|
||
return { | ||
fileName, | ||
fileDir: downloadDir, | ||
filePath, | ||
}; | ||
}; | ||
|
||
export const download = async (url: string, dir: string): Promise<void> => { | ||
const { filePath, fileDir } = getDownloadedFileInfo(url, dir); | ||
|
||
if (fs.existsSync(filePath)) { | ||
fs.unlinkSync(filePath); | ||
} | ||
|
||
fs.ensureDirSync(fileDir); | ||
const writeStream = fs.createWriteStream(filePath); | ||
const response = await axios.default({ | ||
url, | ||
method: 'GET', | ||
responseType: 'stream', | ||
maxContentLength: 5000, | ||
}); | ||
|
||
response.data.pipe(writeStream); | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
writeStream.on('finish', resolve); | ||
writeStream.on('error', reject); | ||
}); | ||
}; | ||
|
||
export const verifyChecksum = async (filePath: string, expectedChecksum: string): Promise<void> => { | ||
const fileStream = fs.createReadStream(filePath); | ||
const dataHash = crypto.createHash('sha256'); | ||
const fileHash = await new Promise<Buffer>((resolve, reject) => { | ||
fileStream.on('data', (datum: Buffer) => { | ||
dataHash.update(datum); | ||
}); | ||
fileStream.on('error', error => { | ||
reject(error); | ||
}); | ||
fileStream.on('end', () => { | ||
resolve(dataHash.digest()); | ||
}); | ||
}); | ||
|
||
const fileChecksum = fileHash.toString('hex'); | ||
if (fileChecksum !== expectedChecksum) { | ||
throw new Error( | ||
`File checksum: ${fileChecksum} mismatched with expected checksum: ${expectedChecksum}`, | ||
); | ||
} | ||
}; | ||
|
||
export const getChecksum = (url: string, dir: string): string => { | ||
const { filePath } = getDownloadedFileInfo(url, dir); | ||
const content = fs.readFileSync(`${filePath}.SHA256`, 'utf8'); | ||
|
||
if (!content) { | ||
throw new Error(`Invalid filepath: ${filePath}`); | ||
} | ||
|
||
return content.split(' ')[0]; | ||
}; | ||
|
||
export const downloadAndValidate = async ( | ||
url: string, | ||
snapshotDirPath: string, | ||
extractSnapshotPath: string, | ||
): Promise<void> => { | ||
await download(url, snapshotDirPath); | ||
await download(`${url}.SHA256`, snapshotDirPath); | ||
const { filePath } = getDownloadedFileInfo(url, snapshotDirPath); | ||
const checksum = getChecksum(url, snapshotDirPath); | ||
await verifyChecksum(filePath, checksum); | ||
await extractTarBall(filePath, extractSnapshotPath); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters