import fs from 'node:fs/promises';import path from 'node:path';import crypto from 'node:crypto'; const base=new URL(process.argv[2]||'https://bhylu.com/examples/go/');if(!((base.protocol==='https:'&&base.hostname==='bhylu.com')||(base.protocol==='http:'&&['127.0.0.1','localhost'].includes(base.hostname)))||!base.pathname.endsWith('/examples/go/'))throw Error('Use Bhylu HTTPS or an explicit local /examples/go/ preview URL');const target=path.resolve('bhylu-go-examples');await fs.mkdir(target);const response=await fetch(new URL('files.json',base));if(!response.ok)throw Error('Source manifest unavailable');const manifest=await response.json();if(manifest.format!==1||!Array.isArray(manifest.files))throw Error('Unsupported manifest');const seen=new Set();for(const f of manifest.files){if(!/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(f.name)||f.url!==f.name+'.txt'||seen.has(f.name)||!/^[a-f0-9]{64}$/.test(f.sha256))throw Error('Invalid source entry');seen.add(f.name);const result=await fetch(new URL(f.url,base));if(!result.ok)throw Error('Missing '+f.name);const bytes=Buffer.from(await result.arrayBuffer());if(crypto.createHash('sha256').update(bytes).digest('hex')!==f.sha256)throw Error('Source hash mismatch');await fs.writeFile(path.join(target,f.name),bytes,{flag:'wx'});}await fs.writeFile(path.join(target,'files.json'),JSON.stringify(manifest,null,2),{flag:'wx'});console.log('Saved '+target+'. Review README.txt; execute individual sources with your separate Go installation (go run filename.go).');