MCPcopy
hub / github.com/entropic-dev/entropic / fetchObject

Function fetchObject

cli/lib/fetch-object.js:17–48  ·  view source on GitHub ↗
({ registry, cache }, integrity, load = false)

Source from the content-addressed store, hash-verified

15const EMPTY_BUF = Buffer.from([]);
16
17async function fetchObject({ registry, cache }, integrity, load = false) {
18 const parsed = ssri.parse(integrity);
19 const algo = parsed.pickAlgorithm();
20 const [{ digest }] = parsed[algo];
21
22 if (digest === EMPTY_HASH && algo === 'sha512') {
23 return load ? { data: EMPTY_BUF } : true;
24 }
25
26 if (await cacache.get.hasContent(cache, integrity)) {
27 return load ? cacache.get(cache, integrity) : true;
28 }
29
30 const response = await fetch(
31 `${registry}/v1/objects/object/${algo}/${encodeURIComponent(digest)}`
32 );
33
34 if (response.status > 399) {
35 throw new Error('error fetching object');
36 }
37
38 let destIntegrity = null;
39 const dest = cacache.put.stream(cache, integrity);
40 dest.on('integrity', i => (destIntegrity = i));
41 await pipeline(response.body, dest);
42
43 if (!parsed.match(destIntegrity)) {
44 throw new Error('file integrity mismatch!');
45 }
46
47 return load ? cacache.get(cache, integrity) : true;
48}

Callers 4

fetchPackageVersionFunction · 0.85
visitPackageFunction · 0.85
unfurlTreeFunction · 0.85
buildFromMetaFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected