Slice detached element recovery encoding and audit progressive canvas
This commit is contained in:
@@ -112,3 +112,53 @@ test('repeated owned containers reuse facts assembled earlier in the same asynch
|
||||
const captured=await sealRecoveryImageAsync(17,1,1,dataArray([unsafe,unsafe]));
|
||||
assert.deepEqual(captured.payload,[{getValue:{value:1}},{getValue:{value:2}}]);
|
||||
});
|
||||
|
||||
const ownedRecord=async()=>{
|
||||
const {detachElementData}=await import('../../src/lib/canvas-core/element-data.ts');
|
||||
return detachElementData({id:'large',incarnation:1,structure:'node',ownerCanvasId:'canvas',componentId:'text',dataVersion:1,geometry:{x:0,y:0,width:100,height:100,rotation:0},data:{text:'',rows:Array.from({length:1000},(_,i)=>({index:i,text:'owned-中文😀-'+i}))}});
|
||||
};
|
||||
test('first-time detached element JSON is normalized inside slices rather than captured in one walk',async()=>{
|
||||
const {sealRecoveryImageAsync,sealedRecoveryByteLength}=await import('../../src/lib/canvas-runtime/recovery-encoding.ts');
|
||||
const element=await ownedRecord(),stringify=JSON.stringify;let strings=0,first,tick=0;
|
||||
JSON.stringify=function(value,...args){if(typeof value==='string'&&value.startsWith('owned-'))strings++;return Reflect.apply(stringify,this,[value,...args]);};
|
||||
try{
|
||||
const image=await sealRecoveryImageAsync(17,1,1,element,{now:()=>tick++,frameBudgetMs:1,yieldTask:async()=>{first??=strings;}});
|
||||
assert.ok(first>=0&&first<128,'detached data was fully encoded before the first yield');assert.equal(strings,1000);
|
||||
assert.deepEqual(image.payload,element);assert.equal(sealedRecoveryByteLength(image),new TextEncoder().encode(stringify(image)).byteLength);
|
||||
}finally{JSON.stringify=stringify;}
|
||||
});
|
||||
|
||||
test('inherited toJSON accessors at admission keep detached elements on the synchronous capture path',async()=>{
|
||||
const {sealRecoveryImageAsync}=await import('../../src/lib/canvas-runtime/recovery-encoding.ts');
|
||||
const left=await ownedRecord(),right=await ownedRecord(),previous=Object.getOwnPropertyDescriptor(Object.prototype,'toJSON');let calls=0,yields=0,tick=0;
|
||||
try{
|
||||
Object.defineProperty(Object.prototype,'toJSON',{configurable:true,get(){calls++;return undefined;}});
|
||||
const expected=sealRecoveryImage(17,1,1,left),syncCalls=calls;calls=0;
|
||||
const image=await sealRecoveryImageAsync(17,1,1,right,{now:()=>tick++,frameBudgetMs:1,yieldTask:async()=>{yields++;}});
|
||||
assert.equal(calls,syncCalls);assert.equal(yields,0);assert.deepEqual(image.payload,expected.payload);
|
||||
}finally{if(previous)Object.defineProperty(Object.prototype,'toJSON',previous);else delete Object.prototype.toJSON;}
|
||||
});
|
||||
|
||||
test('unknown callbacks invalidate the detached JSON environment without changing earlier captures',async()=>{
|
||||
const {dataRecord}=await import('../../src/lib/canvas-core/data-containers.ts');
|
||||
const {sealRecoveryImageAsync}=await import('../../src/lib/canvas-runtime/recovery-encoding.ts');
|
||||
const previous=Object.getOwnPropertyDescriptor(Array.prototype,'toJSON');
|
||||
const cleanup=()=>{if(previous)Object.defineProperty(Array.prototype,'toJSON',previous);else delete Array.prototype.toJSON;};
|
||||
const make=async()=>dataRecord({left:await ownedRecord(),bridge:{get value(){Object.defineProperty(Array.prototype,'toJSON',{configurable:true,value(){return ['changed-by-callback'];}});return 'bridge';}},right:await ownedRecord()});
|
||||
try{
|
||||
const expected=sealRecoveryImage(17,1,1,await make());cleanup();
|
||||
const payload=await make();let tick=0,yields=0;
|
||||
const pending=sealRecoveryImageAsync(17,1,1,payload,{now:()=>tick++,frameBudgetMs:1,yieldTask:async()=>{yields++;}});
|
||||
cleanup();const image=await pending;assert.ok(yields>0);assert.deepEqual(image.payload,expected.payload);
|
||||
assert.equal(image.payload.left.data.rows.length,1000);assert.deepEqual(image.payload.right.data.rows,['changed-by-callback']);
|
||||
}finally{cleanup();}
|
||||
});
|
||||
|
||||
test('deferred detached subtrees enforce the depth bound both cold and after cache reuse',async()=>{
|
||||
const {dataRecord}=await import('../../src/lib/canvas-core/data-containers.ts');
|
||||
const {sealRecoveryImageAsync}=await import('../../src/lib/canvas-runtime/recovery-encoding.ts');
|
||||
const nest=leaf=>{for(let i=0;i<61;i++)leaf=dataRecord({child:leaf});return leaf;};
|
||||
const cold=await ownedRecord();await assert.rejects(sealRecoveryImageAsync(17,1,1,nest(cold)),/invalid_recovery_payload/);
|
||||
const warm=await ownedRecord();await sealRecoveryImageAsync(17,1,1,warm);
|
||||
await assert.rejects(sealRecoveryImageAsync(17,1,1,nest(warm)),/invalid_recovery_payload/);
|
||||
});
|
||||
|
||||
@@ -591,7 +591,7 @@ module.exports=function(source){return nativeTransform.call(this,source).replace
|
||||
}
|
||||
} finally {
|
||||
releaseScopeStream();releaseAbandoned();releaseFirstModel();releaseImport();releaseApply();releaseUpload(); socket?.close();
|
||||
if (chrome) { const exit = new Promise(resolve => { if (chrome.exitCode !== null) resolve(); else chrome.once('exit', resolve); }); chrome.kill(); await Promise.race([exit,pause(3000)]);if(chrome.exitCode===null)chrome.kill('SIGKILL');await exit; }
|
||||
if (chrome) { const exit = new Promise(resolve => { if (chrome.exitCode !== null) resolve(); else chrome.once('exit', resolve); }); chrome.kill(); await Promise.race([exit,pause(3000)]);if(chrome.exitCode===null)chrome.kill('SIGKILL');await exit;chrome.stdin?.destroy();chrome.stdout?.destroy();chrome.stderr?.destroy(); }
|
||||
if (next?.pid) { const exit = new Promise(resolve => { if (next.exitCode !== null) resolve(); else next.once('exit', resolve); }); try { process.kill(-next.pid, 'SIGTERM'); } catch {} await Promise.race([exit, pause(3000)]); if (next.exitCode === null) try { process.kill(-next.pid, 'SIGKILL'); } catch {} }
|
||||
upstream.closeAllConnections(); await new Promise(resolve => upstream.close(resolve));
|
||||
await rm(directory, { recursive: true, force: true, maxRetries: 5 }); await rm(profile, { recursive: true, force: true, maxRetries: 5 });
|
||||
|
||||
Reference in New Issue
Block a user