| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #!/usr/bin/env node
- import {readFileSync,writeFileSync,mkdirSync,statSync} from 'node:fs';
- import {createHash} from 'node:crypto';
- import {join} from 'node:path';
- import {withXiaoshuRuntime} from './lib/xiaoshu-runtime-compat.mjs';
- const keyFile=process.env.XIAOSHU_MASTER_KEY_FILE;
- const token=process.env.XIAOSHU_SYNC_OPERATOR_TOKEN;
- const backupDir=process.env.XIAOSHU_DIRECT_DEPLOY_REPORT_DIR;
- if(!keyFile||!token||!backupDir)throw Error('Private key file, operator session, and backup directory required');
- if((statSync(keyFile).mode&0o077)!==0)throw Error('Master key file must be private');
- const key=readFileSync(keyFile,'utf8').trim();
- if(!key)throw Error('Master key file is empty');
- const base=(process.env.XIAOSHU_PARSE_URL||'https://server.xiaoshu.pro/parse').replace(/\/$/,'');
- const app=process.env.XIAOSHU_PARSE_APP_ID||'7pIbDBJmKx_main';
- const headers={'Content-Type':'application/json','X-Parse-Application-Id':app,'X-Parse-Master-Key':key};
- const hash=s=>createHash('sha256').update(s).digest('hex');
- async function api(path,method='GET',body){
- const response=await fetch(base+path,{method,headers,...(body?{body:JSON.stringify(body)}:{}),signal:AbortSignal.timeout(60000)});
- const result=await response.json();
- if(!response.ok||result.error)throw Error('Parse function API HTTP '+response.status);
- return result;
- }
- const name='xiaoshu.sync.direct-v1';
- const found=(await api('/classes/Function?'+new URLSearchParams({where:JSON.stringify({name}),limit:'2'}))).results;
- if(found.length!==1||found[0].path!=='xiaoshu/sync/direct-v1'||found[0].enabled!==true)throw Error('Unexpected direct sync function');
- const current=await api('/classes/Function/'+found[0].objectId);
- if(!current.code?.includes('await u.fetch({useMasterKey:true})'))throw Error('Unknown direct sync source');
- if(current.code.includes('XIAOSHU_ISOLATED_RUNTIME_COMPAT_V1'))throw Error('Runtime compatibility already installed');
- const updated=withXiaoshuRuntime(current.code);
- new Function(updated);
- mkdirSync(backupDir,{recursive:true,mode:0o700});
- if((statSync(backupDir).mode&0o077)!==0)throw Error('Backup directory must be private');
- const backup=join(backupDir,'function-before.json');
- writeFileSync(backup,JSON.stringify(current),{mode:0o600,flag:'wx'});
- let changed=false;
- try{
- await api('/classes/Function/'+current.objectId,'PUT',{code:updated});changed=true;
- const actual=await api('/classes/Function/'+current.objectId);
- if(actual.code!==updated)throw Error('Deployed function readback mismatch');
- const url=base.replace(/\/parse$/,'/api/functions/xiaoshu/sync/direct-v1');
- const response=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({token,params:{action:'status'}}),signal:AbortSignal.timeout(60000)});
- const result=await response.json();
- if(!response.ok||!result.success||result.data?.source!=='old-api/WXAPP'||result.data?.bridgeUsed!==false)throw Error('Direct sync status smoke failed');
- writeFileSync(join(backupDir,'function-after.json'),JSON.stringify(actual),{mode:0o600,flag:'wx'});
- console.log(JSON.stringify({deployed:true,before:hash(current.code),after:hash(updated),statusHttp:response.status,backup}));
- }catch(error){
- if(changed){
- await api('/classes/Function/'+current.objectId,'PUT',{code:current.code});
- const restored=await api('/classes/Function/'+current.objectId);
- if(restored.code!==current.code)throw Error('Direct sync rollback readback failed; backup at '+backup);
- }
- throw error;
- }
|