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

Is std.popen() Supported? #139

Open LijieZhang1998 opened 3 months ago

LijieZhang1998 commented 3 months ago

Hi, quickjs supports popen function. But when I added a js module file that uses this function for test purpose, it's not executed and no error is thrown. Do you have any idea? Thanks.


import * as std from 'std';

function fetchPolyfill(resource, init) {
  init = init || {
    method: 'GET',
    headers: null,
    body: null,
  };

  init.method = init.method.toUpperCase();

  // curl command
  let curlCmd = `curl -s -X${init.method.toUpperCase()} "${resource}"`;

  if (init.headers != null && Object.keys(init.headers).length > 0) {
    curlCmd = `${curlCmd} ${Object.entries(init.headers).map(n => `-H '${n[0]}: ${n[1]}'`).join(' ')}`
  }
  if (init.method != 'GET') {
    let body = init.body;

    if (typeof body != 'string') {
      body = JSON.stringify(body);
    }

    curlCmd = `${curlCmd} -d '${body}'`
  }

  const spErr = {};
  const sp = std.popen(curlCmd, 'r', spErr);
  const curlOutput = sp.readAsString();
  const responseUrl = resource;
  const responseHeaders = {}; 
  let responseOk = true;     
  let responseStatus = 200; 

  const p = new Promise((resolve, reject) => {
    const response = {
      headers: responseHeaders,
      ok: responseOk,
      url: responseUrl,
      type: 'json',
      text: () => curlOutput,
      json: () => JSON.parse(curlOutput),
    };

    resolve(response);
  });

  return p;
}

export default fetchPolyfill;