Socket
Socket
Sign inDemoInstall

nodejs-eval

Package Overview
Dependencies
2
Maintainers
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nodejs-eval

Evaluate arbitrary JavaScript from Python using a NodeJS sidecar


Maintainers
1

Readme

NodeJS Eval

Evaluate arbitrary JavaScript from Python, using a NodeJS sidecar process.

This combines:

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install nodejs-eval

License

nodejs-eval is distributed under the terms of the MIT license.

Usage instructions

Basic synchronous call

JavaScript code is evaluated as a function body within a synchronous or asynchronous JavaScript function, within an ECMAScript module.

Note that the NodeJS evaluator always only supports async mode on the Python side. However, the supplied JavaScript code can be either sync or async.

    from nodejs_eval import evaluator

    async with evaluator() as e:
        result = await e.run("return 6*7;")
        assert result == 42

Basic asynchronous call

    from nodejs_eval import evaluator

    async with evaluator() as e:
        result = await e.run_async(
            """
await new Promise(r => setTimeout(r, 2000));
return 6*7;
""")
        assert result == 42

Storing state on this

Evaluations run with a consistent JavaScript this context, so state can be stored on it:

    from nodejs_eval import evaluator

    async with evaluator() as e:
        await e.run("this.x = 6*7;")
        result = await e.run("return this.x;")
        assert result == 42

Import using dynamic await import()

Because code is executed within an ECMAScript module, you can use the dynamic async import() to import other modules.

This is easiest done in async mode:

    from nodejs_eval import evaluator

    async with evaluator() as e:
        result = await e.run_async(
            """
const os = await import("os");
return os.cpus();
"""
        )
        assert len(result) > 0

FAQs


Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc