MCPcopy Create free account
hub / github.com/callstack/agent-device / downloadRemoteArtifact

Function downloadRemoteArtifact

src/remote/daemon-artifacts.ts:347–435  ·  view source on GitHub ↗
(params: DownloadRemoteArtifactParams)

Source from the content-addressed store, hash-verified

345};
346
347export async function downloadRemoteArtifact(params: DownloadRemoteArtifactParams): Promise<void> {
348 const artifactUrl = new URL(buildDaemonArtifactUrl(params.baseUrl, params.artifactId));
349 const transport = artifactUrl.protocol === 'https:' ? https : http;
350 await fs.promises.mkdir(path.dirname(params.destinationPath), { recursive: true });
351 await new Promise<void>((resolve, reject) => {
352 let settled = false;
353 const timeoutMs = params.timeoutMs ?? REMOTE_ARTIFACT_DOWNLOAD_TIMEOUT_MS;
354 const settle = (error?: Error) => {
355 if (settled) return;
356 settled = true;
357 clearTimeout(timeoutHandle);
358 if (error) {
359 void fs.promises.rm(params.destinationPath, { force: true }).finally(() => reject(error));
360 return;
361 }
362 resolve();
363 };
364 const request = transport.request(
365 {
366 protocol: artifactUrl.protocol,
367 host: artifactUrl.hostname,
368 port: artifactUrl.port,
369 method: 'GET',
370 path: artifactUrl.pathname + artifactUrl.search,
371 headers: buildDaemonHttpAuthHeaders(params.token),
372 },
373 (res) => {
374 if ((res.statusCode ?? 500) >= 400) {
375 let body = '';
376 res.setEncoding('utf8');
377 res.on('data', (chunk) => {
378 body += chunk;
379 });
380 res.on('end', () => {
381 settle(
382 new AppError('COMMAND_FAILED', 'Failed to download remote artifact', {
383 artifactId: params.artifactId,
384 statusCode: res.statusCode,
385 requestId: params.requestId,
386 body,
387 }),
388 );
389 });
390 return;
391 }
392 res.on('aborted', () => {
393 settle(
394 new AppError('COMMAND_FAILED', 'Remote artifact download was interrupted', {
395 artifactId: params.artifactId,
396 requestId: params.requestId,
397 }),
398 );
399 });
400 void pipeline(res, fs.createWriteStream(params.destinationPath)).then(
401 () => settle(),
402 (error: unknown) => settle(error instanceof Error ? error : new Error(String(error))),
403 );
404 },

Callers 2

Calls 3

buildDaemonArtifactUrlFunction · 0.85
settleFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…