forked from mtusi2026/mtusi2026lab6
13 lines
3.7 KiB
TypeScript
13 lines
3.7 KiB
TypeScript
import {readFile,writeFile,statfs} from 'node:fs/promises';import {resolve} from 'node:path';
|
|
const root=resolve(import.meta.dir,'..'),receipt=process.argv[2];if(!receipt||!resolve(receipt).startsWith(root+'/.local/oci-copy-'))throw new Error('own copy receipt required');
|
|
const copy=JSON.parse(await readFile(receipt,'utf8'));const stats=JSON.parse(await readFile(copy.statReceipt,'utf8'));const discovery=JSON.parse(await readFile(stats.discovery,'utf8'));const image=discovery.images.find((i:any)=>i.id==='openobserve');
|
|
const missing=stats.blobs.filter((b:any)=>b.images.includes('openobserve')&&!b.present);const cap=10_784_147;if(missing.reduce((s:number,b:any)=>s+b.size,0)!==cap)throw new Error('approved byte set differs');const fs=await statfs(copy.output);if(fs.bavail*fs.bsize<2_000_000_000)throw new Error('insufficient own filesystem space');
|
|
async function bounded(response:Response,max:number){if(!response.ok||!response.body)throw new Error('public registry HTTP request failed');const chunks:Uint8Array[]=[];let size=0;for await(const part of response.body){size+=part.length;if(size>max)throw new Error('public response exceeds cap');chunks.push(part);}const output=new Uint8Array(size);let offset=0;for(const c of chunks){output.set(c,offset);offset+=c.length;}return output;}
|
|
const tokenResponse=await bounded(await fetch('https://public.ecr.aws/token/?service=public.ecr.aws&scope=repository%3Azinclabs%2Fopenobserve%3Apull',{signal:AbortSignal.timeout(20_000)}),32_768);const token=JSON.parse(new TextDecoder().decode(tokenResponse)).token;if(typeof token!=='string'||token.length<16)throw new Error('public registry pull token missing');
|
|
const downloaded=[];let networkBytes=tokenResponse.length;
|
|
for(const blob of missing){if(!/^sha256:[a-f0-9]{64}$/.test(blob.digest)||blob.size<=0||blob.size>cap)throw new Error('invalid approved descriptor');const bytes=await bounded(await fetch('https://public.ecr.aws/v2/zinclabs/openobserve/blobs/'+blob.digest,{headers:{Authorization:'Bearer '+token},signal:AbortSignal.timeout(60_000)}),blob.size);if(bytes.length!==blob.size||'sha256:'+new Bun.CryptoHasher('sha256').update(bytes).digest('hex')!==blob.digest)throw new Error('public blob digest/size mismatch');await writeFile(resolve(copy.output,'blobs/sha256',blob.digest.slice(7)),bytes,{flag:'wx',mode:0o600});networkBytes+=bytes.length;downloaded.push({digest:blob.digest,size:bytes.length,shaVerified:true});}
|
|
const manifest=await readFile(resolve(discovery.output,'openobserve-manifest.json'));if('sha256:'+new Bun.CryptoHasher('sha256').update(manifest).digest('hex')!==image.manifestDigest)throw new Error('manifest mismatch');await writeFile(resolve(copy.output,'blobs/sha256',image.manifestDigest.slice(7)),manifest,{flag:'wx',mode:0o600});
|
|
// Read every descriptor again: a complete local set, not download count, permits import.
|
|
for(const d of [image.manifest.config,...image.manifest.layers]){const b=await readFile(resolve(copy.output,'blobs/sha256',d.digest.slice(7)));if(b.length!==d.size||'sha256:'+new Bun.CryptoHasher('sha256').update(b).digest('hex')!==d.digest)throw new Error('complete local descriptor verification failed');}
|
|
await writeFile(resolve(copy.output,'OPENOBSERVE-COMPLETE.json'),JSON.stringify({checkedAt:new Date().toISOString(),copyReceipt:receipt,manifestDigest:image.manifestDigest,configDigest:image.manifest.config.digest,compressedDescriptorBytes:image.declaredBytes,downloaded,networkBlobBytes:cap,networkMetadataBytes:tokenResponse.length,networkBytes,complete:true,imported:false},null,2)+'\n',{flag:'wx',mode:0o600});console.log(JSON.stringify({receipt:resolve(copy.output,'OPENOBSERVE-COMPLETE.json'),networkBlobBytes:cap,networkMetadataBytes:tokenResponse.length,complete:true,imported:false}));
|