import {readPressureRuntimeSource} from './read-pressure-runtime.mjs'; // The multi-app worker provides a deliberately small REST facade, not the Parse SDK. // Adapt only Xiaoshu gateway-local bindings inside its own isolated worker. export async function resolveXiaoshuRuntime(nativeParse, nativePsql, request, loadEnvironment) { if (typeof nativeParse.User === 'function' && typeof nativePsql.tx === 'function') { return { Parse: nativeParse, Psql: nativePsql }; } const load = loadEnvironment || (async () => { const { workerData } = await import('node:worker_threads'); const { createRequire } = await import('node:module'); const require = createRequire('/opt/future-server/package.json'); const SDK = require('parse/node'); const coreModule = require('parse/lib/node/CoreManager.js'); return { SDK, CoreManager: coreModule.default || coreModule, workerData }; }); const { SDK, CoreManager, workerData } = await load(); const db = nativePsql.db; if (workerData?.companyId !== '7pIbDBJmKx' || workerData.config?.appId !== '7pIbDBJmKx_main' || nativeParse.applicationId !== workerData.config.appId || !nativeParse.masterKey || nativeParse.masterKey !== workerData.config.masterKey || typeof workerData.token !== 'string' || !workerData.token || !/^http:\/\/127\.0\.0\.1:\d+\/data\/?$/.test(nativeParse.serverURL) || typeof db?.tx !== 'function') { throw new Error('Unsupported Xiaoshu isolated runtime'); } // One tenant per worker thread; these SDK settings never cross tenant workers. SDK.initialize(nativeParse.applicationId, undefined, nativeParse.masterKey); SDK.serverURL = nativeParse.serverURL; CoreManager.set('REQUEST_HEADERS', { 'x-fmode-worker-token': workerData.token }); CoreManager.set('REQUEST_ATTEMPT_LIMIT', 1); for (const [field, className] of [['user', '_User'], ['company', 'Company'], ['account', 'Account']]) { const value = request[field]; if (!value) continue; const json = typeof value.toJSON === 'function' ? value.toJSON() : value; if (!json.objectId) throw new Error('Invalid Xiaoshu runtime identity'); request[field] = SDK.Object.fromJSON({ ...json, className }); } return { Parse: SDK, Psql: db }; } export function withXiaoshuRuntime(code,options={}) { const marker = '// XIAOSHU_ISOLATED_RUNTIME_COMPAT_V1'; if (code.includes(marker)) return code; const pattern = /async function handler\s*\(/g; if ([...code.matchAll(pattern)].length !== 1) throw new Error('Expected exactly one async handler'); return code.replace(pattern, 'async function xiaoshuBusinessHandler(') + '\n' + marker + '\n' + resolveXiaoshuRuntime.toString() + '\n' + (options.readPressure ? readPressureRuntimeSource() : '') + ` async function handler(request, response) { try { const runtime = await resolveXiaoshuRuntime(Parse, Psql, request); Parse = runtime.Parse; Psql = runtime.Psql; } catch (error) { console.error('[XiaoshuRuntime] ' + String(error?.message || error)); return response.status(503).json({ success: false, error: 'runtime_compatibility_error', message: '小树服务暂时不可用,请稍后重试', retcode: -1, retmsg: '小树服务暂时不可用,请稍后重试', result: null }); } ${options.readPressure?`const pressureStartedAt=Date.now(); if(typeof response.on==='function')response.on('finish',()=>{const durationMs=Date.now()-pressureStartedAt;if(response.statusCode>=400||durationMs>=1000||Math.random()<0.01)console.info(JSON.stringify({event:'xiaoshu_read_pressure',operation:readPressurePolicy(${JSON.stringify(options.readPressure)},request.body?.params||{}).operation,status:response.statusCode,durationMs}));}); try { return await dispatchReadPressure(Psql,request,response,${JSON.stringify(options.readPressure)},async bounded=>{const original=Psql;Psql=bounded;try{return await xiaoshuBusinessHandler(request,response);}finally{Psql=original;}},async()=>{ const input=inputOf(request),context=await auth(request),payload=input.payload&&typeof input.payload==='object'?input.payload:{}; if(context.storeId)await prepareStoreRequest(context,input,payload); requireRole(context,['ops-manager','ops-staff','ops-auditor']); if(context.current.get('passwordResetRequired')===true)fail(403,'请先修改临时密码'); return{company:companyIdOf(context,input),scope:JSON.stringify([roleOf(context),context.storeId||0])}; }); }catch(error){ if(response.headersSent)return response; const status=Number(error.status||(error.code==='57014'?408:500)),message=status===429?'查询较多,请稍后重试':status===408?'查询暂未完成,请稍后重试':status<500?String(error.message||'请求失败'):'数据加载失败,请稍后重试'; return response.status(status).json({success:false,error:status===429?'read_capacity_busy':'read_failed',message,retcode:-1,retmsg:message,...(status===429?{retryAfterMs:2000}:{})}); }`:'return xiaoshuBusinessHandler(request, response);'} } `; }