markwylde / workerbox

A secure sandbox to execute untrusted user JavaScript, in a web browser, without any risk to your own domain/site/page.
https://workerbox.net
MIT License
125 stars 3 forks source link

Callback functions don't resolve returned values #9

Closed markwylde closed 1 year ago

markwylde commented 1 year ago

Two way communication for functions works, but it appears to not be recursive.

So, if a callback is set, that then returns a value, that value will never be resolved to the main library.

Simply, the following test should pass, but doesn't :(

test('callback as a function can return a value', async (t) => {
  t.plan(1);

  const run = await createWorkerBox(serverUrl, { appendVersion: false });

  let storedCallback;
  const scope = {
    setCallback: (fn) => {
      storedCallback = fn;
    }
  };

  await run(`
    setCallback(() => {
      console.log('ran, but....');
      return 'worked';
    });
  `, scope);

  await new Promise(resolve => setTimeout(resolve, 300));

  t.equal(await storedCallback(), 'worked');
});
markwylde commented 1 year ago

It's still not a recursive solution, as I think if a callback passes another callback, again it won't get a return value.

I need to cleanup how all these prepareArgs and parseArgs functions work, and come up with a more generic and cleaner recursive solution.

But for now, this should fix the immediate issue.