MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / downloadHop

Function downloadHop

pkg/npm/install.js:230–310  ·  view source on GitHub ↗
(url, dest, loopback)

Source from the content-addressed store, hash-verified

228}
229
230function downloadHop(url, dest, loopback) {
231 return new Promise((resolve, reject) => {
232 let settled = false;
233 let response = null;
234 let timer = null;
235
236 function finish(err, result) {
237 if (settled) return;
238 settled = true;
239 if (timer) clearTimeout(timer);
240 if (err) reject(err);
241 else resolve(result);
242 }
243
244 // A loopback hop gets a fresh agent, so no proxy configuration applies.
245 const get = loopback
246 ? (target, callback) => http.get(target, { agent: false }, callback)
247 : (target, callback) => https.get(target, callback);
248 const req = get(url, (res) => {
249 response = res;
250 let piped = false;
251 // Before the body is piped, a response error ends the hop directly;
252 // afterwards the pipeline reports it, so the partial file is discarded
253 // before the hop settles.
254 res.on('error', (err) => { if (!piped) finish(err); });
255 const redirectCodes = new Set([301, 302, 303, 307, 308]);
256 if (redirectCodes.has(res.statusCode)) {
257 const location = res.headers.location;
258 if (loopback) {
259 res.destroy();
260 finish(new Error(`Refusing redirect from loopback download ${url}`));
261 return;
262 }
263 if (!location) {
264 res.destroy();
265 finish(new Error(`Redirect with no location for ${url}`));
266 return;
267 }
268 let next;
269 try {
270 next = validateUrl(new URL(location, url).href);
271 } catch (err) {
272 res.destroy();
273 finish(err);
274 return;
275 }
276 res.destroy();
277 finish(null, { redirect: next });
278 return;
279 }
280 if (res.statusCode !== 200) {
281 res.destroy();
282 finish(new Error(`HTTP ${res.statusCode} for ${url}`));
283 return;
284 }
285
286 const file = fs.createWriteStream(dest, { flags: 'w' });
287 piped = true;

Callers 1

downloadFunction · 0.85

Calls 5

validateUrlFunction · 0.85
discardFunction · 0.85
finishFunction · 0.70
getMethod · 0.65
getFunction · 0.50

Tested by

no test coverage detected