|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import { createReadStream, createWriteStream } from 'node:fs'; |
| 3 | +import * as os from 'node:os'; |
| 4 | +import * as path from 'node:path'; |
| 5 | +import { Readable } from 'node:stream'; |
| 6 | +import { pipeline } from 'node:stream/promises'; |
| 7 | +import { createGunzip } from 'node:zlib'; |
| 8 | +import decompress from 'decompress'; |
| 9 | +import fetch from '../fetch.js'; |
| 10 | + |
| 11 | +// Add GitHub repository configuration via environment variables with defaults |
| 12 | +const GRAPH_NODE_GITHUB_OWNER = process.env.GRAPH_NODE_GITHUB_OWNER || 'graphprotocol'; |
| 13 | +const GRAPH_NODE_GITHUB_REPO = process.env.GRAPH_NODE_GITHUB_REPO || 'graph-node'; |
| 14 | + |
| 15 | +function getPlatformBinaryName(): string { |
| 16 | + const platform = os.platform(); |
| 17 | + const arch = os.arch(); |
| 18 | + |
| 19 | + if (platform === 'linux' && arch === 'x64') return 'gnd-linux-x86_64.gz'; |
| 20 | + if (platform === 'linux' && arch === 'arm64') return 'gnd-linux-aarch64.gz'; |
| 21 | + if (platform === 'darwin' && arch === 'x64') return 'gnd-macos-x86_64.gz'; |
| 22 | + if (platform === 'darwin' && arch === 'arm64') return 'gnd-macos-aarch64.gz'; |
| 23 | + if (platform === 'win32' && arch === 'x64') return 'gnd-windows-x86_64.exe.zip'; |
| 24 | + |
| 25 | + throw new Error(`Unsupported platform: ${platform} ${arch}`); |
| 26 | +} |
| 27 | + |
| 28 | +export async function getGlobalBinDir(): Promise<string> { |
| 29 | + const platform = os.platform(); |
| 30 | + let binDir: string; |
| 31 | + |
| 32 | + if (platform === 'win32') { |
| 33 | + // Prefer %USERPROFILE%\gnd\bin |
| 34 | + binDir = path.join(process.env.USERPROFILE || os.homedir(), 'gnd', 'bin'); |
| 35 | + } else { |
| 36 | + binDir = path.join(os.homedir(), '.local', 'bin'); |
| 37 | + } |
| 38 | + |
| 39 | + await fs.promises.mkdir(binDir, { recursive: true }); |
| 40 | + return binDir; |
| 41 | +} |
| 42 | + |
| 43 | +async function getLatestGithubRelease(owner: string, repo: string) { |
| 44 | + const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases/latest`); |
| 45 | + const data = await res.json(); |
| 46 | + return data.tag_name; |
| 47 | +} |
| 48 | + |
| 49 | +export async function getLatestGraphNodeRelease(): Promise<string> { |
| 50 | + return getLatestGithubRelease(GRAPH_NODE_GITHUB_OWNER, GRAPH_NODE_GITHUB_REPO); |
| 51 | +} |
| 52 | + |
| 53 | +export async function downloadGraphNodeRelease( |
| 54 | + release: string, |
| 55 | + outputDir: string, |
| 56 | + onProgress?: (downloaded: number, total: number | null) => void, |
| 57 | +): Promise<string> { |
| 58 | + const fileName = getPlatformBinaryName(); |
| 59 | + |
| 60 | + try { |
| 61 | + return await downloadGithubRelease( |
| 62 | + GRAPH_NODE_GITHUB_OWNER, |
| 63 | + GRAPH_NODE_GITHUB_REPO, |
| 64 | + release, |
| 65 | + outputDir, |
| 66 | + fileName, |
| 67 | + onProgress, |
| 68 | + ); |
| 69 | + } catch (e) { |
| 70 | + if (e === 404) { |
| 71 | + throw new Error( |
| 72 | + `Graph Node release ${release} does not exist, please check the release page for the correct release tag`, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + throw new Error(`Failed to download: ${release}`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +async function downloadGithubRelease( |
| 81 | + owner: string, |
| 82 | + repo: string, |
| 83 | + release: string, |
| 84 | + outputDir: string, |
| 85 | + fileName: string, |
| 86 | + onProgress?: (downloaded: number, total: number | null) => void, |
| 87 | +): Promise<string> { |
| 88 | + const url = `https://github.com/${owner}/${repo}/releases/download/${release}/${fileName}`; |
| 89 | + return downloadFile(url, path.join(outputDir, fileName), onProgress); |
| 90 | +} |
| 91 | + |
| 92 | +export async function downloadFile( |
| 93 | + url: string, |
| 94 | + outputPath: string, |
| 95 | + onProgress?: (downloaded: number, total: number | null) => void, |
| 96 | +): Promise<string> { |
| 97 | + return download(url, outputPath, onProgress); |
| 98 | +} |
| 99 | + |
| 100 | +export async function download( |
| 101 | + url: string, |
| 102 | + outputPath: string, |
| 103 | + onProgress?: (downloaded: number, total: number | null) => void, |
| 104 | +): Promise<string> { |
| 105 | + const res = await fetch(url); |
| 106 | + if (!res.ok || !res.body) { |
| 107 | + throw res.status; |
| 108 | + } |
| 109 | + |
| 110 | + const totalLength = Number(res.headers.get('content-length')) || null; |
| 111 | + let downloaded = 0; |
| 112 | + |
| 113 | + const fileStream = fs.createWriteStream(outputPath); |
| 114 | + const nodeStream = Readable.from(res.body); |
| 115 | + |
| 116 | + nodeStream.on('data', chunk => { |
| 117 | + downloaded += chunk.length; |
| 118 | + onProgress?.(downloaded, totalLength); |
| 119 | + }); |
| 120 | + |
| 121 | + nodeStream.pipe(fileStream); |
| 122 | + |
| 123 | + await new Promise<void>((resolve, reject) => { |
| 124 | + nodeStream.on('error', reject); |
| 125 | + fileStream.on('finish', resolve); |
| 126 | + fileStream.on('error', reject); |
| 127 | + }); |
| 128 | + |
| 129 | + return outputPath; |
| 130 | +} |
| 131 | + |
| 132 | +export async function extractGz(gzPath: string, outputPath?: string): Promise<string> { |
| 133 | + const outPath = outputPath || path.join(path.dirname(gzPath), path.basename(gzPath, '.gz')); |
| 134 | + |
| 135 | + await pipeline(createReadStream(gzPath), createGunzip(), createWriteStream(outPath)); |
| 136 | + |
| 137 | + return outPath; |
| 138 | +} |
| 139 | + |
| 140 | +export async function extractZipAndGetExe(zipPath: string, outputDir: string): Promise<string> { |
| 141 | + const files = await decompress(zipPath, outputDir); |
| 142 | + const exe = files.filter(file => file.path.endsWith('.exe')); |
| 143 | + |
| 144 | + if (exe.length !== 1) { |
| 145 | + throw new Error(`Expected 1 executable file in zip, got ${exe.length}`); |
| 146 | + } |
| 147 | + |
| 148 | + return path.join(outputDir, exe[0].path); |
| 149 | +} |
| 150 | + |
| 151 | +export async function moveFileToBinDir(srcPath: string, binDir?: string): Promise<string> { |
| 152 | + const targetDir = binDir || (await getGlobalBinDir()); |
| 153 | + const platform = os.platform(); |
| 154 | + const binaryName = platform === 'win32' ? 'gnd.exe' : 'gnd'; |
| 155 | + const destPath = path.join(targetDir, binaryName); |
| 156 | + await fs.promises.rename(srcPath, destPath); |
| 157 | + return destPath; |
| 158 | +} |
| 159 | + |
| 160 | +export async function moveFile(srcPath: string, destPath: string): Promise<string> { |
| 161 | + await fs.promises.rename(srcPath, destPath); |
| 162 | + return destPath; |
| 163 | +} |
0 commit comments