Giter Site home page Giter Site logo

Comments (3)

JonasJonny avatar JonasJonny commented on June 12, 2024 1

@helloanoop Can you try this? For my use case it works fine.

const { NodeVM } = require('vm2');
const { getAbcRequest } = require('...');

async getData(script) {
  try {
    const vm2 = new NodeVM({
      sandbox: {
        getAbcRequest,
      },
    });

    const asyncVM2 = vm2.run(`module.exports = async () => { ${script} }`, 'vm');
    return await asyncVM2();
  } catch(err) {
    return {
      err,
    };
  }
}
const response = await getData(`
  // script
  try {
    const response = await getAbcRequest();
    return response;  
  } catch(err) {
    throw err;
  }
`);

console.log('response', response);

from vm2.

XmiliaH avatar XmiliaH commented on June 12, 2024

This is expected. As JonasJonny pointed out for NodeVM module.exports as the script is run as a module.

from vm2.

helloanoop avatar helloanoop commented on June 12, 2024

Thanks @JonasJonny @XmiliaH

We were able to use the solution proposed by @JonasJonny

In case if anyone in the future bumps into a similar issue, here is the PR that fixed the issue in Bruno: https://github.com/usebruno/bruno/pull/145/files

from vm2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.