// Read-only; original state and identifiers remain in private storage. import { constants } from 'node:fs'; import { readFile, open } from 'node:fs/promises'; import { MAReadClient } from '../gateway/ma-client.mjs'; import { loadMAToken,loadCatalogKey } from '../gateway/private-config.mjs'; export async function liveClient(syntheticSequences = {}) { const config=JSON.parse(await readFile('.local/environment.json','utf8')); const prior=JSON.parse(await readFile('.local/ma-readonly-results.json','utf8')); if(prior.me.player_filter.length!==1 || prior.player.name!=='Adaptive Music DEV')throw Error('denied'); const catalogKey=await loadCatalogKey('.local'); const client=new MAReadClient({baseUrl:config.ma_url,token:await loadMAToken('.local'),target:prior.me.player_filter[0], approvedProviders:prior.me.provider_filter,syntheticSequences,catalogKey,campaign:'development',expectedName:'Adaptive Music DEV',expectedProvider:prior.player.provider}); return {client,catalogKey}; } export async function savePrivate(name,data) { const file=await open(`.local/${name}`,constants.O_WRONLY|constants.O_CREAT|constants.O_NOFOLLOW,0o600); try {const info=await file.stat();if(!info.isFile() || info.uid!==process.getuid() || (info.mode&0o777)!==0o600)throw Error('denied');await file.truncate(0);await file.writeFile(JSON.stringify(data,null,2)+'\n');await file.sync();}finally{await file.close();} } if(process.argv[1]?.endsWith('live_ma_preflight.mjs')) { try {const {client}=await liveClient();await client.verifyTarget();const q=await client.queueState();const choices=await client.catalog(); console.log(JSON.stringify({isolation_verified:true,state:q.state,items:q.items,shuffle:q.shuffle_enabled,playlists:choices.filter(c=>c.kind==='playlist').length})); } catch(error){console.log(JSON.stringify({isolation_verified:false,error:error.code??'unavailable',mutations_sent:0}));process.exitCode=1;} }