aeternity / aesophia_http

ISC License
6 stars 9 forks source link

Use get requests instead of post, add headers to enable caching (not working) #108

Closed davidyuk closed 2 years ago

davidyuk commented 2 years ago

I'm trying to enable caching of compiler responses as I'm proposed in https://github.com/aeternity/aepp-sdk-js/issues/1681#issuecomment-1289111343

Firstly I've added the "Cache-Control" header as middleware, but I found that POST requests can't be cached https://stackoverflow.com/a/626083/6176994. Later I new that GET requests don't support the request body in some environments (for example fetch in browser fails with "Request with GET/HEAD method cannot have body").

The rest option is to encode parameters in query part of URL instead of request body, but URL length is limited in some environments https://stackoverflow.com/a/417184/6176994 It could work well in modern browsers the same way as data URLs, but it needs additional testing.

To use POST in compiler methods is not semantic because compiler doesn't have its own state, but there is no easy way to replace it.

A page for testing:

<!DOCTYPE html>
<html lang="en">
<body>
<a href="./test.html">test.html</a>
<div id="output"></div>
<script>
  async function getRequestTime() {
    const timeBefore = Date.now();
    const request = await fetch('http://localhost:3080/compile', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        code: 'main contract Identity =\n  entrypoint main_fun (x:int) = x',
      }),
    });
    await request.json();
    return Date.now() - timeBefore;
  }

  (async () => {
    const output = document.getElementById('output');
    for (let i = 0; i < 10; i++) {
      output.innerText += `compile request ${i}: ${await getRequestTime()}\n`;
    }
  })();
</script>
</body>
</html>
marc0olo commented 2 years ago

@davidyuk what's the reason for caching the compile output? I don't understand that 🤔

marc0olo commented 2 years ago

actually I dislike this route. we still want to avoid calling the http compiler on aepp / backend initialization via source code. that's what https://github.com/aeternity/aepp-sdk-js/issues/1681 is about mainly. once a contract is deployed we could and should 100% rely on the ACI only.

I can't believe we still discuss this 😢

if we speak about "on-demand" compilation by e.g. AEstudio the caching won't help anyway because source-code changes too frequently. there we discussed that we'd like to revisit using erlscripten to remove the heavy dependency to the http compiler

dincho commented 2 years ago

I absolutely agree with @marc0olo the public HTTP compiler should be depreciated and there is no any other sensible use-case of it than online editors (i.e. contracts app, aestudio) which should use their own versions (CORS protected).

To sumarize this once again, apps should compile during development and package their ACI's for runtime usages (calldata).

davidyuk commented 2 years ago

@marc0olo this was intended as a workaround for existing apps to don't have to make urgent changes in sdk, but anyway, HTTP is not working this way