fix: reconcile BPM sources and retain safe storage diagnostics
This commit is contained in:
1 parent
7e42a6dcc6
commit
33e9e9c108
9 files changed
+179
-40
No files matched your search
@@ -8,13 +8,20 @@ function todoArrival(row){
|
||||
return {...normalizeArrival({taskId:row.taskId,startTime:row.receivedAt,serialNum:row.serialNum,processName:row.processName,activityName:row.nodeName,processInstanceSubject:row.title},'todo:'+row.portalSource),kind:'todo',instanceId:String(row.instanceId),revision:row.revision,processVersion:row.processVersion||'',recordKey:JSON.stringify([row.instanceId,row.taskId,row.revision])};
|
||||
}
|
||||
class BpmArrivalMonitor{
|
||||
constructor({workspace,manager,scanner,provider,settings,hubClient=new BpmSignalRClient(),clock=()=>Date.now(),debounceMs=750,pollMs=5000,retryMs=[2000,5000,15000,60000]}){
|
||||
Object.assign(this,{workspace,manager,scanner,provider,settings,hubClient,clock,debounceMs,pollMs,retryMs});this.entries=new Map();this.todoPending=new Map();this.stopped=false;
|
||||
constructor({workspace,manager,scanner,provider,settings,onTodoReconciled,hubClient=new BpmSignalRClient(),clock=()=>Date.now(),debounceMs=750,pollMs=5000,retryMs=[2000,5000,15000,60000],sourceRetryMs=[2000,5000,15000,60000,300000]}){
|
||||
Object.assign(this,{workspace,manager,scanner,provider,settings,onTodoReconciled,hubClient,clock,debounceMs,pollMs,retryMs,sourceRetryMs});this.entries=new Map();this.todoPending=new Map();this.stopped=false;
|
||||
this.principal={user:{username:'系统监听',role:'admin'},sessionId:'monitor:'+crypto.randomUUID()};
|
||||
}
|
||||
allowed(){const config=this.scanner.store.config();return config.enabled&&dayPolicy(new Date(this.clock()),this.settings().schedule,this.workspace.snapshot().calendar.overrides).scan;}
|
||||
active(entry){return !this.stopped&&this.entries.get(entry.id)===entry&&this.workspace.getConnection(entry.id).status!=='disabled';}
|
||||
snapshot(id){const e=this.entries.get(id);return e?{status:e.status,lastSignalAt:e.lastSignalAt||null,lastSyncAt:e.lastSyncAt||null,lastFailureAt:e.lastFailureAt||null,nextRetryAt:Number.isFinite(e.nextRetry)?new Date(e.nextRetry).toISOString():null}: {status:this.workspace.getConnection(id).status==='disabled'||!this.scanner.store.config().enabled?'paused':'waiting',lastSignalAt:null,lastSyncAt:null};}
|
||||
active(entry){try{return !this.stopped&&this.entries.get(entry.id)===entry&&this.workspace.getConnection(entry.id).status!=='disabled';}catch{return false;}}
|
||||
source(entry,kind){return entry.sources[kind]||=( {status:'pending',failures:0,nextAttempt:0,lastSuccessAt:null,lastFailureAt:null} );}
|
||||
sourceSuccess(entry,kind){if(!entry||!this.active(entry))return;const source=this.source(entry,kind);Object.assign(source,{status:'healthy',failures:0,nextAttempt:0,lastSuccessAt:new Date(this.clock()).toISOString(),message:null});if(['todo','inform'].every(k=>this.source(entry,k).status==='healthy'))entry.lastSyncAt=new Date(this.clock()).toISOString();}
|
||||
sourceFailure(entry,kind,error){if(!entry||!this.active(entry))return;const source=this.source(entry,kind),delay=this.sourceRetryMs[Math.min(source.failures++,this.sourceRetryMs.length-1)];Object.assign(source,{status:'error',lastFailureAt:new Date(this.clock()).toISOString(),nextAttempt:this.clock()+delay,message:error?.status===401?'需要恢复BPM登录。':error?.status===403?'没有读取权限。':error?.status===409?'连接忙碌或已暂停。':'读取失败,等待重试。'});entry.lastFailureAt=source.lastFailureAt;entry.dirty.add(kind);}
|
||||
snapshot(id){const e=this.entries.get(id);if(e)return {status:e.status,lastSignalAt:e.lastSignalAt||null,lastSyncAt:e.lastSyncAt||null,lastFailureAt:e.lastFailureAt||null,nextRetryAt:Number.isFinite(e.nextRetry)?new Date(e.nextRetry).toISOString():null,nextReconcileAt:new Date(e.nextReconcile).toISOString(),sources:Object.fromEntries(['todo','inform'].map(kind=>{const s=this.source(e,kind);return [kind,{status:s.status,lastSuccessAt:s.lastSuccessAt,lastFailureAt:s.lastFailureAt,retryAt:s.nextAttempt?new Date(s.nextAttempt).toISOString():null,message:s.message||null}];}))};
|
||||
let status='removed';try{status=this.workspace.getConnection(id).status==='disabled'||!this.scanner.store.config().enabled?'paused':'waiting';}catch{}return {status,lastSignalAt:null,lastSyncAt:null,nextReconcileAt:null,sources:{}};
|
||||
}
|
||||
updateInterval(entry,config){if(entry.intervalMinutes===config.intervalMinutes)return;entry.intervalMinutes=config.intervalMinutes;entry.nextReconcile=(entry.lastReconcileAt??this.clock())+config.intervalMinutes*60000;}
|
||||
async reconfigure(){await this.tick();}
|
||||
start(){if(this.timer||this.stopped)return;this.timer=setInterval(()=>this.tick().catch(()=>{}),this.pollMs);this.timer.unref?.();this.tick().catch(()=>{});}
|
||||
async tick(){
|
||||
if(this.stopped)return;const config=this.scanner.store.config(),connections=this.workspace.snapshot().connections,ids=new Set(connections.map(c=>c.id));
|
||||
@@ -22,10 +29,11 @@ class BpmArrivalMonitor{
|
||||
if(!config.enabled)return;
|
||||
for(const c of connections){
|
||||
if(c.status==='disabled'||c.status==='disconnected'&&!c.lastConnectedAt)continue;
|
||||
let entry=this.entries.get(c.id);if(!entry){entry={id:c.id,status:'connecting',nextRetry:0,nextReconcile:this.clock()+config.intervalMinutes*60000,failures:0,dirty:new Set(),controller:new AbortController(),attempt:0};this.entries.set(c.id,entry);}
|
||||
let entry=this.entries.get(c.id);if(!entry){entry={id:c.id,status:'connecting',nextRetry:0,nextReconcile:this.clock()+config.intervalMinutes*60000,lastReconcileAt:this.clock(),intervalMinutes:config.intervalMinutes,sources:{},failures:0,dirty:new Set(),controller:new AbortController(),attempt:0};this.entries.set(c.id,entry);}
|
||||
this.updateInterval(entry,config);
|
||||
if(entry.handle&&entry.epoch!==(this.manager.epoch?.(c.id)||0)){await this.stopConnection(c.id);continue;}
|
||||
if(!entry.handle&&!entry.connecting&&this.clock()>=entry.nextRetry)this.connect(entry,c);
|
||||
if(this.clock()>=entry.nextReconcile){entry.nextReconcile=this.clock()+config.intervalMinutes*60000;this.queue(entry,['todo','inform']);}
|
||||
if(this.clock()>=entry.nextReconcile){entry.nextReconcile=this.clock()+config.intervalMinutes*60000;for(const kind of ['todo','inform'])this.source(entry,kind).nextAttempt=0;this.queue(entry,['todo','inform']);}
|
||||
else if(entry.dirty.size&&!entry.sync&&!entry.debounce)this.queue(entry,[]);
|
||||
}
|
||||
}
|
||||
@@ -42,30 +50,35 @@ class BpmArrivalMonitor{
|
||||
return this.hubClient.connect(saved?.storageState,{signal:entry.controller.signal,onEvent:name=>{if(!this.active(entry)||entry.attempt!==attempt||entry.epoch!==(this.manager.epoch?.(entry.id)||0))return;entry.lastSignalAt=new Date(this.clock()).toISOString();this.queue(entry,name==='worklistChanged'?['todo']:name==='informlistChanged'?['inform']:['todo','inform']);},onClose:()=>{closed=true;if(this.active(entry)&&entry.attempt===attempt){entry.handle=null;this.retry(entry);}}});
|
||||
});
|
||||
if(!handle)return;if(!this.active(entry)||closed){await handle.stop();return;}
|
||||
entry.handle=handle;entry.connectedAt=this.clock();entry.status='listening';entry.nextRetry=Infinity;entry.nextReconcile=this.clock()+this.scanner.store.config().intervalMinutes*60000;this.queue(entry,['todo','inform']);
|
||||
entry.handle=handle;entry.connectedAt=this.clock();entry.status='listening';entry.nextRetry=Infinity;entry.lastReconcileAt=this.clock();entry.nextReconcile=this.clock()+this.scanner.store.config().intervalMinutes*60000;this.queue(entry,['todo','inform']);
|
||||
})().catch(error=>{if(!this.active(entry))return;if(error.status===401)this.manager.observe?.(entry.id,'portal',error);this.retry(entry,error.status===409?'busy':error.status===401?'login_required':'polling',error.status===409?5000:error.status===401?5*60000:undefined);if(![401,409].includes(error.status))this.queue(entry,['todo','inform']);}).finally(()=>{entry.connecting=null;});
|
||||
}
|
||||
queue(entry,kinds){if(!this.active(entry))return;for(const kind of kinds)entry.dirty.add(kind);if(entry.sync||entry.debounce||!entry.dirty.size||!this.allowed()||this.manager.sessions.has(entry.id))return;
|
||||
entry.debounce=setTimeout(()=>{entry.debounce=null;if(!this.active(entry)||!this.allowed())return;const kinds=[...entry.dirty];entry.dirty.clear();entry.sync=this.synchronize(entry,kinds).finally(()=>{entry.sync=null;if(entry.dirty.size&&this.active(entry))this.queue(entry,[]);});},this.debounceMs);entry.debounce.unref?.();
|
||||
dueKinds(entry){return [...entry.dirty].filter(kind=>this.clock()>=this.source(entry,kind).nextAttempt);}
|
||||
queue(entry,kinds){if(!this.active(entry))return;for(const kind of kinds)entry.dirty.add(kind);if(entry.sync||entry.debounce||!this.dueKinds(entry).length||!this.allowed()||this.manager.sessions.has(entry.id))return;
|
||||
entry.debounce=setTimeout(()=>{entry.debounce=null;if(!this.active(entry)||!this.allowed())return;const kinds=this.dueKinds(entry);for(const kind of kinds)entry.dirty.delete(kind);entry.sync=this.synchronize(entry,kinds).finally(()=>{entry.sync=null;if(entry.dirty.size&&this.active(entry))this.queue(entry,[]);});},this.debounceMs);entry.debounce.unref?.();
|
||||
}
|
||||
async synchronize(entry,kinds){let valid=true;
|
||||
for(const kind of kinds){if(!this.active(entry))return;
|
||||
try{
|
||||
if(kind==='inform'){await this.scanner.scan(entry.id,this.principal);}
|
||||
if(kind==='inform'){this.source(entry,kind).status='running';await this.scanner.scan(entry.id,this.principal);this.sourceSuccess(entry,kind);}
|
||||
else await this.startTodoScan(entry.id,this.principal).promise;
|
||||
}catch(error){valid=false;entry.lastFailureAt=new Date(this.clock()).toISOString();}
|
||||
}catch(error){valid=false;if(kind==='inform')this.sourceFailure(entry,kind,error);else if(this.active(entry)){entry.dirty.add(kind);const source=this.source(entry,kind);if(!source.nextAttempt)source.nextAttempt=this.clock()+this.sourceRetryMs[0];}}
|
||||
}
|
||||
if(valid&&this.active(entry))entry.lastSyncAt=new Date(this.clock()).toISOString();
|
||||
if(valid&&this.active(entry)&&kinds.includes('todo')&&kinds.includes('inform')){entry.lastReconcileAt=this.clock();entry.nextReconcile=this.clock()+entry.intervalMinutes*60000;}
|
||||
}
|
||||
startTodoScan(id,principal){
|
||||
const fail=(message,status)=>{throw Object.assign(new Error(message),{status});};if(this.stopped)fail('监听服务正在停止。',503);
|
||||
const connection=this.workspace.getConnection(id);if(!principal?.sessionId||!['admin','operator'].includes(principal.user?.role)||principal.user.role!=='admin'&&connection.owner!==principal.user.username)fail('仅连接负责人或管理员可以同步。',403);
|
||||
if(connection.status==='disabled')fail('此连接已停用。',409);if(this.todoPending.has(id))fail('待办对账正在运行。',409);
|
||||
const runId=this.scanner.store.begin(id,principal.user.username,'todo');
|
||||
const runId=this.scanner.store.begin(id,principal.user.username,'todo'),startedAt=new Date(this.clock()).toISOString(),epoch=this.manager.epoch?.(id)||0,entry=this.entries.get(id);
|
||||
if(entry)this.source(entry,'todo').status='running';
|
||||
const promise=Promise.resolve().then(async()=>{
|
||||
try{let rows;try{rows=await this.provider.scan(connection,principal);}catch(error){if(error.status!==401)throw error;const result=await this.manager.ensure(connection,principal);if(result.status!=='connected')throw error;rows=await this.provider.scan(connection,principal);}
|
||||
const current=this.workspace.getConnection(id);if(this.stopped||current.status==='disabled')fail('连接已暂停,本次对账未提交。',409);return {...this.scanner.store.complete(runId,id,rows.map(todoArrival),current),runId};
|
||||
}catch(error){this.scanner.store.fail(runId,error.status===401?'需要恢复BPM登录。':error.status===409?'连接忙碌或已暂停。':'待办对账失败,请稍后重试。');throw error;}
|
||||
const current=this.workspace.getConnection(id);if(this.stopped||current.status==='disabled'||epoch!==(this.manager.epoch?.(id)||0))fail('连接已暂停或身份已变化,本次对账未提交。',409);
|
||||
const result=this.scanner.store.complete(runId,id,rows.map(todoArrival),current),finishedAt=new Date(this.clock()).toISOString();
|
||||
if(this.onTodoReconciled)await this.onTodoReconciled({connectionId:id,runId,rows:structuredClone(rows),startedAt,finishedAt});
|
||||
this.sourceSuccess(entry,'todo');return {...result,runId};
|
||||
}catch(error){this.scanner.store.fail(runId,error.status===401?'需要恢复BPM登录。':error.status===409?'连接忙碌或已暂停。':'待办对账失败,请稍后重试。');this.sourceFailure(entry,'todo',error);throw error;}
|
||||
}).finally(()=>this.todoPending.delete(id));this.todoPending.set(id,promise);return {runId,promise};
|
||||
}
|
||||
async stopConnection(id){const entry=this.entries.get(id);if(!entry){await this.todoPending.get(id)?.catch(()=>{});return;}this.entries.delete(id);entry.controller.abort();clearTimeout(entry.debounce);await Promise.allSettled([entry.handle?.stop(),entry.connecting,entry.sync,this.todoPending.get(id)]);}
|
||||
|
||||
@@ -16,7 +16,7 @@ async function bpmScanApi({scanner,workspace,bpmMonitor,orderRules,user,sessionI
|
||||
if(route==='/api/bpm-scan'&&method==='GET'){reply(200,{...scanner.store.status(),connections:workspace.snapshot().connections.map(({id,name,status,owner})=>({id,name,status,owner,monitoring:bpmMonitor?.snapshot(id)}))});return true;}
|
||||
if(route==='/api/bpm-scan/config'&&method==='POST'){
|
||||
if(user.role!=='admin')fail('仅管理员可以配置自动扫描。',403);
|
||||
const config=scanner.store.saveConfig(body);workspace.record(user,'保存自动扫描',`${config.enabled?'开启':'暂停'},间隔 ${config.intervalMinutes} 分钟`);reply(200,config);return true;
|
||||
const config=scanner.store.saveConfig(body);await bpmMonitor?.reconfigure?.();workspace.record(user,'保存自动扫描',`${config.enabled?'开启':'暂停'},间隔 ${config.intervalMinutes} 分钟`);reply(200,config);return true;
|
||||
}
|
||||
if(route==='/api/bpm-scan/run'&&method==='POST'){
|
||||
if(!body||Object.keys(body).some(k=>!['connectionId','kind'].includes(k))||typeof body.connectionId!=='string'||body.kind!==undefined&&!['todo','inform'].includes(body.kind))fail('请选择 BPM 连接及有效到达类型。');
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
'use strict';
|
||||
const fs=require('node:fs'),path=require('node:path'),{DatabaseSync}=require('node:sqlite'),{readSupervisor}=require('../../platform/service/control.cjs'),{redact}=require('../../shared/redact.cjs');
|
||||
const {monitorEventLoopDelay}=require('node:perf_hooks');
|
||||
function timestamp(value){if(typeof value!=='string')return null;const normalized=/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/.test(value)?value.replace(' ','T')+'Z':value;const n=Date.parse(normalized);return Number.isFinite(n)?new Date(n).toISOString():null;}
|
||||
const latest=(...values)=>values.map(timestamp).filter(Boolean).sort().at(-1)||null;
|
||||
const groupStatus=items=>!items.length?'unconfigured':['error','unknown','running','pending','healthy','paused','disabled'].find(s=>items.some(i=>i.status===s))||'pending';
|
||||
class RuntimeHealth {
|
||||
constructor({stateDir,startedAt=new Date().toISOString(),instanceId='',...sources}){
|
||||
constructor({stateDir,startedAt=new Date().toISOString(),instanceId='',clock=()=>Date.now(),...sources}){
|
||||
try {
|
||||
Object.assign(this,{stateDir,startedAt,instanceId,sources});const dir=path.join(stateDir,'health');fs.mkdirSync(dir,{recursive:true,mode:0o700});this.db=new DatabaseSync(path.join(dir,'health.sqlite'));
|
||||
Object.assign(this,{stateDir,startedAt,instanceId,sources,clock});const dir=path.join(stateDir,'health');fs.mkdirSync(dir,{recursive:true,mode:0o700});this.db=new DatabaseSync(path.join(dir,'health.sqlite'));
|
||||
this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=2000;
|
||||
CREATE TABLE IF NOT EXISTS observations(kind TEXT NOT NULL,id TEXT NOT NULL,last_success TEXT,last_failure TEXT,PRIMARY KEY(kind,id));`);
|
||||
} catch(error) {
|
||||
@@ -29,9 +30,20 @@ class RuntimeHealth {
|
||||
});});
|
||||
safe('scan','BPM 扫描','#records',()=>{
|
||||
const {config={},runs=[]}=this.sources.scanner?.store.status()||{};
|
||||
for(const r of runs)this.remember('scan',r.connectionId,{successAt:r.status==='success'?r.finishedAt:null,failureAt:['error','interrupted'].includes(r.status)?r.finishedAt:null});
|
||||
return connections.filter(c=>c.status!=='disabled').map(c=>{const run=runs.find(r=>r.connectionId===c.id),status=run?.status==='running'?'running':config.enabled===false?'paused':!run?'pending':run.status==='success'?'healthy':'error';return {id:c.id,name:redact(c.name||'BPM 连接'),owner:c.owner,status,checkedAt:timestamp(run?.finishedAt||run?.startedAt),detail:config.enabled===false?'定时扫描已暂停,保留最近运行记录。':run?.message?redact(run.message):run?.status==='success'?`最近扫描 ${run.seen||0} 条,新增 ${run.added||0} 条。`:'等待扫描结果。',...this.remember('scan',c.id)};});
|
||||
for(const r of runs)this.remember('scan',r.connectionId+':'+(r.kind||'inform'),{successAt:r.status==='success'?r.finishedAt:null,failureAt:['error','interrupted'].includes(r.status)?r.finishedAt:null});
|
||||
return connections.filter(c=>c.status!=='disabled').flatMap(c=>['todo','inform'].map(kind=>{
|
||||
const id=c.id+':'+kind,run=runs.find(r=>r.connectionId===c.id&&(r.kind||'inform')===kind),history=this.remember('scan',id),checkedAt=timestamp(run?.finishedAt||run?.startedAt)||latest(history.lastSuccessAt,history.lastFailureAt);
|
||||
const failed=history.lastFailureAt&&(!history.lastSuccessAt||Date.parse(history.lastFailureAt)>Date.parse(history.lastSuccessAt));
|
||||
const stale=!!checkedAt&&this.clock()-Date.parse(checkedAt)>Math.max(60000,(config.intervalMinutes||5)*120000+30000);
|
||||
const status=config.enabled===false?'paused':run?.status==='running'?'running':failed||run&&['error','interrupted'].includes(run.status)?'error':!history.lastSuccessAt||stale?'pending':'healthy';
|
||||
const label=kind==='todo'?'待办':'待阅',detail=config.enabled===false?'自动同步已暂停,保留最近观测。':status==='error'?redact(run?.message||label+'上次读取失败,等待重试。'):stale?'最近成功已超过完整对账周期,等待核实。':history.lastSuccessAt?`最近读取 ${run?.seen||0} 条;结果仅代表最近对账。`:'等待首次完整对账。';
|
||||
return {id,connectionId:c.id,kind,name:redact(c.name||'BPM 连接')+' · '+label,owner:c.owner,status,stale,checkedAt,detail,...history};
|
||||
}));
|
||||
});
|
||||
if(this.sources.monitor)safe('listener','BPM 到达监听','#bpm',()=>connections.filter(c=>c.status!=='disabled').map(c=>{
|
||||
const observed=this.sources.monitor.snapshot(c.id),status=observed.status==='listening'?'healthy':['login_required','error'].includes(observed.status)?'error':observed.status==='paused'?'paused':'pending';
|
||||
return {id:c.id,owner:c.owner,name:redact(c.name||'BPM 连接'),status,checkedAt:timestamp(observed.lastSignalAt||observed.lastSyncAt),nextReconcileAt:timestamp(observed.nextReconcileAt),detail:status==='healthy'?'推送已连接;实际读取结果请查看待办/待阅对账。':status==='error'?'监听不可用,请核对连接;完整对账继续作为补偿。':'等待监听或通过完整对账发现到达。'};
|
||||
}));
|
||||
safe('storage','SMB 存储','#storage',()=> (this.sources.storage?.store.servers()||[]).map(s=>{
|
||||
const checkedAt=timestamp(s.state?.checkedAt),status=!s.enabled?'disabled':!checkedAt?'pending':s.state.readable&&s.state.writable?'healthy':s.state.readable?'pending':'error';
|
||||
return {id:s.id,name:redact(s.name||'存储服务器'),status,checkedAt,detail:redact(s.state?.message||'尚未检查连接。'),...this.remember('storage',s.id,{successAt:status==='healthy'?checkedAt:null,failureAt:status==='error'?checkedAt:null})};
|
||||
@@ -50,14 +62,15 @@ class RuntimeHealth {
|
||||
return groups;
|
||||
}
|
||||
snapshot(user){
|
||||
const now=Date.now(),groups=this.collect().map(g=>{if(!['bpm','scan','execution'].includes(g.key)||user.role==='admin'||g.status==='unknown')return g;const items=g.items.filter(i=>i.global||i.owner===user.username);return {...g,items,status:groupStatus(items)};});
|
||||
const now=this.clock(),groups=this.collect().map(g=>{if(!['bpm','scan','listener','execution'].includes(g.key)||user.role==='admin'||g.status==='unknown')return g;const items=g.items.filter(i=>i.global||i.owner===user.username);return {...g,items,status:groupStatus(items)};});
|
||||
let saved;try{saved=readSupervisor(this.stateDir);}catch{saved={state:null,events:[],autostart:null};}
|
||||
const s=saved.state,managed=Boolean(this.instanceId&&s?.childInstanceId===this.instanceId),fresh=managed&&now-Date.parse(s.updatedAt)<45000;
|
||||
const guard=managed?{status:fresh?s.status:'stale',heartbeatAt:s.heartbeatAt,updatedAt:s.updatedAt,restartCount:s.restartCount,childStartedAt:s.childStartedAt,reason:s.reason,nextRestartAt:s.nextRestartAt,events:saved.events.map(e=>({at:e.at,kind:e.kind,code:e.detail.code,delayMs:e.detail.delayMs,reason:e.detail.reason}))}:{status:'unmanaged',restartCount:0,events:[]};
|
||||
const autostart={enabled:!!(saved.autostart?.enabled&&saved.autostart?.linkPath&&fs.existsSync(saved.autostart.linkPath)),method:'current_user_logon',installedAt:saved.autostart?.installedAt||null};
|
||||
return {at:new Date(now).toISOString(),runtime:{startedAt:this.startedAt,uptimeSeconds:Math.max(0,Math.floor((now-Date.parse(this.startedAt))/1000)),managed,guard,autostart},groups};
|
||||
const eventLoop={observed:!!this.loopDelay,since:this.loopDelay?this.loopObservedAt:null,maxMs:this.loopDelay?Math.round(this.loopDelay.max/1e4)/100:0,meanMs:this.loopDelay&&Number.isFinite(this.loopDelay.mean)?Math.round(this.loopDelay.mean/1e4)/100:0};
|
||||
return {at:new Date(now).toISOString(),runtime:{startedAt:this.startedAt,uptimeSeconds:Math.max(0,Math.floor((now-Date.parse(this.startedAt))/1000)),managed,guard,autostart,eventLoop},groups};
|
||||
}
|
||||
start(){if(this.timer)return;const observe=()=>{try{this.collect();}catch{}};observe();this.timer=setInterval(observe,15000);this.timer.unref?.();}
|
||||
close(){clearInterval(this.timer);this.timer=null;this.db?.close();this.db=null;}
|
||||
start(){if(this.timer)return;this.loopDelay=monitorEventLoopDelay({resolution:20});this.loopDelay.enable();this.loopObservedAt=new Date(this.clock()).toISOString();const observe=()=>{try{this.collect();}catch{}};observe();this.timer=setInterval(observe,15000);this.timer.unref?.();}
|
||||
close(){clearInterval(this.timer);this.timer=null;this.loopDelay?.disable();this.loopDelay=null;this.db?.close();this.db=null;}
|
||||
}
|
||||
module.exports={RuntimeHealth,timestamp};
|
||||
@@ -5,9 +5,11 @@ const path = require('node:path');
|
||||
const crypto = require('node:crypto');
|
||||
const { execFileSync } = require('node:child_process');
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const fail = () => Object.assign(new Error('BPM 会话存储不可用,请重新登录或联系管理员。'), { status: 503 });
|
||||
const fail = (cause,stage) => cause?.code==='STORAGE_UNAVAILABLE'&&!stage?cause:Object.assign(new Error('加密存储暂时不可用,请查看系统诊断。',cause?{cause}:undefined), { status: 503,code:'STORAGE_UNAVAILABLE',stage:stage||'db_read' });
|
||||
const duplicate = () => Object.assign(new Error('该 BPM 账号已开启自动续登,请使用已有连接或先解除原账号托管。'), { status: 409, code: 'BPM_ACCOUNT_DUPLICATE' });
|
||||
const digest = value => crypto.createHash('sha256').update(value).digest('hex');
|
||||
const securedDirectories=new Map();
|
||||
function directoryIdentity(dir){const info=fs.lstatSync(dir,{bigint:true});if(!info.isDirectory()||info.isSymbolicLink())throw Object.assign(Error('Storage directory must be a real directory'),{code:'ESTORAGEPATH'});return [info.dev,info.ino,info.birthtimeNs].join(':');}
|
||||
|
||||
function powershell(script, input) {
|
||||
const utf8='[Console]::InputEncoding=New-Object System.Text.UTF8Encoding; [Console]::OutputEncoding=New-Object System.Text.UTF8Encoding; ';
|
||||
@@ -18,7 +20,11 @@ function powershell(script, input) {
|
||||
function secureDirectory(dir) {
|
||||
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
if (process.platform === 'win32') {
|
||||
const key=path.resolve(dir).toLowerCase(),identity=directoryIdentity(dir);
|
||||
if(securedDirectories.get(key)===identity)return;
|
||||
powershell('$ErrorActionPreference="Stop"; $p=[Console]::In.ReadToEnd(); $s=[System.Security.Principal.WindowsIdentity]::GetCurrent().User; $a=New-Object System.Security.AccessControl.DirectorySecurity; $a.SetAccessRuleProtection($true,$false); $a.SetOwner($s); $r=New-Object System.Security.AccessControl.FileSystemAccessRule($s,"FullControl","ContainerInherit,ObjectInherit","None","Allow"); $a.AddAccessRule($r); [System.IO.Directory]::SetAccessControl($p,$a)', dir);
|
||||
if(directoryIdentity(dir)!==identity)throw Object.assign(Error('Storage directory changed during protection'),{code:'ESTORAGECHANGED'});
|
||||
securedDirectories.set(key,identity);if(securedDirectories.size>512)securedDirectories.delete(securedDirectories.keys().next().value);
|
||||
} else fs.chmodSync(dir, 0o700);
|
||||
}
|
||||
function dpapi(value, decrypt = false) {
|
||||
@@ -31,10 +37,11 @@ class BpmVault {
|
||||
try {
|
||||
this.stateDir = path.resolve(stateDir);
|
||||
secureDirectory(this.stateDir);
|
||||
} catch { throw fail(); }
|
||||
} catch(cause) { throw fail(cause,'directory_permissions'); }
|
||||
}
|
||||
ensureKey() {
|
||||
if (this.key) return this.key;
|
||||
let stage='key_directory';
|
||||
try {
|
||||
// The key always stays on the service user's local disk, even when project code is on a shared drive.
|
||||
const keyDir = process.platform === 'win32'
|
||||
@@ -43,17 +50,17 @@ class BpmVault {
|
||||
secureDirectory(keyDir);
|
||||
const keyFile = path.join(keyDir, digest(this.stateDir.toLowerCase()) + '.key');
|
||||
if (!fs.existsSync(keyFile)) {
|
||||
stage='key_create';
|
||||
const raw = crypto.randomBytes(32);
|
||||
const protectedKey = process.platform === 'win32' ? dpapi(raw) : raw;
|
||||
try { fs.writeFileSync(keyFile, protectedKey, { flag: 'wx', mode: 0o600 }); }
|
||||
catch (error) { if (error.code !== 'EEXIST') throw error; }
|
||||
try {stage='key_protect';const protectedKey = process.platform === 'win32' ? dpapi(raw) : raw;stage='key_write';try{fs.writeFileSync(keyFile, protectedKey, { flag: 'wx', mode: 0o600 });}catch(error){if(error.code!=='EEXIST')throw error;}}
|
||||
finally { raw.fill(0); }
|
||||
}
|
||||
const protectedKey = fs.readFileSync(keyFile);
|
||||
stage='key_read';const protectedKey = fs.readFileSync(keyFile);
|
||||
stage='key_unprotect';
|
||||
this.key = process.platform === 'win32' ? dpapi(protectedKey, true) : protectedKey;
|
||||
if (this.key.length !== 32) throw fail();
|
||||
stage='key_length';if (this.key.length !== 32) throw Object.assign(Error('Invalid key length'),{code:'ESTORAGEKEY'});
|
||||
return this.key;
|
||||
} catch { throw fail(); }
|
||||
} catch(cause) { this.key?.fill(0);this.key=null;throw fail(cause,stage); }
|
||||
}
|
||||
filename(id) {
|
||||
if (typeof id !== 'string' || !id || id.length > 200) throw Object.assign(new Error('连接标识无效。'), { status: 400 });
|
||||
@@ -65,7 +72,7 @@ class BpmVault {
|
||||
try{db.exec(`PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA secure_delete=ON;
|
||||
CREATE TABLE IF NOT EXISTS accounts(connection_id TEXT PRIMARY KEY,username_key TEXT NOT NULL UNIQUE,payload BLOB NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS sessions(connection_id TEXT PRIMARY KEY,payload BLOB NOT NULL);`);this.db=db;return db;}
|
||||
catch{db.close();throw fail();}
|
||||
catch(cause){db.close();throw fail(cause,'db_open');}
|
||||
}
|
||||
accountKey(username) {
|
||||
if(typeof username!=='string'||!username.trim()||username.length>150)throw Object.assign(new Error('请输入有效的 BPM 账号。'),{status:400});
|
||||
@@ -85,11 +92,11 @@ class BpmVault {
|
||||
}
|
||||
putAccount(id,state) {
|
||||
try{this.accounts().prepare('INSERT INTO accounts(connection_id,username_key,payload) VALUES(?,?,?) ON CONFLICT(connection_id) DO UPDATE SET username_key=excluded.username_key,payload=excluded.payload').run(id.slice(8),this.accountKey(state.username),this.encode(id,state));}
|
||||
catch(cause){if(cause.errcode===2067)throw duplicate();if(cause.status)throw cause;throw fail();}
|
||||
catch(cause){if(cause.errcode===2067)throw duplicate();if(cause.status)throw cause;throw fail(cause,'account_write');}
|
||||
}
|
||||
async accountAvailability(connectionId,username) {
|
||||
try{const row=this.accounts().prepare('SELECT connection_id FROM accounts WHERE username_key=?').get(this.accountKey(username));return {available:!row||row.connection_id===connectionId};}
|
||||
catch(cause){if(cause.status)throw cause;throw fail();}
|
||||
catch(cause){if(cause.status)throw cause;throw fail(cause,'account_read');}
|
||||
}
|
||||
async migrateAccounts(connectionIds) {
|
||||
const db=this.accounts(),legacy=[];
|
||||
@@ -99,7 +106,7 @@ class BpmVault {
|
||||
try{for(const item of legacy)this.putAccount(item.id,item.state);db.exec('COMMIT');}catch(cause){db.exec('ROLLBACK');throw cause;}
|
||||
// Committed database rows become authoritative before legacy ciphertext is removed.
|
||||
for(const item of legacy)await fsp.rm(this.filename(item.id),{force:true});
|
||||
}catch(cause){if(cause.status)throw cause;throw fail();}
|
||||
}catch(cause){if(cause.status)throw cause;throw fail(cause,'account_migrate');}
|
||||
}
|
||||
async read(id) {
|
||||
const filename = this.filename(id);
|
||||
@@ -116,7 +123,7 @@ class BpmVault {
|
||||
await fsp.rm(filename,{force:true});return saved;
|
||||
}
|
||||
return state;
|
||||
} catch (error) { if (error.code === 'ENOENT') return null;if(error.code==='BPM_ACCOUNT_DUPLICATE')throw error;throw fail(); }
|
||||
} catch (error) { if (error.code === 'ENOENT') return null;if(error.code==='BPM_ACCOUNT_DUPLICATE')throw error;throw fail(error); }
|
||||
}
|
||||
async write(id, state) {
|
||||
const filename = this.filename(id);
|
||||
@@ -126,9 +133,9 @@ class BpmVault {
|
||||
// EXDEV. SQLite publishes the encrypted payload in one atomic transaction.
|
||||
this.accounts().prepare('INSERT INTO sessions(connection_id,payload) VALUES(?,?) ON CONFLICT(connection_id) DO UPDATE SET payload=excluded.payload').run(id,this.encode(id,state));
|
||||
await fsp.rm(filename,{force:true}).catch(()=>{});
|
||||
} catch { throw fail(); }
|
||||
} catch(cause) { throw fail(cause); }
|
||||
}
|
||||
async remove(id) { try { await fsp.rm(this.filename(id), { force: true });if(id.startsWith('account:'))this.accounts().prepare('DELETE FROM accounts WHERE connection_id=?').run(id.slice(8));else this.accounts().prepare('DELETE FROM sessions WHERE connection_id=?').run(id); } catch { throw fail(); } }
|
||||
async remove(id) { try { await fsp.rm(this.filename(id), { force: true });if(id.startsWith('account:'))this.accounts().prepare('DELETE FROM accounts WHERE connection_id=?').run(id.slice(8));else this.accounts().prepare('DELETE FROM sessions WHERE connection_id=?').run(id); } catch(cause) { throw fail(cause,'session_remove'); } }
|
||||
destroy() { this.db?.close();this.db=null;this.key?.fill(0);this.key=null; }
|
||||
}
|
||||
module.exports = { BpmVault };
|
||||
+4
-1
@@ -1,8 +1,11 @@
|
||||
const {createServer,startService}=require('./app/bridge.cjs');
|
||||
const {main}=require('./app/application.cjs');
|
||||
const {safeDiagnostic}=require('./shared/diagnostics.cjs');
|
||||
const startupAt=performance.now();
|
||||
if (require.main === module) main().then(app=>{
|
||||
const stop=()=>app.stop().then(()=>process.exit(0),()=>process.exit(1));
|
||||
process.once('SIGINT',stop);process.once('SIGTERM',stop);
|
||||
process.on('message',message=>{if(message==='order:shutdown')stop();});
|
||||
}).catch(error => { console.error('启动失败:'+error.message); process.exitCode=1; });
|
||||
console.info('启动耗时(ms):'+Math.round(performance.now()-startupAt));
|
||||
}).catch(error => { console.error('启动失败:'+JSON.stringify(safeDiagnostic(error,{durationMs:performance.now()-startupAt}))); process.exitCode=1; });
|
||||
module.exports = { createServer, startService, main };
|
||||
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
const stages=new Set(['startup','directory_permissions','key_directory','key_create','key_protect','key_write','key_read','key_unprotect','key_length','db_open','db_read','db_write','encrypt','decrypt','session_read','session_write','session_remove','account_read','account_write','account_migrate']);
|
||||
const safeCode=value=>typeof value==='string'&&/^(?:E[A-Z0-9_]{1,63}|SQLITE_[A-Z0-9_]{1,56}|BPM_[A-Z0-9_]{1,56}|STORAGE_UNAVAILABLE)$/.test(value)?value:undefined;
|
||||
function safeDiagnostic(error,{stage='startup',durationMs}={}){
|
||||
const chain=[];let current=error;while(current&&typeof current==='object'&&chain.length<5&&!chain.includes(current)){chain.push(current);current=current.cause;}
|
||||
const originalCode=[...chain].reverse().map(item=>safeCode(item.code)).find(Boolean);
|
||||
const ownStage=chain.map(item=>item.stage).find(value=>stages.has(value));
|
||||
const frame=chain.flatMap(item=>typeof item.stack==='string'?item.stack.split('\n').slice(1):[]).map(line=>line.match(/(?:^|[\\/])(src[\\/][A-Za-z0-9_.\\/-]+\.(?:cjs|js):\d+):\d+\)?$/)?.[1]).find(Boolean);
|
||||
return {type:['Error','TypeError','RangeError','SyntaxError','AggregateError','DOMException'].includes(error?.name)?error.name:'Error',stage:ownStage||stage,code:safeCode(error?.code),causeCode:originalCode,program:frame?.replaceAll('\\','/'),...(Number.isFinite(durationMs)&&durationMs>=0?{durationMs:Math.round(durationMs)}:{})};
|
||||
}
|
||||
module.exports={safeDiagnostic};
|
||||
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
|
||||
const {RuntimeHealth}=require('../../src/modules/health/runtime-health.cjs');
|
||||
test('health separates successful todo from failed inform and reports listener separately',t=>{
|
||||
const stateDir=fs.mkdtempSync(path.join(os.tmpdir(),'health-source-')),now=new Date().toISOString();
|
||||
const health=new RuntimeHealth({stateDir,workspace:{snapshot:()=>({connections:[{id:'c',name:'合成连接',owner:'worker',status:'connected'}]})},scanner:{store:{status:()=>({config:{enabled:true,intervalMinutes:1},runs:[{connectionId:'c',kind:'todo',status:'success',finishedAt:now,seen:1},{connectionId:'c',kind:'inform',status:'error',finishedAt:now,message:'合成读取失败'}]})}},monitor:{snapshot:()=>({status:'listening',lastSyncAt:now,sources:{}})}});
|
||||
t.after(()=>{health.close();fs.rmSync(stateDir,{recursive:true,force:true});});
|
||||
const data=health.snapshot({username:'worker',role:'operator'}),scan=data.groups.find(group=>group.key==='scan');
|
||||
assert.equal(scan.status,'error');assert.equal(scan.items.find(item=>item.kind==='todo').status,'healthy');assert.equal(scan.items.find(item=>item.kind==='inform').status,'error');
|
||||
assert.equal(data.groups.find(group=>group.key==='listener').status,'healthy');
|
||||
});
|
||||
test('an old successful source becomes stale without making external requests',t=>{
|
||||
const stateDir=fs.mkdtempSync(path.join(os.tmpdir(),'health-stale-')),old=new Date(Date.now()-10*60000).toISOString();
|
||||
const health=new RuntimeHealth({stateDir,workspace:{snapshot:()=>({connections:[{id:'c',owner:'worker'}]})},scanner:{scan:()=>assert.fail('no external scan'),store:{status:()=>({config:{enabled:true,intervalMinutes:1},runs:[{connectionId:'c',kind:'todo',status:'success',finishedAt:old}]})}}});
|
||||
t.after(()=>{health.close();fs.rmSync(stateDir,{recursive:true,force:true});});
|
||||
const item=health.snapshot({role:'admin'}).groups.find(group=>group.key==='scan').items.find(item=>item.kind==='todo');
|
||||
assert.ok(item,'todo source must have its own health item');assert.equal(item.status,'pending');assert.equal(item.stale,true);assert.equal(item.lastSuccessAt,old);
|
||||
});
|
||||
test('health reports process event loop observations without exposing host data',async t=>{
|
||||
const stateDir=fs.mkdtempSync(path.join(os.tmpdir(),'health-loop-')),health=new RuntimeHealth({stateDir});
|
||||
t.after(()=>{health.close();fs.rmSync(stateDir,{recursive:true,force:true});});
|
||||
health.start();await new Promise(resolve=>setTimeout(resolve,35));
|
||||
const observed=health.snapshot({role:'admin'}).runtime.eventLoop;
|
||||
assert.ok(observed,'process event loop observations must be available');assert.equal(observed.observed,true);assert.ok(Number.isFinite(observed.maxMs));assert.ok(observed.maxMs>=0);
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
|
||||
const {WorkspaceStore}=require('../../src/modules/workspace/store.cjs');
|
||||
const {BpmScanStore}=require('../../src/modules/bpm/scan-store.cjs');
|
||||
const {BpmScanner}=require('../../src/modules/bpm/scanner.cjs');
|
||||
const {BpmArrivalMonitor}=require('../../src/modules/bpm/arrival-monitor.cjs');
|
||||
async function until(predicate){const end=Date.now()+1500;while(!predicate()){if(Date.now()>end)assert.fail('expected monitor transition');await new Promise(resolve=>setTimeout(resolve,5));}}
|
||||
function fixture(t,{interval=1440,onTodoReconciled}={}){
|
||||
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'arrival-recovery-')),workspace=new WorkspaceStore(path.join(dir,'workspace')),scanStore=new BpmScanStore(path.join(dir,'scan'));
|
||||
let scanner,monitor;t.after(async()=>{await monitor?.stop();await scanner?.stop();scanStore.close();workspace.close();fs.rmSync(dir,{recursive:true,force:true});});
|
||||
const admin={username:'admin',role:'admin'},connection=workspace.createConnection({name:'合成连接',owner:'admin'},admin,[admin]);
|
||||
let now=Date.parse('2026-10-21T01:00:00Z'),todoCalls=0,failed=false,callbacks,hold;
|
||||
workspace.updateConnectionSession(connection.id,{status:'connected',identity:'synthetic',checkedAt:new Date(now).toISOString(),checks:{portal:{ok:true,message:'synthetic'}}},admin);
|
||||
scanStore.saveConfig({enabled:true,intervalMinutes:interval});
|
||||
const row={source:'todo',portalSource:'portal-v4',taskId:'T',instanceId:'I',revision:1,processName:'合成订单',nodeName:'通知',processVersion:'1',serialNum:'S',title:'订单号:TEST',receivedAt:'2026-10-21T00:30:00Z'};
|
||||
const manager={sessions:new Map(),epoch:()=>0,ensure:async()=>({status:'connected'}),run:async(_,fn)=>fn(),authorize(){},vault:()=>({read:async()=>({storageState:{cookies:[],origins:[]}})}),scanArrivals:async()=>[]};
|
||||
const provider={scan:async()=>{todoCalls++;if(hold)await hold;if(failed)throw Object.assign(Error('synthetic network failure'),{code:'ETIMEDOUT'});return [structuredClone(row)];}};
|
||||
scanner=new BpmScanner({workspace,store:scanStore,manager,settings:()=>({schedule:{weekends:true}})});
|
||||
monitor=new BpmArrivalMonitor({workspace,manager,scanner,provider,onTodoReconciled,clock:()=>now,settings:()=>({schedule:{weekends:true}}),hubClient:{connect:async(_,options)=>{callbacks=options;return {stop:async()=>{}};}},debounceMs:1,sourceRetryMs:[100,200,1000]});
|
||||
return {workspace,scanStore,connection,admin,monitor,row,get now(){return now;},get calls(){return todoCalls;},advance:ms=>now+=ms,fail:value=>failed=value,hint:()=>callbacks.onEvent('worklistChanged'),hold:value=>hold=value};
|
||||
}
|
||||
test('reducing a live reconciliation interval applies from the last completed reconciliation',async t=>{
|
||||
const f=fixture(t);await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
||||
f.advance(10*60000);f.scanStore.saveConfig({enabled:true,intervalMinutes:1});await f.monitor.tick();
|
||||
await until(()=>f.calls===2);assert.ok(Date.parse(f.monitor.snapshot(f.connection.id).nextReconcileAt)<=f.now+60000);
|
||||
});
|
||||
test('failed todo sources keep an independent retry with backoff despite hint bursts',async t=>{
|
||||
const f=fixture(t);await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
||||
f.fail(true);f.hint();await until(()=>f.scanStore.status().runs.some(run=>run.kind==='todo'&&run.status==='error'));
|
||||
let state=f.monitor.snapshot(f.connection.id);assert.equal(state.sources.todo.status,'error');assert.equal(state.sources.inform.status,'healthy');
|
||||
const calls=f.calls;f.fail(false);for(let i=0;i<20;i++)f.hint();f.advance(50);await f.monitor.tick();await new Promise(resolve=>setTimeout(resolve,15));assert.equal(f.calls,calls);
|
||||
f.advance(51);await f.monitor.tick();await until(()=>f.calls===calls+1&&f.monitor.snapshot(f.connection.id).sources.todo.status==='healthy');
|
||||
});
|
||||
test('only fully committed authorized todo reads publish trusted execution hints',async t=>{
|
||||
const hints=[],f=fixture(t,{onTodoReconciled:payload=>hints.push(payload)});await f.monitor.tick();await until(()=>f.monitor.snapshot(f.connection.id).lastSyncAt);
|
||||
assert.equal(hints.length,1);assert.equal(hints[0].connectionId,f.connection.id);assert.deepEqual(hints[0].rows,[f.row]);assert.ok(hints[0].runId);assert.ok(Date.parse(hints[0].startedAt)<=Date.parse(hints[0].finishedAt));
|
||||
let release;f.hold(new Promise(resolve=>release=resolve));f.hint();await until(()=>f.calls===2);f.workspace.disableConnection(f.connection.id,f.admin);
|
||||
const stopping=f.monitor.stopConnection(f.connection.id);release();await stopping;assert.equal(hints.length,1);
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),cp=require('node:child_process');
|
||||
let safeDiagnostic;try{safeDiagnostic=require('../../src/shared/diagnostics.cjs').safeDiagnostic;}catch(error){if(error.code!=='MODULE_NOT_FOUND')throw error;}
|
||||
test('storage diagnostics preserve useful stages and codes without messages, paths or secrets',()=>{
|
||||
assert.equal(typeof safeDiagnostic,'function');
|
||||
const cause=Object.assign(Error('synthetic-secret password=value C:/private/form'),{code:'EACCES',path:'C:/private/form'}),error=Object.assign(Error('public message',{cause}),{stage:'key_read',code:'STORAGE_UNAVAILABLE',status:503});
|
||||
const data=safeDiagnostic(error);assert.equal(data.stage,'key_read');assert.equal(data.causeCode,'EACCES');assert.doesNotMatch(JSON.stringify(data),/synthetic-secret|password|private|form|public message/);
|
||||
});
|
||||
test('a secured directory can be reused but replacement must be secured again',{skip:process.platform!=='win32'},t=>{
|
||||
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'vault-acl-reuse-'));let unavailable=false,calls=0;
|
||||
t.mock.method(cp,'execFileSync',()=>{calls++;if(unavailable)throw Object.assign(Error('synthetic shell failure'),{code:'ETIMEDOUT'});return '';});
|
||||
delete require.cache[require.resolve('../../src/platform/security/vault.cjs')];const {BpmVault}=require('../../src/platform/security/vault.cjs');
|
||||
t.after(()=>{fs.rmSync(dir,{recursive:true,force:true});fs.rmSync(dir+'-old',{recursive:true,force:true});});
|
||||
const first=new BpmVault({stateDir:dir});first.destroy();unavailable=true;
|
||||
const second=new BpmVault({stateDir:dir});second.destroy();assert.equal(calls,1);
|
||||
fs.renameSync(dir,dir+'-old');fs.mkdirSync(dir);
|
||||
assert.throws(()=>new BpmVault({stateDir:dir}),error=>error.status===503&&error.stage==='directory_permissions'&&error.cause.code==='ETIMEDOUT');
|
||||
});
|
||||
test('key read failures retain the original cause behind a safe public 503',{skip:process.platform!=='win32'},t=>{
|
||||
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'vault-cause-'));
|
||||
t.mock.method(cp,'execFileSync',()=> '');
|
||||
const originalExists=fs.existsSync,originalRead=fs.readFileSync;
|
||||
t.mock.method(fs,'existsSync',file=>/[\\/]bpm-keys[\\/].+\.key$/.test(String(file))?true:originalExists(file));
|
||||
t.mock.method(fs,'readFileSync',(file,...args)=>{if(/[\\/]bpm-keys[\\/].+\.key$/.test(String(file)))throw Object.assign(Error('synthetic-secret'),{code:'EACCES'});return originalRead(file,...args);});
|
||||
delete require.cache[require.resolve('../../src/platform/security/vault.cjs')];const {BpmVault}=require('../../src/platform/security/vault.cjs'),vault=new BpmVault({stateDir:dir});
|
||||
t.after(()=>{vault.destroy();fs.rmSync(dir,{recursive:true,force:true});});
|
||||
assert.throws(()=>vault.encode('synthetic',{value:'synthetic'}),error=>error.status===503&&error.stage==='key_read'&&error.cause.code==='EACCES'&&!error.message.includes('synthetic-secret'));
|
||||
});
|
||||
Reference in new issue
Block a user