Skip to content

Is std.popen() Supported?  #139

@LijieZhang1998

Description

@LijieZhang1998

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions