second-state / wasmedge-quickjs

A high-performance, secure, extensible, and OCI-complaint JavaScript runtime for WasmEdge.
Apache License 2.0
477 stars 59 forks source link

Asynchronous operations are not available in the http server #97

Open gudaoxuri opened 1 year ago

gudaoxuri commented 1 year ago
import { createServer, fetch } from 'http';

createServer((req, resp) => {
  req.on('data',async (body) => {

    if(req.url == '/wait'){
      print("fetch...");
      let response = await fetch('http://127.0.0.1:8001/echo', { method: 'POST', body: 'hello server' })
      let result = await response.text()
      print("fetched:",result);
      resp.end(result)
    }else{
      resp.end(body)
    }

  })
}).listen(8001,'0.0.0.0', () => {
  print('listen 8001 ...\n');
})

async function test_fetch() {
  let resp = await fetch('http://127.0.0.1:8001/wait', { method: 'POST', body: 'hello server' })
  print('fetch client recv:', await resp.text())
  print()
}

async function run_test() {
  await test_fetch()
  exit(0)
}

run_test()

Output after execution.

wait...
fetched: hello server

No output fetch client recv:hello server

So, after adding async/await operations to the http server, the request cannot get a response.

To return an empty response using CURL.

curl -d 'hello server' http://127.0.0.1:8001/wait -X POST
curl: (52) Empty response from the server