1
0
Fork
You've already forked nodelixir
0
Provides an Elixir API for calling Node.js functions. Forked from elixir-nodejs [https://github.com/revelrylabs/elixir-nodejs]
  • Elixir 87.4%
  • JavaScript 12.6%
2022年12月09日 18:46:32 +01:00
config Add GNU Affero General Public License version 3 2022年11月21日 17:48:58 +01:00
lib Remove :binary option and make it default to always handle UTF8 characters. 2022年11月28日 23:45:56 +01:00
priv Fork of elixir-nodejs at https://github.com/revelrylabs/elixir-nodejs at commit b5c96047c54b70873fc1efa4f716233c569d23c8 2022年11月21日 15:23:05 +01:00
test Remove :binary option and make it default to always handle UTF8 characters. 2022年11月28日 23:45:56 +01:00
.coveralls.yml Fork of elixir-nodejs at https://github.com/revelrylabs/elixir-nodejs at commit b5c96047c54b70873fc1efa4f716233c569d23c8 2022年11月21日 15:23:05 +01:00
.formatter.exs Fork of elixir-nodejs at https://github.com/revelrylabs/elixir-nodejs at commit b5c96047c54b70873fc1efa4f716233c569d23c8 2022年11月21日 15:23:05 +01:00
.gitignore Fork of elixir-nodejs at https://github.com/revelrylabs/elixir-nodejs at commit b5c96047c54b70873fc1efa4f716233c569d23c8 2022年11月21日 15:23:05 +01:00
.tool-versions Fork of elixir-nodejs at https://github.com/revelrylabs/elixir-nodejs at commit b5c96047c54b70873fc1efa4f716233c569d23c8 2022年11月21日 15:23:05 +01:00
.travis.yml Add GNU Affero General Public License version 3 2022年11月21日 17:48:58 +01:00
CHANGELOG.md Fork of elixir-nodejs at https://github.com/revelrylabs/elixir-nodejs at commit b5c96047c54b70873fc1efa4f716233c569d23c8 2022年11月21日 15:23:05 +01:00
LICENSE Add GNU Affero General Public License version 3 2022年11月21日 17:48:58 +01:00
mix.exs Update dependencies requirements 2022年11月22日 18:31:03 +01:00
mix.lock Update dependencies requirements 2022年11月22日 18:31:03 +01:00
Original license MIT.svg Update dependencies requirements 2022年11月22日 18:31:03 +01:00
README.md Fix readme link by changing branch name from "master" to "main". 2022年12月09日 18:46:32 +01:00
This fork license AGPLv3.svg Update dependencies requirements 2022年11月22日 18:31:03 +01:00

Nodelixir

This project is a fork of elixir-nodejs

Original code is licensed under: MIT This fork's code is licensed under: AGPLv3

Provides an Elixir API for calling Node.js functions.

Prerequisites

  • Elixir >= 1.14
  • NodeJS >= 18.11

Installation

def deps do
 [
 {:nodejs, "~> 2.0"}
 ]
end

Starting the service

Add NodeJS to your Supervisor as a child, pointing the required path option at the directory containing your JavaScript modules.

supervisor(NodeJS, [[path: "/node_app_root", pool_size: 4]])

Calling JavaScript module functions with NodeJS.call(module, args \\ []).

If the module exports a function directly, like this:

module.exports = (x) => x

You can call it like this:

NodeJS.call("echo", ["hello"]) #=> {:ok, "hello"}

There is also a call! form that throws on error instead of returning a tuple:

NodeJS.call!("echo", ["hello"]) #=> "hello"

If the module exports an object with named functions like:

exports.add = (a, b) => a + b
exports.sub = (a, b) => a - b

You can call them like this:

NodeJS.call({"math", :add}, [1, 2]) # => {:ok, 3}
NodeJS.call({"math", :sub}, [1, 2]) # => {:ok, -1}

There Are Rules & Limitations (Unfortunately)

  • Function arguments must be serializable to JSON.
  • Return values must be serializable to JSON. (Objects with circular references will definitely fail.)
  • Modules must be requested relative to the path that was given to the Supervisor. E.g., for a path of /node_app_root and a file /node_app_root/foo/index.js your module request should be for "foo/index.js" or "foo/index" or "foo".

Running the tests

Since the test suite requires npm dependencies before you can run the tests you will first need to run

cd test/js && npm install && cd ../..

After that you should be able to run

mix test

Handling Callbacks and Promises

You can see examples of using promises in the tests here:

https://codeberg.org/Etaweb/nodelixir/src/branch/main/test/nodejs_test.exs#L125

and from the JavaScript code here:

module.exports = async function echo(x, delay = 1000) {
 return new Promise((resolve) => setTimeout(() => resolve(x), delay))
}

https://codeberg.org/Etaweb/nodelixir/src/branch/main/test/js/slow-async-echo.js