Skip to content
New issue

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

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

Already on GitHub? # to your account

Is std.popen() Supported? #139

Open
LijieZhang1998 opened this issue May 15, 2024 · 1 comment
Open

Is std.popen() Supported? #139

LijieZhang1998 opened this issue May 15, 2024 · 1 comment

Comments

@LijieZhang1998
Copy link

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;
@L-jasmine
Copy link
Collaborator

Sorry, although qjs supports popen, wasm cannot support calling cmd.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants