MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / selfHealBundle

Function selfHealBundle

scripts/npm-shim.js:112–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

110// Download + cache the platform bundle from GitHub Releases. Returns
111// {command, args}; exits the process with guidance if it can't.
112async function selfHealBundle() {
113 var version = readVersion();
114 var bundlesDir = path.join(process.env.CODEGRAPH_INSTALL_DIR || path.join(os.homedir(), '.codegraph'), 'bundles');
115 var dest = path.join(bundlesDir, target + '-' + version);
116
117 // Already downloaded by a previous run? Use it even when downloads are
118 // disabled — CODEGRAPH_NO_DOWNLOAD blocks fetching, not a cached bundle.
119 var cached = launcherIn(dest);
120 if (cached) { pruneOldBundles(bundlesDir, dest); return cached; }
121
122 if (process.env.CODEGRAPH_NO_DOWNLOAD) {
123 fail('the network fallback is disabled (CODEGRAPH_NO_DOWNLOAD is set).');
124 }
125
126 var asset = 'codegraph-' + target + (isWindows ? '.zip' : '.tar.gz');
127 var base = process.env.CODEGRAPH_DOWNLOAD_BASE || ('https://github.com/' + REPO + '/releases/download');
128 var url = base + '/v' + version + '/' + asset;
129
130 process.stderr.write(
131 'codegraph: platform bundle missing (registry did not provide ' + pkg + ').\n' +
132 'codegraph: downloading ' + asset + ' from GitHub Releases (' + version + ')...\n'
133 );
134
135 // Stage inside bundlesDir so the final rename is on the same filesystem (atomic,
136 // no EXDEV across tmpfs). Strip the archive's top-level codegraph-<target>/ dir.
137 fs.mkdirSync(bundlesDir, { recursive: true });
138 var stage = fs.mkdtempSync(path.join(bundlesDir, '.dl-'));
139 try {
140 var archivePath = path.join(stage, asset);
141 await download(url, archivePath, 6);
142 await verifyChecksum(archivePath, asset, base, version);
143 var extracted = path.join(stage, 'bundle');
144 fs.mkdirSync(extracted);
145 extract(archivePath, extracted);
146
147 var raced = launcherIn(dest); // another process may have finished meanwhile
148 if (raced) { rmrf(stage); return raced; }
149 try {
150 fs.renameSync(extracted, dest);
151 } catch (e) {
152 var other = launcherIn(dest); // lost the race but theirs is valid
153 if (other) { rmrf(stage); return other; }
154 throw e;
155 }
156 } catch (e) {
157 rmrf(stage);
158 fail('download failed (' + e.message + ').\n URL: ' + url);
159 }
160 rmrf(stage);
161
162 var ready = launcherIn(dest);
163 if (!ready) fail('downloaded bundle is missing its launcher under ' + dest + '.');
164 pruneOldBundles(bundlesDir, dest);
165 process.stderr.write('codegraph: bundle ready.\n');
166 return ready;
167}
168
169function readVersion() {

Callers 1

mainFunction · 0.85

Calls 10

readVersionFunction · 0.85
launcherInFunction · 0.85
pruneOldBundlesFunction · 0.85
verifyChecksumFunction · 0.85
rmrfFunction · 0.85
failFunction · 0.70
downloadFunction · 0.70
extractFunction · 0.70
writeMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected