Description
See also #100
Being able to run the compilation/computation inside a WebWorker or in an iframe would make it significantly easier to secure the scripts to guard against abuse and it would enhance the viewing experience by moving expensive or runaway calculations out of the UI thread.
After a conversation with @viebel he recommended trying to prototype by replacing eval()
with an async version that channels messages through the frames/contexts. Something like this would do…
function isolatedEval( inputSourceCode, mode, options = { timeout: 1000 } ) {
return new Promise( ( response, reject ) => {
const [ port1, port2 ] = new MessageChannel();
let alreadyFinished = false;
let handle;
klipseWorker.postMessage( { inputSourceCode, mode, options }, [ port2 ] );
handle = setTimeout( () => {
alreadyFinished = true;
port1.postMessage( 'cancel' );
port1.close();
reject( 'timeout' );
}, timeout );
port1.onmessage = ( { data: { error, output, wasSuccess } } ) => {
clearTimeout( handle );
port1.close();
if ( alreadyFinished ) {
return;
}
alreadyFinished = true;
if ( wasSuccess ) {
resolve( output );
} else {
reject( error );
}
};
} );
}
If running inside an iframe we could support view-only DOM creation by sending .innerHTML
across the frame as a string and then stiching it up on the host side.