import fs from 'node:fs/promises'; import crypto from 'node:crypto'; import {request,invoke} from '../server/scripts/platform-client.mjs'; const name='ksfResearchLibrary',route='/'+name,work='work/database-enrichment-20261110'; await fs.mkdir(work,{recursive:true}); const source=(await fs.readFile('server/src/research-library-bridge.mjs','utf8')).replace(/^export /gm,''); const entry=`\nasync function handler(request,response){const crypto=await import('node:crypto');const fs=await import('node:fs/promises');const cp=await import('node:child_process');return createResearchLibraryBridge({Parse,crypto,fs,spawn:cp.spawn,loadDatabase:async()=>{const m=await import('parse-server/lib/Config.js');const C=m.default?.default||m.default;const a=C.get('FKieRbIoGt_main').database?.adapter;if(a?.constructor?.name!=='PostgresStorageAdapter'||typeof a._client?.tx!=='function')throw Error('INCOMPATIBLE_DATABASE');return a._client;}})(request,response);}\n`; const code=source+entry;new Function(code); const sha=s=>crypto.createHash('sha256').update(s).digest('hex'); await fs.writeFile(work+'/research-library-bridge.js',code); const matches=(await request('GET','/classes/Function?'+new URLSearchParams({where:JSON.stringify({$or:[{name},{path:route}]}),limit:'3'}))).results; if(matches.length>1||matches.some(x=>x.name!==name||x.path!==route))throw Error('FUNCTION_CONFLICT'); if(matches.length&&matches[0].code!==code){const priorFile=work+'/bridge-deployed.json';const prior=JSON.parse(await fs.readFile(priorFile,'utf8'));if(prior.sha256!==sha(matches[0].code))throw Error('UNKNOWN_FUNCTION_BASELINE');} if(!process.argv.includes('--apply')){ console.log(JSON.stringify({ok:true,function:name,action:matches.length?'update':'create',sha256:sha(code),databaseWrites:0})); }else{ if(matches.length)await fs.writeFile(work+'/bridge-before-'+Date.now()+'.json',JSON.stringify(matches[0],null,2)); let id=matches[0]?.objectId; if(!id)id=(await request('POST','/classes/Function',{name,path:route,code,ACL:{}})).objectId; else if(matches[0].code!==code)await request('PUT','/classes/Function/'+id,{code}); const read=await request('GET','/classes/Function/'+id);if(read.code!==code||read.path!==route)throw Error('FUNCTION_READBACK_FAILED'); const receipt={ok:true,name,objectId:id,sha256:sha(code),updatedAt:new Date().toISOString(),businessDataWrites:0}; await fs.writeFile(work+'/bridge-deployed.json',JSON.stringify(receipt,null,2)); const probe=await invoke(name,{action:'probe'});await fs.writeFile(work+'/bridge-probe.json',JSON.stringify(probe,null,2)); console.log(JSON.stringify({deployment:receipt,probe})); }