repair-direct-sync-runtime.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env node
  2. import {readFileSync,writeFileSync,mkdirSync,statSync} from 'node:fs';
  3. import {createHash} from 'node:crypto';
  4. import {join} from 'node:path';
  5. import {withXiaoshuRuntime} from './lib/xiaoshu-runtime-compat.mjs';
  6. const keyFile=process.env.XIAOSHU_MASTER_KEY_FILE;
  7. const token=process.env.XIAOSHU_SYNC_OPERATOR_TOKEN;
  8. const backupDir=process.env.XIAOSHU_DIRECT_DEPLOY_REPORT_DIR;
  9. if(!keyFile||!token||!backupDir)throw Error('Private key file, operator session, and backup directory required');
  10. if((statSync(keyFile).mode&0o077)!==0)throw Error('Master key file must be private');
  11. const key=readFileSync(keyFile,'utf8').trim();
  12. if(!key)throw Error('Master key file is empty');
  13. const base=(process.env.XIAOSHU_PARSE_URL||'https://server.xiaoshu.pro/parse').replace(/\/$/,'');
  14. const app=process.env.XIAOSHU_PARSE_APP_ID||'7pIbDBJmKx_main';
  15. const headers={'Content-Type':'application/json','X-Parse-Application-Id':app,'X-Parse-Master-Key':key};
  16. const hash=s=>createHash('sha256').update(s).digest('hex');
  17. async function api(path,method='GET',body){
  18. const response=await fetch(base+path,{method,headers,...(body?{body:JSON.stringify(body)}:{}),signal:AbortSignal.timeout(60000)});
  19. const result=await response.json();
  20. if(!response.ok||result.error)throw Error('Parse function API HTTP '+response.status);
  21. return result;
  22. }
  23. const name='xiaoshu.sync.direct-v1';
  24. const found=(await api('/classes/Function?'+new URLSearchParams({where:JSON.stringify({name}),limit:'2'}))).results;
  25. if(found.length!==1||found[0].path!=='xiaoshu/sync/direct-v1'||found[0].enabled!==true)throw Error('Unexpected direct sync function');
  26. const current=await api('/classes/Function/'+found[0].objectId);
  27. if(!current.code?.includes('await u.fetch({useMasterKey:true})'))throw Error('Unknown direct sync source');
  28. if(current.code.includes('XIAOSHU_ISOLATED_RUNTIME_COMPAT_V1'))throw Error('Runtime compatibility already installed');
  29. const updated=withXiaoshuRuntime(current.code);
  30. new Function(updated);
  31. mkdirSync(backupDir,{recursive:true,mode:0o700});
  32. if((statSync(backupDir).mode&0o077)!==0)throw Error('Backup directory must be private');
  33. const backup=join(backupDir,'function-before.json');
  34. writeFileSync(backup,JSON.stringify(current),{mode:0o600,flag:'wx'});
  35. let changed=false;
  36. try{
  37. await api('/classes/Function/'+current.objectId,'PUT',{code:updated});changed=true;
  38. const actual=await api('/classes/Function/'+current.objectId);
  39. if(actual.code!==updated)throw Error('Deployed function readback mismatch');
  40. const url=base.replace(/\/parse$/,'/api/functions/xiaoshu/sync/direct-v1');
  41. const response=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({token,params:{action:'status'}}),signal:AbortSignal.timeout(60000)});
  42. const result=await response.json();
  43. if(!response.ok||!result.success||result.data?.source!=='old-api/WXAPP'||result.data?.bridgeUsed!==false)throw Error('Direct sync status smoke failed');
  44. writeFileSync(join(backupDir,'function-after.json'),JSON.stringify(actual),{mode:0o600,flag:'wx'});
  45. console.log(JSON.stringify({deployed:true,before:hash(current.code),after:hash(updated),statusHttp:response.status,backup}));
  46. }catch(error){
  47. if(changed){
  48. await api('/classes/Function/'+current.objectId,'PUT',{code:current.code});
  49. const restored=await api('/classes/Function/'+current.objectId);
  50. if(restored.code!==current.code)throw Error('Direct sync rollback readback failed; backup at '+backup);
  51. }
  52. throw error;
  53. }