Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to specify a conversion function for a column #7

Open
simonw opened this issue Mar 20, 2024 · 2 comments
Open

Allow user to specify a conversion function for a column #7

simonw opened this issue Mar 20, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Contributor

simonw commented Mar 20, 2024

Super advanced: ability to reformat data to populate columns correctly, for example reformatting dates to match. This is likely to be a separate feature, there's a lot of depth to this one and it affects how imports work too.

Might also need a bit of persistence around this in order to remember what those conversions were for future loads into the same table.

_Originally posted by @simonw in #3

@simonw
Copy link
Contributor Author

simonw commented Mar 20, 2024

Worked with Claude Opus to figure out this recipe for running code hopefully-safely in a worker:

workerScript = `
self.onmessage = function(event) {
  const userCode = event.data.userCode;;
  try {
    const result = eval(userCode);
    self.postMessage({ success: true, result: result });
  } catch (error) {
    self.postMessage({ success: false, error: error.message });
  }
};
`;

function safeEval(userProvidedCode, timeLimit) {
  timeLimit = timeLimit || 1000;
  let worker = new Worker(URL.createObjectURL(new Blob([workerScript], { type: 'text/javascript' })));
  worker.onmessage = function(event) {
    if (event.data.success) {
      console.log('Result:', event.data.result);
    } else {
      console.error('Error:', event.data.error);
    }
    worker.terminate();
    worker = null;
  };
  worker.postMessage({ userCode: userProvidedCode });
  setTimeout(function() {
    if (worker) {
      worker.terminate();
      console.error('Execution timed out');
    }
  }, timeLimit);
}

Claude conversation here: https://gist.github.com/simonw/ef14e52da14e995f986221865ab72a55

@simonw simonw added the enhancement New feature or request label Mar 20, 2024
@simonw
Copy link
Contributor Author

simonw commented Mar 20, 2024

... or I could use QuickJS as a sandbox, finally figured out a good recipe for loading that here: https://til.simonwillison.net/npm/self-hosted-quickjs

@simonw simonw transferred this issue from datasette/datasette-paste Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant