Coder Social home page Coder Social logo

nodejs-eval's Introduction

NodeJS Eval

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

This combines:

  • The Python nodejs-bin project, which bundles NodeJS (with npm and npx) into a PyPI package, and
  • The JavaScript/TypeScript http-eval project, which runs a JavaScript evaluation server on a Unix domain socket.

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

nodejs-eval's People

Stargazers

Andreas Motl avatar

Watchers

 avatar

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.